uvcvideodevicecontrol.cpp 3.87 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#include "uvcvideodevicecontrol.h"

QT_BEGIN_NAMESPACE

Q_GLOBAL_STATIC(QList<UVCVideoDeviceInfo>, deviceList)

// this context may never get deleted. Is that a problem?
uvc_context_t* UVCVideoDeviceControl::ctx = NULL;

UVCVideoDeviceControl::UVCVideoDeviceControl(QObject *parent)
    : QVideoDeviceSelectorControl(parent)
{
    m_session = qobject_cast<UVCCameraSession*>(parent);
    selected = 0;
    updateDevices();
}

UVCVideoDeviceControl::~UVCVideoDeviceControl()
{
}

int UVCVideoDeviceControl::deviceCount() const
{
    updateDevices();
    return deviceList->count();
}

QString UVCVideoDeviceControl::deviceName(int index) const
{
    updateDevices();

    if (index >= 0 && index <= deviceList->count())
        return QString::fromUtf8(deviceList->at(index).first.constData());

    return QString();
}

QString UVCVideoDeviceControl::deviceDescription(int index) const
{
    updateDevices();

    if (index >= 0 && index <= deviceList->count())
        return deviceList->at(index).second;

    return QString();
}

int UVCVideoDeviceControl::defaultDevice() const
{
    return 0;
}

int UVCVideoDeviceControl::selectedDevice() const
{
    return selected;
}

void UVCVideoDeviceControl::setSelectedDevice(int index)
{
    updateDevices();

    if (index >= 0 && index < deviceList->count()) {
        if (m_session) {
            QString device = deviceList->at(index).first;

            uvc_device_t **devList;
            uvc_error_t res;
            res = uvc_get_device_list(ctx, &devList);
            if (res < 0) {
                qDebug() << "uvc_get_device_list" << uvc_strerror(res);
                return;
            }

            int idx = 0;
            while (1) {
                uvc_device_t *dev = devList[idx];
                idx++;
                if (dev == NULL)
                    break;
                uvc_device_descriptor_t *desc;
                res = uvc_get_device_descriptor(dev, &desc);
                if (res < 0) {
                    qDebug() << "uvc_get_device_descriptor" << uvc_strerror(res);
                    return;
                }
                bool selectedDevice = QString::number(desc->idProduct).compare(device) == 0;
                uvc_free_device_descriptor(desc);

                if (selectedDevice) {
                    m_session->setDevice(dev);
                    break;
                }
            }
            uvc_free_device_list(devList, 0);
        }
        selected = index;
    }
}

const QList<UVCVideoDeviceInfo> &UVCVideoDeviceControl::availableDevices()
{
    updateDevices();
    return *deviceList;
}

void UVCVideoDeviceControl::updateDevices()
{
    static QElapsedTimer timer;
    if (timer.isValid() && timer.elapsed() < 500) // ms
        return;

    deviceList->clear();

    uvc_error_t res;

    if (!ctx) {
        // Init libuvc
        qDebug() << "Initializing libuvc context...";
        res = uvc_init(&ctx, NULL);
        if (res < 0) {
            qDebug() << "uvc_init" << uvc_strerror(res);
            return;
        }
    }

    uvc_device_t **devList;
    res = uvc_get_device_list(ctx, &devList);
    if (res < 0) {
        qDebug() << "uvc_get_device_list" << uvc_strerror(res);
        return;
    }

    int idx = 0;
    while (1) {
        uvc_device_t *dev = devList[idx];
        idx++;
        if (dev == NULL)
            break;
        uvc_device_descriptor_t *desc;
        res = uvc_get_device_descriptor(dev, &desc);
        if (res < 0) {
            qDebug() << "uvc_get_device_descriptor" << uvc_strerror(res);
            break;
        }

        if (QString(desc->product).isEmpty())
            continue;

        UVCVideoDeviceInfo devInfo;

        devInfo.first = QString::number(desc->idProduct).toUtf8();
        devInfo.second = QString(desc->product).toUtf8();

        deviceList->append(devInfo);

        uvc_free_device_descriptor(desc);
    }

    uvc_free_device_list(devList, 0);

    timer.restart();
}