2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-uridecodebin
22 * @title: uridecodebin
24 * Decodes data from a URI into raw media. It selects a source element that can
25 * handle the given #GstURIDecodeBin:uri scheme and connects it to a decodebin.
28 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
29 * with newer GLib versions (>= 2.31.0) */
30 #define GLIB_DISABLE_DEPRECATION_WARNINGS
39 #include <glib/gi18n-lib.h>
40 #include <gst/pbutils/missing-plugins.h>
42 #include "gstplay-enum.h"
43 #include "gstrawcaps.h"
44 #include "gstplaybackelements.h"
45 #include "gstplaybackutils.h"
47 #define GST_TYPE_URI_DECODE_BIN \
48 (gst_uri_decode_bin_get_type())
49 #define GST_URI_DECODE_BIN(obj) \
50 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBin))
51 #define GST_URI_DECODE_BIN_CLASS(klass) \
52 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBinClass))
53 #define GST_IS_URI_DECODE_BIN(obj) \
54 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DECODE_BIN))
55 #define GST_IS_URI_DECODE_BIN_CLASS(klass) \
56 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DECODE_BIN))
57 #define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
59 typedef struct _GstURIDecodeBin GstURIDecodeBin;
60 typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;
62 #define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(&((GstURIDecodeBin*)(dec))->lock))
63 #define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(&((GstURIDecodeBin*)(dec))->lock))
65 typedef struct _GstURIDecodeBinStream
69 } GstURIDecodeBinStream;
74 * uridecodebin element struct
76 struct _GstURIDecodeBin
78 GstBin parent_instance;
80 GMutex lock; /* lock for constructing */
82 GMutex factories_lock;
83 guint32 factories_cookie;
84 GList *factories; /* factories we can use for selecting elements */
87 guint64 connection_speed;
94 guint64 buffer_duration; /* When buffering, buffer duration (ns) */
95 guint buffer_size; /* When buffering, buffer size (bytes) */
97 gboolean use_buffering;
98 gboolean force_sw_decoders;
102 GstElement *typefind;
103 guint have_type_id; /* have-type signal id from typefind */
104 /* without holding ref */
106 /* Holding strong reference to decodebin */
107 GSList *pending_decodebins;
111 /* for dynamic sources */
112 guint src_np_sig_id; /* new-pad signal id */
113 guint src_nmp_sig_id; /* no-more-pads signal id */
115 GList *missing_plugin_errors;
117 gboolean async_pending; /* async-start has been emitted */
119 gboolean expose_allstreams; /* Whether to expose unknown type streams or not */
121 guint64 ring_buffer_max_size; /* 0 means disabled */
124 struct _GstURIDecodeBinClass
126 GstBinClass parent_class;
128 /* signal fired when we found a pad that we cannot decode */
129 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
131 /* signal fired to know if we continue trying to decode the given caps */
132 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
134 /* signal fired to get a list of factories to try to autoplug */
135 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
137 /* signal fired to sort the factories */
138 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
139 GstCaps * caps, GValueArray * factories);
140 /* signal fired to select from the proposed list of factories */
141 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
142 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
143 /* signal fired when a autoplugged element that is not linked downstream
144 * or exposed wants to query something */
145 gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
148 /* emitted when all data is decoded */
149 void (*drained) (GstElement * element);
152 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
155 GST_STATIC_CAPS_ANY);
157 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
159 GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
160 #define GST_CAT_DEFAULT gst_uri_decode_bin_debug
166 SIGNAL_AUTOPLUG_CONTINUE,
167 SIGNAL_AUTOPLUG_FACTORIES,
168 SIGNAL_AUTOPLUG_SELECT,
169 SIGNAL_AUTOPLUG_SORT,
170 SIGNAL_AUTOPLUG_QUERY,
177 #define DEFAULT_PROP_URI NULL
178 #define DEFAULT_PROP_SOURCE NULL
179 #define DEFAULT_CONNECTION_SPEED 0
180 #define DEFAULT_CAPS (gst_static_caps_get (&default_raw_caps))
181 #define DEFAULT_SUBTITLE_ENCODING NULL
182 #define DEFAULT_BUFFER_DURATION -1
183 #define DEFAULT_BUFFER_SIZE -1
184 #define DEFAULT_DOWNLOAD FALSE
185 #define DEFAULT_USE_BUFFERING FALSE
186 #define DEFAULT_FORCE_SW_DECODERS FALSE
187 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
188 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
195 PROP_CONNECTION_SPEED,
197 PROP_SUBTITLE_ENCODING,
199 PROP_BUFFER_DURATION,
202 PROP_FORCE_SW_DECODERS,
203 PROP_EXPOSE_ALL_STREAMS,
204 PROP_RING_BUFFER_MAX_SIZE
207 static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };
209 GType gst_uri_decode_bin_get_type (void);
210 #define gst_uri_decode_bin_parent_class parent_class
211 G_DEFINE_TYPE (GstURIDecodeBin, gst_uri_decode_bin, GST_TYPE_BIN);
214 GST_DEBUG_CATEGORY_INIT (gst_uri_decode_bin_debug, "uridecodebin", 0, "URI decoder element"); \
215 playback_element_init (plugin);
216 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (uridecodebin, "uridecodebin",
217 GST_RANK_NONE, GST_TYPE_URI_DECODE_BIN, _do_init);
219 static void remove_decoders (GstURIDecodeBin * bin, gboolean force);
220 static void gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
221 const GValue * value, GParamSpec * pspec);
222 static void gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
223 GValue * value, GParamSpec * pspec);
224 static void gst_uri_decode_bin_finalize (GObject * obj);
226 static void handle_message (GstBin * bin, GstMessage * msg);
228 static gboolean gst_uri_decode_bin_query (GstElement * element,
230 static GstStateChangeReturn gst_uri_decode_bin_change_state (GstElement *
231 element, GstStateChange transition);
234 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
235 GValue * return_accu, const GValue * handler_return, gpointer dummy)
239 myboolean = g_value_get_boolean (handler_return);
240 g_value_set_boolean (return_accu, myboolean);
242 /* stop emission if FALSE */
247 _gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
248 GValue * return_accu, const GValue * handler_return, gpointer dummy)
253 myboolean = g_value_get_boolean (handler_return);
254 retboolean = g_value_get_boolean (return_accu);
256 g_value_set_boolean (return_accu, myboolean || retboolean);
262 _gst_array_accumulator (GSignalInvocationHint * ihint,
263 GValue * return_accu, const GValue * handler_return, gpointer dummy)
267 array = g_value_get_boxed (handler_return);
268 g_value_set_boxed (return_accu, array);
274 _gst_select_accumulator (GSignalInvocationHint * ihint,
275 GValue * return_accu, const GValue * handler_return, gpointer dummy)
277 GstAutoplugSelectResult res;
279 res = g_value_get_enum (handler_return);
280 g_value_set_enum (return_accu, res);
282 /* Call the next handler in the chain (if any) when the current callback
283 * returns TRY. This makes it possible to register separate autoplug-select
284 * handlers that implement different TRY/EXPOSE/SKIP strategies.
286 if (res == GST_AUTOPLUG_SELECT_TRY)
293 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
294 GValue * return_accu, const GValue * handler_return, gpointer dummy)
298 array = g_value_get_boxed (handler_return);
299 g_value_set_boxed (return_accu, array);
308 gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
311 /* by default we always continue */
315 /* Must be called with factories lock! */
317 gst_uri_decode_bin_update_factories_list (GstURIDecodeBin * dec)
320 GList *factories, *tmp;
322 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
323 if (!dec->factories || dec->factories_cookie != cookie) {
325 gst_plugin_feature_list_free (dec->factories);
327 gst_element_factory_list_get_elements
328 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
330 if (dec->force_sw_decoders) {
331 /* filter out Hardware class elements */
332 dec->factories = NULL;
333 for (tmp = factories; tmp; tmp = g_list_next (tmp)) {
334 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
335 if (!gst_element_factory_list_is_type (factory,
336 GST_ELEMENT_FACTORY_TYPE_HARDWARE)) {
337 dec->factories = g_list_prepend (dec->factories, factory);
339 gst_object_unref (factory);
342 g_list_free (factories);
344 dec->factories = factories;
348 g_list_sort (dec->factories, gst_playback_utils_compare_factories_func);
349 dec->factories_cookie = cookie;
354 gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
359 GstURIDecodeBin *dec = GST_URI_DECODE_BIN_CAST (element);
361 GST_DEBUG_OBJECT (element, "finding factories");
363 /* return all compatible factories for caps */
364 g_mutex_lock (&dec->factories_lock);
365 gst_uri_decode_bin_update_factories_list (dec);
367 gst_element_factory_list_filter (dec->factories, caps, GST_PAD_SINK,
368 gst_caps_is_fixed (caps));
369 g_mutex_unlock (&dec->factories_lock);
371 result = g_value_array_new (g_list_length (list));
372 for (tmp = list; tmp; tmp = tmp->next) {
373 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
376 g_value_init (&val, G_TYPE_OBJECT);
377 g_value_set_object (&val, factory);
378 g_value_array_append (result, &val);
379 g_value_unset (&val);
381 gst_plugin_feature_list_free (list);
383 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
389 gst_uri_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
390 GstCaps * caps, GValueArray * factories)
395 static GstAutoplugSelectResult
396 gst_uri_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
397 GstCaps * caps, GstElementFactory * factory)
399 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
402 return GST_AUTOPLUG_SELECT_TRY;
406 gst_uri_decode_bin_autoplug_query (GstElement * element, GstPad * pad,
409 /* No query handled here */
414 gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
416 GObjectClass *gobject_class;
417 GstElementClass *gstelement_class;
418 GstBinClass *gstbin_class;
420 gobject_class = G_OBJECT_CLASS (klass);
421 gstelement_class = GST_ELEMENT_CLASS (klass);
422 gstbin_class = GST_BIN_CLASS (klass);
424 gobject_class->set_property = gst_uri_decode_bin_set_property;
425 gobject_class->get_property = gst_uri_decode_bin_get_property;
426 gobject_class->finalize = gst_uri_decode_bin_finalize;
428 g_object_class_install_property (gobject_class, PROP_URI,
429 g_param_spec_string ("uri", "URI", "URI to decode",
430 DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_SOURCE,
433 g_param_spec_object ("source", "Source", "Source object used",
434 GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
436 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
437 g_param_spec_uint64 ("connection-speed", "Connection Speed",
438 "Network connection speed in kbps (0 = unknown)",
439 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_CAPS,
443 g_param_spec_boxed ("caps", "Caps",
444 "The caps on which to stop decoding. (NULL = default)",
445 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
447 g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
448 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
449 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
450 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
451 "be checked for an encoding to use. If that is not set either, "
452 "ISO-8859-15 will be assumed.", NULL,
453 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
456 g_param_spec_int ("buffer-size", "Buffer size (bytes)",
457 "Buffer size when buffering streams (-1 default value)",
458 -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
459 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
460 g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
461 g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
462 "Buffer duration when buffering streams (-1 default value)",
463 -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
464 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 * GstURIDecodeBin::download:
469 * For certain media type, enable download buffering.
471 g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
472 g_param_spec_boolean ("download", "Download",
473 "Attempt download buffering when buffering network streams",
474 DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
476 * GstURIDecodeBin::use-buffering:
478 * Emit BUFFERING messages based on low-/high-percent thresholds of the
479 * demuxed or parsed data.
480 * When download buffering is activated and used for the current media
481 * type, this property does nothing. Otherwise perform buffering on the
482 * demuxed or parsed media.
484 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
485 g_param_spec_boolean ("use-buffering", "Use Buffering",
486 "Perform buffering on demuxed/parsed media",
487 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
490 * GstURIDecodeBin::force-sw-decoders:
492 * While auto-plugging, if set to %TRUE, those decoders within
493 * "Hardware" klass will be ignored. Otherwise they will be tried.
497 g_object_class_install_property (gobject_class, PROP_FORCE_SW_DECODERS,
498 g_param_spec_boolean ("force-sw-decoders", "Software Docoders Only",
499 "Use only sofware decoders to process streams",
500 DEFAULT_FORCE_SW_DECODERS,
501 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
504 * GstURIDecodeBin::expose-all-streams:
506 * Expose streams of unknown type.
508 * If set to %FALSE, then only the streams that can be decoded to the final
509 * caps (see 'caps' property) will have a pad exposed. Streams that do not
510 * match those caps but could have been decoded will not have decoder plugged
511 * in internally and will not have a pad exposed.
513 g_object_class_install_property (gobject_class, PROP_EXPOSE_ALL_STREAMS,
514 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
515 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
516 DEFAULT_EXPOSE_ALL_STREAMS,
517 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
520 * GstURIDecodeBin::ring-buffer-max-size:
522 * The maximum size of the ring buffer in kilobytes. If set to 0, the ring
523 * buffer is disabled. Default is 0.
525 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
526 g_param_spec_uint64 ("ring-buffer-max-size",
527 "Max. ring buffer size (bytes)",
528 "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
529 0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
530 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
533 * GstURIDecodeBin::unknown-type:
534 * @bin: The uridecodebin.
535 * @pad: the new pad containing caps that cannot be resolved to a 'final'.
537 * @caps: the #GstCaps of the pad that cannot be resolved.
539 * This signal is emitted when a pad for which there is no further possible
540 * decoding is added to the uridecodebin.
542 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
543 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
544 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, unknown_type),
545 NULL, NULL, NULL, G_TYPE_NONE, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
548 * GstURIDecodeBin::autoplug-continue:
549 * @bin: The uridecodebin.
551 * @caps: The #GstCaps found.
553 * This signal is emitted whenever uridecodebin finds a new stream. It is
554 * emitted before looking for any elements that can handle that stream.
556 * > Invocation of signal handlers stops after the first signal handler
557 * > returns %FALSE. Signal handlers are invoked in the order they were
560 * Returns: %TRUE if you wish uridecodebin to look for elements that can
561 * handle the given @caps. If %FALSE, those caps will be considered as
562 * final and the pad will be exposed as such (see 'pad-added' signal of
565 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
566 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
567 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
568 autoplug_continue), _gst_boolean_accumulator, NULL,
569 NULL, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
572 * GstURIDecodeBin::autoplug-factories:
573 * @bin: The uridecodebin.
575 * @caps: The #GstCaps found.
577 * This function is emitted when an array of possible factories for @caps on
578 * @pad is needed. Uridecodebin will by default return an array with all
579 * compatible factories, sorted by rank.
581 * If this function returns NULL, @pad will be exposed as a final caps.
583 * If this function returns an empty array, the pad will be considered as
584 * having an unhandled type media type.
586 * > Only the signal handler that is connected first will ever by invoked.
587 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
588 * > signal, they will never be invoked!
590 * Returns: a #GValueArray* with a list of factories to try. The factories are
591 * by default tried in the returned order or based on the index returned by
594 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
595 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
596 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
597 autoplug_factories), _gst_array_accumulator, NULL,
598 NULL, G_TYPE_VALUE_ARRAY, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
601 * GstURIDecodeBin::autoplug-sort:
602 * @bin: The uridecodebin.
604 * @caps: The #GstCaps.
605 * @factories: A #GValueArray of possible #GstElementFactory to use.
607 * Once decodebin has found the possible #GstElementFactory objects to try
608 * for @caps on @pad, this signal is emitted. The purpose of the signal is for
609 * the application to perform additional sorting or filtering on the element
612 * The callee should copy and modify @factories or return %NULL if the
613 * order should not change.
615 * > Invocation of signal handlers stops after one signal handler has
616 * > returned something else than %NULL. Signal handlers are invoked in
617 * > the order they were connected in.
618 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
619 * > signal, they will never be invoked!
621 * Returns: A new sorted array of #GstElementFactory objects.
623 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
624 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
625 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_sort),
626 _gst_array_hasvalue_accumulator, NULL,
627 NULL, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
628 G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
631 * GstURIDecodeBin::autoplug-select:
632 * @bin: The uridecodebin.
634 * @caps: The #GstCaps.
635 * @factory: A #GstElementFactory to use.
637 * This signal is emitted once uridecodebin has found all the possible
638 * #GstElementFactory that can be used to handle the given @caps. For each of
639 * those factories, this signal is emitted.
641 * The signal handler should return a #GstAutoplugSelectResult enum
642 * value indicating what decodebin should do next.
644 * A value of #GstAutoplugSelectResult::try will try to autoplug an element from
647 * A value of #GstAutoplugSelectResult::expose will expose @pad without plugging
650 * A value of #GstAutoplugSelectResult::skip will skip @factory and move to the
653 * > The signal handler will not be invoked if any of the previously
654 * > registered signal handlers (if any) return a value other than
655 * > GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
656 * > GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
657 * > registered next (again, if any) can override that decision.
659 * Returns: a #GstAutoplugSelectResult that indicates the required
660 * operation. The default handler will always return
661 * #GstAutoplugSelectResult::try.
663 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
664 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
665 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
666 autoplug_select), _gst_select_accumulator, NULL,
667 NULL, GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
668 GST_TYPE_ELEMENT_FACTORY);
671 * GstDecodeBin::autoplug-query:
672 * @bin: The decodebin.
673 * @child: The child element doing the query
675 * @query: The #GstQuery.
677 * This signal is emitted whenever an autoplugged element that is
678 * not linked downstream yet and not exposed does a query. It can
679 * be used to tell the element about the downstream supported caps
682 * Returns: %TRUE if the query was handled, %FALSE otherwise.
684 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
685 g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
686 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_query),
687 _gst_boolean_or_accumulator, NULL, NULL, G_TYPE_BOOLEAN, 3, GST_TYPE_PAD,
688 GST_TYPE_ELEMENT, GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
691 * GstURIDecodeBin::drained:
693 * This signal is emitted when the data for the current uri is played.
695 gst_uri_decode_bin_signals[SIGNAL_DRAINED] =
696 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
698 G_STRUCT_OFFSET (GstURIDecodeBinClass, drained), NULL, NULL, NULL,
699 G_TYPE_NONE, 0, G_TYPE_NONE);
702 * GstURIDecodeBin::source-setup:
703 * @bin: the uridecodebin.
704 * @source: source element
706 * This signal is emitted after the source element has been created, so
707 * it can be configured by setting additional properties (e.g. set a
708 * proxy server for an http source, or set the device and read speed for
709 * an audio cd source). This is functionally equivalent to connecting to
710 * the notify::source signal, but more convenient.
712 gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP] =
713 g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
714 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
716 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
717 gst_element_class_set_static_metadata (gstelement_class,
718 "URI Decoder", "Generic/Bin/Decoder",
719 "Autoplug and decode an URI to raw media",
720 "Wim Taymans <wim.taymans@gmail.com>");
722 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_query);
723 gstelement_class->change_state =
724 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_change_state);
726 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);
728 klass->autoplug_continue =
729 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
730 klass->autoplug_factories =
731 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
732 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_sort);
733 klass->autoplug_select =
734 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_select);
735 klass->autoplug_query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_query);
739 gst_uri_decode_bin_init (GstURIDecodeBin * dec)
741 /* first filter out the interesting element factories */
742 g_mutex_init (&dec->factories_lock);
744 g_mutex_init (&dec->lock);
746 dec->uri = g_strdup (DEFAULT_PROP_URI);
747 dec->connection_speed = DEFAULT_CONNECTION_SPEED;
748 dec->caps = DEFAULT_CAPS;
749 dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
751 dec->buffer_duration = DEFAULT_BUFFER_DURATION;
752 dec->buffer_size = DEFAULT_BUFFER_SIZE;
753 dec->download = DEFAULT_DOWNLOAD;
754 dec->use_buffering = DEFAULT_USE_BUFFERING;
755 dec->force_sw_decoders = DEFAULT_FORCE_SW_DECODERS;
756 dec->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
757 dec->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
759 GST_OBJECT_FLAG_SET (dec, GST_ELEMENT_FLAG_SOURCE);
760 gst_bin_set_suppressed_flags (GST_BIN (dec),
761 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
765 gst_uri_decode_bin_finalize (GObject * obj)
767 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (obj);
769 remove_decoders (dec, TRUE);
770 g_mutex_clear (&dec->lock);
771 g_mutex_clear (&dec->factories_lock);
773 g_free (dec->encoding);
775 gst_plugin_feature_list_free (dec->factories);
777 gst_caps_unref (dec->caps);
779 G_OBJECT_CLASS (parent_class)->finalize (obj);
783 gst_uri_decode_bin_set_encoding (GstURIDecodeBin * dec, const gchar * encoding)
787 GST_URI_DECODE_BIN_LOCK (dec);
789 /* set property first */
790 GST_OBJECT_LOCK (dec);
791 g_free (dec->encoding);
792 dec->encoding = g_strdup (encoding);
793 GST_OBJECT_UNLOCK (dec);
795 /* set the property on all decodebins now */
796 for (walk = dec->decodebins; walk; walk = g_slist_next (walk)) {
797 GObject *decodebin = G_OBJECT (walk->data);
799 g_object_set (decodebin, "subtitle-encoding", encoding, NULL);
801 GST_URI_DECODE_BIN_UNLOCK (dec);
805 gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
806 const GValue * value, GParamSpec * pspec)
808 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
812 GST_OBJECT_LOCK (dec);
814 dec->uri = g_value_dup_string (value);
815 GST_OBJECT_UNLOCK (dec);
817 case PROP_CONNECTION_SPEED:
818 GST_OBJECT_LOCK (dec);
819 dec->connection_speed = g_value_get_uint64 (value) * 1000;
820 GST_OBJECT_UNLOCK (dec);
823 GST_OBJECT_LOCK (dec);
825 gst_caps_unref (dec->caps);
826 dec->caps = g_value_dup_boxed (value);
827 GST_OBJECT_UNLOCK (dec);
829 case PROP_SUBTITLE_ENCODING:
830 gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
832 case PROP_BUFFER_SIZE:
833 dec->buffer_size = g_value_get_int (value);
835 case PROP_BUFFER_DURATION:
836 dec->buffer_duration = g_value_get_int64 (value);
839 dec->download = g_value_get_boolean (value);
841 case PROP_USE_BUFFERING:
842 dec->use_buffering = g_value_get_boolean (value);
844 case PROP_FORCE_SW_DECODERS:
845 dec->force_sw_decoders = g_value_get_boolean (value);
847 case PROP_EXPOSE_ALL_STREAMS:
848 dec->expose_allstreams = g_value_get_boolean (value);
850 case PROP_RING_BUFFER_MAX_SIZE:
851 dec->ring_buffer_max_size = g_value_get_uint64 (value);
854 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
860 gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
861 GValue * value, GParamSpec * pspec)
863 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
867 GST_OBJECT_LOCK (dec);
868 g_value_set_string (value, dec->uri);
869 GST_OBJECT_UNLOCK (dec);
872 GST_OBJECT_LOCK (dec);
873 g_value_set_object (value, dec->source);
874 GST_OBJECT_UNLOCK (dec);
876 case PROP_CONNECTION_SPEED:
877 GST_OBJECT_LOCK (dec);
878 g_value_set_uint64 (value, dec->connection_speed / 1000);
879 GST_OBJECT_UNLOCK (dec);
882 GST_OBJECT_LOCK (dec);
883 g_value_set_boxed (value, dec->caps);
884 GST_OBJECT_UNLOCK (dec);
886 case PROP_SUBTITLE_ENCODING:
887 GST_OBJECT_LOCK (dec);
888 g_value_set_string (value, dec->encoding);
889 GST_OBJECT_UNLOCK (dec);
891 case PROP_BUFFER_SIZE:
892 GST_OBJECT_LOCK (dec);
893 g_value_set_int (value, dec->buffer_size);
894 GST_OBJECT_UNLOCK (dec);
896 case PROP_BUFFER_DURATION:
897 GST_OBJECT_LOCK (dec);
898 g_value_set_int64 (value, dec->buffer_duration);
899 GST_OBJECT_UNLOCK (dec);
902 g_value_set_boolean (value, dec->download);
904 case PROP_USE_BUFFERING:
905 g_value_set_boolean (value, dec->use_buffering);
907 case PROP_FORCE_SW_DECODERS:
908 g_value_set_boolean (value, dec->force_sw_decoders);
910 case PROP_EXPOSE_ALL_STREAMS:
911 g_value_set_boolean (value, dec->expose_allstreams);
913 case PROP_RING_BUFFER_MAX_SIZE:
914 g_value_set_uint64 (value, dec->ring_buffer_max_size);
917 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
923 do_async_start (GstURIDecodeBin * dbin)
927 dbin->async_pending = TRUE;
929 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
930 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (dbin), message);
934 do_async_done (GstURIDecodeBin * dbin)
938 if (dbin->async_pending) {
939 GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
941 gst_message_new_async_done (GST_OBJECT_CAST (dbin),
942 GST_CLOCK_TIME_NONE);
943 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (dbin), message);
945 dbin->async_pending = FALSE;
949 #define DEFAULT_QUEUE_SIZE (3 * GST_SECOND)
950 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
951 #define DEFAULT_QUEUE_THRESHOLD ((DEFAULT_QUEUE_SIZE * 95) / 100)
954 unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
955 GstURIDecodeBin * decoder)
959 capsstr = gst_caps_to_string (caps);
960 GST_ELEMENT_WARNING (decoder, STREAM, CODEC_NOT_FOUND,
961 (_("No decoder available for type \'%s\'."), capsstr), (NULL));
965 /* add a streaminfo that indicates that the stream is handled by the
966 * given element. This usually means that a stream without actual data is
967 * produced but one that is sunken by an element. Examples of this are:
968 * cdaudio, a hardware decoder/sink, dvd meta bins etc...
971 add_element_stream (GstElement * element, GstURIDecodeBin * decoder)
973 g_warning ("add element stream");
976 /* when the decoder element signals that no more pads will be generated, we
977 * can commit the current group.
980 no_more_pads_full (GstElement * element, gboolean subs,
981 GstURIDecodeBin * decoder)
986 GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);
988 GST_URI_DECODE_BIN_LOCK (decoder);
989 final = (decoder->pending == 0);
991 /* nothing pending, we can exit */
995 /* the object has no pending no_more_pads */
996 if (!g_object_get_data (G_OBJECT (element), "pending"))
998 g_object_set_data (G_OBJECT (element), "pending", NULL);
1001 final = (decoder->pending == 0);
1004 GST_URI_DECODE_BIN_UNLOCK (decoder);
1007 /* If we got not a single stream yet, that means that all
1008 * decodebins had missing plugins for all of their streams!
1010 if (!decoder->streams || g_hash_table_size (decoder->streams) == 0) {
1011 if (decoder->missing_plugin_errors) {
1012 GString *str = g_string_new ("");
1015 for (l = decoder->missing_plugin_errors; l; l = l->next) {
1016 GstMessage *msg = l->data;
1019 gst_message_parse_error (msg, NULL, &debug);
1020 g_string_append (str, debug);
1022 gst_message_unref (msg);
1024 g_list_free (decoder->missing_plugin_errors);
1025 decoder->missing_plugin_errors = NULL;
1027 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1028 ("no suitable plugins found:\n%s", str->str));
1029 g_string_free (str, TRUE);
1031 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1032 ("no suitable plugins found"));
1035 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
1037 do_async_done (decoder);
1044 no_more_pads (GstElement * element, GstURIDecodeBin * decoder)
1046 no_more_pads_full (element, FALSE, decoder);
1050 source_no_more_pads (GstElement * element, GstURIDecodeBin * bin)
1052 GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
1053 GST_ELEMENT_NAME (element));
1055 g_signal_handler_disconnect (element, bin->src_np_sig_id);
1056 bin->src_np_sig_id = 0;
1057 g_signal_handler_disconnect (element, bin->src_nmp_sig_id);
1058 bin->src_nmp_sig_id = 0;
1060 no_more_pads_full (element, FALSE, bin);
1064 configure_stream_buffering (GstURIDecodeBin * decoder)
1066 GstElement *queue = NULL;
1067 GHashTableIter iter;
1068 gpointer key, value;
1071 /* automatic configuration enabled ? */
1072 if (decoder->buffer_size != -1)
1075 GST_URI_DECODE_BIN_LOCK (decoder);
1077 queue = gst_object_ref (decoder->queue);
1079 g_hash_table_iter_init (&iter, decoder->streams);
1080 while (g_hash_table_iter_next (&iter, &key, &value)) {
1081 GstURIDecodeBinStream *stream = value;
1083 if (stream->bitrate && bitrate >= 0)
1084 bitrate += stream->bitrate;
1088 GST_URI_DECODE_BIN_UNLOCK (decoder);
1090 GST_DEBUG_OBJECT (decoder, "overall bitrate %d", bitrate);
1098 /* all streams have a bitrate;
1099 * configure queue size based on queue duration using combined bitrate */
1100 g_object_get (queue, "max-size-time", &time, NULL);
1101 GST_DEBUG_OBJECT (decoder, "queue buffering time %" GST_TIME_FORMAT,
1102 GST_TIME_ARGS (time));
1104 bytes = gst_util_uint64_scale (time, bitrate, 8 * GST_SECOND);
1105 GST_DEBUG_OBJECT (decoder, "corresponds to buffer size %d", bytes);
1106 g_object_set (queue, "max-size-bytes", bytes, NULL);
1110 gst_object_unref (queue);
1113 static GstPadProbeReturn
1114 decoded_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
1117 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1118 GstURIDecodeBin *decoder = user_data;
1120 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1122 /* look for a bitrate tag */
1123 switch (GST_EVENT_TYPE (event)) {
1128 GstURIDecodeBinStream *stream;
1130 gst_event_parse_tag (event, &list);
1131 if (!gst_tag_list_get_uint_index (list, GST_TAG_NOMINAL_BITRATE, 0,
1133 gst_tag_list_get_uint_index (list, GST_TAG_BITRATE, 0, &bitrate);
1135 GST_DEBUG_OBJECT (pad, "found bitrate %u", bitrate);
1137 GST_URI_DECODE_BIN_LOCK (decoder);
1138 stream = g_hash_table_lookup (decoder->streams, pad);
1139 GST_URI_DECODE_BIN_UNLOCK (decoder);
1141 stream->bitrate = bitrate;
1142 /* no longer need this probe now */
1143 gst_pad_remove_probe (pad, stream->probe_id);
1144 /* configure buffer if possible */
1145 configure_stream_buffering (decoder);
1155 return GST_PAD_PROBE_OK;
1160 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1162 GstPad *gpad = GST_PAD_CAST (user_data);
1164 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
1165 gst_pad_store_sticky_event (gpad, *event);
1170 /* Called by the signal handlers when a decodebin has found a new raw pad */
1172 new_decoded_pad_added_cb (GstElement * element, GstPad * pad,
1173 GstURIDecodeBin * decoder)
1176 GstPadTemplate *pad_tmpl;
1178 GstURIDecodeBinStream *stream;
1180 GST_DEBUG_OBJECT (element, "new decoded pad, name: <%s>", GST_PAD_NAME (pad));
1182 GST_URI_DECODE_BIN_LOCK (decoder);
1183 padname = g_strdup_printf ("src_%u", decoder->numpads);
1185 GST_URI_DECODE_BIN_UNLOCK (decoder);
1187 pad_tmpl = gst_static_pad_template_get (&srctemplate);
1188 newpad = gst_ghost_pad_new_from_template (padname, pad, pad_tmpl);
1189 gst_object_unref (pad_tmpl);
1192 /* store ref to the ghostpad so we can remove it */
1193 g_object_set_data (G_OBJECT (pad), "uridecodebin.ghostpad", newpad);
1195 /* add event probe to monitor tags */
1196 stream = g_slice_alloc0 (sizeof (GstURIDecodeBinStream));
1198 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1199 decoded_pad_event_probe, decoder, NULL);
1200 GST_URI_DECODE_BIN_LOCK (decoder);
1201 g_hash_table_insert (decoder->streams, pad, stream);
1202 GST_URI_DECODE_BIN_UNLOCK (decoder);
1204 gst_pad_set_active (newpad, TRUE);
1205 gst_pad_sticky_events_foreach (pad, copy_sticky_events, newpad);
1206 gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
1209 static GstPadProbeReturn
1210 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
1213 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1214 GstURIDecodeBin *decoder = user_data;
1216 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1218 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1219 GST_DEBUG_OBJECT (pad, "we received EOS");
1221 g_signal_emit (decoder,
1222 gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1224 /* never drop events */
1225 return GST_PAD_PROBE_OK;
1228 /* called when we found a raw pad on the source element. We need to set up a
1229 * padprobe to detect EOS before exposing the pad. */
1231 expose_decoded_pad (GstElement * element, GstPad * pad,
1232 GstURIDecodeBin * decoder)
1234 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1235 source_pad_event_probe, decoder, NULL);
1237 new_decoded_pad_added_cb (element, pad, decoder);
1241 pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
1245 GST_DEBUG_OBJECT (element, "pad removed name: <%s:%s>",
1246 GST_DEBUG_PAD_NAME (pad));
1248 /* we only care about srcpads */
1249 if (!GST_PAD_IS_SRC (pad))
1252 if (!(ghost = g_object_get_data (G_OBJECT (pad), "uridecodebin.ghostpad")))
1255 /* unghost the pad */
1256 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ghost), NULL);
1258 /* deactivate and remove */
1259 gst_pad_set_active (pad, FALSE);
1260 gst_element_remove_pad (GST_ELEMENT_CAST (decoder), ghost);
1267 GST_WARNING_OBJECT (element, "no ghost pad found");
1272 /* helper function to lookup stuff in lists */
1274 array_has_value (const gchar * values[], const gchar * value)
1278 for (i = 0; values[i]; i++) {
1279 if (g_str_has_prefix (value, values[i]))
1286 array_has_uri_value (const gchar * values[], const gchar * value)
1290 for (i = 0; values[i]; i++) {
1291 if (!g_ascii_strncasecmp (value, values[i], strlen (values[i])))
1297 /* list of URIs that we consider to be streams and that need buffering.
1298 * We have no mechanism yet to figure this out with a query. */
1299 static const gchar *stream_uris[] = { "http://", "https://", "mms://",
1300 "mmsh://", "mmsu://", "mmst://", "fd://", "myth://", "ssh://",
1301 "ftp://", "sftp://",
1305 /* list of URIs that need a queue because they are pretty bursty */
1306 static const gchar *queue_uris[] = { "cdda://", NULL };
1308 /* blacklisted URIs, we know they will always fail. */
1309 static const gchar *blacklisted_uris[] = { NULL };
1311 /* media types that use adaptive streaming */
1312 static const gchar *adaptive_media[] = {
1313 "application/x-hls", "application/vnd.ms-sstr+xml",
1314 "application/dash+xml", NULL
1317 #define IS_STREAM_URI(uri) (array_has_uri_value (stream_uris, uri))
1318 #define IS_QUEUE_URI(uri) (array_has_uri_value (queue_uris, uri))
1319 #define IS_BLACKLISTED_URI(uri) (array_has_uri_value (blacklisted_uris, uri))
1320 #define IS_ADAPTIVE_MEDIA(media) (array_has_value (adaptive_media, media))
1323 * Generate and configure a source element.
1325 * Returns: (transfer full): a new #GstElement
1328 gen_source_element (GstURIDecodeBin * decoder)
1330 GObjectClass *source_class;
1334 GstSchedulingFlags flags;
1340 GST_LOG_OBJECT (decoder, "finding source for %s", decoder->uri);
1342 if (!gst_uri_is_valid (decoder->uri))
1345 if (IS_BLACKLISTED_URI (decoder->uri))
1346 goto uri_blacklisted;
1349 gst_element_make_from_uri (GST_URI_SRC, decoder->uri, "source", &err);
1353 GST_LOG_OBJECT (decoder, "found source type %s", G_OBJECT_TYPE_NAME (source));
1355 source_class = G_OBJECT_GET_CLASS (source);
1357 pspec = g_object_class_find_property (source_class, "connection-speed");
1358 if (pspec != NULL) {
1359 guint64 speed = decoder->connection_speed / 1000;
1360 gboolean wrong_type = FALSE;
1362 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
1363 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
1365 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1366 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
1367 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
1369 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1370 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
1371 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
1373 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1374 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
1375 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
1377 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1379 GST_WARNING_OBJECT (decoder,
1380 "The connection speed property %" G_GUINT64_FORMAT
1381 " of type %s is not useful not setting it", speed,
1382 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
1387 g_object_set (source, "connection-speed", speed, NULL);
1389 GST_DEBUG_OBJECT (decoder,
1390 "setting connection-speed=%" G_GUINT64_FORMAT " to source element",
1395 pspec = g_object_class_find_property (source_class, "subtitle-encoding");
1396 if (pspec != NULL && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
1397 GST_DEBUG_OBJECT (decoder,
1398 "setting subtitle-encoding=%s to source element", decoder->encoding);
1399 g_object_set (source, "subtitle-encoding", decoder->encoding, NULL);
1402 /* Sink reference before passing it to signal handler.
1403 * Language binding might otherwise sink it and then unref.
1404 * A floating ref is also tricky for a native signal handler in case
1405 * of a "transfer floating" call followed by unref
1406 * (e.g. some container_add and then container_remove).
1407 * Bottom line; best not have a floating ref boldly going into unknown code.
1409 g_object_ref_sink (source);
1411 g_signal_emit (decoder, gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP],
1414 decoder->is_stream = IS_STREAM_URI (decoder->uri);
1416 query = gst_query_new_scheduling ();
1417 if (gst_element_query (source, query)) {
1418 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
1419 if ((flags & GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED))
1420 decoder->is_stream = TRUE;
1422 gst_query_unref (query);
1424 GST_LOG_OBJECT (decoder, "source is stream: %d", decoder->is_stream);
1426 decoder->need_queue = IS_QUEUE_URI (decoder->uri);
1427 GST_LOG_OBJECT (decoder, "source needs queue: %d", decoder->need_queue);
1434 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1435 (_("No URI specified to play from.")), (NULL));
1440 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1441 (_("Invalid URI \"%s\"."), decoder->uri), (NULL));
1442 g_clear_error (&err);
1447 GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
1448 (_("This stream type cannot be played yet.")), (NULL));
1453 /* whoops, could not create the source element, dig a little deeper to
1454 * figure out what might be wrong. */
1455 if (err != NULL && err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL) {
1458 prot = gst_uri_get_protocol (decoder->uri);
1462 gst_element_post_message (GST_ELEMENT_CAST (decoder),
1463 gst_missing_uri_source_message_new (GST_ELEMENT (decoder), prot));
1465 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
1466 (_("No URI handler implemented for \"%s\"."), prot), (NULL));
1470 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1471 ("%s", (err) ? err->message : "URI was not accepted by any element"),
1472 ("No element accepted URI '%s'", decoder->uri));
1475 g_clear_error (&err);
1483 * @all_raw: pointer to hold the result
1485 * check if the caps of the pad are all raw. The caps are all raw if
1486 * all of its structures contain audio/x-raw or video/x-raw.
1488 * Returns: %FALSE @pad caps are not set and raw
1491 has_raw_caps (GstPad * pad, GstCaps * rawcaps)
1493 GstCaps *caps, *intersection;
1495 gboolean res = FALSE;
1497 caps = gst_pad_get_current_caps (pad);
1501 GST_DEBUG_OBJECT (pad, "have caps %" GST_PTR_FORMAT, caps);
1503 capssize = gst_caps_get_size (caps);
1504 /* no caps, skip and move to the next pad */
1505 if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1508 intersection = gst_caps_intersect (caps, rawcaps);
1509 res = !gst_caps_is_empty (intersection);
1510 gst_caps_unref (intersection);
1513 gst_caps_unref (caps);
1518 post_missing_plugin_error (GstElement * dec, const gchar * element_name)
1522 msg = gst_missing_element_message_new (dec, element_name);
1523 gst_element_post_message (dec, msg);
1525 GST_ELEMENT_ERROR (dec, CORE, MISSING_PLUGIN,
1526 (_("Missing element '%s' - check your GStreamer installation."),
1527 element_name), (NULL));
1528 do_async_done (GST_URI_DECODE_BIN (dec));
1533 * @decoder: a #GstURIDecodeBin
1534 * @is_raw: are all pads raw data
1535 * @have_out: does the source have output
1536 * @is_dynamic: is this a dynamic source
1537 * @use_queue: put a queue before raw output pads
1539 * Check the source of @decoder and collect information about it.
1541 * @is_raw will be set to TRUE if the source only produces raw pads. When this
1542 * function returns, all of the raw pad of the source will be added
1545 * @have_out: will be set to TRUE if the source has output pads.
1547 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1550 * Returns: FALSE if a fatal error occurred while scanning.
1553 analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
1554 gboolean * have_out, gboolean * is_dynamic, gboolean use_queue)
1556 GstIterator *pads_iter;
1557 gboolean done = FALSE;
1558 gboolean res = TRUE;
1561 GValue item = { 0, };
1565 *is_dynamic = FALSE;
1567 g_object_get (decoder, "caps", &rawcaps, NULL);
1569 rawcaps = DEFAULT_CAPS;
1571 pads_iter = gst_element_iterate_src_pads (decoder->source);
1573 switch (gst_iterator_next (pads_iter, &item)) {
1574 case GST_ITERATOR_ERROR:
1577 case GST_ITERATOR_DONE:
1580 case GST_ITERATOR_RESYNC:
1581 /* reset results and resync */
1584 *is_dynamic = FALSE;
1585 gst_iterator_resync (pads_iter);
1587 case GST_ITERATOR_OK:
1588 pad = g_value_dup_object (&item);
1589 /* we now officially have an output pad */
1592 /* if FALSE, this pad has no caps and we continue with the next pad. */
1593 if (!has_raw_caps (pad, rawcaps)) {
1594 gst_object_unref (pad);
1595 g_value_reset (&item);
1598 /* caps on source pad are all raw, we can add the pad */
1599 GstElement *outelem;
1605 /* insert a queue element right before the raw pad */
1606 outelem = gst_element_factory_make ("queue2", NULL);
1610 gst_bin_add (GST_BIN_CAST (decoder), outelem);
1612 sinkpad = gst_element_get_static_pad (outelem, "sink");
1613 gst_pad_link (pad, sinkpad);
1614 gst_object_unref (sinkpad);
1616 /* save queue pointer so we can remove it later */
1617 decoder->queue = outelem;
1619 /* get the new raw srcpad */
1620 gst_object_unref (pad);
1621 pad = gst_element_get_static_pad (outelem, "src");
1623 outelem = decoder->source;
1625 expose_decoded_pad (outelem, pad, decoder);
1627 gst_object_unref (pad);
1628 g_value_reset (&item);
1632 g_value_unset (&item);
1633 gst_iterator_free (pads_iter);
1634 gst_caps_unref (rawcaps);
1637 GstElementClass *elemclass;
1640 /* element has no output pads, check for padtemplates that list SOMETIMES
1642 elemclass = GST_ELEMENT_GET_CLASS (decoder->source);
1644 walk = gst_element_class_get_pad_template_list (elemclass);
1645 while (walk != NULL) {
1646 GstPadTemplate *templ;
1648 templ = (GstPadTemplate *) walk->data;
1649 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1650 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES)
1654 walk = g_list_next (walk);
1661 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1663 gst_object_unref (pad);
1664 g_value_unset (&item);
1665 gst_iterator_free (pads_iter);
1666 gst_caps_unref (rawcaps);
1672 /* Remove all decodebin from ourself
1673 * If force is FALSE, then the decodebin instances will be stored in
1674 * pending_decodebins for re-use later on.
1675 * If force is TRUE, then all decodebin instances will be unreferenced
1676 * and cleared, including the pending ones. */
1678 remove_decoders (GstURIDecodeBin * bin, gboolean force)
1682 for (walk = bin->decodebins; walk; walk = g_slist_next (walk)) {
1683 GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1685 GST_DEBUG_OBJECT (bin, "removing old decoder element");
1687 /* Even if we reuse this decodebin, the previous topology will
1689 g_object_set_data (G_OBJECT (decoder), "uridecodebin-topology", NULL);
1692 gst_element_set_state (decoder, GST_STATE_NULL);
1693 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1697 gst_element_set_state (decoder, GST_STATE_READY);
1698 /* add it to our list of pending decodebins */
1699 gst_object_ref (decoder);
1700 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1701 /* restore some properties we might have changed */
1702 g_object_set (decoder, "sink-caps", NULL, NULL);
1703 caps = DEFAULT_CAPS;
1704 g_object_set (decoder, "caps", caps, NULL);
1705 gst_caps_unref (caps);
1707 bin->pending_decodebins =
1708 g_slist_prepend (bin->pending_decodebins, decoder);
1711 g_slist_free (bin->decodebins);
1712 bin->decodebins = NULL;
1716 for (tmp = bin->pending_decodebins; tmp; tmp = tmp->next) {
1717 gst_element_set_state ((GstElement *) tmp->data, GST_STATE_NULL);
1718 gst_object_unref ((GstElement *) tmp->data);
1720 g_slist_free (bin->pending_decodebins);
1721 bin->pending_decodebins = NULL;
1727 proxy_unknown_type_signal (GstElement * decodebin, GstPad * pad, GstCaps * caps,
1728 GstURIDecodeBin * dec)
1730 GST_DEBUG_OBJECT (dec, "unknown-type signaled");
1733 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1737 proxy_autoplug_continue_signal (GstElement * decodebin, GstPad * pad,
1738 GstCaps * caps, GstURIDecodeBin * dec)
1743 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, pad, caps,
1746 GST_DEBUG_OBJECT (dec, "autoplug-continue returned %d", result);
1751 static GValueArray *
1752 proxy_autoplug_factories_signal (GstElement * decodebin, GstPad * pad,
1753 GstCaps * caps, GstURIDecodeBin * dec)
1755 GValueArray *result;
1758 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, pad, caps,
1761 GST_DEBUG_OBJECT (dec, "autoplug-factories returned %p", result);
1766 static GValueArray *
1767 proxy_autoplug_sort_signal (GstElement * decodebin, GstPad * pad,
1768 GstCaps * caps, GValueArray * factories, GstURIDecodeBin * dec)
1770 GValueArray *result;
1773 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, pad, caps,
1774 factories, &result);
1776 GST_DEBUG_OBJECT (dec, "autoplug-sort returned %p", result);
1781 static GstAutoplugSelectResult
1782 proxy_autoplug_select_signal (GstElement * decodebin, GstPad * pad,
1783 GstCaps * caps, GstElementFactory * factory, GstURIDecodeBin * dec)
1785 GstAutoplugSelectResult result;
1788 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, factory,
1791 GST_DEBUG_OBJECT (dec, "autoplug-select returned %d", result);
1797 proxy_autoplug_query_signal (GstElement * decodebin, GstPad * pad,
1798 GstElement * element, GstQuery * query, GstURIDecodeBin * dec)
1800 gboolean ret = FALSE;
1803 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY], 0, pad, element, query,
1806 GST_DEBUG_OBJECT (dec, "autoplug-query returned %d", ret);
1812 proxy_drained_signal (GstElement * decodebin, GstURIDecodeBin * dec)
1814 GST_DEBUG_OBJECT (dec, "drained signaled");
1816 g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1819 /* make a decodebin and connect to all the signals */
1821 make_decoder (GstURIDecodeBin * decoder)
1823 GstElement *decodebin;
1824 gboolean unref_dbin = FALSE;
1826 /* re-use pending decodebin */
1827 if (decoder->pending_decodebins) {
1828 GSList *first = decoder->pending_decodebins;
1829 GST_LOG_OBJECT (decoder, "re-using pending decodebin");
1830 decodebin = (GstElement *) first->data;
1831 decoder->pending_decodebins =
1832 g_slist_delete_link (decoder->pending_decodebins, first);
1835 GST_LOG_OBJECT (decoder, "making new decodebin");
1837 /* now create the decoder element */
1838 decodebin = gst_element_factory_make ("decodebin", NULL);
1844 if (decodebin->numsinkpads == 0)
1847 /* connect signals to proxy */
1848 g_signal_connect (decodebin, "unknown-type",
1849 G_CALLBACK (proxy_unknown_type_signal), decoder);
1850 g_signal_connect (decodebin, "autoplug-continue",
1851 G_CALLBACK (proxy_autoplug_continue_signal), decoder);
1852 g_signal_connect (decodebin, "autoplug-factories",
1853 G_CALLBACK (proxy_autoplug_factories_signal), decoder);
1854 g_signal_connect (decodebin, "autoplug-sort",
1855 G_CALLBACK (proxy_autoplug_sort_signal), decoder);
1856 g_signal_connect (decodebin, "autoplug-select",
1857 G_CALLBACK (proxy_autoplug_select_signal), decoder);
1858 g_signal_connect (decodebin, "autoplug-query",
1859 G_CALLBACK (proxy_autoplug_query_signal), decoder);
1860 g_signal_connect (decodebin, "drained",
1861 G_CALLBACK (proxy_drained_signal), decoder);
1863 /* set up callbacks to create the links between decoded data
1864 * and video/audio/subtitle rendering/output. */
1865 g_signal_connect (decodebin,
1866 "pad-added", G_CALLBACK (new_decoded_pad_added_cb), decoder);
1867 g_signal_connect (decodebin,
1868 "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
1869 g_signal_connect (decodebin, "no-more-pads",
1870 G_CALLBACK (no_more_pads), decoder);
1871 g_signal_connect (decodebin,
1872 "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
1875 g_object_set (decodebin, "force-sw-decoders", decoder->force_sw_decoders,
1878 /* configure caps if we have any */
1880 g_object_set (decodebin, "caps", decoder->caps, NULL);
1882 /* Propagate expose-all-streams and connection-speed properties */
1883 g_object_set (decodebin, "expose-all-streams", decoder->expose_allstreams,
1884 "connection-speed", decoder->connection_speed / 1000, NULL);
1886 if (!decoder->is_stream || decoder->is_adaptive) {
1887 /* propagate the use-buffering property but only when we are not already
1888 * doing stream buffering with queue2. FIXME, we might want to do stream
1889 * buffering with the multiqueue buffering instead of queue2. */
1890 g_object_set (decodebin, "use-buffering", decoder->use_buffering
1891 || decoder->is_adaptive, NULL);
1893 if (decoder->use_buffering || decoder->is_adaptive) {
1897 /* configure sizes when buffering */
1898 if ((max_bytes = decoder->buffer_size) == -1)
1899 max_bytes = 2 * 1024 * 1024;
1900 if ((max_time = decoder->buffer_duration) == -1)
1901 max_time = 5 * GST_SECOND;
1903 g_object_set (decodebin, "max-size-bytes", max_bytes, "max-size-buffers",
1904 (guint) 0, "max-size-time", max_time, NULL);
1908 g_object_set_data (G_OBJECT (decodebin), "pending", GINT_TO_POINTER (1));
1909 g_object_set (decodebin, "subtitle-encoding", decoder->encoding, NULL);
1911 GST_LOG_OBJECT (decoder, "have %d pending dynamic objects", decoder->pending);
1913 gst_bin_add (GST_BIN_CAST (decoder), decodebin);
1915 decoder->decodebins = g_slist_prepend (decoder->decodebins, decodebin);
1916 /* Unref if this decodebin came from our pending_decodebins,
1917 * since we were holding strong reference to decodebin and gst_bin_add()
1918 * will increase refcount */
1920 gst_object_unref (decodebin);
1927 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "decodebin");
1928 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1929 ("No decodebin element, check your installation"));
1930 do_async_done (decoder);
1935 gst_object_unref (decodebin);
1936 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1937 ("No typefind element, decodebin is unusable, check your installation"));
1938 do_async_done (decoder);
1943 /* signaled when we have a stream and we need to configure the download
1944 * buffering or regular buffering */
1946 type_found (GstElement * typefind, guint probability,
1947 GstCaps * caps, GstURIDecodeBin * decoder)
1949 GstElement *src_elem, *dec_elem, *queue = NULL;
1951 const gchar *media_type, *elem_name;
1952 gboolean do_download = FALSE;
1954 GST_DEBUG_OBJECT (decoder, "typefind found caps %" GST_PTR_FORMAT, caps);
1956 s = gst_caps_get_structure (caps, 0);
1957 media_type = gst_structure_get_name (s);
1959 decoder->is_adaptive = IS_ADAPTIVE_MEDIA (media_type);
1961 /* only enable download buffering if the upstream duration is known */
1962 if (decoder->download) {
1965 do_download = (gst_element_query_duration (typefind, GST_FORMAT_BYTES, &dur)
1969 dec_elem = make_decoder (decoder);
1973 if (decoder->is_adaptive) {
1974 src_elem = typefind;
1977 elem_name = "downloadbuffer";
1979 elem_name = "queue2";
1981 queue = gst_element_factory_make (elem_name, NULL);
1983 goto no_buffer_element;
1985 decoder->queue = queue;
1987 GST_DEBUG_OBJECT (decoder, "check media-type %s, %d", media_type,
1991 gchar *temp_template, *filename;
1992 const gchar *tmp_dir, *prgname;
1994 tmp_dir = g_get_user_cache_dir ();
1995 prgname = g_get_prgname ();
1996 if (prgname == NULL)
1997 prgname = "GStreamer";
1999 filename = g_strdup_printf ("%s-XXXXXX", prgname);
2001 /* build our filename */
2002 temp_template = g_build_filename (tmp_dir, filename, NULL);
2004 GST_DEBUG_OBJECT (decoder, "enable download buffering in %s (%s, %s, %s)",
2005 temp_template, tmp_dir, prgname, filename);
2007 /* configure progressive download for selected media types */
2008 g_object_set (queue, "temp-template", temp_template, NULL);
2011 g_free (temp_template);
2013 g_object_set (queue, "use-buffering", TRUE, NULL);
2014 g_object_set (queue, "ring-buffer-max-size",
2015 decoder->ring_buffer_max_size, NULL);
2016 /* Disable max-size-buffers */
2017 g_object_set (queue, "max-size-buffers", 0, NULL);
2020 /* If buffer size or duration are set, set them on the element */
2021 if (decoder->buffer_size != -1)
2022 g_object_set (queue, "max-size-bytes", decoder->buffer_size, NULL);
2023 if (decoder->buffer_duration != -1)
2024 g_object_set (queue, "max-size-time", decoder->buffer_duration, NULL);
2026 gst_bin_add (GST_BIN_CAST (decoder), queue);
2028 if (!gst_element_link_pads (typefind, "src", queue, "sink"))
2029 goto could_not_link;
2033 /* to force caps on the decodebin element and avoid reparsing stuff by
2034 * typefind. It also avoids a deadlock in the way typefind activates pads in
2035 * the state change */
2036 g_object_set (dec_elem, "sink-caps", caps, NULL);
2038 if (!gst_element_link_pads (src_elem, "src", dec_elem, "sink"))
2039 goto could_not_link;
2041 /* PLAYING in one go might fail (see bug #632782) */
2042 gst_element_set_state (dec_elem, GST_STATE_PAUSED);
2043 gst_element_sync_state_with_parent (dec_elem);
2045 gst_element_sync_state_with_parent (queue);
2052 /* error was posted */
2057 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2058 (NULL), ("Can't link typefind to decodebin element"));
2059 do_async_done (decoder);
2064 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), elem_name);
2069 /* setup a streaming source. This will first plug a typefind element to the
2070 * source. After we find the type, we decide to plug a queue2 and continue to
2071 * plug a decodebin starting from the found caps */
2073 setup_streaming (GstURIDecodeBin * decoder)
2075 GstElement *typefind;
2077 /* now create the decoder element */
2078 typefind = gst_element_factory_make ("typefind", NULL);
2082 gst_bin_add (GST_BIN_CAST (decoder), typefind);
2084 if (!gst_element_link_pads (decoder->source, NULL, typefind, "sink"))
2085 goto could_not_link;
2087 decoder->typefind = typefind;
2089 /* connect a signal to find out when the typefind element found
2091 decoder->have_type_id =
2092 g_signal_connect (decoder->typefind, "have-type",
2093 G_CALLBACK (type_found), decoder);
2100 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "typefind");
2101 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
2102 ("No typefind element, check your installation"));
2103 do_async_done (decoder);
2108 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2109 (NULL), ("Can't link source to typefind element"));
2110 gst_bin_remove (GST_BIN_CAST (decoder), typefind);
2111 do_async_done (decoder);
2117 free_stream (gpointer value)
2119 g_slice_free (GstURIDecodeBinStream, value);
2122 /* remove source and all related elements */
2124 remove_source (GstURIDecodeBin * bin)
2126 GstElement *source = bin->source;
2129 GST_DEBUG_OBJECT (bin, "removing old src element");
2130 gst_element_set_state (source, GST_STATE_NULL);
2132 if (bin->src_np_sig_id) {
2133 g_signal_handler_disconnect (source, bin->src_np_sig_id);
2134 bin->src_np_sig_id = 0;
2136 if (bin->src_nmp_sig_id) {
2137 g_signal_handler_disconnect (source, bin->src_nmp_sig_id);
2138 bin->src_nmp_sig_id = 0;
2140 GST_OBJECT_LOCK (bin);
2142 GST_OBJECT_UNLOCK (bin);
2143 gst_bin_remove (GST_BIN_CAST (bin), source);
2146 GST_DEBUG_OBJECT (bin, "removing old queue element");
2147 gst_element_set_state (bin->queue, GST_STATE_NULL);
2148 gst_bin_remove (GST_BIN_CAST (bin), bin->queue);
2151 if (bin->typefind) {
2152 GST_DEBUG_OBJECT (bin, "removing old typefind element");
2153 gst_element_set_state (bin->typefind, GST_STATE_NULL);
2154 gst_bin_remove (GST_BIN_CAST (bin), bin->typefind);
2155 bin->typefind = NULL;
2158 g_hash_table_destroy (bin->streams);
2159 bin->streams = NULL;
2163 /* is called when a dynamic source element created a new pad. */
2165 source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
2167 GstElement *decoder;
2171 GST_URI_DECODE_BIN_LOCK (bin);
2172 GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
2173 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
2175 g_object_get (bin, "caps", &rawcaps, NULL);
2177 rawcaps = DEFAULT_CAPS;
2179 /* if this is a pad with all raw caps, we can expose it */
2180 if (has_raw_caps (pad, rawcaps)) {
2181 /* it's all raw, create output pads. */
2182 GST_URI_DECODE_BIN_UNLOCK (bin);
2183 gst_caps_unref (rawcaps);
2184 expose_decoded_pad (element, pad, bin);
2187 gst_caps_unref (rawcaps);
2189 /* not raw, create decoder */
2190 decoder = make_decoder (bin);
2194 /* and link to decoder */
2195 sinkpad = gst_element_get_static_pad (decoder, "sink");
2196 if (gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)
2197 goto could_not_link;
2198 gst_object_unref (sinkpad);
2200 GST_DEBUG_OBJECT (bin, "linked decoder to new source pad");
2202 gst_element_sync_state_with_parent (decoder);
2203 GST_URI_DECODE_BIN_UNLOCK (bin);
2210 /* error was posted */
2211 GST_URI_DECODE_BIN_UNLOCK (bin);
2216 gst_object_unref (sinkpad);
2217 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
2218 (NULL), ("Can't link source to decoder element"));
2219 GST_URI_DECODE_BIN_UNLOCK (bin);
2220 do_async_done (bin);
2226 is_live_source (GstElement * source)
2228 GObjectClass *source_class = NULL;
2229 gboolean is_live = FALSE;
2232 source_class = G_OBJECT_GET_CLASS (source);
2233 pspec = g_object_class_find_property (source_class, "is-live");
2234 if (!pspec || G_PARAM_SPEC_VALUE_TYPE (pspec) != G_TYPE_BOOLEAN)
2237 g_object_get (G_OBJECT (source), "is-live", &is_live, NULL);
2242 /* construct and run the source and decoder elements until we found
2243 * all the streams or until a preroll queue has been filled.
2246 setup_source (GstURIDecodeBin * decoder)
2248 gboolean is_raw, have_out, is_dynamic;
2251 GST_DEBUG_OBJECT (decoder, "setup source");
2253 /* delete old src */
2254 remove_source (decoder);
2256 decoder->pending = 0;
2258 /* create and configure an element that can handle the uri */
2259 source = gen_source_element (decoder);
2260 GST_OBJECT_LOCK (decoder);
2261 if (!(decoder->source = source)) {
2262 GST_OBJECT_UNLOCK (decoder);
2266 GST_OBJECT_UNLOCK (decoder);
2268 /* state will be merged later - if file is not found, error will be
2269 * handled by the application right after. */
2270 gst_bin_add (GST_BIN_CAST (decoder), decoder->source);
2271 /* bin now has a ref, but the local reference is not counted */
2272 g_object_unref (decoder->source);
2274 /* notify of the new source used */
2275 g_object_notify (G_OBJECT (decoder), "source");
2277 if (is_live_source (decoder->source))
2278 decoder->is_stream = FALSE;
2280 /* remove the old decoders now, if any */
2281 remove_decoders (decoder, FALSE);
2283 /* stream admin setup */
2284 decoder->streams = g_hash_table_new_full (NULL, NULL, NULL, free_stream);
2286 /* see if the source element emits raw audio/video all by itself,
2287 * if so, we can create streams for the pads and be done with it.
2288 * Also check that is has source pads, if not, we assume it will
2289 * do everything itself. */
2290 if (!analyse_source (decoder, &is_raw, &have_out, &is_dynamic,
2291 decoder->need_queue))
2292 goto invalid_source;
2295 GST_DEBUG_OBJECT (decoder, "Source provides all raw data");
2296 /* source provides raw data, we added the pads and we can now signal a
2297 * no_more pads because we are done. */
2298 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
2299 do_async_done (decoder);
2302 if (!have_out && !is_dynamic) {
2303 GST_DEBUG_OBJECT (decoder, "Source has no output pads");
2304 /* create a stream to indicate that this uri is handled by a self
2305 * contained element. We are now done. */
2306 add_element_stream (decoder->source, decoder);
2310 GST_DEBUG_OBJECT (decoder, "Source has dynamic output pads");
2311 /* connect a handler for the new-pad signal */
2312 decoder->src_np_sig_id =
2313 g_signal_connect (decoder->source, "pad-added",
2314 G_CALLBACK (source_new_pad), decoder);
2315 decoder->src_nmp_sig_id =
2316 g_signal_connect (decoder->source, "no-more-pads",
2317 G_CALLBACK (source_no_more_pads), decoder);
2318 g_object_set_data (G_OBJECT (decoder->source), "pending",
2319 GINT_TO_POINTER (1));
2322 if (decoder->is_stream) {
2323 GST_DEBUG_OBJECT (decoder, "Setting up streaming");
2324 /* do the stream things here */
2325 if (!setup_streaming (decoder))
2326 goto streaming_failed;
2328 GstElement *dec_elem;
2330 /* no streaming source, we can link now */
2331 GST_DEBUG_OBJECT (decoder, "Plugging decodebin to source");
2333 dec_elem = make_decoder (decoder);
2337 if (!gst_element_link_pads (decoder->source, NULL, dec_elem, "sink"))
2338 goto could_not_link;
2346 /* error message was already posted */
2351 GST_ELEMENT_ERROR (decoder, CORE, FAILED,
2352 (_("Source element is invalid.")), (NULL));
2357 /* message was posted */
2362 /* message was posted */
2367 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2368 (NULL), ("Can't link source to decoder element"));
2374 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
2375 GList * structure_list)
2379 for (l = structure_list; l != NULL; l = l->next) {
2380 GValue val = { 0, };
2383 *first = gst_structure_copy ((GstStructure *) l->data);
2385 g_value_init (&val, GST_TYPE_STRUCTURE);
2386 g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
2387 gst_value_list_append_value (list_val, &val);
2388 g_value_unset (&val);
2392 /* if it's a redirect message with multiple redirect locations we might
2393 * want to pick a different 'best' location depending on the required
2394 * bitrates and the connection speed */
2396 handle_redirect_message (GstURIDecodeBin * dec, GstMessage * msg)
2398 const GValue *locations_list, *location_val;
2399 GstMessage *new_msg;
2400 GstStructure *new_structure = NULL;
2401 GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
2402 GValue new_list = { 0, };
2404 const GstStructure *structure;
2406 GST_DEBUG_OBJECT (dec, "redirect message: %" GST_PTR_FORMAT, msg);
2407 GST_DEBUG_OBJECT (dec, "connection speed: %" G_GUINT64_FORMAT,
2408 dec->connection_speed);
2410 structure = gst_message_get_structure (msg);
2411 if (dec->connection_speed == 0 || structure == NULL)
2414 locations_list = gst_structure_get_value (structure, "locations");
2415 if (locations_list == NULL)
2418 size = gst_value_list_get_size (locations_list);
2422 /* maintain existing order as much as possible, just sort references
2423 * with too high a bitrate to the end (the assumption being that if
2424 * bitrates are given they are given for all interesting streams and
2425 * that the you-need-at-least-version-xyz redirect has the same bitrate
2426 * as the lowest referenced redirect alternative) */
2427 for (i = 0; i < size; ++i) {
2428 const GstStructure *s;
2431 location_val = gst_value_list_get_value (locations_list, i);
2432 s = (const GstStructure *) g_value_get_boxed (location_val);
2433 if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
2434 GST_DEBUG_OBJECT (dec, "no bitrate: %" GST_PTR_FORMAT, s);
2435 l_neutral = g_list_append (l_neutral, (gpointer) s);
2436 } else if (bitrate > dec->connection_speed) {
2437 GST_DEBUG_OBJECT (dec, "bitrate too high: %" GST_PTR_FORMAT, s);
2438 l_bad = g_list_append (l_bad, (gpointer) s);
2439 } else if (bitrate <= dec->connection_speed) {
2440 GST_DEBUG_OBJECT (dec, "bitrate OK: %" GST_PTR_FORMAT, s);
2441 l_good = g_list_append (l_good, (gpointer) s);
2445 g_value_init (&new_list, GST_TYPE_LIST);
2446 value_list_append_structure_list (&new_list, &new_structure, l_good);
2447 value_list_append_structure_list (&new_list, &new_structure, l_neutral);
2448 value_list_append_structure_list (&new_list, &new_structure, l_bad);
2449 gst_structure_take_value (new_structure, "locations", &new_list);
2451 g_list_free (l_good);
2452 g_list_free (l_neutral);
2453 g_list_free (l_bad);
2455 new_msg = gst_message_new_element (msg->src, new_structure);
2456 gst_message_unref (msg);
2458 GST_DEBUG_OBJECT (dec, "new redirect message: %" GST_PTR_FORMAT, new_msg);
2463 make_topology_message (GstURIDecodeBin * dec)
2466 GstStructure *aggregated_topology = NULL;
2467 GValue list = G_VALUE_INIT;
2468 GstCaps *caps = NULL;
2469 gchar *name, *proto;
2471 aggregated_topology = gst_structure_new_empty ("stream-topology");
2472 g_value_init (&list, GST_TYPE_LIST);
2474 for (tmp = dec->decodebins; tmp; tmp = tmp->next) {
2475 GValue item = G_VALUE_INIT;
2476 GstStructure *dec_topology =
2477 g_object_get_data (G_OBJECT (tmp->data), "uridecodebin-topology");
2479 g_value_init (&item, GST_TYPE_STRUCTURE);
2480 gst_value_set_structure (&item, dec_topology);
2481 gst_value_list_append_and_take_value (&list, &item);
2484 gst_structure_take_value (aggregated_topology, "next", &list);
2486 /* This is a bit wacky, but that's the best way I can find to express
2487 * uridecodebin 'caps' as subsequently shown by gst-discoverer */
2488 proto = gst_uri_get_protocol (dec->uri);
2489 name = g_strdup_printf ("application/%s", proto);
2492 caps = gst_caps_new_empty_simple (name);
2495 gst_structure_set (aggregated_topology, "caps", GST_TYPE_CAPS, caps, NULL);
2496 gst_caps_unref (caps);
2498 return gst_message_new_element (GST_OBJECT (dec), aggregated_topology);
2502 check_topology (gpointer data, gpointer user_data)
2504 gboolean *has_topo = user_data;
2506 if (g_object_get_data (data, "uridecodebin-topology") == NULL)
2511 handle_message (GstBin * bin, GstMessage * msg)
2513 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (bin);
2515 switch (GST_MESSAGE_TYPE (msg)) {
2516 case GST_MESSAGE_ELEMENT:{
2518 if (gst_message_has_name (msg, "stream-topology")) {
2519 GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (msg));
2520 gboolean has_all_topo = TRUE;
2522 if (dec->pending || (dec->decodebins && dec->decodebins->next != NULL)) {
2523 const GstStructure *structure;
2525 /* If there is only one, just let it through, so this case is if
2526 * there is more than one.
2529 structure = gst_message_get_structure (msg);
2531 g_object_set_data_full (G_OBJECT (element), "uridecodebin-topology",
2532 gst_structure_copy (structure),
2533 (GDestroyNotify) gst_structure_free);
2535 gst_message_unref (msg);
2538 g_slist_foreach (dec->decodebins, check_topology, &has_all_topo);
2540 msg = make_topology_message (dec);
2542 } else if (gst_message_has_name (msg, "redirect")) {
2543 /* sort redirect messages based on the connection speed. This simplifies
2544 * the user of this element as it can in most cases just pick the first item
2545 * of the sorted list as a good redirection candidate. It can of course
2546 * choose something else from the list if it has a better way. */
2547 msg = handle_redirect_message (dec, msg);
2551 case GST_MESSAGE_ERROR:{
2554 /* Filter out missing plugin error messages from the decodebins. Only if
2555 * all decodebins exposed no streams we will report a missing plugin
2556 * error from no_more_pads_full()
2558 gst_message_parse_error (msg, &err, NULL);
2559 if (g_error_matches (err, GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN)
2560 || g_error_matches (err, GST_STREAM_ERROR,
2561 GST_STREAM_ERROR_CODEC_NOT_FOUND)) {
2562 dec->missing_plugin_errors =
2563 g_list_prepend (dec->missing_plugin_errors, gst_message_ref (msg));
2565 no_more_pads_full (GST_ELEMENT (GST_MESSAGE_SRC (msg)), FALSE,
2566 GST_URI_DECODE_BIN (bin));
2567 gst_message_unref (msg);
2570 g_clear_error (&err);
2578 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
2581 /* generic struct passed to all query fold methods
2582 * FIXME, move to core.
2593 typedef void (*QueryInitFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2594 typedef void (*QueryDoneFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2596 /* for duration/position we collect all durations/positions and take
2597 * the MAX of all valid results */
2599 decoder_query_init (GstURIDecodeBin * dec, QueryFold * fold)
2603 fold->seekable = TRUE;
2608 decoder_query_duration_fold (const GValue * item, GValue * ret,
2611 GstPad *pad = g_value_get_object (item);
2613 if (gst_pad_query (pad, fold->query)) {
2616 g_value_set_boolean (ret, TRUE);
2618 gst_query_parse_duration (fold->query, NULL, &duration);
2620 GST_DEBUG_OBJECT (pad, "got duration %" G_GINT64_FORMAT, duration);
2622 if (duration > fold->max)
2623 fold->max = duration;
2629 decoder_query_duration_done (GstURIDecodeBin * dec, QueryFold * fold)
2633 gst_query_parse_duration (fold->query, &format, NULL);
2634 /* store max in query result */
2635 gst_query_set_duration (fold->query, format, fold->max);
2637 GST_DEBUG ("max duration %" G_GINT64_FORMAT, fold->max);
2641 decoder_query_position_fold (const GValue * item, GValue * ret,
2644 GstPad *pad = g_value_get_object (item);
2646 if (gst_pad_query (pad, fold->query)) {
2649 g_value_set_boolean (ret, TRUE);
2651 gst_query_parse_position (fold->query, NULL, &position);
2653 GST_DEBUG_OBJECT (pad, "got position %" G_GINT64_FORMAT, position);
2655 if (position > fold->max)
2656 fold->max = position;
2663 decoder_query_position_done (GstURIDecodeBin * dec, QueryFold * fold)
2667 gst_query_parse_position (fold->query, &format, NULL);
2668 /* store max in query result */
2669 gst_query_set_position (fold->query, format, fold->max);
2671 GST_DEBUG_OBJECT (dec, "max position %" G_GINT64_FORMAT, fold->max);
2675 decoder_query_latency_fold (const GValue * item, GValue * ret, QueryFold * fold)
2677 GstPad *pad = g_value_get_object (item);
2679 if (gst_pad_query (pad, fold->query)) {
2680 GstClockTime min, max;
2683 gst_query_parse_latency (fold->query, &live, &min, &max);
2685 GST_DEBUG_OBJECT (pad,
2686 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2687 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
2690 /* for the combined latency we collect the MAX of all min latencies and
2691 * the MIN of all max latencies */
2692 if (min > fold->min)
2694 if (fold->max == -1)
2696 else if (max < fold->max)
2702 GST_LOG_OBJECT (pad, "latency query failed");
2703 g_value_set_boolean (ret, FALSE);
2710 decoder_query_latency_done (GstURIDecodeBin * dec, QueryFold * fold)
2712 /* store max in query result */
2713 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
2715 GST_DEBUG_OBJECT (dec,
2716 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2717 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
2721 /* we are seekable if all srcpads are seekable */
2723 decoder_query_seeking_fold (const GValue * item, GValue * ret, QueryFold * fold)
2725 GstPad *pad = g_value_get_object (item);
2727 if (gst_pad_query (pad, fold->query)) {
2730 g_value_set_boolean (ret, TRUE);
2731 gst_query_parse_seeking (fold->query, NULL, &seekable, NULL, NULL);
2733 GST_DEBUG_OBJECT (pad, "got seekable %d", seekable);
2736 fold->seekable = seekable;
2743 decoder_query_seeking_done (GstURIDecodeBin * dec, QueryFold * fold)
2747 gst_query_parse_seeking (fold->query, &format, NULL, NULL, NULL);
2748 gst_query_set_seeking (fold->query, format, fold->seekable, 0, -1);
2750 GST_DEBUG_OBJECT (dec, "seekable %d", fold->seekable);
2753 /* generic fold, return first valid result */
2755 decoder_query_generic_fold (const GValue * item, GValue * ret, QueryFold * fold)
2757 GstPad *pad = g_value_get_object (item);
2760 if ((res = gst_pad_query (pad, fold->query))) {
2761 g_value_set_boolean (ret, TRUE);
2762 GST_DEBUG_OBJECT (pad, "answered query %p", fold->query);
2765 /* and stop as soon as we have a valid result */
2770 /* we're a bin, the default query handler iterates sink elements, which we don't
2771 * have normally. We should just query all source pads.
2774 gst_uri_decode_bin_query (GstElement * element, GstQuery * query)
2776 GstURIDecodeBin *decoder;
2777 gboolean res = FALSE;
2779 GstIteratorFoldFunction fold_func;
2780 QueryInitFunction fold_init = NULL;
2781 QueryDoneFunction fold_done = NULL;
2782 QueryFold fold_data;
2784 gboolean default_ret = FALSE;
2786 decoder = GST_URI_DECODE_BIN (element);
2788 switch (GST_QUERY_TYPE (query)) {
2789 case GST_QUERY_DURATION:
2790 /* iterate and collect durations */
2791 fold_func = (GstIteratorFoldFunction) decoder_query_duration_fold;
2792 fold_init = decoder_query_init;
2793 fold_done = decoder_query_duration_done;
2795 case GST_QUERY_POSITION:
2796 /* iterate and collect durations */
2797 fold_func = (GstIteratorFoldFunction) decoder_query_position_fold;
2798 fold_init = decoder_query_init;
2799 fold_done = decoder_query_position_done;
2801 case GST_QUERY_LATENCY:
2802 /* iterate and collect durations */
2803 fold_func = (GstIteratorFoldFunction) decoder_query_latency_fold;
2804 fold_init = decoder_query_init;
2805 fold_done = decoder_query_latency_done;
2808 case GST_QUERY_SEEKING:
2809 /* iterate and collect durations */
2810 fold_func = (GstIteratorFoldFunction) decoder_query_seeking_fold;
2811 fold_init = decoder_query_init;
2812 fold_done = decoder_query_seeking_done;
2815 fold_func = (GstIteratorFoldFunction) decoder_query_generic_fold;
2819 fold_data.query = query;
2821 g_value_init (&ret, G_TYPE_BOOLEAN);
2822 g_value_set_boolean (&ret, default_ret);
2824 iter = gst_element_iterate_src_pads (element);
2825 GST_DEBUG_OBJECT (element, "Sending query %p (type %d) to src pads",
2826 query, GST_QUERY_TYPE (query));
2829 fold_init (decoder, &fold_data);
2832 GstIteratorResult ires;
2834 ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
2837 case GST_ITERATOR_RESYNC:
2838 gst_iterator_resync (iter);
2840 fold_init (decoder, &fold_data);
2841 g_value_set_boolean (&ret, default_ret);
2843 case GST_ITERATOR_OK:
2844 case GST_ITERATOR_DONE:
2845 res = g_value_get_boolean (&ret);
2846 if (fold_done != NULL && res)
2847 fold_done (decoder, &fold_data);
2855 gst_iterator_free (iter);
2860 static GstStateChangeReturn
2861 gst_uri_decode_bin_change_state (GstElement * element,
2862 GstStateChange transition)
2864 GstStateChangeReturn ret;
2865 GstURIDecodeBin *decoder;
2867 decoder = GST_URI_DECODE_BIN (element);
2869 switch (transition) {
2870 case GST_STATE_CHANGE_READY_TO_PAUSED:
2871 do_async_start (decoder);
2877 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2878 if (ret == GST_STATE_CHANGE_FAILURE)
2881 switch (transition) {
2882 case GST_STATE_CHANGE_READY_TO_PAUSED:
2883 GST_DEBUG ("ready to paused");
2884 if (!setup_source (decoder))
2887 ret = GST_STATE_CHANGE_ASYNC;
2889 /* And now sync the states of everything we added */
2890 g_slist_foreach (decoder->decodebins,
2891 (GFunc) gst_element_sync_state_with_parent, NULL);
2892 if (decoder->typefind)
2893 ret = gst_element_set_state (decoder->typefind, GST_STATE_PAUSED);
2894 if (ret == GST_STATE_CHANGE_FAILURE)
2897 ret = gst_element_set_state (decoder->queue, GST_STATE_PAUSED);
2898 if (ret == GST_STATE_CHANGE_FAILURE)
2900 if (decoder->source)
2901 ret = gst_element_set_state (decoder->source, GST_STATE_PAUSED);
2902 if (ret == GST_STATE_CHANGE_FAILURE)
2904 if (ret == GST_STATE_CHANGE_SUCCESS)
2905 ret = GST_STATE_CHANGE_ASYNC;
2908 case GST_STATE_CHANGE_PAUSED_TO_READY:
2909 GST_DEBUG ("paused to ready");
2910 remove_decoders (decoder, FALSE);
2911 remove_source (decoder);
2912 do_async_done (decoder);
2913 g_list_free_full (decoder->missing_plugin_errors,
2914 (GDestroyNotify) gst_message_unref);
2915 decoder->missing_plugin_errors = NULL;
2917 case GST_STATE_CHANGE_READY_TO_NULL:
2918 GST_DEBUG ("ready to null");
2919 remove_decoders (decoder, TRUE);
2920 remove_source (decoder);
2926 if (ret == GST_STATE_CHANGE_NO_PREROLL)
2927 do_async_done (decoder);
2934 do_async_done (decoder);
2935 return GST_STATE_CHANGE_FAILURE;
2939 /* clean up leftover groups */
2940 do_async_done (decoder);
2941 return GST_STATE_CHANGE_FAILURE;