From 469a18793a11b45ffa6f7ed3f873bddbae8e900d Mon Sep 17 00:00:00 2001 From: Christian Stromme Date: Mon, 21 Nov 2016 17:16:57 +0100 Subject: DirectShow: Add exposure control Makes it possible to manually control the shutter speed and/or the aperture on cameras that supports it. [ChangeLog][DirectShow] Added support for manual camera exposure control Change-Id: I340964f899fec365df870834b230c1d743ceb2e8 Reviewed-by: Andy Shaw --- src/plugins/directshow/camera/camera.pri | 6 +- .../camera/directshowcameraexposurecontrol.cpp | 411 +++++++++++++++++++++ .../camera/directshowcameraexposurecontrol.h | 100 +++++ src/plugins/directshow/camera/dscameraservice.cpp | 18 +- src/plugins/directshow/camera/dscameraservice.h | 4 +- 5 files changed, 530 insertions(+), 9 deletions(-) create mode 100644 src/plugins/directshow/camera/directshowcameraexposurecontrol.cpp create mode 100644 src/plugins/directshow/camera/directshowcameraexposurecontrol.h (limited to 'src/plugins/directshow') diff --git a/src/plugins/directshow/camera/camera.pri b/src/plugins/directshow/camera/camera.pri index d8ee59aa9..6ea69477b 100644 --- a/src/plugins/directshow/camera/camera.pri +++ b/src/plugins/directshow/camera/camera.pri @@ -11,7 +11,8 @@ HEADERS += \ $$PWD/dscamerasession.h \ $$PWD/directshowcameraglobal.h \ $$PWD/dscameraviewfindersettingscontrol.h \ - $$PWD/dscameraimageprocessingcontrol.h + $$PWD/dscameraimageprocessingcontrol.h \ + $$PWD/directshowcameraexposurecontrol.h SOURCES += \ $$PWD/dscameraservice.cpp \ @@ -21,7 +22,8 @@ SOURCES += \ $$PWD/dsimagecapturecontrol.cpp \ $$PWD/dscamerasession.cpp \ $$PWD/dscameraviewfindersettingscontrol.cpp \ - $$PWD/dscameraimageprocessingcontrol.cpp + $$PWD/dscameraimageprocessingcontrol.cpp \ + $$PWD/directshowcameraexposurecontrol.cpp *-msvc*:INCLUDEPATH += $$(DXSDK_DIR)/include QMAKE_USE += directshow diff --git a/src/plugins/directshow/camera/directshowcameraexposurecontrol.cpp b/src/plugins/directshow/camera/directshowcameraexposurecontrol.cpp new file mode 100644 index 000000000..7ece366ea --- /dev/null +++ b/src/plugins/directshow/camera/directshowcameraexposurecontrol.cpp @@ -0,0 +1,411 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "directshowcameraexposurecontrol.h" +#include "dscamerasession.h" +#include "directshowglobal.h" +#include "directshowutils.h" + +#include +#include + +QT_BEGIN_NAMESPACE + +static qreal convertToSec(long v) { return (v < 0) ? (1 / std::pow(2., qreal(v))) : std::pow(2., qreal(v)); } +static Q_DECL_CONSTEXPR qreal convertToFvalue(long v) { return qreal(v) / 10.; } + +DirectShowCameraExposureControl::DirectShowCameraExposureControl(DSCameraSession *session) + : m_session(session) + , m_shutterSpeedValues({ 0, 0, 0, 0, 0 }) + , m_apertureValues({ 0, 0, 0, 0, 0 }) + , m_requestedShutterSpeed(qreal(0.0)) + , m_currentShutterSpeed(qreal(-1.0)) + , m_requestedAperture(qreal(0.0)) + , m_currentAperture(qreal(-1.0)) + , m_requestedExposureMode(QCameraExposure::ExposureAuto) + , m_currentExposureMode(QCameraExposure::ExposureAuto) +{ + Q_ASSERT(m_session); + connect(m_session, &DSCameraSession::statusChanged, + this, &DirectShowCameraExposureControl::onStatusChanged); +} + +bool DirectShowCameraExposureControl::isParameterSupported(QCameraExposureControl::ExposureParameter parameter) const +{ + if (parameter == ShutterSpeed) + return (m_shutterSpeedValues.caps & CameraControl_Flags_Manual); + if (parameter == Aperture) + return (m_apertureValues.caps & CameraControl_Flags_Manual); + if (parameter == ExposureMode) + return true; + + return false; +} + +QVariantList DirectShowCameraExposureControl::supportedParameterRange(QCameraExposureControl::ExposureParameter parameter, + bool *continuous) const +{ + if (continuous) + *continuous = false; + + if (parameter == ShutterSpeed) + return m_supportedShutterSpeeds; + + if (parameter == Aperture) + return m_supportedApertureValues; + + if (parameter == ExposureMode) + return m_supportedExposureModes; + + return QVariantList(); +} + +QVariant DirectShowCameraExposureControl::requestedValue(QCameraExposureControl::ExposureParameter parameter) const +{ + if (parameter == ShutterSpeed) + return QVariant::fromValue(m_requestedShutterSpeed); + + if (parameter == Aperture) + return QVariant::fromValue(m_requestedAperture); + + if (parameter == ExposureMode) + return QVariant::fromValue(m_requestedExposureMode); + + return QVariant(); +} + +QVariant DirectShowCameraExposureControl::actualValue(QCameraExposureControl::ExposureParameter parameter) const +{ + if (parameter == ExposureMode) + return QVariant::fromValue(m_currentExposureMode); + + if (parameter == ShutterSpeed) { + return qFuzzyCompare(m_currentShutterSpeed, qreal(-1.0)) + ? QVariant() + : QVariant::fromValue(m_currentShutterSpeed); + } + + if (parameter == Aperture) { + return qFuzzyCompare(m_currentAperture, qreal(-1.0)) + ? QVariant() + : QVariant::fromValue(m_currentAperture); + } + + return QVariant(); +} + +bool DirectShowCameraExposureControl::setValue(QCameraExposureControl::ExposureParameter parameter, + const QVariant &value) +{ + IAMCameraControl *cameraControl = nullptr; + const DirectShowUtils::ScopedSafeRelease rControl { &cameraControl }; + if (!m_session->getCameraControlInterface(&cameraControl)) + return false; + + // Reset exposure mode if the value is invalid. + if (!value.isValid()) { + m_requestedExposureMode = QCameraExposure::ExposureAuto; + return setExposureMode(cameraControl, m_requestedExposureMode); + } + + if (parameter == ShutterSpeed || parameter == Aperture) { + bool ok = false; + const qreal newValue = value.toReal(&ok); + if (!ok) + return false; + + // Change the exposure mode first + setExposureMode(cameraControl, QCameraExposure::ExposureManual); + + if (parameter == ShutterSpeed) { + m_requestedShutterSpeed = newValue; + return setShutterSpeed(cameraControl, m_requestedShutterSpeed); + } else { + m_requestedAperture = newValue; + return setAperture(cameraControl, m_requestedAperture); + } + } + + if (parameter == ExposureMode) { + m_requestedExposureMode = value.value(); + return setExposureMode(cameraControl, m_requestedExposureMode); + } + + return false; +} + +void DirectShowCameraExposureControl::onStatusChanged(QCamera::Status status) +{ + const bool shouldUpdate = (qFuzzyCompare(m_currentAperture, qreal(-1.0)) && qFuzzyCompare(m_currentShutterSpeed, qreal(-1.0))); + + if (status == QCamera::LoadedStatus && shouldUpdate) + updateExposureSettings(); + + if (status == QCamera::UnloadedStatus) { + m_supportedApertureValues.clear(); + m_supportedExposureModes.clear(); + m_supportedShutterSpeeds.clear(); + m_currentAperture = qreal(-1.0); + m_currentShutterSpeed = qreal(-1.0); + m_currentExposureMode = QCameraExposure::ExposureAuto; + } +} + +void DirectShowCameraExposureControl::updateExposureSettings() +{ + IAMCameraControl *cameraControl = nullptr; + const DirectShowUtils::ScopedSafeRelease rControl { &cameraControl }; + if (!m_session->getCameraControlInterface(&cameraControl)) + return; + + const auto updateValues = [cameraControl](long property, + ExposureValues ¤tValues, + QVariantList ¶meterRange, + const std::function &converter, + bool *changed) -> bool { + ExposureValues values { 0, 0, 0, 0, 0 }; + if (FAILED(cameraControl->GetRange(property, + &values.minValue, + &values.maxValue, + &values.stepping, + &values.defaultValue, + &values.caps))) { + return false; + } + + const bool minValueChanged = values.minValue != currentValues.minValue; + const bool maxValueChanged = values.maxValue != currentValues.maxValue; + const bool steppingChanged = values.stepping != currentValues.stepping; + + if (minValueChanged || maxValueChanged || steppingChanged) { + parameterRange.clear(); + long nextValue = values.minValue; + while (nextValue != values.maxValue && values.stepping != 0) { + parameterRange << converter(nextValue); + nextValue += values.stepping; + } + + if (changed) + *changed = true; + } + + currentValues = values; + return true; + }; + + const auto getCurrentValue = [cameraControl](long property, const std::function &converter, qreal *value) -> bool { + long currentValue; + long currentFlags; + if (FAILED(cameraControl->Get(property, ¤tValue, ¤tFlags))) + return false; + + *value = converter(currentValue); + return true; + }; + + // Shutter speed + bool changed = false; + if (!updateValues(CameraControl_Exposure, m_shutterSpeedValues, m_supportedShutterSpeeds, convertToSec, &changed)) + qCDebug(qtDirectShowPlugin, "Unable to update the shutter speed values"); + + if (changed) + Q_EMIT parameterRangeChanged(int(ShutterSpeed)); + + if ((m_shutterSpeedValues.caps & CameraControl_Flags_Manual)) { + if (getCurrentValue(CameraControl_Exposure, convertToSec, &m_currentShutterSpeed)) { + if (m_currentExposureMode == QCameraExposure::ExposureManual) + setShutterSpeed(cameraControl, m_requestedShutterSpeed); + } else { + m_currentShutterSpeed = qreal(-1.0); + qCDebug(qtDirectShowPlugin, "Unable to get the current shutter speed!"); + } + } + + // Aperture + changed = false; + if (!updateValues(CameraControl_Iris, m_apertureValues, m_supportedApertureValues, convertToFvalue, &changed)) + qCDebug(qtDirectShowPlugin, "Unable to update the aperture values"); + + if (changed) + Q_EMIT parameterRangeChanged(int(Aperture)); + + if (getCurrentValue(CameraControl_Iris, convertToFvalue, &m_currentAperture)) { + if (m_currentExposureMode == QCameraExposure::ExposureManual) + setAperture(cameraControl, m_requestedAperture); + } else { + m_currentAperture = qreal(-1.0); + qCDebug(qtDirectShowPlugin, "Unable to get the current aperture value!"); + } + + // Update exposure modes + const bool hasAutoExposure = (m_apertureValues.caps & CameraControl_Flags_Auto) + || (m_shutterSpeedValues.caps & CameraControl_Flags_Auto); + const bool hasManualExposure = (m_apertureValues.caps & CameraControl_Flags_Manual) + || (m_shutterSpeedValues.caps & CameraControl_Flags_Manual); + + QVariantList exposureModes; + if (hasAutoExposure && !m_supportedExposureModes.contains(QVariant::fromValue(QCameraExposure::ExposureAuto))) + exposureModes << QVariant::fromValue(QCameraExposure::ExposureAuto); + + if (hasManualExposure && !m_supportedExposureModes.contains(QVariant::fromValue(QCameraExposure::ExposureManual))) + exposureModes << QVariant::fromValue(QCameraExposure::ExposureManual); + + if (!exposureModes.isEmpty() || !m_supportedExposureModes.isEmpty()) { + m_supportedExposureModes = exposureModes; + Q_EMIT parameterRangeChanged(int(ExposureMode)); + } +} + +bool DirectShowCameraExposureControl::setShutterSpeed(IAMCameraControl *cameraControl, qreal shutterSpeed) +{ + if (m_currentExposureMode != QCameraExposure::ExposureManual) { + qCDebug(qtDirectShowPlugin, "Trying to set shutter speed value while in auto exposure mode!"); + return false; + } + + if (qFuzzyCompare(m_currentShutterSpeed, shutterSpeed)) + return true; + + if ((m_shutterSpeedValues.caps & CameraControl_Flags_Manual) == 0) + return false; + + if (!m_supportedShutterSpeeds.contains(QVariant::fromValue(shutterSpeed))) + return false; + + if (qFuzzyIsNull(shutterSpeed) || (shutterSpeed < qreal(0.0))) + return false; + + const long newValue = long(log2(shutterSpeed)); + if (FAILED(cameraControl->Set(CameraControl_Exposure, newValue, CameraControl_Flags_Manual))) { + qCDebug(qtDirectShowPlugin, "Unable to set shutter speed value to: %d", int(shutterSpeed)); + return false; + } + + m_currentShutterSpeed = shutterSpeed; + Q_EMIT actualValueChanged(int(ShutterSpeed)); + return true; +} + +bool DirectShowCameraExposureControl::setAperture(IAMCameraControl *cameraControl, qreal aperture) +{ + if (m_currentExposureMode != QCameraExposure::ExposureManual) { + qCDebug(qtDirectShowPlugin, "Trying to set aperture value while in auto exposure mode!"); + return false; + } + + if (qFuzzyCompare(m_currentAperture, aperture)) + return true; + + if ((m_apertureValues.caps & CameraControl_Flags_Manual) == 0) + return false; + + if (!m_supportedApertureValues.contains(QVariant::fromValue(aperture))) + return false; + + if (aperture < qreal(0.0)) + return false; + + const long newValue = long(10 * aperture); + if (FAILED(cameraControl->Set(CameraControl_Iris, newValue, CameraControl_Flags_Manual))) { + qCDebug(qtDirectShowPlugin, "Unable to set aperture value to: %d", int(aperture)); + return false; + } + + m_currentAperture = aperture; + Q_EMIT actualValueChanged(int(Aperture)); + + return true; +} + +bool DirectShowCameraExposureControl::setExposureMode(IAMCameraControl *cameraControl, QCameraExposure::ExposureMode mode) +{ + if (m_currentExposureMode == mode) + return true; + + bool exposureModeChanged = true; + + // Set auto exposure mode + if (mode == QCameraExposure::ExposureAuto) { + if ((m_apertureValues.caps & CameraControl_Flags_Auto) + && FAILED(cameraControl->Set(CameraControl_Iris, 0, CameraControl_Flags_Auto))) { + qCDebug(qtDirectShowPlugin, "Setting auto exposure mode failed!"); + exposureModeChanged = false; + } + + if ((m_shutterSpeedValues.caps & CameraControl_Flags_Auto) + && FAILED(cameraControl->Set(CameraControl_Exposure, 0, CameraControl_Flags_Auto))) { + qCDebug(qtDirectShowPlugin, "Setting auto exposure mode failed"); + exposureModeChanged = false; + } + + if (exposureModeChanged) { + m_currentExposureMode = mode; + Q_EMIT actualValueChanged(int(ExposureMode)); + } + + return exposureModeChanged; + } + + // Change the current exposure mode to manual first. + m_currentExposureMode = QCameraExposure::ExposureManual; + + const qreal newShutterSpeed = qFuzzyCompare(m_requestedShutterSpeed, -1.0) + ? convertToSec(m_shutterSpeedValues.defaultValue) + : m_requestedShutterSpeed; + if ((m_shutterSpeedValues.caps & CameraControl_Flags_Manual)) + setShutterSpeed(cameraControl, newShutterSpeed); + + const qreal newAperture = qFuzzyCompare(m_requestedAperture, -1.0) + ? convertToFvalue(m_apertureValues.defaultValue) + : m_requestedAperture; + if ((m_apertureValues.caps & CameraControl_Flags_Manual)) + setAperture(cameraControl, newAperture); + + + // Check if any of the values changed. + exposureModeChanged = (qFuzzyCompare(m_currentShutterSpeed, newShutterSpeed) + || qFuzzyCompare(m_currentAperture, newAperture)); + + if (exposureModeChanged) + Q_EMIT actualValueChanged(int(ExposureMode)); + + return exposureModeChanged; +} + +QT_END_NAMESPACE diff --git a/src/plugins/directshow/camera/directshowcameraexposurecontrol.h b/src/plugins/directshow/camera/directshowcameraexposurecontrol.h new file mode 100644 index 000000000..db3fc5984 --- /dev/null +++ b/src/plugins/directshow/camera/directshowcameraexposurecontrol.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIRECTSHOWCAMERAEXPOSURECONTROL_H +#define DIRECTSHOWCAMERAEXPOSURECONTROL_H + +#include + +struct IAMCameraControl; + +QT_BEGIN_NAMESPACE + +class DSCameraSession; + +class DirectShowCameraExposureControl : public QCameraExposureControl +{ + Q_OBJECT +public: + DirectShowCameraExposureControl(DSCameraSession *session); + + bool isParameterSupported(ExposureParameter parameter) const override; + QVariantList supportedParameterRange(ExposureParameter parameter, bool *continuous) const override; + QVariant requestedValue(ExposureParameter parameter) const override; + QVariant actualValue(ExposureParameter parameter) const override; + bool setValue(ExposureParameter parameter, const QVariant &value) override; + +private Q_SLOTS: + void onStatusChanged(QCamera::Status status); + +private: + DSCameraSession *m_session; + + struct ExposureValues + { + long caps; + long minValue; + long maxValue; + long stepping; + long defaultValue; + } m_shutterSpeedValues, m_apertureValues; + + qreal m_requestedShutterSpeed; + qreal m_currentShutterSpeed; + + qreal m_requestedAperture; + qreal m_currentAperture; + + QVariantList m_supportedShutterSpeeds; + QVariantList m_supportedApertureValues; + QVariantList m_supportedExposureModes; + + QCameraExposure::ExposureMode m_requestedExposureMode; + QCameraExposure::ExposureMode m_currentExposureMode; + + void updateExposureSettings(); + + bool setShutterSpeed(IAMCameraControl *cameraControl, qreal shutterSpeed); + bool setAperture(IAMCameraControl *cameraControl, qreal aperture); + bool setExposureMode(IAMCameraControl *cameraControl, QCameraExposure::ExposureMode mode); +}; + +QT_END_NAMESPACE + +#endif // DIRECTSHOWCAMERAEXPOSURECONTROL_H diff --git a/src/plugins/directshow/camera/dscameraservice.cpp b/src/plugins/directshow/camera/dscameraservice.cpp index 836f1aaa5..c11111be1 100644 --- a/src/plugins/directshow/camera/dscameraservice.cpp +++ b/src/plugins/directshow/camera/dscameraservice.cpp @@ -48,19 +48,21 @@ #include "dsimagecapturecontrol.h" #include "dscameraviewfindersettingscontrol.h" #include "dscameraimageprocessingcontrol.h" +#include "directshowcameraexposurecontrol.h" QT_BEGIN_NAMESPACE DSCameraService::DSCameraService(QObject *parent): QMediaService(parent) + , m_session(new DSCameraSession(this)) + , m_control(new DSCameraControl(m_session)) + , m_videoDevice(new DSVideoDeviceControl(m_session)) , m_videoRenderer(0) + , m_imageCapture(new DSImageCaptureControl(m_session)) + , m_viewfinderSettings(new DSCameraViewfinderSettingsControl(m_session)) + , m_imageProcessingControl(new DSCameraImageProcessingControl(m_session)) + , m_exposureControl(new DirectShowCameraExposureControl(m_session)) { - m_session = new DSCameraSession(this); - m_control = new DSCameraControl(m_session); - m_videoDevice = new DSVideoDeviceControl(m_session); - m_imageCapture = new DSImageCaptureControl(m_session); - m_viewfinderSettings = new DSCameraViewfinderSettingsControl(m_session); - m_imageProcessingControl = new DSCameraImageProcessingControl(m_session); } DSCameraService::~DSCameraService() @@ -72,6 +74,7 @@ DSCameraService::~DSCameraService() delete m_videoRenderer; delete m_imageCapture; delete m_session; + delete m_exposureControl; } QMediaControl* DSCameraService::requestControl(const char *name) @@ -98,6 +101,9 @@ QMediaControl* DSCameraService::requestControl(const char *name) if (qstrcmp(name, QCameraImageProcessingControl_iid) == 0) return m_imageProcessingControl; + if (qstrcmp(name, QCameraExposureControl_iid) == 0) + return m_exposureControl; + return 0; } diff --git a/src/plugins/directshow/camera/dscameraservice.h b/src/plugins/directshow/camera/dscameraservice.h index 8976e41cf..12b07bd84 100644 --- a/src/plugins/directshow/camera/dscameraservice.h +++ b/src/plugins/directshow/camera/dscameraservice.h @@ -52,6 +52,7 @@ class DSVideoDeviceControl; class DSImageCaptureControl; class DSCameraViewfinderSettingsControl; class DSCameraImageProcessingControl; +class DirectShowCameraExposureControl; class DSCameraService : public QMediaService { @@ -65,13 +66,14 @@ public: virtual void releaseControl(QMediaControl *control); private: - DSCameraControl *m_control; DSCameraSession *m_session; + DSCameraControl *m_control; DSVideoDeviceControl *m_videoDevice; QMediaControl *m_videoRenderer; DSImageCaptureControl *m_imageCapture; DSCameraViewfinderSettingsControl *m_viewfinderSettings; DSCameraImageProcessingControl *m_imageProcessingControl; + DirectShowCameraExposureControl *m_exposureControl; }; QT_END_NAMESPACE -- cgit v1.2.1