2 * Copyright (C) 2006 Edward Hervey <edward@fluendo.com>
3 * Copyright (C) 2007 Jan Schmidt <jan@fluendo.com>
4 * Copyright (C) 2007 Wim Taymans <wim@fluendo.com>
5 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * SECTION:element-multiqueue
27 * @see_also: #GstQueue
31 * Multiqueue is similar to a normal #GstQueue with the following additional
35 * <itemizedlist><title>Multiple streamhandling</title>
37 * The element handles queueing data on more than one stream at once. To
38 * achieve such a feature it has request sink pads (sink%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"
115 #include <gst/glib-compat-private.h>
119 * @sinkpad: associated sink #GstPad
120 * @srcpad: associated source #GstPad
122 * Structure containing all information and properties about
125 typedef struct _GstSingleQueue GstSingleQueue;
127 struct _GstSingleQueue
129 /* unique identifier of the queue */
132 GstMultiQueue *mqueue;
137 /* flowreturn of previous srcpad push */
138 GstFlowReturn srcresult;
139 /* If something was actually pushed on
140 * this pad after flushing/pad activation
141 * and the srcresult corresponds to something
147 GstSegment sink_segment;
148 GstSegment src_segment;
150 /* position of src/sink */
151 GstClockTime sinktime, srctime;
152 /* TRUE if either position needs to be recalculated */
153 gboolean sink_tainted, src_tainted;
157 GstDataQueueSize max_size, extra_size;
158 GstClockTime cur_time;
162 /* Protected by global lock */
163 guint32 nextid; /* ID of the next object waiting to be pushed */
164 guint32 oldid; /* ID of the last object pushed (last in a series) */
165 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
166 GstClockTime next_time; /* End running time of next buffer to be pushed */
167 GstClockTime last_time; /* Start running time of last pushed buffer */
168 GCond turn; /* SingleQueue turn waiting conditional */
170 /* for serialized queries */
176 /* Extension of GstDataQueueItem structure for our usage */
177 typedef struct _GstMultiQueueItem GstMultiQueueItem;
179 struct _GstMultiQueueItem
181 GstMiniObject *object;
186 GDestroyNotify destroy;
192 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
193 static void gst_single_queue_free (GstSingleQueue * squeue);
195 static void wake_up_next_non_linked (GstMultiQueue * mq);
196 static void compute_high_id (GstMultiQueue * mq);
197 static void compute_high_time (GstMultiQueue * mq);
198 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
199 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
201 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
203 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
206 GST_STATIC_CAPS_ANY);
208 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
211 GST_STATIC_CAPS_ANY);
213 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
214 #define GST_CAT_DEFAULT (multi_queue_debug)
216 /* Signals and args */
224 /* default limits, we try to keep up to 2 seconds of data and if there is not
225 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
226 * there is data in the queues. Normally, the byte and time limits are not hit
227 * in theses conditions. */
228 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
229 #define DEFAULT_MAX_SIZE_BUFFERS 5
230 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
232 /* second limits. When we hit one of the above limits we are probably dealing
233 * with a badly muxed file and we scale the limits to these emergency values.
234 * This is currently not yet implemented.
235 * Since we dynamically scale the queue buffer size up to the limits but avoid
236 * going above the max-size-buffers when we can, we don't really need this
237 * aditional extra size. */
238 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
239 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
240 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
242 #define DEFAULT_USE_BUFFERING FALSE
243 #define DEFAULT_LOW_PERCENT 10
244 #define DEFAULT_HIGH_PERCENT 99
245 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
250 PROP_EXTRA_SIZE_BYTES,
251 PROP_EXTRA_SIZE_BUFFERS,
252 PROP_EXTRA_SIZE_TIME,
254 PROP_MAX_SIZE_BUFFERS,
259 PROP_SYNC_BY_RUNNING_TIME,
263 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
264 g_mutex_lock (&q->qlock); \
267 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
268 g_mutex_unlock (&q->qlock); \
271 static void gst_multi_queue_finalize (GObject * object);
272 static void gst_multi_queue_set_property (GObject * object,
273 guint prop_id, const GValue * value, GParamSpec * pspec);
274 static void gst_multi_queue_get_property (GObject * object,
275 guint prop_id, GValue * value, GParamSpec * pspec);
277 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
278 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
279 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
280 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
281 element, GstStateChange transition);
283 static void gst_multi_queue_loop (GstPad * pad);
286 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
287 #define gst_multi_queue_parent_class parent_class
288 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
291 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
294 gst_multi_queue_class_init (GstMultiQueueClass * klass)
296 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
297 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
299 gobject_class->set_property = gst_multi_queue_set_property;
300 gobject_class->get_property = gst_multi_queue_get_property;
305 * GstMultiQueue::underrun:
306 * @multiqueue: the multqueue instance
308 * This signal is emitted from the streaming thread when there is
309 * no data in any of the queues inside the multiqueue instance (underrun).
311 * This indicates either starvation or EOS from the upstream data sources.
313 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
314 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
315 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
316 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
319 * GstMultiQueue::overrun:
320 * @multiqueue: the multiqueue instance
322 * Reports that one of the queues in the multiqueue is full (overrun).
323 * A queue is full if the total amount of data inside it (num-buffers, time,
324 * size) is higher than the boundary values which can be set through the
325 * GObject properties.
327 * This can be used as an indicator of pre-roll.
329 gst_multi_queue_signals[SIGNAL_OVERRUN] =
330 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
331 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
332 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
336 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
337 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
338 "Max. amount of data in the queue (bytes, 0=disable)",
339 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
340 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
342 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
343 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
344 DEFAULT_MAX_SIZE_BUFFERS,
345 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
347 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
348 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
349 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
352 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
353 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
354 " (NOT IMPLEMENTED)",
355 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
358 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
359 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
360 " (NOT IMPLEMENTED)",
361 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
362 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
363 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
364 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
365 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
366 " (NOT IMPLEMENTED)",
367 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 * GstMultiQueue:use-buffering
373 * Enable the buffering option in multiqueue so that BUFFERING messages are
374 * emited based on low-/high-percent thresholds.
376 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
377 g_param_spec_boolean ("use-buffering", "Use buffering",
378 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
379 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 * GstMultiQueue:low-percent
383 * Low threshold percent for buffering to start.
385 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
386 g_param_spec_int ("low-percent", "Low percent",
387 "Low threshold for buffering to start", 0, 100,
388 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 * GstMultiQueue:high-percent
392 * High threshold percent for buffering to finish.
394 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
395 g_param_spec_int ("high-percent", "High percent",
396 "High threshold for buffering to finish", 0, 100,
397 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 * GstMultiQueue:sync-by-running-time
402 * If enabled multiqueue will synchronize deactivated or not-linked streams
403 * to the activated and linked streams by taking the running time.
404 * Otherwise multiqueue will synchronize the deactivated or not-linked
405 * streams by keeping the order in which buffers and events arrived compared
406 * to active and linked streams.
408 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
409 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
410 "Synchronize deactivated or not-linked streams by running time",
411 DEFAULT_SYNC_BY_RUNNING_TIME,
412 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 gobject_class->finalize = gst_multi_queue_finalize;
416 gst_element_class_set_static_metadata (gstelement_class,
418 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
419 gst_element_class_add_pad_template (gstelement_class,
420 gst_static_pad_template_get (&sinktemplate));
421 gst_element_class_add_pad_template (gstelement_class,
422 gst_static_pad_template_get (&srctemplate));
424 gstelement_class->request_new_pad =
425 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
426 gstelement_class->release_pad =
427 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
428 gstelement_class->change_state =
429 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
433 gst_multi_queue_init (GstMultiQueue * mqueue)
435 mqueue->nbqueues = 0;
436 mqueue->queues = NULL;
438 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
439 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
440 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
442 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
443 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
444 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
446 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
447 mqueue->low_percent = DEFAULT_LOW_PERCENT;
448 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
450 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
454 mqueue->high_time = GST_CLOCK_TIME_NONE;
456 g_mutex_init (&mqueue->qlock);
460 gst_multi_queue_finalize (GObject * object)
462 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
464 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
465 g_list_free (mqueue->queues);
466 mqueue->queues = NULL;
467 mqueue->queues_cookie++;
469 /* free/unref instance data */
470 g_mutex_clear (&mqueue->qlock);
472 G_OBJECT_CLASS (parent_class)->finalize (object);
475 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
476 GList * tmp = mq->queues; \
478 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
479 q->max_size.format = mq->max_size.format; \
480 tmp = g_list_next(tmp); \
485 gst_multi_queue_set_property (GObject * object, guint prop_id,
486 const GValue * value, GParamSpec * pspec)
488 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
491 case PROP_MAX_SIZE_BYTES:
492 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
493 mq->max_size.bytes = g_value_get_uint (value);
494 SET_CHILD_PROPERTY (mq, bytes);
495 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
497 case PROP_MAX_SIZE_BUFFERS:
498 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
499 mq->max_size.visible = g_value_get_uint (value);
500 SET_CHILD_PROPERTY (mq, visible);
501 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
503 case PROP_MAX_SIZE_TIME:
504 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
505 mq->max_size.time = g_value_get_uint64 (value);
506 SET_CHILD_PROPERTY (mq, time);
507 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
509 case PROP_EXTRA_SIZE_BYTES:
510 mq->extra_size.bytes = g_value_get_uint (value);
512 case PROP_EXTRA_SIZE_BUFFERS:
513 mq->extra_size.visible = g_value_get_uint (value);
515 case PROP_EXTRA_SIZE_TIME:
516 mq->extra_size.time = g_value_get_uint64 (value);
518 case PROP_USE_BUFFERING:
519 mq->use_buffering = g_value_get_boolean (value);
521 case PROP_LOW_PERCENT:
522 mq->low_percent = g_value_get_int (value);
524 case PROP_HIGH_PERCENT:
525 mq->high_percent = g_value_get_int (value);
527 case PROP_SYNC_BY_RUNNING_TIME:
528 mq->sync_by_running_time = g_value_get_boolean (value);
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537 gst_multi_queue_get_property (GObject * object, guint prop_id,
538 GValue * value, GParamSpec * pspec)
540 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
542 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
545 case PROP_EXTRA_SIZE_BYTES:
546 g_value_set_uint (value, mq->extra_size.bytes);
548 case PROP_EXTRA_SIZE_BUFFERS:
549 g_value_set_uint (value, mq->extra_size.visible);
551 case PROP_EXTRA_SIZE_TIME:
552 g_value_set_uint64 (value, mq->extra_size.time);
554 case PROP_MAX_SIZE_BYTES:
555 g_value_set_uint (value, mq->max_size.bytes);
557 case PROP_MAX_SIZE_BUFFERS:
558 g_value_set_uint (value, mq->max_size.visible);
560 case PROP_MAX_SIZE_TIME:
561 g_value_set_uint64 (value, mq->max_size.time);
563 case PROP_USE_BUFFERING:
564 g_value_set_boolean (value, mq->use_buffering);
566 case PROP_LOW_PERCENT:
567 g_value_set_int (value, mq->low_percent);
569 case PROP_HIGH_PERCENT:
570 g_value_set_int (value, mq->high_percent);
572 case PROP_SYNC_BY_RUNNING_TIME:
573 g_value_set_boolean (value, mq->sync_by_running_time);
576 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
580 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
584 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
586 GstIterator *it = NULL;
588 GstSingleQueue *squeue;
589 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
592 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
593 squeue = gst_pad_get_element_private (pad);
597 if (squeue->sinkpad == pad)
598 opad = gst_object_ref (squeue->srcpad);
599 else if (squeue->srcpad == pad)
600 opad = gst_object_ref (squeue->sinkpad);
604 g_value_init (&val, GST_TYPE_PAD);
605 g_value_set_object (&val, opad);
606 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
607 g_value_unset (&val);
609 gst_object_unref (opad);
612 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
623 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
624 const gchar * name, const GstCaps * caps)
626 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
627 GstSingleQueue *squeue;
631 sscanf (name + 4, "_%u", &temp_id);
632 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
635 /* Create a new single queue, add the sink and source pad and return the sink pad */
636 squeue = gst_single_queue_new (mqueue, temp_id);
638 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
639 GST_DEBUG_PAD_NAME (squeue->sinkpad));
641 return squeue ? squeue->sinkpad : NULL;
645 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
647 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
648 GstSingleQueue *sq = NULL;
651 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
653 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
654 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
655 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
656 sq = (GstSingleQueue *) tmp->data;
658 if (sq->sinkpad == pad)
663 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
664 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
668 /* FIXME: The removal of the singlequeue should probably not happen until it
669 * finishes draining */
671 /* remove it from the list */
672 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
673 mqueue->queues_cookie++;
675 /* FIXME : recompute next-non-linked */
676 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
678 /* delete SingleQueue */
679 gst_data_queue_set_flushing (sq->queue, TRUE);
681 gst_pad_set_active (sq->srcpad, FALSE);
682 gst_pad_set_active (sq->sinkpad, FALSE);
683 gst_pad_set_element_private (sq->srcpad, NULL);
684 gst_pad_set_element_private (sq->sinkpad, NULL);
685 gst_element_remove_pad (element, sq->srcpad);
686 gst_element_remove_pad (element, sq->sinkpad);
687 gst_single_queue_free (sq);
690 static GstStateChangeReturn
691 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
693 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
694 GstSingleQueue *sq = NULL;
695 GstStateChangeReturn result;
697 switch (transition) {
698 case GST_STATE_CHANGE_READY_TO_PAUSED:{
701 /* Set all pads to non-flushing */
702 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
703 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
704 sq = (GstSingleQueue *) tmp->data;
705 sq->flushing = FALSE;
707 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
710 case GST_STATE_CHANGE_PAUSED_TO_READY:{
713 /* Un-wait all waiting pads */
714 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
715 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
716 sq = (GstSingleQueue *) tmp->data;
718 g_cond_signal (&sq->turn);
720 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
727 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
729 switch (transition) {
738 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
743 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
747 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
748 sq->srcresult = GST_FLOW_FLUSHING;
749 gst_data_queue_set_flushing (sq->queue, TRUE);
753 /* wake up non-linked task */
754 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
756 g_cond_signal (&sq->turn);
757 sq->last_query = FALSE;
758 g_cond_signal (&sq->query_handled);
759 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
761 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
762 result = gst_pad_pause_task (sq->srcpad);
763 sq->sink_tainted = sq->src_tainted = TRUE;
765 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
766 gst_single_queue_flush_queue (sq, full);
767 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
768 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
769 /* All pads start off not-linked for a smooth kick-off */
770 sq->srcresult = GST_FLOW_OK;
773 sq->max_size.visible = mq->max_size.visible;
777 sq->last_oldid = G_MAXUINT32;
778 sq->next_time = GST_CLOCK_TIME_NONE;
779 sq->last_time = GST_CLOCK_TIME_NONE;
780 gst_data_queue_set_flushing (sq->queue, FALSE);
782 /* Reset high time to be recomputed next */
783 mq->high_time = GST_CLOCK_TIME_NONE;
785 sq->flushing = FALSE;
786 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
788 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
790 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
797 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
799 GstDataQueueSize size;
801 gboolean post = FALSE;
803 /* nothing to dowhen we are not in buffering mode */
804 if (!mq->use_buffering)
807 gst_data_queue_get_level (sq->queue, &size);
809 GST_DEBUG_OBJECT (mq,
810 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
811 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
812 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
814 /* get bytes and time percentages and take the max */
819 if (sq->max_size.time > 0) {
820 tmp = (sq->cur_time * 100) / sq->max_size.time;
821 percent = MAX (percent, tmp);
823 if (sq->max_size.bytes > 0) {
824 tmp = (size.bytes * 100) / sq->max_size.bytes;
825 percent = MAX (percent, tmp);
831 if (percent >= mq->high_percent) {
832 mq->buffering = FALSE;
834 /* make sure it increases */
835 percent = MAX (mq->percent, percent);
837 if (percent == mq->percent)
838 /* don't post if nothing changed */
841 /* else keep last value we posted */
842 mq->percent = percent;
844 if (percent < mq->low_percent) {
845 mq->buffering = TRUE;
846 mq->percent = percent;
853 /* scale to high percent so that it becomes the 100% mark */
854 percent = percent * 100 / mq->high_percent;
859 GST_DEBUG_OBJECT (mq, "buffering %d percent", percent);
860 message = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
862 gst_element_post_message (GST_ELEMENT_CAST (mq), message);
864 GST_DEBUG_OBJECT (mq, "filled %d percent", percent);
868 /* calculate the diff between running time on the sink and src of the queue.
869 * This is the total amount of time in the queue.
872 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
874 gint64 sink_time, src_time;
876 if (sq->sink_tainted) {
877 sink_time = sq->sinktime =
878 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
879 sq->sink_segment.position);
881 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
882 /* if we have a time, we become untainted and use the time */
883 sq->sink_tainted = FALSE;
885 sink_time = sq->sinktime;
887 if (sq->src_tainted) {
888 src_time = sq->srctime =
889 gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
890 sq->src_segment.position);
891 /* if we have a time, we become untainted and use the time */
892 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
893 sq->src_tainted = FALSE;
895 src_time = sq->srctime;
897 GST_DEBUG_OBJECT (mq,
898 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
899 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
901 /* This allows for streams with out of order timestamping - sometimes the
902 * emerging timestamp is later than the arriving one(s) */
903 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
904 sq->cur_time = sink_time - src_time;
908 /* updating the time level can change the buffering state */
909 update_buffering (mq, sq);
914 /* take a SEGMENT event and apply the values to segment, updating the time
917 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
918 GstSegment * segment)
920 gst_event_copy_segment (event, segment);
922 /* now configure the values, we use these to track timestamps on the
924 if (segment->format != GST_FORMAT_TIME) {
925 /* non-time format, pretent the current time segment is closed with a
926 * 0 start and unknown stop time. */
927 segment->format = GST_FORMAT_TIME;
932 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
934 if (segment == &sq->sink_segment)
935 sq->sink_tainted = TRUE;
937 sq->src_tainted = TRUE;
939 GST_DEBUG_OBJECT (mq,
940 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
942 /* segment can update the time level of the queue */
943 update_time_level (mq, sq);
945 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
948 /* take a buffer and update segment, updating the time level of the queue. */
950 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
951 GstClockTime duration, GstSegment * segment)
953 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
955 /* if no timestamp is set, assume it's continuous with the previous
957 if (timestamp == GST_CLOCK_TIME_NONE)
958 timestamp = segment->position;
961 if (duration != GST_CLOCK_TIME_NONE)
962 timestamp += duration;
964 GST_DEBUG_OBJECT (mq, "queue %d, position updated to %" GST_TIME_FORMAT,
965 sq->id, GST_TIME_ARGS (timestamp));
967 segment->position = timestamp;
969 if (segment == &sq->sink_segment)
970 sq->sink_tainted = TRUE;
972 sq->src_tainted = TRUE;
974 /* calc diff with other end */
975 update_time_level (mq, sq);
976 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
980 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
982 GstClockTime time = GST_CLOCK_TIME_NONE;
984 if (GST_IS_BUFFER (object)) {
985 GstBuffer *buf = GST_BUFFER_CAST (object);
987 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
988 time = GST_BUFFER_TIMESTAMP (buf);
989 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
990 time += GST_BUFFER_DURATION (buf);
991 if (time > segment->stop)
992 time = segment->stop;
993 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
995 } else if (GST_IS_BUFFER_LIST (object)) {
996 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1000 n = gst_buffer_list_length (list);
1001 for (i = 0; i < n; i++) {
1002 buf = gst_buffer_list_get (list, i);
1003 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1004 time = GST_BUFFER_TIMESTAMP (buf);
1005 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1006 time += GST_BUFFER_DURATION (buf);
1007 if (time > segment->stop)
1008 time = segment->stop;
1009 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1016 } else if (GST_IS_EVENT (object)) {
1017 GstEvent *event = GST_EVENT_CAST (object);
1019 /* For newsegment events return the running time of the start position */
1020 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1021 const GstSegment *new_segment;
1023 gst_event_parse_segment (event, &new_segment);
1024 if (new_segment->format == GST_FORMAT_TIME) {
1026 gst_segment_to_running_time (new_segment, GST_FORMAT_TIME,
1027 new_segment->start);
1036 static GstFlowReturn
1037 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1038 GstMiniObject * object)
1040 GstFlowReturn result = GST_FLOW_OK;
1042 if (GST_IS_BUFFER (object)) {
1044 GstClockTime timestamp, duration;
1046 buffer = GST_BUFFER_CAST (object);
1047 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1048 duration = GST_BUFFER_DURATION (buffer);
1050 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1052 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1053 gst_data_queue_limits_changed (sq->queue);
1055 GST_DEBUG_OBJECT (mq,
1056 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1057 sq->id, buffer, GST_TIME_ARGS (timestamp));
1059 result = gst_pad_push (sq->srcpad, buffer);
1060 } else if (GST_IS_EVENT (object)) {
1063 event = GST_EVENT_CAST (object);
1065 switch (GST_EVENT_TYPE (event)) {
1067 result = GST_FLOW_EOS;
1069 case GST_EVENT_SEGMENT:
1070 apply_segment (mq, sq, event, &sq->src_segment);
1071 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1072 gst_data_queue_limits_changed (sq->queue);
1078 GST_DEBUG_OBJECT (mq,
1079 "SingleQueue %d : Pushing event %p of type %s",
1080 sq->id, event, GST_EVENT_TYPE_NAME (event));
1082 gst_pad_push_event (sq->srcpad, event);
1083 } else if (GST_IS_QUERY (object)) {
1087 query = GST_QUERY_CAST (object);
1089 res = gst_pad_peer_query (sq->srcpad, query);
1091 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1092 sq->last_query = res;
1093 g_cond_signal (&sq->query_handled);
1094 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1096 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1104 static GstMiniObject *
1105 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1110 item->object = NULL;
1116 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1118 if (!item->is_query && item->object)
1119 gst_mini_object_unref (item->object);
1120 g_slice_free (GstMultiQueueItem, item);
1123 /* takes ownership of passed mini object! */
1124 static GstMultiQueueItem *
1125 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1127 GstMultiQueueItem *item;
1129 item = g_slice_new (GstMultiQueueItem);
1130 item->object = object;
1131 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1132 item->posid = curid;
1133 item->is_query = GST_IS_QUERY (object);
1135 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1136 item->duration = GST_BUFFER_DURATION (object);
1137 if (item->duration == GST_CLOCK_TIME_NONE)
1139 item->visible = TRUE;
1143 static GstMultiQueueItem *
1144 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1146 GstMultiQueueItem *item;
1148 item = g_slice_new (GstMultiQueueItem);
1149 item->object = object;
1150 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1151 item->posid = curid;
1152 item->is_query = GST_IS_QUERY (object);
1156 item->visible = FALSE;
1160 /* Each main loop attempts to push buffers until the return value
1161 * is not-linked. not-linked pads are not allowed to push data beyond
1162 * any linked pads, so they don't 'rush ahead of the pack'.
1165 gst_multi_queue_loop (GstPad * pad)
1168 GstMultiQueueItem *item;
1169 GstDataQueueItem *sitem;
1171 GstMiniObject *object = NULL;
1173 GstFlowReturn result;
1174 GstClockTime next_time;
1177 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1180 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1185 /* Get something from the queue, blocking until that happens, or we get
1187 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1190 item = (GstMultiQueueItem *) sitem;
1191 newid = item->posid;
1193 /* steal the object and destroy the item */
1194 object = gst_multi_queue_item_steal_object (item);
1195 gst_multi_queue_item_destroy (item);
1197 is_buffer = GST_IS_BUFFER (object);
1199 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1200 next_time = get_running_time (&sq->src_segment, object, TRUE);
1202 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1203 sq->id, newid, sq->last_oldid);
1205 /* If we're not-linked, we do some extra work because we might need to
1206 * wait before pushing. If we're linked but there's a gap in the IDs,
1207 * or it's the first loop, or we just passed the previous highid,
1208 * we might need to wake some sleeping pad up, so there's extra work
1210 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1211 if (sq->srcresult == GST_FLOW_NOT_LINKED
1212 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1213 || sq->last_oldid > mq->highid) {
1214 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1215 gst_flow_get_name (sq->srcresult));
1217 /* Check again if we're flushing after the lock is taken,
1218 * the flush flag might have been changed in the meantime */
1220 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1224 /* Update the nextid so other threads know when to wake us up */
1226 sq->next_time = next_time;
1228 /* Update the oldid (the last ID we output) for highid tracking */
1229 if (sq->last_oldid != G_MAXUINT32)
1230 sq->oldid = sq->last_oldid;
1232 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1233 /* Go to sleep until it's time to push this buffer */
1235 /* Recompute the highid */
1236 compute_high_id (mq);
1237 /* Recompute the high time */
1238 compute_high_time (mq);
1240 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1241 (mq->high_time == GST_CLOCK_TIME_NONE
1242 || next_time >= mq->high_time))
1243 || (!mq->sync_by_running_time && newid > mq->highid))
1244 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1246 GST_DEBUG_OBJECT (mq,
1247 "queue %d sleeping for not-linked wakeup with "
1248 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1249 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1250 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1252 /* Wake up all non-linked pads before we sleep */
1253 wake_up_next_non_linked (mq);
1256 g_cond_wait (&sq->turn, &mq->qlock);
1260 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1264 /* Recompute the high time */
1265 compute_high_time (mq);
1267 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1268 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1269 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1270 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1273 /* Re-compute the high_id in case someone else pushed */
1274 compute_high_id (mq);
1276 compute_high_id (mq);
1277 /* Wake up all non-linked pads */
1278 wake_up_next_non_linked (mq);
1280 /* We're done waiting, we can clear the nextid and nexttime */
1282 sq->next_time = GST_CLOCK_TIME_NONE;
1284 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1289 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1290 gst_flow_get_name (sq->srcresult));
1292 /* Update time stats */
1293 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1294 next_time = get_running_time (&sq->src_segment, object, FALSE);
1295 if (next_time != GST_CLOCK_TIME_NONE) {
1296 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1297 sq->last_time = next_time;
1298 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1299 /* Wake up all non-linked pads now that we advanced the high time */
1300 mq->high_time = next_time;
1301 wake_up_next_non_linked (mq);
1304 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1306 /* Try to push out the new object */
1307 result = gst_single_queue_push_one (mq, sq, object);
1310 /* Check if we pushed something already and if this is
1311 * now a switch from an active to a non-active stream.
1313 * If it is, we reset all the waiting streams, let them
1314 * push another buffer to see if they're now active again.
1315 * This allows faster switching between streams and prevents
1316 * deadlocks if downstream does any waiting too.
1318 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1319 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1320 && result == GST_FLOW_NOT_LINKED) {
1323 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1326 compute_high_id (mq);
1328 /* maybe no-one is waiting */
1329 if (mq->numwaiting > 0) {
1330 /* Else figure out which singlequeue(s) need waking up */
1331 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1332 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1334 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1335 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1336 sq2->pushed = FALSE;
1337 sq2->srcresult = GST_FLOW_OK;
1338 g_cond_signal (&sq2->turn);
1346 sq->srcresult = result;
1347 sq->last_oldid = newid;
1348 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1350 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1351 && result != GST_FLOW_EOS)
1354 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1355 gst_flow_get_name (sq->srcresult));
1362 gst_mini_object_unref (object);
1364 /* Need to make sure wake up any sleeping pads when we exit */
1365 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1366 compute_high_time (mq);
1367 compute_high_id (mq);
1368 wake_up_next_non_linked (mq);
1369 sq->last_query = FALSE;
1370 g_cond_signal (&sq->query_handled);
1371 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1373 /* upstream needs to see fatal result ASAP to shut things down,
1374 * but might be stuck in one of our other full queues;
1375 * so empty this one and trigger dynamic queue growth. At
1376 * this point the srcresult is not OK, NOT_LINKED
1377 * or EOS, i.e. a real failure */
1378 gst_single_queue_flush_queue (sq, FALSE);
1379 single_queue_underrun_cb (sq->queue, sq);
1380 gst_data_queue_set_flushing (sq->queue, TRUE);
1381 gst_pad_pause_task (sq->srcpad);
1382 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1383 "SingleQueue[%d] task paused, reason:%s",
1384 sq->id, gst_flow_get_name (sq->srcresult));
1390 * gst_multi_queue_chain:
1392 * This is similar to GstQueue's chain function, except:
1393 * _ we don't have leak behaviours,
1394 * _ we push with a unique id (curid)
1396 static GstFlowReturn
1397 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1401 GstMultiQueueItem *item;
1403 GstClockTime timestamp, duration;
1405 sq = gst_pad_get_element_private (pad);
1408 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1412 /* Get a unique incrementing id */
1413 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1415 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1416 sq->id, buffer, curid);
1418 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1420 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1421 duration = GST_BUFFER_DURATION (buffer);
1423 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1426 /* update time level, we must do this after pushing the data in the queue so
1427 * that we never end up filling the queue first. */
1428 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1431 return sq->srcresult;
1436 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1437 sq->id, gst_flow_get_name (sq->srcresult));
1438 gst_multi_queue_item_destroy (item);
1443 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
1444 gst_buffer_unref (buffer);
1445 return GST_FLOW_EOS;
1450 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
1451 GstPadMode mode, gboolean active)
1457 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1458 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1460 /* mq is NULL if the pad is activated/deactivated before being
1461 * added to the multiqueue */
1463 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1466 case GST_PAD_MODE_PUSH:
1468 /* All pads start off linked until they push one buffer */
1469 sq->srcresult = GST_FLOW_OK;
1471 gst_data_queue_set_flushing (sq->queue, FALSE);
1473 sq->srcresult = GST_FLOW_FLUSHING;
1474 sq->last_query = FALSE;
1475 g_cond_signal (&sq->query_handled);
1476 gst_data_queue_set_flushing (sq->queue, TRUE);
1477 gst_data_queue_flush (sq->queue);
1487 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1488 gst_object_unref (mq);
1495 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1500 GstMultiQueueItem *item;
1503 GstEvent *sref = NULL;
1505 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1506 mq = (GstMultiQueue *) parent;
1508 type = GST_EVENT_TYPE (event);
1511 case GST_EVENT_FLUSH_START:
1512 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1515 res = gst_pad_push_event (sq->srcpad, event);
1517 gst_single_queue_flush (mq, sq, TRUE, FALSE);
1520 case GST_EVENT_FLUSH_STOP:
1521 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1524 res = gst_pad_push_event (sq->srcpad, event);
1526 gst_single_queue_flush (mq, sq, FALSE, FALSE);
1528 case GST_EVENT_SEGMENT:
1529 /* take ref because the queue will take ownership and we need the event
1530 * afterwards to update the segment */
1531 sref = gst_event_ref (event);
1535 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1536 res = gst_pad_push_event (sq->srcpad, event);
1542 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1546 /* Get an unique incrementing id. */
1547 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1549 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
1551 GST_DEBUG_OBJECT (mq,
1552 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1553 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1555 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1558 /* mark EOS when we received one, we must do that after putting the
1559 * buffer in the queue because EOS marks the buffer as filled. No need to take
1560 * a lock, the _check_full happens from this thread only, right before pushing
1561 * into dataqueue. */
1565 /* EOS affects the buffering state */
1566 update_buffering (mq, sq);
1567 single_queue_overrun_cb (sq->queue, sq);
1569 case GST_EVENT_SEGMENT:
1570 apply_segment (mq, sq, sref, &sq->sink_segment);
1571 gst_event_unref (sref);
1581 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1582 sq->id, gst_flow_get_name (sq->srcresult));
1584 gst_event_unref (sref);
1585 gst_multi_queue_item_destroy (item);
1590 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1591 gst_event_unref (event);
1598 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1604 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1605 mq = (GstMultiQueue *) parent;
1607 switch (GST_QUERY_TYPE (query)) {
1609 if (GST_QUERY_IS_SERIALIZED (query)) {
1611 GstMultiQueueItem *item;
1613 /* Get an unique incrementing id. */
1614 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1616 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
1618 GST_DEBUG_OBJECT (mq,
1619 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
1620 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
1622 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1623 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
1624 g_cond_wait (&sq->query_handled, &mq->qlock);
1625 res = sq->last_query;
1626 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1628 /* default handling */
1629 res = gst_pad_query_default (pad, parent, query);
1637 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
1638 GstPadMode mode, gboolean active)
1644 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1647 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1650 case GST_PAD_MODE_PUSH:
1652 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
1654 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
1655 /* make sure streaming finishes */
1656 result |= gst_pad_stop_task (pad);
1667 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1669 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1670 GstMultiQueue *mq = sq->mqueue;
1673 switch (GST_EVENT_TYPE (event)) {
1674 case GST_EVENT_RECONFIGURE:
1675 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1676 if (sq->srcresult == GST_FLOW_NOT_LINKED)
1677 sq->srcresult = GST_FLOW_OK;
1678 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1680 ret = gst_pad_push_event (sq->sinkpad, event);
1683 ret = gst_pad_push_event (sq->sinkpad, event);
1691 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1695 /* FIXME, Handle position offset depending on queue size */
1696 switch (GST_QUERY_TYPE (query)) {
1698 /* default handling */
1699 res = gst_pad_query_default (pad, parent, query);
1706 * Next-non-linked functions
1709 /* WITH LOCK TAKEN */
1711 wake_up_next_non_linked (GstMultiQueue * mq)
1715 /* maybe no-one is waiting */
1716 if (mq->numwaiting < 1)
1719 /* Else figure out which singlequeue(s) need waking up */
1720 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1721 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1723 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1724 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
1725 && sq->next_time != GST_CLOCK_TIME_NONE
1726 && sq->next_time >= mq->high_time)
1727 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
1728 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1729 g_cond_signal (&sq->turn);
1735 /* WITH LOCK TAKEN */
1737 compute_high_id (GstMultiQueue * mq)
1739 /* The high-id is either the highest id among the linked pads, or if all
1740 * pads are not-linked, it's the lowest not-linked pad */
1742 guint32 lowest = G_MAXUINT32;
1743 guint32 highid = G_MAXUINT32;
1745 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1746 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1748 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1749 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1751 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1752 /* No need to consider queues which are not waiting */
1753 if (sq->nextid == 0) {
1754 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1758 if (sq->nextid < lowest)
1759 lowest = sq->nextid;
1760 } else if (sq->srcresult != GST_FLOW_EOS) {
1761 /* If we don't have a global highid, or the global highid is lower than
1762 * this single queue's last outputted id, store the queue's one,
1763 * unless the singlequeue is at EOS (srcresult = EOS) */
1764 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1769 if (highid == G_MAXUINT32 || lowest < highid)
1770 mq->highid = lowest;
1772 mq->highid = highid;
1774 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1778 /* WITH LOCK TAKEN */
1780 compute_high_time (GstMultiQueue * mq)
1782 /* The high-id is either the highest id among the linked pads, or if all
1783 * pads are not-linked, it's the lowest not-linked pad */
1785 GstClockTime highest = GST_CLOCK_TIME_NONE;
1786 GstClockTime lowest = GST_CLOCK_TIME_NONE;
1788 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1789 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1792 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
1793 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
1794 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
1796 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1797 /* No need to consider queues which are not waiting */
1798 if (sq->next_time == GST_CLOCK_TIME_NONE) {
1799 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1803 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
1804 lowest = sq->next_time;
1805 } else if (sq->srcresult != GST_FLOW_EOS) {
1806 /* If we don't have a global highid, or the global highid is lower than
1807 * this single queue's last outputted id, store the queue's one,
1808 * unless the singlequeue is at EOS (srcresult = EOS) */
1809 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
1810 highest = sq->last_time;
1814 mq->high_time = highest;
1817 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
1818 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
1821 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
1822 ((q)->max_size.format) <= (value))
1825 * GstSingleQueue functions
1828 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1830 GstMultiQueue *mq = sq->mqueue;
1832 GstDataQueueSize size;
1833 gboolean filled = TRUE;
1835 gst_data_queue_get_level (sq->queue, &size);
1838 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
1839 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
1840 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
1843 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1845 /* check if we reached the hard time/bytes limits */
1846 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
1847 IS_FILLED (sq, time, sq->cur_time)) {
1851 /* if hard limits are not reached then we allow one more buffer in the full
1852 * queue, but only if any of the other singelqueues are empty */
1853 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1854 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1859 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
1860 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
1864 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
1866 if (gst_data_queue_is_empty (oq->queue)) {
1867 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
1868 if (IS_FILLED (sq, visible, size.visible)) {
1869 sq->max_size.visible = size.visible + 1;
1870 GST_DEBUG_OBJECT (mq,
1871 "Queue %d is empty, bumping single queue %d max visible to %d",
1872 oq->id, sq->id, sq->max_size.visible);
1880 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1882 /* Overrun is always forwarded, since this is blocking the upstream element */
1884 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
1885 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1890 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1892 gboolean empty = TRUE;
1893 GstMultiQueue *mq = sq->mqueue;
1896 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1897 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
1901 "Single Queue %d is empty, Checking other single queues", sq->id);
1904 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1905 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1906 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1908 if (gst_data_queue_is_full (oq->queue)) {
1909 GstDataQueueSize size;
1911 gst_data_queue_get_level (oq->queue, &size);
1912 if (IS_FILLED (oq, visible, size.visible)) {
1913 oq->max_size.visible = size.visible + 1;
1914 GST_DEBUG_OBJECT (mq,
1915 "queue %d is filled, bumping its max visible to %d", oq->id,
1916 oq->max_size.visible);
1917 gst_data_queue_limits_changed (oq->queue);
1920 if (!gst_data_queue_is_empty (oq->queue))
1923 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1926 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1927 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1932 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1933 guint64 time, GstSingleQueue * sq)
1936 GstMultiQueue *mq = sq->mqueue;
1938 GST_DEBUG_OBJECT (mq,
1939 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1940 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1941 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1943 /* we are always filled on EOS */
1947 /* we never go past the max visible items unless we are in buffering mode */
1948 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
1951 /* check time or bytes */
1952 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
1958 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
1960 GstDataQueueItem *sitem;
1961 gboolean was_flushing = FALSE;
1963 while (!gst_data_queue_is_empty (sq->queue)) {
1964 GstMiniObject *data;
1966 /* FIXME: If this fails here although the queue is not empty,
1967 * we're flushing... but we want to rescue all sticky
1968 * events nonetheless.
1970 if (!gst_data_queue_pop (sq->queue, &sitem)) {
1971 was_flushing = TRUE;
1972 gst_data_queue_set_flushing (sq->queue, FALSE);
1976 data = sitem->object;
1978 if (!full && GST_IS_EVENT (data) && GST_EVENT_IS_STICKY (data) &&
1979 GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
1980 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
1981 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
1984 sitem->destroy (sitem);
1987 gst_data_queue_flush (sq->queue);
1989 gst_data_queue_set_flushing (sq->queue, TRUE);
1993 gst_single_queue_free (GstSingleQueue * sq)
1996 gst_data_queue_flush (sq->queue);
1997 g_object_unref (sq->queue);
1998 g_cond_clear (&sq->turn);
1999 g_cond_clear (&sq->query_handled);
2003 static GstSingleQueue *
2004 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2009 guint temp_id = (id == -1) ? 0 : id;
2011 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2013 /* Find an unused queue ID, if possible the passed one */
2014 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2015 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2016 /* This works because the IDs are sorted in ascending order */
2017 if (sq2->id == temp_id) {
2018 /* If this ID was requested by the caller return NULL,
2019 * otherwise just get us the next one */
2021 temp_id = sq2->id + 1;
2024 } else if (sq2->id > temp_id) {
2029 sq = g_new0 (GstSingleQueue, 1);
2033 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2034 mqueue->queues_cookie++;
2036 /* copy over max_size and extra_size so we don't need to take the lock
2037 * any longer when checking if the queue is full. */
2038 sq->max_size.visible = mqueue->max_size.visible;
2039 sq->max_size.bytes = mqueue->max_size.bytes;
2040 sq->max_size.time = mqueue->max_size.time;
2042 sq->extra_size.visible = mqueue->extra_size.visible;
2043 sq->extra_size.bytes = mqueue->extra_size.bytes;
2044 sq->extra_size.time = mqueue->extra_size.time;
2046 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2048 sq->mqueue = mqueue;
2049 sq->srcresult = GST_FLOW_FLUSHING;
2051 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2052 single_queue_check_full,
2053 (GstDataQueueFullCallback) single_queue_overrun_cb,
2054 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2056 sq->flushing = FALSE;
2057 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2058 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2062 sq->next_time = GST_CLOCK_TIME_NONE;
2063 sq->last_time = GST_CLOCK_TIME_NONE;
2064 g_cond_init (&sq->turn);
2065 g_cond_init (&sq->query_handled);
2067 sq->sinktime = GST_CLOCK_TIME_NONE;
2068 sq->srctime = GST_CLOCK_TIME_NONE;
2069 sq->sink_tainted = TRUE;
2070 sq->src_tainted = TRUE;
2072 name = g_strdup_printf ("sink_%u", sq->id);
2073 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
2076 gst_pad_set_chain_function (sq->sinkpad,
2077 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2078 gst_pad_set_activatemode_function (sq->sinkpad,
2079 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2080 gst_pad_set_event_function (sq->sinkpad,
2081 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2082 gst_pad_set_query_function (sq->sinkpad,
2083 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2084 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2085 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2086 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2088 name = g_strdup_printf ("src_%u", sq->id);
2089 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2092 gst_pad_set_activatemode_function (sq->srcpad,
2093 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2094 gst_pad_set_event_function (sq->srcpad,
2095 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2096 gst_pad_set_query_function (sq->srcpad,
2097 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2098 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2099 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2100 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2102 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2103 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2105 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2107 /* only activate the pads when we are not in the NULL state
2108 * and add the pad under the state_lock to prevend state changes
2109 * between activating and adding */
2110 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2111 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2112 gst_pad_set_active (sq->srcpad, TRUE);
2113 gst_pad_set_active (sq->sinkpad, TRUE);
2115 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2116 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2117 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2119 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",