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>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:element-multiqueue
26 * @see_also: #GstQueue
30 * Multiqueue is similar to a normal #GstQueue with the following additional
34 * <itemizedlist><title>Multiple streamhandling</title>
36 * The element handles queueing data on more than one stream at once. To
37 * achieve such a feature it has request sink pads (sink%d) and
38 * 'sometimes' src pads (src%d).
40 * When requesting a given sinkpad with gst_element_get_request_pad(),
41 * the associated srcpad for that stream will be created.
42 * Example: requesting sink1 will generate src1.
47 * <itemizedlist><title>Non-starvation on multiple streams</title>
49 * If more than one stream is used with the element, the streams' queues
50 * will be dynamically grown (up to a limit), in order to ensure that no
51 * stream is risking data starvation. This guarantees that at any given
52 * time there are at least N bytes queued and available for each individual
55 * If an EOS event comes through a srcpad, the associated queue will be
56 * considered as 'not-empty' in the queue-size-growing algorithm.
61 * <itemizedlist><title>Non-linked srcpads graceful handling</title>
63 * In order to better support dynamic switching between streams, the multiqueue
64 * (unlike the current GStreamer queue) continues to push buffers on non-linked
65 * pads rather than shutting down.
67 * In addition, to prevent a non-linked stream from very quickly consuming all
68 * available buffers and thus 'racing ahead' of the other streams, the element
69 * must ensure that buffers and inlined events for a non-linked stream are pushed
70 * in the same order as they were received, relative to the other streams
71 * controlled by the element. This means that a buffer cannot be pushed to a
72 * non-linked pad any sooner than buffers in any other stream which were received
80 * Data is queued until one of the limits specified by the
81 * #GstMultiQueue:max-size-buffers, #GstMultiQueue:max-size-bytes and/or
82 * #GstMultiQueue:max-size-time properties has been reached. Any attempt to push
83 * more buffers into the queue will block the pushing thread until more space
84 * becomes available. #GstMultiQueue:extra-size-buffers,
87 * #GstMultiQueue:extra-size-bytes and #GstMultiQueue:extra-size-time are
91 * The default queue size limits are 5 buffers, 10MB of data, or
92 * two second worth of data, whichever is reached first. Note that the number
93 * of buffers will dynamically grow depending on the fill level of
97 * The #GstMultiQueue::underrun signal is emitted when all of the queues
98 * are empty. The #GstMultiQueue::overrun signal is emitted when one of the
100 * Both signals are emitted from the context of the streaming thread.
104 * Last reviewed on 2008-01-25 (0.10.17)
112 #include "gstmultiqueue.h"
116 * @sinkpad: associated sink #GstPad
117 * @srcpad: associated source #GstPad
119 * Structure containing all information and properties about
122 typedef struct _GstSingleQueue GstSingleQueue;
124 struct _GstSingleQueue
126 /* unique identifier of the queue */
129 GstMultiQueue *mqueue;
134 /* flowreturn of previous srcpad push */
135 GstFlowReturn srcresult;
138 GstSegment sink_segment;
139 GstSegment src_segment;
141 /* position of src/sink */
142 GstClockTime sinktime, srctime;
143 /* TRUE if either position needs to be recalculated */
144 gboolean sink_tainted, src_tainted;
148 GstDataQueueSize max_size, extra_size;
149 GstClockTime cur_time;
152 /* Protected by global lock */
153 guint32 nextid; /* ID of the next object waiting to be pushed */
154 guint32 oldid; /* ID of the last object pushed (last in a series) */
155 GCond *turn; /* SingleQueue turn waiting conditional */
159 /* Extension of GstDataQueueItem structure for our usage */
160 typedef struct _GstMultiQueueItem GstMultiQueueItem;
162 struct _GstMultiQueueItem
164 GstMiniObject *object;
169 GDestroyNotify destroy;
173 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue);
174 static void gst_single_queue_free (GstSingleQueue * squeue);
176 static void wake_up_next_non_linked (GstMultiQueue * mq);
177 static void compute_high_id (GstMultiQueue * mq);
178 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
179 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
181 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink%d",
184 GST_STATIC_CAPS_ANY);
186 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
189 GST_STATIC_CAPS_ANY);
191 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
192 #define GST_CAT_DEFAULT (multi_queue_debug)
194 /* Signals and args */
202 /* default limits, we try to keep up to 2 seconds of data and if there is not
203 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
204 * there is data in the queues. Normally, the byte and time limits are not hit
205 * in theses conditions. */
206 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
207 #define DEFAULT_MAX_SIZE_BUFFERS 5
208 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
210 /* second limits. When we hit one of the above limits we are probably dealing
211 * with a badly muxed file and we scale the limits to these emergency values.
212 * This is currently not yet implemented.
213 * Since we dynamically scale the queue buffer size up to the limits but avoid
214 * going above the max-size-buffers when we can, we don't really need this
215 * aditional extra size. */
216 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
217 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
218 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
220 #define DEFAULT_USE_BUFFERING FALSE
221 #define DEFAULT_LOW_PERCENT 10
222 #define DEFAULT_HIGH_PERCENT 99
227 PROP_EXTRA_SIZE_BYTES,
228 PROP_EXTRA_SIZE_BUFFERS,
229 PROP_EXTRA_SIZE_TIME,
231 PROP_MAX_SIZE_BUFFERS,
239 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
240 g_mutex_lock (q->qlock); \
243 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
244 g_mutex_unlock (q->qlock); \
247 static void gst_multi_queue_finalize (GObject * object);
248 static void gst_multi_queue_set_property (GObject * object,
249 guint prop_id, const GValue * value, GParamSpec * pspec);
250 static void gst_multi_queue_get_property (GObject * object,
251 guint prop_id, GValue * value, GParamSpec * pspec);
253 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
254 GstPadTemplate * temp, const gchar * name);
255 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
257 static void gst_multi_queue_loop (GstPad * pad);
259 #define _do_init(bla) \
260 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
262 GST_BOILERPLATE_FULL (GstMultiQueue, gst_multi_queue, GstElement,
263 GST_TYPE_ELEMENT, _do_init);
265 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
268 gst_multi_queue_base_init (gpointer g_class)
270 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
272 gst_element_class_set_details_simple (gstelement_class,
274 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
275 gst_element_class_add_pad_template (gstelement_class,
276 gst_static_pad_template_get (&sinktemplate));
277 gst_element_class_add_pad_template (gstelement_class,
278 gst_static_pad_template_get (&srctemplate));
282 gst_multi_queue_class_init (GstMultiQueueClass * klass)
284 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
285 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
287 gobject_class->set_property = gst_multi_queue_set_property;
288 gobject_class->get_property = gst_multi_queue_get_property;
293 * GstMultiQueue::underrun:
294 * @multiqueue: the multqueue instance
296 * This signal is emitted from the streaming thread when there is
297 * no data in any of the queues inside the multiqueue instance (underrun).
299 * This indicates either starvation or EOS from the upstream data sources.
301 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
302 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
303 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
304 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
307 * GstMultiQueue::overrun:
308 * @multiqueue: the multiqueue instance
310 * Reports that one of the queues in the multiqueue is full (overrun).
311 * A queue is full if the total amount of data inside it (num-buffers, time,
312 * size) is higher than the boundary values which can be set through the
313 * GObject properties.
315 * This can be used as an indicator of pre-roll.
317 gst_multi_queue_signals[SIGNAL_OVERRUN] =
318 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
319 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
320 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
324 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
325 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
326 "Max. amount of data in the queue (bytes, 0=disable)",
327 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
328 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
330 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
331 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
332 DEFAULT_MAX_SIZE_BUFFERS,
333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
335 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
336 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
337 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
340 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
341 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
342 " (NOT IMPLEMENTED)",
343 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
346 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
347 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
348 " (NOT IMPLEMENTED)",
349 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
352 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
353 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
354 " (NOT IMPLEMENTED)",
355 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 * GstMultiQueue:use-buffering
361 * Enable the buffering option in multiqueue so that BUFFERING messages are
362 * emited based on low-/high-percent thresholds.
366 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
367 g_param_spec_boolean ("use-buffering", "Use buffering",
368 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
369 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 * GstMultiQueue:low-percent
373 * Low threshold percent for buffering to start.
377 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
378 g_param_spec_int ("low-percent", "Low percent",
379 "Low threshold for buffering to start", 0, 100,
380 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
382 * GstMultiQueue:high-percent
384 * High threshold percent for buffering to finish.
388 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
389 g_param_spec_int ("high-percent", "High percent",
390 "High threshold for buffering to finish", 0, 100,
391 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 gobject_class->finalize = gst_multi_queue_finalize;
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);
403 gst_multi_queue_init (GstMultiQueue * mqueue, GstMultiQueueClass * klass)
405 mqueue->nbqueues = 0;
406 mqueue->queues = NULL;
408 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
409 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
410 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
412 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
413 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
414 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
416 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
417 mqueue->low_percent = DEFAULT_LOW_PERCENT;
418 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
422 mqueue->nextnotlinked = -1;
424 mqueue->qlock = g_mutex_new ();
428 gst_multi_queue_finalize (GObject * object)
430 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
432 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
433 g_list_free (mqueue->queues);
434 mqueue->queues = NULL;
435 mqueue->queues_cookie++;
437 /* free/unref instance data */
438 g_mutex_free (mqueue->qlock);
440 G_OBJECT_CLASS (parent_class)->finalize (object);
443 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
444 GList * tmp = mq->queues; \
446 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
447 q->max_size.format = mq->max_size.format; \
448 tmp = g_list_next(tmp); \
453 gst_multi_queue_set_property (GObject * object, guint prop_id,
454 const GValue * value, GParamSpec * pspec)
456 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
459 case PROP_MAX_SIZE_BYTES:
460 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
461 mq->max_size.bytes = g_value_get_uint (value);
462 SET_CHILD_PROPERTY (mq, bytes);
463 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
465 case PROP_MAX_SIZE_BUFFERS:
466 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
467 mq->max_size.visible = g_value_get_uint (value);
468 SET_CHILD_PROPERTY (mq, visible);
469 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
471 case PROP_MAX_SIZE_TIME:
472 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
473 mq->max_size.time = g_value_get_uint64 (value);
474 SET_CHILD_PROPERTY (mq, time);
475 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
477 case PROP_EXTRA_SIZE_BYTES:
478 mq->extra_size.bytes = g_value_get_uint (value);
480 case PROP_EXTRA_SIZE_BUFFERS:
481 mq->extra_size.visible = g_value_get_uint (value);
483 case PROP_EXTRA_SIZE_TIME:
484 mq->extra_size.time = g_value_get_uint64 (value);
486 case PROP_USE_BUFFERING:
487 mq->use_buffering = g_value_get_boolean (value);
489 case PROP_LOW_PERCENT:
490 mq->low_percent = g_value_get_int (value);
492 case PROP_HIGH_PERCENT:
493 mq->high_percent = g_value_get_int (value);
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502 gst_multi_queue_get_property (GObject * object, guint prop_id,
503 GValue * value, GParamSpec * pspec)
505 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
507 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
510 case PROP_EXTRA_SIZE_BYTES:
511 g_value_set_uint (value, mq->extra_size.bytes);
513 case PROP_EXTRA_SIZE_BUFFERS:
514 g_value_set_uint (value, mq->extra_size.visible);
516 case PROP_EXTRA_SIZE_TIME:
517 g_value_set_uint64 (value, mq->extra_size.time);
519 case PROP_MAX_SIZE_BYTES:
520 g_value_set_uint (value, mq->max_size.bytes);
522 case PROP_MAX_SIZE_BUFFERS:
523 g_value_set_uint (value, mq->max_size.visible);
525 case PROP_MAX_SIZE_TIME:
526 g_value_set_uint64 (value, mq->max_size.time);
528 case PROP_USE_BUFFERING:
529 g_value_set_boolean (value, mq->use_buffering);
531 case PROP_LOW_PERCENT:
532 g_value_set_int (value, mq->low_percent);
534 case PROP_HIGH_PERCENT:
535 g_value_set_int (value, mq->high_percent);
538 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
542 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
546 gst_multi_queue_iterate_internal_links (GstPad * pad)
548 GstIterator *it = NULL;
550 GstSingleQueue *squeue;
551 GstMultiQueue *mq = GST_MULTI_QUEUE (gst_pad_get_parent (pad));
553 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
554 squeue = gst_pad_get_element_private (pad);
558 if (squeue->sinkpad == pad)
559 opad = gst_object_ref (squeue->srcpad);
560 else if (squeue->srcpad == pad)
561 opad = gst_object_ref (squeue->sinkpad);
565 it = gst_iterator_new_single (GST_TYPE_PAD, opad,
566 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
568 gst_object_unref (opad);
571 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
572 gst_object_unref (mq);
583 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
586 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
587 GstSingleQueue *squeue;
589 GST_LOG_OBJECT (element, "name : %s", GST_STR_NULL (name));
591 /* Create a new single queue, add the sink and source pad and return the sink pad */
592 squeue = gst_single_queue_new (mqueue);
594 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
595 mqueue->queues = g_list_append (mqueue->queues, squeue);
596 mqueue->queues_cookie++;
597 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
599 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
600 GST_DEBUG_PAD_NAME (squeue->sinkpad));
602 return squeue->sinkpad;
606 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
608 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
609 GstSingleQueue *sq = NULL;
612 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
614 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
615 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
616 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
617 sq = (GstSingleQueue *) tmp->data;
619 if (sq->sinkpad == pad)
624 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
625 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
629 /* FIXME: The removal of the singlequeue should probably not happen until it
630 * finishes draining */
632 /* remove it from the list */
633 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
634 mqueue->queues_cookie++;
636 /* FIXME : recompute next-non-linked */
637 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
639 /* delete SingleQueue */
640 gst_data_queue_set_flushing (sq->queue, TRUE);
642 gst_pad_set_active (sq->srcpad, FALSE);
643 gst_pad_set_active (sq->sinkpad, FALSE);
644 gst_pad_set_element_private (sq->srcpad, NULL);
645 gst_pad_set_element_private (sq->sinkpad, NULL);
646 gst_element_remove_pad (element, sq->srcpad);
647 gst_element_remove_pad (element, sq->sinkpad);
648 gst_single_queue_free (sq);
652 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
656 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
660 sq->srcresult = GST_FLOW_WRONG_STATE;
661 gst_data_queue_set_flushing (sq->queue, TRUE);
663 /* wake up non-linked task */
664 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
666 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
667 g_cond_signal (sq->turn);
668 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
670 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
671 result = gst_pad_pause_task (sq->srcpad);
672 sq->sink_tainted = sq->src_tainted = TRUE;
674 gst_data_queue_flush (sq->queue);
675 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
676 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
677 /* All pads start off not-linked for a smooth kick-off */
678 sq->srcresult = GST_FLOW_OK;
680 sq->max_size.visible = mq->max_size.visible;
684 gst_data_queue_set_flushing (sq->queue, FALSE);
686 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
688 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
695 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
697 GstDataQueueSize size;
699 gboolean post = FALSE;
701 /* nothing to dowhen we are not in buffering mode */
702 if (!mq->use_buffering)
705 gst_data_queue_get_level (sq->queue, &size);
707 GST_DEBUG_OBJECT (mq,
708 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
709 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
710 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
712 /* get bytes and time percentages and take the max */
717 if (sq->max_size.time > 0) {
718 tmp = (sq->cur_time * 100) / sq->max_size.time;
719 percent = MAX (percent, tmp);
721 if (sq->max_size.bytes > 0) {
722 tmp = (size.bytes * 100) / sq->max_size.bytes;
723 percent = MAX (percent, tmp);
729 if (percent >= mq->high_percent) {
730 mq->buffering = FALSE;
732 /* make sure it increases */
733 percent = MAX (mq->percent, percent);
735 if (percent == mq->percent)
736 /* don't post if nothing changed */
739 /* else keep last value we posted */
740 mq->percent = percent;
742 if (percent < mq->low_percent) {
743 mq->buffering = TRUE;
744 mq->percent = percent;
751 /* scale to high percent so that it becomes the 100% mark */
752 percent = percent * 100 / mq->high_percent;
757 GST_DEBUG_OBJECT (mq, "buffering %d percent", percent);
758 message = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
760 gst_element_post_message (GST_ELEMENT_CAST (mq), message);
762 GST_DEBUG_OBJECT (mq, "filled %d percent", percent);
766 /* calculate the diff between running time on the sink and src of the queue.
767 * This is the total amount of time in the queue.
770 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
772 gint64 sink_time, src_time;
774 if (sq->sink_tainted) {
775 sink_time = sq->sinktime =
776 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
777 sq->sink_segment.last_stop);
779 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
780 /* if we have a time, we become untainted and use the time */
781 sq->sink_tainted = FALSE;
783 sink_time = sq->sinktime;
785 if (sq->src_tainted) {
786 src_time = sq->srctime =
787 gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
788 sq->src_segment.last_stop);
789 /* if we have a time, we become untainted and use the time */
790 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
791 sq->src_tainted = FALSE;
793 src_time = sq->srctime;
795 GST_DEBUG_OBJECT (mq,
796 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
797 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
799 /* This allows for streams with out of order timestamping - sometimes the
800 * emerging timestamp is later than the arriving one(s) */
801 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
802 sq->cur_time = sink_time - src_time;
806 /* updating the time level can change the buffering state */
807 update_buffering (mq, sq);
812 /* take a NEWSEGMENT event and apply the values to segment, updating the time
815 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
816 GstSegment * segment)
821 gint64 start, stop, time;
823 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
824 &format, &start, &stop, &time);
826 /* now configure the values, we use these to track timestamps on the
828 if (format != GST_FORMAT_TIME) {
829 /* non-time format, pretent the current time segment is closed with a
830 * 0 start and unknown stop time. */
832 format = GST_FORMAT_TIME;
838 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
840 gst_segment_set_newsegment_full (segment, update,
841 rate, arate, format, start, stop, time);
843 if (segment == &sq->sink_segment)
844 sq->sink_tainted = TRUE;
846 sq->src_tainted = TRUE;
848 GST_DEBUG_OBJECT (mq,
849 "queue %d, configured NEWSEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
851 /* segment can update the time level of the queue */
852 update_time_level (mq, sq);
854 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
857 /* take a buffer and update segment, updating the time level of the queue. */
859 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
860 GstClockTime duration, GstSegment * segment)
862 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
864 /* if no timestamp is set, assume it's continuous with the previous
866 if (timestamp == GST_CLOCK_TIME_NONE)
867 timestamp = segment->last_stop;
870 if (duration != GST_CLOCK_TIME_NONE)
871 timestamp += duration;
873 GST_DEBUG_OBJECT (mq, "queue %d, last_stop updated to %" GST_TIME_FORMAT,
874 sq->id, GST_TIME_ARGS (timestamp));
876 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
878 if (segment == &sq->sink_segment)
879 sq->sink_tainted = TRUE;
881 sq->src_tainted = TRUE;
883 /* calc diff with other end */
884 update_time_level (mq, sq);
885 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
889 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
890 GstMiniObject * object)
892 GstFlowReturn result = GST_FLOW_OK;
894 if (GST_IS_BUFFER (object)) {
896 GstClockTime timestamp, duration;
899 buffer = GST_BUFFER_CAST (object);
900 timestamp = GST_BUFFER_TIMESTAMP (buffer);
901 duration = GST_BUFFER_DURATION (buffer);
902 caps = GST_BUFFER_CAPS (buffer);
904 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
906 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
907 gst_data_queue_limits_changed (sq->queue);
909 GST_DEBUG_OBJECT (mq,
910 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
911 sq->id, buffer, GST_TIME_ARGS (timestamp));
913 /* Set caps on pad before pushing, this avoids core calling the acceptcaps
914 * function on the srcpad, which will call acceptcaps upstream, which might
915 * not accept these caps (anymore). */
916 if (caps && caps != GST_PAD_CAPS (sq->srcpad))
917 gst_pad_set_caps (sq->srcpad, caps);
919 result = gst_pad_push (sq->srcpad, buffer);
920 } else if (GST_IS_EVENT (object)) {
923 event = GST_EVENT_CAST (object);
925 switch (GST_EVENT_TYPE (event)) {
927 result = GST_FLOW_UNEXPECTED;
929 case GST_EVENT_NEWSEGMENT:
930 apply_segment (mq, sq, event, &sq->src_segment);
931 /* Applying the segment may have made the queue non-full again, unblock it if needed */
932 gst_data_queue_limits_changed (sq->queue);
938 GST_DEBUG_OBJECT (mq,
939 "SingleQueue %d : Pushing event %p of type %s",
940 sq->id, event, GST_EVENT_TYPE_NAME (event));
942 gst_pad_push_event (sq->srcpad, event);
944 g_warning ("Unexpected object in singlequeue %d (refcounting problem?)",
952 static GstMiniObject *
953 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
964 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
967 gst_mini_object_unref (item->object);
968 g_slice_free (GstMultiQueueItem, item);
971 /* takes ownership of passed mini object! */
972 static GstMultiQueueItem *
973 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
975 GstMultiQueueItem *item;
977 item = g_slice_new (GstMultiQueueItem);
978 item->object = object;
979 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
982 item->size = GST_BUFFER_SIZE (object);
983 item->duration = GST_BUFFER_DURATION (object);
984 if (item->duration == GST_CLOCK_TIME_NONE)
986 item->visible = TRUE;
990 static GstMultiQueueItem *
991 gst_multi_queue_event_item_new (GstMiniObject * object, guint32 curid)
993 GstMultiQueueItem *item;
995 item = g_slice_new (GstMultiQueueItem);
996 item->object = object;
997 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1002 item->visible = FALSE;
1006 /* Each main loop attempts to push buffers until the return value
1007 * is not-linked. not-linked pads are not allowed to push data beyond
1008 * any linked pads, so they don't 'rush ahead of the pack'.
1011 gst_multi_queue_loop (GstPad * pad)
1014 GstMultiQueueItem *item;
1015 GstDataQueueItem *sitem;
1017 GstMiniObject *object;
1019 GstFlowReturn result;
1021 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1024 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1026 /* Get something from the queue, blocking until that happens, or we get
1028 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1031 item = (GstMultiQueueItem *) sitem;
1032 newid = item->posid;
1034 /* steal the object and destroy the item */
1035 object = gst_multi_queue_item_steal_object (item);
1036 gst_multi_queue_item_destroy (item);
1038 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d", sq->id, newid);
1040 /* If we're not-linked, we do some extra work because we might need to
1041 * wait before pushing. If we're linked but there's a gap in the IDs,
1042 * or it's the first loop, or we just passed the previous highid,
1043 * we might need to wake some sleeping pad up, so there's extra work
1045 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1046 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1047 gst_flow_get_name (sq->srcresult));
1049 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1051 /* Update the nextid so other threads know when to wake us up */
1054 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1055 /* Go to sleep until it's time to push this buffer */
1057 /* Recompute the highid */
1058 compute_high_id (mq);
1059 while (newid > mq->highid && sq->srcresult == GST_FLOW_NOT_LINKED) {
1060 GST_DEBUG_OBJECT (mq, "queue %d sleeping for not-linked wakeup with "
1061 "newid %u and highid %u", sq->id, newid, mq->highid);
1064 /* Wake up all non-linked pads before we sleep */
1065 wake_up_next_non_linked (mq);
1068 g_cond_wait (sq->turn, mq->qlock);
1071 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1072 "wakeup with newid %u and highid %u", sq->id, newid, mq->highid);
1075 /* Re-compute the high_id in case someone else pushed */
1076 compute_high_id (mq);
1078 compute_high_id (mq);
1079 /* Wake up all non-linked pads */
1080 wake_up_next_non_linked (mq);
1082 /* We're done waiting, we can clear the nextid */
1085 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1088 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1089 gst_flow_get_name (sq->srcresult));
1091 /* Try to push out the new object */
1092 result = gst_single_queue_push_one (mq, sq, object);
1093 sq->srcresult = result;
1096 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1097 && result != GST_FLOW_UNEXPECTED)
1100 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1101 gst_flow_get_name (sq->srcresult));
1107 /* Need to make sure wake up any sleeping pads when we exit */
1108 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1109 compute_high_id (mq);
1110 wake_up_next_non_linked (mq);
1111 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1113 /* upstream needs to see fatal result ASAP to shut things down,
1114 * but might be stuck in one of our other full queues;
1115 * so empty this one and trigger dynamic queue growth. At
1116 * this point the srcresult is not OK, NOT_LINKED
1117 * or UNEXPECTED, i.e. a real failure */
1118 gst_data_queue_flush (sq->queue);
1119 single_queue_underrun_cb (sq->queue, sq);
1120 gst_data_queue_set_flushing (sq->queue, TRUE);
1121 gst_pad_pause_task (sq->srcpad);
1122 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1123 "SingleQueue[%d] task paused, reason:%s",
1124 sq->id, gst_flow_get_name (sq->srcresult));
1130 * gst_multi_queue_chain:
1132 * This is similar to GstQueue's chain function, except:
1133 * _ we don't have leak behavioures,
1134 * _ we push with a unique id (curid)
1136 static GstFlowReturn
1137 gst_multi_queue_chain (GstPad * pad, GstBuffer * buffer)
1141 GstMultiQueueItem *item;
1143 GstClockTime timestamp, duration;
1145 sq = gst_pad_get_element_private (pad);
1148 /* Get a unique incrementing id */
1149 curid = mq->counter++;
1151 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1152 sq->id, buffer, curid);
1154 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1156 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1157 duration = GST_BUFFER_DURATION (buffer);
1159 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1162 /* update time level, we must do this after pushing the data in the queue so
1163 * that we never end up filling the queue first. */
1164 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1167 return sq->srcresult;
1172 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1173 sq->id, gst_flow_get_name (sq->srcresult));
1174 gst_multi_queue_item_destroy (item);
1180 gst_multi_queue_sink_activate_push (GstPad * pad, gboolean active)
1184 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1187 /* All pads start off linked until they push one buffer */
1188 sq->srcresult = GST_FLOW_OK;
1190 sq->srcresult = GST_FLOW_WRONG_STATE;
1191 gst_data_queue_flush (sq->queue);
1197 gst_multi_queue_sink_event (GstPad * pad, GstEvent * event)
1202 GstMultiQueueItem *item;
1205 GstEvent *sref = NULL;
1207 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1208 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1210 type = GST_EVENT_TYPE (event);
1213 case GST_EVENT_FLUSH_START:
1214 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1217 res = gst_pad_push_event (sq->srcpad, event);
1219 gst_single_queue_flush (mq, sq, TRUE);
1222 case GST_EVENT_FLUSH_STOP:
1223 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1226 res = gst_pad_push_event (sq->srcpad, event);
1228 gst_single_queue_flush (mq, sq, FALSE);
1230 case GST_EVENT_NEWSEGMENT:
1231 /* take ref because the queue will take ownership and we need the event
1232 * afterwards to update the segment */
1233 sref = gst_event_ref (event);
1237 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1238 res = gst_pad_push_event (sq->srcpad, event);
1244 /* Get an unique incrementing id. protected with the STREAM_LOCK, unserialized
1245 * events already got pushed and don't end up in the queue. */
1246 curid = mq->counter++;
1248 item = gst_multi_queue_event_item_new ((GstMiniObject *) event, curid);
1250 GST_DEBUG_OBJECT (mq,
1251 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1252 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1254 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1257 /* mark EOS when we received one, we must do that after putting the
1258 * buffer in the queue because EOS marks the buffer as filled. No need to take
1259 * a lock, the _check_full happens from this thread only, right before pushing
1260 * into dataqueue. */
1264 /* EOS affects the buffering state */
1265 update_buffering (mq, sq);
1266 single_queue_overrun_cb (sq->queue, sq);
1268 case GST_EVENT_NEWSEGMENT:
1269 apply_segment (mq, sq, sref, &sq->sink_segment);
1270 gst_event_unref (sref);
1276 gst_object_unref (mq);
1281 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1282 sq->id, gst_flow_get_name (sq->srcresult));
1284 gst_event_unref (sref);
1285 gst_multi_queue_item_destroy (item);
1291 gst_multi_queue_getcaps (GstPad * pad)
1293 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1297 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1299 GST_LOG_OBJECT (otherpad, "Getting caps from the peer of this pad");
1301 result = gst_pad_peer_get_caps (otherpad);
1303 result = gst_caps_new_any ();
1309 gst_multi_queue_acceptcaps (GstPad * pad, GstCaps * caps)
1311 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1315 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1317 GST_LOG_OBJECT (otherpad, "Accept caps from the peer of this pad");
1319 result = gst_pad_peer_accept_caps (otherpad, caps);
1324 static GstFlowReturn
1325 gst_multi_queue_bufferalloc (GstPad * pad, guint64 offset, guint size,
1326 GstCaps * caps, GstBuffer ** buf)
1328 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1330 return gst_pad_alloc_buffer (sq->srcpad, offset, size, caps, buf);
1334 gst_multi_queue_src_activate_push (GstPad * pad, gboolean active)
1338 gboolean result = FALSE;
1340 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1343 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1346 result = gst_single_queue_flush (mq, sq, FALSE);
1348 result = gst_single_queue_flush (mq, sq, TRUE);
1349 /* make sure streaming finishes */
1350 result |= gst_pad_stop_task (pad);
1356 gst_multi_queue_src_event (GstPad * pad, GstEvent * event)
1358 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1360 return gst_pad_push_event (sq->sinkpad, event);
1364 gst_multi_queue_src_query (GstPad * pad, GstQuery * query)
1366 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1370 /* FIXME, Handle position offset depending on queue size */
1372 /* default handling */
1373 if (!(peerpad = gst_pad_get_peer (sq->sinkpad)))
1376 res = gst_pad_query (peerpad, query);
1378 gst_object_unref (peerpad);
1385 GST_LOG_OBJECT (sq->sinkpad, "Couldn't send query because we have no peer");
1391 * Next-non-linked functions
1394 /* WITH LOCK TAKEN */
1396 wake_up_next_non_linked (GstMultiQueue * mq)
1400 /* maybe no-one is waiting */
1401 if (mq->numwaiting < 1)
1404 /* Else figure out which singlequeue(s) need waking up */
1405 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1406 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1408 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1409 if (sq->nextid != 0 && sq->nextid <= mq->highid) {
1410 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1411 g_cond_signal (sq->turn);
1417 /* WITH LOCK TAKEN */
1419 compute_high_id (GstMultiQueue * mq)
1421 /* The high-id is either the highest id among the linked pads, or if all
1422 * pads are not-linked, it's the lowest not-linked pad */
1424 guint32 lowest = G_MAXUINT32;
1425 guint32 highid = G_MAXUINT32;
1427 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1428 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1430 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1431 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1433 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1434 /* No need to consider queues which are not waiting */
1435 if (sq->nextid == 0) {
1436 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1440 if (sq->nextid < lowest)
1441 lowest = sq->nextid;
1442 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1443 /* If we don't have a global highid, or the global highid is lower than
1444 * this single queue's last outputted id, store the queue's one,
1445 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1446 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1451 if (highid == G_MAXUINT32 || lowest < highid)
1452 mq->highid = lowest;
1454 mq->highid = highid;
1456 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1460 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
1461 ((q)->max_size.format) <= (value))
1464 * GstSingleQueue functions
1467 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1469 GstMultiQueue *mq = sq->mqueue;
1471 GstDataQueueSize size;
1472 gboolean filled = FALSE;
1474 gst_data_queue_get_level (sq->queue, &size);
1476 GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
1478 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1479 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1480 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1481 GstDataQueueSize ssize;
1483 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
1485 if (gst_data_queue_is_empty (oq->queue)) {
1486 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
1487 if (IS_FILLED (sq, visible, size.visible)) {
1488 sq->max_size.visible = size.visible + 1;
1489 GST_DEBUG_OBJECT (mq,
1490 "Another queue is empty, bumping single queue %d max visible to %d",
1491 sq->id, sq->max_size.visible);
1493 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1496 /* check if we reached the hard time/bytes limits */
1497 gst_data_queue_get_level (oq->queue, &ssize);
1499 GST_DEBUG_OBJECT (mq,
1500 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1501 G_GUINT64_FORMAT, oq->id, ssize.visible, oq->max_size.visible,
1502 ssize.bytes, oq->max_size.bytes, oq->cur_time, oq->max_size.time);
1504 /* if this queue is filled completely we must signal overrun.
1505 * FIXME, this seems wrong in many ways
1506 * - we're comparing the filled level of this queue against the
1507 * values of the other one
1508 * - we should only do this after we found no empty queues, ie, move
1509 * this check outside of the loop
1510 * - the debug statement talks about a different queue than the one
1511 * we are checking here.
1513 if (sq->is_eos || IS_FILLED (sq, bytes, ssize.bytes) ||
1514 IS_FILLED (sq, time, sq->cur_time)) {
1515 GST_LOG_OBJECT (mq, "Queue %d is filled", oq->id);
1519 /* no queues were empty */
1520 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1522 /* Overrun is always forwarded, since this is blocking the upstream element */
1524 GST_DEBUG_OBJECT (mq, "A queue is filled, signalling overrun");
1525 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1533 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1535 gboolean empty = TRUE;
1536 GstMultiQueue *mq = sq->mqueue;
1540 "Single Queue %d is empty, Checking other single queues", sq->id);
1542 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1543 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1544 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1546 if (gst_data_queue_is_full (oq->queue)) {
1547 GstDataQueueSize size;
1549 gst_data_queue_get_level (oq->queue, &size);
1550 if (IS_FILLED (oq, visible, size.visible)) {
1551 oq->max_size.visible = size.visible + 1;
1552 GST_DEBUG_OBJECT (mq,
1553 "queue %d is filled, bumping its max visible to %d", oq->id,
1554 oq->max_size.visible);
1555 gst_data_queue_limits_changed (oq->queue);
1558 if (!gst_data_queue_is_empty (oq->queue))
1561 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1564 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1565 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1570 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1571 guint64 time, GstSingleQueue * sq)
1574 GstMultiQueue *mq = sq->mqueue;
1576 GST_DEBUG_OBJECT (mq,
1577 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1578 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1579 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1581 /* we are always filled on EOS */
1585 /* we never go past the max visible items unless we are in buffering mode */
1586 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
1589 /* check time or bytes */
1590 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
1596 gst_single_queue_free (GstSingleQueue * sq)
1599 gst_data_queue_flush (sq->queue);
1600 g_object_unref (sq->queue);
1601 g_cond_free (sq->turn);
1605 static GstSingleQueue *
1606 gst_single_queue_new (GstMultiQueue * mqueue)
1611 sq = g_new0 (GstSingleQueue, 1);
1613 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1614 sq->id = mqueue->nbqueues++;
1616 /* copy over max_size and extra_size so we don't need to take the lock
1617 * any longer when checking if the queue is full. */
1618 sq->max_size.visible = mqueue->max_size.visible;
1619 sq->max_size.bytes = mqueue->max_size.bytes;
1620 sq->max_size.time = mqueue->max_size.time;
1622 sq->extra_size.visible = mqueue->extra_size.visible;
1623 sq->extra_size.bytes = mqueue->extra_size.bytes;
1624 sq->extra_size.time = mqueue->extra_size.time;
1626 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1628 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
1630 sq->mqueue = mqueue;
1631 sq->srcresult = GST_FLOW_WRONG_STATE;
1632 sq->queue = gst_data_queue_new_full ((GstDataQueueCheckFullFunction)
1633 single_queue_check_full,
1634 (GstDataQueueFullCallback) single_queue_overrun_cb,
1635 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
1637 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1638 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1642 sq->turn = g_cond_new ();
1644 sq->sinktime = GST_CLOCK_TIME_NONE;
1645 sq->srctime = GST_CLOCK_TIME_NONE;
1646 sq->sink_tainted = TRUE;
1647 sq->src_tainted = TRUE;
1649 tmp = g_strdup_printf ("sink%d", sq->id);
1650 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, tmp);
1653 gst_pad_set_chain_function (sq->sinkpad,
1654 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
1655 gst_pad_set_activatepush_function (sq->sinkpad,
1656 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_push));
1657 gst_pad_set_event_function (sq->sinkpad,
1658 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
1659 gst_pad_set_getcaps_function (sq->sinkpad,
1660 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1661 gst_pad_set_acceptcaps_function (sq->sinkpad,
1662 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1663 gst_pad_set_bufferalloc_function (sq->sinkpad,
1664 GST_DEBUG_FUNCPTR (gst_multi_queue_bufferalloc));
1665 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
1666 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1668 tmp = g_strdup_printf ("src%d", sq->id);
1669 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, tmp);
1672 gst_pad_set_activatepush_function (sq->srcpad,
1673 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_push));
1674 gst_pad_set_getcaps_function (sq->srcpad,
1675 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1676 gst_pad_set_acceptcaps_function (sq->srcpad,
1677 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1678 gst_pad_set_event_function (sq->srcpad,
1679 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
1680 gst_pad_set_query_function (sq->srcpad,
1681 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
1682 gst_pad_set_iterate_internal_links_function (sq->srcpad,
1683 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1685 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
1686 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
1688 /* only activate the pads when we are not in the NULL state
1689 * and add the pad under the state_lock to prevend state changes
1690 * between activating and adding */
1691 g_static_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
1692 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
1693 gst_pad_set_active (sq->srcpad, TRUE);
1694 gst_pad_set_active (sq->sinkpad, TRUE);
1696 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
1697 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
1698 g_static_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
1700 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",