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,
264 PROP_SYNC_BY_RUNNING_TIME,
268 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
269 g_mutex_lock (&q->qlock); \
272 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
273 g_mutex_unlock (&q->qlock); \
276 #define SET_PERCENT(mq, perc) G_STMT_START { \
277 if (perc != mq->percent) { \
278 mq->percent = perc; \
279 mq->percent_changed = TRUE; \
280 GST_DEBUG_OBJECT (mq, "buffering %d percent", perc); \
284 static void gst_multi_queue_finalize (GObject * object);
285 static void gst_multi_queue_set_property (GObject * object,
286 guint prop_id, const GValue * value, GParamSpec * pspec);
287 static void gst_multi_queue_get_property (GObject * object,
288 guint prop_id, GValue * value, GParamSpec * pspec);
290 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
291 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
292 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
293 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
294 element, GstStateChange transition);
296 static void gst_multi_queue_loop (GstPad * pad);
299 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
300 #define gst_multi_queue_parent_class parent_class
301 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
304 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
307 gst_multi_queue_class_init (GstMultiQueueClass * klass)
309 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
310 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
312 gobject_class->set_property = gst_multi_queue_set_property;
313 gobject_class->get_property = gst_multi_queue_get_property;
318 * GstMultiQueue::underrun:
319 * @multiqueue: the multiqueue instance
321 * This signal is emitted from the streaming thread when there is
322 * no data in any of the queues inside the multiqueue instance (underrun).
324 * This indicates either starvation or EOS from the upstream data sources.
326 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
327 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
328 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
329 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
332 * GstMultiQueue::overrun:
333 * @multiqueue: the multiqueue instance
335 * Reports that one of the queues in the multiqueue is full (overrun).
336 * A queue is full if the total amount of data inside it (num-buffers, time,
337 * size) is higher than the boundary values which can be set through the
338 * GObject properties.
340 * This can be used as an indicator of pre-roll.
342 gst_multi_queue_signals[SIGNAL_OVERRUN] =
343 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
344 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
345 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
349 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
350 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
351 "Max. amount of data in the queue (bytes, 0=disable)",
352 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
353 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
354 G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
356 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
357 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
358 DEFAULT_MAX_SIZE_BUFFERS,
359 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
360 G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
362 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
363 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
364 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
365 G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
368 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
369 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
370 " (NOT IMPLEMENTED)",
371 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
372 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
374 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
375 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
376 " (NOT IMPLEMENTED)",
377 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
378 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
379 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
380 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
381 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
382 " (NOT IMPLEMENTED)",
383 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
387 * GstMultiQueue:use-buffering
389 * Enable the buffering option in multiqueue so that BUFFERING messages are
390 * emitted based on low-/high-percent thresholds.
392 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
393 g_param_spec_boolean ("use-buffering", "Use buffering",
394 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
395 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
396 G_PARAM_STATIC_STRINGS));
398 * GstMultiQueue:low-percent
400 * Low threshold percent for buffering to start.
402 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
403 g_param_spec_int ("low-percent", "Low percent",
404 "Low threshold for buffering to start", 0, 100,
405 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
407 * GstMultiQueue:high-percent
409 * High threshold percent for buffering to finish.
411 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
412 g_param_spec_int ("high-percent", "High percent",
413 "High threshold for buffering to finish", 0, 100,
414 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 * GstMultiQueue:sync-by-running-time
419 * If enabled multiqueue will synchronize deactivated or not-linked streams
420 * to the activated and linked streams by taking the running time.
421 * Otherwise multiqueue will synchronize the deactivated or not-linked
422 * streams by keeping the order in which buffers and events arrived compared
423 * to active and linked streams.
425 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
426 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
427 "Synchronize deactivated or not-linked streams by running time",
428 DEFAULT_SYNC_BY_RUNNING_TIME,
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 gobject_class->finalize = gst_multi_queue_finalize;
433 gst_element_class_set_static_metadata (gstelement_class,
435 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
436 gst_element_class_add_pad_template (gstelement_class,
437 gst_static_pad_template_get (&sinktemplate));
438 gst_element_class_add_pad_template (gstelement_class,
439 gst_static_pad_template_get (&srctemplate));
441 gstelement_class->request_new_pad =
442 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
443 gstelement_class->release_pad =
444 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
445 gstelement_class->change_state =
446 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
450 gst_multi_queue_init (GstMultiQueue * mqueue)
452 mqueue->nbqueues = 0;
453 mqueue->queues = NULL;
455 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
456 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
457 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
459 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
460 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
461 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
463 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
464 mqueue->low_percent = DEFAULT_LOW_PERCENT;
465 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
467 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
471 mqueue->high_time = GST_CLOCK_TIME_NONE;
473 g_mutex_init (&mqueue->qlock);
474 g_mutex_init (&mqueue->buffering_post_lock);
478 gst_multi_queue_finalize (GObject * object)
480 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
482 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
483 g_list_free (mqueue->queues);
484 mqueue->queues = NULL;
485 mqueue->queues_cookie++;
487 /* free/unref instance data */
488 g_mutex_clear (&mqueue->qlock);
489 g_mutex_clear (&mqueue->buffering_post_lock);
491 G_OBJECT_CLASS (parent_class)->finalize (object);
494 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
495 GList * tmp = mq->queues; \
497 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
498 q->max_size.format = mq->max_size.format; \
499 update_buffering (mq, q); \
500 gst_data_queue_limits_changed (q->queue); \
501 tmp = g_list_next(tmp); \
506 gst_multi_queue_set_property (GObject * object, guint prop_id,
507 const GValue * value, GParamSpec * pspec)
509 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
512 case PROP_MAX_SIZE_BYTES:
513 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
514 mq->max_size.bytes = g_value_get_uint (value);
515 SET_CHILD_PROPERTY (mq, bytes);
516 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
517 gst_multi_queue_post_buffering (mq);
519 case PROP_MAX_SIZE_BUFFERS:
522 gint new_size = g_value_get_uint (value);
524 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
526 mq->max_size.visible = new_size;
530 GstDataQueueSize size;
531 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
532 gst_data_queue_get_level (q->queue, &size);
534 GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
535 " current: %d, current max %d", q->id, new_size, size.visible,
536 q->max_size.visible);
538 /* do not reduce max size below current level if the single queue
539 * has grown because of empty queue */
541 q->max_size.visible = new_size;
542 } else if (q->max_size.visible == 0) {
543 q->max_size.visible = MAX (new_size, size.visible);
544 } else if (new_size > size.visible) {
545 q->max_size.visible = new_size;
547 update_buffering (mq, q);
548 gst_data_queue_limits_changed (q->queue);
549 tmp = g_list_next (tmp);
552 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
553 gst_multi_queue_post_buffering (mq);
557 case PROP_MAX_SIZE_TIME:
558 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
559 mq->max_size.time = g_value_get_uint64 (value);
560 SET_CHILD_PROPERTY (mq, time);
561 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
562 gst_multi_queue_post_buffering (mq);
564 case PROP_EXTRA_SIZE_BYTES:
565 mq->extra_size.bytes = g_value_get_uint (value);
567 case PROP_EXTRA_SIZE_BUFFERS:
568 mq->extra_size.visible = g_value_get_uint (value);
570 case PROP_EXTRA_SIZE_TIME:
571 mq->extra_size.time = g_value_get_uint64 (value);
573 case PROP_USE_BUFFERING:
574 mq->use_buffering = g_value_get_boolean (value);
575 if (!mq->use_buffering && mq->buffering) {
576 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
577 mq->buffering = FALSE;
578 GST_DEBUG_OBJECT (mq, "buffering 100 percent");
579 SET_PERCENT (mq, 100);
580 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
583 if (mq->use_buffering) {
586 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
588 mq->buffering = TRUE;
591 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
592 update_buffering (mq, q);
593 gst_data_queue_limits_changed (q->queue);
594 tmp = g_list_next (tmp);
597 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
599 gst_multi_queue_post_buffering (mq);
601 case PROP_LOW_PERCENT:
602 mq->low_percent = g_value_get_int (value);
604 case PROP_HIGH_PERCENT:
605 mq->high_percent = g_value_get_int (value);
607 case PROP_SYNC_BY_RUNNING_TIME:
608 mq->sync_by_running_time = g_value_get_boolean (value);
611 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
617 gst_multi_queue_get_property (GObject * object, guint prop_id,
618 GValue * value, GParamSpec * pspec)
620 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
622 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
625 case PROP_EXTRA_SIZE_BYTES:
626 g_value_set_uint (value, mq->extra_size.bytes);
628 case PROP_EXTRA_SIZE_BUFFERS:
629 g_value_set_uint (value, mq->extra_size.visible);
631 case PROP_EXTRA_SIZE_TIME:
632 g_value_set_uint64 (value, mq->extra_size.time);
634 case PROP_MAX_SIZE_BYTES:
635 g_value_set_uint (value, mq->max_size.bytes);
637 case PROP_MAX_SIZE_BUFFERS:
638 g_value_set_uint (value, mq->max_size.visible);
640 case PROP_MAX_SIZE_TIME:
641 g_value_set_uint64 (value, mq->max_size.time);
643 case PROP_USE_BUFFERING:
644 g_value_set_boolean (value, mq->use_buffering);
646 case PROP_LOW_PERCENT:
647 g_value_set_int (value, mq->low_percent);
649 case PROP_HIGH_PERCENT:
650 g_value_set_int (value, mq->high_percent);
652 case PROP_SYNC_BY_RUNNING_TIME:
653 g_value_set_boolean (value, mq->sync_by_running_time);
656 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
660 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
664 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
666 GstIterator *it = NULL;
668 GstSingleQueue *squeue;
669 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
672 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
673 squeue = gst_pad_get_element_private (pad);
677 if (squeue->sinkpad == pad)
678 opad = gst_object_ref (squeue->srcpad);
679 else if (squeue->srcpad == pad)
680 opad = gst_object_ref (squeue->sinkpad);
684 g_value_init (&val, GST_TYPE_PAD);
685 g_value_set_object (&val, opad);
686 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
687 g_value_unset (&val);
689 gst_object_unref (opad);
692 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
703 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
704 const gchar * name, const GstCaps * caps)
706 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
707 GstSingleQueue *squeue;
711 sscanf (name + 4, "_%u", &temp_id);
712 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
715 /* Create a new single queue, add the sink and source pad and return the sink pad */
716 squeue = gst_single_queue_new (mqueue, temp_id);
718 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
719 GST_DEBUG_PAD_NAME (squeue->sinkpad));
721 return squeue ? squeue->sinkpad : NULL;
725 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
727 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
728 GstSingleQueue *sq = NULL;
731 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
733 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
734 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
735 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
736 sq = (GstSingleQueue *) tmp->data;
738 if (sq->sinkpad == pad)
743 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
744 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
748 /* FIXME: The removal of the singlequeue should probably not happen until it
749 * finishes draining */
751 /* remove it from the list */
752 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
753 mqueue->queues_cookie++;
755 /* FIXME : recompute next-non-linked */
756 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
758 /* delete SingleQueue */
759 gst_data_queue_set_flushing (sq->queue, TRUE);
761 gst_pad_set_active (sq->srcpad, FALSE);
762 gst_pad_set_active (sq->sinkpad, FALSE);
763 gst_pad_set_element_private (sq->srcpad, NULL);
764 gst_pad_set_element_private (sq->sinkpad, NULL);
765 gst_element_remove_pad (element, sq->srcpad);
766 gst_element_remove_pad (element, sq->sinkpad);
767 gst_single_queue_free (sq);
770 static GstStateChangeReturn
771 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
773 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
774 GstSingleQueue *sq = NULL;
775 GstStateChangeReturn result;
777 switch (transition) {
778 case GST_STATE_CHANGE_READY_TO_PAUSED:{
781 /* Set all pads to non-flushing */
782 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
783 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
784 sq = (GstSingleQueue *) tmp->data;
785 sq->flushing = FALSE;
788 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
789 SET_CHILD_PROPERTY (mqueue, visible);
791 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
792 gst_multi_queue_post_buffering (mqueue);
796 case GST_STATE_CHANGE_PAUSED_TO_READY:{
799 /* Un-wait all waiting pads */
800 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
801 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
802 sq = (GstSingleQueue *) tmp->data;
804 g_cond_signal (&sq->turn);
806 sq->last_query = FALSE;
807 g_cond_signal (&sq->query_handled);
809 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
816 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
818 switch (transition) {
827 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
832 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
836 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
837 sq->srcresult = GST_FLOW_FLUSHING;
838 gst_data_queue_set_flushing (sq->queue, TRUE);
842 /* wake up non-linked task */
843 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
845 g_cond_signal (&sq->turn);
846 sq->last_query = FALSE;
847 g_cond_signal (&sq->query_handled);
848 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
850 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
851 result = gst_pad_pause_task (sq->srcpad);
852 sq->sink_tainted = sq->src_tainted = TRUE;
854 gst_single_queue_flush_queue (sq, full);
856 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
857 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
858 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
859 sq->has_src_segment = FALSE;
860 /* All pads start off not-linked for a smooth kick-off */
861 sq->srcresult = GST_FLOW_OK;
864 sq->max_size.visible = mq->max_size.visible;
868 sq->last_oldid = G_MAXUINT32;
869 sq->next_time = GST_CLOCK_TIME_NONE;
870 sq->last_time = GST_CLOCK_TIME_NONE;
871 gst_data_queue_set_flushing (sq->queue, FALSE);
873 /* Reset high time to be recomputed next */
874 mq->high_time = GST_CLOCK_TIME_NONE;
876 sq->flushing = FALSE;
877 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
879 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
881 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
887 /* WITH LOCK TAKEN */
889 get_percentage (GstSingleQueue * sq)
891 GstDataQueueSize size;
894 gst_data_queue_get_level (sq->queue, &size);
896 GST_DEBUG_OBJECT (sq->mqueue,
897 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
898 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
899 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
901 /* get bytes and time percentages and take the max */
902 if (sq->is_eos || sq->srcresult == GST_FLOW_NOT_LINKED) {
906 if (sq->max_size.time > 0) {
907 tmp = (sq->cur_time * 100) / sq->max_size.time;
908 percent = MAX (percent, tmp);
910 if (sq->max_size.bytes > 0) {
911 tmp = (size.bytes * 100) / sq->max_size.bytes;
912 percent = MAX (percent, tmp);
919 /* WITH LOCK TAKEN */
921 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
925 /* nothing to dowhen we are not in buffering mode */
926 if (!mq->use_buffering)
929 percent = get_percentage (sq);
932 if (percent >= mq->high_percent) {
933 mq->buffering = FALSE;
935 /* make sure it increases */
936 percent = MAX (mq->percent, percent);
938 SET_PERCENT (mq, percent);
941 gboolean is_buffering = TRUE;
943 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
944 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
946 if (get_percentage (oq) >= 100) {
947 is_buffering = FALSE;
953 if (is_buffering && percent < mq->low_percent) {
954 mq->buffering = TRUE;
955 SET_PERCENT (mq, percent);
961 gst_multi_queue_post_buffering (GstMultiQueue * mq)
963 GstMessage *msg = NULL;
965 g_mutex_lock (&mq->buffering_post_lock);
966 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
967 if (mq->percent_changed) {
968 gint percent = mq->percent;
970 mq->percent_changed = FALSE;
972 percent = percent * 100 / mq->high_percent;
977 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
978 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
980 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
983 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
985 g_mutex_unlock (&mq->buffering_post_lock);
988 /* calculate the diff between running time on the sink and src of the queue.
989 * This is the total amount of time in the queue.
992 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
994 gint64 sink_time, src_time;
996 if (sq->sink_tainted) {
997 sink_time = sq->sinktime =
998 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
999 sq->sink_segment.position);
1001 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
1002 /* if we have a time, we become untainted and use the time */
1003 sq->sink_tainted = FALSE;
1005 sink_time = sq->sinktime;
1007 if (sq->src_tainted) {
1008 GstSegment *segment;
1011 if (sq->has_src_segment) {
1012 segment = &sq->src_segment;
1013 position = sq->src_segment.position;
1016 * If the src pad had no segment yet, use the sink segment
1017 * to avoid signalling overrun if the received sink segment has a
1018 * a position > max-size-time while the src pad time would be the default=0
1020 * This can happen when switching pads on chained/adaptive streams and the
1021 * new chain has a segment with a much larger position
1023 segment = &sq->sink_segment;
1024 position = sq->sink_segment.position;
1027 src_time = sq->srctime =
1028 gst_segment_to_running_time (segment, GST_FORMAT_TIME, position);
1029 /* if we have a time, we become untainted and use the time */
1030 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
1031 sq->src_tainted = FALSE;
1033 src_time = sq->srctime;
1035 GST_DEBUG_OBJECT (mq,
1036 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
1037 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
1039 /* This allows for streams with out of order timestamping - sometimes the
1040 * emerging timestamp is later than the arriving one(s) */
1041 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
1042 sq->cur_time = sink_time - src_time;
1046 /* updating the time level can change the buffering state */
1047 update_buffering (mq, sq);
1052 /* take a SEGMENT event and apply the values to segment, updating the time
1053 * level of queue. */
1055 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1056 GstSegment * segment)
1058 gst_event_copy_segment (event, segment);
1060 /* now configure the values, we use these to track timestamps on the
1062 if (segment->format != GST_FORMAT_TIME) {
1063 /* non-time format, pretent the current time segment is closed with a
1064 * 0 start and unknown stop time. */
1065 segment->format = GST_FORMAT_TIME;
1070 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1072 if (segment == &sq->sink_segment)
1073 sq->sink_tainted = TRUE;
1075 sq->has_src_segment = TRUE;
1076 sq->src_tainted = TRUE;
1079 GST_DEBUG_OBJECT (mq,
1080 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1082 /* segment can update the time level of the queue */
1083 update_time_level (mq, sq);
1085 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1086 gst_multi_queue_post_buffering (mq);
1089 /* take a buffer and update segment, updating the time level of the queue. */
1091 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1092 GstClockTime duration, GstSegment * segment)
1094 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1096 /* if no timestamp is set, assume it's continuous with the previous
1098 if (timestamp == GST_CLOCK_TIME_NONE)
1099 timestamp = segment->position;
1102 if (duration != GST_CLOCK_TIME_NONE)
1103 timestamp += duration;
1105 GST_DEBUG_OBJECT (mq, "queue %d, position updated to %" GST_TIME_FORMAT,
1106 sq->id, GST_TIME_ARGS (timestamp));
1108 segment->position = timestamp;
1110 if (segment == &sq->sink_segment)
1111 sq->sink_tainted = TRUE;
1113 sq->src_tainted = TRUE;
1115 /* calc diff with other end */
1116 update_time_level (mq, sq);
1117 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1118 gst_multi_queue_post_buffering (mq);
1122 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1123 GstSegment * segment)
1125 GstClockTime timestamp;
1126 GstClockTime duration;
1128 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1130 gst_event_parse_gap (event, ×tamp, &duration);
1132 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1134 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1135 timestamp += duration;
1138 segment->position = timestamp;
1140 if (segment == &sq->sink_segment)
1141 sq->sink_tainted = TRUE;
1143 sq->src_tainted = TRUE;
1145 /* calc diff with other end */
1146 update_time_level (mq, sq);
1149 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1150 gst_multi_queue_post_buffering (mq);
1154 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1156 GstClockTime time = GST_CLOCK_TIME_NONE;
1158 if (GST_IS_BUFFER (object)) {
1159 GstBuffer *buf = GST_BUFFER_CAST (object);
1161 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1162 time = GST_BUFFER_TIMESTAMP (buf);
1163 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1164 time += GST_BUFFER_DURATION (buf);
1165 if (time > segment->stop)
1166 time = segment->stop;
1167 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1169 } else if (GST_IS_BUFFER_LIST (object)) {
1170 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1174 n = gst_buffer_list_length (list);
1175 for (i = 0; i < n; i++) {
1176 buf = gst_buffer_list_get (list, i);
1177 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1178 time = GST_BUFFER_TIMESTAMP (buf);
1179 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1180 time += GST_BUFFER_DURATION (buf);
1181 if (time > segment->stop)
1182 time = segment->stop;
1183 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1190 } else if (GST_IS_EVENT (object)) {
1191 GstEvent *event = GST_EVENT_CAST (object);
1193 /* For newsegment events return the running time of the start position */
1194 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1195 const GstSegment *new_segment;
1197 gst_event_parse_segment (event, &new_segment);
1198 if (new_segment->format == GST_FORMAT_TIME) {
1200 gst_segment_to_running_time (new_segment, GST_FORMAT_TIME,
1201 new_segment->start);
1210 static GstFlowReturn
1211 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1212 GstMiniObject * object)
1214 GstFlowReturn result = sq->srcresult;
1216 if (GST_IS_BUFFER (object)) {
1218 GstClockTime timestamp, duration;
1220 buffer = GST_BUFFER_CAST (object);
1221 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1222 duration = GST_BUFFER_DURATION (buffer);
1224 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1226 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1227 gst_data_queue_limits_changed (sq->queue);
1229 GST_DEBUG_OBJECT (mq,
1230 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1231 sq->id, buffer, GST_TIME_ARGS (timestamp));
1233 result = gst_pad_push (sq->srcpad, buffer);
1234 } else if (GST_IS_EVENT (object)) {
1237 event = GST_EVENT_CAST (object);
1239 switch (GST_EVENT_TYPE (event)) {
1241 result = GST_FLOW_EOS;
1243 case GST_EVENT_SEGMENT:
1244 apply_segment (mq, sq, event, &sq->src_segment);
1245 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1246 gst_data_queue_limits_changed (sq->queue);
1249 apply_gap (mq, sq, event, &sq->src_segment);
1250 /* Applying the gap may have made the queue non-full again, unblock it if needed */
1251 gst_data_queue_limits_changed (sq->queue);
1257 GST_DEBUG_OBJECT (mq,
1258 "SingleQueue %d : Pushing event %p of type %s",
1259 sq->id, event, GST_EVENT_TYPE_NAME (event));
1261 gst_pad_push_event (sq->srcpad, event);
1262 } else if (GST_IS_QUERY (object)) {
1266 query = GST_QUERY_CAST (object);
1268 res = gst_pad_peer_query (sq->srcpad, query);
1270 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1271 sq->last_query = res;
1272 sq->last_handled_query = query;
1273 g_cond_signal (&sq->query_handled);
1274 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1276 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1284 static GstMiniObject *
1285 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1290 item->object = NULL;
1296 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1298 if (!item->is_query && item->object)
1299 gst_mini_object_unref (item->object);
1300 g_slice_free (GstMultiQueueItem, item);
1303 /* takes ownership of passed mini object! */
1304 static GstMultiQueueItem *
1305 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1307 GstMultiQueueItem *item;
1309 item = g_slice_new (GstMultiQueueItem);
1310 item->object = object;
1311 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1312 item->posid = curid;
1313 item->is_query = GST_IS_QUERY (object);
1315 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1316 item->duration = GST_BUFFER_DURATION (object);
1317 if (item->duration == GST_CLOCK_TIME_NONE)
1319 item->visible = TRUE;
1323 static GstMultiQueueItem *
1324 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1326 GstMultiQueueItem *item;
1328 item = g_slice_new (GstMultiQueueItem);
1329 item->object = object;
1330 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1331 item->posid = curid;
1332 item->is_query = GST_IS_QUERY (object);
1336 item->visible = FALSE;
1340 /* Each main loop attempts to push buffers until the return value
1341 * is not-linked. not-linked pads are not allowed to push data beyond
1342 * any linked pads, so they don't 'rush ahead of the pack'.
1345 gst_multi_queue_loop (GstPad * pad)
1348 GstMultiQueueItem *item;
1349 GstDataQueueItem *sitem;
1351 GstMiniObject *object = NULL;
1353 GstFlowReturn result;
1354 GstClockTime next_time;
1356 gboolean do_update_buffering = FALSE;
1358 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1361 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1366 /* Get something from the queue, blocking until that happens, or we get
1368 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1371 item = (GstMultiQueueItem *) sitem;
1372 newid = item->posid;
1374 /* steal the object and destroy the item */
1375 object = gst_multi_queue_item_steal_object (item);
1376 gst_multi_queue_item_destroy (item);
1378 is_buffer = GST_IS_BUFFER (object);
1380 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1381 next_time = get_running_time (&sq->src_segment, object, TRUE);
1383 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1384 sq->id, newid, sq->last_oldid);
1386 /* If we're not-linked, we do some extra work because we might need to
1387 * wait before pushing. If we're linked but there's a gap in the IDs,
1388 * or it's the first loop, or we just passed the previous highid,
1389 * we might need to wake some sleeping pad up, so there's extra work
1391 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1392 if (sq->srcresult == GST_FLOW_NOT_LINKED
1393 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1394 || sq->last_oldid > mq->highid) {
1395 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1396 gst_flow_get_name (sq->srcresult));
1398 /* Check again if we're flushing after the lock is taken,
1399 * the flush flag might have been changed in the meantime */
1401 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1405 /* Update the nextid so other threads know when to wake us up */
1407 sq->next_time = next_time;
1409 /* Update the oldid (the last ID we output) for highid tracking */
1410 if (sq->last_oldid != G_MAXUINT32)
1411 sq->oldid = sq->last_oldid;
1413 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1414 /* Go to sleep until it's time to push this buffer */
1416 /* Recompute the highid */
1417 compute_high_id (mq);
1418 /* Recompute the high time */
1419 compute_high_time (mq);
1421 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1422 (mq->high_time == GST_CLOCK_TIME_NONE
1423 || next_time >= mq->high_time))
1424 || (!mq->sync_by_running_time && newid > mq->highid))
1425 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1427 GST_DEBUG_OBJECT (mq,
1428 "queue %d sleeping for not-linked wakeup with "
1429 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1430 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1431 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1433 /* Wake up all non-linked pads before we sleep */
1434 wake_up_next_non_linked (mq);
1437 g_cond_wait (&sq->turn, &mq->qlock);
1441 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1445 /* Recompute the high time */
1446 compute_high_time (mq);
1448 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1449 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1450 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1451 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1454 /* Re-compute the high_id in case someone else pushed */
1455 compute_high_id (mq);
1457 compute_high_id (mq);
1458 /* Wake up all non-linked pads */
1459 wake_up_next_non_linked (mq);
1461 /* We're done waiting, we can clear the nextid and nexttime */
1463 sq->next_time = GST_CLOCK_TIME_NONE;
1465 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1470 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1471 gst_flow_get_name (sq->srcresult));
1473 /* Update time stats */
1474 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1475 next_time = get_running_time (&sq->src_segment, object, FALSE);
1476 if (next_time != GST_CLOCK_TIME_NONE) {
1477 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1478 sq->last_time = next_time;
1479 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1480 /* Wake up all non-linked pads now that we advanced the high time */
1481 mq->high_time = next_time;
1482 wake_up_next_non_linked (mq);
1485 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1487 /* Try to push out the new object */
1488 result = gst_single_queue_push_one (mq, sq, object);
1491 /* Check if we pushed something already and if this is
1492 * now a switch from an active to a non-active stream.
1494 * If it is, we reset all the waiting streams, let them
1495 * push another buffer to see if they're now active again.
1496 * This allows faster switching between streams and prevents
1497 * deadlocks if downstream does any waiting too.
1499 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1500 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1501 && result == GST_FLOW_NOT_LINKED) {
1504 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1507 compute_high_id (mq);
1508 do_update_buffering = TRUE;
1510 /* maybe no-one is waiting */
1511 if (mq->numwaiting > 0) {
1512 /* Else figure out which singlequeue(s) need waking up */
1513 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1514 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1516 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1517 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1518 sq2->pushed = FALSE;
1519 sq2->srcresult = GST_FLOW_OK;
1520 g_cond_signal (&sq2->turn);
1528 sq->srcresult = result;
1529 sq->last_oldid = newid;
1531 if (do_update_buffering)
1532 update_buffering (mq, sq);
1534 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1535 gst_multi_queue_post_buffering (mq);
1537 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1538 && result != GST_FLOW_EOS)
1541 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1542 gst_flow_get_name (sq->srcresult));
1544 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1545 if (mq->numwaiting > 0 && sq->srcresult == GST_FLOW_EOS) {
1546 compute_high_time (mq);
1547 compute_high_id (mq);
1548 wake_up_next_non_linked (mq);
1550 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1557 gst_mini_object_unref (object);
1559 /* Need to make sure wake up any sleeping pads when we exit */
1560 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1561 compute_high_time (mq);
1562 compute_high_id (mq);
1563 wake_up_next_non_linked (mq);
1564 sq->last_query = FALSE;
1565 g_cond_signal (&sq->query_handled);
1567 /* Post an error message if we got EOS while downstream
1568 * has returned an error flow return. After EOS there
1569 * will be no further buffer which could propagate the
1571 if (sq->is_eos && sq->srcresult < GST_FLOW_EOS) {
1572 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1573 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
1574 ("Internal data stream error."),
1575 ("streaming stopped, reason %s", gst_flow_get_name (sq->srcresult)));
1577 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1580 /* upstream needs to see fatal result ASAP to shut things down,
1581 * but might be stuck in one of our other full queues;
1582 * so empty this one and trigger dynamic queue growth. At
1583 * this point the srcresult is not OK, NOT_LINKED
1584 * or EOS, i.e. a real failure */
1585 gst_single_queue_flush_queue (sq, FALSE);
1586 single_queue_underrun_cb (sq->queue, sq);
1587 gst_data_queue_set_flushing (sq->queue, TRUE);
1588 gst_pad_pause_task (sq->srcpad);
1589 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1590 "SingleQueue[%d] task paused, reason:%s",
1591 sq->id, gst_flow_get_name (sq->srcresult));
1597 * gst_multi_queue_chain:
1599 * This is similar to GstQueue's chain function, except:
1600 * _ we don't have leak behaviours,
1601 * _ we push with a unique id (curid)
1603 static GstFlowReturn
1604 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1608 GstMultiQueueItem *item;
1610 GstClockTime timestamp, duration;
1612 sq = gst_pad_get_element_private (pad);
1615 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1619 /* Get a unique incrementing id */
1620 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1622 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1623 sq->id, buffer, curid);
1625 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1627 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1628 duration = GST_BUFFER_DURATION (buffer);
1630 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1633 /* update time level, we must do this after pushing the data in the queue so
1634 * that we never end up filling the queue first. */
1635 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1638 return sq->srcresult;
1643 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1644 sq->id, gst_flow_get_name (sq->srcresult));
1645 gst_multi_queue_item_destroy (item);
1650 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
1651 gst_buffer_unref (buffer);
1652 return GST_FLOW_EOS;
1657 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
1658 GstPadMode mode, gboolean active)
1664 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1665 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1667 /* mq is NULL if the pad is activated/deactivated before being
1668 * added to the multiqueue */
1670 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1673 case GST_PAD_MODE_PUSH:
1675 /* All pads start off linked until they push one buffer */
1676 sq->srcresult = GST_FLOW_OK;
1678 gst_data_queue_set_flushing (sq->queue, FALSE);
1680 sq->srcresult = GST_FLOW_FLUSHING;
1681 sq->last_query = FALSE;
1682 g_cond_signal (&sq->query_handled);
1683 gst_data_queue_set_flushing (sq->queue, TRUE);
1685 /* Wait until streaming thread has finished */
1687 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1688 GST_PAD_STREAM_LOCK (pad);
1690 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1691 gst_data_queue_flush (sq->queue);
1693 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1694 GST_PAD_STREAM_UNLOCK (pad);
1696 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1706 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1707 gst_object_unref (mq);
1714 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1719 GstMultiQueueItem *item;
1722 GstEvent *sref = NULL;
1724 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1725 mq = (GstMultiQueue *) parent;
1727 type = GST_EVENT_TYPE (event);
1730 case GST_EVENT_FLUSH_START:
1731 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1734 res = gst_pad_push_event (sq->srcpad, event);
1736 gst_single_queue_flush (mq, sq, TRUE, FALSE);
1739 case GST_EVENT_FLUSH_STOP:
1740 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1743 res = gst_pad_push_event (sq->srcpad, event);
1745 gst_single_queue_flush (mq, sq, FALSE, FALSE);
1748 case GST_EVENT_SEGMENT:
1750 /* take ref because the queue will take ownership and we need the event
1751 * afterwards to update the segment */
1752 sref = gst_event_ref (event);
1756 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1757 res = gst_pad_push_event (sq->srcpad, event);
1763 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1767 /* Get an unique incrementing id. */
1768 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1770 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
1772 GST_DEBUG_OBJECT (mq,
1773 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1774 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1776 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1779 /* mark EOS when we received one, we must do that after putting the
1780 * buffer in the queue because EOS marks the buffer as filled. */
1783 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1786 /* Post an error message if we got EOS while downstream
1787 * has returned an error flow return. After EOS there
1788 * will be no further buffer which could propagate the
1790 if (sq->srcresult < GST_FLOW_EOS) {
1791 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1792 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
1793 ("Internal data stream error."),
1794 ("streaming stopped, reason %s",
1795 gst_flow_get_name (sq->srcresult)));
1797 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1800 /* EOS affects the buffering state */
1801 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1802 update_buffering (mq, sq);
1803 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1804 single_queue_overrun_cb (sq->queue, sq);
1805 gst_multi_queue_post_buffering (mq);
1807 case GST_EVENT_SEGMENT:
1808 apply_segment (mq, sq, sref, &sq->sink_segment);
1809 gst_event_unref (sref);
1812 apply_gap (mq, sq, sref, &sq->sink_segment);
1813 gst_event_unref (sref);
1822 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1823 sq->id, gst_flow_get_name (sq->srcresult));
1825 gst_event_unref (sref);
1826 gst_multi_queue_item_destroy (item);
1831 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1832 gst_event_unref (event);
1839 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1845 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1846 mq = (GstMultiQueue *) parent;
1848 switch (GST_QUERY_TYPE (query)) {
1850 if (GST_QUERY_IS_SERIALIZED (query)) {
1852 GstMultiQueueItem *item;
1854 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1855 if (sq->srcresult != GST_FLOW_OK)
1858 /* serialized events go in the queue. We need to be certain that we
1859 * don't cause deadlocks waiting for the query return value. We check if
1860 * the queue is empty (nothing is blocking downstream and the query can
1861 * be pushed for sure) or we are not buffering. If we are buffering,
1862 * the pipeline waits to unblock downstream until our queue fills up
1863 * completely, which can not happen if we block on the query..
1864 * Therefore we only potentially block when we are not buffering. */
1865 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
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 *) query, curid);
1871 GST_DEBUG_OBJECT (mq,
1872 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
1873 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
1874 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1875 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
1876 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1877 /* it might be that the query has been taken out of the queue
1878 * while we were unlocked. So, we need to check if the last
1879 * handled query is the same one than the one we just
1880 * pushed. If it is, we don't need to wait for the condition
1881 * variable, otherwise we wait for the condition variable to
1883 if (sq->last_handled_query != query)
1884 g_cond_wait (&sq->query_handled, &mq->qlock);
1885 res = sq->last_query;
1886 sq->last_handled_query = NULL;
1888 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
1889 "queue is not empty");
1892 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1894 /* default handling */
1895 res = gst_pad_query_default (pad, parent, query);
1903 GST_DEBUG_OBJECT (mq, "Flushing");
1904 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1910 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
1911 GstPadMode mode, gboolean active)
1917 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1920 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1923 case GST_PAD_MODE_PUSH:
1925 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
1927 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
1928 /* make sure streaming finishes */
1929 result |= gst_pad_stop_task (pad);
1940 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1942 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1943 GstMultiQueue *mq = sq->mqueue;
1946 switch (GST_EVENT_TYPE (event)) {
1947 case GST_EVENT_RECONFIGURE:
1948 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1949 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1950 sq->srcresult = GST_FLOW_OK;
1951 g_cond_signal (&sq->turn);
1953 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1955 ret = gst_pad_push_event (sq->sinkpad, event);
1958 ret = gst_pad_push_event (sq->sinkpad, event);
1966 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1970 /* FIXME, Handle position offset depending on queue size */
1971 switch (GST_QUERY_TYPE (query)) {
1973 /* default handling */
1974 res = gst_pad_query_default (pad, parent, query);
1981 * Next-non-linked functions
1984 /* WITH LOCK TAKEN */
1986 wake_up_next_non_linked (GstMultiQueue * mq)
1990 /* maybe no-one is waiting */
1991 if (mq->numwaiting < 1)
1994 /* Else figure out which singlequeue(s) need waking up */
1995 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1996 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1998 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1999 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
2000 && sq->next_time != GST_CLOCK_TIME_NONE
2001 && sq->next_time >= mq->high_time)
2002 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
2003 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2004 g_cond_signal (&sq->turn);
2010 /* WITH LOCK TAKEN */
2012 compute_high_id (GstMultiQueue * mq)
2014 /* The high-id is either the highest id among the linked pads, or if all
2015 * pads are not-linked, it's the lowest not-linked pad */
2017 guint32 lowest = G_MAXUINT32;
2018 guint32 highid = G_MAXUINT32;
2020 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2021 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2023 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2024 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2026 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2027 /* No need to consider queues which are not waiting */
2028 if (sq->nextid == 0) {
2029 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2033 if (sq->nextid < lowest)
2034 lowest = sq->nextid;
2035 } else if (sq->srcresult != GST_FLOW_EOS) {
2036 /* If we don't have a global highid, or the global highid is lower than
2037 * this single queue's last outputted id, store the queue's one,
2038 * unless the singlequeue is at EOS (srcresult = EOS) */
2039 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2044 if (highid == G_MAXUINT32 || lowest < highid)
2045 mq->highid = lowest;
2047 mq->highid = highid;
2049 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2053 /* WITH LOCK TAKEN */
2055 compute_high_time (GstMultiQueue * mq)
2057 /* The high-id is either the highest id among the linked pads, or if all
2058 * pads are not-linked, it's the lowest not-linked pad */
2060 GstClockTime highest = GST_CLOCK_TIME_NONE;
2061 GstClockTime lowest = GST_CLOCK_TIME_NONE;
2063 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2064 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2067 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
2068 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
2069 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
2071 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2072 /* No need to consider queues which are not waiting */
2073 if (sq->next_time == GST_CLOCK_TIME_NONE) {
2074 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2078 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
2079 lowest = sq->next_time;
2080 } else if (sq->srcresult != GST_FLOW_EOS) {
2081 /* If we don't have a global highid, or the global highid is lower than
2082 * this single queue's last outputted id, store the queue's one,
2083 * unless the singlequeue is at EOS (srcresult = EOS) */
2084 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
2085 highest = sq->last_time;
2089 mq->high_time = highest;
2092 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
2093 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
2096 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2097 ((q)->max_size.format) <= (value))
2100 * GstSingleQueue functions
2103 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2105 GstMultiQueue *mq = sq->mqueue;
2107 GstDataQueueSize size;
2108 gboolean filled = TRUE;
2109 gboolean all_not_linked = TRUE;
2110 gboolean empty_found = FALSE;
2112 gst_data_queue_get_level (sq->queue, &size);
2115 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2116 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2117 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2120 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2122 /* check if we reached the hard time/bytes limits */
2123 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
2124 IS_FILLED (sq, time, sq->cur_time)) {
2128 /* Search for empty or unlinked queues */
2129 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2130 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2135 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2136 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2140 all_not_linked = FALSE;
2141 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2142 if (gst_data_queue_is_empty (oq->queue)) {
2143 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2149 if (!mq->queues || !mq->queues->next)
2150 all_not_linked = FALSE;
2152 /* if hard limits are not reached then we allow one more buffer in the full
2153 * queue, but only if any of the other singelqueues are empty or all are
2155 if (all_not_linked || empty_found) {
2156 if (all_not_linked) {
2157 GST_LOG_OBJECT (mq, "All other queues are not linked");
2159 if (IS_FILLED (sq, visible, size.visible)) {
2160 sq->max_size.visible = size.visible + 1;
2161 GST_DEBUG_OBJECT (mq,
2162 "Bumping single queue %d max visible to %d",
2163 sq->id, sq->max_size.visible);
2169 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2171 /* Overrun is always forwarded, since this is blocking the upstream element */
2173 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2174 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2179 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2181 gboolean empty = TRUE;
2182 GstMultiQueue *mq = sq->mqueue;
2185 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2186 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2190 "Single Queue %d is empty, Checking other single queues", sq->id);
2193 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2194 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2195 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2197 if (gst_data_queue_is_full (oq->queue)) {
2198 GstDataQueueSize size;
2200 gst_data_queue_get_level (oq->queue, &size);
2201 if (IS_FILLED (oq, visible, size.visible)) {
2202 oq->max_size.visible = size.visible + 1;
2203 GST_DEBUG_OBJECT (mq,
2204 "queue %d is filled, bumping its max visible to %d", oq->id,
2205 oq->max_size.visible);
2206 gst_data_queue_limits_changed (oq->queue);
2209 if (!gst_data_queue_is_empty (oq->queue))
2212 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2215 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2216 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2221 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2222 guint64 time, GstSingleQueue * sq)
2225 GstMultiQueue *mq = sq->mqueue;
2227 GST_DEBUG_OBJECT (mq,
2228 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2229 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2230 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2232 /* we are always filled on EOS */
2236 /* we never go past the max visible items unless we are in buffering mode */
2237 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2240 /* check time or bytes */
2241 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
2247 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2249 GstDataQueueItem *sitem;
2250 GstMultiQueueItem *mitem;
2251 gboolean was_flushing = FALSE;
2253 while (!gst_data_queue_is_empty (sq->queue)) {
2254 GstMiniObject *data;
2256 /* FIXME: If this fails here although the queue is not empty,
2257 * we're flushing... but we want to rescue all sticky
2258 * events nonetheless.
2260 if (!gst_data_queue_pop (sq->queue, &sitem)) {
2261 was_flushing = TRUE;
2262 gst_data_queue_set_flushing (sq->queue, FALSE);
2266 mitem = (GstMultiQueueItem *) sitem;
2268 data = sitem->object;
2270 if (!full && !mitem->is_query && GST_IS_EVENT (data)
2271 && GST_EVENT_IS_STICKY (data)
2272 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2273 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2274 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2277 sitem->destroy (sitem);
2280 gst_data_queue_flush (sq->queue);
2282 gst_data_queue_set_flushing (sq->queue, TRUE);
2284 GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2285 update_buffering (sq->mqueue, sq);
2286 GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2287 gst_multi_queue_post_buffering (sq->mqueue);
2291 gst_single_queue_free (GstSingleQueue * sq)
2294 gst_data_queue_flush (sq->queue);
2295 g_object_unref (sq->queue);
2296 g_cond_clear (&sq->turn);
2297 g_cond_clear (&sq->query_handled);
2301 static GstSingleQueue *
2302 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2307 guint temp_id = (id == -1) ? 0 : id;
2309 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2311 /* Find an unused queue ID, if possible the passed one */
2312 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2313 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2314 /* This works because the IDs are sorted in ascending order */
2315 if (sq2->id == temp_id) {
2316 /* If this ID was requested by the caller return NULL,
2317 * otherwise just get us the next one */
2319 temp_id = sq2->id + 1;
2322 } else if (sq2->id > temp_id) {
2327 sq = g_new0 (GstSingleQueue, 1);
2331 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2332 mqueue->queues_cookie++;
2334 /* copy over max_size and extra_size so we don't need to take the lock
2335 * any longer when checking if the queue is full. */
2336 sq->max_size.visible = mqueue->max_size.visible;
2337 sq->max_size.bytes = mqueue->max_size.bytes;
2338 sq->max_size.time = mqueue->max_size.time;
2340 sq->extra_size.visible = mqueue->extra_size.visible;
2341 sq->extra_size.bytes = mqueue->extra_size.bytes;
2342 sq->extra_size.time = mqueue->extra_size.time;
2344 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2346 sq->mqueue = mqueue;
2347 sq->srcresult = GST_FLOW_FLUSHING;
2349 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2350 single_queue_check_full,
2351 (GstDataQueueFullCallback) single_queue_overrun_cb,
2352 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2354 sq->flushing = FALSE;
2355 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2356 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2360 sq->next_time = GST_CLOCK_TIME_NONE;
2361 sq->last_time = GST_CLOCK_TIME_NONE;
2362 g_cond_init (&sq->turn);
2363 g_cond_init (&sq->query_handled);
2365 sq->sinktime = GST_CLOCK_TIME_NONE;
2366 sq->srctime = GST_CLOCK_TIME_NONE;
2367 sq->sink_tainted = TRUE;
2368 sq->src_tainted = TRUE;
2370 name = g_strdup_printf ("sink_%u", sq->id);
2371 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
2374 gst_pad_set_chain_function (sq->sinkpad,
2375 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2376 gst_pad_set_activatemode_function (sq->sinkpad,
2377 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2378 gst_pad_set_event_function (sq->sinkpad,
2379 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2380 gst_pad_set_query_function (sq->sinkpad,
2381 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2382 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2383 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2384 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2386 name = g_strdup_printf ("src_%u", sq->id);
2387 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2390 gst_pad_set_activatemode_function (sq->srcpad,
2391 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2392 gst_pad_set_event_function (sq->srcpad,
2393 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2394 gst_pad_set_query_function (sq->srcpad,
2395 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2396 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2397 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2398 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2400 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2401 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2403 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2405 /* only activate the pads when we are not in the NULL state
2406 * and add the pad under the state_lock to prevend state changes
2407 * between activating and adding */
2408 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2409 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2410 gst_pad_set_active (sq->srcpad, TRUE);
2411 gst_pad_set_active (sq->sinkpad, TRUE);
2413 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2414 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2415 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2417 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",