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_ERROR (mq, STREAM, FAILED,
1925 ("Internal data stream error."),
1926 ("streaming stopped, reason %s", gst_flow_get_name (sq->srcresult)));
1928 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1931 /* upstream needs to see fatal result ASAP to shut things down,
1932 * but might be stuck in one of our other full queues;
1933 * so empty this one and trigger dynamic queue growth. At
1934 * this point the srcresult is not OK, NOT_LINKED
1935 * or EOS, i.e. a real failure */
1936 gst_single_queue_flush_queue (sq, FALSE);
1937 single_queue_underrun_cb (sq->queue, sq);
1938 gst_data_queue_set_flushing (sq->queue, TRUE);
1939 gst_pad_pause_task (sq->srcpad);
1940 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1941 "SingleQueue[%d] task paused, reason:%s",
1942 sq->id, gst_flow_get_name (sq->srcresult));
1948 * gst_multi_queue_chain:
1950 * This is similar to GstQueue's chain function, except:
1951 * _ we don't have leak behaviours,
1952 * _ we push with a unique id (curid)
1954 static GstFlowReturn
1955 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1959 GstMultiQueueItem *item;
1961 GstClockTime timestamp, duration;
1963 sq = gst_pad_get_element_private (pad);
1966 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1972 /* Get a unique incrementing id */
1973 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1975 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1976 duration = GST_BUFFER_DURATION (buffer);
1979 "SingleQueue %d : about to enqueue buffer %p with id %d (pts:%"
1980 GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT " dur:%" GST_TIME_FORMAT ")",
1981 sq->id, buffer, curid, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1982 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (duration));
1984 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1986 /* Update interleave before pushing data into queue */
1987 if (mq->use_interleave) {
1988 GstClockTime val = timestamp;
1989 GstClockTimeDiff dval;
1991 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1992 if (val == GST_CLOCK_TIME_NONE)
1993 val = sq->sink_segment.position;
1994 if (duration != GST_CLOCK_TIME_NONE)
1997 dval = my_segment_to_running_time (&sq->sink_segment, val);
1998 if (GST_CLOCK_STIME_IS_VALID (dval)) {
1999 sq->cached_sinktime = dval;
2000 GST_DEBUG_OBJECT (mq,
2001 "Queue %d cached sink time now %" G_GINT64_FORMAT " %"
2002 GST_STIME_FORMAT, sq->id, sq->cached_sinktime,
2003 GST_STIME_ARGS (sq->cached_sinktime));
2004 calculate_interleave (mq);
2006 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2009 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
2012 /* update time level, we must do this after pushing the data in the queue so
2013 * that we never end up filling the queue first. */
2014 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
2017 return sq->srcresult;
2022 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2023 sq->id, gst_flow_get_name (sq->srcresult));
2024 gst_multi_queue_item_destroy (item);
2029 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
2030 gst_buffer_unref (buffer);
2031 return GST_FLOW_EOS;
2036 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
2037 GstPadMode mode, gboolean active)
2043 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2044 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
2046 /* mq is NULL if the pad is activated/deactivated before being
2047 * added to the multiqueue */
2049 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2052 case GST_PAD_MODE_PUSH:
2054 /* All pads start off linked until they push one buffer */
2055 sq->srcresult = GST_FLOW_OK;
2057 gst_data_queue_set_flushing (sq->queue, FALSE);
2059 sq->srcresult = GST_FLOW_FLUSHING;
2060 sq->last_query = FALSE;
2061 g_cond_signal (&sq->query_handled);
2062 gst_data_queue_set_flushing (sq->queue, TRUE);
2064 /* Wait until streaming thread has finished */
2066 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2067 GST_PAD_STREAM_LOCK (pad);
2069 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2070 gst_data_queue_flush (sq->queue);
2072 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2073 GST_PAD_STREAM_UNLOCK (pad);
2075 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2085 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2086 gst_object_unref (mq);
2092 static GstFlowReturn
2093 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2098 GstMultiQueueItem *item;
2099 gboolean res = TRUE;
2100 GstFlowReturn flowret = GST_FLOW_OK;
2102 GstEvent *sref = NULL;
2104 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2105 mq = (GstMultiQueue *) parent;
2107 type = GST_EVENT_TYPE (event);
2110 case GST_EVENT_STREAM_START:
2112 if (mq->sync_by_running_time) {
2113 GstStreamFlags stream_flags;
2114 gst_event_parse_stream_flags (event, &stream_flags);
2115 if ((stream_flags & GST_STREAM_FLAG_SPARSE)) {
2116 GST_INFO_OBJECT (mq, "SingleQueue %d is a sparse stream", sq->id);
2117 sq->is_sparse = TRUE;
2121 sq->thread = g_thread_self ();
2123 /* Remove EOS flag */
2127 case GST_EVENT_FLUSH_START:
2128 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
2131 res = gst_pad_push_event (sq->srcpad, event);
2133 gst_single_queue_flush (mq, sq, TRUE, FALSE);
2136 case GST_EVENT_FLUSH_STOP:
2137 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
2140 res = gst_pad_push_event (sq->srcpad, event);
2142 gst_single_queue_flush (mq, sq, FALSE, FALSE);
2145 case GST_EVENT_SEGMENT:
2146 sref = gst_event_ref (event);
2149 /* take ref because the queue will take ownership and we need the event
2150 * afterwards to update the segment */
2151 sref = gst_event_ref (event);
2152 if (mq->use_interleave) {
2153 GstClockTime val, dur;
2155 gst_event_parse_gap (event, &val, &dur);
2156 if (GST_CLOCK_TIME_IS_VALID (val)) {
2157 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2158 if (GST_CLOCK_TIME_IS_VALID (dur))
2160 stime = my_segment_to_running_time (&sq->sink_segment, val);
2161 if (GST_CLOCK_STIME_IS_VALID (stime)) {
2162 sq->cached_sinktime = stime;
2163 calculate_interleave (mq);
2165 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2171 if (!(GST_EVENT_IS_SERIALIZED (event))) {
2172 res = gst_pad_push_event (sq->srcpad, event);
2178 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2182 /* Get an unique incrementing id. */
2183 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2185 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
2187 GST_DEBUG_OBJECT (mq,
2188 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
2189 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
2191 if (!gst_data_queue_push (sq->queue, (GstDataQueueItem *) item))
2194 /* mark EOS when we received one, we must do that after putting the
2195 * buffer in the queue because EOS marks the buffer as filled. */
2198 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2201 /* Post an error message if we got EOS while downstream
2202 * has returned an error flow return. After EOS there
2203 * will be no further buffer which could propagate the
2205 if (sq->srcresult < GST_FLOW_EOS) {
2206 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2207 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
2208 ("Internal data stream error."),
2209 ("streaming stopped, reason %s",
2210 gst_flow_get_name (sq->srcresult)));
2212 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2215 /* EOS affects the buffering state */
2216 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2217 update_buffering (mq, sq);
2218 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2219 single_queue_overrun_cb (sq->queue, sq);
2220 gst_multi_queue_post_buffering (mq);
2222 case GST_EVENT_SEGMENT:
2223 apply_segment (mq, sq, sref, &sq->sink_segment);
2224 gst_event_unref (sref);
2225 /* a new segment allows us to accept more buffers if we got EOS
2226 * from downstream */
2227 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2228 if (sq->srcresult == GST_FLOW_EOS)
2229 sq->srcresult = GST_FLOW_OK;
2230 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2234 apply_gap (mq, sq, sref, &sq->sink_segment);
2235 gst_event_unref (sref);
2242 flowret = GST_FLOW_ERROR;
2243 GST_DEBUG_OBJECT (mq, "SingleQueue %d : returning %s", sq->id,
2244 gst_flow_get_name (flowret));
2249 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2250 sq->id, gst_flow_get_name (sq->srcresult));
2252 gst_event_unref (sref);
2253 gst_multi_queue_item_destroy (item);
2254 return sq->srcresult;
2258 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return GST_FLOW_EOS");
2259 gst_event_unref (event);
2260 return GST_FLOW_EOS;
2265 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
2271 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2272 mq = (GstMultiQueue *) parent;
2274 switch (GST_QUERY_TYPE (query)) {
2276 if (GST_QUERY_IS_SERIALIZED (query)) {
2278 GstMultiQueueItem *item;
2280 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2281 if (sq->srcresult != GST_FLOW_OK)
2284 /* serialized events go in the queue. We need to be certain that we
2285 * don't cause deadlocks waiting for the query return value. We check if
2286 * the queue is empty (nothing is blocking downstream and the query can
2287 * be pushed for sure) or we are not buffering. If we are buffering,
2288 * the pipeline waits to unblock downstream until our queue fills up
2289 * completely, which can not happen if we block on the query..
2290 * Therefore we only potentially block when we are not buffering. */
2291 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
2292 /* Get an unique incrementing id. */
2293 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2295 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
2297 GST_DEBUG_OBJECT (mq,
2298 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
2299 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
2300 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2301 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
2302 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2303 if (!res || sq->flushing)
2305 /* it might be that the query has been taken out of the queue
2306 * while we were unlocked. So, we need to check if the last
2307 * handled query is the same one than the one we just
2308 * pushed. If it is, we don't need to wait for the condition
2309 * variable, otherwise we wait for the condition variable to
2311 if (sq->last_handled_query != query)
2312 g_cond_wait (&sq->query_handled, &mq->qlock);
2313 res = sq->last_query;
2314 sq->last_handled_query = NULL;
2316 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
2317 "queue is not empty");
2320 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2322 /* default handling */
2323 res = gst_pad_query_default (pad, parent, query);
2331 GST_DEBUG_OBJECT (mq, "Flushing");
2332 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2338 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2339 GstPadMode mode, gboolean active)
2345 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2348 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2351 case GST_PAD_MODE_PUSH:
2353 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
2355 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
2356 /* make sure streaming finishes */
2357 result |= gst_pad_stop_task (pad);
2368 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2370 GstSingleQueue *sq = gst_pad_get_element_private (pad);
2371 GstMultiQueue *mq = sq->mqueue;
2374 switch (GST_EVENT_TYPE (event)) {
2375 case GST_EVENT_RECONFIGURE:
2376 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2377 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2378 sq->srcresult = GST_FLOW_OK;
2379 g_cond_signal (&sq->turn);
2381 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2383 ret = gst_pad_push_event (sq->sinkpad, event);
2386 ret = gst_pad_push_event (sq->sinkpad, event);
2394 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2398 /* FIXME, Handle position offset depending on queue size */
2399 switch (GST_QUERY_TYPE (query)) {
2401 /* default handling */
2402 res = gst_pad_query_default (pad, parent, query);
2409 * Next-non-linked functions
2412 /* WITH LOCK TAKEN */
2414 wake_up_next_non_linked (GstMultiQueue * mq)
2418 /* maybe no-one is waiting */
2419 if (mq->numwaiting < 1)
2422 if (mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (mq->high_time)) {
2423 /* Else figure out which singlequeue(s) need waking up */
2424 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2425 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2426 if (sq->srcresult == GST_FLOW_NOT_LINKED
2427 && GST_CLOCK_STIME_IS_VALID (sq->next_time)
2428 && sq->next_time <= mq->high_time) {
2429 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2430 g_cond_signal (&sq->turn);
2434 /* Else figure out which singlequeue(s) need waking up */
2435 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2436 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2437 if (sq->srcresult == GST_FLOW_NOT_LINKED &&
2438 sq->nextid != 0 && sq->nextid <= mq->highid) {
2439 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2440 g_cond_signal (&sq->turn);
2446 /* WITH LOCK TAKEN */
2448 compute_high_id (GstMultiQueue * mq)
2450 /* The high-id is either the highest id among the linked pads, or if all
2451 * pads are not-linked, it's the lowest not-linked pad */
2453 guint32 lowest = G_MAXUINT32;
2454 guint32 highid = G_MAXUINT32;
2456 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2457 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2459 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2460 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2462 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2463 /* No need to consider queues which are not waiting */
2464 if (sq->nextid == 0) {
2465 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2469 if (sq->nextid < lowest)
2470 lowest = sq->nextid;
2471 } else if (!GST_PAD_IS_EOS (sq->srcpad) && sq->srcresult != GST_FLOW_EOS) {
2472 /* If we don't have a global highid, or the global highid is lower than
2473 * this single queue's last outputted id, store the queue's one,
2474 * unless the singlequeue output is at EOS */
2475 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2480 if (highid == G_MAXUINT32 || lowest < highid)
2481 mq->highid = lowest;
2483 mq->highid = highid;
2485 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2489 /* WITH LOCK TAKEN */
2490 static GstClockTimeDiff
2491 compute_high_time (GstMultiQueue * mq, guint groupid)
2493 /* The high-time is either the highest last time among the linked
2494 * pads, or if all pads are not-linked, it's the lowest nex time of
2497 GstClockTimeDiff highest = GST_CLOCK_STIME_NONE;
2498 GstClockTimeDiff lowest = GST_CLOCK_STIME_NONE;
2499 GstClockTimeDiff group_high = GST_CLOCK_STIME_NONE;
2500 GstClockTimeDiff group_low = GST_CLOCK_STIME_NONE;
2501 /* Number of streams which belong to groupid */
2502 guint group_count = 0;
2504 if (!mq->sync_by_running_time)
2505 return GST_CLOCK_STIME_NONE;
2507 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2508 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2511 "inspecting sq:%d (group:%d) , next_time:%" GST_STIME_FORMAT
2512 ", last_time:%" GST_STIME_FORMAT ", srcresult:%s", sq->id, sq->groupid,
2513 GST_STIME_ARGS (sq->next_time), GST_STIME_ARGS (sq->last_time),
2514 gst_flow_get_name (sq->srcresult));
2516 if (sq->groupid == groupid)
2519 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2520 /* No need to consider queues which are not waiting */
2521 if (!GST_CLOCK_STIME_IS_VALID (sq->next_time)) {
2522 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2526 if (lowest == GST_CLOCK_STIME_NONE || sq->next_time < lowest)
2527 lowest = sq->next_time;
2528 if (sq->groupid == groupid && (group_low == GST_CLOCK_STIME_NONE
2529 || sq->next_time < group_low))
2530 group_low = sq->next_time;
2531 } else if (!GST_PAD_IS_EOS (sq->srcpad) && sq->srcresult != GST_FLOW_EOS) {
2532 /* If we don't have a global high time, or the global high time
2533 * is lower than this single queue's last outputted time, store
2534 * the queue's one, unless the singlequeue output is at EOS. */
2535 if (highest == GST_CLOCK_STIME_NONE
2536 || (sq->last_time != GST_CLOCK_STIME_NONE && sq->last_time > highest))
2537 highest = sq->last_time;
2538 if (sq->groupid == groupid && (group_high == GST_CLOCK_STIME_NONE
2539 || (sq->last_time != GST_CLOCK_STIME_NONE
2540 && sq->last_time > group_high)))
2541 group_high = sq->last_time;
2544 "highest now %" GST_STIME_FORMAT " lowest %" GST_STIME_FORMAT,
2545 GST_STIME_ARGS (highest), GST_STIME_ARGS (lowest));
2546 if (sq->groupid == groupid)
2548 "grouphigh %" GST_STIME_FORMAT " grouplow %" GST_STIME_FORMAT,
2549 GST_STIME_ARGS (group_high), GST_STIME_ARGS (group_low));
2552 if (highest == GST_CLOCK_STIME_NONE)
2553 mq->high_time = lowest;
2555 mq->high_time = highest;
2557 GST_LOG_OBJECT (mq, "group count %d for groupid %u", group_count, groupid);
2559 "High time is now : %" GST_STIME_FORMAT ", lowest non-linked %"
2560 GST_STIME_FORMAT, GST_STIME_ARGS (mq->high_time),
2561 GST_STIME_ARGS (lowest));
2563 /* If there's only one stream of a given type, use the global high */
2564 if (group_count < 2)
2565 return mq->high_time;
2567 if (group_high == GST_CLOCK_STIME_NONE)
2572 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2573 ((q)->max_size.format) <= (value))
2576 * GstSingleQueue functions
2579 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2581 GstMultiQueue *mq = sq->mqueue;
2583 GstDataQueueSize size;
2584 gboolean filled = TRUE;
2585 gboolean empty_found = FALSE;
2587 gst_data_queue_get_level (sq->queue, &size);
2590 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2591 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2592 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2595 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2597 /* check if we reached the hard time/bytes limits;
2598 time limit is only taken into account for non-sparse streams */
2599 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
2600 (!sq->is_sparse && IS_FILLED (sq, time, sq->cur_time))) {
2604 /* Search for empty queues */
2605 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2606 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2611 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2612 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2616 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2617 if (gst_data_queue_is_empty (oq->queue) && !oq->is_sparse) {
2618 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2624 /* if hard limits are not reached then we allow one more buffer in the full
2625 * queue, but only if any of the other singelqueues are empty */
2627 if (IS_FILLED (sq, visible, size.visible)) {
2628 sq->max_size.visible = size.visible + 1;
2629 GST_DEBUG_OBJECT (mq,
2630 "Bumping single queue %d max visible to %d",
2631 sq->id, sq->max_size.visible);
2637 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2639 /* Overrun is always forwarded, since this is blocking the upstream element */
2641 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2642 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2647 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2649 gboolean empty = TRUE;
2650 GstMultiQueue *mq = sq->mqueue;
2653 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2654 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2658 "Single Queue %d is empty, Checking other single queues", sq->id);
2661 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2662 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2663 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2665 if (gst_data_queue_is_full (oq->queue)) {
2666 GstDataQueueSize size;
2668 gst_data_queue_get_level (oq->queue, &size);
2669 if (IS_FILLED (oq, visible, size.visible)) {
2670 oq->max_size.visible = size.visible + 1;
2671 GST_DEBUG_OBJECT (mq,
2672 "queue %d is filled, bumping its max visible to %d", oq->id,
2673 oq->max_size.visible);
2674 gst_data_queue_limits_changed (oq->queue);
2677 if (!gst_data_queue_is_empty (oq->queue) || oq->is_sparse)
2680 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2683 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2684 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2689 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2690 guint64 time, GstSingleQueue * sq)
2693 GstMultiQueue *mq = sq->mqueue;
2695 GST_DEBUG_OBJECT (mq,
2696 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2697 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2698 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2700 /* we are always filled on EOS */
2704 /* we never go past the max visible items unless we are in buffering mode */
2705 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2708 /* check time or bytes */
2709 res = IS_FILLED (sq, bytes, bytes);
2710 /* We only care about limits in time if we're not a sparse stream or
2711 * we're not syncing by running time */
2712 if (!sq->is_sparse || !mq->sync_by_running_time) {
2713 /* If unlinked, take into account the extra unlinked cache time */
2714 if (mq->sync_by_running_time && sq->srcresult == GST_FLOW_NOT_LINKED) {
2715 if (sq->cur_time > mq->unlinked_cache_time)
2716 res |= IS_FILLED (sq, time, sq->cur_time - mq->unlinked_cache_time);
2720 res |= IS_FILLED (sq, time, sq->cur_time);
2727 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2729 GstDataQueueItem *sitem;
2730 GstMultiQueueItem *mitem;
2731 gboolean was_flushing = FALSE;
2733 while (!gst_data_queue_is_empty (sq->queue)) {
2734 GstMiniObject *data;
2736 /* FIXME: If this fails here although the queue is not empty,
2737 * we're flushing... but we want to rescue all sticky
2738 * events nonetheless.
2740 if (!gst_data_queue_pop (sq->queue, &sitem)) {
2741 was_flushing = TRUE;
2742 gst_data_queue_set_flushing (sq->queue, FALSE);
2746 mitem = (GstMultiQueueItem *) sitem;
2748 data = sitem->object;
2750 if (!full && !mitem->is_query && GST_IS_EVENT (data)
2751 && GST_EVENT_IS_STICKY (data)
2752 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2753 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2754 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2757 sitem->destroy (sitem);
2760 gst_data_queue_flush (sq->queue);
2762 gst_data_queue_set_flushing (sq->queue, TRUE);
2764 GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2765 update_buffering (sq->mqueue, sq);
2766 GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2767 gst_multi_queue_post_buffering (sq->mqueue);
2771 gst_single_queue_free (GstSingleQueue * sq)
2774 gst_data_queue_flush (sq->queue);
2775 g_object_unref (sq->queue);
2776 g_cond_clear (&sq->turn);
2777 g_cond_clear (&sq->query_handled);
2781 static GstSingleQueue *
2782 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2785 GstMultiQueuePad *mqpad;
2786 GstPadTemplate *templ;
2789 guint temp_id = (id == -1) ? 0 : id;
2791 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2793 /* Find an unused queue ID, if possible the passed one */
2794 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2795 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2796 /* This works because the IDs are sorted in ascending order */
2797 if (sq2->id == temp_id) {
2798 /* If this ID was requested by the caller return NULL,
2799 * otherwise just get us the next one */
2801 temp_id = sq2->id + 1;
2803 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2806 } else if (sq2->id > temp_id) {
2811 sq = g_new0 (GstSingleQueue, 1);
2814 sq->groupid = DEFAULT_PAD_GROUP_ID;
2816 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2817 mqueue->queues_cookie++;
2819 /* copy over max_size and extra_size so we don't need to take the lock
2820 * any longer when checking if the queue is full. */
2821 sq->max_size.visible = mqueue->max_size.visible;
2822 sq->max_size.bytes = mqueue->max_size.bytes;
2823 sq->max_size.time = mqueue->max_size.time;
2825 sq->extra_size.visible = mqueue->extra_size.visible;
2826 sq->extra_size.bytes = mqueue->extra_size.bytes;
2827 sq->extra_size.time = mqueue->extra_size.time;
2829 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2831 sq->mqueue = mqueue;
2832 sq->srcresult = GST_FLOW_FLUSHING;
2834 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2835 single_queue_check_full,
2836 (GstDataQueueFullCallback) single_queue_overrun_cb,
2837 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2839 sq->is_sparse = FALSE;
2840 sq->flushing = FALSE;
2842 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2843 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2847 sq->next_time = GST_CLOCK_STIME_NONE;
2848 sq->last_time = GST_CLOCK_STIME_NONE;
2849 g_cond_init (&sq->turn);
2850 g_cond_init (&sq->query_handled);
2852 sq->sinktime = GST_CLOCK_STIME_NONE;
2853 sq->srctime = GST_CLOCK_STIME_NONE;
2854 sq->sink_tainted = TRUE;
2855 sq->src_tainted = TRUE;
2857 name = g_strdup_printf ("sink_%u", sq->id);
2858 templ = gst_static_pad_template_get (&sinktemplate);
2859 sq->sinkpad = g_object_new (GST_TYPE_MULTIQUEUE_PAD, "name", name,
2860 "direction", templ->direction, "template", templ, NULL);
2861 gst_object_unref (templ);
2864 mqpad = (GstMultiQueuePad *) sq->sinkpad;
2867 gst_pad_set_chain_function (sq->sinkpad,
2868 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2869 gst_pad_set_activatemode_function (sq->sinkpad,
2870 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2871 gst_pad_set_event_full_function (sq->sinkpad,
2872 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2873 gst_pad_set_query_function (sq->sinkpad,
2874 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2875 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2876 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2877 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2879 name = g_strdup_printf ("src_%u", sq->id);
2880 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2883 gst_pad_set_activatemode_function (sq->srcpad,
2884 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2885 gst_pad_set_event_function (sq->srcpad,
2886 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2887 gst_pad_set_query_function (sq->srcpad,
2888 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2889 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2890 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2891 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2893 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2894 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2896 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2898 /* only activate the pads when we are not in the NULL state
2899 * and add the pad under the state_lock to prevend state changes
2900 * between activating and adding */
2901 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2902 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2903 gst_pad_set_active (sq->srcpad, TRUE);
2904 gst_pad_set_active (sq->sinkpad, TRUE);
2906 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2907 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2908 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2910 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",