gstdecodebin2.c 144 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
3
 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 5
 * Copyright (C) <2011> Hewlett-Packard Development Company, L.P.
 *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
6 7
 * Copyright (C) <2013> Collabora Ltd.
 *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21 22
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
23 24 25
 */

/**
26
 * SECTION:element-decodebin
27
 *
28 29 30
 * #GstBin that auto-magically constructs a decoding pipeline using available
 * decoders and demuxers via auto-plugging.
 *
31 32
 * decodebin is considered stable now and replaces the old #decodebin element.
 * #uridecodebin uses decodebin internally and is often more convenient to
33
 * use, as it creates a suitable source element as well.
34 35
 */

36 37
/* Implementation notes:
 *
38
 * The following section describes how decodebin works internally.
39
 *
40
 * The first part of decodebin is its typefind element, which tries
41 42
 * to determine the media type of the input stream. If the type is found
 * autoplugging starts.
43
 *
44
 * decodebin internally organizes the elements it autoplugged into GstDecodeChains
45
 * and GstDecodeGroups. A decode chain is a single chain of decoding, this
46
 * means that if decodebin every autoplugs an element with two+ srcpads
47 48 49 50 51 52 53 54
 * (e.g. a demuxer) this will end the chain and everything following this
 * demuxer will be put into decode groups below the chain. Otherwise,
 * if an element has a single srcpad that outputs raw data the decode chain
 * is ended too and a GstDecodePad is stored and blocked.
 *
 * A decode group combines a number of chains that are created by a
 * demuxer element. All those chains are connected through a multiqueue to
 * the demuxer. A new group for the same demuxer is only created if the
55
 * demuxer has signaled no-more-pads, in which case all following pads
56 57 58 59 60
 * create a new chain in the new group.
 *
 * This continues until the top-level decode chain is complete. A decode
 * chain is complete if it either ends with a blocked endpad, if autoplugging
 * stopped because no suitable plugins could be found or if the active group
61 62
 * is complete. A decode group on the other hand is complete if all child
 * chains are complete.
63 64
 *
 * If this happens at some point, all endpads of all active groups are exposed.
65
 * For this decodebin adds the endpads, signals no-more-pads and then unblocks
66 67
 * them. Now playback starts.
 *
68
 * If one of the chains that end on a endpad receives EOS decodebin checks
69 70 71 72 73 74 75
 * if all chains and groups are drained. In that case everything goes into EOS.
 * If there is a chain where the active group is drained but there exist next
 * groups, the active group is hidden (endpads are removed) and the next group
 * is exposed. This means that in some cases more pads may be created even
 * after the initial no-more-pads signal. This happens for example with
 * so-called "chained oggs", most commonly found among ogg/vorbis internet
 * radio streams.
76 77 78 79 80 81 82 83 84 85
 *
 * Note 1: If we're talking about blocked endpads this really means that the
 * *target* pads of the endpads are blocked. Pads that are exposed to the outside
 * should never ever be blocked!
 *
 * Note 2: If a group is complete and the parent's chain demuxer adds new pads
 * but never signaled no-more-pads this additional pads will be ignored!
 *
 */

86 87 88 89
/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
 * with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS

90 91 92 93
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

96 97
#include <string.h>
#include <gst/gst.h>
98
#include <gst/pbutils/pbutils.h>
99

100
#include "gstplay-enum.h"
101
#include "gstplayback.h"
102
#include "gstrawcaps.h"
103

104 105 106
/* Also used by gsturidecodebin.c */
gint _decode_bin_compare_factories_func (gconstpointer p1, gconstpointer p2);

107 108 109 110 111 112 113 114
/* generic templates */
static GstStaticPadTemplate decoder_bin_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

