Skip to content

Commit

Permalink
fix rs-multicam bug (the same colorizer was used for all devices, res…
Browse files Browse the repository at this point in the history
…ulting in new unique-id for each new frame, causing allocation of new textures and memory leak)
  • Loading branch information
AnnaRomanov committed Aug 7, 2019
1 parent aef6795 commit 57e3ee4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
17 changes: 10 additions & 7 deletions examples/multicam/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, rs2::colorizer> colorizers; // Declare map from device serial number to colorizer (utility class to convert depth data RGB colorspace)
std::vector<rs2::pipeline> pipelines;
std::vector<rs2::pipeline> 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
Expand Down Expand Up @@ -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);
}
```

Expand Down
21 changes: 13 additions & 8 deletions examples/multicam/rs-multicam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, rs2::colorizer> colorizers; // Declare map from device serial number to colorizer (utility class to convert depth data RGB colorspace)

std::vector<rs2::pipeline> pipelines;
std::vector<rs2::pipeline> pipelines;

// Start a streaming pipe per each connected device
for (auto&& dev : ctx.query_devices())
Expand All @@ -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<int, rs2::frame> render_frames;

// Main app loop
while (app)
{
Expand All @@ -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<int, rs2::frame> 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
Expand Down

0 comments on commit 57e3ee4

Please sign in to comment.