/
gstparsebin.c
4377 lines (3716 loc) · 135 KB
/
gstparsebin.c
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
/* GStreamer
* Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
* Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
* Copyright (C) <2011> Hewlett-Packard Development Company, L.P.
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
* Copyright (C) <2013> Collabora Ltd.
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
* Copyright (C) <2015-2016> Centricular Ltd
* @author: Edward Hervey <edward@centricular.com>
* @author: Jan Schmidt <jan@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-parsebin
* @title: parsebin
*
* #GstBin that auto-magically constructs a parsing pipeline
* using available parsers and demuxers via auto-plugging.
*
* parsebin unpacks the contents of the input stream to the
* level of parsed elementary streams, but unlike decodebin
* it doesn't connect decoder elements. The output pads
* produce packetised encoded data with timestamps where possible,
* or send missing-element messages where not.
*
* > parsebin is still experimental API and a technology preview.
* > Its behaviour and exposed API is subject to change.
*/
/* Implementation notes:
*
* The following section describes how ParseBin works internally.
*
* The first part of ParseBin is its typefind element, which tries
* to determine the media type of the input stream. If the type is found
* autoplugging starts.
*
* ParseBin internally organizes the elements it autoplugged into
* GstParseChains and GstParseGroups. A parse chain is a single chain
* of parsing, this
* means that if ParseBin ever autoplugs an element with two+ srcpads
* (e.g. a demuxer) this will end the chain and everything following this
* demuxer will be put into parse groups below the chain. Otherwise,
* if an element has a single srcpad that outputs raw data the parse chain
* is ended too and a GstParsePad is stored and blocked.
*
* A parse group combines a number of chains that are created by a
* demuxer element.
*
* This continues until the top-level parse chain is complete. A parse
* chain is complete if it either ends with a blocked elementary stream,
* if autoplugging stopped because no suitable plugins could be found
* or if the active group is complete. A parse group on the other hand
* is complete if all child chains are complete.
*
* If this happens at some point, all end pads of all active groups are exposed.
* For this ParseBin adds the end pads, and then unblocks them. Now playback starts.
*
* If one of the chains that end on a endpad receives EOS ParseBin checks
* if all chains and groups are drained. In that case everything goes into EOS.
* If there is a chain where the active group is drained but there exist next
* groups, the active group is hidden (endpads are removed) and the next group
* is exposed. This means that in some cases more pads may be created even
* after the initial no-more-pads signal. This happens for example with
* so-called "chained oggs", most commonly found among ogg/vorbis internet
* radio streams.
*
* Note 1: If we're talking about blocked endpads this really means that the
* *target* pads of the endpads are blocked. Pads that are exposed to the outside
* should never ever be blocked!
*
* Note 2: If a group is complete and the parent's chain demuxer adds new pads
* but never signaled no-more-pads this additional pads will be ignored!
*
*/
/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
* with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst-i18n-plugin.h>
#include <string.h>
#include <gst/gst.h>
#include <gst/pbutils/pbutils.h>
#include "gstplay-enum.h"
#include "gstplaybackelements.h"
#include "gstplaybackutils.h"
#include "gstrawcaps.h"
/* generic templates */
static GstStaticPadTemplate parse_bin_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate parse_bin_src_template =
GST_STATIC_PAD_TEMPLATE ("src_%u",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS_ANY);
GST_DEBUG_CATEGORY_STATIC (gst_parse_bin_debug);
#define GST_CAT_DEFAULT gst_parse_bin_debug
typedef struct _GstPendingPad GstPendingPad;
typedef struct _GstParseElement GstParseElement;
typedef struct _GstParseChain GstParseChain;
typedef struct _GstParseGroup GstParseGroup;
typedef struct _GstParsePad GstParsePad;
typedef GstGhostPadClass GstParsePadClass;
typedef struct _GstParseBin GstParseBin;
typedef struct _GstParseBinClass GstParseBinClass;
#define GST_TYPE_PARSE_BIN (gst_parse_bin_get_type())
#define GST_PARSE_BIN_CAST(obj) ((GstParseBin*)(obj))
#define GST_PARSE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PARSE_BIN,GstParseBin))
#define GST_PARSE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PARSE_BIN,GstParseBinClass))
#define GST_IS_parse_bin(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PARSE_BIN))
#define GST_IS_parse_bin_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PARSE_BIN))
/**
* GstParseBin:
*
* The opaque #GstParseBin data structure
*/
struct _GstParseBin
{
GstBin bin; /* we extend GstBin */
/* properties */
gchar *encoding; /* encoding of subtitles */
guint64 connection_speed;
GstElement *typefind; /* this holds the typefind object */
GMutex expose_lock; /* Protects exposal and removal of groups */
GstParseChain *parse_chain; /* Top level parse chain */
guint nbpads; /* unique identifier for source pads */
GMutex factories_lock;
guint32 factories_cookie; /* Cookie from last time when factories was updated */
GList *factories; /* factories we can use for selecting elements */
GMutex subtitle_lock; /* Protects changes to subtitles and encoding */
GList *subtitles; /* List of elements with subtitle-encoding,
* protected by above mutex! */
gboolean have_type; /* if we received the have_type signal */
guint have_type_id; /* signal id for have-type from typefind */
GMutex dyn_lock; /* lock protecting pad blocking */
gboolean shutdown; /* if we are shutting down */
GList *blocked_pads; /* pads that have set to block */
gboolean expose_allstreams; /* Whether to expose unknown type streams or not */
GList *filtered; /* elements for which error messages are filtered */
GList *filtered_errors; /* filtered error messages */
GMutex cleanup_lock; /* Mutex used to protect the cleanup thread */
GThread *cleanup_thread; /* thread used to free chains asynchronously.
* We store it to make sure we end up joining it
* before stopping the element.
* Protected by the object lock */
};
struct _GstParseBinClass
{
GstBinClass parent_class;
/* signal fired when we found a pad that we cannot parse */
void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
/* signal fired to know if we continue trying to parse the given caps */
gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
GstCaps * caps);
/* signal fired to get a list of factories to try to autoplug */
GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
GstCaps * caps);
/* signal fired to sort the factories */
GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
GstCaps * caps, GValueArray * factories);
/* signal fired to select from the proposed list of factories */
GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
GstPad * pad, GstCaps * caps, GstElementFactory * factory);
/* signal fired when a autoplugged element that is not linked downstream
* or exposed wants to query something */
gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
GstQuery * query);
/* fired when the last group is drained */
void (*drained) (GstElement * element);
};
/* signals */
enum
{
SIGNAL_UNKNOWN_TYPE,
SIGNAL_AUTOPLUG_CONTINUE,
SIGNAL_AUTOPLUG_FACTORIES,
SIGNAL_AUTOPLUG_SELECT,
SIGNAL_AUTOPLUG_SORT,
SIGNAL_AUTOPLUG_QUERY,
SIGNAL_DRAINED,
LAST_SIGNAL
};
#define DEFAULT_SUBTITLE_ENCODING NULL
/* by default we use the automatic values above */
#define DEFAULT_EXPOSE_ALL_STREAMS TRUE
#define DEFAULT_CONNECTION_SPEED 0
/* Properties */
enum
{
PROP_0,
PROP_SUBTITLE_ENCODING,
PROP_SINK_CAPS,
PROP_EXPOSE_ALL_STREAMS,
PROP_CONNECTION_SPEED
};
static GstBinClass *parent_class;
static guint gst_parse_bin_signals[LAST_SIGNAL] = { 0 };
static void type_found (GstElement * typefind, guint probability,
GstCaps * caps, GstParseBin * parse_bin);
static gboolean gst_parse_bin_autoplug_continue (GstElement * element,
GstPad * pad, GstCaps * caps);
static GValueArray *gst_parse_bin_autoplug_factories (GstElement *
element, GstPad * pad, GstCaps * caps);
static GValueArray *gst_parse_bin_autoplug_sort (GstElement * element,
GstPad * pad, GstCaps * caps, GValueArray * factories);
static GstAutoplugSelectResult gst_parse_bin_autoplug_select (GstElement *
element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
static gboolean gst_parse_bin_autoplug_query (GstElement * element,
GstPad * pad, GstQuery * query);
static void gst_parse_bin_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_parse_bin_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
GstParseChain * chain);
static GstStateChangeReturn gst_parse_bin_change_state (GstElement * element,
GstStateChange transition);
static void gst_parse_bin_handle_message (GstBin * bin, GstMessage * message);
static void gst_parse_pad_update_caps (GstParsePad * parsepad, GstCaps * caps);
static void gst_parse_pad_update_tags (GstParsePad * parsepad,
GstTagList * tags);
static GstEvent *gst_parse_pad_stream_start_event (GstParsePad * parsepad,
GstEvent * event);
static void gst_parse_pad_update_stream_collection (GstParsePad * parsepad,
GstStreamCollection * collection);
static GstCaps *get_pad_caps (GstPad * pad);
static GstStreamType guess_stream_type_from_caps (GstCaps * caps);
#define EXPOSE_LOCK(parsebin) G_STMT_START { \
GST_LOG_OBJECT (parsebin, \
"expose locking from thread %p", \
g_thread_self ()); \
g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->expose_lock); \
GST_LOG_OBJECT (parsebin, \
"expose locked from thread %p", \
g_thread_self ()); \
} G_STMT_END
#define EXPOSE_UNLOCK(parsebin) G_STMT_START { \
GST_LOG_OBJECT (parsebin, \
"expose unlocking from thread %p", \
g_thread_self ()); \
g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->expose_lock); \
} G_STMT_END
#define DYN_LOCK(parsebin) G_STMT_START { \
GST_LOG_OBJECT (parsebin, \
"dynlocking from thread %p", \
g_thread_self ()); \
g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->dyn_lock); \
GST_LOG_OBJECT (parsebin, \
"dynlocked from thread %p", \
g_thread_self ()); \
} G_STMT_END
#define DYN_UNLOCK(parsebin) G_STMT_START { \
GST_LOG_OBJECT (parsebin, \
"dynunlocking from thread %p", \
g_thread_self ()); \
g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->dyn_lock); \
} G_STMT_END
#define SUBTITLE_LOCK(parsebin) G_STMT_START { \
GST_LOG_OBJECT (parsebin, \
"subtitle locking from thread %p", \
g_thread_self ()); \
g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->subtitle_lock); \
GST_LOG_OBJECT (parsebin, \
"subtitle lock from thread %p", \
g_thread_self ()); \
} G_STMT_END
#define SUBTITLE_UNLOCK(parsebin) G_STMT_START { \
GST_LOG_OBJECT (parsebin, \
"subtitle unlocking from thread %p", \
g_thread_self ()); \
g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->subtitle_lock); \
} G_STMT_END
struct _GstPendingPad
{
GstPad *pad;
GstParseChain *chain;
gulong event_probe_id;
gulong notify_caps_id;
};
struct _GstParseElement
{
GstElement *element;
GstElement *capsfilter; /* Optional capsfilter for Parser/Convert */
gulong pad_added_id;
gulong pad_removed_id;
gulong no_more_pads_id;
};
/* GstParseGroup
*
* Streams belonging to the same group/chain of a media file
*
* When changing something here lock the parent chain!
*/
struct _GstParseGroup
{
GstParseBin *parsebin;
GstParseChain *parent;
gboolean no_more_pads; /* TRUE if the demuxer signaled no-more-pads */
gboolean drained; /* TRUE if the all children are drained */
GList *children; /* List of GstParseChains in this group */
};
struct _GstParseChain
{
GstParseGroup *parent;
GstParseBin *parsebin;
GMutex lock; /* Protects this chain and its groups */
GstPad *pad; /* srcpad that caused creation of this chain */
GstCaps *start_caps; /* The initial caps of this chain */
gboolean drained; /* TRUE if the all children are drained */
gboolean demuxer; /* TRUE if elements->data is a demuxer */
gboolean parsed; /* TRUE if any elements are a parser */
GList *elements; /* All elements in this group, first
is the latest and most downstream element */
/* Note: there are only groups if the last element of this chain
* is a demuxer, otherwise the chain will end with an endpad.
* The other way around this means, that endpad only exists if this
* chain doesn't end with a demuxer! */
GstParseGroup *active_group; /* Currently active group */
GList *next_groups; /* head is newest group, tail is next group.
a new group will be created only if the head
group had no-more-pads. If it's only exposed
all new pads will be ignored! */
GList *pending_pads; /* Pads that have no fixed caps yet */
GstParsePad *current_pad; /* Current ending pad of the chain that can't
* be exposed yet but would be the same as endpad
* once it can be exposed */
GstParsePad *endpad; /* Pad of this chain that could be exposed */
gboolean deadend; /* This chain is incomplete and can't be completed,
e.g. no suitable parser could be found
e.g. stream got EOS without buffers
*/
gchar *deadend_details;
GstCaps *endcaps; /* Caps that were used when linking to the endpad
or that resulted in the deadend
*/
/* FIXME: This should be done directly via a thread! */
GList *old_groups; /* Groups that should be freed later */
};
static void gst_parse_chain_free (GstParseChain * chain);
static GstParseChain *gst_parse_chain_new (GstParseBin * parsebin,
GstParseGroup * group, GstPad * pad, GstCaps * start_caps);
static void gst_parse_group_hide (GstParseGroup * group);
static void gst_parse_group_free (GstParseGroup * group);
static GstParseGroup *gst_parse_group_new (GstParseBin * parsebin,
GstParseChain * chain);
static gboolean gst_parse_chain_is_complete (GstParseChain * chain);
static gboolean gst_parse_chain_expose (GstParseChain * chain,
GList ** endpads, gboolean * missing_plugin,
GString * missing_plugin_details, gboolean * last_group,
gboolean * uncollected_streams);
static void build_fallback_collection (GstParseChain * chain,
GstStreamCollection * collection);
static gboolean gst_parse_chain_is_drained (GstParseChain * chain);
static gboolean gst_parse_group_is_complete (GstParseGroup * group);
static gboolean gst_parse_group_is_drained (GstParseGroup * group);
static gboolean gst_parse_bin_expose (GstParseBin * parsebin);
#define CHAIN_MUTEX_LOCK(chain) G_STMT_START { \
GST_LOG_OBJECT (chain->parsebin, \
"locking chain %p from thread %p", \
chain, g_thread_self ()); \
g_mutex_lock (&chain->lock); \
GST_LOG_OBJECT (chain->parsebin, \
"locked chain %p from thread %p", \
chain, g_thread_self ()); \
} G_STMT_END
#define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START { \
GST_LOG_OBJECT (chain->parsebin, \
"unlocking chain %p from thread %p", \
chain, g_thread_self ()); \
g_mutex_unlock (&chain->lock); \
} G_STMT_END
/* GstParsePad
*
* GstPad private used for source pads of chains
*/
struct _GstParsePad
{
GstGhostPad parent;
GstParseBin *parsebin;
GstParseChain *chain;
gboolean blocked; /* the *target* pad is blocked */
gboolean exposed; /* the pad is exposed */
gboolean drained; /* an EOS has been seen on the pad */
gulong block_id;
gboolean in_a_fallback_collection;
GstStreamCollection *active_collection;
GstStream *active_stream;
};
GType gst_parse_pad_get_type (void);
G_DEFINE_TYPE (GstParsePad, gst_parse_pad, GST_TYPE_GHOST_PAD);
#define GST_TYPE_PARSE_PAD (gst_parse_pad_get_type ())
#define GST_PARSE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PARSE_PAD,GstParsePad))
static GstParsePad *gst_parse_pad_new (GstParseBin * parsebin,
GstParseChain * chain);
static void gst_parse_pad_activate (GstParsePad * parsepad,
GstParseChain * chain);
static void gst_parse_pad_unblock (GstParsePad * parsepad);
static void gst_parse_pad_set_blocked (GstParsePad * parsepad,
gboolean blocked);
static gboolean gst_parse_pad_query (GstPad * pad, GstObject * parent,
GstQuery * query);
static GstPadProbeReturn
gst_parse_pad_event (GstPad * pad, GstPadProbeInfo * info, gpointer user_data);
static void gst_pending_pad_free (GstPendingPad * ppad);
static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
gpointer data);
/********************************
* Standard GObject boilerplate *
********************************/
static void gst_parse_bin_dispose (GObject * object);
static void gst_parse_bin_finalize (GObject * object);
static GType gst_parse_bin_get_type (void);
G_DEFINE_TYPE (GstParseBin, gst_parse_bin, GST_TYPE_BIN);
#define _do_init \
GST_DEBUG_CATEGORY_INIT (gst_parse_bin_debug, "parsebin", 0, "parser bin");\
playback_element_init (plugin);
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (parsebin, "parsebin", GST_RANK_NONE,
GST_TYPE_PARSE_BIN, _do_init);
static gboolean
_gst_boolean_accumulator (GSignalInvocationHint * ihint,
GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
gboolean myboolean;
myboolean = g_value_get_boolean (handler_return);
g_value_set_boolean (return_accu, myboolean);
/* stop emission if FALSE */
return myboolean;
}
static gboolean
_gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
gboolean myboolean;
gboolean retboolean;
myboolean = g_value_get_boolean (handler_return);
retboolean = g_value_get_boolean (return_accu);
g_value_set_boolean (return_accu, myboolean || retboolean);
return TRUE;
}
/* we collect the first result */
static gboolean
_gst_array_accumulator (GSignalInvocationHint * ihint,
GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
gpointer array;
array = g_value_get_boxed (handler_return);
g_value_set_boxed (return_accu, array);
return FALSE;
}
static gboolean
_gst_select_accumulator (GSignalInvocationHint * ihint,
GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
GstAutoplugSelectResult res;
res = g_value_get_enum (handler_return);
g_value_set_enum (return_accu, res);
/* Call the next handler in the chain (if any) when the current callback
* returns TRY. This makes it possible to register separate autoplug-select
* handlers that implement different TRY/EXPOSE/SKIP strategies.
*/
if (res == GST_AUTOPLUG_SELECT_TRY)
return TRUE;
return FALSE;
}
static gboolean
_gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
gpointer array;
array = g_value_get_boxed (handler_return);
g_value_set_boxed (return_accu, array);
if (array != NULL)
return FALSE;
return TRUE;
}
static void
gst_parse_bin_class_init (GstParseBinClass * klass)
{
GObjectClass *gobject_klass;
GstElementClass *gstelement_klass;
GstBinClass *gstbin_klass;
gobject_klass = (GObjectClass *) klass;
gstelement_klass = (GstElementClass *) klass;
gstbin_klass = (GstBinClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gobject_klass->dispose = gst_parse_bin_dispose;
gobject_klass->finalize = gst_parse_bin_finalize;
gobject_klass->set_property = gst_parse_bin_set_property;
gobject_klass->get_property = gst_parse_bin_get_property;
/**
* GstParseBin::unknown-type:
* @bin: The ParseBin.
* @pad: The new pad containing caps that cannot be resolved to a 'final'
* stream type.
* @caps: The #GstCaps of the pad that cannot be resolved.
*
* This signal is emitted when a pad for which there is no further possible
* parsing is added to the ParseBin.
*/
gst_parse_bin_signals[SIGNAL_UNKNOWN_TYPE] =
g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, unknown_type),
NULL, NULL, NULL, G_TYPE_NONE, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
/**
* GstParseBin::autoplug-continue:
* @bin: The ParseBin.
* @pad: The #GstPad.
* @caps: The #GstCaps found.
*
* This signal is emitted whenever ParseBin finds a new stream. It is
* emitted before looking for any elements that can handle that stream.
*
* > Invocation of signal handlers stops after the first signal handler
* > returns %FALSE. Signal handlers are invoked in the order they were
* > connected in.
*
* Returns: %TRUE if you wish ParseBin to look for elements that can
* handle the given @caps. If %FALSE, those caps will be considered as
* final and the pad will be exposed as such (see 'pad-added' signal of
* #GstElement).
*/
gst_parse_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_continue),
_gst_boolean_accumulator, NULL, NULL, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
GST_TYPE_CAPS);
/**
* GstParseBin::autoplug-factories:
* @bin: The ParseBin.
* @pad: The #GstPad.
* @caps: The #GstCaps found.
*
* This function is emitted when an array of possible factories for @caps on
* @pad is needed. ParseBin will by default return an array with all
* compatible factories, sorted by rank.
*
* If this function returns NULL, @pad will be exposed as a final caps.
*
* If this function returns an empty array, the pad will be considered as
* having an unhandled type media type.
*
* > Only the signal handler that is connected first will ever by invoked.
* > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
* > signal, they will never be invoked!
*
* Returns: a #GValueArray* with a list of factories to try. The factories are
* by default tried in the returned order or based on the index returned by
* "autoplug-select".
*/
gst_parse_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass,
autoplug_factories), _gst_array_accumulator, NULL,
NULL, G_TYPE_VALUE_ARRAY, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
/**
* GstParseBin::autoplug-sort:
* @bin: The ParseBin.
* @pad: The #GstPad.
* @caps: The #GstCaps.
* @factories: A #GValueArray of possible #GstElementFactory to use.
*
* Once ParseBin has found the possible #GstElementFactory objects to try
* for @caps on @pad, this signal is emitted. The purpose of the signal is for
* the application to perform additional sorting or filtering on the element
* factory array.
*
* The callee should copy and modify @factories or return %NULL if the
* order should not change.
*
* > Invocation of signal handlers stops after one signal handler has
* > returned something else than %NULL. Signal handlers are invoked in
* > the order they were connected in.
* > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
* > signal, they will never be invoked!
*
* Returns: A new sorted array of #GstElementFactory objects.
*/
gst_parse_bin_signals[SIGNAL_AUTOPLUG_SORT] =
g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_sort),
_gst_array_hasvalue_accumulator, NULL,
NULL, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
/**
* GstParseBin::autoplug-select:
* @bin: The ParseBin.
* @pad: The #GstPad.
* @caps: The #GstCaps.
* @factory: A #GstElementFactory to use.
*
* This signal is emitted once ParseBin has found all the possible
* #GstElementFactory that can be used to handle the given @caps. For each of
* those factories, this signal is emitted.
*
* The signal handler should return a #GstAutoplugSelectResult enum
* value indicating what ParseBin should do next.
*
* A value of #GstAutoplugSelectResult::try will try to autoplug an element from
* @factory.
*
* A value of #GstAutoplugSelectResult::expose will expose @pad without plugging
* any element to it.
*
* A value of #GstAutoplugSelectResult::skip will skip @factory and move to the
* next factory.
*
* > The signal handler will not be invoked if any of the previously
* > registered signal handlers (if any) return a value other than
* > GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
* > GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
* > registered next (again, if any) can override that decision.
*
* Returns: a #GstAutoplugSelectResult that indicates the required
* operation. the default handler will always return
* #GstAutoplugSelectResult::try.
*/
gst_parse_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_select),
_gst_select_accumulator, NULL, NULL, GST_TYPE_AUTOPLUG_SELECT_RESULT, 3,
GST_TYPE_PAD, GST_TYPE_CAPS, GST_TYPE_ELEMENT_FACTORY);
/**
* GstParseBin::autoplug-query:
* @bin: The ParseBin.
* @child: The child element doing the query
* @pad: The #GstPad.
* @element: The #GstElement.
* @query: The #GstQuery.
*
* This signal is emitted whenever an autoplugged element that is
* not linked downstream yet and not exposed does a query. It can
* be used to tell the element about the downstream supported caps
* for example.
*
* Returns: %TRUE if the query was handled, %FALSE otherwise.
*/
gst_parse_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_query),
_gst_boolean_or_accumulator, NULL, NULL, G_TYPE_BOOLEAN, 3, GST_TYPE_PAD,
GST_TYPE_ELEMENT, GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
/**
* GstParseBin::drained:
* @bin: The ParseBin
*
* This signal is emitted once ParseBin has finished parsing all the data.
*/
gst_parse_bin_signals[SIGNAL_DRAINED] =
g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, drained),
NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
g_param_spec_string ("subtitle-encoding", "subtitle encoding",
"Encoding to assume if input subtitles are not in UTF-8 encoding. "
"If not set, the GST_SUBTITLE_ENCODING environment variable will "
"be checked for an encoding to use. If that is not set either, "
"ISO-8859-15 will be assumed.", NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
g_param_spec_boxed ("sink-caps", "Sink Caps",
"The caps of the input data. (NULL = use typefind element)",
GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* GstParseBin::expose-all-streams:
*
* Expose streams of unknown type.
*
* If set to %FALSE, then only the streams that can be parsed to the final
* caps (see 'caps' property) will have a pad exposed. Streams that do not
* match those caps but could have been parsed will not have parser plugged
* in internally and will not have a pad exposed.
*/
g_object_class_install_property (gobject_klass, PROP_EXPOSE_ALL_STREAMS,
g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
"Expose all streams, including those of unknown type or that don't match the 'caps' property",
DEFAULT_EXPOSE_ALL_STREAMS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* GstParseBin2::connection-speed:
*
* Network connection speed in kbps (0 = unknownw)
*/
g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
g_param_spec_uint64 ("connection-speed", "Connection Speed",
"Network connection speed in kbps (0 = unknown)",
0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
klass->autoplug_continue =
GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_continue);
klass->autoplug_factories =
GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_factories);
klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_sort);
klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_select);
klass->autoplug_query = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_query);
gst_element_class_add_pad_template (gstelement_klass,
gst_static_pad_template_get (&parse_bin_sink_template));
gst_element_class_add_pad_template (gstelement_klass,
gst_static_pad_template_get (&parse_bin_src_template));
gst_element_class_set_static_metadata (gstelement_klass,
"Parse Bin", "Generic/Bin/Parser",
"Parse and de-multiplex to elementary stream",
"Jan Schmidt <jan@centricular.com>, "
"Edward Hervey <edward@centricular.com>");
gstelement_klass->change_state =
GST_DEBUG_FUNCPTR (gst_parse_bin_change_state);
gstbin_klass->handle_message =
GST_DEBUG_FUNCPTR (gst_parse_bin_handle_message);
g_type_class_ref (GST_TYPE_PARSE_PAD);
}
static void
gst_parse_bin_update_factories_list (GstParseBin * parsebin)
{
guint cookie;
cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
if (!parsebin->factories || parsebin->factories_cookie != cookie) {
if (parsebin->factories)
gst_plugin_feature_list_free (parsebin->factories);
parsebin->factories =
gst_element_factory_list_get_elements
(GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
parsebin->factories =
g_list_sort (parsebin->factories,
gst_playback_utils_compare_factories_func);
parsebin->factories_cookie = cookie;
}
}
static void
gst_parse_bin_init (GstParseBin * parse_bin)
{
/* first filter out the interesting element factories */
g_mutex_init (&parse_bin->factories_lock);
/* we create the typefind element only once */
parse_bin->typefind = gst_element_factory_make ("typefind", "typefind");
if (!parse_bin->typefind) {
g_warning ("can't find typefind element, ParseBin will not work");
} else {
GstPad *pad;
GstPad *gpad;
GstPadTemplate *pad_tmpl;
/* add the typefind element */
if (!gst_bin_add (GST_BIN (parse_bin), parse_bin->typefind)) {
g_warning ("Could not add typefind element, ParseBin will not work");
gst_object_unref (parse_bin->typefind);
parse_bin->typefind = NULL;
}
/* get the sinkpad */
pad = gst_element_get_static_pad (parse_bin->typefind, "sink");
/* get the pad template */
pad_tmpl = gst_static_pad_template_get (&parse_bin_sink_template);
/* ghost the sink pad to ourself */
gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
gst_pad_set_active (gpad, TRUE);
gst_element_add_pad (GST_ELEMENT (parse_bin), gpad);
gst_object_unref (pad_tmpl);
gst_object_unref (pad);
}
g_mutex_init (&parse_bin->expose_lock);
parse_bin->parse_chain = NULL;
g_mutex_init (&parse_bin->dyn_lock);
parse_bin->shutdown = FALSE;
parse_bin->blocked_pads = NULL;
g_mutex_init (&parse_bin->subtitle_lock);
parse_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
parse_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
parse_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
g_mutex_init (&parse_bin->cleanup_lock);
parse_bin->cleanup_thread = NULL;
GST_OBJECT_FLAG_SET (parse_bin, GST_BIN_FLAG_STREAMS_AWARE);
}
static void
gst_parse_bin_dispose (GObject * object)
{
GstParseBin *parse_bin;
parse_bin = GST_PARSE_BIN (object);
if (parse_bin->factories)
gst_plugin_feature_list_free (parse_bin->factories);
parse_bin->factories = NULL;
if (parse_bin->parse_chain)
gst_parse_chain_free (parse_bin->parse_chain);
parse_bin->parse_chain = NULL;
g_free (parse_bin->encoding);
parse_bin->encoding = NULL;
g_list_free (parse_bin->subtitles);
parse_bin->subtitles = NULL;
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gst_parse_bin_finalize (GObject * object)
{
GstParseBin *parse_bin;
parse_bin = GST_PARSE_BIN (object);
g_mutex_clear (&parse_bin->expose_lock);
g_mutex_clear (&parse_bin->dyn_lock);
g_mutex_clear (&parse_bin->subtitle_lock);
g_mutex_clear (&parse_bin->factories_lock);
g_mutex_clear (&parse_bin->cleanup_lock);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_parse_bin_set_sink_caps (GstParseBin * parsebin, GstCaps * caps)
{
GST_DEBUG_OBJECT (parsebin, "Setting new caps: %" GST_PTR_FORMAT, caps);
g_object_set (parsebin->typefind, "force-caps", caps, NULL);
}
static GstCaps *
gst_parse_bin_get_sink_caps (GstParseBin * parsebin)
{
GstCaps *caps;
GST_DEBUG_OBJECT (parsebin, "Getting currently set caps");
g_object_get (parsebin->typefind, "force-caps", &caps, NULL);
return caps;
}
static void
gst_parse_bin_set_subs_encoding (GstParseBin * parsebin, const gchar * encoding)
{
GList *walk;
GST_DEBUG_OBJECT (parsebin, "Setting new encoding: %s",
GST_STR_NULL (encoding));
SUBTITLE_LOCK (parsebin);
g_free (parsebin->encoding);
parsebin->encoding = g_strdup (encoding);
/* set the subtitle encoding on all added elements */
for (walk = parsebin->subtitles; walk; walk = g_list_next (walk)) {
g_object_set (G_OBJECT (walk->data), "subtitle-encoding",
parsebin->encoding, NULL);
}
SUBTITLE_UNLOCK (parsebin);
}
static gchar *
gst_parse_bin_get_subs_encoding (GstParseBin * parsebin)
{
gchar *encoding;
GST_DEBUG_OBJECT (parsebin, "Getting currently set encoding");