Tutorial
Determining the connection state for a camera device is readily available to any Intel® RealSense™ application by directly querying the SenseManager. Once established, the connection state can further be monitored by subscribing to Intel® RealSense™ SDK callback.
Determine Connection State Directly
int main() try { auto pSession = PXCSession::CreateInstance(); auto pSenseManager = pSession->CreateSenseManager(); if (pSenseManager->Init() == PXC_STATUS_NO_ERROR) { if (pSenseManager->IsConnected()) { std::cout << "Camera is connected"<< endl; } else { std::cerr << "Please connect your camera"<< std::endl; throw("Camera not connected"); }
Please be advised that the IsConnected method is only valid between Init() and Close() functions.
Determine Connection State via Callbacks
Defining the Handler
The handler for the event is derived from the PXCCapture::Handler. Therein one must implement the OnDeviceListChanged() method.
class MyHandler : public PXCCapture::Handler { public: virtual void PXCAPI OnDeviceListChanged(void) { std::cerr << "Camera has been unplugged"<< std::endl; } }; MyHandler gHandler;
Registering the Handler
The next step involves registering the handler via the SubscribeCaptureCallbacks function. This registers the set of callback functions for any camera device events. In the above case, this maps to the OnDeviceListChanged method function, which is called when there is a change in the device list. Here it is possible to enumerate the device list via QueryDeviceInfo as per the Enumerating Modules and Camera Devices tutorial.
std::cout << "\nPXCSenseManager Initializing OK\n========================\n"; auto pCaptureMgr = pSenseManager->QueryCaptureManager(); auto pDevice = pCaptureMgr->QueryDevice(); PXCCapture::DeviceInfo deviceInfo; pDevice->QueryDeviceInfo(&deviceInfo); auto pCapture = pCaptureMgr->QueryCapture(); pCapture->SubscribeCaptureCallbacks(&gHandler); . . . if all fails, release resources . . .
Conclusion
It is possible to determine the camera device connection state statically by directly querying the SenseManager. You can also dynamically get the connection state by using the combination of the PXCCapture::SubscribeCaptureCallbacks() method and mapping to the OnDeviceListChanged() method of the derived as shown above.
Used in combination, you can get the static and dynamic camera device connection state at any time during the life cycle of application, and logic can be developed to counter changes in the connection state.
About the Author
Rudy Cazabon is a member of the Intel Software Innovator program and is an avid technologist in the area of graphics for games and computer vision.
Resources
Intel® RealSense™ SDK Documentation
A Developer’s Guide To Intel® RealSense™ Camera Detection Methods
Coding for 2 at Once - Intel RealSense User Facing Cameras
(Pending completion) Enumerating Modules and Camera Devices