2 * Copyright (C) 2006 Edward Hervey <edward@fluendo.com>
3 * Copyright (C) 2007 Jan Schmidt <jan@fluendo.com>
4 * Copyright (C) 2007 Wim Taymans <wim@fluendo.com>
5 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * SECTION:element-multiqueue
28 * @see_also: #GstQueue
30 * Multiqueue is similar to a normal #GstQueue with the following additional
33 * 1) Multiple streamhandling
35 * * The element handles queueing data on more than one stream at once. To
36 * achieve such a feature it has request sink pads (sink%u) and
37 * 'sometimes' src pads (src%u). When requesting a given sinkpad with gst_element_request_pad(),
38 * the associated srcpad for that stream will be created.
39 * Example: requesting sink1 will generate src1.
41 * 2) Non-starvation on multiple stream
43 * * If more than one stream is used with the element, the streams' queues
44 * will be dynamically grown (up to a limit), in order to ensure that no
45 * stream is risking data starvation. This guarantees that at any given
46 * time there are at least N bytes queued and available for each individual
47 * stream. If an EOS event comes through a srcpad, the associated queue will be
48 * considered as 'not-empty' in the queue-size-growing algorithm.
50 * 3) Non-linked srcpads graceful handling
52 * * In order to better support dynamic switching between streams, the multiqueue
53 * (unlike the current GStreamer queue) continues to push buffers on non-linked
54 * pads rather than shutting down. In addition, to prevent a non-linked stream from very quickly consuming all
55 * available buffers and thus 'racing ahead' of the other streams, the element
56 * must ensure that buffers and inlined events for a non-linked stream are pushed
57 * in the same order as they were received, relative to the other streams
58 * controlled by the element. This means that a buffer cannot be pushed to a
59 * non-linked pad any sooner than buffers in any other stream which were received
62 * Data is queued until one of the limits specified by the
63 * #GstMultiQueue:max-size-buffers, #GstMultiQueue:max-size-bytes and/or
64 * #GstMultiQueue:max-size-time properties has been reached. Any attempt to push
65 * more buffers into the queue will block the pushing thread until more space
66 * becomes available. #GstMultiQueue:extra-size-buffers,
69 * #GstMultiQueue:extra-size-bytes and #GstMultiQueue:extra-size-time are
72 * The default queue size limits are 5 buffers, 10MB of data, or
73 * two second worth of data, whichever is reached first. Note that the number
74 * of buffers will dynamically grow depending on the fill level of
77 * The #GstMultiQueue::underrun signal is emitted when all of the queues
78 * are empty. The #GstMultiQueue::overrun signal is emitted when one of the
80 * Both signals are emitted from the context of the streaming thread.
82 * When using #GstMultiQueue:sync-by-running-time the unlinked streams will
83 * be throttled by the highest running-time of linked streams. This allows
84 * further relinking of those unlinked streams without them being in the
85 * future (i.e. to achieve gapless playback).
86 * When dealing with streams which have got different consumption requirements
87 * downstream (ex: video decoders which will consume more buffer (in time) than
88 * audio decoders), it is recommended to group streams of the same type
89 * by using the pad "group-id" property. This will further throttle streams
90 * in time within that group.
99 #include "gstmultiqueue.h"
100 #include <gst/glib-compat-private.h>
104 * @sinkpad: associated sink #GstPad
105 * @srcpad: associated source #GstPad
107 * Structure containing all information and properties about
110 typedef struct _GstSingleQueue GstSingleQueue;
112 struct _GstSingleQueue
114 /* unique identifier of the queue */
116 /* group of streams to which this queue belongs to */
118 GstClockTimeDiff group_high_time;
120 GstMultiQueue *mqueue;
125 /* flowreturn of previous srcpad push */
126 GstFlowReturn srcresult;
127 /* If something was actually pushed on
128 * this pad after flushing/pad activation
129 * and the srcresult corresponds to something
135 GstSegment sink_segment;
136 GstSegment src_segment;
137 gboolean has_src_segment; /* preferred over initializing the src_segment to
138 * UNDEFINED as this doesn't requires adding ifs
139 * in every segment usage */
141 /* position of src/sink */
142 GstClockTimeDiff sinktime, srctime;
143 /* cached input value, used for interleave */
144 GstClockTimeDiff cached_sinktime;
145 /* TRUE if either position needs to be recalculated */
146 gboolean sink_tainted, src_tainted;
150 GstDataQueueSize max_size, extra_size;
151 GstClockTime cur_time;
157 /* Protected by global lock */
158 guint32 nextid; /* ID of the next object waiting to be pushed */
159 guint32 oldid; /* ID of the last object pushed (last in a series) */
160 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
161 GstClockTimeDiff next_time; /* End running time of next buffer to be pushed */
162 GstClockTimeDiff last_time; /* Start running time of last pushed buffer */
163 GCond turn; /* SingleQueue turn waiting conditional */
165 /* for serialized queries */
168 GstQuery *last_handled_query;
170 /* For interleave calculation */
171 GThread *thread; /* Streaming thread of SingleQueue */
172 GstClockTime interleave; /* Calculated interleve within the thread */
176 /* Extension of GstDataQueueItem structure for our usage */
177 typedef struct _GstMultiQueueItem GstMultiQueueItem;
179 struct _GstMultiQueueItem
181 GstMiniObject *object;
186 GDestroyNotify destroy;
192 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
193 static void gst_single_queue_free (GstSingleQueue * squeue);
195 static void wake_up_next_non_linked (GstMultiQueue * mq);
196 static void compute_high_id (GstMultiQueue * mq);
197 static void compute_high_time (GstMultiQueue * mq, guint groupid);
198 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
199 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
201 static void update_buffering (GstMultiQueue * mq, GstSingleQueue * sq);
202 static void gst_multi_queue_post_buffering (GstMultiQueue * mq);
203 static void recheck_buffering_status (GstMultiQueue * mq);
205 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
207 static void calculate_interleave (GstMultiQueue * mq, GstSingleQueue * sq);
209 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
212 GST_STATIC_CAPS_ANY);
214 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
217 GST_STATIC_CAPS_ANY);
219 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
220 #define GST_CAT_DEFAULT (multi_queue_debug)
222 /* Signals and args */
230 /* default limits, we try to keep up to 2 seconds of data and if there is not
231 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
232 * there is data in the queues. Normally, the byte and time limits are not hit
233 * in theses conditions. */
234 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
235 #define DEFAULT_MAX_SIZE_BUFFERS 5
236 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
238 /* second limits. When we hit one of the above limits we are probably dealing
239 * with a badly muxed file and we scale the limits to these emergency values.
240 * This is currently not yet implemented.
241 * Since we dynamically scale the queue buffer size up to the limits but avoid
242 * going above the max-size-buffers when we can, we don't really need this
243 * aditional extra size. */
244 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
245 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
246 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
248 #define DEFAULT_USE_BUFFERING FALSE
249 #define DEFAULT_LOW_WATERMARK 0.01
250 #define DEFAULT_HIGH_WATERMARK 0.99
251 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
252 #define DEFAULT_USE_INTERLEAVE FALSE
253 #define DEFAULT_UNLINKED_CACHE_TIME 250 * GST_MSECOND
255 #define DEFAULT_MINIMUM_INTERLEAVE (250 * GST_MSECOND)
260 PROP_EXTRA_SIZE_BYTES,
261 PROP_EXTRA_SIZE_BUFFERS,
262 PROP_EXTRA_SIZE_TIME,
264 PROP_MAX_SIZE_BUFFERS,
271 PROP_SYNC_BY_RUNNING_TIME,
273 PROP_UNLINKED_CACHE_TIME,
274 PROP_MINIMUM_INTERLEAVE,
278 /* Explanation for buffer levels and percentages:
280 * The buffering_level functions here return a value in a normalized range
281 * that specifies the current fill level of a queue. The range goes from 0 to
282 * MAX_BUFFERING_LEVEL. The low/high watermarks also use this same range.
284 * This is not to be confused with the buffering_percent value, which is
285 * a *relative* quantity - relative to the low/high watermarks.
286 * buffering_percent = 0% means overall buffering_level is at the low watermark.
287 * buffering_percent = 100% means overall buffering_level is at the high watermark.
288 * buffering_percent is used for determining if the fill level has reached
289 * the high watermark, and for producing BUFFERING messages. This value
290 * always uses a 0..100 range (since it is a percentage).
292 * To avoid future confusions, whenever "buffering level" is mentioned, it
293 * refers to the absolute level which is in the 0..MAX_BUFFERING_LEVEL
294 * range. Whenever "buffering_percent" is mentioned, it refers to the
295 * percentage value that is relative to the low/high watermark. */
297 /* Using a buffering level range of 0..1000000 to allow for a
298 * resolution in ppm (1 ppm = 0.0001%) */
299 #define MAX_BUFFERING_LEVEL 1000000
301 /* How much 1% makes up in the buffer level range */
302 #define BUF_LEVEL_PERCENT_FACTOR ((MAX_BUFFERING_LEVEL) / 100)
304 /* GstMultiQueuePad */
306 #define DEFAULT_PAD_GROUP_ID 0
314 #define GST_TYPE_MULTIQUEUE_PAD (gst_multiqueue_pad_get_type())
315 #define GST_MULTIQUEUE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePad))
316 #define GST_IS_MULTIQUEUE_PAD(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTIQUEUE_PAD))
317 #define GST_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
318 #define GST_IS_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_MULTIQUEUE_PAD))
319 #define GST_MULTIQUEUE_PAD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
321 struct _GstMultiQueuePad
328 struct _GstMultiQueuePadClass
330 GstPadClass parent_class;
333 GType gst_multiqueue_pad_get_type (void);
335 G_DEFINE_TYPE (GstMultiQueuePad, gst_multiqueue_pad, GST_TYPE_PAD);
337 gst_multiqueue_pad_get_property (GObject * object, guint prop_id,
338 GValue * value, GParamSpec * pspec)
340 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
343 case PROP_PAD_GROUP_ID:
345 g_value_set_uint (value, pad->sq->groupid);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 gst_multiqueue_pad_set_property (GObject * object, guint prop_id,
355 const GValue * value, GParamSpec * pspec)
357 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
360 case PROP_PAD_GROUP_ID:
361 GST_OBJECT_LOCK (pad);
363 pad->sq->groupid = g_value_get_uint (value);
364 GST_OBJECT_UNLOCK (pad);
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 gst_multiqueue_pad_class_init (GstMultiQueuePadClass * klass)
375 GObjectClass *gobject_class = (GObjectClass *) klass;
377 gobject_class->set_property = gst_multiqueue_pad_set_property;
378 gobject_class->get_property = gst_multiqueue_pad_get_property;
381 * GstMultiQueuePad:group-id:
383 * Group to which this pad belongs.
387 g_object_class_install_property (gobject_class, PROP_PAD_GROUP_ID,
388 g_param_spec_uint ("group-id", "Group ID",
389 "Group to which this pad belongs", 0, G_MAXUINT32,
390 DEFAULT_PAD_GROUP_ID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 gst_multiqueue_pad_init (GstMultiQueuePad * pad)
400 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
401 g_mutex_lock (&q->qlock); \
404 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
405 g_mutex_unlock (&q->qlock); \
408 #define SET_PERCENT(mq, perc) G_STMT_START { \
409 if (perc != mq->buffering_percent) { \
410 mq->buffering_percent = perc; \
411 mq->buffering_percent_changed = TRUE; \
412 GST_DEBUG_OBJECT (mq, "buffering %d percent", perc); \
416 /* Convenience function */
417 static inline GstClockTimeDiff
418 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
420 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
422 if (GST_CLOCK_TIME_IS_VALID (val)) {
424 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
433 static void gst_multi_queue_finalize (GObject * object);
434 static void gst_multi_queue_set_property (GObject * object,
435 guint prop_id, const GValue * value, GParamSpec * pspec);
436 static void gst_multi_queue_get_property (GObject * object,
437 guint prop_id, GValue * value, GParamSpec * pspec);
439 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
440 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
441 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
442 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
443 element, GstStateChange transition);
445 static void gst_multi_queue_loop (GstPad * pad);
448 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
449 #define gst_multi_queue_parent_class parent_class
450 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
453 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
456 gst_multi_queue_class_init (GstMultiQueueClass * klass)
458 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
459 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
461 gobject_class->set_property = gst_multi_queue_set_property;
462 gobject_class->get_property = gst_multi_queue_get_property;
467 * GstMultiQueue::underrun:
468 * @multiqueue: the multiqueue instance
470 * This signal is emitted from the streaming thread when there is
471 * no data in any of the queues inside the multiqueue instance (underrun).
473 * This indicates either starvation or EOS from the upstream data sources.
475 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
476 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
477 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
478 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
481 * GstMultiQueue::overrun:
482 * @multiqueue: the multiqueue instance
484 * Reports that one of the queues in the multiqueue is full (overrun).
485 * A queue is full if the total amount of data inside it (num-buffers, time,
486 * size) is higher than the boundary values which can be set through the
487 * GObject properties.
489 * This can be used as an indicator of pre-roll.
491 gst_multi_queue_signals[SIGNAL_OVERRUN] =
492 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
493 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
494 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
498 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
499 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
500 "Max. amount of data in the queue (bytes, 0=disable)",
501 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
502 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
503 G_PARAM_STATIC_STRINGS));
504 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
505 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
506 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
507 DEFAULT_MAX_SIZE_BUFFERS,
508 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
509 G_PARAM_STATIC_STRINGS));
510 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
511 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
512 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
513 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
514 G_PARAM_STATIC_STRINGS));
516 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
517 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
518 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
519 " (NOT IMPLEMENTED)",
520 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
521 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
522 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
523 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
524 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
525 " (NOT IMPLEMENTED)",
526 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
527 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
528 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
529 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
530 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
531 " (NOT IMPLEMENTED)",
532 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
533 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
536 * GstMultiQueue:use-buffering:
538 * Enable the buffering option in multiqueue so that BUFFERING messages are
539 * emitted based on low-/high-percent thresholds.
541 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
542 g_param_spec_boolean ("use-buffering", "Use buffering",
543 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
544 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
545 G_PARAM_STATIC_STRINGS));
547 * GstMultiQueue:low-percent:
549 * Low threshold percent for buffering to start.
551 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
552 g_param_spec_int ("low-percent", "Low percent",
553 "Low threshold for buffering to start. Only used if use-buffering is True "
554 "(Deprecated: use low-watermark instead)",
555 0, 100, DEFAULT_LOW_WATERMARK * 100,
556 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
558 * GstMultiQueue:high-percent:
560 * High threshold percent for buffering to finish.
562 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
563 g_param_spec_int ("high-percent", "High percent",
564 "High threshold for buffering to finish. Only used if use-buffering is True "
565 "(Deprecated: use high-watermark instead)",
566 0, 100, DEFAULT_HIGH_WATERMARK * 100,
567 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
569 * GstMultiQueue:low-watermark:
571 * Low threshold watermark for buffering to start.
575 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
576 g_param_spec_double ("low-watermark", "Low watermark",
577 "Low threshold for buffering to start. Only used if use-buffering is True",
578 0.0, 1.0, DEFAULT_LOW_WATERMARK,
579 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
581 * GstMultiQueue:high-watermark:
583 * High threshold watermark for buffering to finish.
587 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
588 g_param_spec_double ("high-watermark", "High watermark",
589 "High threshold for buffering to finish. Only used if use-buffering is True",
590 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
591 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
594 * GstMultiQueue:sync-by-running-time:
596 * If enabled multiqueue will synchronize deactivated or not-linked streams
597 * to the activated and linked streams by taking the running time.
598 * Otherwise multiqueue will synchronize the deactivated or not-linked
599 * streams by keeping the order in which buffers and events arrived compared
600 * to active and linked streams.
602 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
603 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
604 "Synchronize deactivated or not-linked streams by running time",
605 DEFAULT_SYNC_BY_RUNNING_TIME,
606 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
608 g_object_class_install_property (gobject_class, PROP_USE_INTERLEAVE,
609 g_param_spec_boolean ("use-interleave", "Use interleave",
610 "Adjust time limits based on input interleave",
611 DEFAULT_USE_INTERLEAVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
613 g_object_class_install_property (gobject_class, PROP_UNLINKED_CACHE_TIME,
614 g_param_spec_uint64 ("unlinked-cache-time", "Unlinked cache time (ns)",
615 "Extra buffering in time for unlinked streams (if 'sync-by-running-time')",
616 0, G_MAXUINT64, DEFAULT_UNLINKED_CACHE_TIME,
617 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
618 G_PARAM_STATIC_STRINGS));
620 g_object_class_install_property (gobject_class, PROP_MINIMUM_INTERLEAVE,
621 g_param_spec_uint64 ("min-interleave-time", "Minimum interleave time",
622 "Minimum extra buffering for deinterleaving (size of the queues) when use-interleave=true",
623 0, G_MAXUINT64, DEFAULT_MINIMUM_INTERLEAVE,
624 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
625 G_PARAM_STATIC_STRINGS));
627 gobject_class->finalize = gst_multi_queue_finalize;
629 gst_element_class_set_static_metadata (gstelement_class,
631 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
632 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
633 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
635 gstelement_class->request_new_pad =
636 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
637 gstelement_class->release_pad =
638 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
639 gstelement_class->change_state =
640 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
644 gst_multi_queue_init (GstMultiQueue * mqueue)
646 mqueue->nbqueues = 0;
647 mqueue->queues = NULL;
649 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
650 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
651 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
653 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
654 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
655 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
657 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
658 mqueue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
659 mqueue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
661 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
662 mqueue->use_interleave = DEFAULT_USE_INTERLEAVE;
663 mqueue->min_interleave_time = DEFAULT_MINIMUM_INTERLEAVE;
664 mqueue->unlinked_cache_time = DEFAULT_UNLINKED_CACHE_TIME;
668 mqueue->high_time = GST_CLOCK_STIME_NONE;
670 g_mutex_init (&mqueue->qlock);
671 g_mutex_init (&mqueue->buffering_post_lock);
675 gst_multi_queue_finalize (GObject * object)
677 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
679 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
680 g_list_free (mqueue->queues);
681 mqueue->queues = NULL;
682 mqueue->queues_cookie++;
684 /* free/unref instance data */
685 g_mutex_clear (&mqueue->qlock);
686 g_mutex_clear (&mqueue->buffering_post_lock);
688 G_OBJECT_CLASS (parent_class)->finalize (object);
691 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
692 GList * tmp = mq->queues; \
694 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
695 q->max_size.format = mq->max_size.format; \
696 update_buffering (mq, q); \
697 gst_data_queue_limits_changed (q->queue); \
698 tmp = g_list_next(tmp); \
703 gst_multi_queue_set_property (GObject * object, guint prop_id,
704 const GValue * value, GParamSpec * pspec)
706 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
709 case PROP_MAX_SIZE_BYTES:
710 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
711 mq->max_size.bytes = g_value_get_uint (value);
712 SET_CHILD_PROPERTY (mq, bytes);
713 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
714 gst_multi_queue_post_buffering (mq);
716 case PROP_MAX_SIZE_BUFFERS:
719 gint new_size = g_value_get_uint (value);
721 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
723 mq->max_size.visible = new_size;
727 GstDataQueueSize size;
728 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
729 gst_data_queue_get_level (q->queue, &size);
731 GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
732 " current: %d, current max %d", q->id, new_size, size.visible,
733 q->max_size.visible);
735 /* do not reduce max size below current level if the single queue
736 * has grown because of empty queue */
738 q->max_size.visible = new_size;
739 } else if (q->max_size.visible == 0) {
740 q->max_size.visible = MAX (new_size, size.visible);
741 } else if (new_size > size.visible) {
742 q->max_size.visible = new_size;
744 update_buffering (mq, q);
745 gst_data_queue_limits_changed (q->queue);
746 tmp = g_list_next (tmp);
749 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
750 gst_multi_queue_post_buffering (mq);
754 case PROP_MAX_SIZE_TIME:
755 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
756 mq->max_size.time = g_value_get_uint64 (value);
757 SET_CHILD_PROPERTY (mq, time);
758 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
759 gst_multi_queue_post_buffering (mq);
761 case PROP_EXTRA_SIZE_BYTES:
762 mq->extra_size.bytes = g_value_get_uint (value);
764 case PROP_EXTRA_SIZE_BUFFERS:
765 mq->extra_size.visible = g_value_get_uint (value);
767 case PROP_EXTRA_SIZE_TIME:
768 mq->extra_size.time = g_value_get_uint64 (value);
770 case PROP_USE_BUFFERING:
771 mq->use_buffering = g_value_get_boolean (value);
772 recheck_buffering_status (mq);
774 case PROP_LOW_PERCENT:
775 mq->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
776 /* Recheck buffering status - the new low_watermark value might
777 * be above the current fill level. If the old low_watermark one
778 * was below the current level, this means that mq->buffering is
779 * disabled and needs to be re-enabled. */
780 recheck_buffering_status (mq);
782 case PROP_HIGH_PERCENT:
783 mq->high_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
784 recheck_buffering_status (mq);
786 case PROP_LOW_WATERMARK:
787 mq->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
788 recheck_buffering_status (mq);
790 case PROP_HIGH_WATERMARK:
791 mq->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
792 recheck_buffering_status (mq);
794 case PROP_SYNC_BY_RUNNING_TIME:
795 mq->sync_by_running_time = g_value_get_boolean (value);
797 case PROP_USE_INTERLEAVE:
798 mq->use_interleave = g_value_get_boolean (value);
800 case PROP_UNLINKED_CACHE_TIME:
801 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
802 mq->unlinked_cache_time = g_value_get_uint64 (value);
803 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
804 gst_multi_queue_post_buffering (mq);
806 case PROP_MINIMUM_INTERLEAVE:
807 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
808 mq->min_interleave_time = g_value_get_uint64 (value);
809 if (mq->use_interleave)
810 calculate_interleave (mq, NULL);
811 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
814 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
820 gst_multi_queue_get_property (GObject * object, guint prop_id,
821 GValue * value, GParamSpec * pspec)
823 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
825 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
828 case PROP_EXTRA_SIZE_BYTES:
829 g_value_set_uint (value, mq->extra_size.bytes);
831 case PROP_EXTRA_SIZE_BUFFERS:
832 g_value_set_uint (value, mq->extra_size.visible);
834 case PROP_EXTRA_SIZE_TIME:
835 g_value_set_uint64 (value, mq->extra_size.time);
837 case PROP_MAX_SIZE_BYTES:
838 g_value_set_uint (value, mq->max_size.bytes);
840 case PROP_MAX_SIZE_BUFFERS:
841 g_value_set_uint (value, mq->max_size.visible);
843 case PROP_MAX_SIZE_TIME:
844 g_value_set_uint64 (value, mq->max_size.time);
846 case PROP_USE_BUFFERING:
847 g_value_set_boolean (value, mq->use_buffering);
849 case PROP_LOW_PERCENT:
850 g_value_set_int (value, mq->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
852 case PROP_HIGH_PERCENT:
853 g_value_set_int (value, mq->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
855 case PROP_LOW_WATERMARK:
856 g_value_set_double (value, mq->low_watermark /
857 (gdouble) MAX_BUFFERING_LEVEL);
859 case PROP_HIGH_WATERMARK:
860 g_value_set_double (value, mq->high_watermark /
861 (gdouble) MAX_BUFFERING_LEVEL);
863 case PROP_SYNC_BY_RUNNING_TIME:
864 g_value_set_boolean (value, mq->sync_by_running_time);
866 case PROP_USE_INTERLEAVE:
867 g_value_set_boolean (value, mq->use_interleave);
869 case PROP_UNLINKED_CACHE_TIME:
870 g_value_set_uint64 (value, mq->unlinked_cache_time);
872 case PROP_MINIMUM_INTERLEAVE:
873 g_value_set_uint64 (value, mq->min_interleave_time);
876 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
880 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
884 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
886 GstIterator *it = NULL;
888 GstSingleQueue *squeue;
889 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
892 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
893 squeue = gst_pad_get_element_private (pad);
897 if (squeue->sinkpad == pad)
898 opad = gst_object_ref (squeue->srcpad);
899 else if (squeue->srcpad == pad)
900 opad = gst_object_ref (squeue->sinkpad);
904 g_value_init (&val, GST_TYPE_PAD);
905 g_value_set_object (&val, opad);
906 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
907 g_value_unset (&val);
909 gst_object_unref (opad);
912 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
923 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
924 const gchar * name, const GstCaps * caps)
926 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
927 GstSingleQueue *squeue;
932 sscanf (name + 4, "_%u", &temp_id);
933 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
936 /* Create a new single queue, add the sink and source pad and return the sink pad */
937 squeue = gst_single_queue_new (mqueue, temp_id);
939 new_pad = squeue ? squeue->sinkpad : NULL;
941 GST_DEBUG_OBJECT (mqueue, "Returning pad %" GST_PTR_FORMAT, new_pad);
947 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
949 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
950 GstSingleQueue *sq = NULL;
953 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
955 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
956 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
957 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
958 sq = (GstSingleQueue *) tmp->data;
960 if (sq->sinkpad == pad)
965 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
966 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
970 /* FIXME: The removal of the singlequeue should probably not happen until it
971 * finishes draining */
973 /* remove it from the list */
974 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
975 mqueue->queues_cookie++;
977 /* FIXME : recompute next-non-linked */
978 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
980 /* delete SingleQueue */
981 gst_data_queue_set_flushing (sq->queue, TRUE);
983 gst_pad_set_active (sq->srcpad, FALSE);
984 gst_pad_set_active (sq->sinkpad, FALSE);
985 gst_pad_set_element_private (sq->srcpad, NULL);
986 gst_pad_set_element_private (sq->sinkpad, NULL);
987 gst_element_remove_pad (element, sq->srcpad);
988 gst_element_remove_pad (element, sq->sinkpad);
989 gst_single_queue_free (sq);
992 static GstStateChangeReturn
993 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
995 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
996 GstSingleQueue *sq = NULL;
997 GstStateChangeReturn result;
999 switch (transition) {
1000 case GST_STATE_CHANGE_READY_TO_PAUSED:{
1003 /* Set all pads to non-flushing */
1004 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1005 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1006 sq = (GstSingleQueue *) tmp->data;
1007 sq->flushing = FALSE;
1010 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
1011 SET_CHILD_PROPERTY (mqueue, visible);
1013 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1014 gst_multi_queue_post_buffering (mqueue);
1018 case GST_STATE_CHANGE_PAUSED_TO_READY:{
1021 /* Un-wait all waiting pads */
1022 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1023 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1024 sq = (GstSingleQueue *) tmp->data;
1025 sq->flushing = TRUE;
1026 g_cond_signal (&sq->turn);
1028 sq->last_query = FALSE;
1029 g_cond_signal (&sq->query_handled);
1031 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1038 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1040 switch (transition) {
1049 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
1054 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
1058 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1059 sq->srcresult = GST_FLOW_FLUSHING;
1060 gst_data_queue_set_flushing (sq->queue, TRUE);
1062 sq->flushing = TRUE;
1064 /* wake up non-linked task */
1065 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
1067 g_cond_signal (&sq->turn);
1068 sq->last_query = FALSE;
1069 g_cond_signal (&sq->query_handled);
1070 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1072 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
1073 result = gst_pad_pause_task (sq->srcpad);
1074 sq->sink_tainted = sq->src_tainted = TRUE;
1076 gst_single_queue_flush_queue (sq, full);
1078 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1079 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1080 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1081 sq->has_src_segment = FALSE;
1082 /* All pads start off not-linked for a smooth kick-off */
1083 sq->srcresult = GST_FLOW_OK;
1086 sq->max_size.visible = mq->max_size.visible;
1090 sq->last_oldid = G_MAXUINT32;
1091 sq->next_time = GST_CLOCK_STIME_NONE;
1092 sq->last_time = GST_CLOCK_STIME_NONE;
1093 sq->cached_sinktime = GST_CLOCK_STIME_NONE;
1094 sq->group_high_time = GST_CLOCK_STIME_NONE;
1095 gst_data_queue_set_flushing (sq->queue, FALSE);
1097 /* We will become active again on the next buffer/gap */
1100 /* Reset high time to be recomputed next */
1101 mq->high_time = GST_CLOCK_STIME_NONE;
1103 sq->flushing = FALSE;
1104 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1106 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
1108 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
1114 /* WITH LOCK TAKEN */
1116 get_buffering_level (GstSingleQueue * sq)
1118 GstDataQueueSize size;
1119 gint buffering_level, tmp;
1121 gst_data_queue_get_level (sq->queue, &size);
1123 GST_DEBUG_OBJECT (sq->mqueue,
1124 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1125 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
1126 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1128 /* get bytes and time buffer levels and take the max */
1129 if (sq->is_eos || sq->srcresult == GST_FLOW_NOT_LINKED || sq->is_sparse) {
1130 buffering_level = MAX_BUFFERING_LEVEL;
1132 buffering_level = 0;
1133 if (sq->max_size.time > 0) {
1135 gst_util_uint64_scale (sq->cur_time,
1136 MAX_BUFFERING_LEVEL, sq->max_size.time);
1137 buffering_level = MAX (buffering_level, tmp);
1139 if (sq->max_size.bytes > 0) {
1141 gst_util_uint64_scale_int (size.bytes,
1142 MAX_BUFFERING_LEVEL, sq->max_size.bytes);
1143 buffering_level = MAX (buffering_level, tmp);
1147 return buffering_level;
1150 /* WITH LOCK TAKEN */
1152 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
1154 gint buffering_level, percent;
1156 /* nothing to dowhen we are not in buffering mode */
1157 if (!mq->use_buffering)
1160 buffering_level = get_buffering_level (sq);
1162 /* scale so that if buffering_level equals the high watermark,
1163 * the percentage is 100% */
1164 percent = gst_util_uint64_scale (buffering_level, 100, mq->high_watermark);
1169 if (mq->buffering) {
1170 if (buffering_level >= mq->high_watermark) {
1171 mq->buffering = FALSE;
1173 /* make sure it increases */
1174 percent = MAX (mq->buffering_percent, percent);
1176 SET_PERCENT (mq, percent);
1179 gboolean is_buffering = TRUE;
1181 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
1182 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
1184 if (get_buffering_level (oq) >= mq->high_watermark) {
1185 is_buffering = FALSE;
1191 if (is_buffering && buffering_level < mq->low_watermark) {
1192 mq->buffering = TRUE;
1193 SET_PERCENT (mq, percent);
1199 gst_multi_queue_post_buffering (GstMultiQueue * mq)
1201 GstMessage *msg = NULL;
1203 g_mutex_lock (&mq->buffering_post_lock);
1204 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1205 if (mq->buffering_percent_changed) {
1206 gint percent = mq->buffering_percent;
1208 mq->buffering_percent_changed = FALSE;
1210 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
1211 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
1213 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1216 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
1218 g_mutex_unlock (&mq->buffering_post_lock);
1222 recheck_buffering_status (GstMultiQueue * mq)
1224 if (!mq->use_buffering && mq->buffering) {
1225 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1226 mq->buffering = FALSE;
1227 GST_DEBUG_OBJECT (mq,
1228 "Buffering property disabled, but queue was still buffering; "
1229 "setting buffering percentage to 100%%");
1230 SET_PERCENT (mq, 100);
1231 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1234 if (mq->use_buffering) {
1238 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1240 /* force buffering percentage to be recalculated */
1241 old_perc = mq->buffering_percent;
1242 mq->buffering_percent = 0;
1246 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
1247 update_buffering (mq, q);
1248 gst_data_queue_limits_changed (q->queue);
1249 tmp = g_list_next (tmp);
1252 GST_DEBUG_OBJECT (mq,
1253 "Recalculated buffering percentage: old: %d%% new: %d%%",
1254 old_perc, mq->buffering_percent);
1256 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1259 gst_multi_queue_post_buffering (mq);
1263 calculate_interleave (GstMultiQueue * mq, GstSingleQueue * sq)
1265 GstClockTimeDiff low, high;
1266 GstClockTime interleave, other_interleave = 0;
1269 low = high = GST_CLOCK_STIME_NONE;
1270 interleave = mq->interleave;
1271 /* Go over all single queues and calculate lowest/highest value */
1272 for (tmp = mq->queues; tmp; tmp = tmp->next) {
1273 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1274 /* Ignore sparse streams for interleave calculation */
1277 /* If a stream is not active yet (hasn't received any buffers), set
1278 * a maximum interleave to allow it to receive more data */
1281 "queue %d is not active yet, forcing interleave to 5s", oq->id);
1282 mq->interleave = 5 * GST_SECOND;
1283 /* Update max-size time */
1284 mq->max_size.time = mq->interleave;
1285 SET_CHILD_PROPERTY (mq, time);
1289 /* Calculate within each streaming thread */
1290 if (sq && sq->thread != oq->thread) {
1291 if (oq->interleave > other_interleave)
1292 other_interleave = oq->interleave;
1296 if (GST_CLOCK_STIME_IS_VALID (oq->cached_sinktime)) {
1297 if (low == GST_CLOCK_STIME_NONE || oq->cached_sinktime < low)
1298 low = oq->cached_sinktime;
1299 if (high == GST_CLOCK_STIME_NONE || oq->cached_sinktime > high)
1300 high = oq->cached_sinktime;
1303 "queue %d , sinktime:%" GST_STIME_FORMAT " low:%" GST_STIME_FORMAT
1304 " high:%" GST_STIME_FORMAT, oq->id,
1305 GST_STIME_ARGS (oq->cached_sinktime), GST_STIME_ARGS (low),
1306 GST_STIME_ARGS (high));
1309 if (GST_CLOCK_STIME_IS_VALID (low) && GST_CLOCK_STIME_IS_VALID (high)) {
1310 interleave = high - low;
1311 /* Padding of interleave and minimum value */
1312 interleave = (150 * interleave / 100) + mq->min_interleave_time;
1314 sq->interleave = interleave;
1316 interleave = MAX (interleave, other_interleave);
1318 /* Update the stored interleave if:
1319 * * No data has arrived yet (high == low)
1320 * * Or it went higher
1321 * * Or it went lower and we've gone past the previous interleave needed */
1322 if (high == low || interleave > mq->interleave ||
1323 ((mq->last_interleave_update + (2 * MIN (GST_SECOND,
1324 mq->interleave)) < low)
1325 && interleave < (mq->interleave * 3 / 4))) {
1326 /* Update the interleave */
1327 mq->interleave = interleave;
1328 mq->last_interleave_update = high;
1329 /* Update max-size time */
1330 mq->max_size.time = mq->interleave;
1331 SET_CHILD_PROPERTY (mq, time);
1336 GST_DEBUG_OBJECT (mq,
1337 "low:%" GST_STIME_FORMAT " high:%" GST_STIME_FORMAT " interleave:%"
1338 GST_TIME_FORMAT " mq->interleave:%" GST_TIME_FORMAT
1339 " last_interleave_update:%" GST_STIME_FORMAT, GST_STIME_ARGS (low),
1340 GST_STIME_ARGS (high), GST_TIME_ARGS (interleave),
1341 GST_TIME_ARGS (mq->interleave),
1342 GST_STIME_ARGS (mq->last_interleave_update));
1346 /* calculate the diff between running time on the sink and src of the queue.
1347 * This is the total amount of time in the queue.
1348 * WITH LOCK TAKEN */
1350 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
1352 GstClockTimeDiff sink_time, src_time;
1354 if (sq->sink_tainted) {
1355 sink_time = sq->sinktime = my_segment_to_running_time (&sq->sink_segment,
1356 sq->sink_segment.position);
1358 GST_DEBUG_OBJECT (mq,
1359 "queue %d sink_segment.position:%" GST_TIME_FORMAT ", sink_time:%"
1360 GST_STIME_FORMAT, sq->id, GST_TIME_ARGS (sq->sink_segment.position),
1361 GST_STIME_ARGS (sink_time));
1363 if (G_UNLIKELY (sq->last_time == GST_CLOCK_STIME_NONE)) {
1364 /* If the single queue still doesn't have a last_time set, this means
1365 * that nothing has been pushed out yet.
1366 * In order for the high_time computation to be as efficient as possible,
1367 * we set the last_time */
1368 sq->last_time = sink_time;
1370 if (G_UNLIKELY (sink_time != GST_CLOCK_STIME_NONE)) {
1371 /* if we have a time, we become untainted and use the time */
1372 sq->sink_tainted = FALSE;
1373 if (mq->use_interleave) {
1374 sq->cached_sinktime = sink_time;
1375 calculate_interleave (mq, sq);
1379 sink_time = sq->sinktime;
1381 if (sq->src_tainted) {
1382 GstSegment *segment;
1385 if (sq->has_src_segment) {
1386 segment = &sq->src_segment;
1387 position = sq->src_segment.position;
1390 * If the src pad had no segment yet, use the sink segment
1391 * to avoid signalling overrun if the received sink segment has a
1392 * a position > max-size-time while the src pad time would be the default=0
1394 * This can happen when switching pads on chained/adaptive streams and the
1395 * new chain has a segment with a much larger position
1397 segment = &sq->sink_segment;
1398 position = sq->sink_segment.position;
1401 src_time = sq->srctime = my_segment_to_running_time (segment, position);
1402 /* if we have a time, we become untainted and use the time */
1403 if (G_UNLIKELY (src_time != GST_CLOCK_STIME_NONE)) {
1404 sq->src_tainted = FALSE;
1407 src_time = sq->srctime;
1409 GST_DEBUG_OBJECT (mq,
1410 "queue %d, sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT, sq->id,
1411 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
1413 /* This allows for streams with out of order timestamping - sometimes the
1414 * emerging timestamp is later than the arriving one(s) */
1415 if (G_LIKELY (GST_CLOCK_STIME_IS_VALID (sink_time) &&
1416 GST_CLOCK_STIME_IS_VALID (src_time) && sink_time > src_time))
1417 sq->cur_time = sink_time - src_time;
1421 /* updating the time level can change the buffering state */
1422 update_buffering (mq, sq);
1427 /* take a SEGMENT event and apply the values to segment, updating the time
1428 * level of queue. */
1430 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1431 GstSegment * segment)
1433 gst_event_copy_segment (event, segment);
1435 /* now configure the values, we use these to track timestamps on the
1437 if (segment->format != GST_FORMAT_TIME) {
1438 /* non-time format, pretent the current time segment is closed with a
1439 * 0 start and unknown stop time. */
1440 segment->format = GST_FORMAT_TIME;
1445 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1447 /* Make sure we have a valid initial segment position (and not garbage
1449 if (segment->rate > 0.0)
1450 segment->position = segment->start;
1452 segment->position = segment->stop;
1453 if (segment == &sq->sink_segment)
1454 sq->sink_tainted = TRUE;
1456 sq->has_src_segment = TRUE;
1457 sq->src_tainted = TRUE;
1460 GST_DEBUG_OBJECT (mq,
1461 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1463 /* segment can update the time level of the queue */
1464 update_time_level (mq, sq);
1466 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1467 gst_multi_queue_post_buffering (mq);
1470 /* take a buffer and update segment, updating the time level of the queue. */
1472 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1473 GstClockTime duration, GstSegment * segment)
1475 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1477 /* if no timestamp is set, assume it's continuous with the previous
1479 if (timestamp == GST_CLOCK_TIME_NONE)
1480 timestamp = segment->position;
1483 if (duration != GST_CLOCK_TIME_NONE)
1484 timestamp += duration;
1486 GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1487 sq->id, segment == &sq->sink_segment ? "sink" : "src",
1488 GST_TIME_ARGS (timestamp));
1490 segment->position = timestamp;
1492 if (segment == &sq->sink_segment)
1493 sq->sink_tainted = TRUE;
1495 sq->src_tainted = TRUE;
1497 /* calc diff with other end */
1498 update_time_level (mq, sq);
1499 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1500 gst_multi_queue_post_buffering (mq);
1504 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1505 GstSegment * segment)
1507 GstClockTime timestamp;
1508 GstClockTime duration;
1510 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1512 gst_event_parse_gap (event, ×tamp, &duration);
1514 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1516 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1517 timestamp += duration;
1520 segment->position = timestamp;
1522 if (segment == &sq->sink_segment)
1523 sq->sink_tainted = TRUE;
1525 sq->src_tainted = TRUE;
1527 /* calc diff with other end */
1528 update_time_level (mq, sq);
1531 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1532 gst_multi_queue_post_buffering (mq);
1535 static GstClockTimeDiff
1536 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1538 GstClockTimeDiff time = GST_CLOCK_STIME_NONE;
1540 if (GST_IS_BUFFER (object)) {
1541 GstBuffer *buf = GST_BUFFER_CAST (object);
1542 GstClockTime btime = GST_BUFFER_DTS_OR_PTS (buf);
1544 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1545 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1546 btime += GST_BUFFER_DURATION (buf);
1547 if (btime > segment->stop)
1548 btime = segment->stop;
1549 time = my_segment_to_running_time (segment, btime);
1551 } else if (GST_IS_BUFFER_LIST (object)) {
1552 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1556 n = gst_buffer_list_length (list);
1557 for (i = 0; i < n; i++) {
1559 buf = gst_buffer_list_get (list, i);
1560 btime = GST_BUFFER_DTS_OR_PTS (buf);
1561 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1562 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1563 btime += GST_BUFFER_DURATION (buf);
1564 if (btime > segment->stop)
1565 btime = segment->stop;
1566 time = my_segment_to_running_time (segment, btime);
1573 } else if (GST_IS_EVENT (object)) {
1574 GstEvent *event = GST_EVENT_CAST (object);
1576 /* For newsegment events return the running time of the start position */
1577 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1578 const GstSegment *new_segment;
1580 gst_event_parse_segment (event, &new_segment);
1581 if (new_segment->format == GST_FORMAT_TIME) {
1583 my_segment_to_running_time ((GstSegment *) new_segment,
1584 new_segment->start);
1593 static GstFlowReturn
1594 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1595 GstMiniObject * object, gboolean * allow_drop)
1597 GstFlowReturn result = sq->srcresult;
1599 if (GST_IS_BUFFER (object)) {
1601 GstClockTime timestamp, duration;
1603 buffer = GST_BUFFER_CAST (object);
1604 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1605 duration = GST_BUFFER_DURATION (buffer);
1607 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1609 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1610 gst_data_queue_limits_changed (sq->queue);
1612 if (G_UNLIKELY (*allow_drop)) {
1613 GST_DEBUG_OBJECT (mq,
1614 "SingleQueue %d : Dropping EOS buffer %p with ts %" GST_TIME_FORMAT,
1615 sq->id, buffer, GST_TIME_ARGS (timestamp));
1616 gst_buffer_unref (buffer);
1618 GST_DEBUG_OBJECT (mq,
1619 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1620 sq->id, buffer, GST_TIME_ARGS (timestamp));
1621 result = gst_pad_push (sq->srcpad, buffer);
1623 } else if (GST_IS_EVENT (object)) {
1626 event = GST_EVENT_CAST (object);
1628 switch (GST_EVENT_TYPE (event)) {
1630 result = GST_FLOW_EOS;
1631 if (G_UNLIKELY (*allow_drop))
1632 *allow_drop = FALSE;
1634 case GST_EVENT_STREAM_START:
1635 result = GST_FLOW_OK;
1636 if (G_UNLIKELY (*allow_drop))
1637 *allow_drop = FALSE;
1639 case GST_EVENT_SEGMENT:
1640 apply_segment (mq, sq, event, &sq->src_segment);
1641 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1642 gst_data_queue_limits_changed (sq->queue);
1643 if (G_UNLIKELY (*allow_drop)) {
1644 result = GST_FLOW_OK;
1645 *allow_drop = FALSE;
1649 apply_gap (mq, sq, event, &sq->src_segment);
1650 /* Applying the gap may have made the queue non-full again, unblock it if needed */
1651 gst_data_queue_limits_changed (sq->queue);
1657 if (G_UNLIKELY (*allow_drop)) {
1658 GST_DEBUG_OBJECT (mq,
1659 "SingleQueue %d : Dropping EOS event %p of type %s",
1660 sq->id, event, GST_EVENT_TYPE_NAME (event));
1661 gst_event_unref (event);
1663 GST_DEBUG_OBJECT (mq,
1664 "SingleQueue %d : Pushing event %p of type %s",
1665 sq->id, event, GST_EVENT_TYPE_NAME (event));
1667 gst_pad_push_event (sq->srcpad, event);
1669 } else if (GST_IS_QUERY (object)) {
1673 query = GST_QUERY_CAST (object);
1675 if (G_UNLIKELY (*allow_drop)) {
1676 GST_DEBUG_OBJECT (mq,
1677 "SingleQueue %d : Dropping EOS query %p", sq->id, query);
1678 gst_query_unref (query);
1681 res = gst_pad_peer_query (sq->srcpad, query);
1684 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1685 sq->last_query = res;
1686 sq->last_handled_query = query;
1687 g_cond_signal (&sq->query_handled);
1688 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1690 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1698 static GstMiniObject *
1699 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1704 item->object = NULL;
1710 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1712 if (!item->is_query && item->object)
1713 gst_mini_object_unref (item->object);
1714 g_slice_free (GstMultiQueueItem, item);
1717 /* takes ownership of passed mini object! */
1718 static GstMultiQueueItem *
1719 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1721 GstMultiQueueItem *item;
1723 item = g_slice_new (GstMultiQueueItem);
1724 item->object = object;
1725 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1726 item->posid = curid;
1727 item->is_query = GST_IS_QUERY (object);
1729 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1730 item->duration = GST_BUFFER_DURATION (object);
1731 if (item->duration == GST_CLOCK_TIME_NONE)
1733 item->visible = TRUE;
1737 static GstMultiQueueItem *
1738 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1740 GstMultiQueueItem *item;
1742 item = g_slice_new (GstMultiQueueItem);
1743 item->object = object;
1744 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1745 item->posid = curid;
1746 item->is_query = GST_IS_QUERY (object);
1750 item->visible = FALSE;
1754 /* Each main loop attempts to push buffers until the return value
1755 * is not-linked. not-linked pads are not allowed to push data beyond
1756 * any linked pads, so they don't 'rush ahead of the pack'.
1759 gst_multi_queue_loop (GstPad * pad)
1762 GstMultiQueueItem *item;
1763 GstDataQueueItem *sitem;
1765 GstMiniObject *object = NULL;
1767 GstFlowReturn result;
1768 GstClockTimeDiff next_time;
1770 gboolean do_update_buffering = FALSE;
1771 gboolean dropping = FALSE;
1773 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1777 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1782 /* Get something from the queue, blocking until that happens, or we get
1784 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1787 item = (GstMultiQueueItem *) sitem;
1788 newid = item->posid;
1790 /* steal the object and destroy the item */
1791 object = gst_multi_queue_item_steal_object (item);
1792 gst_multi_queue_item_destroy (item);
1794 is_buffer = GST_IS_BUFFER (object);
1796 /* Get running time of the item. Events will have GST_CLOCK_STIME_NONE */
1797 next_time = get_running_time (&sq->src_segment, object, FALSE);
1799 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1800 sq->id, newid, sq->last_oldid);
1802 /* If we're not-linked, we do some extra work because we might need to
1803 * wait before pushing. If we're linked but there's a gap in the IDs,
1804 * or it's the first loop, or we just passed the previous highid,
1805 * we might need to wake some sleeping pad up, so there's extra work
1807 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1808 if (sq->srcresult == GST_FLOW_NOT_LINKED
1809 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1810 || sq->last_oldid > mq->highid) {
1811 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1812 gst_flow_get_name (sq->srcresult));
1814 /* Check again if we're flushing after the lock is taken,
1815 * the flush flag might have been changed in the meantime */
1817 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1821 /* Update the nextid so other threads know when to wake us up */
1823 /* Take into account the extra cache time since we're unlinked */
1824 if (GST_CLOCK_STIME_IS_VALID (next_time))
1825 next_time += mq->unlinked_cache_time;
1826 sq->next_time = next_time;
1828 /* Update the oldid (the last ID we output) for highid tracking */
1829 if (sq->last_oldid != G_MAXUINT32)
1830 sq->oldid = sq->last_oldid;
1832 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1833 gboolean should_wait;
1834 /* Go to sleep until it's time to push this buffer */
1836 /* Recompute the highid */
1837 compute_high_id (mq);
1838 /* Recompute the high time */
1839 compute_high_time (mq, sq->groupid);
1841 GST_DEBUG_OBJECT (mq,
1842 "groupid %d high_time %" GST_STIME_FORMAT " next_time %"
1843 GST_STIME_FORMAT, sq->groupid, GST_STIME_ARGS (sq->group_high_time),
1844 GST_STIME_ARGS (next_time));
1846 if (mq->sync_by_running_time) {
1847 if (sq->group_high_time == GST_CLOCK_STIME_NONE) {
1848 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
1849 (mq->high_time == GST_CLOCK_STIME_NONE
1850 || next_time > mq->high_time);
1852 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
1853 next_time > sq->group_high_time;
1856 should_wait = newid > mq->highid;
1858 while (should_wait && sq->srcresult == GST_FLOW_NOT_LINKED) {
1860 GST_DEBUG_OBJECT (mq,
1861 "queue %d sleeping for not-linked wakeup with "
1862 "newid %u, highid %u, next_time %" GST_STIME_FORMAT
1863 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1864 GST_STIME_ARGS (next_time), GST_STIME_ARGS (sq->group_high_time));
1866 /* Wake up all non-linked pads before we sleep */
1867 wake_up_next_non_linked (mq);
1870 g_cond_wait (&sq->turn, &mq->qlock);
1874 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1878 /* Recompute the high time and ID */
1879 compute_high_time (mq, sq->groupid);
1880 compute_high_id (mq);
1882 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1883 "wakeup with newid %u, highid %u, next_time %" GST_STIME_FORMAT
1884 ", high_time %" GST_STIME_FORMAT " mq high_time %" GST_STIME_FORMAT,
1885 sq->id, newid, mq->highid,
1886 GST_STIME_ARGS (next_time), GST_STIME_ARGS (sq->group_high_time),
1887 GST_STIME_ARGS (mq->high_time));
1889 if (mq->sync_by_running_time) {
1890 if (sq->group_high_time == GST_CLOCK_STIME_NONE) {
1891 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
1892 (mq->high_time == GST_CLOCK_STIME_NONE
1893 || next_time > mq->high_time);
1895 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
1896 next_time > sq->group_high_time;
1899 should_wait = newid > mq->highid;
1902 /* Re-compute the high_id in case someone else pushed */
1903 compute_high_id (mq);
1904 compute_high_time (mq, sq->groupid);
1906 compute_high_id (mq);
1907 compute_high_time (mq, sq->groupid);
1908 /* Wake up all non-linked pads */
1909 wake_up_next_non_linked (mq);
1911 /* We're done waiting, we can clear the nextid and nexttime */
1913 sq->next_time = GST_CLOCK_STIME_NONE;
1915 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1920 GST_LOG_OBJECT (mq, "sq:%d BEFORE PUSHING sq->srcresult: %s", sq->id,
1921 gst_flow_get_name (sq->srcresult));
1923 /* Update time stats */
1924 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1925 next_time = get_running_time (&sq->src_segment, object, TRUE);
1926 if (GST_CLOCK_STIME_IS_VALID (next_time)) {
1927 if (sq->last_time == GST_CLOCK_STIME_NONE || sq->last_time < next_time)
1928 sq->last_time = next_time;
1929 if (mq->high_time == GST_CLOCK_STIME_NONE || mq->high_time <= next_time) {
1930 /* Wake up all non-linked pads now that we advanced the high time */
1931 mq->high_time = next_time;
1932 wake_up_next_non_linked (mq);
1935 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1937 /* Try to push out the new object */
1938 result = gst_single_queue_push_one (mq, sq, object, &dropping);
1941 /* Check if we pushed something already and if this is
1942 * now a switch from an active to a non-active stream.
1944 * If it is, we reset all the waiting streams, let them
1945 * push another buffer to see if they're now active again.
1946 * This allows faster switching between streams and prevents
1947 * deadlocks if downstream does any waiting too.
1949 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1950 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1951 && result == GST_FLOW_NOT_LINKED) {
1954 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1957 compute_high_id (mq);
1958 compute_high_time (mq, sq->groupid);
1959 do_update_buffering = TRUE;
1961 /* maybe no-one is waiting */
1962 if (mq->numwaiting > 0) {
1963 /* Else figure out which singlequeue(s) need waking up */
1964 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1965 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1967 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1968 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1969 sq2->pushed = FALSE;
1970 sq2->srcresult = GST_FLOW_OK;
1971 g_cond_signal (&sq2->turn);
1980 /* now hold on a bit;
1981 * can not simply throw this result to upstream, because
1982 * that might already be onto another segment, so we have to make
1983 * sure we are relaying the correct info wrt proper segment */
1984 if (result == GST_FLOW_EOS && !dropping &&
1985 sq->srcresult != GST_FLOW_NOT_LINKED) {
1986 GST_DEBUG_OBJECT (mq, "starting EOS drop on sq %d", sq->id);
1988 /* pretend we have not seen EOS yet for upstream's sake */
1989 result = sq->srcresult;
1990 } else if (dropping && gst_data_queue_is_empty (sq->queue)) {
1991 /* queue empty, so stop dropping
1992 * we can commit the result we have now,
1993 * which is either OK after a segment, or EOS */
1994 GST_DEBUG_OBJECT (mq, "committed EOS drop on sq %d", sq->id);
1996 result = GST_FLOW_EOS;
1998 sq->srcresult = result;
1999 sq->last_oldid = newid;
2001 if (do_update_buffering)
2002 update_buffering (mq, sq);
2004 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2005 gst_multi_queue_post_buffering (mq);
2007 GST_LOG_OBJECT (mq, "sq:%d AFTER PUSHING sq->srcresult: %s (is_eos:%d)",
2008 sq->id, gst_flow_get_name (sq->srcresult), GST_PAD_IS_EOS (sq->srcpad));
2010 /* Need to make sure wake up any sleeping pads when we exit */
2011 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2012 if (mq->numwaiting > 0 && (GST_PAD_IS_EOS (sq->srcpad)
2013 || sq->srcresult == GST_FLOW_EOS)) {
2014 compute_high_time (mq, sq->groupid);
2015 compute_high_id (mq);
2016 wake_up_next_non_linked (mq);
2018 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2023 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
2024 && result != GST_FLOW_EOS)
2032 gst_mini_object_unref (object);
2034 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2035 sq->last_query = FALSE;
2036 g_cond_signal (&sq->query_handled);
2038 /* Post an error message if we got EOS while downstream
2039 * has returned an error flow return. After EOS there
2040 * will be no further buffer which could propagate the
2042 if (sq->is_eos && sq->srcresult < GST_FLOW_EOS) {
2043 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2044 GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
2046 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2049 /* upstream needs to see fatal result ASAP to shut things down,
2050 * but might be stuck in one of our other full queues;
2051 * so empty this one and trigger dynamic queue growth. At
2052 * this point the srcresult is not OK, NOT_LINKED
2053 * or EOS, i.e. a real failure */
2054 gst_single_queue_flush_queue (sq, FALSE);
2055 single_queue_underrun_cb (sq->queue, sq);
2056 gst_data_queue_set_flushing (sq->queue, TRUE);
2057 gst_pad_pause_task (sq->srcpad);
2058 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
2059 "SingleQueue[%d] task paused, reason:%s",
2060 sq->id, gst_flow_get_name (sq->srcresult));
2066 * gst_multi_queue_chain:
2068 * This is similar to GstQueue's chain function, except:
2069 * _ we don't have leak behaviours,
2070 * _ we push with a unique id (curid)
2072 static GstFlowReturn
2073 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2077 GstMultiQueueItem *item;
2079 GstClockTime timestamp, duration;
2081 sq = gst_pad_get_element_private (pad);
2084 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2090 /* Get a unique incrementing id */
2091 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2093 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
2094 duration = GST_BUFFER_DURATION (buffer);
2097 "SingleQueue %d : about to enqueue buffer %p with id %d (pts:%"
2098 GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT " dur:%" GST_TIME_FORMAT ")",
2099 sq->id, buffer, curid, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2100 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (duration));
2102 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
2104 /* Update interleave before pushing data into queue */
2105 if (mq->use_interleave) {
2106 GstClockTime val = timestamp;
2107 GstClockTimeDiff dval;
2109 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2110 if (val == GST_CLOCK_TIME_NONE)
2111 val = sq->sink_segment.position;
2112 if (duration != GST_CLOCK_TIME_NONE)
2115 dval = my_segment_to_running_time (&sq->sink_segment, val);
2116 if (GST_CLOCK_STIME_IS_VALID (dval)) {
2117 sq->cached_sinktime = dval;
2118 GST_DEBUG_OBJECT (mq,
2119 "Queue %d cached sink time now %" G_GINT64_FORMAT " %"
2120 GST_STIME_FORMAT, sq->id, sq->cached_sinktime,
2121 GST_STIME_ARGS (sq->cached_sinktime));
2122 calculate_interleave (mq, sq);
2124 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2127 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
2130 /* update time level, we must do this after pushing the data in the queue so
2131 * that we never end up filling the queue first. */
2132 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
2135 return sq->srcresult;
2140 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2141 sq->id, gst_flow_get_name (sq->srcresult));
2142 gst_multi_queue_item_destroy (item);
2147 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
2148 gst_buffer_unref (buffer);
2149 return GST_FLOW_EOS;
2154 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
2155 GstPadMode mode, gboolean active)
2161 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2162 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
2164 /* mq is NULL if the pad is activated/deactivated before being
2165 * added to the multiqueue */
2167 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2170 case GST_PAD_MODE_PUSH:
2172 /* All pads start off linked until they push one buffer */
2173 sq->srcresult = GST_FLOW_OK;
2175 gst_data_queue_set_flushing (sq->queue, FALSE);
2177 sq->srcresult = GST_FLOW_FLUSHING;
2178 sq->last_query = FALSE;
2179 g_cond_signal (&sq->query_handled);
2180 gst_data_queue_set_flushing (sq->queue, TRUE);
2182 /* Wait until streaming thread has finished */
2184 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2185 GST_PAD_STREAM_LOCK (pad);
2187 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2188 gst_data_queue_flush (sq->queue);
2190 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2191 GST_PAD_STREAM_UNLOCK (pad);
2193 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2203 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2204 gst_object_unref (mq);
2210 static GstFlowReturn
2211 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2216 GstMultiQueueItem *item;
2217 gboolean res = TRUE;
2218 GstFlowReturn flowret = GST_FLOW_OK;
2220 GstEvent *sref = NULL;
2222 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2223 mq = (GstMultiQueue *) parent;
2225 type = GST_EVENT_TYPE (event);
2228 case GST_EVENT_STREAM_START:
2230 if (mq->sync_by_running_time) {
2231 GstStreamFlags stream_flags;
2232 gst_event_parse_stream_flags (event, &stream_flags);
2233 if ((stream_flags & GST_STREAM_FLAG_SPARSE)) {
2234 GST_INFO_OBJECT (mq, "SingleQueue %d is a sparse stream", sq->id);
2235 sq->is_sparse = TRUE;
2239 sq->thread = g_thread_self ();
2241 /* Remove EOS flag */
2245 case GST_EVENT_FLUSH_START:
2246 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
2249 res = gst_pad_push_event (sq->srcpad, event);
2251 gst_single_queue_flush (mq, sq, TRUE, FALSE);
2254 case GST_EVENT_FLUSH_STOP:
2255 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
2258 res = gst_pad_push_event (sq->srcpad, event);
2260 gst_single_queue_flush (mq, sq, FALSE, FALSE);
2263 case GST_EVENT_SEGMENT:
2264 sref = gst_event_ref (event);
2267 /* take ref because the queue will take ownership and we need the event
2268 * afterwards to update the segment */
2269 sref = gst_event_ref (event);
2270 if (mq->use_interleave) {
2271 GstClockTime val, dur;
2273 gst_event_parse_gap (event, &val, &dur);
2274 if (GST_CLOCK_TIME_IS_VALID (val)) {
2275 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2276 if (GST_CLOCK_TIME_IS_VALID (dur))
2278 stime = my_segment_to_running_time (&sq->sink_segment, val);
2279 if (GST_CLOCK_STIME_IS_VALID (stime)) {
2280 sq->cached_sinktime = stime;
2281 calculate_interleave (mq, sq);
2283 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2289 if (!(GST_EVENT_IS_SERIALIZED (event))) {
2290 res = gst_pad_push_event (sq->srcpad, event);
2296 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2300 /* Get an unique incrementing id. */
2301 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2303 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
2305 GST_DEBUG_OBJECT (mq,
2306 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
2307 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
2309 if (!gst_data_queue_push (sq->queue, (GstDataQueueItem *) item))
2312 /* mark EOS when we received one, we must do that after putting the
2313 * buffer in the queue because EOS marks the buffer as filled. */
2316 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2319 /* Post an error message if we got EOS while downstream
2320 * has returned an error flow return. After EOS there
2321 * will be no further buffer which could propagate the
2323 if (sq->srcresult < GST_FLOW_EOS) {
2324 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2325 GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
2327 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2330 /* EOS affects the buffering state */
2331 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2332 update_buffering (mq, sq);
2333 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2334 single_queue_overrun_cb (sq->queue, sq);
2335 gst_multi_queue_post_buffering (mq);
2337 case GST_EVENT_SEGMENT:
2338 apply_segment (mq, sq, sref, &sq->sink_segment);
2339 gst_event_unref (sref);
2340 /* a new segment allows us to accept more buffers if we got EOS
2341 * from downstream */
2342 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2343 if (sq->srcresult == GST_FLOW_EOS)
2344 sq->srcresult = GST_FLOW_OK;
2345 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2349 apply_gap (mq, sq, sref, &sq->sink_segment);
2350 gst_event_unref (sref);
2357 flowret = GST_FLOW_ERROR;
2358 GST_DEBUG_OBJECT (mq, "SingleQueue %d : returning %s", sq->id,
2359 gst_flow_get_name (flowret));
2364 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2365 sq->id, gst_flow_get_name (sq->srcresult));
2367 gst_event_unref (sref);
2368 gst_multi_queue_item_destroy (item);
2369 return sq->srcresult;
2373 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return GST_FLOW_EOS");
2374 gst_event_unref (event);
2375 return GST_FLOW_EOS;
2380 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
2386 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2387 mq = (GstMultiQueue *) parent;
2389 switch (GST_QUERY_TYPE (query)) {
2391 if (GST_QUERY_IS_SERIALIZED (query)) {
2393 GstMultiQueueItem *item;
2395 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2396 if (sq->srcresult != GST_FLOW_OK)
2399 /* serialized events go in the queue. We need to be certain that we
2400 * don't cause deadlocks waiting for the query return value. We check if
2401 * the queue is empty (nothing is blocking downstream and the query can
2402 * be pushed for sure) or we are not buffering. If we are buffering,
2403 * the pipeline waits to unblock downstream until our queue fills up
2404 * completely, which can not happen if we block on the query..
2405 * Therefore we only potentially block when we are not buffering. */
2406 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
2407 /* Get an unique incrementing id. */
2408 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2410 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
2412 GST_DEBUG_OBJECT (mq,
2413 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
2414 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
2415 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2416 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
2417 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2418 if (!res || sq->flushing)
2420 /* it might be that the query has been taken out of the queue
2421 * while we were unlocked. So, we need to check if the last
2422 * handled query is the same one than the one we just
2423 * pushed. If it is, we don't need to wait for the condition
2424 * variable, otherwise we wait for the condition variable to
2426 while (!sq->flushing && sq->srcresult == GST_FLOW_OK
2427 && sq->last_handled_query != query)
2428 g_cond_wait (&sq->query_handled, &mq->qlock);
2429 res = sq->last_query;
2430 sq->last_handled_query = NULL;
2432 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
2433 "queue is not empty");
2436 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2438 /* default handling */
2439 res = gst_pad_query_default (pad, parent, query);
2447 GST_DEBUG_OBJECT (mq, "Flushing");
2448 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2454 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2455 GstPadMode mode, gboolean active)
2461 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2464 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2467 case GST_PAD_MODE_PUSH:
2469 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
2471 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
2472 /* make sure streaming finishes */
2473 result |= gst_pad_stop_task (pad);
2484 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2486 GstSingleQueue *sq = gst_pad_get_element_private (pad);
2487 GstMultiQueue *mq = sq->mqueue;
2490 switch (GST_EVENT_TYPE (event)) {
2491 case GST_EVENT_RECONFIGURE:
2492 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2493 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2494 sq->srcresult = GST_FLOW_OK;
2495 g_cond_signal (&sq->turn);
2497 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2499 ret = gst_pad_push_event (sq->sinkpad, event);
2502 ret = gst_pad_push_event (sq->sinkpad, event);
2510 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2514 /* FIXME, Handle position offset depending on queue size */
2515 switch (GST_QUERY_TYPE (query)) {
2517 /* default handling */
2518 res = gst_pad_query_default (pad, parent, query);
2525 * Next-non-linked functions
2528 /* WITH LOCK TAKEN */
2530 wake_up_next_non_linked (GstMultiQueue * mq)
2534 /* maybe no-one is waiting */
2535 if (mq->numwaiting < 1)
2538 if (mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (mq->high_time)) {
2539 /* Else figure out which singlequeue(s) need waking up */
2540 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2541 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2542 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2543 GstClockTimeDiff high_time;
2545 if (GST_CLOCK_STIME_IS_VALID (sq->group_high_time))
2546 high_time = sq->group_high_time;
2548 high_time = mq->high_time;
2550 if (GST_CLOCK_STIME_IS_VALID (sq->next_time) &&
2551 GST_CLOCK_STIME_IS_VALID (high_time)
2552 && sq->next_time <= high_time) {
2553 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2554 g_cond_signal (&sq->turn);
2559 /* Else figure out which singlequeue(s) need waking up */
2560 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2561 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2562 if (sq->srcresult == GST_FLOW_NOT_LINKED &&
2563 sq->nextid != 0 && sq->nextid <= mq->highid) {
2564 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2565 g_cond_signal (&sq->turn);
2571 /* WITH LOCK TAKEN */
2573 compute_high_id (GstMultiQueue * mq)
2575 /* The high-id is either the highest id among the linked pads, or if all
2576 * pads are not-linked, it's the lowest not-linked pad */
2578 guint32 lowest = G_MAXUINT32;
2579 guint32 highid = G_MAXUINT32;
2581 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2582 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2584 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2585 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2587 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2588 /* No need to consider queues which are not waiting */
2589 if (sq->nextid == 0) {
2590 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2594 if (sq->nextid < lowest)
2595 lowest = sq->nextid;
2596 } else if (!GST_PAD_IS_EOS (sq->srcpad) && sq->srcresult != GST_FLOW_EOS) {
2597 /* If we don't have a global highid, or the global highid is lower than
2598 * this single queue's last outputted id, store the queue's one,
2599 * unless the singlequeue output is at EOS */
2600 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2605 if (highid == G_MAXUINT32 || lowest < highid)
2606 mq->highid = lowest;
2608 mq->highid = highid;
2610 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2614 /* WITH LOCK TAKEN */
2616 compute_high_time (GstMultiQueue * mq, guint groupid)
2618 /* The high-time is either the highest last time among the linked
2619 * pads, or if all pads are not-linked, it's the lowest nex time of
2622 GstClockTimeDiff highest = GST_CLOCK_STIME_NONE;
2623 GstClockTimeDiff lowest = GST_CLOCK_STIME_NONE;
2624 GstClockTimeDiff group_high = GST_CLOCK_STIME_NONE;
2625 GstClockTimeDiff group_low = GST_CLOCK_STIME_NONE;
2626 GstClockTimeDiff res;
2627 /* Number of streams which belong to groupid */
2628 guint group_count = 0;
2630 if (!mq->sync_by_running_time)
2631 /* return GST_CLOCK_STIME_NONE; */
2634 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2635 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2638 "inspecting sq:%d (group:%d) , next_time:%" GST_STIME_FORMAT
2639 ", last_time:%" GST_STIME_FORMAT ", srcresult:%s", sq->id, sq->groupid,
2640 GST_STIME_ARGS (sq->next_time), GST_STIME_ARGS (sq->last_time),
2641 gst_flow_get_name (sq->srcresult));
2643 if (sq->groupid == groupid)
2646 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2647 /* No need to consider queues which are not waiting */
2648 if (!GST_CLOCK_STIME_IS_VALID (sq->next_time)) {
2649 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2653 if (lowest == GST_CLOCK_STIME_NONE || sq->next_time < lowest)
2654 lowest = sq->next_time;
2655 if (sq->groupid == groupid && (group_low == GST_CLOCK_STIME_NONE
2656 || sq->next_time < group_low))
2657 group_low = sq->next_time;
2658 } else if (!GST_PAD_IS_EOS (sq->srcpad) && sq->srcresult != GST_FLOW_EOS) {
2659 /* If we don't have a global high time, or the global high time
2660 * is lower than this single queue's last outputted time, store
2661 * the queue's one, unless the singlequeue output is at EOS. */
2662 if (highest == GST_CLOCK_STIME_NONE
2663 || (sq->last_time != GST_CLOCK_STIME_NONE && sq->last_time > highest))
2664 highest = sq->last_time;
2665 if (sq->groupid == groupid && (group_high == GST_CLOCK_STIME_NONE
2666 || (sq->last_time != GST_CLOCK_STIME_NONE
2667 && sq->last_time > group_high)))
2668 group_high = sq->last_time;
2671 "highest now %" GST_STIME_FORMAT " lowest %" GST_STIME_FORMAT,
2672 GST_STIME_ARGS (highest), GST_STIME_ARGS (lowest));
2673 if (sq->groupid == groupid)
2675 "grouphigh %" GST_STIME_FORMAT " grouplow %" GST_STIME_FORMAT,
2676 GST_STIME_ARGS (group_high), GST_STIME_ARGS (group_low));
2679 if (highest == GST_CLOCK_STIME_NONE)
2680 mq->high_time = lowest;
2682 mq->high_time = highest;
2684 /* If there's only one stream of a given type, use the global high */
2685 if (group_count < 2)
2686 res = GST_CLOCK_STIME_NONE;
2687 else if (group_high == GST_CLOCK_STIME_NONE)
2692 GST_LOG_OBJECT (mq, "group count %d for groupid %u", group_count, groupid);
2694 "MQ High time is now : %" GST_STIME_FORMAT ", group %d high time %"
2695 GST_STIME_FORMAT ", lowest non-linked %" GST_STIME_FORMAT,
2696 GST_STIME_ARGS (mq->high_time), groupid, GST_STIME_ARGS (mq->high_time),
2697 GST_STIME_ARGS (lowest));
2699 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2700 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2701 if (groupid == sq->groupid)
2702 sq->group_high_time = res;
2706 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2707 ((q)->max_size.format) <= (value))
2710 * GstSingleQueue functions
2713 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2715 GstMultiQueue *mq = sq->mqueue;
2717 GstDataQueueSize size;
2718 gboolean filled = TRUE;
2719 gboolean empty_found = FALSE;
2721 gst_data_queue_get_level (sq->queue, &size);
2724 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2725 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2726 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2729 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2731 /* check if we reached the hard time/bytes limits;
2732 time limit is only taken into account for non-sparse streams */
2733 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
2734 (!sq->is_sparse && IS_FILLED (sq, time, sq->cur_time))) {
2738 /* Search for empty queues */
2739 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2740 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2745 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2746 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2750 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2751 if (gst_data_queue_is_empty (oq->queue) && !oq->is_sparse) {
2752 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2758 /* if hard limits are not reached then we allow one more buffer in the full
2759 * queue, but only if any of the other singelqueues are empty */
2761 if (IS_FILLED (sq, visible, size.visible)) {
2762 sq->max_size.visible = size.visible + 1;
2763 GST_DEBUG_OBJECT (mq,
2764 "Bumping single queue %d max visible to %d",
2765 sq->id, sq->max_size.visible);
2771 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2773 /* Overrun is always forwarded, since this is blocking the upstream element */
2775 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2776 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2781 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2783 gboolean empty = TRUE;
2784 GstMultiQueue *mq = sq->mqueue;
2787 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2788 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2792 "Single Queue %d is empty, Checking other single queues", sq->id);
2795 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2796 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2797 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2799 if (gst_data_queue_is_full (oq->queue)) {
2800 GstDataQueueSize size;
2802 gst_data_queue_get_level (oq->queue, &size);
2803 if (IS_FILLED (oq, visible, size.visible)) {
2804 oq->max_size.visible = size.visible + 1;
2805 GST_DEBUG_OBJECT (mq,
2806 "queue %d is filled, bumping its max visible to %d", oq->id,
2807 oq->max_size.visible);
2808 gst_data_queue_limits_changed (oq->queue);
2811 if (!gst_data_queue_is_empty (oq->queue) || oq->is_sparse)
2814 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2817 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2818 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2823 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2824 guint64 time, GstSingleQueue * sq)
2827 GstMultiQueue *mq = sq->mqueue;
2829 GST_DEBUG_OBJECT (mq,
2830 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2831 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2832 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2834 /* we are always filled on EOS */
2838 /* we never go past the max visible items unless we are in buffering mode */
2839 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2842 /* check time or bytes */
2843 res = IS_FILLED (sq, bytes, bytes);
2844 /* We only care about limits in time if we're not a sparse stream or
2845 * we're not syncing by running time */
2846 if (!sq->is_sparse || !mq->sync_by_running_time) {
2847 /* If unlinked, take into account the extra unlinked cache time */
2848 if (mq->sync_by_running_time && sq->srcresult == GST_FLOW_NOT_LINKED) {
2849 if (sq->cur_time > mq->unlinked_cache_time)
2850 res |= IS_FILLED (sq, time, sq->cur_time - mq->unlinked_cache_time);
2854 res |= IS_FILLED (sq, time, sq->cur_time);
2861 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2863 GstDataQueueItem *sitem;
2864 GstMultiQueueItem *mitem;
2865 gboolean was_flushing = FALSE;
2867 while (!gst_data_queue_is_empty (sq->queue)) {
2868 GstMiniObject *data;
2870 /* FIXME: If this fails here although the queue is not empty,
2871 * we're flushing... but we want to rescue all sticky
2872 * events nonetheless.
2874 if (!gst_data_queue_pop (sq->queue, &sitem)) {
2875 was_flushing = TRUE;
2876 gst_data_queue_set_flushing (sq->queue, FALSE);
2880 mitem = (GstMultiQueueItem *) sitem;
2882 data = sitem->object;
2884 if (!full && !mitem->is_query && GST_IS_EVENT (data)
2885 && GST_EVENT_IS_STICKY (data)
2886 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2887 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2888 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2891 sitem->destroy (sitem);
2894 gst_data_queue_flush (sq->queue);
2896 gst_data_queue_set_flushing (sq->queue, TRUE);
2898 GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2899 update_buffering (sq->mqueue, sq);
2900 GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2901 gst_multi_queue_post_buffering (sq->mqueue);
2905 gst_single_queue_free (GstSingleQueue * sq)
2908 gst_data_queue_flush (sq->queue);
2909 g_object_unref (sq->queue);
2910 g_cond_clear (&sq->turn);
2911 g_cond_clear (&sq->query_handled);
2915 static GstSingleQueue *
2916 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2919 GstMultiQueuePad *mqpad;
2920 GstPadTemplate *templ;
2923 guint temp_id = (id == -1) ? 0 : id;
2925 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2927 /* Find an unused queue ID, if possible the passed one */
2928 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2929 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2930 /* This works because the IDs are sorted in ascending order */
2931 if (sq2->id == temp_id) {
2932 /* If this ID was requested by the caller return NULL,
2933 * otherwise just get us the next one */
2935 temp_id = sq2->id + 1;
2937 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2940 } else if (sq2->id > temp_id) {
2945 sq = g_new0 (GstSingleQueue, 1);
2948 sq->groupid = DEFAULT_PAD_GROUP_ID;
2949 sq->group_high_time = GST_CLOCK_STIME_NONE;
2951 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2952 mqueue->queues_cookie++;
2954 /* copy over max_size and extra_size so we don't need to take the lock
2955 * any longer when checking if the queue is full. */
2956 sq->max_size.visible = mqueue->max_size.visible;
2957 sq->max_size.bytes = mqueue->max_size.bytes;
2958 sq->max_size.time = mqueue->max_size.time;
2960 sq->extra_size.visible = mqueue->extra_size.visible;
2961 sq->extra_size.bytes = mqueue->extra_size.bytes;
2962 sq->extra_size.time = mqueue->extra_size.time;
2964 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2966 sq->mqueue = mqueue;
2967 sq->srcresult = GST_FLOW_FLUSHING;
2969 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2970 single_queue_check_full,
2971 (GstDataQueueFullCallback) single_queue_overrun_cb,
2972 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2974 sq->is_sparse = FALSE;
2975 sq->flushing = FALSE;
2977 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2978 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2982 sq->next_time = GST_CLOCK_STIME_NONE;
2983 sq->last_time = GST_CLOCK_STIME_NONE;
2984 g_cond_init (&sq->turn);
2985 g_cond_init (&sq->query_handled);
2987 sq->sinktime = GST_CLOCK_STIME_NONE;
2988 sq->srctime = GST_CLOCK_STIME_NONE;
2989 sq->sink_tainted = TRUE;
2990 sq->src_tainted = TRUE;
2992 name = g_strdup_printf ("sink_%u", sq->id);
2993 templ = gst_static_pad_template_get (&sinktemplate);
2994 sq->sinkpad = g_object_new (GST_TYPE_MULTIQUEUE_PAD, "name", name,
2995 "direction", templ->direction, "template", templ, NULL);
2996 gst_object_unref (templ);
2999 mqpad = (GstMultiQueuePad *) sq->sinkpad;
3002 gst_pad_set_chain_function (sq->sinkpad,
3003 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
3004 gst_pad_set_activatemode_function (sq->sinkpad,
3005 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
3006 gst_pad_set_event_full_function (sq->sinkpad,
3007 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
3008 gst_pad_set_query_function (sq->sinkpad,
3009 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
3010 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
3011 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
3012 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
3014 name = g_strdup_printf ("src_%u", sq->id);
3015 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
3018 gst_pad_set_activatemode_function (sq->srcpad,
3019 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
3020 gst_pad_set_event_function (sq->srcpad,
3021 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
3022 gst_pad_set_query_function (sq->srcpad,
3023 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
3024 gst_pad_set_iterate_internal_links_function (sq->srcpad,
3025 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
3026 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
3028 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
3029 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
3031 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
3033 /* only activate the pads when we are not in the NULL state
3034 * and add the pad under the state_lock to prevend state changes
3035 * between activating and adding */
3036 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
3037 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
3038 gst_pad_set_active (sq->srcpad, TRUE);
3039 gst_pad_set_active (sq->sinkpad, TRUE);
3041 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
3042 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
3043 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
3045 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",