gsturidecodebin.c 87 KB
Newer Older
1
/* GStreamer
Wim Taymans's avatar
Wim Taymans committed
2
 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3
4
5
6
7
8
9
10
11
12
13
14
15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16
17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * 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
29
30
31
/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
 * with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS

32
33
34
35
36
37
38
39
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <string.h>

#include <gst/gst.h>
#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
48
49
50
51
52
53
54
55
56
#define GST_TYPE_URI_DECODE_BIN \
  (gst_uri_decode_bin_get_type())
#define GST_URI_DECODE_BIN(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBin))
#define GST_URI_DECODE_BIN_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBinClass))
#define GST_IS_URI_DECODE_BIN(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DECODE_BIN))
#define GST_IS_URI_DECODE_BIN_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DECODE_BIN))
57
#define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
58
59
60
61

typedef struct _GstURIDecodeBin GstURIDecodeBin;
typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;

62
63
#define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(&((GstURIDecodeBin*)(dec))->lock))
#define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(&((GstURIDecodeBin*)(dec))->lock))
64

65
66
67
68
69
70
typedef struct _GstURIDecodeBinStream
{
  gulong probe_id;
  guint bitrate;
} GstURIDecodeBinStream;

71
72
73
74
75
/**
 * GstURIDecodeBin
 *
 * uridecodebin element struct
 */
76
77
78
79
struct _GstURIDecodeBin
{
  GstBin parent_instance;

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
89
  GstCaps *caps;
  gchar *encoding;
90
91

  gboolean is_stream;
92
  gboolean is_adaptive;
93
  gboolean need_queue;
94
95
  guint64 buffer_duration;      /* When buffering, buffer duration (ns) */
  guint buffer_size;            /* When buffering, buffer size (bytes) */
96
  gboolean download;
97
  gboolean use_buffering;
98

99
  GstElement *source;
100
  GstElement *queue;
101
102
  GstElement *typefind;
  guint have_type_id;           /* have-type signal id from typefind */
103
  GSList *decodebins;
104
  GSList *pending_decodebins;
105
  GHashTable *streams;
Wim Taymans's avatar
Wim Taymans committed
106
  guint numpads;
107
108
109
110
111

  /* for dynamic sources */
  guint src_np_sig_id;          /* new-pad signal id */
  guint src_nmp_sig_id;         /* no-more-pads signal id */
  gint pending;
112
  GList *missing_plugin_errors;
113

Piotr Fusik's avatar
Piotr Fusik committed
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
122
123
};

struct _GstURIDecodeBinClass
{
  GstBinClass parent_class;
124
125
126
127
128
129
130
131
132
133

  /* signal fired when we found a pad that we cannot decode */
  void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);

  /* signal fired to know if we continue trying to decode the given caps */
    gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
      GstCaps * caps);
  /* signal fired to get a list of factories to try to autoplug */
  GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
      GstCaps * caps);
134
135
136
  /* signal fired to sort the factories */
  GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
      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
141
142
143
  /* signal fired when a autoplugged element that is not linked downstream
   * or exposed wants to query something */
    gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
      GstQuery * query);
144

Piotr Fusik's avatar
Piotr Fusik committed
145
  /* emitted when all data is decoded */
146
  void (*drained) (GstElement * element);
147
148
};

Wim Taymans's avatar
Wim Taymans committed
149
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
150
151
152
153
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);

154
155
static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);

156
157
158
GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
#define GST_CAT_DEFAULT gst_uri_decode_bin_debug

159
160
161
162
163
164
165
/* signals */
enum
{
  SIGNAL_UNKNOWN_TYPE,
  SIGNAL_AUTOPLUG_CONTINUE,
  SIGNAL_AUTOPLUG_FACTORIES,
  SIGNAL_AUTOPLUG_SELECT,
166
  SIGNAL_AUTOPLUG_SORT,
167
168
  SIGNAL_AUTOPLUG_QUERY,
  SIGNAL_DRAINED,
169
  SIGNAL_SOURCE_SETUP,
170
171
172
173
  LAST_SIGNAL
};

