gstdecodebin2.c 163 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
  GList *filtered_errors;       /* filtered error messages */
187 188

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

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

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

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

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

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

239
/* when playing, keep a max of 2MB of data but try to keep the number of buffers
240 241 242 243 244
 * as low as possible (try to aim for 5 buffers) */
#define AUTO_PLAY_SIZE_BYTES        2 * 1024 * 1024
#define AUTO_PLAY_SIZE_BUFFERS      5
#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 274 275 276 277
};

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

278 279
static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);

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

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

286
static void decodebin_set_queue_size (GstDecodeBin * dbin,
287
    GstElement * multiqueue, gboolean preroll, gboolean seekable);
288 289
static void decodebin_set_queue_size_full (GstDecodeBin * dbin,
    GstElement * multiqueue, gboolean use_buffering, gboolean preroll,
290
    gboolean seekable);
291

292
static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
293 294 295
    GstPad * pad, GstCaps * caps);
static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
    element, GstPad * pad, GstCaps * caps);
296 297 298 299
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);
300 301
static gboolean gst_decode_bin_autoplug_query (GstElement * element,
    GstPad * pad, GstQuery * query);
302

303 304 305 306 307 308
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);
309
static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
310
    GstDecodeChain * chain);
311

312 313
static void flush_chain (GstDecodeChain * chain, gboolean flushing);
static void flush_group (GstDecodeGroup * group, gboolean flushing);
314 315 316
static GstPad *find_sink_pad (GstElement * element);
static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
    GstStateChange transition);
317
static void gst_decode_bin_handle_message (GstBin * bin, GstMessage * message);
318

319 320
static gboolean check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad);

321 322
static GstCaps *get_pad_caps (GstPad * pad);

323
#define EXPOSE_LOCK(dbin) G_STMT_START {				\
324
    GST_LOG_OBJECT (dbin,						\
325
		    "expose locking from thread %p",			\
326
		    g_thread_self ());					\
327
    g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->expose_lock);		\
328
    GST_LOG_OBJECT (dbin,						\
329
		    "expose locked from thread %p",			\
330 331 332
		    g_thread_self ());					\
} G_STMT_END

333
#define EXPOSE_UNLOCK(dbin) G_STMT_START {				\
334
    GST_LOG_OBJECT (dbin,						\
335
		    "expose unlocking from thread %p",			\
336
		    g_thread_self ());					\
337
    g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->expose_lock);		\
338 339
} G_STMT_END

340
#define DYN_LOCK(dbin) G_STMT_START {			\
341 342 343
    GST_LOG_OBJECT (dbin,						\
		    "dynlocking from thread %p",			\
		    g_thread_self ());					\
344
    g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock);			\
345 346 347 348 349
    GST_LOG_OBJECT (dbin,						\
		    "dynlocked from thread %p",				\
		    g_thread_self ());					\
} G_STMT_END

350
#define DYN_UNLOCK(dbin) G_STMT_START {			\
351 352 353
    GST_LOG_OBJECT (dbin,						\
		    "dynunlocking from thread %p",			\
		    g_thread_self ());					\
354
    g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock);		\
355 356
} G_STMT_END

357 358 359 360
#define SUBTITLE_LOCK(dbin) G_STMT_START {				\
    GST_LOG_OBJECT (dbin,						\
		    "subtitle locking from thread %p",			\
		    g_thread_self ());					\
361
    g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock);		\
362 363 364 365 366 367 368 369 370
    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 ());					\
371
    g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock);		\
372 373
} G_STMT_END

374 375 376 377 378
struct _GstPendingPad
{
  GstPad *pad;
  GstDecodeChain *chain;
  gulong event_probe_id;
379
  gulong notify_caps_id;
380 381
};

382 383 384 385
struct _GstDecodeElement
{
  GstElement *element;
  GstElement *capsfilter;       /* Optional capsfilter for Parser/Convert */
386 387 388
  gulong pad_added_id;
  gulong pad_removed_id;
  gulong no_more_pads_id;
389 390
};

391 392 393 394
/* GstDecodeGroup
 *
 * Streams belonging to the same group/chain of a media file
 *
395
 * When changing something here lock the parent chain!
396 397 398 399
 */
