RealSense Cross Platform API
RealSense Cross-platform API
Loading...
Searching...
No Matches
rs_context.hpp
Go to the documentation of this file.
1// License: Apache 2.0. See LICENSE file in root directory.
2// Copyright(c) 2017 RealSense, Inc. All Rights Reserved.
3
4#ifndef LIBREALSENSE_RS2_CONTEXT_HPP
5#define LIBREALSENSE_RS2_CONTEXT_HPP
6
7#include "rs_types.hpp"
9#include "rs_processing.hpp"
10
11namespace rs2
12{
14 {
15 public:
17 :_removed(removed), _added(added) {}
18
23 bool was_removed(const rs2::device& dev) const
24 {
25 rs2_error* e = nullptr;
26
27 if (!dev)
28 return false;
29
30 auto res = rs2_device_list_contains(_removed.get_list(), dev.get().get(), &e);
32
33 return res > 0;
34 }
35
40 bool was_added(const rs2::device& dev) const
41 {
42 rs2_error* e = nullptr;
43
44 if (!dev)
45 return false;
46
47 auto res = rs2_device_list_contains(_added.get_list(), dev.get().get(), &e);
49
50 return res > 0;
51 }
52
58 {
59 return _added;
60 }
61
62 private:
63 device_list _removed;
64 device_list _added;
65 };
66
67 template<class T>
69 {
70 T _callback;
71
72 public:
73 explicit devices_changed_callback(T callback) : _callback(callback) {}
74
75 void on_devices_changed(rs2_device_list* removed, rs2_device_list* added) override
76 {
77 std::shared_ptr<rs2_device_list> old(removed, rs2_delete_device_list);
78 std::shared_ptr<rs2_device_list> news(added, rs2_delete_device_list);
79
80
81 event_information info({ device_list(old), device_list(news) });
82 _callback(info);
83 }
84
85 void release() override { delete this; }
86 };
87
88 class pipeline;
89 class device_hub;
90 class software_device;
91
96 class context
97 {
98 public:
101 {
102 }
103 context( char const * json_settings = nullptr )
104 {
105 rs2_error* e = nullptr;
106 _context = std::shared_ptr<rs2_context>(
107 rs2_create_context_ex( RS2_API_VERSION, json_settings, &e ),
109 error::handle(e);
110 }
111 context( std::string const & json_settings )
112 : context( json_settings.c_str() )
113 {
114 }
115
116 operator bool() const { return !! _context; }
117
123 {
124 rs2_error* e = nullptr;
125 std::shared_ptr<rs2_device_list> list(
126 rs2_query_devices(_context.get(), &e),
128 error::handle(e);
129
130 return device_list(list);
131 }
132
138 {
139 rs2_error* e = nullptr;
140 std::shared_ptr<rs2_device_list> list(
141 rs2_query_devices_ex(_context.get(), mask, &e),
143 error::handle(e);
144
145 return device_list(list);
146 }
147
152 std::vector<sensor> query_all_sensors() const
153 {
154 std::vector<sensor> results;
155 for (auto&& dev : query_devices())
156 {
157 auto sensors = dev.query_sensors();
158 std::copy(sensors.begin(), sensors.end(), std::back_inserter(results));
159 }
160 return results;
161 }
162
163
165 {
166 rs2_error* e = nullptr;
167 std::shared_ptr<rs2_device> dev(
170 error::handle(e);
171 return device{ dev };
172 }
173
178 template<class T>
180 {
181 rs2_error* e = nullptr;
183 new devices_changed_callback<T>(std::move(callback)), &e);
184 error::handle(e);
185 }
186
194 playback load_device(const std::string& file)
195 {
196 rs2_error* e = nullptr;
197 auto device = std::shared_ptr<rs2_device>(
198 rs2_context_add_device(_context.get(), file.c_str(), &e),
201
202 return playback { device };
203 }
204
205 void unload_device(const std::string& file)
206 {
207 rs2_error* e = nullptr;
208 rs2_context_remove_device(_context.get(), file.c_str(), &e);
210 }
211
213 {
214 rs2_error* e = nullptr;
217 }
218
219 void convert_bag_to_db3(const std::string& input, const std::string& output)
220 {
221 rs2_error* e = nullptr;
222 rs2_convert_bag_to_db3(input.c_str(), output.c_str(), _context.get(), nullptr, nullptr, &e);
224 }
225
226 template<class T>
227 void convert_bag_to_db3(const std::string& input, const std::string& output, T callback)
228 {
229 rs2_error* e = nullptr;
230 rs2_convert_bag_to_db3(input.c_str(), output.c_str(), _context.get(),
231 [](const float progress, void* user) { (*static_cast<T*>(user))(progress); },
232 &callback, &e);
234 }
235
236 context(std::shared_ptr<rs2_context> ctx)
237 : _context(ctx)
238 {}
239 explicit operator std::shared_ptr<rs2_context>() { return _context; };
240 protected:
241 friend class rs2::pipeline;
242 friend class rs2::device_hub;
244
245 std::shared_ptr<rs2_context> _context;
246 };
247
252 {
253 public:
254 explicit device_hub(context ctx)
255 {
256 rs2_error* e = nullptr;
257 _device_hub = std::shared_ptr<rs2_device_hub>(
258 rs2_create_device_hub(ctx._context.get(), &e),
260 error::handle(e);
261 }
262
268 {
269 rs2_error* e = nullptr;
270 std::shared_ptr<rs2_device> dev(
271 rs2_device_hub_wait_for_device(_device_hub.get(), &e),
273
274 error::handle(e);
275
276 return device(dev);
277
278 }
279
283 bool is_connected(const device& dev) const
284 {
285 rs2_error* e = nullptr;
286 auto res = rs2_device_hub_is_device_connected(_device_hub.get(), dev._dev.get(), &e);
287 error::handle(e);
288
289 return res > 0 ? true : false;
290
291 }
292
293 explicit operator std::shared_ptr<rs2_device_hub>() { return _device_hub; }
294 explicit device_hub(std::shared_ptr<rs2_device_hub> hub) : _device_hub(std::move(hub)) {}
295 private:
296 std::shared_ptr<rs2_device_hub> _device_hub;
297 };
298
299}
300#endif // LIBREALSENSE_RS2_CONTEXT_HPP
Definition rs_context.hpp:97
void set_devices_changed_callback(T callback)
Definition rs_context.hpp:179
context(std::string const &json_settings)
Definition rs_context.hpp:111
device_list query_devices() const
Definition rs_context.hpp:122
context(uninitialized_t)
Definition rs_context.hpp:100
context(char const *json_settings=nullptr)
Definition rs_context.hpp:103
uninitialized_t
Definition rs_context.hpp:99
@ uninitialized
Definition rs_context.hpp:99
void unload_device(const std::string &file)
Definition rs_context.hpp:205
device_list query_devices(int mask) const
Definition rs_context.hpp:137
void unload_tracking_module()
Definition rs_context.hpp:212
std::vector< sensor > query_all_sensors() const
Generate a flat list of all available sensors from all RealSense devices.
Definition rs_context.hpp:152
std::shared_ptr< rs2_context > _context
Definition rs_context.hpp:245
playback load_device(const std::string &file)
Definition rs_context.hpp:194
device get_sensor_parent(const sensor &s) const
Definition rs_context.hpp:164
void convert_bag_to_db3(const std::string &input, const std::string &output)
Definition rs_context.hpp:219
void convert_bag_to_db3(const std::string &input, const std::string &output, T callback)
Definition rs_context.hpp:227
context(std::shared_ptr< rs2_context > ctx)
Definition rs_context.hpp:236
Definition rs_context.hpp:252
device wait_for_device() const
Definition rs_context.hpp:267
bool is_connected(const device &dev) const
Definition rs_context.hpp:283
device_hub(std::shared_ptr< rs2_device_hub > hub)
Definition rs_context.hpp:294
device_hub(context ctx)
Definition rs_context.hpp:254
Definition rs_device.hpp:1058
Definition rs_device.hpp:20
const std::shared_ptr< rs2_device > & get() const
Definition rs_device.hpp:164
std::shared_ptr< rs2_device > _dev
Definition rs_device.hpp:209
Definition rs_context.hpp:69
void release() override
Definition rs_context.hpp:85
void on_devices_changed(rs2_device_list *removed, rs2_device_list *added) override
Definition rs_context.hpp:75
devices_changed_callback(T callback)
Definition rs_context.hpp:73
static void handle(rs2_error *e)
Definition rs_types.hpp:167
Definition rs_context.hpp:14
event_information(device_list removed, device_list added)
Definition rs_context.hpp:16
bool was_added(const rs2::device &dev) const
Definition rs_context.hpp:40
bool was_removed(const rs2::device &dev) const
Definition rs_context.hpp:23
device_list get_new_devices() const
Definition rs_context.hpp:57
Definition rs_pipeline.hpp:363
Definition rs_record_playback.hpp:28
Definition rs_sensor.hpp:104
std::shared_ptr< rs2_sensor > _sensor
Definition rs_sensor.hpp:388
Definition rs_internal.hpp:234
Definition rs_processing_gl.hpp:13
#define RS2_API_VERSION
Definition rs.h:44
void rs2_delete_context(rs2_context *context)
Frees the relevant context object.
rs2_device * rs2_device_hub_wait_for_device(const rs2_device_hub *hub, rs2_error **error)
void rs2_delete_device_hub(const rs2_device_hub *hub)
Frees the relevant device hub object.
int rs2_device_hub_is_device_connected(const rs2_device_hub *hub, const rs2_device *device, rs2_error **error)
rs2_context * rs2_create_context_ex(int api_version, const char *json_settings, rs2_error **error)
Creates RealSense context that is required for the rest of the API, and accepts a settings JSON.
void rs2_context_unload_tracking_module(rs2_context *ctx, rs2_error **error)
void rs2_convert_bag_to_db3(const char *input_bag_path, const char *output_db3_path, const rs2_context *ctx, rs2_update_progress_callback_ptr callback, void *client_data, rs2_error **error)
rs2_device_hub * rs2_create_device_hub(const rs2_context *context, rs2_error **error)
Creates RealSense device_hub .
void rs2_context_remove_device(rs2_context *ctx, const char *file, rs2_error **error)
rs2_device * rs2_context_add_device(rs2_context *ctx, const char *file, rs2_error **error)
void rs2_set_devices_changed_callback_cpp(rs2_context *context, rs2_devices_changed_callback *callback, rs2_error **error)
rs2_device_list * rs2_query_devices_ex(const rs2_context *context, int product_mask, rs2_error **error)
rs2_device_list * rs2_query_devices(const rs2_context *context, rs2_error **error)
void rs2_delete_device(rs2_device *device)
int rs2_device_list_contains(const rs2_device_list *info_list, const rs2_device *device, rs2_error **error)
void rs2_delete_device_list(rs2_device_list *info_list)
rs2_device * rs2_create_device_from_sensor(const rs2_sensor *sensor, rs2_error **error)
struct rs2_device_list rs2_device_list
Definition rs_types.h:301
struct rs2_error rs2_error
Definition rs_types.h:293
Definition rs_types.hpp:82