gstdecodebin.c 59 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) <2004> 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
 *
 * 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.
 */

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

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

26
27
#include <string.h>
#include <gst/gst.h>
28
#include <gst/pbutils/pbutils.h>
29

30
31
#include "gstplay-marshal.h"

32
/* generic templates */
33
static GstStaticPadTemplate decoder_bin_sink_template =
34
35
36
37
38
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

39
static GstStaticPadTemplate decoder_bin_src_template =
40
41
42
43
44
45
46
47
GST_STATIC_PAD_TEMPLATE ("src%d",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);

GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
#define GST_CAT_DEFAULT gst_decode_bin_debug

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
48
49
50
51
52
#define GST_TYPE_DECODE_BIN             (gst_decode_bin_get_type())
#define GST_DECODE_BIN(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
#define GST_DECODE_BIN_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
#define GST_IS_DECODE_BIN(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
#define GST_IS_DECODE_BIN_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
53
54
55
56

typedef struct _GstDecodeBin GstDecodeBin;
typedef struct _GstDecodeBinClass GstDecodeBinClass;

57
58
59
60
61
/**
 * GstDecodeBin:
 *
 * Auto-plugging decoder element structure
 */
62
63
struct _GstDecodeBin
{
64
  GstBin bin;                   /* we extend GstBin */
65

66
  GstElement *typefind;         /* this holds the typefind object */
67
  GstElement *fakesink;
68

69
  GList *dynamics;              /* list of dynamic connections */
70

71
72
  GList *queues;                /* list of demuxer-decoder queues */

73
74
  GList *probes;                /* list of PadProbeData */

75
  GList *factories;             /* factories we can use for selecting elements */
76
  gint numpads;
77
  gint numwaiting;
78

79
  gboolean have_type;
80
  guint have_type_id;           /* signal id for the typefind element */
81
82

  gboolean shutting_down;       /* stop pluggin if we're shutting down */
83
84

  GType queue_type;             /* store the GType of queues, to aid in recognising them */
85
86

  GMutex *cb_mutex;             /* Mutex for multi-threaded callbacks, such as removing the fakesink */
87
88
89
90
91
};

struct _GstDecodeBinClass
{
  GstBinClass parent_class;
92

93
  /* signal we fire when a new pad has been decoded into raw audio/video */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
94
  void (*new_decoded_pad) (GstElement * element, GstPad * pad, gboolean last);
95
96
  /* signal we fire when a pad has been removed */
  void (*removed_decoded_pad) (GstElement * element, GstPad * pad);
97
  /* signal fired when we found a pad that we cannot decode */
98
  void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
99
100
101
102
103
};

/* signals */
enum
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
104
  SIGNAL_NEW_DECODED_PAD,
105
  SIGNAL_REMOVED_DECODED_PAD,
106
  SIGNAL_UNKNOWN_TYPE,
Wim Taymans's avatar
Wim Taymans committed
107
  SIGNAL_REDIRECT,
108
109
110
  LAST_SIGNAL
};

111
112
113
114
115
116
117
118

typedef struct
{
  GstPad *pad;
  gulong sigid;
  gboolean done;
} PadProbeData;

119
120
/* this structure is created for all dynamic pads that could get created
 * at runtime */
121
122
typedef struct
{
123
124
125
  GstDecodeBin *decode_bin;     /* pointer to ourself */

  GstElement *element;          /* the element sending the signal */
126
127
  gint np_sig_id;               /* signal id of new_pad */
  gint nmp_sig_id;              /* signal id of no_more_pads */
128
129
130

  GstPad *pad;                  /* the pad sending the signal */
  gint caps_sig_id;             /* signal id of caps */
131
132
133
}
GstDynamic;

134
135
136
static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
static void gst_decode_bin_init (GstDecodeBin * decode_bin);
static void gst_decode_bin_dispose (GObject * object);
137
static void gst_decode_bin_finalize (GObject * object);
138

139
140
static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
    GstStateChange transition);
141

142
static gboolean add_fakesink (GstDecodeBin * decode_bin);
143
144
static void remove_fakesink (GstDecodeBin * decode_bin);

145
static void dynamic_free (GstDynamic * dyn);
146
static void free_dynamics (GstDecodeBin * decode_bin);
147
148
static void type_found (GstElement * typefind, guint probability,
    GstCaps * caps, GstDecodeBin * decode_bin);
149
150
static GstElement *try_to_link_1 (GstDecodeBin * decode_bin,
    GstElement * origelement, GstPad * pad, GList * factories);
151
152
static void close_link (GstElement * element, GstDecodeBin * decode_bin);
static void close_pad_link (GstElement * element, GstPad * pad,
153
    GstCaps * caps, GstDecodeBin * decode_bin, gboolean more);
154
155
static void unlinked (GstPad * pad, GstPad * peerpad,
    GstDecodeBin * decode_bin);
