9 #include <condition_variable> 48 return std::make_pair(stream, index) <
65 template<
class Stream_Profile>
66 static bool match(
const Stream_Profile& a,
const Stream_Profile& b)
70 if (a.stream_index() != -1 && b.stream_index() != -1 && (a.stream_index() != b.stream_index()))
74 if (a.fps() != 0 && b.fps() != 0 && (a.fps() != b.fps()))
78 if (
auto vid_a = dynamic_cast<video_stream_profile_interface*>(a))
80 if (
auto vid_b = dynamic_cast<video_stream_profile_interface*>(b))
82 if (vid_a->get_width() != 0 && vid_b->get_width() != 0 && (vid_a->get_width() != vid_b->get_width()))
84 if (vid_a->get_height() != 0 && vid_b->get_height() != 0 && (vid_a->get_height() != vid_b->get_height()))
104 if (
auto vid_a = dynamic_cast<video_stream_profile_interface*>(a))
106 if (vid_a->get_width() != 0 && b.
width != 0 && (vid_a->get_width() != b.
width))
108 if (vid_a->get_height() != 0 && b.
height != 0 && (vid_a->get_height() != b.
height))
118 for (
auto request : others)
124 if (
auto vid_a = dynamic_cast<video_stream_profile_interface*>(a))
126 for (
auto request : others)
128 if (
auto vid_b = dynamic_cast<video_stream_profile_interface*>(request.get()))
130 if (vid_a->get_width() != 0 && vid_b->get_width() != 0 && (vid_a->get_width() != vid_b->get_width()))
132 if (vid_a->get_framerate() != 0 && vid_b->get_framerate() != 0 && (vid_a->get_framerate() != vid_b->get_framerate()))
143 for (
auto request : others)
149 if (
auto vid_a = dynamic_cast<video_stream_profile_interface*>(a))
151 for (
auto request : others)
154 if ((vid_a->get_height() == vid_a->get_width()) && (request.height == request.width))
156 if (vid_a->get_width() != 0 && request.width != 0 && (vid_a->get_width() != request.width))
158 if (vid_a->get_height() != 0 && request.height != 0 && (vid_a->get_height() != request.height))
170 if (
auto vid_a = dynamic_cast<video_stream_profile_interface*>(a))
172 if (vid_a->get_width() == 0 || vid_a->get_height() == 0)
return true;
200 std::map<
index_type, std::shared_ptr<stream_profile_interface>> profiles,
201 std::map<int, stream_profiles> dev_to_profiles)
202 : _profiles(
std::move(profiles)),
203 _dev_to_profiles(
std::move(dev_to_profiles)),
204 _results(
std::move(results))
210 for (
auto && kvp : _dev_to_profiles) {
211 auto&& sub = _results.at(kvp.first);
212 sub->open(kvp.second);
219 for (
auto&& sensor : _results)
220 sensor.second->start(callback);
225 for (
auto&& sensor : _results)
226 sensor.second->stop();
231 for (
auto&& sensor : _results)
232 sensor.second->close();
234 std::map<index_type, std::shared_ptr<stream_profile_interface>>
get_profiles()
const 241 return _dev_to_profiles;
246 std::map<index_type, std::shared_ptr<stream_profile_interface>> _profiles;
247 std::map<index_type, sensor_interface*> _devices;
248 std::map<int, sensor_interface*> _results;
249 std::map<int, stream_profiles> _dev_to_profiles;
257 _presets.erase({
stream, index });
269 auto itr = _requests.begin();
270 while( itr != _requests.end())
272 if(itr->first.stream == stream)
274 _requests.erase(itr++);
285 _requests.erase({
stream, -1 });
286 _presets[{
stream, -1 }] = preset;
292 _requests.erase({
stream, index});
293 _presets[{
stream, index}] = preset;
314 for (
auto&& sensor : stream._results)
315 sensor.second->close();
332 for (
auto && kvp : c.map_streams(dev))
334 if (kvp.second->get_stream_type() == stream && kvp.second->get_stream_index() == index)
337 auto it = _requests.erase({
stream, index});
343 auto mapping = map_streams(dev);
349 std::set<index_type> all_streams;
350 for (
auto && kvp : mapping)
351 all_streams.insert({ kvp.second->get_stream_type(), kvp.second->get_stream_index() });
354 for (
auto && kvp : _presets)
361 throw std::runtime_error(
"Config couldn't configure all streams");
363 for (
auto && kvp : _requests)
370 throw std::runtime_error(
"Config couldn't configure all streams");
375 std::map<int, stream_profiles> dev_to_profiles;
376 std::map<index_type, sensor_interface*> stream_to_dev;
377 std::map<index_type, std::shared_ptr<stream_profile_interface>> stream_to_profile;
379 std::map<int, sensor_interface*> sensors_map;
382 if (mapping.find(i) != mapping.end())
388 for (
auto && kvp : mapping) {
389 dev_to_profiles[kvp.first].push_back(kvp.second);
390 index_type idx{ kvp.second->get_stream_type(), kvp.second->get_stream_index() };
391 stream_to_dev.emplace(idx, sensors_map.at(kvp.first));
392 stream_to_profile[idx] = kvp.second;
396 return multistream(std::move(sensors_map), std::move(stream_to_profile), std::move(dev_to_profiles));
400 static bool sort_highest_framerate(
const std::shared_ptr<stream_profile_interface> lhs,
const std::shared_ptr<stream_profile_interface> rhs) {
401 return lhs->get_framerate() < rhs->get_framerate();
404 static bool sort_largest_image(std::shared_ptr<stream_profile_interface> lhs, std::shared_ptr<stream_profile_interface> rhs) {
405 if (
auto a = dynamic_cast<video_stream_profile_interface*>(lhs.get()))
406 if (
auto b = dynamic_cast<video_stream_profile_interface*>(rhs.get()))
407 return a->get_width()*a->get_height() < b->get_width()*b->get_height();
408 return sort_highest_framerate(lhs, rhs);
411 static bool sort_best_quality( std::shared_ptr<stream_profile_interface> lhs, std::shared_ptr<stream_profile_interface> rhs) {
412 if (
auto a = dynamic_cast<video_stream_profile_interface*>(lhs.get()))
414 if (
auto b = dynamic_cast<video_stream_profile_interface*>(rhs.get()))
416 return std::make_tuple((a->get_height() == 640 && a->get_height() == 480), (lhs->get_framerate() == 30), (lhs->get_format() ==
RS2_FORMAT_Z16), (lhs->get_format() ==
RS2_FORMAT_Y8), (lhs->get_format() ==
RS2_FORMAT_RGB8),
int(lhs->get_format()))
417 < std::make_tuple((b->get_width() == 640 && b->get_height() == 480), (rhs->get_framerate() == 30), (rhs->get_format() ==
RS2_FORMAT_Z16), (rhs->get_format() ==
RS2_FORMAT_Y8), (rhs->get_format() ==
RS2_FORMAT_RGB8),
int(rhs->get_format()));
421 return sort_highest_framerate(lhs, rhs);
424 static void auto_complete(std::vector<request_type> &requests,
sensor_interface &target)
427 for (
auto & request : requests)
430 for (
auto candidate : candidates)
432 if (
match(candidate.get(), request) && !
contradicts(candidate.get(), requests))
434 request = to_request(candidate.get());
450 if (
auto vid = dynamic_cast<video_stream_profile_interface*>(profile))
452 r.
width = vid->get_width();
453 r.
height = vid->get_height();
463 std::multimap<int, std::shared_ptr<stream_profile_interface>> map_streams(
device_interface* dev)
const 465 std::multimap<int, std::shared_ptr<stream_profile_interface>> out;
466 std::set<index_type> satisfied_streams;
473 std::vector<request_type> targets;
477 for (
auto && kvp : _requests)
479 if (satisfied_streams.count(kvp.first))
continue;
482 auto it = std::find_if(
begin(profiles),
end(profiles),
483 [&kvp](
const std::shared_ptr<stream_profile_interface>& profile)
485 return match(profile.get(), kvp.second);
487 if (it !=
end(profiles))
489 targets.push_back(kvp.second);
490 satisfied_streams.insert(kvp.first);
496 for (
auto && kvp : _presets)
498 if (satisfied_streams.count(kvp.first))
continue;
505 std::sort(
begin(profiles),
end(profiles), sort_best_quality);
508 std::sort(
begin(profiles),
end(profiles), sort_largest_image);
511 std::sort(
begin(profiles),
end(profiles), sort_highest_framerate);
513 default:
throw std::runtime_error(
"Unknown preset selected");
516 for (
auto itr: profiles)
518 auto stream =
index_type{ itr->get_stream_type() ,itr->get_stream_index() };
521 return to_request(itr.get());
531 targets.push_back(result);
532 satisfied_streams.insert({ result.stream, result.stream_index });
536 if (targets.size() > 0)
538 auto_complete(targets, sub);
540 for (
auto && t : targets)
542 for (
auto && p : profiles)
544 if (
match(p.get(), t))
546 out.emplace((
int)i, p);
559 std::map<index_type, request_type> _requests;
560 std::map<index_type, config_preset> _presets;
virtual rs2_format get_format() const =0
static bool match(stream_profile_interface *a, const request_type &b)
Definition: config.h:93
virtual rs2_stream get_stream_type() const =0
void close(multistream stream)
Definition: config.h:312
int height
Definition: config.h:36
void stop()
Definition: config.h:223
std::map< index_type, request_type > get_requests()
Definition: config.h:184
void disable_stream(rs2_stream stream)
Definition: config.h:262
config_preset
Definition: config.h:22
virtual size_t get_sensors_count() const =0
void close()
Definition: config.h:229
int index
Definition: config.h:44
void start(T callback)
Definition: config.h:217
multistream(std::map< int, sensor_interface *> results, std::map< index_type, std::shared_ptr< stream_profile_interface >> profiles, std::map< int, stream_profiles > dev_to_profiles)
Definition: config.h:199
Definition: rs_sensor.h:58
sql::statement::iterator end(sql::statement &stmt)
Definition: rs_sensor.h:23
Definition: streaming.h:131
void disable_all()
Definition: config.h:306
static bool has_wildcards(const request_type &a)
Definition: config.h:177
static bool has_wildcards(stream_profile_interface *a)
Definition: config.h:167
Definition: rs_sensor.h:62
void open()
Definition: config.h:208
static bool match(const Stream_Profile &a, const Stream_Profile &b)
Definition: config.h:66
rs2_stream stream
Definition: config.h:43
config()
Definition: config.h:252
Definition: streaming.h:46
sql::statement::iterator begin(sql::statement &stmt)
virtual sensor_interface & get_sensor(size_t i)=0
Definition: rs_sensor.h:50
virtual stream_profiles get_stream_profiles() const =0
multistream resolve(device_interface *dev)
Definition: config.h:341
static bool contradicts(stream_profile_interface *a, stream_profiles others)
Definition: config.h:116
Definition: rs_sensor.h:57
rs2_format
Format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:55
virtual const std::string & get_info(rs2_camera_info info) const =0
std::vector< std::shared_ptr< stream_profile_interface >> stream_profiles
Definition: streaming.h:104
Definition: rs_sensor.h:41
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:38
std::map< int, stream_profiles > get_profiles_per_sensor() const
Definition: config.h:239
int stream_index
Definition: config.h:35
rs2_stream stream
Definition: config.h:34
static bool contradicts(stream_profile_interface *a, const std::vector< request_type > &others)
Definition: config.h:141
multistream()
Definition: config.h:197
bool can_enable_stream(device_interface *dev, rs2_stream stream, int index, int width, int height, rs2_format format, int fps)
Definition: config.h:328
int width
Definition: config.h:36
Definition: rs_sensor.h:40
int fps
Definition: config.h:38
static bool match_stream(const index_type &a, const index_type &b)
Definition: config.h:55
virtual int get_stream_index() const =0
std::map< index_type, std::shared_ptr< stream_profile_interface > > get_profiles() const
Definition: config.h:234
void enable_stream(rs2_stream stream, int index, config_preset preset)
Definition: config.h:290
Definition: streaming.h:106
virtual uint32_t get_framerate() const =0
std::map< index_type, config_preset > get_presets()
Definition: config.h:189
bool operator<(const index_type &other) const
Definition: config.h:46
void enable_stream(rs2_stream stream, config_preset preset)
Definition: config.h:283
void enable_all(config_preset p)
Definition: config.h:297
rs2_format format
Definition: config.h:37
void enable_stream(rs2_stream stream, int index, int width, int height, rs2_format format, int fps)
Definition: config.h:255
Definition: rs_sensor.h:66