struct _GstDecodeGroup
{
  GstDecodeBin *dbin;
400 401 402
  GstDecodeChain *parent;

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

405 406 407 408 409 410 411 412 413 414
  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 */
415 416
};

417 418 419 420
struct _GstDecodeChain
{
  GstDecodeGroup *parent;
  GstDecodeBin *dbin;
421

422
  GMutex lock;                  /* Protects this chain and its groups */
423

424 425
  GstPad *pad;                  /* srcpad that caused creation of this chain */

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

433 434 435 436 437 438 439 440 441 442 443 444
  /* 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 */

445 446 447
  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 */
448 449 450
  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
451
                                   e.g. stream got EOS without buffers
452
                                 */
453
  gchar *deadend_details;
454 455 456
  GstCaps *endcaps;             /* Caps that were used when linking to the endpad
                                   or that resulted in the deadend
                                 */
457 458 459 460 461 462 463

  /* 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,
464
    GstDecodeGroup * group, GstPad * pad);
465 466
static void gst_decode_group_hide (GstDecodeGroup * group);
static void gst_decode_group_free (GstDecodeGroup * group);
467 468 469 470
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,
471 472
    GList ** endpads, gboolean * missing_plugin,
    GString * missing_plugin_details, gboolean * last_group);
473
static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
474
static gboolean gst_decode_chain_reset_buffering (GstDecodeChain * chain);
475 476 477 478
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);
479
static gboolean gst_decode_group_reset_buffering (GstDecodeGroup * group);
480 481

static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
482
static void gst_decode_bin_reset_buffering (GstDecodeBin * dbin);
483 484 485 486 487

#define CHAIN_MUTEX_LOCK(chain) G_STMT_START {				\
    GST_LOG_OBJECT (chain->dbin,					\
		    "locking chain %p from thread %p",			\
		    chain, g_thread_self ());				\
488
    g_mutex_lock (&chain->lock);						\
489 490 491 492 493 494 495 496 497
    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 ());				\
498
    g_mutex_unlock (&chain->lock);					\
499
} G_STMT_END
500 501 502

/* GstDecodePad
 *
503
 * GstPad private used for source pads of chains
504 505 506
 */
struct _GstDecodePad
{
507 508
  GstGhostPad parent;
  GstDecodeBin *dbin;
509
  GstDecodeChain *chain;
510

511 512
  gboolean blocked;             /* the *target* pad is blocked */
  gboolean exposed;             /* the pad is exposed */
513
  gboolean drained;             /* an EOS has been seen on the pad */
514 515

  gulong block_id;
516 517
};

518
GType gst_decode_pad_get_type (void);
519 520 521 522
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))

523
static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin,
524
    GstDecodeChain * chain);
525
static void gst_decode_pad_activate (GstDecodePad * dpad,
526
    GstDecodeChain * chain);
527
static void gst_decode_pad_unblock (GstDecodePad * dpad);
528
static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
529 530
static gboolean gst_decode_pad_query (GstPad * pad, GstObject * parent,
    GstQuery * query);
531

532
static void gst_pending_pad_free (GstPendingPad * ppad);
Wim Taymans's avatar
Wim Taymans committed
533 534
static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
    gpointer data);
535

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
/********************************
 * 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 =
565
        g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
        &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;
}

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
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;
}

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
/* 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);

626 627 628 629 630 631 632
  /* 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;

633 634 635
  return FALSE;
}

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
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;
}

652 653 654 655 656
static void
gst_decode_bin_class_init (GstDecodeBinClass * klass)
{
  GObjectClass *gobject_klass;
  GstElementClass *gstelement_klass;
657
  GstBinClass *gstbin_klass;
658 659 660

  gobject_klass = (GObjectClass *) klass;
  gstelement_klass = (GstElementClass *) klass;
661
  gstbin_klass = (GstBinClass *) klass;
662 663 664

  parent_class = g_type_class_peek_parent (klass);

665 666 667 668
  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;
669

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

  /**
687
   * GstDecodeBin::autoplug-continue:
688
   * @bin: The decodebin.
689
   * @pad: The #GstPad.
690 691
   * @caps: The #GstCaps found.
   *
692
   * This signal is emitted whenever decodebin finds a new stream. It is
693 694
   * emitted before looking for any elements that can handle that stream.
   *
695 696 697 698 699 700
   * <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>
   *
701
   * Returns: #TRUE if you wish decodebin to look for elements that can
702
   * handle the given @caps. If #FALSE, those caps will be considered as
703 704
   * final and the pad will be exposed as such (see 'pad-added' signal of
   * #GstElement).
705
   */