static GstStaticPadTemplate decoder_bin_src_template =
115
GST_STATIC_PAD_TEMPLATE ("src_%u",
116 117 118 119 120 121 122
    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

123
typedef struct _GstPendingPad GstPendingPad;
124
typedef struct _GstDecodeElement GstDecodeElement;
125
typedef struct _GstDecodeChain GstDecodeChain;
126 127
typedef struct _GstDecodeGroup GstDecodeGroup;
typedef struct _GstDecodePad GstDecodePad;
128
typedef GstGhostPadClass GstDecodePadClass;
129 130 131 132
typedef struct _GstDecodeBin GstDecodeBin;
typedef struct _GstDecodeBinClass GstDecodeBinClass;

#define GST_TYPE_DECODE_BIN             (gst_decode_bin_get_type())
133
#define GST_DECODE_BIN_CAST(obj)        ((GstDecodeBin*)(obj))
134 135 136 137 138
#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))

139
/**
140
 *  GstDecodeBin:
141
 *
142
 *  The opaque #GstDecodeBin data structure
143
 */
144 145 146 147
struct _GstDecodeBin
{
  GstBin bin;                   /* we extend GstBin */

148 149 150
  /* properties */
  GstCaps *caps;                /* caps on which to stop decoding */
  gchar *encoding;              /* encoding of subtitles */
151 152 153 154 155 156
  gboolean use_buffering;       /* configure buffering on multiqueues */
  gint low_percent;
  gint high_percent;
  guint max_size_bytes;
  guint max_size_buffers;
  guint64 max_size_time;
157
  gboolean post_stream_topology;
158
  guint64 connection_speed;
159

160 161
  GstElement *typefind;         /* this holds the typefind object */

162
  GMutex expose_lock;           /* Protects exposal and removal of groups */
163
  GstDecodeChain *decode_chain; /* Top level decode chain */
164
  guint nbpads;                 /* unique identifier for source pads */
165

166
  GMutex factories_lock;
167
  guint32 factories_cookie;     /* Cookie from last time when factories was updated */
168
  GList *factories;             /* factories we can use for selecting elements */
169

170
  GMutex subtitle_lock;         /* Protects changes to subtitles and encoding */
171
  GList *subtitles;             /* List of elements with subtitle-encoding,
172
                                 * protected by above mutex! */
173

174
  gboolean have_type;           /* if we received the have_type signal */
175
  guint have_type_id;           /* signal id for have-type from typefind */
176

Piotr Fusik's avatar
Piotr Fusik committed
177
  gboolean async_pending;       /* async-start has been emitted */
178

179
  GMutex dyn_lock;              /* lock protecting pad blocking */
180 181
  gboolean shutdown;            /* if we are shutting down */
  GList *blocked_pads;          /* pads that have set to block */
182 183

  gboolean expose_allstreams;   /* Whether to expose unknow type streams or not */
184

185
  GList *filtered;              /* elements for which error messages are filtered */
186 187

  GList *buffering_status;      /* element currently buffering messages */
188 189 190 191 192 193 194 195
};

struct _GstDecodeBinClass
{
  GstBinClass parent_class;

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

197
  /* signal fired to know if we continue trying to decode the given caps */
198 199 200 201 202
    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);
203 204 205
  /* signal fired to sort the factories */
  GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
      GstCaps * caps, GValueArray * factories);
206
  /* signal fired to select from the proposed list of factories */
207 208
    GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
      GstPad * pad, GstCaps * caps, GstElementFactory * factory);
209 210 211 212
  /* 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);
213 214 215

  /* fired when the last group is drained */
  void (*drained) (GstElement * element);
216 217 218 219 220 221 222
};

/* signals */
enum
{
  SIGNAL_UNKNOWN_TYPE,
  SIGNAL_AUTOPLUG_CONTINUE,
223 224
  SIGNAL_AUTOPLUG_FACTORIES,
  SIGNAL_AUTOPLUG_SELECT,
225
  SIGNAL_AUTOPLUG_SORT,
226
  SIGNAL_AUTOPLUG_QUERY,
227
  SIGNAL_DRAINED,
228 229 230
  LAST_SIGNAL
};

231 232
/* automatic sizes, while prerolling we buffer up to 2MB, we ignore time
 * and buffers in this case. */
