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/qplatformmediaintegration_p.h>
19#include <QtMultimedia/private/qplatformmediacapture_p.h>
21#if QT_CONFIG(gstreamer_qt_api)
22# include <QtMultimedia/spi/qgstreamervideosource.h>
29 Q_Q(QMediaCaptureSession);
31 if (sink == videoSink)
34 videoSink->setSource(
nullptr);
39 captureSession->setVideoPreview(sink);
40 emit q->videoOutputChanged();
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
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
135 static constexpr auto Member = &QMediaCaptureSessionPrivate::camera;
142#if QT_CONFIG(gstreamer_qt_api)
144struct QMediaCaptureSession::ObjectTraits<QGStreamerVideoSource>
146 static constexpr bool IsCamera =
true;
147 static constexpr auto Member = &QMediaCaptureSessionPrivate::nativeVideoSource;
148 static constexpr auto Setter = &QMediaCaptureSession::setNativeVideoSource;
149 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setCamera;
150 static constexpr auto PlatformObjectProvider = &QGStreamerVideoSource::platformVideoSource;
151 static constexpr auto ChangeNotifier = &QMediaCaptureSession::nativeVideoSourceChanged;
156struct QMediaCaptureSession::ObjectTraits<QScreenCapture>
158 static constexpr bool IsCamera =
false;
159 static constexpr auto Member = &QMediaCaptureSessionPrivate::screenCapture;
160 static constexpr auto Setter = &QMediaCaptureSession::setScreenCapture;
161 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setScreenCapture;
162 static constexpr auto PlatformObjectProvider = &QScreenCapture::platformScreenCapture;
163 static constexpr auto ChangeNotifier = &QMediaCaptureSession::screenCaptureChanged;
167struct QMediaCaptureSession::ObjectTraits<QWindowCapture>
169 static constexpr bool IsCamera =
false;
170 static constexpr auto Member = &QMediaCaptureSessionPrivate::windowCapture;
171 static constexpr auto Setter = &QMediaCaptureSession::setWindowCapture;
172 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setWindowCapture;
173 static constexpr auto PlatformObjectProvider = &QWindowCapture::platformWindowCapture;
174 static constexpr auto ChangeNotifier = &QMediaCaptureSession::windowCaptureChanged;
178struct QMediaCaptureSession::ObjectTraits<QVideoFrameInput>
180 static constexpr bool IsCamera =
false;
181 static constexpr auto Member = &QMediaCaptureSessionPrivate::videoFrameInput;
182 static constexpr auto Setter = &QMediaCaptureSession::setVideoFrameInput;
183 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setVideoFrameInput;
184 static constexpr auto PlatformObjectProvider = &QVideoFrameInput::platformVideoFrameInput;
185 static constexpr auto ChangeNotifier = &QMediaCaptureSession::videoFrameInputChanged;
189struct QMediaCaptureSession::ObjectTraits<QAudioBufferInput>
191 static constexpr bool IsCamera =
false;
192 static constexpr auto Member = &QMediaCaptureSessionPrivate::audioBufferInput;
193 static constexpr auto Setter = &QMediaCaptureSession::setAudioBufferInput;
194 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setAudioBufferInput;
195 static constexpr auto PlatformObjectProvider = &QAudioBufferInput::platformAudioBufferInput;
196 static constexpr auto ChangeNotifier = &QMediaCaptureSession::audioBufferInputChanged;
200struct QMediaCaptureSession::ObjectTraits<QImageCapture>
202 static constexpr bool IsCamera =
false;
203 static constexpr auto Member = &QMediaCaptureSessionPrivate::imageCapture;
204 static constexpr auto Setter = &QMediaCaptureSession::setImageCapture;
205 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setImageCapture;
206 static constexpr auto PlatformObjectProvider = &QImageCapture::platformImageCapture;
207 static constexpr auto ChangeNotifier = &QMediaCaptureSession::imageCaptureChanged;
211struct QMediaCaptureSession::ObjectTraits<QMediaRecorder>
213 static constexpr bool IsCamera =
false;
214 static constexpr auto Member = &QMediaCaptureSessionPrivate::recorder;
215 static constexpr auto Setter = &QMediaCaptureSession::setRecorder;
216 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setMediaRecorder;
217 static constexpr auto PlatformObjectProvider = &QMediaRecorder::platformRecoder;
218 static constexpr auto ChangeNotifier = &QMediaCaptureSession::recorderChanged;
221template <
typename Object>
223 Q_D(QMediaCaptureSession);
225 using Traits = QMediaCaptureSession::ObjectTraits<Object>;
227 Object *oldObject = qobject_cast<Object *>(d->*Traits::Member);
228 if (oldObject == object)
231 if constexpr (Traits::IsCamera) {
232 if (!QPlatformMediaIntegration::instance()->isCameraSwitchingDuringRecordingSupported()
233 && recorder() && recorder()->recorderState() == QMediaRecorder::RecordingState) {
234 qWarning(
"This media backend does not support camera switching during recording");
239 d->*Traits::Member = object;
241 if (d->captureSession)
242 std::invoke(Traits::PlatformSetter, d->captureSession,
nullptr);
245 if (oldObject->captureSession() && oldObject->captureSession() !=
this)
246 std::invoke(Traits::Setter, oldObject->captureSession(),
nullptr);
247 oldObject->setCaptureSession(
nullptr);
251 if (
auto *otherSession = object->captureSession())
252 std::invoke(Traits::Setter, otherSession,
nullptr);
253 if (d->captureSession)
254 std::invoke(Traits::PlatformSetter, d->captureSession,
255 std::invoke(Traits::PlatformObjectProvider, object));
256 object->setCaptureSession(
this);
259 emit (
this->*Traits::ChangeNotifier)();
263
264
266 : QObject{ *
new QMediaCaptureSessionPrivate, parent }
268 Q_D(QMediaCaptureSession);
270 auto maybeCaptureSession = QPlatformMediaIntegration::instance()->createCaptureSession();
271 if (maybeCaptureSession) {
272 d->captureSession.reset(maybeCaptureSession.value());
273 d->captureSession->setCaptureSession(
this);
275 qWarning() <<
"Failed to initialize QMediaCaptureSession" << maybeCaptureSession.error();
280
281
284 Q_D(QMediaCaptureSession);
287 setNativeVideoSource(
nullptr);
288 setRecorder(
nullptr);
289 setImageCapture(
nullptr);
290 setScreenCapture(
nullptr);
291 setWindowCapture(
nullptr);
292 setVideoFrameInput(
nullptr);
293 setAudioBufferInput(
nullptr);
294 setAudioInput(
nullptr);
295 setAudioOutput(
nullptr);
296 d->setVideoSink(
nullptr);
297 d->captureSession.reset();
300
301
302
303
306
307
308
309
312 Q_D(
const QMediaCaptureSession);
313 return d->audioInput;
317
318
319
320
323 Q_D(QMediaCaptureSession);
325 QAudioInput *oldInput = d->audioInput;
326 if (oldInput == input)
331 d->audioInput =
nullptr;
334 if (d->captureSession)
335 d->captureSession->setAudioInput(
nullptr);
337 oldInput->setDisconnectFunction({});
339 input->setDisconnectFunction([
this](){ setAudioInput(
nullptr); });
340 if (d->captureSession)
341 d->captureSession->setAudioInput(input->handle());
343 d->audioInput = input;
344 emit audioInputChanged();
348
349
350
351
352
355 Q_D(
const QMediaCaptureSession);
357 return d->audioBufferInput;
366
367
368
369
370
371
372
375
376
377
378
379
380
381
384 Q_D(
const QMediaCaptureSession);
390#if QT_CONFIG(gstreamer_qt_api)
391 Q_D(QMediaCaptureSession);
392 if (d->nativeVideoSource && camera) {
394 qWarning(
"Setting camera, when gstreamer video source is connected, is not supported");
403
404
405
406
407
408
409
410
411
412
413
416
417
418
419
420
421
422
423
424
425
426
430 Q_D(
const QMediaCaptureSession);
431 return d->nativeVideoSource;
436#if QT_CONFIG(gstreamer_qt_api)
437 Q_D(QMediaCaptureSession);
439 auto *gstreamerVideoSource = qobject_cast<QGStreamerVideoSource *>(videoSource);
441 if (videoSource && !gstreamerVideoSource) {
442 qCritical() <<
"Unsupported video source type; QGStreamerVideoSource is expected.";
446 if (d->camera && gstreamerVideoSource) {
448 qWarning(
"Setting GStreamer video source, when camera is connected, is not supported");
452 setObject(gstreamerVideoSource);
455 qCritical() <<
"Only gstreamer video source is supported";
461
462
463
464
465
466
467
468
471
472
473
474
475
476
477
478
481 Q_D(QMediaCaptureSession);
482 return d->screenCapture;
487 setObject(screenCapture);
491
492
493
494
495
496
497
498
501
502
503
504
505
506
507
508
511 Q_D(QMediaCaptureSession);
512 return d->windowCapture;
517 setObject(windowCapture);
521
522
523
524
525
526
529 Q_D(
const QMediaCaptureSession);
530 return d->videoFrameInput;
539
540
541
542
543
544
545
547
548
549
550
551
552
553
556 Q_D(QMediaCaptureSession);
557 return d->imageCapture;
562 setObject(imageCapture);
565
566
567
568
569
570
571
573
574
575
576
577
578
579
583 Q_D(QMediaCaptureSession);
592
593
594
595
596
597
598
599
600
601
603
604
605
606
609 Q_D(
const QMediaCaptureSession);
610 return d->videoOutput;
613
614
615
616
617
618
619
622 Q_D(QMediaCaptureSession);
623 if (d->videoOutput == output)
625 QVideoSink *sink = qobject_cast<QVideoSink *>(output);
626 if (!sink && output) {
627 auto *mo = output->metaObject();
628 mo->invokeMethod(output,
"videoSink", Q_RETURN_ARG(QVideoSink *, sink));
630 d->videoOutput = output;
631 d->setVideoSink(sink);
635
636
637
638
639
640
641
644 Q_D(QMediaCaptureSession);
645 d->videoOutput =
nullptr;
646 d->setVideoSink(sink);
650
651
654 Q_D(
const QMediaCaptureSession);
658
659
660
661
664 Q_D(QMediaCaptureSession);
666 QAudioOutput *oldOutput = d->audioOutput;
667 if (oldOutput == output)
673 d->audioOutput =
nullptr;
675 if (d->captureSession)
676 d->captureSession->setAudioOutput(
nullptr);
678 oldOutput->setDisconnectFunction({});
680 output->setDisconnectFunction([
this](){ setAudioOutput(
nullptr); });
681 if (d->captureSession)
682 d->captureSession->setAudioOutput(output->handle());
684 d->audioOutput = output;
685 emit audioOutputChanged();
688
689
690
691
692
693
695
696
697
698
701 Q_D(
const QMediaCaptureSession);
702 return d->audioOutput;
706
707
710 Q_D(
const QMediaCaptureSession);
711 return d->captureSession.get();
714
715
716
717
720
721
722
723
726
727
728
729
732
733
734
735
738
739
740
741
744
745
746
747
750
751
752
753
756#include "moc_qmediacapturesession.cpp"
Combined button and popup list for selecting options.