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 "camerabinrecorder.h"
41#include "camerabincontrol.h"
42#include "camerabinresourcepolicy.h"
43#include "camerabinaudioencoder.h"
44#include "camerabinvideoencoder.h"
45#include "camerabincontainer.h"
46#include <QtCore/QDebug>
47
48
49QT_BEGIN_NAMESPACE
50
51CameraBinRecorder::CameraBinRecorder(CameraBinSession *session)
52 :QMediaRecorderControl(session),
53 m_session(session),
54 m_state(QMediaRecorder::StoppedState),
55 m_status(QMediaRecorder::UnloadedStatus)
56{
57 connect(asender: m_session, SIGNAL(statusChanged(QCamera::Status)), SLOT(updateStatus()));
58 connect(asender: m_session, SIGNAL(pendingStateChanged(QCamera::State)), SLOT(updateStatus()));
59 connect(asender: m_session, SIGNAL(busyChanged(bool)), SLOT(updateStatus()));
60
61 connect(asender: m_session, SIGNAL(durationChanged(qint64)), SIGNAL(durationChanged(qint64)));
62 connect(sender: m_session, SIGNAL(mutedChanged(bool)), receiver: this, SIGNAL(mutedChanged(bool)));
63 connect(sender: m_session->cameraControl()->resourcePolicy(), SIGNAL(canCaptureChanged()),
64 receiver: this, SLOT(updateStatus()));
65}
66
67CameraBinRecorder::~CameraBinRecorder()
68{
69}
70
71QUrl CameraBinRecorder::outputLocation() const
72{
73 return m_session->outputLocation();
74}
75
76bool CameraBinRecorder::setOutputLocation(const QUrl &sink)
77{
78 m_session->setOutputLocation(sink);
79 return true;
80}
81
82QMediaRecorder::State CameraBinRecorder::state() const
83{
84 return m_state;
85}
86
87QMediaRecorder::Status CameraBinRecorder::status() const
88{
89 return m_status;
90}
91
92void CameraBinRecorder::updateStatus()
93{
94 QCamera::Status sessionStatus = m_session->status();
95
96 QMediaRecorder::State oldState = m_state;
97 QMediaRecorder::Status oldStatus = m_status;
98
99 if (sessionStatus == QCamera::ActiveStatus &&
100 m_session->captureMode().testFlag(flag: QCamera::CaptureVideo)) {
101
102 if (!m_session->cameraControl()->resourcePolicy()->canCapture()) {
103 m_status = QMediaRecorder::UnavailableStatus;
104 m_state = QMediaRecorder::StoppedState;
105 m_session->stopVideoRecording();
106 } else if (m_state == QMediaRecorder::RecordingState) {
107 m_status = QMediaRecorder::RecordingStatus;
108 } else {
109 m_status = m_session->isBusy() ?
110 QMediaRecorder::FinalizingStatus :
111 QMediaRecorder::LoadedStatus;
112 }
113 } else {
114 if (m_state == QMediaRecorder::RecordingState) {
115 m_state = QMediaRecorder::StoppedState;
116 m_session->stopVideoRecording();
117 }
118 m_status = m_session->pendingState() == QCamera::ActiveState
119 && m_session->captureMode().testFlag(flag: QCamera::CaptureVideo)
120 ? QMediaRecorder::LoadingStatus
121 : QMediaRecorder::UnloadedStatus;
122 }
123
124 if (m_state != oldState)
125 emit stateChanged(state: m_state);
126
127 if (m_status != oldStatus)
128 emit statusChanged(status: m_status);
129}
130
131qint64 CameraBinRecorder::duration() const
132{
133 return m_session->duration();
134}
135
136
137void CameraBinRecorder::applySettings()
138{
139#if QT_CONFIG(gstreamer_encodingprofiles)
140 CameraBinContainer *containerControl = m_session->mediaContainerControl();
141 CameraBinAudioEncoder *audioEncoderControl = m_session->audioEncodeControl();
142 CameraBinVideoEncoder *videoEncoderControl = m_session->videoEncodeControl();
143
144 containerControl->resetActualContainerFormat();
145 audioEncoderControl->resetActualSettings();
146 videoEncoderControl->resetActualSettings();
147
148 //encodebin doesn't like the encoding profile with ANY caps,
149 //if container and codecs are not specified,
150 //try to find a commonly used supported combination
151 if (containerControl->containerFormat().isEmpty() &&
152 audioEncoderControl->audioSettings().codec().isEmpty() &&
153 videoEncoderControl->videoSettings().codec().isEmpty()) {
154
155 QList<QStringList> candidates;
156
157 // By order of preference
158
159 // .mp4 (h264, AAC)
160 candidates.append(t: QStringList() << "video/quicktime, variant=(string)iso" << "video/x-h264" << "audio/mpeg, mpegversion=(int)4");
161
162 // .mp4 (h264, AC3)
163 candidates.append(t: QStringList() << "video/quicktime, variant=(string)iso" << "video/x-h264" << "audio/x-ac3");
164
165 // .mp4 (h264, MP3)
166 candidates.append(t: QStringList() << "video/quicktime, variant=(string)iso" << "video/x-h264" << "audio/mpeg, mpegversion=(int)1, layer=(int)3");
167
168 // .mkv (h264, AAC)
169 candidates.append(t: QStringList() << "video/x-matroska" << "video/x-h264" << "audio/mpeg, mpegversion=(int)4");
170
171 // .mkv (h264, AC3)
172 candidates.append(t: QStringList() << "video/x-matroska" << "video/x-h264" << "audio/x-ac3");
173
174 // .mkv (h264, MP3)
175 candidates.append(t: QStringList() << "video/x-matroska" << "video/x-h264" << "audio/mpeg, mpegversion=(int)1, layer=(int)3");
176
177 // .mov (h264, AAC)
178 candidates.append(t: QStringList() << "video/quicktime" << "video/x-h264" << "audio/mpeg, mpegversion=(int)4");
179
180 // .mov (h264, MP3)
181 candidates.append(t: QStringList() << "video/quicktime" << "video/x-h264" << "audio/mpeg, mpegversion=(int)1, layer=(int)3");
182
183 // .webm (VP8, Vorbis)
184 candidates.append(t: QStringList() << "video/webm" << "video/x-vp8" << "audio/x-vorbis");
185
186 // .ogg (Theora, Vorbis)
187 candidates.append(t: QStringList() << "application/ogg" << "video/x-theora" << "audio/x-vorbis");
188
189 // .avi (DivX, MP3)
190 candidates.append(t: QStringList() << "video/x-msvideo" << "video/x-divx" << "audio/mpeg, mpegversion=(int)1, layer=(int)3");
191
192 for (const QStringList &candidate : qAsConst(t&: candidates)) {
193 if (containerControl->supportedContainers().contains(str: candidate[0]) &&
194 videoEncoderControl->supportedVideoCodecs().contains(str: candidate[1]) &&
195 audioEncoderControl->supportedAudioCodecs().contains(str: candidate[2])) {
196 containerControl->setActualContainerFormat(candidate[0]);
197
198 QVideoEncoderSettings videoSettings = videoEncoderControl->videoSettings();
199 videoSettings.setCodec(candidate[1]);
200 videoEncoderControl->setActualVideoSettings(videoSettings);
201
202 QAudioEncoderSettings audioSettings = audioEncoderControl->audioSettings();
203 audioSettings.setCodec(candidate[2]);
204 audioEncoderControl->setActualAudioSettings(audioSettings);
205
206 break;
207 }
208 }
209 }
210#endif
211}
212
213#if QT_CONFIG(gstreamer_encodingprofiles)
214
215GstEncodingContainerProfile *CameraBinRecorder::videoProfile()
216{
217 GstEncodingContainerProfile *containerProfile = m_session->mediaContainerControl()->createProfile();
218
219 if (containerProfile) {
220 GstEncodingProfile *audioProfile = m_session->audioEncodeControl()->createProfile();
221 GstEncodingProfile *videoProfile = m_session->videoEncodeControl()->createProfile();
222
223 if (audioProfile) {
224 if (!gst_encoding_container_profile_add_profile(container: containerProfile, profile: audioProfile))
225 gst_encoding_profile_unref(audioProfile);
226 }
227 if (videoProfile) {
228 if (!gst_encoding_container_profile_add_profile(container: containerProfile, profile: videoProfile))
229 gst_encoding_profile_unref(videoProfile);
230 }
231 }
232
233 return containerProfile;
234}
235
236#endif
237
238void CameraBinRecorder::setState(QMediaRecorder::State state)
239{
240 if (m_state == state)
241 return;
242
243 QMediaRecorder::State oldState = m_state;
244 QMediaRecorder::Status oldStatus = m_status;
245
246 switch (state) {
247 case QMediaRecorder::StoppedState:
248 m_state = state;
249 m_status = QMediaRecorder::FinalizingStatus;
250 m_session->stopVideoRecording();
251 break;
252 case QMediaRecorder::PausedState:
253 emit error(error: QMediaRecorder::ResourceError, errorString: tr(s: "QMediaRecorder::pause() is not supported by camerabin2."));
254 break;
255 case QMediaRecorder::RecordingState:
256
257 if (m_session->status() != QCamera::ActiveStatus) {
258 emit error(error: QMediaRecorder::ResourceError, errorString: tr(s: "Service has not been started"));
259 } else if (!m_session->cameraControl()->resourcePolicy()->canCapture()) {
260 emit error(error: QMediaRecorder::ResourceError, errorString: tr(s: "Recording permissions are not available"));
261 } else {
262 m_session->recordVideo();
263 m_state = state;
264 m_status = QMediaRecorder::RecordingStatus;
265 emit actualLocationChanged(location: m_session->outputLocation());
266 }
267 }
268
269 if (m_state != oldState)
270 emit stateChanged(state: m_state);
271
272 if (m_status != oldStatus)
273 emit statusChanged(status: m_status);
274}
275
276bool CameraBinRecorder::isMuted() const
277{
278 return m_session->isMuted();
279}
280
281qreal CameraBinRecorder::volume() const
282{
283 return 1.0;
284}
285
286void CameraBinRecorder::setMuted(bool muted)
287{
288 m_session->setMuted(muted);
289}
290
291void CameraBinRecorder::setVolume(qreal volume)
292{
293 if (!qFuzzyCompare(p1: volume, p2: qreal(1.0)))
294 qWarning() << "Media service doesn't support recorder audio gain.";
295}
296
297QT_END_NAMESPACE
298
299

source code of qtmultimedia/src/plugins/gstreamer/camerabin/camerabinrecorder.cpp