rs-callback.cpp
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 #include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
5 #include <iostream>
6 #include <map>
7 #include <chrono>
8 #include <mutex>
9 #include <thread>
10 
11 // The callback example demonstrates asynchronous usage of the pipeline
12 int main(int argc, char * argv[]) try
13 {
14  //rs2::log_to_console(RS2_LOG_SEVERITY_ERROR);
15 
16  std::map<int, int> counters;
17  std::map<int, std::string> stream_names;
18  std::mutex mutex;
19 
20  // Define frame callback
21  // The callback is executed on a sensor thread and can be called simultaneously from multiple sensors
22  // Therefore any modification to common memory should be done under lock
23  auto callback = [&](const rs2::frame& frame)
24  {
25  std::lock_guard<std::mutex> lock(mutex);
26  if (rs2::frameset fs = frame.as<rs2::frameset>())
27  {
28  // With callbacks, all synchronized stream will arrive in a single frameset
29  for (const rs2::frame& f : fs)
30  counters[f.get_profile().unique_id()]++;
31  }
32  else
33  {
34  // Stream that bypass synchronization (such as IMU) will produce single frames
35  counters[frame.get_profile().unique_id()]++;
36  }
37  };
38 
39  // Declare RealSense pipeline, encapsulating the actual device and sensors.
41 
42  // Start streaming through the callback with default recommended configuration
43  // The default video configuration contains Depth and Color streams
44  // If a device is capable to stream IMU data, both Gyro and Accelerometer are enabled by default
45  //
47 
48  // Collect the enabled streams names
49  for (auto p : profiles.get_streams())
50  stream_names[p.unique_id()] = p.stream_name();
51 
52  std::cout << "RealSense callback sample" << std::endl << std::endl;
53 
54  while (true)
55  {
56  std::this_thread::sleep_for(std::chrono::seconds(1));
57 
58  std::lock_guard<std::mutex> lock(mutex);
59 
60  std::cout << "\r";
61  for (auto p : counters)
62  {
63  std::cout << stream_names[p.first] << "[" << p.first << "]: " << p.second << " [frames] || ";
64  }
65  }
66 
67  return EXIT_SUCCESS;
68 }
69 catch (const rs2::error & e)
70 {
71  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
72  return EXIT_FAILURE;
73 }
74 catch (const std::exception& e)
75 {
76  std::cerr << e.what() << std::endl;
77  return EXIT_FAILURE;
78 }
static const textual_icon lock
Definition: model-views.h:218
GLfloat GLfloat p
Definition: glext.h:12687
GLint GLint GLsizei GLuint * counters
Definition: glext.h:5682
e
Definition: rmse.py:177
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
GLdouble f
std::ostream & cout()
def callback(frame)
Definition: t265_stereo.py:91
std::vector< stream_profile > get_streams() const
Definition: rs_pipeline.hpp:29
std::ostream & cerr()
int main(int argc, char *argv[])
Definition: rs-callback.cpp:12
pipeline_profile start()
const std::string & get_failed_function() const
Definition: rs_types.hpp:112
T as() const
Definition: rs_frame.hpp:580


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40