forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs_context.hpp
256 lines (214 loc) · 7.27 KB
/
rs_context.hpp
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
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
#ifndef LIBREALSENSE_RS2_CONTEXT_HPP
#define LIBREALSENSE_RS2_CONTEXT_HPP
#include "rs_types.hpp"
#include "rs_record_playback.hpp"
#include "rs_processing.hpp"
namespace rs2
{
class event_information
{
public:
event_information(device_list removed, device_list added)
:_removed(removed), _added(added) {}
/**
* check if specific device was disconnected
* \return true if device disconnected, false if device connected
*/
bool was_removed(const rs2::device& dev) const
{
rs2_error* e = nullptr;
if (!dev)
return false;
auto res = rs2_device_list_contains(_removed.get_list(), dev.get().get(), &e);
error::handle(e);
return res > 0;
}
/**
* check if specific device was added
* \return true if device added, false otherwise
*/
bool was_added(const rs2::device& dev) const
{
rs2_error* e = nullptr;
if (!dev)
return false;
auto res = rs2_device_list_contains(_added.get_list(), dev.get().get(), &e);
error::handle(e);
return res > 0;
}
/**
* returns a list of all newly connected devices
* \return the list of all new connected devices
*/
device_list get_new_devices() const
{
return _added;
}
/**
* returns a list of all newly removed devices
* \return the list of all newly removed devices
*/
device_list get_removed_devices() const
{
return _removed;
}
private:
device_list _removed;
device_list _added;
};
template<class T>
class devices_changed_callback : public rs2_devices_changed_callback
{
T _callback;
public:
explicit devices_changed_callback(T callback) : _callback(callback) {}
void on_devices_changed(rs2_device_list* removed, rs2_device_list* added) override
{
std::shared_ptr<rs2_device_list> old(removed, rs2_delete_device_list);
std::shared_ptr<rs2_device_list> news(added, rs2_delete_device_list);
event_information info({ device_list(old), device_list(news) });
_callback(info);
}
void release() override { delete this; }
};
class pipeline;
class device_hub;
/**
* default librealsense context class
* includes realsense API version as provided by RS2_API_VERSION macro
*/
class context
{
public:
context()
{
rs2_error* e = nullptr;
_context = std::shared_ptr<rs2_context>(
rs2_create_context(RS2_API_VERSION, &e),
rs2_delete_context);
error::handle(e);
}
/**
* create a static snapshot of all connected devices at the time of the call
* \return the list of devices connected devices at the time of the call
*/
device_list query_devices() const
{
rs2_error* e = nullptr;
std::shared_ptr<rs2_device_list> list(
rs2_query_devices(_context.get(), &e),
rs2_delete_device_list);
error::handle(e);
return device_list(list);
}
/**
* @brief Generate a flat list of all available sensors from all RealSense devices
* @return List of sensors
*/
std::vector<sensor> query_all_sensors() const
{
std::vector<sensor> results;
for (auto&& dev : query_devices())
{
auto sensors = dev.query_sensors();
std::copy(sensors.begin(), sensors.end(), std::back_inserter(results));
}
return results;
}
device get_sensor_parent(const sensor& s) const
{
rs2_error* e = nullptr;
std::shared_ptr<rs2_device> dev(
rs2_create_device_from_sensor(s._sensor.get(), &e),
rs2_delete_device);
error::handle(e);
return device{ dev };
}
/**
* register devices changed callback
* \param[in] callback devices changed callback
*/
template<class T>
void set_devices_changed_callback(T callback)
{
rs2_error* e = nullptr;
rs2_set_devices_changed_callback_cpp(_context.get(),
new devices_changed_callback<T>(std::move(callback)), &e);
error::handle(e);
}
/**
* Creates a device from a RealSense file
*
* On successful load, the device will be appended to the context and a devices_changed event triggered
* @param file Path to a RealSense File
* @return A playback device matching the given file
*/
playback load_device(const std::string& file)
{
rs2_error* e = nullptr;
auto device = std::shared_ptr<rs2_device>(
rs2_context_add_device(_context.get(), file.c_str(), &e),
rs2_delete_device);
rs2::error::handle(e);
return playback { device };
}
void unload_device(const std::string& file)
{
rs2_error* e = nullptr;
rs2_context_remove_device(_context.get(), file.c_str(), &e);
rs2::error::handle(e);
}
protected:
friend class rs2::pipeline;
friend class rs2::device_hub;
context(std::shared_ptr<rs2_context> ctx)
: _context(ctx)
{}
std::shared_ptr<rs2_context> _context;
};
/**
* device_hub class - encapsulate the handling of connect and disconnect events
*/
class device_hub
{
public:
explicit device_hub(context ctx)
: _ctx(std::move(ctx))
{
rs2_error* e = nullptr;
_device_hub = std::shared_ptr<rs2_device_hub>(
rs2_create_device_hub(_ctx._context.get(), &e),
rs2_delete_device_hub);
error::handle(e);
}
/**
* If any device is connected return it, otherwise wait until next RealSense device connects.
* Calling this method multiple times will cycle through connected devices
*/
device wait_for_device() const
{
rs2_error* e = nullptr;
std::shared_ptr<rs2_device> dev(
rs2_device_hub_wait_for_device(_ctx._context.get(), _device_hub.get(), &e),
rs2_delete_device);
error::handle(e);
return device(dev);
}
/**
* Checks if device is still connected
*/
bool is_connected(const device& dev) const
{
rs2_error* e = nullptr;
auto res = rs2_device_hub_is_device_connected(_device_hub.get(), dev._dev.get(), &e);
error::handle(e);
return res > 0 ? true : false;
}
private:
context _ctx;
std::shared_ptr<rs2_device_hub> _device_hub;
};
}
#endif // LIBREALSENSE_RS2_CONTEXT_HPP