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;
141 GstSegment sink_segment;
142 GstSegment src_segment;
144 /* position of src/sink */
145 GstClockTime sinktime, srctime;
146 /* TRUE if either position needs to be recalculated */
147 gboolean sink_tainted, src_tainted;
151 GstDataQueueSize max_size, extra_size;
152 GstClockTime cur_time;
156 /* Protected by global lock */
157 guint32 nextid; /* ID of the next object waiting to be pushed */
158 guint32 oldid; /* ID of the last object pushed (last in a series) */
159 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
160 GstClockTime next_time; /* End running time of next buffer to be pushed */
161 GstClockTime last_time; /* Start running time of last pushed buffer */
162 GCond *turn; /* SingleQueue turn waiting conditional */
166 /* Extension of GstDataQueueItem structure for our usage */
167 typedef struct _GstMultiQueueItem GstMultiQueueItem;
169 struct _GstMultiQueueItem
171 GstMiniObject *object;
176 GDestroyNotify destroy;
180 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
181 static void gst_single_queue_free (GstSingleQueue * squeue);
183 static void wake_up_next_non_linked (GstMultiQueue * mq);
184 static void compute_high_id (GstMultiQueue * mq);
185 static void compute_high_time (GstMultiQueue * mq);
186 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
187 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
189 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
192 GST_STATIC_CAPS_ANY);
194 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
197 GST_STATIC_CAPS_ANY);
199 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
200 #define GST_CAT_DEFAULT (multi_queue_debug)
202 /* Signals and args */
210 /* default limits, we try to keep up to 2 seconds of data and if there is not
211 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
212 * there is data in the queues. Normally, the byte and time limits are not hit
213 * in theses conditions. */
214 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
215 #define DEFAULT_MAX_SIZE_BUFFERS 5
216 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
218 /* second limits. When we hit one of the above limits we are probably dealing
219 * with a badly muxed file and we scale the limits to these emergency values.
220 * This is currently not yet implemented.
221 * Since we dynamically scale the queue buffer size up to the limits but avoid
222 * going above the max-size-buffers when we can, we don't really need this
223 * aditional extra size. */
224 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
225 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
226 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
228 #define DEFAULT_USE_BUFFERING FALSE
229 #define DEFAULT_LOW_PERCENT 10
230 #define DEFAULT_HIGH_PERCENT 99
231 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
236 PROP_EXTRA_SIZE_BYTES,
237 PROP_EXTRA_SIZE_BUFFERS,
238 PROP_EXTRA_SIZE_TIME,
240 PROP_MAX_SIZE_BUFFERS,
245 PROP_SYNC_BY_RUNNING_TIME,
249 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
250 g_mutex_lock (q->qlock); \
253 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
254 g_mutex_unlock (q->qlock); \
257 static void gst_multi_queue_finalize (GObject * object);
258 static void gst_multi_queue_set_property (GObject * object,
259 guint prop_id, const GValue * value, GParamSpec * pspec);
260 static void gst_multi_queue_get_property (GObject * object,
261 guint prop_id, GValue * value, GParamSpec * pspec);
263 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
264 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
265 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
266 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
267 element, GstStateChange transition);
269 static void gst_multi_queue_loop (GstPad * pad);
272 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
273 #define gst_multi_queue_parent_class parent_class
274 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
277 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
280 gst_multi_queue_class_init (GstMultiQueueClass * klass)
282 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
283 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
285 gobject_class->set_property = gst_multi_queue_set_property;
286 gobject_class->get_property = gst_multi_queue_get_property;
291 * GstMultiQueue::underrun:
292 * @multiqueue: the multqueue instance
294 * This signal is emitted from the streaming thread when there is
295 * no data in any of the queues inside the multiqueue instance (underrun).
297 * This indicates either starvation or EOS from the upstream data sources.
299 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
300 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
301 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
302 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
305 * GstMultiQueue::overrun:
306 * @multiqueue: the multiqueue instance
308 * Reports that one of the queues in the multiqueue is full (overrun).
309 * A queue is full if the total amount of data inside it (num-buffers, time,
310 * size) is higher than the boundary values which can be set through the
311 * GObject properties.
313 * This can be used as an indicator of pre-roll.
315 gst_multi_queue_signals[SIGNAL_OVERRUN] =
316 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
317 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
318 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
322 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
323 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
324 "Max. amount of data in the queue (bytes, 0=disable)",
325 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
326 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
328 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
329 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
330 DEFAULT_MAX_SIZE_BUFFERS,
331 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
332 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
333 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
334 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
335 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
338 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
339 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
340 " (NOT IMPLEMENTED)",
341 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
342 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
344 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
345 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
346 " (NOT IMPLEMENTED)",
347 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
350 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
351 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
352 " (NOT IMPLEMENTED)",
353 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
354 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 * GstMultiQueue:use-buffering
359 * Enable the buffering option in multiqueue so that BUFFERING messages are
360 * emited based on low-/high-percent thresholds.
364 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
365 g_param_spec_boolean ("use-buffering", "Use buffering",
366 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
367 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 * GstMultiQueue:low-percent
371 * Low threshold percent for buffering to start.
375 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
376 g_param_spec_int ("low-percent", "Low percent",
377 "Low threshold for buffering to start", 0, 100,
378 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 * GstMultiQueue:high-percent
382 * High threshold percent for buffering to finish.
386 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
387 g_param_spec_int ("high-percent", "High percent",
388 "High threshold for buffering to finish", 0, 100,
389 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 * GstMultiQueue:sync-by-running-time
394 * If enabled multiqueue will synchronize deactivated or not-linked streams
395 * to the activated and linked streams by taking the running time.
396 * Otherwise multiqueue will synchronize the deactivated or not-linked
397 * streams by keeping the order in which buffers and events arrived compared
398 * to active and linked streams.
402 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
403 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
404 "Synchronize deactivated or not-linked streams by running time",
405 DEFAULT_SYNC_BY_RUNNING_TIME,
406 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 gobject_class->finalize = gst_multi_queue_finalize;
410 gst_element_class_set_details_simple (gstelement_class,
412 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
413 gst_element_class_add_pad_template (gstelement_class,
414 gst_static_pad_template_get (&sinktemplate));
415 gst_element_class_add_pad_template (gstelement_class,
416 gst_static_pad_template_get (&srctemplate));
418 gstelement_class->request_new_pad =
419 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
420 gstelement_class->release_pad =
421 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
422 gstelement_class->change_state =
423 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
427 gst_multi_queue_init (GstMultiQueue * mqueue)
429 mqueue->nbqueues = 0;
430 mqueue->queues = NULL;
432 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
433 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
434 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
436 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
437 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
438 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
440 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
441 mqueue->low_percent = DEFAULT_LOW_PERCENT;
442 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
444 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
448 mqueue->high_time = GST_CLOCK_TIME_NONE;
450 mqueue->qlock = g_mutex_new ();
454 gst_multi_queue_finalize (GObject * object)
456 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
458 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
459 g_list_free (mqueue->queues);
460 mqueue->queues = NULL;
461 mqueue->queues_cookie++;
463 /* free/unref instance data */
464 g_mutex_free (mqueue->qlock);
466 G_OBJECT_CLASS (parent_class)->finalize (object);
469 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
470 GList * tmp = mq->queues; \
472 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
473 q->max_size.format = mq->max_size.format; \
474 tmp = g_list_next(tmp); \
479 gst_multi_queue_set_property (GObject * object, guint prop_id,
480 const GValue * value, GParamSpec * pspec)
482 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
485 case PROP_MAX_SIZE_BYTES:
486 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
487 mq->max_size.bytes = g_value_get_uint (value);
488 SET_CHILD_PROPERTY (mq, bytes);
489 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
491 case PROP_MAX_SIZE_BUFFERS:
492 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
493 mq->max_size.visible = g_value_get_uint (value);
494 SET_CHILD_PROPERTY (mq, visible);
495 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
497 case PROP_MAX_SIZE_TIME:
498 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
499 mq->max_size.time = g_value_get_uint64 (value);
500 SET_CHILD_PROPERTY (mq, time);
501 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
503 case PROP_EXTRA_SIZE_BYTES:
504 mq->extra_size.bytes = g_value_get_uint (value);
506 case PROP_EXTRA_SIZE_BUFFERS:
507 mq->extra_size.visible = g_value_get_uint (value);
509 case PROP_EXTRA_SIZE_TIME:
510 mq->extra_size.time = g_value_get_uint64 (value);
512 case PROP_USE_BUFFERING:
513 mq->use_buffering = g_value_get_boolean (value);
515 case PROP_LOW_PERCENT:
516 mq->low_percent = g_value_get_int (value);
518 case PROP_HIGH_PERCENT:
519 mq->high_percent = g_value_get_int (value);
521 case PROP_SYNC_BY_RUNNING_TIME:
522 mq->sync_by_running_time = g_value_get_boolean (value);
525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531 gst_multi_queue_get_property (GObject * object, guint prop_id,
532 GValue * value, GParamSpec * pspec)
534 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
536 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
539 case PROP_EXTRA_SIZE_BYTES:
540 g_value_set_uint (value, mq->extra_size.bytes);
542 case PROP_EXTRA_SIZE_BUFFERS:
543 g_value_set_uint (value, mq->extra_size.visible);
545 case PROP_EXTRA_SIZE_TIME:
546 g_value_set_uint64 (value, mq->extra_size.time);
548 case PROP_MAX_SIZE_BYTES:
549 g_value_set_uint (value, mq->max_size.bytes);
551 case PROP_MAX_SIZE_BUFFERS:
552 g_value_set_uint (value, mq->max_size.visible);
554 case PROP_MAX_SIZE_TIME:
555 g_value_set_uint64 (value, mq->max_size.time);
557 case PROP_USE_BUFFERING:
558 g_value_set_boolean (value, mq->use_buffering);
560 case PROP_LOW_PERCENT:
561 g_value_set_int (value, mq->low_percent);
563 case PROP_HIGH_PERCENT:
564 g_value_set_int (value, mq->high_percent);
566 case PROP_SYNC_BY_RUNNING_TIME:
567 g_value_set_boolean (value, mq->sync_by_running_time);
570 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
574 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
578 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
580 GstIterator *it = NULL;
582 GstSingleQueue *squeue;
583 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
586 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
587 squeue = gst_pad_get_element_private (pad);
591 if (squeue->sinkpad == pad)
592 opad = gst_object_ref (squeue->srcpad);
593 else if (squeue->srcpad == pad)
594 opad = gst_object_ref (squeue->sinkpad);
598 g_value_init (&val, GST_TYPE_PAD);
599 g_value_set_object (&val, opad);
600 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
601 g_value_unset (&val);
603 gst_object_unref (opad);
606 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
617 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
618 const gchar * name, const GstCaps * caps)
620 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
621 GstSingleQueue *squeue;
625 sscanf (name + 4, "_%u", &temp_id);
626 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
629 /* Create a new single queue, add the sink and source pad and return the sink pad */
630 squeue = gst_single_queue_new (mqueue, temp_id);
632 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
633 GST_DEBUG_PAD_NAME (squeue->sinkpad));
635 return squeue ? squeue->sinkpad : NULL;
639 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
641 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
642 GstSingleQueue *sq = NULL;
645 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
647 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
648 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
649 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
650 sq = (GstSingleQueue *) tmp->data;
652 if (sq->sinkpad == pad)
657 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
658 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
662 /* FIXME: The removal of the singlequeue should probably not happen until it
663 * finishes draining */
665 /* remove it from the list */
666 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
667 mqueue->queues_cookie++;
669 /* FIXME : recompute next-non-linked */
670 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
672 /* delete SingleQueue */
673 gst_data_queue_set_flushing (sq->queue, TRUE);
675 gst_pad_set_active (sq->srcpad, FALSE);
676 gst_pad_set_active (sq->sinkpad, FALSE);
677 gst_pad_set_element_private (sq->srcpad, NULL);
678 gst_pad_set_element_private (sq->sinkpad, NULL);
679 gst_element_remove_pad (element, sq->srcpad);
680 gst_element_remove_pad (element, sq->sinkpad);
681 gst_single_queue_free (sq);
684 static GstStateChangeReturn
685 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
687 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
688 GstSingleQueue *sq = NULL;
689 GstStateChangeReturn result;
691 switch (transition) {
692 case GST_STATE_CHANGE_READY_TO_PAUSED:{
695 /* Set all pads to non-flushing */
696 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
697 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
698 sq = (GstSingleQueue *) tmp->data;
699 sq->flushing = FALSE;
701 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
704 case GST_STATE_CHANGE_PAUSED_TO_READY:{
707 /* Un-wait all waiting pads */
708 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
709 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
710 sq = (GstSingleQueue *) tmp->data;
712 g_cond_signal (sq->turn);
714 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
721 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
723 switch (transition) {
735 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
739 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
743 sq->srcresult = GST_FLOW_WRONG_STATE;
744 gst_data_queue_set_flushing (sq->queue, TRUE);
748 /* wake up non-linked task */
749 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
751 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
752 g_cond_signal (sq->turn);
753 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
755 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
756 result = gst_pad_pause_task (sq->srcpad);
757 sq->sink_tainted = sq->src_tainted = TRUE;
759 gst_data_queue_flush (sq->queue);
760 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
761 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
762 /* All pads start off not-linked for a smooth kick-off */
763 sq->srcresult = GST_FLOW_OK;
765 sq->max_size.visible = mq->max_size.visible;
769 sq->last_oldid = G_MAXUINT32;
770 sq->next_time = GST_CLOCK_TIME_NONE;
771 sq->last_time = GST_CLOCK_TIME_NONE;
772 gst_data_queue_set_flushing (sq->queue, FALSE);
774 /* Reset high time to be recomputed next */
775 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
776 mq->high_time = GST_CLOCK_TIME_NONE;
777 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
779 sq->flushing = FALSE;
781 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
783 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
790 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
792 GstDataQueueSize size;
794 gboolean post = FALSE;
796 /* nothing to dowhen we are not in buffering mode */
797 if (!mq->use_buffering)
800 gst_data_queue_get_level (sq->queue, &size);
802 GST_DEBUG_OBJECT (mq,
803 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
804 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
805 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
807 /* get bytes and time percentages and take the max */
812 if (sq->max_size.time > 0) {
813 tmp = (sq->cur_time * 100) / sq->max_size.time;
814 percent = MAX (percent, tmp);
816 if (sq->max_size.bytes > 0) {
817 tmp = (size.bytes * 100) / sq->max_size.bytes;
818 percent = MAX (percent, tmp);
824 if (percent >= mq->high_percent) {
825 mq->buffering = FALSE;
827 /* make sure it increases */
828 percent = MAX (mq->percent, percent);
830 if (percent == mq->percent)
831 /* don't post if nothing changed */
834 /* else keep last value we posted */
835 mq->percent = percent;
837 if (percent < mq->low_percent) {
838 mq->buffering = TRUE;
839 mq->percent = percent;
846 /* scale to high percent so that it becomes the 100% mark */
847 percent = percent * 100 / mq->high_percent;
852 GST_DEBUG_OBJECT (mq, "buffering %d percent", percent);
853 message = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
855 gst_element_post_message (GST_ELEMENT_CAST (mq), message);
857 GST_DEBUG_OBJECT (mq, "filled %d percent", percent);
861 /* calculate the diff between running time on the sink and src of the queue.
862 * This is the total amount of time in the queue.
865 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
867 gint64 sink_time, src_time;
869 if (sq->sink_tainted) {
870 sink_time = sq->sinktime =
871 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
872 sq->sink_segment.position);
874 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
875 /* if we have a time, we become untainted and use the time */
876 sq->sink_tainted = FALSE;
878 sink_time = sq->sinktime;
880 if (sq->src_tainted) {
881 src_time = sq->srctime =
882 gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
883 sq->src_segment.position);
884 /* if we have a time, we become untainted and use the time */
885 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
886 sq->src_tainted = FALSE;
888 src_time = sq->srctime;
890 GST_DEBUG_OBJECT (mq,
891 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
892 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
894 /* This allows for streams with out of order timestamping - sometimes the
895 * emerging timestamp is later than the arriving one(s) */
896 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
897 sq->cur_time = sink_time - src_time;
901 /* updating the time level can change the buffering state */
902 update_buffering (mq, sq);
907 /* take a SEGMENT event and apply the values to segment, updating the time
910 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
911 GstSegment * segment)
913 gst_event_copy_segment (event, segment);
915 /* now configure the values, we use these to track timestamps on the
917 if (segment->format != GST_FORMAT_TIME) {
918 /* non-time format, pretent the current time segment is closed with a
919 * 0 start and unknown stop time. */
920 segment->format = GST_FORMAT_TIME;
925 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
927 if (segment == &sq->sink_segment)
928 sq->sink_tainted = TRUE;
930 sq->src_tainted = TRUE;
932 GST_DEBUG_OBJECT (mq,
933 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
935 /* segment can update the time level of the queue */
936 update_time_level (mq, sq);
938 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
941 /* take a buffer and update segment, updating the time level of the queue. */
943 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
944 GstClockTime duration, GstSegment * segment)
946 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
948 /* if no timestamp is set, assume it's continuous with the previous
950 if (timestamp == GST_CLOCK_TIME_NONE)
951 timestamp = segment->position;
954 if (duration != GST_CLOCK_TIME_NONE)
955 timestamp += duration;
957 GST_DEBUG_OBJECT (mq, "queue %d, position updated to %" GST_TIME_FORMAT,
958 sq->id, GST_TIME_ARGS (timestamp));
960 segment->position = timestamp;
962 if (segment == &sq->sink_segment)
963 sq->sink_tainted = TRUE;
965 sq->src_tainted = TRUE;
967 /* calc diff with other end */
968 update_time_level (mq, sq);
969 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
973 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
975 GstClockTime time = GST_CLOCK_TIME_NONE;
977 if (GST_IS_BUFFER (object)) {
978 GstBuffer *buf = GST_BUFFER_CAST (object);
980 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
981 time = GST_BUFFER_TIMESTAMP (buf);
982 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
983 time += GST_BUFFER_DURATION (buf);
984 if (time > segment->stop)
985 time = segment->stop;
986 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
988 } else if (GST_IS_BUFFER_LIST (object)) {
989 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
993 n = gst_buffer_list_length (list);
994 for (i = 0; i < n; i++) {
995 buf = gst_buffer_list_get (list, i);
996 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
997 time = GST_BUFFER_TIMESTAMP (buf);
998 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
999 time += GST_BUFFER_DURATION (buf);
1000 if (time > segment->stop)
1001 time = segment->stop;
1002 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1009 } else if (GST_IS_EVENT (object)) {
1010 GstEvent *event = GST_EVENT_CAST (object);
1012 /* For newsegment events return the running time of the start position */
1013 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1014 const GstSegment *new_segment;
1016 gst_event_parse_segment (event, &new_segment);
1017 if (new_segment->format == GST_FORMAT_TIME) {
1019 gst_segment_to_running_time (new_segment, GST_FORMAT_TIME,
1020 new_segment->start);
1029 static GstFlowReturn
1030 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1031 GstMiniObject * object)
1033 GstFlowReturn result = GST_FLOW_OK;
1035 if (GST_IS_BUFFER (object)) {
1037 GstClockTime timestamp, duration;
1039 buffer = GST_BUFFER_CAST (object);
1040 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1041 duration = GST_BUFFER_DURATION (buffer);
1043 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1045 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1046 gst_data_queue_limits_changed (sq->queue);
1048 GST_DEBUG_OBJECT (mq,
1049 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1050 sq->id, buffer, GST_TIME_ARGS (timestamp));
1052 result = gst_pad_push (sq->srcpad, buffer);
1053 } else if (GST_IS_EVENT (object)) {
1056 event = GST_EVENT_CAST (object);
1058 switch (GST_EVENT_TYPE (event)) {
1060 result = GST_FLOW_EOS;
1062 case GST_EVENT_SEGMENT:
1063 apply_segment (mq, sq, event, &sq->src_segment);
1064 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1065 gst_data_queue_limits_changed (sq->queue);
1071 GST_DEBUG_OBJECT (mq,
1072 "SingleQueue %d : Pushing event %p of type %s",
1073 sq->id, event, GST_EVENT_TYPE_NAME (event));
1075 gst_pad_push_event (sq->srcpad, event);
1077 g_warning ("Unexpected object in singlequeue %d (refcounting problem?)",
1085 static GstMiniObject *
1086 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1091 item->object = NULL;
1097 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1100 gst_mini_object_unref (item->object);
1101 g_slice_free (GstMultiQueueItem, item);
1104 /* takes ownership of passed mini object! */
1105 static GstMultiQueueItem *
1106 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1108 GstMultiQueueItem *item;
1110 item = g_slice_new (GstMultiQueueItem);
1111 item->object = object;
1112 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1113 item->posid = curid;
1115 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1116 item->duration = GST_BUFFER_DURATION (object);
1117 if (item->duration == GST_CLOCK_TIME_NONE)
1119 item->visible = TRUE;
1123 static GstMultiQueueItem *
1124 gst_multi_queue_event_item_new (GstMiniObject * object, guint32 curid)
1126 GstMultiQueueItem *item;
1128 item = g_slice_new (GstMultiQueueItem);
1129 item->object = object;
1130 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1131 item->posid = curid;
1135 item->visible = FALSE;
1139 /* Each main loop attempts to push buffers until the return value
1140 * is not-linked. not-linked pads are not allowed to push data beyond
1141 * any linked pads, so they don't 'rush ahead of the pack'.
1144 gst_multi_queue_loop (GstPad * pad)
1147 GstMultiQueueItem *item;
1148 GstDataQueueItem *sitem;
1150 GstMiniObject *object = NULL;
1152 GstFlowReturn result;
1153 GstClockTime next_time;
1155 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1158 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1163 /* Get something from the queue, blocking until that happens, or we get
1165 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1168 item = (GstMultiQueueItem *) sitem;
1169 newid = item->posid;
1171 /* steal the object and destroy the item */
1172 object = gst_multi_queue_item_steal_object (item);
1173 gst_multi_queue_item_destroy (item);
1175 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1176 next_time = get_running_time (&sq->src_segment, object, TRUE);
1178 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1179 sq->id, newid, sq->last_oldid);
1181 /* If we're not-linked, we do some extra work because we might need to
1182 * wait before pushing. If we're linked but there's a gap in the IDs,
1183 * or it's the first loop, or we just passed the previous highid,
1184 * we might need to wake some sleeping pad up, so there's extra work
1186 if (sq->srcresult == GST_FLOW_NOT_LINKED
1187 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1188 || sq->last_oldid > mq->highid) {
1189 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1190 gst_flow_get_name (sq->srcresult));
1192 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1194 /* Check again if we're flushing after the lock is taken,
1195 * the flush flag might have been changed in the meantime */
1197 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1201 /* Update the nextid so other threads know when to wake us up */
1203 sq->next_time = next_time;
1205 /* Update the oldid (the last ID we output) for highid tracking */
1206 if (sq->last_oldid != G_MAXUINT32)
1207 sq->oldid = sq->last_oldid;
1209 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1210 /* Go to sleep until it's time to push this buffer */
1212 /* Recompute the highid */
1213 compute_high_id (mq);
1214 /* Recompute the high time */
1215 compute_high_time (mq);
1217 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1218 (mq->high_time == GST_CLOCK_TIME_NONE
1219 || next_time >= mq->high_time))
1220 || (!mq->sync_by_running_time && newid > mq->highid))
1221 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1223 GST_DEBUG_OBJECT (mq,
1224 "queue %d sleeping for not-linked wakeup with "
1225 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1226 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1227 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1229 /* Wake up all non-linked pads before we sleep */
1230 wake_up_next_non_linked (mq);
1233 g_cond_wait (sq->turn, mq->qlock);
1237 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1241 /* Recompute the high time */
1242 compute_high_time (mq);
1244 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1245 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1246 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1247 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1250 /* Re-compute the high_id in case someone else pushed */
1251 compute_high_id (mq);
1253 compute_high_id (mq);
1254 /* Wake up all non-linked pads */
1255 wake_up_next_non_linked (mq);
1257 /* We're done waiting, we can clear the nextid and nexttime */
1259 sq->next_time = GST_CLOCK_TIME_NONE;
1261 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1267 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1268 gst_flow_get_name (sq->srcresult));
1270 /* Update time stats */
1271 next_time = get_running_time (&sq->src_segment, object, FALSE);
1272 if (next_time != GST_CLOCK_TIME_NONE) {
1273 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1274 sq->last_time = next_time;
1275 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1276 /* Wake up all non-linked pads now that we advanced the high time */
1277 mq->high_time = next_time;
1278 wake_up_next_non_linked (mq);
1282 /* Try to push out the new object */
1283 result = gst_single_queue_push_one (mq, sq, object);
1284 sq->srcresult = result;
1287 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1288 && result != GST_FLOW_EOS)
1291 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1292 gst_flow_get_name (sq->srcresult));
1294 sq->last_oldid = newid;
1301 gst_mini_object_unref (object);
1303 /* Need to make sure wake up any sleeping pads when we exit */
1304 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1305 compute_high_id (mq);
1306 wake_up_next_non_linked (mq);
1307 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1309 /* upstream needs to see fatal result ASAP to shut things down,
1310 * but might be stuck in one of our other full queues;
1311 * so empty this one and trigger dynamic queue growth. At
1312 * this point the srcresult is not OK, NOT_LINKED
1313 * or EOS, i.e. a real failure */
1314 gst_data_queue_flush (sq->queue);
1315 single_queue_underrun_cb (sq->queue, sq);
1316 gst_data_queue_set_flushing (sq->queue, TRUE);
1317 gst_pad_pause_task (sq->srcpad);
1318 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1319 "SingleQueue[%d] task paused, reason:%s",
1320 sq->id, gst_flow_get_name (sq->srcresult));
1326 * gst_multi_queue_chain:
1328 * This is similar to GstQueue's chain function, except:
1329 * _ we don't have leak behaviours,
1330 * _ we push with a unique id (curid)
1332 static GstFlowReturn
1333 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1337 GstMultiQueueItem *item;
1339 GstClockTime timestamp, duration;
1341 sq = gst_pad_get_element_private (pad);
1344 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1348 /* Get a unique incrementing id */
1349 curid = G_ATOMIC_INT_ADD ((gint *) & mq->counter, 1);
1351 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1352 sq->id, buffer, curid);
1354 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1356 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1357 duration = GST_BUFFER_DURATION (buffer);
1359 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1362 /* update time level, we must do this after pushing the data in the queue so
1363 * that we never end up filling the queue first. */
1364 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1367 return sq->srcresult;
1372 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1373 sq->id, gst_flow_get_name (sq->srcresult));
1374 gst_multi_queue_item_destroy (item);
1379 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
1380 gst_buffer_unref (buffer);
1381 return GST_FLOW_EOS;
1386 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
1387 GstPadMode mode, gboolean active)
1392 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1395 case GST_PAD_MODE_PUSH:
1397 /* All pads start off linked until they push one buffer */
1398 sq->srcresult = GST_FLOW_OK;
1400 sq->srcresult = GST_FLOW_WRONG_STATE;
1401 gst_data_queue_flush (sq->queue);
1413 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1418 GstMultiQueueItem *item;
1421 GstEvent *sref = NULL;
1423 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1424 mq = (GstMultiQueue *) parent;
1426 type = GST_EVENT_TYPE (event);
1429 case GST_EVENT_FLUSH_START:
1430 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1433 res = gst_pad_push_event (sq->srcpad, event);
1435 gst_single_queue_flush (mq, sq, TRUE);
1438 case GST_EVENT_FLUSH_STOP:
1439 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1442 res = gst_pad_push_event (sq->srcpad, event);
1444 gst_single_queue_flush (mq, sq, FALSE);
1446 case GST_EVENT_SEGMENT:
1447 /* take ref because the queue will take ownership and we need the event
1448 * afterwards to update the segment */
1449 sref = gst_event_ref (event);
1453 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1454 res = gst_pad_push_event (sq->srcpad, event);
1460 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1464 /* Get an unique incrementing id. */
1465 curid = G_ATOMIC_INT_ADD ((gint *) & mq->counter, 1);
1467 item = gst_multi_queue_event_item_new ((GstMiniObject *) event, curid);
1469 GST_DEBUG_OBJECT (mq,
1470 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1471 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1473 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1476 /* mark EOS when we received one, we must do that after putting the
1477 * buffer in the queue because EOS marks the buffer as filled. No need to take
1478 * a lock, the _check_full happens from this thread only, right before pushing
1479 * into dataqueue. */
1483 /* EOS affects the buffering state */
1484 update_buffering (mq, sq);
1485 single_queue_overrun_cb (sq->queue, sq);
1487 case GST_EVENT_SEGMENT:
1488 apply_segment (mq, sq, sref, &sq->sink_segment);
1489 gst_event_unref (sref);
1499 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1500 sq->id, gst_flow_get_name (sq->srcresult));
1502 gst_event_unref (sref);
1503 gst_multi_queue_item_destroy (item);
1508 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1509 gst_event_unref (event);
1516 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1520 switch (GST_QUERY_TYPE (query)) {
1521 case GST_QUERY_ACCEPT_CAPS:
1522 case GST_QUERY_CAPS:
1524 /* default handling */
1525 res = gst_pad_query_default (pad, parent, query);
1532 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
1533 GstPadMode mode, gboolean active)
1539 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1542 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1545 case GST_PAD_MODE_PUSH:
1547 result = gst_single_queue_flush (mq, sq, FALSE);
1549 result = gst_single_queue_flush (mq, sq, TRUE);
1550 /* make sure streaming finishes */
1551 result |= gst_pad_stop_task (pad);
1562 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1564 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1566 return gst_pad_push_event (sq->sinkpad, event);
1570 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1574 /* FIXME, Handle position offset depending on queue size */
1575 switch (GST_QUERY_TYPE (query)) {
1577 /* default handling */
1578 res = gst_pad_query_default (pad, parent, query);
1585 * Next-non-linked functions
1588 /* WITH LOCK TAKEN */
1590 wake_up_next_non_linked (GstMultiQueue * mq)
1594 /* maybe no-one is waiting */
1595 if (mq->numwaiting < 1)
1598 /* Else figure out which singlequeue(s) need waking up */
1599 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1600 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1602 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1603 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
1604 && sq->next_time != GST_CLOCK_TIME_NONE
1605 && sq->next_time >= mq->high_time)
1606 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
1607 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1608 g_cond_signal (sq->turn);
1614 /* WITH LOCK TAKEN */
1616 compute_high_id (GstMultiQueue * mq)
1618 /* The high-id is either the highest id among the linked pads, or if all
1619 * pads are not-linked, it's the lowest not-linked pad */
1621 guint32 lowest = G_MAXUINT32;
1622 guint32 highid = G_MAXUINT32;
1624 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1625 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1627 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1628 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1630 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1631 /* No need to consider queues which are not waiting */
1632 if (sq->nextid == 0) {
1633 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1637 if (sq->nextid < lowest)
1638 lowest = sq->nextid;
1639 } else if (sq->srcresult != GST_FLOW_EOS) {
1640 /* If we don't have a global highid, or the global highid is lower than
1641 * this single queue's last outputted id, store the queue's one,
1642 * unless the singlequeue is at EOS (srcresult = EOS) */
1643 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1648 if (highid == G_MAXUINT32 || lowest < highid)
1649 mq->highid = lowest;
1651 mq->highid = highid;
1653 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1657 /* WITH LOCK TAKEN */
1659 compute_high_time (GstMultiQueue * mq)
1661 /* The high-id is either the highest id among the linked pads, or if all
1662 * pads are not-linked, it's the lowest not-linked pad */
1664 GstClockTime highest = GST_CLOCK_TIME_NONE;
1665 GstClockTime lowest = GST_CLOCK_TIME_NONE;
1667 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1668 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1671 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
1672 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
1673 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
1675 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1676 /* No need to consider queues which are not waiting */
1677 if (sq->next_time == GST_CLOCK_TIME_NONE) {
1678 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1682 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
1683 lowest = sq->next_time;
1684 } else if (sq->srcresult != GST_FLOW_EOS) {
1685 /* If we don't have a global highid, or the global highid is lower than
1686 * this single queue's last outputted id, store the queue's one,
1687 * unless the singlequeue is at EOS (srcresult = EOS) */
1688 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
1689 highest = sq->last_time;
1693 mq->high_time = highest;
1696 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
1697 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
1700 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
1701 ((q)->max_size.format) <= (value))
1704 * GstSingleQueue functions
1707 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1709 GstMultiQueue *mq = sq->mqueue;
1711 GstDataQueueSize size;
1712 gboolean filled = FALSE;
1714 gst_data_queue_get_level (sq->queue, &size);
1716 GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
1718 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1719 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1720 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1721 GstDataQueueSize ssize;
1723 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
1725 if (gst_data_queue_is_empty (oq->queue)) {
1726 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
1727 if (IS_FILLED (sq, visible, size.visible)) {
1728 sq->max_size.visible = size.visible + 1;
1729 GST_DEBUG_OBJECT (mq,
1730 "Another queue is empty, bumping single queue %d max visible to %d",
1731 sq->id, sq->max_size.visible);
1733 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1736 /* check if we reached the hard time/bytes limits */
1737 gst_data_queue_get_level (oq->queue, &ssize);
1739 GST_DEBUG_OBJECT (mq,
1740 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1741 G_GUINT64_FORMAT, oq->id, ssize.visible, oq->max_size.visible,
1742 ssize.bytes, oq->max_size.bytes, oq->cur_time, oq->max_size.time);
1744 /* if this queue is filled completely we must signal overrun.
1745 * FIXME, this seems wrong in many ways
1746 * - we're comparing the filled level of this queue against the
1747 * values of the other one
1748 * - we should only do this after we found no empty queues, ie, move
1749 * this check outside of the loop
1750 * - the debug statement talks about a different queue than the one
1751 * we are checking here.
1753 if (sq->is_eos || IS_FILLED (sq, bytes, ssize.bytes) ||
1754 IS_FILLED (sq, time, sq->cur_time)) {
1755 GST_LOG_OBJECT (mq, "Queue %d is filled", oq->id);
1759 /* no queues were empty */
1760 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1762 /* Overrun is always forwarded, since this is blocking the upstream element */
1764 GST_DEBUG_OBJECT (mq, "A queue is filled, signalling overrun");
1765 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1773 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1775 gboolean empty = TRUE;
1776 GstMultiQueue *mq = sq->mqueue;
1780 "Single Queue %d is empty, Checking other single queues", sq->id);
1782 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1783 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1784 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1786 if (gst_data_queue_is_full (oq->queue)) {
1787 GstDataQueueSize size;
1789 gst_data_queue_get_level (oq->queue, &size);
1790 if (IS_FILLED (oq, visible, size.visible)) {
1791 oq->max_size.visible = size.visible + 1;
1792 GST_DEBUG_OBJECT (mq,
1793 "queue %d is filled, bumping its max visible to %d", oq->id,
1794 oq->max_size.visible);
1795 gst_data_queue_limits_changed (oq->queue);
1798 if (!gst_data_queue_is_empty (oq->queue))
1801 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1804 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1805 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1810 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1811 guint64 time, GstSingleQueue * sq)
1814 GstMultiQueue *mq = sq->mqueue;
1816 GST_DEBUG_OBJECT (mq,
1817 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1818 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1819 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1821 /* we are always filled on EOS */
1825 /* we never go past the max visible items unless we are in buffering mode */
1826 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
1829 /* check time or bytes */
1830 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
1836 gst_single_queue_free (GstSingleQueue * sq)
1839 gst_data_queue_flush (sq->queue);
1840 g_object_unref (sq->queue);
1841 g_cond_free (sq->turn);
1845 static GstSingleQueue *
1846 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
1851 guint temp_id = (id == -1) ? 0 : id;
1853 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1855 /* Find an unused queue ID, if possible the passed one */
1856 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1857 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1858 /* This works because the IDs are sorted in ascending order */
1859 if (sq2->id == temp_id) {
1860 /* If this ID was requested by the caller return NULL,
1861 * otherwise just get us the next one */
1863 temp_id = sq2->id + 1;
1866 } else if (sq2->id > temp_id) {
1871 sq = g_new0 (GstSingleQueue, 1);
1875 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
1876 mqueue->queues_cookie++;
1878 /* copy over max_size and extra_size so we don't need to take the lock
1879 * any longer when checking if the queue is full. */
1880 sq->max_size.visible = mqueue->max_size.visible;
1881 sq->max_size.bytes = mqueue->max_size.bytes;
1882 sq->max_size.time = mqueue->max_size.time;
1884 sq->extra_size.visible = mqueue->extra_size.visible;
1885 sq->extra_size.bytes = mqueue->extra_size.bytes;
1886 sq->extra_size.time = mqueue->extra_size.time;
1888 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
1890 sq->mqueue = mqueue;
1891 sq->srcresult = GST_FLOW_WRONG_STATE;
1892 sq->queue = gst_data_queue_new_full ((GstDataQueueCheckFullFunction)
1893 single_queue_check_full,
1894 (GstDataQueueFullCallback) single_queue_overrun_cb,
1895 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
1897 sq->flushing = FALSE;
1898 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1899 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1903 sq->next_time = GST_CLOCK_TIME_NONE;
1904 sq->last_time = GST_CLOCK_TIME_NONE;
1905 sq->turn = g_cond_new ();
1907 sq->sinktime = GST_CLOCK_TIME_NONE;
1908 sq->srctime = GST_CLOCK_TIME_NONE;
1909 sq->sink_tainted = TRUE;
1910 sq->src_tainted = TRUE;
1912 name = g_strdup_printf ("sink_%u", sq->id);
1913 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
1916 gst_pad_set_chain_function (sq->sinkpad,
1917 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
1918 gst_pad_set_activatemode_function (sq->sinkpad,
1919 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
1920 gst_pad_set_event_function (sq->sinkpad,
1921 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
1922 gst_pad_set_query_function (sq->sinkpad,
1923 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
1924 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
1925 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1926 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1928 name = g_strdup_printf ("src_%u", sq->id);
1929 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
1932 gst_pad_set_activatemode_function (sq->srcpad,
1933 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
1934 gst_pad_set_event_function (sq->srcpad,
1935 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
1936 gst_pad_set_query_function (sq->srcpad,
1937 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
1938 gst_pad_set_iterate_internal_links_function (sq->srcpad,
1939 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1940 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1942 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
1943 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
1945 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1947 /* only activate the pads when we are not in the NULL state
1948 * and add the pad under the state_lock to prevend state changes
1949 * between activating and adding */
1950 g_static_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
1951 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
1952 gst_pad_set_active (sq->srcpad, TRUE);
1953 gst_pad_set_active (sq->sinkpad, TRUE);
1955 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
1956 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
1957 g_static_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
1959 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",