cpp-stride.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 4 - low latency with callbacks and zero copy //
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 #include <iostream>
17 
18 int main() try
19 {
21  printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
22  if (ctx.get_device_count() == 0) return EXIT_FAILURE;
23 
24  rs::device * dev = ctx.get_device(0);
25  printf("\nUsing device 0, an %s\n", dev->get_name());
26  printf(" Serial number: %s\n", dev->get_serial());
27  printf(" Firmware version: %s\n", dev->get_firmware_version());
28 
29  const auto streams = 4;
30  std::vector<uint16_t> supported_streams = { (uint16_t)rs::stream::depth, (uint16_t)rs::stream::color, (uint16_t)rs::stream::infrared};
31  const size_t max_queue_size = 1; // To minimize latency prefer frame drops
32  single_consumer_queue<rs::frame> frames_queue[streams];
33  texture_buffer buffers[streams];
34  std::atomic<bool> running(true);
35 
36  struct resolution
37  {
38  int width;
39  int height;
41  };
42  std::map<rs::stream, resolution> resolutions;
43 
45  supported_streams.push_back((uint16_t)rs::stream::infrared2);
46 
47  for (auto i : supported_streams)
48  {
49  dev->set_frame_callback((rs::stream)i, [dev, &running, &frames_queue, &resolutions, i, max_queue_size](rs::frame frame)
50  {
51  if (running && frames_queue[i].size() <= max_queue_size) frames_queue[i].enqueue(std::move(frame));
52  });
53  }
54 
59  dev->enable_stream(rs::stream::infrared2, 0, 0, rs::format::y8, 60, rs::output_buffer_format::native);
60 
65  resolutions[rs::stream::infrared2] = { dev->get_stream_width(rs::stream::infrared2), dev->get_stream_height(rs::stream::infrared2), rs::format::y8 };
66 
67  glfwInit();
68 
69  auto max_aspect_ratio = 0.0f;
70  for (auto i : supported_streams)
71  {
72  auto aspect_ratio = static_cast<float>(resolutions[static_cast<rs::stream>(i)].height) / static_cast<float>(resolutions[static_cast<rs::stream>(i)].width);
73  if (max_aspect_ratio < aspect_ratio)
74  max_aspect_ratio = aspect_ratio;
75  };
76 
77  auto win = glfwCreateWindow(1100, int(1100 * max_aspect_ratio), "librealsense - stride", nullptr, nullptr);
79 
80  dev->start();
81 
82  while (!glfwWindowShouldClose(win))
83  {
85  rs::frame frame;
86 
87  int w, h;
88  glfwGetFramebufferSize(win, &w, &h);
89  glViewport(0, 0, w, h);
90  glClear(GL_COLOR_BUFFER_BIT);
91 
92  glfwGetWindowSize(win, &w, &h);
93  glLoadIdentity();
94  glOrtho(0, w, h, 0, -1, +1);
95 
96  for (auto i = 0; i < streams; i++)
97  {
98  if(!dev->supports(rs::capabilities(i))) continue;
99 
100  auto res = resolutions[(rs::stream)i];
101 
102  if (frames_queue[i].try_dequeue(&frame))
103  {
104  buffers[i].upload(frame);
105  }
106 
107  auto x = (i % 2) * (w / 2);
108  auto y = (i / 2) * (h / 2);
109  buffers[i].show(x, y, w / 2, h / 2, res.width, res.height);
110  }
111 
112  glfwSwapBuffers(win);
113  }
114 
115  running = false;
116 
117  for (auto i : supported_streams) frames_queue[i].clear();
118 
119  dev->stop();
120 
121  for (auto i : supported_streams)
122  {
123  if (dev->is_stream_enabled((rs::stream)i))
124  dev->disable_stream((rs::stream)i);
125  }
126 
127  return EXIT_SUCCESS;
128 }
129 catch (const rs::error & e)
130 {
131  printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
132  printf(" %s\n", e.what());
133  return EXIT_FAILURE;
134 }
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
int main()
Definition: cpp-stride.cpp:18
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
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
GLsizeiptr size
Definition: glext.h:532
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
capabilities
Specifies various capabilities of a RealSense device.
Definition: rs.hpp:169
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