gsturidecodebin.c 58.3 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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:element-uridecodebin
 *
 * Decodes data from a URI into raw media.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <string.h>

#include <gst/gst.h>
#include <gst/gst-i18n-plugin.h>

35
#include "gstfactorylists.h"
36
#include "gstplay-marshal.h"
37
#include "gstplay-enum.h"
38

39
40
41
42
43
44
45
46
47
48
#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))
49
#define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
50
51
52
53

typedef struct _GstURIDecodeBin GstURIDecodeBin;
typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;

54
55
56
57
#define GST_URI_DECODE_BIN_GET_LOCK(dec) (((GstURIDecodeBin*)(dec))->lock)
#define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
#define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(GST_URI_DECODE_BIN_GET_LOCK(dec)))

58
59
60
61
62
/**
 * GstURIDecodeBin
 *
 * uridecodebin element struct
 */
63
64
65
66
struct _GstURIDecodeBin
{
  GstBin parent_instance;

67
68
69
70
  GMutex *lock;                 /* lock for constructing */

  GValueArray *factories;       /* factories we can use for selecting elements */

71
  gchar *uri;
72
  guint connection_speed;
73
74
  GstCaps *caps;
  gchar *encoding;
75
76

  gboolean is_stream;
77
  gboolean need_queue;
78
79
  guint64 buffer_duration;      /* When streaming, buffer duration (ns) */
  guint buffer_size;            /* When streaming, buffer size (bytes) */
80
  gboolean download;
81

82
  GstElement *source;
83
84
  GstElement *typefind;
  guint have_type_id;           /* have-type signal id from typefind */
85
  GSList *decodebins;
86
87
  GSList *srcpads;
  gint numpads;
88
89
90
91
92

  /* for dynamic sources */
  guint src_np_sig_id;          /* new-pad signal id */
  guint src_nmp_sig_id;         /* no-more-pads signal id */
  gint pending;
93
94

  gboolean async_pending;       /* async-start has been emited */
95
96
97
98
99
};

struct _GstURIDecodeBinClass
{
  GstBinClass parent_class;
100
101
102
103
104
105
106
107
108
109
110

  /* 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);
  /* signal fired to select from the proposed list of factories */
111
112
    GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
      GstPad * pad, GstCaps * caps, GValueArray * factories);
113
114
115

  /* emited when all data is decoded */
  void (*drained) (GstElement * element);
116
117
};

118
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
119
120
121
122
123
124
125
126
127
128
129
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);

GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
#define GST_CAT_DEFAULT gst_uri_decode_bin_debug

static const GstElementDetails gst_uri_decode_bin_details =
GST_ELEMENT_DETAILS ("URI Decoder",
    "Generic/Bin/Decoder",
    "Autoplug and decode an URI to raw media",
130
    "Wim Taymans <wim.taymans@gmail.com>");
131

132
133
134
135
136
137
138
/* signals */
enum
{
  SIGNAL_UNKNOWN_TYPE,
  SIGNAL_AUTOPLUG_CONTINUE,
  SIGNAL_AUTOPLUG_FACTORIES,
  SIGNAL_AUTOPLUG_SELECT,
139
  SIGNAL_DRAINED,
140
141
142
143
144
  LAST_SIGNAL
};

/* properties */
#define DEFAULT_PROP_URI	    NULL
145
#define DEFAULT_PROP_SOURCE	    NULL
146
#define DEFAULT_CONNECTION_SPEED    0
147
148
#define DEFAULT_CAPS                NULL
#define DEFAULT_SUBTITLE_ENCODING   NULL
149
150
#define DEFAULT_BUFFER_DURATION     -1
#define DEFAULT_BUFFER_SIZE         -1
151
#define DEFAULT_DOWNLOAD            FALSE
152

153
154
155
156
enum
{
  PROP_0,
  PROP_URI,
157
  PROP_SOURCE,
158
  PROP_CONNECTION_SPEED,
159
160
  PROP_CAPS,
  PROP_SUBTITLE_ENCODING,
161
162
  PROP_BUFFER_SIZE,
  PROP_BUFFER_DURATION,
163
  PROP_DOWNLOAD,
164
165
166
  PROP_LAST
};

167
168
static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };

169
170
171
172
173
174
175
176
GST_BOILERPLATE (GstURIDecodeBin, gst_uri_decode_bin, GstBin, GST_TYPE_BIN);

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);

177
178
static void handle_message (GstBin * bin, GstMessage * msg);

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
static gboolean gst_uri_decode_bin_query (GstElement * element,
    GstQuery * query);
static GstStateChangeReturn gst_uri_decode_bin_change_state (GstElement *
    element, GstStateChange transition);

static void
gst_uri_decode_bin_base_init (gpointer g_class)
{
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&srctemplate));
  gst_element_class_set_details (gstelement_class, &gst_uri_decode_bin_details);
}

194
195
196
197
198
199
200
201
202
203
204
205
206
207
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;
}

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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);

  return FALSE;
}