Wim Taymans's avatar
Wim Taymans committed
156
157
static void new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic);
static void no_more_pads (GstElement * element, GstDynamic * dynamic);
158
static void new_caps (GstPad * pad, GParamSpec * unused, GstDynamic * dynamic);
159

160
static void queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin);
161
static void queue_underrun_cb (GstElement * queue, GstDecodeBin * decode_bin);
162

163
164
static gboolean is_demuxer_element (GstElement * srcelement);

165
static GstElementClass *parent_class;
166
static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
167

Stefan Kost's avatar
Stefan Kost committed
168
static const GstElementDetails gst_decode_bin_details =
169
170
171
GST_ELEMENT_DETAILS ("Decoder Bin",
    "Generic/Bin/Decoder",
    "Autoplug and decode to raw media",
172
    "Wim Taymans <wim.taymans@gmail.com>");
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212


static GType
gst_decode_bin_get_type (void)
{
  static GType gst_decode_bin_type = 0;

  if (!gst_decode_bin_type) {
    static const GTypeInfo gst_decode_bin_info = {
      sizeof (GstDecodeBinClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_decode_bin_class_init,
      NULL,
      NULL,
      sizeof (GstDecodeBin),
      0,
      (GInstanceInitFunc) gst_decode_bin_init,
      NULL
    };

    gst_decode_bin_type =
        g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
        &gst_decode_bin_info, 0);
  }

  return gst_decode_bin_type;
}

static void
gst_decode_bin_class_init (GstDecodeBinClass * klass)
{
  GObjectClass *gobject_klass;
  GstElementClass *gstelement_klass;
  GstBinClass *gstbin_klass;

  gobject_klass = (GObjectClass *) klass;
  gstelement_klass = (GstElementClass *) klass;
  gstbin_klass = (GstBinClass *) klass;

213
  parent_class = g_type_class_peek_parent (klass);
214

215
216
217
218
219
220
221
222
223
  /**
   * GstDecodeBin::new-decoded-pad:
   * @bin: The decodebin
   * @pad: The newly created pad
   * @islast: #TRUE if this is the last pad to be added. Deprecated.
   *
   * This signal gets emitted as soon as a new pad of the same type as one of
   * the valid 'raw' types is added.
   */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
224
225
226
227
  gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
      g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
228
      gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
229
      G_TYPE_BOOLEAN);
230
231
232
233
234
235
236
  /**
   * GstDecodeBin::removed-decoded-pad:
   * @bin: The decodebin
   * @pad: The pad that was removed
   *
   * This signal is emitted when a 'final' caps pad has been removed.
   */
237
238
239
240
241
  gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
      g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
      gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
242
243
244
245
246
247
248
249
250
251
  /**
   * GstDecodeBin::unknown-type:
   * @bin: The decodebin
   * @pad: The new pad containing caps that cannot be resolved to a 'final'
   *       stream type.
   * @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 decodebin.
   */
252
253
254
  gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
      g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
255
      NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
256
      GST_TYPE_PAD, GST_TYPE_CAPS);
257

258
  gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_decode_bin_dispose);
259
  gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_decode_bin_finalize);
260

261
262
  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&decoder_bin_sink_template));
263
264
265
266
267
268
269
270
271
  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&decoder_bin_src_template));

  gst_element_class_set_details (gstelement_klass, &gst_decode_bin_details);

  gstelement_klass->change_state =
      GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
}

272
273
/* check if the bin is dynamic.
 *
274
 * If there are no outstanding dynamic connections, the bin is
275
276
 * considered to be non-dynamic.
 */
277
278
279
280
281
282
static gboolean
gst_decode_bin_is_dynamic (GstDecodeBin * decode_bin)
{
  return decode_bin->dynamics != NULL;
}

283
284
/* the filter function for selecting the elements we can use in
 * autoplugging */
285
286
287
288
289
static gboolean
gst_decode_bin_factory_filter (GstPluginFeature * feature,
    GstDecodeBin * decode_bin)
{
  guint rank;
290
  const gchar *klass;
291

292
  /* we only care about element factories */
293
294
295
  if (!GST_IS_ELEMENT_FACTORY (feature))
    return FALSE;

296
  klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
297
  /* only demuxers, decoders and parsers can play */
298
  if (strstr (klass, "Demux") == NULL &&
299
300
      strstr (klass, "Decoder") == NULL && strstr (klass, "Parse") == NULL &&
      strstr (klass, "Depayloader") == NULL) {
301
302
303
    return FALSE;
  }

304
  /* only select elements with autoplugging rank */
305
  rank = gst_plugin_feature_get_rank (feature);
306
  if (rank < GST_RANK_MARGINAL)
307
308
309
310
311
    return FALSE;

  return TRUE;
}

312
/* function used to sort element features */
313
314
315
static gint
compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
{
316
  gint diff;
Wim Taymans's avatar
Wim Taymans committed
317
  const gchar *rname1, *rname2;
318
319
320
321

  diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
  if (diff != 0)
    return diff;
Wim Taymans's avatar
Wim Taymans committed
322
323
324
325
326
327
328

  rname1 = gst_plugin_feature_get_name (f1);
  rname2 = gst_plugin_feature_get_name (f2);

  diff = strcmp (rname2, rname1);

  return diff;
329
330
}

