Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qmediacapturesession.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6#include "qaudiodevice.h"
7#include "qcamera.h"
8#include "qmediarecorder.h"
9#include "qimagecapture.h"
10#include "qvideosink.h"
11#include "qscreencapture.h"
12#include "qwindowcapture.h"
13
16#include "qaudioinput.h"
17#include "qaudiooutput.h"
18
20
22{
23 if (sink == videoSink)
24 return;
25 if (videoSink)
26 videoSink->setSource(nullptr);
28 if (sink)
29 sink->setSource(q);
32 emit q->videoOutputChanged();
33}
34
115 : QObject(parent),
117{
118 d_ptr->q = this;
119 auto maybeCaptureSession = QPlatformMediaIntegration::instance()->createCaptureSession();
120 if (maybeCaptureSession) {
121 d_ptr->captureSession = maybeCaptureSession.value();
122 d_ptr->captureSession->setCaptureSession(this);
123 } else {
124 qWarning() << "Failed to initialize QMediaCaptureSession" << maybeCaptureSession.error();
125 }
126}
127
132{
133 setCamera(nullptr);
134 setRecorder(nullptr);
135 setImageCapture(nullptr);
136 setScreenCapture(nullptr);
137 setWindowCapture(nullptr);
138 setAudioInput(nullptr);
139 setAudioOutput(nullptr);
140 d_ptr->setVideoSink(nullptr);
141 delete d_ptr->captureSession;
142 delete d_ptr;
143}
156{
157 return d_ptr->audioInput;
158}
159
166{
167 QAudioInput *oldInput = d_ptr->audioInput;
168 if (oldInput == input)
169 return;
170
171 // To avoid double emit of audioInputChanged
172 // from recursive setAudioInput(nullptr) call.
173 d_ptr->audioInput = nullptr;
174
175 if (d_ptr->captureSession)
176 d_ptr->captureSession->setAudioInput(nullptr);
177 if (oldInput)
178 oldInput->setDisconnectFunction({});
179 if (input) {
180 input->setDisconnectFunction([this](){ setAudioInput(nullptr); });
181 if (d_ptr->captureSession)
182 d_ptr->captureSession->setAudioInput(input->handle());
183 }
184 d_ptr->audioInput = input;
186}
187
206{
207 return d_ptr->camera;
208}
209
211{
212 // TODO: come up with an unification of the captures setup
213 QCamera *oldCamera = d_ptr->camera;
214 if (oldCamera == camera)
215 return;
216 d_ptr->camera = camera;
217 if (d_ptr->captureSession)
218 d_ptr->captureSession->setCamera(nullptr);
219 if (oldCamera) {
220 if (oldCamera->captureSession() && oldCamera->captureSession() != this)
221 oldCamera->captureSession()->setCamera(nullptr);
222 oldCamera->setCaptureSession(nullptr);
223 }
224 if (camera) {
225 if (camera->captureSession())
226 camera->captureSession()->setCamera(nullptr);
227 if (d_ptr->captureSession)
228 d_ptr->captureSession->setCamera(camera->platformCamera());
229 camera->setCaptureSession(this);
230 }
232}
233
254{
255 return d_ptr ? d_ptr->screenCapture : nullptr;
256}
257
259{
260 // TODO: come up with an unification of the captures setup
261 QScreenCapture *oldScreenCapture = d_ptr->screenCapture;
262 if (oldScreenCapture == screenCapture)
263 return;
265 if (d_ptr->captureSession)
266 d_ptr->captureSession->setScreenCapture(nullptr);
267 if (oldScreenCapture) {
268 if (oldScreenCapture->captureSession() && oldScreenCapture->captureSession() != this)
269 oldScreenCapture->captureSession()->setScreenCapture(nullptr);
270 oldScreenCapture->setCaptureSession(nullptr);
271 }
272 if (screenCapture) {
275 if (d_ptr->captureSession)
276 d_ptr->captureSession->setScreenCapture(screenCapture->platformScreenCapture());
277 screenCapture->setCaptureSession(this);
278 }
280}
281
302 return d_ptr ? d_ptr->windowCapture : nullptr;
303}
304
306{
307 // TODO: come up with an unification of the captures setup
308 QWindowCapture *oldCapture = d_ptr->windowCapture;
309 if (oldCapture == windowCapture)
310 return;
312 if (d_ptr->captureSession)
313 d_ptr->captureSession->setWindowCapture(nullptr);
314 if (oldCapture) {
315 if (oldCapture->captureSession() && oldCapture->captureSession() != this)
316 oldCapture->captureSession()->setWindowCapture(nullptr);
317 oldCapture->setCaptureSession(nullptr);
318 }
319 if (windowCapture) {
322 if (d_ptr->captureSession)
323 d_ptr->captureSession->setWindowCapture(windowCapture->platformWindowCapture());
324 windowCapture->setCaptureSession(this);
325 }
327}
328
346{
347 return d_ptr->imageCapture;
348}
349
351{
352 // TODO: come up with an unification of the captures setup
353 QImageCapture *oldImageCapture = d_ptr->imageCapture;
354 if (oldImageCapture == imageCapture)
355 return;
356 d_ptr->imageCapture = imageCapture;
357 if (d_ptr->captureSession)
358 d_ptr->captureSession->setImageCapture(nullptr);
359 if (oldImageCapture) {
360 if (oldImageCapture->captureSession() && oldImageCapture->captureSession() != this)
361 oldImageCapture->captureSession()->setImageCapture(nullptr);
362 oldImageCapture->setCaptureSession(nullptr);
363 }
364 if (imageCapture) {
367 if (d_ptr->captureSession)
368 d_ptr->captureSession->setImageCapture(imageCapture->platformImageCapture());
369 imageCapture->setCaptureSession(this);
370 }
372}
391{
392 return d_ptr->recorder;
393}
394
396{
397 QMediaRecorder *oldRecorder = d_ptr->recorder;
398 if (oldRecorder == recorder)
399 return;
400 d_ptr->recorder = recorder;
401 if (d_ptr->captureSession)
402 d_ptr->captureSession->setMediaRecorder(nullptr);
403 if (oldRecorder) {
404 if (oldRecorder->captureSession() && oldRecorder->captureSession() != this)
405 oldRecorder->captureSession()->setRecorder(nullptr);
406 oldRecorder->setCaptureSession(nullptr);
407 }
408 if (recorder) {
411 if (d_ptr->captureSession)
413 recorder->setCaptureSession(this);
414 }
416}
434{
435 Q_D(const QMediaCaptureSession);
436 return d->videoOutput;
437}
447{
449 if (d->videoOutput == output)
450 return;
451 QVideoSink *sink = qobject_cast<QVideoSink *>(output);
452 if (!sink && output) {
453 auto *mo = output->metaObject();
454 mo->invokeMethod(output, "videoSink", Q_RETURN_ARG(QVideoSink *, sink));
455 }
456 d->videoOutput = output;
457 d->setVideoSink(sink);
458}
459
469{
471 d->videoOutput = nullptr;
472 d->setVideoSink(sink);
473}
474
479{
480 Q_D(const QMediaCaptureSession);
481 return d->videoSink;
482}
489{
490 QAudioOutput *oldOutput = d_ptr->audioOutput;
491 if (oldOutput == output)
492 return;
493
494 // We don't want to end up with signal emitted
495 // twice (from recursive call setAudioInput(nullptr)
496 // from oldOutput->setDisconnectFunction():
497 d_ptr->audioOutput = nullptr;
498
499 if (d_ptr->captureSession)
500 d_ptr->captureSession->setAudioOutput(nullptr);
501 if (oldOutput)
502 oldOutput->setDisconnectFunction({});
503 if (output) {
504 output->setDisconnectFunction([this](){ setAudioOutput(nullptr); });
505 if (d_ptr->captureSession)
506 d_ptr->captureSession->setAudioOutput(output->handle());
507 }
508 d_ptr->audioOutput = output;
510}
524{
525 Q_D(const QMediaCaptureSession);
526 return d->audioOutput;
527}
528
572
573#include "moc_qmediacapturesession.cpp"
\qmltype AudioInput \instantiates QAudioInput
Definition qaudioinput.h:19
\qmltype AudioOutput \instantiates QAudioOutput
The QCamera class provides interface for system camera devices.
Definition qcamera.h:28
QMediaCaptureSession * captureSession() const
Returns the capture session this camera is connected to, or a nullptr if the camera is not connected ...
Definition qcamera.cpp:391
\inmodule QtMultimedia
QMediaCaptureSession * captureSession() const
Returns the capture session this camera is connected to, or a nullptr if the camera is not connected ...
QPointer< QImageCapture > imageCapture
QPlatformMediaCaptureSession * captureSession
void setVideoSink(QVideoSink *sink)
QPointer< QScreenCapture > screenCapture
QPointer< QWindowCapture > windowCapture
QPointer< QMediaRecorder > recorder
The QMediaCaptureSession class allows capturing of audio and video content.
void setVideoSink(QVideoSink *sink)
Sets a QVideoSink, (sink), to a video preview for the capture session.
void setCamera(QCamera *camera)
void setAudioInput(QAudioInput *input)
Sets the audio input device to input.
void setRecorder(QMediaRecorder *recorder)
QPlatformMediaCaptureSession * platformSession() const
void setVideoOutput(QObject *output)
Sets a QObject, (output), to a video preview for the capture session.
QAudioOutput * audioOutput
\qmlproperty AudioOutput QtMultimedia::CaptureSession::audioOutput
void setAudioOutput(QAudioOutput *output)
Sets the audio output device to {output}.
QMediaRecorder * recorder
\qmlproperty MediaRecorder QtMultimedia::CaptureSession::recorder
QScreenCapture * screenCapture
\qmlproperty ScreenCapture QtMultimedia::CaptureSession::screenCapture
void setWindowCapture(QWindowCapture *windowCapture)
QWindowCapture * windowCapture
\qmlproperty WindowCapture QtMultimedia::CaptureSession::windowCapture
QMediaCaptureSession(QObject *parent=nullptr)
Creates a session for media capture from the parent object.
void setImageCapture(QImageCapture *imageCapture)
QCamera * camera
\qmlproperty Camera QtMultimedia::CaptureSession::camera
~QMediaCaptureSession()
Destroys the session.
QAudioInput * audioInput
\qmlproperty AudioInput QtMultimedia::CaptureSession::audioInput
QVideoSink * videoSink() const
Returns the QVideoSink for the session.
void setScreenCapture(QScreenCapture *screenCapture)
QImageCapture * imageCapture
\qmlproperty ImageCapture QtMultimedia::CaptureSession::imageCapture
QObject * videoOutput
\qmlproperty VideoOutput QtMultimedia::CaptureSession::videoOutput
\inmodule QtMultimedia
QMediaCaptureSession * captureSession() const
Returns the media capture session.
QPlatformMediaRecorder * platformRecoder() const
\inmodule QtCore
Definition qobject.h:103
virtual void setVideoPreview(QVideoSink *)
virtual void setImageCapture(QPlatformImageCapture *)
virtual void setAudioInput(QPlatformAudioInput *input)=0
virtual void setWindowCapture(QPlatformSurfaceCapture *)
virtual void setCamera(QPlatformCamera *)
virtual void setAudioOutput(QPlatformAudioOutput *)
void setCaptureSession(QMediaCaptureSession *session)
virtual void setScreenCapture(QPlatformSurfaceCapture *)
virtual void setMediaRecorder(QPlatformMediaRecorder *)
static QPlatformMediaIntegration * instance()
\inmodule QtMultimedia
QMediaCaptureSession * captureSession() const
Returns the capture session this QScreenCapture is connected to.
The QVideoSink class represents a generic sink for video data.
Definition qvideosink.h:22
\inmodule QtMultimedia
QMediaCaptureSession * captureSession() const
QMediaRecorder * recorder
Definition camera.cpp:20
QCamera * camera
Definition camera.cpp:19
QImageCapture * imageCapture
Definition camera.cpp:21
auto mo
[7]
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
#define Q_RETURN_ARG(Type, data)
Definition qobjectdefs.h:64
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLsizei GLenum GLboolean sink
GLenum GLenum GLenum input
#define emit
QT_BEGIN_NAMESPACE typedef uchar * output