234
235
236
237
238
239
240
241
static gboolean
gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
    GstCaps * caps)
{
  /* by default we always continue */
  return TRUE;
}

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
static GValueArray *
gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
    GstCaps * caps)
{
  GValueArray *result;

  GST_DEBUG_OBJECT (element, "finding factories");

  /* return all compatible factories for caps */
  result =
      gst_factory_list_filter (GST_URI_DECODE_BIN_CAST (element)->factories,
      caps);

  GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);

  return result;
}


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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",
278
          DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279

280
281
282
283
  g_object_class_install_property (gobject_class, PROP_SOURCE,
      g_param_spec_object ("source", "Source", "Source object used",
          GST_TYPE_ELEMENT, G_PARAM_READABLE));

284
285
286
  g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
      g_param_spec_uint ("connection-speed", "Connection Speed",
          "Network connection speed in kbps (0 = unknown)",
287
          0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
288
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289

290
291
292
  g_object_class_install_property (gobject_class, PROP_CAPS,
      g_param_spec_boxed ("caps", "Caps",
          "The caps on which to stop decoding. (NULL = default)",
293
          GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294
295
296
297
298
299

  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, "
300
301
          "ISO-8859-15 will be assumed.", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
302

303
304
305
306
307
308
309
310
311
312
313
  g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
      g_param_spec_int ("buffer-size", "Buffer size (bytes)",
          "Buffer size when buffering network streams",
          -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)",
          "Buffer duration when buffering network streams",
          -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

314
315
316
317
318
  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));

319
320
  /**
   * GstURIDecodeBin::unknown-type:
321
   * @bin: The uridecodebin
322
323
   * @pad: the new pad containing caps that cannot be resolved to a 'final'
   * stream type.
324
325
326
327
328
329
330
331
   * @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),
332
      NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
333
334
335
336
      GST_TYPE_PAD, GST_TYPE_CAPS);

  /**
   * GstURIDecodeBin::autoplug-continue:
337
   * @bin: The uridecodebin
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
   * @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.
   *
   * Returns: #TRUE if you wish uridecodebin to look for elements that can
   * handle the given @caps. If #FALSE, those caps will be considered as
   * final and the pad will be exposed as such (see 'new-decoded-pad'
   * signal).
   */
  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,
353
      gst_play_marshal_BOOLEAN__OBJECT_BOXED, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
354
355
356
357
      GST_TYPE_CAPS);

  /**
   * GstURIDecodeBin::autoplug-factories:
358
   * @bin: The decodebin
359
360
361
362
   * @pad: The #GstPad.
   * @caps: The #GstCaps found.
   *
   * This function is emited when an array of possible factories for @caps on
363
364
365
366
367
368
369
   * @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.
370
371
372
373
374
375
376
377
   *
   * 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,
378
          autoplug_factories), _gst_array_accumulator, NULL,
379
      gst_play_marshal_BOXED__OBJECT_BOXED, G_TYPE_VALUE_ARRAY, 2,
380
381
382
383
384
385
      GST_TYPE_PAD, GST_TYPE_CAPS);

  /**
   * GstURIDecodeBin::autoplug-select:
   * @pad: The #GstPad.
   * @caps: The #GstCaps.
386
   * @factory: A #GstElementFactory to use
387
388
   *
   * This signal is emitted once uridecodebin has found all the possible
389
390
391
392
393
394
395
396
397
398
399
400
401
402
   * #GstElementFactory that can be used to handle the given @caps. For each of
   * those factories, this signal is emited.
   *
   * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
   * value indicating what decodebin2 should do next.
   *
   * 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.
403
   *
404
405
406
   * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
   * operation. The default handler will always return
   * #GST_AUTOPLUG_SELECT_TRY.
407
408
409
410
   */
  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,
411
          autoplug_select), _gst_select_accumulator, NULL,
412
      gst_play_marshal_ENUM__OBJECT_BOXED_OBJECT,
413
414
      GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
      GST_TYPE_ELEMENT_FACTORY);
415

416
417
418
419
420
421
422
423
424
425
426
  /**
   * 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,
      gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);

427
428
429
  gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_query);
  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_uri_decode_bin_change_state);
430

431
432
  gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);

433
434
  klass->autoplug_continue =
      GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
435
436
  klass->autoplug_factories =
      GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
437
438
439
440
441
}

static void
gst_uri_decode_bin_init (GstURIDecodeBin * dec, GstURIDecodeBinClass * klass)
{
442
443
444
445
446
  /* first filter out the interesting element factories */
  dec->factories = gst_factory_list_get_elements (GST_FACTORY_LIST_DECODER);

  dec->lock = g_mutex_new ();

447
  dec->uri = g_strdup (DEFAULT_PROP_URI);
448
449
450
  dec->connection_speed = DEFAULT_CONNECTION_SPEED;
  dec->caps = DEFAULT_CAPS;
  dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
451
452
453

  dec->buffer_duration = DEFAULT_BUFFER_DURATION;
  dec->buffer_size = DEFAULT_BUFFER_SIZE;
454
  dec->download = DEFAULT_DOWNLOAD;
