diff options
author | Yoann Lopes <yoann.lopes@theqtcompany.com> | 2015-04-09 16:09:39 +0200 |
---|---|---|
committer | Yoann Lopes <yoann.lopes@theqtcompany.com> | 2015-04-14 14:49:56 +0000 |
commit | f9145aca166ad2ca1514524ce88ded7834eb207c (patch) | |
tree | 66e6acb2ae354361250c0b3228a6c3cdb0366cfe /src/plugins/directshow/camera/dsvideodevicecontrol.cpp | |
parent | 07606dde9a1f6b89f582ff8f1fbc53e2d4eb2c22 (diff) |
DirectShow: correctly update camera list.
8923c0ff fixed the list not being updated after plugging/unplugging a
camera from the system. However, it was only a partial fix affecting
only QCameraInfo::availableCameras(). DSVideoDeviceControl was still
internally keeping a list of cameras that was never updated, causing
the QCamera constructor to not take into account new or removed
cameras.
Change-Id: Ie5e79c46002017b1e85bfc53c6391a2a747361a0
Task-number: QTBUG-39708
Reviewed-by: Christian Stromme <christian.stromme@theqtcompany.com>
Diffstat (limited to 'src/plugins/directshow/camera/dsvideodevicecontrol.cpp')
-rw-r--r-- | src/plugins/directshow/camera/dsvideodevicecontrol.cpp | 72 |
1 files changed, 47 insertions, 25 deletions
diff --git a/src/plugins/directshow/camera/dsvideodevicecontrol.cpp b/src/plugins/directshow/camera/dsvideodevicecontrol.cpp index 869299457..c3d23e027 100644 --- a/src/plugins/directshow/camera/dsvideodevicecontrol.cpp +++ b/src/plugins/directshow/camera/dsvideodevicecontrol.cpp @@ -33,6 +33,7 @@ #include <QDebug> #include <QFile> +#include <qelapsedtimer.h> #include "dsvideodevicecontrol.h" #include "dscamerasession.h" @@ -48,33 +49,37 @@ extern const CLSID CLSID_VideoInputDeviceCategory; QT_BEGIN_NAMESPACE +Q_GLOBAL_STATIC(QList<DSVideoDeviceInfo>, deviceList) + DSVideoDeviceControl::DSVideoDeviceControl(QObject *parent) : QVideoDeviceSelectorControl(parent) { m_session = qobject_cast<DSCameraSession*>(parent); - - enumerateDevices(&m_devices, &m_descriptions); - selected = 0; } int DSVideoDeviceControl::deviceCount() const { - return m_devices.count(); + updateDevices(); + return deviceList->count(); } QString DSVideoDeviceControl::deviceName(int index) const { - if (index >= 0 && index <= m_devices.count()) - return QString::fromUtf8(m_devices.at(index).constData()); + updateDevices(); + + if (index >= 0 && index <= deviceList->count()) + return QString::fromUtf8(deviceList->at(index).first.constData()); return QString(); } QString DSVideoDeviceControl::deviceDescription(int index) const { - if (index >= 0 && index <= m_descriptions.count()) - return m_descriptions.at(index); + updateDevices(); + + if (index >= 0 && index <= deviceList->count()) + return deviceList->at(index).second; return QString(); } @@ -89,10 +94,34 @@ int DSVideoDeviceControl::selectedDevice() const return selected; } -void DSVideoDeviceControl::enumerateDevices(QList<QByteArray> *devices, QStringList *descriptions) +void DSVideoDeviceControl::setSelectedDevice(int index) { - devices->clear(); - descriptions->clear(); + updateDevices(); + + if (index >= 0 && index < deviceList->count()) { + if (m_session) { + QString device = deviceList->at(index).first; + if (device.startsWith("ds:")) + device.remove(0,3); + m_session->setDevice(device); + } + selected = index; + } +} + +const QList<DSVideoDeviceInfo> &DSVideoDeviceControl::availableDevices() +{ + updateDevices(); + return *deviceList; +} + +void DSVideoDeviceControl::updateDevices() +{ + static QElapsedTimer timer; + if (timer.isValid() && timer.elapsed() < 500) // ms + return; + + deviceList->clear(); ICreateDevEnum* pDevEnum = NULL; IEnumMoniker* pEnum = NULL; @@ -116,7 +145,9 @@ void DSVideoDeviceControl::enumerateDevices(QList<QByteArray> *devices, QStringL if (SUCCEEDED(hr)) { QString output(QString::fromWCharArray(strName)); mallocInterface->Free(strName); - devices->append(output.toUtf8().constData()); + + DSVideoDeviceInfo devInfo; + devInfo.first = output.toUtf8(); IPropertyBag *pPropBag; hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)(&pPropBag)); @@ -130,7 +161,9 @@ void DSVideoDeviceControl::enumerateDevices(QList<QByteArray> *devices, QStringL } pPropBag->Release(); } - descriptions->append(output); + devInfo.second = output; + + deviceList->append(devInfo); } pMoniker->Release(); } @@ -139,19 +172,8 @@ void DSVideoDeviceControl::enumerateDevices(QList<QByteArray> *devices, QStringL } pDevEnum->Release(); } -} -void DSVideoDeviceControl::setSelectedDevice(int index) -{ - if (index >= 0 && index < m_devices.count()) { - if (m_session) { - QString device = m_devices.at(index); - if (device.startsWith("ds:")) - device.remove(0,3); - m_session->setDevice(device); - } - selected = index; - } + timer.restart(); } QT_END_NAMESPACE |