cpp-callback.cpp
Go to the documentation of this file.
00001 // License: Apache 2.0. See LICENSE file in root directory.
00002 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
00003 
00004 #include <librealsense/rs.hpp>
00005 #include <thread>
00006 #include <iostream>
00007 
00008 int main() try
00009 {
00010     rs::context ctx;
00011     if(ctx.get_device_count() == 0) return EXIT_FAILURE;
00012     rs::device * dev = ctx.get_device(0);
00013 
00014     dev->enable_stream(rs::stream::depth, rs::preset::best_quality);
00015     dev->enable_stream(rs::stream::color, rs::preset::best_quality);
00016 
00017     // rs::device is NOT designed to be thread safe, so you should not currently make calls
00018     // against it from your callback thread. If you need access to information like 
00019     // intrinsics/extrinsics, formats, etc., capture them ahead of time
00020     rs::intrinsics depth_intrin, color_intrin;
00021     rs::format depth_format, color_format;
00022     depth_intrin = dev->get_stream_intrinsics(rs::stream::depth);
00023     depth_format = dev->get_stream_format(rs::stream::depth);
00024     color_intrin = dev->get_stream_intrinsics(rs::stream::color);
00025     color_format = dev->get_stream_format(rs::stream::color);
00026 
00027     // Set callbacks prior to calling start()
00028     auto depth_callback = [depth_intrin, depth_format](rs::frame f)
00029     {
00030         std::cout << depth_intrin.width << "x" << depth_intrin.height
00031             << " " << depth_format << "\tat t = " << f.get_timestamp() << " ms" << std::endl;
00032     };
00033     auto color_callback = [color_intrin, color_format](rs::frame f)
00034     {
00035         std::cout << color_intrin.width << "x" << color_intrin.height
00036             << " " << color_format << "\tat t = " << f.get_timestamp() << " ms" << std::endl;
00037     };
00038 
00039     dev->set_frame_callback(rs::stream::depth, depth_callback);
00040     dev->set_frame_callback(rs::stream::color, color_callback);
00041 
00042     // Between start() and stop(), you will receive calls to the specified callbacks from background threads
00043     dev->start();
00044     std::this_thread::sleep_for(std::chrono::seconds(10));
00045     // NOTE: No need to call wait_for_frames() or poll_for_frames(), though they should still work as designed
00046     dev->stop();
00047 
00048     return EXIT_SUCCESS;
00049 }
00050 catch(const rs::error & e)
00051 {
00052     std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
00053     return EXIT_FAILURE;
00054 }


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Tue Jun 25 2019 19:54:38