From 33b27c3c15aaca611d5ed0164afa3cd2e9f891a7 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Thu, 12 Feb 2015 10:17:09 +0100 Subject: Image encoder control - version for OS X/iOS QImageEncoderControl - implementation for AVFoundation plugin (OS X/iOS, at the moment iOS >= 7.0). Change-Id: Ibc2c3ae48252dd4698e263f5abca5c328482d5e7 Reviewed-by: Yoann Lopes --- src/plugins/avfoundation/camera/avfcameraservice.h | 3 + .../avfoundation/camera/avfcameraservice.mm | 6 + src/plugins/avfoundation/camera/avfcamerasession.h | 1 + .../avfoundation/camera/avfcamerasession.mm | 25 +- .../avfoundation/camera/avfimagecapturecontrol.h | 1 + .../avfoundation/camera/avfimageencodercontrol.h | 77 ++++++ .../avfoundation/camera/avfimageencodercontrol.mm | 273 +++++++++++++++++++++ src/plugins/avfoundation/camera/camera.pro | 6 +- 8 files changed, 386 insertions(+), 6 deletions(-) create mode 100644 src/plugins/avfoundation/camera/avfimageencodercontrol.h create mode 100644 src/plugins/avfoundation/camera/avfimageencodercontrol.mm diff --git a/src/plugins/avfoundation/camera/avfcameraservice.h b/src/plugins/avfoundation/camera/avfcameraservice.h index babee3dcb..d4feada91 100644 --- a/src/plugins/avfoundation/camera/avfcameraservice.h +++ b/src/plugins/avfoundation/camera/avfcameraservice.h @@ -57,6 +57,7 @@ class AVFCameraExposureControl; class AVFCameraZoomControl; class AVFCameraViewfinderSettingsControl2; class AVFCameraViewfinderSettingsControl; +class AVFImageEncoderControl; class AVFCameraService : public QMediaService { @@ -81,6 +82,7 @@ public: AVFCameraRendererControl *videoOutput() const {return m_videoOutput; } AVFCameraViewfinderSettingsControl2 *viewfinderSettingsControl2() const {return m_viewfinderSettingsControl2; } AVFCameraViewfinderSettingsControl *viewfinderSettingsControl() const {return m_viewfinderSettingsControl; } + AVFImageEncoderControl *imageEncoderControl() const {return m_imageEncoderControl; } private: AVFCameraSession *m_session; @@ -97,6 +99,7 @@ private: AVFCameraZoomControl *m_cameraZoomControl; AVFCameraViewfinderSettingsControl2 *m_viewfinderSettingsControl2; AVFCameraViewfinderSettingsControl *m_viewfinderSettingsControl; + AVFImageEncoderControl *m_imageEncoderControl; }; QT_END_NAMESPACE diff --git a/src/plugins/avfoundation/camera/avfcameraservice.mm b/src/plugins/avfoundation/camera/avfcameraservice.mm index f9c7e5ae6..0485abdcd 100644 --- a/src/plugins/avfoundation/camera/avfcameraservice.mm +++ b/src/plugins/avfoundation/camera/avfcameraservice.mm @@ -51,6 +51,7 @@ #include "avfcamerafocuscontrol.h" #include "avfcameraexposurecontrol.h" #include "avfcameraviewfindersettingscontrol.h" +#include "avfimageencodercontrol.h" #ifdef Q_OS_IOS #include "avfcamerazoomcontrol.h" @@ -87,6 +88,7 @@ AVFCameraService::AVFCameraService(QObject *parent): #endif m_viewfinderSettingsControl2 = new AVFCameraViewfinderSettingsControl2(this); m_viewfinderSettingsControl = new AVFCameraViewfinderSettingsControl(this); + m_imageEncoderControl = new AVFImageEncoderControl(this); } AVFCameraService::~AVFCameraService() @@ -112,6 +114,7 @@ AVFCameraService::~AVFCameraService() #endif delete m_viewfinderSettingsControl2; delete m_viewfinderSettingsControl; + delete m_imageEncoderControl; delete m_session; } @@ -152,6 +155,9 @@ QMediaControl *AVFCameraService::requestControl(const char *name) if (qstrcmp(name, QCameraViewfinderSettingsControl_iid) == 0) return m_viewfinderSettingsControl; + if (qstrcmp(name, QImageEncoderControl_iid) == 0) + return m_imageEncoderControl; + if (qstrcmp(name,QMediaVideoProbeControl_iid) == 0) { AVFMediaVideoProbeControl *videoProbe = 0; videoProbe = new AVFMediaVideoProbeControl(this); diff --git a/src/plugins/avfoundation/camera/avfcamerasession.h b/src/plugins/avfoundation/camera/avfcamerasession.h index c5bae660c..4f418cb1d 100644 --- a/src/plugins/avfoundation/camera/avfcamerasession.h +++ b/src/plugins/avfoundation/camera/avfcamerasession.h @@ -98,6 +98,7 @@ Q_SIGNALS: private: static void updateCameraDevices(); void attachInputDevices(); + void applyImageEncoderSettings(); void applyViewfinderSettings(); static QByteArray m_defaultCameraDevice; diff --git a/src/plugins/avfoundation/camera/avfcamerasession.mm b/src/plugins/avfoundation/camera/avfcamerasession.mm index 5570aa83b..98fbb9865 100644 --- a/src/plugins/avfoundation/camera/avfcamerasession.mm +++ b/src/plugins/avfoundation/camera/avfcamerasession.mm @@ -40,6 +40,7 @@ #include "avfaudioinputselectorcontrol.h" #include "avfmediavideoprobecontrol.h" #include "avfcameraviewfindersettingscontrol.h" +#include "avfimageencodercontrol.h" #include #include @@ -276,6 +277,7 @@ void AVFCameraSession::setState(QCamera::State newState) Q_EMIT readyToConfigureConnections(); [m_captureSession commitConfiguration]; [m_captureSession startRunning]; + applyImageEncoderSettings(); applyViewfinderSettings(); } @@ -366,12 +368,27 @@ void AVFCameraSession::attachInputDevices() } } +void AVFCameraSession::applyImageEncoderSettings() +{ + if (AVFImageEncoderControl *control = m_service->imageEncoderControl()) + control->applySettings(); +} + void AVFCameraSession::applyViewfinderSettings() { - if (AVFCameraViewfinderSettingsControl2 *control = m_service->viewfinderSettingsControl2()) { - QCameraViewfinderSettings settings(control->requestedSettings()); - // TODO: Adjust the resolution (from image encoder control), updating 'settings'. - control->setViewfinderSettings(settings); + if (AVFCameraViewfinderSettingsControl2 *vfControl = m_service->viewfinderSettingsControl2()) { + QCameraViewfinderSettings vfSettings(vfControl->requestedSettings()); + if (AVFImageEncoderControl *imControl = m_service->imageEncoderControl()) { + const QSize imageResolution(imControl->imageSettings().resolution()); + if (!imageResolution.isNull() && imageResolution.isValid()) { + vfSettings.setResolution(imageResolution); + vfControl->setViewfinderSettings(vfSettings); + return; + } + } + + if (!vfSettings.isNull()) + vfControl->applySettings(); } } diff --git a/src/plugins/avfoundation/camera/avfimagecapturecontrol.h b/src/plugins/avfoundation/camera/avfimagecapturecontrol.h index c4cb2fa57..c27abd39b 100644 --- a/src/plugins/avfoundation/camera/avfimagecapturecontrol.h +++ b/src/plugins/avfoundation/camera/avfimagecapturecontrol.h @@ -56,6 +56,7 @@ public: QCameraImageCapture::DriveMode driveMode() const { return QCameraImageCapture::SingleImageCapture; } void setDriveMode(QCameraImageCapture::DriveMode ) {} + AVCaptureStillImageOutput *stillImageOutput() const {return m_stillImageOutput;} int capture(const QString &fileName); void cancelCapture(); diff --git a/src/plugins/avfoundation/camera/avfimageencodercontrol.h b/src/plugins/avfoundation/camera/avfimageencodercontrol.h new file mode 100644 index 000000000..fcb665a03 --- /dev/null +++ b/src/plugins/avfoundation/camera/avfimageencodercontrol.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 AVFIMAGEENCODERCONTROL_H +#define AVFIMAGEENCODERCONTROL_H + +#include +#include + +#include +#include +#include + +@class AVCaptureDeviceFormat; + +QT_BEGIN_NAMESPACE + +class AVFCameraService; + +class AVFImageEncoderControl : public QImageEncoderControl +{ + Q_OBJECT + + friend class AVFCameraSession; +public: + AVFImageEncoderControl(AVFCameraService *service); + + QStringList supportedImageCodecs() const Q_DECL_OVERRIDE; + QString imageCodecDescription(const QString &codecName) const Q_DECL_OVERRIDE; + QList supportedResolutions(const QImageEncoderSettings &settings, + bool *continuous) const Q_DECL_OVERRIDE; + QImageEncoderSettings imageSettings() const Q_DECL_OVERRIDE; + void setImageSettings(const QImageEncoderSettings &settings) Q_DECL_OVERRIDE; + +private: + AVFCameraService *m_service; + QImageEncoderSettings m_settings; + + void applySettings(); + bool videoCaptureDeviceIsValid() const; +}; + +QSize qt_image_high_resolution(AVCaptureDeviceFormat *fomat); + +QT_END_NAMESPACE + +#endif diff --git a/src/plugins/avfoundation/camera/avfimageencodercontrol.mm b/src/plugins/avfoundation/camera/avfimageencodercontrol.mm new file mode 100644 index 000000000..ea25665eb --- /dev/null +++ b/src/plugins/avfoundation/camera/avfimageencodercontrol.mm @@ -0,0 +1,273 @@ +/**************************************************************************** +** +** 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 "avfcameraviewfindersettingscontrol.h" +#include "avfimageencodercontrol.h" +#include "avfimagecapturecontrol.h" +#include "avfcamerautility.h" +#include "avfcamerasession.h" +#include "avfcameraservice.h" +#include "avfcameradebug.h" + +#include + +#include +#include + +#include + +QT_BEGIN_NAMESPACE + +QSize qt_image_high_resolution(AVCaptureDeviceFormat *format) +{ + Q_ASSERT(format); + QSize res; +#if defined(Q_OS_IOS) && QT_IOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__IPHONE_8_0) + if (QSysInfo::MacintoshVersion >= QSysInfo::MV_IOS_8_0) { + const CMVideoDimensions hrDim(format.highResolutionStillImageDimensions); + res.setWidth(hrDim.width); + res.setHeight(hrDim.height); + } +#endif + return res; +} + +AVFImageEncoderControl::AVFImageEncoderControl(AVFCameraService *service) + : m_service(service) +{ + Q_ASSERT(service); +} + +QStringList AVFImageEncoderControl::supportedImageCodecs() const +{ + return QStringList() << QLatin1String("jpeg"); +} + +QString AVFImageEncoderControl::imageCodecDescription(const QString &codecName) const +{ + if (codecName == QLatin1String("jpeg")) + return tr("JPEG image"); + + return QString(); +} + +QList AVFImageEncoderControl::supportedResolutions(const QImageEncoderSettings &settings, + bool *continuous) const +{ + Q_UNUSED(settings) + + QList resolutions; + + if (!videoCaptureDeviceIsValid()) + return resolutions; + +#if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_7, __IPHONE_7_0) + if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_7, QSysInfo::MV_IOS_7_0)) { + AVCaptureDevice *captureDevice = m_service->session()->videoCaptureDevice(); + for (AVCaptureDeviceFormat *format in captureDevice.formats) { + if (qt_is_video_range_subtype(format)) + continue; + + const QSize res(qt_device_format_resolution(format)); + if (!res.isNull() && res.isValid()) + resolutions << res; +#if defined(Q_OS_IOS) && QT_IOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__IPHONE_8_0) + if (QSysInfo::MacintoshVersion >= QSysInfo::MV_IOS_8_0) { + // From Apple's docs (iOS): + // By default, AVCaptureStillImageOutput emits images with the same dimensions as + // its source AVCaptureDevice instance’s activeFormat.formatDescription. However, + // if you set this property to YES, the receiver emits still images at the capture + // device’s highResolutionStillImageDimensions value. + const QSize hrRes(qt_image_high_resolution(format)); + if (!hrRes.isNull() && hrRes.isValid()) + resolutions << res; + } +#endif + } + } else { +#else + { +#endif + // TODO: resolutions without AVCaptureDeviceFormat ... + } + + if (continuous) + *continuous = false; + + return resolutions; +} + +QImageEncoderSettings AVFImageEncoderControl::imageSettings() const +{ + QImageEncoderSettings settings; + + if (!videoCaptureDeviceIsValid()) + return settings; + +#if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_7, __IPHONE_7_0) + if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_7, QSysInfo::MV_IOS_7_0)) { + AVCaptureDevice *captureDevice = m_service->session()->videoCaptureDevice(); + if (!captureDevice.activeFormat) { + qDebugCamera() << Q_FUNC_INFO << "no active format"; + return settings; + } + + QSize res(qt_device_format_resolution(captureDevice.activeFormat)); +#if defined(Q_OS_IOS) && QT_IOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__IPHONE_8_0) + if (QSysInfo::MacintoshVersion >= QSysInfo::MV_IOS_8_0) { + if (!m_service->imageCaptureControl() || !m_service->imageCaptureControl()->stillImageOutput()) { + qDebugCamera() << Q_FUNC_INFO << "no still image output"; + return settings; + } + + AVCaptureStillImageOutput *stillImageOutput = m_service->imageCaptureControl()->stillImageOutput(); + if (stillImageOutput.highResolutionStillImageOutputEnabled) + res = qt_image_high_resolution(captureDevice.activeFormat); + } +#endif + if (res.isNull() || !res.isValid()) { + qDebugCamera() << Q_FUNC_INFO << "failed to exctract the image resolution"; + return settings; + } + + settings.setResolution(res); + } else { +#else + { +#endif + // TODO: resolution without AVCaptureDeviceFormat. + } + + settings.setCodec(QLatin1String("jpeg")); + + return settings; +} + +void AVFImageEncoderControl::setImageSettings(const QImageEncoderSettings &settings) +{ + if (m_settings == settings || settings.isNull()) + return; + + m_settings = settings; + + applySettings(); +} + +void AVFImageEncoderControl::applySettings() +{ + if (!videoCaptureDeviceIsValid()) + return; + + AVFCameraSession *session = m_service->session(); + if (!session || (session->state() != QCamera::ActiveState + && session->state() != QCamera::LoadedState)) { + return; + } + + if (!m_service->imageCaptureControl() + || !m_service->imageCaptureControl()->stillImageOutput()) { + qDebugCamera() << Q_FUNC_INFO << "no still image output"; + return; + } + + if (m_settings.codec().size() + && m_settings.codec() != QLatin1String("jpeg")) { + qDebugCamera() << Q_FUNC_INFO << "unsupported codec:" << m_settings.codec(); + return; + } + + QSize res(m_settings.resolution()); + if (res.isNull()) { + qDebugCamera() << Q_FUNC_INFO << "invalid resolution:" << res; + return; + } + + if (!res.isValid()) { + // Invalid == default value. + // Here we could choose the best format available, but + // activeFormat is already equal to 'preset high' by default, + // which is good enough, otherwise we can end in some format with low framerates. + return; + } + +#if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_7, __IPHONE_7_0) + if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_7, QSysInfo::MV_IOS_7_0)) { + AVCaptureDevice *captureDevice = m_service->session()->videoCaptureDevice(); + AVCaptureDeviceFormat *match = qt_find_best_resolution_match(captureDevice, res); + + if (!match) { + qDebugCamera() << Q_FUNC_INFO << "unsupported resolution:" << res; + return; + } + + if (match != captureDevice.activeFormat) { + const AVFConfigurationLock lock(captureDevice); + if (!lock) { + qDebugCamera() << Q_FUNC_INFO << "failed to lock for configuration"; + return; + } + captureDevice.activeFormat = match; + } + +#if defined(Q_OS_IOS) && QT_IOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__IPHONE_8_0) + if (QSysInfo::MacintoshVersion >= QSysInfo::MV_IOS_8_0) { + AVCaptureStillImageOutput *imageOutput = m_service->imageCaptureControl()->stillImageOutput(); + if (res == qt_image_high_resolution(captureDevice.activeFormat)) + imageOutput.highResolutionStillImageOutputEnabled = YES; + else + imageOutput.highResolutionStillImageOutputEnabled = NO; + } +#endif + } else { +#else + { +#endif + // TODO: resolution without capture device format ... + } +} + +bool AVFImageEncoderControl::videoCaptureDeviceIsValid() const +{ + if (!m_service->session() || !m_service->session()->videoCaptureDevice()) + return false; + + AVCaptureDevice *captureDevice = m_service->session()->videoCaptureDevice(); + if (!captureDevice.formats || !captureDevice.formats.count) + return false; + + return true; +} + +QT_END_NAMESPACE + +#include "moc_avfimageencodercontrol.cpp" diff --git a/src/plugins/avfoundation/camera/camera.pro b/src/plugins/avfoundation/camera/camera.pro index 19e3e631c..729932eef 100644 --- a/src/plugins/avfoundation/camera/camera.pro +++ b/src/plugins/avfoundation/camera/camera.pro @@ -40,7 +40,8 @@ HEADERS += \ avfcamerafocuscontrol.h \ avfcameraexposurecontrol.h \ avfcamerautility.h \ - avfcameraviewfindersettingscontrol.h + avfcameraviewfindersettingscontrol.h \ + avfimageencodercontrol.h OBJECTIVE_SOURCES += \ avfcameraserviceplugin.mm \ @@ -60,7 +61,8 @@ OBJECTIVE_SOURCES += \ avfcamerafocuscontrol.mm \ avfcameraexposurecontrol.mm \ avfcamerautility.mm \ - avfcameraviewfindersettingscontrol.mm + avfcameraviewfindersettingscontrol.mm \ + avfimageencodercontrol.mm ios { -- cgit v1.2.3