/* properties */
174
175
#define DEFAULT_PROP_URI            NULL
#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
180
#define DEFAULT_BUFFER_DURATION     -1
#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
187
188
189
enum
{
  PROP_0,
  PROP_URI,
190
  PROP_SOURCE,
191
  PROP_CONNECTION_SPEED,
192
193
  PROP_CAPS,
  PROP_SUBTITLE_ENCODING,
194
195
  PROP_BUFFER_SIZE,
  PROP_BUFFER_DURATION,
196
  PROP_DOWNLOAD,
197
  PROP_USE_BUFFERING,
198
  PROP_EXPOSE_ALL_STREAMS,
199
  PROP_RING_BUFFER_MAX_SIZE
200
201
};

202
203
static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };

204
GType gst_uri_decode_bin_get_type (void);
205
206
#define gst_uri_decode_bin_parent_class parent_class
G_DEFINE_TYPE (GstURIDecodeBin, gst_uri_decode_bin, GST_TYPE_BIN);
207

208
static void remove_decoders (GstURIDecodeBin * bin, gboolean force);
209
210
211
212
213
214
static void gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void gst_uri_decode_bin_finalize (GObject * obj);

215
216
static void handle_message (GstBin * bin, GstMessage * msg);

217
static gboolean gst_uri_decode_bin_query (GstElement * element,
218
    GstQuery * query);
219
220
221
static GstStateChangeReturn gst_uri_decode_bin_change_state (GstElement *
    element, GstStateChange transition);

222
223
224
225
226
227
228
229
230
231
232
233
234
235
static gboolean
_gst_boolean_accumulator (GSignalInvocationHint * ihint,
    GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
  gboolean myboolean;

  myboolean = g_value_get_boolean (handler_return);
  if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
    g_value_set_boolean (return_accu, myboolean);

  /* stop emission if FALSE */
  return myboolean;
}

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
static gboolean
_gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
    GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
  gboolean myboolean;
  gboolean retboolean;

  myboolean = g_value_get_boolean (handler_return);
  retboolean = g_value_get_boolean (return_accu);

  if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
    g_value_set_boolean (return_accu, myboolean || retboolean);

  return TRUE;
}

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
static gboolean
_gst_array_accumulator (GSignalInvocationHint * ihint,
    GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
  gpointer array;

  array = g_value_get_boxed (handler_return);
  if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
    g_value_set_boxed (return_accu, array);

  return FALSE;
}

static gboolean
_gst_select_accumulator (GSignalInvocationHint * ihint,
    GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
  GstAutoplugSelectResult res;

  res = g_value_get_enum (handler_return);
  if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
    g_value_set_enum (return_accu, res);

275
276
277
278
279
280
281
  /* Call the next handler in the chain (if any) when the current callback
   * returns TRY. This makes it possible to register separate autoplug-select
   * handlers that implement different TRY/EXPOSE/SKIP strategies.
   */
  if (res == GST_AUTOPLUG_SELECT_TRY)
    return TRUE;

282
283
284
  return FALSE;
}

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
static gboolean
_gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
    GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
  gpointer array;

  array = g_value_get_boxed (handler_return);
  if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
    g_value_set_boxed (return_accu, array);

  if (array != NULL)
    return FALSE;

  return TRUE;
}

301
302
303
304
305
306
307
308
static gboolean
gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
    GstCaps * caps)
{
  /* by default we always continue */
  return TRUE;
}

309
310
311
312
/* Must be called with factories lock! */
static void
gst_uri_decode_bin_update_factories_list (GstURIDecodeBin * dec)
{
313
314
315
316
  guint32 cookie;

  cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
  if (!dec->factories || dec->factories_cookie != cookie) {
317
    if (dec->factories)
318
319
320
321
      gst_plugin_feature_list_free (dec->factories);
    dec->factories =
        gst_element_factory_list_get_elements
        (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
329
330
331
static GValueArray *
gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
    GstCaps * caps)
{
332
  GList *list, *tmp;
333
  GValueArray *result;
334
  GstURIDecodeBin *dec = GST_URI_DECODE_BIN_CAST (element);
335
336
337
338

  GST_DEBUG_OBJECT (element, "finding factories");

  /* return all compatible factories for caps */
339
  g_mutex_lock (&dec->factories_lock);
340
  gst_uri_decode_bin_update_factories_list (dec);
341
342
  list =
      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
347
348
349
350
351
352
353
354
355
356
357
  result = g_value_array_new (g_list_length (list));
  for (tmp = list; tmp; tmp = tmp->next) {
    GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
    GValue val = { 0, };

    g_value_init (&val, G_TYPE_OBJECT);
    g_value_set_object (&val, factory);
    g_value_array_append (result, &val);
    g_value_unset (&val);
  }
  gst_plugin_feature_list_free (list);

358
359
360
361
362
  GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);

  return result;
}

363
364
365
366
static GValueArray *
gst_uri_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
    GstCaps * caps, GValueArray * factories)
{
367
  return NULL;
368
}
369