331
332
333
static void
print_feature (GstPluginFeature * feature)
{
Wim Taymans's avatar
Wim Taymans committed
334
335
336
337
338
  const gchar *rname;

  rname = gst_plugin_feature_get_name (feature);

  GST_DEBUG ("%s", rname);
339
340
}

341
342
343
344
345
static void
gst_decode_bin_init (GstDecodeBin * decode_bin)
{
  GList *factories;

346
347
  decode_bin->cb_mutex = g_mutex_new ();

348
  /* first filter out the interesting element factories */
349
  factories = gst_default_registry_feature_filter (
350
351
352
      (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
      FALSE, decode_bin);

353
  /* sort them according to their ranks */
354
  decode_bin->factories = g_list_sort (factories, (GCompareFunc) compare_ranks);
355
  /* do some debugging */
356
  g_list_foreach (decode_bin->factories, (GFunc) print_feature, NULL);
357

358
  /* we create the typefind element only once */
359
360
  decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
  if (!decode_bin->typefind) {
361
362
    g_warning ("can't find typefind element, decodebin will not work");
  } else {
363
    GstPad *pad, *gpad;
364

365
    /* add the typefind element */
366
367
    if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
      g_warning ("Could not add typefind element, decodebin will not work");
368
      gst_object_unref (decode_bin->typefind);
369
370
      decode_bin->typefind = NULL;
    }
371
372

    /* get the sinkpad */
373
    pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
374

375
    /* ghost the sink pad to ourself */
376
377
378
    gpad = gst_ghost_pad_new ("sink", pad);
    gst_pad_set_active (gpad, TRUE);
    gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
379

380
    gst_object_unref (pad);
381
382
383
384
385
386

    /* connect a signal to find out when the typefind element found
     * a type */
    decode_bin->have_type_id =
        g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
        G_CALLBACK (type_found), decode_bin);
387
  }
388
  add_fakesink (decode_bin);
389

390
  decode_bin->dynamics = NULL;
391
  decode_bin->queues = NULL;
392
  decode_bin->probes = NULL;
393
394
395
396
397
398
399
400
401
}

static void
gst_decode_bin_dispose (GObject * object)
{
  GstDecodeBin *decode_bin;

  decode_bin = GST_DECODE_BIN (object);

402
403
404
  if (decode_bin->factories)
    gst_plugin_feature_list_free (decode_bin->factories);
  decode_bin->factories = NULL;
405

406
  G_OBJECT_CLASS (parent_class)->dispose (object);
407
408
409
410
411

  /* our parent dispose might trigger new signals when pads are unlinked
   * etc. clean up the mess here. */
  /* FIXME do proper cleanup when going to NULL */
  free_dynamics (decode_bin);
412
413
}

