Intel® RealSense™ Cross Platform API  2.13.0
Intel Realsense Cross-platform API
rs_pipeline.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #ifndef LIBREALSENSE_RS2_PIPELINE_HPP
5 #define LIBREALSENSE_RS2_PIPELINE_HPP
6 
7 #include "rs_types.hpp"
8 #include "rs_frame.hpp"
9 #include "rs_context.hpp"
10 
11 namespace rs2
12 {
19  {
20  public:
21 
22  pipeline_profile() : _pipeline_profile(nullptr) {}
23 
29  std::vector<stream_profile> get_streams() const
30  {
31  std::vector<stream_profile> results;
32 
33  rs2_error* e = nullptr;
34  std::shared_ptr<rs2_stream_profile_list> list(
35  rs2_pipeline_profile_get_streams(_pipeline_profile.get(), &e),
37  error::handle(e);
38 
39  auto size = rs2_get_stream_profiles_count(list.get(), &e);
40  error::handle(e);
41 
42  for (auto i = 0; i < size; i++)
43  {
44  stream_profile profile(rs2_get_stream_profile(list.get(), i, &e));
45  error::handle(e);
46  results.push_back(profile);
47  }
48 
49  return results;
50  }
51 
60  stream_profile get_stream(rs2_stream stream_type, int stream_index = -1) const
61  {
62  for (auto&& s : get_streams())
63  {
64  if (s.stream_type() == stream_type && (stream_index == -1 || s.stream_index() == stream_index))
65  {
66  return s;
67  }
68  }
69  throw std::runtime_error("Profile does not contain the requested stream");
70  }
71 
84  {
85  rs2_error* e = nullptr;
86  std::shared_ptr<rs2_device> dev(
87  rs2_pipeline_profile_get_device(_pipeline_profile.get(), &e),
89 
90  error::handle(e);
91 
92  return device(dev);
93  }
94 
100  operator bool() const
101  {
102  return _pipeline_profile != nullptr;
103  }
104 
105  private:
106  pipeline_profile(std::shared_ptr<rs2_pipeline_profile> profile) :
107  _pipeline_profile(profile)
108  {
109 
110  }
111  std::shared_ptr<rs2_pipeline_profile> _pipeline_profile;
112  friend class config;
113  friend class pipeline;
114  };
115 
116  class pipeline;
117 
125  class config
126  {
127  public:
129  {
130  rs2_error* e = nullptr;
131  _config = std::shared_ptr<rs2_config>(
132  rs2_create_config(&e),
134  error::handle(e);
135  }
136 
157  void enable_stream(rs2_stream stream_type, int stream_index, int width, int height, rs2_format format = RS2_FORMAT_ANY, int framerate = 0)
158  {
159  rs2_error* e = nullptr;
160  rs2_config_enable_stream(_config.get(), stream_type, stream_index, width, height, format, framerate, &e);
161  error::handle(e);
162  }
163 
164  //Stream type and possibly also stream index
165  void enable_stream(rs2_stream stream_type, int stream_index = -1)
166  {
167  enable_stream(stream_type, stream_index, 0, 0, RS2_FORMAT_ANY, 0);
168  }
169 
170  //Stream type and resolution, and possibly format and frame rate
171  void enable_stream(rs2_stream stream_type, int width, int height, rs2_format format = RS2_FORMAT_ANY, int framerate = 0)
172  {
173  enable_stream(stream_type, -1, width, height, format, framerate);
174  }
175 
176  //Stream type and format
177  void enable_stream(rs2_stream stream_type, rs2_format format, int framerate = 0)
178  {
179  enable_stream(stream_type, -1, 0, 0, format, framerate);
180  }
181 
182  //Stream type and format
183  void enable_stream(rs2_stream stream_type, int stream_index, rs2_format format, int framerate = 0)
184  {
185  enable_stream(stream_type, stream_index, 0, 0, format, framerate);
186  }
187 
195  {
196  rs2_error* e = nullptr;
197  rs2_config_enable_all_stream(_config.get(), &e);
198  error::handle(e);
199  }
200 
209  void enable_device(const std::string& serial)
210  {
211  rs2_error* e = nullptr;
212  rs2_config_enable_device(_config.get(), serial.c_str(), &e);
213  error::handle(e);
214  }
215 
224  void enable_device_from_file(const std::string& file_name, bool repeat_playback = true)
225  {
226  rs2_error* e = nullptr;
227  rs2_config_enable_device_from_file_repeat_option(_config.get(), file_name.c_str(), repeat_playback, &e);
228  error::handle(e);
229  }
230 
238  void enable_record_to_file(const std::string& file_name)
239  {
240  rs2_error* e = nullptr;
241  rs2_config_enable_record_to_file(_config.get(), file_name.c_str(), &e);
242  error::handle(e);
243  }
244 
252  void disable_stream(rs2_stream stream, int index = -1)
253  {
254  rs2_error* e = nullptr;
255  rs2_config_disable_indexed_stream(_config.get(), stream, index, &e);
256  error::handle(e);
257  }
258 
265  {
266  rs2_error* e = nullptr;
267  rs2_config_disable_all_streams(_config.get(), &e);
268  error::handle(e);
269  }
270 
289  pipeline_profile resolve(std::shared_ptr<rs2_pipeline> p) const
290  {
291  rs2_error* e = nullptr;
292  auto profile = std::shared_ptr<rs2_pipeline_profile>(
293  rs2_config_resolve(_config.get(), p.get(), &e),
295 
296  error::handle(e);
297  return pipeline_profile(profile);
298  }
299 
307  bool can_resolve(std::shared_ptr<rs2_pipeline> p) const
308  {
309  rs2_error* e = nullptr;
310  int res = rs2_config_can_resolve(_config.get(), p.get(), &e);
311  error::handle(e);
312  return res != 0;
313  }
314 
315  std::shared_ptr<rs2_config> get() const
316  {
317  return _config;
318  }
319  private:
320  config(std::shared_ptr<rs2_config> config) : _config(config)
321  {
322  }
323  std::shared_ptr<rs2_config> _config;
324 
325  };
326 
335  class pipeline
336  {
337  public:
338 
346  : _ctx(ctx)
347  {
348  rs2_error* e = nullptr;
349  _pipeline = std::shared_ptr<rs2_pipeline>(
350  rs2_create_pipeline(ctx._context.get(), &e),
352  error::handle(e);
353  }
354 
367  {
368  rs2_error* e = nullptr;
369  auto p = std::shared_ptr<rs2_pipeline_profile>(
370  rs2_pipeline_start(_pipeline.get(), &e),
372 
373  error::handle(e);
374  return pipeline_profile(p);
375  }
376 
396  {
397  rs2_error* e = nullptr;
398  auto p = std::shared_ptr<rs2_pipeline_profile>(
399  rs2_pipeline_start_with_config(_pipeline.get(), config.get().get(), &e),
401 
402  error::handle(e);
403  return pipeline_profile(p);
404  }
405 
406 
414  void stop()
415  {
416  rs2_error* e = nullptr;
417  rs2_pipeline_stop(_pipeline.get(), &e);
418  error::handle(e);
419  }
420 
436  frameset wait_for_frames(unsigned int timeout_ms = 5000) const
437  {
438  rs2_error* e = nullptr;
439  frame f(rs2_pipeline_wait_for_frames(_pipeline.get(), timeout_ms, &e));
440  error::handle(e);
441 
442  return frameset(f);
443  }
444 
459  bool poll_for_frames(frameset* f) const
460  {
461  if (!f)
462  {
463  throw std::invalid_argument("null frameset");
464  }
465  rs2_error* e = nullptr;
466  rs2_frame* frame_ref = nullptr;
467  auto res = rs2_pipeline_poll_for_frames(_pipeline.get(), &frame_ref, &e);
468  error::handle(e);
469 
470  if (res) *f = frameset(frame(frame_ref));
471  return res > 0;
472  }
473 
484  {
485  rs2_error* e = nullptr;
486  auto p = std::shared_ptr<rs2_pipeline_profile>(
487  rs2_pipeline_get_active_profile(_pipeline.get(), &e),
489 
490  error::handle(e);
491  return pipeline_profile(p);
492  }
493 
494  operator std::shared_ptr<rs2_pipeline>() const
495  {
496  return _pipeline;
497  }
498 
499  private:
500  context _ctx;
501  std::shared_ptr<rs2_pipeline> _pipeline;
502  friend class config;
503  };
504 }
505 #endif // LIBREALSENSE_RS2_PROCESSING_HPP
Definition: rs_frame.hpp:21
stream_profile get_stream(rs2_stream stream_type, int stream_index=-1) const
Definition: rs_pipeline.hpp:60
int rs2_pipeline_poll_for_frames(rs2_pipeline *pipe, rs2_frame **output_frame, rs2_error **error)
void rs2_config_enable_device(rs2_config *config, const char *serial, rs2_error **error)
Definition: rs_pipeline.hpp:125
Definition: rs_frame.hpp:202
void stop()
Definition: rs_pipeline.hpp:414
pipeline_profile resolve(std::shared_ptr< rs2_pipeline > p) const
Definition: rs_pipeline.hpp:289
bool poll_for_frames(frameset *f) const
Definition: rs_pipeline.hpp:459
pipeline_profile start(const config &config)
Definition: rs_pipeline.hpp:395
Definition: rs_pipeline.hpp:18
rs2_pipeline_profile * rs2_pipeline_start(rs2_pipeline *pipe, rs2_error **error)
void rs2_config_enable_all_stream(rs2_config *config, rs2_error **error)
void rs2_delete_device(rs2_device *device)
rs2_pipeline * rs2_create_pipeline(rs2_context *ctx, rs2_error **error)
pipeline(context ctx=context())
Definition: rs_pipeline.hpp:345
Definition: rs_frame.hpp:630
Definition: rs_context.hpp:11
void enable_device(const std::string &serial)
Definition: rs_pipeline.hpp:209
rs2_pipeline_profile * rs2_pipeline_get_active_profile(rs2_pipeline *pipe, rs2_error **error)
std::shared_ptr< rs2_config > get() const
Definition: rs_pipeline.hpp:315
void rs2_delete_config(rs2_config *config)
Definition: rs_context.hpp:78
void enable_stream(rs2_stream stream_type, int stream_index, rs2_format format, int framerate=0)
Definition: rs_pipeline.hpp:183
rs2_device * rs2_pipeline_profile_get_device(rs2_pipeline_profile *profile, rs2_error **error)
rs2_pipeline_profile * rs2_config_resolve(rs2_config *config, rs2_pipeline *pipe, rs2_error **error)
rs2_stream_profile_list * rs2_pipeline_profile_get_streams(rs2_pipeline_profile *profile, rs2_error **error)
rs2_frame * rs2_pipeline_wait_for_frames(rs2_pipeline *pipe, unsigned int timeout_ms, rs2_error **error)
void rs2_config_enable_stream(rs2_config *config, rs2_stream stream, int index, int width, int height, rs2_format format, int framerate, rs2_error **error)
void disable_all_streams()
Definition: rs_pipeline.hpp:264
std::vector< stream_profile > get_streams() const
Definition: rs_pipeline.hpp:29
device get_device() const
Definition: rs_pipeline.hpp:83
const rs2_stream_profile * rs2_get_stream_profile(const rs2_stream_profile_list *list, int index, rs2_error **error)
void disable_stream(rs2_stream stream, int index=-1)
Definition: rs_pipeline.hpp:252
Definition: rs_sensor.h:57
rs2_format
Format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:55
pipeline_profile get_active_profile() const
Definition: rs_pipeline.hpp:483
void enable_all_streams()
Definition: rs_pipeline.hpp:194
void enable_stream(rs2_stream stream_type, int stream_index, int width, int height, rs2_format format=RS2_FORMAT_ANY, int framerate=0)
Definition: rs_pipeline.hpp:157
static void handle(rs2_error *e)
Definition: rs_types.hpp:121
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:38
rs2_pipeline_profile * rs2_pipeline_start_with_config(rs2_pipeline *pipe, rs2_config *config, rs2_error **error)
config()
Definition: rs_pipeline.hpp:128
Definition: rs_pipeline.hpp:335
void enable_stream(rs2_stream stream_type, int width, int height, rs2_format format=RS2_FORMAT_ANY, int framerate=0)
Definition: rs_pipeline.hpp:171
void enable_record_to_file(const std::string &file_name)
Definition: rs_pipeline.hpp:238
void rs2_config_enable_device_from_file_repeat_option(rs2_config *config, const char *file, int repeat_playback, rs2_error **error)
void rs2_delete_stream_profiles_list(rs2_stream_profile_list *list)
void rs2_pipeline_stop(rs2_pipeline *pipe, rs2_error **error)
frameset wait_for_frames(unsigned int timeout_ms=5000) const
Definition: rs_pipeline.hpp:436
void enable_stream(rs2_stream stream_type, int stream_index=-1)
Definition: rs_pipeline.hpp:165
void rs2_config_disable_indexed_stream(rs2_config *config, rs2_stream stream, int index, rs2_error **error)
void rs2_config_enable_record_to_file(rs2_config *config, const char *file, rs2_error **error)
void enable_device_from_file(const std::string &file_name, bool repeat_playback=true)
Definition: rs_pipeline.hpp:224
void rs2_config_disable_all_streams(rs2_config *config, rs2_error **error)
rs2_config * rs2_create_config(rs2_error **error)
pipeline_profile start()
Definition: rs_pipeline.hpp:366
int rs2_get_stream_profiles_count(const rs2_stream_profile_list *list, rs2_error **error)
bool can_resolve(std::shared_ptr< rs2_pipeline > p) const
Definition: rs_pipeline.hpp:307
struct rs2_error rs2_error
Definition: rs_types.h:149
int rs2_config_can_resolve(rs2_config *config, rs2_pipeline *pipe, rs2_error **error)
Definition: rs_device.hpp:18
void rs2_delete_pipeline_profile(rs2_pipeline_profile *profile)
void rs2_delete_pipeline(rs2_pipeline *pipe)
struct rs2_frame rs2_frame
Definition: rs_types.h:151
pipeline_profile()
Definition: rs_pipeline.hpp:22
void enable_stream(rs2_stream stream_type, rs2_format format, int framerate=0)
Definition: rs_pipeline.hpp:177