370
371
372
373
374
375
376
377
378
379
static GstAutoplugSelectResult
gst_uri_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
    GstCaps * caps, GstElementFactory * factory)
{
  GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");

  /* Try factory. */
  return GST_AUTOPLUG_SELECT_TRY;
}

380
381
382
383
384
385
386
387
static gboolean
gst_uri_decode_bin_autoplug_query (GstElement * element, GstPad * pad,
    GstQuery * query)
{
  /* No query handled here */
  return FALSE;
}

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
static void
gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
  GstBinClass *gstbin_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gstelement_class = GST_ELEMENT_CLASS (klass);
  gstbin_class = GST_BIN_CLASS (klass);

  gobject_class->set_property = gst_uri_decode_bin_set_property;
  gobject_class->get_property = gst_uri_decode_bin_get_property;
  gobject_class->finalize = gst_uri_decode_bin_finalize;

  g_object_class_install_property (gobject_class, PROP_URI,
      g_param_spec_string ("uri", "URI", "URI to decode",
405
          DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406

407
408
  g_object_class_install_property (gobject_class, PROP_SOURCE,
      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
418
419
  g_object_class_install_property (gobject_class, PROP_CAPS,
      g_param_spec_boxed ("caps", "Caps",
          "The caps on which to stop decoding. (NULL = default)",
420
          GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421
422
423
424
425
426

  g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
      g_param_spec_string ("subtitle-encoding", "subtitle encoding",
          "Encoding to assume if input subtitles are not in UTF-8 encoding. "
          "If not set, the GST_SUBTITLE_ENCODING environment variable will "
          "be checked for an encoding to use. If that is not set either, "
427
428
          "ISO-8859-15 will be assumed.", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429

430
431
  g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
      g_param_spec_int ("buffer-size", "Buffer size (bytes)",
432
          "Buffer size when buffering streams (-1 default value)",
433
434
435
436
          -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
      g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
437
          "Buffer duration when buffering streams (-1 default value)",
438
439
440
          -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

441
442
443
444
445
  /**
   * GstURIDecodeBin::download:
   *
   * For certain media type, enable download buffering.
   */
446
447
448
449
  g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
      g_param_spec_boolean ("download", "Download",
          "Attempt download buffering when buffering network streams",
          DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
450
451
452
453
454
455
456
457
458
459
460
  /**
   * GstURIDecodeBin::use-buffering:
   *
   * Emit BUFFERING messages based on low-/high-percent thresholds of the
   * demuxed or parsed data.
   * When download buffering is activated and used for the current media
   * type, this property does nothing. Otherwise perform buffering on the
   * demuxed or parsed media.
   */
  g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
      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
466
467
468
469
470
471
  /**
   * GstURIDecodeBin::expose-all-streams
   *
   * Expose streams of unknown type.
   *
   * If set to %FALSE, then only the streams that can be decoded to the final
   * caps (see 'caps' property) will have a pad exposed. Streams that do not
   * 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
475
476
477
478
   */
  g_object_class_install_property (gobject_class, PROP_EXPOSE_ALL_STREAMS,
      g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
          "Expose all streams, including those of unknown type or that don't match the 'caps' property",
          DEFAULT_EXPOSE_ALL_STREAMS,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
479
480

  /**
481
   * GstURIDecodeBin::ring-buffer-max-size
482
483
484
485
486
487
488
489
490
491
492
   *
   * The maximum size of the ring buffer in kilobytes. If set to 0, the ring
   * buffer is disabled. Default is 0.
   */
  g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
      g_param_spec_uint64 ("ring-buffer-max-size",
          "Max. ring buffer size (bytes)",
          "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
          0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

493
494
  /**
   * GstURIDecodeBin::unknown-type:
495
496
   * @bin: The uridecodebin.
   * @pad: the new pad containing caps that cannot be resolved to a 'final'.
497
   * stream type.
498
499
500
501
502
503
504
505
   * @caps: the #GstCaps of the pad that cannot be resolved.
   *
   * This signal is emitted when a pad for which there is no further possible
   * decoding is added to the uridecodebin.
   */
  gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
      g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, unknown_type),
506
      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
507
508
509
510
      GST_TYPE_PAD, GST_TYPE_CAPS);

  /**
   * GstURIDecodeBin::autoplug-continue:
511
   * @bin: The uridecodebin.
512
513
514
515
516
517
   * @pad: The #GstPad.
   * @caps: The #GstCaps found.
   *
   * This signal is emitted whenever uridecodebin finds a new stream. It is
   * emitted before looking for any elements that can handle that stream.
   *
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
523
   * Returns: %TRUE if you wish uridecodebin to look for elements that can
   * handle the given @caps. If %FALSE, those caps will be considered as
524
525
   * final and the pad will be exposed as such (see 'pad-added' signal of
   * #GstElement).
526
527
528
529
530
   */
  gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
      g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
          autoplug_continue), _gst_boolean_accumulator, NULL,
531
      g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
532
533
534
535
      GST_TYPE_CAPS);

  /**
   * GstURIDecodeBin::autoplug-factories:
536
   * @bin: The uridecodebin.
537
538
539
   * @pad: The #GstPad.
   * @caps: The #GstCaps found.
   *
Piotr Fusik's avatar
Piotr Fusik committed
540
   * This function is emitted when an array of possible factories for @caps on
541
542
543
544
545
546
547
   * @pad is needed. Uridecodebin will by default return an array with all
   * compatible factories, sorted by rank.
   *
   * If this function returns NULL, @pad will be exposed as a final caps.
   *
   * If this function returns an empty array, the pad will be considered as
   * having an unhandled type media type.
548
   *
549
550
551
   * >   Only the signal handler that is connected first will ever by invoked.
   * >   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
   * >   signal, they will never be invoked!
552
   *
553
554
555
556
557
558
559
   * Returns: a #GValueArray* with a list of factories to try. The factories are
   * by default tried in the returned order or based on the index returned by
   * "autoplug-select".
   */
  gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
      g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
      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
563
      GST_TYPE_PAD, GST_TYPE_CAPS);

564
565
  /**
   * GstURIDecodeBin::autoplug-sort:
566
   * @bin: The uridecodebin.
567
568
569
570
   * @pad: The #GstPad.
   * @caps: The #GstCaps.
   * @factories: A #GValueArray of possible #GstElementFactory to use.
   *
571
   * Once decodebin has found the possible #GstElementFactory objects to try
572
573
574
575
   * for @caps on @pad, this signal is emited. The purpose of the signal is for
   * the application to perform additional sorting or filtering on the element
   * factory array.
   *
576
   * The callee should copy and modify @factories or return %NULL if the
577
578
   * order should not change.
   *
579
   * >   Invocation of signal handlers stops after one signal handler has
580
   * >   returned something else than %NULL. Signal handlers are invoked in
581
582
583
   * >   the order they were connected in.
   * >   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
   * >   signal, they will never be invoked!
584
585
586
587
588
589
590
   *
   * Returns: A new sorted array of #GstElementFactory objects.
   */
  gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
      g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_sort),
      _gst_array_hasvalue_accumulator, NULL,
591
592
      g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
      GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
593

594
595
  /**
   * GstURIDecodeBin::autoplug-select:
596
   * @bin: The uridecodebin.
597
598
   * @pad: The #GstPad.
   * @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
Piotr Fusik's avatar
Piotr Fusik committed
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
609
610
611
612
613
614
615
   *
   * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
   * @factory.
   *
   * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
   * any element to it.
   *
   * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
   * next factory.
616
   *
617
618
619
620
621
   * >   The signal handler will not be invoked if any of the previously
   * >   registered signal handlers (if any) return a value other than
   * >   GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
   * >   GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
   * >   registered next (again, if any) can override that decision.
622
   *
623
624
625
   * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
   * operation. The default handler will always return
   * #GST_AUTOPLUG_SELECT_TRY.
626
627
628
629
   */
  gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
      g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
630
          autoplug_select), _gst_select_accumulator, NULL,
631
      g_cclosure_marshal_generic,
632
633
      GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
      GST_TYPE_ELEMENT_FACTORY);
634

635
636
637
638
639
640
641
642
643
644
645
646
  /**
   * GstDecodeBin::autoplug-query:
   * @bin: The decodebin.
   * @child: The child element doing the query
   * @pad: The #GstPad.
   * @query: The #GstQuery.
   *
   * This signal is emitted whenever an autoplugged element that is
   * not linked downstream yet and not exposed does a query. It can
   * be used to tell the element about the downstream supported caps
   * for example.
   *
647
   * Returns: %TRUE if the query was handled, %FALSE otherwise.
648
649
650
651
652
   */
  gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
      g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_query),
      _gst_boolean_or_accumulator, NULL, g_cclosure_marshal_generic,
653
      G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
654
655
      GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);