455
456
457
458
459
460
461
}

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

462
  g_mutex_free (dec->lock);
463
  g_free (dec->uri);
464
  g_free (dec->encoding);
465
  if (dec->factories)
466
    g_value_array_free (dec->factories);
467
468
  if (dec->caps)
    gst_caps_unref (dec->caps);
469
470
471
472

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

473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
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);
}

495
496
497
498
499
500
501
502
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:
503
      GST_OBJECT_LOCK (dec);
504
505
      g_free (dec->uri);
      dec->uri = g_value_dup_string (value);
506
      GST_OBJECT_UNLOCK (dec);
507
      break;
508
    case PROP_CONNECTION_SPEED:
509
      GST_OBJECT_LOCK (dec);
510
      dec->connection_speed = g_value_get_uint (value) * 1000;
511
512
513
514
515
516
517
518
519
520
      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:
521
      gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
522
      break;
523
524
525
526
527
528
    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;
529
530
531
    case PROP_DOWNLOAD:
      dec->download = g_value_get_boolean (value);
      break;
532
533
534
535
536
537
538
539
540
541
542
543
544
545
    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:
546
      GST_OBJECT_LOCK (dec);
547
      g_value_set_string (value, dec->uri);
548
      GST_OBJECT_UNLOCK (dec);
549
      break;
550
551
552
553
554
    case PROP_SOURCE:
      GST_OBJECT_LOCK (dec);
      g_value_set_object (value, dec->source);
      GST_OBJECT_UNLOCK (dec);
      break;
555
    case PROP_CONNECTION_SPEED:
556
      GST_OBJECT_LOCK (dec);
557
      g_value_set_uint (value, dec->connection_speed / 1000);
558
559
560
561
562
563
564
565
566
567
568
      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);
569
      break;
570
571
572
573
574
575
576
577
578
579
    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;
580
581
582
    case PROP_DOWNLOAD:
      g_value_set_boolean (value, dec->download);
      break;
583
584
585
586
587
588
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
static void
do_async_start (GstURIDecodeBin * dbin)
{
  GstMessage *message;

  dbin->async_pending = TRUE;

  message = gst_message_new_async_start (GST_OBJECT_CAST (dbin), FALSE);
  parent_class->handle_message (GST_BIN_CAST (dbin), message);
}

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

  if (dbin->async_pending) {
Wim Taymans's avatar
Wim Taymans committed
606
    GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
607
608
609
610
611
612
613
    message = gst_message_new_async_done (GST_OBJECT_CAST (dbin));
    parent_class->handle_message (GST_BIN_CAST (dbin), message);

    dbin->async_pending = FALSE;
  }
}

614
615
616
617
618
#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
619
unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
    GstURIDecodeBin * decoder)
{
  gchar *capsstr;

  capsstr = gst_caps_to_string (caps);
  GST_ELEMENT_WARNING (decoder, STREAM, WRONG_TYPE,
      (_("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)
{
648
649
  gboolean final;

650
651
652
  /* setup phase */
  GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);

653
  GST_URI_DECODE_BIN_LOCK (decoder);
654
  final = (decoder->pending == 0);
655
656

  /* nothing pending, we can exit */
657
658
  if (final)
    goto done;
659
660
661

  /* the object has no pending no_more_pads */
  if (!g_object_get_data (G_OBJECT (element), "pending"))
662
    goto done;
663
664
665
  g_object_set_data (G_OBJECT (element), "pending", NULL);

  decoder->pending--;
666
  final = (decoder->pending == 0);
667
668

done:
669
  GST_URI_DECODE_BIN_UNLOCK (decoder);
670
671
672
673
674

  if (final)
    gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));

  return;
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
}

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));

  g_signal_handler_disconnect (G_OBJECT (element), bin->src_np_sig_id);
  bin->src_np_sig_id = 0;
  g_signal_handler_disconnect (G_OBJECT (element), bin->src_nmp_sig_id);
  bin->src_nmp_sig_id = 0;

  no_more_pads_full (element, FALSE, bin);
}

/* Called by the signal handlers when a decodebin has 
 * found a new raw pad.  
 */
static void
701
new_decoded_pad_cb (GstElement * element, GstPad * pad, gboolean last,
702
703
704
    GstURIDecodeBin * decoder)
{
  GstPad *newpad;
705
  gchar *padname;
706
707
708
709

  GST_DEBUG_OBJECT (element, "new decoded pad, name: <%s>. Last: %d",
      GST_PAD_NAME (pad), last);

710
  GST_URI_DECODE_BIN_LOCK (decoder);
711
712
  padname = g_strdup_printf ("src%d", decoder->numpads);
  decoder->numpads++;
713
  GST_URI_DECODE_BIN_UNLOCK (decoder);
714
715
716

  newpad = gst_ghost_pad_new (padname, pad);
  g_free (padname);
717

718
719
720
  /* store ref to the ghostpad so we can remove it */
  g_object_set_data (G_OBJECT (pad), "uridecodebin.ghostpad", newpad);

721
  gst_pad_set_active (newpad, TRUE);
722
723
724
  gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
}

