uvcvideodevicecontrol.cpp 2.78 KB
Newer Older
Thiago C. Santini's avatar
Thiago C. Santini committed
1 2 3 4 5 6
#include "uvcvideodevicecontrol.h"

QT_BEGIN_NAMESPACE

Q_GLOBAL_STATIC(QList<UVCVideoDeviceInfo>, deviceList)

7
bool UVCVideoDeviceControl::loaded = false;
Thiago C. Santini's avatar
Thiago C. Santini committed
8 9

UVCVideoDeviceControl::UVCVideoDeviceControl(QObject *parent)
10 11
	: QVideoDeviceSelectorControl(parent),
	  ctx(NULL),
12
      selected(0)
Thiago C. Santini's avatar
Thiago C. Santini committed
13
{
14 15 16 17 18 19 20
	m_session = qobject_cast<UVCCameraSession*>(parent);
	updateDevices();

	if (!loaded) {
		loaded = true;
		qInfo() << "Loaded uvcengine!";
	}
Thiago C. Santini's avatar
Thiago C. Santini committed
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
}

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;
70 71
			if ( m_session->setDevice(device) )
				selected = index;
Thiago C. Santini's avatar
Thiago C. Santini committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85
        }
    }
}

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

void UVCVideoDeviceControl::updateDevices()
{
    static QElapsedTimer timer;
    if (timer.isValid() && timer.elapsed() < 500) // ms
86
		return;
Thiago C. Santini's avatar
Thiago C. Santini committed
87 88 89

    deviceList->clear();

90 91 92 93 94 95 96 97
	uvc_error_t res;
	uvc_context_t *ctx;
	res = uvc_init(&ctx, NULL);
	if (res < 0) {
		qWarning() << "uvc_init" << uvc_strerror(res);
		return ;
	}
	uvc_device_t **devList;
Thiago C. Santini's avatar
Thiago C. Santini committed
98 99
    res = uvc_get_device_list(ctx, &devList);
    if (res < 0) {
100
        qWarning() << "uvc_get_device_list" << uvc_strerror(res);
Thiago C. Santini's avatar
Thiago C. Santini committed
101 102 103 104 105 106 107 108 109 110 111 112
        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) {
113
            qWarning() << "uvc_get_device_descriptor" << uvc_strerror(res);
Thiago C. Santini's avatar
Thiago C. Santini committed
114 115 116 117 118 119 120 121
            break;
        }

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

        UVCVideoDeviceInfo devInfo;

122
        devInfo.first = QString(desc->product).toUtf8();
Thiago C. Santini's avatar
Thiago C. Santini committed
123 124 125 126 127 128 129
        devInfo.second = QString(desc->product).toUtf8();

        deviceList->append(devInfo);

        uvc_free_device_descriptor(desc);
    }

130 131
	uvc_free_device_list(devList, 0);
	uvc_exit(ctx);
Thiago C. Santini's avatar
Thiago C. Santini committed
132 133 134

    timer.restart();
}