414
415
416
417
418
419
420
421
422
423
static void
gst_decode_bin_finalize (GObject * object)
{
  GstDecodeBin *decode_bin = GST_DECODE_BIN (object);

  g_mutex_free (decode_bin->cb_mutex);

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

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
struct DynFind
{
  GstElement *elem;
  GstPad *pad;
};

static gint
find_dynamic (GstDynamic * dyn, struct DynFind *info)
{
  if (dyn->element == info->elem && dyn->pad == info->pad)
    return 0;
  return 1;
}

/* Add either an element (for dynamic pads/pad-added watching) or a
 * pad (for delayed caps/notify::caps watching) to the dynamic list,
 * taking care to ignore repeat entries so we don't end up handling a
 * pad twice, for example */
static void
dynamic_add (GstElement * element, GstPad * pad, GstDecodeBin * decode_bin)
Wim Taymans's avatar
Wim Taymans committed
444
445
{
  GstDynamic *dyn;
446
447
  struct DynFind find_info;
  GList *found;
Wim Taymans's avatar
Wim Taymans committed
448

449
  g_return_if_fail (element != NULL);
450

451
452
453
454
455
456
457
  /* do a search that this entry doesn't already exist */
  find_info.elem = element;
  find_info.pad = pad;
  found = g_list_find_custom (decode_bin->dynamics, &find_info,
      (GCompareFunc) find_dynamic);
  if (found != NULL)
    goto exit;
Wim Taymans's avatar
Wim Taymans committed
458

459
  /* take refs */
Wim Taymans's avatar
Wim Taymans committed
460
  dyn = g_new0 (GstDynamic, 1);
461
  dyn->element = gst_object_ref (element);
462
  dyn->decode_bin = gst_object_ref (decode_bin);
463
464
465
466
467
468
469
  if (pad) {
    dyn->pad = gst_object_ref (pad);
    GST_DEBUG_OBJECT (decode_bin, "dynamic create for pad %" GST_PTR_FORMAT,
        pad);
    dyn->caps_sig_id = g_signal_connect (G_OBJECT (pad), "notify::caps",
        G_CALLBACK (new_caps), dyn);
  } else {
470
471
    GST_DEBUG_OBJECT (decode_bin, "dynamic create for element %"
        GST_PTR_FORMAT, element);
472
473
474
475
476
477
478
479
    dyn->np_sig_id = g_signal_connect (G_OBJECT (element), "pad-added",
        G_CALLBACK (new_pad), dyn);
    dyn->nmp_sig_id = g_signal_connect (G_OBJECT (element), "no-more-pads",
        G_CALLBACK (no_more_pads), dyn);
  }

  /* and add this element to the dynamic elements */
  decode_bin->dynamics = g_list_prepend (decode_bin->dynamics, dyn);
Wim Taymans's avatar
Wim Taymans committed
480

481
482
483
484
485
486
487
488
489
  return;
exit:
  if (element) {
    GST_DEBUG_OBJECT (decode_bin, "Dynamic element already added: %"
        GST_PTR_FORMAT, element);
  } else {
    GST_DEBUG_OBJECT (decode_bin, "Dynamic pad already added: %"
        GST_PTR_FORMAT, pad);
  }
Wim Taymans's avatar
Wim Taymans committed
490
491
492
493
494
}

static void
dynamic_free (GstDynamic * dyn)
{
495
496
  GST_DEBUG_OBJECT (dyn->decode_bin, "dynamic free");

Wim Taymans's avatar
Wim Taymans committed
497
  /* disconnect signals */
498
499
500
501
502
503
504
505
506
507
508
509
510
  if (dyn->np_sig_id)
    g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->np_sig_id);
  if (dyn->nmp_sig_id)
    g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->nmp_sig_id);
  if (dyn->caps_sig_id)
    g_signal_handler_disconnect (G_OBJECT (dyn->pad), dyn->caps_sig_id);

  if (dyn->pad)
    gst_object_unref (dyn->pad);
  dyn->pad = NULL;
  if (dyn->element)
    gst_object_unref (dyn->element);
  dyn->element = NULL;
Wim Taymans's avatar
Wim Taymans committed
511

512
  gst_object_unref (dyn->decode_bin);
Wim Taymans's avatar
Wim Taymans committed
513
  dyn->decode_bin = NULL;
514

Wim Taymans's avatar
Wim Taymans committed
515
516
517
  g_free (dyn);
}

518
519
520
521
522
523
524
525
526
527
528
529
530
531
static void
free_dynamics (GstDecodeBin * decode_bin)
{
  GList *dyns;

  for (dyns = decode_bin->dynamics; dyns; dyns = g_list_next (dyns)) {
    GstDynamic *dynamic = (GstDynamic *) dyns->data;

    dynamic_free (dynamic);
  }
  g_list_free (decode_bin->dynamics);
  decode_bin->dynamics = NULL;
}

532
/* this function runs through the element factories and returns a list
533
 * of all elements that are able to sink the given caps
534
 */
535
536
537
538
539
540
static GList *
find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
{
  GList *factories;
  GList *to_try = NULL;

541
  /* loop over all the factories */
542
543
544
545
546
547
  for (factories = decode_bin->factories; factories;
      factories = g_list_next (factories)) {
    GstElementFactory *factory = GST_ELEMENT_FACTORY (factories->data);
    const GList *templates;
    GList *walk;

548
    /* get the templates from the element factory */
549
    templates = gst_element_factory_get_static_pad_templates (factory);
550
    for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
551
      GstStaticPadTemplate *templ = walk->data;
552

553
      /* we only care about the sink templates */
554
555
      if (templ->direction == GST_PAD_SINK) {
        GstCaps *intersect;
556
        GstCaps *tmpl_caps;
557

558
        /* try to intersect the caps with the caps of the template */
559
560
561
562
563
        tmpl_caps = gst_static_caps_get (&templ->static_caps);

        intersect = gst_caps_intersect (caps, tmpl_caps);
        gst_caps_unref (tmpl_caps);

564
        /* check if the intersection is empty */
565
        if (!gst_caps_is_empty (intersect)) {
566
          /* non empty intersection, we can use this element */
Wim Taymans's avatar
Wim Taymans committed
567
568
          to_try = g_list_prepend (to_try, factory);
          gst_caps_unref (intersect);
569
          break;
570
        }
Wim Taymans's avatar
Wim Taymans committed
571
        gst_caps_unref (intersect);
572
573
574
      }
    }
  }
Wim Taymans's avatar
Wim Taymans committed
575
576
  to_try = g_list_reverse (to_try);

577
578
579
  return to_try;
}

Wim Taymans's avatar
Wim Taymans committed
580
581
582
583
584
static gboolean
mimetype_is_raw (const gchar * mimetype)
{
  return g_str_has_prefix (mimetype, "video/x-raw") ||
      g_str_has_prefix (mimetype, "audio/x-raw") ||
585
      g_str_has_prefix (mimetype, "text/plain") ||
586
      g_str_has_prefix (mimetype, "text/x-pango-markup");
Wim Taymans's avatar
Wim Taymans committed
587
588
}

