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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 decodebin2.
34 #include <gst/gst-i18n-plugin.h>
35 #include <gst/pbutils/missing-plugins.h>
37 #include "gstplay-marshal.h"
38 #include "gstplay-enum.h"
39 #include "gstrawcaps.h"
41 #include "gst/glib-compat-private.h"
43 #define GST_TYPE_URI_DECODE_BIN \
44 (gst_uri_decode_bin_get_type())
45 #define GST_URI_DECODE_BIN(obj) \
46 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBin))
47 #define GST_URI_DECODE_BIN_CLASS(klass) \
48 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBinClass))
49 #define GST_IS_URI_DECODE_BIN(obj) \
50 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DECODE_BIN))
51 #define GST_IS_URI_DECODE_BIN_CLASS(klass) \
52 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DECODE_BIN))
53 #define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
55 typedef struct _GstURIDecodeBin GstURIDecodeBin;
56 typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;
58 #define GST_URI_DECODE_BIN_GET_LOCK(dec) (((GstURIDecodeBin*)(dec))->lock)
59 #define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
60 #define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
62 typedef struct _GstURIDecodeBinStream
66 } GstURIDecodeBinStream;
71 * uridecodebin element struct
73 struct _GstURIDecodeBin
75 GstBin parent_instance;
77 GMutex *lock; /* lock for constructing */
79 GMutex *factories_lock;
80 guint32 factories_cookie;
81 GList *factories; /* factories we can use for selecting elements */
84 guint connection_speed;
91 guint64 buffer_duration; /* When buffering, buffer duration (ns) */
92 guint buffer_size; /* When buffering, buffer size (bytes) */
94 gboolean use_buffering;
99 guint have_type_id; /* have-type signal id from typefind */
101 GSList *pending_decodebins;
105 /* for dynamic sources */
106 guint src_np_sig_id; /* new-pad signal id */
107 guint src_nmp_sig_id; /* no-more-pads signal id */
110 gboolean async_pending; /* async-start has been emitted */
112 gboolean expose_allstreams; /* Whether to expose unknow type streams or not */
114 guint64 ring_buffer_max_size; /* 0 means disabled */
117 struct _GstURIDecodeBinClass
119 GstBinClass parent_class;
121 /* signal fired when we found a pad that we cannot decode */
122 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
124 /* signal fired to know if we continue trying to decode the given caps */
125 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
127 /* signal fired to get a list of factories to try to autoplug */
128 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
130 /* signal fired to sort the factories */
131 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
132 GstCaps * caps, GValueArray * factories);
133 /* signal fired to select from the proposed list of factories */
134 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
135 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
137 /* emitted when all data is decoded */
138 void (*drained) (GstElement * element);
141 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
144 GST_STATIC_CAPS_ANY);
146 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
148 GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
149 #define GST_CAT_DEFAULT gst_uri_decode_bin_debug
155 SIGNAL_AUTOPLUG_CONTINUE,
156 SIGNAL_AUTOPLUG_FACTORIES,
157 SIGNAL_AUTOPLUG_SELECT,
159 SIGNAL_AUTOPLUG_SORT,
165 #define DEFAULT_PROP_URI NULL
166 #define DEFAULT_PROP_SOURCE NULL
167 #define DEFAULT_CONNECTION_SPEED 0
168 #define DEFAULT_CAPS (gst_static_caps_get (&default_raw_caps))
169 #define DEFAULT_SUBTITLE_ENCODING NULL
170 #define DEFAULT_BUFFER_DURATION -1
171 #define DEFAULT_BUFFER_SIZE -1
172 #define DEFAULT_DOWNLOAD FALSE
173 #define DEFAULT_USE_BUFFERING FALSE
174 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
175 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
182 PROP_CONNECTION_SPEED,
184 PROP_SUBTITLE_ENCODING,
186 PROP_BUFFER_DURATION,
189 PROP_EXPOSE_ALL_STREAMS,
190 PROP_RING_BUFFER_MAX_SIZE,
194 static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };
196 GType gst_uri_decode_bin_get_type (void);
197 GST_BOILERPLATE (GstURIDecodeBin, gst_uri_decode_bin, GstBin, GST_TYPE_BIN);
199 static void remove_decoders (GstURIDecodeBin * bin, gboolean force);
200 static void gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
201 const GValue * value, GParamSpec * pspec);
202 static void gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
203 GValue * value, GParamSpec * pspec);
204 static void gst_uri_decode_bin_finalize (GObject * obj);
206 static void handle_message (GstBin * bin, GstMessage * msg);
208 static gboolean gst_uri_decode_bin_query (GstElement * element,
210 static GstStateChangeReturn gst_uri_decode_bin_change_state (GstElement *
211 element, GstStateChange transition);
214 gst_uri_decode_bin_base_init (gpointer g_class)
216 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
218 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
219 gst_element_class_set_details_simple (gstelement_class,
220 "URI Decoder", "Generic/Bin/Decoder",
221 "Autoplug and decode an URI to raw media",
222 "Wim Taymans <wim.taymans@gmail.com>");
226 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
227 GValue * return_accu, const GValue * handler_return, gpointer dummy)
231 myboolean = g_value_get_boolean (handler_return);
232 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
233 g_value_set_boolean (return_accu, myboolean);
235 /* stop emission if FALSE */
240 _gst_array_accumulator (GSignalInvocationHint * ihint,
241 GValue * return_accu, const GValue * handler_return, gpointer dummy)
245 array = g_value_get_boxed (handler_return);
246 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
247 g_value_set_boxed (return_accu, array);
253 _gst_select_accumulator (GSignalInvocationHint * ihint,
254 GValue * return_accu, const GValue * handler_return, gpointer dummy)
256 GstAutoplugSelectResult res;
258 res = g_value_get_enum (handler_return);
259 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
260 g_value_set_enum (return_accu, res);
266 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
267 GValue * return_accu, const GValue * handler_return, gpointer dummy)
271 array = g_value_get_boxed (handler_return);
272 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
273 g_value_set_boxed (return_accu, array);
282 gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
285 /* by default we always continue */
289 /* Must be called with factories lock! */
291 gst_uri_decode_bin_update_factories_list (GstURIDecodeBin * dec)
293 if (!dec->factories ||
294 dec->factories_cookie !=
295 gst_default_registry_get_feature_list_cookie ()) {
297 gst_plugin_feature_list_free (dec->factories);
299 gst_element_factory_list_get_elements
300 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
301 dec->factories_cookie = gst_default_registry_get_feature_list_cookie ();
306 gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
311 GstURIDecodeBin *dec = GST_URI_DECODE_BIN_CAST (element);
313 GST_DEBUG_OBJECT (element, "finding factories");
315 /* return all compatible factories for caps */
316 g_mutex_lock (dec->factories_lock);
317 gst_uri_decode_bin_update_factories_list (dec);
319 gst_element_factory_list_filter (dec->factories, caps, GST_PAD_SINK,
321 g_mutex_unlock (dec->factories_lock);
323 result = g_value_array_new (g_list_length (list));
324 for (tmp = list; tmp; tmp = tmp->next) {
325 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
328 g_value_init (&val, G_TYPE_OBJECT);
329 g_value_set_object (&val, factory);
330 g_value_array_append (result, &val);
331 g_value_unset (&val);
333 gst_plugin_feature_list_free (list);
335 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
341 gst_uri_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
342 GstCaps * caps, GValueArray * factories)
347 static GstAutoplugSelectResult
348 gst_uri_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
349 GstCaps * caps, GstElementFactory * factory)
351 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
354 return GST_AUTOPLUG_SELECT_TRY;
358 gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
360 GObjectClass *gobject_class;
361 GstElementClass *gstelement_class;
362 GstBinClass *gstbin_class;
364 gobject_class = G_OBJECT_CLASS (klass);
365 gstelement_class = GST_ELEMENT_CLASS (klass);
366 gstbin_class = GST_BIN_CLASS (klass);
368 gobject_class->set_property = gst_uri_decode_bin_set_property;
369 gobject_class->get_property = gst_uri_decode_bin_get_property;
370 gobject_class->finalize = gst_uri_decode_bin_finalize;
372 g_object_class_install_property (gobject_class, PROP_URI,
373 g_param_spec_string ("uri", "URI", "URI to decode",
374 DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (gobject_class, PROP_SOURCE,
377 g_param_spec_object ("source", "Source", "Source object used",
378 GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
380 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
381 g_param_spec_uint ("connection-speed", "Connection Speed",
382 "Network connection speed in kbps (0 = unknown)",
383 0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 g_object_class_install_property (gobject_class, PROP_CAPS,
387 g_param_spec_boxed ("caps", "Caps",
388 "The caps on which to stop decoding. (NULL = default)",
389 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391 g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
392 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
393 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
394 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
395 "be checked for an encoding to use. If that is not set either, "
396 "ISO-8859-15 will be assumed.", NULL,
397 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
400 g_param_spec_int ("buffer-size", "Buffer size (bytes)",
401 "Buffer size when buffering streams (-1 default value)",
402 -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
405 g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
406 "Buffer duration when buffering streams (-1 default value)",
407 -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 * GstURIDecodeBin::download:
413 * For certain media type, enable download buffering.
415 g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
416 g_param_spec_boolean ("download", "Download",
417 "Attempt download buffering when buffering network streams",
418 DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
420 * GstURIDecodeBin::use-buffering:
422 * Emit BUFFERING messages based on low-/high-percent thresholds of the
423 * demuxed or parsed data.
424 * When download buffering is activated and used for the current media
425 * type, this property does nothing. Otherwise perform buffering on the
426 * demuxed or parsed media.
430 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
431 g_param_spec_boolean ("use-buffering", "Use Buffering",
432 "Perform buffering on demuxed/parsed media",
433 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
436 * GstURIDecodeBin::expose-all-streams
438 * Expose streams of unknown type.
440 * If set to %FALSE, then only the streams that can be decoded to the final
441 * caps (see 'caps' property) will have a pad exposed. Streams that do not
442 * match those caps but could have been decoded will not have decoder plugged
443 * in internally and will not have a pad exposed.
447 g_object_class_install_property (gobject_class, PROP_EXPOSE_ALL_STREAMS,
448 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
449 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
450 DEFAULT_EXPOSE_ALL_STREAMS,
451 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 * GstURIDecodeBin::ring-buffer-max-size
456 * The maximum size of the ring buffer in kilobytes. If set to 0, the ring
457 * buffer is disabled. Default is 0.
461 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
462 g_param_spec_uint64 ("ring-buffer-max-size",
463 "Max. ring buffer size (bytes)",
464 "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
465 0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
466 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
469 * GstURIDecodeBin::unknown-type:
470 * @bin: The uridecodebin.
471 * @pad: the new pad containing caps that cannot be resolved to a 'final'.
473 * @caps: the #GstCaps of the pad that cannot be resolved.
475 * This signal is emitted when a pad for which there is no further possible
476 * decoding is added to the uridecodebin.
478 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
479 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
480 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, unknown_type),
481 NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
482 GST_TYPE_PAD, GST_TYPE_CAPS);
485 * GstURIDecodeBin::autoplug-continue:
486 * @bin: The uridecodebin.
488 * @caps: The #GstCaps found.
490 * This signal is emitted whenever uridecodebin finds a new stream. It is
491 * emitted before looking for any elements that can handle that stream.
494 * Invocation of signal handlers stops after the first signal handler
495 * returns #FALSE. Signal handlers are invoked in the order they were
499 * Returns: #TRUE if you wish uridecodebin to look for elements that can
500 * handle the given @caps. If #FALSE, those caps will be considered as
501 * final and the pad will be exposed as such (see 'new-decoded-pad'
504 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
505 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
506 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
507 autoplug_continue), _gst_boolean_accumulator, NULL,
508 gst_play_marshal_BOOLEAN__OBJECT_BOXED, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
512 * GstURIDecodeBin::autoplug-factories:
513 * @bin: The uridecodebin.
515 * @caps: The #GstCaps found.
517 * This function is emitted when an array of possible factories for @caps on
518 * @pad is needed. Uridecodebin will by default return an array with all
519 * compatible factories, sorted by rank.
521 * If this function returns NULL, @pad will be exposed as a final caps.
523 * If this function returns an empty array, the pad will be considered as
524 * having an unhandled type media type.
527 * Only the signal handler that is connected first will ever by invoked.
528 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
529 * signal, they will never be invoked!
532 * Returns: a #GValueArray* with a list of factories to try. The factories are
533 * by default tried in the returned order or based on the index returned by
536 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
537 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
538 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
539 autoplug_factories), _gst_array_accumulator, NULL,
540 gst_play_marshal_BOXED__OBJECT_BOXED, G_TYPE_VALUE_ARRAY, 2,
541 GST_TYPE_PAD, GST_TYPE_CAPS);
544 * GstURIDecodeBin::autoplug-sort:
545 * @bin: The uridecodebin.
547 * @caps: The #GstCaps.
548 * @factories: A #GValueArray of possible #GstElementFactory to use.
550 * Once decodebin2 has found the possible #GstElementFactory objects to try
551 * for @caps on @pad, this signal is emitted. The purpose of the signal is for
552 * the application to perform additional sorting or filtering on the element
555 * The callee should copy and modify @factories or return #NULL if the
556 * order should not change.
559 * Invocation of signal handlers stops after one signal handler has
560 * returned something else than #NULL. Signal handlers are invoked in
561 * the order they were connected in.
562 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
563 * signal, they will never be invoked!
566 * Returns: A new sorted array of #GstElementFactory objects.
570 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
571 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
572 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_sort),
573 _gst_array_hasvalue_accumulator, NULL,
574 gst_play_marshal_BOXED__OBJECT_BOXED_BOXED, G_TYPE_VALUE_ARRAY, 3,
575 GST_TYPE_PAD, GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY);
578 * GstURIDecodeBin::autoplug-select:
579 * @bin: The uridecodebin.
581 * @caps: The #GstCaps.
582 * @factory: A #GstElementFactory to use.
584 * This signal is emitted once uridecodebin has found all the possible
585 * #GstElementFactory that can be used to handle the given @caps. For each of
586 * those factories, this signal is emitted.
588 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
589 * value indicating what decodebin2 should do next.
591 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
594 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
597 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
601 * Only the signal handler that is connected first will ever by invoked.
602 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
603 * signal, they will never be invoked!
606 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
607 * operation. The default handler will always return
608 * #GST_AUTOPLUG_SELECT_TRY.
610 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
611 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
612 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
613 autoplug_select), _gst_select_accumulator, NULL,
614 gst_play_marshal_ENUM__OBJECT_BOXED_OBJECT,
615 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
616 GST_TYPE_ELEMENT_FACTORY);
619 * GstURIDecodeBin::drained:
621 * This signal is emitted when the data for the current uri is played.
623 gst_uri_decode_bin_signals[SIGNAL_DRAINED] =
624 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
626 G_STRUCT_OFFSET (GstURIDecodeBinClass, drained), NULL, NULL,
627 gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
630 * GstURIDecodeBin::source-setup:
631 * @bin: the uridecodebin.
632 * @source: source element
634 * This signal is emitted after the source element has been created, so
635 * it can be configured by setting additional properties (e.g. set a
636 * proxy server for an http source, or set the device and read speed for
637 * an audio cd source). This is functionally equivalent to connecting to
638 * the notify::source signal, but more convenient.
642 gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP] =
643 g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
644 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
645 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
647 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_query);
648 gstelement_class->change_state =
649 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_change_state);
651 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);
653 klass->autoplug_continue =
654 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
655 klass->autoplug_factories =
656 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
657 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_sort);
658 klass->autoplug_select =
659 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_select);
663 gst_uri_decode_bin_init (GstURIDecodeBin * dec, GstURIDecodeBinClass * klass)
665 /* first filter out the interesting element factories */
666 dec->factories_lock = g_mutex_new ();
668 dec->lock = g_mutex_new ();
670 dec->uri = g_strdup (DEFAULT_PROP_URI);
671 dec->connection_speed = DEFAULT_CONNECTION_SPEED;
672 dec->caps = DEFAULT_CAPS;
673 dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
675 dec->buffer_duration = DEFAULT_BUFFER_DURATION;
676 dec->buffer_size = DEFAULT_BUFFER_SIZE;
677 dec->download = DEFAULT_DOWNLOAD;
678 dec->use_buffering = DEFAULT_USE_BUFFERING;
679 dec->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
680 dec->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
682 GST_OBJECT_FLAG_SET (dec, GST_ELEMENT_IS_SOURCE);
686 gst_uri_decode_bin_finalize (GObject * obj)
688 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (obj);
690 remove_decoders (dec, TRUE);
691 g_mutex_free (dec->lock);
692 g_mutex_free (dec->factories_lock);
694 g_free (dec->encoding);
696 gst_plugin_feature_list_free (dec->factories);
698 gst_caps_unref (dec->caps);
700 G_OBJECT_CLASS (parent_class)->finalize (obj);
704 gst_uri_decode_bin_set_encoding (GstURIDecodeBin * dec, const gchar * encoding)
708 GST_URI_DECODE_BIN_LOCK (dec);
710 /* set property first */
711 GST_OBJECT_LOCK (dec);
712 g_free (dec->encoding);
713 dec->encoding = g_strdup (encoding);
714 GST_OBJECT_UNLOCK (dec);
716 /* set the property on all decodebins now */
717 for (walk = dec->decodebins; walk; walk = g_slist_next (walk)) {
718 GObject *decodebin = G_OBJECT (walk->data);
720 g_object_set (decodebin, "subtitle-encoding", encoding, NULL);
722 GST_URI_DECODE_BIN_UNLOCK (dec);
726 gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
727 const GValue * value, GParamSpec * pspec)
729 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
733 GST_OBJECT_LOCK (dec);
735 dec->uri = g_value_dup_string (value);
736 GST_OBJECT_UNLOCK (dec);
738 case PROP_CONNECTION_SPEED:
739 GST_OBJECT_LOCK (dec);
740 dec->connection_speed = g_value_get_uint (value) * 1000;
741 GST_OBJECT_UNLOCK (dec);
744 GST_OBJECT_LOCK (dec);
746 gst_caps_unref (dec->caps);
747 dec->caps = g_value_dup_boxed (value);
748 GST_OBJECT_UNLOCK (dec);
750 case PROP_SUBTITLE_ENCODING:
751 gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
753 case PROP_BUFFER_SIZE:
754 dec->buffer_size = g_value_get_int (value);
756 case PROP_BUFFER_DURATION:
757 dec->buffer_duration = g_value_get_int64 (value);
760 dec->download = g_value_get_boolean (value);
762 case PROP_USE_BUFFERING:
763 dec->use_buffering = g_value_get_boolean (value);
765 case PROP_EXPOSE_ALL_STREAMS:
766 dec->expose_allstreams = g_value_get_boolean (value);
768 case PROP_RING_BUFFER_MAX_SIZE:
769 dec->ring_buffer_max_size = g_value_get_uint64 (value);
772 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
778 gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
779 GValue * value, GParamSpec * pspec)
781 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
785 GST_OBJECT_LOCK (dec);
786 g_value_set_string (value, dec->uri);
787 GST_OBJECT_UNLOCK (dec);
790 GST_OBJECT_LOCK (dec);
791 g_value_set_object (value, dec->source);
792 GST_OBJECT_UNLOCK (dec);
794 case PROP_CONNECTION_SPEED:
795 GST_OBJECT_LOCK (dec);
796 g_value_set_uint (value, dec->connection_speed / 1000);
797 GST_OBJECT_UNLOCK (dec);
800 GST_OBJECT_LOCK (dec);
801 g_value_set_boxed (value, dec->caps);
802 GST_OBJECT_UNLOCK (dec);
804 case PROP_SUBTITLE_ENCODING:
805 GST_OBJECT_LOCK (dec);
806 g_value_set_string (value, dec->encoding);
807 GST_OBJECT_UNLOCK (dec);
809 case PROP_BUFFER_SIZE:
810 GST_OBJECT_LOCK (dec);
811 g_value_set_int (value, dec->buffer_size);
812 GST_OBJECT_UNLOCK (dec);
814 case PROP_BUFFER_DURATION:
815 GST_OBJECT_LOCK (dec);
816 g_value_set_int64 (value, dec->buffer_duration);
817 GST_OBJECT_UNLOCK (dec);
820 g_value_set_boolean (value, dec->download);
822 case PROP_USE_BUFFERING:
823 g_value_set_boolean (value, dec->use_buffering);
825 case PROP_EXPOSE_ALL_STREAMS:
826 g_value_set_boolean (value, dec->expose_allstreams);
828 case PROP_RING_BUFFER_MAX_SIZE:
829 g_value_set_uint64 (value, dec->ring_buffer_max_size);
832 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
838 do_async_start (GstURIDecodeBin * dbin)
842 dbin->async_pending = TRUE;
844 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin), FALSE);
845 parent_class->handle_message (GST_BIN_CAST (dbin), message);
849 do_async_done (GstURIDecodeBin * dbin)
853 if (dbin->async_pending) {
854 GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
855 message = gst_message_new_async_done (GST_OBJECT_CAST (dbin));
856 parent_class->handle_message (GST_BIN_CAST (dbin), message);
858 dbin->async_pending = FALSE;
862 #define DEFAULT_QUEUE_SIZE (3 * GST_SECOND)
863 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
864 #define DEFAULT_QUEUE_THRESHOLD ((DEFAULT_QUEUE_SIZE * 95) / 100)
867 unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
868 GstURIDecodeBin * decoder)
872 capsstr = gst_caps_to_string (caps);
873 GST_ELEMENT_WARNING (decoder, STREAM, CODEC_NOT_FOUND,
874 (_("No decoder available for type \'%s\'."), capsstr), (NULL));
878 /* add a streaminfo that indicates that the stream is handled by the
879 * given element. This usually means that a stream without actual data is
880 * produced but one that is sunken by an element. Examples of this are:
881 * cdaudio, a hardware decoder/sink, dvd meta bins etc...
884 add_element_stream (GstElement * element, GstURIDecodeBin * decoder)
886 g_warning ("add element stream");
889 /* when the decoder element signals that no more pads will be generated, we
890 * can commit the current group.
893 no_more_pads_full (GstElement * element, gboolean subs,
894 GstURIDecodeBin * decoder)
899 GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);
901 GST_URI_DECODE_BIN_LOCK (decoder);
902 final = (decoder->pending == 0);
904 /* nothing pending, we can exit */
908 /* the object has no pending no_more_pads */
909 if (!g_object_get_data (G_OBJECT (element), "pending"))
911 g_object_set_data (G_OBJECT (element), "pending", NULL);
914 final = (decoder->pending == 0);
917 GST_URI_DECODE_BIN_UNLOCK (decoder);
920 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
926 no_more_pads (GstElement * element, GstURIDecodeBin * decoder)
928 no_more_pads_full (element, FALSE, decoder);
932 source_no_more_pads (GstElement * element, GstURIDecodeBin * bin)
934 GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
935 GST_ELEMENT_NAME (element));
937 g_signal_handler_disconnect (element, bin->src_np_sig_id);
938 bin->src_np_sig_id = 0;
939 g_signal_handler_disconnect (element, bin->src_nmp_sig_id);
940 bin->src_nmp_sig_id = 0;
942 no_more_pads_full (element, FALSE, bin);
946 configure_stream_buffering (GstURIDecodeBin * decoder)
948 GstElement *queue = NULL;
953 /* automatic configuration enabled ? */
954 if (decoder->buffer_size != -1)
957 GST_URI_DECODE_BIN_LOCK (decoder);
959 queue = gst_object_ref (decoder->queue);
961 g_hash_table_iter_init (&iter, decoder->streams);
962 while (g_hash_table_iter_next (&iter, &key, &value)) {
963 GstURIDecodeBinStream *stream = value;
965 if (stream->bitrate && bitrate >= 0)
966 bitrate += stream->bitrate;
970 GST_URI_DECODE_BIN_UNLOCK (decoder);
972 GST_DEBUG_OBJECT (decoder, "overall bitrate %d", bitrate);
980 /* all streams have a bitrate;
981 * configure queue size based on queue duration using combined bitrate */
982 g_object_get (queue, "max-size-time", &time, NULL);
983 GST_DEBUG_OBJECT (decoder, "queue buffering time %" GST_TIME_FORMAT,
984 GST_TIME_ARGS (time));
986 bytes = gst_util_uint64_scale (time, bitrate, 8 * GST_SECOND);
987 GST_DEBUG_OBJECT (decoder, "corresponds to buffer size %d", bytes);
988 g_object_set (queue, "max-size-bytes", bytes, NULL);
992 gst_object_unref (queue);
996 decoded_pad_event_probe (GstPad * pad, GstEvent * event,
997 GstURIDecodeBin * decoder)
999 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1001 /* look for a bitrate tag */
1002 switch (GST_EVENT_TYPE (event)) {
1007 GstURIDecodeBinStream *stream;
1009 gst_event_parse_tag (event, &list);
1010 if (!gst_tag_list_get_uint_index (list, GST_TAG_NOMINAL_BITRATE, 0,
1012 gst_tag_list_get_uint_index (list, GST_TAG_BITRATE, 0, &bitrate);
1014 GST_DEBUG_OBJECT (pad, "found bitrate %u", bitrate);
1016 GST_URI_DECODE_BIN_LOCK (decoder);
1017 stream = g_hash_table_lookup (decoder->streams, pad);
1018 GST_URI_DECODE_BIN_UNLOCK (decoder);
1020 stream->bitrate = bitrate;
1021 /* no longer need this probe now */
1022 gst_pad_remove_event_probe (pad, stream->probe_id);
1023 /* configure buffer if possible */
1024 configure_stream_buffering (decoder);
1037 /* Called by the signal handlers when a decodebin has
1038 * found a new raw pad.
1041 new_decoded_pad_cb (GstElement * element, GstPad * pad, gboolean last,
1042 GstURIDecodeBin * decoder)
1045 GstPadTemplate *pad_tmpl;
1047 GstURIDecodeBinStream *stream;
1049 GST_DEBUG_OBJECT (element, "new decoded pad, name: <%s>. Last: %d",
1050 GST_PAD_NAME (pad), last);
1052 GST_URI_DECODE_BIN_LOCK (decoder);
1053 padname = g_strdup_printf ("src%d", decoder->numpads);
1055 GST_URI_DECODE_BIN_UNLOCK (decoder);
1057 pad_tmpl = gst_static_pad_template_get (&srctemplate);
1058 newpad = gst_ghost_pad_new_from_template (padname, pad, pad_tmpl);
1059 gst_object_unref (pad_tmpl);
1062 /* store ref to the ghostpad so we can remove it */
1063 g_object_set_data (G_OBJECT (pad), "uridecodebin.ghostpad", newpad);
1065 /* add event probe to monitor tags */
1066 stream = g_slice_alloc0 (sizeof (GstURIDecodeBinStream));
1068 gst_pad_add_event_probe (pad, G_CALLBACK (decoded_pad_event_probe),
1070 GST_URI_DECODE_BIN_LOCK (decoder);
1071 g_hash_table_insert (decoder->streams, pad, stream);
1072 GST_URI_DECODE_BIN_UNLOCK (decoder);
1074 gst_pad_set_active (newpad, TRUE);
1075 gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
1079 source_pad_event_probe (GstPad * pad, GstEvent * event,
1080 GstURIDecodeBin * decoder)
1082 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1084 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1085 GST_DEBUG_OBJECT (pad, "we received EOS");
1087 g_signal_emit (decoder,
1088 gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1090 /* never drop events */
1094 /* called when we found a raw pad on the source element. We need to set up a
1095 * padprobe to detect EOS before exposing the pad. */
1097 expose_decoded_pad (GstElement * element, GstPad * pad,
1098 GstURIDecodeBin * decoder)
1100 gst_pad_add_event_probe (pad, G_CALLBACK (source_pad_event_probe), decoder);
1102 new_decoded_pad_cb (element, pad, FALSE, decoder);
1106 pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
1110 GST_DEBUG_OBJECT (element, "pad removed name: <%s:%s>",
1111 GST_DEBUG_PAD_NAME (pad));
1113 /* we only care about srcpads */
1114 if (!GST_PAD_IS_SRC (pad))
1117 if (!(ghost = g_object_get_data (G_OBJECT (pad), "uridecodebin.ghostpad")))
1120 /* unghost the pad */
1121 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ghost), NULL);
1123 /* deactivate and remove */
1124 gst_pad_set_active (pad, FALSE);
1125 gst_element_remove_pad (GST_ELEMENT_CAST (decoder), ghost);
1132 GST_WARNING_OBJECT (element, "no ghost pad found");
1137 /* helper function to lookup stuff in lists */
1139 array_has_value (const gchar * values[], const gchar * value)
1143 for (i = 0; values[i]; i++) {
1144 if (g_str_has_prefix (value, values[i]))
1151 array_has_uri_value (const gchar * values[], const gchar * value)
1155 for (i = 0; values[i]; i++) {
1156 if (!g_ascii_strncasecmp (value, values[i], strlen (values[i])))
1162 /* list of URIs that we consider to be streams and that need buffering.
1163 * We have no mechanism yet to figure this out with a query. */
1164 static const gchar *stream_uris[] = { "http://", "https://", "mms://",
1165 "mmsh://", "mmsu://", "mmst://", "fd://", "myth://", "ssh://",
1166 "ftp://", "sftp://",
1170 /* list of URIs that need a queue because they are pretty bursty */
1171 static const gchar *queue_uris[] = { "cdda://", NULL };
1173 /* blacklisted URIs, we know they will always fail. */
1174 static const gchar *blacklisted_uris[] = { NULL };
1176 /* mime types that we don't consider to be media types */
1178 static const gchar *no_media_mimes[] = {
1179 "application/x-executable", "application/x-bzip", "application/x-gzip",
1180 "application/zip", "application/x-compress", NULL
1184 /* media types we can download */
1185 static const gchar *download_media[] = {
1186 "video/quicktime", "video/mj2", "audio/x-m4a", "application/x-3gp",
1187 "video/x-flv", "video/x-msvideo", "video/webm", NULL
1190 #define IS_STREAM_URI(uri) (array_has_uri_value (stream_uris, uri))
1191 #define IS_QUEUE_URI(uri) (array_has_uri_value (queue_uris, uri))
1192 #define IS_BLACKLISTED_URI(uri) (array_has_uri_value (blacklisted_uris, uri))
1193 #define IS_NO_MEDIA_MIME(mime) (array_has_value (no_media_mimes, mime))
1194 #define IS_DOWNLOAD_MEDIA(media) (array_has_value (download_media, media))
1197 * Generate and configure a source element.
1200 gen_source_element (GstURIDecodeBin * decoder)
1207 GST_LOG_OBJECT (decoder, "finding source for %s", decoder->uri);
1209 if (!gst_uri_is_valid (decoder->uri))
1212 if (IS_BLACKLISTED_URI (decoder->uri))
1213 goto uri_blacklisted;
1215 source = gst_element_make_from_uri (GST_URI_SRC, decoder->uri, "source");
1219 GST_LOG_OBJECT (decoder, "found source type %s", G_OBJECT_TYPE_NAME (source));
1221 decoder->is_stream = IS_STREAM_URI (decoder->uri);
1222 GST_LOG_OBJECT (decoder, "source is stream: %d", decoder->is_stream);
1224 decoder->need_queue = IS_QUEUE_URI (decoder->uri);
1225 GST_LOG_OBJECT (decoder, "source needs queue: %d", decoder->need_queue);
1227 /* make HTTP sources send extra headers so we get icecast
1228 * metadata in case the stream is an icecast stream */
1229 if (!strncmp (decoder->uri, "http://", 7) &&
1230 g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1232 GST_LOG_OBJECT (decoder, "configuring iradio-mode");
1233 g_object_set (source, "iradio-mode", TRUE, NULL);
1236 if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1237 "connection-speed")) {
1238 GST_DEBUG_OBJECT (decoder,
1239 "setting connection-speed=%d to source element",
1240 decoder->connection_speed / 1000);
1241 g_object_set (source, "connection-speed",
1242 decoder->connection_speed / 1000, NULL);
1244 if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1245 "subtitle-encoding")) {
1246 GST_DEBUG_OBJECT (decoder,
1247 "setting subtitle-encoding=%s to source element", decoder->encoding);
1248 g_object_set (source, "subtitle-encoding", decoder->encoding, NULL);
1255 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1256 (_("No URI specified to play from.")), (NULL));
1261 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1262 (_("Invalid URI \"%s\"."), decoder->uri), (NULL));
1267 GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
1268 (_("This stream type cannot be played yet.")), (NULL));
1273 gchar *prot = gst_uri_get_protocol (decoder->uri);
1275 /* whoops, could not create the source element, dig a little deeper to
1276 * figure out what might be wrong. */
1281 gst_missing_uri_source_message_new (GST_ELEMENT_CAST (decoder), prot);
1282 gst_element_post_message (GST_ELEMENT_CAST (decoder), msg);
1284 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
1285 (_("No URI handler implemented for \"%s\"."), prot), (NULL));
1297 * @all_raw: pointer to hold the result
1299 * check if the caps of the pad are all raw. The caps are all raw if
1300 * all of its structures contain audio/x-raw or video/x-raw.
1302 * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
1305 has_all_raw_caps (GstPad * pad, GstCaps * rawcaps, gboolean * all_raw)
1307 GstCaps *caps, *intersection;
1309 gboolean res = FALSE;
1311 caps = gst_pad_get_caps_reffed (pad);
1315 GST_DEBUG_OBJECT (pad, "have caps %" GST_PTR_FORMAT, caps);
1317 capssize = gst_caps_get_size (caps);
1318 /* no caps, skip and move to the next pad */
1319 if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1322 intersection = gst_caps_intersect (caps, rawcaps);
1323 *all_raw = !gst_caps_is_empty (intersection)
1324 && (gst_caps_get_size (intersection) == capssize);
1325 gst_caps_unref (intersection);
1330 gst_caps_unref (caps);
1335 post_missing_plugin_error (GstElement * dec, const gchar * element_name)
1339 msg = gst_missing_element_message_new (dec, element_name);
1340 gst_element_post_message (dec, msg);
1342 GST_ELEMENT_ERROR (dec, CORE, MISSING_PLUGIN,
1343 (_("Missing element '%s' - check your GStreamer installation."),
1344 element_name), (NULL));
1349 * @decoder: a #GstURIDecodeBin
1350 * @is_raw: are all pads raw data
1351 * @have_out: does the source have output
1352 * @is_dynamic: is this a dynamic source
1353 * @use_queue: put a queue before raw output pads
1355 * Check the source of @decoder and collect information about it.
1357 * @is_raw will be set to TRUE if the source only produces raw pads. When this
1358 * function returns, all of the raw pad of the source will be added
1361 * @have_out: will be set to TRUE if the source has output pads.
1363 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1366 * Returns: FALSE if a fatal error occured while scanning.
1369 analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
1370 gboolean * have_out, gboolean * is_dynamic, gboolean use_queue)
1372 GstIterator *pads_iter;
1373 gboolean done = FALSE;
1374 gboolean res = TRUE;
1380 *is_dynamic = FALSE;
1382 g_object_get (decoder, "caps", &rawcaps, NULL);
1384 rawcaps = DEFAULT_CAPS;
1386 pads_iter = gst_element_iterate_src_pads (decoder->source);
1388 switch (gst_iterator_next (pads_iter, (gpointer) & pad)) {
1389 case GST_ITERATOR_ERROR:
1392 case GST_ITERATOR_DONE:
1395 case GST_ITERATOR_RESYNC:
1396 /* reset results and resync */
1399 *is_dynamic = FALSE;
1400 gst_iterator_resync (pads_iter);
1402 case GST_ITERATOR_OK:
1403 /* we now officially have an output pad */
1406 /* if FALSE, this pad has no caps and we continue with the next pad. */
1407 if (!has_all_raw_caps (pad, rawcaps, is_raw)) {
1408 gst_object_unref (pad);
1412 /* caps on source pad are all raw, we can add the pad */
1414 GstElement *outelem;
1419 /* insert a queue element right before the raw pad */
1420 outelem = gst_element_factory_make ("queue2", NULL);
1424 gst_bin_add (GST_BIN_CAST (decoder), outelem);
1426 sinkpad = gst_element_get_static_pad (outelem, "sink");
1427 gst_pad_link (pad, sinkpad);
1428 gst_object_unref (sinkpad);
1429 gst_object_unref (pad);
1431 /* save queue pointer so we can remove it later */
1432 decoder->queue = outelem;
1434 /* get the new raw srcpad */
1435 pad = gst_element_get_static_pad (outelem, "src");
1437 outelem = decoder->source;
1439 expose_decoded_pad (outelem, pad, decoder);
1441 gst_object_unref (pad);
1445 gst_iterator_free (pads_iter);
1446 gst_caps_unref (rawcaps);
1449 GstElementClass *elemclass;
1452 /* element has no output pads, check for padtemplates that list SOMETIMES
1454 elemclass = GST_ELEMENT_GET_CLASS (decoder->source);
1456 walk = gst_element_class_get_pad_template_list (elemclass);
1457 while (walk != NULL) {
1458 GstPadTemplate *templ;
1460 templ = (GstPadTemplate *) walk->data;
1461 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1462 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES)
1466 walk = g_list_next (walk);
1473 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1475 gst_object_unref (pad);
1476 gst_iterator_free (pads_iter);
1477 gst_caps_unref (rawcaps);
1483 /* Remove all decodebin2 from ourself
1484 * If force is FALSE, then the decodebin2 instances will be stored in
1485 * pending_decodebins for re-use later on.
1486 * If force is TRUE, then all decodebin2 instances will be unreferenced
1487 * and cleared, including the pending ones. */
1489 remove_decoders (GstURIDecodeBin * bin, gboolean force)
1493 for (walk = bin->decodebins; walk; walk = g_slist_next (walk)) {
1494 GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1496 GST_DEBUG_OBJECT (bin, "removing old decoder element");
1498 gst_element_set_state (decoder, GST_STATE_NULL);
1499 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1503 gst_element_set_state (decoder, GST_STATE_READY);
1504 /* add it to our list of pending decodebins */
1505 g_object_ref (decoder);
1506 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1507 /* restore some properties we might have changed */
1508 g_object_set (decoder, "sink-caps", NULL, NULL);
1509 caps = DEFAULT_CAPS;
1510 g_object_set (decoder, "caps", caps, NULL);
1511 gst_caps_unref (caps);
1513 bin->pending_decodebins =
1514 g_slist_prepend (bin->pending_decodebins, decoder);
1517 g_slist_free (bin->decodebins);
1518 bin->decodebins = NULL;
1522 for (tmp = bin->pending_decodebins; tmp; tmp = tmp->next) {
1523 gst_element_set_state ((GstElement *) tmp->data, GST_STATE_NULL);
1524 gst_object_unref ((GstElement *) tmp->data);
1526 g_slist_free (bin->pending_decodebins);
1527 bin->pending_decodebins = NULL;
1531 /* Don't loose the SOURCE flag */
1532 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_IS_SOURCE);
1536 proxy_unknown_type_signal (GstElement * element, GstPad * pad, GstCaps * caps,
1537 GstURIDecodeBin * dec)
1539 GST_DEBUG_OBJECT (dec, "unknown-type signaled");
1542 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1546 proxy_autoplug_continue_signal (GstElement * element, GstPad * pad,
1547 GstCaps * caps, GstURIDecodeBin * dec)
1552 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, pad, caps,
1555 GST_DEBUG_OBJECT (dec, "autoplug-continue returned %d", result);
1560 static GValueArray *
1561 proxy_autoplug_factories_signal (GstElement * element, GstPad * pad,
1562 GstCaps * caps, GstURIDecodeBin * dec)
1564 GValueArray *result;
1567 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, pad, caps,
1570 GST_DEBUG_OBJECT (dec, "autoplug-factories returned %p", result);
1575 static GValueArray *
1576 proxy_autoplug_sort_signal (GstElement * element, GstPad * pad,
1577 GstCaps * caps, GValueArray * factories, GstURIDecodeBin * dec)
1579 GValueArray *result;
1582 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, pad, caps,
1583 factories, &result);
1585 GST_DEBUG_OBJECT (dec, "autoplug-sort returned %p", result);
1590 static GstAutoplugSelectResult
1591 proxy_autoplug_select_signal (GstElement * element, GstPad * pad,
1592 GstCaps * caps, GstElementFactory * factory, GstURIDecodeBin * dec)
1594 GstAutoplugSelectResult result;
1597 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, factory,
1600 GST_DEBUG_OBJECT (dec, "autoplug-select returned %d", result);
1606 proxy_drained_signal (GstElement * element, GstURIDecodeBin * dec)
1608 GST_DEBUG_OBJECT (dec, "drained signaled");
1610 g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1613 /* make a decodebin and connect to all the signals */
1615 make_decoder (GstURIDecodeBin * decoder)
1617 GstElement *decodebin;
1619 /* re-use pending decodebin2 */
1620 if (decoder->pending_decodebins) {
1621 GSList *first = decoder->pending_decodebins;
1622 GST_LOG_OBJECT (decoder, "re-using pending decodebin2");
1623 decodebin = (GstElement *) first->data;
1624 decoder->pending_decodebins =
1625 g_slist_delete_link (decoder->pending_decodebins, first);
1627 GST_LOG_OBJECT (decoder, "making new decodebin2");
1629 /* now create the decoder element */
1630 decodebin = gst_element_factory_make ("decodebin2", NULL);
1636 if (decodebin->numsinkpads == 0)
1639 /* connect signals to proxy */
1640 g_signal_connect (decodebin, "unknown-type",
1641 G_CALLBACK (proxy_unknown_type_signal), decoder);
1642 g_signal_connect (decodebin, "autoplug-continue",
1643 G_CALLBACK (proxy_autoplug_continue_signal), decoder);
1644 g_signal_connect (decodebin, "autoplug-factories",
1645 G_CALLBACK (proxy_autoplug_factories_signal), decoder);
1646 g_signal_connect (decodebin, "autoplug-sort",
1647 G_CALLBACK (proxy_autoplug_sort_signal), decoder);
1648 g_signal_connect (decodebin, "autoplug-select",
1649 G_CALLBACK (proxy_autoplug_select_signal), decoder);
1650 g_signal_connect (decodebin, "drained",
1651 G_CALLBACK (proxy_drained_signal), decoder);
1653 /* set up callbacks to create the links between decoded data
1654 * and video/audio/subtitle rendering/output. */
1655 g_signal_connect (decodebin,
1656 "new-decoded-pad", G_CALLBACK (new_decoded_pad_cb), decoder);
1657 g_signal_connect (decodebin,
1658 "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
1659 g_signal_connect (decodebin, "no-more-pads",
1660 G_CALLBACK (no_more_pads), decoder);
1661 g_signal_connect (decodebin,
1662 "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
1665 /* configure caps if we have any */
1667 g_object_set (decodebin, "caps", decoder->caps, NULL);
1669 /* Propagate expose-all-streams property */
1670 g_object_set (decodebin, "expose-all-streams", decoder->expose_allstreams,
1673 if (!decoder->is_stream) {
1674 /* propagate the use-buffering property but only when we are not already
1675 * doing stream buffering with queue2. FIXME, we might want to do stream
1676 * buffering with the multiqueue buffering instead of queue2. */
1677 g_object_set (decodebin, "use-buffering", decoder->use_buffering, NULL);
1679 if (decoder->use_buffering) {
1683 /* configure sizes when buffering */
1684 if ((max_bytes = decoder->buffer_size) == -1)
1685 max_bytes = 2 * 1024 * 1024;
1686 if ((max_time = decoder->buffer_duration) == -1)
1687 max_time = 2 * GST_SECOND;
1689 g_object_set (decodebin, "max-size-bytes", max_bytes, "max-size-buffers",
1690 (guint) 0, "max-size-time", max_time, NULL);
1694 g_object_set_data (G_OBJECT (decodebin), "pending", GINT_TO_POINTER (1));
1695 g_object_set (decodebin, "subtitle-encoding", decoder->encoding, NULL);
1697 GST_LOG_OBJECT (decoder, "have %d pending dynamic objects", decoder->pending);
1699 gst_bin_add (GST_BIN_CAST (decoder), decodebin);
1701 decoder->decodebins = g_slist_prepend (decoder->decodebins, decodebin);
1708 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "decodebin2");
1709 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1710 ("No decodebin2 element, check your installation"));
1715 gst_object_unref (decodebin);
1716 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1717 ("No typefind element, decodebin2 is unusable, check your installation"));
1722 /* signaled when we have a stream and we need to configure the download
1723 * buffering or regular buffering */
1725 type_found (GstElement * typefind, guint probability,
1726 GstCaps * caps, GstURIDecodeBin * decoder)
1728 GstElement *dec_elem, *queue;
1730 const gchar *media_type;
1732 GST_DEBUG_OBJECT (decoder, "typefind found caps %" GST_PTR_FORMAT, caps);
1734 s = gst_caps_get_structure (caps, 0);
1735 media_type = gst_structure_get_name (s);
1737 /* remember if we need download buffering */
1738 decoder->is_download = IS_DOWNLOAD_MEDIA (media_type) && decoder->download;
1739 /* only enable download buffering if the upstream duration is known */
1740 if (decoder->is_download) {
1741 GstFormat fmt = GST_FORMAT_BYTES;
1744 decoder->is_download = (gst_element_query_duration (typefind, &fmt, &dur)
1745 && fmt == GST_FORMAT_BYTES && dur != -1);
1748 dec_elem = make_decoder (decoder);
1752 queue = gst_element_factory_make ("queue2", NULL);
1756 g_object_set (queue, "use-buffering", TRUE, NULL);
1757 g_object_set (queue, "ring-buffer-max-size", decoder->ring_buffer_max_size,
1760 GST_DEBUG_OBJECT (decoder, "check media-type %s, %d", media_type,
1763 if (decoder->is_download) {
1764 gchar *temp_template, *filename;
1765 const gchar *tmp_dir, *prgname;
1767 tmp_dir = g_get_tmp_dir ();
1768 prgname = g_get_prgname ();
1769 if (prgname == NULL)
1770 prgname = "GStreamer";
1772 filename = g_strdup_printf ("%s-XXXXXX", prgname);
1774 /* build our filename */
1775 temp_template = g_build_filename (tmp_dir, filename, NULL);
1777 GST_DEBUG_OBJECT (decoder, "enable download buffering in %s (%s, %s, %s)",
1778 temp_template, tmp_dir, prgname, filename);
1780 /* configure progressive download for selected media types */
1781 g_object_set (queue, "temp-template", temp_template, NULL);
1784 g_free (temp_template);
1787 /* Disable max-size-buffers */
1788 g_object_set (queue, "max-size-buffers", 0, NULL);
1790 /* If buffer size or duration are set, set them on the queue2 element */
1791 if (decoder->buffer_size != -1)
1792 g_object_set (queue, "max-size-bytes", decoder->buffer_size, NULL);
1793 if (decoder->buffer_duration != -1)
1794 g_object_set (queue, "max-size-time", decoder->buffer_duration, NULL);
1796 gst_bin_add (GST_BIN_CAST (decoder), queue);
1798 if (!gst_element_link_pads (typefind, "src", queue, "sink"))
1799 goto could_not_link;
1801 /* to force caps on the decodebin element and avoid reparsing stuff by
1802 * typefind. It also avoids a deadlock in the way typefind activates pads in
1803 * the state change */
1804 g_object_set (dec_elem, "sink-caps", caps, NULL);
1806 if (!gst_element_link_pads (queue, "src", dec_elem, "sink"))
1807 goto could_not_link;
1809 /* PLAYING in one go might fail (see bug #632782) */
1810 gst_element_set_state (dec_elem, GST_STATE_PAUSED);
1811 gst_element_set_state (dec_elem, GST_STATE_PLAYING);
1812 gst_element_set_state (queue, GST_STATE_PLAYING);
1814 do_async_done (decoder);
1821 /* error was posted */
1826 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1827 (NULL), ("Can't link typefind to decodebin2 element"));
1832 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1837 /* setup a streaming source. This will first plug a typefind element to the
1838 * source. After we find the type, we decide to plug a queue2 and continue to
1839 * plug a decodebin2 starting from the found caps */
1841 setup_streaming (GstURIDecodeBin * decoder)
1843 GstElement *typefind;
1845 /* now create the decoder element */
1846 typefind = gst_element_factory_make ("typefind", NULL);
1850 gst_bin_add (GST_BIN_CAST (decoder), typefind);
1852 if (!gst_element_link_pads (decoder->source, NULL, typefind, "sink"))
1853 goto could_not_link;
1855 decoder->typefind = typefind;
1857 /* connect a signal to find out when the typefind element found
1859 decoder->have_type_id =
1860 g_signal_connect (decoder->typefind, "have-type",
1861 G_CALLBACK (type_found), decoder);
1863 do_async_start (decoder);
1870 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "typefind");
1871 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1872 ("No typefind element, check your installation"));
1877 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1878 (NULL), ("Can't link source to typefind element"));
1879 gst_bin_remove (GST_BIN_CAST (decoder), typefind);
1880 /* Don't loose the SOURCE flag */
1881 GST_OBJECT_FLAG_SET (decoder, GST_ELEMENT_IS_SOURCE);
1887 free_stream (gpointer value)
1889 g_slice_free (GstURIDecodeBinStream, value);
1892 /* remove source and all related elements */
1894 remove_source (GstURIDecodeBin * bin)
1896 GstElement *source = bin->source;
1899 GST_DEBUG_OBJECT (bin, "removing old src element");
1900 gst_element_set_state (source, GST_STATE_NULL);
1902 if (bin->src_np_sig_id) {
1903 g_signal_handler_disconnect (source, bin->src_np_sig_id);
1904 bin->src_np_sig_id = 0;
1906 if (bin->src_nmp_sig_id) {
1907 g_signal_handler_disconnect (source, bin->src_nmp_sig_id);
1908 bin->src_nmp_sig_id = 0;
1910 gst_bin_remove (GST_BIN_CAST (bin), source);
1914 GST_DEBUG_OBJECT (bin, "removing old queue element");
1915 gst_element_set_state (bin->queue, GST_STATE_NULL);
1916 gst_bin_remove (GST_BIN_CAST (bin), bin->queue);
1919 if (bin->typefind) {
1920 GST_DEBUG_OBJECT (bin, "removing old typefind element");
1921 gst_element_set_state (bin->typefind, GST_STATE_NULL);
1922 gst_bin_remove (GST_BIN_CAST (bin), bin->typefind);
1923 bin->typefind = NULL;
1926 g_hash_table_destroy (bin->streams);
1927 bin->streams = NULL;
1929 /* Don't loose the SOURCE flag */
1930 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_IS_SOURCE);
1933 /* is called when a dynamic source element created a new pad. */
1935 source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
1937 GstElement *decoder;
1941 GST_URI_DECODE_BIN_LOCK (bin);
1942 GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
1943 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
1945 g_object_get (bin, "caps", &rawcaps, NULL);
1947 rawcaps = DEFAULT_CAPS;
1949 /* if this is a pad with all raw caps, we can expose it */
1950 if (has_all_raw_caps (pad, rawcaps, &is_raw) && is_raw) {
1951 /* it's all raw, create output pads. */
1952 GST_URI_DECODE_BIN_UNLOCK (bin);
1953 gst_caps_unref (rawcaps);
1954 expose_decoded_pad (element, pad, bin);
1957 gst_caps_unref (rawcaps);
1959 /* not raw, create decoder */
1960 decoder = make_decoder (bin);
1964 /* and link to decoder */
1965 if (!gst_element_link_pads (bin->source, NULL, decoder, "sink"))
1966 goto could_not_link;
1968 GST_DEBUG_OBJECT (bin, "linked decoder to new pad");
1970 gst_element_set_state (decoder, GST_STATE_PLAYING);
1971 GST_URI_DECODE_BIN_UNLOCK (bin);
1978 /* error was posted */
1979 GST_URI_DECODE_BIN_UNLOCK (bin);
1984 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
1985 (NULL), ("Can't link source to decoder element"));
1986 GST_URI_DECODE_BIN_UNLOCK (bin);
1991 /* construct and run the source and decoder elements until we found
1992 * all the streams or until a preroll queue has been filled.
1995 setup_source (GstURIDecodeBin * decoder)
1997 gboolean is_raw, have_out, is_dynamic;
1999 GST_DEBUG_OBJECT (decoder, "setup source");
2001 /* delete old src */
2002 remove_source (decoder);
2004 decoder->pending = 0;
2006 /* create and configure an element that can handle the uri */
2007 if (!(decoder->source = gen_source_element (decoder)))
2010 /* state will be merged later - if file is not found, error will be
2011 * handled by the application right after. */
2012 gst_bin_add (GST_BIN_CAST (decoder), decoder->source);
2014 /* notify of the new source used */
2015 g_object_notify (G_OBJECT (decoder), "source");
2017 g_signal_emit (decoder, gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP],
2018 0, decoder->source);
2020 /* remove the old decoders now, if any */
2021 remove_decoders (decoder, FALSE);
2023 /* stream admin setup */
2024 decoder->streams = g_hash_table_new_full (NULL, NULL, NULL, free_stream);
2026 /* see if the source element emits raw audio/video all by itself,
2027 * if so, we can create streams for the pads and be done with it.
2028 * Also check that is has source pads, if not, we assume it will
2029 * do everything itself. */
2030 if (!analyse_source (decoder, &is_raw, &have_out, &is_dynamic,
2031 decoder->need_queue))
2032 goto invalid_source;
2035 GST_DEBUG_OBJECT (decoder, "Source provides all raw data");
2036 /* source provides raw data, we added the pads and we can now signal a
2037 * no_more pads because we are done. */
2038 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
2041 if (!have_out && !is_dynamic) {
2042 GST_DEBUG_OBJECT (decoder, "Source has no output pads");
2043 /* create a stream to indicate that this uri is handled by a self
2044 * contained element. We are now done. */
2045 add_element_stream (decoder->source, decoder);
2049 GST_DEBUG_OBJECT (decoder, "Source has dynamic output pads");
2050 /* connect a handler for the new-pad signal */
2051 decoder->src_np_sig_id =
2052 g_signal_connect (decoder->source, "pad-added",
2053 G_CALLBACK (source_new_pad), decoder);
2054 decoder->src_nmp_sig_id =
2055 g_signal_connect (decoder->source, "no-more-pads",
2056 G_CALLBACK (source_no_more_pads), decoder);
2057 g_object_set_data (G_OBJECT (decoder->source), "pending",
2058 GINT_TO_POINTER (1));
2061 if (decoder->is_stream) {
2062 GST_DEBUG_OBJECT (decoder, "Setting up streaming");
2063 /* do the stream things here */
2064 if (!setup_streaming (decoder))
2065 goto streaming_failed;
2067 GstElement *dec_elem;
2069 /* no streaming source, we can link now */
2070 GST_DEBUG_OBJECT (decoder, "Plugging decodebin to source");
2072 dec_elem = make_decoder (decoder);
2076 if (!gst_element_link_pads (decoder->source, NULL, dec_elem, "sink"))
2077 goto could_not_link;
2085 /* error message was already posted */
2090 GST_ELEMENT_ERROR (decoder, CORE, FAILED,
2091 (_("Source element is invalid.")), (NULL));
2096 /* message was posted */
2101 /* message was posted */
2106 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2107 (NULL), ("Can't link source to decoder element"));
2113 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
2114 GList * structure_list)
2118 for (l = structure_list; l != NULL; l = l->next) {
2119 GValue val = { 0, };
2122 *first = gst_structure_copy ((GstStructure *) l->data);
2124 g_value_init (&val, GST_TYPE_STRUCTURE);
2125 g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
2126 gst_value_list_append_value (list_val, &val);
2127 g_value_unset (&val);
2131 /* if it's a redirect message with multiple redirect locations we might
2132 * want to pick a different 'best' location depending on the required
2133 * bitrates and the connection speed */
2135 handle_redirect_message (GstURIDecodeBin * dec, GstMessage * msg)
2137 const GValue *locations_list, *location_val;
2138 GstMessage *new_msg;
2139 GstStructure *new_structure = NULL;
2140 GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
2141 GValue new_list = { 0, };
2144 GST_DEBUG_OBJECT (dec, "redirect message: %" GST_PTR_FORMAT, msg);
2145 GST_DEBUG_OBJECT (dec, "connection speed: %u", dec->connection_speed);
2147 if (dec->connection_speed == 0 || msg->structure == NULL)
2150 locations_list = gst_structure_get_value (msg->structure, "locations");
2151 if (locations_list == NULL)
2154 size = gst_value_list_get_size (locations_list);
2158 /* maintain existing order as much as possible, just sort references
2159 * with too high a bitrate to the end (the assumption being that if
2160 * bitrates are given they are given for all interesting streams and
2161 * that the you-need-at-least-version-xyz redirect has the same bitrate
2162 * as the lowest referenced redirect alternative) */
2163 for (i = 0; i < size; ++i) {
2164 const GstStructure *s;
2167 location_val = gst_value_list_get_value (locations_list, i);
2168 s = (const GstStructure *) g_value_get_boxed (location_val);
2169 if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
2170 GST_DEBUG_OBJECT (dec, "no bitrate: %" GST_PTR_FORMAT, s);
2171 l_neutral = g_list_append (l_neutral, (gpointer) s);
2172 } else if (bitrate > dec->connection_speed) {
2173 GST_DEBUG_OBJECT (dec, "bitrate too high: %" GST_PTR_FORMAT, s);
2174 l_bad = g_list_append (l_bad, (gpointer) s);
2175 } else if (bitrate <= dec->connection_speed) {
2176 GST_DEBUG_OBJECT (dec, "bitrate OK: %" GST_PTR_FORMAT, s);
2177 l_good = g_list_append (l_good, (gpointer) s);
2181 g_value_init (&new_list, GST_TYPE_LIST);
2182 value_list_append_structure_list (&new_list, &new_structure, l_good);
2183 value_list_append_structure_list (&new_list, &new_structure, l_neutral);
2184 value_list_append_structure_list (&new_list, &new_structure, l_bad);
2185 gst_structure_set_value (new_structure, "locations", &new_list);
2186 g_value_unset (&new_list);
2188 g_list_free (l_good);
2189 g_list_free (l_neutral);
2190 g_list_free (l_bad);
2192 new_msg = gst_message_new_element (msg->src, new_structure);
2193 gst_message_unref (msg);
2195 GST_DEBUG_OBJECT (dec, "new redirect message: %" GST_PTR_FORMAT, new_msg);
2200 handle_message (GstBin * bin, GstMessage * msg)
2202 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT && msg->structure != NULL
2203 && gst_structure_has_name (msg->structure, "redirect")) {
2204 /* sort redirect messages based on the connection speed. This simplifies
2205 * the user of this element as it can in most cases just pick the first item
2206 * of the sorted list as a good redirection candidate. It can of course
2207 * choose something else from the list if it has a better way. */
2208 msg = handle_redirect_message (GST_URI_DECODE_BIN (bin), msg);
2210 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
2213 /* generic struct passed to all query fold methods
2214 * FIXME, move to core.
2225 typedef void (*QueryInitFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2226 typedef void (*QueryDoneFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2228 /* for duration/position we collect all durations/positions and take
2229 * the MAX of all valid results */
2231 decoder_query_init (GstURIDecodeBin * dec, QueryFold * fold)
2235 fold->seekable = TRUE;
2240 decoder_query_duration_fold (GstPad * item, GValue * ret, QueryFold * fold)
2242 if (gst_pad_query (item, fold->query)) {
2245 g_value_set_boolean (ret, TRUE);
2247 gst_query_parse_duration (fold->query, NULL, &duration);
2249 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
2251 if (duration > fold->max)
2252 fold->max = duration;
2254 gst_object_unref (item);
2259 decoder_query_duration_done (GstURIDecodeBin * dec, QueryFold * fold)
2263 gst_query_parse_duration (fold->query, &format, NULL);
2264 /* store max in query result */
2265 gst_query_set_duration (fold->query, format, fold->max);
2267 GST_DEBUG ("max duration %" G_GINT64_FORMAT, fold->max);
2271 decoder_query_position_fold (GstPad * item, GValue * ret, QueryFold * fold)
2273 if (gst_pad_query (item, fold->query)) {
2276 g_value_set_boolean (ret, TRUE);
2278 gst_query_parse_position (fold->query, NULL, &position);
2280 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
2282 if (position > fold->max)
2283 fold->max = position;
2286 gst_object_unref (item);
2291 decoder_query_position_done (GstURIDecodeBin * dec, QueryFold * fold)
2295 gst_query_parse_position (fold->query, &format, NULL);
2296 /* store max in query result */
2297 gst_query_set_position (fold->query, format, fold->max);
2299 GST_DEBUG_OBJECT (dec, "max position %" G_GINT64_FORMAT, fold->max);
2303 decoder_query_latency_fold (GstPad * item, GValue * ret, QueryFold * fold)
2305 if (gst_pad_query (item, fold->query)) {
2306 GstClockTime min, max;
2309 g_value_set_boolean (ret, TRUE);
2311 gst_query_parse_latency (fold->query, &live, &min, &max);
2313 GST_DEBUG_OBJECT (item,
2314 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2315 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
2317 /* for the combined latency we collect the MAX of all min latencies and
2318 * the MIN of all max latencies */
2319 if (min > fold->min)
2321 if (fold->max == -1)
2323 else if (max < fold->max)
2325 if (fold->live == FALSE)
2329 gst_object_unref (item);
2334 decoder_query_latency_done (GstURIDecodeBin * dec, QueryFold * fold)
2336 /* store max in query result */
2337 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
2339 GST_DEBUG_OBJECT (dec,
2340 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2341 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
2345 /* we are seekable if all srcpads are seekable */
2347 decoder_query_seeking_fold (GstPad * item, GValue * ret, QueryFold * fold)
2349 if (gst_pad_query (item, fold->query)) {
2352 g_value_set_boolean (ret, TRUE);
2353 gst_query_parse_seeking (fold->query, NULL, &seekable, NULL, NULL);
2355 GST_DEBUG_OBJECT (item, "got seekable %d", seekable);
2357 if (fold->seekable == TRUE)
2358 fold->seekable = seekable;
2360 gst_object_unref (item);
2366 decoder_query_seeking_done (GstURIDecodeBin * dec, QueryFold * fold)
2370 gst_query_parse_seeking (fold->query, &format, NULL, NULL, NULL);
2371 gst_query_set_seeking (fold->query, format, fold->seekable, 0, -1);
2373 GST_DEBUG_OBJECT (dec, "seekable %d", fold->seekable);
2376 /* generic fold, return first valid result */
2378 decoder_query_generic_fold (GstPad * item, GValue * ret, QueryFold * fold)
2382 if ((res = gst_pad_query (item, fold->query))) {
2383 g_value_set_boolean (ret, TRUE);
2384 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
2387 gst_object_unref (item);
2389 /* and stop as soon as we have a valid result */
2394 /* we're a bin, the default query handler iterates sink elements, which we don't
2395 * have normally. We should just query all source pads.
2398 gst_uri_decode_bin_query (GstElement * element, GstQuery * query)
2400 GstURIDecodeBin *decoder;
2401 gboolean res = FALSE;
2403 GstIteratorFoldFunction fold_func;
2404 QueryInitFunction fold_init = NULL;
2405 QueryDoneFunction fold_done = NULL;
2406 QueryFold fold_data;
2409 decoder = GST_URI_DECODE_BIN (element);
2411 switch (GST_QUERY_TYPE (query)) {
2412 case GST_QUERY_DURATION:
2413 /* iterate and collect durations */
2414 fold_func = (GstIteratorFoldFunction) decoder_query_duration_fold;
2415 fold_init = decoder_query_init;
2416 fold_done = decoder_query_duration_done;
2418 case GST_QUERY_POSITION:
2419 /* iterate and collect durations */
2420 fold_func = (GstIteratorFoldFunction) decoder_query_position_fold;
2421 fold_init = decoder_query_init;
2422 fold_done = decoder_query_position_done;
2424 case GST_QUERY_LATENCY:
2425 /* iterate and collect durations */
2426 fold_func = (GstIteratorFoldFunction) decoder_query_latency_fold;
2427 fold_init = decoder_query_init;
2428 fold_done = decoder_query_latency_done;
2430 case GST_QUERY_SEEKING:
2431 /* iterate and collect durations */
2432 fold_func = (GstIteratorFoldFunction) decoder_query_seeking_fold;
2433 fold_init = decoder_query_init;
2434 fold_done = decoder_query_seeking_done;
2437 fold_func = (GstIteratorFoldFunction) decoder_query_generic_fold;
2441 fold_data.query = query;
2443 g_value_init (&ret, G_TYPE_BOOLEAN);
2444 g_value_set_boolean (&ret, FALSE);
2446 iter = gst_element_iterate_src_pads (element);
2447 GST_DEBUG_OBJECT (element, "Sending query %p (type %d) to src pads",
2448 query, GST_QUERY_TYPE (query));
2451 fold_init (decoder, &fold_data);
2454 GstIteratorResult ires;
2456 ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
2459 case GST_ITERATOR_RESYNC:
2460 gst_iterator_resync (iter);
2462 fold_init (decoder, &fold_data);
2463 g_value_set_boolean (&ret, FALSE);
2465 case GST_ITERATOR_OK:
2466 case GST_ITERATOR_DONE:
2467 res = g_value_get_boolean (&ret);
2468 if (fold_done != NULL && res)
2469 fold_done (decoder, &fold_data);
2477 gst_iterator_free (iter);
2482 static GstStateChangeReturn
2483 gst_uri_decode_bin_change_state (GstElement * element,
2484 GstStateChange transition)
2486 GstStateChangeReturn ret;
2487 GstURIDecodeBin *decoder;
2489 decoder = GST_URI_DECODE_BIN (element);
2491 switch (transition) {
2492 case GST_STATE_CHANGE_READY_TO_PAUSED:
2493 if (!setup_source (decoder))
2500 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2502 switch (transition) {
2503 case GST_STATE_CHANGE_READY_TO_PAUSED:
2504 GST_DEBUG ("ready to paused");
2505 if (ret == GST_STATE_CHANGE_FAILURE)
2508 case GST_STATE_CHANGE_PAUSED_TO_READY:
2509 GST_DEBUG ("paused to ready");
2510 remove_decoders (decoder, FALSE);
2511 remove_source (decoder);
2512 do_async_done (decoder);
2514 case GST_STATE_CHANGE_READY_TO_NULL:
2515 GST_DEBUG ("ready to null");
2516 remove_decoders (decoder, TRUE);
2517 remove_source (decoder);
2527 return GST_STATE_CHANGE_FAILURE;
2531 /* clean up leftover groups */
2532 return GST_STATE_CHANGE_FAILURE;
2536 gboolean gst_decode_bin_plugin_init (GstPlugin * plugin);
2539 gst_uri_decode_bin_plugin_init (GstPlugin * plugin)
2541 GST_DEBUG_CATEGORY_INIT (gst_uri_decode_bin_debug, "uridecodebin", 0,
2542 "URI decoder element");
2545 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2547 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2548 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2549 #endif /* ENABLE_NLS */
2551 return gst_element_register (plugin, "uridecodebin", GST_RANK_NONE,
2552 GST_TYPE_URI_DECODE_BIN);
2556 plugin_init (GstPlugin * plugin)
2558 if (!gst_decode_bin_plugin_init (plugin))
2560 if (!gst_uri_decode_bin_plugin_init (plugin))
2566 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2569 "URI Decoder bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,