2 * Copyright (C) 2006 Edward Hervey <edward@fluendo.com>
3 * Copyright (C) 2007 Jan Schmidt <jan@fluendo.com>
4 * Copyright (C) 2007 Wim Taymans <wim@fluendo.com>
5 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * SECTION:element-multiqueue
27 * @see_also: #GstQueue
31 * Multiqueue is similar to a normal #GstQueue with the following additional
35 * <itemizedlist><title>Multiple streamhandling</title>
37 * The element handles queueing data on more than one stream at once. To
38 * achieve such a feature it has request sink pads (sink%u) and
39 * 'sometimes' src pads (src%u).
41 * When requesting a given sinkpad with gst_element_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.
112 #include "gstmultiqueue.h"
113 #include <gst/glib-compat-private.h>
117 * @sinkpad: associated sink #GstPad
118 * @srcpad: associated source #GstPad
120 * Structure containing all information and properties about
123 typedef struct _GstSingleQueue GstSingleQueue;
125 struct _GstSingleQueue
127 /* unique identifier of the queue */
130 GstMultiQueue *mqueue;
135 /* flowreturn of previous srcpad push */
136 GstFlowReturn srcresult;
137 /* If something was actually pushed on
138 * this pad after flushing/pad activation
139 * and the srcresult corresponds to something
145 GstSegment sink_segment;
146 GstSegment src_segment;
147 gboolean has_src_segment; /* preferred over initializing the src_segment to
148 * UNDEFINED as this doesn't requires adding ifs
149 * in every segment usage */
151 /* position of src/sink */
152 GstClockTimeDiff sinktime, srctime;
153 /* cached input value, used for interleave */
154 GstClockTimeDiff cached_sinktime;
155 /* TRUE if either position needs to be recalculated */
156 gboolean sink_tainted, src_tainted;
160 GstDataQueueSize max_size, extra_size;
161 GstClockTime cur_time;
167 /* Protected by global lock */
168 guint32 nextid; /* ID of the next object waiting to be pushed */
169 guint32 oldid; /* ID of the last object pushed (last in a series) */
170 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
171 GstClockTimeDiff next_time; /* End running time of next buffer to be pushed */
172 GstClockTimeDiff last_time; /* Start running time of last pushed buffer */
173 GCond turn; /* SingleQueue turn waiting conditional */
175 /* for serialized queries */
178 GstQuery *last_handled_query;
180 /* For interleave calculation */
185 /* Extension of GstDataQueueItem structure for our usage */
186 typedef struct _GstMultiQueueItem GstMultiQueueItem;
188 struct _GstMultiQueueItem
190 GstMiniObject *object;
195 GDestroyNotify destroy;
201 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
202 static void gst_single_queue_free (GstSingleQueue * squeue);
204 static void wake_up_next_non_linked (GstMultiQueue * mq);
205 static void compute_high_id (GstMultiQueue * mq);
206 static void compute_high_time (GstMultiQueue * mq);
207 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
208 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
210 static void update_buffering (GstMultiQueue * mq, GstSingleQueue * sq);
211 static void gst_multi_queue_post_buffering (GstMultiQueue * mq);
213 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
215 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
218 GST_STATIC_CAPS_ANY);
220 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
223 GST_STATIC_CAPS_ANY);
225 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
226 #define GST_CAT_DEFAULT (multi_queue_debug)
228 /* Signals and args */
236 /* default limits, we try to keep up to 2 seconds of data and if there is not
237 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
238 * there is data in the queues. Normally, the byte and time limits are not hit
239 * in theses conditions. */
240 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
241 #define DEFAULT_MAX_SIZE_BUFFERS 5
242 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
244 /* second limits. When we hit one of the above limits we are probably dealing
245 * with a badly muxed file and we scale the limits to these emergency values.
246 * This is currently not yet implemented.
247 * Since we dynamically scale the queue buffer size up to the limits but avoid
248 * going above the max-size-buffers when we can, we don't really need this
249 * aditional extra size. */
250 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
251 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
252 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
254 #define DEFAULT_USE_BUFFERING FALSE
255 #define DEFAULT_LOW_PERCENT 10
256 #define DEFAULT_HIGH_PERCENT 99
257 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
258 #define DEFAULT_USE_INTERLEAVE FALSE
259 #define DEFAULT_UNLINKED_CACHE_TIME 250 * GST_MSECOND
264 PROP_EXTRA_SIZE_BYTES,
265 PROP_EXTRA_SIZE_BUFFERS,
266 PROP_EXTRA_SIZE_TIME,
268 PROP_MAX_SIZE_BUFFERS,
273 PROP_SYNC_BY_RUNNING_TIME,
275 PROP_UNLINKED_CACHE_TIME,
279 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
280 g_mutex_lock (&q->qlock); \
283 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
284 g_mutex_unlock (&q->qlock); \
287 #define SET_PERCENT(mq, perc) G_STMT_START { \
288 if (perc != mq->percent) { \
289 mq->percent = perc; \
290 mq->percent_changed = TRUE; \
291 GST_DEBUG_OBJECT (mq, "buffering %d percent", perc); \
295 /* Convenience function */
296 static inline GstClockTimeDiff
297 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
299 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
301 if (GST_CLOCK_TIME_IS_VALID (val)) {
303 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
312 static void gst_multi_queue_finalize (GObject * object);
313 static void gst_multi_queue_set_property (GObject * object,
314 guint prop_id, const GValue * value, GParamSpec * pspec);
315 static void gst_multi_queue_get_property (GObject * object,
316 guint prop_id, GValue * value, GParamSpec * pspec);
318 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
319 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
320 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
321 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
322 element, GstStateChange transition);
324 static void gst_multi_queue_loop (GstPad * pad);
327 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
328 #define gst_multi_queue_parent_class parent_class
329 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
332 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
335 gst_multi_queue_class_init (GstMultiQueueClass * klass)
337 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
338 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
340 gobject_class->set_property = gst_multi_queue_set_property;
341 gobject_class->get_property = gst_multi_queue_get_property;
346 * GstMultiQueue::underrun:
347 * @multiqueue: the multiqueue instance
349 * This signal is emitted from the streaming thread when there is
350 * no data in any of the queues inside the multiqueue instance (underrun).
352 * This indicates either starvation or EOS from the upstream data sources.
354 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
355 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
356 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
357 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
360 * GstMultiQueue::overrun:
361 * @multiqueue: the multiqueue instance
363 * Reports that one of the queues in the multiqueue is full (overrun).
364 * A queue is full if the total amount of data inside it (num-buffers, time,
365 * size) is higher than the boundary values which can be set through the
366 * GObject properties.
368 * This can be used as an indicator of pre-roll.
370 gst_multi_queue_signals[SIGNAL_OVERRUN] =
371 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
372 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
373 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
377 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
378 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
379 "Max. amount of data in the queue (bytes, 0=disable)",
380 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
381 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
382 G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
384 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
385 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
386 DEFAULT_MAX_SIZE_BUFFERS,
387 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
388 G_PARAM_STATIC_STRINGS));
389 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
390 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
391 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
392 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
393 G_PARAM_STATIC_STRINGS));
395 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
396 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
397 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
398 " (NOT IMPLEMENTED)",
399 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
400 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
401 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
402 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
403 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
404 " (NOT IMPLEMENTED)",
405 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
406 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
407 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
408 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
409 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
410 " (NOT IMPLEMENTED)",
411 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
412 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 * GstMultiQueue:use-buffering
417 * Enable the buffering option in multiqueue so that BUFFERING messages are
418 * emitted based on low-/high-percent thresholds.
420 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
421 g_param_spec_boolean ("use-buffering", "Use buffering",
422 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
423 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
424 G_PARAM_STATIC_STRINGS));
426 * GstMultiQueue:low-percent
428 * Low threshold percent for buffering to start.
430 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
431 g_param_spec_int ("low-percent", "Low percent",
432 "Low threshold for buffering to start", 0, 100,
433 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 * GstMultiQueue:high-percent
437 * High threshold percent for buffering to finish.
439 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
440 g_param_spec_int ("high-percent", "High percent",
441 "High threshold for buffering to finish", 0, 100,
442 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 * GstMultiQueue:sync-by-running-time
447 * If enabled multiqueue will synchronize deactivated or not-linked streams
448 * to the activated and linked streams by taking the running time.
449 * Otherwise multiqueue will synchronize the deactivated or not-linked
450 * streams by keeping the order in which buffers and events arrived compared
451 * to active and linked streams.
453 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
454 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
455 "Synchronize deactivated or not-linked streams by running time",
456 DEFAULT_SYNC_BY_RUNNING_TIME,
457 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
459 g_object_class_install_property (gobject_class, PROP_USE_INTERLEAVE,
460 g_param_spec_boolean ("use-interleave", "Use interleave",
461 "Adjust time limits based on input interleave",
462 DEFAULT_USE_INTERLEAVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
464 g_object_class_install_property (gobject_class, PROP_UNLINKED_CACHE_TIME,
465 g_param_spec_uint64 ("unlinked-cache-time", "Unlinked cache time (ns)",
466 "Extra buffering in time for unlinked streams (if 'sync-by-running-time')",
467 0, G_MAXUINT64, DEFAULT_UNLINKED_CACHE_TIME,
468 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
469 G_PARAM_STATIC_STRINGS));
472 gobject_class->finalize = gst_multi_queue_finalize;
474 gst_element_class_set_static_metadata (gstelement_class,
476 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
477 gst_element_class_add_pad_template (gstelement_class,
478 gst_static_pad_template_get (&sinktemplate));
479 gst_element_class_add_pad_template (gstelement_class,
480 gst_static_pad_template_get (&srctemplate));
482 gstelement_class->request_new_pad =
483 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
484 gstelement_class->release_pad =
485 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
486 gstelement_class->change_state =
487 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
491 gst_multi_queue_init (GstMultiQueue * mqueue)
493 mqueue->nbqueues = 0;
494 mqueue->queues = NULL;
496 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
497 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
498 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
500 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
501 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
502 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
504 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
505 mqueue->low_percent = DEFAULT_LOW_PERCENT;
506 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
508 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
509 mqueue->use_interleave = DEFAULT_USE_INTERLEAVE;
510 mqueue->unlinked_cache_time = DEFAULT_UNLINKED_CACHE_TIME;
514 mqueue->high_time = GST_CLOCK_STIME_NONE;
516 g_mutex_init (&mqueue->qlock);
517 g_mutex_init (&mqueue->buffering_post_lock);
521 gst_multi_queue_finalize (GObject * object)
523 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
525 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
526 g_list_free (mqueue->queues);
527 mqueue->queues = NULL;
528 mqueue->queues_cookie++;
530 /* free/unref instance data */
531 g_mutex_clear (&mqueue->qlock);
532 g_mutex_clear (&mqueue->buffering_post_lock);
534 G_OBJECT_CLASS (parent_class)->finalize (object);
537 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
538 GList * tmp = mq->queues; \
540 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
541 q->max_size.format = mq->max_size.format; \
542 update_buffering (mq, q); \
543 gst_data_queue_limits_changed (q->queue); \
544 tmp = g_list_next(tmp); \
549 gst_multi_queue_set_property (GObject * object, guint prop_id,
550 const GValue * value, GParamSpec * pspec)
552 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
555 case PROP_MAX_SIZE_BYTES:
556 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
557 mq->max_size.bytes = g_value_get_uint (value);
558 SET_CHILD_PROPERTY (mq, bytes);
559 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
560 gst_multi_queue_post_buffering (mq);
562 case PROP_MAX_SIZE_BUFFERS:
565 gint new_size = g_value_get_uint (value);
567 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
569 mq->max_size.visible = new_size;
573 GstDataQueueSize size;
574 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
575 gst_data_queue_get_level (q->queue, &size);
577 GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
578 " current: %d, current max %d", q->id, new_size, size.visible,
579 q->max_size.visible);
581 /* do not reduce max size below current level if the single queue
582 * has grown because of empty queue */
584 q->max_size.visible = new_size;
585 } else if (q->max_size.visible == 0) {
586 q->max_size.visible = MAX (new_size, size.visible);
587 } else if (new_size > size.visible) {
588 q->max_size.visible = new_size;
590 update_buffering (mq, q);
591 gst_data_queue_limits_changed (q->queue);
592 tmp = g_list_next (tmp);
595 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
596 gst_multi_queue_post_buffering (mq);
600 case PROP_MAX_SIZE_TIME:
601 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
602 mq->max_size.time = g_value_get_uint64 (value);
603 SET_CHILD_PROPERTY (mq, time);
604 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
605 gst_multi_queue_post_buffering (mq);
607 case PROP_EXTRA_SIZE_BYTES:
608 mq->extra_size.bytes = g_value_get_uint (value);
610 case PROP_EXTRA_SIZE_BUFFERS:
611 mq->extra_size.visible = g_value_get_uint (value);
613 case PROP_EXTRA_SIZE_TIME:
614 mq->extra_size.time = g_value_get_uint64 (value);
616 case PROP_USE_BUFFERING:
617 mq->use_buffering = g_value_get_boolean (value);
618 if (!mq->use_buffering && mq->buffering) {
619 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
620 mq->buffering = FALSE;
621 GST_DEBUG_OBJECT (mq, "buffering 100 percent");
622 SET_PERCENT (mq, 100);
623 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
626 if (mq->use_buffering) {
629 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
633 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
634 update_buffering (mq, q);
635 gst_data_queue_limits_changed (q->queue);
636 tmp = g_list_next (tmp);
639 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
641 gst_multi_queue_post_buffering (mq);
643 case PROP_LOW_PERCENT:
644 mq->low_percent = g_value_get_int (value);
646 case PROP_HIGH_PERCENT:
647 mq->high_percent = g_value_get_int (value);
649 case PROP_SYNC_BY_RUNNING_TIME:
650 mq->sync_by_running_time = g_value_get_boolean (value);
652 case PROP_USE_INTERLEAVE:
653 mq->use_interleave = g_value_get_boolean (value);
655 case PROP_UNLINKED_CACHE_TIME:
656 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
657 mq->unlinked_cache_time = g_value_get_uint64 (value);
658 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
659 gst_multi_queue_post_buffering (mq);
662 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
668 gst_multi_queue_get_property (GObject * object, guint prop_id,
669 GValue * value, GParamSpec * pspec)
671 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
673 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
676 case PROP_EXTRA_SIZE_BYTES:
677 g_value_set_uint (value, mq->extra_size.bytes);
679 case PROP_EXTRA_SIZE_BUFFERS:
680 g_value_set_uint (value, mq->extra_size.visible);
682 case PROP_EXTRA_SIZE_TIME:
683 g_value_set_uint64 (value, mq->extra_size.time);
685 case PROP_MAX_SIZE_BYTES:
686 g_value_set_uint (value, mq->max_size.bytes);
688 case PROP_MAX_SIZE_BUFFERS:
689 g_value_set_uint (value, mq->max_size.visible);
691 case PROP_MAX_SIZE_TIME:
692 g_value_set_uint64 (value, mq->max_size.time);
694 case PROP_USE_BUFFERING:
695 g_value_set_boolean (value, mq->use_buffering);
697 case PROP_LOW_PERCENT:
698 g_value_set_int (value, mq->low_percent);
700 case PROP_HIGH_PERCENT:
701 g_value_set_int (value, mq->high_percent);
703 case PROP_SYNC_BY_RUNNING_TIME:
704 g_value_set_boolean (value, mq->sync_by_running_time);
706 case PROP_USE_INTERLEAVE:
707 g_value_set_boolean (value, mq->use_interleave);
709 case PROP_UNLINKED_CACHE_TIME:
710 g_value_set_uint64 (value, mq->unlinked_cache_time);
713 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
717 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
721 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
723 GstIterator *it = NULL;
725 GstSingleQueue *squeue;
726 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
729 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
730 squeue = gst_pad_get_element_private (pad);
734 if (squeue->sinkpad == pad)
735 opad = gst_object_ref (squeue->srcpad);
736 else if (squeue->srcpad == pad)
737 opad = gst_object_ref (squeue->sinkpad);
741 g_value_init (&val, GST_TYPE_PAD);
742 g_value_set_object (&val, opad);
743 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
744 g_value_unset (&val);
746 gst_object_unref (opad);
749 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
760 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
761 const gchar * name, const GstCaps * caps)
763 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
764 GstSingleQueue *squeue;
768 sscanf (name + 4, "_%u", &temp_id);
769 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
772 /* Create a new single queue, add the sink and source pad and return the sink pad */
773 squeue = gst_single_queue_new (mqueue, temp_id);
775 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
776 GST_DEBUG_PAD_NAME (squeue->sinkpad));
778 return squeue ? squeue->sinkpad : NULL;
782 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
784 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
785 GstSingleQueue *sq = NULL;
788 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
790 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
791 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
792 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
793 sq = (GstSingleQueue *) tmp->data;
795 if (sq->sinkpad == pad)
800 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
801 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
805 /* FIXME: The removal of the singlequeue should probably not happen until it
806 * finishes draining */
808 /* remove it from the list */
809 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
810 mqueue->queues_cookie++;
812 /* FIXME : recompute next-non-linked */
813 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
815 /* delete SingleQueue */
816 gst_data_queue_set_flushing (sq->queue, TRUE);
818 gst_pad_set_active (sq->srcpad, FALSE);
819 gst_pad_set_active (sq->sinkpad, FALSE);
820 gst_pad_set_element_private (sq->srcpad, NULL);
821 gst_pad_set_element_private (sq->sinkpad, NULL);
822 gst_element_remove_pad (element, sq->srcpad);
823 gst_element_remove_pad (element, sq->sinkpad);
824 gst_single_queue_free (sq);
827 static GstStateChangeReturn
828 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
830 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
831 GstSingleQueue *sq = NULL;
832 GstStateChangeReturn result;
834 switch (transition) {
835 case GST_STATE_CHANGE_READY_TO_PAUSED:{
838 /* Set all pads to non-flushing */
839 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
840 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
841 sq = (GstSingleQueue *) tmp->data;
842 sq->flushing = FALSE;
845 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
846 SET_CHILD_PROPERTY (mqueue, visible);
848 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
849 gst_multi_queue_post_buffering (mqueue);
853 case GST_STATE_CHANGE_PAUSED_TO_READY:{
856 /* Un-wait all waiting pads */
857 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
858 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
859 sq = (GstSingleQueue *) tmp->data;
861 g_cond_signal (&sq->turn);
863 sq->last_query = FALSE;
864 g_cond_signal (&sq->query_handled);
866 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
873 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
875 switch (transition) {
884 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
889 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
893 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
894 sq->srcresult = GST_FLOW_FLUSHING;
895 gst_data_queue_set_flushing (sq->queue, TRUE);
899 /* wake up non-linked task */
900 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
902 g_cond_signal (&sq->turn);
903 sq->last_query = FALSE;
904 g_cond_signal (&sq->query_handled);
905 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
907 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
908 result = gst_pad_pause_task (sq->srcpad);
909 sq->sink_tainted = sq->src_tainted = TRUE;
911 gst_single_queue_flush_queue (sq, full);
913 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
914 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
915 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
916 sq->has_src_segment = FALSE;
917 /* All pads start off not-linked for a smooth kick-off */
918 sq->srcresult = GST_FLOW_OK;
921 sq->max_size.visible = mq->max_size.visible;
925 sq->last_oldid = G_MAXUINT32;
926 sq->next_time = GST_CLOCK_STIME_NONE;
927 sq->last_time = GST_CLOCK_STIME_NONE;
928 sq->cached_sinktime = GST_CLOCK_STIME_NONE;
929 gst_data_queue_set_flushing (sq->queue, FALSE);
931 /* Reset high time to be recomputed next */
932 mq->high_time = GST_CLOCK_STIME_NONE;
934 sq->flushing = FALSE;
935 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
937 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
939 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
945 /* WITH LOCK TAKEN */
947 get_percentage (GstSingleQueue * sq)
949 GstDataQueueSize size;
952 gst_data_queue_get_level (sq->queue, &size);
954 GST_DEBUG_OBJECT (sq->mqueue,
955 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
956 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
957 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
959 /* get bytes and time percentages and take the max */
960 if (sq->is_eos || sq->srcresult == GST_FLOW_NOT_LINKED || sq->is_sparse) {
964 if (sq->max_size.time > 0) {
965 tmp = (sq->cur_time * 100) / sq->max_size.time;
966 percent = MAX (percent, tmp);
968 if (sq->max_size.bytes > 0) {
969 tmp = (size.bytes * 100) / sq->max_size.bytes;
970 percent = MAX (percent, tmp);
977 /* WITH LOCK TAKEN */
979 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
983 /* nothing to dowhen we are not in buffering mode */
984 if (!mq->use_buffering)
987 percent = get_percentage (sq);
990 if (percent >= mq->high_percent) {
991 mq->buffering = FALSE;
993 /* make sure it increases */
994 percent = MAX (mq->percent, percent);
996 SET_PERCENT (mq, percent);
999 gboolean is_buffering = TRUE;
1001 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
1002 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
1004 if (get_percentage (oq) >= mq->high_percent) {
1005 is_buffering = FALSE;
1011 if (is_buffering && percent < mq->low_percent) {
1012 mq->buffering = TRUE;
1013 SET_PERCENT (mq, percent);
1019 gst_multi_queue_post_buffering (GstMultiQueue * mq)
1021 GstMessage *msg = NULL;
1023 g_mutex_lock (&mq->buffering_post_lock);
1024 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1025 if (mq->percent_changed) {
1026 gint percent = mq->percent;
1028 mq->percent_changed = FALSE;
1030 percent = percent * 100 / mq->high_percent;
1035 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
1036 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
1038 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1041 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
1043 g_mutex_unlock (&mq->buffering_post_lock);
1047 calculate_interleave (GstMultiQueue * mq)
1049 GstClockTimeDiff low, high;
1050 GstClockTime interleave;
1053 low = high = GST_CLOCK_STIME_NONE;
1054 interleave = mq->interleave;
1055 /* Go over all single queues and calculate lowest/highest value */
1056 for (tmp = mq->queues; tmp; tmp = tmp->next) {
1057 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1058 /* Ignore sparse streams for interleave calculation */
1061 /* If a stream is not active yet (hasn't received any buffers), set
1062 * a maximum interleave to allow it to receive more data */
1065 "queue %d is not active yet, forcing interleave to 5s", sq->id);
1066 mq->interleave = 5 * GST_SECOND;
1067 /* Update max-size time */
1068 mq->max_size.time = mq->interleave;
1069 SET_CHILD_PROPERTY (mq, time);
1072 if (GST_CLOCK_STIME_IS_VALID (sq->cached_sinktime)) {
1073 if (low == GST_CLOCK_STIME_NONE || sq->cached_sinktime < low)
1074 low = sq->cached_sinktime;
1075 if (high == GST_CLOCK_STIME_NONE || sq->cached_sinktime > high)
1076 high = sq->cached_sinktime;
1079 "queue %d , sinktime:%" GST_STIME_FORMAT " low:%" GST_STIME_FORMAT
1080 " high:%" GST_STIME_FORMAT, sq->id,
1081 GST_STIME_ARGS (sq->cached_sinktime), GST_STIME_ARGS (low),
1082 GST_STIME_ARGS (high));
1085 if (GST_CLOCK_STIME_IS_VALID (low) && GST_CLOCK_STIME_IS_VALID (high)) {
1086 interleave = high - low;
1087 /* Padding of interleave and minimum value */
1088 /* FIXME : Make the minimum time interleave a property */
1089 interleave = (150 * interleave / 100) + 250 * GST_MSECOND;
1091 /* Update the stored interleave if:
1092 * * No data has arrived yet (high == low)
1093 * * Or it went higher
1094 * * Or it went lower and we've gone past the previous interleave needed */
1095 if (high == low || interleave > mq->interleave ||
1096 ((mq->last_interleave_update + (2 * MIN (GST_SECOND,
1097 mq->interleave)) < low)
1098 && interleave < (mq->interleave * 3 / 4))) {
1099 /* Update the interleave */
1100 mq->interleave = interleave;
1101 mq->last_interleave_update = high;
1102 /* Update max-size time */
1103 mq->max_size.time = mq->interleave;
1104 SET_CHILD_PROPERTY (mq, time);
1109 GST_DEBUG_OBJECT (mq,
1110 "low:%" GST_STIME_FORMAT " high:%" GST_STIME_FORMAT " interleave:%"
1111 GST_TIME_FORMAT " mq->interleave:%" GST_TIME_FORMAT
1112 " last_interleave_update:%" GST_STIME_FORMAT, GST_STIME_ARGS (low),
1113 GST_STIME_ARGS (high), GST_TIME_ARGS (interleave),
1114 GST_TIME_ARGS (mq->interleave),
1115 GST_STIME_ARGS (mq->last_interleave_update));
1119 /* calculate the diff between running time on the sink and src of the queue.
1120 * This is the total amount of time in the queue.
1121 * WITH LOCK TAKEN */
1123 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
1125 GstClockTimeDiff sink_time, src_time;
1127 if (sq->sink_tainted) {
1128 sink_time = sq->sinktime = my_segment_to_running_time (&sq->sink_segment,
1129 sq->sink_segment.position);
1131 GST_DEBUG_OBJECT (mq,
1132 "queue %d sink_segment.position:%" GST_TIME_FORMAT ", sink_time:%"
1133 GST_STIME_FORMAT, sq->id, GST_TIME_ARGS (sq->sink_segment.position),
1134 GST_STIME_ARGS (sink_time));
1136 if (G_UNLIKELY (sq->last_time == GST_CLOCK_STIME_NONE)) {
1137 /* If the single queue still doesn't have a last_time set, this means
1138 * that nothing has been pushed out yet.
1139 * In order for the high_time computation to be as efficient as possible,
1140 * we set the last_time */
1141 sq->last_time = sink_time;
1143 if (G_UNLIKELY (sink_time != GST_CLOCK_STIME_NONE)) {
1144 /* if we have a time, we become untainted and use the time */
1145 sq->sink_tainted = FALSE;
1146 if (mq->use_interleave) {
1147 sq->cached_sinktime = sink_time;
1148 calculate_interleave (mq);
1152 sink_time = sq->sinktime;
1154 if (sq->src_tainted) {
1155 GstSegment *segment;
1158 if (sq->has_src_segment) {
1159 segment = &sq->src_segment;
1160 position = sq->src_segment.position;
1163 * If the src pad had no segment yet, use the sink segment
1164 * to avoid signalling overrun if the received sink segment has a
1165 * a position > max-size-time while the src pad time would be the default=0
1167 * This can happen when switching pads on chained/adaptive streams and the
1168 * new chain has a segment with a much larger position
1170 segment = &sq->sink_segment;
1171 position = sq->sink_segment.position;
1174 src_time = sq->srctime = my_segment_to_running_time (segment, position);
1175 /* if we have a time, we become untainted and use the time */
1176 if (G_UNLIKELY (src_time != GST_CLOCK_STIME_NONE)) {
1177 sq->src_tainted = FALSE;
1180 src_time = sq->srctime;
1182 GST_DEBUG_OBJECT (mq,
1183 "queue %d, sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT, sq->id,
1184 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
1186 /* This allows for streams with out of order timestamping - sometimes the
1187 * emerging timestamp is later than the arriving one(s) */
1188 if (G_LIKELY (GST_CLOCK_STIME_IS_VALID (sink_time) &&
1189 GST_CLOCK_STIME_IS_VALID (src_time) && sink_time > src_time))
1190 sq->cur_time = sink_time - src_time;
1194 /* updating the time level can change the buffering state */
1195 update_buffering (mq, sq);
1200 /* take a SEGMENT event and apply the values to segment, updating the time
1201 * level of queue. */
1203 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1204 GstSegment * segment)
1206 gst_event_copy_segment (event, segment);
1208 /* now configure the values, we use these to track timestamps on the
1210 if (segment->format != GST_FORMAT_TIME) {
1211 /* non-time format, pretent the current time segment is closed with a
1212 * 0 start and unknown stop time. */
1213 segment->format = GST_FORMAT_TIME;
1218 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1220 /* Make sure we have a valid initial segment position (and not garbage
1222 if (segment->rate > 0.0)
1223 segment->position = segment->start;
1225 segment->position = segment->stop;
1226 if (segment == &sq->sink_segment)
1227 sq->sink_tainted = TRUE;
1229 sq->has_src_segment = TRUE;
1230 sq->src_tainted = TRUE;
1233 GST_DEBUG_OBJECT (mq,
1234 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1236 /* segment can update the time level of the queue */
1237 update_time_level (mq, sq);
1239 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1240 gst_multi_queue_post_buffering (mq);
1243 /* take a buffer and update segment, updating the time level of the queue. */
1245 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1246 GstClockTime duration, GstSegment * segment)
1248 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1250 /* if no timestamp is set, assume it's continuous with the previous
1252 if (timestamp == GST_CLOCK_TIME_NONE)
1253 timestamp = segment->position;
1256 if (duration != GST_CLOCK_TIME_NONE)
1257 timestamp += duration;
1259 GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1260 sq->id, segment == &sq->sink_segment ? "sink" : "src",
1261 GST_TIME_ARGS (timestamp));
1263 segment->position = timestamp;
1265 if (segment == &sq->sink_segment)
1266 sq->sink_tainted = TRUE;
1268 sq->src_tainted = TRUE;
1270 /* calc diff with other end */
1271 update_time_level (mq, sq);
1272 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1273 gst_multi_queue_post_buffering (mq);
1277 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1278 GstSegment * segment)
1280 GstClockTime timestamp;
1281 GstClockTime duration;
1283 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1285 gst_event_parse_gap (event, ×tamp, &duration);
1287 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1289 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1290 timestamp += duration;
1293 segment->position = timestamp;
1295 if (segment == &sq->sink_segment)
1296 sq->sink_tainted = TRUE;
1298 sq->src_tainted = TRUE;
1300 /* calc diff with other end */
1301 update_time_level (mq, sq);
1304 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1305 gst_multi_queue_post_buffering (mq);
1308 static GstClockTimeDiff
1309 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1311 GstClockTimeDiff time = GST_CLOCK_STIME_NONE;
1313 if (GST_IS_BUFFER (object)) {
1314 GstBuffer *buf = GST_BUFFER_CAST (object);
1315 GstClockTime btime = GST_BUFFER_DTS_OR_PTS (buf);
1317 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1318 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1319 btime += GST_BUFFER_DURATION (buf);
1320 if (btime > segment->stop)
1321 btime = segment->stop;
1322 time = my_segment_to_running_time (segment, btime);
1324 } else if (GST_IS_BUFFER_LIST (object)) {
1325 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1329 n = gst_buffer_list_length (list);
1330 for (i = 0; i < n; i++) {
1332 buf = gst_buffer_list_get (list, i);
1333 btime = GST_BUFFER_DTS_OR_PTS (buf);
1334 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1335 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1336 btime += GST_BUFFER_DURATION (buf);
1337 if (btime > segment->stop)
1338 btime = segment->stop;
1339 time = my_segment_to_running_time (segment, btime);
1346 } else if (GST_IS_EVENT (object)) {
1347 GstEvent *event = GST_EVENT_CAST (object);
1349 /* For newsegment events return the running time of the start position */
1350 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1351 const GstSegment *new_segment;
1353 gst_event_parse_segment (event, &new_segment);
1354 if (new_segment->format == GST_FORMAT_TIME) {
1356 my_segment_to_running_time ((GstSegment *) new_segment,
1357 new_segment->start);
1366 static GstFlowReturn
1367 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1368 GstMiniObject * object, gboolean * allow_drop)
1370 GstFlowReturn result = sq->srcresult;
1372 if (GST_IS_BUFFER (object)) {
1374 GstClockTime timestamp, duration;
1376 buffer = GST_BUFFER_CAST (object);
1377 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1378 duration = GST_BUFFER_DURATION (buffer);
1380 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1382 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1383 gst_data_queue_limits_changed (sq->queue);
1385 if (G_UNLIKELY (*allow_drop)) {
1386 GST_DEBUG_OBJECT (mq,
1387 "SingleQueue %d : Dropping EOS buffer %p with ts %" GST_TIME_FORMAT,
1388 sq->id, buffer, GST_TIME_ARGS (timestamp));
1389 gst_buffer_unref (buffer);
1391 GST_DEBUG_OBJECT (mq,
1392 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1393 sq->id, buffer, GST_TIME_ARGS (timestamp));
1394 result = gst_pad_push (sq->srcpad, buffer);
1396 } else if (GST_IS_EVENT (object)) {
1399 event = GST_EVENT_CAST (object);
1401 switch (GST_EVENT_TYPE (event)) {
1403 result = GST_FLOW_EOS;
1404 if (G_UNLIKELY (*allow_drop))
1405 *allow_drop = FALSE;
1407 case GST_EVENT_SEGMENT:
1408 apply_segment (mq, sq, event, &sq->src_segment);
1409 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1410 gst_data_queue_limits_changed (sq->queue);
1411 if (G_UNLIKELY (*allow_drop)) {
1412 result = GST_FLOW_OK;
1413 *allow_drop = FALSE;
1417 apply_gap (mq, sq, event, &sq->src_segment);
1418 /* Applying the gap may have made the queue non-full again, unblock it if needed */
1419 gst_data_queue_limits_changed (sq->queue);
1425 if (G_UNLIKELY (*allow_drop)) {
1426 GST_DEBUG_OBJECT (mq,
1427 "SingleQueue %d : Dropping EOS event %p of type %s",
1428 sq->id, event, GST_EVENT_TYPE_NAME (event));
1429 gst_event_unref (event);
1431 GST_DEBUG_OBJECT (mq,
1432 "SingleQueue %d : Pushing event %p of type %s",
1433 sq->id, event, GST_EVENT_TYPE_NAME (event));
1435 gst_pad_push_event (sq->srcpad, event);
1437 } else if (GST_IS_QUERY (object)) {
1441 query = GST_QUERY_CAST (object);
1443 if (G_UNLIKELY (*allow_drop)) {
1444 GST_DEBUG_OBJECT (mq,
1445 "SingleQueue %d : Dropping EOS query %p", sq->id, query);
1446 gst_query_unref (query);
1449 res = gst_pad_peer_query (sq->srcpad, query);
1452 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1453 sq->last_query = res;
1454 sq->last_handled_query = query;
1455 g_cond_signal (&sq->query_handled);
1456 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1458 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1466 static GstMiniObject *
1467 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1472 item->object = NULL;
1478 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1480 if (!item->is_query && item->object)
1481 gst_mini_object_unref (item->object);
1482 g_slice_free (GstMultiQueueItem, item);
1485 /* takes ownership of passed mini object! */
1486 static GstMultiQueueItem *
1487 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1489 GstMultiQueueItem *item;
1491 item = g_slice_new (GstMultiQueueItem);
1492 item->object = object;
1493 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1494 item->posid = curid;
1495 item->is_query = GST_IS_QUERY (object);
1497 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1498 item->duration = GST_BUFFER_DURATION (object);
1499 if (item->duration == GST_CLOCK_TIME_NONE)
1501 item->visible = TRUE;
1505 static GstMultiQueueItem *
1506 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1508 GstMultiQueueItem *item;
1510 item = g_slice_new (GstMultiQueueItem);
1511 item->object = object;
1512 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1513 item->posid = curid;
1514 item->is_query = GST_IS_QUERY (object);
1518 item->visible = FALSE;
1522 /* Each main loop attempts to push buffers until the return value
1523 * is not-linked. not-linked pads are not allowed to push data beyond
1524 * any linked pads, so they don't 'rush ahead of the pack'.
1527 gst_multi_queue_loop (GstPad * pad)
1530 GstMultiQueueItem *item;
1531 GstDataQueueItem *sitem;
1533 GstMiniObject *object = NULL;
1535 GstFlowReturn result;
1536 GstClockTimeDiff next_time;
1538 gboolean do_update_buffering = FALSE;
1539 gboolean dropping = FALSE;
1541 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1545 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1550 /* Get something from the queue, blocking until that happens, or we get
1552 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1555 item = (GstMultiQueueItem *) sitem;
1556 newid = item->posid;
1558 /* steal the object and destroy the item */
1559 object = gst_multi_queue_item_steal_object (item);
1560 gst_multi_queue_item_destroy (item);
1562 is_buffer = GST_IS_BUFFER (object);
1564 /* Get running time of the item. Events will have GST_CLOCK_STIME_NONE */
1565 next_time = get_running_time (&sq->src_segment, object, FALSE);
1567 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1568 sq->id, newid, sq->last_oldid);
1570 /* If we're not-linked, we do some extra work because we might need to
1571 * wait before pushing. If we're linked but there's a gap in the IDs,
1572 * or it's the first loop, or we just passed the previous highid,
1573 * we might need to wake some sleeping pad up, so there's extra work
1575 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1576 if (sq->srcresult == GST_FLOW_NOT_LINKED
1577 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1578 || sq->last_oldid > mq->highid) {
1579 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1580 gst_flow_get_name (sq->srcresult));
1582 /* Check again if we're flushing after the lock is taken,
1583 * the flush flag might have been changed in the meantime */
1585 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1589 /* Update the nextid so other threads know when to wake us up */
1591 /* Take into account the extra cache time since we're unlinked */
1592 if (GST_CLOCK_STIME_IS_VALID (next_time))
1593 next_time += mq->unlinked_cache_time;
1594 sq->next_time = next_time;
1596 /* Update the oldid (the last ID we output) for highid tracking */
1597 if (sq->last_oldid != G_MAXUINT32)
1598 sq->oldid = sq->last_oldid;
1600 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1601 /* Go to sleep until it's time to push this buffer */
1603 /* Recompute the highid */
1604 compute_high_id (mq);
1605 /* Recompute the high time */
1606 compute_high_time (mq);
1608 while (((mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (next_time)
1609 && (mq->high_time == GST_CLOCK_STIME_NONE
1610 || next_time > mq->high_time))
1611 || (!mq->sync_by_running_time && newid > mq->highid))
1612 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1614 GST_DEBUG_OBJECT (mq,
1615 "queue %d sleeping for not-linked wakeup with "
1616 "newid %u, highid %u, next_time %" GST_STIME_FORMAT
1617 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1618 GST_STIME_ARGS (next_time), GST_STIME_ARGS (mq->high_time));
1620 /* Wake up all non-linked pads before we sleep */
1621 wake_up_next_non_linked (mq);
1624 g_cond_wait (&sq->turn, &mq->qlock);
1628 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1632 /* Recompute the high time */
1633 compute_high_time (mq);
1635 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1636 "wakeup with newid %u, highid %u, next_time %" GST_STIME_FORMAT
1637 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1638 GST_STIME_ARGS (next_time), GST_STIME_ARGS (mq->high_time));
1641 /* Re-compute the high_id in case someone else pushed */
1642 compute_high_id (mq);
1643 compute_high_time (mq);
1645 compute_high_id (mq);
1646 compute_high_time (mq);
1647 /* Wake up all non-linked pads */
1648 wake_up_next_non_linked (mq);
1650 /* We're done waiting, we can clear the nextid and nexttime */
1652 sq->next_time = GST_CLOCK_STIME_NONE;
1654 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1659 GST_LOG_OBJECT (mq, "sq:%d BEFORE PUSHING sq->srcresult: %s", sq->id,
1660 gst_flow_get_name (sq->srcresult));
1662 /* Update time stats */
1663 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1664 next_time = get_running_time (&sq->src_segment, object, TRUE);
1665 if (GST_CLOCK_STIME_IS_VALID (next_time)) {
1666 if (sq->last_time == GST_CLOCK_STIME_NONE || sq->last_time < next_time)
1667 sq->last_time = next_time;
1668 if (mq->high_time == GST_CLOCK_STIME_NONE || mq->high_time <= next_time) {
1669 /* Wake up all non-linked pads now that we advanced the high time */
1670 mq->high_time = next_time;
1671 wake_up_next_non_linked (mq);
1674 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1676 /* Try to push out the new object */
1677 result = gst_single_queue_push_one (mq, sq, object, &dropping);
1680 /* Check if we pushed something already and if this is
1681 * now a switch from an active to a non-active stream.
1683 * If it is, we reset all the waiting streams, let them
1684 * push another buffer to see if they're now active again.
1685 * This allows faster switching between streams and prevents
1686 * deadlocks if downstream does any waiting too.
1688 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1689 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1690 && result == GST_FLOW_NOT_LINKED) {
1693 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1696 compute_high_id (mq);
1697 do_update_buffering = TRUE;
1699 /* maybe no-one is waiting */
1700 if (mq->numwaiting > 0) {
1701 /* Else figure out which singlequeue(s) need waking up */
1702 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1703 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1705 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1706 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1707 sq2->pushed = FALSE;
1708 sq2->srcresult = GST_FLOW_OK;
1709 g_cond_signal (&sq2->turn);
1718 /* now hold on a bit;
1719 * can not simply throw this result to upstream, because
1720 * that might already be onto another segment, so we have to make
1721 * sure we are relaying the correct info wrt proper segment */
1722 if (result == GST_FLOW_EOS && !dropping &&
1723 sq->srcresult != GST_FLOW_NOT_LINKED) {
1724 GST_DEBUG_OBJECT (mq, "starting EOS drop on sq %d", sq->id);
1726 /* pretend we have not seen EOS yet for upstream's sake */
1727 result = sq->srcresult;
1728 } else if (dropping && gst_data_queue_is_empty (sq->queue)) {
1729 /* queue empty, so stop dropping
1730 * we can commit the result we have now,
1731 * which is either OK after a segment, or EOS */
1732 GST_DEBUG_OBJECT (mq, "committed EOS drop on sq %d", sq->id);
1734 result = GST_FLOW_EOS;
1736 sq->srcresult = result;
1737 sq->last_oldid = newid;
1739 if (do_update_buffering)
1740 update_buffering (mq, sq);
1742 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1743 gst_multi_queue_post_buffering (mq);
1748 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1749 && result != GST_FLOW_EOS)
1752 GST_LOG_OBJECT (mq, "sq:%d AFTER PUSHING sq->srcresult: %s", sq->id,
1753 gst_flow_get_name (sq->srcresult));
1755 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1756 if (mq->numwaiting > 0 && sq->srcresult == GST_FLOW_EOS) {
1757 compute_high_time (mq);
1758 compute_high_id (mq);
1759 wake_up_next_non_linked (mq);
1761 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1768 gst_mini_object_unref (object);
1770 /* Need to make sure wake up any sleeping pads when we exit */
1771 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1772 compute_high_time (mq);
1773 compute_high_id (mq);
1774 wake_up_next_non_linked (mq);
1775 sq->last_query = FALSE;
1776 g_cond_signal (&sq->query_handled);
1778 /* Post an error message if we got EOS while downstream
1779 * has returned an error flow return. After EOS there
1780 * will be no further buffer which could propagate the
1782 if (sq->is_eos && sq->srcresult < GST_FLOW_EOS) {
1783 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1784 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
1785 ("Internal data stream error."),
1786 ("streaming stopped, reason %s", gst_flow_get_name (sq->srcresult)));
1788 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1791 /* upstream needs to see fatal result ASAP to shut things down,
1792 * but might be stuck in one of our other full queues;
1793 * so empty this one and trigger dynamic queue growth. At
1794 * this point the srcresult is not OK, NOT_LINKED
1795 * or EOS, i.e. a real failure */
1796 gst_single_queue_flush_queue (sq, FALSE);
1797 single_queue_underrun_cb (sq->queue, sq);
1798 gst_data_queue_set_flushing (sq->queue, TRUE);
1799 gst_pad_pause_task (sq->srcpad);
1800 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1801 "SingleQueue[%d] task paused, reason:%s",
1802 sq->id, gst_flow_get_name (sq->srcresult));
1808 * gst_multi_queue_chain:
1810 * This is similar to GstQueue's chain function, except:
1811 * _ we don't have leak behaviours,
1812 * _ we push with a unique id (curid)
1814 static GstFlowReturn
1815 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1819 GstMultiQueueItem *item;
1821 GstClockTime timestamp, duration;
1823 sq = gst_pad_get_element_private (pad);
1826 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1832 /* Get a unique incrementing id */
1833 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1835 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1836 duration = GST_BUFFER_DURATION (buffer);
1839 "SingleQueue %d : about to enqueue buffer %p with id %d (pts:%"
1840 GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT " dur:%" GST_TIME_FORMAT ")",
1841 sq->id, buffer, curid, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1842 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (duration));
1844 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1846 /* Update interleave before pushing data into queue */
1847 if (mq->use_interleave) {
1848 GstClockTime val = timestamp;
1849 GstClockTimeDiff dval;
1851 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1852 if (val == GST_CLOCK_TIME_NONE)
1853 val = sq->sink_segment.position;
1854 if (duration != GST_CLOCK_TIME_NONE)
1857 dval = my_segment_to_running_time (&sq->sink_segment, val);
1858 if (GST_CLOCK_STIME_IS_VALID (dval)) {
1859 sq->cached_sinktime = dval;
1860 GST_DEBUG_OBJECT (mq,
1861 "Queue %d cached sink time now %" G_GINT64_FORMAT " %"
1862 GST_STIME_FORMAT, sq->id, sq->cached_sinktime,
1863 GST_STIME_ARGS (sq->cached_sinktime));
1864 calculate_interleave (mq);
1866 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1869 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1872 /* update time level, we must do this after pushing the data in the queue so
1873 * that we never end up filling the queue first. */
1874 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1877 return sq->srcresult;
1882 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1883 sq->id, gst_flow_get_name (sq->srcresult));
1884 gst_multi_queue_item_destroy (item);
1889 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
1890 gst_buffer_unref (buffer);
1891 return GST_FLOW_EOS;
1896 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
1897 GstPadMode mode, gboolean active)
1903 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1904 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1906 /* mq is NULL if the pad is activated/deactivated before being
1907 * added to the multiqueue */
1909 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1912 case GST_PAD_MODE_PUSH:
1914 /* All pads start off linked until they push one buffer */
1915 sq->srcresult = GST_FLOW_OK;
1917 gst_data_queue_set_flushing (sq->queue, FALSE);
1919 sq->srcresult = GST_FLOW_FLUSHING;
1920 sq->last_query = FALSE;
1921 g_cond_signal (&sq->query_handled);
1922 gst_data_queue_set_flushing (sq->queue, TRUE);
1924 /* Wait until streaming thread has finished */
1926 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1927 GST_PAD_STREAM_LOCK (pad);
1929 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1930 gst_data_queue_flush (sq->queue);
1932 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1933 GST_PAD_STREAM_UNLOCK (pad);
1935 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1945 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1946 gst_object_unref (mq);
1952 static GstFlowReturn
1953 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1958 GstMultiQueueItem *item;
1959 gboolean res = TRUE;
1960 GstFlowReturn flowret = GST_FLOW_OK;
1962 GstEvent *sref = NULL;
1964 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1965 mq = (GstMultiQueue *) parent;
1967 type = GST_EVENT_TYPE (event);
1970 case GST_EVENT_STREAM_START:
1972 if (mq->sync_by_running_time) {
1973 GstStreamFlags stream_flags;
1974 gst_event_parse_stream_flags (event, &stream_flags);
1975 if ((stream_flags & GST_STREAM_FLAG_SPARSE)) {
1976 GST_INFO_OBJECT (mq, "SingleQueue %d is a sparse stream", sq->id);
1977 sq->is_sparse = TRUE;
1979 sq->thread = g_thread_self ();
1982 sq->thread = g_thread_self ();
1984 /* Remove EOS flag */
1988 case GST_EVENT_FLUSH_START:
1989 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1992 res = gst_pad_push_event (sq->srcpad, event);
1994 gst_single_queue_flush (mq, sq, TRUE, FALSE);
1997 case GST_EVENT_FLUSH_STOP:
1998 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
2001 res = gst_pad_push_event (sq->srcpad, event);
2003 gst_single_queue_flush (mq, sq, FALSE, FALSE);
2006 case GST_EVENT_SEGMENT:
2007 sref = gst_event_ref (event);
2010 /* take ref because the queue will take ownership and we need the event
2011 * afterwards to update the segment */
2012 sref = gst_event_ref (event);
2013 if (mq->use_interleave) {
2014 GstClockTime val, dur;
2016 gst_event_parse_gap (event, &val, &dur);
2017 if (GST_CLOCK_TIME_IS_VALID (val)) {
2018 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2019 if (GST_CLOCK_TIME_IS_VALID (dur))
2021 stime = my_segment_to_running_time (&sq->sink_segment, val);
2022 if (GST_CLOCK_STIME_IS_VALID (stime)) {
2023 sq->cached_sinktime = stime;
2024 calculate_interleave (mq);
2026 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2032 if (!(GST_EVENT_IS_SERIALIZED (event))) {
2033 res = gst_pad_push_event (sq->srcpad, event);
2039 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2043 /* Get an unique incrementing id. */
2044 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2046 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
2048 GST_DEBUG_OBJECT (mq,
2049 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
2050 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
2052 if (!gst_data_queue_push (sq->queue, (GstDataQueueItem *) item))
2055 /* mark EOS when we received one, we must do that after putting the
2056 * buffer in the queue because EOS marks the buffer as filled. */
2059 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2062 /* Post an error message if we got EOS while downstream
2063 * has returned an error flow return. After EOS there
2064 * will be no further buffer which could propagate the
2066 if (sq->srcresult < GST_FLOW_EOS) {
2067 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2068 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
2069 ("Internal data stream error."),
2070 ("streaming stopped, reason %s",
2071 gst_flow_get_name (sq->srcresult)));
2073 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2076 /* EOS affects the buffering state */
2077 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2078 update_buffering (mq, sq);
2079 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2080 single_queue_overrun_cb (sq->queue, sq);
2081 gst_multi_queue_post_buffering (mq);
2083 case GST_EVENT_SEGMENT:
2084 apply_segment (mq, sq, sref, &sq->sink_segment);
2085 gst_event_unref (sref);
2086 /* a new segment allows us to accept more buffers if we got EOS
2087 * from downstream */
2088 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2089 if (sq->srcresult == GST_FLOW_EOS)
2090 sq->srcresult = GST_FLOW_OK;
2091 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2095 apply_gap (mq, sq, sref, &sq->sink_segment);
2096 gst_event_unref (sref);
2103 flowret = GST_FLOW_ERROR;
2104 GST_DEBUG_OBJECT (mq, "SingleQueue %d : returning %s", sq->id,
2105 gst_flow_get_name (flowret));
2110 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2111 sq->id, gst_flow_get_name (sq->srcresult));
2113 gst_event_unref (sref);
2114 gst_multi_queue_item_destroy (item);
2115 return sq->srcresult;
2119 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return GST_FLOW_EOS");
2120 gst_event_unref (event);
2121 return GST_FLOW_EOS;
2126 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
2132 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2133 mq = (GstMultiQueue *) parent;
2135 switch (GST_QUERY_TYPE (query)) {
2137 if (GST_QUERY_IS_SERIALIZED (query)) {
2139 GstMultiQueueItem *item;
2141 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2142 if (sq->srcresult != GST_FLOW_OK)
2145 /* serialized events go in the queue. We need to be certain that we
2146 * don't cause deadlocks waiting for the query return value. We check if
2147 * the queue is empty (nothing is blocking downstream and the query can
2148 * be pushed for sure) or we are not buffering. If we are buffering,
2149 * the pipeline waits to unblock downstream until our queue fills up
2150 * completely, which can not happen if we block on the query..
2151 * Therefore we only potentially block when we are not buffering. */
2152 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
2153 /* Get an unique incrementing id. */
2154 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2156 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
2158 GST_DEBUG_OBJECT (mq,
2159 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
2160 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
2161 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2162 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
2163 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2164 /* it might be that the query has been taken out of the queue
2165 * while we were unlocked. So, we need to check if the last
2166 * handled query is the same one than the one we just
2167 * pushed. If it is, we don't need to wait for the condition
2168 * variable, otherwise we wait for the condition variable to
2170 if (sq->last_handled_query != query)
2171 g_cond_wait (&sq->query_handled, &mq->qlock);
2172 res = sq->last_query;
2173 sq->last_handled_query = NULL;
2175 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
2176 "queue is not empty");
2179 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2181 /* default handling */
2182 res = gst_pad_query_default (pad, parent, query);
2190 GST_DEBUG_OBJECT (mq, "Flushing");
2191 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2197 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2198 GstPadMode mode, gboolean active)
2204 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2207 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2210 case GST_PAD_MODE_PUSH:
2212 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
2214 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
2215 /* make sure streaming finishes */
2216 result |= gst_pad_stop_task (pad);
2227 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2229 GstSingleQueue *sq = gst_pad_get_element_private (pad);
2230 GstMultiQueue *mq = sq->mqueue;
2233 switch (GST_EVENT_TYPE (event)) {
2234 case GST_EVENT_RECONFIGURE:
2235 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2236 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2237 sq->srcresult = GST_FLOW_OK;
2238 g_cond_signal (&sq->turn);
2240 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2242 ret = gst_pad_push_event (sq->sinkpad, event);
2245 ret = gst_pad_push_event (sq->sinkpad, event);
2253 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2257 /* FIXME, Handle position offset depending on queue size */
2258 switch (GST_QUERY_TYPE (query)) {
2260 /* default handling */
2261 res = gst_pad_query_default (pad, parent, query);
2268 * Next-non-linked functions
2271 /* WITH LOCK TAKEN */
2273 wake_up_next_non_linked (GstMultiQueue * mq)
2277 /* maybe no-one is waiting */
2278 if (mq->numwaiting < 1)
2281 if (mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (mq->high_time)) {
2282 /* Else figure out which singlequeue(s) need waking up */
2283 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2284 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2285 if (sq->srcresult == GST_FLOW_NOT_LINKED
2286 && GST_CLOCK_STIME_IS_VALID (sq->next_time)
2287 && sq->next_time <= mq->high_time) {
2288 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2289 g_cond_signal (&sq->turn);
2293 /* Else figure out which singlequeue(s) need waking up */
2294 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2295 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2296 if (sq->srcresult == GST_FLOW_NOT_LINKED &&
2297 sq->nextid != 0 && sq->nextid <= mq->highid) {
2298 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2299 g_cond_signal (&sq->turn);
2305 /* WITH LOCK TAKEN */
2307 compute_high_id (GstMultiQueue * mq)
2309 /* The high-id is either the highest id among the linked pads, or if all
2310 * pads are not-linked, it's the lowest not-linked pad */
2312 guint32 lowest = G_MAXUINT32;
2313 guint32 highid = G_MAXUINT32;
2315 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2316 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2318 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2319 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2321 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2322 /* No need to consider queues which are not waiting */
2323 if (sq->nextid == 0) {
2324 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2328 if (sq->nextid < lowest)
2329 lowest = sq->nextid;
2330 } else if (sq->srcresult != GST_FLOW_EOS) {
2331 /* If we don't have a global highid, or the global highid is lower than
2332 * this single queue's last outputted id, store the queue's one,
2333 * unless the singlequeue is at EOS (srcresult = EOS) */
2334 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2339 if (highid == G_MAXUINT32 || lowest < highid)
2340 mq->highid = lowest;
2342 mq->highid = highid;
2344 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2348 /* WITH LOCK TAKEN */
2350 compute_high_time (GstMultiQueue * mq)
2352 /* The high-time is either the highest last time among the linked
2353 * pads, or if all pads are not-linked, it's the lowest nex time of
2356 GstClockTimeDiff highest = GST_CLOCK_STIME_NONE;
2357 GstClockTimeDiff lowest = GST_CLOCK_STIME_NONE;
2359 if (!mq->sync_by_running_time)
2362 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2363 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2366 "inspecting sq:%d , next_time:%" GST_STIME_FORMAT ", last_time:%"
2367 GST_STIME_FORMAT ", srcresult:%s", sq->id,
2368 GST_STIME_ARGS (sq->next_time), GST_STIME_ARGS (sq->last_time),
2369 gst_flow_get_name (sq->srcresult));
2371 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2372 /* No need to consider queues which are not waiting */
2373 if (!GST_CLOCK_STIME_IS_VALID (sq->next_time)) {
2374 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2378 if (lowest == GST_CLOCK_STIME_NONE || sq->next_time < lowest)
2379 lowest = sq->next_time;
2380 } else if (sq->srcresult != GST_FLOW_EOS) {
2381 /* If we don't have a global high time, or the global high time
2382 * is lower than this single queue's last outputted time, store
2383 * the queue's one, unless the singlequeue is at EOS (srcresult
2385 if (highest == GST_CLOCK_STIME_NONE
2386 || (sq->last_time != GST_CLOCK_STIME_NONE && sq->last_time > highest))
2387 highest = sq->last_time;
2390 "highest now %" GST_STIME_FORMAT " lowest %" GST_STIME_FORMAT,
2391 GST_STIME_ARGS (highest), GST_STIME_ARGS (lowest));
2394 if (highest == GST_CLOCK_STIME_NONE)
2395 mq->high_time = lowest;
2397 mq->high_time = highest;
2400 "High time is now : %" GST_STIME_FORMAT ", lowest non-linked %"
2401 GST_STIME_FORMAT, GST_STIME_ARGS (mq->high_time),
2402 GST_STIME_ARGS (lowest));
2405 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2406 ((q)->max_size.format) <= (value))
2409 * GstSingleQueue functions
2412 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2414 GstMultiQueue *mq = sq->mqueue;
2416 GstDataQueueSize size;
2417 gboolean filled = TRUE;
2418 gboolean empty_found = FALSE;
2420 gst_data_queue_get_level (sq->queue, &size);
2423 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2424 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2425 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2428 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2430 /* check if we reached the hard time/bytes limits */
2431 if (sq->is_eos || sq->is_sparse || IS_FILLED (sq, bytes, size.bytes) ||
2432 IS_FILLED (sq, time, sq->cur_time)) {
2436 /* Search for empty queues */
2437 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2438 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2443 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2444 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2448 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2449 if (gst_data_queue_is_empty (oq->queue) && !oq->is_sparse) {
2450 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2456 /* if hard limits are not reached then we allow one more buffer in the full
2457 * queue, but only if any of the other singelqueues are empty */
2459 if (IS_FILLED (sq, visible, size.visible)) {
2460 sq->max_size.visible = size.visible + 1;
2461 GST_DEBUG_OBJECT (mq,
2462 "Bumping single queue %d max visible to %d",
2463 sq->id, sq->max_size.visible);
2469 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2471 /* Overrun is always forwarded, since this is blocking the upstream element */
2473 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2474 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2479 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2481 gboolean empty = TRUE;
2482 GstMultiQueue *mq = sq->mqueue;
2485 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2486 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2490 "Single Queue %d is empty, Checking other single queues", sq->id);
2493 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2494 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2495 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2497 if (gst_data_queue_is_full (oq->queue)) {
2498 GstDataQueueSize size;
2500 gst_data_queue_get_level (oq->queue, &size);
2501 if (IS_FILLED (oq, visible, size.visible)) {
2502 oq->max_size.visible = size.visible + 1;
2503 GST_DEBUG_OBJECT (mq,
2504 "queue %d is filled, bumping its max visible to %d", oq->id,
2505 oq->max_size.visible);
2506 gst_data_queue_limits_changed (oq->queue);
2509 if (!gst_data_queue_is_empty (oq->queue) || oq->is_sparse)
2512 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2515 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2516 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2521 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2522 guint64 time, GstSingleQueue * sq)
2525 GstMultiQueue *mq = sq->mqueue;
2527 GST_DEBUG_OBJECT (mq,
2528 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2529 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2530 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2532 /* we are always filled on EOS */
2536 /* we never go past the max visible items unless we are in buffering mode */
2537 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2540 /* check time or bytes */
2541 res = IS_FILLED (sq, bytes, bytes);
2542 /* We only care about limits in time if we're not a sparse stream or
2543 * we're not syncing by running time */
2544 if (!sq->is_sparse || !mq->sync_by_running_time) {
2545 /* If unlinked, take into account the extra unlinked cache time */
2546 if (mq->sync_by_running_time && sq->srcresult == GST_FLOW_NOT_LINKED) {
2547 if (sq->cur_time > mq->unlinked_cache_time)
2548 res |= IS_FILLED (sq, time, sq->cur_time - mq->unlinked_cache_time);
2552 res |= IS_FILLED (sq, time, sq->cur_time);
2559 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2561 GstDataQueueItem *sitem;
2562 GstMultiQueueItem *mitem;
2563 gboolean was_flushing = FALSE;
2565 while (!gst_data_queue_is_empty (sq->queue)) {
2566 GstMiniObject *data;
2568 /* FIXME: If this fails here although the queue is not empty,
2569 * we're flushing... but we want to rescue all sticky
2570 * events nonetheless.
2572 if (!gst_data_queue_pop (sq->queue, &sitem)) {
2573 was_flushing = TRUE;
2574 gst_data_queue_set_flushing (sq->queue, FALSE);
2578 mitem = (GstMultiQueueItem *) sitem;
2580 data = sitem->object;
2582 if (!full && !mitem->is_query && GST_IS_EVENT (data)
2583 && GST_EVENT_IS_STICKY (data)
2584 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2585 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2586 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2589 sitem->destroy (sitem);
2592 gst_data_queue_flush (sq->queue);
2594 gst_data_queue_set_flushing (sq->queue, TRUE);
2596 GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2597 update_buffering (sq->mqueue, sq);
2598 GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2599 gst_multi_queue_post_buffering (sq->mqueue);
2603 gst_single_queue_free (GstSingleQueue * sq)
2606 gst_data_queue_flush (sq->queue);
2607 g_object_unref (sq->queue);
2608 g_cond_clear (&sq->turn);
2609 g_cond_clear (&sq->query_handled);
2613 static GstSingleQueue *
2614 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2619 guint temp_id = (id == -1) ? 0 : id;
2621 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2623 /* Find an unused queue ID, if possible the passed one */
2624 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2625 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2626 /* This works because the IDs are sorted in ascending order */
2627 if (sq2->id == temp_id) {
2628 /* If this ID was requested by the caller return NULL,
2629 * otherwise just get us the next one */
2631 temp_id = sq2->id + 1;
2634 } else if (sq2->id > temp_id) {
2639 sq = g_new0 (GstSingleQueue, 1);
2643 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2644 mqueue->queues_cookie++;
2646 /* copy over max_size and extra_size so we don't need to take the lock
2647 * any longer when checking if the queue is full. */
2648 sq->max_size.visible = mqueue->max_size.visible;
2649 sq->max_size.bytes = mqueue->max_size.bytes;
2650 sq->max_size.time = mqueue->max_size.time;
2652 sq->extra_size.visible = mqueue->extra_size.visible;
2653 sq->extra_size.bytes = mqueue->extra_size.bytes;
2654 sq->extra_size.time = mqueue->extra_size.time;
2656 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2658 sq->mqueue = mqueue;
2659 sq->srcresult = GST_FLOW_FLUSHING;
2661 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2662 single_queue_check_full,
2663 (GstDataQueueFullCallback) single_queue_overrun_cb,
2664 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2666 sq->is_sparse = FALSE;
2667 sq->flushing = FALSE;
2669 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2670 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2674 sq->next_time = GST_CLOCK_STIME_NONE;
2675 sq->last_time = GST_CLOCK_STIME_NONE;
2676 g_cond_init (&sq->turn);
2677 g_cond_init (&sq->query_handled);
2679 sq->sinktime = GST_CLOCK_STIME_NONE;
2680 sq->srctime = GST_CLOCK_STIME_NONE;
2681 sq->sink_tainted = TRUE;
2682 sq->src_tainted = TRUE;
2684 name = g_strdup_printf ("sink_%u", sq->id);
2685 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
2688 gst_pad_set_chain_function (sq->sinkpad,
2689 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2690 gst_pad_set_activatemode_function (sq->sinkpad,
2691 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2692 gst_pad_set_event_full_function (sq->sinkpad,
2693 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2694 gst_pad_set_query_function (sq->sinkpad,
2695 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2696 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2697 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2698 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2700 name = g_strdup_printf ("src_%u", sq->id);
2701 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2704 gst_pad_set_activatemode_function (sq->srcpad,
2705 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2706 gst_pad_set_event_function (sq->srcpad,
2707 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2708 gst_pad_set_query_function (sq->srcpad,
2709 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2710 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2711 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2712 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2714 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2715 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2717 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2719 /* only activate the pads when we are not in the NULL state
2720 * and add the pad under the state_lock to prevend state changes
2721 * between activating and adding */
2722 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2723 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2724 gst_pad_set_active (sq->srcpad, TRUE);
2725 gst_pad_set_active (sq->sinkpad, TRUE);
2727 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2728 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2729 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2731 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",