706 707 708
  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),
709
      _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
710 711 712
      G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);

  /**
713
   * GstDecodeBin::autoplug-factories:
714
   * @bin: The decodebin.
715 716 717 718
   * @pad: The #GstPad.
   * @caps: The #GstCaps found.
   *
   * This function is emited when an array of possible factories for @caps on
719
   * @pad is needed. Decodebin will by default return an array with all
720
   * compatible factories, sorted by rank.
721 722 723
   *
   * If this function returns NULL, @pad will be exposed as a final caps.
   *
724
   * If this function returns an empty array, the pad will be considered as
725
   * having an unhandled type media type.
726 727 728 729 730 731
   *
   * <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>
732 733 734 735 736 737 738 739
   *
   * 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,
740
          autoplug_factories), _gst_array_accumulator, NULL,
741
      g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
742
      GST_TYPE_PAD, GST_TYPE_CAPS);
743

744
  /**
745
   * GstDecodeBin::autoplug-sort:
746
   * @bin: The decodebin.
747 748 749 750
   * @pad: The #GstPad.
   * @caps: The #GstCaps.
   * @factories: A #GValueArray of possible #GstElementFactory to use.
   *
751
   * Once decodebin has found the possible #GstElementFactory objects to try
752 753 754 755
   * 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.
   *
756 757 758 759 760 761 762 763 764 765
   * 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>
766 767 768 769 770 771
   *
   * 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),
772
      _gst_array_hasvalue_accumulator, NULL,
773 774
      g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
      GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
775

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

819 820 821 822 823
  /**
   * GstDecodeBin::autoplug-query:
   * @bin: The decodebin.
   * @child: The child element doing the query
   * @pad: The #GstPad.
824
   * @element: The #GstElement.
825 826 827 828 829 830 831 832 833 834 835 836 837
   * @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,
838
      G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
839 840
      GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);

841
  /**
842
   * GstDecodeBin::drained
843
   * @bin: The decodebin
844
   *
845
   * This signal is emitted once decodebin has finished decoding all the data.
846 847 848 849
   */
  gst_decode_bin_signals[SIGNAL_DRAINED] =
      g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
850
      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
851

852 853
  g_object_class_install_property (gobject_klass, PROP_CAPS,
      g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
854
          GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
855

856 857 858 859 860
  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, "
861 862
          "ISO-8859-15 will be assumed.", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
863

864 865 866 867 868
  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));

869
  /**
870
   * GstDecodeBin::use-buffering
871
   *
872
   * Activate buffering in decodebin. This will instruct the multiqueues behind
873 874 875 876
   * decoders to emit BUFFERING messages.
   */
  g_object_class_install_property (gobject_klass, PROP_USE_BUFFERING,
      g_param_spec_boolean ("use-buffering", "Use Buffering",
877
          "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
878 879 880
          DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
881
   * GstDecodeBin:low-percent
882
   *
883
   * Low threshold percent for buffering to start.
884 885 886
   */
  g_object_class_install_property (gobject_klass, PROP_LOW_PERCENT,
      g_param_spec_int ("low-percent", "Low percent",
887
          "Low threshold for buffering to start", 0, 100,
888 889
          DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  /**
890
   * GstDecodeBin:high-percent
891
   *
892
   * High threshold percent for buffering to finish.
893 894 895
   */
  g_object_class_install_property (gobject_klass, PROP_HIGH_PERCENT,
      g_param_spec_int ("high-percent", "High percent",
896
          "High threshold for buffering to finish", 0, 100,
897 898 899
          DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
900
   * GstDecodeBin:max-size-bytes
901
   *
Piotr Fusik's avatar
Piotr Fusik committed
902
   * Max amount of bytes in the queue (0=automatic).
903 904 905
   */
  g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BYTES,
      g_param_spec_uint ("max-size-bytes", "Max. size (bytes)",
906 907
          "Max. amount of bytes in the queue (0=automatic)",
          0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,