233 234
#define AUTO_PREROLL_SIZE_BYTES                  2 * 1024 * 1024
#define AUTO_PREROLL_SIZE_BUFFERS                0
235 236
#define AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME      10 * GST_SECOND
#define AUTO_PREROLL_SEEKABLE_SIZE_TIME          0
237

238
/* when playing, keep a max of 2MB of data but try to keep the number of buffers
239 240 241
 * as low as possible (try to aim for 5 buffers) */
#define AUTO_PLAY_SIZE_BYTES        2 * 1024 * 1024
#define AUTO_PLAY_SIZE_BUFFERS      5
242
#define AUTO_PLAY_ADAPTIVE_SIZE_BUFFERS 2
243 244
#define AUTO_PLAY_SIZE_TIME         0

245 246 247 248
#define DEFAULT_SUBTITLE_ENCODING NULL
#define DEFAULT_USE_BUFFERING     FALSE
#define DEFAULT_LOW_PERCENT       10
#define DEFAULT_HIGH_PERCENT      99
249 250
/* by default we use the automatic values above */
#define DEFAULT_MAX_SIZE_BYTES    0
251 252
#define DEFAULT_MAX_SIZE_BUFFERS  0
#define DEFAULT_MAX_SIZE_TIME     0
253
#define DEFAULT_POST_STREAM_TOPOLOGY FALSE
254
#define DEFAULT_EXPOSE_ALL_STREAMS  TRUE
255
#define DEFAULT_CONNECTION_SPEED    0
256

257 258 259 260 261
/* Properties */
enum
{
  PROP_0,
  PROP_CAPS,
262 263
  PROP_SUBTITLE_ENCODING,
  PROP_SINK_CAPS,
264 265 266 267 268 269
  PROP_USE_BUFFERING,
  PROP_LOW_PERCENT,
  PROP_HIGH_PERCENT,
  PROP_MAX_SIZE_BYTES,
  PROP_MAX_SIZE_BUFFERS,
  PROP_MAX_SIZE_TIME,
270
  PROP_POST_STREAM_TOPOLOGY,
271
  PROP_EXPOSE_ALL_STREAMS,
272
  PROP_CONNECTION_SPEED,
273
  PROP_LAST
274 275 276 277 278
};

static GstBinClass *parent_class;
static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };

279 280
static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);

281 282
static void do_async_start (GstDecodeBin * dbin);
static void do_async_done (GstDecodeBin * dbin);
283 284 285 286

static void type_found (GstElement * typefind, guint probability,
    GstCaps * caps, GstDecodeBin * decode_bin);

287
static void decodebin_set_queue_size (GstDecodeBin * dbin,
288 289
    GstElement * multiqueue, gboolean preroll, gboolean seekable,
    gboolean adaptive_streaming);
290

291
static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
292 293 294
    GstPad * pad, GstCaps * caps);
static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
    element, GstPad * pad, GstCaps * caps);
295 296 297 298
static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
    GstPad * pad, GstCaps * caps, GValueArray * factories);
static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
    element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
299 300
static gboolean gst_decode_bin_autoplug_query (GstElement * element,
    GstPad * pad, GstQuery * query);
301

302 303 304 305 306 307
static void gst_decode_bin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_decode_bin_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
308
static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
309
    GstDecodeChain * chain);
310 311 312 313

static GstPad *find_sink_pad (GstElement * element);
static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
    GstStateChange transition);
314
static void gst_decode_bin_handle_message (GstBin * bin, GstMessage * message);
315

316 317
static gboolean check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad);

318
#define EXPOSE_LOCK(dbin) G_STMT_START {				\
319
    GST_LOG_OBJECT (dbin,						\
320
		    "expose locking from thread %p",			\
321
		    g_thread_self ());					\
322
    g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->expose_lock);		\
323
    GST_LOG_OBJECT (dbin,						\
324
		    "expose locked from thread %p",			\
325 326 327
		    g_thread_self ());					\
} G_STMT_END

328
#define EXPOSE_UNLOCK(dbin) G_STMT_START {				\
329
    GST_LOG_OBJECT (dbin,						\
330
		    "expose unlocking from thread %p",			\
331
		    g_thread_self ());					\