725
726
727
728
729
730
731
732
static void
pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
{
  GstPad *ghost;

  GST_DEBUG_OBJECT (element, "pad removed name: <%s:%s>",
      GST_DEBUG_PAD_NAME (pad));

733
734
735
736
  /* we only care about srcpads */
  if (!GST_PAD_IS_SRC (pad))
    return;

737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
  if (!(ghost = g_object_get_data (G_OBJECT (pad), "uridecodebin.ghostpad")))
    goto no_ghost;

  /* unghost the pad */
  gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ghost), NULL);

  /* deactivate and remove */
  gst_pad_set_active (pad, FALSE);
  gst_element_remove_pad (GST_ELEMENT_CAST (decoder), ghost);

  return;

  /* ERRORS */
no_ghost:
  {
    GST_WARNING_OBJECT (element, "no ghost pad found");
    return;
  }
}

757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* helper function to lookup stuff in lists */
static gboolean
array_has_value (const gchar * values[], const gchar * value)
{
  gint i;

  for (i = 0; values[i]; i++) {
    if (g_str_has_prefix (value, values[i]))
      return TRUE;
  }
  return FALSE;
}

/* list of URIs that we consider to be streams and that need buffering.
 * We have no mechanism yet to figure this out with a query. */
static const gchar *stream_uris[] = { "http://", "mms://", "mmsh://",
773
  "mmsu://", "mmst://", "fd://", NULL
774
775
};

776
777
778
/* list of URIs that need a queue because they are pretty bursty */
static const gchar *queue_uris[] = { "cdda://", NULL };

779
780
781
782
783
784
785
786
787
788
789
/* blacklisted URIs, we know they will always fail. */
static const gchar *blacklisted_uris[] = { NULL };

/* mime types that we don't consider to be media types */
#if 0
static const gchar *no_media_mimes[] = {
  "application/x-executable", "application/x-bzip", "application/x-gzip",
  "application/zip", "application/x-compress", NULL
};
#endif

790
791
792
/* media types we consider raw media */
static const gchar *raw_media[] = {
  "audio/x-raw", "video/x-raw", "text/plain", "text/x-pango-markup",
793
  "video/x-dvd-subpicture", "subpicture/x-", NULL
794
795
};

796
797
798
799
800
/* media types we can download */
static const gchar *download_media[] = {
  "video/quicktime", "video/x-flv", NULL
};

801
#define IS_STREAM_URI(uri)          (array_has_value (stream_uris, uri))
802
#define IS_QUEUE_URI(uri)           (array_has_value (queue_uris, uri))
803
804
#define IS_BLACKLISTED_URI(uri)     (array_has_value (blacklisted_uris, uri))
#define IS_NO_MEDIA_MIME(mime)      (array_has_value (no_media_mimes, mime))
805
#define IS_RAW_MEDIA(media)         (array_has_value (raw_media, media))
806
#define IS_DOWNLOAD_MEDIA(media)    (array_has_value (download_media, media))
807
808
809
810
811
812
813
814
815
816
817
818

/*
 * Generate and configure a source element.
 */
static GstElement *
gen_source_element (GstURIDecodeBin * decoder)
{
  GstElement *source;

  if (!decoder->uri)
    goto no_uri;

819
820
  GST_LOG_OBJECT (decoder, "finding source for %s", decoder->uri);

821
822
823
824
825
826
827
828
829
830
  if (!gst_uri_is_valid (decoder->uri))
    goto invalid_uri;

  if (IS_BLACKLISTED_URI (decoder->uri))
    goto uri_blacklisted;

  source = gst_element_make_from_uri (GST_URI_SRC, decoder->uri, "source");
  if (!source)
    goto no_source;

831
832
  GST_LOG_OBJECT (decoder, "found source type %s", G_OBJECT_TYPE_NAME (source));

833
  decoder->is_stream = IS_STREAM_URI (decoder->uri);
834
835
  GST_LOG_OBJECT (decoder, "source is stream: %d", decoder->is_stream);

836
837
838
  decoder->need_queue = IS_QUEUE_URI (decoder->uri);
  GST_LOG_OBJECT (decoder, "source needs queue: %d", decoder->need_queue);

839
840
841
842
843
  /* make HTTP sources send extra headers so we get icecast
   * metadata in case the stream is an icecast stream */
  if (!strncmp (decoder->uri, "http://", 7) &&
      g_object_class_find_property (G_OBJECT_GET_CLASS (source),
          "iradio-mode")) {
844
    GST_LOG_OBJECT (decoder, "configuring iradio-mode");
845
846
    g_object_set (source, "iradio-mode", TRUE, NULL);
  }
847
848
849
850
851
852
853
854
855

  if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
          "connection-speed")) {
    GST_DEBUG_OBJECT (decoder,
        "setting connection-speed=%d to source element",
        decoder->connection_speed / 1000);
    g_object_set (source, "connection-speed",
        decoder->connection_speed / 1000, NULL);
  }