656
657
658
659
660
661
662
663
664
  /**
   * GstURIDecodeBin::drained:
   *
   * This signal is emitted when the data for the current uri is played.
   */
  gst_uri_decode_bin_signals[SIGNAL_DRAINED] =
      g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstURIDecodeBinClass, drained), NULL, NULL,
665
      g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
666

667
668
669
670
671
672
673
674
675
676
677
678
679
680
  /**
   * GstURIDecodeBin::source-setup:
   * @bin: the uridecodebin.
   * @source: source element
   *
   * This signal is emitted after the source element has been created, so
   * it can be configured by setting additional properties (e.g. set a
   * proxy server for an http source, or set the device and read speed for
   * an audio cd source). This is functionally equivalent to connecting to
   * the notify::source signal, but more convenient.
   */
  gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP] =
      g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
      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
686
687
688
      "URI Decoder", "Generic/Bin/Decoder",
      "Autoplug and decode an URI to raw media",
      "Wim Taymans <wim.taymans@gmail.com>");

689
690
691
  gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_query);
  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_uri_decode_bin_change_state);
692

693
694
  gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);

695
696
  klass->autoplug_continue =
      GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
697
698
  klass->autoplug_factories =
      GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
699
  klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_sort);
700
701
  klass->autoplug_select =
      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
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
715
716
  dec->connection_speed = DEFAULT_CONNECTION_SPEED;
  dec->caps = DEFAULT_CAPS;
  dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
