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
27 * @see_also: #GstQueue
31 * Multiqueue is similar to a normal #GstQueue with the following additional
35 * <itemizedlist><title>Multiple streamhandling</title>
37 * The element handles queueing data on more than one stream at once. To
38 * achieve such a feature it has request sink pads (sink%u) and
39 * 'sometimes' src pads (src%u).
41 * When requesting a given sinkpad with gst_element_request_pad(),
42 * the associated srcpad for that stream will be created.
43 * Example: requesting sink1 will generate src1.
48 * <itemizedlist><title>Non-starvation on multiple streams</title>
50 * If more than one stream is used with the element, the streams' queues
51 * will be dynamically grown (up to a limit), in order to ensure that no
52 * stream is risking data starvation. This guarantees that at any given
53 * time there are at least N bytes queued and available for each individual
56 * If an EOS event comes through a srcpad, the associated queue will be
57 * considered as 'not-empty' in the queue-size-growing algorithm.
62 * <itemizedlist><title>Non-linked srcpads graceful handling</title>
64 * In order to better support dynamic switching between streams, the multiqueue
65 * (unlike the current GStreamer queue) continues to push buffers on non-linked
66 * pads rather than shutting down.
68 * In addition, to prevent a non-linked stream from very quickly consuming all
69 * available buffers and thus 'racing ahead' of the other streams, the element
70 * must ensure that buffers and inlined events for a non-linked stream are pushed
71 * in the same order as they were received, relative to the other streams
72 * controlled by the element. This means that a buffer cannot be pushed to a
73 * non-linked pad any sooner than buffers in any other stream which were received
81 * Data is queued until one of the limits specified by the
82 * #GstMultiQueue:max-size-buffers, #GstMultiQueue:max-size-bytes and/or
83 * #GstMultiQueue:max-size-time properties has been reached. Any attempt to push
84 * more buffers into the queue will block the pushing thread until more space
85 * becomes available. #GstMultiQueue:extra-size-buffers,
88 * #GstMultiQueue:extra-size-bytes and #GstMultiQueue:extra-size-time are
92 * The default queue size limits are 5 buffers, 10MB of data, or
93 * two second worth of data, whichever is reached first. Note that the number
94 * of buffers will dynamically grow depending on the fill level of
98 * The #GstMultiQueue::underrun signal is emitted when all of the queues
99 * are empty. The #GstMultiQueue::overrun signal is emitted when one of the
101 * Both signals are emitted from the context of the streaming thread.
104 * When using #GstMultiQueue:sync-by-running-time the unlinked streams will
105 * be throttled by the highest running-time of linked streams. This allows
106 * further relinking of those unlinked streams without them being in the
107 * future (i.e. to achieve gapless playback).
108 * When dealing with streams which have got different consumption requirements
109 * downstream (ex: video decoders which will consume more buffer (in time) than
110 * audio decoders), it is recommended to group streams of the same type
111 * by using the pad "group-id" property. This will further throttle streams
112 * in time within that group.
123 #include "gstmultiqueue.h"
124 #include <gst/glib-compat-private.h>
128 * @sinkpad: associated sink #GstPad
129 * @srcpad: associated source #GstPad
131 * Structure containing all information and properties about
134 typedef struct _GstSingleQueue GstSingleQueue;
136 struct _GstSingleQueue
138 /* unique identifier of the queue */
140 /* group of streams to which this queue belongs to */
143 GstMultiQueue *mqueue;
148 /* flowreturn of previous srcpad push */
149 GstFlowReturn srcresult;
150 /* If something was actually pushed on
151 * this pad after flushing/pad activation
152 * and the srcresult corresponds to something
158 GstSegment sink_segment;
159 GstSegment src_segment;
160 gboolean has_src_segment; /* preferred over initializing the src_segment to
161 * UNDEFINED as this doesn't requires adding ifs
162 * in every segment usage */
164 /* position of src/sink */
165 GstClockTimeDiff sinktime, srctime;
166 /* cached input value, used for interleave */
167 GstClockTimeDiff cached_sinktime;
168 /* TRUE if either position needs to be recalculated */
169 gboolean sink_tainted, src_tainted;
173 GstDataQueueSize max_size, extra_size;
174 GstClockTime cur_time;
180 /* Protected by global lock */
181 guint32 nextid; /* ID of the next object waiting to be pushed */
182 guint32 oldid; /* ID of the last object pushed (last in a series) */
183 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
184 GstClockTimeDiff next_time; /* End running time of next buffer to be pushed */
185 GstClockTimeDiff last_time; /* Start running time of last pushed buffer */
186 GCond turn; /* SingleQueue turn waiting conditional */
188 /* for serialized queries */
191 GstQuery *last_handled_query;
193 /* For interleave calculation */
198 /* Extension of GstDataQueueItem structure for our usage */
199 typedef struct _GstMultiQueueItem GstMultiQueueItem;
201 struct _GstMultiQueueItem
203 GstMiniObject *object;
208 GDestroyNotify destroy;
214 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
215 static void gst_single_queue_free (GstSingleQueue * squeue);
217 static void wake_up_next_non_linked (GstMultiQueue * mq);
218 static void compute_high_id (GstMultiQueue * mq);
219 static GstClockTimeDiff compute_high_time (GstMultiQueue * mq, guint groupid);
220 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
221 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
223 static void update_buffering (GstMultiQueue * mq, GstSingleQueue * sq);
224 static void gst_multi_queue_post_buffering (GstMultiQueue * mq);
225 static void recheck_buffering_status (GstMultiQueue * mq);
227 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
229 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
232 GST_STATIC_CAPS_ANY);
234 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
237 GST_STATIC_CAPS_ANY);
239 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
240 #define GST_CAT_DEFAULT (multi_queue_debug)
242 /* Signals and args */
250 /* default limits, we try to keep up to 2 seconds of data and if there is not
251 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
252 * there is data in the queues. Normally, the byte and time limits are not hit
253 * in theses conditions. */
254 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
255 #define DEFAULT_MAX_SIZE_BUFFERS 5
256 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
258 /* second limits. When we hit one of the above limits we are probably dealing
259 * with a badly muxed file and we scale the limits to these emergency values.
260 * This is currently not yet implemented.
261 * Since we dynamically scale the queue buffer size up to the limits but avoid
262 * going above the max-size-buffers when we can, we don't really need this
263 * aditional extra size. */
264 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
265 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
266 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
268 #define DEFAULT_USE_BUFFERING FALSE
269 #define DEFAULT_LOW_PERCENT 10
270 #define DEFAULT_HIGH_PERCENT 99
271 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
272 #define DEFAULT_USE_INTERLEAVE FALSE
273 #define DEFAULT_UNLINKED_CACHE_TIME 250 * GST_MSECOND
278 PROP_EXTRA_SIZE_BYTES,
279 PROP_EXTRA_SIZE_BUFFERS,
280 PROP_EXTRA_SIZE_TIME,
282 PROP_MAX_SIZE_BUFFERS,
287 PROP_SYNC_BY_RUNNING_TIME,
289 PROP_UNLINKED_CACHE_TIME,
293 /* GstMultiQueuePad */
295 #define DEFAULT_PAD_GROUP_ID 0
303 #define GST_TYPE_MULTIQUEUE_PAD (gst_multiqueue_pad_get_type())
304 #define GST_MULTIQUEUE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePad))
305 #define GST_IS_MULTIQUEUE_PAD(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTIQUEUE_PAD))
306 #define GST_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
307 #define GST_IS_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_MULTIQUEUE_PAD))
308 #define GST_MULTIQUEUE_PAD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
310 struct _GstMultiQueuePad
317 struct _GstMultiQueuePadClass
319 GstPadClass parent_class;
322 GType gst_multiqueue_pad_get_type (void);
324 G_DEFINE_TYPE (GstMultiQueuePad, gst_multiqueue_pad, GST_TYPE_PAD);
326 gst_multiqueue_pad_get_property (GObject * object, guint prop_id,
327 GValue * value, GParamSpec * pspec)
329 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
332 case PROP_PAD_GROUP_ID:
334 g_value_set_uint (value, pad->sq->groupid);
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343 gst_multiqueue_pad_set_property (GObject * object, guint prop_id,
344 const GValue * value, GParamSpec * pspec)
346 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
349 case PROP_PAD_GROUP_ID:
350 GST_OBJECT_LOCK (pad);
352 pad->sq->groupid = g_value_get_uint (value);
353 GST_OBJECT_UNLOCK (pad);
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
362 gst_multiqueue_pad_class_init (GstMultiQueuePadClass * klass)
364 GObjectClass *gobject_class = (GObjectClass *) klass;
366 gobject_class->set_property = gst_multiqueue_pad_set_property;
367 gobject_class->get_property = gst_multiqueue_pad_get_property;
370 * GstMultiQueuePad:group-id:
372 * Group to which this pad belongs.
376 g_object_class_install_property (gobject_class, PROP_PAD_GROUP_ID,
377 g_param_spec_uint ("group-id", "Group ID",
378 "Group to which this pad belongs", 0, G_MAXUINT32,
379 DEFAULT_PAD_GROUP_ID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 gst_multiqueue_pad_init (GstMultiQueuePad * pad)
389 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
390 g_mutex_lock (&q->qlock); \
393 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
394 g_mutex_unlock (&q->qlock); \
397 #define SET_PERCENT(mq, perc) G_STMT_START { \
398 if (perc != mq->percent) { \
399 mq->percent = perc; \
400 mq->percent_changed = TRUE; \
401 GST_DEBUG_OBJECT (mq, "buffering %d percent", perc); \
405 /* Convenience function */
406 static inline GstClockTimeDiff
407 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
409 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
411 if (GST_CLOCK_TIME_IS_VALID (val)) {
413 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
422 static void gst_multi_queue_finalize (GObject * object);
423 static void gst_multi_queue_set_property (GObject * object,
424 guint prop_id, const GValue * value, GParamSpec * pspec);
425 static void gst_multi_queue_get_property (GObject * object,
426 guint prop_id, GValue * value, GParamSpec * pspec);
428 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
429 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
430 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
431 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
432 element, GstStateChange transition);
434 static void gst_multi_queue_loop (GstPad * pad);
437 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
438 #define gst_multi_queue_parent_class parent_class
439 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
442 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
445 gst_multi_queue_class_init (GstMultiQueueClass * klass)
447 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
448 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
450 gobject_class->set_property = gst_multi_queue_set_property;
451 gobject_class->get_property = gst_multi_queue_get_property;
456 * GstMultiQueue::underrun:
457 * @multiqueue: the multiqueue instance
459 * This signal is emitted from the streaming thread when there is
460 * no data in any of the queues inside the multiqueue instance (underrun).
462 * This indicates either starvation or EOS from the upstream data sources.
464 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
465 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
466 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
467 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
470 * GstMultiQueue::overrun:
471 * @multiqueue: the multiqueue instance
473 * Reports that one of the queues in the multiqueue is full (overrun).
474 * A queue is full if the total amount of data inside it (num-buffers, time,
475 * size) is higher than the boundary values which can be set through the
476 * GObject properties.
478 * This can be used as an indicator of pre-roll.
480 gst_multi_queue_signals[SIGNAL_OVERRUN] =
481 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
482 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
483 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
487 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
488 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
489 "Max. amount of data in the queue (bytes, 0=disable)",
490 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
491 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
492 G_PARAM_STATIC_STRINGS));
493 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
494 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
495 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
496 DEFAULT_MAX_SIZE_BUFFERS,
497 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
498 G_PARAM_STATIC_STRINGS));
499 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
500 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
501 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
502 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
503 G_PARAM_STATIC_STRINGS));
505 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
506 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
507 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
508 " (NOT IMPLEMENTED)",
509 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
510 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
511 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
512 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
513 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
514 " (NOT IMPLEMENTED)",
515 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
516 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
517 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
518 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
519 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
520 " (NOT IMPLEMENTED)",
521 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
522 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
525 * GstMultiQueue:use-buffering
527 * Enable the buffering option in multiqueue so that BUFFERING messages are
528 * emitted based on low-/high-percent thresholds.
530 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
531 g_param_spec_boolean ("use-buffering", "Use buffering",
532 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
533 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
534 G_PARAM_STATIC_STRINGS));
536 * GstMultiQueue:low-percent
538 * Low threshold percent for buffering to start.
540 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
541 g_param_spec_int ("low-percent", "Low percent",
542 "Low threshold for buffering to start", 0, 100,
543 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
545 * GstMultiQueue:high-percent
547 * High threshold percent for buffering to finish.
549 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
550 g_param_spec_int ("high-percent", "High percent",
551 "High threshold for buffering to finish", 0, 100,
552 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
555 * GstMultiQueue:sync-by-running-time
557 * If enabled multiqueue will synchronize deactivated or not-linked streams
558 * to the activated and linked streams by taking the running time.
559 * Otherwise multiqueue will synchronize the deactivated or not-linked
560 * streams by keeping the order in which buffers and events arrived compared
561 * to active and linked streams.
563 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
564 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
565 "Synchronize deactivated or not-linked streams by running time",
566 DEFAULT_SYNC_BY_RUNNING_TIME,
567 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
569 g_object_class_install_property (gobject_class, PROP_USE_INTERLEAVE,
570 g_param_spec_boolean ("use-interleave", "Use interleave",
571 "Adjust time limits based on input interleave",
572 DEFAULT_USE_INTERLEAVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
574 g_object_class_install_property (gobject_class, PROP_UNLINKED_CACHE_TIME,
575 g_param_spec_uint64 ("unlinked-cache-time", "Unlinked cache time (ns)",
576 "Extra buffering in time for unlinked streams (if 'sync-by-running-time')",
577 0, G_MAXUINT64, DEFAULT_UNLINKED_CACHE_TIME,
578 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
579 G_PARAM_STATIC_STRINGS));
582 gobject_class->finalize = gst_multi_queue_finalize;
584 gst_element_class_set_static_metadata (gstelement_class,
586 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
587 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
588 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
590 gstelement_class->request_new_pad =
591 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
592 gstelement_class->release_pad =
593 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
594 gstelement_class->change_state =
595 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
599 gst_multi_queue_init (GstMultiQueue * mqueue)
601 mqueue->nbqueues = 0;
602 mqueue->queues = NULL;
604 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
605 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
606 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
608 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
609 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
610 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
612 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
613 mqueue->low_percent = DEFAULT_LOW_PERCENT;
614 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
616 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
617 mqueue->use_interleave = DEFAULT_USE_INTERLEAVE;
618 mqueue->unlinked_cache_time = DEFAULT_UNLINKED_CACHE_TIME;
622 mqueue->high_time = GST_CLOCK_STIME_NONE;
624 g_mutex_init (&mqueue->qlock);
625 g_mutex_init (&mqueue->buffering_post_lock);
629 gst_multi_queue_finalize (GObject * object)
631 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
633 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
634 g_list_free (mqueue->queues);
635 mqueue->queues = NULL;
636 mqueue->queues_cookie++;
638 /* free/unref instance data */
639 g_mutex_clear (&mqueue->qlock);
640 g_mutex_clear (&mqueue->buffering_post_lock);
642 G_OBJECT_CLASS (parent_class)->finalize (object);
645 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
646 GList * tmp = mq->queues; \
648 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
649 q->max_size.format = mq->max_size.format; \
650 update_buffering (mq, q); \
651 gst_data_queue_limits_changed (q->queue); \
652 tmp = g_list_next(tmp); \
657 gst_multi_queue_set_property (GObject * object, guint prop_id,
658 const GValue * value, GParamSpec * pspec)
660 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
663 case PROP_MAX_SIZE_BYTES:
664 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
665 mq->max_size.bytes = g_value_get_uint (value);
666 SET_CHILD_PROPERTY (mq, bytes);
667 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
668 gst_multi_queue_post_buffering (mq);
670 case PROP_MAX_SIZE_BUFFERS:
673 gint new_size = g_value_get_uint (value);
675 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
677 mq->max_size.visible = new_size;
681 GstDataQueueSize size;
682 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
683 gst_data_queue_get_level (q->queue, &size);
685 GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
686 " current: %d, current max %d", q->id, new_size, size.visible,
687 q->max_size.visible);
689 /* do not reduce max size below current level if the single queue
690 * has grown because of empty queue */
692 q->max_size.visible = new_size;
693 } else if (q->max_size.visible == 0) {
694 q->max_size.visible = MAX (new_size, size.visible);
695 } else if (new_size > size.visible) {
696 q->max_size.visible = new_size;
698 update_buffering (mq, q);
699 gst_data_queue_limits_changed (q->queue);
700 tmp = g_list_next (tmp);
703 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
704 gst_multi_queue_post_buffering (mq);
708 case PROP_MAX_SIZE_TIME:
709 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
710 mq->max_size.time = g_value_get_uint64 (value);
711 SET_CHILD_PROPERTY (mq, time);
712 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
713 gst_multi_queue_post_buffering (mq);
715 case PROP_EXTRA_SIZE_BYTES:
716 mq->extra_size.bytes = g_value_get_uint (value);
718 case PROP_EXTRA_SIZE_BUFFERS:
719 mq->extra_size.visible = g_value_get_uint (value);
721 case PROP_EXTRA_SIZE_TIME:
722 mq->extra_size.time = g_value_get_uint64 (value);
724 case PROP_USE_BUFFERING:
725 mq->use_buffering = g_value_get_boolean (value);
726 recheck_buffering_status (mq);
728 case PROP_LOW_PERCENT:
729 mq->low_percent = g_value_get_int (value);
730 /* Recheck buffering status - the new low-percent value might
731 * be above the current fill level. If the old low-percent one
732 * was below the current level, this means that mq->buffering is
733 * disabled and needs to be re-enabled. */
734 recheck_buffering_status (mq);
736 case PROP_HIGH_PERCENT:
737 mq->high_percent = g_value_get_int (value);
738 recheck_buffering_status (mq);
740 case PROP_SYNC_BY_RUNNING_TIME:
741 mq->sync_by_running_time = g_value_get_boolean (value);
743 case PROP_USE_INTERLEAVE:
744 mq->use_interleave = g_value_get_boolean (value);
746 case PROP_UNLINKED_CACHE_TIME:
747 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
748 mq->unlinked_cache_time = g_value_get_uint64 (value);
749 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
750 gst_multi_queue_post_buffering (mq);
753 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
759 gst_multi_queue_get_property (GObject * object, guint prop_id,
760 GValue * value, GParamSpec * pspec)
762 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
764 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
767 case PROP_EXTRA_SIZE_BYTES:
768 g_value_set_uint (value, mq->extra_size.bytes);
770 case PROP_EXTRA_SIZE_BUFFERS:
771 g_value_set_uint (value, mq->extra_size.visible);
773 case PROP_EXTRA_SIZE_TIME:
774 g_value_set_uint64 (value, mq->extra_size.time);
776 case PROP_MAX_SIZE_BYTES:
777 g_value_set_uint (value, mq->max_size.bytes);
779 case PROP_MAX_SIZE_BUFFERS:
780 g_value_set_uint (value, mq->max_size.visible);
782 case PROP_MAX_SIZE_TIME:
783 g_value_set_uint64 (value, mq->max_size.time);
785 case PROP_USE_BUFFERING:
786 g_value_set_boolean (value, mq->use_buffering);
788 case PROP_LOW_PERCENT:
789 g_value_set_int (value, mq->low_percent);
791 case PROP_HIGH_PERCENT:
792 g_value_set_int (value, mq->high_percent);
794 case PROP_SYNC_BY_RUNNING_TIME:
795 g_value_set_boolean (value, mq->sync_by_running_time);
797 case PROP_USE_INTERLEAVE:
798 g_value_set_boolean (value, mq->use_interleave);
800 case PROP_UNLINKED_CACHE_TIME:
801 g_value_set_uint64 (value, mq->unlinked_cache_time);
804 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
808 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
812 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
814 GstIterator *it = NULL;
816 GstSingleQueue *squeue;
817 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
820 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
821 squeue = gst_pad_get_element_private (pad);
825 if (squeue->sinkpad == pad)
826 opad = gst_object_ref (squeue->srcpad);
827 else if (squeue->srcpad == pad)
828 opad = gst_object_ref (squeue->sinkpad);
832 g_value_init (&val, GST_TYPE_PAD);
833 g_value_set_object (&val, opad);
834 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
835 g_value_unset (&val);
837 gst_object_unref (opad);
840 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
851 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
852 const gchar * name, const GstCaps * caps)
854 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
855 GstSingleQueue *squeue;
860 sscanf (name + 4, "_%u", &temp_id);
861 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
864 /* Create a new single queue, add the sink and source pad and return the sink pad */
865 squeue = gst_single_queue_new (mqueue, temp_id);
867 new_pad = squeue ? squeue->sinkpad : NULL;
869 GST_DEBUG_OBJECT (mqueue, "Returning pad %" GST_PTR_FORMAT, new_pad);
875 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
877 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
878 GstSingleQueue *sq = NULL;
881 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
883 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
884 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
885 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
886 sq = (GstSingleQueue *) tmp->data;
888 if (sq->sinkpad == pad)
893 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
894 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
898 /* FIXME: The removal of the singlequeue should probably not happen until it
899 * finishes draining */
901 /* remove it from the list */
902 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
903 mqueue->queues_cookie++;
905 /* FIXME : recompute next-non-linked */
906 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
908 /* delete SingleQueue */
909 gst_data_queue_set_flushing (sq->queue, TRUE);
911 gst_pad_set_active (sq->srcpad, FALSE);
912 gst_pad_set_active (sq->sinkpad, FALSE);
913 gst_pad_set_element_private (sq->srcpad, NULL);
914 gst_pad_set_element_private (sq->sinkpad, NULL);
915 gst_element_remove_pad (element, sq->srcpad);
916 gst_element_remove_pad (element, sq->sinkpad);
917 gst_single_queue_free (sq);
920 static GstStateChangeReturn
921 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
923 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
924 GstSingleQueue *sq = NULL;
925 GstStateChangeReturn result;
927 switch (transition) {
928 case GST_STATE_CHANGE_READY_TO_PAUSED:{
931 /* Set all pads to non-flushing */
932 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
933 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
934 sq = (GstSingleQueue *) tmp->data;
935 sq->flushing = FALSE;
938 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
939 SET_CHILD_PROPERTY (mqueue, visible);
941 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
942 gst_multi_queue_post_buffering (mqueue);
946 case GST_STATE_CHANGE_PAUSED_TO_READY:{
949 /* Un-wait all waiting pads */
950 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
951 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
952 sq = (GstSingleQueue *) tmp->data;
954 g_cond_signal (&sq->turn);
956 sq->last_query = FALSE;
957 g_cond_signal (&sq->query_handled);
959 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
966 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
968 switch (transition) {
977 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
982 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
986 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
987 sq->srcresult = GST_FLOW_FLUSHING;
988 gst_data_queue_set_flushing (sq->queue, TRUE);
992 /* wake up non-linked task */
993 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
995 g_cond_signal (&sq->turn);
996 sq->last_query = FALSE;
997 g_cond_signal (&sq->query_handled);
998 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1000 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
1001 result = gst_pad_pause_task (sq->srcpad);
1002 sq->sink_tainted = sq->src_tainted = TRUE;
1004 gst_single_queue_flush_queue (sq, full);
1006 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1007 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1008 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1009 sq->has_src_segment = FALSE;
1010 /* All pads start off not-linked for a smooth kick-off */
1011 sq->srcresult = GST_FLOW_OK;
1014 sq->max_size.visible = mq->max_size.visible;
1018 sq->last_oldid = G_MAXUINT32;
1019 sq->next_time = GST_CLOCK_STIME_NONE;
1020 sq->last_time = GST_CLOCK_STIME_NONE;
1021 sq->cached_sinktime = GST_CLOCK_STIME_NONE;
1022 gst_data_queue_set_flushing (sq->queue, FALSE);
1024 /* We will become active again on the next buffer/gap */
1027 /* Reset high time to be recomputed next */
1028 mq->high_time = GST_CLOCK_STIME_NONE;
1030 sq->flushing = FALSE;
1031 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1033 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
1035 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
1041 /* WITH LOCK TAKEN */
1043 get_percentage (GstSingleQueue * sq)
1045 GstDataQueueSize size;
1048 gst_data_queue_get_level (sq->queue, &size);
1050 GST_DEBUG_OBJECT (sq->mqueue,
1051 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1052 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
1053 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1055 /* get bytes and time percentages and take the max */
1056 if (sq->is_eos || sq->srcresult == GST_FLOW_NOT_LINKED || sq->is_sparse) {
1060 if (sq->max_size.time > 0) {
1061 tmp = (sq->cur_time * 100) / sq->max_size.time;
1062 percent = MAX (percent, tmp);
1064 if (sq->max_size.bytes > 0) {
1065 tmp = (size.bytes * 100) / sq->max_size.bytes;
1066 percent = MAX (percent, tmp);
1073 /* WITH LOCK TAKEN */
1075 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
1079 /* nothing to dowhen we are not in buffering mode */
1080 if (!mq->use_buffering)
1083 percent = get_percentage (sq);
1085 if (mq->buffering) {
1086 if (percent >= mq->high_percent) {
1087 mq->buffering = FALSE;
1089 /* make sure it increases */
1090 percent = MAX (mq->percent, percent);
1092 SET_PERCENT (mq, percent);
1095 gboolean is_buffering = TRUE;
1097 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
1098 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
1100 if (get_percentage (oq) >= mq->high_percent) {
1101 is_buffering = FALSE;
1107 if (is_buffering && percent < mq->low_percent) {
1108 mq->buffering = TRUE;
1109 SET_PERCENT (mq, percent);
1115 gst_multi_queue_post_buffering (GstMultiQueue * mq)
1117 GstMessage *msg = NULL;
1119 g_mutex_lock (&mq->buffering_post_lock);
1120 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1121 if (mq->percent_changed) {
1122 gint percent = mq->percent;
1124 mq->percent_changed = FALSE;
1126 percent = percent * 100 / mq->high_percent;
1131 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
1132 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
1134 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1137 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
1139 g_mutex_unlock (&mq->buffering_post_lock);
1143 recheck_buffering_status (GstMultiQueue * mq)
1145 if (!mq->use_buffering && mq->buffering) {
1146 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1147 mq->buffering = FALSE;
1148 GST_DEBUG_OBJECT (mq,
1149 "Buffering property disabled, but queue was still buffering; setting percentage to 100%%");
1150 SET_PERCENT (mq, 100);
1151 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1154 if (mq->use_buffering) {
1158 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1160 /* force fill level percentage to be recalculated */
1161 old_perc = mq->percent;
1166 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
1167 update_buffering (mq, q);
1168 gst_data_queue_limits_changed (q->queue);
1169 tmp = g_list_next (tmp);
1172 GST_DEBUG_OBJECT (mq, "Recalculated fill level: old: %d%% new: %d%%",
1173 old_perc, mq->percent);
1175 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1178 gst_multi_queue_post_buffering (mq);
1182 calculate_interleave (GstMultiQueue * mq)
1184 GstClockTimeDiff low, high;
1185 GstClockTime interleave;
1188 low = high = GST_CLOCK_STIME_NONE;
1189 interleave = mq->interleave;
1190 /* Go over all single queues and calculate lowest/highest value */
1191 for (tmp = mq->queues; tmp; tmp = tmp->next) {
1192 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1193 /* Ignore sparse streams for interleave calculation */
1196 /* If a stream is not active yet (hasn't received any buffers), set
1197 * a maximum interleave to allow it to receive more data */
1200 "queue %d is not active yet, forcing interleave to 5s", sq->id);
1201 mq->interleave = 5 * GST_SECOND;
1202 /* Update max-size time */
1203 mq->max_size.time = mq->interleave;
1204 SET_CHILD_PROPERTY (mq, time);
1207 if (GST_CLOCK_STIME_IS_VALID (sq->cached_sinktime)) {
1208 if (low == GST_CLOCK_STIME_NONE || sq->cached_sinktime < low)
1209 low = sq->cached_sinktime;
1210 if (high == GST_CLOCK_STIME_NONE || sq->cached_sinktime > high)
1211 high = sq->cached_sinktime;
1214 "queue %d , sinktime:%" GST_STIME_FORMAT " low:%" GST_STIME_FORMAT
1215 " high:%" GST_STIME_FORMAT, sq->id,
1216 GST_STIME_ARGS (sq->cached_sinktime), GST_STIME_ARGS (low),
1217 GST_STIME_ARGS (high));
1220 if (GST_CLOCK_STIME_IS_VALID (low) && GST_CLOCK_STIME_IS_VALID (high)) {
1221 interleave = high - low;
1222 /* Padding of interleave and minimum value */
1223 /* FIXME : Make the minimum time interleave a property */
1224 interleave = (150 * interleave / 100) + 250 * GST_MSECOND;
1226 /* Update the stored interleave if:
1227 * * No data has arrived yet (high == low)
1228 * * Or it went higher
1229 * * Or it went lower and we've gone past the previous interleave needed */
1230 if (high == low || interleave > mq->interleave ||
1231 ((mq->last_interleave_update + (2 * MIN (GST_SECOND,
1232 mq->interleave)) < low)
1233 && interleave < (mq->interleave * 3 / 4))) {
1234 /* Update the interleave */
1235 mq->interleave = interleave;
1236 mq->last_interleave_update = high;
1237 /* Update max-size time */
1238 mq->max_size.time = mq->interleave;
1239 SET_CHILD_PROPERTY (mq, time);
1244 GST_DEBUG_OBJECT (mq,
1245 "low:%" GST_STIME_FORMAT " high:%" GST_STIME_FORMAT " interleave:%"
1246 GST_TIME_FORMAT " mq->interleave:%" GST_TIME_FORMAT
1247 " last_interleave_update:%" GST_STIME_FORMAT, GST_STIME_ARGS (low),
1248 GST_STIME_ARGS (high), GST_TIME_ARGS (interleave),
1249 GST_TIME_ARGS (mq->interleave),
1250 GST_STIME_ARGS (mq->last_interleave_update));
1254 /* calculate the diff between running time on the sink and src of the queue.
1255 * This is the total amount of time in the queue.
1256 * WITH LOCK TAKEN */
1258 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
1260 GstClockTimeDiff sink_time, src_time;
1262 if (sq->sink_tainted) {
1263 sink_time = sq->sinktime = my_segment_to_running_time (&sq->sink_segment,
1264 sq->sink_segment.position);
1266 GST_DEBUG_OBJECT (mq,
1267 "queue %d sink_segment.position:%" GST_TIME_FORMAT ", sink_time:%"
1268 GST_STIME_FORMAT, sq->id, GST_TIME_ARGS (sq->sink_segment.position),
1269 GST_STIME_ARGS (sink_time));
1271 if (G_UNLIKELY (sq->last_time == GST_CLOCK_STIME_NONE)) {
1272 /* If the single queue still doesn't have a last_time set, this means
1273 * that nothing has been pushed out yet.
1274 * In order for the high_time computation to be as efficient as possible,
1275 * we set the last_time */
1276 sq->last_time = sink_time;
1278 if (G_UNLIKELY (sink_time != GST_CLOCK_STIME_NONE)) {
1279 /* if we have a time, we become untainted and use the time */
1280 sq->sink_tainted = FALSE;
1281 if (mq->use_interleave) {
1282 sq->cached_sinktime = sink_time;
1283 calculate_interleave (mq);
1287 sink_time = sq->sinktime;
1289 if (sq->src_tainted) {
1290 GstSegment *segment;
1293 if (sq->has_src_segment) {
1294 segment = &sq->src_segment;
1295 position = sq->src_segment.position;
1298 * If the src pad had no segment yet, use the sink segment
1299 * to avoid signalling overrun if the received sink segment has a
1300 * a position > max-size-time while the src pad time would be the default=0
1302 * This can happen when switching pads on chained/adaptive streams and the
1303 * new chain has a segment with a much larger position
1305 segment = &sq->sink_segment;
1306 position = sq->sink_segment.position;
1309 src_time = sq->srctime = my_segment_to_running_time (segment, position);
1310 /* if we have a time, we become untainted and use the time */
1311 if (G_UNLIKELY (src_time != GST_CLOCK_STIME_NONE)) {
1312 sq->src_tainted = FALSE;
1315 src_time = sq->srctime;
1317 GST_DEBUG_OBJECT (mq,
1318 "queue %d, sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT, sq->id,
1319 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
1321 /* This allows for streams with out of order timestamping - sometimes the
1322 * emerging timestamp is later than the arriving one(s) */
1323 if (G_LIKELY (GST_CLOCK_STIME_IS_VALID (sink_time) &&
1324 GST_CLOCK_STIME_IS_VALID (src_time) && sink_time > src_time))
1325 sq->cur_time = sink_time - src_time;
1329 /* updating the time level can change the buffering state */
1330 update_buffering (mq, sq);
1335 /* take a SEGMENT event and apply the values to segment, updating the time
1336 * level of queue. */
1338 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1339 GstSegment * segment)
1341 gst_event_copy_segment (event, segment);
1343 /* now configure the values, we use these to track timestamps on the
1345 if (segment->format != GST_FORMAT_TIME) {
1346 /* non-time format, pretent the current time segment is closed with a
1347 * 0 start and unknown stop time. */
1348 segment->format = GST_FORMAT_TIME;
1353 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1355 /* Make sure we have a valid initial segment position (and not garbage
1357 if (segment->rate > 0.0)
1358 segment->position = segment->start;
1360 segment->position = segment->stop;
1361 if (segment == &sq->sink_segment)
1362 sq->sink_tainted = TRUE;
1364 sq->has_src_segment = TRUE;
1365 sq->src_tainted = TRUE;
1368 GST_DEBUG_OBJECT (mq,
1369 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1371 /* segment can update the time level of the queue */
1372 update_time_level (mq, sq);
1374 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1375 gst_multi_queue_post_buffering (mq);
1378 /* take a buffer and update segment, updating the time level of the queue. */
1380 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1381 GstClockTime duration, GstSegment * segment)
1383 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1385 /* if no timestamp is set, assume it's continuous with the previous
1387 if (timestamp == GST_CLOCK_TIME_NONE)
1388 timestamp = segment->position;
1391 if (duration != GST_CLOCK_TIME_NONE)
1392 timestamp += duration;
1394 GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1395 sq->id, segment == &sq->sink_segment ? "sink" : "src",
1396 GST_TIME_ARGS (timestamp));
1398 segment->position = timestamp;
1400 if (segment == &sq->sink_segment)
1401 sq->sink_tainted = TRUE;
1403 sq->src_tainted = TRUE;
1405 /* calc diff with other end */
1406 update_time_level (mq, sq);
1407 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1408 gst_multi_queue_post_buffering (mq);
1412 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1413 GstSegment * segment)
1415 GstClockTime timestamp;
1416 GstClockTime duration;
1418 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1420 gst_event_parse_gap (event, ×tamp, &duration);
1422 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1424 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1425 timestamp += duration;
1428 segment->position = timestamp;
1430 if (segment == &sq->sink_segment)
1431 sq->sink_tainted = TRUE;
1433 sq->src_tainted = TRUE;
1435 /* calc diff with other end */
1436 update_time_level (mq, sq);
1439 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1440 gst_multi_queue_post_buffering (mq);
1443 static GstClockTimeDiff
1444 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1446 GstClockTimeDiff time = GST_CLOCK_STIME_NONE;
1448 if (GST_IS_BUFFER (object)) {
1449 GstBuffer *buf = GST_BUFFER_CAST (object);
1450 GstClockTime btime = GST_BUFFER_DTS_OR_PTS (buf);
1452 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1453 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1454 btime += GST_BUFFER_DURATION (buf);
1455 if (btime > segment->stop)
1456 btime = segment->stop;
1457 time = my_segment_to_running_time (segment, btime);
1459 } else if (GST_IS_BUFFER_LIST (object)) {
1460 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1464 n = gst_buffer_list_length (list);
1465 for (i = 0; i < n; i++) {
1467 buf = gst_buffer_list_get (list, i);
1468 btime = GST_BUFFER_DTS_OR_PTS (buf);
1469 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1470 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1471 btime += GST_BUFFER_DURATION (buf);
1472 if (btime > segment->stop)
1473 btime = segment->stop;
1474 time = my_segment_to_running_time (segment, btime);
1481 } else if (GST_IS_EVENT (object)) {
1482 GstEvent *event = GST_EVENT_CAST (object);
1484 /* For newsegment events return the running time of the start position */
1485 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1486 const GstSegment *new_segment;
1488 gst_event_parse_segment (event, &new_segment);
1489 if (new_segment->format == GST_FORMAT_TIME) {
1491 my_segment_to_running_time ((GstSegment *) new_segment,
1492 new_segment->start);
1501 static GstFlowReturn
1502 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1503 GstMiniObject * object, gboolean * allow_drop)
1505 GstFlowReturn result = sq->srcresult;
1507 if (GST_IS_BUFFER (object)) {
1509 GstClockTime timestamp, duration;
1511 buffer = GST_BUFFER_CAST (object);
1512 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1513 duration = GST_BUFFER_DURATION (buffer);
1515 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1517 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1518 gst_data_queue_limits_changed (sq->queue);
1520 if (G_UNLIKELY (*allow_drop)) {
1521 GST_DEBUG_OBJECT (mq,
1522 "SingleQueue %d : Dropping EOS buffer %p with ts %" GST_TIME_FORMAT,
1523 sq->id, buffer, GST_TIME_ARGS (timestamp));
1524 gst_buffer_unref (buffer);
1526 GST_DEBUG_OBJECT (mq,
1527 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1528 sq->id, buffer, GST_TIME_ARGS (timestamp));
1529 result = gst_pad_push (sq->srcpad, buffer);
1531 } else if (GST_IS_EVENT (object)) {
1534 event = GST_EVENT_CAST (object);
1536 switch (GST_EVENT_TYPE (event)) {
1538 result = GST_FLOW_EOS;
1539 if (G_UNLIKELY (*allow_drop))
1540 *allow_drop = FALSE;
1542 case GST_EVENT_SEGMENT:
1543 apply_segment (mq, sq, event, &sq->src_segment);
1544 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1545 gst_data_queue_limits_changed (sq->queue);
1546 if (G_UNLIKELY (*allow_drop)) {
1547 result = GST_FLOW_OK;
1548 *allow_drop = FALSE;
1552 apply_gap (mq, sq, event, &sq->src_segment);
1553 /* Applying the gap may have made the queue non-full again, unblock it if needed */
1554 gst_data_queue_limits_changed (sq->queue);
1560 if (G_UNLIKELY (*allow_drop)) {
1561 GST_DEBUG_OBJECT (mq,
1562 "SingleQueue %d : Dropping EOS event %p of type %s",
1563 sq->id, event, GST_EVENT_TYPE_NAME (event));
1564 gst_event_unref (event);
1566 GST_DEBUG_OBJECT (mq,
1567 "SingleQueue %d : Pushing event %p of type %s",
1568 sq->id, event, GST_EVENT_TYPE_NAME (event));
1570 gst_pad_push_event (sq->srcpad, event);
1572 } else if (GST_IS_QUERY (object)) {
1576 query = GST_QUERY_CAST (object);
1578 if (G_UNLIKELY (*allow_drop)) {
1579 GST_DEBUG_OBJECT (mq,
1580 "SingleQueue %d : Dropping EOS query %p", sq->id, query);
1581 gst_query_unref (query);
1584 res = gst_pad_peer_query (sq->srcpad, query);
1587 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1588 sq->last_query = res;
1589 sq->last_handled_query = query;
1590 g_cond_signal (&sq->query_handled);
1591 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1593 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1601 static GstMiniObject *
1602 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1607 item->object = NULL;
1613 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1615 if (!item->is_query && item->object)
1616 gst_mini_object_unref (item->object);
1617 g_slice_free (GstMultiQueueItem, item);
1620 /* takes ownership of passed mini object! */
1621 static GstMultiQueueItem *
1622 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1624 GstMultiQueueItem *item;
1626 item = g_slice_new (GstMultiQueueItem);
1627 item->object = object;
1628 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1629 item->posid = curid;
1630 item->is_query = GST_IS_QUERY (object);
1632 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1633 item->duration = GST_BUFFER_DURATION (object);
1634 if (item->duration == GST_CLOCK_TIME_NONE)
1636 item->visible = TRUE;
1640 static GstMultiQueueItem *
1641 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1643 GstMultiQueueItem *item;
1645 item = g_slice_new (GstMultiQueueItem);
1646 item->object = object;
1647 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1648 item->posid = curid;
1649 item->is_query = GST_IS_QUERY (object);
1653 item->visible = FALSE;
1657 /* Each main loop attempts to push buffers until the return value
1658 * is not-linked. not-linked pads are not allowed to push data beyond
1659 * any linked pads, so they don't 'rush ahead of the pack'.
1662 gst_multi_queue_loop (GstPad * pad)
1665 GstMultiQueueItem *item;
1666 GstDataQueueItem *sitem;
1668 GstMiniObject *object = NULL;
1670 GstFlowReturn result;
1671 GstClockTimeDiff next_time;
1673 gboolean do_update_buffering = FALSE;
1674 gboolean dropping = FALSE;
1676 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1680 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1685 /* Get something from the queue, blocking until that happens, or we get
1687 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1690 item = (GstMultiQueueItem *) sitem;
1691 newid = item->posid;
1693 /* steal the object and destroy the item */
1694 object = gst_multi_queue_item_steal_object (item);
1695 gst_multi_queue_item_destroy (item);
1697 is_buffer = GST_IS_BUFFER (object);
1699 /* Get running time of the item. Events will have GST_CLOCK_STIME_NONE */
1700 next_time = get_running_time (&sq->src_segment, object, FALSE);
1702 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1703 sq->id, newid, sq->last_oldid);
1705 /* If we're not-linked, we do some extra work because we might need to
1706 * wait before pushing. If we're linked but there's a gap in the IDs,
1707 * or it's the first loop, or we just passed the previous highid,
1708 * we might need to wake some sleeping pad up, so there's extra work
1710 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1711 if (sq->srcresult == GST_FLOW_NOT_LINKED
1712 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1713 || sq->last_oldid > mq->highid) {
1714 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1715 gst_flow_get_name (sq->srcresult));
1717 /* Check again if we're flushing after the lock is taken,
1718 * the flush flag might have been changed in the meantime */
1720 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1724 /* Update the nextid so other threads know when to wake us up */
1726 /* Take into account the extra cache time since we're unlinked */
1727 if (GST_CLOCK_STIME_IS_VALID (next_time))
1728 next_time += mq->unlinked_cache_time;
1729 sq->next_time = next_time;
1731 /* Update the oldid (the last ID we output) for highid tracking */
1732 if (sq->last_oldid != G_MAXUINT32)
1733 sq->oldid = sq->last_oldid;
1735 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1736 GstClockTimeDiff high_time;
1737 /* Go to sleep until it's time to push this buffer */
1739 /* Recompute the highid */
1740 compute_high_id (mq);
1741 /* Recompute the high time */
1742 high_time = compute_high_time (mq, sq->groupid);
1744 GST_DEBUG_OBJECT (mq,
1745 "groupid %d high_time %" GST_STIME_FORMAT " next_time %"
1746 GST_STIME_FORMAT, sq->groupid, GST_STIME_ARGS (high_time),
1747 GST_STIME_ARGS (next_time));
1749 while (((mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (next_time)
1750 && (high_time == GST_CLOCK_STIME_NONE
1751 || next_time > high_time))
1752 || (!mq->sync_by_running_time && newid > mq->highid))
1753 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1755 GST_DEBUG_OBJECT (mq,
1756 "queue %d sleeping for not-linked wakeup with "
1757 "newid %u, highid %u, next_time %" GST_STIME_FORMAT
1758 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1759 GST_STIME_ARGS (next_time), GST_STIME_ARGS (high_time));
1761 /* Wake up all non-linked pads before we sleep */
1762 wake_up_next_non_linked (mq);
1765 g_cond_wait (&sq->turn, &mq->qlock);
1769 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1773 /* Recompute the high time and ID */
1774 high_time = compute_high_time (mq, sq->groupid);
1775 compute_high_id (mq);
1777 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1778 "wakeup with newid %u, highid %u, next_time %" GST_STIME_FORMAT
1779 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1780 GST_STIME_ARGS (next_time), GST_STIME_ARGS (high_time));
1783 /* Re-compute the high_id in case someone else pushed */
1784 compute_high_id (mq);
1785 compute_high_time (mq, sq->groupid);
1787 compute_high_id (mq);
1788 compute_high_time (mq, sq->groupid);
1789 /* Wake up all non-linked pads */
1790 wake_up_next_non_linked (mq);
1792 /* We're done waiting, we can clear the nextid and nexttime */
1794 sq->next_time = GST_CLOCK_STIME_NONE;
1796 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1801 GST_LOG_OBJECT (mq, "sq:%d BEFORE PUSHING sq->srcresult: %s", sq->id,
1802 gst_flow_get_name (sq->srcresult));
1804 /* Update time stats */
1805 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1806 next_time = get_running_time (&sq->src_segment, object, TRUE);
1807 if (GST_CLOCK_STIME_IS_VALID (next_time)) {
1808 if (sq->last_time == GST_CLOCK_STIME_NONE || sq->last_time < next_time)
1809 sq->last_time = next_time;
1810 if (mq->high_time == GST_CLOCK_STIME_NONE || mq->high_time <= next_time) {
1811 /* Wake up all non-linked pads now that we advanced the high time */
1812 mq->high_time = next_time;
1813 wake_up_next_non_linked (mq);
1816 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1818 /* Try to push out the new object */
1819 result = gst_single_queue_push_one (mq, sq, object, &dropping);
1822 /* Check if we pushed something already and if this is
1823 * now a switch from an active to a non-active stream.
1825 * If it is, we reset all the waiting streams, let them
1826 * push another buffer to see if they're now active again.
1827 * This allows faster switching between streams and prevents
1828 * deadlocks if downstream does any waiting too.
1830 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1831 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1832 && result == GST_FLOW_NOT_LINKED) {
1835 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1838 compute_high_id (mq);
1839 do_update_buffering = TRUE;
1841 /* maybe no-one is waiting */
1842 if (mq->numwaiting > 0) {
1843 /* Else figure out which singlequeue(s) need waking up */
1844 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1845 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1847 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1848 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1849 sq2->pushed = FALSE;
1850 sq2->srcresult = GST_FLOW_OK;
1851 g_cond_signal (&sq2->turn);
1860 /* now hold on a bit;
1861 * can not simply throw this result to upstream, because
1862 * that might already be onto another segment, so we have to make
1863 * sure we are relaying the correct info wrt proper segment */
1864 if (result == GST_FLOW_EOS && !dropping &&
1865 sq->srcresult != GST_FLOW_NOT_LINKED) {
1866 GST_DEBUG_OBJECT (mq, "starting EOS drop on sq %d", sq->id);
1868 /* pretend we have not seen EOS yet for upstream's sake */
1869 result = sq->srcresult;
1870 } else if (dropping && gst_data_queue_is_empty (sq->queue)) {
1871 /* queue empty, so stop dropping
1872 * we can commit the result we have now,
1873 * which is either OK after a segment, or EOS */
1874 GST_DEBUG_OBJECT (mq, "committed EOS drop on sq %d", sq->id);
1876 result = GST_FLOW_EOS;
1878 sq->srcresult = result;
1879 sq->last_oldid = newid;
1881 if (do_update_buffering)
1882 update_buffering (mq, sq);
1884 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1885 gst_multi_queue_post_buffering (mq);
1887 GST_LOG_OBJECT (mq, "sq:%d AFTER PUSHING sq->srcresult: %s (is_eos:%d)",
1888 sq->id, gst_flow_get_name (sq->srcresult), GST_PAD_IS_EOS (sq->srcpad));
1890 /* Need to make sure wake up any sleeping pads when we exit */
1891 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1892 if (mq->numwaiting > 0 && (GST_PAD_IS_EOS (sq->srcpad)
1893 || sq->srcresult == GST_FLOW_EOS)) {
1894 compute_high_time (mq, sq->groupid);
1895 compute_high_id (mq);
1896 wake_up_next_non_linked (mq);
1898 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1903 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1904 && result != GST_FLOW_EOS)
1912 gst_mini_object_unref (object);
1914 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1915 sq->last_query = FALSE;
1916 g_cond_signal (&sq->query_handled);
1918 /* Post an error message if we got EOS while downstream
1919 * has returned an error flow return. After EOS there
1920 * will be no further buffer which could propagate the
1922 if (sq->is_eos && sq->srcresult < GST_FLOW_EOS) {
1923 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1924 GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
1926 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1929 /* upstream needs to see fatal result ASAP to shut things down,
1930 * but might be stuck in one of our other full queues;
1931 * so empty this one and trigger dynamic queue growth. At
1932 * this point the srcresult is not OK, NOT_LINKED
1933 * or EOS, i.e. a real failure */
1934 gst_single_queue_flush_queue (sq, FALSE);
1935 single_queue_underrun_cb (sq->queue, sq);
1936 gst_data_queue_set_flushing (sq->queue, TRUE);
1937 gst_pad_pause_task (sq->srcpad);
1938 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1939 "SingleQueue[%d] task paused, reason:%s",
1940 sq->id, gst_flow_get_name (sq->srcresult));
1946 * gst_multi_queue_chain:
1948 * This is similar to GstQueue's chain function, except:
1949 * _ we don't have leak behaviours,
1950 * _ we push with a unique id (curid)
1952 static GstFlowReturn
1953 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1957 GstMultiQueueItem *item;
1959 GstClockTime timestamp, duration;
1961 sq = gst_pad_get_element_private (pad);
1964 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1970 /* Get a unique incrementing id */
1971 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1973 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1974 duration = GST_BUFFER_DURATION (buffer);
1977 "SingleQueue %d : about to enqueue buffer %p with id %d (pts:%"
1978 GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT " dur:%" GST_TIME_FORMAT ")",
1979 sq->id, buffer, curid, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1980 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (duration));
1982 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1984 /* Update interleave before pushing data into queue */
1985 if (mq->use_interleave) {
1986 GstClockTime val = timestamp;
1987 GstClockTimeDiff dval;
1989 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1990 if (val == GST_CLOCK_TIME_NONE)
1991 val = sq->sink_segment.position;
1992 if (duration != GST_CLOCK_TIME_NONE)
1995 dval = my_segment_to_running_time (&sq->sink_segment, val);
1996 if (GST_CLOCK_STIME_IS_VALID (dval)) {
1997 sq->cached_sinktime = dval;
1998 GST_DEBUG_OBJECT (mq,
1999 "Queue %d cached sink time now %" G_GINT64_FORMAT " %"
2000 GST_STIME_FORMAT, sq->id, sq->cached_sinktime,
2001 GST_STIME_ARGS (sq->cached_sinktime));
2002 calculate_interleave (mq);
2004 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2007 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
2010 /* update time level, we must do this after pushing the data in the queue so
2011 * that we never end up filling the queue first. */
2012 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
2015 return sq->srcresult;
2020 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2021 sq->id, gst_flow_get_name (sq->srcresult));
2022 gst_multi_queue_item_destroy (item);
2027 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
2028 gst_buffer_unref (buffer);
2029 return GST_FLOW_EOS;
2034 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
2035 GstPadMode mode, gboolean active)
2041 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2042 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
2044 /* mq is NULL if the pad is activated/deactivated before being
2045 * added to the multiqueue */
2047 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2050 case GST_PAD_MODE_PUSH:
2052 /* All pads start off linked until they push one buffer */
2053 sq->srcresult = GST_FLOW_OK;
2055 gst_data_queue_set_flushing (sq->queue, FALSE);
2057 sq->srcresult = GST_FLOW_FLUSHING;
2058 sq->last_query = FALSE;
2059 g_cond_signal (&sq->query_handled);
2060 gst_data_queue_set_flushing (sq->queue, TRUE);
2062 /* Wait until streaming thread has finished */
2064 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2065 GST_PAD_STREAM_LOCK (pad);
2067 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2068 gst_data_queue_flush (sq->queue);
2070 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2071 GST_PAD_STREAM_UNLOCK (pad);
2073 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2083 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2084 gst_object_unref (mq);
2090 static GstFlowReturn
2091 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2096 GstMultiQueueItem *item;
2097 gboolean res = TRUE;
2098 GstFlowReturn flowret = GST_FLOW_OK;
2100 GstEvent *sref = NULL;
2102 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2103 mq = (GstMultiQueue *) parent;
2105 type = GST_EVENT_TYPE (event);
2108 case GST_EVENT_STREAM_START:
2110 if (mq->sync_by_running_time) {
2111 GstStreamFlags stream_flags;
2112 gst_event_parse_stream_flags (event, &stream_flags);
2113 if ((stream_flags & GST_STREAM_FLAG_SPARSE)) {
2114 GST_INFO_OBJECT (mq, "SingleQueue %d is a sparse stream", sq->id);
2115 sq->is_sparse = TRUE;
2119 sq->thread = g_thread_self ();
2121 /* Remove EOS flag */
2125 case GST_EVENT_FLUSH_START:
2126 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
2129 res = gst_pad_push_event (sq->srcpad, event);
2131 gst_single_queue_flush (mq, sq, TRUE, FALSE);
2134 case GST_EVENT_FLUSH_STOP:
2135 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
2138 res = gst_pad_push_event (sq->srcpad, event);
2140 gst_single_queue_flush (mq, sq, FALSE, FALSE);
2143 case GST_EVENT_SEGMENT:
2144 sref = gst_event_ref (event);
2147 /* take ref because the queue will take ownership and we need the event
2148 * afterwards to update the segment */
2149 sref = gst_event_ref (event);
2150 if (mq->use_interleave) {
2151 GstClockTime val, dur;
2153 gst_event_parse_gap (event, &val, &dur);
2154 if (GST_CLOCK_TIME_IS_VALID (val)) {
2155 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2156 if (GST_CLOCK_TIME_IS_VALID (dur))
2158 stime = my_segment_to_running_time (&sq->sink_segment, val);
2159 if (GST_CLOCK_STIME_IS_VALID (stime)) {
2160 sq->cached_sinktime = stime;
2161 calculate_interleave (mq);
2163 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2169 if (!(GST_EVENT_IS_SERIALIZED (event))) {
2170 res = gst_pad_push_event (sq->srcpad, event);
2176 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2180 /* Get an unique incrementing id. */
2181 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2183 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
2185 GST_DEBUG_OBJECT (mq,
2186 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
2187 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
2189 if (!gst_data_queue_push (sq->queue, (GstDataQueueItem *) item))
2192 /* mark EOS when we received one, we must do that after putting the
2193 * buffer in the queue because EOS marks the buffer as filled. */
2196 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2199 /* Post an error message if we got EOS while downstream
2200 * has returned an error flow return. After EOS there
2201 * will be no further buffer which could propagate the
2203 if (sq->srcresult < GST_FLOW_EOS) {
2204 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2205 GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
2207 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2210 /* EOS affects the buffering state */
2211 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2212 update_buffering (mq, sq);
2213 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2214 single_queue_overrun_cb (sq->queue, sq);
2215 gst_multi_queue_post_buffering (mq);
2217 case GST_EVENT_SEGMENT:
2218 apply_segment (mq, sq, sref, &sq->sink_segment);
2219 gst_event_unref (sref);
2220 /* a new segment allows us to accept more buffers if we got EOS
2221 * from downstream */
2222 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2223 if (sq->srcresult == GST_FLOW_EOS)
2224 sq->srcresult = GST_FLOW_OK;
2225 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2229 apply_gap (mq, sq, sref, &sq->sink_segment);
2230 gst_event_unref (sref);
2237 flowret = GST_FLOW_ERROR;
2238 GST_DEBUG_OBJECT (mq, "SingleQueue %d : returning %s", sq->id,
2239 gst_flow_get_name (flowret));
2244 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2245 sq->id, gst_flow_get_name (sq->srcresult));
2247 gst_event_unref (sref);
2248 gst_multi_queue_item_destroy (item);
2249 return sq->srcresult;
2253 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return GST_FLOW_EOS");
2254 gst_event_unref (event);
2255 return GST_FLOW_EOS;
2260 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
2266 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2267 mq = (GstMultiQueue *) parent;
2269 switch (GST_QUERY_TYPE (query)) {
2271 if (GST_QUERY_IS_SERIALIZED (query)) {
2273 GstMultiQueueItem *item;
2275 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2276 if (sq->srcresult != GST_FLOW_OK)
2279 /* serialized events go in the queue. We need to be certain that we
2280 * don't cause deadlocks waiting for the query return value. We check if
2281 * the queue is empty (nothing is blocking downstream and the query can
2282 * be pushed for sure) or we are not buffering. If we are buffering,
2283 * the pipeline waits to unblock downstream until our queue fills up
2284 * completely, which can not happen if we block on the query..
2285 * Therefore we only potentially block when we are not buffering. */
2286 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
2287 /* Get an unique incrementing id. */
2288 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2290 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
2292 GST_DEBUG_OBJECT (mq,
2293 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
2294 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
2295 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2296 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
2297 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2298 if (!res || sq->flushing)
2300 /* it might be that the query has been taken out of the queue
2301 * while we were unlocked. So, we need to check if the last
2302 * handled query is the same one than the one we just
2303 * pushed. If it is, we don't need to wait for the condition
2304 * variable, otherwise we wait for the condition variable to
2306 if (sq->last_handled_query != query)
2307 g_cond_wait (&sq->query_handled, &mq->qlock);
2308 res = sq->last_query;
2309 sq->last_handled_query = NULL;
2311 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
2312 "queue is not empty");
2315 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2317 /* default handling */
2318 res = gst_pad_query_default (pad, parent, query);
2326 GST_DEBUG_OBJECT (mq, "Flushing");
2327 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2333 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2334 GstPadMode mode, gboolean active)
2340 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2343 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2346 case GST_PAD_MODE_PUSH:
2348 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
2350 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
2351 /* make sure streaming finishes */
2352 result |= gst_pad_stop_task (pad);
2363 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2365 GstSingleQueue *sq = gst_pad_get_element_private (pad);
2366 GstMultiQueue *mq = sq->mqueue;
2369 switch (GST_EVENT_TYPE (event)) {
2370 case GST_EVENT_RECONFIGURE:
2371 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2372 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2373 sq->srcresult = GST_FLOW_OK;
2374 g_cond_signal (&sq->turn);
2376 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2378 ret = gst_pad_push_event (sq->sinkpad, event);
2381 ret = gst_pad_push_event (sq->sinkpad, event);
2389 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2393 /* FIXME, Handle position offset depending on queue size */
2394 switch (GST_QUERY_TYPE (query)) {
2396 /* default handling */
2397 res = gst_pad_query_default (pad, parent, query);
2404 * Next-non-linked functions
2407 /* WITH LOCK TAKEN */
2409 wake_up_next_non_linked (GstMultiQueue * mq)
2413 /* maybe no-one is waiting */
2414 if (mq->numwaiting < 1)
2417 if (mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (mq->high_time)) {
2418 /* Else figure out which singlequeue(s) need waking up */
2419 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2420 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2421 if (sq->srcresult == GST_FLOW_NOT_LINKED
2422 && GST_CLOCK_STIME_IS_VALID (sq->next_time)
2423 && sq->next_time <= mq->high_time) {
2424 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2425 g_cond_signal (&sq->turn);
2429 /* Else figure out which singlequeue(s) need waking up */
2430 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2431 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2432 if (sq->srcresult == GST_FLOW_NOT_LINKED &&
2433 sq->nextid != 0 && sq->nextid <= mq->highid) {
2434 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2435 g_cond_signal (&sq->turn);
2441 /* WITH LOCK TAKEN */
2443 compute_high_id (GstMultiQueue * mq)
2445 /* The high-id is either the highest id among the linked pads, or if all
2446 * pads are not-linked, it's the lowest not-linked pad */
2448 guint32 lowest = G_MAXUINT32;
2449 guint32 highid = G_MAXUINT32;
2451 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2452 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2454 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2455 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2457 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2458 /* No need to consider queues which are not waiting */
2459 if (sq->nextid == 0) {
2460 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2464 if (sq->nextid < lowest)
2465 lowest = sq->nextid;
2466 } else if (!GST_PAD_IS_EOS (sq->srcpad) && sq->srcresult != GST_FLOW_EOS) {
2467 /* If we don't have a global highid, or the global highid is lower than
2468 * this single queue's last outputted id, store the queue's one,
2469 * unless the singlequeue output is at EOS */
2470 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2475 if (highid == G_MAXUINT32 || lowest < highid)
2476 mq->highid = lowest;
2478 mq->highid = highid;
2480 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2484 /* WITH LOCK TAKEN */
2485 static GstClockTimeDiff
2486 compute_high_time (GstMultiQueue * mq, guint groupid)
2488 /* The high-time is either the highest last time among the linked
2489 * pads, or if all pads are not-linked, it's the lowest nex time of
2492 GstClockTimeDiff highest = GST_CLOCK_STIME_NONE;
2493 GstClockTimeDiff lowest = GST_CLOCK_STIME_NONE;
2494 GstClockTimeDiff group_high = GST_CLOCK_STIME_NONE;
2495 GstClockTimeDiff group_low = GST_CLOCK_STIME_NONE;
2496 /* Number of streams which belong to groupid */
2497 guint group_count = 0;
2499 if (!mq->sync_by_running_time)
2500 return GST_CLOCK_STIME_NONE;
2502 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2503 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2506 "inspecting sq:%d (group:%d) , next_time:%" GST_STIME_FORMAT
2507 ", last_time:%" GST_STIME_FORMAT ", srcresult:%s", sq->id, sq->groupid,
2508 GST_STIME_ARGS (sq->next_time), GST_STIME_ARGS (sq->last_time),
2509 gst_flow_get_name (sq->srcresult));
2511 if (sq->groupid == groupid)
2514 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2515 /* No need to consider queues which are not waiting */
2516 if (!GST_CLOCK_STIME_IS_VALID (sq->next_time)) {
2517 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2521 if (lowest == GST_CLOCK_STIME_NONE || sq->next_time < lowest)
2522 lowest = sq->next_time;
2523 if (sq->groupid == groupid && (group_low == GST_CLOCK_STIME_NONE
2524 || sq->next_time < group_low))
2525 group_low = sq->next_time;
2526 } else if (!GST_PAD_IS_EOS (sq->srcpad) && sq->srcresult != GST_FLOW_EOS) {
2527 /* If we don't have a global high time, or the global high time
2528 * is lower than this single queue's last outputted time, store
2529 * the queue's one, unless the singlequeue output is at EOS. */
2530 if (highest == GST_CLOCK_STIME_NONE
2531 || (sq->last_time != GST_CLOCK_STIME_NONE && sq->last_time > highest))
2532 highest = sq->last_time;
2533 if (sq->groupid == groupid && (group_high == GST_CLOCK_STIME_NONE
2534 || (sq->last_time != GST_CLOCK_STIME_NONE
2535 && sq->last_time > group_high)))
2536 group_high = sq->last_time;
2539 "highest now %" GST_STIME_FORMAT " lowest %" GST_STIME_FORMAT,
2540 GST_STIME_ARGS (highest), GST_STIME_ARGS (lowest));
2541 if (sq->groupid == groupid)
2543 "grouphigh %" GST_STIME_FORMAT " grouplow %" GST_STIME_FORMAT,
2544 GST_STIME_ARGS (group_high), GST_STIME_ARGS (group_low));
2547 if (highest == GST_CLOCK_STIME_NONE)
2548 mq->high_time = lowest;
2550 mq->high_time = highest;
2552 GST_LOG_OBJECT (mq, "group count %d for groupid %u", group_count, groupid);
2554 "High time is now : %" GST_STIME_FORMAT ", lowest non-linked %"
2555 GST_STIME_FORMAT, GST_STIME_ARGS (mq->high_time),
2556 GST_STIME_ARGS (lowest));
2558 /* If there's only one stream of a given type, use the global high */
2559 if (group_count < 2)
2560 return mq->high_time;
2562 if (group_high == GST_CLOCK_STIME_NONE)
2567 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2568 ((q)->max_size.format) <= (value))
2571 * GstSingleQueue functions
2574 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2576 GstMultiQueue *mq = sq->mqueue;
2578 GstDataQueueSize size;
2579 gboolean filled = TRUE;
2580 gboolean empty_found = FALSE;
2582 gst_data_queue_get_level (sq->queue, &size);
2585 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2586 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2587 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2590 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2592 /* check if we reached the hard time/bytes limits;
2593 time limit is only taken into account for non-sparse streams */
2594 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
2595 (!sq->is_sparse && IS_FILLED (sq, time, sq->cur_time))) {
2599 /* Search for empty queues */
2600 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2601 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2606 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2607 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2611 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2612 if (gst_data_queue_is_empty (oq->queue) && !oq->is_sparse) {
2613 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2619 /* if hard limits are not reached then we allow one more buffer in the full
2620 * queue, but only if any of the other singelqueues are empty */
2622 if (IS_FILLED (sq, visible, size.visible)) {
2623 sq->max_size.visible = size.visible + 1;
2624 GST_DEBUG_OBJECT (mq,
2625 "Bumping single queue %d max visible to %d",
2626 sq->id, sq->max_size.visible);
2632 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2634 /* Overrun is always forwarded, since this is blocking the upstream element */
2636 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2637 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2642 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2644 gboolean empty = TRUE;
2645 GstMultiQueue *mq = sq->mqueue;
2648 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2649 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2653 "Single Queue %d is empty, Checking other single queues", sq->id);
2656 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2657 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2658 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2660 if (gst_data_queue_is_full (oq->queue)) {
2661 GstDataQueueSize size;
2663 gst_data_queue_get_level (oq->queue, &size);
2664 if (IS_FILLED (oq, visible, size.visible)) {
2665 oq->max_size.visible = size.visible + 1;
2666 GST_DEBUG_OBJECT (mq,
2667 "queue %d is filled, bumping its max visible to %d", oq->id,
2668 oq->max_size.visible);
2669 gst_data_queue_limits_changed (oq->queue);
2672 if (!gst_data_queue_is_empty (oq->queue) || oq->is_sparse)
2675 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2678 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2679 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2684 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2685 guint64 time, GstSingleQueue * sq)
2688 GstMultiQueue *mq = sq->mqueue;
2690 GST_DEBUG_OBJECT (mq,
2691 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2692 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2693 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2695 /* we are always filled on EOS */
2699 /* we never go past the max visible items unless we are in buffering mode */
2700 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2703 /* check time or bytes */
2704 res = IS_FILLED (sq, bytes, bytes);
2705 /* We only care about limits in time if we're not a sparse stream or
2706 * we're not syncing by running time */
2707 if (!sq->is_sparse || !mq->sync_by_running_time) {
2708 /* If unlinked, take into account the extra unlinked cache time */
2709 if (mq->sync_by_running_time && sq->srcresult == GST_FLOW_NOT_LINKED) {
2710 if (sq->cur_time > mq->unlinked_cache_time)
2711 res |= IS_FILLED (sq, time, sq->cur_time - mq->unlinked_cache_time);
2715 res |= IS_FILLED (sq, time, sq->cur_time);
2722 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2724 GstDataQueueItem *sitem;
2725 GstMultiQueueItem *mitem;
2726 gboolean was_flushing = FALSE;
2728 while (!gst_data_queue_is_empty (sq->queue)) {
2729 GstMiniObject *data;
2731 /* FIXME: If this fails here although the queue is not empty,
2732 * we're flushing... but we want to rescue all sticky
2733 * events nonetheless.
2735 if (!gst_data_queue_pop (sq->queue, &sitem)) {
2736 was_flushing = TRUE;
2737 gst_data_queue_set_flushing (sq->queue, FALSE);
2741 mitem = (GstMultiQueueItem *) sitem;
2743 data = sitem->object;
2745 if (!full && !mitem->is_query && GST_IS_EVENT (data)
2746 && GST_EVENT_IS_STICKY (data)
2747 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2748 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2749 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2752 sitem->destroy (sitem);
2755 gst_data_queue_flush (sq->queue);
2757 gst_data_queue_set_flushing (sq->queue, TRUE);
2759 GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2760 update_buffering (sq->mqueue, sq);
2761 GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2762 gst_multi_queue_post_buffering (sq->mqueue);
2766 gst_single_queue_free (GstSingleQueue * sq)
2769 gst_data_queue_flush (sq->queue);
2770 g_object_unref (sq->queue);
2771 g_cond_clear (&sq->turn);
2772 g_cond_clear (&sq->query_handled);
2776 static GstSingleQueue *
2777 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2780 GstMultiQueuePad *mqpad;
2781 GstPadTemplate *templ;
2784 guint temp_id = (id == -1) ? 0 : id;
2786 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2788 /* Find an unused queue ID, if possible the passed one */
2789 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2790 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2791 /* This works because the IDs are sorted in ascending order */
2792 if (sq2->id == temp_id) {
2793 /* If this ID was requested by the caller return NULL,
2794 * otherwise just get us the next one */
2796 temp_id = sq2->id + 1;
2798 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2801 } else if (sq2->id > temp_id) {
2806 sq = g_new0 (GstSingleQueue, 1);
2809 sq->groupid = DEFAULT_PAD_GROUP_ID;
2811 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2812 mqueue->queues_cookie++;
2814 /* copy over max_size and extra_size so we don't need to take the lock
2815 * any longer when checking if the queue is full. */
2816 sq->max_size.visible = mqueue->max_size.visible;
2817 sq->max_size.bytes = mqueue->max_size.bytes;
2818 sq->max_size.time = mqueue->max_size.time;
2820 sq->extra_size.visible = mqueue->extra_size.visible;
2821 sq->extra_size.bytes = mqueue->extra_size.bytes;
2822 sq->extra_size.time = mqueue->extra_size.time;
2824 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2826 sq->mqueue = mqueue;
2827 sq->srcresult = GST_FLOW_FLUSHING;
2829 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2830 single_queue_check_full,
2831 (GstDataQueueFullCallback) single_queue_overrun_cb,
2832 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2834 sq->is_sparse = FALSE;
2835 sq->flushing = FALSE;
2837 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2838 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2842 sq->next_time = GST_CLOCK_STIME_NONE;
2843 sq->last_time = GST_CLOCK_STIME_NONE;
2844 g_cond_init (&sq->turn);
2845 g_cond_init (&sq->query_handled);
2847 sq->sinktime = GST_CLOCK_STIME_NONE;
2848 sq->srctime = GST_CLOCK_STIME_NONE;
2849 sq->sink_tainted = TRUE;
2850 sq->src_tainted = TRUE;
2852 name = g_strdup_printf ("sink_%u", sq->id);
2853 templ = gst_static_pad_template_get (&sinktemplate);
2854 sq->sinkpad = g_object_new (GST_TYPE_MULTIQUEUE_PAD, "name", name,
2855 "direction", templ->direction, "template", templ, NULL);
2856 gst_object_unref (templ);
2859 mqpad = (GstMultiQueuePad *) sq->sinkpad;
2862 gst_pad_set_chain_function (sq->sinkpad,
2863 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2864 gst_pad_set_activatemode_function (sq->sinkpad,
2865 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2866 gst_pad_set_event_full_function (sq->sinkpad,
2867 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2868 gst_pad_set_query_function (sq->sinkpad,
2869 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2870 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2871 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2872 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2874 name = g_strdup_printf ("src_%u", sq->id);
2875 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2878 gst_pad_set_activatemode_function (sq->srcpad,
2879 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2880 gst_pad_set_event_function (sq->srcpad,
2881 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2882 gst_pad_set_query_function (sq->srcpad,
2883 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2884 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2885 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2886 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2888 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2889 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2891 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2893 /* only activate the pads when we are not in the NULL state
2894 * and add the pad under the state_lock to prevend state changes
2895 * between activating and adding */
2896 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2897 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2898 gst_pad_set_active (sq->srcpad, TRUE);
2899 gst_pad_set_active (sq->sinkpad, TRUE);
2901 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2902 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2903 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2905 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",