2 * Copyright (C) 2006 Edward Hervey <edward@fluendo.com>
3 * Copyright (C) 2007 Jan Schmidt <jan@fluendo.com>
4 * Copyright (C) 2007 Wim Taymans <wim@fluendo.com>
5 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:element-multiqueue
27 * @see_also: #GstQueue
31 * Multiqueue is similar to a normal #GstQueue with the following additional
35 * <itemizedlist><title>Multiple streamhandling</title>
37 * The element handles queueing data on more than one stream at once. To
38 * achieve such a feature it has request sink pads (sink%d) and
39 * 'sometimes' src pads (src%d).
41 * When requesting a given sinkpad with gst_element_get_request_pad(),
42 * the associated srcpad for that stream will be created.
43 * Example: requesting sink1 will generate src1.
48 * <itemizedlist><title>Non-starvation on multiple streams</title>
50 * If more than one stream is used with the element, the streams' queues
51 * will be dynamically grown (up to a limit), in order to ensure that no
52 * stream is risking data starvation. This guarantees that at any given
53 * time there are at least N bytes queued and available for each individual
56 * If an EOS event comes through a srcpad, the associated queue will be
57 * considered as 'not-empty' in the queue-size-growing algorithm.
62 * <itemizedlist><title>Non-linked srcpads graceful handling</title>
64 * In order to better support dynamic switching between streams, the multiqueue
65 * (unlike the current GStreamer queue) continues to push buffers on non-linked
66 * pads rather than shutting down.
68 * In addition, to prevent a non-linked stream from very quickly consuming all
69 * available buffers and thus 'racing ahead' of the other streams, the element
70 * must ensure that buffers and inlined events for a non-linked stream are pushed
71 * in the same order as they were received, relative to the other streams
72 * controlled by the element. This means that a buffer cannot be pushed to a
73 * non-linked pad any sooner than buffers in any other stream which were received
81 * Data is queued until one of the limits specified by the
82 * #GstMultiQueue:max-size-buffers, #GstMultiQueue:max-size-bytes and/or
83 * #GstMultiQueue:max-size-time properties has been reached. Any attempt to push
84 * more buffers into the queue will block the pushing thread until more space
85 * becomes available. #GstMultiQueue:extra-size-buffers,
88 * #GstMultiQueue:extra-size-bytes and #GstMultiQueue:extra-size-time are
92 * The default queue size limits are 5 buffers, 10MB of data, or
93 * two second worth of data, whichever is reached first. Note that the number
94 * of buffers will dynamically grow depending on the fill level of
98 * The #GstMultiQueue::underrun signal is emitted when all of the queues
99 * are empty. The #GstMultiQueue::overrun signal is emitted when one of the
101 * Both signals are emitted from the context of the streaming thread.
105 * Last reviewed on 2008-01-25 (0.10.17)
114 #include "gstmultiqueue.h"
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;
190 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
191 static void gst_single_queue_free (GstSingleQueue * squeue);
193 static void wake_up_next_non_linked (GstMultiQueue * mq);
194 static void compute_high_id (GstMultiQueue * mq);
195 static void compute_high_time (GstMultiQueue * mq);
196 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
197 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
199 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
202 GST_STATIC_CAPS_ANY);
204 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
207 GST_STATIC_CAPS_ANY);
209 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
210 #define GST_CAT_DEFAULT (multi_queue_debug)
212 /* Signals and args */
220 /* default limits, we try to keep up to 2 seconds of data and if there is not
221 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
222 * there is data in the queues. Normally, the byte and time limits are not hit
223 * in theses conditions. */
224 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
225 #define DEFAULT_MAX_SIZE_BUFFERS 5
226 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
228 /* second limits. When we hit one of the above limits we are probably dealing
229 * with a badly muxed file and we scale the limits to these emergency values.
230 * This is currently not yet implemented.
231 * Since we dynamically scale the queue buffer size up to the limits but avoid
232 * going above the max-size-buffers when we can, we don't really need this
233 * aditional extra size. */
234 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
235 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
236 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
238 #define DEFAULT_USE_BUFFERING FALSE
239 #define DEFAULT_LOW_PERCENT 10
240 #define DEFAULT_HIGH_PERCENT 99
241 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
246 PROP_EXTRA_SIZE_BYTES,
247 PROP_EXTRA_SIZE_BUFFERS,
248 PROP_EXTRA_SIZE_TIME,
250 PROP_MAX_SIZE_BUFFERS,
255 PROP_SYNC_BY_RUNNING_TIME,
259 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
260 g_mutex_lock (&q->qlock); \
263 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
264 g_mutex_unlock (&q->qlock); \
267 static void gst_multi_queue_finalize (GObject * object);
268 static void gst_multi_queue_set_property (GObject * object,
269 guint prop_id, const GValue * value, GParamSpec * pspec);
270 static void gst_multi_queue_get_property (GObject * object,
271 guint prop_id, GValue * value, GParamSpec * pspec);
273 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
274 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
275 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
276 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
277 element, GstStateChange transition);
279 static void gst_multi_queue_loop (GstPad * pad);
282 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
283 #define gst_multi_queue_parent_class parent_class
284 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
287 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
290 gst_multi_queue_class_init (GstMultiQueueClass * klass)
292 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
293 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
295 gobject_class->set_property = gst_multi_queue_set_property;
296 gobject_class->get_property = gst_multi_queue_get_property;
301 * GstMultiQueue::underrun:
302 * @multiqueue: the multqueue instance
304 * This signal is emitted from the streaming thread when there is
305 * no data in any of the queues inside the multiqueue instance (underrun).
307 * This indicates either starvation or EOS from the upstream data sources.
309 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
310 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
311 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
312 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
315 * GstMultiQueue::overrun:
316 * @multiqueue: the multiqueue instance
318 * Reports that one of the queues in the multiqueue is full (overrun).
319 * A queue is full if the total amount of data inside it (num-buffers, time,
320 * size) is higher than the boundary values which can be set through the
321 * GObject properties.
323 * This can be used as an indicator of pre-roll.
325 gst_multi_queue_signals[SIGNAL_OVERRUN] =
326 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
327 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
328 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
332 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
333 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
334 "Max. amount of data in the queue (bytes, 0=disable)",
335 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
338 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
339 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
340 DEFAULT_MAX_SIZE_BUFFERS,
341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
343 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
344 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
345 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
348 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
349 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
350 " (NOT IMPLEMENTED)",
351 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
354 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
355 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
356 " (NOT IMPLEMENTED)",
357 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
358 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
360 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
361 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
362 " (NOT IMPLEMENTED)",
363 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 * GstMultiQueue:use-buffering
369 * Enable the buffering option in multiqueue so that BUFFERING messages are
370 * emited based on low-/high-percent thresholds.
372 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
373 g_param_spec_boolean ("use-buffering", "Use buffering",
374 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
375 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 * GstMultiQueue:low-percent
379 * Low threshold percent for buffering to start.
381 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
382 g_param_spec_int ("low-percent", "Low percent",
383 "Low threshold for buffering to start", 0, 100,
384 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 * GstMultiQueue:high-percent
388 * High threshold percent for buffering to finish.
390 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
391 g_param_spec_int ("high-percent", "High percent",
392 "High threshold for buffering to finish", 0, 100,
393 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396 * GstMultiQueue:sync-by-running-time
398 * If enabled multiqueue will synchronize deactivated or not-linked streams
399 * to the activated and linked streams by taking the running time.
400 * Otherwise multiqueue will synchronize the deactivated or not-linked
401 * streams by keeping the order in which buffers and events arrived compared
402 * to active and linked streams.
404 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
405 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
406 "Synchronize deactivated or not-linked streams by running time",
407 DEFAULT_SYNC_BY_RUNNING_TIME,
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410 gobject_class->finalize = gst_multi_queue_finalize;
412 gst_element_class_set_static_metadata (gstelement_class,
414 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
415 gst_element_class_add_pad_template (gstelement_class,
416 gst_static_pad_template_get (&sinktemplate));
417 gst_element_class_add_pad_template (gstelement_class,
418 gst_static_pad_template_get (&srctemplate));
420 gstelement_class->request_new_pad =
421 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
422 gstelement_class->release_pad =
423 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
424 gstelement_class->change_state =
425 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
429 gst_multi_queue_init (GstMultiQueue * mqueue)
431 mqueue->nbqueues = 0;
432 mqueue->queues = NULL;
434 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
435 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
436 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
438 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
439 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
440 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
442 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
443 mqueue->low_percent = DEFAULT_LOW_PERCENT;
444 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
446 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
450 mqueue->high_time = GST_CLOCK_TIME_NONE;
452 g_mutex_init (&mqueue->qlock);
456 gst_multi_queue_finalize (GObject * object)
458 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
460 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
461 g_list_free (mqueue->queues);
462 mqueue->queues = NULL;
463 mqueue->queues_cookie++;
465 /* free/unref instance data */
466 g_mutex_clear (&mqueue->qlock);
468 G_OBJECT_CLASS (parent_class)->finalize (object);
471 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
472 GList * tmp = mq->queues; \
474 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
475 q->max_size.format = mq->max_size.format; \
476 tmp = g_list_next(tmp); \
481 gst_multi_queue_set_property (GObject * object, guint prop_id,
482 const GValue * value, GParamSpec * pspec)
484 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
487 case PROP_MAX_SIZE_BYTES:
488 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
489 mq->max_size.bytes = g_value_get_uint (value);
490 SET_CHILD_PROPERTY (mq, bytes);
491 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
493 case PROP_MAX_SIZE_BUFFERS:
494 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
495 mq->max_size.visible = g_value_get_uint (value);
496 SET_CHILD_PROPERTY (mq, visible);
497 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
499 case PROP_MAX_SIZE_TIME:
500 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
501 mq->max_size.time = g_value_get_uint64 (value);
502 SET_CHILD_PROPERTY (mq, time);
503 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
505 case PROP_EXTRA_SIZE_BYTES:
506 mq->extra_size.bytes = g_value_get_uint (value);
508 case PROP_EXTRA_SIZE_BUFFERS:
509 mq->extra_size.visible = g_value_get_uint (value);
511 case PROP_EXTRA_SIZE_TIME:
512 mq->extra_size.time = g_value_get_uint64 (value);
514 case PROP_USE_BUFFERING:
515 mq->use_buffering = g_value_get_boolean (value);
517 case PROP_LOW_PERCENT:
518 mq->low_percent = g_value_get_int (value);
520 case PROP_HIGH_PERCENT:
521 mq->high_percent = g_value_get_int (value);
523 case PROP_SYNC_BY_RUNNING_TIME:
524 mq->sync_by_running_time = g_value_get_boolean (value);
527 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
533 gst_multi_queue_get_property (GObject * object, guint prop_id,
534 GValue * value, GParamSpec * pspec)
536 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
538 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
541 case PROP_EXTRA_SIZE_BYTES:
542 g_value_set_uint (value, mq->extra_size.bytes);
544 case PROP_EXTRA_SIZE_BUFFERS:
545 g_value_set_uint (value, mq->extra_size.visible);
547 case PROP_EXTRA_SIZE_TIME:
548 g_value_set_uint64 (value, mq->extra_size.time);
550 case PROP_MAX_SIZE_BYTES:
551 g_value_set_uint (value, mq->max_size.bytes);
553 case PROP_MAX_SIZE_BUFFERS:
554 g_value_set_uint (value, mq->max_size.visible);
556 case PROP_MAX_SIZE_TIME:
557 g_value_set_uint64 (value, mq->max_size.time);
559 case PROP_USE_BUFFERING:
560 g_value_set_boolean (value, mq->use_buffering);
562 case PROP_LOW_PERCENT:
563 g_value_set_int (value, mq->low_percent);
565 case PROP_HIGH_PERCENT:
566 g_value_set_int (value, mq->high_percent);
568 case PROP_SYNC_BY_RUNNING_TIME:
569 g_value_set_boolean (value, mq->sync_by_running_time);
572 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
576 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
580 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
582 GstIterator *it = NULL;
584 GstSingleQueue *squeue;
585 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
588 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
589 squeue = gst_pad_get_element_private (pad);
593 if (squeue->sinkpad == pad)
594 opad = gst_object_ref (squeue->srcpad);
595 else if (squeue->srcpad == pad)
596 opad = gst_object_ref (squeue->sinkpad);
600 g_value_init (&val, GST_TYPE_PAD);
601 g_value_set_object (&val, opad);
602 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
603 g_value_unset (&val);
605 gst_object_unref (opad);
608 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
619 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
620 const gchar * name, const GstCaps * caps)
622 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
623 GstSingleQueue *squeue;
627 sscanf (name + 4, "_%u", &temp_id);
628 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
631 /* Create a new single queue, add the sink and source pad and return the sink pad */
632 squeue = gst_single_queue_new (mqueue, temp_id);
634 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
635 GST_DEBUG_PAD_NAME (squeue->sinkpad));
637 return squeue ? squeue->sinkpad : NULL;
641 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
643 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
644 GstSingleQueue *sq = NULL;
647 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
649 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
650 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
651 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
652 sq = (GstSingleQueue *) tmp->data;
654 if (sq->sinkpad == pad)
659 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
660 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
664 /* FIXME: The removal of the singlequeue should probably not happen until it
665 * finishes draining */
667 /* remove it from the list */
668 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
669 mqueue->queues_cookie++;
671 /* FIXME : recompute next-non-linked */
672 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
674 /* delete SingleQueue */
675 gst_data_queue_set_flushing (sq->queue, TRUE);
677 gst_pad_set_active (sq->srcpad, FALSE);
678 gst_pad_set_active (sq->sinkpad, FALSE);
679 gst_pad_set_element_private (sq->srcpad, NULL);
680 gst_pad_set_element_private (sq->sinkpad, NULL);
681 gst_element_remove_pad (element, sq->srcpad);
682 gst_element_remove_pad (element, sq->sinkpad);
683 gst_single_queue_free (sq);
686 static GstStateChangeReturn
687 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
689 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
690 GstSingleQueue *sq = NULL;
691 GstStateChangeReturn result;
693 switch (transition) {
694 case GST_STATE_CHANGE_READY_TO_PAUSED:{
697 /* Set all pads to non-flushing */
698 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
699 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
700 sq = (GstSingleQueue *) tmp->data;
701 sq->flushing = FALSE;
703 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
706 case GST_STATE_CHANGE_PAUSED_TO_READY:{
709 /* Un-wait all waiting pads */
710 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
711 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
712 sq = (GstSingleQueue *) tmp->data;
714 g_cond_signal (&sq->turn);
716 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
723 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
725 switch (transition) {
737 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
741 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
745 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
746 sq->srcresult = GST_FLOW_FLUSHING;
747 gst_data_queue_set_flushing (sq->queue, TRUE);
751 /* wake up non-linked task */
752 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
754 g_cond_signal (&sq->turn);
755 sq->last_query = FALSE;
756 g_cond_signal (&sq->query_handled);
757 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
759 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
760 result = gst_pad_pause_task (sq->srcpad);
761 sq->sink_tainted = sq->src_tainted = TRUE;
763 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
764 gst_data_queue_flush (sq->queue);
765 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
766 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
767 /* All pads start off not-linked for a smooth kick-off */
768 sq->srcresult = GST_FLOW_OK;
771 sq->max_size.visible = mq->max_size.visible;
775 sq->last_oldid = G_MAXUINT32;
776 sq->next_time = GST_CLOCK_TIME_NONE;
777 sq->last_time = GST_CLOCK_TIME_NONE;
778 gst_data_queue_set_flushing (sq->queue, FALSE);
780 /* Reset high time to be recomputed next */
781 mq->high_time = GST_CLOCK_TIME_NONE;
783 sq->flushing = FALSE;
784 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
786 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
788 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
795 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
797 GstDataQueueSize size;
799 gboolean post = FALSE;
801 /* nothing to dowhen we are not in buffering mode */
802 if (!mq->use_buffering)
805 gst_data_queue_get_level (sq->queue, &size);
807 GST_DEBUG_OBJECT (mq,
808 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
809 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
810 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
812 /* get bytes and time percentages and take the max */
817 if (sq->max_size.time > 0) {
818 tmp = (sq->cur_time * 100) / sq->max_size.time;
819 percent = MAX (percent, tmp);
821 if (sq->max_size.bytes > 0) {
822 tmp = (size.bytes * 100) / sq->max_size.bytes;
823 percent = MAX (percent, tmp);
829 if (percent >= mq->high_percent) {
830 mq->buffering = FALSE;
832 /* make sure it increases */
833 percent = MAX (mq->percent, percent);
835 if (percent == mq->percent)
836 /* don't post if nothing changed */
839 /* else keep last value we posted */
840 mq->percent = percent;
842 if (percent < mq->low_percent) {
843 mq->buffering = TRUE;
844 mq->percent = percent;
851 /* scale to high percent so that it becomes the 100% mark */
852 percent = percent * 100 / mq->high_percent;
857 GST_DEBUG_OBJECT (mq, "buffering %d percent", percent);
858 message = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
860 gst_element_post_message (GST_ELEMENT_CAST (mq), message);
862 GST_DEBUG_OBJECT (mq, "filled %d percent", percent);
866 /* calculate the diff between running time on the sink and src of the queue.
867 * This is the total amount of time in the queue.
870 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
872 gint64 sink_time, src_time;
874 if (sq->sink_tainted) {
875 sink_time = sq->sinktime =
876 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
877 sq->sink_segment.position);
879 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
880 /* if we have a time, we become untainted and use the time */
881 sq->sink_tainted = FALSE;
883 sink_time = sq->sinktime;
885 if (sq->src_tainted) {
886 src_time = sq->srctime =
887 gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
888 sq->src_segment.position);
889 /* if we have a time, we become untainted and use the time */
890 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
891 sq->src_tainted = FALSE;
893 src_time = sq->srctime;
895 GST_DEBUG_OBJECT (mq,
896 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
897 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
899 /* This allows for streams with out of order timestamping - sometimes the
900 * emerging timestamp is later than the arriving one(s) */
901 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
902 sq->cur_time = sink_time - src_time;
906 /* updating the time level can change the buffering state */
907 update_buffering (mq, sq);
912 /* take a SEGMENT event and apply the values to segment, updating the time
915 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
916 GstSegment * segment)
918 gst_event_copy_segment (event, segment);
920 /* now configure the values, we use these to track timestamps on the
922 if (segment->format != GST_FORMAT_TIME) {
923 /* non-time format, pretent the current time segment is closed with a
924 * 0 start and unknown stop time. */
925 segment->format = GST_FORMAT_TIME;
930 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
932 if (segment == &sq->sink_segment)
933 sq->sink_tainted = TRUE;
935 sq->src_tainted = TRUE;
937 GST_DEBUG_OBJECT (mq,
938 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
940 /* segment can update the time level of the queue */
941 update_time_level (mq, sq);
943 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
946 /* take a buffer and update segment, updating the time level of the queue. */
948 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
949 GstClockTime duration, GstSegment * segment)
951 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
953 /* if no timestamp is set, assume it's continuous with the previous
955 if (timestamp == GST_CLOCK_TIME_NONE)
956 timestamp = segment->position;
959 if (duration != GST_CLOCK_TIME_NONE)
960 timestamp += duration;
962 GST_DEBUG_OBJECT (mq, "queue %d, position updated to %" GST_TIME_FORMAT,
963 sq->id, GST_TIME_ARGS (timestamp));
965 segment->position = timestamp;
967 if (segment == &sq->sink_segment)
968 sq->sink_tainted = TRUE;
970 sq->src_tainted = TRUE;
972 /* calc diff with other end */
973 update_time_level (mq, sq);
974 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
978 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
980 GstClockTime time = GST_CLOCK_TIME_NONE;
982 if (GST_IS_BUFFER (object)) {
983 GstBuffer *buf = GST_BUFFER_CAST (object);
985 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
986 time = GST_BUFFER_TIMESTAMP (buf);
987 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
988 time += GST_BUFFER_DURATION (buf);
989 if (time > segment->stop)
990 time = segment->stop;
991 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
993 } else if (GST_IS_BUFFER_LIST (object)) {
994 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
998 n = gst_buffer_list_length (list);
999 for (i = 0; i < n; i++) {
1000 buf = gst_buffer_list_get (list, i);
1001 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1002 time = GST_BUFFER_TIMESTAMP (buf);
1003 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1004 time += GST_BUFFER_DURATION (buf);
1005 if (time > segment->stop)
1006 time = segment->stop;
1007 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1014 } else if (GST_IS_EVENT (object)) {
1015 GstEvent *event = GST_EVENT_CAST (object);
1017 /* For newsegment events return the running time of the start position */
1018 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1019 const GstSegment *new_segment;
1021 gst_event_parse_segment (event, &new_segment);
1022 if (new_segment->format == GST_FORMAT_TIME) {
1024 gst_segment_to_running_time (new_segment, GST_FORMAT_TIME,
1025 new_segment->start);
1034 static GstFlowReturn
1035 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1036 GstMiniObject * object)
1038 GstFlowReturn result = GST_FLOW_OK;
1040 if (GST_IS_BUFFER (object)) {
1042 GstClockTime timestamp, duration;
1044 buffer = GST_BUFFER_CAST (object);
1045 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1046 duration = GST_BUFFER_DURATION (buffer);
1048 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1050 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1051 gst_data_queue_limits_changed (sq->queue);
1053 GST_DEBUG_OBJECT (mq,
1054 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1055 sq->id, buffer, GST_TIME_ARGS (timestamp));
1057 result = gst_pad_push (sq->srcpad, buffer);
1058 } else if (GST_IS_EVENT (object)) {
1061 event = GST_EVENT_CAST (object);
1063 switch (GST_EVENT_TYPE (event)) {
1065 result = GST_FLOW_EOS;
1067 case GST_EVENT_SEGMENT:
1068 apply_segment (mq, sq, event, &sq->src_segment);
1069 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1070 gst_data_queue_limits_changed (sq->queue);
1076 GST_DEBUG_OBJECT (mq,
1077 "SingleQueue %d : Pushing event %p of type %s",
1078 sq->id, event, GST_EVENT_TYPE_NAME (event));
1080 gst_pad_push_event (sq->srcpad, event);
1081 } else if (GST_IS_QUERY (object)) {
1085 query = GST_QUERY_CAST (object);
1087 res = gst_pad_peer_query (sq->srcpad, query);
1089 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1090 sq->last_query = res;
1091 g_cond_signal (&sq->query_handled);
1092 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1094 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1102 static GstMiniObject *
1103 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1108 item->object = NULL;
1114 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1117 gst_mini_object_unref (item->object);
1118 g_slice_free (GstMultiQueueItem, item);
1121 /* takes ownership of passed mini object! */
1122 static GstMultiQueueItem *
1123 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1125 GstMultiQueueItem *item;
1127 item = g_slice_new (GstMultiQueueItem);
1128 item->object = object;
1129 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1130 item->posid = curid;
1132 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1133 item->duration = GST_BUFFER_DURATION (object);
1134 if (item->duration == GST_CLOCK_TIME_NONE)
1136 item->visible = TRUE;
1140 static GstMultiQueueItem *
1141 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1143 GstMultiQueueItem *item;
1145 item = g_slice_new (GstMultiQueueItem);
1146 item->object = object;
1147 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1148 item->posid = curid;
1152 item->visible = FALSE;
1156 /* Each main loop attempts to push buffers until the return value
1157 * is not-linked. not-linked pads are not allowed to push data beyond
1158 * any linked pads, so they don't 'rush ahead of the pack'.
1161 gst_multi_queue_loop (GstPad * pad)
1164 GstMultiQueueItem *item;
1165 GstDataQueueItem *sitem;
1167 GstMiniObject *object = NULL;
1169 GstFlowReturn result;
1170 GstClockTime next_time;
1173 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1176 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1181 /* Get something from the queue, blocking until that happens, or we get
1183 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1186 item = (GstMultiQueueItem *) sitem;
1187 newid = item->posid;
1189 /* steal the object and destroy the item */
1190 object = gst_multi_queue_item_steal_object (item);
1191 gst_multi_queue_item_destroy (item);
1193 is_buffer = GST_IS_BUFFER (object);
1195 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1196 next_time = get_running_time (&sq->src_segment, object, TRUE);
1198 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1199 sq->id, newid, sq->last_oldid);
1201 /* If we're not-linked, we do some extra work because we might need to
1202 * wait before pushing. If we're linked but there's a gap in the IDs,
1203 * or it's the first loop, or we just passed the previous highid,
1204 * we might need to wake some sleeping pad up, so there's extra work
1206 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1207 if (sq->srcresult == GST_FLOW_NOT_LINKED
1208 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1209 || sq->last_oldid > mq->highid) {
1210 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1211 gst_flow_get_name (sq->srcresult));
1213 /* Check again if we're flushing after the lock is taken,
1214 * the flush flag might have been changed in the meantime */
1216 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1220 /* Update the nextid so other threads know when to wake us up */
1222 sq->next_time = next_time;
1224 /* Update the oldid (the last ID we output) for highid tracking */
1225 if (sq->last_oldid != G_MAXUINT32)
1226 sq->oldid = sq->last_oldid;
1228 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1229 /* Go to sleep until it's time to push this buffer */
1231 /* Recompute the highid */
1232 compute_high_id (mq);
1233 /* Recompute the high time */
1234 compute_high_time (mq);
1236 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1237 (mq->high_time == GST_CLOCK_TIME_NONE
1238 || next_time >= mq->high_time))
1239 || (!mq->sync_by_running_time && newid > mq->highid))
1240 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1242 GST_DEBUG_OBJECT (mq,
1243 "queue %d sleeping for not-linked wakeup with "
1244 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1245 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1246 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1248 /* Wake up all non-linked pads before we sleep */
1249 wake_up_next_non_linked (mq);
1252 g_cond_wait (&sq->turn, &mq->qlock);
1256 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1260 /* Recompute the high time */
1261 compute_high_time (mq);
1263 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1264 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1265 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1266 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1269 /* Re-compute the high_id in case someone else pushed */
1270 compute_high_id (mq);
1272 compute_high_id (mq);
1273 /* Wake up all non-linked pads */
1274 wake_up_next_non_linked (mq);
1276 /* We're done waiting, we can clear the nextid and nexttime */
1278 sq->next_time = GST_CLOCK_TIME_NONE;
1280 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1285 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1286 gst_flow_get_name (sq->srcresult));
1288 /* Update time stats */
1289 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1290 next_time = get_running_time (&sq->src_segment, object, FALSE);
1291 if (next_time != GST_CLOCK_TIME_NONE) {
1292 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1293 sq->last_time = next_time;
1294 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1295 /* Wake up all non-linked pads now that we advanced the high time */
1296 mq->high_time = next_time;
1297 wake_up_next_non_linked (mq);
1300 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1302 /* Try to push out the new object */
1303 result = gst_single_queue_push_one (mq, sq, object);
1306 /* Check if we pushed something already and if this is
1307 * now a switch from an active to a non-active stream.
1309 * If it is, we reset all the waiting streams, let them
1310 * push another buffer to see if they're now active again.
1311 * This allows faster switching between streams and prevents
1312 * deadlocks if downstream does any waiting too.
1314 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1315 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1316 && result == GST_FLOW_NOT_LINKED) {
1319 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1322 compute_high_id (mq);
1324 /* maybe no-one is waiting */
1325 if (mq->numwaiting > 0) {
1326 /* Else figure out which singlequeue(s) need waking up */
1327 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1328 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1330 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1331 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1332 sq2->pushed = FALSE;
1333 sq2->srcresult = GST_FLOW_OK;
1334 g_cond_signal (&sq2->turn);
1342 sq->srcresult = result;
1343 sq->last_oldid = newid;
1344 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1346 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1347 && result != GST_FLOW_EOS)
1350 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1351 gst_flow_get_name (sq->srcresult));
1358 gst_mini_object_unref (object);
1360 /* Need to make sure wake up any sleeping pads when we exit */
1361 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1362 compute_high_time (mq);
1363 compute_high_id (mq);
1364 wake_up_next_non_linked (mq);
1365 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1367 /* upstream needs to see fatal result ASAP to shut things down,
1368 * but might be stuck in one of our other full queues;
1369 * so empty this one and trigger dynamic queue growth. At
1370 * this point the srcresult is not OK, NOT_LINKED
1371 * or EOS, i.e. a real failure */
1372 gst_data_queue_flush (sq->queue);
1373 single_queue_underrun_cb (sq->queue, sq);
1374 gst_data_queue_set_flushing (sq->queue, TRUE);
1375 gst_pad_pause_task (sq->srcpad);
1376 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1377 "SingleQueue[%d] task paused, reason:%s",
1378 sq->id, gst_flow_get_name (sq->srcresult));
1384 * gst_multi_queue_chain:
1386 * This is similar to GstQueue's chain function, except:
1387 * _ we don't have leak behaviours,
1388 * _ we push with a unique id (curid)
1390 static GstFlowReturn
1391 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1395 GstMultiQueueItem *item;
1397 GstClockTime timestamp, duration;
1399 sq = gst_pad_get_element_private (pad);
1402 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1406 /* Get a unique incrementing id */
1407 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1409 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1410 sq->id, buffer, curid);
1412 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1414 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1415 duration = GST_BUFFER_DURATION (buffer);
1417 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1420 /* update time level, we must do this after pushing the data in the queue so
1421 * that we never end up filling the queue first. */
1422 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1425 return sq->srcresult;
1430 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1431 sq->id, gst_flow_get_name (sq->srcresult));
1432 gst_multi_queue_item_destroy (item);
1437 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
1438 gst_buffer_unref (buffer);
1439 return GST_FLOW_EOS;
1444 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
1445 GstPadMode mode, gboolean active)
1451 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1452 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1454 /* mq is NULL if the pad is activated/deactivated before being
1455 * added to the multiqueue */
1457 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1460 case GST_PAD_MODE_PUSH:
1462 /* All pads start off linked until they push one buffer */
1463 sq->srcresult = GST_FLOW_OK;
1466 sq->srcresult = GST_FLOW_FLUSHING;
1467 gst_data_queue_flush (sq->queue);
1477 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1478 gst_object_unref (mq);
1485 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1490 GstMultiQueueItem *item;
1493 GstEvent *sref = NULL;
1495 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1496 mq = (GstMultiQueue *) parent;
1498 type = GST_EVENT_TYPE (event);
1501 case GST_EVENT_FLUSH_START:
1502 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1505 res = gst_pad_push_event (sq->srcpad, event);
1507 gst_single_queue_flush (mq, sq, TRUE);
1510 case GST_EVENT_FLUSH_STOP:
1511 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1514 res = gst_pad_push_event (sq->srcpad, event);
1516 gst_single_queue_flush (mq, sq, FALSE);
1518 case GST_EVENT_SEGMENT:
1519 /* take ref because the queue will take ownership and we need the event
1520 * afterwards to update the segment */
1521 sref = gst_event_ref (event);
1525 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1526 res = gst_pad_push_event (sq->srcpad, event);
1532 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1536 /* Get an unique incrementing id. */
1537 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1539 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
1541 GST_DEBUG_OBJECT (mq,
1542 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1543 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1545 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1548 /* mark EOS when we received one, we must do that after putting the
1549 * buffer in the queue because EOS marks the buffer as filled. No need to take
1550 * a lock, the _check_full happens from this thread only, right before pushing
1551 * into dataqueue. */
1555 /* EOS affects the buffering state */
1556 update_buffering (mq, sq);
1557 single_queue_overrun_cb (sq->queue, sq);
1559 case GST_EVENT_SEGMENT:
1560 apply_segment (mq, sq, sref, &sq->sink_segment);
1561 gst_event_unref (sref);
1571 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1572 sq->id, gst_flow_get_name (sq->srcresult));
1574 gst_event_unref (sref);
1575 gst_multi_queue_item_destroy (item);
1580 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1581 gst_event_unref (event);
1588 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1594 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1595 mq = (GstMultiQueue *) parent;
1597 switch (GST_QUERY_TYPE (query)) {
1599 if (GST_QUERY_IS_SERIALIZED (query)) {
1601 GstMultiQueueItem *item;
1603 /* Get an unique incrementing id. */
1604 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1606 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
1608 GST_DEBUG_OBJECT (mq,
1609 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
1610 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
1612 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1613 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
1614 g_cond_wait (&sq->query_handled, &mq->qlock);
1615 res = sq->last_query;
1616 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1618 /* default handling */
1619 res = gst_pad_query_default (pad, parent, query);
1627 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
1628 GstPadMode mode, gboolean active)
1634 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1637 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1640 case GST_PAD_MODE_PUSH:
1642 result = gst_single_queue_flush (mq, sq, FALSE);
1644 result = gst_single_queue_flush (mq, sq, TRUE);
1645 /* make sure streaming finishes */
1646 result |= gst_pad_stop_task (pad);
1657 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1659 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1661 return gst_pad_push_event (sq->sinkpad, event);
1665 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1669 /* FIXME, Handle position offset depending on queue size */
1670 switch (GST_QUERY_TYPE (query)) {
1672 /* default handling */
1673 res = gst_pad_query_default (pad, parent, query);
1680 * Next-non-linked functions
1683 /* WITH LOCK TAKEN */
1685 wake_up_next_non_linked (GstMultiQueue * mq)
1689 /* maybe no-one is waiting */
1690 if (mq->numwaiting < 1)
1693 /* Else figure out which singlequeue(s) need waking up */
1694 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1695 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1697 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1698 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
1699 && sq->next_time != GST_CLOCK_TIME_NONE
1700 && sq->next_time >= mq->high_time)
1701 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
1702 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1703 g_cond_signal (&sq->turn);
1709 /* WITH LOCK TAKEN */
1711 compute_high_id (GstMultiQueue * mq)
1713 /* The high-id is either the highest id among the linked pads, or if all
1714 * pads are not-linked, it's the lowest not-linked pad */
1716 guint32 lowest = G_MAXUINT32;
1717 guint32 highid = G_MAXUINT32;
1719 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1720 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1722 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1723 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1725 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1726 /* No need to consider queues which are not waiting */
1727 if (sq->nextid == 0) {
1728 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1732 if (sq->nextid < lowest)
1733 lowest = sq->nextid;
1734 } else if (sq->srcresult != GST_FLOW_EOS) {
1735 /* If we don't have a global highid, or the global highid is lower than
1736 * this single queue's last outputted id, store the queue's one,
1737 * unless the singlequeue is at EOS (srcresult = EOS) */
1738 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1743 if (highid == G_MAXUINT32 || lowest < highid)
1744 mq->highid = lowest;
1746 mq->highid = highid;
1748 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1752 /* WITH LOCK TAKEN */
1754 compute_high_time (GstMultiQueue * mq)
1756 /* The high-id is either the highest id among the linked pads, or if all
1757 * pads are not-linked, it's the lowest not-linked pad */
1759 GstClockTime highest = GST_CLOCK_TIME_NONE;
1760 GstClockTime lowest = GST_CLOCK_TIME_NONE;
1762 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1763 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1766 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
1767 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
1768 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
1770 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1771 /* No need to consider queues which are not waiting */
1772 if (sq->next_time == GST_CLOCK_TIME_NONE) {
1773 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1777 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
1778 lowest = sq->next_time;
1779 } else if (sq->srcresult != GST_FLOW_EOS) {
1780 /* If we don't have a global highid, or the global highid is lower than
1781 * this single queue's last outputted id, store the queue's one,
1782 * unless the singlequeue is at EOS (srcresult = EOS) */
1783 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
1784 highest = sq->last_time;
1788 mq->high_time = highest;
1791 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
1792 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
1795 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
1796 ((q)->max_size.format) <= (value))
1799 * GstSingleQueue functions
1802 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1804 GstMultiQueue *mq = sq->mqueue;
1806 GstDataQueueSize size;
1807 gboolean filled = FALSE;
1809 gst_data_queue_get_level (sq->queue, &size);
1811 GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
1813 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1814 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1815 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1816 GstDataQueueSize ssize;
1818 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
1820 if (gst_data_queue_is_empty (oq->queue)) {
1821 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
1822 if (IS_FILLED (sq, visible, size.visible)) {
1823 sq->max_size.visible = size.visible + 1;
1824 GST_DEBUG_OBJECT (mq,
1825 "Another queue is empty, bumping single queue %d max visible to %d",
1826 sq->id, sq->max_size.visible);
1829 /* check if we reached the hard time/bytes limits */
1830 gst_data_queue_get_level (oq->queue, &ssize);
1832 GST_DEBUG_OBJECT (mq,
1833 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1834 G_GUINT64_FORMAT, oq->id, ssize.visible, oq->max_size.visible,
1835 ssize.bytes, oq->max_size.bytes, oq->cur_time, oq->max_size.time);
1837 /* if this queue is filled completely we must signal overrun.
1838 * FIXME, this seems wrong in many ways
1839 * - we're comparing the filled level of this queue against the
1840 * values of the other one
1841 * - we should only do this after we found no empty queues, ie, move
1842 * this check outside of the loop
1843 * - the debug statement talks about a different queue than the one
1844 * we are checking here.
1846 if (sq->is_eos || IS_FILLED (sq, bytes, ssize.bytes) ||
1847 IS_FILLED (sq, time, sq->cur_time)) {
1848 GST_LOG_OBJECT (mq, "Queue %d is filled EOS %d", sq->id, sq->is_eos);
1852 /* no queues were empty */
1853 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1855 /* Overrun is always forwarded, since this is blocking the upstream element */
1857 GST_DEBUG_OBJECT (mq, "A queue is filled, signalling overrun");
1858 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1865 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1867 gboolean empty = TRUE;
1868 GstMultiQueue *mq = sq->mqueue;
1872 "Single Queue %d is empty, Checking other single queues", sq->id);
1874 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1875 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1876 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1878 if (gst_data_queue_is_full (oq->queue)) {
1879 GstDataQueueSize size;
1881 gst_data_queue_get_level (oq->queue, &size);
1882 if (IS_FILLED (oq, visible, size.visible)) {
1883 oq->max_size.visible = size.visible + 1;
1884 GST_DEBUG_OBJECT (mq,
1885 "queue %d is filled, bumping its max visible to %d", oq->id,
1886 oq->max_size.visible);
1887 gst_data_queue_limits_changed (oq->queue);
1890 if (!gst_data_queue_is_empty (oq->queue))
1893 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1896 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1897 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1902 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1903 guint64 time, GstSingleQueue * sq)
1906 GstMultiQueue *mq = sq->mqueue;
1908 GST_DEBUG_OBJECT (mq,
1909 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1910 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1911 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1913 /* we are always filled on EOS */
1917 /* we never go past the max visible items unless we are in buffering mode */
1918 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
1921 /* check time or bytes */
1922 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
1928 gst_single_queue_free (GstSingleQueue * sq)
1931 gst_data_queue_flush (sq->queue);
1932 g_object_unref (sq->queue);
1933 g_cond_clear (&sq->turn);
1934 g_cond_clear (&sq->query_handled);
1938 static GstSingleQueue *
1939 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
1944 guint temp_id = (id == -1) ? 0 : id;
1946 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1948 /* Find an unused queue ID, if possible the passed one */
1949 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1950 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1951 /* This works because the IDs are sorted in ascending order */
1952 if (sq2->id == temp_id) {
1953 /* If this ID was requested by the caller return NULL,
1954 * otherwise just get us the next one */
1956 temp_id = sq2->id + 1;
1959 } else if (sq2->id > temp_id) {
1964 sq = g_new0 (GstSingleQueue, 1);
1968 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
1969 mqueue->queues_cookie++;
1971 /* copy over max_size and extra_size so we don't need to take the lock
1972 * any longer when checking if the queue is full. */
1973 sq->max_size.visible = mqueue->max_size.visible;
1974 sq->max_size.bytes = mqueue->max_size.bytes;
1975 sq->max_size.time = mqueue->max_size.time;
1977 sq->extra_size.visible = mqueue->extra_size.visible;
1978 sq->extra_size.bytes = mqueue->extra_size.bytes;
1979 sq->extra_size.time = mqueue->extra_size.time;
1981 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
1983 sq->mqueue = mqueue;
1984 sq->srcresult = GST_FLOW_FLUSHING;
1986 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
1987 single_queue_check_full,
1988 (GstDataQueueFullCallback) single_queue_overrun_cb,
1989 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
1991 sq->flushing = FALSE;
1992 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1993 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1997 sq->next_time = GST_CLOCK_TIME_NONE;
1998 sq->last_time = GST_CLOCK_TIME_NONE;
1999 g_cond_init (&sq->turn);
2000 g_cond_init (&sq->query_handled);
2002 sq->sinktime = GST_CLOCK_TIME_NONE;
2003 sq->srctime = GST_CLOCK_TIME_NONE;
2004 sq->sink_tainted = TRUE;
2005 sq->src_tainted = TRUE;
2007 name = g_strdup_printf ("sink_%u", sq->id);
2008 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
2011 gst_pad_set_chain_function (sq->sinkpad,
2012 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2013 gst_pad_set_activatemode_function (sq->sinkpad,
2014 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2015 gst_pad_set_event_function (sq->sinkpad,
2016 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2017 gst_pad_set_query_function (sq->sinkpad,
2018 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2019 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2020 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2021 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2023 name = g_strdup_printf ("src_%u", sq->id);
2024 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2027 gst_pad_set_activatemode_function (sq->srcpad,
2028 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2029 gst_pad_set_event_function (sq->srcpad,
2030 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2031 gst_pad_set_query_function (sq->srcpad,
2032 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2033 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2034 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2035 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2037 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2038 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2040 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2042 /* only activate the pads when we are not in the NULL state
2043 * and add the pad under the state_lock to prevend state changes
2044 * between activating and adding */
2045 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2046 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2047 gst_pad_set_active (sq->srcpad, TRUE);
2048 gst_pad_set_active (sq->sinkpad, TRUE);
2050 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2051 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2052 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2054 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",