forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftware-device.cpp
399 lines (342 loc) · 15.2 KB
/
software-device.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2018 Intel Corporation. All Rights Reserved.
#include "software-device.h"
#include "stream.h"
namespace librealsense
{
software_device::software_device()
: device(std::make_shared<context>(backend_type::standard), {}),
_user_destruction_callback()
{
register_info(RS2_CAMERA_INFO_NAME, "Software-Device");
}
librealsense::software_device::~software_device()
{
if (_user_destruction_callback)
_user_destruction_callback->on_destruction();
}
software_sensor& software_device::add_software_sensor(const std::string& name)
{
auto sensor = std::make_shared<software_sensor>(name, this);
add_sensor(sensor);
_software_sensors.push_back(sensor);
return *sensor;
}
void software_device::register_extrinsic(const stream_interface& stream)
{
uint32_t max_idx = 0;
std::set<uint32_t> bad_groups;
for (auto & pair : _extrinsics) {
if (pair.second.first > max_idx) max_idx = pair.second.first;
if (bad_groups.count(pair.second.first)) continue; // already tried the group
rs2_extrinsics ext;
if (environment::get_instance().get_extrinsics_graph().try_fetch_extrinsics(stream, *pair.second.second, &ext)) {
register_stream_to_extrinsic_group(stream, pair.second.first);
return;
}
}
register_stream_to_extrinsic_group(stream, max_idx+1);
}
void software_device::register_destruction_callback(software_device_destruction_callback_ptr callback)
{
_user_destruction_callback = std::move(callback);
}
software_sensor& software_device::get_software_sensor(int index)
{
if (index >= _software_sensors.size())
{
throw rs2::error("Requested index is out of range!");
}
return *_software_sensors[index];
}
std::shared_ptr<software_device_info> software_device::get_info() {
if (!_info)
_info = std::make_shared<software_device_info>(std::dynamic_pointer_cast< software_device>(shared_from_this()));
return _info;
}
void software_device::set_matcher_type(rs2_matchers matcher)
{
_matcher = matcher;
}
software_sensor::software_sensor(std::string name, software_device* owner)
: sensor_base(name, owner, &_pbs),
_stereo_extension([this]() { return stereo_extension(this); })
{
_metadata_parsers = md_constant_parser::create_metadata_parser_map();
_unique_id = unique_id::generate_id();
}
std::shared_ptr<matcher> software_device::create_matcher(const frame_holder& frame) const
{
std::vector<stream_interface*> profiles;
for (auto&& s : _software_sensors)
for (auto&& p : s->get_stream_profiles())
profiles.push_back(p.get());
return matcher_factory::create(_matcher, profiles);
}
std::shared_ptr<stream_profile_interface> software_sensor::add_video_stream(rs2_video_stream video_stream, bool is_default)
{
auto currProfile = find_profile_by_uid(video_stream.uid);
if (currProfile)
{
//LOG_WARNING("Video stream unique ID already exist!");
//throw rs2::error("Stream unique ID already exist!");
}
auto profile = std::make_shared<video_stream_profile>(
platform::stream_profile{ (uint32_t)video_stream.width, (uint32_t)video_stream.height, (uint32_t)video_stream.fps, 0 });
profile->set_dims(video_stream.width, video_stream.height);
profile->set_format(video_stream.fmt);
profile->set_framerate(video_stream.fps);
profile->set_stream_index(video_stream.index);
profile->set_stream_type(video_stream.type);
profile->set_unique_id(video_stream.uid);
profile->set_intrinsics([=]() {return video_stream.intrinsics; });
if (is_default) profile->tag_profile(profile_tag::PROFILE_TAG_DEFAULT);
_profiles.push_back(profile);
return profile;
}
std::shared_ptr<stream_profile_interface> software_sensor::add_motion_stream(rs2_motion_stream motion_stream, bool is_default)
{
auto currProfile = find_profile_by_uid(motion_stream.uid);
if (currProfile)
{
LOG_WARNING("Motion stream unique ID already exist!");
throw rs2::error("Stream unique ID already exist!");
}
auto profile = std::make_shared<motion_stream_profile>(
platform::stream_profile{ 0, 0, (uint32_t)motion_stream.fps, 0 });
profile->set_format(motion_stream.fmt);
profile->set_framerate(motion_stream.fps);
profile->set_stream_index(motion_stream.index);
profile->set_stream_type(motion_stream.type);
profile->set_unique_id(motion_stream.uid);
profile->set_intrinsics([=]() {return motion_stream.intrinsics; });
if (is_default) profile->tag_profile(profile_tag::PROFILE_TAG_DEFAULT);
_profiles.push_back(profile);
return std::move(profile);
}
std::shared_ptr<stream_profile_interface> software_sensor::add_pose_stream(rs2_pose_stream pose_stream, bool is_default)
{
auto currProfile = find_profile_by_uid(pose_stream.uid);
if (currProfile)
{
LOG_WARNING("Pose stream unique ID already exist!");
throw rs2::error("Stream unique ID already exist!");
}
auto profile = std::make_shared<pose_stream_profile>(
platform::stream_profile{ 0, 0, (uint32_t)pose_stream.fps, 0 });
profile->set_format(pose_stream.fmt);
profile->set_framerate(pose_stream.fps);
profile->set_stream_index(pose_stream.index);
profile->set_stream_type(pose_stream.type);
profile->set_unique_id(pose_stream.uid);
if (is_default) profile->tag_profile(profile_tag::PROFILE_TAG_DEFAULT);
_profiles.push_back(profile);
return std::move(profile);
}
std::shared_ptr<stream_profile_interface> software_sensor::find_profile_by_uid(int uid)
{
auto filtFunc = [&](std::shared_ptr<stream_profile_interface> profile)
{
return profile->get_unique_id() == uid;
};
auto profile = std::find_if(_profiles.begin(), _profiles.end(), filtFunc);
if ( profile != _profiles.end() ) {
return *profile;
} else {
return std::shared_ptr<stream_profile_interface>();
}
}
bool software_sensor::extend_to(rs2_extension extension_type, void ** ptr)
{
if (extension_type == RS2_EXTENSION_DEPTH_SENSOR)
{
if (supports_option(RS2_OPTION_DEPTH_UNITS))
{
*ptr = &(*_stereo_extension);
return true;
}
}
else if (extension_type == RS2_EXTENSION_DEPTH_STEREO_SENSOR)
{
if (supports_option(RS2_OPTION_DEPTH_UNITS) &&
supports_option(RS2_OPTION_STEREO_BASELINE))
{
*ptr = &(*_stereo_extension);
return true;
}
}
return false;
}
stream_profiles software_sensor::init_stream_profiles()
{
return _profiles;
}
void software_sensor::open(const stream_profiles& requests)
{
if (_is_streaming)
throw wrong_api_call_sequence_exception("open(...) failed. Software device is streaming!");
else if (_is_opened)
throw wrong_api_call_sequence_exception("open(...) failed. Software device is already opened!");
_is_opened = true;
set_active_streams(requests);
}
void software_sensor::close()
{
if (_is_streaming)
throw wrong_api_call_sequence_exception("close() failed. Software device is streaming!");
else if (!_is_opened)
throw wrong_api_call_sequence_exception("close() failed. Software device was not opened!");
_is_opened = false;
set_active_streams({});
}
void software_sensor::start(frame_callback_ptr callback)
{
if (_is_streaming)
throw wrong_api_call_sequence_exception("start_streaming(...) failed. Software device is already streaming!");
else if (!_is_opened)
throw wrong_api_call_sequence_exception("start_streaming(...) failed. Software device was not opened!");
_source.get_published_size_option()->set(0);
_source.init(_metadata_parsers);
_source.set_sensor(this->shared_from_this());
_source.set_callback(callback);
_is_streaming = true;
raise_on_before_streaming_changes(true);
}
void software_sensor::stop()
{
if (!_is_streaming)
throw wrong_api_call_sequence_exception("stop_streaming() failed. Software device is not streaming!");
_is_streaming = false;
raise_on_before_streaming_changes(false);
_source.flush();
_source.reset();
}
void software_sensor::set_metadata(rs2_frame_metadata_value key, rs2_metadata_type value)
{
_metadata_map[key] = value;
}
void software_sensor::on_video_frame(rs2_software_video_frame software_frame)
{
if (!_is_streaming) {
software_frame.deleter(software_frame.pixels);
return;
}
frame_additional_data data;
data.timestamp = software_frame.timestamp;
data.timestamp_domain = software_frame.domain;
data.frame_number = software_frame.frame_number;
data.metadata_size = 0;
for (auto i : _metadata_map)
{
auto size_of_enum = sizeof(rs2_frame_metadata_value);
auto size_of_data = sizeof(rs2_metadata_type);
if (data.metadata_size + size_of_enum + size_of_data > 255)
{
continue; //stop adding metadata to frame
}
memcpy(data.metadata_blob.data() + data.metadata_size, &i.first, size_of_enum);
data.metadata_size += static_cast<uint32_t>(size_of_enum);
memcpy(data.metadata_blob.data() + data.metadata_size, &i.second, size_of_data);
data.metadata_size += static_cast<uint32_t>(size_of_data);
}
rs2_extension extension = software_frame.profile->profile->get_stream_type() == RS2_STREAM_DEPTH ?
RS2_EXTENSION_DEPTH_FRAME : RS2_EXTENSION_VIDEO_FRAME;
auto frame = _source.alloc_frame(extension, 0, data, false);
if (!frame)
{
LOG_WARNING("Dropped video frame. alloc_frame(...) returned nullptr");
return;
}
auto vid_profile = dynamic_cast<video_stream_profile_interface*>(software_frame.profile->profile);
auto vid_frame = dynamic_cast<video_frame*>(frame);
vid_frame->assign(vid_profile->get_width(), vid_profile->get_height(), software_frame.stride, software_frame.bpp * 8);
frame->set_stream(std::dynamic_pointer_cast<stream_profile_interface>(software_frame.profile->profile->shared_from_this()));
frame->attach_continuation(frame_continuation{ [=]() {
software_frame.deleter(software_frame.pixels);
}, software_frame.pixels });
auto sd = dynamic_cast<software_device*>(_owner);
sd->register_extrinsic(*vid_profile);
_source.invoke_callback(frame);
}
void software_sensor::on_motion_frame(rs2_software_motion_frame software_frame)
{
if (!_is_streaming) return;
frame_additional_data data;
data.timestamp = software_frame.timestamp;
data.timestamp_domain = software_frame.domain;
data.frame_number = software_frame.frame_number;
data.metadata_size = 0;
for (auto i : _metadata_map)
{
auto size_of_enum = sizeof(rs2_frame_metadata_value);
auto size_of_data = sizeof(rs2_metadata_type);
memcpy(data.metadata_blob.data() + data.metadata_size, &i.first, size_of_enum);
data.metadata_size += static_cast<uint32_t>(size_of_enum);
memcpy(data.metadata_blob.data() + data.metadata_size, &i.second, size_of_data);
data.metadata_size += static_cast<uint32_t>(size_of_data);
}
auto frame = _source.alloc_frame(RS2_EXTENSION_MOTION_FRAME, 0, data, false);
if (!frame)
{
LOG_WARNING("Dropped motion frame. alloc_frame(...) returned nullptr");
return;
}
frame->set_stream(std::dynamic_pointer_cast<stream_profile_interface>(software_frame.profile->profile->shared_from_this()));
frame->attach_continuation(frame_continuation{ [=]() {
software_frame.deleter(software_frame.data);
}, software_frame.data });
_source.invoke_callback(frame);
}
void software_sensor::on_pose_frame(rs2_software_pose_frame software_frame)
{
if (!_is_streaming) return;
frame_additional_data data;
data.timestamp = software_frame.timestamp;
data.timestamp_domain = software_frame.domain;
data.frame_number = software_frame.frame_number;
data.metadata_size = 0;
for (auto i : _metadata_map)
{
auto size_of_enum = sizeof(rs2_frame_metadata_value);
auto size_of_data = sizeof(rs2_metadata_type);
memcpy(data.metadata_blob.data() + data.metadata_size, &i.first, size_of_enum);
data.metadata_size += static_cast<uint32_t>(size_of_enum);
memcpy(data.metadata_blob.data() + data.metadata_size, &i.second, size_of_data);
data.metadata_size += static_cast<uint32_t>(size_of_data);
}
auto frame = _source.alloc_frame(RS2_EXTENSION_POSE_FRAME, 0, data, false);
if (!frame)
{
LOG_WARNING("Dropped pose frame. alloc_frame(...) returned nullptr");
return;
}
frame->set_stream(std::dynamic_pointer_cast<stream_profile_interface>(software_frame.profile->profile->shared_from_this()));
frame->attach_continuation(frame_continuation{ [=]() {
software_frame.deleter(software_frame.data);
}, software_frame.data });
_source.invoke_callback(frame);
}
void software_sensor::on_notification(rs2_software_notification notif)
{
notification n{ notif.category, notif.type, notif.severity, notif.description };
n.serialized_data = notif.serialized_data;
_notifications_processor->raise_notification(n);
}
void software_sensor::add_read_only_option(rs2_option option, float val)
{
register_option(option, std::make_shared<const_value_option>("bypass sensor read only option",
lazy<float>([=]() { return val; })));
}
void software_sensor::update_read_only_option(rs2_option option, float val)
{
if (auto opt = dynamic_cast<readonly_float_option*>(&get_option(option)))
opt->update(val);
else
throw invalid_value_exception(to_string() << "option " << get_string(option) << " is not read-only or is deprecated type");
}
void software_sensor::add_option(rs2_option option, option_range range, bool is_writable)
{
register_option(option, (is_writable? std::make_shared<float_option>(range) :
std::make_shared<readonly_float_option>(range)));
}
}