diff options
author | Timur Pocheptsov <Timur.Pocheptsov@digia.com> | 2015-03-31 09:20:50 +0200 |
---|---|---|
committer | Timur Pocheptsov <Timur.Pocheptsov@digia.com> | 2015-04-13 14:26:12 +0000 |
commit | 6187b72c51d0c05955d9d814baf8866b8e70fcfe (patch) | |
tree | 1498623c10c6eb6b7aafd34902dc6b876c0e0646 | |
parent | d9a988971f13b18450db16ef29d92b3bd399b7c5 (diff) | |
download | qtmultimedia-6187b72c51d0c05955d9d814baf8866b8e70fcfe.tar.gz |
Camera flash control - version for iOS/OS X
AVCaptureDevice has API to work with camera flash on both iOS and OS X
(quite limited on OS X though) - so camera flash control can be implemented
in AVFoundation plugin.
Task-number: QTBUG-37996
Change-Id: Ie9aaed09a709e7d09ccc1cedded93a69fea93975
Reviewed-by: Yoann Lopes <yoann.lopes@theqtcompany.com>
5 files changed, 317 insertions, 2 deletions
diff --git a/src/plugins/avfoundation/camera/avfcameraflashcontrol.h b/src/plugins/avfoundation/camera/avfcameraflashcontrol.h new file mode 100644 index 000000000..96722c6f4 --- /dev/null +++ b/src/plugins/avfoundation/camera/avfcameraflashcontrol.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef AVFCAMERAFLASHCONTROL_H +#define AVFCAMERAFLASHCONTROL_H + +#include <QtMultimedia/qcameraflashcontrol.h> +#include <QtMultimedia/qcamera.h> + +#include <QtCore/qlist.h> + +QT_BEGIN_NAMESPACE + +class AVFCameraService; +class AVFCameraSession; + +class AVFCameraFlashControl : public QCameraFlashControl +{ + Q_OBJECT +public: + AVFCameraFlashControl(AVFCameraService *service); + + QCameraExposure::FlashModes flashMode() const Q_DECL_OVERRIDE; + void setFlashMode(QCameraExposure::FlashModes mode) Q_DECL_OVERRIDE; + bool isFlashModeSupported(QCameraExposure::FlashModes mode) const Q_DECL_OVERRIDE; + bool isFlashReady() const Q_DECL_OVERRIDE; + +private Q_SLOTS: + void cameraStateChanged(QCamera::State newState); + +private: + bool applyFlashSettings(); + + AVFCameraService *m_service; + AVFCameraSession *m_session; + + // Set of bits: + QCameraExposure::FlashModes m_supportedModes; + // Only one bit set actually: + QCameraExposure::FlashModes m_flashMode; +}; + +QT_END_NAMESPACE + + +#endif // AVFCAMERAFLASHCONTROL_H + diff --git a/src/plugins/avfoundation/camera/avfcameraflashcontrol.mm b/src/plugins/avfoundation/camera/avfcameraflashcontrol.mm new file mode 100644 index 000000000..0eef95e92 --- /dev/null +++ b/src/plugins/avfoundation/camera/avfcameraflashcontrol.mm @@ -0,0 +1,227 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "avfcameraflashcontrol.h" +#include "avfcamerautility.h" +#include "avfcamerasession.h" +#include "avfcameraservice.h" +#include "avfcameradebug.h" + +#include <QtCore/qdebug.h> + +#include <AVFoundation/AVFoundation.h> + + +AVFCameraFlashControl::AVFCameraFlashControl(AVFCameraService *service) + : m_service(service) + , m_session(0) + , m_supportedModes(QCameraExposure::FlashOff) + , m_flashMode(QCameraExposure::FlashOff) +{ + Q_ASSERT(service); + m_session = m_service->session(); + Q_ASSERT(m_session); + + connect(m_session, SIGNAL(stateChanged(QCamera::State)), SLOT(cameraStateChanged(QCamera::State))); +} + +QCameraExposure::FlashModes AVFCameraFlashControl::flashMode() const +{ + return m_flashMode; +} + +void AVFCameraFlashControl::setFlashMode(QCameraExposure::FlashModes mode) +{ + if (m_flashMode == mode) + return; + + if (m_session->state() == QCamera::ActiveState && !isFlashModeSupported(mode)) { + qDebugCamera() << Q_FUNC_INFO << "unsupported mode" << mode; + return; + } + + m_flashMode = mode; + + if (m_session->state() != QCamera::ActiveState) + return; + + applyFlashSettings(); +} + +bool AVFCameraFlashControl::isFlashModeSupported(QCameraExposure::FlashModes mode) const +{ + // From what QCameraExposure has, we can support only these: + // FlashAuto = 0x1, + // FlashOff = 0x2, + // FlashOn = 0x4, + // AVCaptureDevice has these flash modes: + // AVCaptureFlashModeAuto + // AVCaptureFlashModeOff + // AVCaptureFlashModeOn + // QCameraExposure also has: + // FlashTorch = 0x20, --> "Constant light source." + // FlashVideoLight = 0x40. --> "Constant light source." + // AVCaptureDevice: + // AVCaptureTorchModeOff (no mapping) + // AVCaptureTorchModeOn --> FlashVideoLight + // AVCaptureTorchModeAuto (no mapping) + + return m_supportedModes & mode; +} + +bool AVFCameraFlashControl::isFlashReady() const +{ + if (m_session->state() != QCamera::ActiveState) + return false; + + AVCaptureDevice *captureDevice = m_session->videoCaptureDevice(); + if (!captureDevice) + return false; + + if (!captureDevice.hasFlash && !captureDevice.hasTorch) + return false; + + if (!isFlashModeSupported(m_flashMode)) + return false; + +#ifdef Q_OS_IOS + // AVCaptureDevice's docs: + // "The flash may become unavailable if, for example, + // the device overheats and needs to cool off." + if (m_flashMode != QCameraExposure::FlashVideoLight) + return [captureDevice isFlashAvailable]; + + return [captureDevice isTorchAvailable]; +#endif + + return true; +} + +void AVFCameraFlashControl::cameraStateChanged(QCamera::State newState) +{ + if (newState == QCamera::UnloadedState) { + m_supportedModes = QCameraExposure::FlashOff; + Q_EMIT flashReady(false); + } else if (newState == QCamera::ActiveState) { + m_supportedModes = QCameraExposure::FlashOff; + AVCaptureDevice *captureDevice = m_session->videoCaptureDevice(); + if (!captureDevice) { + qDebugCamera() << Q_FUNC_INFO << "no capture device in 'Active' state"; + Q_EMIT flashReady(false); + return; + } + + if (captureDevice.hasFlash) { + if ([captureDevice isFlashModeSupported:AVCaptureFlashModeOn]) + m_supportedModes |= QCameraExposure::FlashOn; + if ([captureDevice isFlashModeSupported:AVCaptureFlashModeAuto]) + m_supportedModes |= QCameraExposure::FlashAuto; + } + + if (captureDevice.hasTorch && [captureDevice isTorchModeSupported:AVCaptureTorchModeOn]) + m_supportedModes |= QCameraExposure::FlashVideoLight; + + Q_EMIT flashReady(applyFlashSettings()); + } +} + +bool AVFCameraFlashControl::applyFlashSettings() +{ + Q_ASSERT(m_session->state() == QCamera::ActiveState); + + AVCaptureDevice *captureDevice = m_session->videoCaptureDevice(); + if (!captureDevice) { + qDebugCamera() << Q_FUNC_INFO << "no capture device found"; + return false; + } + + if (!isFlashModeSupported(m_flashMode)) { + qDebugCamera() << Q_FUNC_INFO << "unsupported mode" << m_flashMode; + return false; + } + + if (!captureDevice.hasFlash && !captureDevice.hasTorch) { + // FlashOff is the only mode we support. + // Return false - flash is not ready. + return false; + } + + const AVFConfigurationLock lock(captureDevice); + + if (m_flashMode != QCameraExposure::FlashVideoLight) { + if (captureDevice.torchMode != AVCaptureTorchModeOff) { +#ifdef Q_OS_IOS + if (![captureDevice isTorchAvailable]) { + qDebugCamera() << Q_FUNC_INFO << "torch is not available at the moment"; + return false; + } +#endif + captureDevice.torchMode = AVCaptureTorchModeOff; + } +#ifdef Q_OS_IOS + if (![captureDevice isFlashAvailable]) { + // We'd like to switch flash (into some mode), but it's not available: + qDebugCamera() << Q_FUNC_INFO << "flash is not available at the moment"; + return false; + } +#endif + } else { + if (captureDevice.flashMode != AVCaptureFlashModeOff) { +#ifdef Q_OS_IOS + if (![captureDevice isFlashAvailable]) { + qDebugCamera() << Q_FUNC_INFO << "flash is not available at the moment"; + return false; + } +#endif + captureDevice.flashMode = AVCaptureFlashModeOff; + } + +#ifdef Q_OS_IOS + if (![captureDevice isTorchAvailable]) { + qDebugCamera() << Q_FUNC_INFO << "torch is not available at the moment"; + return false; + } +#endif + } + + if (m_flashMode == QCameraExposure::FlashOff) + captureDevice.flashMode = AVCaptureFlashModeOff; + else if (m_flashMode == QCameraExposure::FlashOn) + captureDevice.flashMode = AVCaptureFlashModeOn; + else if (m_flashMode == QCameraExposure::FlashAuto) + captureDevice.flashMode = AVCaptureFlashModeAuto; + else if (m_flashMode == QCameraExposure::FlashVideoLight) + captureDevice.torchMode = AVCaptureTorchModeOn; + + return true; +} diff --git a/src/plugins/avfoundation/camera/avfcameraservice.h b/src/plugins/avfoundation/camera/avfcameraservice.h index d4feada91..d557872a9 100644 --- a/src/plugins/avfoundation/camera/avfcameraservice.h +++ b/src/plugins/avfoundation/camera/avfcameraservice.h @@ -58,6 +58,7 @@ class AVFCameraZoomControl; class AVFCameraViewfinderSettingsControl2; class AVFCameraViewfinderSettingsControl; class AVFImageEncoderControl; +class AVFCameraFlashControl; class AVFCameraService : public QMediaService { @@ -83,6 +84,7 @@ public: AVFCameraViewfinderSettingsControl2 *viewfinderSettingsControl2() const {return m_viewfinderSettingsControl2; } AVFCameraViewfinderSettingsControl *viewfinderSettingsControl() const {return m_viewfinderSettingsControl; } AVFImageEncoderControl *imageEncoderControl() const {return m_imageEncoderControl; } + AVFCameraFlashControl *flashControl() const {return m_flashControl; } private: AVFCameraSession *m_session; @@ -100,6 +102,7 @@ private: AVFCameraViewfinderSettingsControl2 *m_viewfinderSettingsControl2; AVFCameraViewfinderSettingsControl *m_viewfinderSettingsControl; AVFImageEncoderControl *m_imageEncoderControl; + AVFCameraFlashControl *m_flashControl; }; QT_END_NAMESPACE diff --git a/src/plugins/avfoundation/camera/avfcameraservice.mm b/src/plugins/avfoundation/camera/avfcameraservice.mm index 0485abdcd..f163e1299 100644 --- a/src/plugins/avfoundation/camera/avfcameraservice.mm +++ b/src/plugins/avfoundation/camera/avfcameraservice.mm @@ -52,6 +52,7 @@ #include "avfcameraexposurecontrol.h" #include "avfcameraviewfindersettingscontrol.h" #include "avfimageencodercontrol.h" +#include "avfcameraflashcontrol.h" #ifdef Q_OS_IOS #include "avfcamerazoomcontrol.h" @@ -89,6 +90,7 @@ AVFCameraService::AVFCameraService(QObject *parent): m_viewfinderSettingsControl2 = new AVFCameraViewfinderSettingsControl2(this); m_viewfinderSettingsControl = new AVFCameraViewfinderSettingsControl(this); m_imageEncoderControl = new AVFImageEncoderControl(this); + m_flashControl = new AVFCameraFlashControl(this); } AVFCameraService::~AVFCameraService() @@ -115,6 +117,7 @@ AVFCameraService::~AVFCameraService() delete m_viewfinderSettingsControl2; delete m_viewfinderSettingsControl; delete m_imageEncoderControl; + delete m_flashControl; delete m_session; } @@ -158,6 +161,9 @@ QMediaControl *AVFCameraService::requestControl(const char *name) if (qstrcmp(name, QImageEncoderControl_iid) == 0) return m_imageEncoderControl; + if (qstrcmp(name, QCameraFlashControl_iid) == 0) + return m_flashControl; + if (qstrcmp(name,QMediaVideoProbeControl_iid) == 0) { AVFMediaVideoProbeControl *videoProbe = 0; videoProbe = new AVFMediaVideoProbeControl(this); diff --git a/src/plugins/avfoundation/camera/camera.pro b/src/plugins/avfoundation/camera/camera.pro index 729932eef..ac389df77 100644 --- a/src/plugins/avfoundation/camera/camera.pro +++ b/src/plugins/avfoundation/camera/camera.pro @@ -41,7 +41,8 @@ HEADERS += \ avfcameraexposurecontrol.h \ avfcamerautility.h \ avfcameraviewfindersettingscontrol.h \ - avfimageencodercontrol.h + avfimageencodercontrol.h \ + avfcameraflashcontrol.h OBJECTIVE_SOURCES += \ avfcameraserviceplugin.mm \ @@ -62,7 +63,8 @@ OBJECTIVE_SOURCES += \ avfcameraexposurecontrol.mm \ avfcamerautility.mm \ avfcameraviewfindersettingscontrol.mm \ - avfimageencodercontrol.mm + avfimageencodercontrol.mm \ + avfcameraflashcontrol.mm ios { |