# 多設(shè)備示例-MultiDevice
功能描述:本示例主要演示了對多設(shè)備進(jìn)行操作。
> 本示例基于C++ High Level API進(jìn)行演示
首先需要創(chuàng)建一個Context,獲取設(shè)備信息列表用于創(chuàng)建設(shè)備及Pipeline
ob::Context ctx;
//查詢已經(jīng)接入設(shè)備的列表
auto devList = ctx.queryDeviceList();
//獲取接入設(shè)備的數(shù)量
int devCount = devList->deviceCount();
創(chuàng)建設(shè)備,并通過設(shè)備創(chuàng)建Pipeline
//遍歷設(shè)備列表并創(chuàng)建設(shè)備
std::vector<std::shared_ptr<ob::Device>> devices;
for (int i = 0; i < devCount; i++)
{
//獲取設(shè)備并創(chuàng)建pipeline
auto dev = devList->getDevice( i );
auto pipe = std::make_shared< ob::Pipeline >( dev );
pipes.push_back( pipe );
打開多個設(shè)備的深度和彩色流
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++;
}
}
停止所有設(shè)備已打開的流
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();
//停止pipeline
pipe->stop();
i++;
}
}
程序正常退出之后資源將會自動釋放
預(yù)期輸出: