forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_hub.cpp
148 lines (124 loc) · 4.91 KB
/
device_hub.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
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
#include <librealsense2/rs.hpp>
#include "source.h"
#include "core/processing.h"
#include "proc/synthetic-stream.h"
#include "device_hub.h"
namespace librealsense
{
typedef rs2::devices_changed_callback<std::function<void(rs2::event_information& info)>> hub_devices_changed_callback;
std::vector<std::shared_ptr<device_info>> filter_by_vid(std::vector<std::shared_ptr<device_info>> devices , int vid)
{
std::vector<std::shared_ptr<device_info>> result;
for (auto dev : devices)
{
bool filtered = false;
auto data = dev->get_device_data();
for (const auto& uvc : data.uvc_devices)
{
if (uvc.vid == vid || vid == 0)
{
result.push_back(dev);
filtered = true;
break;
}
}
}
return result;
}
device_hub::device_hub(std::shared_ptr<librealsense::context> ctx, int mask, int vid,
bool register_device_notifications)
: _ctx(ctx), _vid(vid),
_device_changes_callback_id(0),
_register_device_notifications(register_device_notifications)
{
_device_list = filter_by_vid(_ctx->query_devices(mask), _vid);
auto cb = new hub_devices_changed_callback([&,mask](rs2::event_information&)
{
std::unique_lock<std::mutex> lock(_mutex);
_device_list = filter_by_vid(_ctx->query_devices(mask), _vid);
// Current device will point to the first available device
_camera_index = 0;
if (_device_list.size() > 0)
{
_cv.notify_all();
}
});
_device_changes_callback_id = _ctx->register_internal_device_callback({ cb, [](rs2_devices_changed_callback* p) { p->release(); } });
}
device_hub::~device_hub()
{
if (_device_changes_callback_id)
_ctx->unregister_internal_device_callback(_device_changes_callback_id);
_ctx->stop();
}
std::shared_ptr<device_interface> device_hub::create_device(const std::string& serial, bool cycle_devices)
{
std::shared_ptr<device_interface> res = nullptr;
for(auto i = 0; ((i< _device_list.size()) && (nullptr == res)); i++)
{
// _camera_index is the curr device that the hub will expose
auto d = _device_list[ (_camera_index + i) % _device_list.size()];
auto dev = d->create_device(_register_device_notifications);
if(serial.size() > 0 )
{
auto new_serial = dev->get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
if(serial == new_serial)
{
res = dev;
cycle_devices = false; // Requesting a device by its serial shall not invoke internal cycling
}
}
else // Use the first selected if "any device" pattern was used
{
res = dev;
}
}
// Advance the internal selection when appropriate
if (res && cycle_devices)
_camera_index = (1+_camera_index) % _device_list.size();
return res;
}
/**
* If any device is connected return it, otherwise wait until next RealSense device connects.
* Calling this method multiple times will cycle through connected devices
*/
std::shared_ptr<device_interface> device_hub::wait_for_device(const std::chrono::milliseconds& timeout, bool loop_through_devices, const std::string& serial)
{
std::unique_lock<std::mutex> lock(_mutex);
std::shared_ptr<device_interface> res = nullptr;
// check if there is at least one device connected
_device_list = filter_by_vid(_ctx->query_devices(RS2_PRODUCT_LINE_ANY), _vid);
if (_device_list.size() > 0)
{
res = create_device(serial, loop_through_devices);
}
if (res) return res;
// block for the requested device to be connected, or till the timeout occurs
if (!_cv.wait_for(lock, timeout, [&]()
{
if (_device_list.size() > 0)
{
res = create_device(serial, loop_through_devices);
}
return res != nullptr;
}))
{
throw std::runtime_error("No device connected");
}
return res;
}
/**
* Checks if device is still connected
*/
bool device_hub::is_connected(const device_interface& dev)
{
std::unique_lock<std::mutex> lock(_mutex);
return dev.is_valid();
}
std::shared_ptr<librealsense::context> device_hub::get_context()
{
return _ctx;
}
}