7#include <QtMultimedia/qaudiobufferinput.h>
8#include <QtMultimedia/qaudiodevice.h>
9#include <QtMultimedia/qaudioinput.h>
10#include <QtMultimedia/qaudiooutput.h>
11#include <QtMultimedia/qcamera.h>
12#include <QtMultimedia/qimagecapture.h>
13#include <QtMultimedia/qmediarecorder.h>
14#include <QtMultimedia/qscreencapture.h>
15#include <QtMultimedia/qvideoframeinput.h>
16#include <QtMultimedia/qvideosink.h>
17#include <QtMultimedia/qwindowcapture.h>
18#include <QtMultimedia/private/qplatformaudioinput_p.h>
19#include <QtMultimedia/private/qplatformaudiooutput_p.h>
20#include <QtMultimedia/private/qplatformmediaintegration_p.h>
21#include <QtMultimedia/private/qplatformmediacapture_p.h>
23#if QT_CONFIG(gstreamer_qt_api)
24# include <QtMultimedia/spi/qgstreamervideosource.h>
31 Q_Q(QMediaCaptureSession);
33 if (sink == videoSink)
36 videoSink->setSource(
nullptr);
41 captureSession->setVideoPreview(sink);
42 Q_EMIT q->videoOutputChanged();
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
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
137 static constexpr auto Member = &QMediaCaptureSessionPrivate::camera;
144#if QT_CONFIG(gstreamer_qt_api)
146struct QMediaCaptureSession::ObjectTraits<QGStreamerVideoSource>
148 static constexpr bool IsCamera =
true;
149 static constexpr auto Member = &QMediaCaptureSessionPrivate::nativeVideoSource;
150 static constexpr auto Setter = &QMediaCaptureSession::setNativeVideoSource;
151 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setCamera;
152 static constexpr auto PlatformObjectProvider = &QGStreamerVideoSource::platformVideoSource;
153 static constexpr auto ChangeNotifier = &QMediaCaptureSession::nativeVideoSourceChanged;
158struct QMediaCaptureSession::ObjectTraits<QScreenCapture>
160 static constexpr bool IsCamera =
false;
161 static constexpr auto Member = &QMediaCaptureSessionPrivate::screenCapture;
162 static constexpr auto Setter = &QMediaCaptureSession::setScreenCapture;
163 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setScreenCapture;
164 static constexpr auto PlatformObjectProvider = &QScreenCapture::platformScreenCapture;
165 static constexpr auto ChangeNotifier = &QMediaCaptureSession::screenCaptureChanged;
169struct QMediaCaptureSession::ObjectTraits<QWindowCapture>
171 static constexpr bool IsCamera =
false;
172 static constexpr auto Member = &QMediaCaptureSessionPrivate::windowCapture;
173 static constexpr auto Setter = &QMediaCaptureSession::setWindowCapture;
174 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setWindowCapture;
175 static constexpr auto PlatformObjectProvider = &QWindowCapture::platformWindowCapture;
176 static constexpr auto ChangeNotifier = &QMediaCaptureSession::windowCaptureChanged;
180struct QMediaCaptureSession::ObjectTraits<QVideoFrameInput>
182 static constexpr bool IsCamera =
false;
183 static constexpr auto Member = &QMediaCaptureSessionPrivate::videoFrameInput;
184 static constexpr auto Setter = &QMediaCaptureSession::setVideoFrameInput;
185 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setVideoFrameInput;
186 static constexpr auto PlatformObjectProvider = &QVideoFrameInput::platformVideoFrameInput;
187 static constexpr auto ChangeNotifier = &QMediaCaptureSession::videoFrameInputChanged;
191struct QMediaCaptureSession::ObjectTraits<QAudioBufferInput>
193 static constexpr bool IsCamera =
false;
194 static constexpr auto Member = &QMediaCaptureSessionPrivate::audioBufferInput;
195 static constexpr auto Setter = &QMediaCaptureSession::setAudioBufferInput;
196 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setAudioBufferInput;
197 static constexpr auto PlatformObjectProvider = &QAudioBufferInput::platformAudioBufferInput;
198 static constexpr auto ChangeNotifier = &QMediaCaptureSession::audioBufferInputChanged;
202struct QMediaCaptureSession::ObjectTraits<QImageCapture>
204 static constexpr bool IsCamera =
false;
205 static constexpr auto Member = &QMediaCaptureSessionPrivate::imageCapture;
206 static constexpr auto Setter = &QMediaCaptureSession::setImageCapture;
207 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setImageCapture;
208 static constexpr auto PlatformObjectProvider = &QImageCapture::platformImageCapture;
209 static constexpr auto ChangeNotifier = &QMediaCaptureSession::imageCaptureChanged;
213struct QMediaCaptureSession::ObjectTraits<QMediaRecorder>
215 static constexpr bool IsCamera =
false;
216 static constexpr auto Member = &QMediaCaptureSessionPrivate::recorder;
217 static constexpr auto Setter = &QMediaCaptureSession::setRecorder;
218 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setMediaRecorder;
219 static constexpr auto PlatformObjectProvider = &QMediaRecorder::platformRecoder;
220 static constexpr auto ChangeNotifier = &QMediaCaptureSession::recorderChanged;
223template <
typename Object>
225 Q_D(QMediaCaptureSession);
227 using Traits = QMediaCaptureSession::ObjectTraits<Object>;
229 Object *oldObject = qobject_cast<Object *>(d->*Traits::Member);
230 if (oldObject == object)
233 if constexpr (Traits::IsCamera) {
234 if (!QPlatformMediaIntegration::instance()->isCameraSwitchingDuringRecordingSupported()
235 && recorder() && recorder()->recorderState() == QMediaRecorder::RecordingState) {
236 qWarning(
"This media backend does not support camera switching during recording");
241 d->*Traits::Member = object;
243 if (d->captureSession)
244 std::invoke(Traits::PlatformSetter, d->captureSession,
nullptr);
247 if (oldObject->captureSession() && oldObject->captureSession() !=
this)
248 std::invoke(Traits::Setter, oldObject->captureSession(),
nullptr);
249 oldObject->setCaptureSession(
nullptr);
253 if (
auto *otherSession = object->captureSession())
254 std::invoke(Traits::Setter, otherSession,
nullptr);
255 if (d->captureSession)
256 std::invoke(Traits::PlatformSetter, d->captureSession,
257 std::invoke(Traits::PlatformObjectProvider, object));
258 object->setCaptureSession(
this);
261 Q_EMIT (
this->*Traits::ChangeNotifier)();
265
266
268 : QObject{ *
new QMediaCaptureSessionPrivate, parent }
270 Q_D(QMediaCaptureSession);
272 auto maybeCaptureSession = QPlatformMediaIntegration::instance()->createCaptureSession();
273 if (maybeCaptureSession) {
274 d->captureSession.reset(maybeCaptureSession.value());
275 d->captureSession->setCaptureSession(
this);
277 qWarning() <<
"Failed to initialize QMediaCaptureSession" << maybeCaptureSession.error();
282
283
286 Q_D(QMediaCaptureSession);
289 setNativeVideoSource(
nullptr);
290 setRecorder(
nullptr);
291 setImageCapture(
nullptr);
292 setScreenCapture(
nullptr);
293 setWindowCapture(
nullptr);
294 setVideoFrameInput(
nullptr);
295 setAudioBufferInput(
nullptr);
296 setAudioInput(
nullptr);
297 setAudioOutput(
nullptr);
298 d->setVideoSink(
nullptr);
299 d->captureSession.reset();
302
303
304
305
308
309
310
311
314 Q_D(
const QMediaCaptureSession);
315 return d->audioInput;
319
320
321
322
325 Q_D(QMediaCaptureSession);
327 QAudioInput *oldInput = d->audioInput;
328 if (oldInput == input)
333 d->audioInput =
nullptr;
336 if (d->captureSession)
337 d->captureSession->setAudioInput(
nullptr);
339 oldInput->setDisconnectFunction({});
341 input->setDisconnectFunction([
this](){ setAudioInput(
nullptr); });
342 if (d->captureSession)
343 d->captureSession->setAudioInput(QPlatformAudioInput::get(input));
345 d->audioInput = input;
346 Q_EMIT audioInputChanged();
350
351
352
353
354
357 Q_D(
const QMediaCaptureSession);
359 return d->audioBufferInput;
368
369
370
371
372
373
374
377
378
379
380
381
382
383
386 Q_D(
const QMediaCaptureSession);
392#if QT_CONFIG(gstreamer_qt_api)
393 Q_D(QMediaCaptureSession);
394 if (d->nativeVideoSource && camera) {
396 qWarning(
"Setting camera, when gstreamer video source is connected, is not supported");
405
406
407
408
409
410
411
412
413
414
415
418
419
420
421
422
423
424
425
426
427
428
432 Q_D(
const QMediaCaptureSession);
433 return d->nativeVideoSource;
438#if QT_CONFIG(gstreamer_qt_api)
439 Q_D(QMediaCaptureSession);
441 auto *gstreamerVideoSource = qobject_cast<QGStreamerVideoSource *>(videoSource);
443 if (videoSource && !gstreamerVideoSource) {
444 qCritical() <<
"Unsupported video source type; QGStreamerVideoSource is expected.";
448 if (d->camera && gstreamerVideoSource) {
450 qWarning(
"Setting GStreamer video source, when camera is connected, is not supported");
454 setObject(gstreamerVideoSource);
457 qCritical() <<
"Only gstreamer video source is supported";
463
464
465
466
467
468
469
470
473
474
475
476
477
478
479
480
483 Q_D(QMediaCaptureSession);
484 return d->screenCapture;
489 setObject(screenCapture);
493
494
495
496
497
498
499
500
503
504
505
506
507
508
509
510
513 Q_D(QMediaCaptureSession);
514 return d->windowCapture;
519 setObject(windowCapture);
523
524
525
526
527
528
531 Q_D(
const QMediaCaptureSession);
532 return d->videoFrameInput;
541
542
543
544
545
546
547
549
550
551
552
553
554
555
558 Q_D(QMediaCaptureSession);
559 return d->imageCapture;
564 setObject(imageCapture);
567
568
569
570
571
572
573
575
576
577
578
579
580
581
585 Q_D(QMediaCaptureSession);
594
595
596
597
598
599
600
601
602
603
605
606
607
608
611 Q_D(
const QMediaCaptureSession);
612 return d->videoOutput;
615
616
617
618
619
620
621
624 Q_D(QMediaCaptureSession);
625 if (d->videoOutput == output)
627 QVideoSink *sink = qobject_cast<QVideoSink *>(output);
628 if (!sink && output) {
629 auto *mo = output->metaObject();
630 mo->invokeMethod(output,
"videoSink", Q_RETURN_ARG(QVideoSink *, sink));
632 d->videoOutput = output;
633 d->setVideoSink(sink);
637
638
639
640
641
642
643
646 Q_D(QMediaCaptureSession);
647 d->videoOutput =
nullptr;
648 d->setVideoSink(sink);
652
653
656 Q_D(
const QMediaCaptureSession);
660
661
662
663
666 Q_D(QMediaCaptureSession);
668 QAudioOutput *oldOutput = d->audioOutput;
669 if (oldOutput == output)
675 d->audioOutput =
nullptr;
677 if (d->captureSession)
678 d->captureSession->setAudioOutput(
nullptr);
680 oldOutput->setDisconnectFunction({});
682 output->setDisconnectFunction([
this](){ setAudioOutput(
nullptr); });
683 if (d->captureSession)
684 d->captureSession->setAudioOutput(QPlatformAudioOutput::get(output));
686 d->audioOutput = output;
687 Q_EMIT audioOutputChanged();
690
691
692
693
694
695
697
698
699
700
703 Q_D(
const QMediaCaptureSession);
704 return d->audioOutput;
708
709
712 Q_D(
const QMediaCaptureSession);
713 return d->captureSession.get();
716
717
718
719
722
723
724
725
728
729
730
731
734
735
736
737
740
741
742
743
746
747
748
749
752
753
754
755
758#include "moc_qmediacapturesession.cpp"
Combined button and popup list for selecting options.