summaryrefslogtreecommitdiffstats
path: root/src/multimedia/recording/qmediacapturesession.cpp
blob: 6e9df9c8da460d5d1d1938dad642d2d789589b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/****************************************************************************
**
** Copyright (C) 2016 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 "qmediacapturesession.h"
#include "qaudiodeviceinfo.h"
#include "qcamera.h"
#include "qmediaencoder.h"
#include "qcameraimagecapture.h"
#include "qvideosink.h"

#include <qpointer.h>

#include "qplatformmediaintegration_p.h"
#include "qplatformmediacapture_p.h"

QT_BEGIN_NAMESPACE

class QMediaCaptureSessionPrivate
{
public:
    QMediaCaptureSession *q = nullptr;
    QPlatformMediaCaptureSession *captureSession;
    QAudioDeviceInfo audioInput;
    QCamera *camera = nullptr;
    QCameraImageCapture *imageCapture = nullptr;
    QMediaEncoder *encoder = nullptr;
    QVideoSink *videoSink = nullptr;
    QPointer<QObject> videoOutput;

    void setVideoSink(QVideoSink *sink)
    {
        if (sink == videoSink)
            return;
        if (videoSink)
            videoSink->setSource(nullptr);
        videoSink = sink;
        if (sink)
            sink->setSource(q);
        captureSession->setVideoPreview(sink);
        emit q->videoOutputChanged();
    }
};

/*!
    \class QMediaCaptureSession

    \brief The QMediaCaptureSession class allows capturing of audio and video content.
    \inmodule QtMultimedia
    \ingroup multimedia

    The QMediaCaptureSession is the central class that manages capturing of media on the local device.

    You can connect a camera and a microphone to QMediaCaptureSession using setCamera() and setAudioInput().
    A preview of the captured media can be seen by setting a QVideoSink of QVideoWidget using setVideoOutput()
    and heard by routing the audio to an output device using setAudioOutput().

    You can capture still images from a camera by setting a QCameraImageCapture object on the capture session,
    and record audio/video using a QMediaEncoder.

    If you need a simple class that records media from the default camera and microphone, you can use QMediaRecorder.
    That class uses a QMediaCaptureSession behind the scene to support audio and video capture.

    \sa QCamera, QAudioDeviceInfo, QMediaEncoder, QCameraImageCapture, QMediaRecorder
*/

/*!
    Creates a session for media capture.
 */
QMediaCaptureSession::QMediaCaptureSession(QObject *parent)
    : QObject(parent),
    d_ptr(new QMediaCaptureSessionPrivate)
{
    d_ptr->q = this;
    d_ptr->captureSession = QPlatformMediaIntegration::instance()->createCaptureSession(QMediaRecorder::AudioAndVideo);

    connect(d_ptr->captureSession, SIGNAL(mutedChanged(bool)), this, SIGNAL(mutedChanged(bool)));
    connect(d_ptr->captureSession, SIGNAL(volumeChanged(qreal)), this, SIGNAL(volumeChanged(qreal)));
}

/*!
    Destroys the session.
 */
QMediaCaptureSession::~QMediaCaptureSession()
{
    if (d_ptr->camera)
        d_ptr->camera->setCaptureSession(nullptr);
    if (d_ptr->encoder)
        d_ptr->encoder->setCaptureSession(nullptr);
    if (d_ptr->imageCapture)
        d_ptr->imageCapture->setCaptureSession(nullptr);
    delete d_ptr->captureSession;
    delete d_ptr;
}

/*!
    Returns false if media capture is not supported.
 */
bool QMediaCaptureSession::isAvailable() const
{
    return d_ptr->captureSession != nullptr;
}

/*!
    Returns the device that is being used to capture audio.
*/
QAudioDeviceInfo QMediaCaptureSession::audioInput() const
{
    return d_ptr->audioInput;
}

/*!
    Sets the audio input device to \a device. If setting it to an empty
    QAudioDeviceInfo the capture session will use the default input as
    defined by the operating system.

    Use setMuted(), if you want to disable audio input.

    \sa muted(), setMuted()
*/
void QMediaCaptureSession::setAudioInput(const QAudioDeviceInfo &device)
{
    d_ptr->audioInput = device;
    emit audioInputChanged();
}

/*!
    \property QMediaCaptureSession::muted

    \brief whether a recording audio stream is muted.
*/