856
857
858
859
860
861
862
  if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
          "subtitle-encoding")) {
    GST_DEBUG_OBJECT (decoder,
        "setting subtitle-encoding=%s to source element", decoder->encoding);
    g_object_set (G_OBJECT (source), "subtitle-encoding", decoder->encoding,
        NULL);
  }
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
  return source;

  /* ERRORS */
no_uri:
  {
    GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
        (_("No URI specified to play from.")), (NULL));
    return NULL;
  }
invalid_uri:
  {
    GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
        (_("Invalid URI \"%s\"."), decoder->uri), (NULL));
    return NULL;
  }
uri_blacklisted:
  {
    GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
        (_("This stream type cannot be played yet.")), (NULL));
    return NULL;
  }
no_source:
  {
    gchar *prot = gst_uri_get_protocol (decoder->uri);

    /* whoops, could not create the source element, dig a little deeper to
     * figure out what might be wrong. */
    if (prot) {
      GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
          (_("No URI handler implemented for \"%s\"."), prot), (NULL));
      g_free (prot);
    } else
      goto invalid_uri;

    return NULL;
  }
}

/**
 * has_all_raw_caps:
 * @pad: a #GstPad
 * @all_raw: pointer to hold the result
 *
 * check if the caps of the pad are all raw. The caps are all raw if
 * all of its structures contain audio/x-raw or video/x-raw.
 *
 * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
 */
static gboolean
has_all_raw_caps (GstPad * pad, gboolean * all_raw)
{
  GstCaps *caps;
  gint capssize;
  guint i, num_raw = 0;
  gboolean res = FALSE;

  caps = gst_pad_get_caps (pad);
  if (caps == NULL)
    return FALSE;

923
924
  GST_DEBUG_OBJECT (pad, "have caps %" GST_PTR_FORMAT, caps);

925
926
927
928
929
930
931
932
  capssize = gst_caps_get_size (caps);
  /* no caps, skip and move to the next pad */
  if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
    goto done;

  /* count the number of raw formats in the caps */
  for (i = 0; i < capssize; ++i) {
    GstStructure *s;
933
    const gchar *media_type;
934
935

    s = gst_caps_get_structure (caps, i);
936
937
938
    media_type = gst_structure_get_name (s);

    GST_DEBUG_OBJECT (pad, "check media-type %s", media_type);
939

940
    if (IS_RAW_MEDIA (media_type))
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
      ++num_raw;
  }

  *all_raw = (num_raw == capssize);
  res = TRUE;

done:
  gst_caps_unref (caps);
  return res;
}

/**
 * analyse_source:
 * @decoder: a #GstURIDecodeBin
 * @is_raw: are all pads raw data
 * @have_out: does the source have output
 * @is_dynamic: is this a dynamic source
958
 * @use_queue: put a queue before raw output pads
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
 *
 * Check the source of @decoder and collect information about it.
 *
 * @is_raw will be set to TRUE if the source only produces raw pads. When this
 * function returns, all of the raw pad of the source will be added
 * to @decoder.
 *
 * @have_out: will be set to TRUE if the source has output pads.
 *
 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
 * on.
 *
 * Returns: FALSE if a fatal error occured while scanning.
 */
static gboolean
analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
975
    gboolean * have_out, gboolean * is_dynamic, gboolean use_queue)
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
{
  GstIterator *pads_iter;
  gboolean done = FALSE;
  gboolean res = TRUE;

  *have_out = FALSE;
  *is_raw = FALSE;
  *is_dynamic = FALSE;

  pads_iter = gst_element_iterate_src_pads (decoder->source);
  while (!done) {
    GstPad *pad;

    switch (gst_iterator_next (pads_iter, (gpointer) & pad)) {
      case GST_ITERATOR_ERROR:
        res = FALSE;
        /* FALLTROUGH */
      case GST_ITERATOR_DONE:
        done = TRUE;
        break;
      case GST_ITERATOR_RESYNC:
        /* reset results and resync */
        *have_out = FALSE;
        *is_raw = FALSE;
        *is_dynamic = FALSE;
        gst_iterator_resync (pads_iter);
        break;
      case GST_ITERATOR_OK:
        /* we now officially have an ouput pad */
        *have_out = TRUE;

        /* if FALSE, this pad has no caps and we continue with the next pad. */
1008
1009
        if (!has_all_raw_caps (pad, is_raw)) {
          gst_object_unref (pad);
1010
          break;
1011
        }
1012
1013

        /* caps on source pad are all raw, we can add the pad */
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
        if (*is_raw) {
          GstElement *outelem;

          if (use_queue) {
            GstPad *sinkpad;

            /* insert a queue element right before the raw pad */
            outelem = gst_element_factory_make ("queue2", "queue");
            gst_bin_add (GST_BIN_CAST (decoder), outelem);

            sinkpad = gst_element_get_static_pad (outelem, "sink");
            gst_pad_link (pad, sinkpad);
            gst_object_unref (sinkpad);
            gst_object_unref (pad);

            /* get the new raw srcpad */
            pad = gst_element_get_static_pad (outelem, "src");
          } else {
            outelem = decoder->source;
          }
          new_decoded_pad_cb (outelem, pad, FALSE, decoder);
        }
1036
        gst_object_unref (pad);
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
        break;
    }
  }
  gst_iterator_free (pads_iter);

  if (!*have_out) {
    GstElementClass *elemclass;
    GList *walk;

    /* element has no output pads, check for padtemplates that list SOMETIMES
     * pads. */
    elemclass = GST_ELEMENT_GET_CLASS (decoder->source);

    walk = gst_element_class_get_pad_template_list (elemclass);
    while (walk != NULL) {
      GstPadTemplate *templ;

      templ = (GstPadTemplate *) walk->data;
      if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
        if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES)
          *is_dynamic = TRUE;
        break;
      }
      walk = g_list_next (walk);
    }
  }

  return res;
}