589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
static void
free_pad_probes (GstDecodeBin * decode_bin)
{
  GList *tmp;

  /* Remove pad probes */
  for (tmp = decode_bin->probes; tmp; tmp = g_list_next (tmp)) {
    PadProbeData *data = (PadProbeData *) tmp->data;

    gst_pad_remove_data_probe (data->pad, data->sigid);
    g_free (data);
  }
  g_list_free (decode_bin->probes);
  decode_bin->probes = NULL;
}

605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/* used when we need to remove a probe because the decoder we plugged failed
 * to activate */
static void
free_pad_probe_for_element (GstDecodeBin * decode_bin, GstElement * element)
{
  GList *l;

  for (l = decode_bin->probes; l != NULL; l = g_list_next (l)) {
    PadProbeData *data = (PadProbeData *) l->data;

    if (GST_ELEMENT_CAST (GST_PAD_PARENT (data->pad)) == element) {
      gst_pad_remove_data_probe (data->pad, data->sigid);
      decode_bin->probes = g_list_delete_link (decode_bin->probes, l);
      g_free (data);
      return;
    }
  }
}

624
static gboolean
625
626
627
add_fakesink (GstDecodeBin * decode_bin)
{
  if (decode_bin->fakesink != NULL)
628
    return TRUE;
629
630
631
632

  g_mutex_lock (decode_bin->cb_mutex);

  decode_bin->fakesink = gst_element_factory_make ("fakesink", "fakesink");
633
634
635
636
637
638
  if (!decode_bin->fakesink)
    goto no_fakesink;

  /* hacky, remove sink flag, we don't want our decodebin to become a sink
   * just because we add a fakesink element to make us ASYNC */
  GST_OBJECT_FLAG_UNSET (decode_bin->fakesink, GST_ELEMENT_IS_SINK);
639

640
641
642
643
644
  /* takes ownership */
  if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->fakesink)) {
    g_warning ("Could not add fakesink element, decodebin will not work");
    gst_object_unref (decode_bin->fakesink);
    decode_bin->fakesink = NULL;
645
646
  }
  g_mutex_unlock (decode_bin->cb_mutex);
647
  return TRUE;
648
649
650
651
652
653

  /* ERRORS */
no_fakesink:
  {
    g_warning ("can't find fakesink element, decodebin will not work");
    g_mutex_unlock (decode_bin->cb_mutex);
654
    return FALSE;
655
  }
656
657
}

658
static void
659
remove_fakesink (GstDecodeBin * decode_bin)
660
{
661
662
663
664
665
666
  gboolean removed_fakesink = FALSE;

  if (decode_bin->fakesink == NULL)
    return;

  g_mutex_lock (decode_bin->cb_mutex);
667
  if (decode_bin->fakesink) {
668
    GST_DEBUG_OBJECT (decode_bin, "Removing fakesink and marking state dirty");
669

670
671
672
    /* Lock the state to prevent it from changing state to non-NULL
     * before it's removed */
    gst_element_set_locked_state (decode_bin->fakesink, TRUE);
673
    /* setting the state to NULL is never async */
674
    gst_element_set_state (decode_bin->fakesink, GST_STATE_NULL);
675
    gst_bin_remove (GST_BIN (decode_bin), decode_bin->fakesink);
676
677
    decode_bin->fakesink = NULL;

678
679
680
681
682
    removed_fakesink = TRUE;
  }
  g_mutex_unlock (decode_bin->cb_mutex);

  if (removed_fakesink) {
683
    free_pad_probes (decode_bin);
684
  }
685
686
}

687
/* this should be implemented with _pad_block() */
688
689
static gboolean
pad_probe (GstPad * pad, GstMiniObject * data, GstDecodeBin * decode_bin)
690
{
691
692
693
694
695
696
697
  GList *tmp;
  gboolean alldone = TRUE;

  for (tmp = decode_bin->probes; tmp; tmp = g_list_next (tmp)) {
    PadProbeData *pdata = (PadProbeData *) tmp->data;

    if (pdata->pad == pad) {
698
699
700
      if (GST_IS_BUFFER (data)) {
        if (!pdata->done)
          decode_bin->numwaiting--;
701
        pdata->done = TRUE;
702
703
704
705
      } else if (GST_IS_EVENT (data) &&
          ((GST_EVENT_TYPE (data) == GST_EVENT_EOS) ||
              (GST_EVENT_TYPE (data) == GST_EVENT_TAG) ||
              (GST_EVENT_TYPE (data) == GST_EVENT_FLUSH_START))) {
706
        /* FIXME, what about NEWSEGMENT? really, use _pad_block()... */
707
708
        if (!pdata->done)
          decode_bin->numwaiting--;
709
        pdata->done = TRUE;
710
      }
711
712
713
714
715
    }

    if (!(pdata->done)) {
      GST_LOG_OBJECT (decode_bin, "Pad probe on pad %" GST_PTR_FORMAT
          " but pad %" GST_PTR_FORMAT " still needs data.", pad, pdata->pad);
716
      alldone = FALSE;
717
    }
718
  }
719
720
721
  if (alldone)
    remove_fakesink (decode_bin);
  return TRUE;
722
723
}

