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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, 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%d) and
39 * 'sometimes' src pads (src%d).
41 * When requesting a given sinkpad with gst_element_get_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.
105 * Last reviewed on 2008-01-25 (0.10.17)
114 #include "gstmultiqueue.h"
118 * @sinkpad: associated sink #GstPad
119 * @srcpad: associated source #GstPad
121 * Structure containing all information and properties about
124 typedef struct _GstSingleQueue GstSingleQueue;
126 struct _GstSingleQueue
128 /* unique identifier of the queue */
131 GstMultiQueue *mqueue;
136 /* flowreturn of previous srcpad push */
137 GstFlowReturn srcresult;
140 GstSegment sink_segment;
141 GstSegment src_segment;
143 /* position of src/sink */
144 GstClockTime sinktime, srctime;
145 /* TRUE if either position needs to be recalculated */
146 gboolean sink_tainted, src_tainted;
150 GstDataQueueSize max_size, extra_size;
151 GstClockTime cur_time;
155 /* Protected by global lock */
156 guint32 nextid; /* ID of the next object waiting to be pushed */
157 guint32 oldid; /* ID of the last object pushed (last in a series) */
158 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
159 GCond *turn; /* SingleQueue turn waiting conditional */
163 /* Extension of GstDataQueueItem structure for our usage */
164 typedef struct _GstMultiQueueItem GstMultiQueueItem;
166 struct _GstMultiQueueItem
168 GstMiniObject *object;
173 GDestroyNotify destroy;
177 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, gint id);
178 static void gst_single_queue_free (GstSingleQueue * squeue);
180 static void wake_up_next_non_linked (GstMultiQueue * mq);
181 static void compute_high_id (GstMultiQueue * mq);
182 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
183 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
185 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink%d",
188 GST_STATIC_CAPS_ANY);
190 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
193 GST_STATIC_CAPS_ANY);
195 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
196 #define GST_CAT_DEFAULT (multi_queue_debug)
198 /* Signals and args */
206 /* default limits, we try to keep up to 2 seconds of data and if there is not
207 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
208 * there is data in the queues. Normally, the byte and time limits are not hit
209 * in theses conditions. */
210 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
211 #define DEFAULT_MAX_SIZE_BUFFERS 5
212 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
214 /* second limits. When we hit one of the above limits we are probably dealing
215 * with a badly muxed file and we scale the limits to these emergency values.
216 * This is currently not yet implemented.
217 * Since we dynamically scale the queue buffer size up to the limits but avoid
218 * going above the max-size-buffers when we can, we don't really need this
219 * aditional extra size. */
220 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
221 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
222 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
224 #define DEFAULT_USE_BUFFERING FALSE
225 #define DEFAULT_LOW_PERCENT 10
226 #define DEFAULT_HIGH_PERCENT 99
231 PROP_EXTRA_SIZE_BYTES,
232 PROP_EXTRA_SIZE_BUFFERS,
233 PROP_EXTRA_SIZE_TIME,
235 PROP_MAX_SIZE_BUFFERS,
243 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
244 g_mutex_lock (q->qlock); \
247 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
248 g_mutex_unlock (q->qlock); \
251 static void gst_multi_queue_finalize (GObject * object);
252 static void gst_multi_queue_set_property (GObject * object,
253 guint prop_id, const GValue * value, GParamSpec * pspec);
254 static void gst_multi_queue_get_property (GObject * object,
255 guint prop_id, GValue * value, GParamSpec * pspec);
257 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
258 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
259 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
260 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
261 element, GstStateChange transition);
263 static void gst_multi_queue_loop (GstPad * pad);
266 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
267 #define gst_multi_queue_parent_class parent_class
268 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
271 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
274 gst_multi_queue_class_init (GstMultiQueueClass * klass)
276 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
277 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
279 gobject_class->set_property = gst_multi_queue_set_property;
280 gobject_class->get_property = gst_multi_queue_get_property;
285 * GstMultiQueue::underrun:
286 * @multiqueue: the multqueue instance
288 * This signal is emitted from the streaming thread when there is
289 * no data in any of the queues inside the multiqueue instance (underrun).
291 * This indicates either starvation or EOS from the upstream data sources.
293 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
294 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
295 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
296 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
299 * GstMultiQueue::overrun:
300 * @multiqueue: the multiqueue instance
302 * Reports that one of the queues in the multiqueue is full (overrun).
303 * A queue is full if the total amount of data inside it (num-buffers, time,
304 * size) is higher than the boundary values which can be set through the
305 * GObject properties.
307 * This can be used as an indicator of pre-roll.
309 gst_multi_queue_signals[SIGNAL_OVERRUN] =
310 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
311 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
312 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
316 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
317 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
318 "Max. amount of data in the queue (bytes, 0=disable)",
319 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
320 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
322 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
323 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
324 DEFAULT_MAX_SIZE_BUFFERS,
325 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
327 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
328 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
329 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
332 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
333 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
334 " (NOT IMPLEMENTED)",
335 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
338 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
339 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
340 " (NOT IMPLEMENTED)",
341 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
342 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
344 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
345 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
346 " (NOT IMPLEMENTED)",
347 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351 * GstMultiQueue:use-buffering
353 * Enable the buffering option in multiqueue so that BUFFERING messages are
354 * emited based on low-/high-percent thresholds.
358 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
359 g_param_spec_boolean ("use-buffering", "Use buffering",
360 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
361 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
363 * GstMultiQueue:low-percent
365 * Low threshold percent for buffering to start.
369 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
370 g_param_spec_int ("low-percent", "Low percent",
371 "Low threshold for buffering to start", 0, 100,
372 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 * GstMultiQueue:high-percent
376 * High threshold percent for buffering to finish.
380 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
381 g_param_spec_int ("high-percent", "High percent",
382 "High threshold for buffering to finish", 0, 100,
383 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 gobject_class->finalize = gst_multi_queue_finalize;
388 gst_element_class_set_details_simple (gstelement_class,
390 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
391 gst_element_class_add_pad_template (gstelement_class,
392 gst_static_pad_template_get (&sinktemplate));
393 gst_element_class_add_pad_template (gstelement_class,
394 gst_static_pad_template_get (&srctemplate));
396 gstelement_class->request_new_pad =
397 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
398 gstelement_class->release_pad =
399 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
400 gstelement_class->change_state =
401 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
405 gst_multi_queue_init (GstMultiQueue * mqueue)
407 mqueue->nbqueues = 0;
408 mqueue->queues = NULL;
410 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
411 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
412 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
414 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
415 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
416 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
418 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
419 mqueue->low_percent = DEFAULT_LOW_PERCENT;
420 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
425 mqueue->qlock = g_mutex_new ();
429 gst_multi_queue_finalize (GObject * object)
431 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
433 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
434 g_list_free (mqueue->queues);
435 mqueue->queues = NULL;
436 mqueue->queues_cookie++;
438 /* free/unref instance data */
439 g_mutex_free (mqueue->qlock);
441 G_OBJECT_CLASS (parent_class)->finalize (object);
444 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
445 GList * tmp = mq->queues; \
447 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
448 q->max_size.format = mq->max_size.format; \
449 tmp = g_list_next(tmp); \
454 gst_multi_queue_set_property (GObject * object, guint prop_id,
455 const GValue * value, GParamSpec * pspec)
457 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
460 case PROP_MAX_SIZE_BYTES:
461 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
462 mq->max_size.bytes = g_value_get_uint (value);
463 SET_CHILD_PROPERTY (mq, bytes);
464 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
466 case PROP_MAX_SIZE_BUFFERS:
467 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
468 mq->max_size.visible = g_value_get_uint (value);
469 SET_CHILD_PROPERTY (mq, visible);
470 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
472 case PROP_MAX_SIZE_TIME:
473 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
474 mq->max_size.time = g_value_get_uint64 (value);
475 SET_CHILD_PROPERTY (mq, time);
476 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
478 case PROP_EXTRA_SIZE_BYTES:
479 mq->extra_size.bytes = g_value_get_uint (value);
481 case PROP_EXTRA_SIZE_BUFFERS:
482 mq->extra_size.visible = g_value_get_uint (value);
484 case PROP_EXTRA_SIZE_TIME:
485 mq->extra_size.time = g_value_get_uint64 (value);
487 case PROP_USE_BUFFERING:
488 mq->use_buffering = g_value_get_boolean (value);
490 case PROP_LOW_PERCENT:
491 mq->low_percent = g_value_get_int (value);
493 case PROP_HIGH_PERCENT:
494 mq->high_percent = g_value_get_int (value);
497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503 gst_multi_queue_get_property (GObject * object, guint prop_id,
504 GValue * value, GParamSpec * pspec)
506 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
508 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
511 case PROP_EXTRA_SIZE_BYTES:
512 g_value_set_uint (value, mq->extra_size.bytes);
514 case PROP_EXTRA_SIZE_BUFFERS:
515 g_value_set_uint (value, mq->extra_size.visible);
517 case PROP_EXTRA_SIZE_TIME:
518 g_value_set_uint64 (value, mq->extra_size.time);
520 case PROP_MAX_SIZE_BYTES:
521 g_value_set_uint (value, mq->max_size.bytes);
523 case PROP_MAX_SIZE_BUFFERS:
524 g_value_set_uint (value, mq->max_size.visible);
526 case PROP_MAX_SIZE_TIME:
527 g_value_set_uint64 (value, mq->max_size.time);
529 case PROP_USE_BUFFERING:
530 g_value_set_boolean (value, mq->use_buffering);
532 case PROP_LOW_PERCENT:
533 g_value_set_int (value, mq->low_percent);
535 case PROP_HIGH_PERCENT:
536 g_value_set_int (value, mq->high_percent);
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
543 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
547 gst_multi_queue_iterate_internal_links (GstPad * pad)
549 GstIterator *it = NULL;
551 GstSingleQueue *squeue;
552 GstMultiQueue *mq = GST_MULTI_QUEUE (gst_pad_get_parent (pad));
555 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
556 squeue = gst_pad_get_element_private (pad);
560 if (squeue->sinkpad == pad)
561 opad = gst_object_ref (squeue->srcpad);
562 else if (squeue->srcpad == pad)
563 opad = gst_object_ref (squeue->sinkpad);
567 g_value_init (&val, GST_TYPE_PAD);
568 g_value_set_object (&val, opad);
569 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
570 g_value_unset (&val);
572 gst_object_unref (opad);
575 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
576 gst_object_unref (mq);
587 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
588 const gchar * name, const GstCaps * caps)
590 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
591 GstSingleQueue *squeue;
595 sscanf (name + 4, "%d", &temp_id);
596 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
599 /* Create a new single queue, add the sink and source pad and return the sink pad */
600 squeue = gst_single_queue_new (mqueue, temp_id);
602 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
603 GST_DEBUG_PAD_NAME (squeue->sinkpad));
605 return squeue ? squeue->sinkpad : NULL;
609 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
611 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
612 GstSingleQueue *sq = NULL;
615 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
617 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
618 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
619 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
620 sq = (GstSingleQueue *) tmp->data;
622 if (sq->sinkpad == pad)
627 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
628 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
632 /* FIXME: The removal of the singlequeue should probably not happen until it
633 * finishes draining */
635 /* remove it from the list */
636 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
637 mqueue->queues_cookie++;
639 /* FIXME : recompute next-non-linked */
640 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
642 /* delete SingleQueue */
643 gst_data_queue_set_flushing (sq->queue, TRUE);
645 gst_pad_set_active (sq->srcpad, FALSE);
646 gst_pad_set_active (sq->sinkpad, FALSE);
647 gst_pad_set_element_private (sq->srcpad, NULL);
648 gst_pad_set_element_private (sq->sinkpad, NULL);
649 gst_element_remove_pad (element, sq->srcpad);
650 gst_element_remove_pad (element, sq->sinkpad);
651 gst_single_queue_free (sq);
654 static GstStateChangeReturn
655 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
657 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
658 GstSingleQueue *sq = NULL;
659 GstStateChangeReturn result;
661 switch (transition) {
662 case GST_STATE_CHANGE_READY_TO_PAUSED:{
665 /* Set all pads to non-flushing */
666 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
667 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
668 sq = (GstSingleQueue *) tmp->data;
669 sq->flushing = FALSE;
671 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
674 case GST_STATE_CHANGE_PAUSED_TO_READY:{
677 /* Un-wait all waiting pads */
678 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
679 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
680 sq = (GstSingleQueue *) tmp->data;
682 g_cond_signal (sq->turn);
684 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
691 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
693 switch (transition) {
705 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
709 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
713 sq->srcresult = GST_FLOW_WRONG_STATE;
714 gst_data_queue_set_flushing (sq->queue, TRUE);
718 /* wake up non-linked task */
719 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
721 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
722 g_cond_signal (sq->turn);
723 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
725 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
726 result = gst_pad_pause_task (sq->srcpad);
727 sq->sink_tainted = sq->src_tainted = TRUE;
729 gst_data_queue_flush (sq->queue);
730 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
731 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
732 /* All pads start off not-linked for a smooth kick-off */
733 sq->srcresult = GST_FLOW_OK;
735 sq->max_size.visible = mq->max_size.visible;
739 sq->last_oldid = G_MAXUINT32;
740 gst_data_queue_set_flushing (sq->queue, FALSE);
742 sq->flushing = FALSE;
744 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
746 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
753 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
755 GstDataQueueSize size;
757 gboolean post = FALSE;
759 /* nothing to dowhen we are not in buffering mode */
760 if (!mq->use_buffering)
763 gst_data_queue_get_level (sq->queue, &size);
765 GST_DEBUG_OBJECT (mq,
766 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
767 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
768 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
770 /* get bytes and time percentages and take the max */
775 if (sq->max_size.time > 0) {
776 tmp = (sq->cur_time * 100) / sq->max_size.time;
777 percent = MAX (percent, tmp);
779 if (sq->max_size.bytes > 0) {
780 tmp = (size.bytes * 100) / sq->max_size.bytes;
781 percent = MAX (percent, tmp);
787 if (percent >= mq->high_percent) {
788 mq->buffering = FALSE;
790 /* make sure it increases */
791 percent = MAX (mq->percent, percent);
793 if (percent == mq->percent)
794 /* don't post if nothing changed */
797 /* else keep last value we posted */
798 mq->percent = percent;
800 if (percent < mq->low_percent) {
801 mq->buffering = TRUE;
802 mq->percent = percent;
809 /* scale to high percent so that it becomes the 100% mark */
810 percent = percent * 100 / mq->high_percent;
815 GST_DEBUG_OBJECT (mq, "buffering %d percent", percent);
816 message = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
818 gst_element_post_message (GST_ELEMENT_CAST (mq), message);
820 GST_DEBUG_OBJECT (mq, "filled %d percent", percent);
824 /* calculate the diff between running time on the sink and src of the queue.
825 * This is the total amount of time in the queue.
828 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
830 gint64 sink_time, src_time;
832 if (sq->sink_tainted) {
833 sink_time = sq->sinktime =
834 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
835 sq->sink_segment.last_stop);
837 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
838 /* if we have a time, we become untainted and use the time */
839 sq->sink_tainted = FALSE;
841 sink_time = sq->sinktime;
843 if (sq->src_tainted) {
844 src_time = sq->srctime =
845 gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
846 sq->src_segment.last_stop);
847 /* if we have a time, we become untainted and use the time */
848 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
849 sq->src_tainted = FALSE;
851 src_time = sq->srctime;
853 GST_DEBUG_OBJECT (mq,
854 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
855 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
857 /* This allows for streams with out of order timestamping - sometimes the
858 * emerging timestamp is later than the arriving one(s) */
859 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
860 sq->cur_time = sink_time - src_time;
864 /* updating the time level can change the buffering state */
865 update_buffering (mq, sq);
870 /* take a NEWSEGMENT event and apply the values to segment, updating the time
873 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
874 GstSegment * segment)
879 gint64 start, stop, time;
881 gst_event_parse_new_segment (event, &update, &rate, &arate,
882 &format, &start, &stop, &time);
884 /* now configure the values, we use these to track timestamps on the
886 if (format != GST_FORMAT_TIME) {
887 /* non-time format, pretent the current time segment is closed with a
888 * 0 start and unknown stop time. */
890 format = GST_FORMAT_TIME;
896 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
898 gst_segment_set_newsegment (segment, update,
899 rate, arate, format, start, stop, time);
901 if (segment == &sq->sink_segment)
902 sq->sink_tainted = TRUE;
904 sq->src_tainted = TRUE;
906 GST_DEBUG_OBJECT (mq,
907 "queue %d, configured NEWSEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
909 /* segment can update the time level of the queue */
910 update_time_level (mq, sq);
912 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
915 /* take a buffer and update segment, updating the time level of the queue. */
917 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
918 GstClockTime duration, GstSegment * segment)
920 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
922 /* if no timestamp is set, assume it's continuous with the previous
924 if (timestamp == GST_CLOCK_TIME_NONE)
925 timestamp = segment->last_stop;
928 if (duration != GST_CLOCK_TIME_NONE)
929 timestamp += duration;
931 GST_DEBUG_OBJECT (mq, "queue %d, last_stop updated to %" GST_TIME_FORMAT,
932 sq->id, GST_TIME_ARGS (timestamp));
934 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
936 if (segment == &sq->sink_segment)
937 sq->sink_tainted = TRUE;
939 sq->src_tainted = TRUE;
941 /* calc diff with other end */
942 update_time_level (mq, sq);
943 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
947 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
948 GstMiniObject * object)
950 GstFlowReturn result = GST_FLOW_OK;
952 if (GST_IS_BUFFER (object)) {
954 GstClockTime timestamp, duration;
959 buffer = GST_BUFFER_CAST (object);
960 timestamp = GST_BUFFER_TIMESTAMP (buffer);
961 duration = GST_BUFFER_DURATION (buffer);
963 caps = GST_BUFFER_CAPS (buffer);
966 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
968 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
969 gst_data_queue_limits_changed (sq->queue);
971 GST_DEBUG_OBJECT (mq,
972 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
973 sq->id, buffer, GST_TIME_ARGS (timestamp));
976 /* Set caps on pad before pushing, this avoids core calling the acceptcaps
977 * function on the srcpad, which will call acceptcaps upstream, which might
978 * not accept these caps (anymore). */
979 if (caps && caps != GST_PAD_CAPS (sq->srcpad))
980 gst_pad_set_caps (sq->srcpad, caps);
983 result = gst_pad_push (sq->srcpad, buffer);
984 } else if (GST_IS_EVENT (object)) {
987 event = GST_EVENT_CAST (object);
989 switch (GST_EVENT_TYPE (event)) {
991 result = GST_FLOW_UNEXPECTED;
993 case GST_EVENT_NEWSEGMENT:
994 apply_segment (mq, sq, event, &sq->src_segment);
995 /* Applying the segment may have made the queue non-full again, unblock it if needed */
996 gst_data_queue_limits_changed (sq->queue);
1002 GST_DEBUG_OBJECT (mq,
1003 "SingleQueue %d : Pushing event %p of type %s",
1004 sq->id, event, GST_EVENT_TYPE_NAME (event));
1006 gst_pad_push_event (sq->srcpad, event);
1008 g_warning ("Unexpected object in singlequeue %d (refcounting problem?)",
1016 static GstMiniObject *
1017 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1022 item->object = NULL;
1028 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1031 gst_mini_object_unref (item->object);
1032 g_slice_free (GstMultiQueueItem, item);
1035 /* takes ownership of passed mini object! */
1036 static GstMultiQueueItem *
1037 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1039 GstMultiQueueItem *item;
1041 item = g_slice_new (GstMultiQueueItem);
1042 item->object = object;
1043 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1044 item->posid = curid;
1046 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1047 item->duration = GST_BUFFER_DURATION (object);
1048 if (item->duration == GST_CLOCK_TIME_NONE)
1050 item->visible = TRUE;
1054 static GstMultiQueueItem *
1055 gst_multi_queue_event_item_new (GstMiniObject * object, guint32 curid)
1057 GstMultiQueueItem *item;
1059 item = g_slice_new (GstMultiQueueItem);
1060 item->object = object;
1061 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1062 item->posid = curid;
1066 item->visible = FALSE;
1070 /* Each main loop attempts to push buffers until the return value
1071 * is not-linked. not-linked pads are not allowed to push data beyond
1072 * any linked pads, so they don't 'rush ahead of the pack'.
1075 gst_multi_queue_loop (GstPad * pad)
1078 GstMultiQueueItem *item;
1079 GstDataQueueItem *sitem;
1081 GstMiniObject *object = NULL;
1083 GstFlowReturn result;
1085 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1088 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1093 /* Get something from the queue, blocking until that happens, or we get
1095 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1098 item = (GstMultiQueueItem *) sitem;
1099 newid = item->posid;
1101 /* steal the object and destroy the item */
1102 object = gst_multi_queue_item_steal_object (item);
1103 gst_multi_queue_item_destroy (item);
1105 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1106 sq->id, newid, sq->last_oldid);
1108 /* If we're not-linked, we do some extra work because we might need to
1109 * wait before pushing. If we're linked but there's a gap in the IDs,
1110 * or it's the first loop, or we just passed the previous highid,
1111 * we might need to wake some sleeping pad up, so there's extra work
1113 if (sq->srcresult == GST_FLOW_NOT_LINKED ||
1114 (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1)) ||
1115 sq->last_oldid > mq->highid) {
1116 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1117 gst_flow_get_name (sq->srcresult));
1119 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1121 /* Check again if we're flushing after the lock is taken,
1122 * the flush flag might have been changed in the meantime */
1124 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1128 /* Update the nextid so other threads know when to wake us up */
1131 /* Update the oldid (the last ID we output) for highid tracking */
1132 if (sq->last_oldid != G_MAXUINT32)
1133 sq->oldid = sq->last_oldid;
1135 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1136 /* Go to sleep until it's time to push this buffer */
1138 /* Recompute the highid */
1139 compute_high_id (mq);
1140 while (newid > mq->highid && sq->srcresult == GST_FLOW_NOT_LINKED) {
1141 GST_DEBUG_OBJECT (mq, "queue %d sleeping for not-linked wakeup with "
1142 "newid %u and highid %u", sq->id, newid, mq->highid);
1145 /* Wake up all non-linked pads before we sleep */
1146 wake_up_next_non_linked (mq);
1149 g_cond_wait (sq->turn, mq->qlock);
1153 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1157 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1158 "wakeup with newid %u and highid %u", sq->id, newid, mq->highid);
1161 /* Re-compute the high_id in case someone else pushed */
1162 compute_high_id (mq);
1164 compute_high_id (mq);
1165 /* Wake up all non-linked pads */
1166 wake_up_next_non_linked (mq);
1168 /* We're done waiting, we can clear the nextid */
1171 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1177 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1178 gst_flow_get_name (sq->srcresult));
1180 /* Try to push out the new object */
1181 result = gst_single_queue_push_one (mq, sq, object);
1182 sq->srcresult = result;
1185 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1186 && result != GST_FLOW_UNEXPECTED)
1189 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1190 gst_flow_get_name (sq->srcresult));
1192 sq->last_oldid = newid;
1198 gst_mini_object_unref (object);
1200 /* Need to make sure wake up any sleeping pads when we exit */
1201 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1202 compute_high_id (mq);
1203 wake_up_next_non_linked (mq);
1204 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1206 /* upstream needs to see fatal result ASAP to shut things down,
1207 * but might be stuck in one of our other full queues;
1208 * so empty this one and trigger dynamic queue growth. At
1209 * this point the srcresult is not OK, NOT_LINKED
1210 * or UNEXPECTED, i.e. a real failure */
1211 gst_data_queue_flush (sq->queue);
1212 single_queue_underrun_cb (sq->queue, sq);
1213 gst_data_queue_set_flushing (sq->queue, TRUE);
1214 gst_pad_pause_task (sq->srcpad);
1215 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1216 "SingleQueue[%d] task paused, reason:%s",
1217 sq->id, gst_flow_get_name (sq->srcresult));
1223 * gst_multi_queue_chain:
1225 * This is similar to GstQueue's chain function, except:
1226 * _ we don't have leak behavioures,
1227 * _ we push with a unique id (curid)
1229 static GstFlowReturn
1230 gst_multi_queue_chain (GstPad * pad, GstBuffer * buffer)
1234 GstMultiQueueItem *item;
1236 GstClockTime timestamp, duration;
1238 sq = gst_pad_get_element_private (pad);
1241 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1245 /* Get a unique incrementing id */
1246 curid = g_atomic_int_exchange_and_add ((gint *) & mq->counter, 1);
1248 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1249 sq->id, buffer, curid);
1251 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1253 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1254 duration = GST_BUFFER_DURATION (buffer);
1256 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1259 /* update time level, we must do this after pushing the data in the queue so
1260 * that we never end up filling the queue first. */
1261 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1264 return sq->srcresult;
1269 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1270 sq->id, gst_flow_get_name (sq->srcresult));
1271 gst_multi_queue_item_destroy (item);
1276 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return UNEXPECTED");
1277 gst_buffer_unref (buffer);
1278 return GST_FLOW_UNEXPECTED;
1283 gst_multi_queue_sink_activate_push (GstPad * pad, gboolean active)
1287 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1290 /* All pads start off linked until they push one buffer */
1291 sq->srcresult = GST_FLOW_OK;
1293 sq->srcresult = GST_FLOW_WRONG_STATE;
1294 gst_data_queue_flush (sq->queue);
1300 gst_multi_queue_sink_event (GstPad * pad, GstEvent * event)
1305 GstMultiQueueItem *item;
1308 GstEvent *sref = NULL;
1310 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1311 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1313 type = GST_EVENT_TYPE (event);
1316 case GST_EVENT_FLUSH_START:
1317 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1320 res = gst_pad_push_event (sq->srcpad, event);
1322 gst_single_queue_flush (mq, sq, TRUE);
1325 case GST_EVENT_FLUSH_STOP:
1326 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1329 res = gst_pad_push_event (sq->srcpad, event);
1331 gst_single_queue_flush (mq, sq, FALSE);
1333 case GST_EVENT_NEWSEGMENT:
1334 /* take ref because the queue will take ownership and we need the event
1335 * afterwards to update the segment */
1336 sref = gst_event_ref (event);
1340 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1341 res = gst_pad_push_event (sq->srcpad, event);
1347 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1351 /* Get an unique incrementing id. */
1352 curid = g_atomic_int_exchange_and_add ((gint *) & mq->counter, 1);
1354 item = gst_multi_queue_event_item_new ((GstMiniObject *) event, curid);
1356 GST_DEBUG_OBJECT (mq,
1357 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1358 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1360 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1363 /* mark EOS when we received one, we must do that after putting the
1364 * buffer in the queue because EOS marks the buffer as filled. No need to take
1365 * a lock, the _check_full happens from this thread only, right before pushing
1366 * into dataqueue. */
1370 /* EOS affects the buffering state */
1371 update_buffering (mq, sq);
1372 single_queue_overrun_cb (sq->queue, sq);
1374 case GST_EVENT_NEWSEGMENT:
1375 apply_segment (mq, sq, sref, &sq->sink_segment);
1376 gst_event_unref (sref);
1382 gst_object_unref (mq);
1387 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1388 sq->id, gst_flow_get_name (sq->srcresult));
1390 gst_event_unref (sref);
1391 gst_multi_queue_item_destroy (item);
1396 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1397 gst_event_unref (event);
1404 gst_multi_queue_getcaps (GstPad * pad)
1406 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1410 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1412 GST_LOG_OBJECT (otherpad, "Getting caps from the peer of this pad");
1414 result = gst_pad_peer_get_caps (otherpad);
1416 result = gst_caps_new_any ();
1422 gst_multi_queue_acceptcaps (GstPad * pad, GstCaps * caps)
1424 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1428 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1430 GST_LOG_OBJECT (otherpad, "Accept caps from the peer of this pad");
1432 result = gst_pad_peer_accept_caps (otherpad, caps);
1438 gst_multi_queue_src_activate_push (GstPad * pad, gboolean active)
1442 gboolean result = FALSE;
1444 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1447 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1450 result = gst_single_queue_flush (mq, sq, FALSE);
1452 result = gst_single_queue_flush (mq, sq, TRUE);
1453 /* make sure streaming finishes */
1454 result |= gst_pad_stop_task (pad);
1460 gst_multi_queue_src_event (GstPad * pad, GstEvent * event)
1462 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1464 return gst_pad_push_event (sq->sinkpad, event);
1468 gst_multi_queue_src_query (GstPad * pad, GstQuery ** query)
1470 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1474 /* FIXME, Handle position offset depending on queue size */
1476 /* default handling */
1477 if (!(peerpad = gst_pad_get_peer (sq->sinkpad)))
1480 res = gst_pad_query (peerpad, query);
1482 gst_object_unref (peerpad);
1489 GST_LOG_OBJECT (sq->sinkpad, "Couldn't send query because we have no peer");
1495 * Next-non-linked functions
1498 /* WITH LOCK TAKEN */
1500 wake_up_next_non_linked (GstMultiQueue * mq)
1504 /* maybe no-one is waiting */
1505 if (mq->numwaiting < 1)
1508 /* Else figure out which singlequeue(s) need waking up */
1509 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1510 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1512 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1513 if (sq->nextid != 0 && sq->nextid <= mq->highid) {
1514 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1515 g_cond_signal (sq->turn);
1521 /* WITH LOCK TAKEN */
1523 compute_high_id (GstMultiQueue * mq)
1525 /* The high-id is either the highest id among the linked pads, or if all
1526 * pads are not-linked, it's the lowest not-linked pad */
1528 guint32 lowest = G_MAXUINT32;
1529 guint32 highid = G_MAXUINT32;
1531 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1532 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1534 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1535 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1537 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1538 /* No need to consider queues which are not waiting */
1539 if (sq->nextid == 0) {
1540 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1544 if (sq->nextid < lowest)
1545 lowest = sq->nextid;
1546 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1547 /* If we don't have a global highid, or the global highid is lower than
1548 * this single queue's last outputted id, store the queue's one,
1549 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1550 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1555 if (highid == G_MAXUINT32 || lowest < highid)
1556 mq->highid = lowest;
1558 mq->highid = highid;
1560 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1564 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
1565 ((q)->max_size.format) <= (value))
1568 * GstSingleQueue functions
1571 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1573 GstMultiQueue *mq = sq->mqueue;
1575 GstDataQueueSize size;
1576 gboolean filled = FALSE;
1578 gst_data_queue_get_level (sq->queue, &size);
1580 GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
1582 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1583 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1584 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1585 GstDataQueueSize ssize;
1587 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
1589 if (gst_data_queue_is_empty (oq->queue)) {
1590 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
1591 if (IS_FILLED (sq, visible, size.visible)) {
1592 sq->max_size.visible = size.visible + 1;
1593 GST_DEBUG_OBJECT (mq,
1594 "Another queue is empty, bumping single queue %d max visible to %d",
1595 sq->id, sq->max_size.visible);
1597 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1600 /* check if we reached the hard time/bytes limits */
1601 gst_data_queue_get_level (oq->queue, &ssize);
1603 GST_DEBUG_OBJECT (mq,
1604 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1605 G_GUINT64_FORMAT, oq->id, ssize.visible, oq->max_size.visible,
1606 ssize.bytes, oq->max_size.bytes, oq->cur_time, oq->max_size.time);
1608 /* if this queue is filled completely we must signal overrun.
1609 * FIXME, this seems wrong in many ways
1610 * - we're comparing the filled level of this queue against the
1611 * values of the other one
1612 * - we should only do this after we found no empty queues, ie, move
1613 * this check outside of the loop
1614 * - the debug statement talks about a different queue than the one
1615 * we are checking here.
1617 if (sq->is_eos || IS_FILLED (sq, bytes, ssize.bytes) ||
1618 IS_FILLED (sq, time, sq->cur_time)) {
1619 GST_LOG_OBJECT (mq, "Queue %d is filled", oq->id);
1623 /* no queues were empty */
1624 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1626 /* Overrun is always forwarded, since this is blocking the upstream element */
1628 GST_DEBUG_OBJECT (mq, "A queue is filled, signalling overrun");
1629 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1637 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1639 gboolean empty = TRUE;
1640 GstMultiQueue *mq = sq->mqueue;
1644 "Single Queue %d is empty, Checking other single queues", sq->id);
1646 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1647 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1648 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1650 if (gst_data_queue_is_full (oq->queue)) {
1651 GstDataQueueSize size;
1653 gst_data_queue_get_level (oq->queue, &size);
1654 if (IS_FILLED (oq, visible, size.visible)) {
1655 oq->max_size.visible = size.visible + 1;
1656 GST_DEBUG_OBJECT (mq,
1657 "queue %d is filled, bumping its max visible to %d", oq->id,
1658 oq->max_size.visible);
1659 gst_data_queue_limits_changed (oq->queue);
1662 if (!gst_data_queue_is_empty (oq->queue))
1665 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1668 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1669 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1674 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1675 guint64 time, GstSingleQueue * sq)
1678 GstMultiQueue *mq = sq->mqueue;
1680 GST_DEBUG_OBJECT (mq,
1681 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1682 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1683 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1685 /* we are always filled on EOS */
1689 /* we never go past the max visible items unless we are in buffering mode */
1690 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
1693 /* check time or bytes */
1694 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
1700 gst_single_queue_free (GstSingleQueue * sq)
1703 gst_data_queue_flush (sq->queue);
1704 g_object_unref (sq->queue);
1705 g_cond_free (sq->turn);
1709 static GstSingleQueue *
1710 gst_single_queue_new (GstMultiQueue * mqueue, gint id)
1715 gint temp_id = (id == -1) ? 0 : id;
1717 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1719 /* Find an unused queue ID, if possible the passed one */
1720 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1721 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1722 /* This works because the IDs are sorted in ascending order */
1723 if (sq2->id == temp_id) {
1724 /* If this ID was requested by the caller return NULL,
1725 * otherwise just get us the next one */
1727 temp_id = sq2->id + 1;
1730 } else if (sq2->id > temp_id) {
1735 sq = g_new0 (GstSingleQueue, 1);
1739 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
1740 mqueue->queues_cookie++;
1742 /* copy over max_size and extra_size so we don't need to take the lock
1743 * any longer when checking if the queue is full. */
1744 sq->max_size.visible = mqueue->max_size.visible;
1745 sq->max_size.bytes = mqueue->max_size.bytes;
1746 sq->max_size.time = mqueue->max_size.time;
1748 sq->extra_size.visible = mqueue->extra_size.visible;
1749 sq->extra_size.bytes = mqueue->extra_size.bytes;
1750 sq->extra_size.time = mqueue->extra_size.time;
1752 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1754 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
1756 sq->mqueue = mqueue;
1757 sq->srcresult = GST_FLOW_WRONG_STATE;
1758 sq->queue = gst_data_queue_new_full ((GstDataQueueCheckFullFunction)
1759 single_queue_check_full,
1760 (GstDataQueueFullCallback) single_queue_overrun_cb,
1761 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
1763 sq->flushing = FALSE;
1764 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1765 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1769 sq->turn = g_cond_new ();
1771 sq->sinktime = GST_CLOCK_TIME_NONE;
1772 sq->srctime = GST_CLOCK_TIME_NONE;
1773 sq->sink_tainted = TRUE;
1774 sq->src_tainted = TRUE;
1776 name = g_strdup_printf ("sink%d", sq->id);
1777 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
1780 gst_pad_set_chain_function (sq->sinkpad,
1781 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
1782 gst_pad_set_activatepush_function (sq->sinkpad,
1783 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_push));
1784 gst_pad_set_event_function (sq->sinkpad,
1785 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
1786 gst_pad_set_getcaps_function (sq->sinkpad,
1787 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1788 gst_pad_set_acceptcaps_function (sq->sinkpad,
1789 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1790 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
1791 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1793 name = g_strdup_printf ("src%d", sq->id);
1794 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
1797 gst_pad_set_activatepush_function (sq->srcpad,
1798 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_push));
1799 gst_pad_set_getcaps_function (sq->srcpad,
1800 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1801 gst_pad_set_acceptcaps_function (sq->srcpad,
1802 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1803 gst_pad_set_event_function (sq->srcpad,
1804 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
1805 gst_pad_set_query_function (sq->srcpad,
1806 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
1807 gst_pad_set_iterate_internal_links_function (sq->srcpad,
1808 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1810 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
1811 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
1813 /* only activate the pads when we are not in the NULL state
1814 * and add the pad under the state_lock to prevend state changes
1815 * between activating and adding */
1816 g_static_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
1817 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
1818 gst_pad_set_active (sq->srcpad, TRUE);
1819 gst_pad_set_active (sq->sinkpad, TRUE);
1821 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
1822 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
1823 g_static_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
1825 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",