static void
remove_decoders (GstURIDecodeBin * bin)
{
  GSList *walk;

1072
  for (walk = bin->decodebins; walk; walk = g_slist_next (walk)) {
1073
1074
1075
1076
1077
1078
    GstElement *decoder = GST_ELEMENT_CAST (walk->data);

    GST_DEBUG_OBJECT (bin, "removing old decoder element");
    gst_element_set_state (decoder, GST_STATE_NULL);
    gst_bin_remove (GST_BIN_CAST (bin), decoder);
  }
1079
1080
  g_slist_free (bin->decodebins);
  bin->decodebins = NULL;
1081
1082
}

1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
static void
remove_pads (GstURIDecodeBin * bin)
{
  GSList *walk;

  for (walk = bin->srcpads; walk; walk = g_slist_next (walk)) {
    GstPad *pad = GST_PAD_CAST (walk->data);

    GST_DEBUG_OBJECT (bin, "removing old pad");
    gst_pad_set_active (pad, FALSE);
    gst_element_remove_pad (GST_ELEMENT_CAST (bin), pad);
  }
  g_slist_free (bin->srcpads);
  bin->srcpads = NULL;
}

1099
1100
1101
1102
static void
proxy_unknown_type_signal (GstElement * element, GstPad * pad, GstCaps * caps,
    GstURIDecodeBin * dec)
{
1103
1104
  GST_DEBUG_OBJECT (dec, "unknown-type signaled");

1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
  g_signal_emit (G_OBJECT (dec),
      gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
}

static gboolean
proxy_autoplug_continue_signal (GstElement * element, GstPad * pad,
    GstCaps * caps, GstURIDecodeBin * dec)
{
  gboolean result;

  g_signal_emit (G_OBJECT (dec),
      gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, pad, caps,
      &result);
1118
1119
1120

  GST_DEBUG_OBJECT (dec, "autoplug-continue returned %d", result);

1121
1122
1123
1124
1125
1126
1127
  return result;
}

static GValueArray *
proxy_autoplug_factories_signal (GstElement * element, GstPad * pad,
    GstCaps * caps, GstURIDecodeBin * dec)
{
1128
  GValueArray *result;
1129
1130
1131
1132

  g_signal_emit (G_OBJECT (dec),
      gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, pad, caps,
      &result);
1133
1134
1135

  GST_DEBUG_OBJECT (dec, "autoplug-factories returned %p", result);

1136
1137
1138
  return result;
}

1139
static GstAutoplugSelectResult
1140
proxy_autoplug_select_signal (GstElement * element, GstPad * pad,
1141
    GstCaps * caps, GstElementFactory * factory, GstURIDecodeBin * dec)
1142
{
1143
  GstAutoplugSelectResult result;
1144
1145

  g_signal_emit (G_OBJECT (dec),
1146
      gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, factory,
1147
      &result);
1148
1149
1150

  GST_DEBUG_OBJECT (dec, "autoplug-select returned %d", result);

1151
1152
1153
  return result;
}

1154
1155
1156
static void
proxy_drained_signal (GstElement * element, GstURIDecodeBin * dec)
{
1157
1158
  GST_DEBUG_OBJECT (dec, "drained signaled");

1159
1160
1161
1162
  g_signal_emit (G_OBJECT (dec),
      gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
}

1163
/* make a decodebin and connect to all the signals */
1164
static GstElement *
1165
make_decoder (GstURIDecodeBin * decoder)
1166
{
1167
  GstElement *decodebin;
1168

1169
1170
  GST_LOG_OBJECT (decoder, "making new decodebin2");

1171
1172
1173
1174
1175
  /* now create the decoder element */
  decodebin = gst_element_factory_make ("decodebin2", NULL);
  if (!decodebin)
    goto no_decodebin;

1176
1177
1178
1179
  /* configure caps if we have any */
  if (decoder->caps)
    g_object_set (decodebin, "caps", decoder->caps, NULL);

1180
1181
1182
1183
1184
1185
1186
1187
1188
  /* connect signals to proxy */
  g_signal_connect (G_OBJECT (decodebin), "unknown-type",
      G_CALLBACK (proxy_unknown_type_signal), decoder);
  g_signal_connect (G_OBJECT (decodebin), "autoplug-continue",
      G_CALLBACK (proxy_autoplug_continue_signal), decoder);
  g_signal_connect (G_OBJECT (decodebin), "autoplug-factories",
      G_CALLBACK (proxy_autoplug_factories_signal), decoder);
  g_signal_connect (G_OBJECT (decodebin), "autoplug-select",
      G_CALLBACK (proxy_autoplug_select_signal), decoder);
1189
1190
  g_signal_connect (G_OBJECT (decodebin), "drained",
      G_CALLBACK (proxy_drained_signal), decoder);
1191

1192
1193
1194
  /* set up callbacks to create the links between decoded data
   * and video/audio/subtitle rendering/output. */
  g_signal_connect (G_OBJECT (decodebin),
1195
1196
1197
      "new-decoded-pad", G_CALLBACK (new_decoded_pad_cb), decoder);
  g_signal_connect (G_OBJECT (decodebin),
      "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
1198
1199
1200
  g_signal_connect (G_OBJECT (decodebin), "no-more-pads",
      G_CALLBACK (no_more_pads), decoder);
  g_signal_connect (G_OBJECT (decodebin),
1201
      "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
1202
  g_object_set_data (G_OBJECT (decodebin), "pending", "1");
1203
1204
  g_object_set (G_OBJECT (decodebin), "subtitle-encoding", decoder->encoding,
      NULL);
1205
  decoder->pending++;
1206
  GST_LOG_OBJECT (decoder, "have %d pending dynamic objects", decoder->pending);
1207

1208
  gst_bin_add (GST_BIN_CAST (decoder), decodebin);
1209

1210
  decoder->decodebins = g_slist_prepend (decoder->decodebins, decodebin);
1211

1212
  return decodebin;
1213
1214
1215
1216
1217
1218
1219
1220

  /* ERRORS */
no_decodebin:
  {
    GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
        (_("Could not create \"decodebin2\" element.")), (NULL));
    return NULL;
  }
1221
1222
1223
1224
1225
1226
1227
}

static void
type_found (GstElement * typefind, guint probability,
    GstCaps * caps, GstURIDecodeBin * decoder)
{
  GstElement *dec_elem, *queue;
1228
1229
  GstStructure *s;
  const gchar *media_type;
1230
1231
1232

  GST_DEBUG_OBJECT (decoder, "typefind found caps %" GST_PTR_FORMAT, caps);

1233
  dec_elem = make_decoder (decoder);
1234
1235
1236
1237
1238
1239
1240
1241
  if (!dec_elem)
    goto no_decodebin;

  queue = gst_element_factory_make ("queue2", NULL);
  if (!queue)
    goto no_queue2;

  g_object_set (G_OBJECT (queue), "use-buffering", TRUE, NULL);
1242
1243
1244
1245
1246
1247
1248
1249

  s = gst_caps_get_structure (caps, 0);
  media_type = gst_structure_get_name (s);

  GST_DEBUG_OBJECT (decoder, "check media-type %s, %d", media_type,
      decoder->download);

  if (IS_DOWNLOAD_MEDIA (media_type) && decoder->download) {
1250
1251
1252
1253
1254
1255
1256
1257
1258
    gchar *temp_template, *filename;
    const gchar *tmp_dir, *prgname;

    tmp_dir = g_get_tmp_dir ();
    prgname = g_get_prgname ();
    if (prgname == NULL)
      prgname = "GStreamer";

    filename = g_strdup_printf ("%s-XXXXXX", prgname);
1259
1260

    /* build our filename */
1261
1262
1263
1264
    temp_template = g_build_filename (tmp_dir, filename, NULL);

    GST_DEBUG_OBJECT (decoder, "enable download buffering in %s (%s, %s, %s)",
        temp_template, tmp_dir, prgname, filename);
1265
1266
1267

    /* configure progressive download for selected media types */
    g_object_set (G_OBJECT (queue), "temp-template", temp_template, NULL);
1268
1269

    g_free (filename);
1270
1271
    g_free (temp_template);
  }
1272

1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
  /* Disable max-size-buffers */
  g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);

  /* If buffer size or duration are set, set them on the queue2 element */
  if (decoder->buffer_size != -1)
    g_object_set (G_OBJECT (queue), "max-size-bytes",
        decoder->buffer_size, NULL);
  if (decoder->buffer_duration != -1)
    g_object_set (G_OBJECT (queue), "max-size-time",
        decoder->buffer_duration, NULL);

1284
1285
  gst_bin_add (GST_BIN_CAST (decoder), queue);

1286
  if (!gst_element_link_pads (typefind, "src", queue, "sink"))
1287
1288
    goto could_not_link;

1289
1290
1291
  /* to force caps on the decodebin element and avoid reparsing stuff by
   * typefind. It also avoids a deadlock in the way typefind activates pads in
   * the state change */
1292
1293
  g_object_set (G_OBJECT (dec_elem), "sink-caps", caps, NULL);

1294
  if (!gst_element_link_pads (queue, "src", dec_elem, "sink"))
1295
1296
1297
    goto could_not_link;

  gst_element_set_state (dec_elem, GST_STATE_PLAYING);
1298
1299
1300
  gst_element_set_state (queue, GST_STATE_PLAYING);

  do_async_done (decoder);
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315

  return;

  /* ERRORS */
no_decodebin:
  {
    /* error was posted */
    return;
  }
could_not_link:
  {
    GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
        (NULL), ("Can't link typefind to decodebin2 element"));
    return;
  }
1316
1317
1318
1319
no_queue2:
  {
    GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
        (_("Could not create \"queue2\" element.")), (NULL));
1320
1321
1322
1323
1324
1325
1326
1327
    return;
  }
}