332
    g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->expose_lock);		\
333 334
} G_STMT_END

335
#define DYN_LOCK(dbin) G_STMT_START {			\
336 337 338
    GST_LOG_OBJECT (dbin,						\
		    "dynlocking from thread %p",			\
		    g_thread_self ());					\
339
    g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock);			\
340 341 342 343 344
    GST_LOG_OBJECT (dbin,						\
		    "dynlocked from thread %p",				\
		    g_thread_self ());					\
} G_STMT_END

345
#define DYN_UNLOCK(dbin) G_STMT_START {			\
346 347 348
    GST_LOG_OBJECT (dbin,						\
		    "dynunlocking from thread %p",			\
		    g_thread_self ());					\
349
    g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock);		\
350 351
} G_STMT_END

352 353 354 355
#define SUBTITLE_LOCK(dbin) G_STMT_START {				\
    GST_LOG_OBJECT (dbin,						\
		    "subtitle locking from thread %p",			\
		    g_thread_self ());					\
356
    g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock);		\
357 358 359 360 361 362 363 364 365
    GST_LOG_OBJECT (dbin,						\
		    "subtitle lock from thread %p",			\
		    g_thread_self ());					\
} G_STMT_END

#define SUBTITLE_UNLOCK(dbin) G_STMT_START {				\
    GST_LOG_OBJECT (dbin,						\
		    "subtitle unlocking from thread %p",		\
		    g_thread_self ());					\
366
    g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock);		\
367 368
} G_STMT_END

369 370 371 372 373
struct _GstPendingPad
{
  GstPad *pad;
  GstDecodeChain *chain;
  gulong event_probe_id;
374
  gulong notify_caps_id;
375 376
};

377 378 379 380
struct _GstDecodeElement
{
  GstElement *element;
  GstElement *capsfilter;       /* Optional capsfilter for Parser/Convert */
381 382 383
  gulong pad_added_id;
  gulong pad_removed_id;
  gulong no_more_pads_id;
384 385
};

386 387 388 389
/* GstDecodeGroup
 *
 * Streams belonging to the same group/chain of a media file
 *
390
 * When changing something here lock the parent chain!
391 392 393 394
 */
struct _GstDecodeGroup
{
  GstDecodeBin *dbin;
395 396 397
  GstDecodeChain *parent;

  GstElement *multiqueue;       /* Used for linking all child chains */
398
  gulong overrunsig;            /* the overrun signal for multiqueue */
399

400 401 402 403 404 405 406 407 408 409
  gboolean overrun;             /* TRUE if the multiqueue signaled overrun. This
                                 * means that we should really expose the group */

  gboolean no_more_pads;        /* TRUE if the demuxer signaled no-more-pads */
  gboolean drained;             /* TRUE if the all children are drained */

  GList *children;              /* List of GstDecodeChains in this group */

  GList *reqpads;               /* List of RequestPads for multiqueue, there is
                                 * exactly one RequestPad per child chain */
410 411
};

412 413 414 415
struct _GstDecodeChain
{
  GstDecodeGroup *parent;
  GstDecodeBin *dbin;
416

417
  GMutex lock;                  /* Protects this chain and its groups */
418

419 420
  GstPad *pad;                  /* srcpad that caused creation of this chain */

421
  gboolean drained;             /* TRUE if the all children are drained */
422
  gboolean demuxer;             /* TRUE if elements->data is a demuxer */
423
  gboolean adaptive_demuxer;    /* TRUE if elements->data is an adaptive streaming demuxer */
424
  gboolean seekable;            /* TRUE if this chain ends on a demuxer and is seekable */
425 426
  GList *elements;              /* All elements in this group, first
                                   is the latest and most downstream element */
427

428 429 430 431 432 433 434 435 436 437 438 439
  /* Note: there are only groups if the last element of this chain
   * is a demuxer, otherwise the chain will end with an endpad.
   * The other way around this means, that endpad only exists if this
   * chain doesn't end with a demuxer! */