717
718
719

  dec->buffer_duration = DEFAULT_BUFFER_DURATION;
  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

Wim Taymans's avatar
Wim Taymans committed
725
  GST_OBJECT_FLAG_SET (dec, GST_ELEMENT_FLAG_SOURCE);
726
727
  gst_bin_set_suppressed_flags (GST_BIN (dec),
      GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
728
729
730
731
732
733
734
}

static void
gst_uri_decode_bin_finalize (GObject * obj)
{
  GstURIDecodeBin *dec = GST_URI_DECODE_BIN (obj);

735
  remove_decoders (dec, TRUE);
736
737
  g_mutex_clear (&dec->lock);
  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
743
  if (dec->caps)
    gst_caps_unref (dec->caps);
744
745
746
747

  G_OBJECT_CLASS (parent_class)->finalize (obj);
}

748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
static void
gst_uri_decode_bin_set_encoding (GstURIDecodeBin * dec, const gchar * encoding)
{
  GSList *walk;

  GST_URI_DECODE_BIN_LOCK (dec);

  /* set property first */
  GST_OBJECT_LOCK (dec);
  g_free (dec->encoding);
  dec->encoding = g_strdup (encoding);
  GST_OBJECT_UNLOCK (dec);

  /* set the property on all decodebins now */
  for (walk = dec->decodebins; walk; walk = g_slist_next (walk)) {
    GObject *decodebin = G_OBJECT (walk->data);

    g_object_set (decodebin, "subtitle-encoding", encoding, NULL);
  }
  GST_URI_DECODE_BIN_UNLOCK (dec);
}