bool QMediaCaptureSession::isMuted() const
{
    return d_ptr->captureSession->isMuted();
}

void QMediaCaptureSession::setMuted(bool muted)
{
    d_ptr->captureSession->setMuted(muted);
}

/*!
    \property QMediaCaptureSession::volume

    \brief the current recording audio volume.

    The volume is scaled linearly from \c 0.0 (silence) to \c 1.0 (full volume). Values outside this
    range will be clamped.

    The default volume is \c 1.0.

    UI volume controls should usually be scaled nonlinearly. For example, using a logarithmic scale
    will produce linear changes in perceived loudness, which is what a user would normally expect
    from a volume control. See QAudio::convertVolume() for more details.
*/

qreal QMediaCaptureSession::volume() const
{
    return d_ptr->captureSession->volume();
}

void QMediaCaptureSession::setVolume(qreal volume)
{
    d_ptr->captureSession->setVolume(volume);
}

/*!
    \property QMediaCaptureSession::camera

    \brief the camera used to capture video.

    Record the camera or take images by adding a camera t the capture session using this property,
*/
QCamera *QMediaCaptureSession::camera() const
{
    return d_ptr->camera;
}

void QMediaCaptureSession::setCamera(QCamera *camera)
{
    if (d_ptr->camera == camera)
        return;
    if (d_ptr->camera)
        d_ptr->camera->setCaptureSession(nullptr);

    d_ptr->camera = camera;
    if (d_ptr->camera)
        d_ptr->camera->setCaptureSession(this);
    emit cameraChanged();
}

/*!
    \property QMediaCaptureSession::imageCapture

    \brief the object used to capture still images.

    Add a QCameraImageCapture object to the capture session to enable
    capturing of still images from the camera.
*/
QCameraImageCapture *QMediaCaptureSession::imageCapture()
{
    return d_ptr->imageCapture;
}

void QMediaCaptureSession::setImageCapture(QCameraImageCapture *imageCapture)
{
    if (d_ptr->imageCapture == imageCapture)
        return;
    if (d_ptr->imageCapture)
        d_ptr->imageCapture->setCaptureSession(nullptr);

    d_ptr->imageCapture = imageCapture;
    if (d_ptr->imageCapture)
        d_ptr->imageCapture->setCaptureSession(this);
    emit imageCaptureChanged();
}

/*!
    \property QMediaCaptureSession::encoder

    \brief the encoder object used to capture audio/video.

    Add a QMediaEncoder object to the capture session to enable
    recording of audio and/or video from the capture session.
*/

QMediaEncoder *QMediaCaptureSession::encoder()
{
    return d_ptr->encoder;
}

void QMediaCaptureSession::setEncoder(QMediaEncoder *encoder)
{
    if (d_ptr->encoder == encoder)
        return;
    if (d_ptr->encoder)
        d_ptr->encoder->setCaptureSession(nullptr);

    d_ptr->encoder = encoder;
    if (d_ptr->encoder)
        d_ptr->encoder->setCaptureSession(this);
    emit encoderChanged();
}

QObject *QMediaCaptureSession::videoOutput() const
{
    Q_D(const QMediaCaptureSession);
    return d->videoOutput;
}

/*!
    Sets a QObject based video preview for the capture session.

    A QObject based preview is expected to have an invokable videoSink()
    method that returns a QVideoSink.

    The previously set preview is detached.
*/
void QMediaCaptureSession::setVideoOutput(QObject *output)
{
    Q_D(QMediaCaptureSession);
    if (d->videoOutput == output)
        return;
    QVideoSink *sink = qobject_cast<QVideoSink *>(output);
    if (!sink && output) {
        auto *mo = output->metaObject();
        mo->invokeMethod(output, "videoSink", Q_RETURN_ARG(QVideoSink *, sink));
    }
    d->videoOutput = output;
    d->setVideoSink(sink);
}

void QMediaCaptureSession::setVideoSink(QVideoSink *sink)
{
    Q_D(QMediaCaptureSession);
    d->videoOutput = nullptr;
    d->setVideoSink(sink);
}

QVideoSink *QMediaCaptureSession::videoSink() const
{
    Q_D(const QMediaCaptureSession);
    return d->videoSink;
}

/*!
    \internal
*/
QPlatformMediaCaptureSession *QMediaCaptureSession::platformSession() const
{
    return d_ptr->captureSession;
}


QT_END_NAMESPACE

#include "moc_qmediacapturesession.cpp"