724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/* FIXME: this should be somehow merged with the queue code in
 * try_to_link_1() to reduce code duplication */
static GstPad *
add_raw_queue (GstDecodeBin * decode_bin, GstPad * pad)
{
  GstElement *queue = NULL;
  GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;

  queue = gst_element_factory_make ("queue", NULL);
  decode_bin->queue_type = G_OBJECT_TYPE (queue);

  g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);
  g_object_set (G_OBJECT (queue), "max-size-time", G_GINT64_CONSTANT (0), NULL);
  g_object_set (G_OBJECT (queue), "max-size-bytes", 8192, NULL);
  gst_bin_add (GST_BIN (decode_bin), queue);
  gst_element_set_state (queue, GST_STATE_READY);
  queuesinkpad = gst_element_get_static_pad (queue, "sink");
  queuesrcpad = gst_element_get_static_pad (queue, "src");

  if (gst_pad_link (pad, queuesinkpad) != GST_PAD_LINK_OK) {
744
745
    GST_WARNING_OBJECT (decode_bin,
        "Linking queue failed, trying without queue");
746
747
748
749
    gst_element_set_state (queue, GST_STATE_NULL);
    gst_object_unref (queuesrcpad);
    gst_object_unref (queuesinkpad);
    gst_bin_remove (GST_BIN (decode_bin), queue);
750
    return pad;
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  }

  decode_bin->queues = g_list_append (decode_bin->queues, queue);
  g_signal_connect (G_OBJECT (queue),
      "overrun", G_CALLBACK (queue_filled_cb), decode_bin);
  g_signal_connect (G_OBJECT (queue),
      "underrun", G_CALLBACK (queue_underrun_cb), decode_bin);

  gst_element_set_state (queue, GST_STATE_PAUSED);
  gst_object_unref (queuesinkpad);

  return queuesrcpad;
}

765
766
767
768
769
770
/* given a pad and a caps from an element, find the list of elements
 * that could connect to the pad
 *
 * If the pad has a raw format, this function will create a ghostpad
 * for the pad onto the decodebin.
 *
771
 * If no compatible elements could be found, this function will signal
772
773
 * the unknown_type signal.
 */
774
775
static void
close_pad_link (GstElement * element, GstPad * pad, GstCaps * caps,
776
    GstDecodeBin * decode_bin, gboolean more)
