1 /* GStreamer
2 * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
3 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * Copyright (C) <2011> Hewlett-Packard Development Company, L.P.
5 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
6 * Copyright (C) <2013> Collabora Ltd.
7 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25 /**
26 * SECTION:element-decodebin
27 * @title: decodebin
28 *
29 * #GstBin that auto-magically constructs a decoding pipeline using available
30 * decoders and demuxers via auto-plugging.
31 *
32 * decodebin is considered stable now and replaces the old #decodebin element.
33 * #uridecodebin uses decodebin internally and is often more convenient to
34 * use, as it creates a suitable source element as well.
35 */
36
37 /* Implementation notes:
38 *
39 * The following section describes how decodebin works internally.
40 *
41 * The first part of decodebin is its typefind element, which tries
42 * to determine the media type of the input stream. If the type is found
43 * autoplugging starts.
44 *
45 * decodebin internally organizes the elements it autoplugged into GstDecodeChains
46 * and GstDecodeGroups. A decode chain is a single chain of decoding, this
47 * means that if decodebin every autoplugs an element with two+ srcpads
48 * (e.g. a demuxer) this will end the chain and everything following this
49 * demuxer will be put into decode groups below the chain. Otherwise,
50 * if an element has a single srcpad that outputs raw data the decode chain
51 * is ended too and a GstDecodePad is stored and blocked.
52 *
53 * A decode group combines a number of chains that are created by a
54 * demuxer element. All those chains are connected through a multiqueue to
55 * the demuxer. A new group for the same demuxer is only created if the
56 * demuxer has signaled no-more-pads, in which case all following pads
57 * create a new chain in the new group.
58 *
59 * This continues until the top-level decode chain is complete. A decode
60 * chain is complete if it either ends with a blocked endpad, if autoplugging
61 * stopped because no suitable plugins could be found or if the active group
62 * is complete. A decode group on the other hand is complete if all child
63 * chains are complete.
64 *
65 * If this happens at some point, all endpads of all active groups are exposed.
66 * For this decodebin adds the endpads, signals no-more-pads and then unblocks
67 * them. Now playback starts.
68 *
69 * If one of the chains that end on a endpad receives EOS decodebin checks
70 * if all chains and groups are drained. In that case everything goes into EOS.
71 * If there is a chain where the active group is drained but there exist next
72 * groups, the active group is hidden (endpads are removed) and the next group
73 * is exposed. This means that in some cases more pads may be created even
74 * after the initial no-more-pads signal. This happens for example with
75 * so-called "chained oggs", most commonly found among ogg/vorbis internet
76 * radio streams.
77 *
78 * Note 1: If we're talking about blocked endpads this really means that the
79 * *target* pads of the endpads are blocked. Pads that are exposed to the outside
80 * should never ever be blocked!
81 *
82 * Note 2: If a group is complete and the parent's chain demuxer adds new pads
83 * but never signaled no-more-pads this additional pads will be ignored!
84 *
85 */
86
87 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
88 * with newer GLib versions (>= 2.31.0) */
89 #define GLIB_DISABLE_DEPRECATION_WARNINGS
90
91 #ifdef HAVE_CONFIG_H
92 #include "config.h"
93 #endif
94
95 #include <gst/gst-i18n-plugin.h>
96
97 #include <string.h>
98 #include <gst/gst.h>
99 #include <gst/pbutils/pbutils.h>
100
101 #include "gstplay-enum.h"
102 #include "gstplayback.h"
103 #include "gstrawcaps.h"
104 #include "gstplaybackutils.h"
105
106 /* generic templates */
107 static GstStaticPadTemplate decoder_bin_sink_template =
108 GST_STATIC_PAD_TEMPLATE ("sink",
109 GST_PAD_SINK,
110 GST_PAD_ALWAYS,
111 GST_STATIC_CAPS_ANY);
112
113 static GstStaticPadTemplate decoder_bin_src_template =
114 GST_STATIC_PAD_TEMPLATE ("src_%u",
115 GST_PAD_SRC,
116 GST_PAD_SOMETIMES,
117 GST_STATIC_CAPS_ANY);
118
119 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
120 #define GST_CAT_DEFAULT gst_decode_bin_debug
121
122 typedef struct _GstPendingPad GstPendingPad;
123 typedef struct _GstDecodeElement GstDecodeElement;
124 typedef struct _GstDemuxerPad GstDemuxerPad;
125 typedef struct _GstDecodeChain GstDecodeChain;
126 typedef struct _GstDecodeGroup GstDecodeGroup;
127 typedef struct _GstDecodePad GstDecodePad;
128 typedef GstGhostPadClass GstDecodePadClass;
129 typedef struct _GstDecodeBin GstDecodeBin;
130 typedef struct _GstDecodeBinClass GstDecodeBinClass;
131
132 #define GST_TYPE_DECODE_BIN (gst_decode_bin_get_type())
133 #define GST_DECODE_BIN_CAST(obj) ((GstDecodeBin*)(obj))
134 #define GST_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
135 #define GST_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
136 #define GST_IS_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
137 #define GST_IS_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
138
139 /**
140 * GstDecodeBin:
141 *
142 * The opaque #GstDecodeBin data structure
143 */
144 struct _GstDecodeBin
145 {
146 GstBin bin; /* we extend GstBin */
147
148 /* properties */
149 GstCaps *caps; /* caps on which to stop decoding */
150 gchar *encoding; /* encoding of subtitles */
151 gboolean use_buffering; /* configure buffering on multiqueues */
152 gint low_percent;
153 gint high_percent;
154 guint max_size_bytes;
155 guint max_size_buffers;
156 guint64 max_size_time;
157 gboolean post_stream_topology;
158 guint64 connection_speed;
159
160 GstElement *typefind; /* this holds the typefind object */
161
162 GMutex expose_lock; /* Protects exposal and removal of groups */
163 GstDecodeChain *decode_chain; /* Top level decode chain */
164 guint nbpads; /* unique identifier for source pads */
165
166 GMutex factories_lock;
167 guint32 factories_cookie; /* Cookie from last time when factories was updated */
168 GList *factories; /* factories we can use for selecting elements */
169
170 GMutex subtitle_lock; /* Protects changes to subtitles and encoding */
171 GList *subtitles; /* List of elements with subtitle-encoding,
172 * protected by above mutex! */
173
174 gboolean have_type; /* if we received the have_type signal */
175 guint have_type_id; /* signal id for have-type from typefind */
176
177 gboolean async_pending; /* async-start has been emitted */
178
179 GMutex dyn_lock; /* lock protecting pad blocking */
180 gboolean shutdown; /* if we are shutting down */
181 GList *blocked_pads; /* pads that have set to block */
182
183 gboolean expose_allstreams; /* Whether to expose unknow type streams or not */
184
185 GList *filtered; /* elements for which error messages are filtered */
186 GList *filtered_errors; /* filtered error messages */
187
188 GList *buffering_status; /* element currently buffering messages */
189 GMutex buffering_lock;
190 GMutex buffering_post_lock;
191
192 GMutex cleanup_lock; /* Mutex used to protect the cleanup thread */
193 GThread *cleanup_thread; /* thread used to free chains asynchronously.
194 * We store it to make sure we end up joining it
195 * before stopping the element.
196 * Protected by the object lock */
197 GList *cleanup_groups; /* List of groups to free */
198 };
199
200 struct _GstDecodeBinClass
201 {
202 GstBinClass parent_class;
203
204 /* signal fired when we found a pad that we cannot decode */
205 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
206
207 /* signal fired to know if we continue trying to decode the given caps */
208 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
209 GstCaps * caps);
210 /* signal fired to get a list of factories to try to autoplug */
211 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
212 GstCaps * caps);
213 /* signal fired to sort the factories */
214 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
215 GstCaps * caps, GValueArray * factories);
216 /* signal fired to select from the proposed list of factories */
217 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
218 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
219 /* signal fired when a autoplugged element that is not linked downstream
220 * or exposed wants to query something */
221 gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
222 GstQuery * query);
223
224 /* fired when the last group is drained */
225 void (*drained) (GstElement * element);
226 };
227
228 /* signals */
229 enum
230 {
231 SIGNAL_UNKNOWN_TYPE,
232 SIGNAL_AUTOPLUG_CONTINUE,
233 SIGNAL_AUTOPLUG_FACTORIES,
234 SIGNAL_AUTOPLUG_SELECT,
235 SIGNAL_AUTOPLUG_SORT,
236 SIGNAL_AUTOPLUG_QUERY,
237 SIGNAL_DRAINED,
238 LAST_SIGNAL
239 };
240
241 /* automatic sizes, while prerolling we buffer up to 2MB, we ignore time
242 * and buffers in this case. */
243 #define AUTO_PREROLL_SIZE_BYTES 2 * 1024 * 1024
244 #define AUTO_PREROLL_SIZE_BUFFERS 0
245 #define AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME 10 * GST_SECOND
246 #define AUTO_PREROLL_SEEKABLE_SIZE_TIME 0
247
248 /* when playing, keep a max of 2MB of data but try to keep the number of buffers
249 * as low as possible (try to aim for 5 buffers) */
250 #define AUTO_PLAY_SIZE_BYTES 2 * 1024 * 1024
251 #define AUTO_PLAY_SIZE_BUFFERS 5
252 #define AUTO_PLAY_SIZE_TIME 0
253
254 #define DEFAULT_SUBTITLE_ENCODING NULL
255 #define DEFAULT_USE_BUFFERING FALSE
256 #define DEFAULT_LOW_PERCENT 10
257 #define DEFAULT_HIGH_PERCENT 99
258 /* by default we use the automatic values above */
259 #define DEFAULT_MAX_SIZE_BYTES 0
260 #define DEFAULT_MAX_SIZE_BUFFERS 0
261 #define DEFAULT_MAX_SIZE_TIME 0
262 #define DEFAULT_POST_STREAM_TOPOLOGY FALSE
263 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
264 #define DEFAULT_CONNECTION_SPEED 0
265
266 /* Properties */
267 enum
268 {
269 PROP_0,
270 PROP_CAPS,
271 PROP_SUBTITLE_ENCODING,
272 PROP_SINK_CAPS,
273 PROP_USE_BUFFERING,
274 PROP_LOW_PERCENT,
275 PROP_HIGH_PERCENT,
276 PROP_MAX_SIZE_BYTES,
277 PROP_MAX_SIZE_BUFFERS,
278 PROP_MAX_SIZE_TIME,
279 PROP_POST_STREAM_TOPOLOGY,
280 PROP_EXPOSE_ALL_STREAMS,
281 PROP_CONNECTION_SPEED
282 };
283
284 static GstBinClass *parent_class;
285 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
286
287 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
288
289 static void do_async_start (GstDecodeBin * dbin);
290 static void do_async_done (GstDecodeBin * dbin);
291
292 static void type_found (GstElement * typefind, guint probability,
293 GstCaps * caps, GstDecodeBin * decode_bin);
294
295 static void decodebin_set_queue_size (GstDecodeBin * dbin,
296 GstElement * multiqueue, gboolean preroll, gboolean seekable);
297 static void decodebin_set_queue_size_full (GstDecodeBin * dbin,
298 GstElement * multiqueue, gboolean use_buffering, gboolean preroll,
299 gboolean seekable);
300
301 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
302 GstPad * pad, GstCaps * caps);
303 static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
304 element, GstPad * pad, GstCaps * caps);
305 static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
306 GstPad * pad, GstCaps * caps, GValueArray * factories);
307 static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
308 element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
309 static gboolean gst_decode_bin_autoplug_query (GstElement * element,
310 GstPad * pad, GstQuery * query);
311
312 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
313 const GValue * value, GParamSpec * pspec);
314 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
315 GValue * value, GParamSpec * pspec);
316 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
317 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
318 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
319 GstDecodeChain * chain);
320
321 static void flush_chain (GstDecodeChain * chain, gboolean flushing);
322 static void flush_group (GstDecodeGroup * group, gboolean flushing);
323 static GstPad *find_sink_pad (GstElement * element);
324 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
325 GstStateChange transition);
326 static void gst_decode_bin_handle_message (GstBin * bin, GstMessage * message);
327 static gboolean gst_decode_bin_remove_element (GstBin * bin,
328 GstElement * element);
329
330 static gboolean check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad);
331
332 static GstCaps *get_pad_caps (GstPad * pad);
333 static void unblock_pads (GstDecodeBin * dbin);
334
335 #define EXPOSE_LOCK(dbin) G_STMT_START { \
336 GST_LOG_OBJECT (dbin, \
337 "expose locking from thread %p", \
338 g_thread_self ()); \
339 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->expose_lock); \
340 GST_LOG_OBJECT (dbin, \
341 "expose locked from thread %p", \
342 g_thread_self ()); \
343 } G_STMT_END
344
345 #define EXPOSE_UNLOCK(dbin) G_STMT_START { \
346 GST_LOG_OBJECT (dbin, \
347 "expose unlocking from thread %p", \
348 g_thread_self ()); \
349 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->expose_lock); \
350 } G_STMT_END
351
352 #define DYN_LOCK(dbin) G_STMT_START { \
353 GST_LOG_OBJECT (dbin, \
354 "dynlocking from thread %p", \
355 g_thread_self ()); \
356 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
357 GST_LOG_OBJECT (dbin, \
358 "dynlocked from thread %p", \
359 g_thread_self ()); \
360 } G_STMT_END
361
362 #define DYN_UNLOCK(dbin) G_STMT_START { \
363 GST_LOG_OBJECT (dbin, \
364 "dynunlocking from thread %p", \
365 g_thread_self ()); \
366 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
367 } G_STMT_END
368
369 #define SUBTITLE_LOCK(dbin) G_STMT_START { \
370 GST_LOG_OBJECT (dbin, \
371 "subtitle locking from thread %p", \
372 g_thread_self ()); \
373 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
374 GST_LOG_OBJECT (dbin, \
375 "subtitle lock from thread %p", \
376 g_thread_self ()); \
377 } G_STMT_END
378
379 #define SUBTITLE_UNLOCK(dbin) G_STMT_START { \
380 GST_LOG_OBJECT (dbin, \
381 "subtitle unlocking from thread %p", \
382 g_thread_self ()); \
383 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
384 } G_STMT_END
385
386 #define BUFFERING_LOCK(dbin) G_STMT_START { \
387 GST_LOG_OBJECT (dbin, \
388 "buffering locking from thread %p", \
389 g_thread_self ()); \
390 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->buffering_lock); \
391 GST_LOG_OBJECT (dbin, \
392 "buffering lock from thread %p", \
393 g_thread_self ()); \
394 } G_STMT_END
395
396 #define BUFFERING_UNLOCK(dbin) G_STMT_START { \
397 GST_LOG_OBJECT (dbin, \
398 "buffering unlocking from thread %p", \
399 g_thread_self ()); \
400 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->buffering_lock); \
401 } G_STMT_END
402
403 struct _GstPendingPad
404 {
405 GstPad *pad;
406 GstDecodeChain *chain;
407 gulong event_probe_id;
408 gulong notify_caps_id;
409 };
410
411 struct _GstDecodeElement
412 {
413 GstElement *element;
414 GstElement *capsfilter; /* Optional capsfilter for Parser/Convert */
415 gulong pad_added_id;
416 gulong pad_removed_id;
417 gulong no_more_pads_id;
418 };
419
420 struct _GstDemuxerPad
421 {
422 GWeakRef weakPad;
423 gulong event_probe_id;
424 gulong query_probe_id;
425 };
426
427
428 /* GstDecodeGroup
429 *
430 * Streams belonging to the same group/chain of a media file
431 *
432 * When changing something here lock the parent chain!
433 */
434 struct _GstDecodeGroup
435 {
436 GstDecodeBin *dbin;
437 GstDecodeChain *parent;
438
439 GstElement *multiqueue; /* Used for linking all child chains */
440 gulong overrunsig; /* the overrun signal for multiqueue */
441
442 gboolean overrun; /* TRUE if the multiqueue signaled overrun. This
443 * means that we should really expose the group */
444
445 gboolean no_more_pads; /* TRUE if the demuxer signaled no-more-pads */
446 gboolean drained; /* TRUE if the all children are drained */
447
448 GList *children; /* List of GstDecodeChains in this group */
449 GList *demuxer_pad_probe_ids;
450
451 GList *reqpads; /* List of RequestPads for multiqueue, there is
452 * exactly one RequestPad per child chain */
453 };
454
455 struct _GstDecodeChain
456 {
457 GstDecodeGroup *parent;
458 GstDecodeBin *dbin;
459
460 gint refs; /* Number of references to this object */
461
462 GMutex lock; /* Protects this chain and its groups */
463
464 GstPad *pad; /* srcpad that caused creation of this chain */
465 gulong pad_probe_id; /* id for the demuxer_source_pad_probe probe */
466
467 gboolean drained; /* TRUE if the all children are drained */
468 gboolean demuxer; /* TRUE if elements->data is a demuxer */
469 gboolean adaptive_demuxer; /* TRUE if elements->data is an adaptive streaming demuxer */
470 gboolean seekable; /* TRUE if this chain ends on a demuxer and is seekable */
471 GList *elements; /* All elements in this group, first
472 is the latest and most downstream element */
473
474 /* Note: there are only groups if the last element of this chain
475 * is a demuxer, otherwise the chain will end with an endpad.
476 * The other way around this means, that endpad only exists if this
477 * chain doesn't end with a demuxer! */
478
479 GstDecodeGroup *active_group; /* Currently active group */
480 GList *next_groups; /* head is newest group, tail is next group.
481 a new group will be created only if the head
482 group had no-more-pads. If it's only exposed
483 all new pads will be ignored! */
484 GList *pending_pads; /* Pads that have no fixed caps yet */
485
486 GstDecodePad *current_pad; /* Current ending pad of the chain that can't
487 * be exposed yet but would be the same as endpad
488 * once it can be exposed */
489 GstDecodePad *endpad; /* Pad of this chain that could be exposed */
490 gboolean deadend; /* This chain is incomplete and can't be completed,
491 e.g. no suitable decoder could be found
492 e.g. stream got EOS without buffers
493 */
494 gchar *deadend_details;
495 GstCaps *endcaps; /* Caps that were used when linking to the endpad
496 or that resulted in the deadend
497 */
498
499 /* FIXME: This should be done directly via a thread! */
500 GList *old_groups; /* Groups that should be freed later */
501 };
502
503 static GstDecodeChain *gst_decode_chain_ref (GstDecodeChain * chain);
504 static void gst_decode_chain_unref (GstDecodeChain * chain);
505 static void gst_decode_chain_free (GstDecodeChain * chain);
506 static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
507 GstDecodeGroup * group, GstPad * pad);
508 static void gst_decode_group_hide (GstDecodeGroup * group);
509 static void gst_decode_group_free (GstDecodeGroup * group);
510 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
511 GstDecodeChain * chain);
512 static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
513 static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
514 GList ** endpads, gboolean * missing_plugin,
515 GString * missing_plugin_details, gboolean * last_group);
516 static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
517 static gboolean gst_decode_chain_reset_buffering (GstDecodeChain * chain);
518 static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
519 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
520 GstPad * pad);
521 static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
522 static gboolean gst_decode_group_reset_buffering (GstDecodeGroup * group);
523
524 static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
525 static void gst_decode_bin_reset_buffering (GstDecodeBin * dbin);
526
527 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START { \
528 GST_LOG_OBJECT (chain->dbin, \
529 "locking chain %p from thread %p", \
530 chain, g_thread_self ()); \
531 g_mutex_lock (&chain->lock); \
532 GST_LOG_OBJECT (chain->dbin, \
533 "locked chain %p from thread %p", \
534 chain, g_thread_self ()); \
535 } G_STMT_END
536
537 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START { \
538 GST_LOG_OBJECT (chain->dbin, \
539 "unlocking chain %p from thread %p", \
540 chain, g_thread_self ()); \
541 g_mutex_unlock (&chain->lock); \
542 } G_STMT_END
543
544 /* GstDecodePad
545 *
546 * GstPad private used for source pads of chains
547 */
548 struct _GstDecodePad
549 {
550 GstGhostPad parent;
551 GstDecodeBin *dbin;
552 GstDecodeChain *chain;
553
554 gboolean blocked; /* the *target* pad is blocked */
555 gboolean exposed; /* the pad is exposed */
556 gboolean drained; /* an EOS has been seen on the pad */
557
558 gulong block_id;
559 };
560
561 GType gst_decode_pad_get_type (void);
562 G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
563 #define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
564 #define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))
565
566 static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin,
567 GstDecodeChain * chain);
568 static void gst_decode_pad_activate (GstDecodePad * dpad,
569 GstDecodeChain * chain);
570 static void gst_decode_pad_unblock (GstDecodePad * dpad);
571 static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
572 static gboolean gst_decode_pad_query (GstPad * pad, GstObject * parent,
573 GstQuery * query);
574 static gboolean gst_decode_pad_is_exposable (GstDecodePad * endpad);
575
576 static void gst_pending_pad_free (GstPendingPad * ppad);
577 static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
578 gpointer data);
579
580 /********************************
581 * Standard GObject boilerplate *
582 ********************************/
583
584 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
585 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
586 static void gst_decode_bin_dispose (GObject * object);
587 static void gst_decode_bin_finalize (GObject * object);
588
589 static GType
gst_decode_bin_get_type(void)590 gst_decode_bin_get_type (void)
591 {
592 static GType gst_decode_bin_type = 0;
593
594 if (!gst_decode_bin_type) {
595 static const GTypeInfo gst_decode_bin_info = {
596 sizeof (GstDecodeBinClass),
597 NULL,
598 NULL,
599 (GClassInitFunc) gst_decode_bin_class_init,
600 NULL,
601 NULL,
602 sizeof (GstDecodeBin),
603 0,
604 (GInstanceInitFunc) gst_decode_bin_init,
605 NULL
606 };
607
608 gst_decode_bin_type =
609 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
610 &gst_decode_bin_info, 0);
611 }
612
613 return gst_decode_bin_type;
614 }
615
616 static gboolean
_gst_boolean_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)617 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
618 GValue * return_accu, const GValue * handler_return, gpointer dummy)
619 {
620 gboolean myboolean;
621
622 myboolean = g_value_get_boolean (handler_return);
623 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
624 g_value_set_boolean (return_accu, myboolean);
625
626 /* stop emission if FALSE */
627 return myboolean;
628 }
629
630 static gboolean
_gst_boolean_or_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)631 _gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
632 GValue * return_accu, const GValue * handler_return, gpointer dummy)
633 {
634 gboolean myboolean;
635 gboolean retboolean;
636
637 myboolean = g_value_get_boolean (handler_return);
638 retboolean = g_value_get_boolean (return_accu);
639
640 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
641 g_value_set_boolean (return_accu, myboolean || retboolean);
642
643 return TRUE;
644 }
645
646 /* we collect the first result */
647 static gboolean
_gst_array_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)648 _gst_array_accumulator (GSignalInvocationHint * ihint,
649 GValue * return_accu, const GValue * handler_return, gpointer dummy)
650 {
651 gpointer array;
652
653 array = g_value_get_boxed (handler_return);
654 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
655 g_value_set_boxed (return_accu, array);
656
657 return FALSE;
658 }
659
660 static gboolean
_gst_select_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)661 _gst_select_accumulator (GSignalInvocationHint * ihint,
662 GValue * return_accu, const GValue * handler_return, gpointer dummy)
663 {
664 GstAutoplugSelectResult res;
665
666 res = g_value_get_enum (handler_return);
667 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
668 g_value_set_enum (return_accu, res);
669
670 /* Call the next handler in the chain (if any) when the current callback
671 * returns TRY. This makes it possible to register separate autoplug-select
672 * handlers that implement different TRY/EXPOSE/SKIP strategies.
673 */
674 if (res == GST_AUTOPLUG_SELECT_TRY)
675 return TRUE;
676
677 return FALSE;
678 }
679
680 static gboolean
_gst_array_hasvalue_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)681 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
682 GValue * return_accu, const GValue * handler_return, gpointer dummy)
683 {
684 gpointer array;
685
686 array = g_value_get_boxed (handler_return);
687 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
688 g_value_set_boxed (return_accu, array);
689
690 if (array != NULL)
691 return FALSE;
692
693 return TRUE;
694 }
695
696 static void
gst_decode_bin_class_init(GstDecodeBinClass * klass)697 gst_decode_bin_class_init (GstDecodeBinClass * klass)
698 {
699 GObjectClass *gobject_klass;
700 GstElementClass *gstelement_klass;
701 GstBinClass *gstbin_klass;
702
703 gobject_klass = (GObjectClass *) klass;
704 gstelement_klass = (GstElementClass *) klass;
705 gstbin_klass = (GstBinClass *) klass;
706
707 parent_class = g_type_class_peek_parent (klass);
708
709 gobject_klass->dispose = gst_decode_bin_dispose;
710 gobject_klass->finalize = gst_decode_bin_finalize;
711 gobject_klass->set_property = gst_decode_bin_set_property;
712 gobject_klass->get_property = gst_decode_bin_get_property;
713
714 /**
715 * GstDecodeBin::unknown-type:
716 * @bin: The decodebin.
717 * @pad: The new pad containing caps that cannot be resolved to a 'final'
718 * stream type.
719 * @caps: The #GstCaps of the pad that cannot be resolved.
720 *
721 * This signal is emitted when a pad for which there is no further possible
722 * decoding is added to the decodebin.
723 */
724 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
725 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
726 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
727 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
728 GST_TYPE_PAD, GST_TYPE_CAPS);
729
730 /**
731 * GstDecodeBin::autoplug-continue:
732 * @bin: The decodebin.
733 * @pad: The #GstPad.
734 * @caps: The #GstCaps found.
735 *
736 * This signal is emitted whenever decodebin finds a new stream. It is
737 * emitted before looking for any elements that can handle that stream.
738 *
739 * > Invocation of signal handlers stops after the first signal handler
740 * > returns %FALSE. Signal handlers are invoked in the order they were
741 * > connected in.
742 *
743 * Returns: %TRUE if you wish decodebin to look for elements that can
744 * handle the given @caps. If %FALSE, those caps will be considered as
745 * final and the pad will be exposed as such (see 'pad-added' signal of
746 * #GstElement).
747 */
748 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
749 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
750 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
751 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
752 G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
753
754 /**
755 * GstDecodeBin::autoplug-factories:
756 * @bin: The decodebin.
757 * @pad: The #GstPad.
758 * @caps: The #GstCaps found.
759 *
760 * This signal is emitted when an array of possible factories for @caps on
761 * @pad is needed. Decodebin will by default return an array with all
762 * compatible factories, sorted by rank.
763 *
764 * If this function returns NULL, @pad will be exposed as a final caps.
765 *
766 * If this function returns an empty array, the pad will be considered as
767 * having an unhandled type media type.
768 *
769 * > Only the signal handler that is connected first will ever by invoked.
770 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
771 * > signal, they will never be invoked!
772 *
773 * Returns: a #GValueArray* with a list of factories to try. The factories are
774 * by default tried in the returned order or based on the index returned by
775 * "autoplug-select".
776 */
777 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
778 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
779 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
780 autoplug_factories), _gst_array_accumulator, NULL,
781 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
782 GST_TYPE_PAD, GST_TYPE_CAPS);
783
784 /**
785 * GstDecodeBin::autoplug-sort:
786 * @bin: The decodebin.
787 * @pad: The #GstPad.
788 * @caps: The #GstCaps.
789 * @factories: A #GValueArray of possible #GstElementFactory to use.
790 *
791 * Once decodebin has found the possible #GstElementFactory objects to try
792 * for @caps on @pad, this signal is emitted. The purpose of the signal is for
793 * the application to perform additional sorting or filtering on the element
794 * factory array.
795 *
796 * The callee should copy and modify @factories or return %NULL if the
797 * order should not change.
798 *
799 * > Invocation of signal handlers stops after one signal handler has
800 * > returned something else than %NULL. Signal handlers are invoked in
801 * > the order they were connected in.
802 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
803 * > signal, they will never be invoked!
804 *
805 * Returns: A new sorted array of #GstElementFactory objects.
806 */
807 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
808 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
809 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
810 _gst_array_hasvalue_accumulator, NULL,
811 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
812 GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
813
814 /**
815 * GstDecodeBin::autoplug-select:
816 * @bin: The decodebin.
817 * @pad: The #GstPad.
818 * @caps: The #GstCaps.
819 * @factory: A #GstElementFactory to use.
820 *
821 * This signal is emitted once decodebin has found all the possible
822 * #GstElementFactory that can be used to handle the given @caps. For each of
823 * those factories, this signal is emitted.
824 *
825 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
826 * value indicating what decodebin should do next.
827 *
828 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
829 * @factory.
830 *
831 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
832 * any element to it.
833 *
834 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
835 * next factory.
836 *
837 * > The signal handler will not be invoked if any of the previously
838 * > registered signal handlers (if any) return a value other than
839 * > GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
840 * > GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
841 * > registered next (again, if any) can override that decision.
842 *
843 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
844 * operation. the default handler will always return
845 * #GST_AUTOPLUG_SELECT_TRY.
846 */
847 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
848 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
849 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
850 _gst_select_accumulator, NULL,
851 g_cclosure_marshal_generic,
852 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
853 GST_TYPE_ELEMENT_FACTORY);
854
855 /**
856 * GstDecodeBin::autoplug-query:
857 * @bin: The decodebin.
858 * @pad: The #GstPad.
859 * @child: The child element doing the query
860 * @query: The #GstQuery.
861 *
862 * This signal is emitted whenever an autoplugged element that is
863 * not linked downstream yet and not exposed does a query. It can
864 * be used to tell the element about the downstream supported caps
865 * for example.
866 *
867 * Returns: %TRUE if the query was handled, %FALSE otherwise.
868 */
869 gst_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
870 g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
871 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_query),
872 _gst_boolean_or_accumulator, NULL, g_cclosure_marshal_generic,
873 G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
874 GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
875
876 /**
877 * GstDecodeBin::drained
878 * @bin: The decodebin
879 *
880 * This signal is emitted once decodebin has finished decoding all the data.
881 */
882 gst_decode_bin_signals[SIGNAL_DRAINED] =
883 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
884 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
885 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
886
887 g_object_class_install_property (gobject_klass, PROP_CAPS,
888 g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
889 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
890
891 g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
892 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
893 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
894 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
895 "be checked for an encoding to use. If that is not set either, "
896 "ISO-8859-15 will be assumed.", NULL,
897 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
898
899 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
900 g_param_spec_boxed ("sink-caps", "Sink Caps",
901 "The caps of the input data. (NULL = use typefind element)",
902 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
903
904 /**
905 * GstDecodeBin::use-buffering
906 *
907 * Activate buffering in decodebin. This will instruct the multiqueues behind
908 * decoders to emit BUFFERING messages.
909 */
910 g_object_class_install_property (gobject_klass, PROP_USE_BUFFERING,
911 g_param_spec_boolean ("use-buffering", "Use Buffering",
912 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
913 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
914
915 /**
916 * GstDecodeBin:low-percent
917 *
918 * Low threshold percent for buffering to start.
919 */
920 g_object_class_install_property (gobject_klass, PROP_LOW_PERCENT,
921 g_param_spec_int ("low-percent", "Low percent",
922 "Low threshold for buffering to start", 0, 100,
923 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
924 /**
925 * GstDecodeBin:high-percent
926 *
927 * High threshold percent for buffering to finish.
928 */
929 g_object_class_install_property (gobject_klass, PROP_HIGH_PERCENT,
930 g_param_spec_int ("high-percent", "High percent",
931 "High threshold for buffering to finish", 0, 100,
932 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
933
934 /**
935 * GstDecodeBin:max-size-bytes
936 *
937 * Max amount of bytes in the queue (0=automatic).
938 */
939 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BYTES,
940 g_param_spec_uint ("max-size-bytes", "Max. size (bytes)",
941 "Max. amount of bytes in the queue (0=automatic)",
942 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
943 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
944 /**
945 * GstDecodeBin:max-size-buffers
946 *
947 * Max amount of buffers in the queue (0=automatic).
948 */
949 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BUFFERS,
950 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
951 "Max. number of buffers in the queue (0=automatic)",
952 0, G_MAXUINT, DEFAULT_MAX_SIZE_BUFFERS,
953 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
954 /**
955 * GstDecodeBin:max-size-time
956 *
957 * Max amount of time in the queue (in ns, 0=automatic).
958 */
959 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_TIME,
960 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
961 "Max. amount of data in the queue (in ns, 0=automatic)",
962 0, G_MAXUINT64,
963 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
964
965 /**
966 * GstDecodeBin::post-stream-topology
967 *
968 * Post stream-topology messages on the bus every time the topology changes.
969 */
970 g_object_class_install_property (gobject_klass, PROP_POST_STREAM_TOPOLOGY,
971 g_param_spec_boolean ("post-stream-topology", "Post Stream Topology",
972 "Post stream-topology messages",
973 DEFAULT_POST_STREAM_TOPOLOGY,
974 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
975
976 /**
977 * GstDecodeBin::expose-all-streams
978 *
979 * Expose streams of unknown type.
980 *
981 * If set to %FALSE, then only the streams that can be decoded to the final
982 * caps (see 'caps' property) will have a pad exposed. Streams that do not
983 * match those caps but could have been decoded will not have decoder plugged
984 * in internally and will not have a pad exposed.
985 */
986 g_object_class_install_property (gobject_klass, PROP_EXPOSE_ALL_STREAMS,
987 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
988 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
989 DEFAULT_EXPOSE_ALL_STREAMS,
990 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
991
992 /**
993 * GstDecodeBin2::connection-speed
994 *
995 * Network connection speed in kbps (0 = unknownw)
996 */
997 g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
998 g_param_spec_uint64 ("connection-speed", "Connection Speed",
999 "Network connection speed in kbps (0 = unknown)",
1000 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
1001 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1002
1003
1004
1005 klass->autoplug_continue =
1006 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_continue);
1007 klass->autoplug_factories =
1008 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_factories);
1009 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_sort);
1010 klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_select);
1011 klass->autoplug_query = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_query);
1012
1013 gst_element_class_add_static_pad_template (gstelement_klass,
1014 &decoder_bin_sink_template);
1015 gst_element_class_add_static_pad_template (gstelement_klass,
1016 &decoder_bin_src_template);
1017
1018 gst_element_class_set_static_metadata (gstelement_klass,
1019 "Decoder Bin", "Generic/Bin/Decoder",
1020 "Autoplug and decode to raw media",
1021 "Edward Hervey <edward.hervey@collabora.co.uk>, "
1022 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1023
1024 gstelement_klass->change_state =
1025 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
1026
1027 gstbin_klass->handle_message =
1028 GST_DEBUG_FUNCPTR (gst_decode_bin_handle_message);
1029
1030 gstbin_klass->remove_element =
1031 GST_DEBUG_FUNCPTR (gst_decode_bin_remove_element);
1032
1033 g_type_class_ref (GST_TYPE_DECODE_PAD);
1034 }
1035
1036 /* Must be called with factories lock! */
1037 static void
gst_decode_bin_update_factories_list(GstDecodeBin * dbin)1038 gst_decode_bin_update_factories_list (GstDecodeBin * dbin)
1039 {
1040 guint cookie;
1041
1042 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
1043 if (!dbin->factories || dbin->factories_cookie != cookie) {
1044 if (dbin->factories)
1045 gst_plugin_feature_list_free (dbin->factories);
1046 dbin->factories =
1047 gst_element_factory_list_get_elements
1048 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
1049 dbin->factories =
1050 g_list_sort (dbin->factories,
1051 gst_playback_utils_compare_factories_func);
1052 dbin->factories_cookie = cookie;
1053 }
1054 }
1055
1056 static void
gst_decode_bin_init(GstDecodeBin * decode_bin)1057 gst_decode_bin_init (GstDecodeBin * decode_bin)
1058 {
1059 /* first filter out the interesting element factories */
1060 g_mutex_init (&decode_bin->factories_lock);
1061
1062 /* we create the typefind element only once */
1063 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
1064 if (!decode_bin->typefind) {
1065 g_warning ("can't find typefind element, decodebin will not work");
1066 } else {
1067 GstPad *pad;
1068 GstPad *gpad;
1069 GstPadTemplate *pad_tmpl;
1070
1071 /* add the typefind element */
1072 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
1073 g_warning ("Could not add typefind element, decodebin will not work");
1074 gst_object_unref (decode_bin->typefind);
1075 decode_bin->typefind = NULL;
1076 }
1077
1078 /* get the sinkpad */
1079 pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
1080
1081 /* get the pad template */
1082 pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
1083
1084 /* ghost the sink pad to ourself */
1085 gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
1086 gst_pad_set_active (gpad, TRUE);
1087 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
1088
1089 gst_object_unref (pad_tmpl);
1090 gst_object_unref (pad);
1091 }
1092
1093 g_mutex_init (&decode_bin->expose_lock);
1094 decode_bin->decode_chain = NULL;
1095
1096 g_mutex_init (&decode_bin->dyn_lock);
1097 decode_bin->shutdown = FALSE;
1098 decode_bin->blocked_pads = NULL;
1099
1100 g_mutex_init (&decode_bin->subtitle_lock);
1101 g_mutex_init (&decode_bin->buffering_lock);
1102 g_mutex_init (&decode_bin->buffering_post_lock);
1103
1104 g_mutex_init (&decode_bin->cleanup_lock);
1105 decode_bin->cleanup_thread = NULL;
1106
1107 decode_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
1108 decode_bin->caps = gst_static_caps_get (&default_raw_caps);
1109 decode_bin->use_buffering = DEFAULT_USE_BUFFERING;
1110 decode_bin->low_percent = DEFAULT_LOW_PERCENT;
1111 decode_bin->high_percent = DEFAULT_HIGH_PERCENT;
1112
1113 decode_bin->max_size_bytes = DEFAULT_MAX_SIZE_BYTES;
1114 decode_bin->max_size_buffers = DEFAULT_MAX_SIZE_BUFFERS;
1115 decode_bin->max_size_time = DEFAULT_MAX_SIZE_TIME;
1116
1117 decode_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
1118 decode_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
1119 }
1120
1121 static void
gst_decode_bin_dispose(GObject * object)1122 gst_decode_bin_dispose (GObject * object)
1123 {
1124 GstDecodeBin *decode_bin;
1125
1126 decode_bin = GST_DECODE_BIN (object);
1127
1128 if (decode_bin->factories)
1129 gst_plugin_feature_list_free (decode_bin->factories);
1130 decode_bin->factories = NULL;
1131
1132 if (decode_bin->decode_chain)
1133 gst_decode_chain_free (decode_bin->decode_chain);
1134 decode_bin->decode_chain = NULL;
1135
1136 if (decode_bin->caps)
1137 gst_caps_unref (decode_bin->caps);
1138 decode_bin->caps = NULL;
1139
1140 g_free (decode_bin->encoding);
1141 decode_bin->encoding = NULL;
1142
1143 g_list_free (decode_bin->subtitles);
1144 decode_bin->subtitles = NULL;
1145
1146 unblock_pads (decode_bin);
1147
1148 G_OBJECT_CLASS (parent_class)->dispose (object);
1149 }
1150
1151 static void
gst_decode_bin_finalize(GObject * object)1152 gst_decode_bin_finalize (GObject * object)
1153 {
1154 GstDecodeBin *decode_bin;
1155
1156 decode_bin = GST_DECODE_BIN (object);
1157
1158 g_mutex_clear (&decode_bin->expose_lock);
1159 g_mutex_clear (&decode_bin->dyn_lock);
1160 g_mutex_clear (&decode_bin->subtitle_lock);
1161 g_mutex_clear (&decode_bin->buffering_lock);
1162 g_mutex_clear (&decode_bin->buffering_post_lock);
1163 g_mutex_clear (&decode_bin->factories_lock);
1164 g_mutex_clear (&decode_bin->cleanup_lock);
1165
1166 G_OBJECT_CLASS (parent_class)->finalize (object);
1167 }
1168
1169 /* _set_caps
1170 * Changes the caps on which decodebin will stop decoding.
1171 * Will unref the previously set one. The refcount of the given caps will be
1172 * increased.
1173 * @caps can be NULL.
1174 *
1175 * MT-safe
1176 */
1177 static void
gst_decode_bin_set_caps(GstDecodeBin * dbin,GstCaps * caps)1178 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
1179 {
1180 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1181
1182 GST_OBJECT_LOCK (dbin);
1183 gst_caps_replace (&dbin->caps, caps);
1184 GST_OBJECT_UNLOCK (dbin);
1185 }
1186
1187 /* _get_caps
1188 * Returns the currently configured caps on which decodebin will stop decoding.
1189 * The returned caps (if not NULL), will have its refcount incremented.
1190 *
1191 * MT-safe
1192 */
1193 static GstCaps *
gst_decode_bin_get_caps(GstDecodeBin * dbin)1194 gst_decode_bin_get_caps (GstDecodeBin * dbin)
1195 {
1196 GstCaps *caps;
1197
1198 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1199
1200 GST_OBJECT_LOCK (dbin);
1201 caps = dbin->caps;
1202 if (caps)
1203 gst_caps_ref (caps);
1204 GST_OBJECT_UNLOCK (dbin);
1205
1206 return caps;
1207 }
1208
1209 static void
gst_decode_bin_set_sink_caps(GstDecodeBin * dbin,GstCaps * caps)1210 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
1211 {
1212 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1213
1214 g_object_set (dbin->typefind, "force-caps", caps, NULL);
1215 }
1216
1217 static GstCaps *
gst_decode_bin_get_sink_caps(GstDecodeBin * dbin)1218 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
1219 {
1220 GstCaps *caps;
1221
1222 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1223
1224 g_object_get (dbin->typefind, "force-caps", &caps, NULL);
1225
1226 return caps;
1227 }
1228
1229 static void
gst_decode_bin_set_subs_encoding(GstDecodeBin * dbin,const gchar * encoding)1230 gst_decode_bin_set_subs_encoding (GstDecodeBin * dbin, const gchar * encoding)
1231 {
1232 GList *walk;
1233
1234 GST_DEBUG_OBJECT (dbin, "Setting new encoding: %s", GST_STR_NULL (encoding));
1235
1236 SUBTITLE_LOCK (dbin);
1237 g_free (dbin->encoding);
1238 dbin->encoding = g_strdup (encoding);
1239
1240 /* set the subtitle encoding on all added elements */
1241 for (walk = dbin->subtitles; walk; walk = g_list_next (walk)) {
1242 g_object_set (G_OBJECT (walk->data), "subtitle-encoding", dbin->encoding,
1243 NULL);
1244 }
1245 SUBTITLE_UNLOCK (dbin);
1246 }
1247
1248 static gchar *
gst_decode_bin_get_subs_encoding(GstDecodeBin * dbin)1249 gst_decode_bin_get_subs_encoding (GstDecodeBin * dbin)
1250 {
1251 gchar *encoding;
1252
1253 GST_DEBUG_OBJECT (dbin, "Getting currently set encoding");
1254
1255 SUBTITLE_LOCK (dbin);
1256 encoding = g_strdup (dbin->encoding);
1257 SUBTITLE_UNLOCK (dbin);
1258
1259 return encoding;
1260 }
1261
1262 static void
gst_decode_bin_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1263 gst_decode_bin_set_property (GObject * object, guint prop_id,
1264 const GValue * value, GParamSpec * pspec)
1265 {
1266 GstDecodeBin *dbin;
1267
1268 dbin = GST_DECODE_BIN (object);
1269
1270 switch (prop_id) {
1271 case PROP_CAPS:
1272 gst_decode_bin_set_caps (dbin, g_value_get_boxed (value));
1273 break;
1274 case PROP_SUBTITLE_ENCODING:
1275 gst_decode_bin_set_subs_encoding (dbin, g_value_get_string (value));
1276 break;
1277 case PROP_SINK_CAPS:
1278 gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
1279 break;
1280 case PROP_USE_BUFFERING:
1281 dbin->use_buffering = g_value_get_boolean (value);
1282 break;
1283 case PROP_LOW_PERCENT:
1284 dbin->low_percent = g_value_get_int (value);
1285 break;
1286 case PROP_HIGH_PERCENT:
1287 dbin->high_percent = g_value_get_int (value);
1288 break;
1289 case PROP_MAX_SIZE_BYTES:
1290 dbin->max_size_bytes = g_value_get_uint (value);
1291 break;
1292 case PROP_MAX_SIZE_BUFFERS:
1293 dbin->max_size_buffers = g_value_get_uint (value);
1294 break;
1295 case PROP_MAX_SIZE_TIME:
1296 dbin->max_size_time = g_value_get_uint64 (value);
1297 break;
1298 case PROP_POST_STREAM_TOPOLOGY:
1299 dbin->post_stream_topology = g_value_get_boolean (value);
1300 break;
1301 case PROP_EXPOSE_ALL_STREAMS:
1302 dbin->expose_allstreams = g_value_get_boolean (value);
1303 break;
1304 case PROP_CONNECTION_SPEED:
1305 GST_OBJECT_LOCK (dbin);
1306 dbin->connection_speed = g_value_get_uint64 (value) * 1000;
1307 GST_OBJECT_UNLOCK (dbin);
1308 break;
1309 default:
1310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1311 break;
1312 }
1313 }
1314
1315 static void
gst_decode_bin_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1316 gst_decode_bin_get_property (GObject * object, guint prop_id,
1317 GValue * value, GParamSpec * pspec)
1318 {
1319 GstDecodeBin *dbin;
1320
1321 dbin = GST_DECODE_BIN (object);
1322 switch (prop_id) {
1323 case PROP_CAPS:
1324 g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
1325 break;
1326 case PROP_SUBTITLE_ENCODING:
1327 g_value_take_string (value, gst_decode_bin_get_subs_encoding (dbin));
1328 break;
1329 case PROP_SINK_CAPS:
1330 g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
1331 break;
1332 case PROP_USE_BUFFERING:
1333 g_value_set_boolean (value, dbin->use_buffering);
1334 break;
1335 case PROP_LOW_PERCENT:
1336 g_value_set_int (value, dbin->low_percent);
1337 break;
1338 case PROP_HIGH_PERCENT:
1339 g_value_set_int (value, dbin->high_percent);
1340 break;
1341 case PROP_MAX_SIZE_BYTES:
1342 g_value_set_uint (value, dbin->max_size_bytes);
1343 break;
1344 case PROP_MAX_SIZE_BUFFERS:
1345 g_value_set_uint (value, dbin->max_size_buffers);
1346 break;
1347 case PROP_MAX_SIZE_TIME:
1348 g_value_set_uint64 (value, dbin->max_size_time);
1349 break;
1350 case PROP_POST_STREAM_TOPOLOGY:
1351 g_value_set_boolean (value, dbin->post_stream_topology);
1352 break;
1353 case PROP_EXPOSE_ALL_STREAMS:
1354 g_value_set_boolean (value, dbin->expose_allstreams);
1355 break;
1356 case PROP_CONNECTION_SPEED:
1357 GST_OBJECT_LOCK (dbin);
1358 g_value_set_uint64 (value, dbin->connection_speed / 1000);
1359 GST_OBJECT_UNLOCK (dbin);
1360 break;
1361 default:
1362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1363 break;
1364 }
1365 }
1366
1367
1368 /*****
1369 * Default autoplug signal handlers
1370 *****/
1371 static gboolean
gst_decode_bin_autoplug_continue(GstElement * element,GstPad * pad,GstCaps * caps)1372 gst_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
1373 GstCaps * caps)
1374 {
1375 GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
1376
1377 /* by default we always continue */
1378 return TRUE;
1379 }
1380
1381 static GValueArray *
gst_decode_bin_autoplug_factories(GstElement * element,GstPad * pad,GstCaps * caps)1382 gst_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
1383 GstCaps * caps)
1384 {
1385 GList *list, *tmp;
1386 GValueArray *result;
1387 GstDecodeBin *dbin = GST_DECODE_BIN_CAST (element);
1388
1389 GST_DEBUG_OBJECT (element, "finding factories");
1390
1391 /* return all compatible factories for caps */
1392 g_mutex_lock (&dbin->factories_lock);
1393 gst_decode_bin_update_factories_list (dbin);
1394 list =
1395 gst_element_factory_list_filter (dbin->factories, caps, GST_PAD_SINK,
1396 gst_caps_is_fixed (caps));
1397 g_mutex_unlock (&dbin->factories_lock);
1398
1399 result = g_value_array_new (g_list_length (list));
1400 for (tmp = list; tmp; tmp = tmp->next) {
1401 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1402 GValue val = { 0, };
1403
1404 g_value_init (&val, G_TYPE_OBJECT);
1405 g_value_set_object (&val, factory);
1406 g_value_array_append (result, &val);
1407 g_value_unset (&val);
1408 }
1409 gst_plugin_feature_list_free (list);
1410
1411 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
1412
1413 return result;
1414 }
1415
1416 static GValueArray *
gst_decode_bin_autoplug_sort(GstElement * element,GstPad * pad,GstCaps * caps,GValueArray * factories)1417 gst_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
1418 GstCaps * caps, GValueArray * factories)
1419 {
1420 return NULL;
1421 }
1422
1423 static GstAutoplugSelectResult
gst_decode_bin_autoplug_select(GstElement * element,GstPad * pad,GstCaps * caps,GstElementFactory * factory)1424 gst_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
1425 GstCaps * caps, GstElementFactory * factory)
1426 {
1427 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
1428
1429 /* Try factory. */
1430 return GST_AUTOPLUG_SELECT_TRY;
1431 }
1432
1433 static gboolean
gst_decode_bin_autoplug_query(GstElement * element,GstPad * pad,GstQuery * query)1434 gst_decode_bin_autoplug_query (GstElement * element, GstPad * pad,
1435 GstQuery * query)
1436 {
1437 /* No query handled here */
1438 return FALSE;
1439 }
1440
1441 /********
1442 * Discovery methods
1443 *****/
1444
1445 static gboolean are_final_caps (GstDecodeBin * dbin, GstCaps * caps);
1446 static gboolean is_demuxer_element (GstElement * srcelement);
1447 static gboolean is_adaptive_demuxer_element (GstElement * srcelement);
1448
1449 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
1450 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GValueArray * factories,
1451 GstDecodeChain * chain, gchar ** deadend_details);
1452 static GList *connect_element (GstDecodeBin * dbin, GstDecodeElement * delem,
1453 GstDecodeChain * chain);
1454 static void expose_pad (GstDecodeBin * dbin, GstElement * src,
1455 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GstDecodeChain * chain);
1456
1457 static void pad_added_cb (GstElement * element, GstPad * pad,
1458 GstDecodeChain * chain);
1459 static void pad_removed_cb (GstElement * element, GstPad * pad,
1460 GstDecodeChain * chain);
1461 static void no_more_pads_cb (GstElement * element, GstDecodeChain * chain);
1462
1463 static GstDecodeGroup *gst_decode_chain_get_current_group (GstDecodeChain *
1464 chain);
1465
1466 static gboolean
clear_sticky_events(GstPad * pad,GstEvent ** event,gpointer user_data)1467 clear_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1468 {
1469 GST_DEBUG_OBJECT (pad, "clearing sticky event %" GST_PTR_FORMAT, *event);
1470 gst_event_unref (*event);
1471 *event = NULL;
1472 return TRUE;
1473 }
1474
1475 static gboolean
copy_sticky_events(GstPad * pad,GstEvent ** event,gpointer user_data)1476 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1477 {
1478 GstPad *gpad = GST_PAD_CAST (user_data);
1479
1480 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
1481 gst_pad_store_sticky_event (gpad, *event);
1482
1483 return TRUE;
1484 }
1485
1486 static void
decode_pad_set_target(GstDecodePad * dpad,GstPad * target)1487 decode_pad_set_target (GstDecodePad * dpad, GstPad * target)
1488 {
1489 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), target);
1490 if (target == NULL)
1491 gst_pad_sticky_events_foreach (GST_PAD_CAST (dpad), clear_sticky_events,
1492 NULL);
1493 else
1494 gst_pad_sticky_events_foreach (target, copy_sticky_events, dpad);
1495 }
1496
1497 /* called when a new pad is discovered. It will perform some basic actions
1498 * before trying to link something to it.
1499 *
1500 * - Check the caps, don't do anything when there are no caps or when they have
1501 * no good type.
1502 * - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1503 * pad.
1504 * - if the caps are non-fixed, setup a handler to continue autoplugging when
1505 * the caps become fixed (connect to notify::caps).
1506 * - get list of factories to autoplug.
1507 * - continue autoplugging to one of the factories.
1508 */
1509 /* returns whether to expose the pad */
1510 static gboolean
analyze_new_pad(GstDecodeBin * dbin,GstElement * src,GstPad * pad,GstCaps * caps,GstDecodeChain * chain,GstDecodeChain ** new_chain)1511 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1512 GstCaps * caps, GstDecodeChain * chain, GstDecodeChain ** new_chain)
1513 {
1514 gboolean apcontinue = TRUE;
1515 GValueArray *factories = NULL, *result = NULL;
1516 GstDecodePad *dpad;
1517 GstElementFactory *factory;
1518 const gchar *classification;
1519 gboolean is_parser_converter = FALSE;
1520 gboolean res;
1521 gchar *deadend_details = NULL;
1522
1523 GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1524 GST_DEBUG_PAD_NAME (pad), caps);
1525
1526 if (new_chain)
1527 *new_chain = chain;
1528
1529 if (chain->elements
1530 && src != ((GstDecodeElement *) chain->elements->data)->element
1531 && src != ((GstDecodeElement *) chain->elements->data)->capsfilter) {
1532 GST_ERROR_OBJECT (dbin, "New pad from not the last element in this chain");
1533 return FALSE;
1534 }
1535
1536 if (chain->endpad) {
1537 GST_ERROR_OBJECT (dbin, "New pad in a chain that is already complete");
1538 return FALSE;
1539 }
1540
1541 if (chain->demuxer) {
1542 GstDecodeGroup *group;
1543 GstDecodeChain *oldchain = chain;
1544 GstDecodeElement *demux = (chain->elements ? chain->elements->data : NULL);
1545
1546 if (chain->current_pad)
1547 gst_object_unref (chain->current_pad);
1548 chain->current_pad = NULL;
1549
1550 /* we are adding a new pad for a demuxer (see is_demuxer_element(),
1551 * start a new chain for it */
1552 CHAIN_MUTEX_LOCK (oldchain);
1553 group = gst_decode_chain_get_current_group (chain);
1554 if (group && !g_list_find (group->children, chain)) {
1555 g_assert (new_chain != NULL);
1556 *new_chain = chain = gst_decode_chain_new (dbin, group, pad);
1557 group->children = g_list_prepend (group->children, chain);
1558 }
1559 CHAIN_MUTEX_UNLOCK (oldchain);
1560 if (!group) {
1561 GST_WARNING_OBJECT (dbin, "No current group");
1562 return FALSE;
1563 }
1564
1565 /* If this is not a dynamic pad demuxer, we're no-more-pads
1566 * already before anything else happens
1567 */
1568 if (demux == NULL || !demux->no_more_pads_id)
1569 group->no_more_pads = TRUE;
1570 }
1571
1572 /* From here on we own a reference to the caps as
1573 * we might create new caps below and would need
1574 * to unref them later */
1575 if (caps)
1576 gst_caps_ref (caps);
1577
1578 if ((caps == NULL) || gst_caps_is_empty (caps))
1579 goto unknown_type;
1580
1581 if (gst_caps_is_any (caps))
1582 goto any_caps;
1583
1584 if (!chain->current_pad)
1585 chain->current_pad = gst_decode_pad_new (dbin, chain);
1586
1587 dpad = gst_object_ref (chain->current_pad);
1588 gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
1589 decode_pad_set_target (dpad, pad);
1590
1591 /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1592 * further autoplugging. Only do this for fixed caps, for unfixed caps
1593 * we will later come here again from the notify::caps handler. The
1594 * problem with unfixed caps is that, we can't reliably tell if the output
1595 * is e.g. accepted by a sink because only parts of the possible final
1596 * caps might be accepted by the sink. */
1597 if (gst_caps_is_fixed (caps))
1598 g_signal_emit (G_OBJECT (dbin),
1599 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, caps,
1600 &apcontinue);
1601 else
1602 apcontinue = TRUE;
1603
1604 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1605 if ((!apcontinue) || are_final_caps (dbin, caps))
1606 goto expose_pad;
1607
1608 /* 1.b For Parser/Converter that can output different stream formats
1609 * we insert a capsfilter with the sorted caps of all possible next
1610 * elements and continue with the capsfilter srcpad */
1611 factory = gst_element_get_factory (src);
1612 classification =
1613 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1614 is_parser_converter = (strstr (classification, "Parser")
1615 && strstr (classification, "Converter"));
1616
1617 /* 1.c when the caps are not fixed yet, we can't be sure what element to
1618 * connect. We delay autoplugging until the caps are fixed */
1619 if (!is_parser_converter && !gst_caps_is_fixed (caps)) {
1620 goto non_fixed;
1621 } else if (!is_parser_converter) {
1622 gst_caps_unref (caps);
1623 caps = gst_pad_get_current_caps (pad);
1624 if (!caps) {
1625 GST_DEBUG_OBJECT (dbin, "No final caps set yet, delaying autoplugging");
1626 gst_object_unref (dpad);
1627 goto setup_caps_delay;
1628 }
1629 }
1630
1631 /* 1.d else get the factories and if there's no compatible factory goto
1632 * unknown_type */
1633 g_signal_emit (G_OBJECT (dbin),
1634 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, dpad, caps,
1635 &factories);
1636
1637 /* NULL means that we can expose the pad */
1638 if (factories == NULL)
1639 goto expose_pad;
1640
1641 /* if the array is empty, we have a type for which we have no decoder */
1642 if (factories->n_values == 0) {
1643 if (!dbin->expose_allstreams) {
1644 GstCaps *raw = gst_static_caps_get (&default_raw_caps);
1645
1646 /* If the caps are raw, this just means we don't want to expose them */
1647 if (gst_caps_is_subset (caps, raw)) {
1648 g_value_array_free (factories);
1649 gst_caps_unref (raw);
1650 gst_object_unref (dpad);
1651 goto discarded_type;
1652 }
1653 gst_caps_unref (raw);
1654 }
1655
1656 /* if not we have a unhandled type with no compatible factories */
1657 g_value_array_free (factories);
1658 gst_object_unref (dpad);
1659 goto unknown_type;
1660 }
1661
1662 /* 1.e sort some more. */
1663 g_signal_emit (G_OBJECT (dbin),
1664 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, dpad, caps, factories,
1665 &result);
1666 if (result) {
1667 g_value_array_free (factories);
1668 factories = result;
1669 }
1670
1671 /* At this point we have a potential decoder, but we might not need it
1672 * if it doesn't match the output caps */
1673 if (!dbin->expose_allstreams && gst_caps_is_fixed (caps)) {
1674 guint i;
1675 const GList *tmps;
1676 gboolean dontuse = FALSE;
1677
1678 GST_DEBUG ("Checking if we can abort early");
1679
1680 /* 1.f Do an early check to see if the candidates are potential decoders, but
1681 * due to the fact that they decode to a mediatype that is not final we don't
1682 * need them */
1683
1684 for (i = 0; i < factories->n_values && !dontuse; i++) {
1685 GstElementFactory *factory =
1686 g_value_get_object (g_value_array_get_nth (factories, i));
1687 GstCaps *tcaps;
1688
1689 /* We are only interested in skipping decoders */
1690 if (strstr (gst_element_factory_get_metadata (factory,
1691 GST_ELEMENT_METADATA_KLASS), "Decoder")) {
1692
1693 GST_DEBUG ("Trying factory %s",
1694 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1695
1696 /* Check the source pad template caps to see if they match raw caps but don't match
1697 * our final caps*/
1698 for (tmps = gst_element_factory_get_static_pad_templates (factory);
1699 tmps && !dontuse; tmps = tmps->next) {
1700 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1701 if (st->direction != GST_PAD_SRC)
1702 continue;
1703 tcaps = gst_static_pad_template_get_caps (st);
1704
1705 apcontinue = TRUE;
1706
1707 /* Emit autoplug-continue to see if the caps are considered to be raw caps */
1708 g_signal_emit (G_OBJECT (dbin),
1709 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, tcaps,
1710 &apcontinue);
1711
1712 /* If autoplug-continue returns TRUE and the caps are not final, don't use them */
1713 if (apcontinue && !are_final_caps (dbin, tcaps))
1714 dontuse = TRUE;
1715 gst_caps_unref (tcaps);
1716 }
1717 }
1718 }
1719
1720 if (dontuse) {
1721 gst_object_unref (dpad);
1722 g_value_array_free (factories);
1723 goto discarded_type;
1724 }
1725 }
1726
1727 /* 1.g now get the factory template caps and insert the capsfilter if this
1728 * is a parser/converter
1729 */
1730 if (is_parser_converter) {
1731 GstCaps *filter_caps;
1732 gint i;
1733 GstElement *capsfilter;
1734 GstPad *p;
1735 GstDecodeElement *delem;
1736
1737 filter_caps = gst_caps_new_empty ();
1738 for (i = 0; i < factories->n_values; i++) {
1739 GstElementFactory *factory =
1740 g_value_get_object (g_value_array_get_nth (factories, i));
1741 GstCaps *tcaps, *intersection;
1742 const GList *tmps;
1743
1744 GST_DEBUG ("Trying factory %s",
1745 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1746
1747 if (gst_element_get_factory (src) == factory ||
1748 gst_element_factory_list_is_type (factory,
1749 GST_ELEMENT_FACTORY_TYPE_PARSER)) {
1750 GST_DEBUG ("Skipping factory");
1751 continue;
1752 }
1753
1754 for (tmps = gst_element_factory_get_static_pad_templates (factory); tmps;
1755 tmps = tmps->next) {
1756 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1757 if (st->direction != GST_PAD_SINK || st->presence != GST_PAD_ALWAYS)
1758 continue;
1759 tcaps = gst_static_pad_template_get_caps (st);
1760 intersection =
1761 gst_caps_intersect_full (tcaps, caps, GST_CAPS_INTERSECT_FIRST);
1762 filter_caps = gst_caps_merge (filter_caps, intersection);
1763 gst_caps_unref (tcaps);
1764 }
1765 }
1766
1767 /* Append the parser caps to prevent any not-negotiated errors */
1768 filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (caps));
1769
1770 if (chain->elements) {
1771 delem = (GstDecodeElement *) chain->elements->data;
1772 capsfilter = delem->capsfilter =
1773 gst_element_factory_make ("capsfilter", NULL);
1774 } else {
1775 delem = g_slice_new0 (GstDecodeElement);
1776 capsfilter = delem->element =
1777 gst_element_factory_make ("capsfilter", NULL);
1778 delem->capsfilter = NULL;
1779 chain->elements = g_list_prepend (chain->elements, delem);
1780 }
1781
1782 g_object_set (G_OBJECT (capsfilter), "caps", filter_caps, NULL);
1783 gst_caps_unref (filter_caps);
1784 gst_element_set_state (capsfilter, GST_STATE_PAUSED);
1785 gst_bin_add (GST_BIN_CAST (dbin), gst_object_ref (capsfilter));
1786
1787 decode_pad_set_target (dpad, NULL);
1788 p = gst_element_get_static_pad (capsfilter, "sink");
1789 gst_pad_link_full (pad, p, GST_PAD_LINK_CHECK_NOTHING);
1790 gst_object_unref (p);
1791 p = gst_element_get_static_pad (capsfilter, "src");
1792 decode_pad_set_target (dpad, p);
1793 pad = p;
1794
1795 gst_caps_unref (caps);
1796
1797 caps = gst_pad_get_current_caps (pad);
1798 if (!caps) {
1799 GST_DEBUG_OBJECT (dbin, "No final caps set yet, delaying autoplugging");
1800 gst_object_unref (dpad);
1801 g_value_array_free (factories);
1802 goto setup_caps_delay;
1803 }
1804 }
1805
1806 /* 1.h else continue autoplugging something from the list. */
1807 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1808 res =
1809 connect_pad (dbin, src, dpad, pad, caps, factories, chain,
1810 &deadend_details);
1811
1812 /* Need to unref the capsfilter srcpad here if
1813 * we inserted a capsfilter */
1814 if (is_parser_converter)
1815 gst_object_unref (pad);
1816
1817 gst_object_unref (dpad);
1818 g_value_array_free (factories);
1819
1820 if (!res)
1821 goto unknown_type;
1822
1823 gst_caps_unref (caps);
1824
1825 return FALSE;
1826
1827 expose_pad:
1828 {
1829 GST_LOG_OBJECT (dbin, "Pad is final and should expose the pad. "
1830 "autoplug-continue:%d", apcontinue);
1831 gst_object_unref (dpad);
1832 gst_caps_unref (caps);
1833 return TRUE;
1834 }
1835
1836 discarded_type:
1837 {
1838 GST_LOG_OBJECT (pad, "Known type, but discarded because not final caps");
1839 chain->deadend = TRUE;
1840 chain->endcaps = caps;
1841 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
1842
1843 /* Try to expose anything */
1844 EXPOSE_LOCK (dbin);
1845 if (dbin->decode_chain) {
1846 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1847 gst_decode_bin_expose (dbin);
1848 }
1849 }
1850 EXPOSE_UNLOCK (dbin);
1851 do_async_done (dbin);
1852
1853 return FALSE;
1854 }
1855
1856 unknown_type:
1857 {
1858 GST_LOG_OBJECT (pad, "Unknown type, posting message and firing signal");
1859
1860 chain->deadend_details = deadend_details;
1861 chain->deadend = TRUE;
1862 chain->endcaps = caps;
1863 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
1864
1865 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1866 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1867
1868 g_signal_emit (G_OBJECT (dbin),
1869 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1870
1871 /* Try to expose anything */
1872 EXPOSE_LOCK (dbin);
1873 if (dbin->decode_chain) {
1874 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1875 gst_decode_bin_expose (dbin);
1876 }
1877 }
1878 EXPOSE_UNLOCK (dbin);
1879
1880 if (src == dbin->typefind) {
1881 if (!caps || gst_caps_is_empty (caps)) {
1882 GST_ELEMENT_ERROR (dbin, STREAM, TYPE_NOT_FOUND,
1883 (_("Could not determine type of stream")), (NULL));
1884 }
1885 do_async_done (dbin);
1886 }
1887 return FALSE;
1888 }
1889 non_fixed:
1890 {
1891 GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1892 gst_object_unref (dpad);
1893 goto setup_caps_delay;
1894 }
1895 any_caps:
1896 {
1897 GST_DEBUG_OBJECT (pad, "pad has ANY caps, delaying auto-pluggin");
1898 goto setup_caps_delay;
1899 }
1900 setup_caps_delay:
1901 {
1902 GstPendingPad *ppad;
1903
1904 /* connect to caps notification */
1905 CHAIN_MUTEX_LOCK (chain);
1906 GST_LOG_OBJECT (dbin, "Chain %p has now %d dynamic pads", chain,
1907 g_list_length (chain->pending_pads));
1908 ppad = g_slice_new0 (GstPendingPad);
1909 ppad->pad = gst_object_ref (pad);
1910 ppad->chain = chain;
1911 ppad->event_probe_id =
1912 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1913 pad_event_cb, ppad, NULL);
1914 chain->pending_pads = g_list_prepend (chain->pending_pads, ppad);
1915 ppad->notify_caps_id = g_signal_connect (pad, "notify::caps",
1916 G_CALLBACK (caps_notify_cb), chain);
1917 CHAIN_MUTEX_UNLOCK (chain);
1918
1919 /* If we're here because we have a Parser/Converter
1920 * we have to unref the pad */
1921 if (is_parser_converter)
1922 gst_object_unref (pad);
1923 if (caps)
1924 gst_caps_unref (caps);
1925
1926 return FALSE;
1927 }
1928 }
1929
1930 static void
add_error_filter(GstDecodeBin * dbin,GstElement * element)1931 add_error_filter (GstDecodeBin * dbin, GstElement * element)
1932 {
1933 GST_OBJECT_LOCK (dbin);
1934 dbin->filtered = g_list_prepend (dbin->filtered, element);
1935 GST_OBJECT_UNLOCK (dbin);
1936 }
1937
1938 static void
remove_error_filter(GstDecodeBin * dbin,GstElement * element,GstMessage ** error)1939 remove_error_filter (GstDecodeBin * dbin, GstElement * element,
1940 GstMessage ** error)
1941 {
1942 GList *l;
1943
1944 GST_OBJECT_LOCK (dbin);
1945 dbin->filtered = g_list_remove (dbin->filtered, element);
1946
1947 if (error)
1948 *error = NULL;
1949
1950 l = dbin->filtered_errors;
1951 while (l) {
1952 GstMessage *msg = l->data;
1953
1954 if (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
1955 /* Get the last error of this element, i.e. the earliest */
1956 if (error)
1957 gst_message_replace (error, msg);
1958 gst_message_unref (msg);
1959 l = dbin->filtered_errors = g_list_delete_link (dbin->filtered_errors, l);
1960 } else {
1961 l = l->next;
1962 }
1963 }
1964 GST_OBJECT_UNLOCK (dbin);
1965 }
1966
1967 typedef struct
1968 {
1969 gboolean ret;
1970 GstPad *peer;
1971 } SendStickyEventsData;
1972
1973 static gboolean
send_sticky_event(GstPad * pad,GstEvent ** event,gpointer user_data)1974 send_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
1975 {
1976 SendStickyEventsData *data = user_data;
1977 gboolean ret;
1978
1979 ret = gst_pad_send_event (data->peer, gst_event_ref (*event));
1980 if (!ret)
1981 data->ret = FALSE;
1982
1983 return data->ret;
1984 }
1985
1986 static gboolean
send_sticky_events(GstDecodeBin * dbin,GstPad * pad)1987 send_sticky_events (GstDecodeBin * dbin, GstPad * pad)
1988 {
1989 SendStickyEventsData data;
1990
1991 data.ret = TRUE;
1992 data.peer = gst_pad_get_peer (pad);
1993
1994 gst_pad_sticky_events_foreach (pad, send_sticky_event, &data);
1995
1996 gst_object_unref (data.peer);
1997
1998 return data.ret;
1999 }
2000
2001 static gchar *
error_message_to_string(GstMessage * msg)2002 error_message_to_string (GstMessage * msg)
2003 {
2004 GError *err;
2005 gchar *debug, *message, *full_message;
2006
2007 gst_message_parse_error (msg, &err, &debug);
2008
2009 message = gst_error_get_message (err->domain, err->code);
2010
2011 if (debug)
2012 full_message = g_strdup_printf ("%s\n%s\n%s", message, err->message, debug);
2013 else
2014 full_message = g_strdup_printf ("%s\n%s", message, err->message);
2015
2016 g_free (message);
2017 g_free (debug);
2018 g_clear_error (&err);
2019
2020 return full_message;
2021 }
2022
2023 static GstPadProbeReturn
demuxer_source_pad_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)2024 demuxer_source_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2025 gpointer user_data)
2026 {
2027 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2028 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
2029 GstDecodeChain *parent_chain = group->parent;
2030
2031 GST_DEBUG_OBJECT (pad, "Saw event %s", GST_EVENT_TYPE_NAME (event));
2032 /* Check if we are the active group, if not we need to proxy the flush
2033 * events to the other groups (of which at least one is exposed, ensuring
2034 * flushing properly propagates downstream of decodebin */
2035 if (parent_chain->active_group == group)
2036 return GST_PAD_PROBE_OK;
2037
2038 switch (GST_EVENT_TYPE (event)) {
2039 case GST_EVENT_FLUSH_START:
2040 case GST_EVENT_FLUSH_STOP:
2041 {
2042 GList *tmp;
2043 GST_DEBUG_OBJECT (pad, "Proxying flush events to inactive groups");
2044 /* Proxy to active group */
2045 for (tmp = parent_chain->active_group->reqpads; tmp; tmp = tmp->next) {
2046 GstPad *reqpad = (GstPad *) tmp->data;
2047 gst_pad_send_event (reqpad, gst_event_ref (event));
2048 }
2049 /* Proxy to other non-active groups (except ourself) */
2050 for (tmp = parent_chain->next_groups; tmp; tmp = tmp->next) {
2051 GList *tmp2;
2052 GstDecodeGroup *tmpgroup = (GstDecodeGroup *) tmp->data;
2053 if (tmpgroup != group) {
2054 for (tmp2 = tmpgroup->reqpads; tmp2; tmp2 = tmp2->next) {
2055 GstPad *reqpad = (GstPad *) tmp2->data;
2056 gst_pad_send_event (reqpad, gst_event_ref (event));
2057 }
2058 }
2059 }
2060 flush_chain (parent_chain,
2061 GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START);
2062 }
2063 break;
2064 default:
2065 break;
2066 }
2067
2068 return GST_PAD_PROBE_OK;
2069 }
2070
2071 typedef struct
2072 {
2073 GstDecodeChain *chain;
2074 GstPad *pad;
2075 } PadExposeData;
2076
2077 /* connect_pad:
2078 *
2079 * Try to connect the given pad to an element created from one of the factories,
2080 * and recursively.
2081 *
2082 * Note that dpad is ghosting pad, and so pad is linked; be sure to unset dpad's
2083 * target before trying to link pad.
2084 *
2085 * Returns TRUE if an element was properly created and linked
2086 */
2087 static gboolean
connect_pad(GstDecodeBin * dbin,GstElement * src,GstDecodePad * dpad,GstPad * pad,GstCaps * caps,GValueArray * factories,GstDecodeChain * chain,gchar ** deadend_details)2088 connect_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2089 GstPad * pad, GstCaps * caps, GValueArray * factories,
2090 GstDecodeChain * chain, gchar ** deadend_details)
2091 {
2092 gboolean res = FALSE;
2093 GstPad *mqpad = NULL;
2094 gboolean is_demuxer = chain->parent && !chain->elements; /* First pad after the demuxer */
2095 GString *error_details = NULL;
2096
2097 g_return_val_if_fail (factories != NULL, FALSE);
2098 g_return_val_if_fail (factories->n_values > 0, FALSE);
2099
2100 GST_DEBUG_OBJECT (dbin,
2101 "pad %s:%s , chain:%p, %d factories, caps %" GST_PTR_FORMAT,
2102 GST_DEBUG_PAD_NAME (pad), chain, factories->n_values, caps);
2103
2104 /* 1. is element demuxer or parser */
2105 if (is_demuxer) {
2106 GST_LOG_OBJECT (src,
2107 "is a demuxer, connecting the pad through multiqueue '%s'",
2108 GST_OBJECT_NAME (chain->parent->multiqueue));
2109
2110 /* Set a flush-start/-stop probe on the downstream events */
2111 chain->pad_probe_id =
2112 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_FLUSH,
2113 demuxer_source_pad_probe, chain->parent, NULL);
2114
2115 decode_pad_set_target (dpad, NULL);
2116 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2117 goto beach;
2118 src = chain->parent->multiqueue;
2119 /* Forward sticky events to mq src pad to allow factory initialization */
2120 gst_pad_sticky_events_foreach (pad, copy_sticky_events, mqpad);
2121 pad = mqpad;
2122 decode_pad_set_target (dpad, pad);
2123 }
2124
2125 error_details = g_string_new ("");
2126
2127 /* 2. Try to create an element and link to it */
2128 while (factories->n_values > 0) {
2129 GstAutoplugSelectResult ret;
2130 GstElementFactory *factory;
2131 GstDecodeElement *delem;
2132 GstElement *element;
2133 GstPad *sinkpad;
2134 GParamSpec *pspec;
2135 gboolean subtitle;
2136 GList *to_connect = NULL;
2137 GList *to_expose = NULL;
2138 gboolean is_parser = FALSE;
2139 gboolean is_decoder = FALSE;
2140
2141 /* Set dpad target to pad again, it might've been unset
2142 * below but we came back here because something failed
2143 */
2144 decode_pad_set_target (dpad, pad);
2145
2146 /* take first factory */
2147 factory = g_value_get_object (g_value_array_get_nth (factories, 0));
2148 /* Remove selected factory from the list. */
2149 g_value_array_remove (factories, 0);
2150
2151 GST_LOG_OBJECT (src, "trying factory %" GST_PTR_FORMAT, factory);
2152
2153 /* Check if the caps are really supported by the factory. The
2154 * factory list is non-empty-subset filtered while caps
2155 * are only accepted by a pad if they are a subset of the
2156 * pad caps.
2157 *
2158 * FIXME: Only do this for fixed caps here. Non-fixed caps
2159 * can happen if a Parser/Converter was autoplugged before
2160 * this. We then assume that it will be able to convert to
2161 * everything that the decoder would want.
2162 *
2163 * A subset check will fail here because the parser caps
2164 * will be generic and while the decoder will only
2165 * support a subset of the parser caps.
2166 */
2167 if (gst_caps_is_fixed (caps)) {
2168 const GList *templs;
2169 gboolean skip = FALSE;
2170
2171 templs = gst_element_factory_get_static_pad_templates (factory);
2172
2173 while (templs) {
2174 GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
2175
2176 if (templ->direction == GST_PAD_SINK) {
2177 GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
2178
2179 if (!gst_caps_is_subset (caps, templcaps)) {
2180 GST_DEBUG_OBJECT (src,
2181 "caps %" GST_PTR_FORMAT " not subset of %" GST_PTR_FORMAT, caps,
2182 templcaps);
2183 gst_caps_unref (templcaps);
2184 skip = TRUE;
2185 break;
2186 }
2187
2188 gst_caps_unref (templcaps);
2189 }
2190 templs = g_list_next (templs);
2191 }
2192 if (skip)
2193 continue;
2194 }
2195
2196 /* If the factory is for a parser we first check if the factory
2197 * was already used for the current chain. If it was used already
2198 * we would otherwise create an infinite loop here because the
2199 * parser apparently accepts its own output as input.
2200 * This is only done for parsers because it's perfectly valid
2201 * to have other element classes after each other because a
2202 * parser is the only one that does not change the data. A
2203 * valid example for this would be multiple id3demux in a row.
2204 */
2205 is_parser = strstr (gst_element_factory_get_metadata (factory,
2206 GST_ELEMENT_METADATA_KLASS), "Parser") != NULL;
2207
2208 if (is_parser) {
2209 gboolean skip = FALSE;
2210 GList *l;
2211
2212 CHAIN_MUTEX_LOCK (chain);
2213 for (l = chain->elements; l; l = l->next) {
2214 GstDecodeElement *delem = (GstDecodeElement *) l->data;
2215 GstElement *otherelement = delem->element;
2216
2217 if (gst_element_get_factory (otherelement) == factory) {
2218 skip = TRUE;
2219 break;
2220 }
2221 }
2222
2223 if (!skip && chain->parent && chain->parent->parent) {
2224 GstDecodeChain *parent_chain = chain->parent->parent;
2225 GstDecodeElement *pelem =
2226 parent_chain->elements ? parent_chain->elements->data : NULL;
2227
2228 if (pelem && gst_element_get_factory (pelem->element) == factory)
2229 skip = TRUE;
2230 }
2231 CHAIN_MUTEX_UNLOCK (chain);
2232 if (skip) {
2233 GST_DEBUG_OBJECT (dbin,
2234 "Skipping factory '%s' because it was already used in this chain",
2235 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory)));
2236 continue;
2237 }
2238
2239 }
2240
2241 /* emit autoplug-select to see what we should do with it. */
2242 g_signal_emit (G_OBJECT (dbin),
2243 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT],
2244 0, dpad, caps, factory, &ret);
2245
2246 switch (ret) {
2247 case GST_AUTOPLUG_SELECT_TRY:
2248 GST_DEBUG_OBJECT (dbin, "autoplug select requested try");
2249 break;
2250 case GST_AUTOPLUG_SELECT_EXPOSE:
2251 GST_DEBUG_OBJECT (dbin, "autoplug select requested expose");
2252 /* expose the pad, we don't have the source element */
2253 expose_pad (dbin, src, dpad, pad, caps, chain);
2254 res = TRUE;
2255 goto beach;
2256 case GST_AUTOPLUG_SELECT_SKIP:
2257 GST_DEBUG_OBJECT (dbin, "autoplug select requested skip");
2258 continue;
2259 default:
2260 GST_WARNING_OBJECT (dbin, "autoplug select returned unhandled %d", ret);
2261 break;
2262 }
2263
2264 /* 2.0. Unlink pad */
2265 decode_pad_set_target (dpad, NULL);
2266
2267 /* 2.1. Try to create an element */
2268 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
2269 GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
2270 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
2271 g_string_append_printf (error_details,
2272 "Could not create an element from %s\n",
2273 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
2274 continue;
2275 }
2276
2277 /* Filter errors, this will prevent the element from causing the pipeline
2278 * to error while we test it using READY state. */
2279 add_error_filter (dbin, element);
2280
2281 /* We don't yet want the bin to control the element's state */
2282 gst_element_set_locked_state (element, TRUE);
2283
2284 /* ... add it ... */
2285 if (!(gst_bin_add (GST_BIN_CAST (dbin), element))) {
2286 GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
2287 GST_ELEMENT_NAME (element));
2288 remove_error_filter (dbin, element, NULL);
2289 g_string_append_printf (error_details, "Couldn't add %s to the bin\n",
2290 GST_ELEMENT_NAME (element));
2291 gst_object_unref (element);
2292 continue;
2293 }
2294
2295 /* Find its sink pad. */
2296 if (!(sinkpad = find_sink_pad (element))) {
2297 GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
2298 GST_ELEMENT_NAME (element));
2299 remove_error_filter (dbin, element, NULL);
2300 g_string_append_printf (error_details,
2301 "Element %s doesn't have a sink pad", GST_ELEMENT_NAME (element));
2302 gst_bin_remove (GST_BIN (dbin), element);
2303 continue;
2304 }
2305
2306 /* ... and try to link */
2307 if ((gst_pad_link_full (pad, sinkpad,
2308 GST_PAD_LINK_CHECK_NOTHING)) != GST_PAD_LINK_OK) {
2309 GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
2310 GST_DEBUG_PAD_NAME (sinkpad));
2311 remove_error_filter (dbin, element, NULL);
2312 g_string_append_printf (error_details, "Link failed on pad %s:%s",
2313 GST_DEBUG_PAD_NAME (sinkpad));
2314 gst_object_unref (sinkpad);
2315 gst_bin_remove (GST_BIN (dbin), element);
2316 continue;
2317 }
2318
2319 /* ... activate it ... */
2320 if ((gst_element_set_state (element,
2321 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
2322 GstMessage *error_msg;
2323
2324 GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
2325 GST_ELEMENT_NAME (element));
2326 remove_error_filter (dbin, element, &error_msg);
2327
2328 if (error_msg) {
2329 gchar *error_string = error_message_to_string (error_msg);
2330 g_string_append_printf (error_details, "Couldn't set %s to READY:\n%s",
2331 GST_ELEMENT_NAME (element), error_string);
2332 gst_message_unref (error_msg);
2333 g_free (error_string);
2334 } else {
2335 g_string_append_printf (error_details, "Couldn't set %s to READY",
2336 GST_ELEMENT_NAME (element));
2337 }
2338 gst_object_unref (sinkpad);
2339 gst_bin_remove (GST_BIN (dbin), element);
2340 continue;
2341 }
2342
2343 /* check if we still accept the caps on the pad after setting
2344 * the element to READY */
2345 if (!gst_pad_query_accept_caps (sinkpad, caps)) {
2346 GstMessage *error_msg;
2347
2348 GST_WARNING_OBJECT (dbin, "Element %s does not accept caps",
2349 GST_ELEMENT_NAME (element));
2350
2351 remove_error_filter (dbin, element, &error_msg);
2352
2353 if (error_msg) {
2354 gchar *error_string = error_message_to_string (error_msg);
2355 g_string_append_printf (error_details,
2356 "Element %s does not accept caps:\n%s", GST_ELEMENT_NAME (element),
2357 error_string);
2358 gst_message_unref (error_msg);
2359 g_free (error_string);
2360 } else {
2361 g_string_append_printf (error_details,
2362 "Element %s does not accept caps", GST_ELEMENT_NAME (element));
2363 }
2364
2365 gst_element_set_state (element, GST_STATE_NULL);
2366 gst_object_unref (sinkpad);
2367 gst_bin_remove (GST_BIN (dbin), element);
2368 continue;
2369 }
2370
2371 gst_object_unref (sinkpad);
2372 GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2373
2374 CHAIN_MUTEX_LOCK (chain);
2375 delem = g_slice_new0 (GstDecodeElement);
2376 delem->element = gst_object_ref (element);
2377 delem->capsfilter = NULL;
2378 chain->elements = g_list_prepend (chain->elements, delem);
2379 chain->demuxer = is_demuxer_element (element);
2380 chain->adaptive_demuxer = is_adaptive_demuxer_element (element);
2381
2382 is_decoder = strstr (gst_element_factory_get_metadata (factory,
2383 GST_ELEMENT_METADATA_KLASS), "Decoder") != NULL;
2384
2385 /* For adaptive streaming demuxer we insert a multiqueue after
2386 * this demuxer.
2387 * Now for the case where we have a container stream inside these
2388 * buffers, another demuxer will be plugged and after this second
2389 * demuxer there will be a second multiqueue. This second multiqueue
2390 * will get smaller buffers and will be the one emitting buffering
2391 * messages.
2392 * If we don't have a container stream inside the fragment buffers,
2393 * we'll insert a multiqueue below right after the next element after
2394 * the adaptive streaming demuxer. This is going to be a parser or
2395 * decoder, and will output smaller buffers.
2396 */
2397 if (chain->parent && chain->parent->parent) {
2398 GstDecodeChain *parent_chain = chain->parent->parent;
2399
2400 if (parent_chain->adaptive_demuxer && (is_parser || is_decoder))
2401 chain->demuxer = TRUE;
2402 }
2403
2404 /* If we are configured to use buffering and there is no demuxer in the
2405 * chain, we still want a multiqueue, otherwise we will ignore the
2406 * use-buffering property. In that case, we will insert a multiqueue after
2407 * the parser or decoder - not elsewhere, otherwise we won't have
2408 * timestamps.
2409 */
2410
2411 if (!chain->parent && (is_parser || is_decoder) && dbin->use_buffering) {
2412 chain->demuxer = TRUE;
2413 if (is_decoder) {
2414 GST_WARNING_OBJECT (dbin,
2415 "Buffering messages used for decoded and non-parsed data");
2416 }
2417 }
2418
2419 CHAIN_MUTEX_UNLOCK (chain);
2420
2421 /* Set connection-speed property if needed */
2422 if (chain->demuxer) {
2423 GParamSpec *pspec;
2424
2425 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2426 "connection-speed"))) {
2427 guint64 speed = dbin->connection_speed / 1000;
2428 gboolean wrong_type = FALSE;
2429
2430 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
2431 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
2432
2433 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2434 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
2435 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
2436
2437 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2438 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
2439 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
2440
2441 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2442 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
2443 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
2444
2445 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2446 } else {
2447 GST_WARNING_OBJECT (dbin,
2448 "The connection speed property %" G_GUINT64_FORMAT " of type %s"
2449 " is not usefull not setting it", speed,
2450 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
2451 wrong_type = TRUE;
2452 }
2453
2454 if (!wrong_type) {
2455 GST_DEBUG_OBJECT (dbin, "setting connection-speed=%" G_GUINT64_FORMAT
2456 " to demuxer element", speed);
2457
2458 g_object_set (element, "connection-speed", speed, NULL);
2459 }
2460 }
2461 }
2462
2463 /* try to configure the subtitle encoding property when we can */
2464 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2465 "subtitle-encoding");
2466 if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
2467 SUBTITLE_LOCK (dbin);
2468 GST_DEBUG_OBJECT (dbin,
2469 "setting subtitle-encoding=%s to element", dbin->encoding);
2470 g_object_set (G_OBJECT (element), "subtitle-encoding", dbin->encoding,
2471 NULL);
2472 SUBTITLE_UNLOCK (dbin);
2473 subtitle = TRUE;
2474 } else {
2475 subtitle = FALSE;
2476 }
2477
2478 /* link this element further */
2479 to_connect = connect_element (dbin, delem, chain);
2480
2481 while (to_connect) {
2482 GstPad *opad = to_connect->data;
2483 gboolean expose_pad = FALSE;
2484 GstDecodeChain *new_chain;
2485 GstCaps *ocaps;
2486
2487 ocaps = get_pad_caps (opad);
2488 expose_pad =
2489 analyze_new_pad (dbin, delem->element, opad, ocaps, chain,
2490 &new_chain);
2491
2492 if (ocaps)
2493 gst_caps_unref (ocaps);
2494
2495 if (expose_pad) {
2496 PadExposeData *expose_data = g_new0 (PadExposeData, 1);
2497 expose_data->chain = new_chain;
2498 expose_data->pad = gst_object_ref (opad);
2499 to_expose = g_list_prepend (to_expose, expose_data);
2500 }
2501
2502 gst_object_unref (opad);
2503 to_connect = g_list_delete_link (to_connect, to_connect);
2504 }
2505 /* any pads left in to_expose are to be exposed */
2506
2507 /* Bring the element to the state of the parent */
2508
2509 /* First lock element's sinkpad stream lock so no data reaches
2510 * the possible new element added when caps are sent by element
2511 * while we're still sending sticky events */
2512 GST_PAD_STREAM_LOCK (sinkpad);
2513
2514 if ((gst_element_set_state (element,
2515 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE ||
2516 !send_sticky_events (dbin, pad)) {
2517 GstDecodeElement *dtmp = NULL;
2518 GstElement *tmp = NULL;
2519 GstMessage *error_msg;
2520
2521 GST_PAD_STREAM_UNLOCK (sinkpad);
2522
2523 GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
2524 GST_ELEMENT_NAME (element));
2525
2526 while (to_expose) {
2527 PadExposeData *expose_data = to_expose->data;
2528 gst_object_unref (expose_data->pad);
2529 g_free (expose_data);
2530 to_expose = g_list_delete_link (to_expose, to_expose);
2531 }
2532
2533 remove_error_filter (dbin, element, &error_msg);
2534
2535 if (error_msg) {
2536 gchar *error_string = error_message_to_string (error_msg);
2537 g_string_append_printf (error_details, "Couldn't set %s to PAUSED:\n%s",
2538 GST_ELEMENT_NAME (element), error_string);
2539 gst_message_unref (error_msg);
2540 g_free (error_string);
2541 } else {
2542 g_string_append_printf (error_details, "Couldn't set %s to PAUSED",
2543 GST_ELEMENT_NAME (element));
2544 }
2545
2546 /* Remove all elements in this chain that were just added. No
2547 * other thread could've added elements in the meantime */
2548 CHAIN_MUTEX_LOCK (chain);
2549 do {
2550 GList *l;
2551
2552 dtmp = chain->elements->data;
2553 tmp = dtmp->element;
2554
2555 /* Disconnect any signal handlers that might be connected
2556 * in connect_element() or analyze_pad() */
2557 if (dtmp->pad_added_id)
2558 g_signal_handler_disconnect (tmp, dtmp->pad_added_id);
2559 if (dtmp->pad_removed_id)
2560 g_signal_handler_disconnect (tmp, dtmp->pad_removed_id);
2561 if (dtmp->no_more_pads_id)
2562 g_signal_handler_disconnect (tmp, dtmp->no_more_pads_id);
2563
2564 for (l = chain->pending_pads; l;) {
2565 GstPendingPad *pp = l->data;
2566 GList *n;
2567
2568 if (GST_PAD_PARENT (pp->pad) != tmp) {
2569 l = l->next;
2570 continue;
2571 }
2572
2573 gst_pending_pad_free (pp);
2574
2575 /* Remove element from the list, update list head and go to the
2576 * next element in the list */
2577 n = l->next;
2578 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2579 l = n;
2580 }
2581
2582 if (dtmp->capsfilter) {
2583 gst_bin_remove (GST_BIN (dbin), dtmp->capsfilter);
2584 gst_element_set_state (dtmp->capsfilter, GST_STATE_NULL);
2585 gst_object_unref (dtmp->capsfilter);
2586 }
2587
2588 gst_bin_remove (GST_BIN (dbin), tmp);
2589 gst_element_set_state (tmp, GST_STATE_NULL);
2590
2591 gst_object_unref (tmp);
2592 g_slice_free (GstDecodeElement, dtmp);
2593
2594 chain->elements = g_list_delete_link (chain->elements, chain->elements);
2595 } while (tmp != element);
2596 CHAIN_MUTEX_UNLOCK (chain);
2597
2598 continue;
2599 } else {
2600 /* Everything went well, the spice must flow now */
2601 GST_PAD_STREAM_UNLOCK (sinkpad);
2602 }
2603
2604 /* Remove error filter now, from now on we can't gracefully
2605 * handle errors of the element anymore */
2606 remove_error_filter (dbin, element, NULL);
2607
2608 /* Now let the bin handle the state */
2609 gst_element_set_locked_state (element, FALSE);
2610
2611 if (subtitle) {
2612 SUBTITLE_LOCK (dbin);
2613 /* we added the element now, add it to the list of subtitle-encoding
2614 * elements when we can set the property */
2615 dbin->subtitles = g_list_prepend (dbin->subtitles, element);
2616 SUBTITLE_UNLOCK (dbin);
2617 }
2618
2619 while (to_expose) {
2620 PadExposeData *expose_data = to_expose->data;
2621 GstCaps *ocaps;
2622
2623 ocaps = get_pad_caps (expose_data->pad);
2624 expose_pad (dbin, delem->element, expose_data->chain->current_pad,
2625 expose_data->pad, ocaps, expose_data->chain);
2626
2627 if (ocaps)
2628 gst_caps_unref (ocaps);
2629
2630 gst_object_unref (expose_data->pad);
2631 g_free (expose_data);
2632 to_expose = g_list_delete_link (to_expose, to_expose);
2633 }
2634
2635 res = TRUE;
2636 break;
2637 }
2638
2639 beach:
2640 if (mqpad)
2641 gst_object_unref (mqpad);
2642
2643 if (error_details)
2644 *deadend_details = g_string_free (error_details, (error_details->len == 0
2645 || res));
2646 else
2647 *deadend_details = NULL;
2648
2649 return res;
2650 }
2651
2652 static GstCaps *
get_pad_caps(GstPad * pad)2653 get_pad_caps (GstPad * pad)
2654 {
2655 GstCaps *caps;
2656
2657 /* first check the pad caps, if this is set, we are positively sure it is
2658 * fixed and exactly what the element will produce. */
2659 caps = gst_pad_get_current_caps (pad);
2660
2661 /* then use the getcaps function if we don't have caps. These caps might not
2662 * be fixed in some cases, in which case analyze_new_pad will set up a
2663 * notify::caps signal to continue autoplugging. */
2664 if (caps == NULL)
2665 caps = gst_pad_query_caps (pad, NULL);
2666
2667 return caps;
2668 }
2669
2670 /* Returns a list of pads that can be connected to already and
2671 * connects to pad-added and related signals */
2672 static GList *
connect_element(GstDecodeBin * dbin,GstDecodeElement * delem,GstDecodeChain * chain)2673 connect_element (GstDecodeBin * dbin, GstDecodeElement * delem,
2674 GstDecodeChain * chain)
2675 {
2676 GstElement *element = delem->element;
2677 GList *pads;
2678 gboolean dynamic = FALSE;
2679 GList *to_connect = NULL;
2680
2681 GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [chain:%p] further",
2682 GST_ELEMENT_NAME (element), chain);
2683
2684 /* 1. Loop over pad templates, grabbing existing pads along the way */
2685 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
2686 pads = g_list_next (pads)) {
2687 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
2688 const gchar *templ_name;
2689
2690 /* we are only interested in source pads */
2691 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
2692 continue;
2693
2694 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2695 GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
2696
2697 /* figure out what kind of pad this is */
2698 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2699 case GST_PAD_ALWAYS:
2700 {
2701 /* get the pad that we need to autoplug */
2702 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2703
2704 if (pad) {
2705 GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
2706 templ_name);
2707 /* here is the pad, we need to autoplug it */
2708 to_connect = g_list_prepend (to_connect, pad);
2709 } else {
2710 /* strange, pad is marked as always but it's not
2711 * there. Fix the element */
2712 GST_WARNING_OBJECT (dbin,
2713 "could not get the pad for always template %s", templ_name);
2714 }
2715 break;
2716 }
2717 case GST_PAD_SOMETIMES:
2718 {
2719 /* try to get the pad to see if it is already created or
2720 * not */
2721 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2722
2723 if (pad) {
2724 GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
2725 templ_name);
2726 /* the pad is created, we need to autoplug it */
2727 to_connect = g_list_prepend (to_connect, pad);
2728 } else {
2729 GST_DEBUG_OBJECT (dbin,
2730 "did not get the sometimes pad of template %s", templ_name);
2731 /* we have an element that will create dynamic pads */
2732 dynamic = TRUE;
2733 }
2734 break;
2735 }
2736 case GST_PAD_REQUEST:
2737 /* ignore request pads */
2738 GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
2739 break;
2740 }
2741 }
2742
2743 /* 2. if there are more potential pads, connect to relevant signals */
2744 if (dynamic) {
2745 GST_LOG_OBJECT (dbin, "Adding signals to element %s in chain %p",
2746 GST_ELEMENT_NAME (element), chain);
2747 delem->pad_added_id = g_signal_connect (element, "pad-added",
2748 G_CALLBACK (pad_added_cb), chain);
2749 delem->pad_removed_id = g_signal_connect (element, "pad-removed",
2750 G_CALLBACK (pad_removed_cb), chain);
2751 delem->no_more_pads_id = g_signal_connect (element, "no-more-pads",
2752 G_CALLBACK (no_more_pads_cb), chain);
2753 }
2754
2755 /* 3. return all pads that can be connected to already */
2756
2757 return to_connect;
2758 }
2759
2760 /* expose_pad:
2761 *
2762 * Expose the given pad on the chain as a decoded pad.
2763 */
2764 static void
expose_pad(GstDecodeBin * dbin,GstElement * src,GstDecodePad * dpad,GstPad * pad,GstCaps * caps,GstDecodeChain * chain)2765 expose_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2766 GstPad * pad, GstCaps * caps, GstDecodeChain * chain)
2767 {
2768 GstPad *mqpad = NULL;
2769
2770 GST_DEBUG_OBJECT (dbin, "pad %s:%s, chain:%p",
2771 GST_DEBUG_PAD_NAME (pad), chain);
2772
2773 /* If this is the first pad for this chain, there are no other elements
2774 * and the source element is not the multiqueue we must link through the
2775 * multiqueue.
2776 *
2777 * This is the case if a demuxer directly exposed a raw pad.
2778 */
2779 if (chain->parent && !chain->elements && src != chain->parent->multiqueue) {
2780 GST_LOG_OBJECT (src, "connecting the pad through multiqueue");
2781
2782 decode_pad_set_target (dpad, NULL);
2783 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2784 goto beach;
2785 pad = mqpad;
2786 decode_pad_set_target (dpad, pad);
2787 }
2788
2789 gst_decode_pad_activate (dpad, chain);
2790 chain->endpad = gst_object_ref (dpad);
2791 chain->endcaps = gst_caps_ref (caps);
2792
2793 EXPOSE_LOCK (dbin);
2794 if (dbin->decode_chain) {
2795 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2796 gst_decode_bin_expose (dbin);
2797 }
2798 }
2799 EXPOSE_UNLOCK (dbin);
2800
2801 if (mqpad)
2802 gst_object_unref (mqpad);
2803
2804 beach:
2805 return;
2806 }
2807
2808 /* check_upstream_seekable:
2809 *
2810 * Check if upstream is seekable.
2811 */
2812 static gboolean
check_upstream_seekable(GstDecodeBin * dbin,GstPad * pad)2813 check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad)
2814 {
2815 GstQuery *query;
2816 gint64 start = -1, stop = -1;
2817 gboolean seekable = FALSE;
2818
2819 query = gst_query_new_seeking (GST_FORMAT_BYTES);
2820 if (!gst_pad_peer_query (pad, query)) {
2821 GST_DEBUG_OBJECT (dbin, "seeking query failed");
2822 goto done;
2823 }
2824
2825 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2826
2827 /* try harder to query upstream size if we didn't get it the first time */
2828 if (seekable && stop == -1) {
2829 GST_DEBUG_OBJECT (dbin, "doing duration query to fix up unset stop");
2830 gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &stop);
2831 }
2832
2833 /* if upstream doesn't know the size, it's likely that it's not seekable in
2834 * practice even if it technically may be seekable */
2835 if (seekable && (start != 0 || stop <= start)) {
2836 GST_DEBUG_OBJECT (dbin, "seekable but unknown start/stop -> disable");
2837 seekable = FALSE;
2838 } else {
2839 GST_DEBUG_OBJECT (dbin, "upstream seekable: %d", seekable);
2840 }
2841
2842 done:
2843 gst_query_unref (query);
2844 return seekable;
2845 }
2846
2847 static void
type_found(GstElement * typefind,guint probability,GstCaps * caps,GstDecodeBin * decode_bin)2848 type_found (GstElement * typefind, guint probability,
2849 GstCaps * caps, GstDecodeBin * decode_bin)
2850 {
2851 GstPad *pad, *sink_pad;
2852 GstDecodeChain *chain;
2853
2854 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
2855
2856 /* If the typefinder (but not something else) finds text/plain - i.e. that's
2857 * the top-level type of the file - then error out.
2858 */
2859 if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
2860 GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
2861 (_("This appears to be a text file")),
2862 ("decodebin cannot decode plain text files"));
2863 goto exit;
2864 }
2865
2866 pad = gst_element_get_static_pad (typefind, "src");
2867 sink_pad = gst_element_get_static_pad (typefind, "sink");
2868
2869 /* need some lock here to prevent race with shutdown state change
2870 * which might yank away e.g. decode_chain while building stuff here.
2871 * In typical cases, STREAM_LOCK is held and handles that, it need not
2872 * be held (if called from a proxied setcaps), so grab it anyway */
2873 GST_PAD_STREAM_LOCK (sink_pad);
2874 /* FIXME: we can only deal with one type, we don't yet support dynamically changing
2875 * caps from the typefind element */
2876 if (decode_bin->have_type || decode_bin->decode_chain) {
2877 } else {
2878 decode_bin->have_type = TRUE;
2879
2880 decode_bin->decode_chain = gst_decode_chain_new (decode_bin, NULL, pad);
2881 chain = gst_decode_chain_ref (decode_bin->decode_chain);
2882
2883 if (analyze_new_pad (decode_bin, typefind, pad, caps,
2884 decode_bin->decode_chain, NULL))
2885 expose_pad (decode_bin, typefind, decode_bin->decode_chain->current_pad,
2886 pad, caps, decode_bin->decode_chain);
2887
2888 gst_decode_chain_unref (chain);
2889 }
2890
2891 GST_PAD_STREAM_UNLOCK (sink_pad);
2892 gst_object_unref (sink_pad);
2893 gst_object_unref (pad);
2894
2895 exit:
2896 return;
2897 }
2898
2899 static GstPadProbeReturn
pad_event_cb(GstPad * pad,GstPadProbeInfo * info,gpointer data)2900 pad_event_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2901 {
2902 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2903 GstPendingPad *ppad = (GstPendingPad *) data;
2904 GstDecodeChain *chain = ppad->chain;
2905 GstDecodeBin *dbin = chain->dbin;
2906
2907 g_assert (ppad);
2908 g_assert (chain);
2909 g_assert (dbin);
2910 switch (GST_EVENT_TYPE (event)) {
2911 case GST_EVENT_EOS:
2912 GST_DEBUG_OBJECT (pad, "Received EOS on a non final pad, this stream "
2913 "ended too early");
2914 chain->deadend = TRUE;
2915 chain->drained = TRUE;
2916 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
2917 /* we don't set the endcaps because NULL endcaps means early EOS */
2918
2919 EXPOSE_LOCK (dbin);
2920 if (dbin->decode_chain)
2921 if (gst_decode_chain_is_complete (dbin->decode_chain))
2922 gst_decode_bin_expose (dbin);
2923 EXPOSE_UNLOCK (dbin);
2924 break;
2925 default:
2926 break;
2927 }
2928 return GST_PAD_PROBE_OK;
2929 }
2930
2931 static void
pad_added_cb(GstElement * element,GstPad * pad,GstDecodeChain * chain)2932 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2933 {
2934 GstCaps *caps;
2935 GstDecodeBin *dbin;
2936 GstDecodeChain *new_chain;
2937
2938 dbin = chain->dbin;
2939
2940 GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
2941 GST_PAD_STREAM_LOCK (pad);
2942 if (!gst_pad_is_active (pad)) {
2943 GST_PAD_STREAM_UNLOCK (pad);
2944 GST_DEBUG_OBJECT (pad, "Ignoring pad-added from a deactivated pad");
2945 return;
2946 }
2947
2948 caps = get_pad_caps (pad);
2949 if (analyze_new_pad (dbin, element, pad, caps, chain, &new_chain))
2950 expose_pad (dbin, element, new_chain->current_pad, pad, caps, new_chain);
2951 if (caps)
2952 gst_caps_unref (caps);
2953
2954 GST_PAD_STREAM_UNLOCK (pad);
2955 }
2956
2957 static GstPadProbeReturn
sink_pad_event_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)2958 sink_pad_event_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2959 {
2960 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
2961 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2962 GstPad *peer = gst_pad_get_peer (pad);
2963 GstPadProbeReturn proberet = GST_PAD_PROBE_OK;
2964
2965 GST_DEBUG_OBJECT (pad, "Got upstream event %s", GST_EVENT_TYPE_NAME (event));
2966
2967 if (peer == NULL) {
2968 GST_DEBUG_OBJECT (pad, "We are unlinked !");
2969 if (group->parent && group->parent->next_groups) {
2970 GstDecodeGroup *last_group =
2971 g_list_last (group->parent->next_groups)->data;
2972 GST_DEBUG_OBJECT (pad, "We could send the event to another group (%p)",
2973 last_group);
2974 /* Grab another sinkpad for that last group through which we will forward this event */
2975 if (last_group->reqpads) {
2976 GstPad *sinkpad = (GstPad *) last_group->reqpads->data;
2977 GstPad *otherpeer = gst_pad_get_peer (sinkpad);
2978 if (otherpeer) {
2979 GST_DEBUG_OBJECT (otherpeer, "Attempting to forward event");
2980 if (gst_pad_send_event (otherpeer, gst_event_ref (event))) {
2981 gst_event_unref (event);
2982 proberet = GST_PAD_PROBE_HANDLED;
2983 }
2984 gst_object_unref (otherpeer);
2985 }
2986 } else {
2987 GST_DEBUG_OBJECT (pad, "No request pads, can't forward event");
2988 }
2989 }
2990 } else {
2991 gst_object_unref (peer);
2992 }
2993
2994 return proberet;
2995 }
2996
2997 static GstPadProbeReturn
sink_pad_query_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)2998 sink_pad_query_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2999 {
3000 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
3001 GstPad *peer = gst_pad_get_peer (pad);
3002 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
3003 GstPadProbeReturn proberet = GST_PAD_PROBE_OK;
3004
3005 GST_DEBUG_OBJECT (pad, "Got upstream query %s", GST_QUERY_TYPE_NAME (query));
3006
3007 if (peer == NULL) {
3008 GST_DEBUG_OBJECT (pad, "We are unlinked !");
3009 if (group->parent && group->parent->next_groups) {
3010 GstDecodeGroup *last_group =
3011 g_list_last (group->parent->next_groups)->data;
3012 GST_DEBUG_OBJECT (pad, "We could send the query to another group");
3013 /* Grab another sinkpad for that last group through which we will forward this event */
3014 if (last_group->reqpads) {
3015 GstPad *sinkpad = (GstPad *) last_group->reqpads->data;
3016 GstPad *otherpeer = gst_pad_get_peer (sinkpad);
3017 if (otherpeer) {
3018 GST_DEBUG_OBJECT (otherpeer, "Attempting to forward query");
3019 if (gst_pad_query (otherpeer, query)) {
3020 proberet = GST_PAD_PROBE_HANDLED;
3021 } else
3022 GST_DEBUG ("FAILURE");
3023 gst_object_unref (otherpeer);
3024 } else
3025 GST_DEBUG_OBJECT (sinkpad, "request pad not connected ??");
3026 } else
3027 GST_DEBUG_OBJECT (pad, "No request pads ???");
3028 }
3029 } else
3030 gst_object_unref (peer);
3031
3032 return proberet;
3033 }
3034
3035 static void
pad_removed_cb(GstElement * element,GstPad * pad,GstDecodeChain * chain)3036 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
3037 {
3038 GList *l;
3039
3040 GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
3041
3042 /* In fact, we don't have to do anything here, the active group will be
3043 * removed when the group's multiqueue is drained */
3044 CHAIN_MUTEX_LOCK (chain);
3045 for (l = chain->pending_pads; l; l = l->next) {
3046 GstPendingPad *ppad = l->data;
3047 GstPad *opad = ppad->pad;
3048
3049 if (pad == opad) {
3050 gst_pending_pad_free (ppad);
3051 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
3052 break;
3053 }
3054 }
3055 CHAIN_MUTEX_UNLOCK (chain);
3056 }
3057
3058 static void
no_more_pads_cb(GstElement * element,GstDecodeChain * chain)3059 no_more_pads_cb (GstElement * element, GstDecodeChain * chain)
3060 {
3061 GstDecodeGroup *group = NULL;
3062
3063 GST_LOG_OBJECT (element, "got no more pads");
3064
3065 CHAIN_MUTEX_LOCK (chain);
3066 if (!chain->elements
3067 || ((GstDecodeElement *) chain->elements->data)->element != element) {
3068 GST_LOG_OBJECT (chain->dbin, "no-more-pads from old chain element '%s'",
3069 GST_OBJECT_NAME (element));
3070 CHAIN_MUTEX_UNLOCK (chain);
3071 return;
3072 } else if (!chain->demuxer) {
3073 GST_LOG_OBJECT (chain->dbin, "no-more-pads from a non-demuxer element '%s'",
3074 GST_OBJECT_NAME (element));
3075 CHAIN_MUTEX_UNLOCK (chain);
3076 return;
3077 }
3078
3079 /* when we received no_more_pads, we can complete the pads of the chain */
3080 if (!chain->next_groups && chain->active_group) {
3081 group = chain->active_group;
3082 } else if (chain->next_groups) {
3083 GList *iter;
3084 for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
3085 group = iter->data;
3086 if (!group->no_more_pads)
3087 break;
3088 }
3089 }
3090 if (!group) {
3091 GST_ERROR_OBJECT (chain->dbin, "can't find group for element");
3092 CHAIN_MUTEX_UNLOCK (chain);
3093 return;
3094 }
3095
3096 GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
3097
3098 group->no_more_pads = TRUE;
3099 /* this group has prerolled enough to not need more pads,
3100 * we can probably set its buffering state to playing now */
3101 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
3102 "'playing' buffering mode", group);
3103 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
3104 (group->parent ? group->parent->seekable : TRUE));
3105 CHAIN_MUTEX_UNLOCK (chain);
3106
3107 EXPOSE_LOCK (chain->dbin);
3108 if (chain->dbin->decode_chain) {
3109 if (gst_decode_chain_is_complete (chain->dbin->decode_chain)) {
3110 gst_decode_bin_expose (chain->dbin);
3111 }
3112 }
3113 EXPOSE_UNLOCK (chain->dbin);
3114 }
3115
3116 static void
caps_notify_cb(GstPad * pad,GParamSpec * unused,GstDecodeChain * chain)3117 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstDecodeChain * chain)
3118 {
3119 GstElement *element;
3120 GList *l;
3121
3122 GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3123
3124 /* Disconnect this; if we still need it, we'll reconnect to this in
3125 * analyze_new_pad */
3126 element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
3127
3128 CHAIN_MUTEX_LOCK (chain);
3129 for (l = chain->pending_pads; l; l = l->next) {
3130 GstPendingPad *ppad = l->data;
3131 if (ppad->pad == pad) {
3132 gst_pending_pad_free (ppad);
3133 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
3134 break;
3135 }
3136 }
3137 CHAIN_MUTEX_UNLOCK (chain);
3138
3139 pad_added_cb (element, pad, chain);
3140
3141 gst_object_unref (element);
3142 }
3143
3144 /* Decide whether an element is a demuxer based on the
3145 * klass and number/type of src pad templates it has */
3146 static gboolean
is_demuxer_element(GstElement * srcelement)3147 is_demuxer_element (GstElement * srcelement)
3148 {
3149 GstElementFactory *srcfactory;
3150 GstElementClass *elemclass;
3151 GList *walk;
3152 const gchar *klass;
3153 gint potential_src_pads = 0;
3154
3155 srcfactory = gst_element_get_factory (srcelement);
3156 klass =
3157 gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
3158
3159 /* Can't be a demuxer unless it has Demux in the klass name */
3160 if (!strstr (klass, "Demux"))
3161 return FALSE;
3162
3163 /* Walk the src pad templates and count how many the element
3164 * might produce */
3165 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
3166
3167 walk = gst_element_class_get_pad_template_list (elemclass);
3168 while (walk != NULL) {
3169 GstPadTemplate *templ;
3170
3171 templ = (GstPadTemplate *) walk->data;
3172 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
3173 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
3174 case GST_PAD_ALWAYS:
3175 case GST_PAD_SOMETIMES:
3176 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
3177 potential_src_pads += 2; /* Might make multiple pads */
3178 else
3179 potential_src_pads += 1;
3180 break;
3181 case GST_PAD_REQUEST:
3182 potential_src_pads += 2;
3183 break;
3184 }
3185 }
3186 walk = g_list_next (walk);
3187 }
3188
3189 if (potential_src_pads < 2)
3190 return FALSE;
3191
3192 return TRUE;
3193 }
3194
3195 static gboolean
is_adaptive_demuxer_element(GstElement * srcelement)3196 is_adaptive_demuxer_element (GstElement * srcelement)
3197 {
3198 GstElementFactory *srcfactory;
3199 const gchar *klass;
3200
3201 srcfactory = gst_element_get_factory (srcelement);
3202 klass =
3203 gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
3204
3205 /* Can't be a demuxer unless it has Demux in the klass name */
3206 if (!strstr (klass, "Demux") || !strstr (klass, "Adaptive"))
3207 return FALSE;
3208
3209 return TRUE;
3210 }
3211
3212 /* Returns TRUE if the caps are compatible with the caps specified in the 'caps'
3213 * property (which by default are the raw caps)
3214 *
3215 * The decodebin_lock should be taken !
3216 */
3217 static gboolean
are_final_caps(GstDecodeBin * dbin,GstCaps * caps)3218 are_final_caps (GstDecodeBin * dbin, GstCaps * caps)
3219 {
3220 gboolean res;
3221
3222 GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
3223
3224 /* lock for getting the caps */
3225 GST_OBJECT_LOCK (dbin);
3226 res = gst_caps_is_subset (caps, dbin->caps);
3227 GST_OBJECT_UNLOCK (dbin);
3228
3229 GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
3230
3231 return res;
3232 }
3233
3234 /* gst_decode_bin_reset_buffering:
3235 *
3236 * Enables buffering on the last multiqueue of each group only,
3237 * disabling the rest
3238 *
3239 */
3240 static void
gst_decode_bin_reset_buffering(GstDecodeBin * dbin)3241 gst_decode_bin_reset_buffering (GstDecodeBin * dbin)
3242 {
3243 if (!dbin->use_buffering)
3244 return;
3245
3246 GST_DEBUG_OBJECT (dbin, "Reseting multiqueues buffering");
3247 if (dbin->decode_chain) {
3248 CHAIN_MUTEX_LOCK (dbin->decode_chain);
3249 gst_decode_chain_reset_buffering (dbin->decode_chain);
3250 CHAIN_MUTEX_UNLOCK (dbin->decode_chain);
3251 }
3252 }
3253
3254 /****
3255 * GstDecodeChain functions
3256 ****/
3257
3258 static gboolean
gst_decode_chain_reset_buffering(GstDecodeChain * chain)3259 gst_decode_chain_reset_buffering (GstDecodeChain * chain)
3260 {
3261 GstDecodeGroup *group;
3262
3263 group = chain->active_group;
3264 GST_LOG_OBJECT (chain->dbin, "Resetting chain %p buffering, active group: %p",
3265 chain, group);
3266 if (group) {
3267 return gst_decode_group_reset_buffering (group);
3268 }
3269 return FALSE;
3270 }
3271
3272 /* gst_decode_chain_get_current_group:
3273 *
3274 * Returns the current group of this chain, to which
3275 * new chains should be attached or NULL if the last
3276 * group didn't have no-more-pads.
3277 *
3278 * Not MT-safe: Call with parent chain lock!
3279 */
3280 static GstDecodeGroup *
gst_decode_chain_get_current_group(GstDecodeChain * chain)3281 gst_decode_chain_get_current_group (GstDecodeChain * chain)
3282 {
3283 GstDecodeGroup *group;
3284
3285 if (!chain->next_groups && chain->active_group
3286 && chain->active_group->overrun && !chain->active_group->no_more_pads) {
3287 GST_WARNING_OBJECT (chain->dbin,
3288 "Currently active group %p is exposed"
3289 " and wants to add a new pad without having signaled no-more-pads",
3290 chain->active_group);
3291 return NULL;
3292 }
3293
3294 if (chain->next_groups && (group = chain->next_groups->data) && group->overrun
3295 && !group->no_more_pads) {
3296 GST_WARNING_OBJECT (chain->dbin,
3297 "Currently newest pending group %p "
3298 "had overflow but didn't signal no-more-pads", group);
3299 return NULL;
3300 }
3301
3302 /* Now we know that we can really return something useful */
3303 if (!chain->active_group) {
3304 chain->active_group = group = gst_decode_group_new (chain->dbin, chain);
3305 } else if (!chain->active_group->overrun
3306 && !chain->active_group->no_more_pads) {
3307 group = chain->active_group;
3308 } else {
3309 GList *iter;
3310 group = NULL;
3311 for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
3312 GstDecodeGroup *next_group = iter->data;
3313
3314 if (!next_group->overrun && !next_group->no_more_pads) {
3315 group = next_group;
3316 break;
3317 }
3318 }
3319 }
3320 if (!group) {
3321 group = gst_decode_group_new (chain->dbin, chain);
3322 chain->next_groups = g_list_append (chain->next_groups, group);
3323 }
3324
3325 return group;
3326 }
3327
3328 static void gst_decode_group_free_internal (GstDecodeGroup * group,
3329 gboolean hide);
3330
3331 static void
gst_decode_chain_unref(GstDecodeChain * chain)3332 gst_decode_chain_unref (GstDecodeChain * chain)
3333 {
3334 if (g_atomic_int_dec_and_test (&chain->refs)) {
3335 g_mutex_clear (&chain->lock);
3336 g_slice_free (GstDecodeChain, chain);
3337 }
3338 }
3339
3340 static GstDecodeChain *
gst_decode_chain_ref(GstDecodeChain * chain)3341 gst_decode_chain_ref (GstDecodeChain * chain)
3342 {
3343 g_atomic_int_inc (&chain->refs);
3344 return chain;
3345 }
3346
3347 static void
gst_decode_chain_free_internal(GstDecodeChain * chain,gboolean hide)3348 gst_decode_chain_free_internal (GstDecodeChain * chain, gboolean hide)
3349 {
3350 GList *l, *set_to_null = NULL;
3351
3352 CHAIN_MUTEX_LOCK (chain);
3353
3354 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hiding" : "Freeing"),
3355 chain);
3356
3357 if (chain->active_group) {
3358 gst_decode_group_free_internal (chain->active_group, hide);
3359 if (!hide)
3360 chain->active_group = NULL;
3361 }
3362
3363 for (l = chain->next_groups; l; l = l->next) {
3364 gst_decode_group_free_internal ((GstDecodeGroup *) l->data, hide);
3365 if (!hide)
3366 l->data = NULL;
3367 }
3368 if (!hide) {
3369 g_list_free (chain->next_groups);
3370 chain->next_groups = NULL;
3371 }
3372
3373 if (!hide) {
3374 for (l = chain->old_groups; l; l = l->next) {
3375 GstDecodeGroup *group = l->data;
3376
3377 gst_decode_group_free (group);
3378 }
3379 g_list_free (chain->old_groups);
3380 chain->old_groups = NULL;
3381 }
3382
3383 for (l = chain->pending_pads; l; l = l->next) {
3384 GstPendingPad *ppad = l->data;
3385 gst_pending_pad_free (ppad);
3386 l->data = NULL;
3387 }
3388 g_list_free (chain->pending_pads);
3389 chain->pending_pads = NULL;
3390
3391 for (l = chain->elements; l; l = l->next) {
3392 GstDecodeElement *delem = l->data;
3393 GstElement *element = delem->element;
3394
3395 if (delem->pad_added_id)
3396 g_signal_handler_disconnect (element, delem->pad_added_id);
3397 delem->pad_added_id = 0;
3398 if (delem->pad_removed_id)
3399 g_signal_handler_disconnect (element, delem->pad_removed_id);
3400 delem->pad_removed_id = 0;
3401 if (delem->no_more_pads_id)
3402 g_signal_handler_disconnect (element, delem->no_more_pads_id);
3403 delem->no_more_pads_id = 0;
3404
3405 if (delem->capsfilter) {
3406 if (GST_OBJECT_PARENT (delem->capsfilter) ==
3407 GST_OBJECT_CAST (chain->dbin))
3408 gst_bin_remove (GST_BIN_CAST (chain->dbin), delem->capsfilter);
3409 if (!hide) {
3410 set_to_null =
3411 g_list_append (set_to_null, gst_object_ref (delem->capsfilter));
3412 }
3413 }
3414
3415 if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->dbin))
3416 gst_bin_remove (GST_BIN_CAST (chain->dbin), element);
3417 if (!hide) {
3418 set_to_null = g_list_append (set_to_null, gst_object_ref (element));
3419 }
3420
3421 SUBTITLE_LOCK (chain->dbin);
3422 /* remove possible subtitle element */
3423 chain->dbin->subtitles = g_list_remove (chain->dbin->subtitles, element);
3424 SUBTITLE_UNLOCK (chain->dbin);
3425
3426 if (!hide) {
3427 if (delem->capsfilter) {
3428 gst_object_unref (delem->capsfilter);
3429 delem->capsfilter = NULL;
3430 }
3431
3432 gst_object_unref (element);
3433 l->data = NULL;
3434
3435 g_slice_free (GstDecodeElement, delem);
3436 }
3437 }
3438 if (!hide) {
3439 g_list_free (chain->elements);
3440 chain->elements = NULL;
3441 }
3442
3443 if (chain->endpad) {
3444 if (chain->endpad->exposed) {
3445 gst_element_remove_pad (GST_ELEMENT_CAST (chain->dbin),
3446 GST_PAD_CAST (chain->endpad));
3447 }
3448
3449 decode_pad_set_target (chain->endpad, NULL);
3450 chain->endpad->exposed = FALSE;
3451 if (!hide) {
3452 gst_object_unref (chain->endpad);
3453 chain->endpad = NULL;
3454 }
3455 }
3456
3457 if (!hide && chain->current_pad) {
3458 gst_object_unref (chain->current_pad);
3459 chain->current_pad = NULL;
3460 }
3461
3462 if (chain->pad) {
3463 gst_object_unref (chain->pad);
3464 chain->pad = NULL;
3465 }
3466
3467 if (chain->endcaps) {
3468 gst_caps_unref (chain->endcaps);
3469 chain->endcaps = NULL;
3470 }
3471 g_free (chain->deadend_details);
3472 chain->deadend_details = NULL;
3473
3474 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hidden" : "Freed"),
3475 chain);
3476 CHAIN_MUTEX_UNLOCK (chain);
3477
3478 while (set_to_null) {
3479 GstElement *element = set_to_null->data;
3480 set_to_null = g_list_delete_link (set_to_null, set_to_null);
3481 gst_element_set_state (element, GST_STATE_NULL);
3482 gst_object_unref (element);
3483 }
3484
3485 if (!hide)
3486 gst_decode_chain_unref (chain);
3487 }
3488
3489 /* gst_decode_chain_free:
3490 *
3491 * Completely frees and removes the chain and all
3492 * child groups from decodebin.
3493 *
3494 * MT-safe, don't hold the chain lock or any child chain's lock
3495 * when calling this!
3496 */
3497 static void
gst_decode_chain_free(GstDecodeChain * chain)3498 gst_decode_chain_free (GstDecodeChain * chain)
3499 {
3500 gst_decode_chain_free_internal (chain, FALSE);
3501 }
3502
3503 /* gst_decode_chain_new:
3504 *
3505 * Creates a new decode chain and initializes it.
3506 *
3507 * It's up to the caller to add it to the list of child chains of
3508 * a group!
3509 */
3510 static GstDecodeChain *
gst_decode_chain_new(GstDecodeBin * dbin,GstDecodeGroup * parent,GstPad * pad)3511 gst_decode_chain_new (GstDecodeBin * dbin, GstDecodeGroup * parent,
3512 GstPad * pad)
3513 {
3514 GstDecodeChain *chain = g_slice_new0 (GstDecodeChain);
3515
3516 GST_DEBUG_OBJECT (dbin, "Creating new chain %p with parent group %p", chain,
3517 parent);
3518
3519 chain->dbin = dbin;
3520 chain->parent = parent;
3521 chain->refs = 1;
3522 g_mutex_init (&chain->lock);
3523 chain->pad = gst_object_ref (pad);
3524
3525 return chain;
3526 }
3527
3528 /****
3529 * GstDecodeGroup functions
3530 ****/
3531
3532 /* The overrun callback is used to expose groups that have not yet had their
3533 * no_more_pads called while the (large) multiqueue overflowed. When this
3534 * happens we must assume that the no_more_pads will not arrive anymore and we
3535 * must expose the pads that we have.
3536 */
3537 static void
multi_queue_overrun_cb(GstElement * queue,GstDecodeGroup * group)3538 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
3539 {
3540 GstDecodeBin *dbin;
3541
3542 dbin = group->dbin;
3543
3544 GST_LOG_OBJECT (dbin, "multiqueue '%s' (%p) is full", GST_OBJECT_NAME (queue),
3545 queue);
3546
3547 group->overrun = TRUE;
3548 /* this group has prerolled enough to not need more pads,
3549 * we can probably set its buffering state to playing now */
3550 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
3551 "'playing' buffering mode", group);
3552 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
3553 (group->parent ? group->parent->seekable : TRUE));
3554
3555 /* FIXME: We should make sure that everything gets exposed now
3556 * even if child chains are not complete because the will never
3557 * be complete! Ignore any non-complete chains when exposing
3558 * and never expose them later
3559 */
3560
3561 EXPOSE_LOCK (dbin);
3562 if (dbin->decode_chain) {
3563 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
3564 if (!gst_decode_bin_expose (dbin))
3565 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
3566 }
3567 }
3568 EXPOSE_UNLOCK (dbin);
3569 }
3570
3571 static void
gst_decode_group_free_internal(GstDecodeGroup * group,gboolean hide)3572 gst_decode_group_free_internal (GstDecodeGroup * group, gboolean hide)
3573 {
3574 GList *l;
3575
3576 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hiding" : "Freeing"),
3577 group);
3578
3579 if (!hide) {
3580 for (l = group->demuxer_pad_probe_ids; l != NULL; l = l->next) {
3581 GstDemuxerPad *demuxer_pad = l->data;
3582 GstPad *sinkpad = g_weak_ref_get (&demuxer_pad->weakPad);
3583
3584 if (sinkpad != NULL) {
3585 gst_pad_remove_probe (sinkpad, demuxer_pad->event_probe_id);
3586 gst_pad_remove_probe (sinkpad, demuxer_pad->query_probe_id);
3587 g_weak_ref_clear (&demuxer_pad->weakPad);
3588 gst_object_unref (sinkpad);
3589 }
3590 g_free (demuxer_pad);
3591 }
3592 g_list_free (group->demuxer_pad_probe_ids);
3593 group->demuxer_pad_probe_ids = NULL;
3594 }
3595
3596 for (l = group->children; l; l = l->next) {
3597 GstDecodeChain *chain = (GstDecodeChain *) l->data;
3598
3599 gst_decode_chain_free_internal (chain, hide);
3600 if (!hide)
3601 l->data = NULL;
3602 }
3603 if (!hide) {
3604 g_list_free (group->children);
3605 group->children = NULL;
3606 }
3607
3608 if (!hide) {
3609 for (l = group->reqpads; l; l = l->next) {
3610 GstPad *pad = l->data;
3611
3612 gst_element_release_request_pad (group->multiqueue, pad);
3613 gst_object_unref (pad);
3614 l->data = NULL;
3615 }
3616 g_list_free (group->reqpads);
3617 group->reqpads = NULL;
3618 }
3619
3620 if (group->multiqueue) {
3621 if (group->overrunsig) {
3622 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
3623 group->overrunsig = 0;
3624 }
3625
3626 if (GST_OBJECT_PARENT (group->multiqueue) == GST_OBJECT_CAST (group->dbin))
3627 gst_bin_remove (GST_BIN_CAST (group->dbin), group->multiqueue);
3628 if (!hide) {
3629 gst_element_set_state (group->multiqueue, GST_STATE_NULL);
3630 gst_object_unref (group->multiqueue);
3631 group->multiqueue = NULL;
3632 }
3633 }
3634
3635 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hid" : "Freed"),
3636 group);
3637 if (!hide)
3638 g_slice_free (GstDecodeGroup, group);
3639 }
3640
3641 /* gst_decode_group_free:
3642 *
3643 * Completely frees and removes the decode group and all
3644 * it's children.
3645 *
3646 * Never call this from any streaming thread!
3647 *
3648 * Not MT-safe, call with parent's chain lock!
3649 */
3650 static void
gst_decode_group_free(GstDecodeGroup * group)3651 gst_decode_group_free (GstDecodeGroup * group)
3652 {
3653 gst_decode_group_free_internal (group, FALSE);
3654 }
3655
3656 /* gst_decode_group_hide:
3657 *
3658 * Hide the decode group only, this means that
3659 * all child endpads are removed from decodebin
3660 * and all signals are unconnected.
3661 *
3662 * No element is set to NULL state and completely
3663 * unrefed here.
3664 *
3665 * Can be called from streaming threads.
3666 *
3667 * Not MT-safe, call with parent's chain lock!
3668 */
3669 static void
gst_decode_group_hide(GstDecodeGroup * group)3670 gst_decode_group_hide (GstDecodeGroup * group)
3671 {
3672 gst_decode_group_free_internal (group, TRUE);
3673 }
3674
3675 /* gst_decode_chain_free_hidden_groups:
3676 *
3677 * Frees any decode groups that were hidden previously.
3678 * This allows keeping memory use from ballooning when
3679 * switching chains repeatedly.
3680 *
3681 * A new throwaway thread will be created to free the
3682 * groups, so any delay does not block the setup of a
3683 * new group.
3684 *
3685 * Not MT-safe, call with parent's chain lock!
3686 */
3687 static void
gst_decode_chain_free_hidden_groups(GList * old_groups)3688 gst_decode_chain_free_hidden_groups (GList * old_groups)
3689 {
3690 GList *l;
3691
3692 for (l = old_groups; l; l = l->next) {
3693 GstDecodeGroup *group = l->data;
3694
3695 gst_decode_group_free (group);
3696 }
3697 g_list_free (old_groups);
3698 }
3699
3700 static void
gst_decode_chain_start_free_hidden_groups_thread(GstDecodeChain * chain)3701 gst_decode_chain_start_free_hidden_groups_thread (GstDecodeChain * chain)
3702 {
3703 GThread *thread;
3704 GError *error = NULL;
3705 GList *old_groups;
3706 GstDecodeBin *dbin = chain->dbin;
3707
3708 old_groups = chain->old_groups;
3709 if (!old_groups)
3710 return;
3711
3712 /* If we already have a thread running, wait for it to finish */
3713 g_mutex_lock (&dbin->cleanup_lock);
3714 if (dbin->cleanup_thread) {
3715 g_thread_join (dbin->cleanup_thread);
3716 dbin->cleanup_thread = NULL;
3717 }
3718
3719 chain->old_groups = NULL;
3720
3721 if (dbin->shutdown) {
3722 /* If we're shutting down, add the groups to be cleaned up in the
3723 * state change handler (which *is* another thread). Also avoids
3724 * playing racy games with the state change handler */
3725 dbin->cleanup_groups = g_list_concat (dbin->cleanup_groups, old_groups);
3726 g_mutex_unlock (&dbin->cleanup_lock);
3727 return;
3728 }
3729
3730 thread = g_thread_try_new ("free-hidden-groups",
3731 (GThreadFunc) gst_decode_chain_free_hidden_groups, old_groups, &error);
3732 if (!thread || error) {
3733 GST_ERROR ("Failed to start free-hidden-groups thread: %s",
3734 error ? error->message : "unknown reason");
3735 g_clear_error (&error);
3736 chain->old_groups = old_groups;
3737 g_mutex_unlock (&dbin->cleanup_lock);
3738 return;
3739 }
3740
3741 dbin->cleanup_thread = thread;
3742 g_mutex_unlock (&dbin->cleanup_lock);
3743
3744 GST_DEBUG_OBJECT (chain->dbin, "Started free-hidden-groups thread");
3745 }
3746
3747 static void
decodebin_set_queue_size(GstDecodeBin * dbin,GstElement * multiqueue,gboolean preroll,gboolean seekable)3748 decodebin_set_queue_size (GstDecodeBin * dbin, GstElement * multiqueue,
3749 gboolean preroll, gboolean seekable)
3750 {
3751 gboolean use_buffering;
3752
3753 /* get the current config from the multiqueue */
3754 g_object_get (multiqueue, "use-buffering", &use_buffering, NULL);
3755
3756 decodebin_set_queue_size_full (dbin, multiqueue, use_buffering, preroll,
3757 seekable);
3758 }
3759
3760 /* configure queue sizes, this depends on the buffering method and if we are
3761 * playing or prerolling. */
3762 static void
decodebin_set_queue_size_full(GstDecodeBin * dbin,GstElement * multiqueue,gboolean use_buffering,gboolean preroll,gboolean seekable)3763 decodebin_set_queue_size_full (GstDecodeBin * dbin, GstElement * multiqueue,
3764 gboolean use_buffering, gboolean preroll, gboolean seekable)
3765 {
3766 guint max_bytes, max_buffers;
3767 guint64 max_time;
3768
3769 GST_DEBUG_OBJECT (multiqueue, "use buffering %d", use_buffering);
3770
3771 if (preroll || use_buffering) {
3772 /* takes queue limits, initially we only queue up up to the max bytes limit,
3773 * with a default of 2MB. we use the same values for buffering mode. */
3774 if (preroll || (max_bytes = dbin->max_size_bytes) == 0)
3775 max_bytes = AUTO_PREROLL_SIZE_BYTES;
3776 if (preroll || (max_buffers = dbin->max_size_buffers) == 0)
3777 max_buffers = AUTO_PREROLL_SIZE_BUFFERS;
3778 if (preroll || (max_time = dbin->max_size_time) == 0) {
3779 if (dbin->use_buffering && !preroll)
3780 max_time = 5 * GST_SECOND;
3781 else
3782 max_time = seekable ? AUTO_PREROLL_SEEKABLE_SIZE_TIME :
3783 AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME;
3784 }
3785 } else {
3786 /* update runtime limits. At runtime, we try to keep the amount of buffers
3787 * in the queues as low as possible (but at least 5 buffers). */
3788 if (dbin->use_buffering)
3789 max_bytes = 0;
3790 else if ((max_bytes = dbin->max_size_bytes) == 0)
3791 max_bytes = AUTO_PLAY_SIZE_BYTES;
3792 if ((max_buffers = dbin->max_size_buffers) == 0)
3793 max_buffers = AUTO_PLAY_SIZE_BUFFERS;
3794 /* this is a multiqueue with disabled buffering, don't limit max_time */
3795 if (dbin->use_buffering)
3796 max_time = 0;
3797 else if ((max_time = dbin->max_size_time) == 0)
3798 max_time = AUTO_PLAY_SIZE_TIME;
3799 }
3800
3801 GST_DEBUG_OBJECT (multiqueue, "setting limits %u bytes, %u buffers, "
3802 "%" G_GUINT64_FORMAT " time", max_bytes, max_buffers, max_time);
3803 g_object_set (multiqueue,
3804 "max-size-bytes", max_bytes, "max-size-time", max_time,
3805 "max-size-buffers", max_buffers, NULL);
3806 }
3807
3808 /* gst_decode_group_new:
3809 * @dbin: Parent decodebin
3810 * @parent: Parent chain or %NULL
3811 *
3812 * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
3813 * of groups.
3814 */
3815 static GstDecodeGroup *
gst_decode_group_new(GstDecodeBin * dbin,GstDecodeChain * parent)3816 gst_decode_group_new (GstDecodeBin * dbin, GstDecodeChain * parent)
3817 {
3818 GstDecodeGroup *group = g_slice_new0 (GstDecodeGroup);
3819 GstElement *mq;
3820 gboolean seekable;
3821
3822 GST_DEBUG_OBJECT (dbin, "Creating new group %p with parent chain %p", group,
3823 parent);
3824
3825 group->dbin = dbin;
3826 group->parent = parent;
3827
3828 mq = group->multiqueue = gst_element_factory_make ("multiqueue", NULL);
3829 if (G_UNLIKELY (!group->multiqueue))
3830 goto missing_multiqueue;
3831
3832 /* configure queue sizes for preroll */
3833 seekable = FALSE;
3834 if (parent && parent->demuxer) {
3835 GstElement *element =
3836 ((GstDecodeElement *) parent->elements->data)->element;
3837 GstPad *pad = gst_element_get_static_pad (element, "sink");
3838 if (pad) {
3839 seekable = parent->seekable = check_upstream_seekable (dbin, pad);
3840 gst_object_unref (pad);
3841 }
3842 }
3843 decodebin_set_queue_size_full (dbin, mq, FALSE, TRUE, seekable);
3844
3845 group->overrunsig = g_signal_connect (mq, "overrun",
3846 G_CALLBACK (multi_queue_overrun_cb), group);
3847 group->demuxer_pad_probe_ids = NULL;
3848
3849 gst_element_set_state (mq, GST_STATE_PAUSED);
3850 gst_bin_add (GST_BIN (dbin), gst_object_ref (mq));
3851
3852 return group;
3853
3854 /* ERRORS */
3855 missing_multiqueue:
3856 {
3857 gst_element_post_message (GST_ELEMENT_CAST (dbin),
3858 gst_missing_element_message_new (GST_ELEMENT_CAST (dbin),
3859 "multiqueue"));
3860 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no multiqueue!"));
3861 g_slice_free (GstDecodeGroup, group);
3862 return NULL;
3863 }
3864 }
3865
3866 /* gst_decode_group_control_demuxer_pad
3867 *
3868 * Adds a new demuxer srcpad to the given group.
3869 *
3870 * Returns the srcpad of the multiqueue corresponding the given pad.
3871 * Returns NULL if there was an error.
3872 */
3873 static GstPad *
gst_decode_group_control_demuxer_pad(GstDecodeGroup * group,GstPad * pad)3874 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
3875 {
3876 GstDecodeBin *dbin;
3877 GstDemuxerPad *demuxer_pad;
3878 GstPad *srcpad, *sinkpad;
3879 GstIterator *it = NULL;
3880 GValue item = { 0, };
3881
3882 dbin = group->dbin;
3883
3884 GST_LOG_OBJECT (dbin, "group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
3885
3886 srcpad = NULL;
3887
3888 if (G_UNLIKELY (!group->multiqueue))
3889 return NULL;
3890
3891 if (!(sinkpad = gst_element_get_request_pad (group->multiqueue, "sink_%u"))) {
3892 GST_ERROR_OBJECT (dbin, "Couldn't get sinkpad from multiqueue");
3893 return NULL;
3894 }
3895
3896 if ((gst_pad_link_full (pad, sinkpad,
3897 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)) {
3898 GST_ERROR_OBJECT (dbin, "Couldn't link demuxer and multiqueue");
3899 goto error;
3900 }
3901
3902 it = gst_pad_iterate_internal_links (sinkpad);
3903
3904 if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
3905 || ((srcpad = g_value_dup_object (&item)) == NULL)) {
3906 GST_ERROR_OBJECT (dbin,
3907 "Couldn't get srcpad from multiqueue for sinkpad %" GST_PTR_FORMAT,
3908 sinkpad);
3909 goto error;
3910 }
3911
3912 CHAIN_MUTEX_LOCK (group->parent);
3913
3914 /* Note: GWeakRefs can't be moved in memory once they're in use, so do a
3915 * dedicated alloc for the GstDemuxerPad struct that contains it */
3916 demuxer_pad = g_new0 (GstDemuxerPad, 1);
3917 demuxer_pad->event_probe_id = gst_pad_add_probe (sinkpad,
3918 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM, sink_pad_event_probe, group, NULL);
3919 demuxer_pad->query_probe_id = gst_pad_add_probe (sinkpad,
3920 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM, sink_pad_query_probe, group, NULL);
3921
3922 g_weak_ref_set (&demuxer_pad->weakPad, sinkpad);
3923 group->demuxer_pad_probe_ids =
3924 g_list_prepend (group->demuxer_pad_probe_ids, demuxer_pad);
3925
3926 group->reqpads = g_list_prepend (group->reqpads, gst_object_ref (sinkpad));
3927 CHAIN_MUTEX_UNLOCK (group->parent);
3928
3929 beach:
3930 if (G_IS_VALUE (&item))
3931 g_value_unset (&item);
3932 if (it)
3933 gst_iterator_free (it);
3934 gst_object_unref (sinkpad);
3935 return srcpad;
3936
3937 error:
3938 gst_element_release_request_pad (group->multiqueue, sinkpad);
3939 goto beach;
3940 }
3941
3942 /* gst_decode_group_is_complete:
3943 *
3944 * Checks if the group is complete, this means that
3945 * a) overrun of the multiqueue or no-more-pads happened
3946 * b) all child chains are complete
3947 *
3948 * Not MT-safe, always call with decodebin expose lock
3949 */
3950 static gboolean
gst_decode_group_is_complete(GstDecodeGroup * group)3951 gst_decode_group_is_complete (GstDecodeGroup * group)
3952 {
3953 GList *l;
3954 gboolean complete = TRUE;
3955
3956 if (!group->overrun && !group->no_more_pads) {
3957 complete = FALSE;
3958 goto out;
3959 }
3960
3961 for (l = group->children; l; l = l->next) {
3962 GstDecodeChain *chain = l->data;
3963
3964 if (!gst_decode_chain_is_complete (chain)) {
3965 complete = FALSE;
3966 goto out;
3967 }
3968 }
3969
3970 out:
3971 GST_DEBUG_OBJECT (group->dbin, "Group %p is complete: %d", group, complete);
3972 return complete;
3973 }
3974
3975 /* gst_decode_chain_is_complete:
3976 *
3977 * Returns TRUE if the chain is complete, this means either
3978 * a) This chain is a dead end, i.e. we have no suitable plugins
3979 * b) This chain ends in an endpad and this is blocked or exposed
3980 *
3981 * Not MT-safe, always call with decodebin expose lock
3982 */
3983 static gboolean
gst_decode_chain_is_complete(GstDecodeChain * chain)3984 gst_decode_chain_is_complete (GstDecodeChain * chain)
3985 {
3986 gboolean complete = FALSE;
3987
3988 CHAIN_MUTEX_LOCK (chain);
3989 if (chain->dbin->shutdown)
3990 goto out;
3991
3992 if (chain->deadend) {
3993 complete = TRUE;
3994 goto out;
3995 }
3996
3997 if (chain->endpad && gst_decode_pad_is_exposable (chain->endpad)) {
3998 complete = TRUE;
3999 goto out;
4000 }
4001
4002 if (chain->demuxer) {
4003 if (chain->active_group
4004 && gst_decode_group_is_complete (chain->active_group)) {
4005 complete = TRUE;
4006 goto out;
4007 }
4008 }
4009
4010 out:
4011 CHAIN_MUTEX_UNLOCK (chain);
4012 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is complete: %d", chain, complete);
4013 return complete;
4014 }
4015
4016 /* Flushing group/chains */
4017 static void
flush_group(GstDecodeGroup * group,gboolean flushing)4018 flush_group (GstDecodeGroup * group, gboolean flushing)
4019 {
4020 GList *tmp;
4021
4022 GST_DEBUG ("group %p flushing:%d", group, flushing);
4023
4024 if (group->drained == flushing)
4025 return;
4026 for (tmp = group->children; tmp; tmp = tmp->next) {
4027 GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
4028 flush_chain (chain, flushing);
4029 }
4030 GST_DEBUG ("Setting group %p to drained:%d", group, flushing);
4031 group->drained = flushing;
4032 }
4033
4034 static void
flush_chain(GstDecodeChain * chain,gboolean flushing)4035 flush_chain (GstDecodeChain * chain, gboolean flushing)
4036 {
4037 GList *tmp;
4038 GstDecodeBin *dbin = chain->dbin;
4039
4040 GST_DEBUG_OBJECT (dbin, "chain %p (pad %s:%s) flushing:%d", chain,
4041 GST_DEBUG_PAD_NAME (chain->pad), flushing);
4042 if (chain->drained == flushing)
4043 return;
4044 /* if unflushing, check if we should switch to last group */
4045 if (flushing == FALSE && chain->next_groups) {
4046 GstDecodeGroup *target_group =
4047 (GstDecodeGroup *) g_list_last (chain->next_groups)->data;
4048 gst_decode_chain_start_free_hidden_groups_thread (chain);
4049 /* Hide active group (we're sure it's not that one we'll be using) */
4050 GST_DEBUG_OBJECT (dbin, "Switching from active group %p to group %p",
4051 chain->active_group, target_group);
4052 gst_decode_group_hide (chain->active_group);
4053 chain->old_groups = g_list_prepend (chain->old_groups, chain->active_group);
4054 chain->active_group = target_group;
4055 /* Hide all groups but the target_group */
4056 for (tmp = chain->next_groups; tmp; tmp = tmp->next) {
4057 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
4058 if (group != target_group) {
4059 gst_decode_group_hide (group);
4060 chain->old_groups = g_list_prepend (chain->old_groups, group);
4061 }
4062 }
4063 /* Clear next groups */
4064 g_list_free (chain->next_groups);
4065 chain->next_groups = NULL;
4066 }
4067 /* Mark all groups as flushing */
4068 if (chain->active_group)
4069 flush_group (chain->active_group, flushing);
4070 for (tmp = chain->next_groups; tmp; tmp = tmp->next) {
4071 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
4072 flush_group (group, flushing);
4073 }
4074 GST_DEBUG ("Setting chain %p to drained:%d", chain, flushing);
4075 chain->drained = flushing;
4076 }
4077
4078 static gboolean
4079 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
4080 gboolean * last_group, gboolean * drained, gboolean * switched);
4081 /* drain_and_switch_chains/groups:
4082 *
4083 * CALL WITH CHAIN LOCK (or group parent) TAKEN !
4084 *
4085 * Goes down the chains/groups until it finds the chain
4086 * to which the drainpad belongs.
4087 *
4088 * It marks that pad/chain as drained and then will figure
4089 * out which group to switch to or not.
4090 *
4091 * last_chain will be set to TRUE if the group to which the
4092 * pad belongs is the last one.
4093 *
4094 * drained will be set to TRUE if the chain/group is drained.
4095 *
4096 * Returns: TRUE if the chain contained the target pad */
4097 static gboolean
drain_and_switch_group(GstDecodeGroup * group,GstDecodePad * drainpad,gboolean * last_group,gboolean * drained,gboolean * switched)4098 drain_and_switch_group (GstDecodeGroup * group, GstDecodePad * drainpad,
4099 gboolean * last_group, gboolean * drained, gboolean * switched)
4100 {
4101 gboolean handled = FALSE;
4102 GList *tmp;
4103
4104 GST_DEBUG ("Checking group %p (target pad %s:%s)",
4105 group, GST_DEBUG_PAD_NAME (drainpad));
4106
4107 /* Definitely can't be in drained groups */
4108 if (G_UNLIKELY (group->drained)) {
4109 goto beach;
4110 }
4111
4112 /* Figure out if all our chains are drained with the
4113 * new information */
4114 group->drained = TRUE;
4115 for (tmp = group->children; tmp; tmp = tmp->next) {
4116 GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
4117 gboolean subdrained = FALSE;
4118
4119 handled |=
4120 drain_and_switch_chains (chain, drainpad, last_group, &subdrained,
4121 switched);
4122 if (!subdrained)
4123 group->drained = FALSE;
4124 }
4125
4126 beach:
4127 GST_DEBUG ("group %p (last_group:%d, drained:%d, switched:%d, handled:%d)",
4128 group, *last_group, group->drained, *switched, handled);
4129 *drained = group->drained;
4130 return handled;
4131 }
4132
4133 static gboolean
drain_and_switch_chains(GstDecodeChain * chain,GstDecodePad * drainpad,gboolean * last_group,gboolean * drained,gboolean * switched)4134 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
4135 gboolean * last_group, gboolean * drained, gboolean * switched)
4136 {
4137 gboolean handled = FALSE;
4138 GstDecodeBin *dbin = chain->dbin;
4139
4140 GST_DEBUG ("Checking chain %p %s:%s (target pad %s:%s)",
4141 chain, GST_DEBUG_PAD_NAME (chain->pad), GST_DEBUG_PAD_NAME (drainpad));
4142
4143 CHAIN_MUTEX_LOCK (chain);
4144
4145 if (chain->pad_probe_id) {
4146 gst_pad_remove_probe (chain->pad, chain->pad_probe_id);
4147 chain->pad_probe_id = 0;
4148 }
4149
4150 /* Definitely can't be in drained chains */
4151 if (G_UNLIKELY (chain->drained)) {
4152 goto beach;
4153 }
4154
4155 if (chain->endpad) {
4156 /* Check if we're reached the target endchain */
4157 if (drainpad != NULL && chain == drainpad->chain) {
4158 GST_DEBUG ("Found the target chain");
4159 drainpad->drained = TRUE;
4160 handled = TRUE;
4161 }
4162
4163 chain->drained = chain->endpad->drained;
4164 goto beach;
4165 }
4166
4167 /* We known there are groups to switch to */
4168 if (chain->next_groups)
4169 *last_group = FALSE;
4170
4171 /* Check the active group */
4172 if (chain->active_group) {
4173 gboolean subdrained = FALSE;
4174 handled = drain_and_switch_group (chain->active_group, drainpad,
4175 last_group, &subdrained, switched);
4176
4177 /* The group is drained, see if we can switch to another */
4178 if ((handled || drainpad == NULL) && subdrained && !*switched) {
4179 if (chain->next_groups) {
4180 /* Switch to next group */
4181 GST_DEBUG_OBJECT (dbin, "Hiding current group %p", chain->active_group);
4182 gst_decode_group_hide (chain->active_group);
4183 chain->old_groups =
4184 g_list_prepend (chain->old_groups, chain->active_group);
4185 GST_DEBUG_OBJECT (dbin, "Switching to next group %p",
4186 chain->next_groups->data);
4187 chain->active_group = chain->next_groups->data;
4188 chain->next_groups =
4189 g_list_delete_link (chain->next_groups, chain->next_groups);
4190 gst_decode_chain_start_free_hidden_groups_thread (chain);
4191 *switched = TRUE;
4192 chain->drained = FALSE;
4193 } else {
4194 GST_DEBUG ("Group %p was the last in chain %p", chain->active_group,
4195 chain);
4196 chain->drained = TRUE;
4197 /* We're drained ! */
4198 }
4199 } else {
4200 if (subdrained && !chain->next_groups)
4201 *drained = TRUE;
4202 }
4203 }
4204
4205 beach:
4206 CHAIN_MUTEX_UNLOCK (chain);
4207
4208 GST_DEBUG ("Chain %p (handled:%d, last_group:%d, drained:%d, switched:%d)",
4209 chain, handled, *last_group, chain->drained, *switched);
4210
4211 *drained = chain->drained;
4212
4213 if (*drained)
4214 g_signal_emit (dbin, gst_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
4215
4216 return handled;
4217 }
4218
4219 /* check if the group is drained, meaning all pads have seen an EOS
4220 * event. */
4221 static gboolean
gst_decode_pad_handle_eos(GstDecodePad * pad)4222 gst_decode_pad_handle_eos (GstDecodePad * pad)
4223 {
4224 gboolean last_group = TRUE;
4225 gboolean switched = FALSE;
4226 gboolean drained = FALSE;
4227 GstDecodeChain *chain = pad->chain;
4228 GstDecodeBin *dbin = chain->dbin;
4229 GstEvent *tmp;
4230
4231 GST_LOG_OBJECT (dbin, "pad %p", pad);
4232
4233 /* Send a stream-group-done event in case downstream needs
4234 * to unblock before we can drain */
4235 tmp = gst_pad_get_sticky_event (GST_PAD (pad), GST_EVENT_STREAM_START, 0);
4236 if (tmp) {
4237 guint group_id;
4238 if (gst_event_parse_group_id (tmp, &group_id)) {
4239 GstPad *peer = gst_pad_get_peer (GST_PAD (pad));
4240
4241 if (peer) {
4242 GST_DEBUG_OBJECT (dbin,
4243 "Sending stream-group-done for group %u to pad %"
4244 GST_PTR_FORMAT, group_id, pad);
4245 gst_pad_send_event (peer, gst_event_new_stream_group_done (group_id));
4246 gst_object_unref (peer);
4247 }
4248 } else {
4249 GST_DEBUG_OBJECT (dbin,
4250 "No group ID to send stream-group-done on pad %" GST_PTR_FORMAT, pad);
4251 }
4252 gst_event_unref (tmp);
4253 }
4254
4255 EXPOSE_LOCK (dbin);
4256 if (dbin->decode_chain) {
4257 drain_and_switch_chains (dbin->decode_chain, pad, &last_group, &drained,
4258 &switched);
4259
4260 if (switched) {
4261 /* If we resulted in a group switch, expose what's needed */
4262 if (gst_decode_chain_is_complete (dbin->decode_chain))
4263 gst_decode_bin_expose (dbin);
4264 }
4265 }
4266 EXPOSE_UNLOCK (dbin);
4267
4268 return last_group;
4269 }
4270
4271 /* gst_decode_group_is_drained:
4272 *
4273 * Check is this group is drained and cache this result.
4274 * The group is drained if all child chains are drained.
4275 *
4276 * Not MT-safe, call with group->parent's lock */
4277 static gboolean
gst_decode_group_is_drained(GstDecodeGroup * group)4278 gst_decode_group_is_drained (GstDecodeGroup * group)
4279 {
4280 GList *l;
4281 gboolean drained = TRUE;
4282
4283 if (group->drained) {
4284 drained = TRUE;
4285 goto out;
4286 }
4287
4288 for (l = group->children; l; l = l->next) {
4289 GstDecodeChain *chain = l->data;
4290
4291 CHAIN_MUTEX_LOCK (chain);
4292 if (!gst_decode_chain_is_drained (chain))
4293 drained = FALSE;
4294 CHAIN_MUTEX_UNLOCK (chain);
4295 if (!drained)
4296 goto out;
4297 }
4298 group->drained = drained;
4299
4300 out:
4301 GST_DEBUG_OBJECT (group->dbin, "Group %p is drained: %d", group, drained);
4302 return drained;
4303 }
4304
4305 /* gst_decode_chain_is_drained:
4306 *
4307 * Check is the chain is drained, which means that
4308 * either
4309 *
4310 * a) it's endpad is drained
4311 * b) there are no pending pads, the active group is drained
4312 * and there are no next groups
4313 *
4314 * Not MT-safe, call with chain lock
4315 */
4316 static gboolean
gst_decode_chain_is_drained(GstDecodeChain * chain)4317 gst_decode_chain_is_drained (GstDecodeChain * chain)
4318 {
4319 gboolean drained = FALSE;
4320
4321 if (chain->endpad) {
4322 drained = chain->endpad->drained;
4323 goto out;
4324 }
4325
4326 if (chain->pending_pads) {
4327 drained = FALSE;
4328 goto out;
4329 }
4330
4331 if (chain->active_group && gst_decode_group_is_drained (chain->active_group)
4332 && !chain->next_groups) {
4333 drained = TRUE;
4334 goto out;
4335 }
4336
4337 out:
4338 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is drained: %d", chain, drained);
4339 return drained;
4340 }
4341
4342 static gboolean
gst_decode_group_reset_buffering(GstDecodeGroup * group)4343 gst_decode_group_reset_buffering (GstDecodeGroup * group)
4344 {
4345 GList *l;
4346 gboolean ret = TRUE;
4347
4348 GST_DEBUG_OBJECT (group->dbin, "Group reset buffering %p %s", group,
4349 GST_ELEMENT_NAME (group->multiqueue));
4350 for (l = group->children; l; l = l->next) {
4351 GstDecodeChain *chain = l->data;
4352
4353 CHAIN_MUTEX_LOCK (chain);
4354 if (!gst_decode_chain_reset_buffering (chain)) {
4355 ret = FALSE;
4356 }
4357 CHAIN_MUTEX_UNLOCK (chain);
4358 }
4359
4360 decodebin_set_queue_size_full (group->dbin, group->multiqueue, !ret,
4361 FALSE, (group->parent ? group->parent->seekable : TRUE));
4362
4363 if (ret) {
4364 /* all chains are buffering already, no need to do it here */
4365 g_object_set (group->multiqueue, "use-buffering", FALSE, NULL);
4366 } else {
4367 g_object_set (group->multiqueue, "use-buffering", TRUE,
4368 "low-percent", group->dbin->low_percent,
4369 "high-percent", group->dbin->high_percent, NULL);
4370 }
4371
4372 GST_DEBUG_OBJECT (group->dbin, "Setting %s buffering to %d",
4373 GST_ELEMENT_NAME (group->multiqueue), !ret);
4374 return TRUE;
4375 }
4376
4377
4378 /* sort_end_pads:
4379 * GCompareFunc to use with lists of GstPad.
4380 * Sorts pads by mime type.
4381 * First video (raw, then non-raw), then audio (raw, then non-raw),
4382 * then others.
4383 *
4384 * Return: negative if a<b, 0 if a==b, positive if a>b
4385 */
4386 static gint
sort_end_pads(GstDecodePad * da,GstDecodePad * db)4387 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
4388 {
4389 gint va, vb;
4390 GstCaps *capsa, *capsb;
4391 GstStructure *sa, *sb;
4392 const gchar *namea, *nameb;
4393 gchar *ida, *idb;
4394 gint ret;
4395
4396 capsa = get_pad_caps (GST_PAD_CAST (da));
4397 capsb = get_pad_caps (GST_PAD_CAST (db));
4398
4399 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
4400 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
4401
4402 namea = gst_structure_get_name (sa);
4403 nameb = gst_structure_get_name (sb);
4404
4405 if (g_strrstr (namea, "video/x-raw"))
4406 va = 0;
4407 else if (g_strrstr (namea, "video/"))
4408 va = 1;
4409 else if (g_strrstr (namea, "audio/x-raw"))
4410 va = 2;
4411 else if (g_strrstr (namea, "audio/"))
4412 va = 3;
4413 else
4414 va = 4;
4415
4416 if (g_strrstr (nameb, "video/x-raw"))
4417 vb = 0;
4418 else if (g_strrstr (nameb, "video/"))
4419 vb = 1;
4420 else if (g_strrstr (nameb, "audio/x-raw"))
4421 vb = 2;
4422 else if (g_strrstr (nameb, "audio/"))
4423 vb = 3;
4424 else
4425 vb = 4;
4426
4427 gst_caps_unref (capsa);
4428 gst_caps_unref (capsb);
4429
4430 if (va != vb)
4431 return va - vb;
4432
4433 /* if otherwise the same, sort by stream-id */
4434 ida = gst_pad_get_stream_id (GST_PAD_CAST (da));
4435 idb = gst_pad_get_stream_id (GST_PAD_CAST (db));
4436 ret = (ida) ? ((idb) ? strcmp (ida, idb) : -1) : 1;
4437 g_free (ida);
4438 g_free (idb);
4439
4440 return ret;
4441 }
4442
4443 static GstCaps *
_gst_element_get_linked_caps(GstElement * src,GstElement * sink,GstElement * capsfilter,GstPad ** srcpad)4444 _gst_element_get_linked_caps (GstElement * src, GstElement * sink,
4445 GstElement * capsfilter, GstPad ** srcpad)
4446 {
4447 GstIterator *it;
4448 GstElement *parent;
4449 GstPad *pad, *peer;
4450 gboolean done = FALSE;
4451 GstCaps *caps = NULL;
4452 GValue item = { 0, };
4453
4454 it = gst_element_iterate_src_pads (src);
4455 while (!done) {
4456 switch (gst_iterator_next (it, &item)) {
4457 case GST_ITERATOR_OK:
4458 pad = g_value_get_object (&item);
4459 peer = gst_pad_get_peer (pad);
4460 if (peer) {
4461 parent = gst_pad_get_parent_element (peer);
4462 if (parent == sink || (capsfilter != NULL && parent == capsfilter)) {
4463 caps = gst_pad_get_current_caps (pad);
4464 *srcpad = gst_object_ref (pad);
4465 done = TRUE;
4466 }
4467
4468 if (parent)
4469 gst_object_unref (parent);
4470 gst_object_unref (peer);
4471 }
4472 g_value_reset (&item);
4473 break;
4474 case GST_ITERATOR_RESYNC:
4475 gst_iterator_resync (it);
4476 break;
4477 case GST_ITERATOR_ERROR:
4478 case GST_ITERATOR_DONE:
4479 done = TRUE;
4480 break;
4481 }
4482 }
4483 g_value_unset (&item);
4484 gst_iterator_free (it);
4485
4486 return caps;
4487 }
4488
4489 static GQuark topology_structure_name = 0;
4490 static GQuark topology_caps = 0;
4491 static GQuark topology_next = 0;
4492 static GQuark topology_pad = 0;
4493 static GQuark topology_element_srcpad = 0;
4494
4495 /* FIXME: Invent gst_structure_take_structure() to prevent all the
4496 * structure copying for nothing
4497 */
4498 static GstStructure *
gst_decode_chain_get_topology(GstDecodeChain * chain)4499 gst_decode_chain_get_topology (GstDecodeChain * chain)
4500 {
4501 GstStructure *s, *u;
4502 GList *l;
4503 GstCaps *caps;
4504
4505 if (G_UNLIKELY ((chain->endpad || chain->deadend)
4506 && (chain->endcaps == NULL))) {
4507 GST_WARNING ("End chain without valid caps !");
4508 return NULL;
4509 }
4510
4511 u = gst_structure_new_id_empty (topology_structure_name);
4512
4513 /* Now at the last element */
4514 if ((chain->elements || !chain->active_group) &&
4515 (chain->endpad || chain->deadend)) {
4516 GstPad *srcpad;
4517
4518 s = gst_structure_new_id_empty (topology_structure_name);
4519 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, chain->endcaps,
4520 NULL);
4521
4522 if (chain->endpad) {
4523 gst_structure_id_set (u, topology_pad, GST_TYPE_PAD, chain->endpad, NULL);
4524
4525 srcpad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (chain->endpad));
4526 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD,
4527 srcpad, NULL);
4528
4529 gst_object_unref (srcpad);
4530 }
4531
4532 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
4533 gst_structure_free (u);
4534 u = s;
4535 } else if (chain->active_group) {
4536 GValue list = { 0, };
4537 GValue item = { 0, };
4538
4539 g_value_init (&list, GST_TYPE_LIST);
4540 g_value_init (&item, GST_TYPE_STRUCTURE);
4541 for (l = chain->active_group->children; l; l = l->next) {
4542 s = gst_decode_chain_get_topology (l->data);
4543 if (s) {
4544 gst_value_set_structure (&item, s);
4545 gst_value_list_append_value (&list, &item);
4546 g_value_reset (&item);
4547 gst_structure_free (s);
4548 }
4549 }
4550 gst_structure_id_set_value (u, topology_next, &list);
4551 g_value_unset (&list);
4552 g_value_unset (&item);
4553 }
4554
4555 /* Get caps between all elements in this chain */
4556 l = (chain->elements && chain->elements->next) ? chain->elements : NULL;
4557 for (; l && l->next; l = l->next) {
4558 GstDecodeElement *delem, *delem_next;
4559 GstElement *elem, *capsfilter, *elem_next;
4560 GstCaps *caps;
4561 GstPad *srcpad;
4562
4563 delem = l->data;
4564 elem = delem->element;
4565 delem_next = l->next->data;
4566 elem_next = delem_next->element;
4567 capsfilter = delem_next->capsfilter;
4568 srcpad = NULL;
4569
4570 caps = _gst_element_get_linked_caps (elem_next, elem, capsfilter, &srcpad);
4571
4572 if (caps) {
4573 s = gst_structure_new_id_empty (topology_structure_name);
4574 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
4575 gst_caps_unref (caps);
4576
4577 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
4578 gst_structure_free (u);
4579 u = s;
4580 }
4581
4582 if (srcpad) {
4583 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, srcpad,
4584 NULL);
4585 gst_object_unref (srcpad);
4586 }
4587 }
4588
4589 /* Caps that resulted in this chain */
4590 caps = get_pad_caps (chain->pad);
4591 if (G_UNLIKELY (!caps)) {
4592 GST_WARNING_OBJECT (chain->pad, "Couldn't get the caps of decode chain");
4593 return u;
4594 }
4595 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
4596 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, chain->pad,
4597 NULL);
4598 gst_caps_unref (caps);
4599
4600 return u;
4601 }
4602
4603 static void
gst_decode_bin_post_topology_message(GstDecodeBin * dbin)4604 gst_decode_bin_post_topology_message (GstDecodeBin * dbin)
4605 {
4606 GstStructure *s;
4607 GstMessage *msg;
4608
4609 s = gst_decode_chain_get_topology (dbin->decode_chain);
4610
4611 if (G_UNLIKELY (s == NULL))
4612 return;
4613 msg = gst_message_new_element (GST_OBJECT (dbin), s);
4614 gst_element_post_message (GST_ELEMENT (dbin), msg);
4615 }
4616
4617 static gboolean
debug_sticky_event(GstPad * pad,GstEvent ** event,gpointer user_data)4618 debug_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
4619 {
4620 GST_DEBUG_OBJECT (pad, "sticky event %s (%p)", GST_EVENT_TYPE_NAME (*event),
4621 *event);
4622 return TRUE;
4623 }
4624
4625
4626 /* Must only be called if the toplevel chain is complete and blocked! */
4627 /* Not MT-safe, call with decodebin expose lock! */
4628 static gboolean
gst_decode_bin_expose(GstDecodeBin * dbin)4629 gst_decode_bin_expose (GstDecodeBin * dbin)
4630 {
4631 GList *tmp, *endpads;
4632 gboolean missing_plugin;
4633 GString *missing_plugin_details;
4634 gboolean already_exposed;
4635 gboolean last_group;
4636
4637 retry:
4638 endpads = NULL;
4639 missing_plugin = FALSE;
4640 already_exposed = TRUE;
4641 last_group = TRUE;
4642
4643 missing_plugin_details = g_string_new ("");
4644
4645 GST_DEBUG_OBJECT (dbin, "Exposing currently active chains/groups");
4646
4647 /* Don't expose if we're currently shutting down */
4648 DYN_LOCK (dbin);
4649 if (G_UNLIKELY (dbin->shutdown)) {
4650 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4651 DYN_UNLOCK (dbin);
4652 return FALSE;
4653 }
4654 DYN_UNLOCK (dbin);
4655
4656 /* Get the pads that we're going to expose and mark things as exposed */
4657 if (!gst_decode_chain_expose (dbin->decode_chain, &endpads, &missing_plugin,
4658 missing_plugin_details, &last_group)) {
4659 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
4660 g_list_free (endpads);
4661 g_string_free (missing_plugin_details, TRUE);
4662 /* Failures could be due to the fact that we are currently shutting down (recheck) */
4663 DYN_LOCK (dbin);
4664 if (G_UNLIKELY (dbin->shutdown)) {
4665 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4666 DYN_UNLOCK (dbin);
4667 return FALSE;
4668 }
4669 DYN_UNLOCK (dbin);
4670 GST_ERROR_OBJECT (dbin, "Broken chain/group tree");
4671 g_return_val_if_reached (FALSE);
4672 return FALSE;
4673 }
4674 if (endpads == NULL) {
4675 if (missing_plugin) {
4676 if (missing_plugin_details->len > 0) {
4677 gchar *details = g_string_free (missing_plugin_details, FALSE);
4678 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
4679 ("no suitable plugins found:\n%s", details));
4680 g_free (details);
4681 } else {
4682 g_string_free (missing_plugin_details, TRUE);
4683 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
4684 ("no suitable plugins found"));
4685 }
4686 } else {
4687 /* in this case, the stream ended without buffers,
4688 * just post a warning */
4689 g_string_free (missing_plugin_details, TRUE);
4690
4691 GST_WARNING_OBJECT (dbin, "All streams finished without buffers. "
4692 "Last group: %d", last_group);
4693 if (last_group) {
4694 GST_ELEMENT_ERROR (dbin, STREAM, FAILED, (NULL),
4695 ("all streams without buffers"));
4696 } else {
4697 gboolean switched = FALSE;
4698 gboolean drained = FALSE;
4699
4700 drain_and_switch_chains (dbin->decode_chain, NULL, &last_group,
4701 &drained, &switched);
4702 GST_ELEMENT_WARNING (dbin, STREAM, FAILED, (NULL),
4703 ("all streams without buffers"));
4704 if (switched) {
4705 if (gst_decode_chain_is_complete (dbin->decode_chain))
4706 goto retry;
4707 else
4708 return FALSE;
4709 }
4710 }
4711 }
4712
4713 do_async_done (dbin);
4714 return FALSE;
4715 }
4716
4717 g_string_free (missing_plugin_details, TRUE);
4718
4719 /* Check if this was called when everything was exposed already */
4720 for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
4721 GstDecodePad *dpad = tmp->data;
4722
4723 already_exposed &= dpad->exposed;
4724 if (!already_exposed)
4725 break;
4726 }
4727 if (already_exposed) {
4728 GST_DEBUG_OBJECT (dbin, "Everything was exposed already!");
4729 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
4730 g_list_free (endpads);
4731 return TRUE;
4732 }
4733
4734 /* going to expose something, reset buffering */
4735 gst_decode_bin_reset_buffering (dbin);
4736
4737 /* Set all already exposed pads to blocked */
4738 for (tmp = endpads; tmp; tmp = tmp->next) {
4739 GstDecodePad *dpad = tmp->data;
4740
4741 if (dpad->exposed) {
4742 GST_DEBUG_OBJECT (dpad, "blocking exposed pad");
4743 gst_decode_pad_set_blocked (dpad, TRUE);
4744 }
4745 }
4746
4747 /* re-order pads : video, then audio, then others */
4748 endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
4749
4750 /* Don't add pads if we are shutting down */
4751 DYN_LOCK (dbin);
4752 if (G_UNLIKELY (dbin->shutdown)) {
4753 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4754 DYN_UNLOCK (dbin);
4755 return FALSE;
4756 }
4757
4758 /* Expose pads */
4759 for (tmp = endpads; tmp; tmp = tmp->next) {
4760 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
4761 gchar *padname;
4762
4763 /* 1. rewrite name */
4764 padname = g_strdup_printf ("src_%u", dbin->nbpads);
4765 dbin->nbpads++;
4766 GST_DEBUG_OBJECT (dbin, "About to expose dpad %s as %s",
4767 GST_OBJECT_NAME (dpad), padname);
4768 gst_object_set_name (GST_OBJECT (dpad), padname);
4769 g_free (padname);
4770
4771 gst_pad_sticky_events_foreach (GST_PAD_CAST (dpad), debug_sticky_event,
4772 dpad);
4773
4774 /* 2. activate and add */
4775 if (!dpad->exposed) {
4776 dpad->exposed = TRUE;
4777 if (!gst_element_add_pad (GST_ELEMENT (dbin), GST_PAD_CAST (dpad))) {
4778 /* not really fatal, we can try to add the other pads */
4779 g_warning ("error adding pad to decodebin");
4780 dpad->exposed = FALSE;
4781 continue;
4782 }
4783 }
4784
4785 /* 3. emit signal */
4786 GST_INFO_OBJECT (dpad, "added new decoded pad");
4787 }
4788 DYN_UNLOCK (dbin);
4789
4790 /* 4. Signal no-more-pads. This allows the application to hook stuff to the
4791 * exposed pads */
4792 GST_LOG_OBJECT (dbin, "signaling no-more-pads");
4793 gst_element_no_more_pads (GST_ELEMENT (dbin));
4794
4795 /* 5. Send a custom element message with the stream topology */
4796 if (dbin->post_stream_topology)
4797 gst_decode_bin_post_topology_message (dbin);
4798
4799 /* 6. Unblock internal pads. The application should have connected stuff now
4800 * so that streaming can continue. */
4801 for (tmp = endpads; tmp; tmp = tmp->next) {
4802 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
4803
4804 GST_DEBUG_OBJECT (dpad, "unblocking");
4805 gst_decode_pad_unblock (dpad);
4806 GST_DEBUG_OBJECT (dpad, "unblocked");
4807 gst_object_unref (dpad);
4808 }
4809 g_list_free (endpads);
4810
4811 do_async_done (dbin);
4812 GST_DEBUG_OBJECT (dbin, "Exposed everything");
4813 return TRUE;
4814 }
4815
4816 /* gst_decode_chain_expose:
4817 *
4818 * Check if the chain can be exposed and add all endpads
4819 * to the endpads list.
4820 *
4821 * Also update the active group's multiqueue to the
4822 * runtime limits.
4823 *
4824 * Not MT-safe, call with decodebin expose lock! *
4825 */
4826 static gboolean
gst_decode_chain_expose(GstDecodeChain * chain,GList ** endpads,gboolean * missing_plugin,GString * missing_plugin_details,gboolean * last_group)4827 gst_decode_chain_expose (GstDecodeChain * chain, GList ** endpads,
4828 gboolean * missing_plugin, GString * missing_plugin_details,
4829 gboolean * last_group)
4830 {
4831 GstDecodeGroup *group;
4832 GList *l;
4833 GstDecodeBin *dbin;
4834
4835 if (chain->deadend) {
4836 if (chain->endcaps) {
4837 if (chain->deadend_details) {
4838 g_string_append (missing_plugin_details, chain->deadend_details);
4839 g_string_append_c (missing_plugin_details, '\n');
4840 } else {
4841 gchar *desc = gst_pb_utils_get_codec_description (chain->endcaps);
4842 gchar *caps_str = gst_caps_to_string (chain->endcaps);
4843 g_string_append_printf (missing_plugin_details,
4844 "Missing decoder: %s (%s)\n", desc, caps_str);
4845 g_free (caps_str);
4846 g_free (desc);
4847 }
4848 *missing_plugin = TRUE;
4849 }
4850 return TRUE;
4851 }
4852
4853 if (chain->endpad) {
4854 if (!gst_decode_pad_is_exposable (chain->endpad) && !chain->endpad->exposed)
4855 return FALSE;
4856 *endpads = g_list_prepend (*endpads, gst_object_ref (chain->endpad));
4857 return TRUE;
4858 }
4859
4860 if (chain->next_groups)
4861 *last_group = FALSE;
4862
4863 group = chain->active_group;
4864 if (!group)
4865 return FALSE;
4866 if (!group->no_more_pads && !group->overrun)
4867 return FALSE;
4868
4869 dbin = group->dbin;
4870
4871 /* we can now disconnect any overrun signal, which is used to expose the
4872 * group. */
4873 if (group->overrunsig) {
4874 GST_LOG_OBJECT (dbin, "Disconnecting overrun");
4875 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
4876 group->overrunsig = 0;
4877 }
4878
4879 for (l = group->children; l; l = l->next) {
4880 GstDecodeChain *childchain = l->data;
4881
4882 if (!gst_decode_chain_expose (childchain, endpads, missing_plugin,
4883 missing_plugin_details, last_group))
4884 return FALSE;
4885 }
4886
4887 return TRUE;
4888 }
4889
4890 /*************************
4891 * GstDecodePad functions
4892 *************************/
4893
4894 static void
gst_decode_pad_class_init(GstDecodePadClass * klass)4895 gst_decode_pad_class_init (GstDecodePadClass * klass)
4896 {
4897 }
4898
4899 static void
gst_decode_pad_init(GstDecodePad * pad)4900 gst_decode_pad_init (GstDecodePad * pad)
4901 {
4902 pad->chain = NULL;
4903 pad->blocked = FALSE;
4904 pad->exposed = FALSE;
4905 pad->drained = FALSE;
4906 gst_object_ref_sink (pad);
4907 }
4908
4909 static GstPadProbeReturn
source_pad_blocked_cb(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)4910 source_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
4911 {
4912 GstDecodePad *dpad = user_data;
4913 GstDecodeChain *chain;
4914 GstDecodeBin *dbin;
4915 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
4916
4917 if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
4918 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
4919
4920 GST_LOG_OBJECT (pad, "Seeing event '%s'", GST_EVENT_TYPE_NAME (event));
4921
4922 if (!GST_EVENT_IS_SERIALIZED (event)) {
4923 /* do not block on sticky or out of band events otherwise the allocation query
4924 from demuxer might block the loop thread */
4925 GST_LOG_OBJECT (pad, "Letting OOB event through");
4926 return GST_PAD_PROBE_PASS;
4927 }
4928
4929 if (GST_EVENT_IS_STICKY (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
4930 /* manually push sticky events to ghost pad to avoid exposing pads
4931 * that don't have the sticky events. Handle EOS separately as we
4932 * want to block the pad on it if we didn't get any buffers before
4933 * EOS and expose the pad then. */
4934 gst_pad_push_event (GST_PAD_CAST (dpad), gst_event_ref (event));
4935
4936 /* let the sticky events pass */
4937 ret = GST_PAD_PROBE_PASS;
4938
4939 /* we only want to try to expose on CAPS events */
4940 if (GST_EVENT_TYPE (event) != GST_EVENT_CAPS) {
4941 GST_LOG_OBJECT (pad, "Letting sticky non-CAPS event through");
4942 goto done;
4943 }
4944 }
4945 } else if (GST_PAD_PROBE_INFO_TYPE (info) &
4946 GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) {
4947 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
4948
4949 if (!GST_QUERY_IS_SERIALIZED (query)) {
4950 /* do not block on non-serialized queries */
4951 GST_LOG_OBJECT (pad, "Letting non-serialized query through");
4952 return GST_PAD_PROBE_PASS;
4953 }
4954 if (!gst_pad_has_current_caps (pad)) {
4955 /* do not block on allocation queries before we have caps,
4956 * this would deadlock because we are doing no autoplugging
4957 * without caps.
4958 * TODO: Try to do autoplugging based on the query caps
4959 */
4960 GST_LOG_OBJECT (pad, "Letting serialized query before caps through");
4961 return GST_PAD_PROBE_PASS;
4962 }
4963 }
4964 chain = dpad->chain;
4965 dbin = chain->dbin;
4966
4967 GST_LOG_OBJECT (dpad, "blocked: dpad->chain:%p", chain);
4968
4969 dpad->blocked = TRUE;
4970
4971 EXPOSE_LOCK (dbin);
4972 if (dbin->decode_chain) {
4973 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
4974 if (!gst_decode_bin_expose (dbin))
4975 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
4976 }
4977 }
4978 EXPOSE_UNLOCK (dbin);
4979
4980 done:
4981 return ret;
4982 }
4983
4984 static GstPadProbeReturn
source_pad_event_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)4985 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
4986 gpointer user_data)
4987 {
4988 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
4989 GstDecodePad *dpad = user_data;
4990 gboolean res = TRUE;
4991
4992 GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
4993
4994 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
4995 GST_DEBUG_OBJECT (pad, "we received EOS");
4996
4997 /* Check if all pads are drained.
4998 * * If there is no next group, we will let the EOS go through.
4999 * * If there is a next group but the current group isn't completely
5000 * drained, we will drop the EOS event.
5001 * * If there is a next group to expose and this was the last non-drained
5002 * pad for that group, we will remove the ghostpad of the current group
5003 * first, which unlinks the peer and so drops the EOS. */
5004 res = gst_decode_pad_handle_eos (dpad);
5005 }
5006 if (res)
5007 return GST_PAD_PROBE_OK;
5008 else
5009 return GST_PAD_PROBE_DROP;
5010 }
5011
5012 static void
gst_decode_pad_set_blocked(GstDecodePad * dpad,gboolean blocked)5013 gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked)
5014 {
5015 GstDecodeBin *dbin = dpad->dbin;
5016 GstPad *opad;
5017
5018 DYN_LOCK (dbin);
5019
5020 GST_DEBUG_OBJECT (dpad, "blocking pad: %d", blocked);
5021
5022 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
5023 if (!opad)
5024 goto out;
5025
5026 /* do not block if shutting down.
5027 * we do not consider/expect it blocked further below, but use other trick */
5028 if (!blocked || !dbin->shutdown) {
5029 if (blocked) {
5030 if (dpad->block_id == 0)
5031 dpad->block_id =
5032 gst_pad_add_probe (opad,
5033 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM |
5034 GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM, source_pad_blocked_cb,
5035 gst_object_ref (dpad), (GDestroyNotify) gst_object_unref);
5036 } else {
5037 if (dpad->block_id != 0) {
5038 gst_pad_remove_probe (opad, dpad->block_id);
5039 dpad->block_id = 0;
5040 }
5041 dpad->blocked = FALSE;
5042 }
5043 }
5044
5045 if (blocked) {
5046 if (dbin->shutdown) {
5047 /* deactivate to force flushing state to prevent NOT_LINKED errors */
5048 gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
5049 /* note that deactivating the target pad would have no effect here,
5050 * since elements are typically connected first (and pads exposed),
5051 * and only then brought to PAUSED state (so pads activated) */
5052 } else {
5053 gst_object_ref (dpad);
5054 dbin->blocked_pads = g_list_prepend (dbin->blocked_pads, dpad);
5055 }
5056 } else {
5057 GList *l;
5058
5059 if ((l = g_list_find (dbin->blocked_pads, dpad))) {
5060 gst_object_unref (dpad);
5061 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, l);
5062 }
5063 }
5064 gst_object_unref (opad);
5065 out:
5066 DYN_UNLOCK (dbin);
5067 }
5068
5069 static void
gst_decode_pad_add_drained_check(GstDecodePad * dpad)5070 gst_decode_pad_add_drained_check (GstDecodePad * dpad)
5071 {
5072 gst_pad_add_probe (GST_PAD_CAST (dpad), GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
5073 source_pad_event_probe, dpad, NULL);
5074 }
5075
5076 static void
gst_decode_pad_activate(GstDecodePad * dpad,GstDecodeChain * chain)5077 gst_decode_pad_activate (GstDecodePad * dpad, GstDecodeChain * chain)
5078 {
5079 g_return_if_fail (chain != NULL);
5080
5081 dpad->chain = chain;
5082 gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
5083 gst_decode_pad_set_blocked (dpad, TRUE);
5084 gst_decode_pad_add_drained_check (dpad);
5085 }
5086
5087 static void
gst_decode_pad_unblock(GstDecodePad * dpad)5088 gst_decode_pad_unblock (GstDecodePad * dpad)
5089 {
5090 gst_decode_pad_set_blocked (dpad, FALSE);
5091 }
5092
5093 static gboolean
gst_decode_pad_event(GstPad * pad,GstObject * parent,GstEvent * event)5094 gst_decode_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
5095 {
5096 GstDecodeBin *dbin = GST_DECODE_BIN (parent);
5097
5098 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK && dbin && dbin->decode_chain) {
5099 GstElement *demuxer = NULL;
5100
5101 /* For adaptive demuxers we send the seek event directly to the demuxer.
5102 * See https://bugzilla.gnome.org/show_bug.cgi?id=606382
5103 */
5104 CHAIN_MUTEX_LOCK (dbin->decode_chain);
5105 if (dbin->decode_chain->adaptive_demuxer) {
5106 GstDecodeElement *delem = dbin->decode_chain->elements->data;
5107 demuxer = gst_object_ref (delem->element);
5108 }
5109 CHAIN_MUTEX_UNLOCK (dbin->decode_chain);
5110
5111 if (demuxer) {
5112 gboolean ret;
5113
5114 GST_DEBUG_OBJECT (dbin,
5115 "Sending SEEK event directly to adaptive streaming demuxer %s",
5116 GST_OBJECT_NAME (demuxer));
5117 ret = gst_element_send_event (demuxer, event);
5118 gst_object_unref (demuxer);
5119 return ret;
5120 }
5121 }
5122
5123 return gst_pad_event_default (pad, parent, event);
5124 }
5125
5126 static gboolean
gst_decode_pad_query(GstPad * pad,GstObject * parent,GstQuery * query)5127 gst_decode_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
5128 {
5129 GstDecodePad *dpad = GST_DECODE_PAD (parent);
5130 gboolean ret = FALSE;
5131
5132 CHAIN_MUTEX_LOCK (dpad->chain);
5133 if (!dpad->exposed && !dpad->dbin->shutdown && !dpad->chain->deadend
5134 && dpad->chain->elements) {
5135 GstDecodeElement *delem = dpad->chain->elements->data;
5136
5137 ret = FALSE;
5138 GST_DEBUG_OBJECT (dpad->dbin,
5139 "calling autoplug-query for %s (element %s): %" GST_PTR_FORMAT,
5140 GST_PAD_NAME (dpad), GST_ELEMENT_NAME (delem->element), query);
5141 g_signal_emit (G_OBJECT (dpad->dbin),
5142 gst_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY], 0, dpad, delem->element,
5143 query, &ret);
5144
5145 if (ret)
5146 GST_DEBUG_OBJECT (dpad->dbin,
5147 "autoplug-query returned %d: %" GST_PTR_FORMAT, ret, query);
5148 else
5149 GST_DEBUG_OBJECT (dpad->dbin, "autoplug-query returned %d", ret);
5150 }
5151 CHAIN_MUTEX_UNLOCK (dpad->chain);
5152
5153 /* If exposed or nothing handled the query use the default handler */
5154 if (!ret)
5155 ret = gst_pad_query_default (pad, parent, query);
5156
5157 return ret;
5158 }
5159
5160 static gboolean
gst_decode_pad_is_exposable(GstDecodePad * endpad)5161 gst_decode_pad_is_exposable (GstDecodePad * endpad)
5162 {
5163 if (endpad->blocked || endpad->exposed)
5164 return TRUE;
5165
5166 return gst_pad_has_current_caps (GST_PAD_CAST (endpad));
5167 }
5168
5169 /*gst_decode_pad_new:
5170 *
5171 * Creates a new GstDecodePad for the given pad.
5172 */
5173 static GstDecodePad *
gst_decode_pad_new(GstDecodeBin * dbin,GstDecodeChain * chain)5174 gst_decode_pad_new (GstDecodeBin * dbin, GstDecodeChain * chain)
5175 {
5176 GstDecodePad *dpad;
5177 GstProxyPad *ppad;
5178 GstPadTemplate *pad_tmpl;
5179
5180 GST_DEBUG_OBJECT (dbin, "making new decodepad");
5181 pad_tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
5182 dpad =
5183 g_object_new (GST_TYPE_DECODE_PAD, "direction", GST_PAD_SRC,
5184 "template", pad_tmpl, NULL);
5185 gst_ghost_pad_construct (GST_GHOST_PAD_CAST (dpad));
5186 dpad->chain = chain;
5187 dpad->dbin = dbin;
5188 gst_object_unref (pad_tmpl);
5189
5190 ppad = gst_proxy_pad_get_internal (GST_PROXY_PAD (dpad));
5191 gst_pad_set_query_function (GST_PAD_CAST (ppad), gst_decode_pad_query);
5192 gst_pad_set_event_function (GST_PAD_CAST (dpad), gst_decode_pad_event);
5193 gst_object_unref (ppad);
5194
5195 return dpad;
5196 }
5197
5198 static void
gst_pending_pad_free(GstPendingPad * ppad)5199 gst_pending_pad_free (GstPendingPad * ppad)
5200 {
5201 g_assert (ppad);
5202 g_assert (ppad->pad);
5203
5204 if (ppad->event_probe_id != 0)
5205 gst_pad_remove_probe (ppad->pad, ppad->event_probe_id);
5206 if (ppad->notify_caps_id)
5207 g_signal_handler_disconnect (ppad->pad, ppad->notify_caps_id);
5208 gst_object_unref (ppad->pad);
5209 g_slice_free (GstPendingPad, ppad);
5210 }
5211
5212 /*****
5213 * Element add/remove
5214 *****/
5215
5216 static void
do_async_start(GstDecodeBin * dbin)5217 do_async_start (GstDecodeBin * dbin)
5218 {
5219 GstMessage *message;
5220
5221 dbin->async_pending = TRUE;
5222
5223 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
5224 parent_class->handle_message (GST_BIN_CAST (dbin), message);
5225 }
5226
5227 static void
do_async_done(GstDecodeBin * dbin)5228 do_async_done (GstDecodeBin * dbin)
5229 {
5230 GstMessage *message;
5231
5232 if (dbin->async_pending) {
5233 message =
5234 gst_message_new_async_done (GST_OBJECT_CAST (dbin),
5235 GST_CLOCK_TIME_NONE);
5236 parent_class->handle_message (GST_BIN_CAST (dbin), message);
5237
5238 dbin->async_pending = FALSE;
5239 }
5240 }
5241
5242 /*****
5243 * convenience functions
5244 *****/
5245
5246 /* find_sink_pad
5247 *
5248 * Returns the first sink pad of the given element, or NULL if it doesn't have
5249 * any.
5250 */
5251
5252 static GstPad *
find_sink_pad(GstElement * element)5253 find_sink_pad (GstElement * element)
5254 {
5255 GstIterator *it;
5256 GstPad *pad = NULL;
5257 GValue item = { 0, };
5258
5259 it = gst_element_iterate_sink_pads (element);
5260
5261 if ((gst_iterator_next (it, &item)) == GST_ITERATOR_OK)
5262 pad = g_value_dup_object (&item);
5263 g_value_unset (&item);
5264 gst_iterator_free (it);
5265
5266 return pad;
5267 }
5268
5269 /* call with dyn_lock held */
5270 static void
unblock_pads(GstDecodeBin * dbin)5271 unblock_pads (GstDecodeBin * dbin)
5272 {
5273 GST_LOG_OBJECT (dbin, "unblocking pads");
5274
5275 while (dbin->blocked_pads) {
5276 GList *tmp = dbin->blocked_pads;
5277 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
5278 GstPad *opad;
5279
5280 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, tmp);
5281 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
5282 if (opad) {
5283
5284 GST_DEBUG_OBJECT (dpad, "unblocking");
5285 if (dpad->block_id != 0) {
5286 gst_pad_remove_probe (opad, dpad->block_id);
5287 dpad->block_id = 0;
5288 }
5289 gst_object_unref (opad);
5290 }
5291
5292 dpad->blocked = FALSE;
5293
5294 /* We release the dyn lock since we want to allow the streaming threads
5295 * to properly stop and not be blocked in our various probes */
5296 DYN_UNLOCK (dbin);
5297 /* make flushing, prevent NOT_LINKED */
5298 gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
5299 DYN_LOCK (dbin);
5300
5301 GST_DEBUG_OBJECT (dpad, "unblocked");
5302 gst_object_unref (dpad);
5303 }
5304 }
5305
5306 static void
gst_decode_chain_stop(GstDecodeBin * dbin,GstDecodeChain * chain,GQueue * elements)5307 gst_decode_chain_stop (GstDecodeBin * dbin, GstDecodeChain * chain,
5308 GQueue * elements)
5309 {
5310 GQueue *internal_elements, internal_elements_ = G_QUEUE_INIT;
5311 GList *l;
5312
5313 CHAIN_MUTEX_LOCK (chain);
5314 if (elements) {
5315 internal_elements = elements;
5316 } else {
5317 internal_elements = &internal_elements_;
5318 }
5319
5320 for (l = chain->next_groups; l; l = l->next) {
5321 GstDecodeGroup *group = l->data;
5322 GList *m;
5323
5324 for (m = group->children; m; m = m->next) {
5325 GstDecodeChain *chain2 = m->data;
5326 gst_decode_chain_stop (dbin, chain2, internal_elements);
5327 }
5328 if (group->multiqueue)
5329 g_queue_push_head (internal_elements, gst_object_ref (group->multiqueue));
5330 }
5331
5332 if (chain->active_group) {
5333 for (l = chain->active_group->children; l; l = l->next) {
5334 GstDecodeChain *chain2 = l->data;
5335 gst_decode_chain_stop (dbin, chain2, internal_elements);
5336 }
5337 if (chain->active_group->multiqueue)
5338 g_queue_push_head (internal_elements,
5339 gst_object_ref (chain->active_group->multiqueue));
5340 }
5341
5342 for (l = chain->old_groups; l; l = l->next) {
5343 GstDecodeGroup *group = l->data;
5344 GList *m;
5345
5346 for (m = group->children; m; m = m->next) {
5347 GstDecodeChain *chain2 = m->data;
5348 gst_decode_chain_stop (dbin, chain2, internal_elements);
5349 }
5350 if (group->multiqueue)
5351 g_queue_push_head (internal_elements, gst_object_ref (group->multiqueue));
5352 }
5353
5354 for (l = chain->elements; l; l = l->next) {
5355 GstDecodeElement *delem = l->data;
5356
5357 if (delem->capsfilter)
5358 g_queue_push_head (internal_elements, gst_object_ref (delem->capsfilter));
5359 g_queue_push_head (internal_elements, gst_object_ref (delem->element));
5360 }
5361
5362 CHAIN_MUTEX_UNLOCK (chain);
5363
5364 if (!elements) {
5365 GstElement *element;
5366
5367 EXPOSE_UNLOCK (dbin);
5368 /* Shut down from bottom to top */
5369 while ((element = g_queue_pop_tail (internal_elements))) {
5370 /* The bin must never ever change the state of this element anymore */
5371 gst_element_set_locked_state (element, TRUE);
5372 gst_element_set_state (element, GST_STATE_NULL);
5373 gst_object_unref (element);
5374 }
5375 g_queue_clear (internal_elements);
5376 EXPOSE_LOCK (dbin);
5377 }
5378 }
5379
5380 static GstStateChangeReturn
gst_decode_bin_change_state(GstElement * element,GstStateChange transition)5381 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
5382 {
5383 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
5384 GstDecodeBin *dbin = GST_DECODE_BIN (element);
5385 GstDecodeChain *chain_to_free = NULL;
5386
5387 switch (transition) {
5388 case GST_STATE_CHANGE_NULL_TO_READY:
5389 if (dbin->typefind == NULL)
5390 goto missing_typefind;
5391 break;
5392 case GST_STATE_CHANGE_READY_TO_PAUSED:
5393 /* Make sure we've cleared all existing chains */
5394 EXPOSE_LOCK (dbin);
5395 if (dbin->decode_chain) {
5396 gst_decode_chain_free (dbin->decode_chain);
5397 dbin->decode_chain = NULL;
5398 }
5399 EXPOSE_UNLOCK (dbin);
5400 DYN_LOCK (dbin);
5401 GST_LOG_OBJECT (dbin, "clearing shutdown flag");
5402 dbin->shutdown = FALSE;
5403 DYN_UNLOCK (dbin);
5404 dbin->have_type = FALSE;
5405 ret = GST_STATE_CHANGE_ASYNC;
5406 do_async_start (dbin);
5407
5408
5409 /* connect a signal to find out when the typefind element found
5410 * a type */
5411 dbin->have_type_id =
5412 g_signal_connect (dbin->typefind, "have-type",
5413 G_CALLBACK (type_found), dbin);
5414 break;
5415 case GST_STATE_CHANGE_PAUSED_TO_READY:
5416 case GST_STATE_CHANGE_READY_TO_NULL:
5417 if (dbin->have_type_id)
5418 g_signal_handler_disconnect (dbin->typefind, dbin->have_type_id);
5419 dbin->have_type_id = 0;
5420 DYN_LOCK (dbin);
5421 GST_LOG_OBJECT (dbin, "setting shutdown flag");
5422 dbin->shutdown = TRUE;
5423 unblock_pads (dbin);
5424 DYN_UNLOCK (dbin);
5425
5426 /* Make sure we don't have cleanup races where
5427 * we might be trying to deactivate pads (in the cleanup thread)
5428 * at the same time as the default element deactivation
5429 * (in PAUSED=>READY) */
5430 g_mutex_lock (&dbin->cleanup_lock);
5431 if (dbin->cleanup_thread) {
5432 g_thread_join (dbin->cleanup_thread);
5433 dbin->cleanup_thread = NULL;
5434 }
5435 g_mutex_unlock (&dbin->cleanup_lock);
5436 default:
5437 break;
5438 }
5439
5440 {
5441 GstStateChangeReturn bret;
5442
5443 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5444 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
5445 goto activate_failed;
5446 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
5447 do_async_done (dbin);
5448 ret = bret;
5449 }
5450 }
5451 switch (transition) {
5452 case GST_STATE_CHANGE_PAUSED_TO_READY:
5453 do_async_done (dbin);
5454 EXPOSE_LOCK (dbin);
5455 if (dbin->decode_chain) {
5456 gst_decode_chain_stop (dbin, dbin->decode_chain, NULL);
5457 chain_to_free = dbin->decode_chain;
5458 gst_decode_chain_free_internal (dbin->decode_chain, TRUE);
5459 dbin->decode_chain = NULL;
5460 }
5461 EXPOSE_UNLOCK (dbin);
5462 if (chain_to_free)
5463 gst_decode_chain_free (chain_to_free);
5464 g_list_free_full (dbin->buffering_status,
5465 (GDestroyNotify) gst_message_unref);
5466 dbin->buffering_status = NULL;
5467 /* Let's do a final check of leftover groups to free */
5468 g_mutex_lock (&dbin->cleanup_lock);
5469 if (dbin->cleanup_groups) {
5470 gst_decode_chain_free_hidden_groups (dbin->cleanup_groups);
5471 dbin->cleanup_groups = NULL;
5472 }
5473 g_mutex_unlock (&dbin->cleanup_lock);
5474 break;
5475 case GST_STATE_CHANGE_READY_TO_NULL:
5476 /* Let's do a final check of leftover groups to free */
5477 g_mutex_lock (&dbin->cleanup_lock);
5478 if (dbin->cleanup_groups) {
5479 gst_decode_chain_free_hidden_groups (dbin->cleanup_groups);
5480 dbin->cleanup_groups = NULL;
5481 }
5482 g_mutex_unlock (&dbin->cleanup_lock);
5483 break;
5484 default:
5485 break;
5486 }
5487
5488 return ret;
5489
5490 /* ERRORS */
5491 missing_typefind:
5492 {
5493 gst_element_post_message (element,
5494 gst_missing_element_message_new (element, "typefind"));
5495 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
5496 return GST_STATE_CHANGE_FAILURE;
5497 }
5498 activate_failed:
5499 {
5500 GST_DEBUG_OBJECT (element,
5501 "element failed to change states -- activation problem?");
5502 do_async_done (dbin);
5503 return GST_STATE_CHANGE_FAILURE;
5504 }
5505 }
5506
5507 static void
gst_decode_bin_handle_message(GstBin * bin,GstMessage * msg)5508 gst_decode_bin_handle_message (GstBin * bin, GstMessage * msg)
5509 {
5510 GstDecodeBin *dbin = GST_DECODE_BIN (bin);
5511 gboolean drop = FALSE;
5512
5513 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
5514 /* Don't pass errors when shutting down. Sometimes,
5515 * elements can generate spurious errors because we set the
5516 * output pads to flushing, and they can't detect that if they
5517 * send an event at exactly the wrong moment */
5518 DYN_LOCK (dbin);
5519 drop = dbin->shutdown;
5520 DYN_UNLOCK (dbin);
5521
5522 if (!drop) {
5523 GST_OBJECT_LOCK (dbin);
5524 drop = (g_list_find (dbin->filtered, GST_MESSAGE_SRC (msg)) != NULL);
5525 if (drop)
5526 dbin->filtered_errors =
5527 g_list_prepend (dbin->filtered_errors, gst_message_ref (msg));
5528 GST_OBJECT_UNLOCK (dbin);
5529 }
5530 } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_BUFFERING) {
5531 gint perc, msg_perc;
5532 gint smaller_perc = 100;
5533 GstMessage *smaller = NULL;
5534 GList *found = NULL;
5535 GList *iter;
5536
5537 /* buffering messages must be aggregated as there might be multiple
5538 * multiqueue in the pipeline and their independent buffering messages
5539 * will confuse the application
5540 *
5541 * decodebin keeps a list of messages received from elements that are
5542 * buffering.
5543 * Rules are:
5544 * 1) Always post the smaller buffering %
5545 * 2) If an element posts a 100% buffering message, remove it from the list
5546 * 3) When there are no more messages on the list, post 100% message
5547 * 4) When an element posts a new buffering message, update the one
5548 * on the list to this new value
5549 */
5550
5551 BUFFERING_LOCK (dbin);
5552 gst_message_parse_buffering (msg, &msg_perc);
5553
5554 GST_DEBUG_OBJECT (dbin, "Got buffering msg %" GST_PTR_FORMAT, msg);
5555
5556 g_mutex_lock (&dbin->buffering_post_lock);
5557
5558 /*
5559 * Single loop for 2 things:
5560 * 1) Look for a message with the same source
5561 * 1.1) If the received message is 100%, remove it from the list
5562 * 2) Find the minimum buffering from the list
5563 */
5564 for (iter = dbin->buffering_status; iter;) {
5565 GstMessage *bufstats = iter->data;
5566 if (GST_MESSAGE_SRC (bufstats) == GST_MESSAGE_SRC (msg)) {
5567 found = iter;
5568 if (msg_perc < 100) {
5569 GST_DEBUG_OBJECT (dbin, "Replacing old buffering msg %"
5570 GST_PTR_FORMAT, iter->data);
5571 gst_message_unref (iter->data);
5572 bufstats = iter->data = gst_message_ref (msg);
5573 } else {
5574 GList *current = iter;
5575
5576 /* remove the element here and avoid confusing the loop */
5577 iter = g_list_next (iter);
5578
5579 GST_DEBUG_OBJECT (dbin, "Deleting old buffering msg %"
5580 GST_PTR_FORMAT, current->data);
5581
5582 gst_message_unref (current->data);
5583 dbin->buffering_status =
5584 g_list_delete_link (dbin->buffering_status, current);
5585
5586 continue;
5587 }
5588 }
5589
5590 gst_message_parse_buffering (bufstats, &perc);
5591 if (perc < smaller_perc) {
5592 smaller_perc = perc;
5593 smaller = bufstats;
5594 }
5595 iter = g_list_next (iter);
5596 }
5597
5598 if (found == NULL && msg_perc < 100) {
5599 if (msg_perc < smaller_perc) {
5600 smaller_perc = msg_perc;
5601 smaller = msg;
5602 }
5603 GST_DEBUG_OBJECT (dbin, "Storing buffering msg %" GST_PTR_FORMAT, msg);
5604 dbin->buffering_status =
5605 g_list_prepend (dbin->buffering_status, gst_message_ref (msg));
5606 }
5607
5608 /* now compute the buffering message that should be posted */
5609 if (smaller_perc == 100) {
5610 g_assert (dbin->buffering_status == NULL);
5611 /* we are posting the original received msg */
5612 } else {
5613 gst_message_replace (&msg, smaller);
5614 }
5615 BUFFERING_UNLOCK (dbin);
5616
5617 GST_DEBUG_OBJECT (dbin, "Forwarding buffering msg %" GST_PTR_FORMAT, msg);
5618 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
5619
5620 g_mutex_unlock (&dbin->buffering_post_lock);
5621 return;
5622 }
5623
5624 if (drop) {
5625 gst_message_unref (msg);
5626 } else {
5627 GST_DEBUG_OBJECT (dbin, "Forwarding msg %" GST_PTR_FORMAT, msg);
5628 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
5629 }
5630 }
5631
5632 static gboolean
gst_decode_bin_remove_element(GstBin * bin,GstElement * element)5633 gst_decode_bin_remove_element (GstBin * bin, GstElement * element)
5634 {
5635 GstDecodeBin *dbin = GST_DECODE_BIN (bin);
5636 gboolean removed = FALSE, post = FALSE;
5637 GList *iter;
5638
5639 BUFFERING_LOCK (bin);
5640 g_mutex_lock (&dbin->buffering_post_lock);
5641 for (iter = dbin->buffering_status; iter; iter = iter->next) {
5642 GstMessage *bufstats = iter->data;
5643
5644 if (GST_MESSAGE_SRC (bufstats) == GST_OBJECT_CAST (element) ||
5645 gst_object_has_as_ancestor (GST_MESSAGE_SRC (bufstats),
5646 GST_OBJECT_CAST (element))) {
5647 gst_message_unref (bufstats);
5648 dbin->buffering_status =
5649 g_list_delete_link (dbin->buffering_status, iter);
5650 removed = TRUE;
5651 break;
5652 }
5653 }
5654
5655 if (removed && dbin->buffering_status == NULL)
5656 post = TRUE;
5657 BUFFERING_UNLOCK (bin);
5658
5659 if (post) {
5660 gst_element_post_message (GST_ELEMENT_CAST (bin),
5661 gst_message_new_buffering (GST_OBJECT_CAST (dbin), 100));
5662 }
5663 g_mutex_unlock (&dbin->buffering_post_lock);
5664
5665 return GST_BIN_CLASS (parent_class)->remove_element (bin, element);
5666 }
5667
5668 gboolean
gst_decode_bin_plugin_init(GstPlugin * plugin)5669 gst_decode_bin_plugin_init (GstPlugin * plugin)
5670 {
5671 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
5672
5673 /* Register some quarks here for the stream topology message */
5674 topology_structure_name = g_quark_from_static_string ("stream-topology");
5675 topology_caps = g_quark_from_static_string ("caps");
5676 topology_next = g_quark_from_static_string ("next");
5677 topology_pad = g_quark_from_static_string ("pad");
5678 topology_element_srcpad = g_quark_from_static_string ("element-srcpad");
5679
5680 return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
5681 GST_TYPE_DECODE_BIN);
5682 }
5683