1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
69
70
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qgstreamercapturesession.h"
#include "qgstreamerrecordercontrol.h"
#include "qgstreamermediacontainercontrol.h"
#include "qgstreameraudioencode.h"
#include "qgstreamervideoencode.h"
#include "qgstreamerimageencode.h"
#include <qmediarecorder.h>
#include <private/qgstreamervideorendererinterface_p.h>
#include <private/qgstreameraudioprobecontrol_p.h>
#include <private/qgstreamerbushelper_p.h>
#include <private/qgstutils_p.h>
#include <gst/gsttagsetter.h>
#include <gst/gstversion.h>
#include <gst/video/video.h>
#include <QtCore/qdebug.h>
#include <QtCore/qurl.h>
#include <QtCore/qset.h>
#include <QCoreApplication>
#include <QtCore/qmetaobject.h>
#include <QtCore/qfile.h>
#include <QtGui/qimage.h>
QT_BEGIN_NAMESPACE
QGstreamerCaptureSession::QGstreamerCaptureSession(QGstreamerCaptureSession::CaptureMode captureMode, QObject *parent)
:QObject(parent),
m_state(StoppedState),
m_pendingState(StoppedState),
m_waitingForEos(false),
m_pipelineMode(EmptyPipeline),
m_captureMode(captureMode),
m_audioProbe(0),
m_audioInputFactory(0),
m_audioPreviewFactory(0),
m_videoInputFactory(0),
m_viewfinder(0),
m_viewfinderInterface(0),
m_audioSrc(0),
m_audioTee(0),
m_audioPreviewQueue(0),
m_audioPreview(0),
m_audioVolume(0),
m_muted(false),
m_volume(1.0),
m_videoSrc(0),
m_videoTee(0),
m_videoPreviewQueue(0),
m_videoPreview(0),
m_imageCaptureBin(0),
m_encodeBin(0),
m_passImage(false),
m_passPrerollImage(false)
{
m_pipeline = gst_pipeline_new("media-capture-pipeline");
qt_gst_object_ref_sink(m_pipeline);
m_bus = gst_element_get_bus(m_pipeline);
m_busHelper = new QGstreamerBusHelper(m_bus, this);
m_busHelper->installMessageFilter(this);
m_audioEncodeControl = new QGstreamerAudioEncode(this);
m_videoEncodeControl = new QGstreamerVideoEncode(this);
m_imageEncodeControl = new QGstreamerImageEncode(this);
m_recorderControl = new QGstreamerRecorderControl(this);
m_mediaContainerControl = new QGstreamerMediaContainerControl(this);
setState(StoppedState);
}
QGstreamerCaptureSession::~QGstreamerCaptureSession()
{
setState(StoppedState);
gst_element_set_state(m_pipeline, GST_STATE_NULL);
gst_object_unref(GST_OBJECT(m_bus));
gst_object_unref(GST_OBJECT(m_pipeline));
}
void QGstreamerCaptureSession::setCaptureMode(CaptureMode mode)
{
m_captureMode = mode;
}
GstElement *QGstreamerCaptureSession::buildEncodeBin()
{
GstElement *encodeBin = gst_bin_new("encode-bin");
GstElement *muxer = gst_element_factory_make( m_mediaContainerControl->formatElementName().constData(), "muxer");
if (!muxer) {
qWarning() << "Could not create a media muxer element:" << m_mediaContainerControl->formatElementName();
gst_object_unref(encodeBin);
return 0;
}
// Output location was rejected in setOutputlocation() if not a local file
QUrl actualSink = QUrl::fromLocalFile(QDir::currentPath()).resolved(m_sink);
GstElement *fileSink = gst_element_factory_make("filesink", "filesink");
g_object_set(G_OBJECT(fileSink), "location", QFile::encodeName(actualSink.toLocalFile()).constData(), NULL);
gst_bin_add_many(GST_BIN(encodeBin), muxer, fileSink, NULL);
if (!gst_element_link(muxer, fileSink)) {
gst_object_unref(encodeBin);
return 0;
}
if (m_captureMode & Audio) {
GstElement *audioConvert = gst_element_factory_make("audioconvert", "audioconvert");
GstElement *audioQueue = gst_element_factory_make("queue", "audio-encode-queue");
m_audioVolume = gst_element_factory_make("volume", "volume");
gst_bin_add_many(GST_BIN(encodeBin), audioConvert, audioQueue, m_audioVolume, NULL);
GstElement *audioEncoder = m_audioEncodeControl->createEncoder();
if (!audioEncoder) {
gst_object_unref(encodeBin);
qWarning() << "Could not create an audio encoder element:" << m_audioEncodeControl->audioSettings().codec();
return 0;
}
gst_bin_add(GST_BIN(encodeBin), audioEncoder);
if (!gst_element_link_many(audioConvert, audioQueue, m_audioVolume, audioEncoder, muxer, NULL)) {
m_audioVolume = 0;
gst_object_unref(encodeBin);
return 0;
}
g_object_set(G_OBJECT(m_audioVolume), "mute", m_muted, NULL);
g_object_set(G_OBJECT(m_audioVolume), "volume", m_volume, NULL);
// add ghostpads
GstPad *pad = gst_element_get_static_pad(audioConvert, "sink");
gst_element_add_pad(GST_ELEMENT(encodeBin), gst_ghost_pad_new("audiosink", pad));
gst_object_unref(GST_OBJECT(pad));
}
if (m_captureMode & Video) {
GstElement *videoQueue = gst_element_factory_make("queue", "video-encode-queue");
GstElement *colorspace = gst_element_factory_make(QT_GSTREAMER_COLORCONVERSION_ELEMENT_NAME, "videoconvert-encoder");
GstElement *videoscale = gst_element_factory_make("videoscale","videoscale-encoder");
gst_bin_add_many(GST_BIN(encodeBin), videoQueue, colorspace, videoscale, NULL);
GstElement *videoEncoder = m_videoEncodeControl->createEncoder();
if (!videoEncoder) {
gst_object_unref(encodeBin);
qWarning() << "Could not create a video encoder element:" << m_videoEncodeControl->videoSettings().codec();
return 0;
}
gst_bin_add(GST_BIN(encodeBin), videoEncoder);
if (!gst_element_link_many(videoQueue, colorspace, videoscale, videoEncoder, muxer, NULL)) {
gst_object_unref(encodeBin);
return 0;
}
// add ghostpads
GstPad *pad = gst_element_get_static_pad(videoQueue, "sink");
gst_element_add_pad(GST_ELEMENT(encodeBin), gst_ghost_pad_new("videosink", pad));
gst_object_unref(GST_OBJECT(pad));
}
return encodeBin;
}
GstElement *QGstreamerCaptureSession::buildAudioSrc()
{
GstElement *audioSrc = 0;
if (m_audioInputFactory)
audioSrc = m_audioInputFactory->buildElement();
else {
QString elementName = "alsasrc";
QString device;
if (m_captureDevice.startsWith("alsa:")) {
device = m_captureDevice.mid(QString("alsa:").length());
} else if (m_captureDevice.startsWith("oss:")) {
elementName = "osssrc";
device = m_captureDevice.mid(QString("oss:").length());
} else if (m_captureDevice.startsWith("pulseaudio:")) {
elementName = "pulsesrc";
} else {
elementName = "autoaudiosrc";
}
audioSrc = gst_element_factory_make(elementName.toLatin1().constData(), "audio_src");
if (audioSrc && !device.isEmpty())
g_object_set(G_OBJECT(audioSrc), "device", device.toLocal8Bit().constData(), NULL);
}
if (!audioSrc) {
emit error(int(QMediaRecorder::ResourceError), tr("Could not create an audio source element"));
audioSrc = gst_element_factory_make("fakesrc", NULL);
}
return audioSrc;
}
GstElement *QGstreamerCaptureSession::buildAudioPreview()
{
GstElement *previewElement = 0;
if (m_audioPreviewFactory) {
previewElement = m_audioPreviewFactory->buildElement();
} else {
#if 1
previewElement = gst_element_factory_make("fakesink", "audio-preview");
#else
GstElement *bin = gst_bin_new("audio-preview-bin");
GstElement *visual = gst_element_factory_make("libvisual_lv_scope", "audio-preview");
GstElement *sink = gst_element_factory_make("ximagesink", NULL);
gst_bin_add_many(GST_BIN(bin), visual, sink, NULL);
gst_element_link_many(visual,sink, NULL);
// add ghostpads
GstPad *pad = gst_element_get_static_pad(visual, "sink");
Q_ASSERT(pad);
gst_element_add_pad(GST_ELEMENT(bin), gst_ghost_pad_new("audiosink", pad));
gst_object_unref(GST_OBJECT(pad));
previewElement = bin;
#endif
}
return previewElement;
}
GstElement *QGstreamerCaptureSession::buildVideoSrc()
{
GstElement *videoSrc = 0;
if (m_videoInputFactory) {
videoSrc = m_videoInputFactory->buildElement();
} else {
videoSrc = gst_element_factory_make("videotestsrc", "video_test_src");
//videoSrc = gst_element_factory_make("v4l2src", "video_test_src");
}
return videoSrc;
}
GstElement *QGstreamerCaptureSession::buildVideoPreview()
{
GstElement *previewElement = 0;
if (m_viewfinderInterface) {
GstElement *bin = gst_bin_new("video-preview-bin");
GstElement *colorspace = gst_element_factory_make(QT_GSTREAMER_COLORCONVERSION_ELEMENT_NAME, "videoconvert-preview");
GstElement *capsFilter = gst_element_factory_make("capsfilter", "capsfilter-video-preview");
GstElement *preview = m_viewfinderInterface->videoSink();
gst_bin_add_many(GST_BIN(bin), colorspace, capsFilter, preview, NULL);
gst_element_link(colorspace,capsFilter);
gst_element_link(capsFilter,preview);
QSize resolution;
qreal frameRate = 0;
if (m_captureMode & Video) {
QVideoEncoderSettings videoSettings = m_videoEncodeControl->videoSettings();
resolution = videoSettings.resolution();
frameRate = videoSettings.frameRate();
} else if (m_captureMode & Image) {
resolution = m_imageEncodeControl->imageSettings().resolution();
}
GstCaps *caps = QGstUtils::videoFilterCaps();
if (!resolution.isEmpty()) {
gst_caps_set_simple(caps, "width", G_TYPE_INT, resolution.width(), NULL);
gst_caps_set_simple(caps, "height", G_TYPE_INT, resolution.height(), NULL);
}
if (frameRate > 0.001) {
QPair<int,int> rate = m_videoEncodeControl->rateAsRational();
//qDebug() << "frame rate:" << num << denum;
gst_caps_set_simple(caps, "framerate", GST_TYPE_FRACTION, rate.first, rate.second, NULL);
}
//qDebug() << "set video preview caps filter:" << gst_caps_to_string(caps);
g_object_set(G_OBJECT(capsFilter), "caps", caps, NULL);
gst_caps_unref(caps);
// add ghostpads
GstPad *pad = gst_element_get_static_pad(colorspace, "sink");
Q_ASSERT(pad);
gst_element_add_pad(GST_ELEMENT(bin), gst_ghost_pad_new("videosink", pad));
gst_object_unref(GST_OBJECT(pad));
previewElement = bin;
} else {
#if 1
previewElement = gst_element_factory_make("fakesink", "video-preview");
#else
GstElement *bin = gst_bin_new("video-preview-bin");
GstElement *colorspace = gst_element_factory_make(QT_GSTREAMER_COLORCONVERSION_ELEMENT_NAME, "videoconvert-preview");
GstElement *preview = gst_element_factory_make("ximagesink", "video-preview");
gst_bin_add_many(GST_BIN(bin), colorspace, preview, NULL);
gst_element_link(colorspace,preview);
// add ghostpads
GstPad *pad = gst_element_get_static_pad(colorspace, "sink");
Q_ASSERT(pad);
gst_element_add_pad(GST_ELEMENT(bin), gst_ghost_pad_new("videosink", pad));
gst_object_unref(GST_OBJECT(pad));
previewElement = bin;
#endif
}
return previewElement;
}
void QGstreamerCaptureSession::probeCaps(GstCaps *caps)
{
#if GST_CHECK_VERSION(1,0,0)
gst_video_info_from_caps(&m_previewInfo, caps);
#else
Q_UNUSED(caps);
#endif
}
bool QGstreamerCaptureSession::probeBuffer(GstBuffer *buffer)
{
if (m_passPrerollImage) {
m_passImage = false;
m_passPrerollImage = false;
return true;
} else if (!m_passImage) {
return false;
}
m_passImage = false;
#if GST_CHECK_VERSION(1,0,0)
QImage img = QGstUtils::bufferToImage(buffer, m_previewInfo);
#else
QImage img = QGstUtils::bufferToImage(buffer);
#endif
if (img.isNull())
return true;
static QMetaMethod exposedSignal = QMetaMethod::fromSignal(&QGstreamerCaptureSession::imageExposed);
exposedSignal.invoke(this,
Qt::QueuedConnection,
Q_ARG(int,m_imageRequestId));
static QMetaMethod capturedSignal = QMetaMethod::fromSignal(&QGstreamerCaptureSession::imageCaptured);
capturedSignal.invoke(this,
Qt::QueuedConnection,
Q_ARG(int,m_imageRequestId),
Q_ARG(QImage,img));
return true;
}
static gboolean saveImageFilter(GstElement *element,
GstBuffer *buffer,
GstPad *pad,
void *appdata)
{
Q_UNUSED(element);
Q_UNUSED(pad);
QGstreamerCaptureSession *session = (QGstreamerCaptureSession *)appdata;
QString fileName = session->m_imageFileName;
if (!fileName.isEmpty()) {
QFile f(fileName);
if (f.open(QFile::WriteOnly)) {
#if GST_CHECK_VERSION(1,0,0)
GstMapInfo info;
if (gst_buffer_map(buffer, &info, GST_MAP_READ)) {
f.write(reinterpret_cast<const char *>(info.data), info.size);
gst_buffer_unmap(buffer, &info);
}
#else
f.write(reinterpret_cast<const char *>(buffer->data), buffer->size);
#endif
f.close();
static QMetaMethod savedSignal = QMetaMethod::fromSignal(&QGstreamerCaptureSession::imageSaved);
savedSignal.invoke(session,
Qt::QueuedConnection,
Q_ARG(int,session->m_imageRequestId),
Q_ARG(QString,fileName));
}
}
return TRUE;
}
GstElement *QGstreamerCaptureSession::buildImageCapture()
{
GstElement *bin = gst_bin_new("image-capture-bin");
GstElement *queue = gst_element_factory_make("queue", "queue-image-capture");
GstElement *colorspace = gst_element_factory_make(QT_GSTREAMER_COLORCONVERSION_ELEMENT_NAME, "videoconvert-image-capture");
GstElement *encoder = gst_element_factory_make("jpegenc", "image-encoder");
GstElement *sink = gst_element_factory_make("fakesink","sink-image-capture");
GstPad *pad = gst_element_get_static_pad(queue, "src");
Q_ASSERT(pad);
addProbeToPad(pad, false);
gst_object_unref(GST_OBJECT(pad));
g_object_set(G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
g_signal_connect(G_OBJECT(sink), "handoff", G_CALLBACK(saveImageFilter), this);
gst_bin_add_many(GST_BIN(bin), queue, colorspace, encoder, sink, NULL);
gst_element_link_many(queue, colorspace, encoder, sink, NULL);
// add ghostpads
pad = gst_element_get_static_pad(queue, "sink");
Q_ASSERT(pad);
gst_element_add_pad(GST_ELEMENT(bin), gst_ghost_pad_new("imagesink", pad));
gst_object_unref(GST_OBJECT(pad));
m_passImage = false;
m_passPrerollImage = true;
m_imageFileName = QString();
return bin;
}
void QGstreamerCaptureSession::captureImage(int requestId, const QString &fileName)
{
m_imageRequestId = requestId;
m_imageFileName = fileName;
m_passImage = true;
}
#define REMOVE_ELEMENT(element) { if (element) {gst_bin_remove(GST_BIN(m_pipeline), element); element = 0;} }
#define UNREF_ELEMENT(element) { if (element) { gst_object_unref(GST_OBJECT(element)); element = 0; } }
bool QGstreamerCaptureSession::rebuildGraph(QGstreamerCaptureSession::PipelineMode newMode)
{
removeAudioBufferProbe();
REMOVE_ELEMENT(m_audioSrc);
REMOVE_ELEMENT(m_audioPreview);
REMOVE_ELEMENT(m_audioPreviewQueue);
REMOVE_ELEMENT(m_audioTee);
REMOVE_ELEMENT(m_videoSrc);
REMOVE_ELEMENT(m_videoPreview);
REMOVE_ELEMENT(m_videoPreviewQueue);
REMOVE_ELEMENT(m_videoTee);
REMOVE_ELEMENT(m_encodeBin);
REMOVE_ELEMENT(m_imageCaptureBin);
m_audioVolume = 0;
bool ok = true;
switch (newMode) {
case EmptyPipeline:
break;
case PreviewPipeline:
if (m_captureMode & Audio) {
m_audioSrc = buildAudioSrc();
m_audioPreview = buildAudioPreview();
ok &= m_audioSrc && m_audioPreview;
if (ok) {
gst_bin_add_many(GST_BIN(m_pipeline), m_audioSrc, m_audioPreview, NULL);
ok &= gst_element_link(m_audioSrc, m_audioPreview);
} else {
UNREF_ELEMENT(m_audioSrc);
UNREF_ELEMENT(m_audioPreview);
}
}
if (m_captureMode & Video || m_captureMode & Image) {
m_videoSrc = buildVideoSrc();
m_videoTee = gst_element_factory_make("tee", "video-preview-tee");
m_videoPreviewQueue = gst_element_factory_make("queue", "video-preview-queue");
m_videoPreview = buildVideoPreview();
m_imageCaptureBin = buildImageCapture();
ok &= m_videoSrc && m_videoTee && m_videoPreviewQueue && m_videoPreview && m_imageCaptureBin;
if (ok) {
gst_bin_add_many(GST_BIN(m_pipeline), m_videoSrc, m_videoTee,
m_videoPreviewQueue, m_videoPreview,
m_imageCaptureBin, NULL);
ok &= gst_element_link(m_videoSrc, m_videoTee);
ok &= gst_element_link(m_videoTee, m_videoPreviewQueue);
ok &= gst_element_link(m_videoPreviewQueue, m_videoPreview);
ok &= gst_element_link(m_videoTee, m_imageCaptureBin);
} else {
UNREF_ELEMENT(m_videoSrc);
UNREF_ELEMENT(m_videoTee);
UNREF_ELEMENT(m_videoPreviewQueue);
UNREF_ELEMENT(m_videoPreview);
UNREF_ELEMENT(m_imageCaptureBin);
}
}
break;
case RecordingPipeline:
m_encodeBin = buildEncodeBin();
gst_bin_add(GST_BIN(m_pipeline), m_encodeBin);
if (m_captureMode & Audio) {
m_audioSrc = buildAudioSrc();
ok &= m_audioSrc != 0;
gst_bin_add(GST_BIN(m_pipeline), m_audioSrc);
ok &= gst_element_link(m_audioSrc, m_encodeBin);
}
if (m_captureMode & Video) {
m_videoSrc = buildVideoSrc();
ok &= m_videoSrc != 0;
gst_bin_add(GST_BIN(m_pipeline), m_videoSrc);
ok &= gst_element_link(m_videoSrc, m_encodeBin);
}
if (!m_metaData.isEmpty())
setMetaData(m_metaData);
break;
case PreviewAndRecordingPipeline:
m_encodeBin = buildEncodeBin();
if (m_encodeBin)
gst_bin_add(GST_BIN(m_pipeline), m_encodeBin);
ok &= m_encodeBin != 0;
if (ok && m_captureMode & Audio) {
m_audioSrc = buildAudioSrc();
m_audioPreview = buildAudioPreview();
m_audioTee = gst_element_factory_make("tee", NULL);
m_audioPreviewQueue = gst_element_factory_make("queue", NULL);
ok &= m_audioSrc && m_audioPreview && m_audioTee && m_audioPreviewQueue;
if (ok) {
gst_bin_add_many(GST_BIN(m_pipeline), m_audioSrc, m_audioTee,
m_audioPreviewQueue, m_audioPreview, NULL);
ok &= gst_element_link(m_audioSrc, m_audioTee);
ok &= gst_element_link(m_audioTee, m_audioPreviewQueue);
ok &= gst_element_link(m_audioPreviewQueue, m_audioPreview);
ok &= gst_element_link(m_audioTee, m_encodeBin);
} else {
UNREF_ELEMENT(m_audioSrc);
UNREF_ELEMENT(m_audioPreview);
UNREF_ELEMENT(m_audioTee);
UNREF_ELEMENT(m_audioPreviewQueue);
}
}
if (ok && (m_captureMode & Video || m_captureMode & Image)) {
m_videoSrc = buildVideoSrc();
m_videoPreview = buildVideoPreview();
m_videoTee = gst_element_factory_make("tee", NULL);
m_videoPreviewQueue = gst_element_factory_make("queue", NULL);
ok &= m_videoSrc && m_videoPreview && m_videoTee && m_videoPreviewQueue;
if (ok) {
gst_bin_add_many(GST_BIN(m_pipeline), m_videoSrc, m_videoTee,
m_videoPreviewQueue, m_videoPreview, NULL);
ok &= gst_element_link(m_videoSrc, m_videoTee);
ok &= gst_element_link(m_videoTee, m_videoPreviewQueue);
ok &= gst_element_link(m_videoPreviewQueue, m_videoPreview);
} else {
UNREF_ELEMENT(m_videoSrc);
UNREF_ELEMENT(m_videoTee);
UNREF_ELEMENT(m_videoPreviewQueue);
UNREF_ELEMENT(m_videoPreview);
}
if (ok && (m_captureMode & Video))
ok &= gst_element_link(m_videoTee, m_encodeBin);
}
if (!m_metaData.isEmpty())
setMetaData(m_metaData);
break;
}
if (!ok) {
emit error(int(QMediaRecorder::FormatError),tr("Failed to build media capture pipeline."));
}
dumpGraph( QString("rebuild_graph_%1_%2").arg(m_pipelineMode).arg(newMode) );
if (m_encodeBin) {
QString fileName = QString("rebuild_graph_encode_%1_%2").arg(m_pipelineMode).arg(newMode);
#if !(GST_DISABLE_GST_DEBUG) && (GST_VERSION_MAJOR >= 0) && (GST_VERSION_MINOR >= 10) && (GST_VERSION_MICRO >= 19)
_gst_debug_bin_to_dot_file(GST_BIN(m_encodeBin), GST_DEBUG_GRAPH_SHOW_ALL, fileName.toLatin1());
#endif
}
if (ok) {
addAudioBufferProbe();
m_pipelineMode = newMode;
} else {
m_pipelineMode = EmptyPipeline;
REMOVE_ELEMENT(m_audioSrc);
REMOVE_ELEMENT(m_audioPreview);
REMOVE_ELEMENT(m_audioPreviewQueue);
REMOVE_ELEMENT(m_audioTee);
REMOVE_ELEMENT(m_videoSrc);
REMOVE_ELEMENT(m_videoPreview);
REMOVE_ELEMENT(m_videoPreviewQueue);
REMOVE_ELEMENT(m_videoTee);
REMOVE_ELEMENT(m_encodeBin);
}
return ok;
}
void QGstreamerCaptureSession::dumpGraph(const QString &fileName)
{
#if !(GST_DISABLE_GST_DEBUG) && (GST_VERSION_MAJOR >= 0) && (GST_VERSION_MINOR >= 10) && (GST_VERSION_MICRO >= 19)
_gst_debug_bin_to_dot_file(GST_BIN(m_pipeline),
GstDebugGraphDetails(/*GST_DEBUG_GRAPH_SHOW_ALL |*/ GST_DEBUG_GRAPH_SHOW_MEDIA_TYPE | GST_DEBUG_GRAPH_SHOW_NON_DEFAULT_PARAMS | GST_DEBUG_GRAPH_SHOW_STATES),
fileName.toLatin1());
#else
Q_UNUSED(fileName);
#endif
}
QUrl QGstreamerCaptureSession::outputLocation() const
{
return m_sink;
}
bool QGstreamerCaptureSession::setOutputLocation(const QUrl& sink)
{
if (!sink.isRelative() && !sink.isLocalFile()) {
qWarning("Output location must be a local file");
return false;
}
m_sink = sink;
return true;
}
void QGstreamerCaptureSession::setAudioInput(QGstreamerElementFactory *audioInput)
{
m_audioInputFactory = audioInput;
}
void QGstreamerCaptureSession::setAudioPreview(QGstreamerElementFactory *audioPreview)
{
m_audioPreviewFactory = audioPreview;
}
void QGstreamerCaptureSession::setVideoInput(QGstreamerVideoInput *videoInput)
{
m_videoInputFactory = videoInput;
}
void QGstreamerCaptureSession::setVideoPreview(QObject *viewfinder)
{
m_viewfinderInterface = qobject_cast<QGstreamerVideoRendererInterface*>(viewfinder);
if (!m_viewfinderInterface)
viewfinder = 0;
if (m_viewfinder != viewfinder) {
bool oldReady = isReady();
if (m_viewfinder) {
disconnect(m_viewfinder, SIGNAL(sinkChanged()),
this, SIGNAL(viewfinderChanged()));
disconnect(m_viewfinder, SIGNAL(readyChanged(bool)),
this, SIGNAL(readyChanged(bool)));
m_busHelper->removeMessageFilter(m_viewfinder);
}
m_viewfinder = viewfinder;
//m_viewfinderHasChanged = true;
if (m_viewfinder) {
connect(m_viewfinder, SIGNAL(sinkChanged()),
this, SIGNAL(viewfinderChanged()));
connect(m_viewfinder, SIGNAL(readyChanged(bool)),
this, SIGNAL(readyChanged(bool)));
m_busHelper->installMessageFilter(m_viewfinder);
}
emit viewfinderChanged();
if (oldReady != isReady())
emit readyChanged(isReady());
}
}
bool QGstreamerCaptureSession::isReady() const
{
//it's possible to use QCamera without any viewfinder attached
return !m_viewfinderInterface || m_viewfinderInterface->isReady();
}
QGstreamerCaptureSession::State QGstreamerCaptureSession::state() const
{
return m_state;
}
QGstreamerCaptureSession::State QGstreamerCaptureSession::pendingState() const
{
return m_pendingState;
}
void QGstreamerCaptureSession::setState(QGstreamerCaptureSession::State newState)
{
if (newState == m_pendingState && !m_waitingForEos)
return;
m_pendingState = newState;
PipelineMode newMode = EmptyPipeline;
switch (newState) {
case PausedState:
case RecordingState:
newMode = PreviewAndRecordingPipeline;
break;
case PreviewState:
newMode = PreviewPipeline;
break;
case StoppedState:
newMode = EmptyPipeline;
break;
}
if (newMode != m_pipelineMode) {
if (m_pipelineMode == PreviewAndRecordingPipeline) {
if (!m_waitingForEos) {
m_waitingForEos = true;
//qDebug() << "Waiting for EOS";
//with live sources it's necessary to send EOS even to pipeline
//before going to STOPPED state
gst_element_send_event(m_pipeline, gst_event_new_eos());
// Unless gstreamer is in GST_STATE_PLAYING our EOS message will not be received.
gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
return;
} else {
m_waitingForEos = false;
//qDebug() << "EOS received";
}
}
//select suitable default codecs/containers, if necessary
m_recorderControl->applySettings();
gst_element_set_state(m_pipeline, GST_STATE_NULL);
if (!rebuildGraph(newMode)) {
m_pendingState = StoppedState;
m_state = StoppedState;
emit stateChanged(StoppedState);
return;
}
}
switch (newState) {
case PausedState:
gst_element_set_state(m_pipeline, GST_STATE_PAUSED);
break;
case RecordingState:
case PreviewState:
gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
break;
case StoppedState:
gst_element_set_state(m_pipeline, GST_STATE_NULL);
}
//we have to do it here, since gstreamer will not emit bus messages any more
if (newState == StoppedState) {
m_state = StoppedState;
emit stateChanged(StoppedState);
}
}
qint64 QGstreamerCaptureSession::duration() const
{
gint64 duration = 0;
if (m_encodeBin && qt_gst_element_query_position(m_encodeBin, GST_FORMAT_TIME, &duration))
return duration / 1000000;
else
return 0;
}
void QGstreamerCaptureSession::setCaptureDevice(const QString &deviceName)
{
m_captureDevice = deviceName;
}
void QGstreamerCaptureSession::setMetaData(const QMap<QByteArray, QVariant> &data)
{
//qDebug() << "QGstreamerCaptureSession::setMetaData" << data;
m_metaData = data;
if (m_encodeBin)
QGstUtils::setMetaData(GST_BIN(m_encodeBin), data);
}
bool QGstreamerCaptureSession::processBusMessage(const QGstreamerMessage &message)
{
GstMessage* gm = message.rawMessage();
if (gm) {
if (GST_MESSAGE_TYPE(gm) == GST_MESSAGE_ERROR) {
GError *err;
gchar *debug;
gst_message_parse_error (gm, &err, &debug);
emit error(int(QMediaRecorder::ResourceError),QString::fromUtf8(err->message));
g_error_free (err);
g_free (debug);
}
if (GST_MESSAGE_SRC(gm) == GST_OBJECT_CAST(m_pipeline)) {
switch (GST_MESSAGE_TYPE(gm)) {
case GST_MESSAGE_DURATION:
break;
case GST_MESSAGE_EOS:
if (m_waitingForEos)
setState(m_pendingState);
break;
case GST_MESSAGE_STATE_CHANGED:
{
GstState oldState;
GstState newState;
GstState pending;
gst_message_parse_state_changed(gm, &oldState, &newState, &pending);
QStringList states;
states << "GST_STATE_VOID_PENDING" << "GST_STATE_NULL" << "GST_STATE_READY" << "GST_STATE_PAUSED" << "GST_STATE_PLAYING";
/*
qDebug() << QString("state changed: old: %1 new: %2 pending: %3") \
.arg(states[oldState]) \
.arg(states[newState]) \
.arg(states[pending]);
#define ENUM_NAME(c,e,v) (c::staticMetaObject.enumerator(c::staticMetaObject.indexOfEnumerator(e)).valueToKey((v)))
qDebug() << "Current session state:" << ENUM_NAME(QGstreamerCaptureSession,"State",m_state);
qDebug() << "Pending session state:" << ENUM_NAME(QGstreamerCaptureSession,"State",m_pendingState);
*/
switch (newState) {
case GST_STATE_VOID_PENDING:
case GST_STATE_NULL:
case GST_STATE_READY:
if (m_state != StoppedState && m_pendingState == StoppedState) {
emit stateChanged(m_state = StoppedState);
dumpGraph("stopped");
}
break;
case GST_STATE_PAUSED:
if (m_state != PausedState && m_pendingState == PausedState)
emit stateChanged(m_state = PausedState);
dumpGraph("paused");
if (m_pipelineMode == RecordingPipeline && !m_metaData.isEmpty())
setMetaData(m_metaData);
break;
case GST_STATE_PLAYING:
{
if ((m_pendingState == PreviewState || m_pendingState == RecordingState) &&
m_state != m_pendingState)
{
m_state = m_pendingState;
emit stateChanged(m_state);
}
if (m_pipelineMode == PreviewPipeline)
dumpGraph("preview");
else
dumpGraph("recording");
}
break;
}
}
break;
default:
break;
}
//qDebug() << "New session state:" << ENUM_NAME(QGstreamerCaptureSession,"State",m_state);
}
}
return false;
}
void QGstreamerCaptureSession::setMuted(bool muted)
{
if (bool(m_muted) != muted) {
m_muted = muted;
if (m_audioVolume)
g_object_set(G_OBJECT(m_audioVolume), "mute", m_muted, NULL);
emit mutedChanged(muted);
}
}
void QGstreamerCaptureSession::setVolume(qreal volume)
{
if (!qFuzzyCompare(double(volume), m_volume)) {
m_volume = volume;
if (m_audioVolume)
g_object_set(G_OBJECT(m_audioVolume), "volume", m_volume, NULL);
emit volumeChanged(volume);
}
}
void QGstreamerCaptureSession::addProbe(QGstreamerAudioProbeControl* probe)
{
Q_ASSERT(!m_audioProbe);
m_audioProbe = probe;
addAudioBufferProbe();
}
void QGstreamerCaptureSession::removeProbe(QGstreamerAudioProbeControl* probe)
{
Q_ASSERT(m_audioProbe == probe);
removeAudioBufferProbe();
m_audioProbe = 0;
}
GstPad *QGstreamerCaptureSession::getAudioProbePad()
{
// first see if preview element is available
if (m_audioPreview) {
GstPad *pad = gst_element_get_static_pad(m_audioPreview, "sink");
if (pad)
return pad;
}
// preview element is not available,
// try to use sink pin of audio encoder.
if (m_encodeBin) {
GstElement *audioEncoder = gst_bin_get_by_name(GST_BIN(m_encodeBin), "audio-encoder-bin");
if (audioEncoder) {
GstPad *pad = gst_element_get_static_pad(audioEncoder, "sink");
gst_object_unref(audioEncoder);
if (pad)
return pad;
}
}
return 0;
}
void QGstreamerCaptureSession::removeAudioBufferProbe()
{
if (!m_audioProbe)
return;
GstPad *pad = getAudioProbePad();
if (pad) {
m_audioProbe->removeProbeFromPad(pad);
gst_object_unref(GST_OBJECT(pad));
}
}
void QGstreamerCaptureSession::addAudioBufferProbe()
{
if (!m_audioProbe)
return;
GstPad *pad = getAudioProbePad();
if (pad) {
m_audioProbe->addProbeToPad(pad);
gst_object_unref(GST_OBJECT(pad));
}
}
QT_END_NAMESPACE
|