RM新时代网站

C++ 熱插拔示例代碼-Hot Plugin

# 熱拔插示例-HotPlugin

 

功能描述:本示例主要演示設備拔插回調的設置,以及拔插之后處理獲取到的流,并通過(guò)ESC_KEY鍵退出程序

> 本示例基于C++ High Level API進(jìn)行演示

 

首先需要創(chuàng )建一個(gè)Context,用于獲取設備信息列表和創(chuàng )建設備

ob::Context ctx;

 

注冊設備回調,分別在設備拔插的時(shí)候執行相關(guān)函數

ctx.setDeviceChangedCallback( []( std::shared_ptr< ob::DeviceList > removedList, std::shared_ptr< ob::DeviceList > addedList ) {
        DeviceDisconnectCallback( removedList );
        DeviceConnectCallback( addedList );
    } )

 

開(kāi)流

void startStream(std::shared_ptr<PipelineHolder> holder) {
std::shared_ptr<FramePrintInfo> printInfo(new FramePrintInfo{});
std::string                     deviceSN = std::string(holder->deviceInfo->serialNumber());
ob::FrameSetCallback            callback = [deviceSN, printInfo](std::shared_ptr<ob::FrameSet> frameSet) {
    // Get the depth data frame
    auto depthFrame = frameSet->depthFrame();
    if(depthFrame) {
        printInfo->depthCount++;
        if(printInfo->depthCount == 15) {
            std::cout << "=====Depth Frame Info====== SN: " << std::string(deviceSN) << ", " << depthFrame << std::endl;
            printInfo->depthCount = 0;
        }
    }

    // Get the ir data frame
    auto irFrame = frameSet->irFrame();
    if(irFrame) {
        printInfo->irCount++;
        if(printInfo->irCount == 15) {
            std::cout << "=====IR Frame Info====== SN: " << std::string(deviceSN) << ", " << std::dynamic_pointer_cast<ob::VideoFrame>(irFrame)
                        << std::endl;
            printInfo->irCount = 0;
        }
    }

    // Get the ir left data frame
    auto irLeftFrame = frameSet->getFrame(OB_FRAME_IR_LEFT);
    if(irLeftFrame) {
        printInfo->irLeftCount++;
        if(printInfo->irLeftCount == 15) {
            std::cout << "=====IR Left Frame Info====== SN: " << std::string(deviceSN) << ", " << std::dynamic_pointer_cast<ob::VideoFrame>(irLeftFrame)
                        << std::endl;
            printInfo->irLeftCount = 0;
        }
    }

    // Get the ir right data frame
    auto irRightFrame = frameSet->getFrame(OB_FRAME_IR_RIGHT);
    if(irRightFrame) {
        printInfo->irRightCount++;
        if(printInfo->irRightCount == 15) {
            std::cout << "=====IR Right Frame Info====== SN: " << std::string(deviceSN) << ", " << std::dynamic_pointer_cast<ob::VideoFrame>(irRightFrame)
                        << std::endl;
            printInfo->irRightCount = 0;
        }
    }

    // Get the color data frame
    auto colorFrame = frameSet->colorFrame();
    if(colorFrame) {
        printInfo->colorCount++;
        if(printInfo->colorCount == 15) {
            std::cout << "=====Color Frame Info====== SN: " << std::string(deviceSN) << ", " << std::dynamic_pointer_cast<ob::VideoFrame>(colorFrame)
                        << std::endl;
            printInfo->colorCount = 0;
        }
    }
};

// Start video stream according to the stream profile of the configuration file.If there is no configuration file, the first stream profile will be used.
try {
    std::cout << "startStream " << holder->deviceInfo << std::endl;
    holder->pipeline->start(nullptr, callback);
    holder->isStarted = true;
}
catch(...) {
    std::cout << "Pipeline start failed!" << std::endl;
    holder->isStarted = false;
}
}

 

 

 

 

停流

void stopStream(std::shared_ptr<PipelineHolder> holder) {
    if(!holder->isStarted) {
        return;
    }

    try {
        std::cout << "stopStream " << holder->deviceInfo << std::endl;
        holder->isStarted = false;
        holder->pipeline->stop();
    }
    catch(ob::Error &e) {
        std::cerr << "stopStream failed., function:" << e.getName() << "\nargs:" << e.getArgs() << "\nmessage:" << e.getMessage()
                  << "\ntype:" << e.getExceptionType() << std::endl;
    }
}

 

程序正常退出之后資源將會(huì )自動(dòng)釋放


RM新时代网站