cpp-callback-2.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 
5 // librealsense Multi-threading Demo 3 - callbacks //
7 
8 #include <librealsense/rs.hpp>
9 #include <cstdio>
10 #include <atomic>
11 #include <map>
12 #include <GLFW/glfw3.h>
13 
14 #include "concurrency.hpp"
15 #include "example.hpp"
16 
17 int main() try
18 {
20  printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
21  if (ctx.get_device_count() == 0) return EXIT_FAILURE;
22 
23  rs::device * dev = ctx.get_device(0);
24  printf("\nUsing device 0, an %s\n", dev->get_name());
25  printf(" Serial number: %s\n", dev->get_serial());
26  printf(" Firmware version: %s\n", dev->get_firmware_version());
27 
28  const auto streams = 4;
29  single_consumer_queue<rs::frame> frames_queue[streams];
30  texture_buffer buffers[streams];
31  std::atomic<bool> running(true);
32 
33  struct resolution
34  {
35  int width;
36  int height;
38  };
39  std::map<rs::stream, resolution> resolutions;
40 
41  for (auto i = 0; i < streams; i++)
42  {
43  // Set callback to be executed when frame of stream i is ready
44  dev->set_frame_callback((rs::stream)i, [dev, &running, &frames_queue, &resolutions, i](rs::frame frame)
45  {
46  if (running) frames_queue[i].enqueue(std::move(frame));
47  // Important: Not constraining the max size of the queue will prevent frames from being dropped at the expense of lattency
48  });
49  }
50 
55  {
57  }
58 
64 
65  glfwInit();
66 
67  auto max_aspect_ratio = 0.0f;
68  for (auto i = 0; i < streams; i++)
69  {
70  auto aspect_ratio = static_cast<float>(resolutions[static_cast<rs::stream>(i)].height) / static_cast<float>(resolutions[static_cast<rs::stream>(i)].width);
71  if (max_aspect_ratio < aspect_ratio)
72  max_aspect_ratio = aspect_ratio;
73  };
74 
75  auto win = glfwCreateWindow(1100, int(1100 * max_aspect_ratio), "librealsense - callback 2", nullptr, nullptr);
77 
78  dev->start();
79 
80  while (!glfwWindowShouldClose(win))
81  {
83  rs::frame frame;
84 
85  int w, h;
86  glfwGetFramebufferSize(win, &w, &h);
87  glViewport(0, 0, w, h);
88  glClear(GL_COLOR_BUFFER_BIT);
89 
90  glfwGetWindowSize(win, &w, &h);
91  glLoadIdentity();
92  glOrtho(0, w, h, 0, -1, +1);
93 
94  for (auto i = 0; i < streams; i++)
95  {
96  auto res = resolutions[(rs::stream)i];
97 
98  if (frames_queue[i].try_dequeue(&frame)) // If new frame of stream i is available
99  {
100  buffers[i].upload(frame.get_data(), res.width, res.height, res.format); // Update the texture
101  }
102 
103  auto x = (i % 2) * (w / 2);
104  auto y = (i / 2) * (h / 2);
105  buffers[i].show(x, y, w / 2, h / 2, res.width, res.height);
106  }
107 
108  glfwSwapBuffers(win);
109  }
110 
111  running = false;
112 
113  for (auto i = 0; i < streams; i++) frames_queue[i].clear();
114 
115  dev->stop();
116 
117  for (auto i = 0; i < streams; i++)
118  {
119  if (dev->is_stream_enabled((rs::stream)i))
120  dev->disable_stream((rs::stream)i);
121  }
122 
123  return EXIT_SUCCESS;
124 }
125 catch (const rs::error & e)
126 {
127  printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
128  printf(" %s\n", e.what());
129  return EXIT_FAILURE;
130 }
Provides convenience methods relating to devices.
Definition: rs.hpp:567
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
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:456
const std::string & get_failed_args() const
Definition: rs.hpp:315
rs_error * e
GLint GLint GLsizei GLsizei height
Definition: glext.h:112
const char * get_firmware_version() const
Retrieves version of firmware currently installed on device.
Definition: rs.hpp:608
GLint GLint GLint GLint GLint GLint y
Definition: glext.h:114
int get_stream_width(stream stream) const
Retrieves width, in pixels, of a specific stream, equivalent to the width field from the stream&#39;s int...
Definition: rs.hpp:744
const GLuint * buffers
Definition: glext.h:529
Exposes librealsense functionality for C++ compilers.
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1944
void stop(rs::source source=rs::source::video)
Ends streaming on all streams for this device.
Definition: rs.hpp:887
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
void show(float rx, float ry, float rw, float rh) const
Definition: example.hpp:189
stream
Streams are different types of data provided by RealSense devices.
Definition: rs.hpp:24
int get_stream_height(stream stream) const
Retrieves height, in pixels, of a specific stream, equivalent to the height field from the stream&#39;s i...
Definition: rs.hpp:755
bool is_stream_enabled(stream stream) const
Determines if specific stream is enabled.
Definition: rs.hpp:733
Context.
Definition: rs.hpp:319
const char * get_name() const
Retrieves human-readable device model string.
Definition: rs.hpp:578
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
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:544
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:531
const void * get_data() const
Definition: rs.hpp:478
int main()
auto ctx
const char * get_serial() const
Retrieves unique serial number of device.
Definition: rs.hpp:588
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:116
void disable_stream(stream stream)
Disables specific stream.
Definition: rs.hpp:723
Frame.
Definition: rs.hpp:392
GLint GLint GLsizei width
Definition: glext.h:112
rs_device * dev
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:484
void set_frame_callback(rs::stream stream, std::function< void(frame)> frame_handler)
Sets callback for frame arrival event.
Definition: rs.hpp:817
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:682
bool supports(capabilities capability) const
Determines device capabilities.
Definition: rs.hpp:1005
GLuint res
Definition: glext.h:8310
device * get_device(int index)
Definition: rs.hpp:354
const std::string & get_failed_function() const
Definition: rs.hpp:314
GLint GLint GLint GLint GLint x
Definition: glext.h:114
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:726
void upload(const void *data, int width, int height, rs::format format, int stride=0)
Definition: example.hpp:73
int get_device_count() const
Definition: rs.hpp:343
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:406


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