Qt 6.x
The Qt SDK
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
5#include "qaudiodevice.h"
6#include "qcamera.h"
7#include "qmediarecorder.h"
8#include "qimagecapture.h"
9#include "qvideosink.h"
10#include "qscreencapture.h"
11#include "qwindowcapture.h"
12
13#include <qpointer.h>
14
17#include "qaudioinput.h"
18#include "qaudiooutput.h"
19
21
23{
24public:
36
38 {
39 if (sink == videoSink)
40 return;
41 if (videoSink)
42 videoSink->setSource(nullptr);
44 if (sink)
45 sink->setSource(q);
48 emit q->videoOutputChanged();
49 }
50};
51
132 : QObject(parent),
134{
135 d_ptr->q = this;
136 auto maybeCaptureSession = QPlatformMediaIntegration::instance()->createCaptureSession();
137 if (maybeCaptureSession) {
138 d_ptr->captureSession = maybeCaptureSession.value();
139 d_ptr->captureSession->setCaptureSession(this);
140 } else {
141 qWarning() << "Failed to initialize QMediaCaptureSession" << maybeCaptureSession.error();
142 }
143}
144
149{
150 setCamera(nullptr);
151 setRecorder(nullptr);
152 setImageCapture(nullptr);
153 setScreenCapture(nullptr);
154 setWindowCapture(nullptr);
155 setAudioInput(nullptr);
156 setAudioOutput(nullptr);
157 d_ptr->setVideoSink(nullptr);
158 delete d_ptr->captureSession;
159 delete d_ptr;
160}
173{
174 return d_ptr->audioInput;
175}
176
183{
184 QAudioInput *oldInput = d_ptr->audioInput;
185 if (oldInput == input)
186 return;
187 d_ptr->audioInput = input;
188 if (d_ptr->captureSession)
189 d_ptr->captureSession->setAudioInput(nullptr);
190 if (oldInput)
191 oldInput->setDisconnectFunction({});
192 if (input) {
193 input->setDisconnectFunction([this](){ setAudioInput(nullptr); });
194 if (d_ptr->captureSession)
195 d_ptr->captureSession->setAudioInput(input->handle());
196 }
198}
199
218{
219 return d_ptr->camera;
220}
221
223{
224 // TODO: come up with an unification of the captures setup
225 QCamera *oldCamera = d_ptr->camera;
226 if (oldCamera == camera)
227 return;
228 d_ptr->camera = camera;
229 if (d_ptr->captureSession)
230 d_ptr->captureSession->setCamera(nullptr);
231 if (oldCamera) {
232 if (oldCamera->captureSession() && oldCamera->captureSession() != this)
233 oldCamera->captureSession()->setCamera(nullptr);
234 oldCamera->setCaptureSession(nullptr);
235 }
236 if (camera) {
237 if (camera->captureSession())
238 camera->captureSession()->setCamera(nullptr);
239 if (d_ptr->captureSession)
240 d_ptr->captureSession->setCamera(camera->platformCamera());
241 camera->setCaptureSession(this);
242 }
244}
245
266{
267 return d_ptr ? d_ptr->screenCapture : nullptr;
268}
269
271{
272 // TODO: come up with an unification of the captures setup
273 QScreenCapture *oldScreenCapture = d_ptr->screenCapture;
274 if (oldScreenCapture == screenCapture)
275 return;
277 if (d_ptr->captureSession)
278 d_ptr->captureSession->setScreenCapture(nullptr);
279 if (oldScreenCapture) {
280 if (oldScreenCapture->captureSession() && oldScreenCapture->captureSession() != this)
281 oldScreenCapture->captureSession()->setScreenCapture(nullptr);
282 oldScreenCapture->setCaptureSession(nullptr);
283 }
284 if (screenCapture) {
287 if (d_ptr->captureSession)
288 d_ptr->captureSession->setScreenCapture(screenCapture->platformScreenCapture());
289 screenCapture->setCaptureSession(this);
290 }
292}
293
314 return d_ptr ? d_ptr->windowCapture : nullptr;
315}
316
318{
319 // TODO: come up with an unification of the captures setup
320 QWindowCapture *oldCapture = d_ptr->windowCapture;
321 if (oldCapture == windowCapture)
322 return;
324 if (d_ptr->captureSession)
325 d_ptr->captureSession->setWindowCapture(nullptr);
326 if (oldCapture) {
327 if (oldCapture->captureSession() && oldCapture->captureSession() != this)
328 oldCapture->captureSession()->setWindowCapture(nullptr);
329 oldCapture->setCaptureSession(nullptr);
330 }
331 if (windowCapture) {
334 if (d_ptr->captureSession)
335 d_ptr->captureSession->setWindowCapture(windowCapture->platformWindowCapture());
336 windowCapture->setCaptureSession(this);
337 }
339}
340
358{
359 return d_ptr->imageCapture;
360}
361
363{
364 // TODO: come up with an unification of the captures setup
365 QImageCapture *oldImageCapture = d_ptr->imageCapture;
366 if (oldImageCapture == imageCapture)
367 return;
368 d_ptr->imageCapture = imageCapture;
369 if (d_ptr->captureSession)
370 d_ptr->captureSession->setImageCapture(nullptr);
371 if (oldImageCapture) {
372 if (oldImageCapture->captureSession() && oldImageCapture->captureSession() != this)
373 oldImageCapture->captureSession()->setImageCapture(nullptr);
374 oldImageCapture->setCaptureSession(nullptr);
375 }
376 if (imageCapture) {
379 if (d_ptr->captureSession)
380 d_ptr->captureSession->setImageCapture(imageCapture->platformImageCapture());
381 imageCapture->setCaptureSession(this);
382 }
384}
403{
404 return d_ptr->recorder;
405}
406
408{
409 QMediaRecorder *oldRecorder = d_ptr->recorder;
410 if (oldRecorder == recorder)
411 return;
412 d_ptr->recorder = recorder;
413 if (d_ptr->captureSession)
414 d_ptr->captureSession->setMediaRecorder(nullptr);
415 if (oldRecorder) {
416 if (oldRecorder->captureSession() && oldRecorder->captureSession() != this)
417 oldRecorder->captureSession()->setRecorder(nullptr);
418 oldRecorder->setCaptureSession(nullptr);
419 }
420 if (recorder) {
423 if (d_ptr->captureSession)
425 recorder->setCaptureSession(this);
426 }
428}
446{
447 Q_D(const QMediaCaptureSession);
448 return d->videoOutput;
449}
459{
461 if (d->videoOutput == output)
462 return;
463 QVideoSink *sink = qobject_cast<QVideoSink *>(output);
464 if (!sink && output) {
465 auto *mo = output->metaObject();
466 mo->invokeMethod(output, "videoSink", Q_RETURN_ARG(QVideoSink *, sink));
467 }
468 d->videoOutput = output;
469 d->setVideoSink(sink);
470}
471
481{
483 d->videoOutput = nullptr;
484 d->setVideoSink(sink);
485}
486
491{
492 Q_D(const QMediaCaptureSession);
493 return d->videoSink;
494}
501{
502 QAudioOutput *oldOutput = d_ptr->audioOutput;
503 if (oldOutput == output)
504 return;
505 d_ptr->audioOutput = output;
506 if (d_ptr->captureSession)
507 d_ptr->captureSession->setAudioOutput(nullptr);
508 if (oldOutput)
509 oldOutput->setDisconnectFunction({});
510 if (output) {
511 output->setDisconnectFunction([this](){ setAudioOutput(nullptr); });
512 if (d_ptr->captureSession)
513 d_ptr->captureSession->setAudioOutput(output->handle());
514 }
516}
530{
531 Q_D(const QMediaCaptureSession);
532 return d->audioOutput;
533}
534
539{
540 return d_ptr->captureSession;
541}
578
579#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:399
\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
QPointer< QScreenCapture > screenCapture
QPointer< QWindowCapture > windowCapture
QPointer< QMediaRecorder > recorder
void setVideoSink(QVideoSink *sink)
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:90
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()
virtual QMaybe< QPlatformMediaCaptureSession * > createCaptureSession()
\inmodule QtCore
Definition qpointer.h:18
\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:162
#define Q_RETURN_ARG(Type, data)
Definition qobjectdefs.h:63
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
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent