2 * Copyright (C) <2015> Jan Schmidt <jan@centricular.com>
3 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-urisourcebin
23 * @title: urisourcebin
25 * urisourcebin is an element for accessing URIs in a uniform manner.
27 * It handles selecting a URI source element and potentially download
28 * buffering for network sources. It produces one or more source pads,
29 * depending on the input source, for feeding to decoding chains or decodebin.
31 * The main configuration is via the #GstURISourceBin:uri property.
33 * > urisourcebin is still experimental API and a technology preview.
34 * > Its behaviour and exposed API is subject to change.
37 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
38 * with newer GLib versions (>= 2.31.0) */
39 #define GLIB_DISABLE_DEPRECATION_WARNINGS
48 #include <glib/gi18n-lib.h>
49 #include <gst/pbutils/missing-plugins.h>
51 #include "gstplay-enum.h"
52 #include "gstrawcaps.h"
53 #include "gstplaybackelements.h"
54 #include "gstplaybackutils.h"
56 #define GST_TYPE_URI_SOURCE_BIN \
57 (gst_uri_source_bin_get_type())
58 #define GST_URI_SOURCE_BIN(obj) \
59 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_SOURCE_BIN,GstURISourceBin))
60 #define GST_URI_SOURCE_BIN_CLASS(klass) \
61 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_SOURCE_BIN,GstURISourceBinClass))
62 #define GST_IS_URI_SOURCE_BIN(obj) \
63 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_SOURCE_BIN))
64 #define GST_IS_URI_SOURCE_BIN_CLASS(klass) \
65 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_SOURCE_BIN))
66 #define GST_URI_SOURCE_BIN_CAST(obj) ((GstURISourceBin *) (obj))
68 typedef struct _GstURISourceBin GstURISourceBin;
69 typedef struct _GstURISourceBinClass GstURISourceBinClass;
70 typedef struct _ChildSrcPadInfo ChildSrcPadInfo;
71 typedef struct _OutputSlotInfo OutputSlotInfo;
73 #define GST_URI_SOURCE_BIN_LOCK(urisrc) (g_mutex_lock(&((GstURISourceBin*)(urisrc))->lock))
74 #define GST_URI_SOURCE_BIN_UNLOCK(urisrc) (g_mutex_unlock(&((GstURISourceBin*)(urisrc))->lock))
76 #define BUFFERING_LOCK(ubin) G_STMT_START { \
77 GST_LOG_OBJECT (ubin, \
78 "buffering locking from thread %p", \
80 g_mutex_lock (&GST_URI_SOURCE_BIN_CAST(ubin)->buffering_lock); \
81 GST_LOG_OBJECT (ubin, \
82 "buffering lock from thread %p", \
86 #define BUFFERING_UNLOCK(ubin) G_STMT_START { \
87 GST_LOG_OBJECT (ubin, \
88 "buffering unlocking from thread %p", \
90 g_mutex_unlock (&GST_URI_SOURCE_BIN_CAST(ubin)->buffering_lock); \
93 /* Track a source pad from the source element and the chain of (optional)
94 * elements that are linked to it up to the output slots */
95 struct _ChildSrcPadInfo
97 GstURISourceBin *urisrc;
99 /* Source pad this info is attached to (reffed) */
102 /* An optional typefind */
103 GstElement *typefind;
105 /* list of output slots */
111 * Handles everything related to outputing, including optional buffering.
113 struct _OutputSlotInfo
115 ChildSrcPadInfo *linked_info; /* source pad info feeding this slot */
117 GstPad *originating_pad; /* Pad that created this OutputSlotInfo (ref held) */
118 GstPad *output_pad; /* Output ghost pad */
120 gboolean is_eos; /* Did EOS get fed into the buffering element */
122 GstElement *queue; /* queue2 or downloadbuffer */
123 GstPad *queue_sinkpad; /* Sink pad of the queue eleemnt */
125 gulong bitrate_changed_id; /* queue bitrate changed notification */
127 guint demuxer_event_probe_id;
133 * urisourcebin element struct
135 struct _GstURISourceBin
137 GstBin parent_instance;
139 GMutex lock; /* lock for constructing */
142 guint64 connection_speed;
144 gboolean activated; /* TRUE if the switch to PAUSED has been completed */
145 gboolean flushing; /* TRUE if switching from PAUSED to READY */
146 GCond activation_cond; /* Uses the urisourcebin lock */
149 gboolean is_adaptive;
150 gboolean demuxer_handles_buffering; /* If TRUE: Don't use buffering elements */
151 guint64 buffer_duration; /* When buffering, buffer duration (ns) */
152 guint buffer_size; /* When buffering, buffer size (bytes) */
154 gboolean use_buffering;
155 gdouble low_watermark;
156 gdouble high_watermark;
160 GList *src_infos; /* List of ChildSrcPadInfo for the source */
162 GstElement *demuxer; /* Adaptive demuxer if any */
166 /* for dynamic sources */
167 guint src_np_sig_id; /* new-pad signal id */
169 guint64 ring_buffer_max_size; /* 0 means disabled */
171 GList *buffering_status; /* element currently buffering messages */
172 gint last_buffering_pct; /* Avoid sending buffering over and over */
173 GMutex buffering_lock;
174 GMutex buffering_post_lock;
177 struct _GstURISourceBinClass
179 GstBinClass parent_class;
181 /* emitted when all data has been drained out
182 * FIXME : What do we need this for ?? */
183 void (*drained) (GstElement * element);
184 /* emitted when all data has been fed into buffering slots (i.e the
185 * actual sources are done) */
186 void (*about_to_finish) (GstElement * element);
189 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
192 GST_STATIC_CAPS_ANY);
194 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
196 GST_DEBUG_CATEGORY_STATIC (gst_uri_source_bin_debug);
197 #define GST_CAT_DEFAULT gst_uri_source_bin_debug
203 SIGNAL_ABOUT_TO_FINISH,
209 #define DEFAULT_PROP_URI NULL
210 #define DEFAULT_PROP_SOURCE NULL
211 #define DEFAULT_CONNECTION_SPEED 0
212 #define DEFAULT_BUFFER_DURATION -1
213 #define DEFAULT_BUFFER_SIZE -1
214 #define DEFAULT_DOWNLOAD FALSE
215 #define DEFAULT_USE_BUFFERING TRUE
216 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
217 #define DEFAULT_LOW_WATERMARK 0.01
218 #define DEFAULT_HIGH_WATERMARK 0.99
220 #define ACTUAL_DEFAULT_BUFFER_SIZE 10 * 1024 * 1024 /* The value used for byte limits when buffer-size == -1 */
221 #define ACTUAL_DEFAULT_BUFFER_DURATION 5 * GST_SECOND /* The value used for time limits when buffer-duration == -1 */
223 #define GET_BUFFER_SIZE(u) ((u)->buffer_size == -1 ? ACTUAL_DEFAULT_BUFFER_SIZE : (u)->buffer_size)
224 #define GET_BUFFER_DURATION(u) ((u)->buffer_duration == -1 ? ACTUAL_DEFAULT_BUFFER_DURATION : (u)->buffer_duration)
226 #define DEFAULT_CAPS (gst_static_caps_get (&default_raw_caps))
232 PROP_CONNECTION_SPEED,
234 PROP_BUFFER_DURATION,
237 PROP_RING_BUFFER_MAX_SIZE,
243 #define CUSTOM_EOS_QUARK _custom_eos_quark_get ()
244 #define CUSTOM_EOS_QUARK_DATA "custom-eos"
246 _custom_eos_quark_get (void)
248 static gsize g_quark;
250 if (g_once_init_enter (&g_quark)) {
252 (gsize) g_quark_from_static_string ("urisourcebin-custom-eos");
253 g_once_init_leave (&g_quark, quark);
258 static void post_missing_plugin_error (GstElement * urisrc,
259 const gchar * element_name);
261 static guint gst_uri_source_bin_signals[LAST_SIGNAL] = { 0 };
263 GType gst_uri_source_bin_get_type (void);
264 #define gst_uri_source_bin_parent_class parent_class
265 G_DEFINE_TYPE (GstURISourceBin, gst_uri_source_bin, GST_TYPE_BIN);
268 GST_DEBUG_CATEGORY_INIT (gst_uri_source_bin_debug, "urisourcebin", 0, "URI source element"); \
269 playback_element_init (plugin);
270 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (urisourcebin, "urisourcebin",
271 GST_RANK_NONE, GST_TYPE_URI_SOURCE_BIN, _do_init);
273 static void gst_uri_source_bin_set_property (GObject * object, guint prop_id,
274 const GValue * value, GParamSpec * pspec);
275 static void gst_uri_source_bin_get_property (GObject * object, guint prop_id,
276 GValue * value, GParamSpec * pspec);
277 static void gst_uri_source_bin_finalize (GObject * obj);
279 static void handle_message (GstBin * bin, GstMessage * msg);
281 static gboolean gst_uri_source_bin_query (GstElement * element,
283 static GstStateChangeReturn gst_uri_source_bin_change_state (GstElement *
284 element, GstStateChange transition);
286 static void handle_new_pad (ChildSrcPadInfo * info, GstPad * srcpad,
288 static gboolean setup_typefind (ChildSrcPadInfo * info);
289 static void remove_demuxer (GstURISourceBin * bin);
290 static void expose_output_pad (GstURISourceBin * urisrc, GstPad * pad);
291 static OutputSlotInfo *new_output_slot (ChildSrcPadInfo * info,
292 gboolean do_download, gboolean is_adaptive, gboolean no_buffering,
293 GstPad * originating_pad);
294 static void free_output_slot (OutputSlotInfo * slot, GstURISourceBin * urisrc);
295 static void free_output_slot_async (GstURISourceBin * urisrc,
296 OutputSlotInfo * slot);
297 static GstPad *create_output_pad (OutputSlotInfo * slot, GstPad * pad);
298 static void remove_buffering_msgs (GstURISourceBin * bin, GstObject * src);
300 static void update_queue_values (GstURISourceBin * urisrc);
301 static GstStructure *get_queue_statistics (GstURISourceBin * urisrc);
304 gst_uri_source_bin_class_init (GstURISourceBinClass * klass)
306 GObjectClass *gobject_class;
307 GstElementClass *gstelement_class;
308 GstBinClass *gstbin_class;
310 gobject_class = G_OBJECT_CLASS (klass);
311 gstelement_class = GST_ELEMENT_CLASS (klass);
312 gstbin_class = GST_BIN_CLASS (klass);
314 gobject_class->set_property = gst_uri_source_bin_set_property;
315 gobject_class->get_property = gst_uri_source_bin_get_property;
316 gobject_class->finalize = gst_uri_source_bin_finalize;
318 g_object_class_install_property (gobject_class, PROP_URI,
319 g_param_spec_string ("uri", "URI", "URI to decode",
320 DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_SOURCE,
323 g_param_spec_object ("source", "Source", "Source object used",
324 GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
327 g_param_spec_uint64 ("connection-speed", "Connection Speed",
328 "Network connection speed in kbps (0 = unknown)",
329 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
332 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
333 g_param_spec_int ("buffer-size", "Buffer size (bytes)",
334 "Buffer size when buffering streams (-1 default value)",
335 -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
338 g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
339 "Buffer duration when buffering streams (-1 default value)",
340 -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 * GstURISourceBin::download:
346 * For certain media type, enable download buffering.
348 g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
349 g_param_spec_boolean ("download", "Download",
350 "Attempt download buffering when buffering network streams",
351 DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 * GstURISourceBin::use-buffering:
356 * Perform buffering using a queue2 element, and emit BUFFERING
357 * messages based on low-/high-percent thresholds of streaming data,
358 * such as adaptive-demuxer streams.
360 * When download buffering is activated and used for the current media
361 * type, this property does nothing.
364 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
365 g_param_spec_boolean ("use-buffering", "Use Buffering",
366 "Perform buffering on demuxed/parsed media",
367 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 * GstURISourceBin::ring-buffer-max-size
372 * The maximum size of the ring buffer in kilobytes. If set to 0, the ring
373 * buffer is disabled. Default is 0.
376 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
377 g_param_spec_uint64 ("ring-buffer-max-size",
378 "Max. ring buffer size (bytes)",
379 "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
380 0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstURISourceBin::low-watermark
386 * Proportion of the queue size (either in bytes or time) for buffering
387 * to restart when crossed from above. Only used if use-buffering is TRUE.
389 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
390 g_param_spec_double ("low-watermark", "Low watermark",
391 "Low threshold for buffering to start. Only used if use-buffering is True",
392 0.0, 1.0, DEFAULT_LOW_WATERMARK,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396 * GstURISourceBin::high-watermark
398 * Proportion of the queue size (either in bytes or time) to complete
399 * buffering. Only used if use-buffering is TRUE.
401 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
402 g_param_spec_double ("high-watermark", "High watermark",
403 "High threshold for buffering to finish. Only used if use-buffering is True",
404 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 * GstURISourceBin::statistics
410 * A GStructure containing the following values based on the values from
411 * all the queue's contained in this urisourcebin.
413 * "minimum-byte-level" G_TYPE_UINT Minimum of the current byte levels
414 * "maximum-byte-level" G_TYPE_UINT Maximum of the current byte levels
415 * "average-byte-level" G_TYPE_UINT Average of the current byte levels
416 * "minimum-time-level" G_TYPE_UINT64 Minimum of the current time levels
417 * "maximum-time-level" G_TYPE_UINT64 Maximum of the current time levels
418 * "average-time-level" G_TYPE_UINT64 Average of the current time levels
420 g_object_class_install_property (gobject_class, PROP_STATISTICS,
421 g_param_spec_boxed ("statistics", "Queue Statistics",
422 "A set of statistics over all the queue-like elements contained in "
423 "this element", GST_TYPE_STRUCTURE,
424 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
427 * GstURISourceBin::drained:
429 * This signal is emitted when the data for the current uri is played.
431 gst_uri_source_bin_signals[SIGNAL_DRAINED] =
432 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
434 G_STRUCT_OFFSET (GstURISourceBinClass, drained), NULL, NULL, NULL,
435 G_TYPE_NONE, 0, G_TYPE_NONE);
438 * GstURISourceBin::about-to-finish:
440 * This signal is emitted when the data for the current uri is played.
442 gst_uri_source_bin_signals[SIGNAL_ABOUT_TO_FINISH] =
443 g_signal_new ("about-to-finish", G_TYPE_FROM_CLASS (klass),
445 G_STRUCT_OFFSET (GstURISourceBinClass, about_to_finish), NULL, NULL, NULL,
446 G_TYPE_NONE, 0, G_TYPE_NONE);
449 * GstURISourceBin::source-setup:
450 * @bin: the urisourcebin.
451 * @source: source element
453 * This signal is emitted after the source element has been created, so
454 * it can be configured by setting additional properties (e.g. set a
455 * proxy server for an http source, or set the device and read speed for
456 * an audio cd source). This is functionally equivalent to connecting to
457 * the notify::source signal, but more convenient.
461 gst_uri_source_bin_signals[SIGNAL_SOURCE_SETUP] =
462 g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
463 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
465 gst_element_class_add_pad_template (gstelement_class,
466 gst_static_pad_template_get (&srctemplate));
467 gst_element_class_set_static_metadata (gstelement_class,
468 "URI reader", "Generic/Bin/Source",
469 "Download and buffer a URI as needed",
470 "Jan Schmidt <jan@centricular.com>");
472 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_source_bin_query);
473 gstelement_class->change_state =
474 GST_DEBUG_FUNCPTR (gst_uri_source_bin_change_state);
476 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);
480 gst_uri_source_bin_init (GstURISourceBin * urisrc)
482 g_mutex_init (&urisrc->lock);
484 g_mutex_init (&urisrc->buffering_lock);
485 g_mutex_init (&urisrc->buffering_post_lock);
487 g_cond_init (&urisrc->activation_cond);
489 urisrc->uri = g_strdup (DEFAULT_PROP_URI);
490 urisrc->connection_speed = DEFAULT_CONNECTION_SPEED;
492 urisrc->buffer_duration = DEFAULT_BUFFER_DURATION;
493 urisrc->buffer_size = DEFAULT_BUFFER_SIZE;
494 urisrc->download = DEFAULT_DOWNLOAD;
495 urisrc->use_buffering = DEFAULT_USE_BUFFERING;
496 urisrc->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
497 urisrc->last_buffering_pct = -1;
498 urisrc->low_watermark = DEFAULT_LOW_WATERMARK;
499 urisrc->high_watermark = DEFAULT_HIGH_WATERMARK;
501 urisrc->demuxer_handles_buffering = FALSE;
503 GST_OBJECT_FLAG_SET (urisrc,
504 GST_ELEMENT_FLAG_SOURCE | GST_BIN_FLAG_STREAMS_AWARE);
505 gst_bin_set_suppressed_flags (GST_BIN (urisrc),
506 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
510 gst_uri_source_bin_finalize (GObject * obj)
512 GstURISourceBin *urisrc = GST_URI_SOURCE_BIN (obj);
514 remove_demuxer (urisrc);
515 g_mutex_clear (&urisrc->lock);
516 g_mutex_clear (&urisrc->buffering_lock);
517 g_mutex_clear (&urisrc->buffering_post_lock);
518 g_free (urisrc->uri);
520 G_OBJECT_CLASS (parent_class)->finalize (obj);
524 gst_uri_source_bin_set_property (GObject * object, guint prop_id,
525 const GValue * value, GParamSpec * pspec)
527 GstURISourceBin *urisrc = GST_URI_SOURCE_BIN (object);
531 GST_OBJECT_LOCK (urisrc);
532 g_free (urisrc->uri);
533 urisrc->uri = g_value_dup_string (value);
534 GST_OBJECT_UNLOCK (urisrc);
536 case PROP_CONNECTION_SPEED:
537 GST_OBJECT_LOCK (urisrc);
538 urisrc->connection_speed = g_value_get_uint64 (value) * 1000;
539 GST_OBJECT_UNLOCK (urisrc);
541 case PROP_BUFFER_SIZE:
542 urisrc->buffer_size = g_value_get_int (value);
543 update_queue_values (urisrc);
545 case PROP_BUFFER_DURATION:
546 urisrc->buffer_duration = g_value_get_int64 (value);
547 update_queue_values (urisrc);
550 urisrc->download = g_value_get_boolean (value);
552 case PROP_USE_BUFFERING:
553 urisrc->use_buffering = g_value_get_boolean (value);
555 case PROP_RING_BUFFER_MAX_SIZE:
556 urisrc->ring_buffer_max_size = g_value_get_uint64 (value);
558 case PROP_LOW_WATERMARK:
559 urisrc->low_watermark = g_value_get_double (value);
560 update_queue_values (urisrc);
562 case PROP_HIGH_WATERMARK:
563 urisrc->high_watermark = g_value_get_double (value);
564 update_queue_values (urisrc);
567 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
573 gst_uri_source_bin_get_property (GObject * object, guint prop_id,
574 GValue * value, GParamSpec * pspec)
576 GstURISourceBin *urisrc = GST_URI_SOURCE_BIN (object);
580 GST_OBJECT_LOCK (urisrc);
581 g_value_set_string (value, urisrc->uri);
582 GST_OBJECT_UNLOCK (urisrc);
585 GST_OBJECT_LOCK (urisrc);
586 g_value_set_object (value, urisrc->source);
587 GST_OBJECT_UNLOCK (urisrc);
589 case PROP_CONNECTION_SPEED:
590 GST_OBJECT_LOCK (urisrc);
591 g_value_set_uint64 (value, urisrc->connection_speed / 1000);
592 GST_OBJECT_UNLOCK (urisrc);
594 case PROP_BUFFER_SIZE:
595 GST_OBJECT_LOCK (urisrc);
596 g_value_set_int (value, urisrc->buffer_size);
597 GST_OBJECT_UNLOCK (urisrc);
599 case PROP_BUFFER_DURATION:
600 GST_OBJECT_LOCK (urisrc);
601 g_value_set_int64 (value, urisrc->buffer_duration);
602 GST_OBJECT_UNLOCK (urisrc);
605 g_value_set_boolean (value, urisrc->download);
607 case PROP_USE_BUFFERING:
608 g_value_set_boolean (value, urisrc->use_buffering);
610 case PROP_RING_BUFFER_MAX_SIZE:
611 g_value_set_uint64 (value, urisrc->ring_buffer_max_size);
613 case PROP_LOW_WATERMARK:
614 g_value_set_double (value, urisrc->low_watermark);
616 case PROP_HIGH_WATERMARK:
617 g_value_set_double (value, urisrc->high_watermark);
619 case PROP_STATISTICS:
620 g_value_take_boxed (value, get_queue_statistics (urisrc));
623 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
629 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
631 GstPad *gpad = GST_PAD_CAST (user_data);
633 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
634 gst_pad_store_sticky_event (gpad, *event);
639 static GstPadProbeReturn
640 demux_pad_events (GstPad * pad, GstPadProbeInfo * info, OutputSlotInfo * slot);
642 /* CALL WITH URISOURCEBIN LOCK */
644 free_child_src_pad_info (ChildSrcPadInfo * info, GstURISourceBin * urisrc)
646 g_assert (info->src_pad);
648 GST_DEBUG_OBJECT (urisrc,
649 "Freeing ChildSrcPadInfo for %" GST_PTR_FORMAT, info->src_pad);
650 if (info->typefind) {
651 gst_element_set_state (info->typefind, GST_STATE_NULL);
652 gst_bin_remove (GST_BIN_CAST (urisrc), info->typefind);
655 gst_object_unref (info->src_pad);
657 g_list_foreach (info->outputs, (GFunc) free_output_slot, urisrc);
658 g_list_free (info->outputs);
663 static ChildSrcPadInfo *
664 get_cspi_for_pad (GstURISourceBin * urisrc, GstPad * pad)
668 for (iter = urisrc->src_infos; iter; iter = iter->next) {
669 ChildSrcPadInfo *info = iter->data;
670 if (info->src_pad == pad)
676 static ChildSrcPadInfo *
677 new_child_src_pad_info (GstURISourceBin * urisrc, GstPad * pad)
679 ChildSrcPadInfo *info;
681 GST_LOG_OBJECT (urisrc, "New ChildSrcPadInfo for %" GST_PTR_FORMAT, pad);
683 info = g_new0 (ChildSrcPadInfo, 1);
684 info->urisrc = urisrc;
685 info->src_pad = gst_object_ref (pad);
687 urisrc->src_infos = g_list_append (urisrc->src_infos, info);
692 /* Called by the signal handlers when a demuxer has produced a new stream */
694 new_demuxer_pad_added_cb (GstElement * element, GstPad * pad,
695 ChildSrcPadInfo * info)
697 GstURISourceBin *urisrc = info->urisrc;
698 OutputSlotInfo *slot;
701 GST_URI_SOURCE_BIN_LOCK (urisrc);
702 /* If the demuxer handles buffering and is streams-aware, we can expose it
703 as-is directly. We still add an event probe to deal with EOS */
705 new_output_slot (info, FALSE, FALSE, urisrc->demuxer_handles_buffering,
707 output_pad = gst_object_ref (slot->output_pad);
709 GST_DEBUG_OBJECT (element,
710 "New streams-aware demuxer pad %s:%s , exposing directly",
711 GST_DEBUG_PAD_NAME (pad));
712 slot->demuxer_event_probe_id =
713 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
714 GST_PAD_PROBE_TYPE_EVENT_FLUSH, (GstPadProbeCallback) demux_pad_events,
717 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
718 expose_output_pad (urisrc, output_pad);
719 gst_object_unref (output_pad);
722 /* Called with lock held */
724 all_slots_are_eos (GstURISourceBin * urisrc)
728 for (tmp = urisrc->src_infos; tmp; tmp = tmp->next) {
729 ChildSrcPadInfo *cspi = tmp->data;
731 for (iter2 = cspi->outputs; iter2; iter2 = iter2->next) {
732 OutputSlotInfo *slot = (OutputSlotInfo *) iter2->data;
733 if (slot->is_eos == FALSE)
740 /* CALL WITH URISOURCEBIN LOCK */
741 static OutputSlotInfo *
742 output_slot_for_originating_pad (ChildSrcPadInfo * info,
743 GstPad * originating_pad)
746 for (iter = info->outputs; iter; iter = iter->next) {
747 OutputSlotInfo *slot = iter->data;
748 if (slot->originating_pad == originating_pad)
755 static GstPadProbeReturn
756 demux_pad_events (GstPad * pad, GstPadProbeInfo * info, OutputSlotInfo * slot)
758 GstURISourceBin *urisrc = slot->linked_info->urisrc;
759 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
760 GstEvent *ev = GST_PAD_PROBE_INFO_EVENT (info);
762 GST_URI_SOURCE_BIN_LOCK (urisrc);
764 switch (GST_EVENT_TYPE (ev)) {
767 gboolean all_streams_eos;
769 GST_LOG_OBJECT (urisrc, "EOS on pad %" GST_PTR_FORMAT, pad);
771 BUFFERING_LOCK (urisrc);
772 /* Mark that we fed an EOS to this slot */
774 all_streams_eos = all_slots_are_eos (urisrc);
775 BUFFERING_UNLOCK (urisrc);
778 /* EOS means this element is no longer buffering */
779 remove_buffering_msgs (urisrc, GST_OBJECT_CAST (slot->queue));
781 if (all_streams_eos) {
782 GST_DEBUG_OBJECT (urisrc, "POSTING ABOUT TO FINISH");
783 g_signal_emit (urisrc,
784 gst_uri_source_bin_signals[SIGNAL_ABOUT_TO_FINISH], 0, NULL);
788 case GST_EVENT_STREAM_START:
789 case GST_EVENT_FLUSH_STOP:
790 BUFFERING_LOCK (urisrc);
791 slot->is_eos = FALSE;
792 BUFFERING_UNLOCK (urisrc);
798 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
803 static GstPadProbeReturn
804 pre_queue_event_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
806 GstURISourceBin *urisrc = GST_URI_SOURCE_BIN (user_data);
807 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
808 GstEvent *ev = GST_PAD_PROBE_INFO_EVENT (info);
810 switch (GST_EVENT_TYPE (ev)) {
813 GST_LOG_OBJECT (urisrc, "EOS on pad %" GST_PTR_FORMAT, pad);
814 GST_DEBUG_OBJECT (urisrc, "POSTING ABOUT TO FINISH");
815 g_signal_emit (urisrc,
816 gst_uri_source_bin_signals[SIGNAL_ABOUT_TO_FINISH], 0, NULL);
825 static GstStructure *
826 get_queue_statistics (GstURISourceBin * urisrc)
828 GstStructure *ret = NULL;
829 guint min_byte_level = 0, max_byte_level = 0;
830 guint64 min_time_level = 0, max_time_level = 0;
831 gdouble avg_byte_level = 0., avg_time_level = 0.;
835 GST_URI_SOURCE_BIN_LOCK (urisrc);
837 for (iter = urisrc->src_infos; iter; iter = iter->next) {
838 ChildSrcPadInfo *info = iter->data;
839 for (cur = info->outputs; cur; cur = cur->next) {
840 OutputSlotInfo *slot = (OutputSlotInfo *) (cur->data);
841 guint byte_limit = 0;
842 guint64 time_limit = 0;
847 g_object_get (slot->queue, "current-level-bytes", &byte_limit,
848 "current-level-time", &time_limit, NULL);
850 if (byte_limit < min_byte_level)
851 min_byte_level = byte_limit;
852 if (byte_limit > max_byte_level)
853 max_byte_level = byte_limit;
854 avg_byte_level = (avg_byte_level * i + byte_limit) / (gdouble) (i + 1);
856 if (time_limit < min_time_level)
857 min_time_level = time_limit;
858 if (time_limit > max_time_level)
859 max_time_level = time_limit;
860 avg_time_level = (avg_time_level * i + time_limit) / (gdouble) (i + 1);
865 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
867 ret = gst_structure_new ("application/x-urisourcebin-stats",
868 "minimum-byte-level", G_TYPE_UINT, (guint) min_byte_level,
869 "maximum-byte-level", G_TYPE_UINT, (guint) max_byte_level,
870 "average-byte-level", G_TYPE_UINT, (guint) avg_byte_level,
871 "minimum-time-level", G_TYPE_UINT64, (guint64) min_time_level,
872 "maximum-time-level", G_TYPE_UINT64, (guint64) max_time_level,
873 "average-time-level", G_TYPE_UINT64, (guint64) avg_time_level, NULL);
879 update_queue_values (GstURISourceBin * urisrc)
883 gdouble low_watermark, high_watermark;
884 guint64 cumulative_bitrate = 0;
887 GST_URI_SOURCE_BIN_LOCK (urisrc);
888 duration = GET_BUFFER_DURATION (urisrc);
889 buffer_size = GET_BUFFER_SIZE (urisrc);
890 low_watermark = urisrc->low_watermark;
891 high_watermark = urisrc->high_watermark;
893 for (iter = urisrc->src_infos; iter; iter = iter->next) {
894 ChildSrcPadInfo *info = iter->data;
895 for (cur = info->outputs; cur; cur = cur->next) {
896 OutputSlotInfo *slot = (OutputSlotInfo *) (cur->data);
902 if (g_object_class_find_property (G_OBJECT_GET_CLASS (slot->queue),
904 g_object_get (G_OBJECT (slot->queue), "bitrate", &bitrate, NULL);
908 cumulative_bitrate += bitrate;
910 GST_TRACE_OBJECT (urisrc,
911 "Unknown bitrate detected from %" GST_PTR_FORMAT
912 ", resetting all bitrates", slot->queue);
913 cumulative_bitrate = 0;
919 GST_DEBUG_OBJECT (urisrc, "recalculating queue limits with cumulative "
920 "bitrate %" G_GUINT64_FORMAT ", buffer size %u, buffer duration %"
921 G_GINT64_FORMAT, cumulative_bitrate, buffer_size, duration);
923 for (iter = urisrc->src_infos; iter; iter = iter->next) {
924 ChildSrcPadInfo *info = iter->data;
925 for (cur = info->outputs; cur; cur = cur->next) {
926 OutputSlotInfo *slot = (OutputSlotInfo *) (cur->data);
932 if (cumulative_bitrate > 0
933 && g_object_class_find_property (G_OBJECT_GET_CLASS (slot->queue),
936 g_object_get (G_OBJECT (slot->queue), "bitrate", &bitrate, NULL);
938 gst_util_uint64_scale (buffer_size, bitrate, cumulative_bitrate);
940 /* if not all queue's have valid bitrates, use the buffer-size as the
942 byte_limit = buffer_size;
945 GST_DEBUG_OBJECT (urisrc,
946 "calculated new limits for queue-like element %" GST_PTR_FORMAT
947 ", bytes:%u, time:%" G_GUINT64_FORMAT
948 ", low-watermark:%f, high-watermark:%f",
949 slot->queue, byte_limit, (guint64) duration, low_watermark,
951 g_object_set (G_OBJECT (slot->queue), "max-size-bytes", byte_limit,
952 "max-size-time", (guint64) duration, "low-watermark", low_watermark,
953 "high-watermark", high_watermark, NULL);
956 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
960 on_queue_bitrate_changed (GstElement * queue, GParamSpec * pspec,
963 GstURISourceBin *urisrc = GST_URI_SOURCE_BIN (user_data);
965 gst_element_call_async (GST_ELEMENT (urisrc),
966 (GstElementCallAsyncFunc) update_queue_values, NULL, NULL);
969 /* Called with lock held */
970 static OutputSlotInfo *
971 new_output_slot (ChildSrcPadInfo * info, gboolean do_download,
972 gboolean is_adaptive, gboolean no_buffering, GstPad * originating_pad)
974 GstURISourceBin *urisrc = info->urisrc;
975 OutputSlotInfo *slot;
977 GstElement *queue = NULL;
978 const gchar *elem_name;
980 GST_DEBUG_OBJECT (urisrc,
981 "do_download:%d is_adaptive:%d, no_buffering:%d, originating_pad:%"
982 GST_PTR_FORMAT, do_download, is_adaptive, no_buffering, originating_pad);
984 slot = g_new0 (OutputSlotInfo, 1);
985 slot->linked_info = info;
987 /* If buffering is required, create the element */
990 elem_name = "downloadbuffer";
992 elem_name = "queue2";
994 queue = gst_element_factory_make (elem_name, NULL);
996 goto no_buffer_element;
1000 slot->bitrate_changed_id =
1001 g_signal_connect (G_OBJECT (queue), "notify::bitrate",
1002 (GCallback) on_queue_bitrate_changed, urisrc);
1005 gchar *temp_template, *filename;
1006 const gchar *tmp_dir, *prgname;
1008 tmp_dir = g_get_user_cache_dir ();
1009 prgname = g_get_prgname ();
1010 if (prgname == NULL)
1011 prgname = "GStreamer";
1013 filename = g_strdup_printf ("%s-XXXXXX", prgname);
1015 /* build our filename */
1016 temp_template = g_build_filename (tmp_dir, filename, NULL);
1018 GST_DEBUG_OBJECT (urisrc, "enable download buffering in %s (%s, %s, %s)",
1019 temp_template, tmp_dir, prgname, filename);
1021 /* configure progressive download for selected media types */
1022 g_object_set (queue, "temp-template", temp_template, NULL);
1025 g_free (temp_template);
1028 GST_LOG_OBJECT (urisrc, "Adding queue2 for adaptive streaming stream");
1029 g_object_set (queue, "use-buffering", urisrc->use_buffering,
1030 "use-tags-bitrate", TRUE, "use-rate-estimate", FALSE, NULL);
1032 GST_LOG_OBJECT (urisrc, "Adding queue for buffering");
1033 g_object_set (queue, "use-buffering", urisrc->use_buffering, NULL);
1036 g_object_set (queue, "ring-buffer-max-size",
1037 urisrc->ring_buffer_max_size, NULL);
1038 /* Disable max-size-buffers - queue based on data rate to the default time limit */
1039 g_object_set (queue, "max-size-buffers", 0, NULL);
1041 /* Don't start buffering until the queue is empty (< 1%).
1042 * Start playback when the queue is 60% full, leaving a bit more room
1043 * for upstream to push more without getting bursty */
1044 g_object_set (queue, "low-percent", 1, "high-percent", 60, NULL);
1046 g_object_set (queue, "low-watermark", urisrc->low_watermark,
1047 "high-watermark", urisrc->high_watermark, NULL);
1050 /* set the necessary limits on the queue-like elements */
1051 g_object_set (queue, "max-size-bytes", GET_BUFFER_SIZE (urisrc),
1052 "max-size-time", (guint64) GET_BUFFER_DURATION (urisrc), NULL);
1054 gst_bin_add (GST_BIN_CAST (urisrc), queue);
1055 gst_element_sync_state_with_parent (queue);
1057 slot->queue_sinkpad = gst_element_get_static_pad (queue, "sink");
1059 /* get the new raw srcpad */
1060 srcpad = gst_element_get_static_pad (queue, "src");
1062 slot->output_pad = create_output_pad (slot, srcpad);
1064 gst_object_unref (srcpad);
1066 gst_pad_link (originating_pad, slot->queue_sinkpad);
1068 /* Expose pad directly */
1069 slot->output_pad = create_output_pad (slot, originating_pad);
1071 slot->originating_pad = gst_object_ref (originating_pad);
1073 /* save output slot so we can remove it later */
1074 info->outputs = g_list_append (info->outputs, slot);
1076 GST_DEBUG_OBJECT (urisrc, "New slot for output_pad %" GST_PTR_FORMAT,
1084 post_missing_plugin_error (GST_ELEMENT_CAST (urisrc), elem_name);
1089 static GstPadProbeReturn
1090 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
1093 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1094 OutputSlotInfo *slot = user_data;
1095 GstURISourceBin *urisrc = slot->linked_info->urisrc;
1097 GST_LOG_OBJECT (pad, "%" GST_PTR_FORMAT, event);
1099 /* A custom EOS will be received if an adaptive demuxer source pad removed a
1100 * pad and buffering was present on that slot */
1101 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS &&
1102 gst_mini_object_get_qdata (GST_MINI_OBJECT_CAST (event),
1103 CUSTOM_EOS_QUARK)) {
1104 GstPadProbeReturn probe_ret = GST_PAD_PROBE_DROP;
1106 GST_DEBUG_OBJECT (pad, "we received custom EOS");
1108 /* remove custom-eos */
1109 gst_mini_object_set_qdata (GST_MINI_OBJECT_CAST (event), CUSTOM_EOS_QUARK,
1112 GST_URI_SOURCE_BIN_LOCK (urisrc);
1115 /* linked_info is old input which is still linked without removal */
1116 GST_DEBUG_OBJECT (pad, "push actual EOS");
1117 gst_pad_push_event (slot->output_pad, event);
1118 probe_ret = GST_PAD_PROBE_HANDLED;
1121 /* And finally remove the output. This is done asynchronously since we can't
1122 * do it from the streaming thread */
1123 free_output_slot_async (urisrc, slot);
1125 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1128 /* never drop events */
1129 return GST_PAD_PROBE_OK;
1132 /* called when we found a raw pad to expose. We set up a
1133 * padprobe to detect EOS before exposing the pad.
1134 * Called with LOCK held. */
1136 create_output_pad (OutputSlotInfo * slot, GstPad * pad)
1138 GstURISourceBin *urisrc = slot->linked_info->urisrc;
1140 GstPadTemplate *pad_tmpl;
1143 /* If the output slot does buffering, add a probe to detect drainage */
1145 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1146 source_pad_event_probe, slot, NULL);
1148 pad_tmpl = gst_static_pad_template_get (&srctemplate);
1150 padname = g_strdup_printf ("src_%u", urisrc->numpads);
1153 newpad = gst_ghost_pad_new_from_template (padname, pad, pad_tmpl);
1154 gst_object_unref (pad_tmpl);
1157 GST_DEBUG_OBJECT (urisrc, "Created output pad %s:%s for pad %s:%s",
1158 GST_DEBUG_PAD_NAME (newpad), GST_DEBUG_PAD_NAME (pad));
1163 static GstPadProbeReturn
1164 expose_block_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
1166 GstURISourceBin *urisrc = (GstURISourceBin *) user_data;
1167 gboolean expose = FALSE;
1169 GST_DEBUG_OBJECT (pad, "blocking");
1171 GST_URI_SOURCE_BIN_LOCK (urisrc);
1172 while (!urisrc->activated && !urisrc->flushing) {
1173 GST_DEBUG_OBJECT (urisrc, "activated:%d flushing:%d", urisrc->activated,
1175 g_cond_wait (&urisrc->activation_cond, &urisrc->lock);
1177 GST_DEBUG_OBJECT (urisrc, "activated:%d flushing:%d", urisrc->activated,
1180 if (!urisrc->flushing)
1182 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1184 gst_element_add_pad (GST_ELEMENT_CAST (urisrc), pad);
1185 GST_DEBUG_OBJECT (pad, "Done blocking, removing probe");
1186 return GST_PAD_PROBE_REMOVE;
1190 expose_output_pad (GstURISourceBin * urisrc, GstPad * pad)
1194 if (gst_object_has_as_parent (GST_OBJECT (pad), GST_OBJECT (urisrc)))
1195 return; /* Pad is already exposed */
1197 target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1199 gst_pad_sticky_events_foreach (target, copy_sticky_events, pad);
1200 gst_object_unref (target);
1202 gst_pad_set_active (pad, TRUE);
1203 GST_URI_SOURCE_BIN_LOCK (urisrc);
1204 if (!urisrc->activated) {
1205 GST_DEBUG_OBJECT (urisrc, "Not fully activated, adding pad once PAUSED !");
1206 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1207 expose_block_probe, urisrc, NULL);
1210 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1213 GST_DEBUG_OBJECT (urisrc, "Exposing pad %" GST_PTR_FORMAT, pad);
1214 gst_element_add_pad (GST_ELEMENT_CAST (urisrc), pad);
1219 demuxer_pad_removed_cb (GstElement * element, GstPad * pad,
1220 ChildSrcPadInfo * info)
1222 GstURISourceBin *urisrc;
1223 OutputSlotInfo *slot;
1225 /* we only care about srcpads */
1226 if (!GST_PAD_IS_SRC (pad))
1229 urisrc = info->urisrc;
1231 GST_DEBUG_OBJECT (urisrc, "pad removed name: <%s:%s>",
1232 GST_DEBUG_PAD_NAME (pad));
1234 GST_URI_SOURCE_BIN_LOCK (urisrc);
1235 slot = output_slot_for_originating_pad (info, pad);
1238 gst_pad_remove_probe (pad, slot->demuxer_event_probe_id);
1239 slot->demuxer_event_probe_id = 0;
1244 /* Propagate custom EOS to buffering elements. The slot will be removed when
1245 * it is received on the output of the buffering elements */
1247 BUFFERING_LOCK (urisrc);
1248 /* Unlink this pad from its output slot and send a fake EOS event
1249 * to drain the queue */
1250 was_eos = slot->is_eos;
1251 slot->is_eos = TRUE;
1252 BUFFERING_UNLOCK (urisrc);
1254 remove_buffering_msgs (urisrc, GST_OBJECT_CAST (slot->queue));
1258 event = gst_event_new_eos ();
1259 s = gst_event_writable_structure (event);
1260 gst_structure_set (s, "urisourcebin-custom-eos", G_TYPE_BOOLEAN, TRUE,
1262 gst_pad_send_event (slot->queue_sinkpad, event);
1265 GST_LOG_OBJECT (urisrc,
1266 "No buffering involved, removing output slot immediately");
1267 /* Remove output slot immediately */
1268 info->outputs = g_list_remove (info->outputs, slot);
1269 free_output_slot (slot, urisrc);
1271 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1276 /* helper function to lookup stuff in lists */
1278 array_has_value (const gchar * values[], const gchar * value)
1282 for (i = 0; values[i]; i++) {
1283 if (g_str_has_prefix (value, values[i]))
1290 array_has_uri_value (const gchar * values[], const gchar * value)
1294 for (i = 0; values[i]; i++) {
1295 if (!g_ascii_strncasecmp (value, values[i], strlen (values[i])))
1301 /* list of URIs that we consider to be streams and that need buffering.
1302 * We have no mechanism yet to figure this out with a query. */
1303 static const gchar *stream_uris[] = { "http://", "https://", "mms://",
1304 "mmsh://", "mmsu://", "mmst://", "fd://", "myth://", "ssh://",
1305 "ftp://", "sftp://",
1309 /* list of URIs that need a queue because they are pretty bursty */
1310 static const gchar *queue_uris[] = { "cdda://", NULL };
1312 /* blacklisted URIs, we know they will always fail. */
1313 static const gchar *blacklisted_uris[] = { NULL };
1315 /* media types that use adaptive streaming */
1316 static const gchar *adaptive_media[] = {
1317 "application/x-hls", "application/vnd.ms-sstr+xml",
1318 "application/dash+xml", NULL
1321 #define IS_STREAM_URI(uri) (array_has_uri_value (stream_uris, uri))
1322 #define IS_QUEUE_URI(uri) (array_has_uri_value (queue_uris, uri))
1323 #define IS_BLACKLISTED_URI(uri) (array_has_uri_value (blacklisted_uris, uri))
1324 #define IS_ADAPTIVE_MEDIA(media) (array_has_value (adaptive_media, media))
1327 * Generate and configure a source element.
1330 gen_source_element (GstURISourceBin * urisrc)
1332 GObjectClass *source_class;
1336 GstSchedulingFlags flags;
1342 GST_LOG_OBJECT (urisrc, "finding source for %s", urisrc->uri);
1344 if (!gst_uri_is_valid (urisrc->uri))
1347 if (IS_BLACKLISTED_URI (urisrc->uri))
1348 goto uri_blacklisted;
1350 source = gst_element_make_from_uri (GST_URI_SRC, urisrc->uri, NULL, &err);
1354 GST_LOG_OBJECT (urisrc, "found source type %s", G_OBJECT_TYPE_NAME (source));
1356 urisrc->is_stream = IS_STREAM_URI (urisrc->uri);
1358 query = gst_query_new_scheduling ();
1359 if (gst_element_query (source, query)) {
1360 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
1361 if ((flags & GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED))
1362 urisrc->is_stream = TRUE;
1364 gst_query_unref (query);
1366 source_class = G_OBJECT_GET_CLASS (source);
1368 if (urisrc->is_stream) {
1369 /* Live sources are not streamable */
1370 pspec = g_object_class_find_property (source_class, "is-live");
1371 if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_BOOLEAN) {
1373 g_object_get (G_OBJECT (source), "is-live", &is_live, NULL);
1375 urisrc->is_stream = FALSE;
1379 GST_LOG_OBJECT (urisrc, "source is stream: %d", urisrc->is_stream);
1381 pspec = g_object_class_find_property (source_class, "connection-speed");
1382 if (pspec != NULL) {
1383 guint64 speed = urisrc->connection_speed / 1000;
1384 gboolean wrong_type = FALSE;
1386 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
1387 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
1389 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1390 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
1391 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
1393 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1394 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
1395 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
1397 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1398 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
1399 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
1401 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1403 GST_WARNING_OBJECT (urisrc,
1404 "The connection speed property %" G_GUINT64_FORMAT
1405 " of type %s is not useful. Not setting it", speed,
1406 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
1411 g_object_set (source, "connection-speed", speed, NULL);
1413 GST_DEBUG_OBJECT (urisrc,
1414 "setting connection-speed=%" G_GUINT64_FORMAT " to source element",
1424 GST_ELEMENT_ERROR (urisrc, RESOURCE, NOT_FOUND,
1425 (_("No URI specified to play from.")), (NULL));
1430 GST_ELEMENT_ERROR (urisrc, RESOURCE, NOT_FOUND,
1431 (_("Invalid URI \"%s\"."), urisrc->uri), (NULL));
1432 g_clear_error (&err);
1437 GST_ELEMENT_ERROR (urisrc, RESOURCE, FAILED,
1438 (_("This stream type cannot be played yet.")), (NULL));
1443 /* whoops, could not create the source element, dig a little deeper to
1444 * figure out what might be wrong. */
1445 if (err != NULL && err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL) {
1448 prot = gst_uri_get_protocol (urisrc->uri);
1452 gst_element_post_message (GST_ELEMENT_CAST (urisrc),
1453 gst_missing_uri_source_message_new (GST_ELEMENT (urisrc), prot));
1455 GST_ELEMENT_ERROR (urisrc, CORE, MISSING_PLUGIN,
1456 (_("No URI handler implemented for \"%s\"."), prot), (NULL));
1460 GST_ELEMENT_ERROR (urisrc, RESOURCE, NOT_FOUND,
1461 ("%s", (err) ? err->message : "URI was not accepted by any element"),
1462 ("No element accepted URI '%s'", urisrc->uri));
1465 g_clear_error (&err);
1471 is_all_raw_caps (GstCaps * caps, GstCaps * rawcaps, gboolean * all_raw)
1473 GstCaps *intersection;
1475 gboolean res = FALSE;
1480 capssize = gst_caps_get_size (caps);
1481 /* no caps, skip and move to the next pad */
1482 if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1485 intersection = gst_caps_intersect (caps, rawcaps);
1486 *all_raw = !gst_caps_is_empty (intersection)
1487 && (gst_caps_get_size (intersection) == capssize);
1488 gst_caps_unref (intersection);
1497 post_missing_plugin_error (GstElement * urisrc, const gchar * element_name)
1501 msg = gst_missing_element_message_new (urisrc, element_name);
1502 gst_element_post_message (urisrc, msg);
1504 GST_ELEMENT_ERROR (urisrc, CORE, MISSING_PLUGIN,
1505 (_("Missing element '%s' - check your GStreamer installation."),
1506 element_name), (NULL));
1511 GstURISourceBin *urisrc;
1517 analyse_pad_foreach (const GValue * item, AnalyseData * data)
1519 GstURISourceBin *urisrc = data->urisrc;
1520 GstPad *pad = g_value_dup_object (item);
1521 ChildSrcPadInfo *info;
1522 GstCaps *padcaps = NULL;
1523 gboolean pad_is_raw;
1524 gboolean res = TRUE;
1526 GST_LOG_OBJECT (urisrc, "pad %" GST_PTR_FORMAT, pad);
1528 data->have_out = TRUE;
1530 /* The info might already exist if there was an iterator resync */
1531 if (get_cspi_for_pad (urisrc, pad)) {
1532 GST_LOG_OBJECT (urisrc, "Already analysed");
1536 info = new_child_src_pad_info (urisrc, pad);
1537 padcaps = gst_pad_query_caps (pad, NULL);
1539 if (!is_all_raw_caps (padcaps, DEFAULT_CAPS, &pad_is_raw) || !pad_is_raw) {
1540 /* if FALSE, this pad has no caps, we setup typefinding on it */
1541 if (!setup_typefind (info)) {
1545 } else if (pad_is_raw) {
1546 /* caps on source pad are all raw, we can add the pad */
1548 OutputSlotInfo *slot;
1550 GST_URI_SOURCE_BIN_LOCK (urisrc);
1551 /* Only use buffering on raw pads in very specific conditions */
1552 GST_DEBUG_OBJECT (urisrc, "use_buffering:%d is_queue:%d",
1553 urisrc->use_buffering, IS_QUEUE_URI (urisrc->uri));
1554 slot = new_output_slot (info, FALSE, FALSE, !urisrc->use_buffering
1555 || !IS_QUEUE_URI (urisrc->uri), pad);
1559 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1563 /* get the new raw srcpad */
1564 output_pad = gst_object_ref (slot->output_pad);
1566 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1568 expose_output_pad (urisrc, output_pad);
1569 gst_object_unref (output_pad);
1571 GST_DEBUG_OBJECT (urisrc, "Handling non-raw pad");
1572 /* The caps are non-raw, we handle it directly */
1573 handle_new_pad (info, pad, padcaps);
1578 gst_caps_unref (padcaps);
1579 gst_object_unref (pad);
1584 * analyse_source_and_expose_raw_pads:
1585 * @urisrc: a #GstURISourceBin
1586 * @all_pads_raw: are all pads raw data
1587 * @have_out: does the source have output
1588 * @is_dynamic: is this a dynamic source
1590 * Check the source of @urisrc and collect information about it.
1592 * All pads will be handled directly. Raw pads are exposed as-is. Pads without
1593 * any caps will have a typefind appended to them, and other pads will be
1596 * @is_raw will be set to TRUE if the source only produces raw pads. When this
1597 * function returns, all of the raw pad of the source will be added
1600 * @have_out: will be set to TRUE if the source has output pads.
1602 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1605 * Returns: FALSE if a fatal error occurred while scanning.
1608 analyse_source_and_expose_raw_pads (GstURISourceBin * urisrc,
1609 gboolean * have_out, gboolean * is_dynamic)
1611 GstElementClass *elemclass;
1612 AnalyseData data = { 0, };
1613 GstIteratorResult iterres;
1615 GstIterator *pads_iter;
1616 gboolean res = TRUE;
1618 pads_iter = gst_element_iterate_src_pads (urisrc->source);
1622 data.have_out = FALSE;
1623 data.urisrc = urisrc;
1625 gst_iterator_foreach (pads_iter,
1626 (GstIteratorForeachFunction) analyse_pad_foreach, &data);
1627 if (iterres == GST_ITERATOR_RESYNC)
1629 if (iterres == GST_ITERATOR_ERROR)
1633 gst_iterator_free (pads_iter);
1635 /* check for padtemplates that list SOMETIMES pads to
1636 * determine if the element is dynamic. */
1637 *is_dynamic = FALSE;
1638 elemclass = GST_ELEMENT_GET_CLASS (urisrc->source);
1639 walk = gst_element_class_get_pad_template_list (elemclass);
1640 while (walk != NULL) {
1641 GstPadTemplate *templ;
1643 templ = (GstPadTemplate *) walk->data;
1644 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1645 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES)
1649 walk = g_list_next (walk);
1652 *have_out = data.have_out;
1657 /* Remove any adaptive demuxer element */
1659 remove_demuxer (GstURISourceBin * bin)
1662 GST_DEBUG_OBJECT (bin, "removing old demuxer element");
1663 gst_element_set_state (bin->demuxer, GST_STATE_NULL);
1664 gst_bin_remove (GST_BIN_CAST (bin), bin->demuxer);
1665 bin->demuxer = NULL;
1666 bin->demuxer_handles_buffering = FALSE;
1670 /* make a demuxer and connect to all the signals */
1672 make_demuxer (GstURISourceBin * urisrc, ChildSrcPadInfo * info, GstCaps * caps)
1674 GList *factories, *eligible, *cur;
1675 GstElement *demuxer = NULL;
1678 GST_LOG_OBJECT (urisrc, "making new adaptive demuxer");
1680 /* now create the demuxer element */
1682 /* FIXME: Fire a signal to get the demuxer? */
1683 factories = gst_element_factory_list_get_elements
1684 (GST_ELEMENT_FACTORY_TYPE_DEMUXER, GST_RANK_MARGINAL);
1686 gst_element_factory_list_filter (factories, caps, GST_PAD_SINK,
1687 gst_caps_is_fixed (caps));
1688 gst_plugin_feature_list_free (factories);
1690 if (eligible == NULL)
1693 eligible = g_list_sort (eligible, gst_plugin_feature_rank_compare_func);
1695 for (cur = eligible; cur != NULL; cur = g_list_next (cur)) {
1696 GstElementFactory *factory = (GstElementFactory *) (cur->data);
1697 const gchar *klass =
1698 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1700 /* Can't be a demuxer unless it has Demux in the klass name */
1701 if (!strstr (klass, "Demux") || !strstr (klass, "Adaptive"))
1704 demuxer = gst_element_factory_create (factory, NULL);
1705 if (!GST_OBJECT_FLAG_IS_SET (demuxer, GST_BIN_FLAG_STREAMS_AWARE)) {
1706 GST_DEBUG_OBJECT (urisrc, "Ignoring non-streams-aware adaptive demuxer");
1707 gst_object_unref (demuxer);
1712 gst_plugin_feature_list_free (eligible);
1717 GST_DEBUG_OBJECT (urisrc, "Created adaptive demuxer %" GST_PTR_FORMAT,
1720 /* set up callbacks to create the links between
1721 * demuxer streams and output */
1722 g_signal_connect (demuxer,
1723 "pad-added", G_CALLBACK (new_demuxer_pad_added_cb), info);
1724 g_signal_connect (demuxer,
1725 "pad-removed", G_CALLBACK (demuxer_pad_removed_cb), info);
1727 /* Propagate connection-speed property */
1728 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (demuxer),
1729 "connection-speed");
1731 g_object_set (demuxer,
1732 "connection-speed", urisrc->connection_speed / 1000, NULL);
1739 /* FIXME: Fire the right error */
1740 GST_ELEMENT_ERROR (urisrc, CORE, MISSING_PLUGIN, (NULL),
1741 ("No demuxer element, check your installation"));
1747 * * Source element adds a new pad
1748 * * typefind has found a type
1751 handle_new_pad (ChildSrcPadInfo * info, GstPad * srcpad, GstCaps * caps)
1753 GstURISourceBin *urisrc = info->urisrc;
1756 const gchar *media_type;
1757 gboolean do_download = FALSE;
1759 GST_URI_SOURCE_BIN_LOCK (urisrc);
1761 /* if this is a pad with all raw caps, we can expose it */
1762 if (is_all_raw_caps (caps, DEFAULT_CAPS, &is_raw) && is_raw) {
1763 OutputSlotInfo *slot;
1766 GST_DEBUG_OBJECT (urisrc, "Found pad with raw caps %" GST_PTR_FORMAT
1767 ", exposing", caps);
1768 slot = new_output_slot (info, FALSE, FALSE, TRUE, srcpad);
1769 output_pad = gst_object_ref (slot->output_pad);
1770 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1772 expose_output_pad (urisrc, slot->output_pad);
1773 gst_object_unref (output_pad);
1776 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1778 s = gst_caps_get_structure (caps, 0);
1779 media_type = gst_structure_get_name (s);
1781 urisrc->is_adaptive = IS_ADAPTIVE_MEDIA (media_type);
1783 if (urisrc->is_adaptive) {
1785 GstPadLinkReturn link_res;
1788 urisrc->demuxer = make_demuxer (urisrc, info, caps);
1789 if (!urisrc->demuxer)
1791 gst_bin_add (GST_BIN_CAST (urisrc), urisrc->demuxer);
1793 /* Query the demuxer to see if it can handle buffering */
1794 query = gst_query_new_buffering (GST_FORMAT_TIME);
1795 urisrc->demuxer_handles_buffering =
1796 gst_element_query (urisrc->demuxer, query);
1797 gst_query_unref (query);
1798 GST_DEBUG_OBJECT (urisrc, "Demuxer handles buffering : %d",
1799 urisrc->demuxer_handles_buffering);
1801 sinkpad = gst_element_get_static_pad (urisrc->demuxer, "sink");
1802 if (sinkpad == NULL)
1803 goto no_demuxer_sink;
1805 link_res = gst_pad_link (srcpad, sinkpad);
1807 gst_object_unref (sinkpad);
1808 if (link_res != GST_PAD_LINK_OK)
1809 goto could_not_link;
1811 gst_element_sync_state_with_parent (urisrc->demuxer);
1812 } else if (!urisrc->is_stream) {
1813 OutputSlotInfo *slot;
1816 /* We don't need buffering here, expose immediately */
1817 GST_URI_SOURCE_BIN_LOCK (urisrc);
1818 slot = new_output_slot (info, FALSE, FALSE, TRUE, srcpad);
1819 output_pad = gst_object_ref (slot->output_pad);
1820 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1821 expose_output_pad (urisrc, output_pad);
1822 gst_object_unref (output_pad);
1824 OutputSlotInfo *slot;
1827 /* only enable download buffering if the upstream duration is known */
1828 if (urisrc->download) {
1829 GstQuery *query = gst_query_new_duration (GST_FORMAT_BYTES);
1830 if (gst_pad_query (srcpad, query)) {
1832 gst_query_parse_duration (query, NULL, &dur);
1833 do_download = (dur != -1);
1835 gst_query_unref (query);
1838 GST_DEBUG_OBJECT (urisrc, "check media-type %s, do_download:%d", media_type,
1841 GST_URI_SOURCE_BIN_LOCK (urisrc);
1842 slot = new_output_slot (info, do_download, FALSE, FALSE, srcpad);
1844 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1845 pre_queue_event_probe, urisrc, NULL);
1847 output_pad = gst_object_ref (slot->output_pad);
1848 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1850 expose_output_pad (urisrc, output_pad);
1851 gst_object_unref (output_pad);
1859 /* error was posted */
1864 GST_ELEMENT_ERROR (urisrc, CORE, NEGOTIATION,
1865 (NULL), ("Adaptive demuxer element has no 'sink' pad"));
1870 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
1871 GST_ELEMENT_ERROR (urisrc, CORE, NEGOTIATION,
1872 (NULL), ("Can't link typefind to adaptive demuxer element"));
1877 /* signaled when we have a stream and we need to configure the download
1878 * buffering or regular buffering */
1880 type_found (GstElement * typefind, guint probability,
1881 GstCaps * caps, ChildSrcPadInfo * info)
1883 GstURISourceBin *urisrc = info->urisrc;
1884 GstPad *srcpad = gst_element_get_static_pad (typefind, "src");
1886 GST_DEBUG_OBJECT (urisrc, "typefind found caps %" GST_PTR_FORMAT
1887 " on pad %" GST_PTR_FORMAT, caps, srcpad);
1888 handle_new_pad (info, srcpad, caps);
1890 gst_object_unref (GST_OBJECT (srcpad));
1893 /* setup typefind for any source. This will first plug a typefind element to the
1894 * source. After we find the type, we decide to whether to plug an adaptive
1895 * demuxer, or just link through queue2 (if needed) and expose the data */
1897 setup_typefind (ChildSrcPadInfo * info)
1899 GstURISourceBin *urisrc = info->urisrc;
1902 /* now create the typefind element */
1903 info->typefind = gst_element_factory_make ("typefind", NULL);
1904 if (!info->typefind)
1907 /* Make sure the bin doesn't set the typefind running yet */
1908 gst_element_set_locked_state (info->typefind, TRUE);
1910 gst_bin_add (GST_BIN_CAST (urisrc), info->typefind);
1912 sinkpad = gst_element_get_static_pad (info->typefind, "sink");
1913 if (gst_pad_link (info->src_pad, sinkpad) != GST_PAD_LINK_OK)
1914 goto could_not_link;
1915 gst_object_unref (sinkpad);
1917 /* connect a signal to find out when the typefind element found
1919 g_signal_connect (info->typefind, "have-type", G_CALLBACK (type_found), info);
1921 /* Now it can start */
1922 gst_element_set_locked_state (info->typefind, FALSE);
1923 gst_element_sync_state_with_parent (info->typefind);
1930 post_missing_plugin_error (GST_ELEMENT_CAST (urisrc), "typefind");
1931 GST_ELEMENT_ERROR (urisrc, CORE, MISSING_PLUGIN, (NULL),
1932 ("No typefind element, check your installation"));
1937 gst_object_unref (sinkpad);
1938 GST_ELEMENT_ERROR (urisrc, CORE, NEGOTIATION,
1939 (NULL), ("Can't link source to typefind element"));
1944 /* CALL WITH URISOURCEBIN LOCK */
1946 free_output_slot (OutputSlotInfo * slot, GstURISourceBin * urisrc)
1948 GST_DEBUG_OBJECT (urisrc,
1949 "removing output slot %" GST_PTR_FORMAT " -> %" GST_PTR_FORMAT,
1950 slot->originating_pad, slot->output_pad);
1953 if (slot->bitrate_changed_id > 0)
1954 g_signal_handler_disconnect (slot->queue, slot->bitrate_changed_id);
1955 slot->bitrate_changed_id = 0;
1957 gst_element_set_locked_state (slot->queue, TRUE);
1958 gst_element_set_state (slot->queue, GST_STATE_NULL);
1959 remove_buffering_msgs (urisrc, GST_OBJECT_CAST (slot->queue));
1960 gst_bin_remove (GST_BIN_CAST (urisrc), slot->queue);
1962 gst_object_unref (slot->queue_sinkpad);
1965 if (slot->demuxer_event_probe_id)
1966 gst_pad_remove_probe (slot->originating_pad, slot->demuxer_event_probe_id);
1968 gst_object_unref (slot->originating_pad);
1969 /* deactivate and remove the srcpad */
1970 gst_pad_set_active (slot->output_pad, FALSE);
1971 gst_element_remove_pad (GST_ELEMENT_CAST (urisrc), slot->output_pad);
1977 call_free_output_slot (GstURISourceBin * urisrc, OutputSlotInfo * slot)
1979 GST_LOG_OBJECT (urisrc, "free output slot in thread pool");
1980 free_output_slot (slot, urisrc);
1983 /* must be called with GST_URI_SOURCE_BIN_LOCK */
1985 free_output_slot_async (GstURISourceBin * urisrc, OutputSlotInfo * slot)
1987 GST_LOG_OBJECT (urisrc, "pushing output slot on thread pool to free");
1988 slot->linked_info->outputs = g_list_remove (slot->linked_info->outputs, slot);
1989 gst_element_call_async (GST_ELEMENT_CAST (urisrc),
1990 (GstElementCallAsyncFunc) call_free_output_slot, slot, NULL);
1993 /* remove source and all related elements */
1995 remove_source (GstURISourceBin * urisrc)
1997 if (urisrc->source) {
1998 GstElement *source = urisrc->source;
2000 GST_DEBUG_OBJECT (urisrc, "removing old src element");
2001 gst_element_set_state (source, GST_STATE_NULL);
2003 if (urisrc->src_np_sig_id) {
2004 g_signal_handler_disconnect (source, urisrc->src_np_sig_id);
2005 urisrc->src_np_sig_id = 0;
2007 gst_bin_remove (GST_BIN_CAST (urisrc), source);
2008 urisrc->source = NULL;
2011 GST_URI_SOURCE_BIN_LOCK (urisrc);
2012 if (urisrc->src_infos) {
2013 g_list_foreach (urisrc->src_infos, (GFunc) free_child_src_pad_info, urisrc);
2014 g_list_free (urisrc->src_infos);
2015 urisrc->src_infos = NULL;
2017 GST_URI_SOURCE_BIN_UNLOCK (urisrc);
2019 if (urisrc->demuxer)
2020 remove_demuxer (urisrc);
2023 /* is called when a dynamic source element created a new pad. */
2025 source_new_pad (GstElement * element, GstPad * pad, GstURISourceBin * urisrc)
2028 ChildSrcPadInfo *info = new_child_src_pad_info (urisrc, pad);
2030 GST_DEBUG_OBJECT (urisrc, "Found new pad %s.%s in source element %s",
2031 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
2033 caps = gst_pad_get_current_caps (pad);
2034 GST_DEBUG_OBJECT (urisrc, "caps %" GST_PTR_FORMAT, caps);
2036 setup_typefind (info);
2038 handle_new_pad (info, pad, caps);
2039 gst_caps_unref (caps);
2043 /* construct and run the source and demuxer elements until we found
2044 * all the streams or until a preroll queue has been filled.
2047 setup_source (GstURISourceBin * urisrc)
2049 gboolean have_out, is_dynamic;
2051 GST_DEBUG_OBJECT (urisrc, "setup source");
2053 /* create and configure an element that can handle the uri */
2054 if (!(urisrc->source = gen_source_element (urisrc)))
2057 /* state will be merged later - if file is not found, error will be
2058 * handled by the application right after. */
2059 gst_bin_add (GST_BIN_CAST (urisrc), urisrc->source);
2061 /* notify of the new source used */
2062 g_object_notify (G_OBJECT (urisrc), "source");
2064 g_signal_emit (urisrc, gst_uri_source_bin_signals[SIGNAL_SOURCE_SETUP],
2067 /* see if the source element emits raw audio/video all by itself,
2068 * if so, we can create streams for the pads and be done with it.
2069 * Also check that is has source pads, if not, we assume it will
2070 * do everything itself. */
2071 if (!analyse_source_and_expose_raw_pads (urisrc, &have_out, &is_dynamic))
2072 goto invalid_source;
2078 GST_DEBUG_OBJECT (urisrc, "Source has dynamic output pads");
2079 /* connect a handler for the new-pad signal */
2080 urisrc->src_np_sig_id =
2081 g_signal_connect (urisrc->source, "pad-added",
2082 G_CALLBACK (source_new_pad), urisrc);
2090 /* error message was already posted */
2095 GST_ELEMENT_ERROR (urisrc, CORE, FAILED,
2096 (_("Source element is invalid.")), (NULL));
2101 GST_ELEMENT_ERROR (urisrc, CORE, FAILED,
2102 (_("Source element has no pads.")), (NULL));
2108 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
2109 GList * structure_list)
2113 for (l = structure_list; l != NULL; l = l->next) {
2114 GValue val = { 0, };
2117 *first = gst_structure_copy ((GstStructure *) l->data);
2119 g_value_init (&val, GST_TYPE_STRUCTURE);
2120 g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
2121 gst_value_list_append_value (list_val, &val);
2122 g_value_unset (&val);
2126 /* if it's a redirect message with multiple redirect locations we might
2127 * want to pick a different 'best' location depending on the required
2128 * bitrates and the connection speed */
2130 handle_redirect_message (GstURISourceBin * urisrc, GstMessage * msg)
2132 const GValue *locations_list, *location_val;
2133 GstMessage *new_msg;
2134 GstStructure *new_structure = NULL;
2135 GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
2136 GValue new_list = { 0, };
2138 const GstStructure *structure;
2140 GST_DEBUG_OBJECT (urisrc, "redirect message: %" GST_PTR_FORMAT, msg);
2141 GST_DEBUG_OBJECT (urisrc, "connection speed: %" G_GUINT64_FORMAT,
2142 urisrc->connection_speed);
2144 structure = gst_message_get_structure (msg);
2145 if (urisrc->connection_speed == 0 || structure == NULL)
2148 locations_list = gst_structure_get_value (structure, "locations");
2149 if (locations_list == NULL)
2152 size = gst_value_list_get_size (locations_list);
2156 /* maintain existing order as much as possible, just sort references
2157 * with too high a bitrate to the end (the assumption being that if
2158 * bitrates are given they are given for all interesting streams and
2159 * that the you-need-at-least-version-xyz redirect has the same bitrate
2160 * as the lowest referenced redirect alternative) */
2161 for (i = 0; i < size; ++i) {
2162 const GstStructure *s;
2165 location_val = gst_value_list_get_value (locations_list, i);
2166 s = (const GstStructure *) g_value_get_boxed (location_val);
2167 if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
2168 GST_DEBUG_OBJECT (urisrc, "no bitrate: %" GST_PTR_FORMAT, s);
2169 l_neutral = g_list_append (l_neutral, (gpointer) s);
2170 } else if (bitrate > urisrc->connection_speed) {
2171 GST_DEBUG_OBJECT (urisrc, "bitrate too high: %" GST_PTR_FORMAT, s);
2172 l_bad = g_list_append (l_bad, (gpointer) s);
2173 } else if (bitrate <= urisrc->connection_speed) {
2174 GST_DEBUG_OBJECT (urisrc, "bitrate OK: %" GST_PTR_FORMAT, s);
2175 l_good = g_list_append (l_good, (gpointer) s);
2179 g_value_init (&new_list, GST_TYPE_LIST);
2180 value_list_append_structure_list (&new_list, &new_structure, l_good);
2181 value_list_append_structure_list (&new_list, &new_structure, l_neutral);
2182 value_list_append_structure_list (&new_list, &new_structure, l_bad);
2183 gst_structure_take_value (new_structure, "locations", &new_list);
2185 g_list_free (l_good);
2186 g_list_free (l_neutral);
2187 g_list_free (l_bad);
2189 new_msg = gst_message_new_element (msg->src, new_structure);
2190 gst_message_unref (msg);
2192 GST_DEBUG_OBJECT (urisrc, "new redirect message: %" GST_PTR_FORMAT, new_msg);
2196 /* CALL WITH URISOURCEBIN LOCK */
2197 static OutputSlotInfo *
2198 output_slot_for_buffering_element (GstURISourceBin * urisrc,
2199 GstElement * element)
2202 for (top = urisrc->src_infos; top; top = top->next) {
2203 ChildSrcPadInfo *info = top->data;
2204 for (iter = info->outputs; iter; iter = iter->next) {
2205 OutputSlotInfo *slot = iter->data;
2206 if (slot->queue == element)
2215 handle_buffering_message (GstURISourceBin * urisrc, GstMessage * msg)
2217 gint perc, msg_perc;
2218 gint smaller_perc = 100;
2219 GstMessage *smaller = NULL;
2220 GList *found = NULL;
2222 OutputSlotInfo *slot;
2224 /* buffering messages must be aggregated as there might be multiple buffering
2225 * elements in the pipeline and their independent buffering messages will
2226 * confuse the application
2228 * urisourcebin keeps a list of messages received from elements that are
2231 * 0) Ignore buffering from elements that are draining (is_eos == TRUE)
2232 * 1) Always post the smaller buffering %
2233 * 2) If an element posts a 100% buffering message, remove it from the list
2234 * 3) When there are no more messages on the list, post 100% message
2235 * 4) When an element posts a new buffering message, update the one
2236 * on the list to this new value
2238 gst_message_parse_buffering (msg, &msg_perc);
2239 GST_LOG_OBJECT (urisrc, "Got buffering msg from %" GST_PTR_FORMAT
2240 " with %d%%", GST_MESSAGE_SRC (msg), msg_perc);
2242 BUFFERING_LOCK (urisrc);
2244 output_slot_for_buffering_element (urisrc,
2245 (GstElement *) GST_MESSAGE_SRC (msg));
2246 if (slot && slot->is_eos) {
2247 /* Ignore buffering messages from queues we marked as EOS,
2248 * we already removed those from the list of buffering
2250 BUFFERING_UNLOCK (urisrc);
2251 gst_message_replace (&msg, NULL);
2256 g_mutex_lock (&urisrc->buffering_post_lock);
2259 * Single loop for 2 things:
2260 * 1) Look for a message with the same source
2261 * 1.1) If the received message is 100%, remove it from the list
2262 * 2) Find the minimum buffering from the list from elements that aren't EOS
2264 for (iter = urisrc->buffering_status; iter;) {
2265 GstMessage *bufstats = iter->data;
2266 gboolean is_eos = FALSE;
2269 output_slot_for_buffering_element (urisrc,
2270 (GstElement *) GST_MESSAGE_SRC (msg));
2272 is_eos = slot->is_eos;
2274 if (GST_MESSAGE_SRC (bufstats) == GST_MESSAGE_SRC (msg)) {
2276 if (msg_perc < 100) {
2277 gst_message_unref (iter->data);
2278 bufstats = iter->data = gst_message_ref (msg);
2280 GList *current = iter;
2282 /* remove the element here and avoid confusing the loop */
2283 iter = g_list_next (iter);
2285 gst_message_unref (current->data);
2286 urisrc->buffering_status =
2287 g_list_delete_link (urisrc->buffering_status, current);
2293 /* only update minimum stat for non-EOS slots */
2295 gst_message_parse_buffering (bufstats, &perc);
2296 if (perc < smaller_perc) {
2297 smaller_perc = perc;
2301 GST_LOG_OBJECT (urisrc, "Ignoring buffering from EOS element");
2303 iter = g_list_next (iter);
2306 if (found == NULL && msg_perc < 100) {
2307 if (msg_perc < smaller_perc) {
2308 smaller_perc = msg_perc;
2311 urisrc->buffering_status =
2312 g_list_prepend (urisrc->buffering_status, gst_message_ref (msg));
2315 if (smaller_perc == urisrc->last_buffering_pct) {
2316 /* Don't repeat our last buffering status */
2317 gst_message_replace (&msg, NULL);
2319 urisrc->last_buffering_pct = smaller_perc;
2321 /* now compute the buffering message that should be posted */
2322 if (smaller_perc == 100) {
2323 g_assert (urisrc->buffering_status == NULL);
2324 /* we are posting the original received msg */
2326 gst_message_replace (&msg, smaller);
2329 BUFFERING_UNLOCK (urisrc);
2332 GST_LOG_OBJECT (urisrc, "Sending buffering msg from %" GST_PTR_FORMAT
2333 " with %d%%", GST_MESSAGE_SRC (msg), smaller_perc);
2334 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN (urisrc), msg);
2336 GST_LOG_OBJECT (urisrc, "Dropped buffering msg as a repeat of %d%%",
2339 g_mutex_unlock (&urisrc->buffering_post_lock);
2342 /* Remove any buffering message from the given source */
2344 remove_buffering_msgs (GstURISourceBin * urisrc, GstObject * src)
2347 gboolean removed = FALSE, post;
2349 BUFFERING_LOCK (urisrc);
2350 g_mutex_lock (&urisrc->buffering_post_lock);
2352 GST_DEBUG_OBJECT (urisrc, "Removing %" GST_PTR_FORMAT
2353 " buffering messages", src);
2355 for (iter = urisrc->buffering_status; iter;) {
2356 GstMessage *bufstats = iter->data;
2357 if (GST_MESSAGE_SRC (bufstats) == src) {
2358 gst_message_unref (bufstats);
2359 urisrc->buffering_status =
2360 g_list_delete_link (urisrc->buffering_status, iter);
2364 iter = g_list_next (iter);
2367 post = (removed && urisrc->buffering_status == NULL);
2368 BUFFERING_UNLOCK (urisrc);
2371 GST_DEBUG_OBJECT (urisrc, "Last buffering element done - posting 100%%");
2373 /* removed the last buffering element, post 100% */
2374 gst_element_post_message (GST_ELEMENT_CAST (urisrc),
2375 gst_message_new_buffering (GST_OBJECT_CAST (urisrc), 100));
2378 g_mutex_unlock (&urisrc->buffering_post_lock);
2382 handle_message (GstBin * bin, GstMessage * msg)
2384 GstURISourceBin *urisrc = GST_URI_SOURCE_BIN (bin);
2386 switch (GST_MESSAGE_TYPE (msg)) {
2387 case GST_MESSAGE_ELEMENT:{
2388 if (gst_message_has_name (msg, "redirect")) {
2389 /* sort redirect messages based on the connection speed. This simplifies
2390 * the user of this element as it can in most cases just pick the first item
2391 * of the sorted list as a good redirection candidate. It can of course
2392 * choose something else from the list if it has a better way. */
2393 msg = handle_redirect_message (urisrc, msg);
2397 case GST_MESSAGE_BUFFERING:
2398 handle_buffering_message (urisrc, msg);
2406 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
2409 /* generic struct passed to all query fold methods
2410 * FIXME, move to core.
2421 typedef void (*QueryInitFunction) (GstURISourceBin * urisrc, QueryFold * fold);
2422 typedef void (*QueryDoneFunction) (GstURISourceBin * urisrc, QueryFold * fold);
2424 /* for duration/position we collect all durations/positions and take
2425 * the MAX of all valid results */
2427 uri_source_query_init (GstURISourceBin * urisrc, QueryFold * fold)
2431 fold->seekable = TRUE;
2436 uri_source_query_duration_fold (const GValue * item, GValue * ret,
2439 GstPad *pad = g_value_get_object (item);
2441 if (gst_pad_query (pad, fold->query)) {
2444 g_value_set_boolean (ret, TRUE);
2446 gst_query_parse_duration (fold->query, NULL, &duration);
2448 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
2450 if (duration > fold->max)
2451 fold->max = duration;
2457 uri_source_query_duration_done (GstURISourceBin * urisrc, QueryFold * fold)
2461 gst_query_parse_duration (fold->query, &format, NULL);
2462 /* store max in query result */
2463 gst_query_set_duration (fold->query, format, fold->max);
2465 GST_DEBUG ("max duration %" G_GINT64_FORMAT, fold->max);
2469 uri_source_query_position_fold (const GValue * item, GValue * ret,
2472 GstPad *pad = g_value_get_object (item);
2474 if (gst_pad_query (pad, fold->query)) {
2477 g_value_set_boolean (ret, TRUE);
2479 gst_query_parse_position (fold->query, NULL, &position);
2481 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
2483 if (position > fold->max)
2484 fold->max = position;
2491 uri_source_query_position_done (GstURISourceBin * urisrc, QueryFold * fold)
2495 gst_query_parse_position (fold->query, &format, NULL);
2496 /* store max in query result */
2497 gst_query_set_position (fold->query, format, fold->max);
2499 GST_DEBUG_OBJECT (urisrc, "max position %" G_GINT64_FORMAT, fold->max);
2503 uri_source_query_latency_fold (const GValue * item, GValue * ret,
2506 GstPad *pad = g_value_get_object (item);
2508 if (gst_pad_query (pad, fold->query)) {
2509 GstClockTime min, max;
2512 gst_query_parse_latency (fold->query, &live, &min, &max);
2514 GST_DEBUG_OBJECT (pad,
2515 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2516 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
2519 /* for the combined latency we collect the MAX of all min latencies and
2520 * the MIN of all max latencies */
2521 if (min > fold->min)
2523 if (fold->max == -1)
2525 else if (max < fold->max)
2531 GST_LOG_OBJECT (pad, "latency query failed");
2532 g_value_set_boolean (ret, FALSE);
2539 uri_source_query_latency_done (GstURISourceBin * urisrc, QueryFold * fold)
2541 /* store max in query result */
2542 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
2544 GST_DEBUG_OBJECT (urisrc,
2545 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2546 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
2550 /* we are seekable if all srcpads are seekable */
2552 uri_source_query_seeking_fold (const GValue * item, GValue * ret,
2555 GstPad *pad = g_value_get_object (item);
2557 if (gst_pad_query (pad, fold->query)) {
2560 g_value_set_boolean (ret, TRUE);
2561 gst_query_parse_seeking (fold->query, NULL, &seekable, NULL, NULL);
2563 GST_DEBUG_OBJECT (item, "got seekable %d", seekable);
2566 fold->seekable = seekable;
2573 uri_source_query_seeking_done (GstURISourceBin * urisrc, QueryFold * fold)
2577 gst_query_parse_seeking (fold->query, &format, NULL, NULL, NULL);
2578 gst_query_set_seeking (fold->query, format, fold->seekable, 0, -1);
2580 GST_DEBUG_OBJECT (urisrc, "seekable %d", fold->seekable);
2583 /* generic fold, return first valid result */
2585 uri_source_query_generic_fold (const GValue * item, GValue * ret,
2588 GstPad *pad = g_value_get_object (item);
2591 if ((res = gst_pad_query (pad, fold->query))) {
2592 g_value_set_boolean (ret, TRUE);
2593 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
2596 /* and stop as soon as we have a valid result */
2600 /* we're a bin, the default query handler iterates sink elements, which we don't
2601 * have normally. We should just query all source pads.
2604 gst_uri_source_bin_query (GstElement * element, GstQuery * query)
2606 GstURISourceBin *urisrc;
2607 gboolean res = FALSE;
2609 GstIteratorFoldFunction fold_func;
2610 QueryInitFunction fold_init = NULL;
2611 QueryDoneFunction fold_done = NULL;
2612 QueryFold fold_data;
2614 gboolean default_ret = FALSE;
2616 urisrc = GST_URI_SOURCE_BIN (element);
2618 switch (GST_QUERY_TYPE (query)) {
2619 case GST_QUERY_DURATION:
2620 /* iterate and collect durations */
2621 fold_func = (GstIteratorFoldFunction) uri_source_query_duration_fold;
2622 fold_init = uri_source_query_init;
2623 fold_done = uri_source_query_duration_done;
2625 case GST_QUERY_POSITION:
2626 /* iterate and collect durations */
2627 fold_func = (GstIteratorFoldFunction) uri_source_query_position_fold;
2628 fold_init = uri_source_query_init;
2629 fold_done = uri_source_query_position_done;
2631 case GST_QUERY_LATENCY:
2632 /* iterate and collect durations */
2633 fold_func = (GstIteratorFoldFunction) uri_source_query_latency_fold;
2634 fold_init = uri_source_query_init;
2635 fold_done = uri_source_query_latency_done;
2638 case GST_QUERY_SEEKING:
2639 /* iterate and collect durations */
2640 fold_func = (GstIteratorFoldFunction) uri_source_query_seeking_fold;
2641 fold_init = uri_source_query_init;
2642 fold_done = uri_source_query_seeking_done;
2645 fold_func = (GstIteratorFoldFunction) uri_source_query_generic_fold;
2649 fold_data.query = query;
2651 g_value_init (&ret, G_TYPE_BOOLEAN);
2652 g_value_set_boolean (&ret, default_ret);
2654 iter = gst_element_iterate_src_pads (element);
2655 GST_DEBUG_OBJECT (element, "Sending query %p (type %d) to src pads",
2656 query, GST_QUERY_TYPE (query));
2659 fold_init (urisrc, &fold_data);
2662 GstIteratorResult ires;
2664 ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
2667 case GST_ITERATOR_RESYNC:
2668 gst_iterator_resync (iter);
2670 fold_init (urisrc, &fold_data);
2671 g_value_set_boolean (&ret, default_ret);
2673 case GST_ITERATOR_OK:
2674 case GST_ITERATOR_DONE:
2675 res = g_value_get_boolean (&ret);
2676 if (fold_done != NULL && res)
2677 fold_done (urisrc, &fold_data);
2685 gst_iterator_free (iter);
2690 static GstStateChangeReturn
2691 gst_uri_source_bin_change_state (GstElement * element,
2692 GstStateChange transition)
2694 GstStateChangeReturn ret;
2695 GstURISourceBin *urisrc = GST_URI_SOURCE_BIN (element);
2697 switch (transition) {
2698 case GST_STATE_CHANGE_READY_TO_PAUSED:
2699 GST_URI_SOURCE_BIN_LOCK (element);
2700 urisrc->flushing = FALSE;
2701 urisrc->activated = FALSE;
2702 GST_URI_SOURCE_BIN_UNLOCK (element);
2703 GST_DEBUG ("ready to paused");
2704 if (!setup_source (urisrc))
2707 case GST_STATE_CHANGE_PAUSED_TO_READY:
2708 GST_URI_SOURCE_BIN_LOCK (element);
2709 urisrc->flushing = TRUE;
2710 g_cond_broadcast (&urisrc->activation_cond);
2711 GST_URI_SOURCE_BIN_UNLOCK (element);
2716 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2717 if (ret == GST_STATE_CHANGE_FAILURE)
2720 switch (transition) {
2721 case GST_STATE_CHANGE_READY_TO_PAUSED:
2723 GST_URI_SOURCE_BIN_LOCK (element);
2724 GST_DEBUG_OBJECT (urisrc, "Potentially exposing pads");
2725 urisrc->activated = TRUE;
2726 g_cond_broadcast (&urisrc->activation_cond);
2727 GST_URI_SOURCE_BIN_UNLOCK (element);
2730 case GST_STATE_CHANGE_PAUSED_TO_READY:
2731 GST_DEBUG ("paused to ready");
2732 remove_source (urisrc);
2733 g_list_free_full (urisrc->buffering_status,
2734 (GDestroyNotify) gst_message_unref);
2735 urisrc->buffering_status = NULL;
2736 urisrc->last_buffering_pct = -1;
2738 case GST_STATE_CHANGE_READY_TO_NULL:
2739 GST_DEBUG ("ready to null");
2740 remove_source (urisrc);
2750 return GST_STATE_CHANGE_FAILURE;
2754 /* clean up leftover groups */
2755 return GST_STATE_CHANGE_FAILURE;