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 GstClockTime sinktime, srctime;
153 /* TRUE if either position needs to be recalculated */
154 gboolean sink_tainted, src_tainted;
158 GstDataQueueSize max_size, extra_size;
159 GstClockTime cur_time;
163 /* Protected by global lock */
164 guint32 nextid; /* ID of the next object waiting to be pushed */
165 guint32 oldid; /* ID of the last object pushed (last in a series) */
166 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
167 GstClockTime next_time; /* End running time of next buffer to be pushed */
168 GstClockTime last_time; /* Start running time of last pushed buffer */
169 GCond turn; /* SingleQueue turn waiting conditional */
171 /* for serialized queries */
174 GstQuery *last_handled_query;
178 /* Extension of GstDataQueueItem structure for our usage */
179 typedef struct _GstMultiQueueItem GstMultiQueueItem;
181 struct _GstMultiQueueItem
183 GstMiniObject *object;
188 GDestroyNotify destroy;
194 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
195 static void gst_single_queue_free (GstSingleQueue * squeue);
197 static void wake_up_next_non_linked (GstMultiQueue * mq);
198 static void compute_high_id (GstMultiQueue * mq);
199 static void compute_high_time (GstMultiQueue * mq);
200 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
201 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
203 static void update_buffering (GstMultiQueue * mq, GstSingleQueue * sq);
204 static void gst_multi_queue_post_buffering (GstMultiQueue * mq);
206 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
208 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
211 GST_STATIC_CAPS_ANY);
213 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
216 GST_STATIC_CAPS_ANY);
218 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
219 #define GST_CAT_DEFAULT (multi_queue_debug)
221 /* Signals and args */
229 /* default limits, we try to keep up to 2 seconds of data and if there is not
230 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
231 * there is data in the queues. Normally, the byte and time limits are not hit
232 * in theses conditions. */
233 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
234 #define DEFAULT_MAX_SIZE_BUFFERS 5
235 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
237 /* second limits. When we hit one of the above limits we are probably dealing
238 * with a badly muxed file and we scale the limits to these emergency values.
239 * This is currently not yet implemented.
240 * Since we dynamically scale the queue buffer size up to the limits but avoid
241 * going above the max-size-buffers when we can, we don't really need this
242 * aditional extra size. */
243 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
244 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
245 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
247 #define DEFAULT_USE_BUFFERING FALSE
248 #define DEFAULT_LOW_PERCENT 10
249 #define DEFAULT_HIGH_PERCENT 99
250 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
255 PROP_EXTRA_SIZE_BYTES,
256 PROP_EXTRA_SIZE_BUFFERS,
257 PROP_EXTRA_SIZE_TIME,
259 PROP_MAX_SIZE_BUFFERS,
261 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
262 PROP_CURR_SIZE_BYTES,
267 PROP_SYNC_BY_RUNNING_TIME,
271 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
272 g_mutex_lock (&q->qlock); \
275 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
276 g_mutex_unlock (&q->qlock); \
279 #define SET_PERCENT(mq, perc) G_STMT_START { \
280 if (perc != mq->percent) { \
281 mq->percent = perc; \
282 mq->percent_changed = TRUE; \
283 GST_DEBUG_OBJECT (mq, "buffering %d percent", perc); \
287 static void gst_multi_queue_finalize (GObject * object);
288 static void gst_multi_queue_set_property (GObject * object,
289 guint prop_id, const GValue * value, GParamSpec * pspec);
290 static void gst_multi_queue_get_property (GObject * object,
291 guint prop_id, GValue * value, GParamSpec * pspec);
293 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
294 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
295 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
296 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
297 element, GstStateChange transition);
299 static void gst_multi_queue_loop (GstPad * pad);
302 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
303 #define gst_multi_queue_parent_class parent_class
304 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
307 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
310 gst_multi_queue_class_init (GstMultiQueueClass * klass)
312 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
313 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
315 gobject_class->set_property = gst_multi_queue_set_property;
316 gobject_class->get_property = gst_multi_queue_get_property;
321 * GstMultiQueue::underrun:
322 * @multiqueue: the multiqueue instance
324 * This signal is emitted from the streaming thread when there is
325 * no data in any of the queues inside the multiqueue instance (underrun).
327 * This indicates either starvation or EOS from the upstream data sources.
329 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
330 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
331 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
332 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
335 * GstMultiQueue::overrun:
336 * @multiqueue: the multiqueue instance
338 * Reports that one of the queues in the multiqueue is full (overrun).
339 * A queue is full if the total amount of data inside it (num-buffers, time,
340 * size) is higher than the boundary values which can be set through the
341 * GObject properties.
343 * This can be used as an indicator of pre-roll.
345 gst_multi_queue_signals[SIGNAL_OVERRUN] =
346 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
347 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
348 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
352 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
353 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
354 "Max. amount of data in the queue (bytes, 0=disable)",
355 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
356 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
357 G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
359 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
360 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
361 DEFAULT_MAX_SIZE_BUFFERS,
362 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
363 G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
365 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
366 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
367 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
368 G_PARAM_STATIC_STRINGS));
369 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
370 g_object_class_install_property (gobject_class, PROP_CURR_SIZE_BYTES,
371 g_param_spec_uint ("curr-size-bytes", "Current buffered size (kB)",
372 "buffered amount of data in the queue (bytes)", 0, G_MAXUINT,
373 0, G_PARAM_READABLE | GST_PARAM_MUTABLE_PLAYING |
374 G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
377 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
378 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
379 " (NOT IMPLEMENTED)",
380 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
382 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
383 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
384 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
385 " (NOT IMPLEMENTED)",
386 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
389 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
390 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
391 " (NOT IMPLEMENTED)",
392 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396 * GstMultiQueue:use-buffering
398 * Enable the buffering option in multiqueue so that BUFFERING messages are
399 * emitted based on low-/high-percent thresholds.
401 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
402 g_param_spec_boolean ("use-buffering", "Use buffering",
403 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
404 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
405 G_PARAM_STATIC_STRINGS));
407 * GstMultiQueue:low-percent
409 * Low threshold percent for buffering to start.
411 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
412 g_param_spec_int ("low-percent", "Low percent",
413 "Low threshold for buffering to start", 0, 100,
414 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416 * GstMultiQueue:high-percent
418 * High threshold percent for buffering to finish.
420 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
421 g_param_spec_int ("high-percent", "High percent",
422 "High threshold for buffering to finish", 0, 100,
423 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 * GstMultiQueue:sync-by-running-time
428 * If enabled multiqueue will synchronize deactivated or not-linked streams
429 * to the activated and linked streams by taking the running time.
430 * Otherwise multiqueue will synchronize the deactivated or not-linked
431 * streams by keeping the order in which buffers and events arrived compared
432 * to active and linked streams.
434 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
435 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
436 "Synchronize deactivated or not-linked streams by running time",
437 DEFAULT_SYNC_BY_RUNNING_TIME,
438 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
440 gobject_class->finalize = gst_multi_queue_finalize;
442 gst_element_class_set_static_metadata (gstelement_class,
444 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
445 gst_element_class_add_pad_template (gstelement_class,
446 gst_static_pad_template_get (&sinktemplate));
447 gst_element_class_add_pad_template (gstelement_class,
448 gst_static_pad_template_get (&srctemplate));
450 gstelement_class->request_new_pad =
451 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
452 gstelement_class->release_pad =
453 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
454 gstelement_class->change_state =
455 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
459 gst_multi_queue_init (GstMultiQueue * mqueue)
461 mqueue->nbqueues = 0;
462 mqueue->queues = NULL;
464 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
465 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
466 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
468 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
469 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
470 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
472 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
473 mqueue->low_percent = DEFAULT_LOW_PERCENT;
474 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
476 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
480 mqueue->high_time = GST_CLOCK_TIME_NONE;
482 g_mutex_init (&mqueue->qlock);
483 g_mutex_init (&mqueue->buffering_post_lock);
487 gst_multi_queue_finalize (GObject * object)
489 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
491 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
492 g_list_free (mqueue->queues);
493 mqueue->queues = NULL;
494 mqueue->queues_cookie++;
496 /* free/unref instance data */
497 g_mutex_clear (&mqueue->qlock);
498 g_mutex_clear (&mqueue->buffering_post_lock);
500 G_OBJECT_CLASS (parent_class)->finalize (object);
503 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
504 GList * tmp = mq->queues; \
506 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
507 q->max_size.format = mq->max_size.format; \
508 update_buffering (mq, q); \
509 gst_data_queue_limits_changed (q->queue); \
510 tmp = g_list_next(tmp); \
515 gst_multi_queue_set_property (GObject * object, guint prop_id,
516 const GValue * value, GParamSpec * pspec)
518 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
521 case PROP_MAX_SIZE_BYTES:
522 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
523 mq->max_size.bytes = g_value_get_uint (value);
524 SET_CHILD_PROPERTY (mq, bytes);
525 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
526 gst_multi_queue_post_buffering (mq);
528 case PROP_MAX_SIZE_BUFFERS:
531 gint new_size = g_value_get_uint (value);
533 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
535 mq->max_size.visible = new_size;
539 GstDataQueueSize size;
540 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
541 gst_data_queue_get_level (q->queue, &size);
543 GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
544 " current: %d, current max %d", q->id, new_size, size.visible,
545 q->max_size.visible);
547 /* do not reduce max size below current level if the single queue
548 * has grown because of empty queue */
550 q->max_size.visible = new_size;
551 } else if (q->max_size.visible == 0) {
552 q->max_size.visible = MAX (new_size, size.visible);
553 } else if (new_size > size.visible) {
554 q->max_size.visible = new_size;
556 update_buffering (mq, q);
557 gst_data_queue_limits_changed (q->queue);
558 tmp = g_list_next (tmp);
561 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
562 gst_multi_queue_post_buffering (mq);
566 case PROP_MAX_SIZE_TIME:
567 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
568 mq->max_size.time = g_value_get_uint64 (value);
569 SET_CHILD_PROPERTY (mq, time);
570 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
571 gst_multi_queue_post_buffering (mq);
573 case PROP_EXTRA_SIZE_BYTES:
574 mq->extra_size.bytes = g_value_get_uint (value);
576 case PROP_EXTRA_SIZE_BUFFERS:
577 mq->extra_size.visible = g_value_get_uint (value);
579 case PROP_EXTRA_SIZE_TIME:
580 mq->extra_size.time = g_value_get_uint64 (value);
582 case PROP_USE_BUFFERING:
583 mq->use_buffering = g_value_get_boolean (value);
584 if (!mq->use_buffering && mq->buffering) {
585 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
586 mq->buffering = FALSE;
587 GST_DEBUG_OBJECT (mq, "buffering 100 percent");
588 SET_PERCENT (mq, 100);
589 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
592 if (mq->use_buffering) {
595 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
599 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
600 update_buffering (mq, q);
601 gst_data_queue_limits_changed (q->queue);
602 tmp = g_list_next (tmp);
605 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
607 gst_multi_queue_post_buffering (mq);
609 case PROP_LOW_PERCENT:
610 mq->low_percent = g_value_get_int (value);
612 case PROP_HIGH_PERCENT:
613 mq->high_percent = g_value_get_int (value);
615 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
617 mq->percent_changed = TRUE;
618 gst_multi_queue_post_buffering (mq);
622 case PROP_SYNC_BY_RUNNING_TIME:
623 mq->sync_by_running_time = g_value_get_boolean (value);
626 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
631 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
633 get_current_size_bytes (GstMultiQueue * mq)
636 GstClockTime highest = GST_CLOCK_TIME_NONE;
637 GstClockTime lowest = GST_CLOCK_TIME_NONE;
638 guint current_size_bytes = 0;
640 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
641 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
642 GstDataQueueSize size;
644 gst_data_queue_get_level (sq->queue, &size);
646 current_size_bytes += size.bytes;
648 GST_DEBUG_OBJECT (mq,
649 "queue %d: bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
650 G_GUINT64_FORMAT, sq->id, size.bytes, sq->max_size.bytes,
651 sq->cur_time, sq->max_size.time);
654 GST_INFO_OBJECT (mq, "current_size_bytes : %u", current_size_bytes);
656 return current_size_bytes;
661 gst_multi_queue_get_property (GObject * object, guint prop_id,
662 GValue * value, GParamSpec * pspec)
664 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
666 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
669 case PROP_EXTRA_SIZE_BYTES:
670 g_value_set_uint (value, mq->extra_size.bytes);
672 case PROP_EXTRA_SIZE_BUFFERS:
673 g_value_set_uint (value, mq->extra_size.visible);
675 case PROP_EXTRA_SIZE_TIME:
676 g_value_set_uint64 (value, mq->extra_size.time);
678 case PROP_MAX_SIZE_BYTES:
679 g_value_set_uint (value, mq->max_size.bytes);
681 case PROP_MAX_SIZE_BUFFERS:
682 g_value_set_uint (value, mq->max_size.visible);
684 case PROP_MAX_SIZE_TIME:
685 g_value_set_uint64 (value, mq->max_size.time);
687 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
688 case PROP_CURR_SIZE_BYTES:
689 g_value_set_uint (value, get_current_size_bytes(mq));
692 case PROP_USE_BUFFERING:
693 g_value_set_boolean (value, mq->use_buffering);
695 case PROP_LOW_PERCENT:
696 g_value_set_int (value, mq->low_percent);
698 case PROP_HIGH_PERCENT:
699 g_value_set_int (value, mq->high_percent);
701 case PROP_SYNC_BY_RUNNING_TIME:
702 g_value_set_boolean (value, mq->sync_by_running_time);
705 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
709 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
713 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
715 GstIterator *it = NULL;
717 GstSingleQueue *squeue;
718 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
721 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
722 squeue = gst_pad_get_element_private (pad);
726 if (squeue->sinkpad == pad)
727 opad = gst_object_ref (squeue->srcpad);
728 else if (squeue->srcpad == pad)
729 opad = gst_object_ref (squeue->sinkpad);
733 g_value_init (&val, GST_TYPE_PAD);
734 g_value_set_object (&val, opad);
735 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
736 g_value_unset (&val);
738 gst_object_unref (opad);
741 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
752 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
753 const gchar * name, const GstCaps * caps)
755 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
756 GstSingleQueue *squeue;
760 sscanf (name + 4, "_%u", &temp_id);
761 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
764 /* Create a new single queue, add the sink and source pad and return the sink pad */
765 squeue = gst_single_queue_new (mqueue, temp_id);
767 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
768 GST_DEBUG_PAD_NAME (squeue->sinkpad));
770 return squeue ? squeue->sinkpad : NULL;
774 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
776 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
777 GstSingleQueue *sq = NULL;
780 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
782 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
783 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
784 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
785 sq = (GstSingleQueue *) tmp->data;
787 if (sq->sinkpad == pad)
792 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
793 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
797 /* FIXME: The removal of the singlequeue should probably not happen until it
798 * finishes draining */
800 /* remove it from the list */
801 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
802 mqueue->queues_cookie++;
804 /* FIXME : recompute next-non-linked */
805 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
807 /* delete SingleQueue */
808 gst_data_queue_set_flushing (sq->queue, TRUE);
810 gst_pad_set_active (sq->srcpad, FALSE);
811 gst_pad_set_active (sq->sinkpad, FALSE);
812 gst_pad_set_element_private (sq->srcpad, NULL);
813 gst_pad_set_element_private (sq->sinkpad, NULL);
814 gst_element_remove_pad (element, sq->srcpad);
815 gst_element_remove_pad (element, sq->sinkpad);
816 gst_single_queue_free (sq);
819 static GstStateChangeReturn
820 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
822 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
823 GstSingleQueue *sq = NULL;
824 GstStateChangeReturn result;
826 switch (transition) {
827 case GST_STATE_CHANGE_READY_TO_PAUSED:{
830 /* Set all pads to non-flushing */
831 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
832 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
833 sq = (GstSingleQueue *) tmp->data;
834 sq->flushing = FALSE;
837 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
838 SET_CHILD_PROPERTY (mqueue, visible);
840 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
841 gst_multi_queue_post_buffering (mqueue);
845 case GST_STATE_CHANGE_PAUSED_TO_READY:{
848 /* Un-wait all waiting pads */
849 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
850 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
851 sq = (GstSingleQueue *) tmp->data;
853 g_cond_signal (&sq->turn);
855 sq->last_query = FALSE;
856 g_cond_signal (&sq->query_handled);
858 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
865 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
867 switch (transition) {
876 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
881 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
885 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
886 sq->srcresult = GST_FLOW_FLUSHING;
887 gst_data_queue_set_flushing (sq->queue, TRUE);
891 /* wake up non-linked task */
892 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
894 g_cond_signal (&sq->turn);
895 sq->last_query = FALSE;
896 g_cond_signal (&sq->query_handled);
897 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
899 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
900 result = gst_pad_pause_task (sq->srcpad);
901 sq->sink_tainted = sq->src_tainted = TRUE;
903 gst_single_queue_flush_queue (sq, full);
905 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
906 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
907 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
908 sq->has_src_segment = FALSE;
909 /* All pads start off not-linked for a smooth kick-off */
910 sq->srcresult = GST_FLOW_OK;
913 sq->max_size.visible = mq->max_size.visible;
917 sq->last_oldid = G_MAXUINT32;
918 sq->next_time = GST_CLOCK_TIME_NONE;
919 sq->last_time = GST_CLOCK_TIME_NONE;
920 gst_data_queue_set_flushing (sq->queue, FALSE);
922 /* Reset high time to be recomputed next */
923 mq->high_time = GST_CLOCK_TIME_NONE;
925 sq->flushing = FALSE;
926 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
928 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
930 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
936 /* WITH LOCK TAKEN */
938 get_percentage (GstSingleQueue * sq)
940 GstDataQueueSize size;
943 gst_data_queue_get_level (sq->queue, &size);
945 GST_DEBUG_OBJECT (sq->mqueue,
946 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
947 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
948 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
950 /* get bytes and time percentages and take the max */
951 if (sq->is_eos || sq->srcresult == GST_FLOW_NOT_LINKED) {
955 if (sq->max_size.time > 0) {
956 tmp = (sq->cur_time * 100) / sq->max_size.time;
957 percent = MAX (percent, tmp);
959 if (sq->max_size.bytes > 0) {
960 tmp = (size.bytes * 100) / sq->max_size.bytes;
961 percent = MAX (percent, tmp);
968 /* WITH LOCK TAKEN */
970 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
974 /* nothing to dowhen we are not in buffering mode */
975 if (!mq->use_buffering)
978 percent = get_percentage (sq);
981 if (percent >= mq->high_percent) {
982 mq->buffering = FALSE;
984 /* make sure it increases */
985 percent = MAX (mq->percent, percent);
987 SET_PERCENT (mq, percent);
990 gboolean is_buffering = TRUE;
992 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
993 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
995 if (get_percentage (oq) >= mq->high_percent) {
996 is_buffering = FALSE;
1002 if (is_buffering && percent < mq->low_percent) {
1003 mq->buffering = TRUE;
1004 SET_PERCENT (mq, percent);
1010 gst_multi_queue_post_buffering (GstMultiQueue * mq)
1012 GstMessage *msg = NULL;
1014 g_mutex_lock (&mq->buffering_post_lock);
1015 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1016 if (mq->percent_changed) {
1017 gint percent = mq->percent;
1019 mq->percent_changed = FALSE;
1021 percent = percent * 100 / mq->high_percent;
1026 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
1027 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
1029 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1032 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
1034 g_mutex_unlock (&mq->buffering_post_lock);
1037 /* calculate the diff between running time on the sink and src of the queue.
1038 * This is the total amount of time in the queue.
1039 * WITH LOCK TAKEN */
1041 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
1043 gint64 sink_time, src_time;
1045 if (sq->sink_tainted) {
1046 sink_time = sq->sinktime =
1047 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
1048 sq->sink_segment.position);
1050 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
1051 /* if we have a time, we become untainted and use the time */
1052 sq->sink_tainted = FALSE;
1054 sink_time = sq->sinktime;
1056 if (sq->src_tainted) {
1057 GstSegment *segment;
1060 if (sq->has_src_segment) {
1061 segment = &sq->src_segment;
1062 position = sq->src_segment.position;
1065 * If the src pad had no segment yet, use the sink segment
1066 * to avoid signalling overrun if the received sink segment has a
1067 * a position > max-size-time while the src pad time would be the default=0
1069 * This can happen when switching pads on chained/adaptive streams and the
1070 * new chain has a segment with a much larger position
1072 segment = &sq->sink_segment;
1073 position = sq->sink_segment.position;
1076 src_time = sq->srctime =
1077 gst_segment_to_running_time (segment, GST_FORMAT_TIME, position);
1078 /* if we have a time, we become untainted and use the time */
1079 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
1080 sq->src_tainted = FALSE;
1082 src_time = sq->srctime;
1084 GST_DEBUG_OBJECT (mq,
1085 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
1086 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
1088 /* This allows for streams with out of order timestamping - sometimes the
1089 * emerging timestamp is later than the arriving one(s) */
1090 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
1091 sq->cur_time = sink_time - src_time;
1095 /* updating the time level can change the buffering state */
1096 update_buffering (mq, sq);
1101 /* take a SEGMENT event and apply the values to segment, updating the time
1102 * level of queue. */
1104 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1105 GstSegment * segment)
1107 gst_event_copy_segment (event, segment);
1109 /* now configure the values, we use these to track timestamps on the
1111 if (segment->format != GST_FORMAT_TIME) {
1112 /* non-time format, pretent the current time segment is closed with a
1113 * 0 start and unknown stop time. */
1114 segment->format = GST_FORMAT_TIME;
1119 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1121 if (segment == &sq->sink_segment)
1122 sq->sink_tainted = TRUE;
1124 sq->has_src_segment = TRUE;
1125 sq->src_tainted = TRUE;
1128 GST_DEBUG_OBJECT (mq,
1129 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1131 /* segment can update the time level of the queue */
1132 update_time_level (mq, sq);
1134 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1135 gst_multi_queue_post_buffering (mq);
1138 /* take a buffer and update segment, updating the time level of the queue. */
1140 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1141 GstClockTime duration, GstSegment * segment)
1143 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1145 /* if no timestamp is set, assume it's continuous with the previous
1147 if (timestamp == GST_CLOCK_TIME_NONE)
1148 timestamp = segment->position;
1151 if (duration != GST_CLOCK_TIME_NONE)
1152 timestamp += duration;
1154 GST_DEBUG_OBJECT (mq, "queue %d, position updated to %" GST_TIME_FORMAT,
1155 sq->id, GST_TIME_ARGS (timestamp));
1157 segment->position = timestamp;
1159 if (segment == &sq->sink_segment)
1160 sq->sink_tainted = TRUE;
1162 sq->src_tainted = TRUE;
1164 /* calc diff with other end */
1165 update_time_level (mq, sq);
1166 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1167 gst_multi_queue_post_buffering (mq);
1171 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1172 GstSegment * segment)
1174 GstClockTime timestamp;
1175 GstClockTime duration;
1177 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1179 gst_event_parse_gap (event, ×tamp, &duration);
1181 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1183 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1184 timestamp += duration;
1187 segment->position = timestamp;
1189 if (segment == &sq->sink_segment)
1190 sq->sink_tainted = TRUE;
1192 sq->src_tainted = TRUE;
1194 /* calc diff with other end */
1195 update_time_level (mq, sq);
1198 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1199 gst_multi_queue_post_buffering (mq);
1203 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1205 GstClockTime time = GST_CLOCK_TIME_NONE;
1207 if (GST_IS_BUFFER (object)) {
1208 GstBuffer *buf = GST_BUFFER_CAST (object);
1210 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1211 time = GST_BUFFER_TIMESTAMP (buf);
1212 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1213 time += GST_BUFFER_DURATION (buf);
1214 if (time > segment->stop)
1215 time = segment->stop;
1216 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1218 } else if (GST_IS_BUFFER_LIST (object)) {
1219 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1223 n = gst_buffer_list_length (list);
1224 for (i = 0; i < n; i++) {
1225 buf = gst_buffer_list_get (list, i);
1226 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1227 time = GST_BUFFER_TIMESTAMP (buf);
1228 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1229 time += GST_BUFFER_DURATION (buf);
1230 if (time > segment->stop)
1231 time = segment->stop;
1232 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1239 } else if (GST_IS_EVENT (object)) {
1240 GstEvent *event = GST_EVENT_CAST (object);
1242 /* For newsegment events return the running time of the start position */
1243 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1244 const GstSegment *new_segment;
1246 gst_event_parse_segment (event, &new_segment);
1247 if (new_segment->format == GST_FORMAT_TIME) {
1249 gst_segment_to_running_time (new_segment, GST_FORMAT_TIME,
1250 new_segment->start);
1259 static GstFlowReturn
1260 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1261 GstMiniObject * object, gboolean * allow_drop)
1263 GstFlowReturn result = sq->srcresult;
1265 if (GST_IS_BUFFER (object)) {
1267 GstClockTime timestamp, duration;
1269 buffer = GST_BUFFER_CAST (object);
1270 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1271 duration = GST_BUFFER_DURATION (buffer);
1273 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1275 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1276 gst_data_queue_limits_changed (sq->queue);
1278 if (G_UNLIKELY (*allow_drop)) {
1279 GST_DEBUG_OBJECT (mq,
1280 "SingleQueue %d : Dropping EOS buffer %p with ts %" GST_TIME_FORMAT,
1281 sq->id, buffer, GST_TIME_ARGS (timestamp));
1282 gst_buffer_unref (buffer);
1284 GST_DEBUG_OBJECT (mq,
1285 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1286 sq->id, buffer, GST_TIME_ARGS (timestamp));
1287 result = gst_pad_push (sq->srcpad, buffer);
1289 } else if (GST_IS_EVENT (object)) {
1292 event = GST_EVENT_CAST (object);
1294 switch (GST_EVENT_TYPE (event)) {
1296 result = GST_FLOW_EOS;
1297 if (G_UNLIKELY (*allow_drop))
1298 *allow_drop = FALSE;
1300 case GST_EVENT_SEGMENT:
1301 apply_segment (mq, sq, event, &sq->src_segment);
1302 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1303 gst_data_queue_limits_changed (sq->queue);
1304 if (G_UNLIKELY (*allow_drop)) {
1305 result = GST_FLOW_OK;
1306 *allow_drop = FALSE;
1310 apply_gap (mq, sq, event, &sq->src_segment);
1311 /* Applying the gap may have made the queue non-full again, unblock it if needed */
1312 gst_data_queue_limits_changed (sq->queue);
1318 if (G_UNLIKELY (*allow_drop)) {
1319 GST_DEBUG_OBJECT (mq,
1320 "SingleQueue %d : Dropping EOS event %p of type %s",
1321 sq->id, event, GST_EVENT_TYPE_NAME (event));
1322 gst_event_unref (event);
1324 GST_DEBUG_OBJECT (mq,
1325 "SingleQueue %d : Pushing event %p of type %s",
1326 sq->id, event, GST_EVENT_TYPE_NAME (event));
1328 gst_pad_push_event (sq->srcpad, event);
1330 } else if (GST_IS_QUERY (object)) {
1334 query = GST_QUERY_CAST (object);
1336 if (G_UNLIKELY (*allow_drop)) {
1337 GST_DEBUG_OBJECT (mq,
1338 "SingleQueue %d : Dropping EOS query %p", sq->id, query);
1339 gst_query_unref (query);
1342 res = gst_pad_peer_query (sq->srcpad, query);
1345 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1346 sq->last_query = res;
1347 sq->last_handled_query = query;
1348 g_cond_signal (&sq->query_handled);
1349 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1351 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1359 static GstMiniObject *
1360 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1365 item->object = NULL;
1371 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1373 if (!item->is_query && item->object)
1374 gst_mini_object_unref (item->object);
1375 g_slice_free (GstMultiQueueItem, item);
1378 /* takes ownership of passed mini object! */
1379 static GstMultiQueueItem *
1380 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1382 GstMultiQueueItem *item;
1384 item = g_slice_new (GstMultiQueueItem);
1385 item->object = object;
1386 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1387 item->posid = curid;
1388 item->is_query = GST_IS_QUERY (object);
1390 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1391 item->duration = GST_BUFFER_DURATION (object);
1392 if (item->duration == GST_CLOCK_TIME_NONE)
1394 item->visible = TRUE;
1398 static GstMultiQueueItem *
1399 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1401 GstMultiQueueItem *item;
1403 item = g_slice_new (GstMultiQueueItem);
1404 item->object = object;
1405 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1406 item->posid = curid;
1407 item->is_query = GST_IS_QUERY (object);
1411 item->visible = FALSE;
1415 /* Each main loop attempts to push buffers until the return value
1416 * is not-linked. not-linked pads are not allowed to push data beyond
1417 * any linked pads, so they don't 'rush ahead of the pack'.
1420 gst_multi_queue_loop (GstPad * pad)
1423 GstMultiQueueItem *item;
1424 GstDataQueueItem *sitem;
1426 GstMiniObject *object = NULL;
1428 GstFlowReturn result;
1429 GstClockTime next_time;
1431 gboolean do_update_buffering = FALSE;
1432 gboolean dropping = FALSE;
1434 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1438 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1443 /* Get something from the queue, blocking until that happens, or we get
1445 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1448 item = (GstMultiQueueItem *) sitem;
1449 newid = item->posid;
1451 /* steal the object and destroy the item */
1452 object = gst_multi_queue_item_steal_object (item);
1453 gst_multi_queue_item_destroy (item);
1455 is_buffer = GST_IS_BUFFER (object);
1457 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1458 next_time = get_running_time (&sq->src_segment, object, TRUE);
1460 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1461 sq->id, newid, sq->last_oldid);
1463 /* If we're not-linked, we do some extra work because we might need to
1464 * wait before pushing. If we're linked but there's a gap in the IDs,
1465 * or it's the first loop, or we just passed the previous highid,
1466 * we might need to wake some sleeping pad up, so there's extra work
1468 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1469 if (sq->srcresult == GST_FLOW_NOT_LINKED
1470 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1471 || sq->last_oldid > mq->highid) {
1472 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1473 gst_flow_get_name (sq->srcresult));
1475 /* Check again if we're flushing after the lock is taken,
1476 * the flush flag might have been changed in the meantime */
1478 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1482 /* Update the nextid so other threads know when to wake us up */
1484 sq->next_time = next_time;
1486 /* Update the oldid (the last ID we output) for highid tracking */
1487 if (sq->last_oldid != G_MAXUINT32)
1488 sq->oldid = sq->last_oldid;
1490 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1491 /* Go to sleep until it's time to push this buffer */
1493 /* Recompute the highid */
1494 compute_high_id (mq);
1495 /* Recompute the high time */
1496 compute_high_time (mq);
1498 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1499 (mq->high_time == GST_CLOCK_TIME_NONE
1500 || next_time >= mq->high_time))
1501 || (!mq->sync_by_running_time && newid > mq->highid))
1502 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1504 GST_DEBUG_OBJECT (mq,
1505 "queue %d sleeping for not-linked wakeup with "
1506 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1507 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1508 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1510 /* Wake up all non-linked pads before we sleep */
1511 wake_up_next_non_linked (mq);
1514 g_cond_wait (&sq->turn, &mq->qlock);
1518 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1522 /* Recompute the high time */
1523 compute_high_time (mq);
1525 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1526 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1527 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1528 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1531 /* Re-compute the high_id in case someone else pushed */
1532 compute_high_id (mq);
1534 compute_high_id (mq);
1535 /* Wake up all non-linked pads */
1536 wake_up_next_non_linked (mq);
1538 /* We're done waiting, we can clear the nextid and nexttime */
1540 sq->next_time = GST_CLOCK_TIME_NONE;
1542 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1547 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1548 gst_flow_get_name (sq->srcresult));
1550 /* Update time stats */
1551 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1552 next_time = get_running_time (&sq->src_segment, object, FALSE);
1553 if (next_time != GST_CLOCK_TIME_NONE) {
1554 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1555 sq->last_time = next_time;
1556 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1557 /* Wake up all non-linked pads now that we advanced the high time */
1558 mq->high_time = next_time;
1559 wake_up_next_non_linked (mq);
1562 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1564 /* Try to push out the new object */
1565 result = gst_single_queue_push_one (mq, sq, object, &dropping);
1568 /* Check if we pushed something already and if this is
1569 * now a switch from an active to a non-active stream.
1571 * If it is, we reset all the waiting streams, let them
1572 * push another buffer to see if they're now active again.
1573 * This allows faster switching between streams and prevents
1574 * deadlocks if downstream does any waiting too.
1576 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1577 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1578 && result == GST_FLOW_NOT_LINKED) {
1581 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1584 compute_high_id (mq);
1585 do_update_buffering = TRUE;
1587 /* maybe no-one is waiting */
1588 if (mq->numwaiting > 0) {
1589 /* Else figure out which singlequeue(s) need waking up */
1590 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1591 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1593 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1594 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1595 sq2->pushed = FALSE;
1596 sq2->srcresult = GST_FLOW_OK;
1597 g_cond_signal (&sq2->turn);
1606 /* now hold on a bit;
1607 * can not simply throw this result to upstream, because
1608 * that might already be onto another segment, so we have to make
1609 * sure we are relaying the correct info wrt proper segment */
1610 if (result == GST_FLOW_EOS && !dropping &&
1611 sq->srcresult != GST_FLOW_NOT_LINKED) {
1612 GST_DEBUG_OBJECT (mq, "starting EOS drop on sq %d", sq->id);
1614 /* pretend we have not seen EOS yet for upstream's sake */
1615 result = sq->srcresult;
1616 } else if (dropping && gst_data_queue_is_empty (sq->queue)) {
1617 /* queue empty, so stop dropping
1618 * we can commit the result we have now,
1619 * which is either OK after a segment, or EOS */
1620 GST_DEBUG_OBJECT (mq, "committed EOS drop on sq %d", sq->id);
1622 result = GST_FLOW_EOS;
1624 sq->srcresult = result;
1625 sq->last_oldid = newid;
1627 if (do_update_buffering)
1628 update_buffering (mq, sq);
1630 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1631 gst_multi_queue_post_buffering (mq);
1636 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1637 && result != GST_FLOW_EOS)
1640 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1641 gst_flow_get_name (sq->srcresult));
1643 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1644 if (mq->numwaiting > 0 && sq->srcresult == GST_FLOW_EOS) {
1645 compute_high_time (mq);
1646 compute_high_id (mq);
1647 wake_up_next_non_linked (mq);
1649 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1656 gst_mini_object_unref (object);
1658 /* Need to make sure wake up any sleeping pads when we exit */
1659 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1660 compute_high_time (mq);
1661 compute_high_id (mq);
1662 wake_up_next_non_linked (mq);
1663 sq->last_query = FALSE;
1664 g_cond_signal (&sq->query_handled);
1666 /* Post an error message if we got EOS while downstream
1667 * has returned an error flow return. After EOS there
1668 * will be no further buffer which could propagate the
1670 if (sq->is_eos && sq->srcresult < GST_FLOW_EOS) {
1671 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1672 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
1673 ("Internal data stream error."),
1674 ("streaming stopped, reason %s", gst_flow_get_name (sq->srcresult)));
1676 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1679 /* upstream needs to see fatal result ASAP to shut things down,
1680 * but might be stuck in one of our other full queues;
1681 * so empty this one and trigger dynamic queue growth. At
1682 * this point the srcresult is not OK, NOT_LINKED
1683 * or EOS, i.e. a real failure */
1684 gst_single_queue_flush_queue (sq, FALSE);
1685 single_queue_underrun_cb (sq->queue, sq);
1686 gst_data_queue_set_flushing (sq->queue, TRUE);
1687 gst_pad_pause_task (sq->srcpad);
1688 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1689 "SingleQueue[%d] task paused, reason:%s",
1690 sq->id, gst_flow_get_name (sq->srcresult));
1696 * gst_multi_queue_chain:
1698 * This is similar to GstQueue's chain function, except:
1699 * _ we don't have leak behaviours,
1700 * _ we push with a unique id (curid)
1702 static GstFlowReturn
1703 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1707 GstMultiQueueItem *item;
1709 GstClockTime timestamp, duration;
1711 sq = gst_pad_get_element_private (pad);
1714 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1718 /* Get a unique incrementing id */
1719 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1721 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1722 sq->id, buffer, curid);
1724 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1726 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1727 duration = GST_BUFFER_DURATION (buffer);
1729 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1732 /* update time level, we must do this after pushing the data in the queue so
1733 * that we never end up filling the queue first. */
1734 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1737 return sq->srcresult;
1742 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1743 sq->id, gst_flow_get_name (sq->srcresult));
1744 gst_multi_queue_item_destroy (item);
1749 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
1750 gst_buffer_unref (buffer);
1751 return GST_FLOW_EOS;
1756 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
1757 GstPadMode mode, gboolean active)
1763 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1764 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1766 /* mq is NULL if the pad is activated/deactivated before being
1767 * added to the multiqueue */
1769 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1772 case GST_PAD_MODE_PUSH:
1774 /* All pads start off linked until they push one buffer */
1775 sq->srcresult = GST_FLOW_OK;
1777 gst_data_queue_set_flushing (sq->queue, FALSE);
1779 sq->srcresult = GST_FLOW_FLUSHING;
1780 sq->last_query = FALSE;
1781 g_cond_signal (&sq->query_handled);
1782 gst_data_queue_set_flushing (sq->queue, TRUE);
1784 /* Wait until streaming thread has finished */
1786 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1787 GST_PAD_STREAM_LOCK (pad);
1789 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1790 gst_data_queue_flush (sq->queue);
1792 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1793 GST_PAD_STREAM_UNLOCK (pad);
1795 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1805 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1806 gst_object_unref (mq);
1813 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1818 GstMultiQueueItem *item;
1821 GstEvent *sref = NULL;
1823 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1824 mq = (GstMultiQueue *) parent;
1826 type = GST_EVENT_TYPE (event);
1829 case GST_EVENT_FLUSH_START:
1830 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1833 res = gst_pad_push_event (sq->srcpad, event);
1835 gst_single_queue_flush (mq, sq, TRUE, FALSE);
1838 case GST_EVENT_FLUSH_STOP:
1839 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1842 res = gst_pad_push_event (sq->srcpad, event);
1844 gst_single_queue_flush (mq, sq, FALSE, FALSE);
1847 case GST_EVENT_SEGMENT:
1849 /* take ref because the queue will take ownership and we need the event
1850 * afterwards to update the segment */
1851 sref = gst_event_ref (event);
1855 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1856 res = gst_pad_push_event (sq->srcpad, event);
1862 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1866 /* Get an unique incrementing id. */
1867 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1869 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
1871 GST_DEBUG_OBJECT (mq,
1872 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1873 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1875 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1878 /* mark EOS when we received one, we must do that after putting the
1879 * buffer in the queue because EOS marks the buffer as filled. */
1882 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1885 /* Post an error message if we got EOS while downstream
1886 * has returned an error flow return. After EOS there
1887 * will be no further buffer which could propagate the
1889 if (sq->srcresult < GST_FLOW_EOS) {
1890 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1891 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
1892 ("Internal data stream error."),
1893 ("streaming stopped, reason %s",
1894 gst_flow_get_name (sq->srcresult)));
1896 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1899 /* EOS affects the buffering state */
1900 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1901 update_buffering (mq, sq);
1902 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1903 single_queue_overrun_cb (sq->queue, sq);
1904 gst_multi_queue_post_buffering (mq);
1906 case GST_EVENT_SEGMENT:
1907 apply_segment (mq, sq, sref, &sq->sink_segment);
1908 gst_event_unref (sref);
1909 /* a new segment allows us to accept more buffers if we got EOS
1910 * from downstream */
1911 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1912 if (sq->srcresult == GST_FLOW_EOS)
1913 sq->srcresult = GST_FLOW_OK;
1914 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1917 apply_gap (mq, sq, sref, &sq->sink_segment);
1918 gst_event_unref (sref);
1927 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1928 sq->id, gst_flow_get_name (sq->srcresult));
1930 gst_event_unref (sref);
1931 gst_multi_queue_item_destroy (item);
1936 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1937 gst_event_unref (event);
1944 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1950 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1951 mq = (GstMultiQueue *) parent;
1953 switch (GST_QUERY_TYPE (query)) {
1955 if (GST_QUERY_IS_SERIALIZED (query)) {
1957 GstMultiQueueItem *item;
1959 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1960 if (sq->srcresult != GST_FLOW_OK)
1963 /* serialized events go in the queue. We need to be certain that we
1964 * don't cause deadlocks waiting for the query return value. We check if
1965 * the queue is empty (nothing is blocking downstream and the query can
1966 * be pushed for sure) or we are not buffering. If we are buffering,
1967 * the pipeline waits to unblock downstream until our queue fills up
1968 * completely, which can not happen if we block on the query..
1969 * Therefore we only potentially block when we are not buffering. */
1970 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
1971 /* Get an unique incrementing id. */
1972 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1974 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
1976 GST_DEBUG_OBJECT (mq,
1977 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
1978 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
1979 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1980 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
1981 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1982 /* it might be that the query has been taken out of the queue
1983 * while we were unlocked. So, we need to check if the last
1984 * handled query is the same one than the one we just
1985 * pushed. If it is, we don't need to wait for the condition
1986 * variable, otherwise we wait for the condition variable to
1988 if (sq->last_handled_query != query)
1989 g_cond_wait (&sq->query_handled, &mq->qlock);
1990 res = sq->last_query;
1991 sq->last_handled_query = NULL;
1993 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
1994 "queue is not empty");
1997 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1999 /* default handling */
2000 res = gst_pad_query_default (pad, parent, query);
2008 GST_DEBUG_OBJECT (mq, "Flushing");
2009 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2015 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2016 GstPadMode mode, gboolean active)
2022 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2025 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2028 case GST_PAD_MODE_PUSH:
2030 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
2032 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
2033 /* make sure streaming finishes */
2034 result |= gst_pad_stop_task (pad);
2045 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2047 GstSingleQueue *sq = gst_pad_get_element_private (pad);
2048 GstMultiQueue *mq = sq->mqueue;
2051 switch (GST_EVENT_TYPE (event)) {
2052 case GST_EVENT_RECONFIGURE:
2053 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2054 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2055 sq->srcresult = GST_FLOW_OK;
2056 g_cond_signal (&sq->turn);
2058 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2060 ret = gst_pad_push_event (sq->sinkpad, event);
2063 ret = gst_pad_push_event (sq->sinkpad, event);
2071 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2075 /* FIXME, Handle position offset depending on queue size */
2076 switch (GST_QUERY_TYPE (query)) {
2078 /* default handling */
2079 res = gst_pad_query_default (pad, parent, query);
2086 * Next-non-linked functions
2089 /* WITH LOCK TAKEN */
2091 wake_up_next_non_linked (GstMultiQueue * mq)
2095 /* maybe no-one is waiting */
2096 if (mq->numwaiting < 1)
2099 /* Else figure out which singlequeue(s) need waking up */
2100 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2101 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2103 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2104 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
2105 && sq->next_time != GST_CLOCK_TIME_NONE
2106 && sq->next_time >= mq->high_time)
2107 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
2108 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2109 g_cond_signal (&sq->turn);
2115 /* WITH LOCK TAKEN */
2117 compute_high_id (GstMultiQueue * mq)
2119 /* The high-id is either the highest id among the linked pads, or if all
2120 * pads are not-linked, it's the lowest not-linked pad */
2122 guint32 lowest = G_MAXUINT32;
2123 guint32 highid = G_MAXUINT32;
2125 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2126 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2128 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2129 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2131 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2132 /* No need to consider queues which are not waiting */
2133 if (sq->nextid == 0) {
2134 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2138 if (sq->nextid < lowest)
2139 lowest = sq->nextid;
2140 } else if (sq->srcresult != GST_FLOW_EOS) {
2141 /* If we don't have a global highid, or the global highid is lower than
2142 * this single queue's last outputted id, store the queue's one,
2143 * unless the singlequeue is at EOS (srcresult = EOS) */
2144 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2149 if (highid == G_MAXUINT32 || lowest < highid)
2150 mq->highid = lowest;
2152 mq->highid = highid;
2154 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2158 /* WITH LOCK TAKEN */
2160 compute_high_time (GstMultiQueue * mq)
2162 /* The high-id is either the highest id among the linked pads, or if all
2163 * pads are not-linked, it's the lowest not-linked pad */
2165 GstClockTime highest = GST_CLOCK_TIME_NONE;
2166 GstClockTime lowest = GST_CLOCK_TIME_NONE;
2168 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2169 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2172 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
2173 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
2174 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
2176 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2177 /* No need to consider queues which are not waiting */
2178 if (sq->next_time == GST_CLOCK_TIME_NONE) {
2179 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2183 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
2184 lowest = sq->next_time;
2185 } else if (sq->srcresult != GST_FLOW_EOS) {
2186 /* If we don't have a global highid, or the global highid is lower than
2187 * this single queue's last outputted id, store the queue's one,
2188 * unless the singlequeue is at EOS (srcresult = EOS) */
2189 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
2190 highest = sq->last_time;
2194 mq->high_time = highest;
2197 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
2198 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
2201 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2202 ((q)->max_size.format) <= (value))
2205 * GstSingleQueue functions
2208 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2210 GstMultiQueue *mq = sq->mqueue;
2212 GstDataQueueSize size;
2213 gboolean filled = TRUE;
2214 gboolean empty_found = FALSE;
2216 gst_data_queue_get_level (sq->queue, &size);
2219 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2220 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2221 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2224 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2226 /* check if we reached the hard time/bytes limits */
2227 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
2228 IS_FILLED (sq, time, sq->cur_time)) {
2232 /* Search for empty queues */
2233 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2234 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2239 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2240 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2244 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2245 if (gst_data_queue_is_empty (oq->queue)) {
2246 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2252 /* if hard limits are not reached then we allow one more buffer in the full
2253 * queue, but only if any of the other singelqueues are empty */
2255 if (IS_FILLED (sq, visible, size.visible)) {
2256 sq->max_size.visible = size.visible + 1;
2257 GST_DEBUG_OBJECT (mq,
2258 "Bumping single queue %d max visible to %d",
2259 sq->id, sq->max_size.visible);
2265 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2267 /* Overrun is always forwarded, since this is blocking the upstream element */
2269 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2270 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2275 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2277 gboolean empty = TRUE;
2278 GstMultiQueue *mq = sq->mqueue;
2281 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2282 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2286 "Single Queue %d is empty, Checking other single queues", sq->id);
2289 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2290 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2291 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2293 if (gst_data_queue_is_full (oq->queue)) {
2294 GstDataQueueSize size;
2296 gst_data_queue_get_level (oq->queue, &size);
2297 if (IS_FILLED (oq, visible, size.visible)) {
2298 oq->max_size.visible = size.visible + 1;
2299 GST_DEBUG_OBJECT (mq,
2300 "queue %d is filled, bumping its max visible to %d", oq->id,
2301 oq->max_size.visible);
2302 gst_data_queue_limits_changed (oq->queue);
2305 if (!gst_data_queue_is_empty (oq->queue))
2308 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2311 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2312 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2317 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2318 guint64 time, GstSingleQueue * sq)
2321 GstMultiQueue *mq = sq->mqueue;
2323 GST_DEBUG_OBJECT (mq,
2324 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2325 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2326 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2328 /* we are always filled on EOS */
2332 /* we never go past the max visible items unless we are in buffering mode */
2333 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2336 /* check time or bytes */
2337 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
2343 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2345 GstDataQueueItem *sitem;
2346 GstMultiQueueItem *mitem;
2347 gboolean was_flushing = FALSE;
2349 while (!gst_data_queue_is_empty (sq->queue)) {
2350 GstMiniObject *data;
2352 /* FIXME: If this fails here although the queue is not empty,
2353 * we're flushing... but we want to rescue all sticky
2354 * events nonetheless.
2356 if (!gst_data_queue_pop (sq->queue, &sitem)) {
2357 was_flushing = TRUE;
2358 gst_data_queue_set_flushing (sq->queue, FALSE);
2362 mitem = (GstMultiQueueItem *) sitem;
2364 data = sitem->object;
2366 if (!full && !mitem->is_query && GST_IS_EVENT (data)
2367 && GST_EVENT_IS_STICKY (data)
2368 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2369 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2370 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2373 sitem->destroy (sitem);
2376 gst_data_queue_flush (sq->queue);
2378 gst_data_queue_set_flushing (sq->queue, TRUE);
2380 GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2381 update_buffering (sq->mqueue, sq);
2382 GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2383 gst_multi_queue_post_buffering (sq->mqueue);
2387 gst_single_queue_free (GstSingleQueue * sq)
2390 gst_data_queue_flush (sq->queue);
2391 g_object_unref (sq->queue);
2392 g_cond_clear (&sq->turn);
2393 g_cond_clear (&sq->query_handled);
2397 static GstSingleQueue *
2398 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2403 guint temp_id = (id == -1) ? 0 : id;
2405 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2407 /* Find an unused queue ID, if possible the passed one */
2408 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2409 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2410 /* This works because the IDs are sorted in ascending order */
2411 if (sq2->id == temp_id) {
2412 /* If this ID was requested by the caller return NULL,
2413 * otherwise just get us the next one */
2415 temp_id = sq2->id + 1;
2418 } else if (sq2->id > temp_id) {
2423 sq = g_new0 (GstSingleQueue, 1);
2427 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2428 mqueue->queues_cookie++;
2430 /* copy over max_size and extra_size so we don't need to take the lock
2431 * any longer when checking if the queue is full. */
2432 sq->max_size.visible = mqueue->max_size.visible;
2433 sq->max_size.bytes = mqueue->max_size.bytes;
2434 sq->max_size.time = mqueue->max_size.time;
2436 sq->extra_size.visible = mqueue->extra_size.visible;
2437 sq->extra_size.bytes = mqueue->extra_size.bytes;
2438 sq->extra_size.time = mqueue->extra_size.time;
2440 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2442 sq->mqueue = mqueue;
2443 sq->srcresult = GST_FLOW_FLUSHING;
2445 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2446 single_queue_check_full,
2447 (GstDataQueueFullCallback) single_queue_overrun_cb,
2448 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2450 sq->flushing = FALSE;
2451 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2452 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2456 sq->next_time = GST_CLOCK_TIME_NONE;
2457 sq->last_time = GST_CLOCK_TIME_NONE;
2458 g_cond_init (&sq->turn);
2459 g_cond_init (&sq->query_handled);
2461 sq->sinktime = GST_CLOCK_TIME_NONE;
2462 sq->srctime = GST_CLOCK_TIME_NONE;
2463 sq->sink_tainted = TRUE;
2464 sq->src_tainted = TRUE;
2466 name = g_strdup_printf ("sink_%u", sq->id);
2467 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
2470 gst_pad_set_chain_function (sq->sinkpad,
2471 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2472 gst_pad_set_activatemode_function (sq->sinkpad,
2473 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2474 gst_pad_set_event_function (sq->sinkpad,
2475 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2476 gst_pad_set_query_function (sq->sinkpad,
2477 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2478 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2479 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2480 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2482 name = g_strdup_printf ("src_%u", sq->id);
2483 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2486 gst_pad_set_activatemode_function (sq->srcpad,
2487 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2488 gst_pad_set_event_function (sq->srcpad,
2489 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2490 gst_pad_set_query_function (sq->srcpad,
2491 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2492 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2493 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2494 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2496 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2497 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2499 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2501 /* only activate the pads when we are not in the NULL state
2502 * and add the pad under the state_lock to prevend state changes
2503 * between activating and adding */
2504 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2505 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2506 gst_pad_set_active (sq->srcpad, TRUE);
2507 gst_pad_set_active (sq->sinkpad, TRUE);
2509 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2510 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2511 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2513 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",