770
771
772
773
774
775
776
777
static void
gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);

  switch (prop_id) {
    case PROP_URI:
778
      GST_OBJECT_LOCK (dec);
779
780
      g_free (dec->uri);
      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
787
788
789
790
791
792
793
794
795
      GST_OBJECT_UNLOCK (dec);
      break;
    case PROP_CAPS:
      GST_OBJECT_LOCK (dec);
      if (dec->caps)
        gst_caps_unref (dec->caps);
      dec->caps = g_value_dup_boxed (value);
      GST_OBJECT_UNLOCK (dec);
      break;
    case PROP_SUBTITLE_ENCODING:
796
      gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
797
      break;
798
799
800
801
802
803
    case PROP_BUFFER_SIZE:
      dec->buffer_size = g_value_get_int (value);
      break;
    case PROP_BUFFER_DURATION:
      dec->buffer_duration = g_value_get_int64 (value);
      break;
804
805
806
    case PROP_DOWNLOAD:
      dec->download = g_value_get_boolean (value);
      break;
807
808
809
    case PROP_USE_BUFFERING:
      dec->use_buffering = g_value_get_boolean (value);
      break;
810
811
812
    case PROP_EXPOSE_ALL_STREAMS:
      dec->expose_allstreams = g_value_get_boolean (value);
      break;
813
814
815
    case PROP_RING_BUFFER_MAX_SIZE:
      dec->ring_buffer_max_size = g_value_get_uint64 (value);
      break;
816
817
818
819
820
821
822
823
824
825
826
827
828
829
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);

  switch (prop_id) {
    case PROP_URI:
830
      GST_OBJECT_LOCK (dec);
831
      g_value_set_string (value, dec->uri);
832
      GST_OBJECT_UNLOCK (dec);
833
      break;
834
835
836
837
838
    case PROP_SOURCE:
      GST_OBJECT_LOCK (dec);
      g_value_set_object (value, dec->source);
      GST_OBJECT_UNLOCK (dec);
      break;
839
    case PROP_CONNECTION_SPEED:
840
      GST_OBJECT_LOCK (dec);
841
      g_value_set_uint64 (value, dec->connection_speed / 1000);
842
843
844
845
846
847
848
849
850
851
852
      GST_OBJECT_UNLOCK (dec);
      break;
    case PROP_CAPS:
      GST_OBJECT_LOCK (dec);
      g_value_set_boxed (value, dec->caps);
      GST_OBJECT_UNLOCK (dec);
      break;
    case PROP_SUBTITLE_ENCODING:
      GST_OBJECT_LOCK (dec);
      g_value_set_string (value, dec->encoding);
      GST_OBJECT_UNLOCK (dec);
853
      break;
854
855
856
857
858
859
860
861
862
863
    case PROP_BUFFER_SIZE:
      GST_OBJECT_LOCK (dec);
      g_value_set_int (value, dec->buffer_size);
      GST_OBJECT_UNLOCK (dec);
      break;
    case PROP_BUFFER_DURATION:
      GST_OBJECT_LOCK (dec);
      g_value_set_int64 (value, dec->buffer_duration);
      GST_OBJECT_UNLOCK (dec);
      break;
864
865
866
    case PROP_DOWNLOAD:
      g_value_set_boolean (value, dec->download);
      break;
867
868
869
    case PROP_USE_BUFFERING:
      g_value_set_boolean (value, dec->use_buffering);
      break;
870
871
872
    case PROP_EXPOSE_ALL_STREAMS:
      g_value_set_boolean (value, dec->expose_allstreams);
      break;
873
874
875
    case PROP_RING_BUFFER_MAX_SIZE:
      g_value_set_uint64 (value, dec->ring_buffer_max_size);
      break;
876
877
878
879
880
881
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

882
883
884
885
886
887
888
static void
do_async_start (GstURIDecodeBin * dbin)
{
  GstMessage *message;

  dbin->async_pending = TRUE;

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
894
895
896
897
898
}

static void
do_async_done (GstURIDecodeBin * dbin)
{
  GstMessage *message;

  if (dbin->async_pending) {
Wim Taymans's avatar
Wim Taymans committed
899
    GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
Wim Taymans's avatar
Wim Taymans committed
900
901
902
    message =
        gst_message_new_async_done (GST_OBJECT_CAST (dbin),
        GST_CLOCK_TIME_NONE);
903
    GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (dbin), message);
904
905
906
907
908

    dbin->async_pending = FALSE;
  }
}

