cpp-callback.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include <librealsense/rs.hpp>
5 #include <thread>
6 #include <iostream>
7 
8 int main() try
9 {
11  if(ctx.get_device_count() == 0) return EXIT_FAILURE;
12  rs::device * dev = ctx.get_device(0);
13 
16 
17  // rs::device is NOT designed to be thread safe, so you should not currently make calls
18  // against it from your callback thread. If you need access to information like
19  // intrinsics/extrinsics, formats, etc., capture them ahead of time
20  rs::intrinsics depth_intrin, color_intrin;
21  rs::format depth_format, color_format;
22  depth_intrin = dev->get_stream_intrinsics(rs::stream::depth);
23  depth_format = dev->get_stream_format(rs::stream::depth);
24  color_intrin = dev->get_stream_intrinsics(rs::stream::color);
25  color_format = dev->get_stream_format(rs::stream::color);
26 
27  // Set callbacks prior to calling start()
28  auto depth_callback = [depth_intrin, depth_format](rs::frame f)
29  {
30  std::cout << depth_intrin.width << "x" << depth_intrin.height
31  << " " << depth_format << "\tat t = " << f.get_timestamp() << " ms" << std::endl;
32  };
33  auto color_callback = [color_intrin, color_format](rs::frame f)
34  {
35  std::cout << color_intrin.width << "x" << color_intrin.height
36  << " " << color_format << "\tat t = " << f.get_timestamp() << " ms" << std::endl;
37  };
38 
39  dev->set_frame_callback(rs::stream::depth, depth_callback);
40  dev->set_frame_callback(rs::stream::color, color_callback);
41 
42  // Between start() and stop(), you will receive calls to the specified callbacks from background threads
43  dev->start();
44  std::this_thread::sleep_for(std::chrono::seconds(10));
45  // NOTE: No need to call wait_for_frames() or poll_for_frames(), though they should still work as designed
46  dev->stop();
47 
48  return EXIT_SUCCESS;
49 }
50 catch(const rs::error & e)
51 {
52  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
53  return EXIT_FAILURE;
54 }
Provides convenience methods relating to devices.
Definition: rs.hpp:567
intrinsics get_stream_intrinsics(stream stream) const
Retrieves intrinsic camera parameters for specific stream.
Definition: rs.hpp:788
void enable_stream(stream stream, int width, int height, format format, int framerate, output_buffer_format output_buffer_type=output_buffer_format::continous)
Enables specific stream and requests specific properties.
Definition: rs.hpp:704
const std::string & get_failed_args() const
Definition: rs.hpp:315
rs_error * e
Video stream intrinsics.
Definition: rs.hpp:252
Exposes librealsense functionality for C++ compilers.
format get_stream_format(stream stream) const
Retrieves pixel format for specific stream.
Definition: rs.hpp:766
void stop(rs::source source=rs::source::video)
Ends streaming on all streams for this device.
Definition: rs.hpp:887
Context.
Definition: rs.hpp:319
GLfloat f
Definition: glext.h:1868
format
Formats: defines how each stream can be encoded. rs_format specifies how a frame is represented in me...
Definition: rs.hpp:42
void start(rs::source source=rs::source::video)
Begins streaming on all enabled streams for this device.
Definition: rs.hpp:879
int main()
Definition: cpp-callback.cpp:8
auto ctx
int height
Definition: rs.h:303
int width
Definition: rs.h:302
GLfloat seconds
Definition: wglext.h:657
Frame.
Definition: rs.hpp:392
rs_device * dev
void set_frame_callback(rs::stream stream, std::function< void(frame)> frame_handler)
Sets callback for frame arrival event.
Definition: rs.hpp:817
device * get_device(int index)
Definition: rs.hpp:354
const std::string & get_failed_function() const
Definition: rs.hpp:314
int get_device_count() const
Definition: rs.hpp:343


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17