20#if QT_CONFIG(gstreamer_qt_api)
21# include "qgstreamervideosource.h"
28 Q_Q(QMediaCaptureSession);
30 if (sink == videoSink)
33 videoSink->setSource(
nullptr);
38 captureSession->setVideoPreview(sink);
39 emit q->videoOutputChanged();
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
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
130struct QMediaCaptureSession::ObjectTraits<QCamera>
132 static constexpr bool IsCamera =
true;
133 static constexpr auto Member = &QMediaCaptureSessionPrivate::camera;
134 static constexpr auto Setter = &QMediaCaptureSession::setCamera;
135 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setCamera;
136 static constexpr auto PlatformObjectProvider = &QCamera::platformCamera;
137 static constexpr auto ChangeNotifier = &QMediaCaptureSession::cameraChanged;
140#if QT_CONFIG(gstreamer_qt_api)
142struct QMediaCaptureSession::ObjectTraits<QGStreamerVideoSource>
144 static constexpr bool IsCamera =
true;
145 static constexpr auto Member = &QMediaCaptureSessionPrivate::nativeVideoSource;
146 static constexpr auto Setter = &QMediaCaptureSession::setNativeVideoSource;
147 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setCamera;
148 static constexpr auto PlatformObjectProvider = &QGStreamerVideoSource::platformVideoSource;
149 static constexpr auto ChangeNotifier = &QMediaCaptureSession::nativeVideoSourceChanged;
154struct QMediaCaptureSession::ObjectTraits<QScreenCapture>
156 static constexpr bool IsCamera =
false;
157 static constexpr auto Member = &QMediaCaptureSessionPrivate::screenCapture;
158 static constexpr auto Setter = &QMediaCaptureSession::setScreenCapture;
159 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setScreenCapture;
160 static constexpr auto PlatformObjectProvider = &QScreenCapture::platformScreenCapture;
161 static constexpr auto ChangeNotifier = &QMediaCaptureSession::screenCaptureChanged;
165struct QMediaCaptureSession::ObjectTraits<QWindowCapture>
167 static constexpr bool IsCamera =
false;
168 static constexpr auto Member = &QMediaCaptureSessionPrivate::windowCapture;
169 static constexpr auto Setter = &QMediaCaptureSession::setWindowCapture;
170 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setWindowCapture;
171 static constexpr auto PlatformObjectProvider = &QWindowCapture::platformWindowCapture;
172 static constexpr auto ChangeNotifier = &QMediaCaptureSession::windowCaptureChanged;
176struct QMediaCaptureSession::ObjectTraits<QVideoFrameInput>
178 static constexpr bool IsCamera =
false;
179 static constexpr auto Member = &QMediaCaptureSessionPrivate::videoFrameInput;
180 static constexpr auto Setter = &QMediaCaptureSession::setVideoFrameInput;
181 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setVideoFrameInput;
182 static constexpr auto PlatformObjectProvider = &QVideoFrameInput::platformVideoFrameInput;
183 static constexpr auto ChangeNotifier = &QMediaCaptureSession::videoFrameInputChanged;
187struct QMediaCaptureSession::ObjectTraits<QAudioBufferInput>
189 static constexpr bool IsCamera =
false;
190 static constexpr auto Member = &QMediaCaptureSessionPrivate::audioBufferInput;
191 static constexpr auto Setter = &QMediaCaptureSession::setAudioBufferInput;
192 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setAudioBufferInput;
193 static constexpr auto PlatformObjectProvider = &QAudioBufferInput::platformAudioBufferInput;
194 static constexpr auto ChangeNotifier = &QMediaCaptureSession::audioBufferInputChanged;
198struct QMediaCaptureSession::ObjectTraits<QImageCapture>
200 static constexpr bool IsCamera =
false;
201 static constexpr auto Member = &QMediaCaptureSessionPrivate::imageCapture;
202 static constexpr auto Setter = &QMediaCaptureSession::setImageCapture;
203 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setImageCapture;
204 static constexpr auto PlatformObjectProvider = &QImageCapture::platformImageCapture;
205 static constexpr auto ChangeNotifier = &QMediaCaptureSession::imageCaptureChanged;
209struct QMediaCaptureSession::ObjectTraits<QMediaRecorder>
211 static constexpr bool IsCamera =
false;
212 static constexpr auto Member = &QMediaCaptureSessionPrivate::recorder;
213 static constexpr auto Setter = &QMediaCaptureSession::setRecorder;
214 static constexpr auto PlatformSetter = &QPlatformMediaCaptureSession::setMediaRecorder;
215 static constexpr auto PlatformObjectProvider = &QMediaRecorder::platformRecoder;
216 static constexpr auto ChangeNotifier = &QMediaCaptureSession::recorderChanged;
219template <
typename Object>
220void QMediaCaptureSession::setObject(Object *object) {
221 Q_D(QMediaCaptureSession);
223 using Traits = QMediaCaptureSession::ObjectTraits<Object>;
225 Object *oldObject = qobject_cast<Object *>(d->*Traits::Member);
226 if (oldObject == object)
229 if constexpr (Traits::IsCamera) {
230 if (!QPlatformMediaIntegration::instance()->isCameraSwitchingDuringRecordingSupported()
231 && recorder() && recorder()->recorderState() == QMediaRecorder::RecordingState) {
232 qWarning(
"This media backend does not support camera switching during recording");
237 d->*Traits::Member = object;
239 if (d->captureSession)
240 std::invoke(Traits::PlatformSetter, d->captureSession,
nullptr);
243 if (oldObject->captureSession() && oldObject->captureSession() !=
this)
244 std::invoke(Traits::Setter, oldObject->captureSession(),
nullptr);
245 oldObject->setCaptureSession(
nullptr);
249 if (
auto *otherSession = object->captureSession())
250 std::invoke(Traits::Setter, otherSession,
nullptr);
251 if (d->captureSession)
252 std::invoke(Traits::PlatformSetter, d->captureSession,
253 std::invoke(Traits::PlatformObjectProvider, object));
254 object->setCaptureSession(
this);
257 emit (
this->*Traits::ChangeNotifier)();
261
262
263QMediaCaptureSession::QMediaCaptureSession(QObject *parent)
264 : QObject{ *
new QMediaCaptureSessionPrivate, parent }
266 Q_D(QMediaCaptureSession);
268 auto maybeCaptureSession = QPlatformMediaIntegration::instance()->createCaptureSession();
269 if (maybeCaptureSession) {
270 d->captureSession.reset(maybeCaptureSession.value());
271 d->captureSession->setCaptureSession(
this);
273 qWarning() <<
"Failed to initialize QMediaCaptureSession" << maybeCaptureSession.error();
278
279
280QMediaCaptureSession::~QMediaCaptureSession()
282 Q_D(QMediaCaptureSession);
285 setNativeVideoSource(
nullptr);
286 setRecorder(
nullptr);
287 setImageCapture(
nullptr);
288 setScreenCapture(
nullptr);
289 setWindowCapture(
nullptr);
290 setVideoFrameInput(
nullptr);
291 setAudioBufferInput(
nullptr);
292 setAudioInput(
nullptr);
293 setAudioOutput(
nullptr);
294 d->setVideoSink(
nullptr);
295 d->captureSession.reset();
298
299
300
301
304
305
306
307
308QAudioInput *QMediaCaptureSession::audioInput()
const
310 Q_D(
const QMediaCaptureSession);
311 return d->audioInput;
315
316
317
318
319void QMediaCaptureSession::setAudioInput(QAudioInput *input)
321 Q_D(QMediaCaptureSession);
323 QAudioInput *oldInput = d->audioInput;
324 if (oldInput == input)
329 d->audioInput =
nullptr;
332 if (d->captureSession)
333 d->captureSession->setAudioInput(
nullptr);
335 oldInput->setDisconnectFunction({});
337 input->setDisconnectFunction([
this](){ setAudioInput(
nullptr); });
338 if (d->captureSession)
339 d->captureSession->setAudioInput(input->handle());
341 d->audioInput = input;
342 emit audioInputChanged();
346
347
348
349
350
351QAudioBufferInput *QMediaCaptureSession::audioBufferInput()
const
353 Q_D(
const QMediaCaptureSession);
355 return d->audioBufferInput;
358void QMediaCaptureSession::setAudioBufferInput(QAudioBufferInput *input)
364
365
366
367
368
369
370
373
374
375
376
377
378
379
380QCamera *QMediaCaptureSession::camera()
const
382 Q_D(
const QMediaCaptureSession);
386void QMediaCaptureSession::setCamera(QCamera *camera)
388#if QT_CONFIG(gstreamer_qt_api)
389 Q_D(QMediaCaptureSession);
390 if (d->nativeVideoSource && camera) {
392 qWarning(
"Setting camera, when gstreamer video source is connected, is not supported");
400QObject *QMediaCaptureSession::nativeVideoSource()
const
402 Q_D(
const QMediaCaptureSession);
403 return d->nativeVideoSource;
406void QMediaCaptureSession::setNativeVideoSource(QObject *videoSource)
408#if QT_CONFIG(gstreamer_qt_api)
409 Q_D(QMediaCaptureSession);
411 auto *gstreamerVideoSource = qobject_cast<QGStreamerVideoSource *>(videoSource);
413 if (videoSource && !gstreamerVideoSource) {
414 qCritical() <<
"Unsupported video source type; QGStreamerVideoSource is expected.";
418 if (d->camera && gstreamerVideoSource) {
420 qWarning(
"Setting GStreamer video source, when camera is connected, is not supported");
424 setObject(gstreamerVideoSource);
427 qCritical() <<
"Only gstreamer video source is supported";
433
434
435
436
437
438
439
440
443
444
445
446
447
448
449
450
451QScreenCapture *QMediaCaptureSession::screenCapture()
453 Q_D(QMediaCaptureSession);
454 return d->screenCapture;
457void QMediaCaptureSession::setScreenCapture(QScreenCapture *screenCapture)
459 setObject(screenCapture);
463
464
465
466
467
468
469
470
473
474
475
476
477
478
479
480
481QWindowCapture *QMediaCaptureSession::windowCapture()
483 Q_D(QMediaCaptureSession);
484 return d->windowCapture;
487void QMediaCaptureSession::setWindowCapture(QWindowCapture *windowCapture)
489 setObject(windowCapture);
493
494
495
496
497
498
499QVideoFrameInput *QMediaCaptureSession::videoFrameInput()
const
501 Q_D(
const QMediaCaptureSession);
502 return d->videoFrameInput;
505void QMediaCaptureSession::setVideoFrameInput(QVideoFrameInput *input)
511
512
513
514
515
516
517
519
520
521
522
523
524
525
526QImageCapture *QMediaCaptureSession::imageCapture()
528 Q_D(QMediaCaptureSession);
529 return d->imageCapture;
532void QMediaCaptureSession::setImageCapture(QImageCapture *imageCapture)
534 setObject(imageCapture);
537
538
539
540
541
542
543
545
546
547
548
549
550
551
553QMediaRecorder *QMediaCaptureSession::recorder()
555 Q_D(QMediaCaptureSession);
559void QMediaCaptureSession::setRecorder(QMediaRecorder *recorder)
564
565
566
567
568
569
570
571
572
573
575
576
577
578
579QObject *QMediaCaptureSession::videoOutput()
const
581 Q_D(
const QMediaCaptureSession);
582 return d->videoOutput;
585
586
587
588
589
590
591
592void QMediaCaptureSession::setVideoOutput(QObject *output)
594 Q_D(QMediaCaptureSession);
595 if (d->videoOutput == output)
597 QVideoSink *sink = qobject_cast<QVideoSink *>(output);
598 if (!sink && output) {
599 auto *mo = output->metaObject();
600 mo->invokeMethod(output,
"videoSink", Q_RETURN_ARG(QVideoSink *, sink));
602 d->videoOutput = output;
603 d->setVideoSink(sink);
607
608
609
610
611
612
613
614void QMediaCaptureSession::setVideoSink(QVideoSink *sink)
616 Q_D(QMediaCaptureSession);
617 d->videoOutput =
nullptr;
618 d->setVideoSink(sink);
622
623
624QVideoSink *QMediaCaptureSession::videoSink()
const
626 Q_D(
const QMediaCaptureSession);
630
631
632
633
634void QMediaCaptureSession::setAudioOutput(QAudioOutput *output)
636 Q_D(QMediaCaptureSession);
638 QAudioOutput *oldOutput = d->audioOutput;
639 if (oldOutput == output)
645 d->audioOutput =
nullptr;
647 if (d->captureSession)
648 d->captureSession->setAudioOutput(
nullptr);
650 oldOutput->setDisconnectFunction({});
652 output->setDisconnectFunction([
this](){ setAudioOutput(
nullptr); });
653 if (d->captureSession)
654 d->captureSession->setAudioOutput(output->handle());
656 d->audioOutput = output;
657 emit audioOutputChanged();
660
661
662
663
664
665
667
668
669
670
671QAudioOutput *QMediaCaptureSession::audioOutput()
const
673 Q_D(
const QMediaCaptureSession);
674 return d->audioOutput;
678
679
680QPlatformMediaCaptureSession *QMediaCaptureSession::platformSession()
const
682 Q_D(
const QMediaCaptureSession);
683 return d->captureSession.get();
686
687
688
689
692
693
694
695
698
699
700
701
704
705
706
707
710
711
712
713
716
717
718
719
722#include "moc_qmediacapturesession.cpp"
Combined button and popup list for selecting options.