909
910
911
912
913
#define DEFAULT_QUEUE_SIZE          (3 * GST_SECOND)
#define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
#define DEFAULT_QUEUE_THRESHOLD     ((DEFAULT_QUEUE_SIZE * 95) / 100)

static void
914
unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
915
916
917
918
919
    GstURIDecodeBin * decoder)
{
  gchar *capsstr;

  capsstr = gst_caps_to_string (caps);
920
  GST_ELEMENT_WARNING (decoder, STREAM, CODEC_NOT_FOUND,
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
      (_("No decoder available for type \'%s\'."), capsstr), (NULL));
  g_free (capsstr);
}

/* add a streaminfo that indicates that the stream is handled by the
 * given element. This usually means that a stream without actual data is
 * produced but one that is sunken by an element. Examples of this are:
 * cdaudio, a hardware decoder/sink, dvd meta bins etc...
 */
static void
add_element_stream (GstElement * element, GstURIDecodeBin * decoder)
{
  g_warning ("add element stream");
}

/* when the decoder element signals that no more pads will be generated, we
 * can commit the current group.
 */
static void
no_more_pads_full (GstElement * element, gboolean subs,
    GstURIDecodeBin * decoder)
{
943
944
  gboolean final;

945
946
947
  /* setup phase */
  GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);

948
  GST_URI_DECODE_BIN_LOCK (decoder);
949
  final = (decoder->pending == 0);
950
951

  /* nothing pending, we can exit */
952
953
  if (final)
    goto done;
954
955
956

  /* the object has no pending no_more_pads */
  if (!g_object_get_data (G_OBJECT (element), "pending"))
957
    goto done;
958
959
960
  g_object_set_data (G_OBJECT (element), "pending", NULL);

  decoder->pending--;
961
  final = (decoder->pending == 0);
962
963

done:
964
  GST_URI_DECODE_BIN_UNLOCK (decoder);
965

966
967
968
969
970
  if (final) {
    /* If we got not a single stream yet, that means that all
     * decodebins had missing plugins for all of their streams!
     */
    if (!decoder->streams || g_hash_table_size (decoder->streams) == 0) {
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
      if (decoder->missing_plugin_errors) {
        GString *str = g_string_new ("");
        GList *l;

        for (l = decoder->missing_plugin_errors; l; l = l->next) {
          GstMessage *msg = l->data;
          gchar *debug;

          gst_message_parse_error (msg, NULL, &debug);
          g_string_append (str, debug);
          g_free (debug);
          gst_message_unref (msg);
        }
        g_list_free (decoder->missing_plugin_errors);
        decoder->missing_plugin_errors = NULL;

        GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
            ("no suitable plugins found:\n%s", str->str));
        g_string_free (str, TRUE);
      } else {
        GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
            ("no suitable plugins found"));
      }
994
995
996
    } else {
      gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
    }
997
    do_async_done (decoder);
998
  }
999
1000

  return;
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
}

static void
no_more_pads (GstElement * element, GstURIDecodeBin * decoder)
{
  no_more_pads_full (element, FALSE, decoder);
}

static void
source_no_more_pads (GstElement * element, GstURIDecodeBin * bin)
{
  GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
      GST_ELEMENT_NAME (element));

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
1019
1020
1021
1022
  bin->src_nmp_sig_id = 0;

  no_more_pads_full (element, FALSE, bin);
}

1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
static void
configure_stream_buffering (GstURIDecodeBin * decoder)
{
  GstElement *queue = NULL;
  GHashTableIter iter;
  gpointer key, value;
  gint bitrate = 0;

  /* automatic configuration enabled ? */
  if (decoder->buffer_size != -1)
    return;

  GST_URI_DECODE_BIN_LOCK (decoder);
  if (decoder->queue)
    queue = gst_object_ref (decoder->queue);

  g_hash_table_iter_init (&iter, decoder->streams);
  while (g_hash_table_iter_next (&iter, &key, &value)) {
    GstURIDecodeBinStream *stream = value;

    if (stream->bitrate && bitrate >= 0)
      bitrate += stream->bitrate;
    else
      bitrate = -1;
  }
  GST_URI_DECODE_BIN_UNLOCK (decoder);

  GST_DEBUG_OBJECT (decoder, "overall bitrate %d", bitrate);
  if (!queue)
    return;

  if (bitrate > 0) {
    guint64 time;
    guint bytes;

    /* all streams have a bitrate;
     * configure queue size based on queue duration using combined bitrate */
    g_object_get (queue, "max-size-time", &time, NULL);
    GST_DEBUG_OBJECT (decoder, "queue buffering time %" GST_TIME_FORMAT,
        GST_TIME_ARGS (time));
    if (time > 0) {
      bytes = gst_util_uint64_scale (time, bitrate, 8 * GST_SECOND);
      GST_DEBUG_OBJECT (decoder, "corresponds to buffer size %d", bytes);
      g_object_set (queue, "max-size-bytes", bytes, NULL);
    }
  }

  gst_object_unref (queue);
}