/* setup a streaming source. This will first plug a typefind element to the
 * source. After we find the type, we decide to plug a queue2 and continue to
 * plug a decodebin2 starting from the found caps */
static gboolean
1328
setup_streaming (GstURIDecodeBin * decoder)
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
{
  GstElement *typefind;

  /* now create the decoder element */
  typefind = gst_element_factory_make ("typefind", NULL);
  if (!typefind)
    goto no_typefind;

  gst_bin_add (GST_BIN_CAST (decoder), typefind);

  if (!gst_element_link (decoder->source, typefind))
    goto could_not_link;

  decoder->typefind = typefind;

  /* connect a signal to find out when the typefind element found
   * a type */
  decoder->have_type_id =
      g_signal_connect (G_OBJECT (decoder->typefind), "have-type",
      G_CALLBACK (type_found), decoder);

1350
  do_async_start (decoder);
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365

  return TRUE;

  /* ERRORS */
no_typefind:
  {
    GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
        (_("Could not create \"typefind\" element.")), (NULL));
    return FALSE;
  }
could_not_link:
  {
    GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
        (NULL), ("Can't link source to typefind element"));
    return FALSE;
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
  }
}

static void
remove_source (GstURIDecodeBin * bin)
{
  GstElement *source = bin->source;

  if (source) {
    GST_DEBUG_OBJECT (bin, "removing old src element");
    gst_element_set_state (source, GST_STATE_NULL);
    gst_bin_remove (GST_BIN_CAST (bin), source);

    if (bin->src_np_sig_id) {
      g_signal_handler_disconnect (G_OBJECT (source), bin->src_np_sig_id);
      bin->src_np_sig_id = 0;
    }
    if (bin->src_nmp_sig_id) {
      g_signal_handler_disconnect (G_OBJECT (source), bin->src_nmp_sig_id);
      bin->src_nmp_sig_id = 0;
    }
    bin->source = NULL;
  }
}