777
778
{
  GstStructure *structure;
779
  const gchar *mimetype;
Wim Taymans's avatar
Wim Taymans committed
780
781
  gchar *padname;
  gint diff;
782

Wim Taymans's avatar
Wim Taymans committed
783
784
785
786
787
788
  padname = gst_pad_get_name (pad);
  diff = strncmp (padname, "current_", 8);
  g_free (padname);

  /* hack.. ignore current pads */
  if (!diff)
789
790
    return;

791
792
  /* the caps is empty, this means the pad has no type, we can only
   * decide to fire the unknown_type signal. */
Wim Taymans's avatar
Wim Taymans committed
793
794
  if (caps == NULL || gst_caps_is_empty (caps))
    goto unknown_type;
795

796
797
  /* the caps is any, this means the pad can be anything and
   * we don't know yet */
Wim Taymans's avatar
Wim Taymans committed
798
799
  if (gst_caps_is_any (caps))
    goto dont_know_yet;
800
801
802

  GST_LOG_OBJECT (element, "trying to close %" GST_PTR_FORMAT, caps);

803
804
  /* FIXME, iterate over more structures? I guess it is possible that
   * this pad has some encoded and some raw pads. This code will fail
805
   * then if the first structure is not the raw type... */
806
807
808
  structure = gst_caps_get_structure (caps, 0);
  mimetype = gst_structure_get_name (structure);

809
  /* first see if this is raw. If the type is raw, we can
810
811
   * create a ghostpad for this pad. It's possible that the caps are not
   * fixed. */
Wim Taymans's avatar
Wim Taymans committed
812
  if (mimetype_is_raw (mimetype)) {
813
    gchar *padname;
814
    GstPad *ghost;
815
    PadProbeData *data;
816

817
818
819
820
821
822
823
824
825
826
827
    /* If we're at a demuxer element but have raw data already
     * we have to add a queue here. For non-raw data this is done
     * in try_to_link_1() */
    if (is_demuxer_element (element)) {
      GST_DEBUG_OBJECT (decode_bin,
          "Element %s is a demuxer, inserting a queue",
          GST_OBJECT_NAME (element));

      pad = add_raw_queue (decode_bin, pad);
    }

828
    /* make a unique name for this new pad */
829
830
831
    padname = g_strdup_printf ("src%d", decode_bin->numpads);
    decode_bin->numpads++;

832
    /* make it a ghostpad */
Andy Wingo's avatar
Andy Wingo committed
833
    ghost = gst_ghost_pad_new (padname, pad);
834
    gst_pad_set_active (ghost, TRUE);
Andy Wingo's avatar
Andy Wingo committed
835
    gst_element_add_pad (GST_ELEMENT (decode_bin), ghost);
836

837
838
839
840
    data = g_new0 (PadProbeData, 1);
    data->pad = pad;
    data->done = FALSE;

841
    /* FIXME, use _pad_block() */
842
843
844
845
846
    data->sigid = gst_pad_add_data_probe (pad, G_CALLBACK (pad_probe),
        decode_bin);
    decode_bin->numwaiting++;

    decode_bin->probes = g_list_append (decode_bin->probes, data);
847

848
849
    GST_LOG_OBJECT (element, "closed pad %s", padname);

850
    /* our own signal with an extra flag that this is the only pad */
851
    GST_DEBUG_OBJECT (decode_bin, "emitting new-decoded-pad");
852
    g_signal_emit (G_OBJECT (decode_bin),
853
        gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD], 0, ghost, !more);
854
    GST_DEBUG_OBJECT (decode_bin, "emitted new-decoded-pad");
855

856
    g_free (padname);
857
858
859
860
861

    /* If we're at a demuxer element pad was set to a queue's
     * srcpad and must be unref'd here */
    if (is_demuxer_element (element))
      gst_object_unref (pad);
Wim Taymans's avatar
Wim Taymans committed
862
863
864
865
  } else {
    GList *to_try;

    /* if the caps has many types, we need to delay */
866
    if (!gst_caps_is_fixed (caps))
Wim Taymans's avatar
Wim Taymans committed
867
      goto many_types;
868

Wim Taymans's avatar
Wim Taymans committed
869
    /* continue plugging, first find all compatible elements */
870
    to_try = find_compatibles (decode_bin, caps);
Wim Taymans's avatar
Wim Taymans committed
871
872
873
874
    if (to_try == NULL)
      /* no compatible elements, we cannot go on */
      goto unknown_type;

875
    if (try_to_link_1 (decode_bin, element, pad, to_try) == NULL) {
876
      g_list_free (to_try);
877
878
879
880
      GST_LOG_OBJECT (pad, "none of the allegedly available elements usable");
      goto unknown_type;
    }

Wim Taymans's avatar
Wim Taymans committed
881
882
883
884
885
    /* can free the list again now */
    g_list_free (to_try);
  }
  return;

886
  /* ERRORS */
Wim Taymans's avatar
Wim Taymans committed
887
888
unknown_type:
  {
889
    GST_LOG_OBJECT (pad, "unknown type found, fire signal");
Wim Taymans's avatar
Wim Taymans committed
890
891
    g_signal_emit (G_OBJECT (decode_bin),
        gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
892
893
894

    gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
        gst_missing_decoder_message_new (GST_ELEMENT_CAST (decode_bin), caps));
895
896
897
898
899
900
901
902
903
904
905
906
907

    if (element == decode_bin->typefind) {
      gchar *desc;

      desc = gst_pb_utils_get_decoder_description (caps);
      GST_ELEMENT_ERROR (decode_bin, STREAM, CODEC_NOT_FOUND,
          (_("A %s plugin is required to play this stream, but not installed."),
              desc),
          ("No decoder to handle media type '%s'",
              gst_structure_get_name (gst_caps_get_structure (caps, 0))));
      g_free (desc);
    }

Wim Taymans's avatar
Wim Taymans committed
908
909
910
911
    return;
  }
dont_know_yet:
  {
912
913
    GST_LOG_OBJECT (pad, "type is not known yet");
    goto setup_caps_delay;
Wim Taymans's avatar
Wim Taymans committed
914
915
916
  }
many_types:
  {
917
    GST_LOG_OBJECT (pad, "many possible types");
918
919
920
921
    goto setup_caps_delay;
  }
setup_caps_delay:
  {
922
    GST_LOG_OBJECT (pad, "setting up a delayed link");
923
    dynamic_add (element, pad, decode_bin);
Wim Taymans's avatar
Wim Taymans committed
924
    return;
925
926
927
  }
}

928
/* Decide whether an element is a demuxer based on the
929
930
931
932
933
934
 * klass and number/type of src pad templates it has */