  GstDecodeGroup *active_group; /* Currently active group */
  GList *next_groups;           /* head is newest group, tail is next group.
                                   a new group will be created only if the head
                                   group had no-more-pads. If it's only exposed
                                   all new pads will be ignored! */
  GList *pending_pads;          /* Pads that have no fixed caps yet */

440 441 442
  GstDecodePad *current_pad;    /* Current ending pad of the chain that can't
                                 * be exposed yet but would be the same as endpad
                                 * once it can be exposed */
443 444 445
  GstDecodePad *endpad;         /* Pad of this chain that could be exposed */
  gboolean deadend;             /* This chain is incomplete and can't be completed,
                                   e.g. no suitable decoder could be found
446
                                   e.g. stream got EOS without buffers
447
                                 */
448 449 450
  GstCaps *endcaps;             /* Caps that were used when linking to the endpad
                                   or that resulted in the deadend
                                 */
451 452 453 454 455 456 457

  /* FIXME: This should be done directly via a thread! */
  GList *old_groups;            /* Groups that should be freed later */
};

static void gst_decode_chain_free (GstDecodeChain * chain);
static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
458
    GstDecodeGroup * group, GstPad * pad);
459 460
static void gst_decode_group_hide (GstDecodeGroup * group);
static void gst_decode_group_free (GstDecodeGroup * group);
461 462 463 464
static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
    GstDecodeChain * chain);
static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
465
    GList ** endpads, gboolean * missing_plugin);
466
static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
467
static gboolean gst_decode_chain_reset_buffering (GstDecodeChain * chain);
468 469 470 471
static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
    GstPad * pad);
static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
472
static gboolean gst_decode_group_reset_buffering (GstDecodeGroup * group);
473 474

static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
475
static void gst_decode_bin_reset_buffering (GstDecodeBin * dbin);
476 477 478 479 480

#define CHAIN_MUTEX_LOCK(chain) G_STMT_START {				\
    GST_LOG_OBJECT (chain->dbin,					\
		    "locking chain %p from thread %p",			\
		    chain, g_thread_self ());				\
481
    g_mutex_lock (&chain->lock);						\
482 483 484 485 486 487 488 489 490
    GST_LOG_OBJECT (chain->dbin,					\
		    "locked chain %p from thread %p",			\
		    chain, g_thread_self ());				\
} G_STMT_END

#define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START {                        \
    GST_LOG_OBJECT (chain->dbin,					\
		    "unlocking chain %p from thread %p",		\
		    chain, g_thread_self ());				\
491
    g_mutex_unlock (&chain->lock);					\
492
} G_STMT_END
493 494 495

/* GstDecodePad
 *
496
 * GstPad private used for source pads of chains
497 498 499
 */
struct _GstDecodePad
{
500 501
  GstGhostPad parent;
  GstDecodeBin *dbin;
502
  GstDecodeChain *chain;
503

504 505
  gboolean blocked;             /* the *target* pad is blocked */
  gboolean exposed;             /* the pad is exposed */
506
  gboolean drained;             /* an EOS has been seen on the pad */
507 508

  gulong block_id;
509 510
};

511
GType gst_decode_pad_get_type (void);
512 513 514 515
G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
#define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
#define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))

516
static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin,
517
    GstDecodeChain * chain);
518
static void gst_decode_pad_activate (GstDecodePad * dpad,
519
    GstDecodeChain * chain);
520
static void gst_decode_pad_unblock (GstDecodePad * dpad);
521
static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
522 523
static gboolean gst_decode_pad_query (GstPad * pad, GstObject * parent,
    GstQuery * query);
524

525
static void gst_pending_pad_free (GstPendingPad * ppad);
Wim Taymans's avatar
Wim Taymans committed
526 527
static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
    gpointer data);
528

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
/********************************
 * Standard GObject boilerplate *
 ********************************/

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);
static void gst_decode_bin_finalize (GObject * object);

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 =
558
        g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
        &gst_decode_bin_info, 0);
  }

  return gst_decode_bin_type;
}

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

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
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;
}

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
/* we collect the first result */
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);

