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)
112 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
113 * with newer GLib versions (>= 2.31.0) */
114 #define GLIB_DISABLE_DEPRECATION_WARNINGS
118 #include "gstmultiqueue.h"
119 #include <gst/glib-compat-private.h>
123 * @sinkpad: associated sink #GstPad
124 * @srcpad: associated source #GstPad
126 * Structure containing all information and properties about
129 typedef struct _GstSingleQueue GstSingleQueue;
131 struct _GstSingleQueue
133 /* unique identifier of the queue */
136 GstMultiQueue *mqueue;
141 /* flowreturn of previous srcpad push */
142 GstFlowReturn srcresult;
145 GstSegment sink_segment;
146 GstSegment src_segment;
148 /* position of src/sink */
149 GstClockTime sinktime, srctime;
150 /* TRUE if either position needs to be recalculated */
151 gboolean sink_tainted, src_tainted;
155 GstDataQueueSize max_size, extra_size;
156 GstClockTime cur_time;
160 /* Protected by global lock */
161 guint32 nextid; /* ID of the next object waiting to be pushed */
162 guint32 oldid; /* ID of the last object pushed (last in a series) */
163 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
164 GstClockTime next_time; /* End running time of next buffer to be pushed */
165 GstClockTime last_time; /* Start running time of last pushed buffer */
166 GCond *turn; /* SingleQueue turn waiting conditional */
170 /* Extension of GstDataQueueItem structure for our usage */
171 typedef struct _GstMultiQueueItem GstMultiQueueItem;
173 struct _GstMultiQueueItem
175 GstMiniObject *object;
180 GDestroyNotify destroy;
184 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, gint id);
185 static void gst_single_queue_free (GstSingleQueue * squeue);
187 static void wake_up_next_non_linked (GstMultiQueue * mq);
188 static void compute_high_id (GstMultiQueue * mq);
189 static void compute_high_time (GstMultiQueue * mq);
190 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
191 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
193 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink%d",
196 GST_STATIC_CAPS_ANY);
198 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
201 GST_STATIC_CAPS_ANY);
203 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
204 #define GST_CAT_DEFAULT (multi_queue_debug)
206 /* Signals and args */
214 /* default limits, we try to keep up to 2 seconds of data and if there is not
215 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
216 * there is data in the queues. Normally, the byte and time limits are not hit
217 * in theses conditions. */
218 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
219 #define DEFAULT_MAX_SIZE_BUFFERS 5
220 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
222 /* second limits. When we hit one of the above limits we are probably dealing
223 * with a badly muxed file and we scale the limits to these emergency values.
224 * This is currently not yet implemented.
225 * Since we dynamically scale the queue buffer size up to the limits but avoid
226 * going above the max-size-buffers when we can, we don't really need this
227 * aditional extra size. */
228 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
229 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
230 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
232 #define DEFAULT_USE_BUFFERING FALSE
233 #define DEFAULT_LOW_PERCENT 10
234 #define DEFAULT_HIGH_PERCENT 99
235 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
240 PROP_EXTRA_SIZE_BYTES,
241 PROP_EXTRA_SIZE_BUFFERS,
242 PROP_EXTRA_SIZE_TIME,
244 PROP_MAX_SIZE_BUFFERS,
249 PROP_SYNC_BY_RUNNING_TIME,
253 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
254 g_mutex_lock (q->qlock); \
257 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
258 g_mutex_unlock (q->qlock); \
261 static void gst_multi_queue_finalize (GObject * object);
262 static void gst_multi_queue_set_property (GObject * object,
263 guint prop_id, const GValue * value, GParamSpec * pspec);
264 static void gst_multi_queue_get_property (GObject * object,
265 guint prop_id, GValue * value, GParamSpec * pspec);
267 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
268 GstPadTemplate * temp, const gchar * name);
269 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
270 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
271 element, GstStateChange transition);
273 static void gst_multi_queue_loop (GstPad * pad);
275 #define _do_init(bla) \
276 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
278 GST_BOILERPLATE_FULL (GstMultiQueue, gst_multi_queue, GstElement,
279 GST_TYPE_ELEMENT, _do_init);
281 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
284 gst_multi_queue_base_init (gpointer g_class)
286 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
288 gst_element_class_set_details_simple (gstelement_class,
290 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
291 gst_element_class_add_pad_template (gstelement_class,
292 gst_static_pad_template_get (&sinktemplate));
293 gst_element_class_add_pad_template (gstelement_class,
294 gst_static_pad_template_get (&srctemplate));
298 gst_multi_queue_class_init (GstMultiQueueClass * klass)
300 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
301 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
303 gobject_class->set_property = gst_multi_queue_set_property;
304 gobject_class->get_property = gst_multi_queue_get_property;
309 * GstMultiQueue::underrun:
310 * @multiqueue: the multqueue instance
312 * This signal is emitted from the streaming thread when there is
313 * no data in any of the queues inside the multiqueue instance (underrun).
315 * This indicates either starvation or EOS from the upstream data sources.
317 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
318 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
319 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
320 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
323 * GstMultiQueue::overrun:
324 * @multiqueue: the multiqueue instance
326 * Reports that one of the queues in the multiqueue is full (overrun).
327 * A queue is full if the total amount of data inside it (num-buffers, time,
328 * size) is higher than the boundary values which can be set through the
329 * GObject properties.
331 * This can be used as an indicator of pre-roll.
333 gst_multi_queue_signals[SIGNAL_OVERRUN] =
334 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
335 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
336 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
340 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
341 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
342 "Max. amount of data in the queue (bytes, 0=disable)",
343 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
346 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
347 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
348 DEFAULT_MAX_SIZE_BUFFERS,
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
351 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
352 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
353 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
356 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
357 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
358 " (NOT IMPLEMENTED)",
359 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
362 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
363 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
364 " (NOT IMPLEMENTED)",
365 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
366 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
368 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
369 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
370 " (NOT IMPLEMENTED)",
371 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
372 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375 * GstMultiQueue:use-buffering
377 * Enable the buffering option in multiqueue so that BUFFERING messages are
378 * emited based on low-/high-percent thresholds.
382 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
383 g_param_spec_boolean ("use-buffering", "Use buffering",
384 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
385 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
387 * GstMultiQueue:low-percent
389 * Low threshold percent for buffering to start.
393 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
394 g_param_spec_int ("low-percent", "Low percent",
395 "Low threshold for buffering to start", 0, 100,
396 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 * GstMultiQueue:high-percent
400 * High threshold percent for buffering to finish.
404 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
405 g_param_spec_int ("high-percent", "High percent",
406 "High threshold for buffering to finish", 0, 100,
407 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410 * GstMultiQueue:sync-by-running-time
412 * If enabled multiqueue will synchronize deactivated or not-linked streams
413 * to the activated and linked streams by taking the running time.
414 * Otherwise multiqueue will synchronize the deactivated or not-linked
415 * streams by keeping the order in which buffers and events arrived compared
416 * to active and linked streams.
420 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
421 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
422 "Synchronize deactivated or not-linked streams by running time",
423 DEFAULT_SYNC_BY_RUNNING_TIME,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 gobject_class->finalize = gst_multi_queue_finalize;
428 gstelement_class->request_new_pad =
429 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
430 gstelement_class->release_pad =
431 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
432 gstelement_class->change_state =
433 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
437 gst_multi_queue_init (GstMultiQueue * mqueue, GstMultiQueueClass * klass)
439 mqueue->nbqueues = 0;
440 mqueue->queues = NULL;
442 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
443 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
444 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
446 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
447 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
448 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
450 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
451 mqueue->low_percent = DEFAULT_LOW_PERCENT;
452 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
454 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
458 mqueue->high_time = GST_CLOCK_TIME_NONE;
460 mqueue->qlock = g_mutex_new ();
464 gst_multi_queue_finalize (GObject * object)
466 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
468 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
469 g_list_free (mqueue->queues);
470 mqueue->queues = NULL;
471 mqueue->queues_cookie++;
473 /* free/unref instance data */
474 g_mutex_free (mqueue->qlock);
476 G_OBJECT_CLASS (parent_class)->finalize (object);
479 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
480 GList * tmp = mq->queues; \
482 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
483 q->max_size.format = mq->max_size.format; \
484 tmp = g_list_next(tmp); \
489 gst_multi_queue_set_property (GObject * object, guint prop_id,
490 const GValue * value, GParamSpec * pspec)
492 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
495 case PROP_MAX_SIZE_BYTES:
496 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
497 mq->max_size.bytes = g_value_get_uint (value);
498 SET_CHILD_PROPERTY (mq, bytes);
499 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
501 case PROP_MAX_SIZE_BUFFERS:
502 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
503 mq->max_size.visible = g_value_get_uint (value);
504 SET_CHILD_PROPERTY (mq, visible);
505 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
507 case PROP_MAX_SIZE_TIME:
508 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
509 mq->max_size.time = g_value_get_uint64 (value);
510 SET_CHILD_PROPERTY (mq, time);
511 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
513 case PROP_EXTRA_SIZE_BYTES:
514 mq->extra_size.bytes = g_value_get_uint (value);
516 case PROP_EXTRA_SIZE_BUFFERS:
517 mq->extra_size.visible = g_value_get_uint (value);
519 case PROP_EXTRA_SIZE_TIME:
520 mq->extra_size.time = g_value_get_uint64 (value);
522 case PROP_USE_BUFFERING:
523 mq->use_buffering = g_value_get_boolean (value);
525 case PROP_LOW_PERCENT:
526 mq->low_percent = g_value_get_int (value);
528 case PROP_HIGH_PERCENT:
529 mq->high_percent = g_value_get_int (value);
531 case PROP_SYNC_BY_RUNNING_TIME:
532 mq->sync_by_running_time = g_value_get_boolean (value);
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 gst_multi_queue_get_property (GObject * object, guint prop_id,
542 GValue * value, GParamSpec * pspec)
544 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
546 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
549 case PROP_EXTRA_SIZE_BYTES:
550 g_value_set_uint (value, mq->extra_size.bytes);
552 case PROP_EXTRA_SIZE_BUFFERS:
553 g_value_set_uint (value, mq->extra_size.visible);
555 case PROP_EXTRA_SIZE_TIME:
556 g_value_set_uint64 (value, mq->extra_size.time);
558 case PROP_MAX_SIZE_BYTES:
559 g_value_set_uint (value, mq->max_size.bytes);
561 case PROP_MAX_SIZE_BUFFERS:
562 g_value_set_uint (value, mq->max_size.visible);
564 case PROP_MAX_SIZE_TIME:
565 g_value_set_uint64 (value, mq->max_size.time);
567 case PROP_USE_BUFFERING:
568 g_value_set_boolean (value, mq->use_buffering);
570 case PROP_LOW_PERCENT:
571 g_value_set_int (value, mq->low_percent);
573 case PROP_HIGH_PERCENT:
574 g_value_set_int (value, mq->high_percent);
576 case PROP_SYNC_BY_RUNNING_TIME:
577 g_value_set_boolean (value, mq->sync_by_running_time);
580 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
584 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
588 gst_multi_queue_iterate_internal_links (GstPad * pad)
590 GstIterator *it = NULL;
592 GstSingleQueue *squeue;
593 GstMultiQueue *mq = GST_MULTI_QUEUE (gst_pad_get_parent (pad));
595 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
596 squeue = gst_pad_get_element_private (pad);
600 if (squeue->sinkpad == pad)
601 opad = gst_object_ref (squeue->srcpad);
602 else if (squeue->srcpad == pad)
603 opad = gst_object_ref (squeue->sinkpad);
607 it = gst_iterator_new_single (GST_TYPE_PAD, opad,
608 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
610 gst_object_unref (opad);
613 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
614 gst_object_unref (mq);
625 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
628 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
629 GstSingleQueue *squeue;
633 sscanf (name + 4, "%d", &temp_id);
634 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
637 /* Create a new single queue, add the sink and source pad and return the sink pad */
638 squeue = gst_single_queue_new (mqueue, temp_id);
640 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
641 GST_DEBUG_PAD_NAME (squeue->sinkpad));
643 return squeue ? squeue->sinkpad : NULL;
647 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
649 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
650 GstSingleQueue *sq = NULL;
653 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
655 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
656 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
657 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
658 sq = (GstSingleQueue *) tmp->data;
660 if (sq->sinkpad == pad)
665 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
666 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
670 /* FIXME: The removal of the singlequeue should probably not happen until it
671 * finishes draining */
673 /* remove it from the list */
674 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
675 mqueue->queues_cookie++;
677 /* FIXME : recompute next-non-linked */
678 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
680 /* delete SingleQueue */
681 gst_data_queue_set_flushing (sq->queue, TRUE);
683 gst_pad_set_active (sq->srcpad, FALSE);
684 gst_pad_set_active (sq->sinkpad, FALSE);
685 gst_pad_set_element_private (sq->srcpad, NULL);
686 gst_pad_set_element_private (sq->sinkpad, NULL);
687 gst_element_remove_pad (element, sq->srcpad);
688 gst_element_remove_pad (element, sq->sinkpad);
689 gst_single_queue_free (sq);
692 static GstStateChangeReturn
693 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
695 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
696 GstSingleQueue *sq = NULL;
697 GstStateChangeReturn result;
699 switch (transition) {
700 case GST_STATE_CHANGE_READY_TO_PAUSED:{
703 /* Set all pads to non-flushing */
704 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
705 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
706 sq = (GstSingleQueue *) tmp->data;
707 sq->flushing = FALSE;
709 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
712 case GST_STATE_CHANGE_PAUSED_TO_READY:{
715 /* Un-wait all waiting pads */
716 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
717 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
718 sq = (GstSingleQueue *) tmp->data;
720 g_cond_signal (sq->turn);
722 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
729 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
731 switch (transition) {
743 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
747 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
751 sq->srcresult = GST_FLOW_WRONG_STATE;
752 gst_data_queue_set_flushing (sq->queue, TRUE);
756 /* wake up non-linked task */
757 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
759 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
760 g_cond_signal (sq->turn);
761 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
763 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
764 result = gst_pad_pause_task (sq->srcpad);
765 sq->sink_tainted = sq->src_tainted = TRUE;
767 gst_data_queue_flush (sq->queue);
768 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
769 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
770 /* All pads start off not-linked for a smooth kick-off */
771 sq->srcresult = GST_FLOW_OK;
773 sq->max_size.visible = mq->max_size.visible;
777 sq->last_oldid = G_MAXUINT32;
778 sq->next_time = GST_CLOCK_TIME_NONE;
779 sq->last_time = GST_CLOCK_TIME_NONE;
780 gst_data_queue_set_flushing (sq->queue, FALSE);
782 /* Reset high time to be recomputed next */
783 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
784 mq->high_time = GST_CLOCK_TIME_NONE;
785 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
787 sq->flushing = FALSE;
789 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
791 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
798 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
800 GstDataQueueSize size;
802 gboolean post = FALSE;
804 /* nothing to dowhen we are not in buffering mode */
805 if (!mq->use_buffering)
808 gst_data_queue_get_level (sq->queue, &size);
810 GST_DEBUG_OBJECT (mq,
811 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
812 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
813 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
815 /* get bytes and time percentages and take the max */
820 if (sq->max_size.time > 0) {
821 tmp = (sq->cur_time * 100) / sq->max_size.time;
822 percent = MAX (percent, tmp);
824 if (sq->max_size.bytes > 0) {
825 tmp = (size.bytes * 100) / sq->max_size.bytes;
826 percent = MAX (percent, tmp);
832 if (percent >= mq->high_percent) {
833 mq->buffering = FALSE;
835 /* make sure it increases */
836 percent = MAX (mq->percent, percent);
838 if (percent == mq->percent)
839 /* don't post if nothing changed */
842 /* else keep last value we posted */
843 mq->percent = percent;
845 if (percent < mq->low_percent) {
846 mq->buffering = TRUE;
847 mq->percent = percent;
854 /* scale to high percent so that it becomes the 100% mark */
855 percent = percent * 100 / mq->high_percent;
860 GST_DEBUG_OBJECT (mq, "buffering %d percent", percent);
861 message = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
863 gst_element_post_message (GST_ELEMENT_CAST (mq), message);
865 GST_DEBUG_OBJECT (mq, "filled %d percent", percent);
869 /* calculate the diff between running time on the sink and src of the queue.
870 * This is the total amount of time in the queue.
873 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
875 gint64 sink_time, src_time;
877 if (sq->sink_tainted) {
878 sink_time = sq->sinktime =
879 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
880 sq->sink_segment.last_stop);
882 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
883 /* if we have a time, we become untainted and use the time */
884 sq->sink_tainted = FALSE;
886 sink_time = sq->sinktime;
888 if (sq->src_tainted) {
889 src_time = sq->srctime =
890 gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
891 sq->src_segment.last_stop);
892 /* if we have a time, we become untainted and use the time */
893 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
894 sq->src_tainted = FALSE;
896 src_time = sq->srctime;
898 GST_DEBUG_OBJECT (mq,
899 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
900 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
902 /* This allows for streams with out of order timestamping - sometimes the
903 * emerging timestamp is later than the arriving one(s) */
904 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
905 sq->cur_time = sink_time - src_time;
909 /* updating the time level can change the buffering state */
910 update_buffering (mq, sq);
915 /* take a NEWSEGMENT event and apply the values to segment, updating the time
918 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
919 GstSegment * segment)
924 gint64 start, stop, time;
926 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
927 &format, &start, &stop, &time);
929 /* now configure the values, we use these to track timestamps on the
931 if (format != GST_FORMAT_TIME) {
932 /* non-time format, pretent the current time segment is closed with a
933 * 0 start and unknown stop time. */
935 format = GST_FORMAT_TIME;
941 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
943 gst_segment_set_newsegment_full (segment, update,
944 rate, arate, format, start, stop, time);
946 if (segment == &sq->sink_segment)
947 sq->sink_tainted = TRUE;
949 sq->src_tainted = TRUE;
951 GST_DEBUG_OBJECT (mq,
952 "queue %d, configured NEWSEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
954 /* segment can update the time level of the queue */
955 update_time_level (mq, sq);
957 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
960 /* take a buffer and update segment, updating the time level of the queue. */
962 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
963 GstClockTime duration, GstSegment * segment)
965 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
967 /* if no timestamp is set, assume it's continuous with the previous
969 if (timestamp == GST_CLOCK_TIME_NONE)
970 timestamp = segment->last_stop;
973 if (duration != GST_CLOCK_TIME_NONE)
974 timestamp += duration;
976 GST_DEBUG_OBJECT (mq, "queue %d, last_stop updated to %" GST_TIME_FORMAT,
977 sq->id, GST_TIME_ARGS (timestamp));
979 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
981 if (segment == &sq->sink_segment)
982 sq->sink_tainted = TRUE;
984 sq->src_tainted = TRUE;
986 /* calc diff with other end */
987 update_time_level (mq, sq);
988 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
992 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
994 GstClockTime time = GST_CLOCK_TIME_NONE;
996 if (GST_IS_BUFFER (object)) {
997 GstBuffer *buf = GST_BUFFER_CAST (object);
999 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1000 time = GST_BUFFER_TIMESTAMP (buf);
1001 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1002 time += GST_BUFFER_DURATION (buf);
1003 if (time > segment->stop)
1004 time = segment->stop;
1005 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1007 } else if (GST_IS_BUFFER_LIST (object)) {
1008 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1009 GstBufferListIterator *it = gst_buffer_list_iterate (list);
1013 while ((buf = gst_buffer_list_iterator_next (it))) {
1014 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1015 time = GST_BUFFER_TIMESTAMP (buf);
1016 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1017 time += GST_BUFFER_DURATION (buf);
1018 if (time > segment->stop)
1019 time = segment->stop;
1020 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1027 } while (gst_buffer_list_iterator_next_group (it));
1028 } else if (GST_IS_EVENT (object)) {
1029 GstEvent *event = GST_EVENT_CAST (object);
1031 /* For newsegment events return the running time of the start position */
1032 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
1033 GstSegment new_segment = *segment;
1035 gdouble rate, applied_rate;
1037 gint64 start, stop, position;
1039 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
1040 &format, &start, &stop, &position);
1041 if (format == GST_FORMAT_TIME) {
1042 gst_segment_set_newsegment_full (&new_segment, update, rate,
1043 applied_rate, format, start, stop, position);
1046 gst_segment_to_running_time (&new_segment, GST_FORMAT_TIME,
1056 static GstFlowReturn
1057 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1058 GstMiniObject * object)
1060 GstFlowReturn result = GST_FLOW_OK;
1062 if (GST_IS_BUFFER (object)) {
1064 GstClockTime timestamp, duration;
1067 buffer = GST_BUFFER_CAST (object);
1068 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1069 duration = GST_BUFFER_DURATION (buffer);
1070 caps = GST_BUFFER_CAPS (buffer);
1072 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1074 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1075 gst_data_queue_limits_changed (sq->queue);
1077 GST_DEBUG_OBJECT (mq,
1078 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1079 sq->id, buffer, GST_TIME_ARGS (timestamp));
1081 /* Set caps on pad before pushing, this avoids core calling the acceptcaps
1082 * function on the srcpad, which will call acceptcaps upstream, which might
1083 * not accept these caps (anymore). */
1084 if (caps && caps != GST_PAD_CAPS (sq->srcpad))
1085 gst_pad_set_caps (sq->srcpad, caps);
1087 result = gst_pad_push (sq->srcpad, buffer);
1088 } else if (GST_IS_EVENT (object)) {
1091 event = GST_EVENT_CAST (object);
1093 switch (GST_EVENT_TYPE (event)) {
1095 result = GST_FLOW_UNEXPECTED;
1097 case GST_EVENT_NEWSEGMENT:
1098 apply_segment (mq, sq, event, &sq->src_segment);
1099 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1100 gst_data_queue_limits_changed (sq->queue);
1106 GST_DEBUG_OBJECT (mq,
1107 "SingleQueue %d : Pushing event %p of type %s",
1108 sq->id, event, GST_EVENT_TYPE_NAME (event));
1110 gst_pad_push_event (sq->srcpad, event);
1112 g_warning ("Unexpected object in singlequeue %d (refcounting problem?)",
1120 static GstMiniObject *
1121 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1126 item->object = NULL;
1132 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1135 gst_mini_object_unref (item->object);
1136 g_slice_free (GstMultiQueueItem, item);
1139 /* takes ownership of passed mini object! */
1140 static GstMultiQueueItem *
1141 gst_multi_queue_buffer_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;
1150 item->size = GST_BUFFER_SIZE (object);
1151 item->duration = GST_BUFFER_DURATION (object);
1152 if (item->duration == GST_CLOCK_TIME_NONE)
1154 item->visible = TRUE;
1158 static GstMultiQueueItem *
1159 gst_multi_queue_event_item_new (GstMiniObject * object, guint32 curid)
1161 GstMultiQueueItem *item;
1163 item = g_slice_new (GstMultiQueueItem);
1164 item->object = object;
1165 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1166 item->posid = curid;
1170 item->visible = FALSE;
1174 /* Each main loop attempts to push buffers until the return value
1175 * is not-linked. not-linked pads are not allowed to push data beyond
1176 * any linked pads, so they don't 'rush ahead of the pack'.
1179 gst_multi_queue_loop (GstPad * pad)
1182 GstMultiQueueItem *item;
1183 GstDataQueueItem *sitem;
1185 GstMiniObject *object = NULL;
1187 GstFlowReturn result;
1188 GstClockTime next_time;
1190 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1193 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1198 /* Get something from the queue, blocking until that happens, or we get
1200 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1203 item = (GstMultiQueueItem *) sitem;
1204 newid = item->posid;
1206 /* steal the object and destroy the item */
1207 object = gst_multi_queue_item_steal_object (item);
1208 gst_multi_queue_item_destroy (item);
1210 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1211 next_time = get_running_time (&sq->src_segment, object, TRUE);
1213 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1214 sq->id, newid, sq->last_oldid);
1216 /* If we're not-linked, we do some extra work because we might need to
1217 * wait before pushing. If we're linked but there's a gap in the IDs,
1218 * or it's the first loop, or we just passed the previous highid,
1219 * we might need to wake some sleeping pad up, so there's extra work
1221 if (sq->srcresult == GST_FLOW_NOT_LINKED
1222 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1223 || sq->last_oldid > mq->highid) {
1224 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1225 gst_flow_get_name (sq->srcresult));
1227 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1229 /* Check again if we're flushing after the lock is taken,
1230 * the flush flag might have been changed in the meantime */
1232 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1236 /* Update the nextid so other threads know when to wake us up */
1238 sq->next_time = next_time;
1240 /* Update the oldid (the last ID we output) for highid tracking */
1241 if (sq->last_oldid != G_MAXUINT32)
1242 sq->oldid = sq->last_oldid;
1244 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1245 /* Go to sleep until it's time to push this buffer */
1247 /* Recompute the highid */
1248 compute_high_id (mq);
1249 /* Recompute the high time */
1250 compute_high_time (mq);
1252 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1253 (mq->high_time == GST_CLOCK_TIME_NONE
1254 || next_time >= mq->high_time))
1255 || (!mq->sync_by_running_time && newid > mq->highid))
1256 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1258 GST_DEBUG_OBJECT (mq,
1259 "queue %d sleeping for not-linked wakeup with "
1260 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1261 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1262 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1264 /* Wake up all non-linked pads before we sleep */
1265 wake_up_next_non_linked (mq);
1268 g_cond_wait (sq->turn, mq->qlock);
1272 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1276 /* Recompute the high time */
1277 compute_high_time (mq);
1279 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1280 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1281 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1282 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1285 /* Re-compute the high_id in case someone else pushed */
1286 compute_high_id (mq);
1288 compute_high_id (mq);
1289 /* Wake up all non-linked pads */
1290 wake_up_next_non_linked (mq);
1292 /* We're done waiting, we can clear the nextid and nexttime */
1294 sq->next_time = GST_CLOCK_TIME_NONE;
1296 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1302 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1303 gst_flow_get_name (sq->srcresult));
1305 /* Update time stats */
1306 next_time = get_running_time (&sq->src_segment, object, FALSE);
1307 if (next_time != GST_CLOCK_TIME_NONE) {
1308 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1309 sq->last_time = next_time;
1310 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1311 /* Wake up all non-linked pads now that we advanced the high time */
1312 mq->high_time = next_time;
1313 wake_up_next_non_linked (mq);
1317 /* Try to push out the new object */
1318 result = gst_single_queue_push_one (mq, sq, object);
1319 sq->srcresult = result;
1322 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1323 && result != GST_FLOW_UNEXPECTED)
1326 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1327 gst_flow_get_name (sq->srcresult));
1329 sq->last_oldid = newid;
1336 gst_mini_object_unref (object);
1338 /* Need to make sure wake up any sleeping pads when we exit */
1339 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1340 compute_high_id (mq);
1341 wake_up_next_non_linked (mq);
1342 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1344 /* upstream needs to see fatal result ASAP to shut things down,
1345 * but might be stuck in one of our other full queues;
1346 * so empty this one and trigger dynamic queue growth. At
1347 * this point the srcresult is not OK, NOT_LINKED
1348 * or UNEXPECTED, i.e. a real failure */
1349 gst_data_queue_flush (sq->queue);
1350 single_queue_underrun_cb (sq->queue, sq);
1351 gst_data_queue_set_flushing (sq->queue, TRUE);
1352 gst_pad_pause_task (sq->srcpad);
1353 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1354 "SingleQueue[%d] task paused, reason:%s",
1355 sq->id, gst_flow_get_name (sq->srcresult));
1361 * gst_multi_queue_chain:
1363 * This is similar to GstQueue's chain function, except:
1364 * _ we don't have leak behaviours,
1365 * _ we push with a unique id (curid)
1367 static GstFlowReturn
1368 gst_multi_queue_chain (GstPad * pad, GstBuffer * buffer)
1372 GstMultiQueueItem *item;
1374 GstClockTime timestamp, duration;
1376 sq = gst_pad_get_element_private (pad);
1379 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1383 /* Get a unique incrementing id */
1384 curid = G_ATOMIC_INT_ADD ((gint *) & mq->counter, 1);
1386 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1387 sq->id, buffer, curid);
1389 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1391 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1392 duration = GST_BUFFER_DURATION (buffer);
1394 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1397 /* update time level, we must do this after pushing the data in the queue so
1398 * that we never end up filling the queue first. */
1399 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1402 return sq->srcresult;
1407 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1408 sq->id, gst_flow_get_name (sq->srcresult));
1409 gst_multi_queue_item_destroy (item);
1414 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return UNEXPECTED");
1415 gst_buffer_unref (buffer);
1416 return GST_FLOW_UNEXPECTED;
1421 gst_multi_queue_sink_activate_push (GstPad * pad, gboolean active)
1425 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1428 /* All pads start off linked until they push one buffer */
1429 sq->srcresult = GST_FLOW_OK;
1431 sq->srcresult = GST_FLOW_WRONG_STATE;
1432 gst_data_queue_flush (sq->queue);
1438 gst_multi_queue_sink_event (GstPad * pad, GstEvent * event)
1443 GstMultiQueueItem *item;
1446 GstEvent *sref = NULL;
1448 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1449 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1451 type = GST_EVENT_TYPE (event);
1454 case GST_EVENT_FLUSH_START:
1455 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1458 res = gst_pad_push_event (sq->srcpad, event);
1460 gst_single_queue_flush (mq, sq, TRUE);
1463 case GST_EVENT_FLUSH_STOP:
1464 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1467 res = gst_pad_push_event (sq->srcpad, event);
1469 gst_single_queue_flush (mq, sq, FALSE);
1471 case GST_EVENT_NEWSEGMENT:
1472 /* take ref because the queue will take ownership and we need the event
1473 * afterwards to update the segment */
1474 sref = gst_event_ref (event);
1478 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1479 res = gst_pad_push_event (sq->srcpad, event);
1485 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1489 /* Get an unique incrementing id. */
1490 curid = G_ATOMIC_INT_ADD ((gint *) & mq->counter, 1);
1492 item = gst_multi_queue_event_item_new ((GstMiniObject *) event, curid);
1494 GST_DEBUG_OBJECT (mq,
1495 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1496 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1498 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1501 /* mark EOS when we received one, we must do that after putting the
1502 * buffer in the queue because EOS marks the buffer as filled. No need to take
1503 * a lock, the _check_full happens from this thread only, right before pushing
1504 * into dataqueue. */
1508 /* EOS affects the buffering state */
1509 update_buffering (mq, sq);
1510 single_queue_overrun_cb (sq->queue, sq);
1512 case GST_EVENT_NEWSEGMENT:
1513 apply_segment (mq, sq, sref, &sq->sink_segment);
1514 gst_event_unref (sref);
1520 gst_object_unref (mq);
1525 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1526 sq->id, gst_flow_get_name (sq->srcresult));
1528 gst_event_unref (sref);
1529 gst_multi_queue_item_destroy (item);
1534 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1535 gst_event_unref (event);
1542 gst_multi_queue_getcaps (GstPad * pad)
1544 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1548 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1550 GST_LOG_OBJECT (otherpad, "Getting caps from the peer of this pad");
1552 result = gst_pad_peer_get_caps (otherpad);
1554 result = gst_caps_new_any ();
1560 gst_multi_queue_acceptcaps (GstPad * pad, GstCaps * caps)
1562 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1566 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1568 GST_LOG_OBJECT (otherpad, "Accept caps from the peer of this pad");
1570 result = gst_pad_peer_accept_caps (otherpad, caps);
1575 static GstFlowReturn
1576 gst_multi_queue_bufferalloc (GstPad * pad, guint64 offset, guint size,
1577 GstCaps * caps, GstBuffer ** buf)
1579 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1581 return gst_pad_alloc_buffer (sq->srcpad, offset, size, caps, buf);
1585 gst_multi_queue_src_activate_push (GstPad * pad, gboolean active)
1589 gboolean result = FALSE;
1591 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1594 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1597 result = gst_single_queue_flush (mq, sq, FALSE);
1599 result = gst_single_queue_flush (mq, sq, TRUE);
1600 /* make sure streaming finishes */
1601 result |= gst_pad_stop_task (pad);
1607 gst_multi_queue_src_event (GstPad * pad, GstEvent * event)
1609 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1611 return gst_pad_push_event (sq->sinkpad, event);
1615 gst_multi_queue_src_query (GstPad * pad, GstQuery * query)
1617 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1621 /* FIXME, Handle position offset depending on queue size */
1623 /* default handling */
1624 if (!(peerpad = gst_pad_get_peer (sq->sinkpad)))
1627 res = gst_pad_query (peerpad, query);
1629 gst_object_unref (peerpad);
1636 GST_LOG_OBJECT (sq->sinkpad, "Couldn't send query because we have no peer");
1642 * Next-non-linked functions
1645 /* WITH LOCK TAKEN */
1647 wake_up_next_non_linked (GstMultiQueue * mq)
1651 /* maybe no-one is waiting */
1652 if (mq->numwaiting < 1)
1655 /* Else figure out which singlequeue(s) need waking up */
1656 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1657 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1659 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1660 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
1661 && sq->next_time != GST_CLOCK_TIME_NONE
1662 && sq->next_time >= mq->high_time)
1663 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
1664 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1665 g_cond_signal (sq->turn);
1671 /* WITH LOCK TAKEN */
1673 compute_high_id (GstMultiQueue * mq)
1675 /* The high-id is either the highest id among the linked pads, or if all
1676 * pads are not-linked, it's the lowest not-linked pad */
1678 guint32 lowest = G_MAXUINT32;
1679 guint32 highid = G_MAXUINT32;
1681 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1682 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1684 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1685 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1687 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1688 /* No need to consider queues which are not waiting */
1689 if (sq->nextid == 0) {
1690 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1694 if (sq->nextid < lowest)
1695 lowest = sq->nextid;
1696 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1697 /* If we don't have a global highid, or the global highid is lower than
1698 * this single queue's last outputted id, store the queue's one,
1699 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1700 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1705 if (highid == G_MAXUINT32 || lowest < highid)
1706 mq->highid = lowest;
1708 mq->highid = highid;
1710 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1714 /* WITH LOCK TAKEN */
1716 compute_high_time (GstMultiQueue * mq)
1718 /* The high-id is either the highest id among the linked pads, or if all
1719 * pads are not-linked, it's the lowest not-linked pad */
1721 GstClockTime highest = GST_CLOCK_TIME_NONE;
1722 GstClockTime lowest = GST_CLOCK_TIME_NONE;
1724 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1725 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1728 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
1729 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
1730 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
1732 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1733 /* No need to consider queues which are not waiting */
1734 if (sq->next_time == GST_CLOCK_TIME_NONE) {
1735 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1739 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
1740 lowest = sq->next_time;
1741 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1742 /* If we don't have a global highid, or the global highid is lower than
1743 * this single queue's last outputted id, store the queue's one,
1744 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1745 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
1746 highest = sq->last_time;
1750 mq->high_time = highest;
1753 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
1754 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
1757 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
1758 ((q)->max_size.format) <= (value))
1761 * GstSingleQueue functions
1764 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1766 GstMultiQueue *mq = sq->mqueue;
1768 GstDataQueueSize size;
1769 gboolean filled = FALSE;
1771 gst_data_queue_get_level (sq->queue, &size);
1773 GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
1775 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1776 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1777 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1778 GstDataQueueSize ssize;
1780 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
1782 if (gst_data_queue_is_empty (oq->queue)) {
1783 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
1784 if (IS_FILLED (sq, visible, size.visible)) {
1785 sq->max_size.visible = size.visible + 1;
1786 GST_DEBUG_OBJECT (mq,
1787 "Another queue is empty, bumping single queue %d max visible to %d",
1788 sq->id, sq->max_size.visible);
1791 /* check if we reached the hard time/bytes limits */
1792 gst_data_queue_get_level (oq->queue, &ssize);
1794 GST_DEBUG_OBJECT (mq,
1795 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1796 G_GUINT64_FORMAT, oq->id, ssize.visible, oq->max_size.visible,
1797 ssize.bytes, oq->max_size.bytes, oq->cur_time, oq->max_size.time);
1799 /* if this queue is filled completely we must signal overrun.
1800 * FIXME, this seems wrong in many ways
1801 * - we're comparing the filled level of this queue against the
1802 * values of the other one
1803 * - we should only do this after we found no empty queues, ie, move
1804 * this check outside of the loop
1805 * - the debug statement talks about a different queue than the one
1806 * we are checking here.
1808 if (sq->is_eos || IS_FILLED (sq, bytes, ssize.bytes) ||
1809 IS_FILLED (sq, time, sq->cur_time)) {
1810 GST_LOG_OBJECT (mq, "Queue %d is filled", oq->id);
1814 /* no queues were empty */
1815 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1817 /* Overrun is always forwarded, since this is blocking the upstream element */
1819 GST_DEBUG_OBJECT (mq, "A queue is filled, signalling overrun");
1820 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1827 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1829 gboolean empty = TRUE;
1830 GstMultiQueue *mq = sq->mqueue;
1834 "Single Queue %d is empty, Checking other single queues", sq->id);
1836 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1837 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1838 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1840 if (gst_data_queue_is_full (oq->queue)) {
1841 GstDataQueueSize size;
1843 gst_data_queue_get_level (oq->queue, &size);
1844 if (IS_FILLED (oq, visible, size.visible)) {
1845 oq->max_size.visible = size.visible + 1;
1846 GST_DEBUG_OBJECT (mq,
1847 "queue %d is filled, bumping its max visible to %d", oq->id,
1848 oq->max_size.visible);
1849 gst_data_queue_limits_changed (oq->queue);
1852 if (!gst_data_queue_is_empty (oq->queue))
1855 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1858 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1859 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1864 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1865 guint64 time, GstSingleQueue * sq)
1868 GstMultiQueue *mq = sq->mqueue;
1870 GST_DEBUG_OBJECT (mq,
1871 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1872 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1873 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1875 /* we are always filled on EOS */
1879 /* we never go past the max visible items unless we are in buffering mode */
1880 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
1883 /* check time or bytes */
1884 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
1890 gst_single_queue_free (GstSingleQueue * sq)
1893 gst_data_queue_flush (sq->queue);
1894 g_object_unref (sq->queue);
1895 g_cond_free (sq->turn);
1899 static GstSingleQueue *
1900 gst_single_queue_new (GstMultiQueue * mqueue, gint id)
1905 gint temp_id = (id == -1) ? 0 : id;
1907 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1909 /* Find an unused queue ID, if possible the passed one */
1910 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1911 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1912 /* This works because the IDs are sorted in ascending order */
1913 if (sq2->id == temp_id) {
1914 /* If this ID was requested by the caller return NULL,
1915 * otherwise just get us the next one */
1917 temp_id = sq2->id + 1;
1920 } else if (sq2->id > temp_id) {
1925 sq = g_new0 (GstSingleQueue, 1);
1929 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
1930 mqueue->queues_cookie++;
1932 /* copy over max_size and extra_size so we don't need to take the lock
1933 * any longer when checking if the queue is full. */
1934 sq->max_size.visible = mqueue->max_size.visible;
1935 sq->max_size.bytes = mqueue->max_size.bytes;
1936 sq->max_size.time = mqueue->max_size.time;
1938 sq->extra_size.visible = mqueue->extra_size.visible;
1939 sq->extra_size.bytes = mqueue->extra_size.bytes;
1940 sq->extra_size.time = mqueue->extra_size.time;
1942 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
1944 sq->mqueue = mqueue;
1945 sq->srcresult = GST_FLOW_WRONG_STATE;
1946 sq->queue = gst_data_queue_new_full ((GstDataQueueCheckFullFunction)
1947 single_queue_check_full,
1948 (GstDataQueueFullCallback) single_queue_overrun_cb,
1949 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
1951 sq->flushing = FALSE;
1952 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1953 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1957 sq->next_time = GST_CLOCK_TIME_NONE;
1958 sq->last_time = GST_CLOCK_TIME_NONE;
1959 sq->turn = g_cond_new ();
1961 sq->sinktime = GST_CLOCK_TIME_NONE;
1962 sq->srctime = GST_CLOCK_TIME_NONE;
1963 sq->sink_tainted = TRUE;
1964 sq->src_tainted = TRUE;
1966 name = g_strdup_printf ("sink%d", sq->id);
1967 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
1970 gst_pad_set_chain_function (sq->sinkpad,
1971 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
1972 gst_pad_set_activatepush_function (sq->sinkpad,
1973 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_push));
1974 gst_pad_set_event_function (sq->sinkpad,
1975 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
1976 gst_pad_set_getcaps_function (sq->sinkpad,
1977 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1978 gst_pad_set_acceptcaps_function (sq->sinkpad,
1979 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1980 gst_pad_set_bufferalloc_function (sq->sinkpad,
1981 GST_DEBUG_FUNCPTR (gst_multi_queue_bufferalloc));
1982 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
1983 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1985 name = g_strdup_printf ("src%d", sq->id);
1986 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
1989 gst_pad_set_activatepush_function (sq->srcpad,
1990 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_push));
1991 gst_pad_set_getcaps_function (sq->srcpad,
1992 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1993 gst_pad_set_acceptcaps_function (sq->srcpad,
1994 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1995 gst_pad_set_event_function (sq->srcpad,
1996 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
1997 gst_pad_set_query_function (sq->srcpad,
1998 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
1999 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2000 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2002 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2003 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2005 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2007 /* only activate the pads when we are not in the NULL state
2008 * and add the pad under the state_lock to prevend state changes
2009 * between activating and adding */
2010 g_static_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2011 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2012 gst_pad_set_active (sq->srcpad, TRUE);
2013 gst_pad_set_active (sq->sinkpad, TRUE);
2015 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2016 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2017 g_static_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2019 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",