1073
static GstPadProbeReturn
Wim Taymans's avatar
Wim Taymans committed
1074
decoded_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
1075
    gpointer user_data)
1076
{
Wim Taymans's avatar
Wim Taymans committed
1077
  GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
1078
1079
  GstURIDecodeBin *decoder = user_data;

1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
  GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);

  /* look for a bitrate tag */
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_TAG:
    {
      GstTagList *list;
      guint bitrate = 0;
      GstURIDecodeBinStream *stream;

      gst_event_parse_tag (event, &list);
      if (!gst_tag_list_get_uint_index (list, GST_TAG_NOMINAL_BITRATE, 0,
              &bitrate)) {
        gst_tag_list_get_uint_index (list, GST_TAG_BITRATE, 0, &bitrate);
      }
      GST_DEBUG_OBJECT (pad, "found bitrate %u", bitrate);
      if (bitrate) {
        GST_URI_DECODE_BIN_LOCK (decoder);
        stream = g_hash_table_lookup (decoder->streams, pad);
        GST_URI_DECODE_BIN_UNLOCK (decoder);
        if (stream) {
          stream->bitrate = bitrate;
          /* no longer need this probe now */
1103
          gst_pad_remove_probe (pad, stream->probe_id);
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
          /* configure buffer if possible */
          configure_stream_buffering (decoder);
        }
      }
      break;
    }
    default:
      break;
  }

  /* never drop */
1115
  return GST_PAD_PROBE_OK;
1116
1117
}

Wim Taymans's avatar
Wim Taymans committed
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129

static gboolean
copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
{
  GstPad *gpad = GST_PAD_CAST (user_data);

  GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
  gst_pad_store_sticky_event (gpad, *event);

  return TRUE;
}

1130
/* Called by the signal handlers when a decodebin has found a new raw pad */
1131
static void
1132
new_decoded_pad_added_cb (GstElement * element, GstPad * pad,
1133
1134
1135
    GstURIDecodeBin * decoder)
{
  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);
Wim Taymans's avatar
Wim Taymans committed
1143
  padname = g_strdup_printf ("src_%u", decoder->numpads);
1144
  decoder->numpads++;
1145
  GST_URI_DECODE_BIN_UNLOCK (decoder);
1146

1147
1148
1149
  pad_tmpl = gst_static_pad_template_get (&srctemplate);
  newpad = gst_ghost_pad_new_from_template (padname, pad, pad_tmpl);
  gst_object_unref (pad_tmpl);
1150
  g_free (padname);
1151

1152
1153
1154
  /* store ref to the ghostpad so we can remove it */
  g_object_set_data (G_OBJECT (pad), "uridecodebin.ghostpad", newpad);

1155
1156
1157
  /* add event probe to monitor tags */
  stream = g_slice_alloc0 (sizeof (GstURIDecodeBinStream));
  stream->probe_id =
Wim Taymans's avatar
Wim Taymans committed
1158
1159
      gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
      decoded_pad_event_probe, decoder, NULL);
1160
1161
1162
1163
  GST_URI_DECODE_BIN_LOCK (decoder);
  g_hash_table_insert (decoder->streams, pad, stream);
  GST_URI_DECODE_BIN_UNLOCK (decoder);

1164
  gst_pad_set_active (newpad, TRUE);
Wim Taymans's avatar
Wim Taymans committed
1165
  gst_pad_sticky_events_foreach (pad, copy_sticky_events, newpad);
1166
1167
1168
  gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
}

1169
static GstPadProbeReturn
Wim Taymans's avatar