/* is called when a dynamic source element created a new pad. */
static void
source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
{
  GstElement *decoder;
  gboolean is_raw;

1398
  GST_URI_DECODE_BIN_LOCK (bin);
1399
1400
1401
1402
1403
1404
  GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
      GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));

  /* if this is a pad with all raw caps, we can expose it */
  if (has_all_raw_caps (pad, &is_raw) && is_raw) {
    /* it's all raw, create output pads. */
1405
    GST_URI_DECODE_BIN_UNLOCK (bin);
1406
    new_decoded_pad_cb (element, pad, FALSE, bin);
1407
1408
1409
1410
    return;
  }

  /* not raw, create decoder */
1411
  decoder = make_decoder (bin);
1412
1413
1414
1415
1416
1417
1418
  if (!decoder)
    goto no_decodebin;

  /* and link to decoder */
  if (!gst_element_link (bin->source, decoder))
    goto could_not_link;

1419
1420
1421
  GST_DEBUG_OBJECT (bin, "linked decoder to new pad");

  gst_element_set_state (decoder, GST_STATE_PLAYING);
1422
  GST_URI_DECODE_BIN_UNLOCK (bin);
1423

1424
1425
1426
1427
1428
1429
  return;

  /* ERRORS */
no_decodebin:
  {
    /* error was posted */
1430
    GST_URI_DECODE_BIN_UNLOCK (bin);
1431
1432
1433
1434
1435
1436
    return;
  }
could_not_link:
  {
    GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
        (NULL), ("Can't link source to decoder element"));
1437
    GST_URI_DECODE_BIN_UNLOCK (bin);
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
    return;
  }
}

/* construct and run the source and decoder elements until we found
 * all the streams or until a preroll queue has been filled.
*/
static gboolean
setup_source (GstURIDecodeBin * decoder)
{
  gboolean is_raw, have_out, is_dynamic;

  GST_DEBUG_OBJECT (decoder, "setup source");

  /* delete old src */
  remove_source (decoder);

Wim Taymans's avatar
Wim Taymans committed
1455
1456
  decoder->pending = 0;

1457
1458
1459
1460
1461
1462
1463
1464
  /* create and configure an element that can handle the uri */
  if (!(decoder->source = gen_source_element (decoder)))
    goto no_source;

  /* state will be merged later - if file is not found, error will be
   * handled by the application right after. */
  gst_bin_add (GST_BIN_CAST (decoder), decoder->source);

Wim Taymans's avatar