1 /* GStreamer
2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-uridecodebin
22 * @title: uridecodebin
23 *
24 * Decodes data from a URI into raw media. It selects a source element that can
25 * handle the given #GstURIDecodeBin:uri scheme and connects it to a decodebin.
26 */
27
28 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
29 * with newer GLib versions (>= 2.31.0) */
30 #define GLIB_DISABLE_DEPRECATION_WARNINGS
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <string.h>
37
38 #include <gst/gst.h>
39 #include <gst/gst-i18n-plugin.h>
40 #include <gst/pbutils/missing-plugins.h>
41
42 #include "gstplay-enum.h"
43 #include "gstrawcaps.h"
44 #include "gstplayback.h"
45 #include "gstplaybackutils.h"
46
47 #define GST_TYPE_URI_DECODE_BIN \
48 (gst_uri_decode_bin_get_type())
49 #define GST_URI_DECODE_BIN(obj) \
50 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBin))
51 #define GST_URI_DECODE_BIN_CLASS(klass) \
52 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBinClass))
53 #define GST_IS_URI_DECODE_BIN(obj) \
54 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DECODE_BIN))
55 #define GST_IS_URI_DECODE_BIN_CLASS(klass) \
56 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DECODE_BIN))
57 #define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
58
59 typedef struct _GstURIDecodeBin GstURIDecodeBin;
60 typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;
61
62 #define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(&((GstURIDecodeBin*)(dec))->lock))
63 #define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(&((GstURIDecodeBin*)(dec))->lock))
64
65 typedef struct _GstURIDecodeBinStream
66 {
67 gulong probe_id;
68 guint bitrate;
69 } GstURIDecodeBinStream;
70
71 /**
72 * GstURIDecodeBin
73 *
74 * uridecodebin element struct
75 */
76 struct _GstURIDecodeBin
77 {
78 GstBin parent_instance;
79
80 GMutex lock; /* lock for constructing */
81
82 GMutex factories_lock;
83 guint32 factories_cookie;
84 GList *factories; /* factories we can use for selecting elements */
85
86 gchar *uri;
87 guint64 connection_speed;
88 GstCaps *caps;
89 gchar *encoding;
90
91 gboolean is_stream;
92 gboolean is_adaptive;
93 gboolean need_queue;
94 guint64 buffer_duration; /* When buffering, buffer duration (ns) */
95 guint buffer_size; /* When buffering, buffer size (bytes) */
96 gboolean download;
97 gboolean use_buffering;
98
99 GstElement *source;
100 GstElement *queue;
101 GstElement *typefind;
102 guint have_type_id; /* have-type signal id from typefind */
103 GSList *decodebins;
104 GSList *pending_decodebins;
105 GHashTable *streams;
106 guint numpads;
107
108 /* for dynamic sources */
109 guint src_np_sig_id; /* new-pad signal id */
110 guint src_nmp_sig_id; /* no-more-pads signal id */
111 gint pending;
112 GList *missing_plugin_errors;
113
114 gboolean async_pending; /* async-start has been emitted */
115
116 gboolean expose_allstreams; /* Whether to expose unknow type streams or not */
117
118 guint64 ring_buffer_max_size; /* 0 means disabled */
119 };
120
121 struct _GstURIDecodeBinClass
122 {
123 GstBinClass parent_class;
124
125 /* signal fired when we found a pad that we cannot decode */
126 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
127
128 /* signal fired to know if we continue trying to decode the given caps */
129 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
130 GstCaps * caps);
131 /* signal fired to get a list of factories to try to autoplug */
132 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
133 GstCaps * caps);
134 /* signal fired to sort the factories */
135 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
136 GstCaps * caps, GValueArray * factories);
137 /* signal fired to select from the proposed list of factories */
138 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
139 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
140 /* signal fired when a autoplugged element that is not linked downstream
141 * or exposed wants to query something */
142 gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
143 GstQuery * query);
144
145 /* emitted when all data is decoded */
146 void (*drained) (GstElement * element);
147 };
148
149 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
150 GST_PAD_SRC,
151 GST_PAD_SOMETIMES,
152 GST_STATIC_CAPS_ANY);
153
154 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
155
156 GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
157 #define GST_CAT_DEFAULT gst_uri_decode_bin_debug
158
159 /* signals */
160 enum
161 {
162 SIGNAL_UNKNOWN_TYPE,
163 SIGNAL_AUTOPLUG_CONTINUE,
164 SIGNAL_AUTOPLUG_FACTORIES,
165 SIGNAL_AUTOPLUG_SELECT,
166 SIGNAL_AUTOPLUG_SORT,
167 SIGNAL_AUTOPLUG_QUERY,
168 SIGNAL_DRAINED,
169 SIGNAL_SOURCE_SETUP,
170 LAST_SIGNAL
171 };
172
173 /* properties */
174 #define DEFAULT_PROP_URI NULL
175 #define DEFAULT_PROP_SOURCE NULL
176 #define DEFAULT_CONNECTION_SPEED 0
177 #define DEFAULT_CAPS (gst_static_caps_get (&default_raw_caps))
178 #define DEFAULT_SUBTITLE_ENCODING NULL
179 #define DEFAULT_BUFFER_DURATION -1
180 #define DEFAULT_BUFFER_SIZE -1
181 #define DEFAULT_DOWNLOAD FALSE
182 #define DEFAULT_USE_BUFFERING FALSE
183 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
184 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
185
186 enum
187 {
188 PROP_0,
189 PROP_URI,
190 PROP_SOURCE,
191 PROP_CONNECTION_SPEED,
192 PROP_CAPS,
193 PROP_SUBTITLE_ENCODING,
194 PROP_BUFFER_SIZE,
195 PROP_BUFFER_DURATION,
196 PROP_DOWNLOAD,
197 PROP_USE_BUFFERING,
198 PROP_EXPOSE_ALL_STREAMS,
199 PROP_RING_BUFFER_MAX_SIZE
200 };
201
202 static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };
203
204 GType gst_uri_decode_bin_get_type (void);
205 #define gst_uri_decode_bin_parent_class parent_class
206 G_DEFINE_TYPE (GstURIDecodeBin, gst_uri_decode_bin, GST_TYPE_BIN);
207
208 static void remove_decoders (GstURIDecodeBin * bin, gboolean force);
209 static void gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
210 const GValue * value, GParamSpec * pspec);
211 static void gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
212 GValue * value, GParamSpec * pspec);
213 static void gst_uri_decode_bin_finalize (GObject * obj);
214
215 static void handle_message (GstBin * bin, GstMessage * msg);
216
217 static gboolean gst_uri_decode_bin_query (GstElement * element,
218 GstQuery * query);
219 static GstStateChangeReturn gst_uri_decode_bin_change_state (GstElement *
220 element, GstStateChange transition);
221
222 static gboolean
_gst_boolean_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)223 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
224 GValue * return_accu, const GValue * handler_return, gpointer dummy)
225 {
226 gboolean myboolean;
227
228 myboolean = g_value_get_boolean (handler_return);
229 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
230 g_value_set_boolean (return_accu, myboolean);
231
232 /* stop emission if FALSE */
233 return myboolean;
234 }
235
236 static gboolean
_gst_boolean_or_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)237 _gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
238 GValue * return_accu, const GValue * handler_return, gpointer dummy)
239 {
240 gboolean myboolean;
241 gboolean retboolean;
242
243 myboolean = g_value_get_boolean (handler_return);
244 retboolean = g_value_get_boolean (return_accu);
245
246 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
247 g_value_set_boolean (return_accu, myboolean || retboolean);
248
249 return TRUE;
250 }
251
252 static gboolean
_gst_array_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)253 _gst_array_accumulator (GSignalInvocationHint * ihint,
254 GValue * return_accu, const GValue * handler_return, gpointer dummy)
255 {
256 gpointer array;
257
258 array = g_value_get_boxed (handler_return);
259 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
260 g_value_set_boxed (return_accu, array);
261
262 return FALSE;
263 }
264
265 static gboolean
_gst_select_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)266 _gst_select_accumulator (GSignalInvocationHint * ihint,
267 GValue * return_accu, const GValue * handler_return, gpointer dummy)
268 {
269 GstAutoplugSelectResult res;
270
271 res = g_value_get_enum (handler_return);
272 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
273 g_value_set_enum (return_accu, res);
274
275 /* Call the next handler in the chain (if any) when the current callback
276 * returns TRY. This makes it possible to register separate autoplug-select
277 * handlers that implement different TRY/EXPOSE/SKIP strategies.
278 */
279 if (res == GST_AUTOPLUG_SELECT_TRY)
280 return TRUE;
281
282 return FALSE;
283 }
284
285 static gboolean
_gst_array_hasvalue_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)286 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
287 GValue * return_accu, const GValue * handler_return, gpointer dummy)
288 {
289 gpointer array;
290
291 array = g_value_get_boxed (handler_return);
292 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
293 g_value_set_boxed (return_accu, array);
294
295 if (array != NULL)
296 return FALSE;
297
298 return TRUE;
299 }
300
301 static gboolean
gst_uri_decode_bin_autoplug_continue(GstElement * element,GstPad * pad,GstCaps * caps)302 gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
303 GstCaps * caps)
304 {
305 /* by default we always continue */
306 return TRUE;
307 }
308
309 /* Must be called with factories lock! */
310 static void
gst_uri_decode_bin_update_factories_list(GstURIDecodeBin * dec)311 gst_uri_decode_bin_update_factories_list (GstURIDecodeBin * dec)
312 {
313 guint32 cookie;
314
315 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
316 if (!dec->factories || dec->factories_cookie != cookie) {
317 if (dec->factories)
318 gst_plugin_feature_list_free (dec->factories);
319 dec->factories =
320 gst_element_factory_list_get_elements
321 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
322 dec->factories =
323 g_list_sort (dec->factories, gst_playback_utils_compare_factories_func);
324 dec->factories_cookie = cookie;
325 }
326 }
327
328 static GValueArray *
gst_uri_decode_bin_autoplug_factories(GstElement * element,GstPad * pad,GstCaps * caps)329 gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
330 GstCaps * caps)
331 {
332 GList *list, *tmp;
333 GValueArray *result;
334 GstURIDecodeBin *dec = GST_URI_DECODE_BIN_CAST (element);
335
336 GST_DEBUG_OBJECT (element, "finding factories");
337
338 /* return all compatible factories for caps */
339 g_mutex_lock (&dec->factories_lock);
340 gst_uri_decode_bin_update_factories_list (dec);
341 list =
342 gst_element_factory_list_filter (dec->factories, caps, GST_PAD_SINK,
343 gst_caps_is_fixed (caps));
344 g_mutex_unlock (&dec->factories_lock);
345
346 result = g_value_array_new (g_list_length (list));
347 for (tmp = list; tmp; tmp = tmp->next) {
348 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
349 GValue val = { 0, };
350
351 g_value_init (&val, G_TYPE_OBJECT);
352 g_value_set_object (&val, factory);
353 g_value_array_append (result, &val);
354 g_value_unset (&val);
355 }
356 gst_plugin_feature_list_free (list);
357
358 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
359
360 return result;
361 }
362
363 static GValueArray *
gst_uri_decode_bin_autoplug_sort(GstElement * element,GstPad * pad,GstCaps * caps,GValueArray * factories)364 gst_uri_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
365 GstCaps * caps, GValueArray * factories)
366 {
367 return NULL;
368 }
369
370 static GstAutoplugSelectResult
gst_uri_decode_bin_autoplug_select(GstElement * element,GstPad * pad,GstCaps * caps,GstElementFactory * factory)371 gst_uri_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
372 GstCaps * caps, GstElementFactory * factory)
373 {
374 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
375
376 /* Try factory. */
377 return GST_AUTOPLUG_SELECT_TRY;
378 }
379
380 static gboolean
gst_uri_decode_bin_autoplug_query(GstElement * element,GstPad * pad,GstQuery * query)381 gst_uri_decode_bin_autoplug_query (GstElement * element, GstPad * pad,
382 GstQuery * query)
383 {
384 /* No query handled here */
385 return FALSE;
386 }
387
388 static void
gst_uri_decode_bin_class_init(GstURIDecodeBinClass * klass)389 gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
390 {
391 GObjectClass *gobject_class;
392 GstElementClass *gstelement_class;
393 GstBinClass *gstbin_class;
394
395 gobject_class = G_OBJECT_CLASS (klass);
396 gstelement_class = GST_ELEMENT_CLASS (klass);
397 gstbin_class = GST_BIN_CLASS (klass);
398
399 gobject_class->set_property = gst_uri_decode_bin_set_property;
400 gobject_class->get_property = gst_uri_decode_bin_get_property;
401 gobject_class->finalize = gst_uri_decode_bin_finalize;
402
403 g_object_class_install_property (gobject_class, PROP_URI,
404 g_param_spec_string ("uri", "URI", "URI to decode",
405 DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406
407 g_object_class_install_property (gobject_class, PROP_SOURCE,
408 g_param_spec_object ("source", "Source", "Source object used",
409 GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
410
411 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
412 g_param_spec_uint64 ("connection-speed", "Connection Speed",
413 "Network connection speed in kbps (0 = unknown)",
414 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
415 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416
417 g_object_class_install_property (gobject_class, PROP_CAPS,
418 g_param_spec_boxed ("caps", "Caps",
419 "The caps on which to stop decoding. (NULL = default)",
420 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421
422 g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
423 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
424 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
425 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
426 "be checked for an encoding to use. If that is not set either, "
427 "ISO-8859-15 will be assumed.", NULL,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429
430 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
431 g_param_spec_int ("buffer-size", "Buffer size (bytes)",
432 "Buffer size when buffering streams (-1 default value)",
433 -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
434 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
436 g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
437 "Buffer duration when buffering streams (-1 default value)",
438 -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
439 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
440
441 /**
442 * GstURIDecodeBin::download:
443 *
444 * For certain media type, enable download buffering.
445 */
446 g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
447 g_param_spec_boolean ("download", "Download",
448 "Attempt download buffering when buffering network streams",
449 DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
450 /**
451 * GstURIDecodeBin::use-buffering:
452 *
453 * Emit BUFFERING messages based on low-/high-percent thresholds of the
454 * demuxed or parsed data.
455 * When download buffering is activated and used for the current media
456 * type, this property does nothing. Otherwise perform buffering on the
457 * demuxed or parsed media.
458 */
459 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
460 g_param_spec_boolean ("use-buffering", "Use Buffering",
461 "Perform buffering on demuxed/parsed media",
462 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463
464 /**
465 * GstURIDecodeBin::expose-all-streams
466 *
467 * Expose streams of unknown type.
468 *
469 * If set to %FALSE, then only the streams that can be decoded to the final
470 * caps (see 'caps' property) will have a pad exposed. Streams that do not
471 * match those caps but could have been decoded will not have decoder plugged
472 * in internally and will not have a pad exposed.
473 */
474 g_object_class_install_property (gobject_class, PROP_EXPOSE_ALL_STREAMS,
475 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
476 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
477 DEFAULT_EXPOSE_ALL_STREAMS,
478 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
479
480 /**
481 * GstURIDecodeBin::ring-buffer-max-size
482 *
483 * The maximum size of the ring buffer in kilobytes. If set to 0, the ring
484 * buffer is disabled. Default is 0.
485 */
486 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
487 g_param_spec_uint64 ("ring-buffer-max-size",
488 "Max. ring buffer size (bytes)",
489 "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
490 0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
491 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
492
493 /**
494 * GstURIDecodeBin::unknown-type:
495 * @bin: The uridecodebin.
496 * @pad: the new pad containing caps that cannot be resolved to a 'final'.
497 * stream type.
498 * @caps: the #GstCaps of the pad that cannot be resolved.
499 *
500 * This signal is emitted when a pad for which there is no further possible
501 * decoding is added to the uridecodebin.
502 */
503 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
504 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
505 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, unknown_type),
506 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
507 GST_TYPE_PAD, GST_TYPE_CAPS);
508
509 /**
510 * GstURIDecodeBin::autoplug-continue:
511 * @bin: The uridecodebin.
512 * @pad: The #GstPad.
513 * @caps: The #GstCaps found.
514 *
515 * This signal is emitted whenever uridecodebin finds a new stream. It is
516 * emitted before looking for any elements that can handle that stream.
517 *
518 * > Invocation of signal handlers stops after the first signal handler
519 * > returns %FALSE. Signal handlers are invoked in the order they were
520 * > connected in.
521 *
522 * Returns: %TRUE if you wish uridecodebin to look for elements that can
523 * handle the given @caps. If %FALSE, those caps will be considered as
524 * final and the pad will be exposed as such (see 'pad-added' signal of
525 * #GstElement).
526 */
527 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
528 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
529 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
530 autoplug_continue), _gst_boolean_accumulator, NULL,
531 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
532 GST_TYPE_CAPS);
533
534 /**
535 * GstURIDecodeBin::autoplug-factories:
536 * @bin: The uridecodebin.
537 * @pad: The #GstPad.
538 * @caps: The #GstCaps found.
539 *
540 * This function is emitted when an array of possible factories for @caps on
541 * @pad is needed. Uridecodebin will by default return an array with all
542 * compatible factories, sorted by rank.
543 *
544 * If this function returns NULL, @pad will be exposed as a final caps.
545 *
546 * If this function returns an empty array, the pad will be considered as
547 * having an unhandled type media type.
548 *
549 * > Only the signal handler that is connected first will ever by invoked.
550 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
551 * > signal, they will never be invoked!
552 *
553 * Returns: a #GValueArray* with a list of factories to try. The factories are
554 * by default tried in the returned order or based on the index returned by
555 * "autoplug-select".
556 */
557 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
558 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
559 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
560 autoplug_factories), _gst_array_accumulator, NULL,
561 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
562 GST_TYPE_PAD, GST_TYPE_CAPS);
563
564 /**
565 * GstURIDecodeBin::autoplug-sort:
566 * @bin: The uridecodebin.
567 * @pad: The #GstPad.
568 * @caps: The #GstCaps.
569 * @factories: A #GValueArray of possible #GstElementFactory to use.
570 *
571 * Once decodebin has found the possible #GstElementFactory objects to try
572 * for @caps on @pad, this signal is emitted. The purpose of the signal is for
573 * the application to perform additional sorting or filtering on the element
574 * factory array.
575 *
576 * The callee should copy and modify @factories or return %NULL if the
577 * order should not change.
578 *
579 * > Invocation of signal handlers stops after one signal handler has
580 * > returned something else than %NULL. Signal handlers are invoked in
581 * > the order they were connected in.
582 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
583 * > signal, they will never be invoked!
584 *
585 * Returns: A new sorted array of #GstElementFactory objects.
586 */
587 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
588 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
589 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_sort),
590 _gst_array_hasvalue_accumulator, NULL,
591 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
592 GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
593
594 /**
595 * GstURIDecodeBin::autoplug-select:
596 * @bin: The uridecodebin.
597 * @pad: The #GstPad.
598 * @caps: The #GstCaps.
599 * @factory: A #GstElementFactory to use.
600 *
601 * This signal is emitted once uridecodebin has found all the possible
602 * #GstElementFactory that can be used to handle the given @caps. For each of
603 * those factories, this signal is emitted.
604 *
605 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
606 * value indicating what decodebin should do next.
607 *
608 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
609 * @factory.
610 *
611 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
612 * any element to it.
613 *
614 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
615 * next factory.
616 *
617 * > The signal handler will not be invoked if any of the previously
618 * > registered signal handlers (if any) return a value other than
619 * > GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
620 * > GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
621 * > registered next (again, if any) can override that decision.
622 *
623 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
624 * operation. The default handler will always return
625 * #GST_AUTOPLUG_SELECT_TRY.
626 */
627 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
628 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
629 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
630 autoplug_select), _gst_select_accumulator, NULL,
631 g_cclosure_marshal_generic,
632 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
633 GST_TYPE_ELEMENT_FACTORY);
634
635 /**
636 * GstDecodeBin::autoplug-query:
637 * @bin: The decodebin.
638 * @child: The child element doing the query
639 * @pad: The #GstPad.
640 * @query: The #GstQuery.
641 *
642 * This signal is emitted whenever an autoplugged element that is
643 * not linked downstream yet and not exposed does a query. It can
644 * be used to tell the element about the downstream supported caps
645 * for example.
646 *
647 * Returns: %TRUE if the query was handled, %FALSE otherwise.
648 */
649 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
650 g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
651 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_query),
652 _gst_boolean_or_accumulator, NULL, g_cclosure_marshal_generic,
653 G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
654 GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
655
656 /**
657 * GstURIDecodeBin::drained:
658 *
659 * This signal is emitted when the data for the current uri is played.
660 */
661 gst_uri_decode_bin_signals[SIGNAL_DRAINED] =
662 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
663 G_SIGNAL_RUN_LAST,
664 G_STRUCT_OFFSET (GstURIDecodeBinClass, drained), NULL, NULL,
665 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
666
667 /**
668 * GstURIDecodeBin::source-setup:
669 * @bin: the uridecodebin.
670 * @source: source element
671 *
672 * This signal is emitted after the source element has been created, so
673 * it can be configured by setting additional properties (e.g. set a
674 * proxy server for an http source, or set the device and read speed for
675 * an audio cd source). This is functionally equivalent to connecting to
676 * the notify::source signal, but more convenient.
677 */
678 gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP] =
679 g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
680 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
681 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
682
683 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
684 gst_element_class_set_static_metadata (gstelement_class,
685 "URI Decoder", "Generic/Bin/Decoder",
686 "Autoplug and decode an URI to raw media",
687 "Wim Taymans <wim.taymans@gmail.com>");
688
689 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_query);
690 gstelement_class->change_state =
691 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_change_state);
692
693 gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);
694
695 klass->autoplug_continue =
696 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
697 klass->autoplug_factories =
698 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
699 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_sort);
700 klass->autoplug_select =
701 GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_select);
702 klass->autoplug_query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_query);
703 }
704
705 static void
gst_uri_decode_bin_init(GstURIDecodeBin * dec)706 gst_uri_decode_bin_init (GstURIDecodeBin * dec)
707 {
708 /* first filter out the interesting element factories */
709 g_mutex_init (&dec->factories_lock);
710
711 g_mutex_init (&dec->lock);
712
713 dec->uri = g_strdup (DEFAULT_PROP_URI);
714 dec->connection_speed = DEFAULT_CONNECTION_SPEED;
715 dec->caps = DEFAULT_CAPS;
716 dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
717
718 dec->buffer_duration = DEFAULT_BUFFER_DURATION;
719 dec->buffer_size = DEFAULT_BUFFER_SIZE;
720 dec->download = DEFAULT_DOWNLOAD;
721 dec->use_buffering = DEFAULT_USE_BUFFERING;
722 dec->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
723 dec->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
724
725 GST_OBJECT_FLAG_SET (dec, GST_ELEMENT_FLAG_SOURCE);
726 gst_bin_set_suppressed_flags (GST_BIN (dec),
727 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
728 }
729
730 static void
gst_uri_decode_bin_finalize(GObject * obj)731 gst_uri_decode_bin_finalize (GObject * obj)
732 {
733 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (obj);
734
735 remove_decoders (dec, TRUE);
736 g_mutex_clear (&dec->lock);
737 g_mutex_clear (&dec->factories_lock);
738 g_free (dec->uri);
739 g_free (dec->encoding);
740 if (dec->factories)
741 gst_plugin_feature_list_free (dec->factories);
742 if (dec->caps)
743 gst_caps_unref (dec->caps);
744
745 G_OBJECT_CLASS (parent_class)->finalize (obj);
746 }
747
748 static void
gst_uri_decode_bin_set_encoding(GstURIDecodeBin * dec,const gchar * encoding)749 gst_uri_decode_bin_set_encoding (GstURIDecodeBin * dec, const gchar * encoding)
750 {
751 GSList *walk;
752
753 GST_URI_DECODE_BIN_LOCK (dec);
754
755 /* set property first */
756 GST_OBJECT_LOCK (dec);
757 g_free (dec->encoding);
758 dec->encoding = g_strdup (encoding);
759 GST_OBJECT_UNLOCK (dec);
760
761 /* set the property on all decodebins now */
762 for (walk = dec->decodebins; walk; walk = g_slist_next (walk)) {
763 GObject *decodebin = G_OBJECT (walk->data);
764
765 g_object_set (decodebin, "subtitle-encoding", encoding, NULL);
766 }
767 GST_URI_DECODE_BIN_UNLOCK (dec);
768 }
769
770 static void
gst_uri_decode_bin_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)771 gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
772 const GValue * value, GParamSpec * pspec)
773 {
774 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
775
776 switch (prop_id) {
777 case PROP_URI:
778 GST_OBJECT_LOCK (dec);
779 g_free (dec->uri);
780 dec->uri = g_value_dup_string (value);
781 GST_OBJECT_UNLOCK (dec);
782 break;
783 case PROP_CONNECTION_SPEED:
784 GST_OBJECT_LOCK (dec);
785 dec->connection_speed = g_value_get_uint64 (value) * 1000;
786 GST_OBJECT_UNLOCK (dec);
787 break;
788 case PROP_CAPS:
789 GST_OBJECT_LOCK (dec);
790 if (dec->caps)
791 gst_caps_unref (dec->caps);
792 dec->caps = g_value_dup_boxed (value);
793 GST_OBJECT_UNLOCK (dec);
794 break;
795 case PROP_SUBTITLE_ENCODING:
796 gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
797 break;
798 case PROP_BUFFER_SIZE:
799 dec->buffer_size = g_value_get_int (value);
800 break;
801 case PROP_BUFFER_DURATION:
802 dec->buffer_duration = g_value_get_int64 (value);
803 break;
804 case PROP_DOWNLOAD:
805 dec->download = g_value_get_boolean (value);
806 break;
807 case PROP_USE_BUFFERING:
808 dec->use_buffering = g_value_get_boolean (value);
809 break;
810 case PROP_EXPOSE_ALL_STREAMS:
811 dec->expose_allstreams = g_value_get_boolean (value);
812 break;
813 case PROP_RING_BUFFER_MAX_SIZE:
814 dec->ring_buffer_max_size = g_value_get_uint64 (value);
815 break;
816 default:
817 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
818 break;
819 }
820 }
821
822 static void
gst_uri_decode_bin_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)823 gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
824 GValue * value, GParamSpec * pspec)
825 {
826 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
827
828 switch (prop_id) {
829 case PROP_URI:
830 GST_OBJECT_LOCK (dec);
831 g_value_set_string (value, dec->uri);
832 GST_OBJECT_UNLOCK (dec);
833 break;
834 case PROP_SOURCE:
835 GST_OBJECT_LOCK (dec);
836 g_value_set_object (value, dec->source);
837 GST_OBJECT_UNLOCK (dec);
838 break;
839 case PROP_CONNECTION_SPEED:
840 GST_OBJECT_LOCK (dec);
841 g_value_set_uint64 (value, dec->connection_speed / 1000);
842 GST_OBJECT_UNLOCK (dec);
843 break;
844 case PROP_CAPS:
845 GST_OBJECT_LOCK (dec);
846 g_value_set_boxed (value, dec->caps);
847 GST_OBJECT_UNLOCK (dec);
848 break;
849 case PROP_SUBTITLE_ENCODING:
850 GST_OBJECT_LOCK (dec);
851 g_value_set_string (value, dec->encoding);
852 GST_OBJECT_UNLOCK (dec);
853 break;
854 case PROP_BUFFER_SIZE:
855 GST_OBJECT_LOCK (dec);
856 g_value_set_int (value, dec->buffer_size);
857 GST_OBJECT_UNLOCK (dec);
858 break;
859 case PROP_BUFFER_DURATION:
860 GST_OBJECT_LOCK (dec);
861 g_value_set_int64 (value, dec->buffer_duration);
862 GST_OBJECT_UNLOCK (dec);
863 break;
864 case PROP_DOWNLOAD:
865 g_value_set_boolean (value, dec->download);
866 break;
867 case PROP_USE_BUFFERING:
868 g_value_set_boolean (value, dec->use_buffering);
869 break;
870 case PROP_EXPOSE_ALL_STREAMS:
871 g_value_set_boolean (value, dec->expose_allstreams);
872 break;
873 case PROP_RING_BUFFER_MAX_SIZE:
874 g_value_set_uint64 (value, dec->ring_buffer_max_size);
875 break;
876 default:
877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
878 break;
879 }
880 }
881
882 static void
do_async_start(GstURIDecodeBin * dbin)883 do_async_start (GstURIDecodeBin * dbin)
884 {
885 GstMessage *message;
886
887 dbin->async_pending = TRUE;
888
889 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
890 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (dbin), message);
891 }
892
893 static void
do_async_done(GstURIDecodeBin * dbin)894 do_async_done (GstURIDecodeBin * dbin)
895 {
896 GstMessage *message;
897
898 if (dbin->async_pending) {
899 GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
900 message =
901 gst_message_new_async_done (GST_OBJECT_CAST (dbin),
902 GST_CLOCK_TIME_NONE);
903 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (dbin), message);
904
905 dbin->async_pending = FALSE;
906 }
907 }
908
909 #define DEFAULT_QUEUE_SIZE (3 * GST_SECOND)
910 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
911 #define DEFAULT_QUEUE_THRESHOLD ((DEFAULT_QUEUE_SIZE * 95) / 100)
912
913 static void
unknown_type_cb(GstElement * element,GstPad * pad,GstCaps * caps,GstURIDecodeBin * decoder)914 unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
915 GstURIDecodeBin * decoder)
916 {
917 gchar *capsstr;
918
919 capsstr = gst_caps_to_string (caps);
920 GST_ELEMENT_WARNING (decoder, STREAM, CODEC_NOT_FOUND,
921 (_("No decoder available for type \'%s\'."), capsstr), (NULL));
922 g_free (capsstr);
923 }
924
925 /* add a streaminfo that indicates that the stream is handled by the
926 * given element. This usually means that a stream without actual data is
927 * produced but one that is sunken by an element. Examples of this are:
928 * cdaudio, a hardware decoder/sink, dvd meta bins etc...
929 */
930 static void
add_element_stream(GstElement * element,GstURIDecodeBin * decoder)931 add_element_stream (GstElement * element, GstURIDecodeBin * decoder)
932 {
933 g_warning ("add element stream");
934 }
935
936 /* when the decoder element signals that no more pads will be generated, we
937 * can commit the current group.
938 */
939 static void
no_more_pads_full(GstElement * element,gboolean subs,GstURIDecodeBin * decoder)940 no_more_pads_full (GstElement * element, gboolean subs,
941 GstURIDecodeBin * decoder)
942 {
943 gboolean final;
944
945 /* setup phase */
946 GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);
947
948 GST_URI_DECODE_BIN_LOCK (decoder);
949 final = (decoder->pending == 0);
950
951 /* nothing pending, we can exit */
952 if (final)
953 goto done;
954
955 /* the object has no pending no_more_pads */
956 if (!g_object_get_data (G_OBJECT (element), "pending"))
957 goto done;
958 g_object_set_data (G_OBJECT (element), "pending", NULL);
959
960 decoder->pending--;
961 final = (decoder->pending == 0);
962
963 done:
964 GST_URI_DECODE_BIN_UNLOCK (decoder);
965
966 if (final) {
967 /* If we got not a single stream yet, that means that all
968 * decodebins had missing plugins for all of their streams!
969 */
970 if (!decoder->streams || g_hash_table_size (decoder->streams) == 0) {
971 if (decoder->missing_plugin_errors) {
972 GString *str = g_string_new ("");
973 GList *l;
974
975 for (l = decoder->missing_plugin_errors; l; l = l->next) {
976 GstMessage *msg = l->data;
977 gchar *debug;
978
979 gst_message_parse_error (msg, NULL, &debug);
980 g_string_append (str, debug);
981 g_free (debug);
982 gst_message_unref (msg);
983 }
984 g_list_free (decoder->missing_plugin_errors);
985 decoder->missing_plugin_errors = NULL;
986
987 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
988 ("no suitable plugins found:\n%s", str->str));
989 g_string_free (str, TRUE);
990 } else {
991 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
992 ("no suitable plugins found"));
993 }
994 } else {
995 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
996 }
997 do_async_done (decoder);
998 }
999
1000 return;
1001 }
1002
1003 static void
no_more_pads(GstElement * element,GstURIDecodeBin * decoder)1004 no_more_pads (GstElement * element, GstURIDecodeBin * decoder)
1005 {
1006 no_more_pads_full (element, FALSE, decoder);
1007 }
1008
1009 static void
source_no_more_pads(GstElement * element,GstURIDecodeBin * bin)1010 source_no_more_pads (GstElement * element, GstURIDecodeBin * bin)
1011 {
1012 GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
1013 GST_ELEMENT_NAME (element));
1014
1015 g_signal_handler_disconnect (element, bin->src_np_sig_id);
1016 bin->src_np_sig_id = 0;
1017 g_signal_handler_disconnect (element, bin->src_nmp_sig_id);
1018 bin->src_nmp_sig_id = 0;
1019
1020 no_more_pads_full (element, FALSE, bin);
1021 }
1022
1023 static void
configure_stream_buffering(GstURIDecodeBin * decoder)1024 configure_stream_buffering (GstURIDecodeBin * decoder)
1025 {
1026 GstElement *queue = NULL;
1027 GHashTableIter iter;
1028 gpointer key, value;
1029 gint bitrate = 0;
1030
1031 /* automatic configuration enabled ? */
1032 if (decoder->buffer_size != -1)
1033 return;
1034
1035 GST_URI_DECODE_BIN_LOCK (decoder);
1036 if (decoder->queue)
1037 queue = gst_object_ref (decoder->queue);
1038
1039 g_hash_table_iter_init (&iter, decoder->streams);
1040 while (g_hash_table_iter_next (&iter, &key, &value)) {
1041 GstURIDecodeBinStream *stream = value;
1042
1043 if (stream->bitrate && bitrate >= 0)
1044 bitrate += stream->bitrate;
1045 else
1046 bitrate = -1;
1047 }
1048 GST_URI_DECODE_BIN_UNLOCK (decoder);
1049
1050 GST_DEBUG_OBJECT (decoder, "overall bitrate %d", bitrate);
1051 if (!queue)
1052 return;
1053
1054 if (bitrate > 0) {
1055 guint64 time;
1056 guint bytes;
1057
1058 /* all streams have a bitrate;
1059 * configure queue size based on queue duration using combined bitrate */
1060 g_object_get (queue, "max-size-time", &time, NULL);
1061 GST_DEBUG_OBJECT (decoder, "queue buffering time %" GST_TIME_FORMAT,
1062 GST_TIME_ARGS (time));
1063 if (time > 0) {
1064 bytes = gst_util_uint64_scale (time, bitrate, 8 * GST_SECOND);
1065 GST_DEBUG_OBJECT (decoder, "corresponds to buffer size %d", bytes);
1066 g_object_set (queue, "max-size-bytes", bytes, NULL);
1067 }
1068 }
1069
1070 gst_object_unref (queue);
1071 }
1072
1073 static GstPadProbeReturn
decoded_pad_event_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)1074 decoded_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
1075 gpointer user_data)
1076 {
1077 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1078 GstURIDecodeBin *decoder = user_data;
1079
1080 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1081
1082 /* look for a bitrate tag */
1083 switch (GST_EVENT_TYPE (event)) {
1084 case GST_EVENT_TAG:
1085 {
1086 GstTagList *list;
1087 guint bitrate = 0;
1088 GstURIDecodeBinStream *stream;
1089
1090 gst_event_parse_tag (event, &list);
1091 if (!gst_tag_list_get_uint_index (list, GST_TAG_NOMINAL_BITRATE, 0,
1092 &bitrate)) {
1093 gst_tag_list_get_uint_index (list, GST_TAG_BITRATE, 0, &bitrate);
1094 }
1095 GST_DEBUG_OBJECT (pad, "found bitrate %u", bitrate);
1096 if (bitrate) {
1097 GST_URI_DECODE_BIN_LOCK (decoder);
1098 stream = g_hash_table_lookup (decoder->streams, pad);
1099 GST_URI_DECODE_BIN_UNLOCK (decoder);
1100 if (stream) {
1101 stream->bitrate = bitrate;
1102 /* no longer need this probe now */
1103 gst_pad_remove_probe (pad, stream->probe_id);
1104 /* configure buffer if possible */
1105 configure_stream_buffering (decoder);
1106 }
1107 }
1108 break;
1109 }
1110 default:
1111 break;
1112 }
1113
1114 /* never drop */
1115 return GST_PAD_PROBE_OK;
1116 }
1117
1118
1119 static gboolean
copy_sticky_events(GstPad * pad,GstEvent ** event,gpointer user_data)1120 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1121 {
1122 GstPad *gpad = GST_PAD_CAST (user_data);
1123
1124 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
1125 gst_pad_store_sticky_event (gpad, *event);
1126
1127 return TRUE;
1128 }
1129
1130 /* Called by the signal handlers when a decodebin has found a new raw pad */
1131 static void
new_decoded_pad_added_cb(GstElement * element,GstPad * pad,GstURIDecodeBin * decoder)1132 new_decoded_pad_added_cb (GstElement * element, GstPad * pad,
1133 GstURIDecodeBin * decoder)
1134 {
1135 GstPad *newpad;
1136 GstPadTemplate *pad_tmpl;
1137 gchar *padname;
1138 GstURIDecodeBinStream *stream;
1139
1140 GST_DEBUG_OBJECT (element, "new decoded pad, name: <%s>", GST_PAD_NAME (pad));
1141
1142 GST_URI_DECODE_BIN_LOCK (decoder);
1143 padname = g_strdup_printf ("src_%u", decoder->numpads);
1144 decoder->numpads++;
1145 GST_URI_DECODE_BIN_UNLOCK (decoder);
1146
1147 pad_tmpl = gst_static_pad_template_get (&srctemplate);
1148 newpad = gst_ghost_pad_new_from_template (padname, pad, pad_tmpl);
1149 gst_object_unref (pad_tmpl);
1150 g_free (padname);
1151
1152 /* store ref to the ghostpad so we can remove it */
1153 g_object_set_data (G_OBJECT (pad), "uridecodebin.ghostpad", newpad);
1154
1155 /* add event probe to monitor tags */
1156 stream = g_slice_alloc0 (sizeof (GstURIDecodeBinStream));
1157 stream->probe_id =
1158 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1159 decoded_pad_event_probe, decoder, NULL);
1160 GST_URI_DECODE_BIN_LOCK (decoder);
1161 g_hash_table_insert (decoder->streams, pad, stream);
1162 GST_URI_DECODE_BIN_UNLOCK (decoder);
1163
1164 gst_pad_set_active (newpad, TRUE);
1165 gst_pad_sticky_events_foreach (pad, copy_sticky_events, newpad);
1166 gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
1167 }
1168
1169 static GstPadProbeReturn
source_pad_event_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)1170 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
1171 gpointer user_data)
1172 {
1173 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1174 GstURIDecodeBin *decoder = user_data;
1175
1176 GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1177
1178 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1179 GST_DEBUG_OBJECT (pad, "we received EOS");
1180
1181 g_signal_emit (decoder,
1182 gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1183 }
1184 /* never drop events */
1185 return GST_PAD_PROBE_OK;
1186 }
1187
1188 /* called when we found a raw pad on the source element. We need to set up a
1189 * padprobe to detect EOS before exposing the pad. */
1190 static void
expose_decoded_pad(GstElement * element,GstPad * pad,GstURIDecodeBin * decoder)1191 expose_decoded_pad (GstElement * element, GstPad * pad,
1192 GstURIDecodeBin * decoder)
1193 {
1194 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1195 source_pad_event_probe, decoder, NULL);
1196
1197 new_decoded_pad_added_cb (element, pad, decoder);
1198 }
1199
1200 static void
pad_removed_cb(GstElement * element,GstPad * pad,GstURIDecodeBin * decoder)1201 pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
1202 {
1203 GstPad *ghost;
1204
1205 GST_DEBUG_OBJECT (element, "pad removed name: <%s:%s>",
1206 GST_DEBUG_PAD_NAME (pad));
1207
1208 /* we only care about srcpads */
1209 if (!GST_PAD_IS_SRC (pad))
1210 return;
1211
1212 if (!(ghost = g_object_get_data (G_OBJECT (pad), "uridecodebin.ghostpad")))
1213 goto no_ghost;
1214
1215 /* unghost the pad */
1216 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ghost), NULL);
1217
1218 /* deactivate and remove */
1219 gst_pad_set_active (pad, FALSE);
1220 gst_element_remove_pad (GST_ELEMENT_CAST (decoder), ghost);
1221
1222 return;
1223
1224 /* ERRORS */
1225 no_ghost:
1226 {
1227 GST_WARNING_OBJECT (element, "no ghost pad found");
1228 return;
1229 }
1230 }
1231
1232 /* helper function to lookup stuff in lists */
1233 static gboolean
array_has_value(const gchar * values[],const gchar * value)1234 array_has_value (const gchar * values[], const gchar * value)
1235 {
1236 gint i;
1237
1238 for (i = 0; values[i]; i++) {
1239 if (g_str_has_prefix (value, values[i]))
1240 return TRUE;
1241 }
1242 return FALSE;
1243 }
1244
1245 static gboolean
array_has_uri_value(const gchar * values[],const gchar * value)1246 array_has_uri_value (const gchar * values[], const gchar * value)
1247 {
1248 gint i;
1249
1250 for (i = 0; values[i]; i++) {
1251 if (!g_ascii_strncasecmp (value, values[i], strlen (values[i])))
1252 return TRUE;
1253 }
1254 return FALSE;
1255 }
1256
1257 /* list of URIs that we consider to be streams and that need buffering.
1258 * We have no mechanism yet to figure this out with a query. */
1259 static const gchar *stream_uris[] = { "http://", "https://", "mms://",
1260 "mmsh://", "mmsu://", "mmst://", "fd://", "myth://", "ssh://",
1261 "ftp://", "sftp://",
1262 NULL
1263 };
1264
1265 /* list of URIs that need a queue because they are pretty bursty */
1266 static const gchar *queue_uris[] = { "cdda://", NULL };
1267
1268 /* blacklisted URIs, we know they will always fail. */
1269 static const gchar *blacklisted_uris[] = { NULL };
1270
1271 /* media types that use adaptive streaming */
1272 static const gchar *adaptive_media[] = {
1273 "application/x-hls", "application/vnd.ms-sstr+xml",
1274 "application/dash+xml", NULL
1275 };
1276
1277 #define IS_STREAM_URI(uri) (array_has_uri_value (stream_uris, uri))
1278 #define IS_QUEUE_URI(uri) (array_has_uri_value (queue_uris, uri))
1279 #define IS_BLACKLISTED_URI(uri) (array_has_uri_value (blacklisted_uris, uri))
1280 #define IS_ADAPTIVE_MEDIA(media) (array_has_value (adaptive_media, media))
1281
1282 /*
1283 * Generate and configure a source element.
1284 */
1285 static GstElement *
gen_source_element(GstURIDecodeBin * decoder)1286 gen_source_element (GstURIDecodeBin * decoder)
1287 {
1288 GObjectClass *source_class;
1289 GstElement *source;
1290 GParamSpec *pspec;
1291 GstQuery *query;
1292 GstSchedulingFlags flags;
1293 GError *err = NULL;
1294
1295 if (!decoder->uri)
1296 goto no_uri;
1297
1298 GST_LOG_OBJECT (decoder, "finding source for %s", decoder->uri);
1299
1300 if (!gst_uri_is_valid (decoder->uri))
1301 goto invalid_uri;
1302
1303 if (IS_BLACKLISTED_URI (decoder->uri))
1304 goto uri_blacklisted;
1305
1306 source =
1307 gst_element_make_from_uri (GST_URI_SRC, decoder->uri, "source", &err);
1308 if (!source)
1309 goto no_source;
1310
1311 GST_LOG_OBJECT (decoder, "found source type %s", G_OBJECT_TYPE_NAME (source));
1312
1313 query = gst_query_new_scheduling ();
1314 if (gst_element_query (source, query)) {
1315 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
1316 decoder->is_stream = flags & GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED;
1317 } else
1318 decoder->is_stream = IS_STREAM_URI (decoder->uri);
1319 gst_query_unref (query);
1320
1321 GST_LOG_OBJECT (decoder, "source is stream: %d", decoder->is_stream);
1322
1323 decoder->need_queue = IS_QUEUE_URI (decoder->uri);
1324 GST_LOG_OBJECT (decoder, "source needs queue: %d", decoder->need_queue);
1325
1326 source_class = G_OBJECT_GET_CLASS (source);
1327
1328 pspec = g_object_class_find_property (source_class, "connection-speed");
1329 if (pspec != NULL) {
1330 guint64 speed = decoder->connection_speed / 1000;
1331 gboolean wrong_type = FALSE;
1332
1333 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
1334 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
1335
1336 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1337 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
1338 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
1339
1340 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1341 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
1342 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
1343
1344 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1345 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
1346 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
1347
1348 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1349 } else {
1350 GST_WARNING_OBJECT (decoder,
1351 "The connection speed property %" G_GUINT64_FORMAT
1352 " of type %s is not usefull not setting it", speed,
1353 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
1354 wrong_type = TRUE;
1355 }
1356
1357 if (!wrong_type) {
1358 g_object_set (source, "connection-speed", speed, NULL);
1359
1360 GST_DEBUG_OBJECT (decoder,
1361 "setting connection-speed=%" G_GUINT64_FORMAT " to source element",
1362 speed);
1363 }
1364 }
1365
1366 pspec = g_object_class_find_property (source_class, "subtitle-encoding");
1367 if (pspec != NULL && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
1368 GST_DEBUG_OBJECT (decoder,
1369 "setting subtitle-encoding=%s to source element", decoder->encoding);
1370 g_object_set (source, "subtitle-encoding", decoder->encoding, NULL);
1371 }
1372 return source;
1373
1374 /* ERRORS */
1375 no_uri:
1376 {
1377 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1378 (_("No URI specified to play from.")), (NULL));
1379 return NULL;
1380 }
1381 invalid_uri:
1382 {
1383 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1384 (_("Invalid URI \"%s\"."), decoder->uri), (NULL));
1385 g_clear_error (&err);
1386 return NULL;
1387 }
1388 uri_blacklisted:
1389 {
1390 GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
1391 (_("This stream type cannot be played yet.")), (NULL));
1392 return NULL;
1393 }
1394 no_source:
1395 {
1396 /* whoops, could not create the source element, dig a little deeper to
1397 * figure out what might be wrong. */
1398 if (err != NULL && err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL) {
1399 gchar *prot;
1400
1401 prot = gst_uri_get_protocol (decoder->uri);
1402 if (prot == NULL)
1403 goto invalid_uri;
1404
1405 gst_element_post_message (GST_ELEMENT_CAST (decoder),
1406 gst_missing_uri_source_message_new (GST_ELEMENT (decoder), prot));
1407
1408 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
1409 (_("No URI handler implemented for \"%s\"."), prot), (NULL));
1410
1411 g_free (prot);
1412 } else {
1413 GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1414 ("%s", (err) ? err->message : "URI was not accepted by any element"),
1415 ("No element accepted URI '%s'", decoder->uri));
1416 }
1417
1418 g_clear_error (&err);
1419 return NULL;
1420 }
1421 }
1422
1423 /**
1424 * has_all_raw_caps:
1425 * @pad: a #GstPad
1426 * @all_raw: pointer to hold the result
1427 *
1428 * check if the caps of the pad are all raw. The caps are all raw if
1429 * all of its structures contain audio/x-raw or video/x-raw.
1430 *
1431 * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
1432 */
1433 static gboolean
has_all_raw_caps(GstPad * pad,GstCaps * rawcaps,gboolean * all_raw)1434 has_all_raw_caps (GstPad * pad, GstCaps * rawcaps, gboolean * all_raw)
1435 {
1436 GstCaps *caps, *intersection;
1437 gint capssize;
1438 gboolean res = FALSE;
1439
1440 caps = gst_pad_query_caps (pad, NULL);
1441 if (caps == NULL)
1442 return FALSE;
1443
1444 GST_DEBUG_OBJECT (pad, "have caps %" GST_PTR_FORMAT, caps);
1445
1446 capssize = gst_caps_get_size (caps);
1447 /* no caps, skip and move to the next pad */
1448 if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1449 goto done;
1450
1451 intersection = gst_caps_intersect (caps, rawcaps);
1452 *all_raw = !gst_caps_is_empty (intersection)
1453 && (gst_caps_get_size (intersection) == capssize);
1454 gst_caps_unref (intersection);
1455
1456 res = TRUE;
1457
1458 done:
1459 gst_caps_unref (caps);
1460 return res;
1461 }
1462
1463 static void
post_missing_plugin_error(GstElement * dec,const gchar * element_name)1464 post_missing_plugin_error (GstElement * dec, const gchar * element_name)
1465 {
1466 GstMessage *msg;
1467
1468 msg = gst_missing_element_message_new (dec, element_name);
1469 gst_element_post_message (dec, msg);
1470
1471 GST_ELEMENT_ERROR (dec, CORE, MISSING_PLUGIN,
1472 (_("Missing element '%s' - check your GStreamer installation."),
1473 element_name), (NULL));
1474 do_async_done (GST_URI_DECODE_BIN (dec));
1475 }
1476
1477 /**
1478 * analyse_source:
1479 * @decoder: a #GstURIDecodeBin
1480 * @is_raw: are all pads raw data
1481 * @have_out: does the source have output
1482 * @is_dynamic: is this a dynamic source
1483 * @use_queue: put a queue before raw output pads
1484 *
1485 * Check the source of @decoder and collect information about it.
1486 *
1487 * @is_raw will be set to TRUE if the source only produces raw pads. When this
1488 * function returns, all of the raw pad of the source will be added
1489 * to @decoder.
1490 *
1491 * @have_out: will be set to TRUE if the source has output pads.
1492 *
1493 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1494 * on.
1495 *
1496 * Returns: FALSE if a fatal error occured while scanning.
1497 */
1498 static gboolean
analyse_source(GstURIDecodeBin * decoder,gboolean * is_raw,gboolean * have_out,gboolean * is_dynamic,gboolean use_queue)1499 analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
1500 gboolean * have_out, gboolean * is_dynamic, gboolean use_queue)
1501 {
1502 GstIterator *pads_iter;
1503 gboolean done = FALSE;
1504 gboolean res = TRUE;
1505 GstCaps *rawcaps;
1506 GstPad *pad;
1507 GValue item = { 0, };
1508
1509 *have_out = FALSE;
1510 *is_raw = FALSE;
1511 *is_dynamic = FALSE;
1512
1513 g_object_get (decoder, "caps", &rawcaps, NULL);
1514 if (!rawcaps)
1515 rawcaps = DEFAULT_CAPS;
1516
1517 pads_iter = gst_element_iterate_src_pads (decoder->source);
1518 while (!done) {
1519 switch (gst_iterator_next (pads_iter, &item)) {
1520 case GST_ITERATOR_ERROR:
1521 res = FALSE;
1522 /* FALLTROUGH */
1523 case GST_ITERATOR_DONE:
1524 done = TRUE;
1525 break;
1526 case GST_ITERATOR_RESYNC:
1527 /* reset results and resync */
1528 *have_out = FALSE;
1529 *is_raw = FALSE;
1530 *is_dynamic = FALSE;
1531 gst_iterator_resync (pads_iter);
1532 break;
1533 case GST_ITERATOR_OK:
1534 pad = g_value_dup_object (&item);
1535 /* we now officially have an ouput pad */
1536 *have_out = TRUE;
1537
1538 /* if FALSE, this pad has no caps and we continue with the next pad. */
1539 if (!has_all_raw_caps (pad, rawcaps, is_raw)) {
1540 gst_object_unref (pad);
1541 g_value_reset (&item);
1542 break;
1543 }
1544
1545 /* caps on source pad are all raw, we can add the pad */
1546 if (*is_raw) {
1547 GstElement *outelem;
1548
1549 if (use_queue) {
1550 GstPad *sinkpad;
1551
1552 /* insert a queue element right before the raw pad */
1553 outelem = gst_element_factory_make ("queue2", NULL);
1554 if (!outelem)
1555 goto no_queue2;
1556
1557 gst_bin_add (GST_BIN_CAST (decoder), outelem);
1558
1559 sinkpad = gst_element_get_static_pad (outelem, "sink");
1560 gst_pad_link (pad, sinkpad);
1561 gst_object_unref (sinkpad);
1562
1563 /* save queue pointer so we can remove it later */
1564 decoder->queue = outelem;
1565
1566 /* get the new raw srcpad */
1567 gst_object_unref (pad);
1568 pad = gst_element_get_static_pad (outelem, "src");
1569 } else {
1570 outelem = decoder->source;
1571 }
1572 expose_decoded_pad (outelem, pad, decoder);
1573 }
1574 gst_object_unref (pad);
1575 g_value_reset (&item);
1576 break;
1577 }
1578 }
1579 g_value_unset (&item);
1580 gst_iterator_free (pads_iter);
1581 gst_caps_unref (rawcaps);
1582
1583 if (!*have_out) {
1584 GstElementClass *elemclass;
1585 GList *walk;
1586
1587 /* element has no output pads, check for padtemplates that list SOMETIMES
1588 * pads. */
1589 elemclass = GST_ELEMENT_GET_CLASS (decoder->source);
1590
1591 walk = gst_element_class_get_pad_template_list (elemclass);
1592 while (walk != NULL) {
1593 GstPadTemplate *templ;
1594
1595 templ = (GstPadTemplate *) walk->data;
1596 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1597 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES)
1598 *is_dynamic = TRUE;
1599 break;
1600 }
1601 walk = g_list_next (walk);
1602 }
1603 }
1604
1605 return res;
1606 no_queue2:
1607 {
1608 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1609
1610 gst_object_unref (pad);
1611 g_value_unset (&item);
1612 gst_iterator_free (pads_iter);
1613 gst_caps_unref (rawcaps);
1614
1615 return FALSE;
1616 }
1617 }
1618
1619 /* Remove all decodebin from ourself
1620 * If force is FALSE, then the decodebin instances will be stored in
1621 * pending_decodebins for re-use later on.
1622 * If force is TRUE, then all decodebin instances will be unreferenced
1623 * and cleared, including the pending ones. */
1624 static void
remove_decoders(GstURIDecodeBin * bin,gboolean force)1625 remove_decoders (GstURIDecodeBin * bin, gboolean force)
1626 {
1627 GSList *walk;
1628
1629 for (walk = bin->decodebins; walk; walk = g_slist_next (walk)) {
1630 GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1631
1632 GST_DEBUG_OBJECT (bin, "removing old decoder element");
1633
1634 /* Even if we reuse this decodebin, the previous topology will
1635 * be irrelevant */
1636 g_object_set_data (G_OBJECT (decoder), "uridecodebin-topology", NULL);
1637
1638 if (force) {
1639 gst_element_set_state (decoder, GST_STATE_NULL);
1640 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1641 } else {
1642 GstCaps *caps;
1643
1644 gst_element_set_state (decoder, GST_STATE_READY);
1645 /* add it to our list of pending decodebins */
1646 g_object_ref (decoder);
1647 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1648 /* restore some properties we might have changed */
1649 g_object_set (decoder, "sink-caps", NULL, NULL);
1650 caps = DEFAULT_CAPS;
1651 g_object_set (decoder, "caps", caps, NULL);
1652 gst_caps_unref (caps);
1653 /* make it freshly floating again */
1654 g_object_force_floating (G_OBJECT (decoder));
1655
1656 bin->pending_decodebins =
1657 g_slist_prepend (bin->pending_decodebins, decoder);
1658 }
1659 }
1660 g_slist_free (bin->decodebins);
1661 bin->decodebins = NULL;
1662 if (force) {
1663 GSList *tmp;
1664
1665 for (tmp = bin->pending_decodebins; tmp; tmp = tmp->next) {
1666 gst_element_set_state ((GstElement *) tmp->data, GST_STATE_NULL);
1667 gst_object_unref ((GstElement *) tmp->data);
1668 }
1669 g_slist_free (bin->pending_decodebins);
1670 bin->pending_decodebins = NULL;
1671
1672 }
1673 }
1674
1675 static void
proxy_unknown_type_signal(GstElement * decodebin,GstPad * pad,GstCaps * caps,GstURIDecodeBin * dec)1676 proxy_unknown_type_signal (GstElement * decodebin, GstPad * pad, GstCaps * caps,
1677 GstURIDecodeBin * dec)
1678 {
1679 GST_DEBUG_OBJECT (dec, "unknown-type signaled");
1680
1681 g_signal_emit (dec,
1682 gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1683 }
1684
1685 static gboolean
proxy_autoplug_continue_signal(GstElement * decodebin,GstPad * pad,GstCaps * caps,GstURIDecodeBin * dec)1686 proxy_autoplug_continue_signal (GstElement * decodebin, GstPad * pad,
1687 GstCaps * caps, GstURIDecodeBin * dec)
1688 {
1689 gboolean result;
1690
1691 g_signal_emit (dec,
1692 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, pad, caps,
1693 &result);
1694
1695 GST_DEBUG_OBJECT (dec, "autoplug-continue returned %d", result);
1696
1697 return result;
1698 }
1699
1700 static GValueArray *
proxy_autoplug_factories_signal(GstElement * decodebin,GstPad * pad,GstCaps * caps,GstURIDecodeBin * dec)1701 proxy_autoplug_factories_signal (GstElement * decodebin, GstPad * pad,
1702 GstCaps * caps, GstURIDecodeBin * dec)
1703 {
1704 GValueArray *result;
1705
1706 g_signal_emit (dec,
1707 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, pad, caps,
1708 &result);
1709
1710 GST_DEBUG_OBJECT (dec, "autoplug-factories returned %p", result);
1711
1712 return result;
1713 }
1714
1715 static GValueArray *
proxy_autoplug_sort_signal(GstElement * decodebin,GstPad * pad,GstCaps * caps,GValueArray * factories,GstURIDecodeBin * dec)1716 proxy_autoplug_sort_signal (GstElement * decodebin, GstPad * pad,
1717 GstCaps * caps, GValueArray * factories, GstURIDecodeBin * dec)
1718 {
1719 GValueArray *result;
1720
1721 g_signal_emit (dec,
1722 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, pad, caps,
1723 factories, &result);
1724
1725 GST_DEBUG_OBJECT (dec, "autoplug-sort returned %p", result);
1726
1727 return result;
1728 }
1729
1730 static GstAutoplugSelectResult
proxy_autoplug_select_signal(GstElement * decodebin,GstPad * pad,GstCaps * caps,GstElementFactory * factory,GstURIDecodeBin * dec)1731 proxy_autoplug_select_signal (GstElement * decodebin, GstPad * pad,
1732 GstCaps * caps, GstElementFactory * factory, GstURIDecodeBin * dec)
1733 {
1734 GstAutoplugSelectResult result;
1735
1736 g_signal_emit (dec,
1737 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, factory,
1738 &result);
1739
1740 GST_DEBUG_OBJECT (dec, "autoplug-select returned %d", result);
1741
1742 return result;
1743 }
1744
1745 static gboolean
proxy_autoplug_query_signal(GstElement * decodebin,GstPad * pad,GstElement * element,GstQuery * query,GstURIDecodeBin * dec)1746 proxy_autoplug_query_signal (GstElement * decodebin, GstPad * pad,
1747 GstElement * element, GstQuery * query, GstURIDecodeBin * dec)
1748 {
1749 gboolean ret = FALSE;
1750
1751 g_signal_emit (dec,
1752 gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY], 0, pad, element, query,
1753 &ret);
1754
1755 GST_DEBUG_OBJECT (dec, "autoplug-query returned %d", ret);
1756
1757 return ret;
1758 }
1759
1760 static void
proxy_drained_signal(GstElement * decodebin,GstURIDecodeBin * dec)1761 proxy_drained_signal (GstElement * decodebin, GstURIDecodeBin * dec)
1762 {
1763 GST_DEBUG_OBJECT (dec, "drained signaled");
1764
1765 g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1766 }
1767
1768 /* make a decodebin and connect to all the signals */
1769 static GstElement *
make_decoder(GstURIDecodeBin * decoder)1770 make_decoder (GstURIDecodeBin * decoder)
1771 {
1772 GstElement *decodebin;
1773
1774 /* re-use pending decodebin */
1775 if (decoder->pending_decodebins) {
1776 GSList *first = decoder->pending_decodebins;
1777 GST_LOG_OBJECT (decoder, "re-using pending decodebin");
1778 decodebin = (GstElement *) first->data;
1779 decoder->pending_decodebins =
1780 g_slist_delete_link (decoder->pending_decodebins, first);
1781 } else {
1782 GST_LOG_OBJECT (decoder, "making new decodebin");
1783
1784 /* now create the decoder element */
1785 decodebin = gst_element_factory_make ("decodebin", NULL);
1786
1787 if (!decodebin)
1788 goto no_decodebin;
1789
1790 /* sanity check */
1791 if (decodebin->numsinkpads == 0)
1792 goto no_typefind;
1793
1794 /* connect signals to proxy */
1795 g_signal_connect (decodebin, "unknown-type",
1796 G_CALLBACK (proxy_unknown_type_signal), decoder);
1797 g_signal_connect (decodebin, "autoplug-continue",
1798 G_CALLBACK (proxy_autoplug_continue_signal), decoder);
1799 g_signal_connect (decodebin, "autoplug-factories",
1800 G_CALLBACK (proxy_autoplug_factories_signal), decoder);
1801 g_signal_connect (decodebin, "autoplug-sort",
1802 G_CALLBACK (proxy_autoplug_sort_signal), decoder);
1803 g_signal_connect (decodebin, "autoplug-select",
1804 G_CALLBACK (proxy_autoplug_select_signal), decoder);
1805 g_signal_connect (decodebin, "autoplug-query",
1806 G_CALLBACK (proxy_autoplug_query_signal), decoder);
1807 g_signal_connect (decodebin, "drained",
1808 G_CALLBACK (proxy_drained_signal), decoder);
1809
1810 /* set up callbacks to create the links between decoded data
1811 * and video/audio/subtitle rendering/output. */
1812 g_signal_connect (decodebin,
1813 "pad-added", G_CALLBACK (new_decoded_pad_added_cb), decoder);
1814 g_signal_connect (decodebin,
1815 "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
1816 g_signal_connect (decodebin, "no-more-pads",
1817 G_CALLBACK (no_more_pads), decoder);
1818 g_signal_connect (decodebin,
1819 "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
1820 }
1821
1822 /* configure caps if we have any */
1823 if (decoder->caps)
1824 g_object_set (decodebin, "caps", decoder->caps, NULL);
1825
1826 /* Propagate expose-all-streams and connection-speed properties */
1827 g_object_set (decodebin, "expose-all-streams", decoder->expose_allstreams,
1828 "connection-speed", decoder->connection_speed / 1000, NULL);
1829
1830 if (!decoder->is_stream || decoder->is_adaptive) {
1831 /* propagate the use-buffering property but only when we are not already
1832 * doing stream buffering with queue2. FIXME, we might want to do stream
1833 * buffering with the multiqueue buffering instead of queue2. */
1834 g_object_set (decodebin, "use-buffering", decoder->use_buffering
1835 || decoder->is_adaptive, NULL);
1836
1837 if (decoder->use_buffering || decoder->is_adaptive) {
1838 guint max_bytes;
1839 guint64 max_time;
1840
1841 /* configure sizes when buffering */
1842 if ((max_bytes = decoder->buffer_size) == -1)
1843 max_bytes = 2 * 1024 * 1024;
1844 if ((max_time = decoder->buffer_duration) == -1)
1845 max_time = 5 * GST_SECOND;
1846
1847 g_object_set (decodebin, "max-size-bytes", max_bytes, "max-size-buffers",
1848 (guint) 0, "max-size-time", max_time, NULL);
1849 }
1850 }
1851
1852 g_object_set_data (G_OBJECT (decodebin), "pending", GINT_TO_POINTER (1));
1853 g_object_set (decodebin, "subtitle-encoding", decoder->encoding, NULL);
1854 decoder->pending++;
1855 GST_LOG_OBJECT (decoder, "have %d pending dynamic objects", decoder->pending);
1856
1857 gst_bin_add (GST_BIN_CAST (decoder), decodebin);
1858
1859 decoder->decodebins = g_slist_prepend (decoder->decodebins, decodebin);
1860
1861 return decodebin;
1862
1863 /* ERRORS */
1864 no_decodebin:
1865 {
1866 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "decodebin");
1867 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1868 ("No decodebin element, check your installation"));
1869 do_async_done (decoder);
1870 return NULL;
1871 }
1872 no_typefind:
1873 {
1874 gst_object_unref (decodebin);
1875 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1876 ("No typefind element, decodebin is unusable, check your installation"));
1877 do_async_done (decoder);
1878 return NULL;
1879 }
1880 }
1881
1882 /* signaled when we have a stream and we need to configure the download
1883 * buffering or regular buffering */
1884 static void
type_found(GstElement * typefind,guint probability,GstCaps * caps,GstURIDecodeBin * decoder)1885 type_found (GstElement * typefind, guint probability,
1886 GstCaps * caps, GstURIDecodeBin * decoder)
1887 {
1888 GstElement *src_elem, *dec_elem, *queue = NULL;
1889 GstStructure *s;
1890 const gchar *media_type, *elem_name;
1891 gboolean do_download = FALSE;
1892
1893 GST_DEBUG_OBJECT (decoder, "typefind found caps %" GST_PTR_FORMAT, caps);
1894
1895 s = gst_caps_get_structure (caps, 0);
1896 media_type = gst_structure_get_name (s);
1897
1898 decoder->is_adaptive = IS_ADAPTIVE_MEDIA (media_type);
1899
1900 /* only enable download buffering if the upstream duration is known */
1901 if (decoder->download) {
1902 gint64 dur;
1903
1904 do_download = (gst_element_query_duration (typefind, GST_FORMAT_BYTES, &dur)
1905 && dur != -1);
1906 }
1907
1908 dec_elem = make_decoder (decoder);
1909 if (!dec_elem)
1910 goto no_decodebin;
1911
1912 if (decoder->is_adaptive) {
1913 src_elem = typefind;
1914 } else {
1915 if (do_download) {
1916 elem_name = "downloadbuffer";
1917 } else {
1918 elem_name = "queue2";
1919 }
1920 queue = gst_element_factory_make (elem_name, NULL);
1921 if (!queue)
1922 goto no_buffer_element;
1923
1924 decoder->queue = queue;
1925
1926 GST_DEBUG_OBJECT (decoder, "check media-type %s, %d", media_type,
1927 do_download);
1928
1929 if (do_download) {
1930 gchar *temp_template, *filename;
1931 const gchar *tmp_dir, *prgname;
1932
1933 tmp_dir = g_get_user_cache_dir ();
1934 prgname = g_get_prgname ();
1935 if (prgname == NULL)
1936 prgname = "GStreamer";
1937
1938 filename = g_strdup_printf ("%s-XXXXXX", prgname);
1939
1940 /* build our filename */
1941 temp_template = g_build_filename (tmp_dir, filename, NULL);
1942
1943 GST_DEBUG_OBJECT (decoder, "enable download buffering in %s (%s, %s, %s)",
1944 temp_template, tmp_dir, prgname, filename);
1945
1946 /* configure progressive download for selected media types */
1947 g_object_set (queue, "temp-template", temp_template, NULL);
1948
1949 g_free (filename);
1950 g_free (temp_template);
1951 } else {
1952 g_object_set (queue, "use-buffering", TRUE, NULL);
1953 g_object_set (queue, "ring-buffer-max-size",
1954 decoder->ring_buffer_max_size, NULL);
1955 /* Disable max-size-buffers */
1956 g_object_set (queue, "max-size-buffers", 0, NULL);
1957 }
1958
1959 /* If buffer size or duration are set, set them on the element */
1960 if (decoder->buffer_size != -1)
1961 g_object_set (queue, "max-size-bytes", decoder->buffer_size, NULL);
1962 if (decoder->buffer_duration != -1)
1963 g_object_set (queue, "max-size-time", decoder->buffer_duration, NULL);
1964
1965 gst_bin_add (GST_BIN_CAST (decoder), queue);
1966
1967 if (!gst_element_link_pads (typefind, "src", queue, "sink"))
1968 goto could_not_link;
1969 src_elem = queue;
1970 }
1971
1972 /* to force caps on the decodebin element and avoid reparsing stuff by
1973 * typefind. It also avoids a deadlock in the way typefind activates pads in
1974 * the state change */
1975 g_object_set (dec_elem, "sink-caps", caps, NULL);
1976
1977 if (!gst_element_link_pads (src_elem, "src", dec_elem, "sink"))
1978 goto could_not_link;
1979
1980 /* PLAYING in one go might fail (see bug #632782) */
1981 gst_element_set_state (dec_elem, GST_STATE_PAUSED);
1982 gst_element_sync_state_with_parent (dec_elem);
1983 if (queue)
1984 gst_element_sync_state_with_parent (queue);
1985
1986 return;
1987
1988 /* ERRORS */
1989 no_decodebin:
1990 {
1991 /* error was posted */
1992 return;
1993 }
1994 could_not_link:
1995 {
1996 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1997 (NULL), ("Can't link typefind to decodebin element"));
1998 do_async_done (decoder);
1999 return;
2000 }
2001 no_buffer_element:
2002 {
2003 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), elem_name);
2004 return;
2005 }
2006 }
2007
2008 /* setup a streaming source. This will first plug a typefind element to the
2009 * source. After we find the type, we decide to plug a queue2 and continue to
2010 * plug a decodebin starting from the found caps */
2011 static gboolean
setup_streaming(GstURIDecodeBin * decoder)2012 setup_streaming (GstURIDecodeBin * decoder)
2013 {
2014 GstElement *typefind;
2015
2016 /* now create the decoder element */
2017 typefind = gst_element_factory_make ("typefind", NULL);
2018 if (!typefind)
2019 goto no_typefind;
2020
2021 gst_bin_add (GST_BIN_CAST (decoder), typefind);
2022
2023 if (!gst_element_link_pads (decoder->source, NULL, typefind, "sink"))
2024 goto could_not_link;
2025
2026 decoder->typefind = typefind;
2027
2028 /* connect a signal to find out when the typefind element found
2029 * a type */
2030 decoder->have_type_id =
2031 g_signal_connect (decoder->typefind, "have-type",
2032 G_CALLBACK (type_found), decoder);
2033
2034 return TRUE;
2035
2036 /* ERRORS */
2037 no_typefind:
2038 {
2039 post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "typefind");
2040 GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
2041 ("No typefind element, check your installation"));
2042 do_async_done (decoder);
2043 return FALSE;
2044 }
2045 could_not_link:
2046 {
2047 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2048 (NULL), ("Can't link source to typefind element"));
2049 gst_bin_remove (GST_BIN_CAST (decoder), typefind);
2050 do_async_done (decoder);
2051 return FALSE;
2052 }
2053 }
2054
2055 static void
free_stream(gpointer value)2056 free_stream (gpointer value)
2057 {
2058 g_slice_free (GstURIDecodeBinStream, value);
2059 }
2060
2061 /* remove source and all related elements */
2062 static void
remove_source(GstURIDecodeBin * bin)2063 remove_source (GstURIDecodeBin * bin)
2064 {
2065 GstElement *source = bin->source;
2066
2067 if (source) {
2068 GST_DEBUG_OBJECT (bin, "removing old src element");
2069 gst_element_set_state (source, GST_STATE_NULL);
2070
2071 if (bin->src_np_sig_id) {
2072 g_signal_handler_disconnect (source, bin->src_np_sig_id);
2073 bin->src_np_sig_id = 0;
2074 }
2075 if (bin->src_nmp_sig_id) {
2076 g_signal_handler_disconnect (source, bin->src_nmp_sig_id);
2077 bin->src_nmp_sig_id = 0;
2078 }
2079 GST_OBJECT_LOCK (bin);
2080 bin->source = NULL;
2081 GST_OBJECT_UNLOCK (bin);
2082 gst_bin_remove (GST_BIN_CAST (bin), source);
2083 }
2084 if (bin->queue) {
2085 GST_DEBUG_OBJECT (bin, "removing old queue element");
2086 gst_element_set_state (bin->queue, GST_STATE_NULL);
2087 gst_bin_remove (GST_BIN_CAST (bin), bin->queue);
2088 bin->queue = NULL;
2089 }
2090 if (bin->typefind) {
2091 GST_DEBUG_OBJECT (bin, "removing old typefind element");
2092 gst_element_set_state (bin->typefind, GST_STATE_NULL);
2093 gst_bin_remove (GST_BIN_CAST (bin), bin->typefind);
2094 bin->typefind = NULL;
2095 }
2096 if (bin->streams) {
2097 g_hash_table_destroy (bin->streams);
2098 bin->streams = NULL;
2099 }
2100 }
2101
2102 /* is called when a dynamic source element created a new pad. */
2103 static void
source_new_pad(GstElement * element,GstPad * pad,GstURIDecodeBin * bin)2104 source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
2105 {
2106 GstElement *decoder;
2107 gboolean is_raw;
2108 GstCaps *rawcaps;
2109
2110 GST_URI_DECODE_BIN_LOCK (bin);
2111 GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
2112 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
2113
2114 g_object_get (bin, "caps", &rawcaps, NULL);
2115 if (!rawcaps)
2116 rawcaps = DEFAULT_CAPS;
2117
2118 /* if this is a pad with all raw caps, we can expose it */
2119 if (has_all_raw_caps (pad, rawcaps, &is_raw) && is_raw) {
2120 /* it's all raw, create output pads. */
2121 GST_URI_DECODE_BIN_UNLOCK (bin);
2122 gst_caps_unref (rawcaps);
2123 expose_decoded_pad (element, pad, bin);
2124 return;
2125 }
2126 gst_caps_unref (rawcaps);
2127
2128 /* not raw, create decoder */
2129 decoder = make_decoder (bin);
2130 if (!decoder)
2131 goto no_decodebin;
2132
2133 /* and link to decoder */
2134 if (!gst_element_link_pads (bin->source, NULL, decoder, "sink"))
2135 goto could_not_link;
2136
2137 GST_DEBUG_OBJECT (bin, "linked decoder to new pad");
2138
2139 gst_element_sync_state_with_parent (decoder);
2140 GST_URI_DECODE_BIN_UNLOCK (bin);
2141
2142 return;
2143
2144 /* ERRORS */
2145 no_decodebin:
2146 {
2147 /* error was posted */
2148 GST_URI_DECODE_BIN_UNLOCK (bin);
2149 return;
2150 }
2151 could_not_link:
2152 {
2153 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
2154 (NULL), ("Can't link source to decoder element"));
2155 GST_URI_DECODE_BIN_UNLOCK (bin);
2156 do_async_done (bin);
2157 return;
2158 }
2159 }
2160
2161 static gboolean
is_live_source(GstElement * source)2162 is_live_source (GstElement * source)
2163 {
2164 GObjectClass *source_class = NULL;
2165 gboolean is_live = FALSE;
2166 GParamSpec *pspec;
2167
2168 source_class = G_OBJECT_GET_CLASS (source);
2169 pspec = g_object_class_find_property (source_class, "is-live");
2170 if (!pspec || G_PARAM_SPEC_VALUE_TYPE (pspec) != G_TYPE_BOOLEAN)
2171 return FALSE;
2172
2173 g_object_get (G_OBJECT (source), "is-live", &is_live, NULL);
2174
2175 return is_live;
2176 }
2177
2178 /* construct and run the source and decoder elements until we found
2179 * all the streams or until a preroll queue has been filled.
2180 */
2181 static gboolean
setup_source(GstURIDecodeBin * decoder)2182 setup_source (GstURIDecodeBin * decoder)
2183 {
2184 gboolean is_raw, have_out, is_dynamic;
2185 GstElement *source;
2186
2187 GST_DEBUG_OBJECT (decoder, "setup source");
2188
2189 /* delete old src */
2190 remove_source (decoder);
2191
2192 decoder->pending = 0;
2193
2194 /* create and configure an element that can handle the uri */
2195 source = gen_source_element (decoder);
2196 GST_OBJECT_LOCK (decoder);
2197 if (!(decoder->source = source)) {
2198 GST_OBJECT_UNLOCK (decoder);
2199
2200 goto no_source;
2201 }
2202 GST_OBJECT_UNLOCK (decoder);
2203
2204 /* state will be merged later - if file is not found, error will be
2205 * handled by the application right after. */
2206 gst_bin_add (GST_BIN_CAST (decoder), decoder->source);
2207
2208 /* notify of the new source used */
2209 g_object_notify (G_OBJECT (decoder), "source");
2210
2211 g_signal_emit (decoder, gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP],
2212 0, decoder->source);
2213
2214 if (is_live_source (decoder->source))
2215 decoder->is_stream = FALSE;
2216
2217 /* remove the old decoders now, if any */
2218 remove_decoders (decoder, FALSE);
2219
2220 /* stream admin setup */
2221 decoder->streams = g_hash_table_new_full (NULL, NULL, NULL, free_stream);
2222
2223 /* see if the source element emits raw audio/video all by itself,
2224 * if so, we can create streams for the pads and be done with it.
2225 * Also check that is has source pads, if not, we assume it will
2226 * do everything itself. */
2227 if (!analyse_source (decoder, &is_raw, &have_out, &is_dynamic,
2228 decoder->need_queue))
2229 goto invalid_source;
2230
2231 if (is_raw) {
2232 GST_DEBUG_OBJECT (decoder, "Source provides all raw data");
2233 /* source provides raw data, we added the pads and we can now signal a
2234 * no_more pads because we are done. */
2235 gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
2236 do_async_done (decoder);
2237 return TRUE;
2238 }
2239 if (!have_out && !is_dynamic) {
2240 GST_DEBUG_OBJECT (decoder, "Source has no output pads");
2241 /* create a stream to indicate that this uri is handled by a self
2242 * contained element. We are now done. */
2243 add_element_stream (decoder->source, decoder);
2244 return TRUE;
2245 }
2246 if (is_dynamic) {
2247 GST_DEBUG_OBJECT (decoder, "Source has dynamic output pads");
2248 /* connect a handler for the new-pad signal */
2249 decoder->src_np_sig_id =
2250 g_signal_connect (decoder->source, "pad-added",
2251 G_CALLBACK (source_new_pad), decoder);
2252 decoder->src_nmp_sig_id =
2253 g_signal_connect (decoder->source, "no-more-pads",
2254 G_CALLBACK (source_no_more_pads), decoder);
2255 g_object_set_data (G_OBJECT (decoder->source), "pending",
2256 GINT_TO_POINTER (1));
2257 decoder->pending++;
2258 } else {
2259 if (decoder->is_stream) {
2260 GST_DEBUG_OBJECT (decoder, "Setting up streaming");
2261 /* do the stream things here */
2262 if (!setup_streaming (decoder))
2263 goto streaming_failed;
2264 } else {
2265 GstElement *dec_elem;
2266
2267 /* no streaming source, we can link now */
2268 GST_DEBUG_OBJECT (decoder, "Plugging decodebin to source");
2269
2270 dec_elem = make_decoder (decoder);
2271 if (!dec_elem)
2272 goto no_decoder;
2273
2274 if (!gst_element_link_pads (decoder->source, NULL, dec_elem, "sink"))
2275 goto could_not_link;
2276 }
2277 }
2278 return TRUE;
2279
2280 /* ERRORS */
2281 no_source:
2282 {
2283 /* error message was already posted */
2284 return FALSE;
2285 }
2286 invalid_source:
2287 {
2288 GST_ELEMENT_ERROR (decoder, CORE, FAILED,
2289 (_("Source element is invalid.")), (NULL));
2290 return FALSE;
2291 }
2292 no_decoder:
2293 {
2294 /* message was posted */
2295 return FALSE;
2296 }
2297 streaming_failed:
2298 {
2299 /* message was posted */
2300 return FALSE;
2301 }
2302 could_not_link:
2303 {
2304 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2305 (NULL), ("Can't link source to decoder element"));
2306 return FALSE;
2307 }
2308 }
2309
2310 static void
value_list_append_structure_list(GValue * list_val,GstStructure ** first,GList * structure_list)2311 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
2312 GList * structure_list)
2313 {
2314 GList *l;
2315
2316 for (l = structure_list; l != NULL; l = l->next) {
2317 GValue val = { 0, };
2318
2319 if (*first == NULL)
2320 *first = gst_structure_copy ((GstStructure *) l->data);
2321
2322 g_value_init (&val, GST_TYPE_STRUCTURE);
2323 g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
2324 gst_value_list_append_value (list_val, &val);
2325 g_value_unset (&val);
2326 }
2327 }
2328
2329 /* if it's a redirect message with multiple redirect locations we might
2330 * want to pick a different 'best' location depending on the required
2331 * bitrates and the connection speed */
2332 static GstMessage *
handle_redirect_message(GstURIDecodeBin * dec,GstMessage * msg)2333 handle_redirect_message (GstURIDecodeBin * dec, GstMessage * msg)
2334 {
2335 const GValue *locations_list, *location_val;
2336 GstMessage *new_msg;
2337 GstStructure *new_structure = NULL;
2338 GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
2339 GValue new_list = { 0, };
2340 guint size, i;
2341 const GstStructure *structure;
2342
2343 GST_DEBUG_OBJECT (dec, "redirect message: %" GST_PTR_FORMAT, msg);
2344 GST_DEBUG_OBJECT (dec, "connection speed: %" G_GUINT64_FORMAT,
2345 dec->connection_speed);
2346
2347 structure = gst_message_get_structure (msg);
2348 if (dec->connection_speed == 0 || structure == NULL)
2349 return msg;
2350
2351 locations_list = gst_structure_get_value (structure, "locations");
2352 if (locations_list == NULL)
2353 return msg;
2354
2355 size = gst_value_list_get_size (locations_list);
2356 if (size < 2)
2357 return msg;
2358
2359 /* maintain existing order as much as possible, just sort references
2360 * with too high a bitrate to the end (the assumption being that if
2361 * bitrates are given they are given for all interesting streams and
2362 * that the you-need-at-least-version-xyz redirect has the same bitrate
2363 * as the lowest referenced redirect alternative) */
2364 for (i = 0; i < size; ++i) {
2365 const GstStructure *s;
2366 gint bitrate = 0;
2367
2368 location_val = gst_value_list_get_value (locations_list, i);
2369 s = (const GstStructure *) g_value_get_boxed (location_val);
2370 if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
2371 GST_DEBUG_OBJECT (dec, "no bitrate: %" GST_PTR_FORMAT, s);
2372 l_neutral = g_list_append (l_neutral, (gpointer) s);
2373 } else if (bitrate > dec->connection_speed) {
2374 GST_DEBUG_OBJECT (dec, "bitrate too high: %" GST_PTR_FORMAT, s);
2375 l_bad = g_list_append (l_bad, (gpointer) s);
2376 } else if (bitrate <= dec->connection_speed) {
2377 GST_DEBUG_OBJECT (dec, "bitrate OK: %" GST_PTR_FORMAT, s);
2378 l_good = g_list_append (l_good, (gpointer) s);
2379 }
2380 }
2381
2382 g_value_init (&new_list, GST_TYPE_LIST);
2383 value_list_append_structure_list (&new_list, &new_structure, l_good);
2384 value_list_append_structure_list (&new_list, &new_structure, l_neutral);
2385 value_list_append_structure_list (&new_list, &new_structure, l_bad);
2386 gst_structure_take_value (new_structure, "locations", &new_list);
2387
2388 g_list_free (l_good);
2389 g_list_free (l_neutral);
2390 g_list_free (l_bad);
2391
2392 new_msg = gst_message_new_element (msg->src, new_structure);
2393 gst_message_unref (msg);
2394
2395 GST_DEBUG_OBJECT (dec, "new redirect message: %" GST_PTR_FORMAT, new_msg);
2396 return new_msg;
2397 }
2398
2399 static GstMessage *
make_topology_message(GstURIDecodeBin * dec)2400 make_topology_message (GstURIDecodeBin * dec)
2401 {
2402 GSList *tmp;
2403 GstStructure *aggregated_topology = NULL;
2404 GValue list = G_VALUE_INIT;
2405 GstCaps *caps = NULL;
2406 gchar *name, *proto;
2407
2408 aggregated_topology = gst_structure_new_empty ("stream-topology");
2409 g_value_init (&list, GST_TYPE_LIST);
2410
2411 for (tmp = dec->decodebins; tmp; tmp = tmp->next) {
2412 GValue item = G_VALUE_INIT;
2413 GstStructure *dec_topology =
2414 g_object_get_data (G_OBJECT (tmp->data), "uridecodebin-topology");
2415
2416 g_value_init (&item, GST_TYPE_STRUCTURE);
2417 gst_value_set_structure (&item, dec_topology);
2418 gst_value_list_append_and_take_value (&list, &item);
2419 }
2420
2421 gst_structure_take_value (aggregated_topology, "next", &list);
2422
2423 /* This is a bit wacky, but that's the best way I can find to express
2424 * uridecodebin 'caps' as subsequently shown by gst-discoverer */
2425 proto = gst_uri_get_protocol (dec->uri);
2426 name = g_strdup_printf ("application/%s", proto);
2427 g_free (proto);
2428
2429 caps = gst_caps_new_empty_simple (name);
2430 g_free (name);
2431
2432 gst_structure_set (aggregated_topology, "caps", GST_TYPE_CAPS, caps, NULL);
2433 gst_caps_unref (caps);
2434
2435 return gst_message_new_element (GST_OBJECT (dec), aggregated_topology);
2436 }
2437
2438 static void
check_topology(gpointer data,gpointer user_data)2439 check_topology (gpointer data, gpointer user_data)
2440 {
2441 gboolean *has_topo = user_data;
2442
2443 if (g_object_get_data (data, "uridecodebin-topology") == NULL)
2444 *has_topo = FALSE;
2445 }
2446
2447 static void
handle_message(GstBin * bin,GstMessage * msg)2448 handle_message (GstBin * bin, GstMessage * msg)
2449 {
2450 GstURIDecodeBin *dec = GST_URI_DECODE_BIN (bin);
2451
2452 switch (GST_MESSAGE_TYPE (msg)) {
2453 case GST_MESSAGE_ELEMENT:{
2454
2455 if (gst_message_has_name (msg, "stream-topology")) {
2456 GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (msg));
2457 gboolean has_all_topo = TRUE;
2458
2459 if (dec->pending || (dec->decodebins && dec->decodebins->next != NULL)) {
2460 const GstStructure *structure;
2461
2462 /* If there is only one, just let it through, so this case is if
2463 * there is more than one.
2464 */
2465
2466 structure = gst_message_get_structure (msg);
2467
2468 g_object_set_data_full (G_OBJECT (element), "uridecodebin-topology",
2469 gst_structure_copy (structure),
2470 (GDestroyNotify) gst_structure_free);
2471
2472 gst_message_unref (msg);
2473 msg = NULL;
2474
2475 g_slist_foreach (dec->decodebins, check_topology, &has_all_topo);
2476 if (has_all_topo)
2477 msg = make_topology_message (dec);
2478 }
2479 } else if (gst_message_has_name (msg, "redirect")) {
2480 /* sort redirect messages based on the connection speed. This simplifies
2481 * the user of this element as it can in most cases just pick the first item
2482 * of the sorted list as a good redirection candidate. It can of course
2483 * choose something else from the list if it has a better way. */
2484 msg = handle_redirect_message (dec, msg);
2485 }
2486 break;
2487 }
2488 case GST_MESSAGE_ERROR:{
2489 GError *err = NULL;
2490
2491 /* Filter out missing plugin error messages from the decodebins. Only if
2492 * all decodebins exposed no streams we will report a missing plugin
2493 * error from no_more_pads_full()
2494 */
2495 gst_message_parse_error (msg, &err, NULL);
2496 if (g_error_matches (err, GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN)
2497 || g_error_matches (err, GST_STREAM_ERROR,
2498 GST_STREAM_ERROR_CODEC_NOT_FOUND)) {
2499 dec->missing_plugin_errors =
2500 g_list_prepend (dec->missing_plugin_errors, gst_message_ref (msg));
2501
2502 no_more_pads_full (GST_ELEMENT (GST_MESSAGE_SRC (msg)), FALSE,
2503 GST_URI_DECODE_BIN (bin));
2504 gst_message_unref (msg);
2505 msg = NULL;
2506 }
2507 g_clear_error (&err);
2508 break;
2509 }
2510 default:
2511 break;
2512 }
2513
2514 if (msg)
2515 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
2516 }
2517
2518 /* generic struct passed to all query fold methods
2519 * FIXME, move to core.
2520 */
2521 typedef struct
2522 {
2523 GstQuery *query;
2524 gint64 min;
2525 gint64 max;
2526 gboolean seekable;
2527 gboolean live;
2528 } QueryFold;
2529
2530 typedef void (*QueryInitFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2531 typedef void (*QueryDoneFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2532
2533 /* for duration/position we collect all durations/positions and take
2534 * the MAX of all valid results */
2535 static void
decoder_query_init(GstURIDecodeBin * dec,QueryFold * fold)2536 decoder_query_init (GstURIDecodeBin * dec, QueryFold * fold)
2537 {
2538 fold->min = 0;
2539 fold->max = -1;
2540 fold->seekable = TRUE;
2541 fold->live = 0;
2542 }
2543
2544 static gboolean
decoder_query_duration_fold(const GValue * item,GValue * ret,QueryFold * fold)2545 decoder_query_duration_fold (const GValue * item, GValue * ret,
2546 QueryFold * fold)
2547 {
2548 GstPad *pad = g_value_get_object (item);
2549
2550 if (gst_pad_query (pad, fold->query)) {
2551 gint64 duration;
2552
2553 g_value_set_boolean (ret, TRUE);
2554
2555 gst_query_parse_duration (fold->query, NULL, &duration);
2556
2557 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
2558
2559 if (duration > fold->max)
2560 fold->max = duration;
2561 }
2562 return TRUE;
2563 }
2564
2565 static void
decoder_query_duration_done(GstURIDecodeBin * dec,QueryFold * fold)2566 decoder_query_duration_done (GstURIDecodeBin * dec, QueryFold * fold)
2567 {
2568 GstFormat format;
2569
2570 gst_query_parse_duration (fold->query, &format, NULL);
2571 /* store max in query result */
2572 gst_query_set_duration (fold->query, format, fold->max);
2573
2574 GST_DEBUG ("max duration %" G_GINT64_FORMAT, fold->max);
2575 }
2576
2577 static gboolean
decoder_query_position_fold(const GValue * item,GValue * ret,QueryFold * fold)2578 decoder_query_position_fold (const GValue * item, GValue * ret,
2579 QueryFold * fold)
2580 {
2581 GstPad *pad = g_value_get_object (item);
2582
2583 if (gst_pad_query (pad, fold->query)) {
2584 gint64 position;
2585
2586 g_value_set_boolean (ret, TRUE);
2587
2588 gst_query_parse_position (fold->query, NULL, &position);
2589
2590 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
2591
2592 if (position > fold->max)
2593 fold->max = position;
2594 }
2595
2596 return TRUE;
2597 }
2598
2599 static void
decoder_query_position_done(GstURIDecodeBin * dec,QueryFold * fold)2600 decoder_query_position_done (GstURIDecodeBin * dec, QueryFold * fold)
2601 {
2602 GstFormat format;
2603
2604 gst_query_parse_position (fold->query, &format, NULL);
2605 /* store max in query result */
2606 gst_query_set_position (fold->query, format, fold->max);
2607
2608 GST_DEBUG_OBJECT (dec, "max position %" G_GINT64_FORMAT, fold->max);
2609 }
2610
2611 static gboolean
decoder_query_latency_fold(const GValue * item,GValue * ret,QueryFold * fold)2612 decoder_query_latency_fold (const GValue * item, GValue * ret, QueryFold * fold)
2613 {
2614 GstPad *pad = g_value_get_object (item);
2615
2616 if (gst_pad_query (pad, fold->query)) {
2617 GstClockTime min, max;
2618 gboolean live;
2619
2620 gst_query_parse_latency (fold->query, &live, &min, &max);
2621
2622 GST_DEBUG_OBJECT (pad,
2623 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2624 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
2625
2626 if (live) {
2627 /* for the combined latency we collect the MAX of all min latencies and
2628 * the MIN of all max latencies */
2629 if (min > fold->min)
2630 fold->min = min;
2631 if (fold->max == -1)
2632 fold->max = max;
2633 else if (max < fold->max)
2634 fold->max = max;
2635
2636 fold->live = TRUE;
2637 }
2638 } else {
2639 GST_LOG_OBJECT (pad, "latency query failed");
2640 g_value_set_boolean (ret, FALSE);
2641 }
2642
2643 return TRUE;
2644 }
2645
2646 static void
decoder_query_latency_done(GstURIDecodeBin * dec,QueryFold * fold)2647 decoder_query_latency_done (GstURIDecodeBin * dec, QueryFold * fold)
2648 {
2649 /* store max in query result */
2650 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
2651
2652 GST_DEBUG_OBJECT (dec,
2653 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2654 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
2655 fold->live);
2656 }
2657
2658 /* we are seekable if all srcpads are seekable */
2659 static gboolean
decoder_query_seeking_fold(const GValue * item,GValue * ret,QueryFold * fold)2660 decoder_query_seeking_fold (const GValue * item, GValue * ret, QueryFold * fold)
2661 {
2662 GstPad *pad = g_value_get_object (item);
2663
2664 if (gst_pad_query (pad, fold->query)) {
2665 gboolean seekable;
2666
2667 g_value_set_boolean (ret, TRUE);
2668 gst_query_parse_seeking (fold->query, NULL, &seekable, NULL, NULL);
2669
2670 GST_DEBUG_OBJECT (item, "got seekable %d", seekable);
2671
2672 if (fold->seekable)
2673 fold->seekable = seekable;
2674 }
2675
2676 return TRUE;
2677 }
2678
2679 static void
decoder_query_seeking_done(GstURIDecodeBin * dec,QueryFold * fold)2680 decoder_query_seeking_done (GstURIDecodeBin * dec, QueryFold * fold)
2681 {
2682 GstFormat format;
2683
2684 gst_query_parse_seeking (fold->query, &format, NULL, NULL, NULL);
2685 gst_query_set_seeking (fold->query, format, fold->seekable, 0, -1);
2686
2687 GST_DEBUG_OBJECT (dec, "seekable %d", fold->seekable);
2688 }
2689
2690 /* generic fold, return first valid result */
2691 static gboolean
decoder_query_generic_fold(const GValue * item,GValue * ret,QueryFold * fold)2692 decoder_query_generic_fold (const GValue * item, GValue * ret, QueryFold * fold)
2693 {
2694 GstPad *pad = g_value_get_object (item);
2695 gboolean res;
2696
2697 if ((res = gst_pad_query (pad, fold->query))) {
2698 g_value_set_boolean (ret, TRUE);
2699 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
2700 }
2701
2702 /* and stop as soon as we have a valid result */
2703 return !res;
2704 }
2705
2706
2707 /* we're a bin, the default query handler iterates sink elements, which we don't
2708 * have normally. We should just query all source pads.
2709 */
2710 static gboolean
gst_uri_decode_bin_query(GstElement * element,GstQuery * query)2711 gst_uri_decode_bin_query (GstElement * element, GstQuery * query)
2712 {
2713 GstURIDecodeBin *decoder;
2714 gboolean res = FALSE;
2715 GstIterator *iter;
2716 GstIteratorFoldFunction fold_func;
2717 QueryInitFunction fold_init = NULL;
2718 QueryDoneFunction fold_done = NULL;
2719 QueryFold fold_data;
2720 GValue ret = { 0 };
2721 gboolean default_ret = FALSE;
2722
2723 decoder = GST_URI_DECODE_BIN (element);
2724
2725 switch (GST_QUERY_TYPE (query)) {
2726 case GST_QUERY_DURATION:
2727 /* iterate and collect durations */
2728 fold_func = (GstIteratorFoldFunction) decoder_query_duration_fold;
2729 fold_init = decoder_query_init;
2730 fold_done = decoder_query_duration_done;
2731 break;
2732 case GST_QUERY_POSITION:
2733 /* iterate and collect durations */
2734 fold_func = (GstIteratorFoldFunction) decoder_query_position_fold;
2735 fold_init = decoder_query_init;
2736 fold_done = decoder_query_position_done;
2737 break;
2738 case GST_QUERY_LATENCY:
2739 /* iterate and collect durations */
2740 fold_func = (GstIteratorFoldFunction) decoder_query_latency_fold;
2741 fold_init = decoder_query_init;
2742 fold_done = decoder_query_latency_done;
2743 default_ret = TRUE;
2744 break;
2745 case GST_QUERY_SEEKING:
2746 /* iterate and collect durations */
2747 fold_func = (GstIteratorFoldFunction) decoder_query_seeking_fold;
2748 fold_init = decoder_query_init;
2749 fold_done = decoder_query_seeking_done;
2750 break;
2751 default:
2752 fold_func = (GstIteratorFoldFunction) decoder_query_generic_fold;
2753 break;
2754 }
2755
2756 fold_data.query = query;
2757
2758 g_value_init (&ret, G_TYPE_BOOLEAN);
2759 g_value_set_boolean (&ret, default_ret);
2760
2761 iter = gst_element_iterate_src_pads (element);
2762 GST_DEBUG_OBJECT (element, "Sending query %p (type %d) to src pads",
2763 query, GST_QUERY_TYPE (query));
2764
2765 if (fold_init)
2766 fold_init (decoder, &fold_data);
2767
2768 while (TRUE) {
2769 GstIteratorResult ires;
2770
2771 ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
2772
2773 switch (ires) {
2774 case GST_ITERATOR_RESYNC:
2775 gst_iterator_resync (iter);
2776 if (fold_init)
2777 fold_init (decoder, &fold_data);
2778 g_value_set_boolean (&ret, default_ret);
2779 break;
2780 case GST_ITERATOR_OK:
2781 case GST_ITERATOR_DONE:
2782 res = g_value_get_boolean (&ret);
2783 if (fold_done != NULL && res)
2784 fold_done (decoder, &fold_data);
2785 goto done;
2786 default:
2787 res = FALSE;
2788 goto done;
2789 }
2790 }
2791 done:
2792 gst_iterator_free (iter);
2793
2794 return res;
2795 }
2796
2797 static GstStateChangeReturn
gst_uri_decode_bin_change_state(GstElement * element,GstStateChange transition)2798 gst_uri_decode_bin_change_state (GstElement * element,
2799 GstStateChange transition)
2800 {
2801 GstStateChangeReturn ret;
2802 GstURIDecodeBin *decoder;
2803
2804 decoder = GST_URI_DECODE_BIN (element);
2805
2806 switch (transition) {
2807 case GST_STATE_CHANGE_READY_TO_PAUSED:
2808 do_async_start (decoder);
2809 break;
2810 default:
2811 break;
2812 }
2813
2814 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2815 if (ret == GST_STATE_CHANGE_FAILURE)
2816 goto setup_failed;
2817
2818 switch (transition) {
2819 case GST_STATE_CHANGE_READY_TO_PAUSED:
2820 GST_DEBUG ("ready to paused");
2821 if (!setup_source (decoder))
2822 goto source_failed;
2823
2824 ret = GST_STATE_CHANGE_ASYNC;
2825
2826 /* And now sync the states of everything we added */
2827 g_slist_foreach (decoder->decodebins,
2828 (GFunc) gst_element_sync_state_with_parent, NULL);
2829 if (decoder->typefind)
2830 ret = gst_element_set_state (decoder->typefind, GST_STATE_PAUSED);
2831 if (ret == GST_STATE_CHANGE_FAILURE)
2832 goto setup_failed;
2833 if (decoder->queue)
2834 ret = gst_element_set_state (decoder->queue, GST_STATE_PAUSED);
2835 if (ret == GST_STATE_CHANGE_FAILURE)
2836 goto setup_failed;
2837 if (decoder->source)
2838 ret = gst_element_set_state (decoder->source, GST_STATE_PAUSED);
2839 if (ret == GST_STATE_CHANGE_FAILURE)
2840 goto setup_failed;
2841 if (ret == GST_STATE_CHANGE_SUCCESS)
2842 ret = GST_STATE_CHANGE_ASYNC;
2843
2844 break;
2845 case GST_STATE_CHANGE_PAUSED_TO_READY:
2846 GST_DEBUG ("paused to ready");
2847 remove_decoders (decoder, FALSE);
2848 remove_source (decoder);
2849 do_async_done (decoder);
2850 g_list_free_full (decoder->missing_plugin_errors,
2851 (GDestroyNotify) gst_message_unref);
2852 decoder->missing_plugin_errors = NULL;
2853 break;
2854 case GST_STATE_CHANGE_READY_TO_NULL:
2855 GST_DEBUG ("ready to null");
2856 remove_decoders (decoder, TRUE);
2857 remove_source (decoder);
2858 break;
2859 default:
2860 break;
2861 }
2862
2863 if (ret == GST_STATE_CHANGE_NO_PREROLL)
2864 do_async_done (decoder);
2865
2866 return ret;
2867
2868 /* ERRORS */
2869 source_failed:
2870 {
2871 do_async_done (decoder);
2872 return GST_STATE_CHANGE_FAILURE;
2873 }
2874 setup_failed:
2875 {
2876 /* clean up leftover groups */
2877 do_async_done (decoder);
2878 return GST_STATE_CHANGE_FAILURE;
2879 }
2880 }
2881
2882 gboolean
gst_uri_decode_bin_plugin_init(GstPlugin * plugin)2883 gst_uri_decode_bin_plugin_init (GstPlugin * plugin)
2884 {
2885 GST_DEBUG_CATEGORY_INIT (gst_uri_decode_bin_debug, "uridecodebin", 0,
2886 "URI decoder element");
2887
2888 return gst_element_register (plugin, "uridecodebin", GST_RANK_NONE,
2889 GST_TYPE_URI_DECODE_BIN);
2890 }
2891