619 620 621 622 623 624 625
  /* 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;

626 627 628
  return FALSE;
}

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
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;
}

645 646 647 648 649
static void
gst_decode_bin_class_init (GstDecodeBinClass * klass)
{
  GObjectClass *gobject_klass;
  GstElementClass *gstelement_klass;
650
  GstBinClass *gstbin_klass;
651 652 653

  gobject_klass = (GObjectClass *) klass;
  gstelement_klass = (GstElementClass *) klass;
654
  gstbin_klass = (GstBinClass *) klass;
655 656 657

  parent_class = g_type_class_peek_parent (klass);

658 659 660 661
  gobject_klass->dispose = gst_decode_bin_dispose;
  gobject_klass->finalize = gst_decode_bin_finalize;
  gobject_klass->set_property = gst_decode_bin_set_property;
  gobject_klass->get_property = gst_decode_bin_get_property;
662

663
  /**
664
   * GstDecodeBin::unknown-type:
665
   * @bin: The decodebin.
666 667
   * @pad: The new pad containing caps that cannot be resolved to a 'final'
   *       stream type.
668
   * @caps: The #GstCaps of the pad that cannot be resolved.
669 670 671 672
   *
   * This signal is emitted when a pad for which there is no further possible
   * decoding is added to the decodebin.
   */
673 674 675
  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),
676
      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
677
      GST_TYPE_PAD, GST_TYPE_CAPS);
678 679

  /**
680
   * GstDecodeBin::autoplug-continue:
681
   * @bin: The decodebin.
682
   * @pad: The #GstPad.
683 684
   * @caps: The #GstCaps found.
   *
685
   * This signal is emitted whenever decodebin finds a new stream. It is
686 687
   * emitted before looking for any elements that can handle that stream.
   *
688 689 690 691 692 693
   * <note>
   *   Invocation of signal handlers stops after the first signal handler
   *   returns #FALSE. Signal handlers are invoked in the order they were
   *   connected in.
   * </note>
   *
694
   * Returns: #TRUE if you wish decodebin to look for elements that can
695
   * handle the given @caps. If #FALSE, those caps will be considered as
696 697
   * final and the pad will be exposed as such (see 'pad-added' signal of
   * #GstElement).
698
   */
699 700 701
  gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
      g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
702
      _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
703 704 705
      G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);

  /**
706
   * GstDecodeBin::autoplug-factories:
707
   * @bin: The decodebin.
708 709 710 711
   * @pad: The #GstPad.
   * @caps: The #GstCaps found.
   *
   * This function is emited when an array of possible factories for @caps on
712
   * @pad is needed. Decodebin will by default return an array with all
713
   * compatible factories, sorted by rank.
714 715 716
   *
   * If this function returns NULL, @pad will be exposed as a final caps.
   *
717
   * If this function returns an empty array, the pad will be considered as
718
   * having an unhandled type media type.
719 720 721 722 723 724
   *
   * <note>
   *   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!
   * </note>
725 726 727 728 729 730 731 732
   *
   * 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_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
      g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
733
          autoplug_factories), _gst_array_accumulator, NULL,
734
      g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
735
      GST_TYPE_PAD, GST_TYPE_CAPS);
736

737
  /**
738
   * GstDecodeBin::autoplug-sort:
739
   * @bin: The decodebin.
740 741 742 743
   * @pad: The #GstPad.
   * @caps: The #GstCaps.
   * @factories: A #GValueArray of possible #GstElementFactory to use.
   *
744
   * Once decodebin has found the possible #GstElementFactory objects to try
745 746 747 748
   * 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.
   *
749 750 751 752 753 754 755 756 757 758
   * The callee should copy and modify @factories or return #NULL if the
   * order should not change.
   *
   * <note>
   *   Invocation of signal handlers stops after one signal handler has
   *   returned something else than #NULL. Signal handlers are invoked in
   *   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!
   * </note>
759 760 761 762 763 764
   *
   * Returns: A new sorted array of #GstElementFactory objects.
   */
  gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
      g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
