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);
654 case PROP_UNLINKED_CACHE_TIME:
655 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
656 mq->unlinked_cache_time = g_value_get_uint64 (value);
657 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
658 gst_multi_queue_post_buffering (mq);
661 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
667 gst_multi_queue_get_property (GObject * object, guint prop_id,
668 GValue * value, GParamSpec * pspec)
670 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
672 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
675 case PROP_EXTRA_SIZE_BYTES:
676 g_value_set_uint (value, mq->extra_size.bytes);
678 case PROP_EXTRA_SIZE_BUFFERS:
679 g_value_set_uint (value, mq->extra_size.visible);
681 case PROP_EXTRA_SIZE_TIME:
682 g_value_set_uint64 (value, mq->extra_size.time);
684 case PROP_MAX_SIZE_BYTES:
685 g_value_set_uint (value, mq->max_size.bytes);
687 case PROP_MAX_SIZE_BUFFERS:
688 g_value_set_uint (value, mq->max_size.visible);
690 case PROP_MAX_SIZE_TIME:
691 g_value_set_uint64 (value, mq->max_size.time);
693 case PROP_USE_BUFFERING:
694 g_value_set_boolean (value, mq->use_buffering);
696 case PROP_LOW_PERCENT:
697 g_value_set_int (value, mq->low_percent);
699 case PROP_HIGH_PERCENT:
700 g_value_set_int (value, mq->high_percent);
702 case PROP_SYNC_BY_RUNNING_TIME:
703 g_value_set_boolean (value, mq->sync_by_running_time);
705 case PROP_USE_INTERLEAVE:
706 g_value_set_boolean (value, mq->use_interleave);
707 case PROP_UNLINKED_CACHE_TIME:
708 g_value_set_uint64 (value, mq->unlinked_cache_time);
711 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
715 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
719 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
721 GstIterator *it = NULL;
723 GstSingleQueue *squeue;
724 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
727 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
728 squeue = gst_pad_get_element_private (pad);
732 if (squeue->sinkpad == pad)
733 opad = gst_object_ref (squeue->srcpad);
734 else if (squeue->srcpad == pad)
735 opad = gst_object_ref (squeue->sinkpad);
739 g_value_init (&val, GST_TYPE_PAD);
740 g_value_set_object (&val, opad);
741 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
742 g_value_unset (&val);
744 gst_object_unref (opad);
747 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
758 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
759 const gchar * name, const GstCaps * caps)
761 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
762 GstSingleQueue *squeue;
766 sscanf (name + 4, "_%u", &temp_id);
767 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
770 /* Create a new single queue, add the sink and source pad and return the sink pad */
771 squeue = gst_single_queue_new (mqueue, temp_id);
773 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
774 GST_DEBUG_PAD_NAME (squeue->sinkpad));
776 return squeue ? squeue->sinkpad : NULL;
780 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
782 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
783 GstSingleQueue *sq = NULL;
786 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
788 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
789 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
790 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
791 sq = (GstSingleQueue *) tmp->data;
793 if (sq->sinkpad == pad)
798 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
799 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
803 /* FIXME: The removal of the singlequeue should probably not happen until it
804 * finishes draining */
806 /* remove it from the list */
807 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
808 mqueue->queues_cookie++;
810 /* FIXME : recompute next-non-linked */
811 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
813 /* delete SingleQueue */
814 gst_data_queue_set_flushing (sq->queue, TRUE);
816 gst_pad_set_active (sq->srcpad, FALSE);
817 gst_pad_set_active (sq->sinkpad, FALSE);
818 gst_pad_set_element_private (sq->srcpad, NULL);
819 gst_pad_set_element_private (sq->sinkpad, NULL);
820 gst_element_remove_pad (element, sq->srcpad);
821 gst_element_remove_pad (element, sq->sinkpad);
822 gst_single_queue_free (sq);
825 static GstStateChangeReturn
826 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
828 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
829 GstSingleQueue *sq = NULL;
830 GstStateChangeReturn result;
832 switch (transition) {
833 case GST_STATE_CHANGE_READY_TO_PAUSED:{
836 /* Set all pads to non-flushing */
837 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
838 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
839 sq = (GstSingleQueue *) tmp->data;
840 sq->flushing = FALSE;
843 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
844 SET_CHILD_PROPERTY (mqueue, visible);
846 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
847 gst_multi_queue_post_buffering (mqueue);
851 case GST_STATE_CHANGE_PAUSED_TO_READY:{
854 /* Un-wait all waiting pads */
855 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
856 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
857 sq = (GstSingleQueue *) tmp->data;
859 g_cond_signal (&sq->turn);
861 sq->last_query = FALSE;
862 g_cond_signal (&sq->query_handled);
864 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
871 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
873 switch (transition) {
882 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
887 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
891 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
892 sq->srcresult = GST_FLOW_FLUSHING;
893 gst_data_queue_set_flushing (sq->queue, TRUE);
897 /* wake up non-linked task */
898 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
900 g_cond_signal (&sq->turn);
901 sq->last_query = FALSE;
902 g_cond_signal (&sq->query_handled);
903 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
905 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
906 result = gst_pad_pause_task (sq->srcpad);
907 sq->sink_tainted = sq->src_tainted = TRUE;
909 gst_single_queue_flush_queue (sq, full);
911 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
912 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
913 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
914 sq->has_src_segment = FALSE;
915 /* All pads start off not-linked for a smooth kick-off */
916 sq->srcresult = GST_FLOW_OK;
919 sq->max_size.visible = mq->max_size.visible;
923 sq->last_oldid = G_MAXUINT32;
924 sq->next_time = GST_CLOCK_STIME_NONE;
925 sq->last_time = GST_CLOCK_STIME_NONE;
926 sq->cached_sinktime = GST_CLOCK_STIME_NONE;
927 gst_data_queue_set_flushing (sq->queue, FALSE);
929 /* Reset high time to be recomputed next */
930 mq->high_time = GST_CLOCK_STIME_NONE;
932 sq->flushing = FALSE;
933 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
935 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
937 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
943 /* WITH LOCK TAKEN */
945 get_percentage (GstSingleQueue * sq)
947 GstDataQueueSize size;
950 gst_data_queue_get_level (sq->queue, &size);
952 GST_DEBUG_OBJECT (sq->mqueue,
953 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
954 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
955 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
957 /* get bytes and time percentages and take the max */
958 if (sq->is_eos || sq->srcresult == GST_FLOW_NOT_LINKED || sq->is_sparse) {
962 if (sq->max_size.time > 0) {
963 tmp = (sq->cur_time * 100) / sq->max_size.time;
964 percent = MAX (percent, tmp);
966 if (sq->max_size.bytes > 0) {
967 tmp = (size.bytes * 100) / sq->max_size.bytes;
968 percent = MAX (percent, tmp);
975 /* WITH LOCK TAKEN */
977 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
981 /* nothing to dowhen we are not in buffering mode */
982 if (!mq->use_buffering)
985 percent = get_percentage (sq);
988 if (percent >= mq->high_percent) {
989 mq->buffering = FALSE;
991 /* make sure it increases */
992 percent = MAX (mq->percent, percent);
994 SET_PERCENT (mq, percent);
997 gboolean is_buffering = TRUE;
999 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
1000 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
1002 if (get_percentage (oq) >= mq->high_percent) {
1003 is_buffering = FALSE;
1009 if (is_buffering && percent < mq->low_percent) {
1010 mq->buffering = TRUE;
1011 SET_PERCENT (mq, percent);
1017 gst_multi_queue_post_buffering (GstMultiQueue * mq)
1019 GstMessage *msg = NULL;
1021 g_mutex_lock (&mq->buffering_post_lock);
1022 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1023 if (mq->percent_changed) {
1024 gint percent = mq->percent;
1026 mq->percent_changed = FALSE;
1028 percent = percent * 100 / mq->high_percent;
1033 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
1034 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
1036 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1039 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
1041 g_mutex_unlock (&mq->buffering_post_lock);
1045 calculate_interleave (GstMultiQueue * mq)
1047 GstClockTimeDiff low, high;
1048 GstClockTime interleave;
1051 low = high = GST_CLOCK_STIME_NONE;
1052 interleave = mq->interleave;
1053 /* Go over all single queues and calculate lowest/highest value */
1054 for (tmp = mq->queues; tmp; tmp = tmp->next) {
1055 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1056 /* Ignore sparse streams for interleave calculation */
1059 /* If a stream is not active yet (hasn't received any buffers), set
1060 * a maximum interleave to allow it to receive more data */
1063 "queue %d is not active yet, forcing interleave to 5s", sq->id);
1064 mq->interleave = 5 * GST_SECOND;
1065 /* Update max-size time */
1066 mq->max_size.time = mq->interleave;
1067 SET_CHILD_PROPERTY (mq, time);
1070 if (GST_CLOCK_STIME_IS_VALID (sq->cached_sinktime)) {
1071 if (low == GST_CLOCK_STIME_NONE || sq->cached_sinktime < low)
1072 low = sq->cached_sinktime;
1073 if (high == GST_CLOCK_STIME_NONE || sq->cached_sinktime > high)
1074 high = sq->cached_sinktime;
1077 "queue %d , sinktime:%" GST_STIME_FORMAT " low:%" GST_STIME_FORMAT
1078 " high:%" GST_STIME_FORMAT, sq->id,
1079 GST_STIME_ARGS (sq->cached_sinktime), GST_STIME_ARGS (low),
1080 GST_STIME_ARGS (high));
1083 if (GST_CLOCK_STIME_IS_VALID (low) && GST_CLOCK_STIME_IS_VALID (high)) {
1084 interleave = high - low;
1085 /* Padding of interleave and minimum value */
1086 /* FIXME : Make the minimum time interleave a property */
1087 interleave = (150 * interleave / 100) + 250 * GST_MSECOND;
1089 /* Update the stored interleave if:
1090 * * No data has arrived yet (high == low)
1091 * * Or it went higher
1092 * * Or it went lower and we've gone past the previous interleave needed */
1093 if (high == low || interleave > mq->interleave ||
1094 ((mq->last_interleave_update + (2 * MIN (GST_SECOND,
1095 mq->interleave)) < low)
1096 && interleave < (mq->interleave * 3 / 4))) {
1097 /* Update the interleave */
1098 mq->interleave = interleave;
1099 mq->last_interleave_update = high;
1100 /* Update max-size time */
1101 mq->max_size.time = mq->interleave;
1102 SET_CHILD_PROPERTY (mq, time);
1107 GST_DEBUG_OBJECT (mq,
1108 "low:%" GST_STIME_FORMAT " high:%" GST_STIME_FORMAT " interleave:%"
1109 GST_TIME_FORMAT " mq->interleave:%" GST_TIME_FORMAT
1110 " last_interleave_update:%" GST_STIME_FORMAT, GST_STIME_ARGS (low),
1111 GST_STIME_ARGS (high), GST_TIME_ARGS (interleave),
1112 GST_TIME_ARGS (mq->interleave),
1113 GST_STIME_ARGS (mq->last_interleave_update));
1117 /* calculate the diff between running time on the sink and src of the queue.
1118 * This is the total amount of time in the queue.
1119 * WITH LOCK TAKEN */
1121 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
1123 GstClockTimeDiff sink_time, src_time;
1125 if (sq->sink_tainted) {
1126 sink_time = sq->sinktime = my_segment_to_running_time (&sq->sink_segment,
1127 sq->sink_segment.position);
1129 GST_DEBUG_OBJECT (mq,
1130 "queue %d sink_segment.position:%" GST_TIME_FORMAT ", sink_time:%"
1131 GST_STIME_FORMAT, sq->id, GST_TIME_ARGS (sq->sink_segment.position),
1132 GST_STIME_ARGS (sink_time));
1134 if (G_UNLIKELY (sq->last_time == GST_CLOCK_STIME_NONE)) {
1135 /* If the single queue still doesn't have a last_time set, this means
1136 * that nothing has been pushed out yet.
1137 * In order for the high_time computation to be as efficient as possible,
1138 * we set the last_time */
1139 sq->last_time = sink_time;
1141 if (G_UNLIKELY (sink_time != GST_CLOCK_STIME_NONE)) {
1142 /* if we have a time, we become untainted and use the time */
1143 sq->sink_tainted = FALSE;
1144 if (mq->use_interleave) {
1145 sq->cached_sinktime = sink_time;
1146 calculate_interleave (mq);
1150 sink_time = sq->sinktime;
1152 if (sq->src_tainted) {
1153 GstSegment *segment;
1156 if (sq->has_src_segment) {
1157 segment = &sq->src_segment;
1158 position = sq->src_segment.position;
1161 * If the src pad had no segment yet, use the sink segment
1162 * to avoid signalling overrun if the received sink segment has a
1163 * a position > max-size-time while the src pad time would be the default=0
1165 * This can happen when switching pads on chained/adaptive streams and the
1166 * new chain has a segment with a much larger position
1168 segment = &sq->sink_segment;
1169 position = sq->sink_segment.position;
1172 src_time = sq->srctime = my_segment_to_running_time (segment, position);
1173 /* if we have a time, we become untainted and use the time */
1174 if (G_UNLIKELY (src_time != GST_CLOCK_STIME_NONE)) {
1175 sq->src_tainted = FALSE;
1178 src_time = sq->srctime;
1180 GST_DEBUG_OBJECT (mq,
1181 "queue %d, sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT, sq->id,
1182 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
1184 /* This allows for streams with out of order timestamping - sometimes the
1185 * emerging timestamp is later than the arriving one(s) */
1186 if (G_LIKELY (GST_CLOCK_STIME_IS_VALID (sink_time) &&
1187 GST_CLOCK_STIME_IS_VALID (src_time) && sink_time > src_time))
1188 sq->cur_time = sink_time - src_time;
1192 /* updating the time level can change the buffering state */
1193 update_buffering (mq, sq);
1198 /* take a SEGMENT event and apply the values to segment, updating the time
1199 * level of queue. */
1201 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1202 GstSegment * segment)
1204 gst_event_copy_segment (event, segment);
1206 /* now configure the values, we use these to track timestamps on the
1208 if (segment->format != GST_FORMAT_TIME) {
1209 /* non-time format, pretent the current time segment is closed with a
1210 * 0 start and unknown stop time. */
1211 segment->format = GST_FORMAT_TIME;
1216 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1218 if (segment == &sq->sink_segment)
1219 sq->sink_tainted = TRUE;
1221 sq->has_src_segment = TRUE;
1222 sq->src_tainted = TRUE;
1225 GST_DEBUG_OBJECT (mq,
1226 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1228 /* segment can update the time level of the queue */
1229 update_time_level (mq, sq);
1231 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1232 gst_multi_queue_post_buffering (mq);
1235 /* take a buffer and update segment, updating the time level of the queue. */
1237 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1238 GstClockTime duration, GstSegment * segment)
1240 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1242 /* if no timestamp is set, assume it's continuous with the previous
1244 if (timestamp == GST_CLOCK_TIME_NONE)
1245 timestamp = segment->position;
1248 if (duration != GST_CLOCK_TIME_NONE)
1249 timestamp += duration;
1251 GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1252 sq->id, segment == &sq->sink_segment ? "sink" : "src",
1253 GST_TIME_ARGS (timestamp));
1255 segment->position = timestamp;
1257 if (segment == &sq->sink_segment)
1258 sq->sink_tainted = TRUE;
1260 sq->src_tainted = TRUE;
1262 /* calc diff with other end */
1263 update_time_level (mq, sq);
1264 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1265 gst_multi_queue_post_buffering (mq);
1269 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1270 GstSegment * segment)
1272 GstClockTime timestamp;
1273 GstClockTime duration;
1275 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1277 gst_event_parse_gap (event, ×tamp, &duration);
1279 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1281 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1282 timestamp += duration;
1285 segment->position = timestamp;
1287 if (segment == &sq->sink_segment)
1288 sq->sink_tainted = TRUE;
1290 sq->src_tainted = TRUE;
1292 /* calc diff with other end */
1293 update_time_level (mq, sq);
1296 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1297 gst_multi_queue_post_buffering (mq);
1300 static GstClockTimeDiff
1301 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1303 GstClockTimeDiff time = GST_CLOCK_STIME_NONE;
1305 if (GST_IS_BUFFER (object)) {
1306 GstBuffer *buf = GST_BUFFER_CAST (object);
1307 GstClockTime btime = GST_BUFFER_DTS_OR_PTS (buf);
1309 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1310 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1311 btime += GST_BUFFER_DURATION (buf);
1312 if (btime > segment->stop)
1313 btime = segment->stop;
1314 time = my_segment_to_running_time (segment, btime);
1316 } else if (GST_IS_BUFFER_LIST (object)) {
1317 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1321 n = gst_buffer_list_length (list);
1322 for (i = 0; i < n; i++) {
1324 buf = gst_buffer_list_get (list, i);
1325 btime = GST_BUFFER_DTS_OR_PTS (buf);
1326 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1327 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1328 btime += GST_BUFFER_DURATION (buf);
1329 if (btime > segment->stop)
1330 btime = segment->stop;
1331 time = my_segment_to_running_time (segment, btime);
1338 } else if (GST_IS_EVENT (object)) {
1339 GstEvent *event = GST_EVENT_CAST (object);
1341 /* For newsegment events return the running time of the start position */
1342 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1343 const GstSegment *new_segment;
1345 gst_event_parse_segment (event, &new_segment);
1346 if (new_segment->format == GST_FORMAT_TIME) {
1348 my_segment_to_running_time ((GstSegment *) new_segment,
1349 new_segment->start);
1358 static GstFlowReturn
1359 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1360 GstMiniObject * object, gboolean * allow_drop)
1362 GstFlowReturn result = sq->srcresult;
1364 if (GST_IS_BUFFER (object)) {
1366 GstClockTime timestamp, duration;
1368 buffer = GST_BUFFER_CAST (object);
1369 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1370 duration = GST_BUFFER_DURATION (buffer);
1372 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1374 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1375 gst_data_queue_limits_changed (sq->queue);
1377 if (G_UNLIKELY (*allow_drop)) {
1378 GST_DEBUG_OBJECT (mq,
1379 "SingleQueue %d : Dropping EOS buffer %p with ts %" GST_TIME_FORMAT,
1380 sq->id, buffer, GST_TIME_ARGS (timestamp));
1381 gst_buffer_unref (buffer);
1383 GST_DEBUG_OBJECT (mq,
1384 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1385 sq->id, buffer, GST_TIME_ARGS (timestamp));
1386 result = gst_pad_push (sq->srcpad, buffer);
1388 } else if (GST_IS_EVENT (object)) {
1391 event = GST_EVENT_CAST (object);
1393 switch (GST_EVENT_TYPE (event)) {
1395 result = GST_FLOW_EOS;
1396 if (G_UNLIKELY (*allow_drop))
1397 *allow_drop = FALSE;
1399 case GST_EVENT_SEGMENT:
1400 apply_segment (mq, sq, event, &sq->src_segment);
1401 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1402 gst_data_queue_limits_changed (sq->queue);
1403 if (G_UNLIKELY (*allow_drop)) {
1404 result = GST_FLOW_OK;
1405 *allow_drop = FALSE;
1409 apply_gap (mq, sq, event, &sq->src_segment);
1410 /* Applying the gap may have made the queue non-full again, unblock it if needed */
1411 gst_data_queue_limits_changed (sq->queue);
1417 if (G_UNLIKELY (*allow_drop)) {
1418 GST_DEBUG_OBJECT (mq,
1419 "SingleQueue %d : Dropping EOS event %p of type %s",
1420 sq->id, event, GST_EVENT_TYPE_NAME (event));
1421 gst_event_unref (event);
1423 GST_DEBUG_OBJECT (mq,
1424 "SingleQueue %d : Pushing event %p of type %s",
1425 sq->id, event, GST_EVENT_TYPE_NAME (event));
1427 gst_pad_push_event (sq->srcpad, event);
1429 } else if (GST_IS_QUERY (object)) {
1433 query = GST_QUERY_CAST (object);
1435 if (G_UNLIKELY (*allow_drop)) {
1436 GST_DEBUG_OBJECT (mq,
1437 "SingleQueue %d : Dropping EOS query %p", sq->id, query);
1438 gst_query_unref (query);
1441 res = gst_pad_peer_query (sq->srcpad, query);
1444 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1445 sq->last_query = res;
1446 sq->last_handled_query = query;
1447 g_cond_signal (&sq->query_handled);
1448 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1450 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1458 static GstMiniObject *
1459 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1464 item->object = NULL;
1470 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1472 if (!item->is_query && item->object)
1473 gst_mini_object_unref (item->object);
1474 g_slice_free (GstMultiQueueItem, item);
1477 /* takes ownership of passed mini object! */
1478 static GstMultiQueueItem *
1479 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1481 GstMultiQueueItem *item;
1483 item = g_slice_new (GstMultiQueueItem);
1484 item->object = object;
1485 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1486 item->posid = curid;
1487 item->is_query = GST_IS_QUERY (object);
1489 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1490 item->duration = GST_BUFFER_DURATION (object);
1491 if (item->duration == GST_CLOCK_TIME_NONE)
1493 item->visible = TRUE;
1497 static GstMultiQueueItem *
1498 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1500 GstMultiQueueItem *item;
1502 item = g_slice_new (GstMultiQueueItem);
1503 item->object = object;
1504 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1505 item->posid = curid;
1506 item->is_query = GST_IS_QUERY (object);
1510 item->visible = FALSE;
1514 /* Each main loop attempts to push buffers until the return value
1515 * is not-linked. not-linked pads are not allowed to push data beyond
1516 * any linked pads, so they don't 'rush ahead of the pack'.
1519 gst_multi_queue_loop (GstPad * pad)
1522 GstMultiQueueItem *item;
1523 GstDataQueueItem *sitem;
1525 GstMiniObject *object = NULL;
1527 GstFlowReturn result;
1528 GstClockTimeDiff next_time;
1530 gboolean do_update_buffering = FALSE;
1531 gboolean dropping = FALSE;
1533 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1537 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1542 /* Get something from the queue, blocking until that happens, or we get
1544 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1547 item = (GstMultiQueueItem *) sitem;
1548 newid = item->posid;
1550 /* steal the object and destroy the item */
1551 object = gst_multi_queue_item_steal_object (item);
1552 gst_multi_queue_item_destroy (item);
1554 is_buffer = GST_IS_BUFFER (object);
1556 /* Get running time of the item. Events will have GST_CLOCK_STIME_NONE */
1557 next_time = get_running_time (&sq->src_segment, object, FALSE);
1559 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1560 sq->id, newid, sq->last_oldid);
1562 /* If we're not-linked, we do some extra work because we might need to
1563 * wait before pushing. If we're linked but there's a gap in the IDs,
1564 * or it's the first loop, or we just passed the previous highid,
1565 * we might need to wake some sleeping pad up, so there's extra work
1567 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1568 if (sq->srcresult == GST_FLOW_NOT_LINKED
1569 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1570 || sq->last_oldid > mq->highid) {
1571 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1572 gst_flow_get_name (sq->srcresult));
1574 /* Check again if we're flushing after the lock is taken,
1575 * the flush flag might have been changed in the meantime */
1577 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1581 /* Update the nextid so other threads know when to wake us up */
1583 /* Take into account the extra cache time since we're unlinked */
1584 if (GST_CLOCK_STIME_IS_VALID (next_time))
1585 next_time += mq->unlinked_cache_time;
1586 sq->next_time = next_time;
1588 /* Update the oldid (the last ID we output) for highid tracking */
1589 if (sq->last_oldid != G_MAXUINT32)
1590 sq->oldid = sq->last_oldid;
1592 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1593 /* Go to sleep until it's time to push this buffer */
1595 /* Recompute the highid */
1596 compute_high_id (mq);
1597 /* Recompute the high time */
1598 compute_high_time (mq);
1600 while (((mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (next_time)
1601 && (mq->high_time == GST_CLOCK_STIME_NONE
1602 || next_time > mq->high_time))
1603 || (!mq->sync_by_running_time && newid > mq->highid))
1604 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1606 GST_DEBUG_OBJECT (mq,
1607 "queue %d sleeping for not-linked wakeup with "
1608 "newid %u, highid %u, next_time %" GST_STIME_FORMAT
1609 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1610 GST_STIME_ARGS (next_time), GST_STIME_ARGS (mq->high_time));
1612 /* Wake up all non-linked pads before we sleep */
1613 wake_up_next_non_linked (mq);
1616 g_cond_wait (&sq->turn, &mq->qlock);
1620 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1624 /* Recompute the high time */
1625 compute_high_time (mq);
1627 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1628 "wakeup with newid %u, highid %u, next_time %" GST_STIME_FORMAT
1629 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1630 GST_STIME_ARGS (next_time), GST_STIME_ARGS (mq->high_time));
1633 /* Re-compute the high_id in case someone else pushed */
1634 compute_high_id (mq);
1635 compute_high_time (mq);
1637 compute_high_id (mq);
1638 compute_high_time (mq);
1639 /* Wake up all non-linked pads */
1640 wake_up_next_non_linked (mq);
1642 /* We're done waiting, we can clear the nextid and nexttime */
1644 sq->next_time = GST_CLOCK_STIME_NONE;
1646 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1651 GST_LOG_OBJECT (mq, "sq:%d BEFORE PUSHING sq->srcresult: %s", sq->id,
1652 gst_flow_get_name (sq->srcresult));
1654 /* Update time stats */
1655 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1656 next_time = get_running_time (&sq->src_segment, object, TRUE);
1657 if (GST_CLOCK_STIME_IS_VALID (next_time)) {
1658 if (sq->last_time == GST_CLOCK_STIME_NONE || sq->last_time < next_time)
1659 sq->last_time = next_time;
1660 if (mq->high_time == GST_CLOCK_STIME_NONE || mq->high_time <= next_time) {
1661 /* Wake up all non-linked pads now that we advanced the high time */
1662 mq->high_time = next_time;
1663 wake_up_next_non_linked (mq);
1666 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1668 /* Try to push out the new object */
1669 result = gst_single_queue_push_one (mq, sq, object, &dropping);
1672 /* Check if we pushed something already and if this is
1673 * now a switch from an active to a non-active stream.
1675 * If it is, we reset all the waiting streams, let them
1676 * push another buffer to see if they're now active again.
1677 * This allows faster switching between streams and prevents
1678 * deadlocks if downstream does any waiting too.
1680 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1681 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1682 && result == GST_FLOW_NOT_LINKED) {
1685 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1688 compute_high_id (mq);
1689 do_update_buffering = TRUE;
1691 /* maybe no-one is waiting */
1692 if (mq->numwaiting > 0) {
1693 /* Else figure out which singlequeue(s) need waking up */
1694 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1695 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1697 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1698 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1699 sq2->pushed = FALSE;
1700 sq2->srcresult = GST_FLOW_OK;
1701 g_cond_signal (&sq2->turn);
1710 /* now hold on a bit;
1711 * can not simply throw this result to upstream, because
1712 * that might already be onto another segment, so we have to make
1713 * sure we are relaying the correct info wrt proper segment */
1714 if (result == GST_FLOW_EOS && !dropping &&
1715 sq->srcresult != GST_FLOW_NOT_LINKED) {
1716 GST_DEBUG_OBJECT (mq, "starting EOS drop on sq %d", sq->id);
1718 /* pretend we have not seen EOS yet for upstream's sake */
1719 result = sq->srcresult;
1720 } else if (dropping && gst_data_queue_is_empty (sq->queue)) {
1721 /* queue empty, so stop dropping
1722 * we can commit the result we have now,
1723 * which is either OK after a segment, or EOS */
1724 GST_DEBUG_OBJECT (mq, "committed EOS drop on sq %d", sq->id);
1726 result = GST_FLOW_EOS;
1728 sq->srcresult = result;
1729 sq->last_oldid = newid;
1731 if (do_update_buffering)
1732 update_buffering (mq, sq);
1734 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1735 gst_multi_queue_post_buffering (mq);
1740 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1741 && result != GST_FLOW_EOS)
1744 GST_LOG_OBJECT (mq, "sq:%d AFTER PUSHING sq->srcresult: %s", sq->id,
1745 gst_flow_get_name (sq->srcresult));
1747 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1748 if (mq->numwaiting > 0 && sq->srcresult == GST_FLOW_EOS) {
1749 compute_high_time (mq);
1750 compute_high_id (mq);
1751 wake_up_next_non_linked (mq);
1753 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1760 gst_mini_object_unref (object);
1762 /* Need to make sure wake up any sleeping pads when we exit */
1763 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1764 compute_high_time (mq);
1765 compute_high_id (mq);
1766 wake_up_next_non_linked (mq);
1767 sq->last_query = FALSE;
1768 g_cond_signal (&sq->query_handled);
1770 /* Post an error message if we got EOS while downstream
1771 * has returned an error flow return. After EOS there
1772 * will be no further buffer which could propagate the
1774 if (sq->is_eos && sq->srcresult < GST_FLOW_EOS) {
1775 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1776 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
1777 ("Internal data stream error."),
1778 ("streaming stopped, reason %s", gst_flow_get_name (sq->srcresult)));
1780 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1783 /* upstream needs to see fatal result ASAP to shut things down,
1784 * but might be stuck in one of our other full queues;
1785 * so empty this one and trigger dynamic queue growth. At
1786 * this point the srcresult is not OK, NOT_LINKED
1787 * or EOS, i.e. a real failure */
1788 gst_single_queue_flush_queue (sq, FALSE);
1789 single_queue_underrun_cb (sq->queue, sq);
1790 gst_data_queue_set_flushing (sq->queue, TRUE);
1791 gst_pad_pause_task (sq->srcpad);
1792 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1793 "SingleQueue[%d] task paused, reason:%s",
1794 sq->id, gst_flow_get_name (sq->srcresult));
1800 * gst_multi_queue_chain:
1802 * This is similar to GstQueue's chain function, except:
1803 * _ we don't have leak behaviours,
1804 * _ we push with a unique id (curid)
1806 static GstFlowReturn
1807 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1811 GstMultiQueueItem *item;
1813 GstClockTime timestamp, duration;
1815 sq = gst_pad_get_element_private (pad);
1818 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1824 /* Get a unique incrementing id */
1825 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1827 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1828 duration = GST_BUFFER_DURATION (buffer);
1831 "SingleQueue %d : about to enqueue buffer %p with id %d (pts:%"
1832 GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT " dur:%" GST_TIME_FORMAT ")",
1833 sq->id, buffer, curid, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1834 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (duration));
1836 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1838 /* Update interleave before pushing data into queue */
1839 if (mq->use_interleave) {
1840 GstClockTime val = timestamp;
1841 GstClockTimeDiff dval;
1843 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1844 if (val == GST_CLOCK_TIME_NONE)
1845 val = sq->sink_segment.position;
1846 if (duration != GST_CLOCK_TIME_NONE)
1849 dval = my_segment_to_running_time (&sq->sink_segment, val);
1850 if (GST_CLOCK_STIME_IS_VALID (dval)) {
1851 sq->cached_sinktime = dval;
1852 GST_DEBUG_OBJECT (mq,
1853 "Queue %d cached sink time now %" G_GINT64_FORMAT " %"
1854 GST_STIME_FORMAT, sq->id, sq->cached_sinktime,
1855 GST_STIME_ARGS (sq->cached_sinktime));
1856 calculate_interleave (mq);
1858 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1861 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1864 /* update time level, we must do this after pushing the data in the queue so
1865 * that we never end up filling the queue first. */
1866 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1869 return sq->srcresult;
1874 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1875 sq->id, gst_flow_get_name (sq->srcresult));
1876 gst_multi_queue_item_destroy (item);
1881 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
1882 gst_buffer_unref (buffer);
1883 return GST_FLOW_EOS;
1888 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
1889 GstPadMode mode, gboolean active)
1895 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1896 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1898 /* mq is NULL if the pad is activated/deactivated before being
1899 * added to the multiqueue */
1901 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1904 case GST_PAD_MODE_PUSH:
1906 /* All pads start off linked until they push one buffer */
1907 sq->srcresult = GST_FLOW_OK;
1909 gst_data_queue_set_flushing (sq->queue, FALSE);
1911 sq->srcresult = GST_FLOW_FLUSHING;
1912 sq->last_query = FALSE;
1913 g_cond_signal (&sq->query_handled);
1914 gst_data_queue_set_flushing (sq->queue, TRUE);
1916 /* Wait until streaming thread has finished */
1918 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1919 GST_PAD_STREAM_LOCK (pad);
1921 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1922 gst_data_queue_flush (sq->queue);
1924 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1925 GST_PAD_STREAM_UNLOCK (pad);
1927 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1937 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1938 gst_object_unref (mq);
1945 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1950 GstMultiQueueItem *item;
1953 GstEvent *sref = NULL;
1955 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1956 mq = (GstMultiQueue *) parent;
1958 type = GST_EVENT_TYPE (event);
1961 case GST_EVENT_STREAM_START:
1963 if (mq->sync_by_running_time) {
1964 GstStreamFlags stream_flags;
1965 gst_event_parse_stream_flags (event, &stream_flags);
1966 if ((stream_flags & GST_STREAM_FLAG_SPARSE)) {
1967 GST_INFO_OBJECT (mq, "SingleQueue %d is a sparse stream", sq->id);
1968 sq->is_sparse = TRUE;
1970 sq->thread = g_thread_self ();
1973 sq->thread = g_thread_self ();
1975 /* Remove EOS flag */
1979 case GST_EVENT_FLUSH_START:
1980 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1983 res = gst_pad_push_event (sq->srcpad, event);
1985 gst_single_queue_flush (mq, sq, TRUE, FALSE);
1988 case GST_EVENT_FLUSH_STOP:
1989 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1992 res = gst_pad_push_event (sq->srcpad, event);
1994 gst_single_queue_flush (mq, sq, FALSE, FALSE);
1997 case GST_EVENT_SEGMENT:
1998 sref = gst_event_ref (event);
2001 /* take ref because the queue will take ownership and we need the event
2002 * afterwards to update the segment */
2003 sref = gst_event_ref (event);
2004 if (mq->use_interleave) {
2005 GstClockTime val, dur;
2007 gst_event_parse_gap (event, &val, &dur);
2008 if (GST_CLOCK_TIME_IS_VALID (val)) {
2009 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2010 if (GST_CLOCK_TIME_IS_VALID (dur))
2012 stime = my_segment_to_running_time (&sq->sink_segment, val);
2013 if (GST_CLOCK_STIME_IS_VALID (stime)) {
2014 sq->cached_sinktime = stime;
2015 calculate_interleave (mq);
2017 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2023 if (!(GST_EVENT_IS_SERIALIZED (event))) {
2024 res = gst_pad_push_event (sq->srcpad, event);
2030 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2034 /* Get an unique incrementing id. */
2035 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2037 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
2039 GST_DEBUG_OBJECT (mq,
2040 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
2041 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
2043 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
2046 /* mark EOS when we received one, we must do that after putting the
2047 * buffer in the queue because EOS marks the buffer as filled. */
2050 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2053 /* Post an error message if we got EOS while downstream
2054 * has returned an error flow return. After EOS there
2055 * will be no further buffer which could propagate the
2057 if (sq->srcresult < GST_FLOW_EOS) {
2058 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2059 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
2060 ("Internal data stream error."),
2061 ("streaming stopped, reason %s",
2062 gst_flow_get_name (sq->srcresult)));
2064 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2067 /* EOS affects the buffering state */
2068 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2069 update_buffering (mq, sq);
2070 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2071 single_queue_overrun_cb (sq->queue, sq);
2072 gst_multi_queue_post_buffering (mq);
2074 case GST_EVENT_SEGMENT:
2075 apply_segment (mq, sq, sref, &sq->sink_segment);
2076 gst_event_unref (sref);
2077 /* a new segment allows us to accept more buffers if we got EOS
2078 * from downstream */
2079 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2080 if (sq->srcresult == GST_FLOW_EOS)
2081 sq->srcresult = GST_FLOW_OK;
2082 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2086 apply_gap (mq, sq, sref, &sq->sink_segment);
2087 gst_event_unref (sref);
2096 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2097 sq->id, gst_flow_get_name (sq->srcresult));
2099 gst_event_unref (sref);
2100 gst_multi_queue_item_destroy (item);
2105 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
2106 gst_event_unref (event);
2113 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
2119 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2120 mq = (GstMultiQueue *) parent;
2122 switch (GST_QUERY_TYPE (query)) {
2124 if (GST_QUERY_IS_SERIALIZED (query)) {
2126 GstMultiQueueItem *item;
2128 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2129 if (sq->srcresult != GST_FLOW_OK)
2132 /* serialized events go in the queue. We need to be certain that we
2133 * don't cause deadlocks waiting for the query return value. We check if
2134 * the queue is empty (nothing is blocking downstream and the query can
2135 * be pushed for sure) or we are not buffering. If we are buffering,
2136 * the pipeline waits to unblock downstream until our queue fills up
2137 * completely, which can not happen if we block on the query..
2138 * Therefore we only potentially block when we are not buffering. */
2139 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
2140 /* Get an unique incrementing id. */
2141 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2143 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
2145 GST_DEBUG_OBJECT (mq,
2146 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
2147 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
2148 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2149 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
2150 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2151 /* it might be that the query has been taken out of the queue
2152 * while we were unlocked. So, we need to check if the last
2153 * handled query is the same one than the one we just
2154 * pushed. If it is, we don't need to wait for the condition
2155 * variable, otherwise we wait for the condition variable to
2157 if (sq->last_handled_query != query)
2158 g_cond_wait (&sq->query_handled, &mq->qlock);
2159 res = sq->last_query;
2160 sq->last_handled_query = NULL;
2162 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
2163 "queue is not empty");
2166 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2168 /* default handling */
2169 res = gst_pad_query_default (pad, parent, query);
2177 GST_DEBUG_OBJECT (mq, "Flushing");
2178 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2184 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2185 GstPadMode mode, gboolean active)
2191 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2194 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2197 case GST_PAD_MODE_PUSH:
2199 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
2201 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
2202 /* make sure streaming finishes */
2203 result |= gst_pad_stop_task (pad);
2214 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2216 GstSingleQueue *sq = gst_pad_get_element_private (pad);
2217 GstMultiQueue *mq = sq->mqueue;
2220 switch (GST_EVENT_TYPE (event)) {
2221 case GST_EVENT_RECONFIGURE:
2222 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2223 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2224 sq->srcresult = GST_FLOW_OK;
2225 g_cond_signal (&sq->turn);
2227 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2229 ret = gst_pad_push_event (sq->sinkpad, event);
2232 ret = gst_pad_push_event (sq->sinkpad, event);
2240 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2244 /* FIXME, Handle position offset depending on queue size */
2245 switch (GST_QUERY_TYPE (query)) {
2247 /* default handling */
2248 res = gst_pad_query_default (pad, parent, query);
2255 * Next-non-linked functions
2258 /* WITH LOCK TAKEN */
2260 wake_up_next_non_linked (GstMultiQueue * mq)
2264 /* maybe no-one is waiting */
2265 if (mq->numwaiting < 1)
2268 if (mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (mq->high_time)) {
2269 /* Else figure out which singlequeue(s) need waking up */
2270 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2271 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2272 if (sq->srcresult == GST_FLOW_NOT_LINKED
2273 && GST_CLOCK_STIME_IS_VALID (sq->next_time)
2274 && sq->next_time <= mq->high_time) {
2275 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2276 g_cond_signal (&sq->turn);
2280 /* Else figure out which singlequeue(s) need waking up */
2281 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2282 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2283 if (sq->srcresult == GST_FLOW_NOT_LINKED &&
2284 sq->nextid != 0 && sq->nextid <= mq->highid) {
2285 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2286 g_cond_signal (&sq->turn);
2292 /* WITH LOCK TAKEN */
2294 compute_high_id (GstMultiQueue * mq)
2296 /* The high-id is either the highest id among the linked pads, or if all
2297 * pads are not-linked, it's the lowest not-linked pad */
2299 guint32 lowest = G_MAXUINT32;
2300 guint32 highid = G_MAXUINT32;
2302 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2303 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2305 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2306 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2308 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2309 /* No need to consider queues which are not waiting */
2310 if (sq->nextid == 0) {
2311 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2315 if (sq->nextid < lowest)
2316 lowest = sq->nextid;
2317 } else if (sq->srcresult != GST_FLOW_EOS) {
2318 /* If we don't have a global highid, or the global highid is lower than
2319 * this single queue's last outputted id, store the queue's one,
2320 * unless the singlequeue is at EOS (srcresult = EOS) */
2321 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2326 if (highid == G_MAXUINT32 || lowest < highid)
2327 mq->highid = lowest;
2329 mq->highid = highid;
2331 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2335 /* WITH LOCK TAKEN */
2337 compute_high_time (GstMultiQueue * mq)
2339 /* The high-time is either the highest last time among the linked
2340 * pads, or if all pads are not-linked, it's the lowest nex time of
2343 GstClockTimeDiff highest = GST_CLOCK_STIME_NONE;
2344 GstClockTimeDiff lowest = GST_CLOCK_STIME_NONE;
2346 if (!mq->sync_by_running_time)
2349 for (tmp = mq->queues; tmp; tmp = tmp->next) {
2350 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2353 "inspecting sq:%d , next_time:%" GST_STIME_FORMAT ", last_time:%"
2354 GST_STIME_FORMAT ", srcresult:%s", sq->id,
2355 GST_STIME_ARGS (sq->next_time), GST_STIME_ARGS (sq->last_time),
2356 gst_flow_get_name (sq->srcresult));
2358 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2359 /* No need to consider queues which are not waiting */
2360 if (!GST_CLOCK_STIME_IS_VALID (sq->next_time)) {
2361 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2365 if (lowest == GST_CLOCK_STIME_NONE || sq->next_time < lowest)
2366 lowest = sq->next_time;
2367 } else if (sq->srcresult != GST_FLOW_EOS) {
2368 /* If we don't have a global high time, or the global high time
2369 * is lower than this single queue's last outputted time, store
2370 * the queue's one, unless the singlequeue is at EOS (srcresult
2372 if (highest == GST_CLOCK_STIME_NONE
2373 || (sq->last_time != GST_CLOCK_STIME_NONE && sq->last_time > highest))
2374 highest = sq->last_time;
2377 "highest now %" GST_STIME_FORMAT " lowest %" GST_STIME_FORMAT,
2378 GST_STIME_ARGS (highest), GST_STIME_ARGS (lowest));
2381 if (highest == GST_CLOCK_STIME_NONE)
2382 mq->high_time = lowest;
2384 mq->high_time = highest;
2387 "High time is now : %" GST_STIME_FORMAT ", lowest non-linked %"
2388 GST_STIME_FORMAT, GST_STIME_ARGS (mq->high_time),
2389 GST_STIME_ARGS (lowest));
2392 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2393 ((q)->max_size.format) <= (value))
2396 * GstSingleQueue functions
2399 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2401 GstMultiQueue *mq = sq->mqueue;
2403 GstDataQueueSize size;
2404 gboolean filled = TRUE;
2405 gboolean empty_found = FALSE;
2407 gst_data_queue_get_level (sq->queue, &size);
2410 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2411 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2412 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2415 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2417 /* check if we reached the hard time/bytes limits */
2418 if (sq->is_eos || sq->is_sparse || IS_FILLED (sq, bytes, size.bytes) ||
2419 IS_FILLED (sq, time, sq->cur_time)) {
2423 /* Search for empty queues */
2424 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2425 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2430 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2431 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2435 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2436 if (gst_data_queue_is_empty (oq->queue) && !oq->is_sparse) {
2437 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2443 /* if hard limits are not reached then we allow one more buffer in the full
2444 * queue, but only if any of the other singelqueues are empty */
2446 if (IS_FILLED (sq, visible, size.visible)) {
2447 sq->max_size.visible = size.visible + 1;
2448 GST_DEBUG_OBJECT (mq,
2449 "Bumping single queue %d max visible to %d",
2450 sq->id, sq->max_size.visible);
2456 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2458 /* Overrun is always forwarded, since this is blocking the upstream element */
2460 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2461 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2466 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2468 gboolean empty = TRUE;
2469 GstMultiQueue *mq = sq->mqueue;
2472 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2473 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2477 "Single Queue %d is empty, Checking other single queues", sq->id);
2480 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2481 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2482 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2484 if (gst_data_queue_is_full (oq->queue)) {
2485 GstDataQueueSize size;
2487 gst_data_queue_get_level (oq->queue, &size);
2488 if (IS_FILLED (oq, visible, size.visible)) {
2489 oq->max_size.visible = size.visible + 1;
2490 GST_DEBUG_OBJECT (mq,
2491 "queue %d is filled, bumping its max visible to %d", oq->id,
2492 oq->max_size.visible);
2493 gst_data_queue_limits_changed (oq->queue);
2496 if (!gst_data_queue_is_empty (oq->queue) || oq->is_sparse)
2499 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2502 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2503 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2508 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2509 guint64 time, GstSingleQueue * sq)
2512 GstMultiQueue *mq = sq->mqueue;
2514 GST_DEBUG_OBJECT (mq,
2515 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2516 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2517 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2519 /* we are always filled on EOS */
2523 /* we never go past the max visible items unless we are in buffering mode */
2524 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2527 /* check time or bytes */
2528 res = IS_FILLED (sq, bytes, bytes);
2529 /* We only care about limits in time if we're not a sparse stream or
2530 * we're not syncing by running time */
2531 if (!sq->is_sparse || !mq->sync_by_running_time) {
2532 /* If unlinked, take into account the extra unlinked cache time */
2533 if (mq->sync_by_running_time && sq->srcresult == GST_FLOW_NOT_LINKED) {
2534 if (sq->cur_time > mq->unlinked_cache_time)
2535 res |= IS_FILLED (sq, time, sq->cur_time - mq->unlinked_cache_time);
2539 res |= IS_FILLED (sq, time, sq->cur_time);
2546 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2548 GstDataQueueItem *sitem;
2549 GstMultiQueueItem *mitem;
2550 gboolean was_flushing = FALSE;
2552 while (!gst_data_queue_is_empty (sq->queue)) {
2553 GstMiniObject *data;
2555 /* FIXME: If this fails here although the queue is not empty,
2556 * we're flushing... but we want to rescue all sticky
2557 * events nonetheless.
2559 if (!gst_data_queue_pop (sq->queue, &sitem)) {
2560 was_flushing = TRUE;
2561 gst_data_queue_set_flushing (sq->queue, FALSE);
2565 mitem = (GstMultiQueueItem *) sitem;
2567 data = sitem->object;
2569 if (!full && !mitem->is_query && GST_IS_EVENT (data)
2570 && GST_EVENT_IS_STICKY (data)
2571 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2572 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2573 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2576 sitem->destroy (sitem);
2579 gst_data_queue_flush (sq->queue);
2581 gst_data_queue_set_flushing (sq->queue, TRUE);
2583 GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2584 update_buffering (sq->mqueue, sq);
2585 GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2586 gst_multi_queue_post_buffering (sq->mqueue);
2590 gst_single_queue_free (GstSingleQueue * sq)
2593 gst_data_queue_flush (sq->queue);
2594 g_object_unref (sq->queue);
2595 g_cond_clear (&sq->turn);
2596 g_cond_clear (&sq->query_handled);
2600 static GstSingleQueue *
2601 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2606 guint temp_id = (id == -1) ? 0 : id;
2608 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2610 /* Find an unused queue ID, if possible the passed one */
2611 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2612 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2613 /* This works because the IDs are sorted in ascending order */
2614 if (sq2->id == temp_id) {
2615 /* If this ID was requested by the caller return NULL,
2616 * otherwise just get us the next one */
2618 temp_id = sq2->id + 1;
2621 } else if (sq2->id > temp_id) {
2626 sq = g_new0 (GstSingleQueue, 1);
2630 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2631 mqueue->queues_cookie++;
2633 /* copy over max_size and extra_size so we don't need to take the lock
2634 * any longer when checking if the queue is full. */
2635 sq->max_size.visible = mqueue->max_size.visible;
2636 sq->max_size.bytes = mqueue->max_size.bytes;
2637 sq->max_size.time = mqueue->max_size.time;
2639 sq->extra_size.visible = mqueue->extra_size.visible;
2640 sq->extra_size.bytes = mqueue->extra_size.bytes;
2641 sq->extra_size.time = mqueue->extra_size.time;
2643 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2645 sq->mqueue = mqueue;
2646 sq->srcresult = GST_FLOW_FLUSHING;
2648 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2649 single_queue_check_full,
2650 (GstDataQueueFullCallback) single_queue_overrun_cb,
2651 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2653 sq->is_sparse = FALSE;
2654 sq->flushing = FALSE;
2656 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2657 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2661 sq->next_time = GST_CLOCK_STIME_NONE;
2662 sq->last_time = GST_CLOCK_STIME_NONE;
2663 g_cond_init (&sq->turn);
2664 g_cond_init (&sq->query_handled);
2666 sq->sinktime = GST_CLOCK_STIME_NONE;
2667 sq->srctime = GST_CLOCK_STIME_NONE;
2668 sq->sink_tainted = TRUE;
2669 sq->src_tainted = TRUE;
2671 name = g_strdup_printf ("sink_%u", sq->id);
2672 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
2675 gst_pad_set_chain_function (sq->sinkpad,
2676 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2677 gst_pad_set_activatemode_function (sq->sinkpad,
2678 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2679 gst_pad_set_event_function (sq->sinkpad,
2680 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2681 gst_pad_set_query_function (sq->sinkpad,
2682 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2683 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2684 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2685 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2687 name = g_strdup_printf ("src_%u", sq->id);
2688 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2691 gst_pad_set_activatemode_function (sq->srcpad,
2692 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2693 gst_pad_set_event_function (sq->srcpad,
2694 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2695 gst_pad_set_query_function (sq->srcpad,
2696 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2697 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2698 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2699 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2701 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2702 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2704 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2706 /* only activate the pads when we are not in the NULL state
2707 * and add the pad under the state_lock to prevend state changes
2708 * between activating and adding */
2709 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2710 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2711 gst_pad_set_active (sq->srcpad, TRUE);
2712 gst_pad_set_active (sq->sinkpad, TRUE);
2714 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2715 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2716 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2718 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",