static gboolean
is_demuxer_element (GstElement * srcelement)
{
  GstElementFactory *srcfactory;
  GstElementClass *elemclass;
935
  GList *walk;
936
937
938
939
940
941
942
  const gchar *klass;
  gint potential_src_pads = 0;

  srcfactory = gst_element_get_factory (srcelement);
  klass = gst_element_factory_get_klass (srcfactory);

  /* Can't be a demuxer unless it has Demux in the klass name */
943
  if (klass == NULL || !strstr (klass, "Demux"))
944
945
946
947
948
949
    return FALSE;

  /* Walk the src pad templates and count how many the element
   * might produce */
  elemclass = GST_ELEMENT_GET_CLASS (srcelement);

950
  walk = gst_element_class_get_pad_template_list (elemclass);
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
  while (walk != NULL) {
    GstPadTemplate *templ;

    templ = (GstPadTemplate *) walk->data;
    if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
      switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
        case GST_PAD_ALWAYS:
        case GST_PAD_SOMETIMES:
          if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
            potential_src_pads += 2;    /* Might make multiple pads */
          else
            potential_src_pads += 1;
          break;
        case GST_PAD_REQUEST:
          potential_src_pads += 2;
          break;
      }
    }
    walk = g_list_next (walk);
  }

  if (potential_src_pads < 2)
    return FALSE;

  return TRUE;
}

978
/*
Wim Taymans's avatar
Wim Taymans committed
979
980
981
 * given a list of element factories, try to link one of the factories
 * to the given pad.
 *
982
 * The function returns the element that was successfully linked to the
Wim Taymans's avatar
Wim Taymans committed
983
984
 * pad.
 */
985
static GstElement *
986
987
try_to_link_1 (GstDecodeBin * decode_bin, GstElement * srcelement, GstPad * pad,
    GList * factories)
988
989
{
  GList *walk;
990
  GstElement *result = NULL;
991
  gboolean isdemux = FALSE;
992
993
994
  GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
  GstElement *queue = NULL;
  GstPad *usedsrcpad = pad;
995
996

  /* Check if the parent of the src pad is a demuxer */
997
998
999
  isdemux = is_demuxer_element (srcelement);

  if (isdemux && factories != NULL) {
1000
1001
    GstPadLinkReturn dqlink;

1002
1003
1004
1005
1006
1007
1008
1009
    /* Insert a queue between demuxer and decoder */
    GST_DEBUG_OBJECT (decode_bin,
        "Element %s is a demuxer, inserting a queue",
        GST_OBJECT_NAME (srcelement));
    queue = gst_element_factory_make ("queue", NULL);
    decode_bin->queue_type = G_OBJECT_TYPE (queue);

    g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);
1010
1011
    g_object_set (G_OBJECT (queue), "max-size-time", G_GINT64_CONSTANT (0),
        NULL);
1012
1013
1014
    g_object_set (G_OBJECT (queue), "max-size-bytes", 8192, NULL);
    gst_bin_add (GST_BIN (decode_bin), queue);
    gst_element_set_state (queue, GST_STATE_READY);
1015
1016
    queuesinkpad = gst_element_get_static_pad (queue, "sink");
    usedsrcpad = queuesrcpad = gst_element_get_static_pad (queue, "src");
1017

1018
1019
    dqlink = gst_pad_link (pad, queuesinkpad);
    g_return_val_if_fail (dqlink == GST_PAD_LINK_OK, NULL);
1020
  }
1021

1022
  /* loop over the factories */
1023
1024
1025
  for (walk = factories; walk; walk = g_list_next (walk)) {
    GstElementFactory *factory = GST_ELEMENT_FACTORY (walk->data);
    GstElement *element;
Wim Taymans's avatar
Wim Taymans committed
1026
    GstPadLinkReturn ret;
1027
    GstPad *sinkpad;
1028

1029
    GST_DEBUG_OBJECT (decode_bin, "trying to link %s",
1030
        gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1031

1032
    /* make an element from the factory first */
1033
    if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1034
1035
      /* hmm, strange. Like with all things in life, let's move on.. */
      GST_WARNING_OBJECT (decode_bin, "could not create an element from %s",
1036
          gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1037
      continue;
1038
    }
1039

1040
    /* try to link the given pad to a sinkpad */
1041
    /* FIXME, find the sinkpad by looping over the pads instead of
1042
     * looking it up by name */
1043
    if ((sinkpad = gst_element_get_static_pad (element, "sink")) == NULL) {
1044
1045
1046
1047
1048
      /* if no pad is found we can't do anything */
      GST_WARNING_OBJECT (decode_bin, "could not find sinkpad in element");
      continue;
    }

1049
    /* now add the element to the bin first */
1050
    GST_DEBUG_OBJECT (decode_bin, "adding %s", GST_OBJECT_NAME (element));
1051
    gst_bin_add (GST_BIN (decode_bin), element);
1052

1053
    /* set to READY first so it is ready, duh. */
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
    if (gst_element_set_state (element,
            GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
      GST_WARNING_OBJECT (decode_bin, "Couldn't set %s to READY",
          GST_ELEMENT_NAME (element));
      /* get rid of the sinkpad */
      gst_object_unref (sinkpad);
      /* this element did not work, remove it again and continue trying
       * other elements, the element will be disposed. */
      /* FIXME: shouldn't we do this before adding it to the bin so that no
       * error messages get through to the app? (tpm) */
      gst_bin_remove (GST_BIN (decode_bin), element);
      continue;
    }