Archived - How to Enumerate Modules and Camera Devices Using the Intel® RealSense™ SDK 2016 R1

The Intel® RealSense™ SDK has been discontinued. No ongoing support or updates will be available.

Abstract

Enumeration of the feature modules and multiple camera devices is an important component in the logic of an application for selecting the appropriate device. This tutorial presents a method for enumerating modules and multiple devices such that an appropriate selection can be made.

Tutorial

Finding the camera devices that are attached to a system along with the capabilities they provide can be simplified by enumerating the devices. The Intel® RealSense™ SDK 2016 R1 provides a mechanism via PXCSession::ImplDesc and PXCCapture::DeviceInfo that enables developers to get back information such as a device-friendly name, supported modules, and more.

This tutorial demonstrates the Intel RealSense SDK classes required for initialization and subsequent module and device enumeration.

Initialization

A usage example for initializing the core Intel RealSense SDK handles is implemented to enable creation of a PXCSession at any point in the application.

int main(int argc, char *argv[])
try
{
    PXCSession *pSession;
    PXCSession::ImplDesc *pDesc;
    PXCCapture *pCapture;
    PXCSenseManager *pSenseManager;
 
    // Initialize
    pSession = PXCSession::CreateInstance();
    pDesc = new PXCSession::ImplDesc();
 
    pDesc->group = PXCSession::ImplGroup::IMPL_GROUP_SENSOR;
    pDesc->subgroup = PXCSession::ImplSubgroup::IMPL_SUBGROUP_VIDEO_CAPTURE;

Enumeration

Enumeration of the modules and the devices is performed by iterating over the modules of PXCSession::ImplDesc and getting the friendly name, and then by iterating over the specific PXCCapture::DeviceInfo and querying the device. In this way it is possible to capture the modules and the device capabilities for each camera that is connected to the system.

        // Enumerate Devices
    std::string temp;
 
// iterating over the present modules
    for (int m = 0; ; m++)
    {
        PXCSession::ImplDesc desc2;
        if (pSession->QueryImpl(pDesc, m, &desc2) < pxcStatus::PXC_STATUS_NO_ERROR)
        {
            break;
        }
        //temp = format("Module[%d]:  %d", m, desc2.friendlyName);
        wstring ws(desc2.friendlyName); string str(ws.begin(), ws.end());
        std::cout << "Module[" << m << "]:  " << str.c_str() << std::endl;
 
        PXCCapture *pCap;
        pSession->CreateImpl<PXCCapture>(&desc2, &pCap);
 
        // interating over the devices
        for (int d = 0; ; d++)
        {
            PXCCapture::DeviceInfo dinfo;
            if (pCap->QueryDeviceInfo(d, &dinfo) < pxcStatus::PXC_STATUS_NO_ERROR)
            {
                break;
            };
            wstring ws(dinfo.name); string str(ws.begin(), ws.end());
            std::cout << "Device[" << d << "]:  " << str.c_str() << std::endl;
 
            /*wstring ws(dinfo.orientation);    string str(ws.begin(), ws.end());
            std::cout << "Device[" << d << "]:  " << str.c_str() << std::endl;
 
            wstring ws(dinfo.model);    string str(ws.begin(), ws.end());
            std::cout << "Device[" << d << "]:  " << str.c_str() << std::endl;*/
 
        }
}

Please note the outer loop required to iterate over the present module and the inner loop required to iterate over the attached devices.

Conclusion

Enumerating camera devices is an important step in any application that may require selecting a specific camera device when several cameras are attached to a system. This tutorial presented a straightforward camera enumeration scheme for developers who want to build any necessary selection logic once the particular camera device and capabilities are identified. A full usage example can be found in the Appendix 1 of this tutorial.

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

Appendix 1 – Sample source code

#include <windows.h>
 
#include <iostream>
#include <string>
#include <cstdio>
//
#include "pxcbase.h"
#include "pxcsensemanager.h"
#include "pxcmetadata.h"
#include "service/pxcsessionservice.h"
 
#include "pxccapture.h"
#include "pxccapturemanager.h"
 
using namespace std;
 
int main(int argc, char *argv[])
try
{
    PXCSession *pSession;
    PXCSession::ImplDesc *pDesc;
    PXCCapture *pCapture;
    PXCSenseManager *pSenseManager;
 
    // Initialize
    pSession = PXCSession::CreateInstance();
    pDesc = new PXCSession::ImplDesc();
 
    pDesc->group = PXCSession::ImplGroup::IMPL_GROUP_SENSOR;
    pDesc->subgroup = PXCSession::ImplSubgroup::IMPL_SUBGROUP_VIDEO_CAPTURE;
 
    // Enumerate Devices
    std::string temp;
 
    for (int m = 0; ; m++)
    {
        PXCSession::ImplDesc desc2;
        if (pSession->QueryImpl(pDesc, m, &desc2) < pxcStatus::PXC_STATUS_NO_ERROR)
        {
            break;
        }
        //temp = format("Module[%d]:  %d", m, desc2.friendlyName);
        wstring ws(desc2.friendlyName); string str(ws.begin(), ws.end());
        std::cout << "Module[" << m << "]:  " << str.c_str() << std::endl;
 
        PXCCapture *pCap;
        pSession->CreateImpl<PXCCapture>(&desc2, &pCap);
 
        // print out all device information
        for (int d = 0; ; d++)
        {
            PXCCapture::DeviceInfo dinfo;
            if (pCap->QueryDeviceInfo(d, &dinfo) < pxcStatus::PXC_STATUS_NO_ERROR)
            {
                break;
            };
            wstring ws(dinfo.name); string str(ws.begin(), ws.end());
            std::cout << "Device[" << d << "]:  " << str.c_str() << std::endl;
 
            /*wstring ws(dinfo.orientation);    string str(ws.begin(), ws.end());
            std::cout << "Device[" << d << "]:  " << str.c_str() << std::endl;
 
            wstring ws(dinfo.model);    string str(ws.begin(), ws.end());
            std::cout << "Device[" << d << "]:  " << str.c_str() << std::endl;*/
 
        }
    }
 
 
    cin.clear();
    cout << endl << "Press any key to continue...";
    cin.ignore();
 
    return 0;
}
catch (const char *c)
{
    std::cerr << "Program aborted: " << c << "\n";
    MessageBox(GetActiveWindow(), (LPCWSTR)c, L"FAIL", 0);
}
catch (std::exception e)
{
    std::cerr << "Program aborted: " << e.what() << "\n";
    MessageBox(GetActiveWindow(), (LPCWSTR)e.what(), L"FAIL", 0);
}
For more complete information about compiler optimizations, see our Optimization Notice.