| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "qgstreamercameracontrol.h" | 
| 41 | #include "qgstreamerimageencode.h" | 
| 42 |  | 
| 43 | #include <QtCore/qdebug.h> | 
| 44 | #include <QtCore/qfile.h> | 
| 45 |  | 
| 46 |  | 
| 47 | QGstreamerCameraControl::QGstreamerCameraControl(QGstreamerCaptureSession *session) | 
| 48 |     :QCameraControl(session), | 
| 49 |     m_captureMode(QCamera::CaptureStillImage), | 
| 50 |     m_session(session), | 
| 51 |     m_state(QCamera::UnloadedState), | 
| 52 |     m_status(QCamera::UnloadedStatus), | 
| 53 |     m_reloadPending(false) | 
| 54 |  | 
| 55 | { | 
| 56 |     connect(sender: m_session, SIGNAL(stateChanged(QGstreamerCaptureSession::State)), | 
| 57 |             receiver: this, SLOT(updateStatus())); | 
| 58 |  | 
| 59 |     connect(asender: m_session->imageEncodeControl(), SIGNAL(settingsChanged()), | 
| 60 |             SLOT(reloadLater())); | 
| 61 |     connect(asender: m_session, SIGNAL(viewfinderChanged()), | 
| 62 |             SLOT(reloadLater())); | 
| 63 |     connect(asender: m_session, SIGNAL(readyChanged(bool)), | 
| 64 |             SLOT(reloadLater())); | 
| 65 |  | 
| 66 |     m_session->setCaptureMode(QGstreamerCaptureSession::Image); | 
| 67 | } | 
| 68 |  | 
| 69 | QGstreamerCameraControl::~QGstreamerCameraControl() | 
| 70 | { | 
| 71 | } | 
| 72 |  | 
| 73 | void QGstreamerCameraControl::setCaptureMode(QCamera::CaptureModes mode) | 
| 74 | { | 
| 75 |     if (m_captureMode == mode || !isCaptureModeSupported(mode)) | 
| 76 |         return; | 
| 77 |  | 
| 78 |     m_captureMode = mode; | 
| 79 |  | 
| 80 |     switch (mode) { | 
| 81 |     case QCamera::CaptureViewfinder: | 
| 82 |     case QCamera::CaptureStillImage: | 
| 83 |         m_session->setCaptureMode(QGstreamerCaptureSession::Image); | 
| 84 |         break; | 
| 85 |     case QCamera::CaptureVideo: | 
| 86 |         m_session->setCaptureMode(QGstreamerCaptureSession::AudioAndVideo); | 
| 87 |         break; | 
| 88 |     case QCamera::CaptureVideo | QCamera::CaptureStillImage: | 
| 89 |         m_session->setCaptureMode(QGstreamerCaptureSession::AudioAndVideoAndImage); | 
| 90 |         break; | 
| 91 |     } | 
| 92 |  | 
| 93 |     emit captureModeChanged(mode); | 
| 94 |     updateStatus(); | 
| 95 |     reloadLater(); | 
| 96 | } | 
| 97 |  | 
| 98 | bool QGstreamerCameraControl::isCaptureModeSupported(QCamera::CaptureModes mode) const | 
| 99 | { | 
| 100 |     //only CaptureStillImage and CaptureVideo bits are allowed | 
| 101 |     return (mode & (QCamera::CaptureStillImage | QCamera::CaptureVideo)) == mode; | 
| 102 | } | 
| 103 |  | 
| 104 | void QGstreamerCameraControl::setState(QCamera::State state) | 
| 105 | { | 
| 106 |     if (m_state == state) | 
| 107 |         return; | 
| 108 |  | 
| 109 |     m_state = state; | 
| 110 |     switch (state) { | 
| 111 |     case QCamera::UnloadedState: | 
| 112 |     case QCamera::LoadedState: | 
| 113 |         m_session->setState(QGstreamerCaptureSession::StoppedState); | 
| 114 |         break; | 
| 115 |     case QCamera::ActiveState: | 
| 116 |         //postpone changing to Active if the session is nor ready yet | 
| 117 |         if (m_session->isReady()) { | 
| 118 |             m_session->setState(QGstreamerCaptureSession::PreviewState); | 
| 119 |         } else { | 
| 120 | #ifdef CAMEABIN_DEBUG | 
| 121 |             qDebug() << "Camera session is not ready yet, postpone activating" ; | 
| 122 | #endif | 
| 123 |         } | 
| 124 |         break; | 
| 125 |     default: | 
| 126 |         emit error(error: QCamera::NotSupportedFeatureError, errorString: tr(s: "State not supported." )); | 
| 127 |     } | 
| 128 |  | 
| 129 |     updateStatus(); | 
| 130 |     emit stateChanged(m_state); | 
| 131 | } | 
| 132 |  | 
| 133 | QCamera::State QGstreamerCameraControl::state() const | 
| 134 | { | 
| 135 |     return m_state; | 
| 136 | } | 
| 137 |  | 
| 138 | void QGstreamerCameraControl::updateStatus() | 
| 139 | { | 
| 140 |     QCamera::Status oldStatus = m_status; | 
| 141 |  | 
| 142 |     switch (m_state) { | 
| 143 |     case QCamera::UnloadedState: | 
| 144 |         m_status = QCamera::UnloadedStatus; | 
| 145 |         break; | 
| 146 |     case QCamera::LoadedState: | 
| 147 |         m_status = QCamera::LoadedStatus; | 
| 148 |         break; | 
| 149 |     case QCamera::ActiveState: | 
| 150 |         if (m_session->state() == QGstreamerCaptureSession::StoppedState) | 
| 151 |             m_status = QCamera::StartingStatus; | 
| 152 |         else | 
| 153 |             m_status = QCamera::ActiveStatus; | 
| 154 |         break; | 
| 155 |     } | 
| 156 |  | 
| 157 |     if (oldStatus != m_status) { | 
| 158 |         //qDebug() << "Status changed:" << m_status; | 
| 159 |         emit statusChanged(m_status); | 
| 160 |     } | 
| 161 | } | 
| 162 |  | 
| 163 | void QGstreamerCameraControl::reloadLater() | 
| 164 | { | 
| 165 |     //qDebug() << "reload pipeline requested"; | 
| 166 |     if (!m_reloadPending && m_state == QCamera::ActiveState) { | 
| 167 |         m_reloadPending = true; | 
| 168 |         m_session->setState(QGstreamerCaptureSession::StoppedState); | 
| 169 |         QMetaObject::invokeMethod(obj: this, member: "reloadPipeline" , type: Qt::QueuedConnection); | 
| 170 |     } | 
| 171 | } | 
| 172 |  | 
| 173 | void QGstreamerCameraControl::reloadPipeline() | 
| 174 | { | 
| 175 |     //qDebug() << "reload pipeline"; | 
| 176 |     if (m_reloadPending) { | 
| 177 |         m_reloadPending = false; | 
| 178 |         if (m_state == QCamera::ActiveState && m_session->isReady()) { | 
| 179 |             m_session->setState(QGstreamerCaptureSession::PreviewState); | 
| 180 |         } | 
| 181 |     } | 
| 182 | } | 
| 183 |  | 
| 184 | bool QGstreamerCameraControl::canChangeProperty(PropertyChangeType changeType, QCamera::Status status) const | 
| 185 | { | 
| 186 |     Q_UNUSED(status); | 
| 187 |  | 
| 188 |     switch (changeType) { | 
| 189 |     case QCameraControl::CaptureMode: | 
| 190 |     case QCameraControl::ImageEncodingSettings: | 
| 191 |     case QCameraControl::VideoEncodingSettings: | 
| 192 |     case QCameraControl::Viewfinder: | 
| 193 |         return true; | 
| 194 |     default: | 
| 195 |         return false; | 
| 196 |     } | 
| 197 | } | 
| 198 |  |