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
23 * Decodes data from a URI into raw media. It selects a source element that can
24 * handle the given #GstURIDecodeBin:uri scheme and connects it to a decodebin.
27 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
28 * with newer GLib versions (>= 2.31.0) */
29 #define GLIB_DISABLE_DEPRECATION_WARNINGS
38 #include <gst/gst-i18n-plugin.h>
39 #include <gst/pbutils/missing-plugins.h>
41 #include "gstplay-enum.h"
42 #include "gstrawcaps.h"
43 #include "gstplayback.h"
45 /* From gstdecodebin2.c */
46 gint _decode_bin_compare_factories_func (gconstpointer p1, gconstpointer p2);
48 #define GST_TYPE_URI_DECODE_BIN \
49 (gst_uri_decode_bin_get_type())
50 #define GST_URI_DECODE_BIN(obj) \
51 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBin))
52 #define GST_URI_DECODE_BIN_CLASS(klass) \
53 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBinClass))
54 #define GST_IS_URI_DECODE_BIN(obj) \
55 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DECODE_BIN))
56 #define GST_IS_URI_DECODE_BIN_CLASS(klass) \
57 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DECODE_BIN))
58 #define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
60 typedef struct _GstURIDecodeBin GstURIDecodeBin;
61 typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;
63 #define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(&((GstURIDecodeBin*)(dec))->lock))
64 #define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(&((GstURIDecodeBin*)(dec))->lock))
66 typedef struct _GstURIDecodeBinStream
70 } GstURIDecodeBinStream;
75 * uridecodebin element struct
77 struct _GstURIDecodeBin
79 GstBin parent_instance;
81 GMutex lock; /* lock for constructing */
83 GMutex factories_lock;
84 guint32 factories_cookie;
85 GList *factories; /* factories we can use for selecting elements */
88 guint64 connection_speed;
95 guint64 buffer_duration; /* When buffering, buffer duration (ns) */
96 guint buffer_size; /* When buffering, buffer size (bytes) */
98 gboolean use_buffering;
102 GstElement *typefind;
103 guint have_type_id; /* have-type signal id from typefind */
105 GSList *pending_decodebins;
109 /* for dynamic sources */
110 guint src_np_sig_id; /* new-pad signal id */
111 guint src_nmp_sig_id; /* no-more-pads signal id */
114 gboolean async_pending; /* async-start has been emitted */
116 gboolean expose_allstreams; /* Whether to expose unknow type streams or not */
118 guint64 ring_buffer_max_size; /* 0 means disabled */
121 struct _GstURIDecodeBinClass
123 GstBinClass parent_class;
125 /* signal fired when we found a pad that we cannot decode */
126 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
128 /* signal fired to know if we continue trying to decode the given caps */
129 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
131 /* signal fired to get a list of factories to try to autoplug */
132 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
134 /* signal fired to sort the factories */
135 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
136 GstCaps * caps, GValueArray * factories);
137 /* signal fired to select from the proposed list of factories */
138 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
139 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
140 /* signal fired when a autoplugged element that is not linked downstream
141 * or exposed wants to query something */
142 gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
145 /* emitted when all data is decoded */
146 void (*drained) (GstElement * element);
149 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
152 GST_STATIC_CAPS_ANY);
154 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
156 GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
157 #define GST_CAT_DEFAULT gst_uri_decode_bin_debug
163 SIGNAL_AUTOPLUG_CONTINUE,
164 SIGNAL_AUTOPLUG_FACTORIES,
165 SIGNAL_AUTOPLUG_SELECT,
166 SIGNAL_AUTOPLUG_SORT,
167 SIGNAL_AUTOPLUG_QUERY,
174 #define DEFAULT_PROP_URI NULL
175 #define DEFAULT_PROP_SOURCE NULL
176 #define DEFAULT_CONNECTION_SPEED 0
177 #define DEFAULT_CAPS (gst_static_caps_get (&default_raw_caps))
178 #define DEFAULT_SUBTITLE_ENCODING NULL
179 #define DEFAULT_BUFFER_DURATION -1
180 #define DEFAULT_BUFFER_SIZE -1
181 #define DEFAULT_DOWNLOAD FALSE
182 #define DEFAULT_USE_BUFFERING FALSE
183 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
184 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
191 PROP_CONNECTION_SPEED,
193 PROP_SUBTITLE_ENCODING,
195 PROP_BUFFER_DURATION,
198 PROP_EXPOSE_ALL_STREAMS,
199 PROP_RING_BUFFER_MAX_SIZE,
203 static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };
205 GType gst_uri_decode_bin_get_type (void);
206 #define gst_uri_decode_bin_parent_class parent_class
207 G_DEFINE_TYPE (GstURIDecodeBin, gst_uri_decode_bin, GST_TYPE_BIN);
209 static void remove_decoders (GstURIDecodeBin * bin, gboolean force);
210 static void gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
211 const GValue * value, GParamSpec * pspec);
212 static void gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
213 GValue * value, GParamSpec * pspec);
214 static void gst_uri_decode_bin_finalize (GObject * obj);
216 static void handle_message (GstBin * bin, GstMessage * msg);
218 static gboolean gst_uri_decode_bin_query (GstElement * element,
220 static GstStateChangeReturn gst_uri_decode_bin_change_state (GstElement *
221 element, GstStateChange transition);
224 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
225 GValue * return_accu, const GValue * handler_return, gpointer dummy)
229 myboolean = g_value_get_boolean (handler_return);
230 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
231 g_value_set_boolean (return_accu, myboolean);
233 /* stop emission if FALSE */
238 _gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
239 GValue * return_accu, const GValue * handler_return, gpointer dummy)
244 myboolean = g_value_get_boolean (handler_return);
245 retboolean = g_value_get_boolean (return_accu);
247 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
248 g_value_set_boolean (return_accu, myboolean || retboolean);
254 _gst_array_accumulator (GSignalInvocationHint * ihint,
255 GValue * return_accu, const GValue * handler_return, gpointer dummy)
259 array = g_value_get_boxed (handler_return);
260 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
261 g_value_set_boxed (return_accu, array);
267 _gst_select_accumulator (GSignalInvocationHint * ihint,
268 GValue * return_accu, const GValue * handler_return, gpointer dummy)
270 GstAutoplugSelectResult res;
272 res = g_value_get_enum (handler_return);
273 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
274 g_value_set_enum (return_accu, res);
276 /* Call the next handler in the chain (if any) when the current callback
277 * returns TRY. This makes it possible to register separate autoplug-select
278 * handlers that implement different TRY/EXPOSE/SKIP strategies.
280 if (res == GST_AUTOPLUG_SELECT_TRY)
287 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
288 GValue * return_accu, const GValue * handler_return, gpointer dummy)
292 array = g_value_get_boxed (handler_return);
293 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
294 g_value_set_boxed (return_accu, array);
303 gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
306 /* by default we always continue */
310 /* Must be called with factories lock! */
312 gst_uri_decode_bin_update_factories_list (GstURIDecodeBin * dec)
316 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
317 if (!dec->factories || dec->factories_cookie != cookie) {
319 gst_plugin_feature_list_free (dec->factories);
321 gst_element_factory_list_get_elements
322 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
324 g_list_sort (dec->factories, _decode_bin_compare_factories_func);
325 dec->factories_cookie = cookie;
330 gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
335 GstURIDecodeBin *dec = GST_URI_DECODE_BIN_CAST (element);
337 GST_DEBUG_OBJECT (element, "finding factories");
339 /* return all compatible factories for caps */
340 g_mutex_lock (&dec->factories_lock);
341 gst_uri_decode_bin_update_factories_list (dec);
343 gst_element_factory_list_filter (dec->factories, caps, GST_PAD_SINK,
344 gst_caps_is_fixed (caps));
345 g_mutex_unlock (&dec->factories_lock);
347 result = g_value_array_new (g_list_length (list));
348 for (tmp = list; tmp; tmp = tmp->next) {
349 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
352 g_value_init (&val, G_TYPE_OBJECT);
353 g_value_set_object (&val, factory);
354 g_value_array_append (result, &val);
355 g_value_unset (&val);
357 gst_plugin_feature_list_free (list);
359 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
365 gst_uri_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
366 GstCaps * caps, GValueArray * factories)
371 static GstAutoplugSelectResult
372 gst_uri_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
373 GstCaps * caps, GstElementFactory * factory)
375 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
378 return GST_AUTOPLUG_SELECT_TRY;
382 gst_uri_decode_bin_autoplug_query (GstElement * element, GstPad * pad,
385 /* No query handled here */
390 gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
392 GObjectClass *gobject_class;
393 GstElementClass *gstelement_class;
394 GstBinClass *gstbin_class;
396 gobject_class = G_OBJECT_CLASS (klass);
397 gstelement_class = GST_ELEMENT_CLASS (klass);
398 gstbin_class = GST_BIN_CLASS (klass);
400 gobject_class->set_property = gst_uri_decode_bin_set_property;
401 gobject_class->get_property = gst_uri_decode_bin_get_property;
402 gobject_class->finalize = gst_uri_decode_bin_finalize;
404 g_object_class_install_property (gobject_class, PROP_URI,
405 g_param_spec_string ("uri", "URI", "URI to decode",
406 DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 g_object_class_install_property (gobject_class, PROP_SOURCE,
409 g_param_spec_object ("source", "Source", "Source object used",
410 GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
413 g_param_spec_uint64 ("connection-speed", "Connection Speed",
414 "Network connection speed in kbps (0 = unknown)",
415 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
418 g_object_class_install_property (gobject_class, PROP_CAPS,
419 g_param_spec_boxed ("caps", "Caps",
420 "The caps on which to stop decoding. (NULL = default)",
421 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423 g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
424 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
425 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
426 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
427 "be checked for an encoding to use. If that is not set either, "
428 "ISO-8859-15 will be assumed.", NULL,
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
432 g_param_spec_int ("buffer-size", "Buffer size (bytes)",
433 "Buffer size when buffering streams (-1 default value)",
434 -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
435 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
436 g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
437 g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
438 "Buffer duration when buffering streams (-1 default value)",
439 -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
443 * GstURIDecodeBin::download:
445 * For certain media type, enable download buffering.
447 g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
448 g_param_spec_boolean ("download", "Download",
449 "Attempt download buffering when buffering network streams",
450 DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
452 * GstURIDecodeBin::use-buffering:
454 * Emit BUFFERING messages based on low-/high-percent thresholds of the
455 * demuxed or parsed data.
456 * When download buffering is activated and used for the current media
457 * type, this property does nothing. Otherwise perform buffering on the
458 * demuxed or parsed media.
462 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
463 g_param_spec_boolean ("use-buffering", "Use Buffering",
464 "Perform buffering on demuxed/parsed media",
465 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
468 * GstURIDecodeBin::expose-all-streams
470 * Expose streams of unknown type.
472 * If set to %FALSE, then only the streams that can be decoded to the final
473 * caps (see 'caps' property) will have a pad exposed. Streams that do not
474 * match those caps but could have been decoded will not have decoder plugged
475 * in internally and will not have a pad exposed.
479 g_object_class_install_property (gobject_class, PROP_EXPOSE_ALL_STREAMS,
480 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
481 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
482 DEFAULT_EXPOSE_ALL_STREAMS,
483 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 * GstURIDecodeBin::ring-buffer-max-size
488 * The maximum size of the ring buffer in kilobytes. If set to 0, the ring
489 * buffer is disabled. Default is 0.
493 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
494 g_param_spec_uint64 ("ring-buffer-max-size",
495 "Max. ring buffer size (bytes)",
496 "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
497 0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
498 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
501 * GstURIDecodeBin::unknown-type:
502 * @bin: The uridecodebin.
503 * @pad: the new pad containing caps that cannot be resolved to a 'final'.
505 * @caps: the #GstCaps of the pad that cannot be resolved.
507 * This signal is emitted when a pad for which there is no further possible
508 * decoding is added to the uridecodebin.
510 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
511 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
512 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, unknown_type),
513 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
514 GST_TYPE_PAD, GST_TYPE_CAPS);
517 * GstURIDecodeBin::autoplug-continue:
518 * @bin: The uridecodebin.
520 * @caps: The #GstCaps found.
522 * This signal is emitted whenever uridecodebin finds a new stream. It is
523 * emitted before looking for any elements that can handle that stream.
526 * Invocation of signal handlers stops after the first signal handler
527 * returns #FALSE. Signal handlers are invoked in the order they were
531 * Returns: #TRUE if you wish uridecodebin to look for elements that can
532 * handle the given @caps. If #FALSE, those caps will be considered as
533 * final and the pad will be exposed as such (see 'pad-added' signal of
536 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
537 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
538 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
539 autoplug_continue), _gst_boolean_accumulator, NULL,
540 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
544 * GstURIDecodeBin::autoplug-factories:
545 * @bin: The uridecodebin.
547 * @caps: The #GstCaps found.
549 * This function is emitted when an array of possible factories for @caps on
550 * @pad is needed. Uridecodebin will by default return an array with all
551 * compatible factories, sorted by rank.
553 * If this function returns NULL, @pad will be exposed as a final caps.
555 * If this function returns an empty array, the pad will be considered as
556 * having an unhandled type media type.
559 * Only the signal handler that is connected first will ever by invoked.
560 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
561 * signal, they will never be invoked!
564 * Returns: a #GValueArray* with a list of factories to try. The factories are
565 * by default tried in the returned order or based on the index returned by
568 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
569 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
570 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
571 autoplug_factories), _gst_array_accumulator, NULL,
572 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
573 GST_TYPE_PAD, GST_TYPE_CAPS);
576 * GstURIDecodeBin::autoplug-sort:
577 * @bin: The uridecodebin.
579 * @caps: The #GstCaps.
580 * @factories: A #GValueArray of possible #GstElementFactory to use.
582 * Once decodebin has found the possible #GstElementFactory objects to try
583 * for @caps on @pad, this signal is emited. The purpose of the signal is for
584 * the application to perform additional sorting or filtering on the element
587 * The callee should copy and modify @factories or return #NULL if the
588 * order should not change.
591 * Invocation of signal handlers stops after one signal handler has
592 * returned something else than #NULL. Signal handlers are invoked in
593 * the order they were connected in.
594 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
595 * signal, they will never be invoked!
598 * Returns: A new sorted array of #GstElementFactory objects.
602 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
603 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
604 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_sort),
605 _gst_array_hasvalue_accumulator, NULL,
606 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
607 GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
610 * GstURIDecodeBin::autoplug-select:
611 * @bin: The uridecodebin.
613 * @caps: The #GstCaps.
614 * @factory: A #GstElementFactory to use.
616 * This signal is emitted once uridecodebin has found all the possible
617 * #GstElementFactory that can be used to handle the given @caps. For each of
618 * those factories, this signal is emitted.
620 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
621 * value indicating what decodebin should do next.
623 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
626 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
629 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
633 * The signal handler will not be invoked if any of the previously
634 * registered signal handlers (if any) return a value other than
635 * GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
636 * GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
637 * registered next (again, if any) can override that decision.
640 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
641 * operation. The default handler will always return
642 * #GST_AUTOPLUG_SELECT_TRY.
644 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
645 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
646 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
647 autoplug_select), _gst_select_accumulator, NULL,
648 g_cclosure_marshal_generic,
649 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
650 GST_TYPE_ELEMENT_FACTORY);
653 * GstDecodeBin::autoplug-query:
654 * @bin: The decodebin.
655 * @child: The child element doing the query
657 * @query: The #GstQuery.
659 * This signal is emitted whenever an autoplugged element that is
660 * not linked downstream yet and not exposed does a query. It can
661 * be used to tell the element about the downstream supported caps
664 * Returns: #TRUE if the query was handled, #FALSE otherwise.
666 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
667 g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
668 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_query),
669 _gst_boolean_or_accumulator, NULL, g_cclosure_marshal_generic,
670 G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
671 GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
674 * GstURIDecodeBin::drained:
676 * This signal is emitted when the data for the current uri is played.
678 gst_uri_decode_bin_signals[SIGNAL_DRAINED] =
679 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
681 G_STRUCT_OFFSET (GstURIDecodeBinClass, drained), NULL, NULL,
682 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
685 * GstURIDecodeBin::source-setup:
686 * @bin: the uridecodebin.
687 * @source: source element
689 * This signal is emitted after the source element has been created, so
690 * it can be configured by setting additional properties (e.g. set a
691 * proxy server for an http source, or set the device and read speed for
692 * an audio cd source). This is functionally equivalent to connecting to
693 * the notify::source signal, but more convenient.
697 gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP] =
698 g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
699 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
700 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
702 gst_element_class_add_pad_template (gstelement_class,
703 gst_static_pad_template_get (&srctemplate));
704 gst_element_class_set_static_metadata (gstelement_class,
705 "URI Decoder", "Generic/Bin/Decoder",
706 "Autoplug and decode an URI to raw media",
707 "Wim Taymans <wim.taymans@gmail.com>");
709 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_query);
710 gstelement_class->change_state =
711 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_change_state);
713 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);
715 klass->autoplug_continue =
716 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
717 klass->autoplug_factories =
718 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
719 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_sort);
720 klass->autoplug_select =
721 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_select);
722 klass->autoplug_query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_query);
726 gst_uri_decode_bin_init (GstURIDecodeBin * dec)
728 /* first filter out the interesting element factories */
729 g_mutex_init (&dec->factories_lock);
731 g_mutex_init (&dec->lock);
733 dec->uri = g_strdup (DEFAULT_PROP_URI);
734 dec->connection_speed = DEFAULT_CONNECTION_SPEED;
735 dec->caps = DEFAULT_CAPS;
736 dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
738 dec->buffer_duration = DEFAULT_BUFFER_DURATION;
739 dec->buffer_size = DEFAULT_BUFFER_SIZE;
740 dec->download = DEFAULT_DOWNLOAD;
741 dec->use_buffering = DEFAULT_USE_BUFFERING;
742 dec->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
743 dec->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
745 GST_OBJECT_FLAG_SET (dec, GST_ELEMENT_FLAG_SOURCE);
749 gst_uri_decode_bin_finalize (GObject * obj)
751 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (obj);
753 remove_decoders (dec, TRUE);
754 g_mutex_clear (&dec->lock);
755 g_mutex_clear (&dec->factories_lock);
757 g_free (dec->encoding);
759 gst_plugin_feature_list_free (dec->factories);
761 gst_caps_unref (dec->caps);
763 G_OBJECT_CLASS (parent_class)->finalize (obj);
767 gst_uri_decode_bin_set_encoding (GstURIDecodeBin * dec, const gchar * encoding)
771 GST_URI_DECODE_BIN_LOCK (dec);
773 /* set property first */
774 GST_OBJECT_LOCK (dec);
775 g_free (dec->encoding);
776 dec->encoding = g_strdup (encoding);
777 GST_OBJECT_UNLOCK (dec);
779 /* set the property on all decodebins now */
780 for (walk = dec->decodebins; walk; walk = g_slist_next (walk)) {
781 GObject *decodebin = G_OBJECT (walk->data);
783 g_object_set (decodebin, "subtitle-encoding", encoding, NULL);
785 GST_URI_DECODE_BIN_UNLOCK (dec);
789 gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
790 const GValue * value, GParamSpec * pspec)
792 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
796 GST_OBJECT_LOCK (dec);
798 dec->uri = g_value_dup_string (value);
799 GST_OBJECT_UNLOCK (dec);
801 case PROP_CONNECTION_SPEED:
802 GST_OBJECT_LOCK (dec);
803 dec->connection_speed = g_value_get_uint64 (value) * 1000;
804 GST_OBJECT_UNLOCK (dec);
807 GST_OBJECT_LOCK (dec);
809 gst_caps_unref (dec->caps);
810 dec->caps = g_value_dup_boxed (value);
811 GST_OBJECT_UNLOCK (dec);
813 case PROP_SUBTITLE_ENCODING:
814 gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
816 case PROP_BUFFER_SIZE:
817 dec->buffer_size = g_value_get_int (value);
819 case PROP_BUFFER_DURATION:
820 dec->buffer_duration = g_value_get_int64 (value);
823 dec->download = g_value_get_boolean (value);
825 case PROP_USE_BUFFERING:
826 dec->use_buffering = g_value_get_boolean (value);
828 case PROP_EXPOSE_ALL_STREAMS:
829 dec->expose_allstreams = g_value_get_boolean (value);
831 case PROP_RING_BUFFER_MAX_SIZE:
832 dec->ring_buffer_max_size = g_value_get_uint64 (value);
835 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
841 gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
842 GValue * value, GParamSpec * pspec)
844 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
848 GST_OBJECT_LOCK (dec);
849 g_value_set_string (value, dec->uri);
850 GST_OBJECT_UNLOCK (dec);
853 GST_OBJECT_LOCK (dec);
854 g_value_set_object (value, dec->source);
855 GST_OBJECT_UNLOCK (dec);
857 case PROP_CONNECTION_SPEED:
858 GST_OBJECT_LOCK (dec);
859 g_value_set_uint64 (value, dec->connection_speed / 1000);
860 GST_OBJECT_UNLOCK (dec);
863 GST_OBJECT_LOCK (dec);
864 g_value_set_boxed (value, dec->caps);
865 GST_OBJECT_UNLOCK (dec);
867 case PROP_SUBTITLE_ENCODING:
868 GST_OBJECT_LOCK (dec);
869 g_value_set_string (value, dec->encoding);
870 GST_OBJECT_UNLOCK (dec);
872 case PROP_BUFFER_SIZE:
873 GST_OBJECT_LOCK (dec);
874 g_value_set_int (value, dec->buffer_size);
875 GST_OBJECT_UNLOCK (dec);
877 case PROP_BUFFER_DURATION:
878 GST_OBJECT_LOCK (dec);
879 g_value_set_int64 (value, dec->buffer_duration);
880 GST_OBJECT_UNLOCK (dec);
883 g_value_set_boolean (value, dec->download);
885 case PROP_USE_BUFFERING:
886 g_value_set_boolean (value, dec->use_buffering);
888 case PROP_EXPOSE_ALL_STREAMS:
889 g_value_set_boolean (value, dec->expose_allstreams);
891 case PROP_RING_BUFFER_MAX_SIZE:
892 g_value_set_uint64 (value, dec->ring_buffer_max_size);
895 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
901 do_async_start (GstURIDecodeBin * dbin)
905 dbin->async_pending = TRUE;
907 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
908 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (dbin), message);
912 do_async_done (GstURIDecodeBin * dbin)
916 if (dbin->async_pending) {
917 GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
919 gst_message_new_async_done (GST_OBJECT_CAST (dbin),
920 GST_CLOCK_TIME_NONE);
921 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (dbin), message);
923 dbin->async_pending = FALSE;
927 #define DEFAULT_QUEUE_SIZE (3 * GST_SECOND)
928 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
929 #define DEFAULT_QUEUE_THRESHOLD ((DEFAULT_QUEUE_SIZE * 95) / 100)
932 unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
933 GstURIDecodeBin * decoder)
937 capsstr = gst_caps_to_string (caps);
938 GST_ELEMENT_WARNING (decoder, STREAM, CODEC_NOT_FOUND,
939 (_("No decoder available for type \'%s\'."), capsstr), (NULL));
943 /* add a streaminfo that indicates that the stream is handled by the
944 * given element. This usually means that a stream without actual data is
945 * produced but one that is sunken by an element. Examples of this are:
946 * cdaudio, a hardware decoder/sink, dvd meta bins etc...
949 add_element_stream (GstElement * element, GstURIDecodeBin * decoder)
951 g_warning ("add element stream");
954 /* when the decoder element signals that no more pads will be generated, we
955 * can commit the current group.
958 no_more_pads_full (GstElement * element, gboolean subs,
959 GstURIDecodeBin * decoder)
964 GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);
966 GST_URI_DECODE_BIN_LOCK (decoder);
967 final = (decoder->pending == 0);
969 /* nothing pending, we can exit */
973 /* the object has no pending no_more_pads */
974 if (!g_object_get_data (G_OBJECT (element), "pending"))
976 g_object_set_data (G_OBJECT (element), "pending", NULL);
979 final = (decoder->pending == 0);
982 GST_URI_DECODE_BIN_UNLOCK (decoder);
985 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
991 no_more_pads (GstElement * element, GstURIDecodeBin * decoder)
993 no_more_pads_full (element, FALSE, decoder);
997 source_no_more_pads (GstElement * element, GstURIDecodeBin * bin)
999 GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
1000 GST_ELEMENT_NAME (element));
1002 g_signal_handler_disconnect (element, bin->src_np_sig_id);
1003 bin->src_np_sig_id = 0;
1004 g_signal_handler_disconnect (element, bin->src_nmp_sig_id);
1005 bin->src_nmp_sig_id = 0;
1007 no_more_pads_full (element, FALSE, bin);
1011 configure_stream_buffering (GstURIDecodeBin * decoder)
1013 GstElement *queue = NULL;
1014 GHashTableIter iter;
1015 gpointer key, value;
1018 /* automatic configuration enabled ? */
1019 if (decoder->buffer_size != -1)
1022 GST_URI_DECODE_BIN_LOCK (decoder);
1024 queue = gst_object_ref (decoder->queue);
1026 g_hash_table_iter_init (&iter, decoder->streams);
1027 while (g_hash_table_iter_next (&iter, &key, &value)) {
1028 GstURIDecodeBinStream *stream = value;
1030 if (stream->bitrate && bitrate >= 0)
1031 bitrate += stream->bitrate;
1035 GST_URI_DECODE_BIN_UNLOCK (decoder);
1037 GST_DEBUG_OBJECT (decoder, "overall bitrate %d", bitrate);
1045 /* all streams have a bitrate;
1046 * configure queue size based on queue duration using combined bitrate */
1047 g_object_get (queue, "max-size-time", &time, NULL);
1048 GST_DEBUG_OBJECT (decoder, "queue buffering time %" GST_TIME_FORMAT,
1049 GST_TIME_ARGS (time));
1051 bytes = gst_util_uint64_scale (time, bitrate, 8 * GST_SECOND);
1052 GST_DEBUG_OBJECT (decoder, "corresponds to buffer size %d", bytes);
1053 g_object_set (queue, "max-size-bytes", bytes, NULL);
1057 gst_object_unref (queue);
1060 static GstPadProbeReturn
1061 decoded_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
1064 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1065 GstURIDecodeBin *decoder = user_data;
1067 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1069 /* look for a bitrate tag */
1070 switch (GST_EVENT_TYPE (event)) {
1075 GstURIDecodeBinStream *stream;
1077 gst_event_parse_tag (event, &list);
1078 if (!gst_tag_list_get_uint_index (list, GST_TAG_NOMINAL_BITRATE, 0,
1080 gst_tag_list_get_uint_index (list, GST_TAG_BITRATE, 0, &bitrate);
1082 GST_DEBUG_OBJECT (pad, "found bitrate %u", bitrate);
1084 GST_URI_DECODE_BIN_LOCK (decoder);
1085 stream = g_hash_table_lookup (decoder->streams, pad);
1086 GST_URI_DECODE_BIN_UNLOCK (decoder);
1088 stream->bitrate = bitrate;
1089 /* no longer need this probe now */
1090 gst_pad_remove_probe (pad, stream->probe_id);
1091 /* configure buffer if possible */
1092 configure_stream_buffering (decoder);
1102 return GST_PAD_PROBE_OK;
1107 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1109 GstPad *gpad = GST_PAD_CAST (user_data);
1111 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
1112 gst_pad_store_sticky_event (gpad, *event);
1117 /* Called by the signal handlers when a decodebin has found a new raw pad */
1119 new_decoded_pad_added_cb (GstElement * element, GstPad * pad,
1120 GstURIDecodeBin * decoder)
1123 GstPadTemplate *pad_tmpl;
1125 GstURIDecodeBinStream *stream;
1127 GST_DEBUG_OBJECT (element, "new decoded pad, name: <%s>", GST_PAD_NAME (pad));
1129 GST_URI_DECODE_BIN_LOCK (decoder);
1130 padname = g_strdup_printf ("src_%u", decoder->numpads);
1132 GST_URI_DECODE_BIN_UNLOCK (decoder);
1134 pad_tmpl = gst_static_pad_template_get (&srctemplate);
1135 newpad = gst_ghost_pad_new_from_template (padname, pad, pad_tmpl);
1136 gst_object_unref (pad_tmpl);
1139 /* store ref to the ghostpad so we can remove it */
1140 g_object_set_data (G_OBJECT (pad), "uridecodebin.ghostpad", newpad);
1142 /* add event probe to monitor tags */
1143 stream = g_slice_alloc0 (sizeof (GstURIDecodeBinStream));
1145 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1146 decoded_pad_event_probe, decoder, NULL);
1147 GST_URI_DECODE_BIN_LOCK (decoder);
1148 g_hash_table_insert (decoder->streams, pad, stream);
1149 GST_URI_DECODE_BIN_UNLOCK (decoder);
1151 gst_pad_set_active (newpad, TRUE);
1152 gst_pad_sticky_events_foreach (pad, copy_sticky_events, newpad);
1153 gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
1156 static GstPadProbeReturn
1157 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
1160 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1161 GstURIDecodeBin *decoder = user_data;
1163 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1165 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1166 GST_DEBUG_OBJECT (pad, "we received EOS");
1168 g_signal_emit (decoder,
1169 gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1171 /* never drop events */
1172 return GST_PAD_PROBE_OK;
1175 /* called when we found a raw pad on the source element. We need to set up a
1176 * padprobe to detect EOS before exposing the pad. */
1178 expose_decoded_pad (GstElement * element, GstPad * pad,
1179 GstURIDecodeBin * decoder)
1181 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1182 source_pad_event_probe, decoder, NULL);
1184 new_decoded_pad_added_cb (element, pad, decoder);
1188 pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
1192 GST_DEBUG_OBJECT (element, "pad removed name: <%s:%s>",
1193 GST_DEBUG_PAD_NAME (pad));
1195 /* we only care about srcpads */
1196 if (!GST_PAD_IS_SRC (pad))
1199 if (!(ghost = g_object_get_data (G_OBJECT (pad), "uridecodebin.ghostpad")))
1202 /* unghost the pad */
1203 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ghost), NULL);
1205 /* deactivate and remove */
1206 gst_pad_set_active (pad, FALSE);
1207 gst_element_remove_pad (GST_ELEMENT_CAST (decoder), ghost);
1214 GST_WARNING_OBJECT (element, "no ghost pad found");
1219 /* helper function to lookup stuff in lists */
1221 array_has_value (const gchar * values[], const gchar * value)
1225 for (i = 0; values[i]; i++) {
1226 if (g_str_has_prefix (value, values[i]))
1233 array_has_uri_value (const gchar * values[], const gchar * value)
1237 for (i = 0; values[i]; i++) {
1238 if (!g_ascii_strncasecmp (value, values[i], strlen (values[i])))
1244 /* list of URIs that we consider to be streams and that need buffering.
1245 * We have no mechanism yet to figure this out with a query. */
1246 static const gchar *stream_uris[] = { "http://", "https://", "mms://",
1247 "mmsh://", "mmsu://", "mmst://", "fd://", "myth://", "ssh://",
1248 "ftp://", "sftp://",
1252 /* list of URIs that need a queue because they are pretty bursty */
1253 static const gchar *queue_uris[] = { "cdda://", NULL };
1255 /* blacklisted URIs, we know they will always fail. */
1256 static const gchar *blacklisted_uris[] = { NULL };
1258 /* media types that use adaptive streaming */
1259 static const gchar *adaptive_media[] = {
1260 "application/x-hls", "application/x-smoothstreaming-manifest",
1261 "application/dash+xml", NULL
1264 #define IS_STREAM_URI(uri) (array_has_uri_value (stream_uris, uri))
1265 #define IS_QUEUE_URI(uri) (array_has_uri_value (queue_uris, uri))
1266 #define IS_BLACKLISTED_URI(uri) (array_has_uri_value (blacklisted_uris, uri))
1267 #define IS_ADAPTIVE_MEDIA(media) (array_has_value (adaptive_media, media))
1270 * Generate and configure a source element.
1273 gen_source_element (GstURIDecodeBin * decoder)
1275 GObjectClass *source_class;
1279 GstSchedulingFlags flags;
1285 GST_LOG_OBJECT (decoder, "finding source for %s", decoder->uri);
1287 if (!gst_uri_is_valid (decoder->uri))
1290 if (IS_BLACKLISTED_URI (decoder->uri))
1291 goto uri_blacklisted;
1294 gst_element_make_from_uri (GST_URI_SRC, decoder->uri, "source", &err);
1298 GST_LOG_OBJECT (decoder, "found source type %s", G_OBJECT_TYPE_NAME (source));
1300 query = gst_query_new_scheduling ();
1301 if (gst_element_query (source, query)) {
1302 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
1303 decoder->is_stream = flags & GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1305 decoder->is_stream = IS_STREAM_URI (decoder->uri);
1306 gst_query_unref (query);
1308 GST_LOG_OBJECT (decoder, "source is stream: %d", decoder->is_stream);
1310 decoder->need_queue = IS_QUEUE_URI (decoder->uri);
1311 GST_LOG_OBJECT (decoder, "source needs queue: %d", decoder->need_queue);
1313 source_class = G_OBJECT_GET_CLASS (source);
1315 /* make HTTP sources send extra headers so we get icecast
1316 * metadata in case the stream is an icecast stream */
1317 if (!strncmp (decoder->uri, "http://", 7)) {
1318 pspec = g_object_class_find_property (source_class, "iradio-mode");
1320 if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
1321 GST_LOG_OBJECT (decoder, "configuring iradio-mode");
1322 g_object_set (source, "iradio-mode", TRUE, NULL);
1326 pspec = g_object_class_find_property (source_class, "connection-speed");
1327 if (pspec != NULL) {
1328 guint64 speed = decoder->connection_speed / 1000;
1329 gboolean wrong_type = FALSE;
1331 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
1332 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
1334 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1335 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
1336 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
1338 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1339 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
1340 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
1342 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1343 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
1344 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
1346 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1348 GST_WARNING_OBJECT (decoder,
1349 "The connection speed property %" G_GUINT64_FORMAT
1350 " of type %s is not usefull not setting it", speed,
1351 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
1355 if (wrong_type == FALSE) {
1356 g_object_set (source, "connection-speed", speed, NULL);
1358 GST_DEBUG_OBJECT (decoder,
1359 "setting connection-speed=%" G_GUINT64_FORMAT " to source element",
1364 pspec = g_object_class_find_property (source_class, "subtitle-encoding");
1365 if (pspec != NULL && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
1366 GST_DEBUG_OBJECT (decoder,
1367 "setting subtitle-encoding=%s to source element", decoder->encoding);
1368 g_object_set (source, "subtitle-encoding", decoder->encoding, NULL);
1375 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1376 (_("No URI specified to play from.")), (NULL));
1381 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1382 (_("Invalid URI \"%s\"."), decoder->uri), (NULL));
1383 g_clear_error (&err);
1388 GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
1389 (_("This stream type cannot be played yet.")), (NULL));
1394 /* whoops, could not create the source element, dig a little deeper to
1395 * figure out what might be wrong. */
1396 if (err != NULL && err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL) {
1399 prot = gst_uri_get_protocol (decoder->uri);
1403 gst_element_post_message (GST_ELEMENT_CAST (decoder),
1404 gst_missing_uri_source_message_new (GST_ELEMENT (decoder), prot));
1406 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
1407 (_("No URI handler implemented for \"%s\"."), prot), (NULL));
1411 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1412 ("%s", (err) ? err->message : "URI was not accepted by any element"),
1413 ("No element accepted URI '%s'", decoder->uri));
1416 g_clear_error (&err);
1424 * @all_raw: pointer to hold the result
1426 * check if the caps of the pad are all raw. The caps are all raw if
1427 * all of its structures contain audio/x-raw or video/x-raw.
1429 * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
1432 has_all_raw_caps (GstPad * pad, GstCaps * rawcaps, gboolean * all_raw)
1434 GstCaps *caps, *intersection;
1436 gboolean res = FALSE;
1438 caps = gst_pad_query_caps (pad, NULL);
1442 GST_DEBUG_OBJECT (pad, "have caps %" GST_PTR_FORMAT, caps);
1444 capssize = gst_caps_get_size (caps);
1445 /* no caps, skip and move to the next pad */
1446 if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1449 intersection = gst_caps_intersect (caps, rawcaps);
1450 *all_raw = !gst_caps_is_empty (intersection)
1451 && (gst_caps_get_size (intersection) == capssize);
1452 gst_caps_unref (intersection);
1457 gst_caps_unref (caps);
1462 post_missing_plugin_error (GstElement * dec, const gchar * element_name)
1466 msg = gst_missing_element_message_new (dec, element_name);
1467 gst_element_post_message (dec, msg);
1469 GST_ELEMENT_ERROR (dec, CORE, MISSING_PLUGIN,
1470 (_("Missing element '%s' - check your GStreamer installation."),
1471 element_name), (NULL));
1476 * @decoder: a #GstURIDecodeBin
1477 * @is_raw: are all pads raw data
1478 * @have_out: does the source have output
1479 * @is_dynamic: is this a dynamic source
1480 * @use_queue: put a queue before raw output pads
1482 * Check the source of @decoder and collect information about it.
1484 * @is_raw will be set to TRUE if the source only produces raw pads. When this
1485 * function returns, all of the raw pad of the source will be added
1488 * @have_out: will be set to TRUE if the source has output pads.
1490 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1493 * Returns: FALSE if a fatal error occured while scanning.
1496 analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
1497 gboolean * have_out, gboolean * is_dynamic, gboolean use_queue)
1499 GstIterator *pads_iter;
1500 gboolean done = FALSE;
1501 gboolean res = TRUE;
1504 GValue item = { 0, };
1508 *is_dynamic = FALSE;
1510 g_object_get (decoder, "caps", &rawcaps, NULL);
1512 rawcaps = DEFAULT_CAPS;
1514 pads_iter = gst_element_iterate_src_pads (decoder->source);
1516 switch (gst_iterator_next (pads_iter, &item)) {
1517 case GST_ITERATOR_ERROR:
1520 case GST_ITERATOR_DONE:
1523 case GST_ITERATOR_RESYNC:
1524 /* reset results and resync */
1527 *is_dynamic = FALSE;
1528 gst_iterator_resync (pads_iter);
1530 case GST_ITERATOR_OK:
1531 pad = g_value_dup_object (&item);
1532 /* we now officially have an ouput pad */
1535 /* if FALSE, this pad has no caps and we continue with the next pad. */
1536 if (!has_all_raw_caps (pad, rawcaps, is_raw)) {
1537 gst_object_unref (pad);
1538 g_value_reset (&item);
1542 /* caps on source pad are all raw, we can add the pad */
1544 GstElement *outelem;
1549 /* insert a queue element right before the raw pad */
1550 outelem = gst_element_factory_make ("queue2", NULL);
1554 gst_bin_add (GST_BIN_CAST (decoder), outelem);
1556 sinkpad = gst_element_get_static_pad (outelem, "sink");
1557 gst_pad_link (pad, sinkpad);
1558 gst_object_unref (sinkpad);
1560 /* save queue pointer so we can remove it later */
1561 decoder->queue = outelem;
1563 /* get the new raw srcpad */
1564 gst_object_unref (pad);
1565 pad = gst_element_get_static_pad (outelem, "src");
1567 outelem = decoder->source;
1569 expose_decoded_pad (outelem, pad, decoder);
1571 gst_object_unref (pad);
1572 g_value_reset (&item);
1576 g_value_unset (&item);
1577 gst_iterator_free (pads_iter);
1578 gst_caps_unref (rawcaps);
1581 GstElementClass *elemclass;
1584 /* element has no output pads, check for padtemplates that list SOMETIMES
1586 elemclass = GST_ELEMENT_GET_CLASS (decoder->source);
1588 walk = gst_element_class_get_pad_template_list (elemclass);
1589 while (walk != NULL) {
1590 GstPadTemplate *templ;
1592 templ = (GstPadTemplate *) walk->data;
1593 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1594 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES)
1598 walk = g_list_next (walk);
1605 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1607 gst_object_unref (pad);
1608 g_value_unset (&item);
1609 gst_iterator_free (pads_iter);
1610 gst_caps_unref (rawcaps);
1616 /* Remove all decodebin from ourself
1617 * If force is FALSE, then the decodebin instances will be stored in
1618 * pending_decodebins for re-use later on.
1619 * If force is TRUE, then all decodebin instances will be unreferenced
1620 * and cleared, including the pending ones. */
1622 remove_decoders (GstURIDecodeBin * bin, gboolean force)
1626 for (walk = bin->decodebins; walk; walk = g_slist_next (walk)) {
1627 GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1629 GST_DEBUG_OBJECT (bin, "removing old decoder element");
1631 gst_element_set_state (decoder, GST_STATE_NULL);
1632 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1636 gst_element_set_state (decoder, GST_STATE_READY);
1637 /* add it to our list of pending decodebins */
1638 g_object_ref (decoder);
1639 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1640 /* restore some properties we might have changed */
1641 g_object_set (decoder, "sink-caps", NULL, NULL);
1642 caps = DEFAULT_CAPS;
1643 g_object_set (decoder, "caps", caps, NULL);
1644 gst_caps_unref (caps);
1645 /* make it freshly floating again */
1646 g_object_force_floating (G_OBJECT (decoder));
1648 bin->pending_decodebins =
1649 g_slist_prepend (bin->pending_decodebins, decoder);
1652 g_slist_free (bin->decodebins);
1653 bin->decodebins = NULL;
1657 for (tmp = bin->pending_decodebins; tmp; tmp = tmp->next) {
1658 gst_element_set_state ((GstElement *) tmp->data, GST_STATE_NULL);
1659 gst_object_unref ((GstElement *) tmp->data);
1661 g_slist_free (bin->pending_decodebins);
1662 bin->pending_decodebins = NULL;
1666 /* Don't loose the SOURCE flag */
1667 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
1671 proxy_unknown_type_signal (GstElement * decodebin, GstPad * pad, GstCaps * caps,
1672 GstURIDecodeBin * dec)
1674 GST_DEBUG_OBJECT (dec, "unknown-type signaled");
1677 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1681 proxy_autoplug_continue_signal (GstElement * decodebin, GstPad * pad,
1682 GstCaps * caps, GstURIDecodeBin * dec)
1687 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, pad, caps,
1690 GST_DEBUG_OBJECT (dec, "autoplug-continue returned %d", result);
1695 static GValueArray *
1696 proxy_autoplug_factories_signal (GstElement * decodebin, GstPad * pad,
1697 GstCaps * caps, GstURIDecodeBin * dec)
1699 GValueArray *result;
1702 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, pad, caps,
1705 GST_DEBUG_OBJECT (dec, "autoplug-factories returned %p", result);
1710 static GValueArray *
1711 proxy_autoplug_sort_signal (GstElement * decodebin, GstPad * pad,
1712 GstCaps * caps, GValueArray * factories, GstURIDecodeBin * dec)
1714 GValueArray *result;
1717 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, pad, caps,
1718 factories, &result);
1720 GST_DEBUG_OBJECT (dec, "autoplug-sort returned %p", result);
1725 static GstAutoplugSelectResult
1726 proxy_autoplug_select_signal (GstElement * decodebin, GstPad * pad,
1727 GstCaps * caps, GstElementFactory * factory, GstURIDecodeBin * dec)
1729 GstAutoplugSelectResult result;
1732 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, factory,
1735 GST_DEBUG_OBJECT (dec, "autoplug-select returned %d", result);
1741 proxy_autoplug_query_signal (GstElement * decodebin, GstPad * pad,
1742 GstElement * element, GstQuery * query, GstURIDecodeBin * dec)
1744 gboolean ret = FALSE;
1747 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY], 0, pad, element, query,
1750 GST_DEBUG_OBJECT (dec, "autoplug-query returned %d", ret);
1756 proxy_drained_signal (GstElement * decodebin, GstURIDecodeBin * dec)
1758 GST_DEBUG_OBJECT (dec, "drained signaled");
1760 g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1763 /* make a decodebin and connect to all the signals */
1765 make_decoder (GstURIDecodeBin * decoder)
1767 GstElement *decodebin;
1769 /* re-use pending decodebin */
1770 if (decoder->pending_decodebins) {
1771 GSList *first = decoder->pending_decodebins;
1772 GST_LOG_OBJECT (decoder, "re-using pending decodebin");
1773 decodebin = (GstElement *) first->data;
1774 decoder->pending_decodebins =
1775 g_slist_delete_link (decoder->pending_decodebins, first);
1777 GST_LOG_OBJECT (decoder, "making new decodebin");
1779 /* now create the decoder element */
1780 decodebin = gst_element_factory_make ("decodebin", NULL);
1786 if (decodebin->numsinkpads == 0)
1789 /* connect signals to proxy */
1790 g_signal_connect (decodebin, "unknown-type",
1791 G_CALLBACK (proxy_unknown_type_signal), decoder);
1792 g_signal_connect (decodebin, "autoplug-continue",
1793 G_CALLBACK (proxy_autoplug_continue_signal), decoder);
1794 g_signal_connect (decodebin, "autoplug-factories",
1795 G_CALLBACK (proxy_autoplug_factories_signal), decoder);
1796 g_signal_connect (decodebin, "autoplug-sort",
1797 G_CALLBACK (proxy_autoplug_sort_signal), decoder);
1798 g_signal_connect (decodebin, "autoplug-select",
1799 G_CALLBACK (proxy_autoplug_select_signal), decoder);
1800 g_signal_connect (decodebin, "autoplug-query",
1801 G_CALLBACK (proxy_autoplug_query_signal), decoder);
1802 g_signal_connect (decodebin, "drained",
1803 G_CALLBACK (proxy_drained_signal), decoder);
1805 /* set up callbacks to create the links between decoded data
1806 * and video/audio/subtitle rendering/output. */
1807 g_signal_connect (decodebin,
1808 "pad-added", G_CALLBACK (new_decoded_pad_added_cb), decoder);
1809 g_signal_connect (decodebin,
1810 "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
1811 g_signal_connect (decodebin, "no-more-pads",
1812 G_CALLBACK (no_more_pads), decoder);
1813 g_signal_connect (decodebin,
1814 "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
1817 /* configure caps if we have any */
1819 g_object_set (decodebin, "caps", decoder->caps, NULL);
1821 /* Propagate expose-all-streams and connection-speed properties */
1822 g_object_set (decodebin, "expose-all-streams", decoder->expose_allstreams,
1823 "connection-speed", decoder->connection_speed / 1000, NULL);
1825 if (!decoder->is_stream || decoder->is_adaptive) {
1826 /* propagate the use-buffering property but only when we are not already
1827 * doing stream buffering with queue2. FIXME, we might want to do stream
1828 * buffering with the multiqueue buffering instead of queue2. */
1829 g_object_set (decodebin, "use-buffering", decoder->use_buffering
1830 || decoder->is_adaptive, NULL);
1832 if (decoder->use_buffering || decoder->is_adaptive) {
1836 /* configure sizes when buffering */
1837 if ((max_bytes = decoder->buffer_size) == -1)
1838 max_bytes = 2 * 1024 * 1024;
1839 if ((max_time = decoder->buffer_duration) == -1)
1840 max_time = 5 * GST_SECOND;
1842 g_object_set (decodebin, "max-size-bytes", max_bytes, "max-size-buffers",
1843 (guint) 0, "max-size-time", max_time, NULL);
1847 g_object_set_data (G_OBJECT (decodebin), "pending", GINT_TO_POINTER (1));
1848 g_object_set (decodebin, "subtitle-encoding", decoder->encoding, NULL);
1850 GST_LOG_OBJECT (decoder, "have %d pending dynamic objects", decoder->pending);
1852 gst_bin_add (GST_BIN_CAST (decoder), decodebin);
1854 decoder->decodebins = g_slist_prepend (decoder->decodebins, decodebin);
1861 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "decodebin");
1862 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1863 ("No decodebin element, check your installation"));
1868 gst_object_unref (decodebin);
1869 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1870 ("No typefind element, decodebin is unusable, check your installation"));
1875 /* signaled when we have a stream and we need to configure the download
1876 * buffering or regular buffering */
1878 type_found (GstElement * typefind, guint probability,
1879 GstCaps * caps, GstURIDecodeBin * decoder)
1881 GstElement *src_elem, *dec_elem, *queue = NULL;
1883 const gchar *media_type;
1884 gboolean do_download = FALSE;
1886 GST_DEBUG_OBJECT (decoder, "typefind found caps %" GST_PTR_FORMAT, caps);
1888 s = gst_caps_get_structure (caps, 0);
1889 media_type = gst_structure_get_name (s);
1891 decoder->is_adaptive = IS_ADAPTIVE_MEDIA (media_type);
1893 /* only enable download buffering if the upstream duration is known */
1894 if (decoder->download) {
1897 do_download = (gst_element_query_duration (typefind, GST_FORMAT_BYTES, &dur)
1901 dec_elem = make_decoder (decoder);
1905 if (decoder->is_adaptive) {
1906 src_elem = typefind;
1908 queue = gst_element_factory_make ("queue2", NULL);
1912 g_object_set (queue, "use-buffering", TRUE, NULL);
1913 g_object_set (queue, "ring-buffer-max-size", decoder->ring_buffer_max_size,
1915 decoder->queue = queue;
1917 GST_DEBUG_OBJECT (decoder, "check media-type %s, %d", media_type,
1921 gchar *temp_template, *filename;
1922 const gchar *tmp_dir, *prgname;
1924 tmp_dir = g_get_user_cache_dir ();
1925 prgname = g_get_prgname ();
1926 if (prgname == NULL)
1927 prgname = "GStreamer";
1929 filename = g_strdup_printf ("%s-XXXXXX", prgname);
1931 /* build our filename */
1932 temp_template = g_build_filename (tmp_dir, filename, NULL);
1934 GST_DEBUG_OBJECT (decoder, "enable download buffering in %s (%s, %s, %s)",
1935 temp_template, tmp_dir, prgname, filename);
1937 /* configure progressive download for selected media types */
1938 g_object_set (queue, "temp-template", temp_template, NULL);
1941 g_free (temp_template);
1944 /* Disable max-size-buffers */
1945 g_object_set (queue, "max-size-buffers", 0, NULL);
1947 /* If buffer size or duration are set, set them on the queue2 element */
1948 if (decoder->buffer_size != -1)
1949 g_object_set (queue, "max-size-bytes", decoder->buffer_size, NULL);
1950 if (decoder->buffer_duration != -1)
1951 g_object_set (queue, "max-size-time", decoder->buffer_duration, NULL);
1953 gst_bin_add (GST_BIN_CAST (decoder), queue);
1955 if (!gst_element_link_pads (typefind, "src", queue, "sink"))
1956 goto could_not_link;
1960 /* to force caps on the decodebin element and avoid reparsing stuff by
1961 * typefind. It also avoids a deadlock in the way typefind activates pads in
1962 * the state change */
1963 g_object_set (dec_elem, "sink-caps", caps, NULL);
1965 if (!gst_element_link_pads (src_elem, "src", dec_elem, "sink"))
1966 goto could_not_link;
1968 /* PLAYING in one go might fail (see bug #632782) */
1969 gst_element_set_state (dec_elem, GST_STATE_PAUSED);
1970 gst_element_set_state (dec_elem, GST_STATE_PLAYING);
1972 gst_element_set_state (queue, GST_STATE_PLAYING);
1974 do_async_done (decoder);
1981 /* error was posted */
1986 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1987 (NULL), ("Can't link typefind to decodebin element"));
1992 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1997 /* setup a streaming source. This will first plug a typefind element to the
1998 * source. After we find the type, we decide to plug a queue2 and continue to
1999 * plug a decodebin starting from the found caps */
2001 setup_streaming (GstURIDecodeBin * decoder)
2003 GstElement *typefind;
2005 /* now create the decoder element */
2006 typefind = gst_element_factory_make ("typefind", NULL);
2010 gst_bin_add (GST_BIN_CAST (decoder), typefind);
2012 if (!gst_element_link_pads (decoder->source, NULL, typefind, "sink"))
2013 goto could_not_link;
2015 decoder->typefind = typefind;
2017 /* connect a signal to find out when the typefind element found
2019 decoder->have_type_id =
2020 g_signal_connect (decoder->typefind, "have-type",
2021 G_CALLBACK (type_found), decoder);
2023 do_async_start (decoder);
2030 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "typefind");
2031 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
2032 ("No typefind element, check your installation"));
2037 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2038 (NULL), ("Can't link source to typefind element"));
2039 gst_bin_remove (GST_BIN_CAST (decoder), typefind);
2040 /* Don't loose the SOURCE flag */
2041 GST_OBJECT_FLAG_SET (decoder, GST_ELEMENT_FLAG_SOURCE);
2047 free_stream (gpointer value)
2049 g_slice_free (GstURIDecodeBinStream, value);
2052 /* remove source and all related elements */
2054 remove_source (GstURIDecodeBin * bin)
2056 GstElement *source = bin->source;
2059 GST_DEBUG_OBJECT (bin, "removing old src element");
2060 gst_element_set_state (source, GST_STATE_NULL);
2062 if (bin->src_np_sig_id) {
2063 g_signal_handler_disconnect (source, bin->src_np_sig_id);
2064 bin->src_np_sig_id = 0;
2066 if (bin->src_nmp_sig_id) {
2067 g_signal_handler_disconnect (source, bin->src_nmp_sig_id);
2068 bin->src_nmp_sig_id = 0;
2070 gst_bin_remove (GST_BIN_CAST (bin), source);
2074 GST_DEBUG_OBJECT (bin, "removing old queue element");
2075 gst_element_set_state (bin->queue, GST_STATE_NULL);
2076 gst_bin_remove (GST_BIN_CAST (bin), bin->queue);
2079 if (bin->typefind) {
2080 GST_DEBUG_OBJECT (bin, "removing old typefind element");
2081 gst_element_set_state (bin->typefind, GST_STATE_NULL);
2082 gst_bin_remove (GST_BIN_CAST (bin), bin->typefind);
2083 bin->typefind = NULL;
2086 g_hash_table_destroy (bin->streams);
2087 bin->streams = NULL;
2089 /* Don't loose the SOURCE flag */
2090 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
2093 /* is called when a dynamic source element created a new pad. */
2095 source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
2097 GstElement *decoder;
2101 GST_URI_DECODE_BIN_LOCK (bin);
2102 GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
2103 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
2105 g_object_get (bin, "caps", &rawcaps, NULL);
2107 rawcaps = DEFAULT_CAPS;
2109 /* if this is a pad with all raw caps, we can expose it */
2110 if (has_all_raw_caps (pad, rawcaps, &is_raw) && is_raw) {
2111 /* it's all raw, create output pads. */
2112 GST_URI_DECODE_BIN_UNLOCK (bin);
2113 gst_caps_unref (rawcaps);
2114 expose_decoded_pad (element, pad, bin);
2117 gst_caps_unref (rawcaps);
2119 /* not raw, create decoder */
2120 decoder = make_decoder (bin);
2124 /* and link to decoder */
2125 if (!gst_element_link_pads (bin->source, NULL, decoder, "sink"))
2126 goto could_not_link;
2128 GST_DEBUG_OBJECT (bin, "linked decoder to new pad");
2130 gst_element_set_state (decoder, GST_STATE_PLAYING);
2131 GST_URI_DECODE_BIN_UNLOCK (bin);
2138 /* error was posted */
2139 GST_URI_DECODE_BIN_UNLOCK (bin);
2144 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
2145 (NULL), ("Can't link source to decoder element"));
2146 GST_URI_DECODE_BIN_UNLOCK (bin);
2152 is_live_source (GstElement * source)
2154 GObjectClass *source_class = NULL;
2155 gboolean is_live = FALSE;
2158 source_class = G_OBJECT_GET_CLASS (source);
2159 pspec = g_object_class_find_property (source_class, "is-live");
2160 if (!pspec || G_PARAM_SPEC_VALUE_TYPE (pspec) != G_TYPE_BOOLEAN)
2163 g_object_get (G_OBJECT (source), "is-live", &is_live, NULL);
2168 /* construct and run the source and decoder elements until we found
2169 * all the streams or until a preroll queue has been filled.
2172 setup_source (GstURIDecodeBin * decoder)
2174 gboolean is_raw, have_out, is_dynamic;
2176 GST_DEBUG_OBJECT (decoder, "setup source");
2178 /* delete old src */
2179 remove_source (decoder);
2181 decoder->pending = 0;
2183 /* create and configure an element that can handle the uri */
2184 if (!(decoder->source = gen_source_element (decoder)))
2187 /* state will be merged later - if file is not found, error will be
2188 * handled by the application right after. */
2189 gst_bin_add (GST_BIN_CAST (decoder), decoder->source);
2191 /* notify of the new source used */
2192 g_object_notify (G_OBJECT (decoder), "source");
2194 g_signal_emit (decoder, gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP],
2195 0, decoder->source);
2197 if (is_live_source (decoder->source))
2198 decoder->is_stream = FALSE;
2200 /* remove the old decoders now, if any */
2201 remove_decoders (decoder, FALSE);
2203 /* stream admin setup */
2204 decoder->streams = g_hash_table_new_full (NULL, NULL, NULL, free_stream);
2206 /* see if the source element emits raw audio/video all by itself,
2207 * if so, we can create streams for the pads and be done with it.
2208 * Also check that is has source pads, if not, we assume it will
2209 * do everything itself. */
2210 if (!analyse_source (decoder, &is_raw, &have_out, &is_dynamic,
2211 decoder->need_queue))
2212 goto invalid_source;
2215 GST_DEBUG_OBJECT (decoder, "Source provides all raw data");
2216 /* source provides raw data, we added the pads and we can now signal a
2217 * no_more pads because we are done. */
2218 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
2221 if (!have_out && !is_dynamic) {
2222 GST_DEBUG_OBJECT (decoder, "Source has no output pads");
2223 /* create a stream to indicate that this uri is handled by a self
2224 * contained element. We are now done. */
2225 add_element_stream (decoder->source, decoder);
2229 GST_DEBUG_OBJECT (decoder, "Source has dynamic output pads");
2230 /* connect a handler for the new-pad signal */
2231 decoder->src_np_sig_id =
2232 g_signal_connect (decoder->source, "pad-added",
2233 G_CALLBACK (source_new_pad), decoder);
2234 decoder->src_nmp_sig_id =
2235 g_signal_connect (decoder->source, "no-more-pads",
2236 G_CALLBACK (source_no_more_pads), decoder);
2237 g_object_set_data (G_OBJECT (decoder->source), "pending",
2238 GINT_TO_POINTER (1));
2241 if (decoder->is_stream) {
2242 GST_DEBUG_OBJECT (decoder, "Setting up streaming");
2243 /* do the stream things here */
2244 if (!setup_streaming (decoder))
2245 goto streaming_failed;
2247 GstElement *dec_elem;
2249 /* no streaming source, we can link now */
2250 GST_DEBUG_OBJECT (decoder, "Plugging decodebin to source");
2252 dec_elem = make_decoder (decoder);
2256 if (!gst_element_link_pads (decoder->source, NULL, dec_elem, "sink"))
2257 goto could_not_link;
2265 /* error message was already posted */
2270 GST_ELEMENT_ERROR (decoder, CORE, FAILED,
2271 (_("Source element is invalid.")), (NULL));
2276 /* message was posted */
2281 /* message was posted */
2286 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2287 (NULL), ("Can't link source to decoder element"));
2293 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
2294 GList * structure_list)
2298 for (l = structure_list; l != NULL; l = l->next) {
2299 GValue val = { 0, };
2302 *first = gst_structure_copy ((GstStructure *) l->data);
2304 g_value_init (&val, GST_TYPE_STRUCTURE);
2305 g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
2306 gst_value_list_append_value (list_val, &val);
2307 g_value_unset (&val);
2311 /* if it's a redirect message with multiple redirect locations we might
2312 * want to pick a different 'best' location depending on the required
2313 * bitrates and the connection speed */
2315 handle_redirect_message (GstURIDecodeBin * dec, GstMessage * msg)
2317 const GValue *locations_list, *location_val;
2318 GstMessage *new_msg;
2319 GstStructure *new_structure = NULL;
2320 GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
2321 GValue new_list = { 0, };
2323 const GstStructure *structure;
2325 GST_DEBUG_OBJECT (dec, "redirect message: %" GST_PTR_FORMAT, msg);
2326 GST_DEBUG_OBJECT (dec, "connection speed: %" G_GUINT64_FORMAT,
2327 dec->connection_speed);
2329 structure = gst_message_get_structure (msg);
2330 if (dec->connection_speed == 0 || structure == NULL)
2333 locations_list = gst_structure_get_value (structure, "locations");
2334 if (locations_list == NULL)
2337 size = gst_value_list_get_size (locations_list);
2341 /* maintain existing order as much as possible, just sort references
2342 * with too high a bitrate to the end (the assumption being that if
2343 * bitrates are given they are given for all interesting streams and
2344 * that the you-need-at-least-version-xyz redirect has the same bitrate
2345 * as the lowest referenced redirect alternative) */
2346 for (i = 0; i < size; ++i) {
2347 const GstStructure *s;
2350 location_val = gst_value_list_get_value (locations_list, i);
2351 s = (const GstStructure *) g_value_get_boxed (location_val);
2352 if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
2353 GST_DEBUG_OBJECT (dec, "no bitrate: %" GST_PTR_FORMAT, s);
2354 l_neutral = g_list_append (l_neutral, (gpointer) s);
2355 } else if (bitrate > dec->connection_speed) {
2356 GST_DEBUG_OBJECT (dec, "bitrate too high: %" GST_PTR_FORMAT, s);
2357 l_bad = g_list_append (l_bad, (gpointer) s);
2358 } else if (bitrate <= dec->connection_speed) {
2359 GST_DEBUG_OBJECT (dec, "bitrate OK: %" GST_PTR_FORMAT, s);
2360 l_good = g_list_append (l_good, (gpointer) s);
2364 g_value_init (&new_list, GST_TYPE_LIST);
2365 value_list_append_structure_list (&new_list, &new_structure, l_good);
2366 value_list_append_structure_list (&new_list, &new_structure, l_neutral);
2367 value_list_append_structure_list (&new_list, &new_structure, l_bad);
2368 gst_structure_take_value (new_structure, "locations", &new_list);
2370 g_list_free (l_good);
2371 g_list_free (l_neutral);
2372 g_list_free (l_bad);
2374 new_msg = gst_message_new_element (msg->src, new_structure);
2375 gst_message_unref (msg);
2377 GST_DEBUG_OBJECT (dec, "new redirect message: %" GST_PTR_FORMAT, new_msg);
2382 handle_message (GstBin * bin, GstMessage * msg)
2384 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT
2385 && gst_message_has_name (msg, "redirect")) {
2386 /* sort redirect messages based on the connection speed. This simplifies
2387 * the user of this element as it can in most cases just pick the first item
2388 * of the sorted list as a good redirection candidate. It can of course
2389 * choose something else from the list if it has a better way. */
2390 msg = handle_redirect_message (GST_URI_DECODE_BIN (bin), msg);
2392 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
2395 /* generic struct passed to all query fold methods
2396 * FIXME, move to core.
2407 typedef void (*QueryInitFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2408 typedef void (*QueryDoneFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2410 /* for duration/position we collect all durations/positions and take
2411 * the MAX of all valid results */
2413 decoder_query_init (GstURIDecodeBin * dec, QueryFold * fold)
2417 fold->seekable = TRUE;
2422 decoder_query_duration_fold (const GValue * item, GValue * ret,
2425 GstPad *pad = g_value_get_object (item);
2427 if (gst_pad_query (pad, fold->query)) {
2430 g_value_set_boolean (ret, TRUE);
2432 gst_query_parse_duration (fold->query, NULL, &duration);
2434 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
2436 if (duration > fold->max)
2437 fold->max = duration;
2443 decoder_query_duration_done (GstURIDecodeBin * dec, QueryFold * fold)
2447 gst_query_parse_duration (fold->query, &format, NULL);
2448 /* store max in query result */
2449 gst_query_set_duration (fold->query, format, fold->max);
2451 GST_DEBUG ("max duration %" G_GINT64_FORMAT, fold->max);
2455 decoder_query_position_fold (const GValue * item, GValue * ret,
2458 GstPad *pad = g_value_get_object (item);
2460 if (gst_pad_query (pad, fold->query)) {
2463 g_value_set_boolean (ret, TRUE);
2465 gst_query_parse_position (fold->query, NULL, &position);
2467 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
2469 if (position > fold->max)
2470 fold->max = position;
2477 decoder_query_position_done (GstURIDecodeBin * dec, QueryFold * fold)
2481 gst_query_parse_position (fold->query, &format, NULL);
2482 /* store max in query result */
2483 gst_query_set_position (fold->query, format, fold->max);
2485 GST_DEBUG_OBJECT (dec, "max position %" G_GINT64_FORMAT, fold->max);
2489 decoder_query_latency_fold (const GValue * item, GValue * ret, QueryFold * fold)
2491 GstPad *pad = g_value_get_object (item);
2493 if (gst_pad_query (pad, fold->query)) {
2494 GstClockTime min, max;
2497 g_value_set_boolean (ret, TRUE);
2499 gst_query_parse_latency (fold->query, &live, &min, &max);
2501 GST_DEBUG_OBJECT (item,
2502 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2503 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
2505 /* for the combined latency we collect the MAX of all min latencies and
2506 * the MIN of all max latencies */
2507 if (min > fold->min)
2509 if (fold->max == -1)
2511 else if (max < fold->max)
2513 if (fold->live == FALSE)
2521 decoder_query_latency_done (GstURIDecodeBin * dec, QueryFold * fold)
2523 /* store max in query result */
2524 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
2526 GST_DEBUG_OBJECT (dec,
2527 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2528 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
2532 /* we are seekable if all srcpads are seekable */
2534 decoder_query_seeking_fold (const GValue * item, GValue * ret, QueryFold * fold)
2536 GstPad *pad = g_value_get_object (item);
2538 if (gst_pad_query (pad, fold->query)) {
2541 g_value_set_boolean (ret, TRUE);
2542 gst_query_parse_seeking (fold->query, NULL, &seekable, NULL, NULL);
2544 GST_DEBUG_OBJECT (item, "got seekable %d", seekable);
2546 if (fold->seekable == TRUE)
2547 fold->seekable = seekable;
2554 decoder_query_seeking_done (GstURIDecodeBin * dec, QueryFold * fold)
2558 gst_query_parse_seeking (fold->query, &format, NULL, NULL, NULL);
2559 gst_query_set_seeking (fold->query, format, fold->seekable, 0, -1);
2561 GST_DEBUG_OBJECT (dec, "seekable %d", fold->seekable);
2564 /* generic fold, return first valid result */
2566 decoder_query_generic_fold (const GValue * item, GValue * ret, QueryFold * fold)
2568 GstPad *pad = g_value_get_object (item);
2571 if ((res = gst_pad_query (pad, fold->query))) {
2572 g_value_set_boolean (ret, TRUE);
2573 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
2576 /* and stop as soon as we have a valid result */
2581 /* we're a bin, the default query handler iterates sink elements, which we don't
2582 * have normally. We should just query all source pads.
2585 gst_uri_decode_bin_query (GstElement * element, GstQuery * query)
2587 GstURIDecodeBin *decoder;
2588 gboolean res = FALSE;
2590 GstIteratorFoldFunction fold_func;
2591 QueryInitFunction fold_init = NULL;
2592 QueryDoneFunction fold_done = NULL;
2593 QueryFold fold_data;
2596 decoder = GST_URI_DECODE_BIN (element);
2598 switch (GST_QUERY_TYPE (query)) {
2599 case GST_QUERY_DURATION:
2600 /* iterate and collect durations */
2601 fold_func = (GstIteratorFoldFunction) decoder_query_duration_fold;
2602 fold_init = decoder_query_init;
2603 fold_done = decoder_query_duration_done;
2605 case GST_QUERY_POSITION:
2606 /* iterate and collect durations */
2607 fold_func = (GstIteratorFoldFunction) decoder_query_position_fold;
2608 fold_init = decoder_query_init;
2609 fold_done = decoder_query_position_done;
2611 case GST_QUERY_LATENCY:
2612 /* iterate and collect durations */
2613 fold_func = (GstIteratorFoldFunction) decoder_query_latency_fold;
2614 fold_init = decoder_query_init;
2615 fold_done = decoder_query_latency_done;
2617 case GST_QUERY_SEEKING:
2618 /* iterate and collect durations */
2619 fold_func = (GstIteratorFoldFunction) decoder_query_seeking_fold;
2620 fold_init = decoder_query_init;
2621 fold_done = decoder_query_seeking_done;
2624 fold_func = (GstIteratorFoldFunction) decoder_query_generic_fold;
2628 fold_data.query = query;
2630 g_value_init (&ret, G_TYPE_BOOLEAN);
2631 g_value_set_boolean (&ret, FALSE);
2633 iter = gst_element_iterate_src_pads (element);
2634 GST_DEBUG_OBJECT (element, "Sending query %p (type %d) to src pads",
2635 query, GST_QUERY_TYPE (query));
2638 fold_init (decoder, &fold_data);
2641 GstIteratorResult ires;
2643 ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
2646 case GST_ITERATOR_RESYNC:
2647 gst_iterator_resync (iter);
2649 fold_init (decoder, &fold_data);
2650 g_value_set_boolean (&ret, FALSE);
2652 case GST_ITERATOR_OK:
2653 case GST_ITERATOR_DONE:
2654 res = g_value_get_boolean (&ret);
2655 if (fold_done != NULL && res)
2656 fold_done (decoder, &fold_data);
2664 gst_iterator_free (iter);
2669 static GstStateChangeReturn
2670 gst_uri_decode_bin_change_state (GstElement * element,
2671 GstStateChange transition)
2673 GstStateChangeReturn ret;
2674 GstURIDecodeBin *decoder;
2676 decoder = GST_URI_DECODE_BIN (element);
2678 switch (transition) {
2679 case GST_STATE_CHANGE_READY_TO_PAUSED:
2680 if (!setup_source (decoder))
2687 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2689 switch (transition) {
2690 case GST_STATE_CHANGE_READY_TO_PAUSED:
2691 GST_DEBUG ("ready to paused");
2692 if (ret == GST_STATE_CHANGE_FAILURE)
2695 case GST_STATE_CHANGE_PAUSED_TO_READY:
2696 GST_DEBUG ("paused to ready");
2697 remove_decoders (decoder, FALSE);
2698 remove_source (decoder);
2699 do_async_done (decoder);
2701 case GST_STATE_CHANGE_READY_TO_NULL:
2702 GST_DEBUG ("ready to null");
2703 remove_decoders (decoder, TRUE);
2704 remove_source (decoder);
2714 return GST_STATE_CHANGE_FAILURE;
2718 /* clean up leftover groups */
2719 return GST_STATE_CHANGE_FAILURE;
2724 gst_uri_decode_bin_plugin_init (GstPlugin * plugin)
2726 GST_DEBUG_CATEGORY_INIT (gst_uri_decode_bin_debug, "uridecodebin", 0,
2727 "URI decoder element");
2729 return gst_element_register (plugin, "uridecodebin", GST_RANK_NONE,
2730 GST_TYPE_URI_DECODE_BIN);