From 57e3ee4d42ede60ad635d6b97f647febd59a4ea3 Mon Sep 17 00:00:00 2001 From: AnnaRomnov Date: Wed, 7 Aug 2019 10:38:06 +0300 Subject: [PATCH] fix rs-multicam bug (the same colorizer was used for all devices, resulting in new unique-id for each new frame, causing allocation of new textures and memory leak) --- examples/multicam/readme.md | 17 ++++++++++------- examples/multicam/rs-multicam.cpp | 21 +++++++++++++-------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/examples/multicam/readme.md b/examples/multicam/readme.md index ca7b9f7a17..f1ec88eba5 100644 --- a/examples/multicam/readme.md +++ b/examples/multicam/readme.md @@ -50,14 +50,14 @@ The `window` class resides in `example.hpp` and lets us easily open a new window Next, we define the objects to be used in the example. ```cpp -rs2::context ctx; // Create librealsense context for managing devices +rs2::context ctx; // Create librealsense context for managing devices -rs2::colorizer colorizer; // Utility class to convert depth data RGB +std::map colorizers; // Declare map from device serial number to colorizer (utility class to convert depth data RGB colorspace) -std::vector pipelines; +std::vector pipelines; ``` -The `rs2::context` encapsulates all of the devices and sensors, and provides some additional functionalities. We employ the `rs2::colorizer ` to convert depth data to RGB format. -In the example we use multiple `rs2::pipeline` objects, each controlling a lifetime of a single HW device. +The `rs2::context` encapsulates all of the devices and sensors, and provides some additional functionalities. We employ the `rs2::colorizer ` to convert depth data to RGB format. +In the example we use multiple `rs2::pipeline` objects, each controlling a lifetime of a single HW device. Similarly, we initialize a separate `rs2::colorizer` object for each device. We keep a mapping from the device's serial number to it's `rs2::colorizer` object, this way we'll be able to apply the correct `rs2::colorizer` to each frame. The example's flow starts with listing and activating all the connected IntelĀ® RealSenseā„¢ devices: ```cpp @@ -117,12 +117,15 @@ for (rs2::frame& f : fs) new_frames.emplace_back(f); ``` -The Depth data is delivered as `uint16_t` type which cannot be rendered directly, therefore we use `rs2::colorizer` to convert the depth representation into human-readable RGB map: +The Depth data is delivered as `uint16_t` type which cannot be rendered directly, therefore we use `rs2::colorizer` to convert the depth representation into human-readable RGB map. We use `rs2::sensor_from_frame` function to retrieve the serial number of the frame's device. Then we can use that device's `rs2::colorizer` to process the frame: ```cpp // Convert the newly-arrived frames to render-friendly format for (const auto& frame : new_frames) { - render_frames[frame.get_profile().unique_id()] = colorizer.process(frame); + // Get the serial number of the current frame's device + auto serial = rs2::sensor_from_frame(frame)->get_info(RS2_CAMERA_INFO_SERIAL_NUMBER); + // Apply the colorizer of the matching device and store the colorized frame + render_frames[frame.get_profile().unique_id()] = colorizers[serial].process(frame); } ``` diff --git a/examples/multicam/rs-multicam.cpp b/examples/multicam/rs-multicam.cpp index 763bb6ff87..30da796fea 100644 --- a/examples/multicam/rs-multicam.cpp +++ b/examples/multicam/rs-multicam.cpp @@ -12,11 +12,11 @@ int main(int argc, char * argv[]) try // Create a simple OpenGL window for rendering: window app(1280, 960, "CPP Multi-Camera Example"); - rs2::context ctx; // Create librealsense context for managing devices + rs2::context ctx; // Create librealsense context for managing devices - rs2::colorizer colorizer; // Utility class to convert depth data RGB colorspace + std::map colorizers; // Declare map from device serial number to colorizer (utility class to convert depth data RGB colorspace) - std::vector pipelines; + std::vector pipelines; // Start a streaming pipe per each connected device for (auto&& dev : ctx.query_devices()) @@ -26,11 +26,10 @@ int main(int argc, char * argv[]) try cfg.enable_device(dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER)); pipe.start(cfg); pipelines.emplace_back(pipe); + // Map from each device's serial number to a different colorizer + colorizers[dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER)] = rs2::colorizer(); } - // We'll keep track for the last frame of each stream available to make the presentation persistent - std::map render_frames; - // Main app loop while (app) { @@ -46,10 +45,16 @@ int main(int argc, char * argv[]) try } } - // Convert the newly-arrived frames to render-firendly format + // We'll keep track for the last frame of each stream available to make the presentation persistent + std::map render_frames; + + // Convert the newly-arrived frames to render-friendly format for (const auto& frame : new_frames) { - render_frames[frame.get_profile().unique_id()] = colorizer.process(frame); + // Get the serial number of the current frame's device + auto serial = rs2::sensor_from_frame(frame)->get_info(RS2_CAMERA_INFO_SERIAL_NUMBER); + // Apply the colorizer of the matching device and store the colorized frame + render_frames[frame.get_profile().unique_id()] = colorizers[serial].process(frame); } // Present all the collected frames with openGl mosaic