forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cpp
141 lines (121 loc) · 4.52 KB
/
source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
#include "source.h"
#include "option.h"
#include "environment.h"
namespace librealsense
{
class frame_queue_size : public option_base
{
public:
frame_queue_size(std::atomic<uint32_t>* ptr, const option_range& opt_range)
: option_base(opt_range),
_ptr(ptr)
{}
void set(float value) override
{
if (!is_valid(value))
throw invalid_value_exception(to_string() << "set(frame_queue_size) failed! Given value " << value << " is out of range.");
*_ptr = static_cast<uint32_t>(value);
_recording_function(*this);
}
float query() const override { return static_cast<float>(_ptr->load()); }
bool is_enabled() const override { return true; }
const char* get_description() const override
{
return "Max number of frames you can hold at a given time. Increasing this number will reduce frame drops but increase latency, and vice versa";
}
private:
std::atomic<uint32_t>* _ptr;
};
std::shared_ptr<option> frame_source::get_published_size_option()
{
return std::make_shared<frame_queue_size>(&_max_publish_list_size, option_range{ 0, 32, 1, 16 });
}
frame_source::frame_source(uint32_t max_publish_list_size)
: _callback(nullptr, [](rs2_frame_callback*) {}),
_max_publish_list_size(max_publish_list_size),
_ts(environment::get_instance().get_time_service())
{}
void frame_source::init(std::shared_ptr<metadata_parser_map> metadata_parsers)
{
std::lock_guard<std::mutex> lock(_callback_mutex);
std::vector<rs2_extension> supported { RS2_EXTENSION_VIDEO_FRAME,
RS2_EXTENSION_COMPOSITE_FRAME,
RS2_EXTENSION_POINTS,
RS2_EXTENSION_DEPTH_FRAME,
RS2_EXTENSION_DISPARITY_FRAME,
RS2_EXTENSION_MOTION_FRAME,
RS2_EXTENSION_POSE_FRAME };
for (auto type : supported)
{
_archive[type] = make_archive(type, &_max_publish_list_size, _ts, metadata_parsers);
}
}
callback_invocation_holder frame_source::begin_callback()
{
return _archive[RS2_EXTENSION_VIDEO_FRAME]->begin_callback();
// return _archive[RS2_EXTENSION_DEPTH_FRAME]->begin_callback();
}
void frame_source::reset()
{
std::lock_guard<std::mutex> lock(_callback_mutex);
_callback.reset();
for (auto&& kvp : _archive)
{
kvp.second.reset();
}
}
frame_interface* frame_source::alloc_frame(rs2_extension type, size_t size, frame_additional_data additional_data, bool requires_memory) const
{
auto it = _archive.find(type);
if (it == _archive.end()) throw wrong_api_call_sequence_exception("Requested frame type is not supported!");
return it->second->alloc_and_track(size, additional_data, requires_memory);
}
void frame_source::set_sensor(std::shared_ptr<sensor_interface> s)
{
for (auto&& a : _archive)
{
a.second->set_sensor(s);
}
}
void frame_source::set_callback(frame_callback_ptr callback)
{
std::lock_guard<std::mutex> lock(_callback_mutex);
_callback = callback;
}
frame_callback_ptr frame_source::get_callback() const
{
std::lock_guard<std::mutex> lock(_callback_mutex);
return _callback;
}
void frame_source::invoke_callback(frame_holder frame) const
{
if (frame)
{
auto callback = frame.frame->get_owner()->begin_callback();
try
{
frame->log_callback_start(_ts ? _ts->get_time() : 0);
if (_callback)
{
frame_interface* ref = nullptr;
std::swap(frame.frame, ref);
_callback->on_frame((rs2_frame*)ref);
}
}
catch(...)
{
LOG_ERROR("Exception was thrown during user callback!");
}
}
}
void frame_source::flush() const
{
for (auto&& kvp : _archive)
{
if (kvp.second)
kvp.second->flush();
}
}
}