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 "gstfactorylists.h"
38 #include "gstplay-marshal.h"
39 #include "gstplay-enum.h"
40 #include "gstrawcaps.h"
42 #define GST_TYPE_URI_DECODE_BIN \
43 (gst_uri_decode_bin_get_type())
44 #define GST_URI_DECODE_BIN(obj) \
45 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBin))
46 #define GST_URI_DECODE_BIN_CLASS(klass) \
47 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBinClass))
48 #define GST_IS_URI_DECODE_BIN(obj) \
49 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DECODE_BIN))
50 #define GST_IS_URI_DECODE_BIN_CLASS(klass) \
51 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DECODE_BIN))
52 #define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
54 typedef struct _GstURIDecodeBin GstURIDecodeBin;
55 typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;
57 #define GST_URI_DECODE_BIN_GET_LOCK(dec) (((GstURIDecodeBin*)(dec))->lock)
58 #define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
59 #define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
64 * uridecodebin element struct
66 struct _GstURIDecodeBin
68 GstBin parent_instance;
70 GMutex *lock; /* lock for constructing */
72 GMutex *factories_lock;
73 guint32 factories_cookie;
74 GValueArray *factories; /* factories we can use for selecting elements */
77 guint connection_speed;
84 guint64 buffer_duration; /* When buffering, buffer duration (ns) */
85 guint buffer_size; /* When buffering, buffer size (bytes) */
87 gboolean use_buffering;
92 guint have_type_id; /* have-type signal id from typefind */
94 GSList *pending_decodebins;
98 /* for dynamic sources */
99 guint src_np_sig_id; /* new-pad signal id */
100 guint src_nmp_sig_id; /* no-more-pads signal id */
103 gboolean async_pending; /* async-start has been emited */
106 struct _GstURIDecodeBinClass
108 GstBinClass parent_class;
110 /* signal fired when we found a pad that we cannot decode */
111 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
113 /* signal fired to know if we continue trying to decode the given caps */
114 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
116 /* signal fired to get a list of factories to try to autoplug */
117 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
119 /* signal fired to select from the proposed list of factories */
120 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
121 GstPad * pad, GstCaps * caps, GValueArray * factories);
123 /* emited when all data is decoded */
124 void (*drained) (GstElement * element);
127 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
130 GST_STATIC_CAPS_ANY);
132 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
134 GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
135 #define GST_CAT_DEFAULT gst_uri_decode_bin_debug
137 static const GstElementDetails gst_uri_decode_bin_details =
138 GST_ELEMENT_DETAILS ("URI Decoder",
139 "Generic/Bin/Decoder",
140 "Autoplug and decode an URI to raw media",
141 "Wim Taymans <wim.taymans@gmail.com>");
147 SIGNAL_AUTOPLUG_CONTINUE,
148 SIGNAL_AUTOPLUG_FACTORIES,
149 SIGNAL_AUTOPLUG_SELECT,
155 #define DEFAULT_PROP_URI NULL
156 #define DEFAULT_PROP_SOURCE NULL
157 #define DEFAULT_CONNECTION_SPEED 0
158 #define DEFAULT_CAPS (gst_static_caps_get (&default_raw_caps))
159 #define DEFAULT_SUBTITLE_ENCODING NULL
160 #define DEFAULT_BUFFER_DURATION -1
161 #define DEFAULT_BUFFER_SIZE -1
162 #define DEFAULT_DOWNLOAD FALSE
163 #define DEFAULT_USE_BUFFERING FALSE
170 PROP_CONNECTION_SPEED,
172 PROP_SUBTITLE_ENCODING,
174 PROP_BUFFER_DURATION,
180 static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };
182 GType gst_uri_decode_bin_get_type (void);
183 GST_BOILERPLATE (GstURIDecodeBin, gst_uri_decode_bin, GstBin, GST_TYPE_BIN);
185 static void remove_decoders (GstURIDecodeBin * bin, gboolean force);
186 static void gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
187 const GValue * value, GParamSpec * pspec);
188 static void gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
189 GValue * value, GParamSpec * pspec);
190 static void gst_uri_decode_bin_finalize (GObject * obj);
192 static void handle_message (GstBin * bin, GstMessage * msg);
194 static gboolean gst_uri_decode_bin_query (GstElement * element,
196 static GstStateChangeReturn gst_uri_decode_bin_change_state (GstElement *
197 element, GstStateChange transition);
200 gst_uri_decode_bin_base_init (gpointer g_class)
202 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
204 gst_element_class_add_pad_template (gstelement_class,
205 gst_static_pad_template_get (&srctemplate));
206 gst_element_class_set_details (gstelement_class, &gst_uri_decode_bin_details);
210 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
211 GValue * return_accu, const GValue * handler_return, gpointer dummy)
215 myboolean = g_value_get_boolean (handler_return);
216 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
217 g_value_set_boolean (return_accu, myboolean);
219 /* stop emission if FALSE */
224 _gst_array_accumulator (GSignalInvocationHint * ihint,
225 GValue * return_accu, const GValue * handler_return, gpointer dummy)
229 array = g_value_get_boxed (handler_return);
230 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
231 g_value_set_boxed (return_accu, array);
237 _gst_select_accumulator (GSignalInvocationHint * ihint,
238 GValue * return_accu, const GValue * handler_return, gpointer dummy)
240 GstAutoplugSelectResult res;
242 res = g_value_get_enum (handler_return);
243 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
244 g_value_set_enum (return_accu, res);
250 gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
253 /* by default we always continue */
257 /* Must be called with factories lock! */
259 gst_uri_decode_bin_update_factories_list (GstURIDecodeBin * dec)
261 if (!dec->factories ||
262 dec->factories_cookie !=
263 gst_default_registry_get_feature_list_cookie ()) {
265 g_value_array_free (dec->factories);
266 dec->factories = gst_factory_list_get_elements (GST_FACTORY_LIST_DECODER);
267 dec->factories_cookie = gst_default_registry_get_feature_list_cookie ();
272 gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
276 GstURIDecodeBin *dec = GST_URI_DECODE_BIN_CAST (element);
278 GST_DEBUG_OBJECT (element, "finding factories");
280 /* return all compatible factories for caps */
281 g_mutex_lock (dec->factories_lock);
282 gst_uri_decode_bin_update_factories_list (dec);
283 result = gst_factory_list_filter (dec->factories, caps);
284 g_mutex_unlock (dec->factories_lock);
286 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
293 gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
295 GObjectClass *gobject_class;
296 GstElementClass *gstelement_class;
297 GstBinClass *gstbin_class;
299 gobject_class = G_OBJECT_CLASS (klass);
300 gstelement_class = GST_ELEMENT_CLASS (klass);
301 gstbin_class = GST_BIN_CLASS (klass);
303 gobject_class->set_property = gst_uri_decode_bin_set_property;
304 gobject_class->get_property = gst_uri_decode_bin_get_property;
305 gobject_class->finalize = gst_uri_decode_bin_finalize;
307 g_object_class_install_property (gobject_class, PROP_URI,
308 g_param_spec_string ("uri", "URI", "URI to decode",
309 DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_SOURCE,
312 g_param_spec_object ("source", "Source", "Source object used",
313 GST_TYPE_ELEMENT, G_PARAM_READABLE));
315 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
316 g_param_spec_uint ("connection-speed", "Connection Speed",
317 "Network connection speed in kbps (0 = unknown)",
318 0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
319 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_CAPS,
322 g_param_spec_boxed ("caps", "Caps",
323 "The caps on which to stop decoding. (NULL = default)",
324 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
327 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
328 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
329 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
330 "be checked for an encoding to use. If that is not set either, "
331 "ISO-8859-15 will be assumed.", NULL,
332 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
335 g_param_spec_int ("buffer-size", "Buffer size (bytes)",
336 "Buffer size when buffering streams (-1 default value)",
337 -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
338 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
340 g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
341 "Buffer duration when buffering streams (-1 default value)",
342 -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
343 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 * GstURIDecodeBin::download:
348 * For certain media type, enable download buffering.
350 g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
351 g_param_spec_boolean ("download", "Download",
352 "Attempt download buffering when buffering network streams",
353 DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 * GstURIDecodeBin::use-buffering:
357 * Emit BUFFERING messages based on low-/high-percent thresholds of the
358 * demuxed or parsed data.
359 * When download buffering is activated and used for the current media
360 * type, this property does nothing. Otherwise perform buffering on the
361 * demuxed or parsed media.
365 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
366 g_param_spec_boolean ("use-buffering", "Use Buffering",
367 "Perform buffering on demuxed/parsed media",
368 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 * GstURIDecodeBin::unknown-type:
372 * @bin: The uridecodebin
373 * @pad: the new pad containing caps that cannot be resolved to a 'final'
375 * @caps: the #GstCaps of the pad that cannot be resolved.
377 * This signal is emitted when a pad for which there is no further possible
378 * decoding is added to the uridecodebin.
380 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
381 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
382 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, unknown_type),
383 NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
384 GST_TYPE_PAD, GST_TYPE_CAPS);
387 * GstURIDecodeBin::autoplug-continue:
388 * @bin: The uridecodebin
390 * @caps: The #GstCaps found.
392 * This signal is emitted whenever uridecodebin finds a new stream. It is
393 * emitted before looking for any elements that can handle that stream.
395 * Returns: #TRUE if you wish uridecodebin to look for elements that can
396 * handle the given @caps. If #FALSE, those caps will be considered as
397 * final and the pad will be exposed as such (see 'new-decoded-pad'
400 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
401 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
402 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
403 autoplug_continue), _gst_boolean_accumulator, NULL,
404 gst_play_marshal_BOOLEAN__OBJECT_BOXED, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
408 * GstURIDecodeBin::autoplug-factories:
409 * @bin: The decodebin
411 * @caps: The #GstCaps found.
413 * This function is emited when an array of possible factories for @caps on
414 * @pad is needed. Uridecodebin will by default return an array with all
415 * compatible factories, sorted by rank.
417 * If this function returns NULL, @pad will be exposed as a final caps.
419 * If this function returns an empty array, the pad will be considered as
420 * having an unhandled type media type.
422 * Returns: a #GValueArray* with a list of factories to try. The factories are
423 * by default tried in the returned order or based on the index returned by
426 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
427 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
428 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
429 autoplug_factories), _gst_array_accumulator, NULL,
430 gst_play_marshal_BOXED__OBJECT_BOXED, G_TYPE_VALUE_ARRAY, 2,
431 GST_TYPE_PAD, GST_TYPE_CAPS);
434 * GstURIDecodeBin::autoplug-select:
436 * @caps: The #GstCaps.
437 * @factory: A #GstElementFactory to use
439 * This signal is emitted once uridecodebin has found all the possible
440 * #GstElementFactory that can be used to handle the given @caps. For each of
441 * those factories, this signal is emited.
443 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
444 * value indicating what decodebin2 should do next.
446 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
449 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
452 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
455 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
456 * operation. The default handler will always return
457 * #GST_AUTOPLUG_SELECT_TRY.
459 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
460 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
461 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
462 autoplug_select), _gst_select_accumulator, NULL,
463 gst_play_marshal_ENUM__OBJECT_BOXED_OBJECT,
464 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
465 GST_TYPE_ELEMENT_FACTORY);
468 * GstURIDecodeBin::drained:
470 * This signal is emitted when the data for the current uri is played.
472 gst_uri_decode_bin_signals[SIGNAL_DRAINED] =
473 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
475 G_STRUCT_OFFSET (GstURIDecodeBinClass, drained), NULL, NULL,
476 gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
478 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_query);
479 gstelement_class->change_state =
480 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_change_state);
482 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);
484 klass->autoplug_continue =
485 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
486 klass->autoplug_factories =
487 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
491 gst_uri_decode_bin_init (GstURIDecodeBin * dec, GstURIDecodeBinClass * klass)
493 /* first filter out the interesting element factories */
494 dec->factories_lock = g_mutex_new ();
495 gst_uri_decode_bin_update_factories_list (dec);
497 dec->lock = g_mutex_new ();
499 dec->uri = g_strdup (DEFAULT_PROP_URI);
500 dec->connection_speed = DEFAULT_CONNECTION_SPEED;
501 dec->caps = DEFAULT_CAPS;
502 dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
504 dec->buffer_duration = DEFAULT_BUFFER_DURATION;
505 dec->buffer_size = DEFAULT_BUFFER_SIZE;
506 dec->download = DEFAULT_DOWNLOAD;
507 dec->use_buffering = DEFAULT_USE_BUFFERING;
511 gst_uri_decode_bin_finalize (GObject * obj)
513 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (obj);
515 remove_decoders (dec, TRUE);
516 g_mutex_free (dec->lock);
517 g_mutex_free (dec->factories_lock);
519 g_free (dec->encoding);
521 g_value_array_free (dec->factories);
523 gst_caps_unref (dec->caps);
525 G_OBJECT_CLASS (parent_class)->finalize (obj);
529 gst_uri_decode_bin_set_encoding (GstURIDecodeBin * dec, const gchar * encoding)
533 GST_URI_DECODE_BIN_LOCK (dec);
535 /* set property first */
536 GST_OBJECT_LOCK (dec);
537 g_free (dec->encoding);
538 dec->encoding = g_strdup (encoding);
539 GST_OBJECT_UNLOCK (dec);
541 /* set the property on all decodebins now */
542 for (walk = dec->decodebins; walk; walk = g_slist_next (walk)) {
543 GObject *decodebin = G_OBJECT (walk->data);
545 g_object_set (decodebin, "subtitle-encoding", encoding, NULL);
547 GST_URI_DECODE_BIN_UNLOCK (dec);
551 gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
552 const GValue * value, GParamSpec * pspec)
554 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
558 GST_OBJECT_LOCK (dec);
560 dec->uri = g_value_dup_string (value);
561 GST_OBJECT_UNLOCK (dec);
563 case PROP_CONNECTION_SPEED:
564 GST_OBJECT_LOCK (dec);
565 dec->connection_speed = g_value_get_uint (value) * 1000;
566 GST_OBJECT_UNLOCK (dec);
569 GST_OBJECT_LOCK (dec);
571 gst_caps_unref (dec->caps);
572 dec->caps = g_value_dup_boxed (value);
573 GST_OBJECT_UNLOCK (dec);
575 case PROP_SUBTITLE_ENCODING:
576 gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
578 case PROP_BUFFER_SIZE:
579 dec->buffer_size = g_value_get_int (value);
581 case PROP_BUFFER_DURATION:
582 dec->buffer_duration = g_value_get_int64 (value);
585 dec->download = g_value_get_boolean (value);
587 case PROP_USE_BUFFERING:
588 dec->use_buffering = g_value_get_boolean (value);
591 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
597 gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
598 GValue * value, GParamSpec * pspec)
600 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
604 GST_OBJECT_LOCK (dec);
605 g_value_set_string (value, dec->uri);
606 GST_OBJECT_UNLOCK (dec);
609 GST_OBJECT_LOCK (dec);
610 g_value_set_object (value, dec->source);
611 GST_OBJECT_UNLOCK (dec);
613 case PROP_CONNECTION_SPEED:
614 GST_OBJECT_LOCK (dec);
615 g_value_set_uint (value, dec->connection_speed / 1000);
616 GST_OBJECT_UNLOCK (dec);
619 GST_OBJECT_LOCK (dec);
620 g_value_set_boxed (value, dec->caps);
621 GST_OBJECT_UNLOCK (dec);
623 case PROP_SUBTITLE_ENCODING:
624 GST_OBJECT_LOCK (dec);
625 g_value_set_string (value, dec->encoding);
626 GST_OBJECT_UNLOCK (dec);
628 case PROP_BUFFER_SIZE:
629 GST_OBJECT_LOCK (dec);
630 g_value_set_int (value, dec->buffer_size);
631 GST_OBJECT_UNLOCK (dec);
633 case PROP_BUFFER_DURATION:
634 GST_OBJECT_LOCK (dec);
635 g_value_set_int64 (value, dec->buffer_duration);
636 GST_OBJECT_UNLOCK (dec);
639 g_value_set_boolean (value, dec->download);
641 case PROP_USE_BUFFERING:
642 g_value_set_boolean (value, dec->use_buffering);
645 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
651 do_async_start (GstURIDecodeBin * dbin)
655 dbin->async_pending = TRUE;
657 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin), FALSE);
658 parent_class->handle_message (GST_BIN_CAST (dbin), message);
662 do_async_done (GstURIDecodeBin * dbin)
666 if (dbin->async_pending) {
667 GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
668 message = gst_message_new_async_done (GST_OBJECT_CAST (dbin));
669 parent_class->handle_message (GST_BIN_CAST (dbin), message);
671 dbin->async_pending = FALSE;
675 #define DEFAULT_QUEUE_SIZE (3 * GST_SECOND)
676 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
677 #define DEFAULT_QUEUE_THRESHOLD ((DEFAULT_QUEUE_SIZE * 95) / 100)
680 unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
681 GstURIDecodeBin * decoder)
685 capsstr = gst_caps_to_string (caps);
686 GST_ELEMENT_WARNING (decoder, STREAM, CODEC_NOT_FOUND,
687 (_("No decoder available for type \'%s\'."), capsstr), (NULL));
691 /* add a streaminfo that indicates that the stream is handled by the
692 * given element. This usually means that a stream without actual data is
693 * produced but one that is sunken by an element. Examples of this are:
694 * cdaudio, a hardware decoder/sink, dvd meta bins etc...
697 add_element_stream (GstElement * element, GstURIDecodeBin * decoder)
699 g_warning ("add element stream");
702 /* when the decoder element signals that no more pads will be generated, we
703 * can commit the current group.
706 no_more_pads_full (GstElement * element, gboolean subs,
707 GstURIDecodeBin * decoder)
712 GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);
714 GST_URI_DECODE_BIN_LOCK (decoder);
715 final = (decoder->pending == 0);
717 /* nothing pending, we can exit */
721 /* the object has no pending no_more_pads */
722 if (!g_object_get_data (G_OBJECT (element), "pending"))
724 g_object_set_data (G_OBJECT (element), "pending", NULL);
727 final = (decoder->pending == 0);
730 GST_URI_DECODE_BIN_UNLOCK (decoder);
733 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
739 no_more_pads (GstElement * element, GstURIDecodeBin * decoder)
741 no_more_pads_full (element, FALSE, decoder);
745 source_no_more_pads (GstElement * element, GstURIDecodeBin * bin)
747 GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
748 GST_ELEMENT_NAME (element));
750 g_signal_handler_disconnect (element, bin->src_np_sig_id);
751 bin->src_np_sig_id = 0;
752 g_signal_handler_disconnect (element, bin->src_nmp_sig_id);
753 bin->src_nmp_sig_id = 0;
755 no_more_pads_full (element, FALSE, bin);
758 /* Called by the signal handlers when a decodebin has
759 * found a new raw pad.
762 new_decoded_pad_cb (GstElement * element, GstPad * pad, gboolean last,
763 GstURIDecodeBin * decoder)
768 GST_DEBUG_OBJECT (element, "new decoded pad, name: <%s>. Last: %d",
769 GST_PAD_NAME (pad), last);
771 GST_URI_DECODE_BIN_LOCK (decoder);
772 padname = g_strdup_printf ("src%d", decoder->numpads);
774 GST_URI_DECODE_BIN_UNLOCK (decoder);
776 newpad = gst_ghost_pad_new (padname, pad);
779 /* store ref to the ghostpad so we can remove it */
780 g_object_set_data (G_OBJECT (pad), "uridecodebin.ghostpad", newpad);
782 gst_pad_set_active (newpad, TRUE);
783 gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
788 source_pad_event_probe (GstPad * pad, GstEvent * event,
789 GstURIDecodeBin * decoder)
791 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
793 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
794 GST_DEBUG_OBJECT (pad, "we received EOS");
796 g_signal_emit (decoder,
797 gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
799 /* never drop events */
803 /* called when we found a raw pad on the source element. We need to set up a
804 * padprobe to detect EOS before exposing the pad. */
806 expose_decoded_pad (GstElement * element, GstPad * pad,
807 GstURIDecodeBin * decoder)
809 gst_pad_add_event_probe (pad, G_CALLBACK (source_pad_event_probe), decoder);
811 new_decoded_pad_cb (element, pad, FALSE, decoder);
815 pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
819 GST_DEBUG_OBJECT (element, "pad removed name: <%s:%s>",
820 GST_DEBUG_PAD_NAME (pad));
822 /* we only care about srcpads */
823 if (!GST_PAD_IS_SRC (pad))
826 if (!(ghost = g_object_get_data (G_OBJECT (pad), "uridecodebin.ghostpad")))
829 /* unghost the pad */
830 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ghost), NULL);
832 /* deactivate and remove */
833 gst_pad_set_active (pad, FALSE);
834 gst_element_remove_pad (GST_ELEMENT_CAST (decoder), ghost);
841 GST_WARNING_OBJECT (element, "no ghost pad found");
846 /* helper function to lookup stuff in lists */
848 array_has_value (const gchar * values[], const gchar * value)
852 for (i = 0; values[i]; i++) {
853 if (g_str_has_prefix (value, values[i]))
860 array_has_uri_value (const gchar * values[], const gchar * value)
864 for (i = 0; values[i]; i++) {
865 if (!g_ascii_strncasecmp (value, values[i], strlen (values[i])))
871 /* list of URIs that we consider to be streams and that need buffering.
872 * We have no mechanism yet to figure this out with a query. */
873 static const gchar *stream_uris[] = { "http://", "mms://", "mmsh://",
874 "mmsu://", "mmst://", "fd://", "myth://", "ssh://", "ftp://", "sftp://",
878 /* list of URIs that need a queue because they are pretty bursty */
879 static const gchar *queue_uris[] = { "cdda://", NULL };
881 /* blacklisted URIs, we know they will always fail. */
882 static const gchar *blacklisted_uris[] = { NULL };
884 /* mime types that we don't consider to be media types */
886 static const gchar *no_media_mimes[] = {
887 "application/x-executable", "application/x-bzip", "application/x-gzip",
888 "application/zip", "application/x-compress", NULL
892 /* media types we can download */
893 static const gchar *download_media[] = {
894 "video/quicktime", "video/x-flv", NULL
897 #define IS_STREAM_URI(uri) (array_has_uri_value (stream_uris, uri))
898 #define IS_QUEUE_URI(uri) (array_has_uri_value (queue_uris, uri))
899 #define IS_BLACKLISTED_URI(uri) (array_has_uri_value (blacklisted_uris, uri))
900 #define IS_NO_MEDIA_MIME(mime) (array_has_value (no_media_mimes, mime))
901 #define IS_DOWNLOAD_MEDIA(media) (array_has_value (download_media, media))
904 * Generate and configure a source element.
907 gen_source_element (GstURIDecodeBin * decoder)
914 GST_LOG_OBJECT (decoder, "finding source for %s", decoder->uri);
916 if (!gst_uri_is_valid (decoder->uri))
919 if (IS_BLACKLISTED_URI (decoder->uri))
920 goto uri_blacklisted;
922 source = gst_element_make_from_uri (GST_URI_SRC, decoder->uri, "source");
926 GST_LOG_OBJECT (decoder, "found source type %s", G_OBJECT_TYPE_NAME (source));
928 decoder->is_stream = IS_STREAM_URI (decoder->uri);
929 GST_LOG_OBJECT (decoder, "source is stream: %d", decoder->is_stream);
931 decoder->need_queue = IS_QUEUE_URI (decoder->uri);
932 GST_LOG_OBJECT (decoder, "source needs queue: %d", decoder->need_queue);
934 /* make HTTP sources send extra headers so we get icecast
935 * metadata in case the stream is an icecast stream */
936 if (!strncmp (decoder->uri, "http://", 7) &&
937 g_object_class_find_property (G_OBJECT_GET_CLASS (source),
939 GST_LOG_OBJECT (decoder, "configuring iradio-mode");
940 g_object_set (source, "iradio-mode", TRUE, NULL);
943 if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
944 "connection-speed")) {
945 GST_DEBUG_OBJECT (decoder,
946 "setting connection-speed=%d to source element",
947 decoder->connection_speed / 1000);
948 g_object_set (source, "connection-speed",
949 decoder->connection_speed / 1000, NULL);
951 if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
952 "subtitle-encoding")) {
953 GST_DEBUG_OBJECT (decoder,
954 "setting subtitle-encoding=%s to source element", decoder->encoding);
955 g_object_set (source, "subtitle-encoding", decoder->encoding, NULL);
962 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
963 (_("No URI specified to play from.")), (NULL));
968 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
969 (_("Invalid URI \"%s\"."), decoder->uri), (NULL));
974 GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
975 (_("This stream type cannot be played yet.")), (NULL));
980 gchar *prot = gst_uri_get_protocol (decoder->uri);
982 /* whoops, could not create the source element, dig a little deeper to
983 * figure out what might be wrong. */
988 gst_missing_uri_source_message_new (GST_ELEMENT_CAST (decoder), prot);
989 gst_element_post_message (GST_ELEMENT_CAST (decoder), msg);
991 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
992 (_("No URI handler implemented for \"%s\"."), prot), (NULL));
1004 * @all_raw: pointer to hold the result
1006 * check if the caps of the pad are all raw. The caps are all raw if
1007 * all of its structures contain audio/x-raw or video/x-raw.
1009 * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
1012 has_all_raw_caps (GstPad * pad, GstCaps * rawcaps, gboolean * all_raw)
1014 GstCaps *caps, *intersection;
1016 gboolean res = FALSE;
1018 caps = gst_pad_get_caps_reffed (pad);
1022 GST_DEBUG_OBJECT (pad, "have caps %" GST_PTR_FORMAT, caps);
1024 capssize = gst_caps_get_size (caps);
1025 /* no caps, skip and move to the next pad */
1026 if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1029 intersection = gst_caps_intersect (caps, rawcaps);
1030 *all_raw = !gst_caps_is_empty (intersection)
1031 && (gst_caps_get_size (intersection) == capssize);
1032 gst_caps_unref (intersection);
1037 gst_caps_unref (caps);
1042 post_missing_plugin_error (GstElement * dec, const gchar * element_name)
1046 msg = gst_missing_element_message_new (dec, element_name);
1047 gst_element_post_message (dec, msg);
1049 GST_ELEMENT_ERROR (dec, CORE, MISSING_PLUGIN,
1050 (_("Missing element '%s' - check your GStreamer installation."),
1051 element_name), (NULL));
1056 * @decoder: a #GstURIDecodeBin
1057 * @is_raw: are all pads raw data
1058 * @have_out: does the source have output
1059 * @is_dynamic: is this a dynamic source
1060 * @use_queue: put a queue before raw output pads
1062 * Check the source of @decoder and collect information about it.
1064 * @is_raw will be set to TRUE if the source only produces raw pads. When this
1065 * function returns, all of the raw pad of the source will be added
1068 * @have_out: will be set to TRUE if the source has output pads.
1070 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1073 * Returns: FALSE if a fatal error occured while scanning.
1076 analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
1077 gboolean * have_out, gboolean * is_dynamic, gboolean use_queue)
1079 GstIterator *pads_iter;
1080 gboolean done = FALSE;
1081 gboolean res = TRUE;
1087 *is_dynamic = FALSE;
1089 g_object_get (decoder, "caps", &rawcaps, NULL);
1091 rawcaps = DEFAULT_CAPS;
1093 pads_iter = gst_element_iterate_src_pads (decoder->source);
1095 switch (gst_iterator_next (pads_iter, (gpointer) & pad)) {
1096 case GST_ITERATOR_ERROR:
1099 case GST_ITERATOR_DONE:
1102 case GST_ITERATOR_RESYNC:
1103 /* reset results and resync */
1106 *is_dynamic = FALSE;
1107 gst_iterator_resync (pads_iter);
1109 case GST_ITERATOR_OK:
1110 /* we now officially have an ouput pad */
1113 /* if FALSE, this pad has no caps and we continue with the next pad. */
1114 if (!has_all_raw_caps (pad, rawcaps, is_raw)) {
1115 gst_object_unref (pad);
1119 /* caps on source pad are all raw, we can add the pad */
1121 GstElement *outelem;
1126 /* insert a queue element right before the raw pad */
1127 outelem = gst_element_factory_make ("queue2", NULL);
1131 gst_bin_add (GST_BIN_CAST (decoder), outelem);
1133 sinkpad = gst_element_get_static_pad (outelem, "sink");
1134 gst_pad_link (pad, sinkpad);
1135 gst_object_unref (sinkpad);
1136 gst_object_unref (pad);
1138 /* save queue pointer so we can remove it later */
1139 decoder->queue = outelem;
1141 /* get the new raw srcpad */
1142 pad = gst_element_get_static_pad (outelem, "src");
1144 outelem = decoder->source;
1146 expose_decoded_pad (outelem, pad, decoder);
1148 gst_object_unref (pad);
1152 gst_iterator_free (pads_iter);
1153 gst_caps_unref (rawcaps);
1156 GstElementClass *elemclass;
1159 /* element has no output pads, check for padtemplates that list SOMETIMES
1161 elemclass = GST_ELEMENT_GET_CLASS (decoder->source);
1163 walk = gst_element_class_get_pad_template_list (elemclass);
1164 while (walk != NULL) {
1165 GstPadTemplate *templ;
1167 templ = (GstPadTemplate *) walk->data;
1168 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1169 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES)
1173 walk = g_list_next (walk);
1180 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1182 gst_object_unref (pad);
1183 gst_iterator_free (pads_iter);
1184 gst_caps_unref (rawcaps);
1190 /* Remove all decodebin2 from ourself
1191 * If force is FALSE, then the decodebin2 instances will be stored in
1192 * pending_decodebins for re-use later on.
1193 * If force is TRUE, then all decodebin2 instances will be unreferenced
1194 * and cleared, including the pending ones. */
1196 remove_decoders (GstURIDecodeBin * bin, gboolean force)
1200 for (walk = bin->decodebins; walk; walk = g_slist_next (walk)) {
1201 GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1203 GST_DEBUG_OBJECT (bin, "removing old decoder element");
1205 gst_element_set_state (decoder, GST_STATE_NULL);
1206 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1210 gst_element_set_state (decoder, GST_STATE_READY);
1211 /* add it to our list of pending decodebins */
1212 g_object_ref (decoder);
1213 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1214 /* restore some properties we might have changed */
1215 g_object_set (decoder, "sink-caps", NULL, NULL);
1216 caps = DEFAULT_CAPS;
1217 g_object_set (decoder, "caps", caps, NULL);
1218 gst_caps_unref (caps);
1220 bin->pending_decodebins =
1221 g_slist_prepend (bin->pending_decodebins, decoder);
1224 g_slist_free (bin->decodebins);
1225 bin->decodebins = NULL;
1229 for (tmp = bin->pending_decodebins; tmp; tmp = tmp->next) {
1230 gst_element_set_state ((GstElement *) tmp->data, GST_STATE_NULL);
1231 gst_object_unref ((GstElement *) tmp->data);
1233 g_slist_free (bin->pending_decodebins);
1234 bin->pending_decodebins = NULL;
1240 remove_pads (GstURIDecodeBin * bin)
1244 for (walk = bin->srcpads; walk; walk = g_slist_next (walk)) {
1245 GstPad *pad = GST_PAD_CAST (walk->data);
1247 GST_DEBUG_OBJECT (bin, "removing old pad");
1248 gst_pad_set_active (pad, FALSE);
1249 gst_element_remove_pad (GST_ELEMENT_CAST (bin), pad);
1251 g_slist_free (bin->srcpads);
1252 bin->srcpads = NULL;
1256 proxy_unknown_type_signal (GstElement * element, GstPad * pad, GstCaps * caps,
1257 GstURIDecodeBin * dec)
1259 GST_DEBUG_OBJECT (dec, "unknown-type signaled");
1262 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1266 proxy_autoplug_continue_signal (GstElement * element, GstPad * pad,
1267 GstCaps * caps, GstURIDecodeBin * dec)
1272 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, pad, caps,
1275 GST_DEBUG_OBJECT (dec, "autoplug-continue returned %d", result);
1280 static GValueArray *
1281 proxy_autoplug_factories_signal (GstElement * element, GstPad * pad,
1282 GstCaps * caps, GstURIDecodeBin * dec)
1284 GValueArray *result;
1287 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, pad, caps,
1290 GST_DEBUG_OBJECT (dec, "autoplug-factories returned %p", result);
1295 static GstAutoplugSelectResult
1296 proxy_autoplug_select_signal (GstElement * element, GstPad * pad,
1297 GstCaps * caps, GstElementFactory * factory, GstURIDecodeBin * dec)
1299 GstAutoplugSelectResult result;
1302 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, factory,
1305 GST_DEBUG_OBJECT (dec, "autoplug-select returned %d", result);
1311 proxy_drained_signal (GstElement * element, GstURIDecodeBin * dec)
1313 GST_DEBUG_OBJECT (dec, "drained signaled");
1315 g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1318 /* make a decodebin and connect to all the signals */
1320 make_decoder (GstURIDecodeBin * decoder)
1322 GstElement *decodebin;
1324 /* re-use pending decodebin2 */
1325 if (decoder->pending_decodebins) {
1326 GSList *first = decoder->pending_decodebins;
1327 GST_LOG_OBJECT (decoder, "re-using pending decodebin2");
1328 decodebin = (GstElement *) first->data;
1329 decoder->pending_decodebins =
1330 g_slist_delete_link (decoder->pending_decodebins, first);
1332 GST_LOG_OBJECT (decoder, "making new decodebin2");
1334 /* now create the decoder element */
1335 decodebin = gst_element_factory_make ("decodebin2", NULL);
1339 /* connect signals to proxy */
1340 g_signal_connect (decodebin, "unknown-type",
1341 G_CALLBACK (proxy_unknown_type_signal), decoder);
1342 g_signal_connect (decodebin, "autoplug-continue",
1343 G_CALLBACK (proxy_autoplug_continue_signal), decoder);
1344 g_signal_connect (decodebin, "autoplug-factories",
1345 G_CALLBACK (proxy_autoplug_factories_signal), decoder);
1346 g_signal_connect (decodebin, "autoplug-select",
1347 G_CALLBACK (proxy_autoplug_select_signal), decoder);
1348 g_signal_connect (decodebin, "drained",
1349 G_CALLBACK (proxy_drained_signal), decoder);
1351 /* set up callbacks to create the links between decoded data
1352 * and video/audio/subtitle rendering/output. */
1353 g_signal_connect (decodebin,
1354 "new-decoded-pad", G_CALLBACK (new_decoded_pad_cb), decoder);
1355 g_signal_connect (decodebin,
1356 "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
1357 g_signal_connect (decodebin, "no-more-pads",
1358 G_CALLBACK (no_more_pads), decoder);
1359 g_signal_connect (decodebin,
1360 "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
1363 /* configure caps if we have any */
1365 g_object_set (decodebin, "caps", decoder->caps, NULL);
1367 if (!decoder->is_stream) {
1368 /* propagate the use-buffering property but only when we are not already
1369 * doing stream buffering with queue2. FIXME, we might want to do stream
1370 * buffering with the multiqueue buffering instead of queue2. */
1371 g_object_set (decodebin, "use-buffering", decoder->use_buffering, NULL);
1373 if (decoder->use_buffering) {
1377 /* configure sizes when buffering */
1378 if ((max_bytes = decoder->buffer_size) == -1)
1379 max_bytes = 2 * 1024 * 1024;
1380 if ((max_time = decoder->buffer_duration) == -1)
1381 max_time = 2 * GST_SECOND;
1383 g_object_set (decodebin, "max-size-bytes", max_bytes, "max-size-buffers",
1384 (guint) 0, "max-size-time", max_time, NULL);
1388 g_object_set_data (G_OBJECT (decodebin), "pending", "1");
1389 g_object_set (decodebin, "subtitle-encoding", decoder->encoding, NULL);
1391 GST_LOG_OBJECT (decoder, "have %d pending dynamic objects", decoder->pending);
1393 gst_bin_add (GST_BIN_CAST (decoder), decodebin);
1395 decoder->decodebins = g_slist_prepend (decoder->decodebins, decodebin);
1402 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "decodebin2");
1407 /* signaled when we have a stream and we need to configure the download
1408 * buffering or regular buffering */
1410 type_found (GstElement * typefind, guint probability,
1411 GstCaps * caps, GstURIDecodeBin * decoder)
1413 GstElement *dec_elem, *queue;
1415 const gchar *media_type;
1417 GST_DEBUG_OBJECT (decoder, "typefind found caps %" GST_PTR_FORMAT, caps);
1419 s = gst_caps_get_structure (caps, 0);
1420 media_type = gst_structure_get_name (s);
1422 /* remember if we need download buffering */
1423 decoder->is_download = IS_DOWNLOAD_MEDIA (media_type) && decoder->download;
1425 dec_elem = make_decoder (decoder);
1429 queue = gst_element_factory_make ("queue2", NULL);
1433 g_object_set (queue, "use-buffering", TRUE, NULL);
1435 GST_DEBUG_OBJECT (decoder, "check media-type %s, %d", media_type,
1438 if (decoder->is_download) {
1439 gchar *temp_template, *filename;
1440 const gchar *tmp_dir, *prgname;
1442 tmp_dir = g_get_tmp_dir ();
1443 prgname = g_get_prgname ();
1444 if (prgname == NULL)
1445 prgname = "GStreamer";
1447 filename = g_strdup_printf ("%s-XXXXXX", prgname);
1449 /* build our filename */
1450 temp_template = g_build_filename (tmp_dir, filename, NULL);
1452 GST_DEBUG_OBJECT (decoder, "enable download buffering in %s (%s, %s, %s)",
1453 temp_template, tmp_dir, prgname, filename);
1455 /* configure progressive download for selected media types */
1456 g_object_set (queue, "temp-template", temp_template, NULL);
1459 g_free (temp_template);
1462 /* Disable max-size-buffers */
1463 g_object_set (queue, "max-size-buffers", 0, NULL);
1465 /* If buffer size or duration are set, set them on the queue2 element */
1466 if (decoder->buffer_size != -1)
1467 g_object_set (queue, "max-size-bytes", decoder->buffer_size, NULL);
1468 if (decoder->buffer_duration != -1)
1469 g_object_set (queue, "max-size-time", decoder->buffer_duration, NULL);
1471 gst_bin_add (GST_BIN_CAST (decoder), queue);
1473 if (!gst_element_link_pads (typefind, "src", queue, "sink"))
1474 goto could_not_link;
1476 /* to force caps on the decodebin element and avoid reparsing stuff by
1477 * typefind. It also avoids a deadlock in the way typefind activates pads in
1478 * the state change */
1479 g_object_set (dec_elem, "sink-caps", caps, NULL);
1481 if (!gst_element_link_pads (queue, "src", dec_elem, "sink"))
1482 goto could_not_link;
1484 gst_element_set_state (dec_elem, GST_STATE_PLAYING);
1485 gst_element_set_state (queue, GST_STATE_PLAYING);
1487 do_async_done (decoder);
1494 /* error was posted */
1499 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1500 (NULL), ("Can't link typefind to decodebin2 element"));
1505 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1510 /* setup a streaming source. This will first plug a typefind element to the
1511 * source. After we find the type, we decide to plug a queue2 and continue to
1512 * plug a decodebin2 starting from the found caps */
1514 setup_streaming (GstURIDecodeBin * decoder)
1516 GstElement *typefind;
1518 /* now create the decoder element */
1519 typefind = gst_element_factory_make ("typefind", NULL);
1523 gst_bin_add (GST_BIN_CAST (decoder), typefind);
1525 if (!gst_element_link_pads (decoder->source, NULL, typefind, "sink"))
1526 goto could_not_link;
1528 decoder->typefind = typefind;
1530 /* connect a signal to find out when the typefind element found
1532 decoder->have_type_id =
1533 g_signal_connect (decoder->typefind, "have-type",
1534 G_CALLBACK (type_found), decoder);
1536 do_async_start (decoder);
1543 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "typefind");
1548 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1549 (NULL), ("Can't link source to typefind element"));
1550 gst_bin_remove (GST_BIN_CAST (decoder), typefind);
1555 /* remove source and all related elements */
1557 remove_source (GstURIDecodeBin * bin)
1559 GstElement *source = bin->source;
1562 GST_DEBUG_OBJECT (bin, "removing old src element");
1563 gst_element_set_state (source, GST_STATE_NULL);
1564 gst_bin_remove (GST_BIN_CAST (bin), source);
1566 if (bin->src_np_sig_id) {
1567 g_signal_handler_disconnect (source, bin->src_np_sig_id);
1568 bin->src_np_sig_id = 0;
1570 if (bin->src_nmp_sig_id) {
1571 g_signal_handler_disconnect (source, bin->src_nmp_sig_id);
1572 bin->src_nmp_sig_id = 0;
1577 GST_DEBUG_OBJECT (bin, "removing old queue element");
1578 gst_element_set_state (bin->queue, GST_STATE_NULL);
1579 gst_bin_remove (GST_BIN_CAST (bin), bin->queue);
1582 if (bin->typefind) {
1583 GST_DEBUG_OBJECT (bin, "removing old typefind element");
1584 gst_element_set_state (bin->typefind, GST_STATE_NULL);
1585 gst_bin_remove (GST_BIN_CAST (bin), bin->typefind);
1586 bin->typefind = NULL;
1590 /* is called when a dynamic source element created a new pad. */
1592 source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
1594 GstElement *decoder;
1598 GST_URI_DECODE_BIN_LOCK (bin);
1599 GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
1600 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
1602 g_object_get (bin, "caps", &rawcaps, NULL);
1604 rawcaps = DEFAULT_CAPS;
1606 /* if this is a pad with all raw caps, we can expose it */
1607 if (has_all_raw_caps (pad, rawcaps, &is_raw) && is_raw) {
1608 /* it's all raw, create output pads. */
1609 GST_URI_DECODE_BIN_UNLOCK (bin);
1610 gst_caps_unref (rawcaps);
1611 expose_decoded_pad (element, pad, bin);
1614 gst_caps_unref (rawcaps);
1616 /* not raw, create decoder */
1617 decoder = make_decoder (bin);
1621 /* and link to decoder */
1622 if (!gst_element_link_pads (bin->source, NULL, decoder, "sink"))
1623 goto could_not_link;
1625 GST_DEBUG_OBJECT (bin, "linked decoder to new pad");
1627 gst_element_set_state (decoder, GST_STATE_PLAYING);
1628 GST_URI_DECODE_BIN_UNLOCK (bin);
1635 /* error was posted */
1636 GST_URI_DECODE_BIN_UNLOCK (bin);
1641 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
1642 (NULL), ("Can't link source to decoder element"));
1643 GST_URI_DECODE_BIN_UNLOCK (bin);
1648 /* construct and run the source and decoder elements until we found
1649 * all the streams or until a preroll queue has been filled.
1652 setup_source (GstURIDecodeBin * decoder)
1654 gboolean is_raw, have_out, is_dynamic;
1656 GST_DEBUG_OBJECT (decoder, "setup source");
1658 /* delete old src */
1659 remove_source (decoder);
1661 decoder->pending = 0;
1663 /* create and configure an element that can handle the uri */
1664 if (!(decoder->source = gen_source_element (decoder)))
1667 /* state will be merged later - if file is not found, error will be
1668 * handled by the application right after. */
1669 gst_bin_add (GST_BIN_CAST (decoder), decoder->source);
1671 /* notify of the new source used */
1672 g_object_notify (G_OBJECT (decoder), "source");
1674 /* remove the old decoders now, if any */
1675 remove_decoders (decoder, FALSE);
1677 /* see if the source element emits raw audio/video all by itself,
1678 * if so, we can create streams for the pads and be done with it.
1679 * Also check that is has source pads, if not, we assume it will
1680 * do everything itself. */
1681 if (!analyse_source (decoder, &is_raw, &have_out, &is_dynamic,
1682 decoder->need_queue))
1683 goto invalid_source;
1686 GST_DEBUG_OBJECT (decoder, "Source provides all raw data");
1687 /* source provides raw data, we added the pads and we can now signal a
1688 * no_more pads because we are done. */
1689 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
1692 if (!have_out && !is_dynamic) {
1693 GST_DEBUG_OBJECT (decoder, "Source has no output pads");
1694 /* create a stream to indicate that this uri is handled by a self
1695 * contained element. We are now done. */
1696 add_element_stream (decoder->source, decoder);
1700 GST_DEBUG_OBJECT (decoder, "Source has dynamic output pads");
1701 /* connect a handler for the new-pad signal */
1702 decoder->src_np_sig_id =
1703 g_signal_connect (decoder->source, "pad-added",
1704 G_CALLBACK (source_new_pad), decoder);
1705 decoder->src_nmp_sig_id =
1706 g_signal_connect (decoder->source, "no-more-pads",
1707 G_CALLBACK (source_no_more_pads), decoder);
1708 g_object_set_data (G_OBJECT (decoder->source), "pending", "1");
1711 if (decoder->is_stream) {
1712 GST_DEBUG_OBJECT (decoder, "Setting up streaming");
1713 /* do the stream things here */
1714 if (!setup_streaming (decoder))
1715 goto streaming_failed;
1717 GstElement *dec_elem;
1719 /* no streaming source, we can link now */
1720 GST_DEBUG_OBJECT (decoder, "Plugging decodebin to source");
1722 dec_elem = make_decoder (decoder);
1726 if (!gst_element_link_pads (decoder->source, NULL, dec_elem, "sink"))
1727 goto could_not_link;
1735 /* error message was already posted */
1740 GST_ELEMENT_ERROR (decoder, CORE, FAILED,
1741 (_("Source element is invalid.")), (NULL));
1746 /* message was posted */
1751 /* message was posted */
1756 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1757 (NULL), ("Can't link source to decoder element"));
1763 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
1764 GList * structure_list)
1768 for (l = structure_list; l != NULL; l = l->next) {
1769 GValue val = { 0, };
1772 *first = gst_structure_copy ((GstStructure *) l->data);
1774 g_value_init (&val, GST_TYPE_STRUCTURE);
1775 g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
1776 gst_value_list_append_value (list_val, &val);
1777 g_value_unset (&val);
1781 /* if it's a redirect message with multiple redirect locations we might
1782 * want to pick a different 'best' location depending on the required
1783 * bitrates and the connection speed */
1785 handle_redirect_message (GstURIDecodeBin * dec, GstMessage * msg)
1787 const GValue *locations_list, *location_val;
1788 GstMessage *new_msg;
1789 GstStructure *new_structure = NULL;
1790 GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
1791 GValue new_list = { 0, };
1794 GST_DEBUG_OBJECT (dec, "redirect message: %" GST_PTR_FORMAT, msg);
1795 GST_DEBUG_OBJECT (dec, "connection speed: %u", dec->connection_speed);
1797 if (dec->connection_speed == 0 || msg->structure == NULL)
1800 locations_list = gst_structure_get_value (msg->structure, "locations");
1801 if (locations_list == NULL)
1804 size = gst_value_list_get_size (locations_list);
1808 /* maintain existing order as much as possible, just sort references
1809 * with too high a bitrate to the end (the assumption being that if
1810 * bitrates are given they are given for all interesting streams and
1811 * that the you-need-at-least-version-xyz redirect has the same bitrate
1812 * as the lowest referenced redirect alternative) */
1813 for (i = 0; i < size; ++i) {
1814 const GstStructure *s;
1817 location_val = gst_value_list_get_value (locations_list, i);
1818 s = (const GstStructure *) g_value_get_boxed (location_val);
1819 if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
1820 GST_DEBUG_OBJECT (dec, "no bitrate: %" GST_PTR_FORMAT, s);
1821 l_neutral = g_list_append (l_neutral, (gpointer) s);
1822 } else if (bitrate > dec->connection_speed) {
1823 GST_DEBUG_OBJECT (dec, "bitrate too high: %" GST_PTR_FORMAT, s);
1824 l_bad = g_list_append (l_bad, (gpointer) s);
1825 } else if (bitrate <= dec->connection_speed) {
1826 GST_DEBUG_OBJECT (dec, "bitrate OK: %" GST_PTR_FORMAT, s);
1827 l_good = g_list_append (l_good, (gpointer) s);
1831 g_value_init (&new_list, GST_TYPE_LIST);
1832 value_list_append_structure_list (&new_list, &new_structure, l_good);
1833 value_list_append_structure_list (&new_list, &new_structure, l_neutral);
1834 value_list_append_structure_list (&new_list, &new_structure, l_bad);
1835 gst_structure_set_value (new_structure, "locations", &new_list);
1836 g_value_unset (&new_list);
1838 g_list_free (l_good);
1839 g_list_free (l_neutral);
1840 g_list_free (l_bad);
1842 new_msg = gst_message_new_element (msg->src, new_structure);
1843 gst_message_unref (msg);
1845 GST_DEBUG_OBJECT (dec, "new redirect message: %" GST_PTR_FORMAT, new_msg);
1850 handle_message (GstBin * bin, GstMessage * msg)
1852 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT && msg->structure != NULL
1853 && gst_structure_has_name (msg->structure, "redirect")) {
1854 /* sort redirect messages based on the connection speed. This simplifies
1855 * the user of this element as it can in most cases just pick the first item
1856 * of the sorted list as a good redirection candidate. It can of course
1857 * choose something else from the list if it has a better way. */
1858 msg = handle_redirect_message (GST_URI_DECODE_BIN (bin), msg);
1860 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
1863 /* generic struct passed to all query fold methods
1864 * FIXME, move to core.
1875 typedef void (*QueryInitFunction) (GstURIDecodeBin * dec, QueryFold * fold);
1876 typedef void (*QueryDoneFunction) (GstURIDecodeBin * dec, QueryFold * fold);
1878 /* for duration/position we collect all durations/positions and take
1879 * the MAX of all valid results */
1881 decoder_query_init (GstURIDecodeBin * dec, QueryFold * fold)
1885 fold->seekable = TRUE;
1890 decoder_query_duration_fold (GstPad * item, GValue * ret, QueryFold * fold)
1892 if (gst_pad_query (item, fold->query)) {
1895 g_value_set_boolean (ret, TRUE);
1897 gst_query_parse_duration (fold->query, NULL, &duration);
1899 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
1901 if (duration > fold->max)
1902 fold->max = duration;
1904 gst_object_unref (item);
1909 decoder_query_duration_done (GstURIDecodeBin * dec, QueryFold * fold)
1913 gst_query_parse_duration (fold->query, &format, NULL);
1914 /* store max in query result */
1915 gst_query_set_duration (fold->query, format, fold->max);
1917 GST_DEBUG ("max duration %" G_GINT64_FORMAT, fold->max);
1921 decoder_query_position_fold (GstPad * item, GValue * ret, QueryFold * fold)
1923 if (gst_pad_query (item, fold->query)) {
1926 g_value_set_boolean (ret, TRUE);
1928 gst_query_parse_position (fold->query, NULL, &position);
1930 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
1932 if (position > fold->max)
1933 fold->max = position;
1936 gst_object_unref (item);
1941 decoder_query_position_done (GstURIDecodeBin * dec, QueryFold * fold)
1945 gst_query_parse_position (fold->query, &format, NULL);
1946 /* store max in query result */
1947 gst_query_set_position (fold->query, format, fold->max);
1949 GST_DEBUG_OBJECT (dec, "max position %" G_GINT64_FORMAT, fold->max);
1953 decoder_query_latency_fold (GstPad * item, GValue * ret, QueryFold * fold)
1955 if (gst_pad_query (item, fold->query)) {
1956 GstClockTime min, max;
1959 g_value_set_boolean (ret, TRUE);
1961 gst_query_parse_latency (fold->query, &live, &min, &max);
1963 GST_DEBUG_OBJECT (item,
1964 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1965 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1967 /* for the combined latency we collect the MAX of all min latencies and
1968 * the MIN of all max latencies */
1969 if (min > fold->min)
1971 if (fold->max == -1)
1973 else if (max < fold->max)
1975 if (fold->live == FALSE)
1979 gst_object_unref (item);
1984 decoder_query_latency_done (GstURIDecodeBin * dec, QueryFold * fold)
1986 /* store max in query result */
1987 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
1989 GST_DEBUG_OBJECT (dec,
1990 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1991 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
1995 /* we are seekable if all srcpads are seekable */
1997 decoder_query_seeking_fold (GstPad * item, GValue * ret, QueryFold * fold)
1999 if (gst_pad_query (item, fold->query)) {
2002 g_value_set_boolean (ret, TRUE);
2003 gst_query_parse_seeking (fold->query, NULL, &seekable, NULL, NULL);
2005 GST_DEBUG_OBJECT (item, "got seekable %d", seekable);
2007 if (fold->seekable == TRUE)
2008 fold->seekable = seekable;
2010 gst_object_unref (item);
2016 decoder_query_seeking_done (GstURIDecodeBin * dec, QueryFold * fold)
2020 gst_query_parse_seeking (fold->query, &format, NULL, NULL, NULL);
2021 gst_query_set_seeking (fold->query, format, fold->seekable, 0, -1);
2023 GST_DEBUG_OBJECT (dec, "seekable %d", fold->seekable);
2026 /* generic fold, return first valid result */
2028 decoder_query_generic_fold (GstPad * item, GValue * ret, QueryFold * fold)
2032 if ((res = gst_pad_query (item, fold->query))) {
2033 g_value_set_boolean (ret, TRUE);
2034 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
2037 gst_object_unref (item);
2039 /* and stop as soon as we have a valid result */
2044 /* we're a bin, the default query handler iterates sink elements, which we don't
2045 * have normally. We should just query all source pads.
2048 gst_uri_decode_bin_query (GstElement * element, GstQuery * query)
2050 GstURIDecodeBin *decoder;
2051 gboolean res = FALSE;
2053 GstIteratorFoldFunction fold_func;
2054 QueryInitFunction fold_init = NULL;
2055 QueryDoneFunction fold_done = NULL;
2056 QueryFold fold_data;
2059 decoder = GST_URI_DECODE_BIN (element);
2061 switch (GST_QUERY_TYPE (query)) {
2062 case GST_QUERY_DURATION:
2063 /* iterate and collect durations */
2064 fold_func = (GstIteratorFoldFunction) decoder_query_duration_fold;
2065 fold_init = decoder_query_init;
2066 fold_done = decoder_query_duration_done;
2068 case GST_QUERY_POSITION:
2069 /* iterate and collect durations */
2070 fold_func = (GstIteratorFoldFunction) decoder_query_position_fold;
2071 fold_init = decoder_query_init;
2072 fold_done = decoder_query_position_done;
2074 case GST_QUERY_LATENCY:
2075 /* iterate and collect durations */
2076 fold_func = (GstIteratorFoldFunction) decoder_query_latency_fold;
2077 fold_init = decoder_query_init;
2078 fold_done = decoder_query_latency_done;
2080 case GST_QUERY_SEEKING:
2081 /* iterate and collect durations */
2082 fold_func = (GstIteratorFoldFunction) decoder_query_seeking_fold;
2083 fold_init = decoder_query_init;
2084 fold_done = decoder_query_seeking_done;
2087 fold_func = (GstIteratorFoldFunction) decoder_query_generic_fold;
2091 fold_data.query = query;
2093 g_value_init (&ret, G_TYPE_BOOLEAN);
2094 g_value_set_boolean (&ret, FALSE);
2096 iter = gst_element_iterate_src_pads (element);
2097 GST_DEBUG_OBJECT (element, "Sending query %p (type %d) to src pads",
2098 query, GST_QUERY_TYPE (query));
2101 fold_init (decoder, &fold_data);
2104 GstIteratorResult ires;
2106 ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
2109 case GST_ITERATOR_RESYNC:
2110 gst_iterator_resync (iter);
2112 fold_init (decoder, &fold_data);
2113 g_value_set_boolean (&ret, FALSE);
2115 case GST_ITERATOR_OK:
2116 case GST_ITERATOR_DONE:
2117 res = g_value_get_boolean (&ret);
2118 if (fold_done != NULL && res)
2119 fold_done (decoder, &fold_data);
2127 gst_iterator_free (iter);
2132 static GstStateChangeReturn
2133 gst_uri_decode_bin_change_state (GstElement * element,
2134 GstStateChange transition)
2136 GstStateChangeReturn ret;
2137 GstURIDecodeBin *decoder;
2139 decoder = GST_URI_DECODE_BIN (element);
2141 switch (transition) {
2142 case GST_STATE_CHANGE_NULL_TO_READY:
2143 g_mutex_lock (decoder->factories_lock);
2144 gst_uri_decode_bin_update_factories_list (decoder);
2145 g_mutex_unlock (decoder->factories_lock);
2147 case GST_STATE_CHANGE_READY_TO_PAUSED:
2148 if (!setup_source (decoder))
2155 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2157 switch (transition) {
2158 case GST_STATE_CHANGE_READY_TO_PAUSED:
2159 GST_DEBUG ("ready to paused");
2160 if (ret == GST_STATE_CHANGE_FAILURE)
2163 case GST_STATE_CHANGE_PAUSED_TO_READY:
2164 GST_DEBUG ("paused to ready");
2165 remove_decoders (decoder, FALSE);
2166 remove_pads (decoder);
2167 remove_source (decoder);
2168 do_async_done (decoder);
2170 case GST_STATE_CHANGE_READY_TO_NULL:
2171 GST_DEBUG ("ready to null");
2172 remove_decoders (decoder, TRUE);
2173 remove_pads (decoder);
2174 remove_source (decoder);
2184 return GST_STATE_CHANGE_FAILURE;
2188 /* clean up leftover groups */
2189 return GST_STATE_CHANGE_FAILURE;
2193 gboolean gst_decode_bin_plugin_init (GstPlugin * plugin);
2196 gst_uri_decode_bin_plugin_init (GstPlugin * plugin)
2198 GST_DEBUG_CATEGORY_INIT (gst_uri_decode_bin_debug, "uridecodebin", 0,
2199 "URI decoder element");
2202 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2204 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2205 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2206 #endif /* ENABLE_NLS */
2208 return gst_element_register (plugin, "uridecodebin", GST_RANK_NONE,
2209 GST_TYPE_URI_DECODE_BIN);
2213 plugin_init (GstPlugin * plugin)
2215 if (!gst_decode_bin_plugin_init (plugin))
2217 if (!gst_uri_decode_bin_plugin_init (plugin))
2223 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2226 "URI Decoder bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,