765
      _gst_array_hasvalue_accumulator, NULL,
766 767
      g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
      GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
768

769
  /**
770
   * GstDecodeBin::autoplug-select:
771
   * @bin: The decodebin.
772
   * @pad: The #GstPad.
773
   * @caps: The #GstCaps.
774
   * @factory: A #GstElementFactory to use.
775
   *
776
   * This signal is emitted once decodebin has found all the possible
777
   * #GstElementFactory that can be used to handle the given @caps. For each of
Piotr Fusik's avatar
Piotr Fusik committed
778
   * those factories, this signal is emitted.
779
   *
780
   * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
781
   * value indicating what decodebin should do next.
782 783 784 785 786 787 788 789 790 791
   *
   * 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.
   *
792
   * <note>
793 794 795 796 797
   *   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.
798 799
   * </note>
   *
800 801 802
   * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
   * operation. the default handler will always return
   * #GST_AUTOPLUG_SELECT_TRY.
803
   */
804 805 806
  gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
      g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
807
      _gst_select_accumulator, NULL,
808
      g_cclosure_marshal_generic,
809 810
      GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
      GST_TYPE_ELEMENT_FACTORY);
811

812 813 814 815 816
  /**
   * GstDecodeBin::autoplug-query:
   * @bin: The decodebin.
   * @child: The child element doing the query
   * @pad: The #GstPad.
817
   * @element: The #GstElement.
818 819 820 821 822 823 824 825 826 827 828 829 830
   * @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.
   *
   * Returns: #TRUE if the query was handled, #FALSE otherwise.
   */
  gst_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
      g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_query),
      _gst_boolean_or_accumulator, NULL, g_cclosure_marshal_generic,
831
      G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
832 833
      GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);

834
  /**
835
   * GstDecodeBin::drained
836
   * @bin: The decodebin
837
   *
838
   * This signal is emitted once decodebin has finished decoding all the data.
839 840 841 842
   */
  gst_decode_bin_signals[SIGNAL_DRAINED] =
      g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
843
      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
844

845 846
  g_object_class_install_property (gobject_klass, PROP_CAPS,
      g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
847
          GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
848

849 850 851 852 853
  g_object_class_install_property (gobject_klass, 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, "
854 855
          "ISO-8859-15 will be assumed.", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
856

857 858 859 860 861
  g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
      g_param_spec_boxed ("sink-caps", "Sink Caps",
          "The caps of the input data. (NULL = use typefind element)",
          GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

862
  /**
863
   * GstDecodeBin::use-buffering
864
   *
865
   * Activate buffering in decodebin. This will instruct the multiqueues behind
866 867 868 869
   * decoders to emit BUFFERING messages.
   */
  g_object_class_install_property (gobject_klass, PROP_USE_BUFFERING,
      g_param_spec_boolean ("use-buffering", "Use Buffering",
870
          "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
871 872 873
          DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
874
   * GstDecodeBin:low-percent
875
   *
876
   * Low threshold percent for buffering to start.
877 878 879
   */
  g_object_class_install_property (gobject_klass, PROP_LOW_PERCENT,
      g_param_spec_int ("low-percent", "Low percent",
880
          "Low threshold for buffering to start", 0, 100,
881 882
          DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  /**
883
   * GstDecodeBin:high-percent
884
   *
885
   * High threshold percent for buffering to finish.
886 887 888
   */
  g_object_class_install_property (gobject_klass, PROP_HIGH_PERCENT,
      g_param_spec_int ("high-percent", "High percent",
889
          "High threshold for buffering to finish", 0, 100,
890 891 892
          DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
893
   * GstDecodeBin:max-size-bytes
894
   *
Piotr Fusik's avatar
Piotr Fusik committed
895
   * Max amount of bytes in the queue (0=automatic).
896 897 898
   */
  g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BYTES,
      g_param_spec_uint ("max-size-bytes", "Max. size (bytes)",
899 900
          "Max. amount of bytes in the queue (0=automatic)",
          0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,