-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMultiDevice.cpp
136 lines (117 loc) · 4.88 KB
/
MultiDevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Notes:
on the Arm/Linux platform ,this sample requires users to compile with Opencv4.2 or above,otherwise, it cannot be rendered.
*/
#include "window.hpp"
#include "libobsensor/ObSensor.hpp"
#include "libobsensor/hpp/Error.hpp"
#include <mutex>
const int maxDeviceCount = 9;
std::vector<std::shared_ptr<ob::Frame>> frames;
std::shared_ptr<ob::Frame> colorFrames[maxDeviceCount];
std::shared_ptr<ob::Frame> depthFrames[maxDeviceCount];
std::shared_ptr<ob::Frame> irFrames[maxDeviceCount];
std::mutex frameMutex;
void StartStream(std::vector<std::shared_ptr<ob::Pipeline>> pipes);
void StopStream(std::vector<std::shared_ptr<ob::Pipeline>> pipes);
int main(int argc, char **argv) try {
// Create a Context
ob::Context ctx;
// Query the list of connected devices
auto devList = ctx.queryDeviceList();
// Get the number of connected devices
int devCount = devList->deviceCount();
// traverse the device list and create a pipe
std::vector<std::shared_ptr<ob::Pipeline>> pipes;
for(int i = 0; i < devCount; i++) {
// Get the device and create the pipeline
auto dev = devList->getDevice(i);
auto pipe = std::make_shared<ob::Pipeline>(dev);
pipes.push_back(pipe);
}
// Open depth and color streams for all devices
StartStream(pipes);
// Create a window for rendering and set the resolution of the window
Window app("MultiDeviceViewer", 1280, 720, RENDER_GRID);
while(app) {
// Render a set of frame in the window, here will render the depth, color or infrared frames of all devices, RENDER_GRID
// means that all frames will be rendered in a grid arrangement
frames.clear();
{
std::lock_guard<std::mutex> lock(frameMutex);
int i = 0;
for(auto pipe: pipes) {
if(colorFrames[i] != nullptr) {
frames.emplace_back(colorFrames[i]);
}
if(depthFrames[i] != nullptr) {
frames.emplace_back(depthFrames[i]);
}
if(irFrames[i] != nullptr) {
frames.emplace_back(irFrames[i]);
}
i++;
}
}
app.addToRender(frames);
}
frames.clear();
StopStream(pipes);
return 0;
}
catch(ob::Error &e) {
std::cerr << "function:" << e.getName() << "\nargs:" << e.getArgs() << "\nmessage:" << e.getMessage() << "\ntype:" << e.getExceptionType() << std::endl;
exit(EXIT_FAILURE);
}
void StartStream(std::vector<std::shared_ptr<ob::Pipeline>> pipes) {
int i = 0;
for(auto &&pipe: pipes) {
std::shared_ptr<ob::Config> config = std::make_shared<ob::Config>();
// Get the depth camera configuration list
auto depthProfileList = pipe->getStreamProfileList(OB_SENSOR_DEPTH);
std::shared_ptr<ob::VideoStreamProfile> depthProfile = nullptr;
if(depthProfileList) {
// Open the default profile of Depth Sensor, which can be configured through the configuration file
depthProfile = std::const_pointer_cast<ob::StreamProfile>(depthProfileList->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
}
config->enableStream(depthProfile);
// Get the color camera configuration list
try {
auto colorProfileList = pipe->getStreamProfileList(OB_SENSOR_COLOR);
std::shared_ptr<ob::VideoStreamProfile> colorProfile = nullptr;
if(colorProfileList) {
// Open the default profile of Color Sensor, which can be configured through the configuration file
colorProfile = std::const_pointer_cast<ob::StreamProfile>(colorProfileList->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
}
config->enableStream(colorProfile);
}
catch(ob::Error &e) {
std::cerr << "Current device is not support color sensor!" << std::endl;
}
// Start the pipeline and pass in the configuration
pipe->start(config, [i](std::shared_ptr<ob::FrameSet> frameSet) {
std::lock_guard<std::mutex> lock(frameMutex);
if(frameSet->colorFrame()) {
colorFrames[i] = frameSet->colorFrame();
}
if(frameSet->depthFrame()) {
depthFrames[i] = frameSet->depthFrame();
}
});
i++;
}
}
void StopStream(std::vector<std::shared_ptr<ob::Pipeline>> pipes) {
int i = 0;
for(auto &&pipe: pipes) {
if(colorFrames[i])
colorFrames->reset();
if(depthFrames[i])
depthFrames->reset();
if(irFrames[i])
irFrames->reset();
// stop the pipeline
pipe->stop();
i++;
}
}