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 */
177 /* Extension of GstDataQueueItem structure for our usage */
178 typedef struct _GstMultiQueueItem GstMultiQueueItem;
180 struct _GstMultiQueueItem
182 GstMiniObject *object;
187 GDestroyNotify destroy;
193 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
194 static void gst_single_queue_free (GstSingleQueue * squeue);
196 static void wake_up_next_non_linked (GstMultiQueue * mq);
197 static void compute_high_id (GstMultiQueue * mq);
198 static void compute_high_time (GstMultiQueue * mq);
199 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
200 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
202 static void update_buffering (GstMultiQueue * mq, GstSingleQueue * sq);
203 static void gst_multi_queue_post_buffering (GstMultiQueue * mq);
205 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
207 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
210 GST_STATIC_CAPS_ANY);
212 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
215 GST_STATIC_CAPS_ANY);
217 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
218 #define GST_CAT_DEFAULT (multi_queue_debug)
220 /* Signals and args */
228 /* default limits, we try to keep up to 2 seconds of data and if there is not
229 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
230 * there is data in the queues. Normally, the byte and time limits are not hit
231 * in theses conditions. */
232 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
233 #define DEFAULT_MAX_SIZE_BUFFERS 5
234 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
236 /* second limits. When we hit one of the above limits we are probably dealing
237 * with a badly muxed file and we scale the limits to these emergency values.
238 * This is currently not yet implemented.
239 * Since we dynamically scale the queue buffer size up to the limits but avoid
240 * going above the max-size-buffers when we can, we don't really need this
241 * aditional extra size. */
242 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
243 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
244 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
246 #define DEFAULT_USE_BUFFERING FALSE
247 #define DEFAULT_LOW_PERCENT 10
248 #define DEFAULT_HIGH_PERCENT 99
249 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
254 PROP_EXTRA_SIZE_BYTES,
255 PROP_EXTRA_SIZE_BUFFERS,
256 PROP_EXTRA_SIZE_TIME,
258 PROP_MAX_SIZE_BUFFERS,
263 PROP_SYNC_BY_RUNNING_TIME,
267 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
268 g_mutex_lock (&q->qlock); \
271 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
272 g_mutex_unlock (&q->qlock); \
275 #define SET_PERCENT(mq, perc) G_STMT_START { \
276 if (perc != mq->percent) { \
277 mq->percent = perc; \
278 mq->percent_changed = TRUE; \
279 GST_DEBUG_OBJECT (mq, "buffering %d percent", perc); \
283 static void gst_multi_queue_finalize (GObject * object);
284 static void gst_multi_queue_set_property (GObject * object,
285 guint prop_id, const GValue * value, GParamSpec * pspec);
286 static void gst_multi_queue_get_property (GObject * object,
287 guint prop_id, GValue * value, GParamSpec * pspec);
289 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
290 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
291 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
292 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
293 element, GstStateChange transition);
295 static void gst_multi_queue_loop (GstPad * pad);
298 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
299 #define gst_multi_queue_parent_class parent_class
300 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
303 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
306 gst_multi_queue_class_init (GstMultiQueueClass * klass)
308 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
309 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
311 gobject_class->set_property = gst_multi_queue_set_property;
312 gobject_class->get_property = gst_multi_queue_get_property;
317 * GstMultiQueue::underrun:
318 * @multiqueue: the multiqueue instance
320 * This signal is emitted from the streaming thread when there is
321 * no data in any of the queues inside the multiqueue instance (underrun).
323 * This indicates either starvation or EOS from the upstream data sources.
325 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
326 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
327 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
328 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
331 * GstMultiQueue::overrun:
332 * @multiqueue: the multiqueue instance
334 * Reports that one of the queues in the multiqueue is full (overrun).
335 * A queue is full if the total amount of data inside it (num-buffers, time,
336 * size) is higher than the boundary values which can be set through the
337 * GObject properties.
339 * This can be used as an indicator of pre-roll.
341 gst_multi_queue_signals[SIGNAL_OVERRUN] =
342 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
343 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
344 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
348 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
349 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
350 "Max. amount of data in the queue (bytes, 0=disable)",
351 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
352 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
353 G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
355 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
356 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
357 DEFAULT_MAX_SIZE_BUFFERS,
358 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
359 G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
361 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
362 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
363 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
364 G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
367 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
368 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
369 " (NOT IMPLEMENTED)",
370 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
373 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
374 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
375 " (NOT IMPLEMENTED)",
376 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
377 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
378 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
379 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
380 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
381 " (NOT IMPLEMENTED)",
382 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
383 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 * GstMultiQueue:use-buffering
388 * Enable the buffering option in multiqueue so that BUFFERING messages are
389 * emitted based on low-/high-percent thresholds.
391 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
392 g_param_spec_boolean ("use-buffering", "Use buffering",
393 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
394 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
395 G_PARAM_STATIC_STRINGS));
397 * GstMultiQueue:low-percent
399 * Low threshold percent for buffering to start.
401 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
402 g_param_spec_int ("low-percent", "Low percent",
403 "Low threshold for buffering to start", 0, 100,
404 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 * GstMultiQueue:high-percent
408 * High threshold percent for buffering to finish.
410 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
411 g_param_spec_int ("high-percent", "High percent",
412 "High threshold for buffering to finish", 0, 100,
413 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416 * GstMultiQueue:sync-by-running-time
418 * If enabled multiqueue will synchronize deactivated or not-linked streams
419 * to the activated and linked streams by taking the running time.
420 * Otherwise multiqueue will synchronize the deactivated or not-linked
421 * streams by keeping the order in which buffers and events arrived compared
422 * to active and linked streams.
424 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
425 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
426 "Synchronize deactivated or not-linked streams by running time",
427 DEFAULT_SYNC_BY_RUNNING_TIME,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 gobject_class->finalize = gst_multi_queue_finalize;
432 gst_element_class_set_static_metadata (gstelement_class,
434 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
435 gst_element_class_add_pad_template (gstelement_class,
436 gst_static_pad_template_get (&sinktemplate));
437 gst_element_class_add_pad_template (gstelement_class,
438 gst_static_pad_template_get (&srctemplate));
440 gstelement_class->request_new_pad =
441 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
442 gstelement_class->release_pad =
443 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
444 gstelement_class->change_state =
445 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
449 gst_multi_queue_init (GstMultiQueue * mqueue)
451 mqueue->nbqueues = 0;
452 mqueue->queues = NULL;
454 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
455 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
456 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
458 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
459 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
460 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
462 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
463 mqueue->low_percent = DEFAULT_LOW_PERCENT;
464 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
466 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
470 mqueue->high_time = GST_CLOCK_TIME_NONE;
472 g_mutex_init (&mqueue->qlock);
473 g_mutex_init (&mqueue->buffering_post_lock);
477 gst_multi_queue_finalize (GObject * object)
479 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
481 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
482 g_list_free (mqueue->queues);
483 mqueue->queues = NULL;
484 mqueue->queues_cookie++;
486 /* free/unref instance data */
487 g_mutex_clear (&mqueue->qlock);
488 g_mutex_clear (&mqueue->buffering_post_lock);
490 G_OBJECT_CLASS (parent_class)->finalize (object);
493 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
494 GList * tmp = mq->queues; \
496 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
497 q->max_size.format = mq->max_size.format; \
498 update_buffering (mq, q); \
499 gst_data_queue_limits_changed (q->queue); \
500 tmp = g_list_next(tmp); \
505 gst_multi_queue_set_property (GObject * object, guint prop_id,
506 const GValue * value, GParamSpec * pspec)
508 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
511 case PROP_MAX_SIZE_BYTES:
512 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
513 mq->max_size.bytes = g_value_get_uint (value);
514 SET_CHILD_PROPERTY (mq, bytes);
515 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
516 gst_multi_queue_post_buffering (mq);
518 case PROP_MAX_SIZE_BUFFERS:
521 gint new_size = g_value_get_uint (value);
523 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
525 mq->max_size.visible = new_size;
529 GstDataQueueSize size;
530 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
531 gst_data_queue_get_level (q->queue, &size);
533 GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
534 " current: %d, current max %d", q->id, new_size, size.visible,
535 q->max_size.visible);
537 /* do not reduce max size below current level if the single queue
538 * has grown because of empty queue */
540 q->max_size.visible = new_size;
541 } else if (q->max_size.visible == 0) {
542 q->max_size.visible = MAX (new_size, size.visible);
543 } else if (new_size > size.visible) {
544 q->max_size.visible = new_size;
546 update_buffering (mq, q);
547 gst_data_queue_limits_changed (q->queue);
548 tmp = g_list_next (tmp);
551 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
552 gst_multi_queue_post_buffering (mq);
556 case PROP_MAX_SIZE_TIME:
557 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
558 mq->max_size.time = g_value_get_uint64 (value);
559 SET_CHILD_PROPERTY (mq, time);
560 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
561 gst_multi_queue_post_buffering (mq);
563 case PROP_EXTRA_SIZE_BYTES:
564 mq->extra_size.bytes = g_value_get_uint (value);
566 case PROP_EXTRA_SIZE_BUFFERS:
567 mq->extra_size.visible = g_value_get_uint (value);
569 case PROP_EXTRA_SIZE_TIME:
570 mq->extra_size.time = g_value_get_uint64 (value);
572 case PROP_USE_BUFFERING:
573 mq->use_buffering = g_value_get_boolean (value);
574 if (!mq->use_buffering && mq->buffering) {
575 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
576 mq->buffering = FALSE;
577 GST_DEBUG_OBJECT (mq, "buffering 100 percent");
578 SET_PERCENT (mq, 100);
579 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
582 if (mq->use_buffering) {
585 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
587 mq->buffering = TRUE;
590 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
591 update_buffering (mq, q);
592 gst_data_queue_limits_changed (q->queue);
593 tmp = g_list_next (tmp);
596 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
598 gst_multi_queue_post_buffering (mq);
600 case PROP_LOW_PERCENT:
601 mq->low_percent = g_value_get_int (value);
603 case PROP_HIGH_PERCENT:
604 mq->high_percent = g_value_get_int (value);
606 case PROP_SYNC_BY_RUNNING_TIME:
607 mq->sync_by_running_time = g_value_get_boolean (value);
610 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
616 gst_multi_queue_get_property (GObject * object, guint prop_id,
617 GValue * value, GParamSpec * pspec)
619 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
621 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
624 case PROP_EXTRA_SIZE_BYTES:
625 g_value_set_uint (value, mq->extra_size.bytes);
627 case PROP_EXTRA_SIZE_BUFFERS:
628 g_value_set_uint (value, mq->extra_size.visible);
630 case PROP_EXTRA_SIZE_TIME:
631 g_value_set_uint64 (value, mq->extra_size.time);
633 case PROP_MAX_SIZE_BYTES:
634 g_value_set_uint (value, mq->max_size.bytes);
636 case PROP_MAX_SIZE_BUFFERS:
637 g_value_set_uint (value, mq->max_size.visible);
639 case PROP_MAX_SIZE_TIME:
640 g_value_set_uint64 (value, mq->max_size.time);
642 case PROP_USE_BUFFERING:
643 g_value_set_boolean (value, mq->use_buffering);
645 case PROP_LOW_PERCENT:
646 g_value_set_int (value, mq->low_percent);
648 case PROP_HIGH_PERCENT:
649 g_value_set_int (value, mq->high_percent);
651 case PROP_SYNC_BY_RUNNING_TIME:
652 g_value_set_boolean (value, mq->sync_by_running_time);
655 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
659 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
663 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
665 GstIterator *it = NULL;
667 GstSingleQueue *squeue;
668 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
671 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
672 squeue = gst_pad_get_element_private (pad);
676 if (squeue->sinkpad == pad)
677 opad = gst_object_ref (squeue->srcpad);
678 else if (squeue->srcpad == pad)
679 opad = gst_object_ref (squeue->sinkpad);
683 g_value_init (&val, GST_TYPE_PAD);
684 g_value_set_object (&val, opad);
685 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
686 g_value_unset (&val);
688 gst_object_unref (opad);
691 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
702 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
703 const gchar * name, const GstCaps * caps)
705 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
706 GstSingleQueue *squeue;
710 sscanf (name + 4, "_%u", &temp_id);
711 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
714 /* Create a new single queue, add the sink and source pad and return the sink pad */
715 squeue = gst_single_queue_new (mqueue, temp_id);
717 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
718 GST_DEBUG_PAD_NAME (squeue->sinkpad));
720 return squeue ? squeue->sinkpad : NULL;
724 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
726 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
727 GstSingleQueue *sq = NULL;
730 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
732 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
733 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
734 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
735 sq = (GstSingleQueue *) tmp->data;
737 if (sq->sinkpad == pad)
742 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
743 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
747 /* FIXME: The removal of the singlequeue should probably not happen until it
748 * finishes draining */
750 /* remove it from the list */
751 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
752 mqueue->queues_cookie++;
754 /* FIXME : recompute next-non-linked */
755 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
757 /* delete SingleQueue */
758 gst_data_queue_set_flushing (sq->queue, TRUE);
760 gst_pad_set_active (sq->srcpad, FALSE);
761 gst_pad_set_active (sq->sinkpad, FALSE);
762 gst_pad_set_element_private (sq->srcpad, NULL);
763 gst_pad_set_element_private (sq->sinkpad, NULL);
764 gst_element_remove_pad (element, sq->srcpad);
765 gst_element_remove_pad (element, sq->sinkpad);
766 gst_single_queue_free (sq);
769 static GstStateChangeReturn
770 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
772 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
773 GstSingleQueue *sq = NULL;
774 GstStateChangeReturn result;
776 switch (transition) {
777 case GST_STATE_CHANGE_READY_TO_PAUSED:{
780 /* Set all pads to non-flushing */
781 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
782 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
783 sq = (GstSingleQueue *) tmp->data;
784 sq->flushing = FALSE;
787 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
788 SET_CHILD_PROPERTY (mqueue, visible);
790 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
791 gst_multi_queue_post_buffering (mqueue);
795 case GST_STATE_CHANGE_PAUSED_TO_READY:{
798 /* Un-wait all waiting pads */
799 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
800 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
801 sq = (GstSingleQueue *) tmp->data;
803 g_cond_signal (&sq->turn);
805 sq->last_query = FALSE;
806 g_cond_signal (&sq->query_handled);
808 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
815 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
817 switch (transition) {
826 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
831 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
835 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
836 sq->srcresult = GST_FLOW_FLUSHING;
837 gst_data_queue_set_flushing (sq->queue, TRUE);
841 /* wake up non-linked task */
842 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
844 g_cond_signal (&sq->turn);
845 sq->last_query = FALSE;
846 g_cond_signal (&sq->query_handled);
847 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
849 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
850 result = gst_pad_pause_task (sq->srcpad);
851 sq->sink_tainted = sq->src_tainted = TRUE;
853 gst_single_queue_flush_queue (sq, full);
855 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
856 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
857 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
858 sq->has_src_segment = FALSE;
859 /* All pads start off not-linked for a smooth kick-off */
860 sq->srcresult = GST_FLOW_OK;
863 sq->max_size.visible = mq->max_size.visible;
867 sq->last_oldid = G_MAXUINT32;
868 sq->next_time = GST_CLOCK_TIME_NONE;
869 sq->last_time = GST_CLOCK_TIME_NONE;
870 gst_data_queue_set_flushing (sq->queue, FALSE);
872 /* Reset high time to be recomputed next */
873 mq->high_time = GST_CLOCK_TIME_NONE;
875 sq->flushing = FALSE;
876 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
878 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
880 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
886 /* WITH LOCK TAKEN */
888 get_percentage (GstSingleQueue * sq)
890 GstDataQueueSize size;
893 gst_data_queue_get_level (sq->queue, &size);
895 GST_DEBUG_OBJECT (sq->mqueue,
896 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
897 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
898 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
900 /* get bytes and time percentages and take the max */
901 if (sq->is_eos || sq->srcresult == GST_FLOW_NOT_LINKED) {
905 if (sq->max_size.time > 0) {
906 tmp = (sq->cur_time * 100) / sq->max_size.time;
907 percent = MAX (percent, tmp);
909 if (sq->max_size.bytes > 0) {
910 tmp = (size.bytes * 100) / sq->max_size.bytes;
911 percent = MAX (percent, tmp);
918 /* WITH LOCK TAKEN */
920 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
924 /* nothing to dowhen we are not in buffering mode */
925 if (!mq->use_buffering)
928 percent = get_percentage (sq);
931 if (percent >= mq->high_percent) {
932 mq->buffering = FALSE;
934 /* make sure it increases */
935 percent = MAX (mq->percent, percent);
937 SET_PERCENT (mq, percent);
940 gboolean is_buffering = TRUE;
942 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
943 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
945 if (get_percentage (oq) >= 100) {
946 is_buffering = FALSE;
952 if (is_buffering && percent < mq->low_percent) {
953 mq->buffering = TRUE;
954 SET_PERCENT (mq, percent);
960 gst_multi_queue_post_buffering (GstMultiQueue * mq)
962 GstMessage *msg = NULL;
964 g_mutex_lock (&mq->buffering_post_lock);
965 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
966 if (mq->percent_changed) {
967 gint percent = mq->percent;
969 mq->percent_changed = FALSE;
971 percent = percent * 100 / mq->high_percent;
976 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
977 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
979 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
982 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
984 g_mutex_unlock (&mq->buffering_post_lock);
987 /* calculate the diff between running time on the sink and src of the queue.
988 * This is the total amount of time in the queue.
991 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
993 gint64 sink_time, src_time;
995 if (sq->sink_tainted) {
996 sink_time = sq->sinktime =
997 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
998 sq->sink_segment.position);
1000 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
1001 /* if we have a time, we become untainted and use the time */
1002 sq->sink_tainted = FALSE;
1004 sink_time = sq->sinktime;
1006 if (sq->src_tainted) {
1007 GstSegment *segment;
1010 if (sq->has_src_segment) {
1011 segment = &sq->src_segment;
1012 position = sq->src_segment.position;
1015 * If the src pad had no segment yet, use the sink segment
1016 * to avoid signalling overrun if the received sink segment has a
1017 * a position > max-size-time while the src pad time would be the default=0
1019 * This can happen when switching pads on chained/adaptive streams and the
1020 * new chain has a segment with a much larger position
1022 segment = &sq->sink_segment;
1023 position = sq->sink_segment.position;
1026 src_time = sq->srctime =
1027 gst_segment_to_running_time (segment, GST_FORMAT_TIME, position);
1028 /* if we have a time, we become untainted and use the time */
1029 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
1030 sq->src_tainted = FALSE;
1032 src_time = sq->srctime;
1034 GST_DEBUG_OBJECT (mq,
1035 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
1036 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
1038 /* This allows for streams with out of order timestamping - sometimes the
1039 * emerging timestamp is later than the arriving one(s) */
1040 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
1041 sq->cur_time = sink_time - src_time;
1045 /* updating the time level can change the buffering state */
1046 update_buffering (mq, sq);
1051 /* take a SEGMENT event and apply the values to segment, updating the time
1052 * level of queue. */
1054 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1055 GstSegment * segment)
1057 gst_event_copy_segment (event, segment);
1059 /* now configure the values, we use these to track timestamps on the
1061 if (segment->format != GST_FORMAT_TIME) {
1062 /* non-time format, pretent the current time segment is closed with a
1063 * 0 start and unknown stop time. */
1064 segment->format = GST_FORMAT_TIME;
1069 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1071 if (segment == &sq->sink_segment)
1072 sq->sink_tainted = TRUE;
1074 sq->has_src_segment = TRUE;
1075 sq->src_tainted = TRUE;
1078 GST_DEBUG_OBJECT (mq,
1079 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1081 /* segment can update the time level of the queue */
1082 update_time_level (mq, sq);
1084 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1085 gst_multi_queue_post_buffering (mq);
1088 /* take a buffer and update segment, updating the time level of the queue. */
1090 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1091 GstClockTime duration, GstSegment * segment)
1093 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1095 /* if no timestamp is set, assume it's continuous with the previous
1097 if (timestamp == GST_CLOCK_TIME_NONE)
1098 timestamp = segment->position;
1101 if (duration != GST_CLOCK_TIME_NONE)
1102 timestamp += duration;
1104 GST_DEBUG_OBJECT (mq, "queue %d, position updated to %" GST_TIME_FORMAT,
1105 sq->id, GST_TIME_ARGS (timestamp));
1107 segment->position = timestamp;
1109 if (segment == &sq->sink_segment)
1110 sq->sink_tainted = TRUE;
1112 sq->src_tainted = TRUE;
1114 /* calc diff with other end */
1115 update_time_level (mq, sq);
1116 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1117 gst_multi_queue_post_buffering (mq);
1121 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1122 GstSegment * segment)
1124 GstClockTime timestamp;
1125 GstClockTime duration;
1127 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1129 gst_event_parse_gap (event, ×tamp, &duration);
1131 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1133 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1134 timestamp += duration;
1137 segment->position = timestamp;
1139 if (segment == &sq->sink_segment)
1140 sq->sink_tainted = TRUE;
1142 sq->src_tainted = TRUE;
1144 /* calc diff with other end */
1145 update_time_level (mq, sq);
1148 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1149 gst_multi_queue_post_buffering (mq);
1153 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1155 GstClockTime time = GST_CLOCK_TIME_NONE;
1157 if (GST_IS_BUFFER (object)) {
1158 GstBuffer *buf = GST_BUFFER_CAST (object);
1160 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1161 time = GST_BUFFER_TIMESTAMP (buf);
1162 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1163 time += GST_BUFFER_DURATION (buf);
1164 if (time > segment->stop)
1165 time = segment->stop;
1166 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1168 } else if (GST_IS_BUFFER_LIST (object)) {
1169 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1173 n = gst_buffer_list_length (list);
1174 for (i = 0; i < n; i++) {
1175 buf = gst_buffer_list_get (list, i);
1176 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1177 time = GST_BUFFER_TIMESTAMP (buf);
1178 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1179 time += GST_BUFFER_DURATION (buf);
1180 if (time > segment->stop)
1181 time = segment->stop;
1182 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1189 } else if (GST_IS_EVENT (object)) {
1190 GstEvent *event = GST_EVENT_CAST (object);
1192 /* For newsegment events return the running time of the start position */
1193 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1194 const GstSegment *new_segment;
1196 gst_event_parse_segment (event, &new_segment);
1197 if (new_segment->format == GST_FORMAT_TIME) {
1199 gst_segment_to_running_time (new_segment, GST_FORMAT_TIME,
1200 new_segment->start);
1209 static GstFlowReturn
1210 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1211 GstMiniObject * object)
1213 GstFlowReturn result = sq->srcresult;
1215 if (GST_IS_BUFFER (object)) {
1217 GstClockTime timestamp, duration;
1219 buffer = GST_BUFFER_CAST (object);
1220 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1221 duration = GST_BUFFER_DURATION (buffer);
1223 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1225 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1226 gst_data_queue_limits_changed (sq->queue);
1228 GST_DEBUG_OBJECT (mq,
1229 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1230 sq->id, buffer, GST_TIME_ARGS (timestamp));
1232 result = gst_pad_push (sq->srcpad, buffer);
1233 } else if (GST_IS_EVENT (object)) {
1236 event = GST_EVENT_CAST (object);
1238 switch (GST_EVENT_TYPE (event)) {
1240 result = GST_FLOW_EOS;
1242 case GST_EVENT_SEGMENT:
1243 apply_segment (mq, sq, event, &sq->src_segment);
1244 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1245 gst_data_queue_limits_changed (sq->queue);
1248 apply_gap (mq, sq, event, &sq->src_segment);
1249 /* Applying the gap may have made the queue non-full again, unblock it if needed */
1250 gst_data_queue_limits_changed (sq->queue);
1256 GST_DEBUG_OBJECT (mq,
1257 "SingleQueue %d : Pushing event %p of type %s",
1258 sq->id, event, GST_EVENT_TYPE_NAME (event));
1260 gst_pad_push_event (sq->srcpad, event);
1261 } else if (GST_IS_QUERY (object)) {
1265 query = GST_QUERY_CAST (object);
1267 res = gst_pad_peer_query (sq->srcpad, query);
1269 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1270 sq->last_query = res;
1271 g_cond_signal (&sq->query_handled);
1272 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1274 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1282 static GstMiniObject *
1283 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1288 item->object = NULL;
1294 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1296 if (!item->is_query && item->object)
1297 gst_mini_object_unref (item->object);
1298 g_slice_free (GstMultiQueueItem, item);
1301 /* takes ownership of passed mini object! */
1302 static GstMultiQueueItem *
1303 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1305 GstMultiQueueItem *item;
1307 item = g_slice_new (GstMultiQueueItem);
1308 item->object = object;
1309 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1310 item->posid = curid;
1311 item->is_query = GST_IS_QUERY (object);
1313 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1314 item->duration = GST_BUFFER_DURATION (object);
1315 if (item->duration == GST_CLOCK_TIME_NONE)
1317 item->visible = TRUE;
1321 static GstMultiQueueItem *
1322 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1324 GstMultiQueueItem *item;
1326 item = g_slice_new (GstMultiQueueItem);
1327 item->object = object;
1328 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1329 item->posid = curid;
1330 item->is_query = GST_IS_QUERY (object);
1334 item->visible = FALSE;
1338 /* Each main loop attempts to push buffers until the return value
1339 * is not-linked. not-linked pads are not allowed to push data beyond
1340 * any linked pads, so they don't 'rush ahead of the pack'.
1343 gst_multi_queue_loop (GstPad * pad)
1346 GstMultiQueueItem *item;
1347 GstDataQueueItem *sitem;
1349 GstMiniObject *object = NULL;
1351 GstFlowReturn result;
1352 GstClockTime next_time;
1354 gboolean do_update_buffering = FALSE;
1356 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1359 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1364 /* Get something from the queue, blocking until that happens, or we get
1366 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1369 item = (GstMultiQueueItem *) sitem;
1370 newid = item->posid;
1372 /* steal the object and destroy the item */
1373 object = gst_multi_queue_item_steal_object (item);
1374 gst_multi_queue_item_destroy (item);
1376 is_buffer = GST_IS_BUFFER (object);
1378 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1379 next_time = get_running_time (&sq->src_segment, object, TRUE);
1381 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1382 sq->id, newid, sq->last_oldid);
1384 /* If we're not-linked, we do some extra work because we might need to
1385 * wait before pushing. If we're linked but there's a gap in the IDs,
1386 * or it's the first loop, or we just passed the previous highid,
1387 * we might need to wake some sleeping pad up, so there's extra work
1389 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1390 if (sq->srcresult == GST_FLOW_NOT_LINKED
1391 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1392 || sq->last_oldid > mq->highid) {
1393 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1394 gst_flow_get_name (sq->srcresult));
1396 /* Check again if we're flushing after the lock is taken,
1397 * the flush flag might have been changed in the meantime */
1399 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1403 /* Update the nextid so other threads know when to wake us up */
1405 sq->next_time = next_time;
1407 /* Update the oldid (the last ID we output) for highid tracking */
1408 if (sq->last_oldid != G_MAXUINT32)
1409 sq->oldid = sq->last_oldid;
1411 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1412 /* Go to sleep until it's time to push this buffer */
1414 /* Recompute the highid */
1415 compute_high_id (mq);
1416 /* Recompute the high time */
1417 compute_high_time (mq);
1419 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1420 (mq->high_time == GST_CLOCK_TIME_NONE
1421 || next_time >= mq->high_time))
1422 || (!mq->sync_by_running_time && newid > mq->highid))
1423 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1425 GST_DEBUG_OBJECT (mq,
1426 "queue %d sleeping for not-linked wakeup with "
1427 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1428 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1429 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1431 /* Wake up all non-linked pads before we sleep */
1432 wake_up_next_non_linked (mq);
1435 g_cond_wait (&sq->turn, &mq->qlock);
1439 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1443 /* Recompute the high time */
1444 compute_high_time (mq);
1446 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1447 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1448 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1449 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1452 /* Re-compute the high_id in case someone else pushed */
1453 compute_high_id (mq);
1455 compute_high_id (mq);
1456 /* Wake up all non-linked pads */
1457 wake_up_next_non_linked (mq);
1459 /* We're done waiting, we can clear the nextid and nexttime */
1461 sq->next_time = GST_CLOCK_TIME_NONE;
1463 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1468 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1469 gst_flow_get_name (sq->srcresult));
1471 /* Update time stats */
1472 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1473 next_time = get_running_time (&sq->src_segment, object, FALSE);
1474 if (next_time != GST_CLOCK_TIME_NONE) {
1475 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1476 sq->last_time = next_time;
1477 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1478 /* Wake up all non-linked pads now that we advanced the high time */
1479 mq->high_time = next_time;
1480 wake_up_next_non_linked (mq);
1483 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1485 /* Try to push out the new object */
1486 result = gst_single_queue_push_one (mq, sq, object);
1489 /* Check if we pushed something already and if this is
1490 * now a switch from an active to a non-active stream.
1492 * If it is, we reset all the waiting streams, let them
1493 * push another buffer to see if they're now active again.
1494 * This allows faster switching between streams and prevents
1495 * deadlocks if downstream does any waiting too.
1497 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1498 if (sq->pushed && sq->srcresult == GST_FLOW_OK
1499 && result == GST_FLOW_NOT_LINKED) {
1502 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1505 compute_high_id (mq);
1506 do_update_buffering = TRUE;
1508 /* maybe no-one is waiting */
1509 if (mq->numwaiting > 0) {
1510 /* Else figure out which singlequeue(s) need waking up */
1511 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1512 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1514 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1515 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1516 sq2->pushed = FALSE;
1517 sq2->srcresult = GST_FLOW_OK;
1518 g_cond_signal (&sq2->turn);
1526 sq->srcresult = result;
1527 sq->last_oldid = newid;
1529 if (do_update_buffering)
1530 update_buffering (mq, sq);
1532 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1533 gst_multi_queue_post_buffering (mq);
1535 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1536 && result != GST_FLOW_EOS)
1539 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1540 gst_flow_get_name (sq->srcresult));
1547 gst_mini_object_unref (object);
1549 /* Need to make sure wake up any sleeping pads when we exit */
1550 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1551 compute_high_time (mq);
1552 compute_high_id (mq);
1553 wake_up_next_non_linked (mq);
1554 sq->last_query = FALSE;
1555 g_cond_signal (&sq->query_handled);
1557 /* Post an error message if we got EOS while downstream
1558 * has returned an error flow return. After EOS there
1559 * will be no further buffer which could propagate the
1561 if (sq->is_eos && sq->srcresult < GST_FLOW_EOS) {
1562 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1563 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
1564 ("Internal data stream error."),
1565 ("streaming stopped, reason %s", gst_flow_get_name (sq->srcresult)));
1567 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1570 /* upstream needs to see fatal result ASAP to shut things down,
1571 * but might be stuck in one of our other full queues;
1572 * so empty this one and trigger dynamic queue growth. At
1573 * this point the srcresult is not OK, NOT_LINKED
1574 * or EOS, i.e. a real failure */
1575 gst_single_queue_flush_queue (sq, FALSE);
1576 single_queue_underrun_cb (sq->queue, sq);
1577 gst_data_queue_set_flushing (sq->queue, TRUE);
1578 gst_pad_pause_task (sq->srcpad);
1579 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1580 "SingleQueue[%d] task paused, reason:%s",
1581 sq->id, gst_flow_get_name (sq->srcresult));
1587 * gst_multi_queue_chain:
1589 * This is similar to GstQueue's chain function, except:
1590 * _ we don't have leak behaviours,
1591 * _ we push with a unique id (curid)
1593 static GstFlowReturn
1594 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1598 GstMultiQueueItem *item;
1600 GstClockTime timestamp, duration;
1602 sq = gst_pad_get_element_private (pad);
1605 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1609 /* Get a unique incrementing id */
1610 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1612 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1613 sq->id, buffer, curid);
1615 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1617 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1618 duration = GST_BUFFER_DURATION (buffer);
1620 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1623 /* update time level, we must do this after pushing the data in the queue so
1624 * that we never end up filling the queue first. */
1625 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1628 return sq->srcresult;
1633 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1634 sq->id, gst_flow_get_name (sq->srcresult));
1635 gst_multi_queue_item_destroy (item);
1640 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
1641 gst_buffer_unref (buffer);
1642 return GST_FLOW_EOS;
1647 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
1648 GstPadMode mode, gboolean active)
1654 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1655 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1657 /* mq is NULL if the pad is activated/deactivated before being
1658 * added to the multiqueue */
1660 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1663 case GST_PAD_MODE_PUSH:
1665 /* All pads start off linked until they push one buffer */
1666 sq->srcresult = GST_FLOW_OK;
1668 gst_data_queue_set_flushing (sq->queue, FALSE);
1670 sq->srcresult = GST_FLOW_FLUSHING;
1671 sq->last_query = FALSE;
1672 g_cond_signal (&sq->query_handled);
1673 gst_data_queue_set_flushing (sq->queue, TRUE);
1675 /* Wait until streaming thread has finished */
1677 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1678 GST_PAD_STREAM_LOCK (pad);
1680 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1681 gst_data_queue_flush (sq->queue);
1683 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1684 GST_PAD_STREAM_UNLOCK (pad);
1686 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1696 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1697 gst_object_unref (mq);
1704 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1709 GstMultiQueueItem *item;
1712 GstEvent *sref = NULL;
1714 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1715 mq = (GstMultiQueue *) parent;
1717 type = GST_EVENT_TYPE (event);
1720 case GST_EVENT_FLUSH_START:
1721 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1724 res = gst_pad_push_event (sq->srcpad, event);
1726 gst_single_queue_flush (mq, sq, TRUE, FALSE);
1729 case GST_EVENT_FLUSH_STOP:
1730 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1733 res = gst_pad_push_event (sq->srcpad, event);
1735 gst_single_queue_flush (mq, sq, FALSE, FALSE);
1738 case GST_EVENT_SEGMENT:
1740 /* take ref because the queue will take ownership and we need the event
1741 * afterwards to update the segment */
1742 sref = gst_event_ref (event);
1746 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1747 res = gst_pad_push_event (sq->srcpad, event);
1753 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1757 /* Get an unique incrementing id. */
1758 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1760 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
1762 GST_DEBUG_OBJECT (mq,
1763 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1764 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1766 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1769 /* mark EOS when we received one, we must do that after putting the
1770 * buffer in the queue because EOS marks the buffer as filled. */
1773 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1776 /* Post an error message if we got EOS while downstream
1777 * has returned an error flow return. After EOS there
1778 * will be no further buffer which could propagate the
1780 if (sq->srcresult < GST_FLOW_EOS) {
1781 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1782 GST_ELEMENT_ERROR (mq, STREAM, FAILED,
1783 ("Internal data stream error."),
1784 ("streaming stopped, reason %s",
1785 gst_flow_get_name (sq->srcresult)));
1787 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1790 /* EOS affects the buffering state */
1791 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1792 update_buffering (mq, sq);
1793 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1794 single_queue_overrun_cb (sq->queue, sq);
1795 gst_multi_queue_post_buffering (mq);
1797 case GST_EVENT_SEGMENT:
1798 apply_segment (mq, sq, sref, &sq->sink_segment);
1799 gst_event_unref (sref);
1802 apply_gap (mq, sq, sref, &sq->sink_segment);
1803 gst_event_unref (sref);
1812 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1813 sq->id, gst_flow_get_name (sq->srcresult));
1815 gst_event_unref (sref);
1816 gst_multi_queue_item_destroy (item);
1821 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1822 gst_event_unref (event);
1829 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1835 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1836 mq = (GstMultiQueue *) parent;
1838 switch (GST_QUERY_TYPE (query)) {
1840 if (GST_QUERY_IS_SERIALIZED (query)) {
1842 GstMultiQueueItem *item;
1844 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1845 if (sq->srcresult != GST_FLOW_OK)
1848 /* serialized events go in the queue. We need to be certain that we
1849 * don't cause deadlocks waiting for the query return value. We check if
1850 * the queue is empty (nothing is blocking downstream and the query can
1851 * be pushed for sure) or we are not buffering. If we are buffering,
1852 * the pipeline waits to unblock downstream until our queue fills up
1853 * completely, which can not happen if we block on the query..
1854 * Therefore we only potentially block when we are not buffering. */
1855 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
1856 /* Get an unique incrementing id. */
1857 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1859 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
1861 GST_DEBUG_OBJECT (mq,
1862 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
1863 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
1864 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
1865 g_cond_wait (&sq->query_handled, &mq->qlock);
1866 res = sq->last_query;
1868 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
1869 "queue is not empty");
1872 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1874 /* default handling */
1875 res = gst_pad_query_default (pad, parent, query);
1883 GST_DEBUG_OBJECT (mq, "Flushing");
1884 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1890 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
1891 GstPadMode mode, gboolean active)
1897 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1900 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1903 case GST_PAD_MODE_PUSH:
1905 result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
1907 result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
1908 /* make sure streaming finishes */
1909 result |= gst_pad_stop_task (pad);
1920 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1922 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1923 GstMultiQueue *mq = sq->mqueue;
1926 switch (GST_EVENT_TYPE (event)) {
1927 case GST_EVENT_RECONFIGURE:
1928 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1929 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1930 sq->srcresult = GST_FLOW_OK;
1931 g_cond_signal (&sq->turn);
1933 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1935 ret = gst_pad_push_event (sq->sinkpad, event);
1938 ret = gst_pad_push_event (sq->sinkpad, event);
1946 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1950 /* FIXME, Handle position offset depending on queue size */
1951 switch (GST_QUERY_TYPE (query)) {
1953 /* default handling */
1954 res = gst_pad_query_default (pad, parent, query);
1961 * Next-non-linked functions
1964 /* WITH LOCK TAKEN */
1966 wake_up_next_non_linked (GstMultiQueue * mq)
1970 /* maybe no-one is waiting */
1971 if (mq->numwaiting < 1)
1974 /* Else figure out which singlequeue(s) need waking up */
1975 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1976 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1978 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1979 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
1980 && sq->next_time != GST_CLOCK_TIME_NONE
1981 && sq->next_time >= mq->high_time)
1982 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
1983 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1984 g_cond_signal (&sq->turn);
1990 /* WITH LOCK TAKEN */
1992 compute_high_id (GstMultiQueue * mq)
1994 /* The high-id is either the highest id among the linked pads, or if all
1995 * pads are not-linked, it's the lowest not-linked pad */
1997 guint32 lowest = G_MAXUINT32;
1998 guint32 highid = G_MAXUINT32;
2000 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2001 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2003 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2004 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2006 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2007 /* No need to consider queues which are not waiting */
2008 if (sq->nextid == 0) {
2009 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2013 if (sq->nextid < lowest)
2014 lowest = sq->nextid;
2015 } else if (sq->srcresult != GST_FLOW_EOS) {
2016 /* If we don't have a global highid, or the global highid is lower than
2017 * this single queue's last outputted id, store the queue's one,
2018 * unless the singlequeue is at EOS (srcresult = EOS) */
2019 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2024 if (highid == G_MAXUINT32 || lowest < highid)
2025 mq->highid = lowest;
2027 mq->highid = highid;
2029 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2033 /* WITH LOCK TAKEN */
2035 compute_high_time (GstMultiQueue * mq)
2037 /* The high-id is either the highest id among the linked pads, or if all
2038 * pads are not-linked, it's the lowest not-linked pad */
2040 GstClockTime highest = GST_CLOCK_TIME_NONE;
2041 GstClockTime lowest = GST_CLOCK_TIME_NONE;
2043 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2044 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2047 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
2048 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
2049 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
2051 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2052 /* No need to consider queues which are not waiting */
2053 if (sq->next_time == GST_CLOCK_TIME_NONE) {
2054 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2058 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
2059 lowest = sq->next_time;
2060 } else if (sq->srcresult != GST_FLOW_EOS) {
2061 /* If we don't have a global highid, or the global highid is lower than
2062 * this single queue's last outputted id, store the queue's one,
2063 * unless the singlequeue is at EOS (srcresult = EOS) */
2064 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
2065 highest = sq->last_time;
2069 mq->high_time = highest;
2072 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
2073 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
2076 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2077 ((q)->max_size.format) <= (value))
2080 * GstSingleQueue functions
2083 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2085 GstMultiQueue *mq = sq->mqueue;
2087 GstDataQueueSize size;
2088 gboolean filled = TRUE;
2089 gboolean all_not_linked = TRUE;
2090 gboolean empty_found = FALSE;
2092 gst_data_queue_get_level (sq->queue, &size);
2095 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2096 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2097 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2100 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2102 /* check if we reached the hard time/bytes limits */
2103 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
2104 IS_FILLED (sq, time, sq->cur_time)) {
2108 /* Search for empty or unlinked queues */
2109 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2110 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2115 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2116 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2120 all_not_linked = FALSE;
2121 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2122 if (gst_data_queue_is_empty (oq->queue)) {
2123 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2129 if (!mq->queues || !mq->queues->next)
2130 all_not_linked = FALSE;
2132 /* if hard limits are not reached then we allow one more buffer in the full
2133 * queue, but only if any of the other singelqueues are empty or all are
2135 if (all_not_linked || empty_found) {
2136 if (all_not_linked) {
2137 GST_LOG_OBJECT (mq, "All other queues are not linked");
2139 if (IS_FILLED (sq, visible, size.visible)) {
2140 sq->max_size.visible = size.visible + 1;
2141 GST_DEBUG_OBJECT (mq,
2142 "Bumping single queue %d max visible to %d",
2143 sq->id, sq->max_size.visible);
2149 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2151 /* Overrun is always forwarded, since this is blocking the upstream element */
2153 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2154 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2159 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2161 gboolean empty = TRUE;
2162 GstMultiQueue *mq = sq->mqueue;
2165 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2166 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2170 "Single Queue %d is empty, Checking other single queues", sq->id);
2173 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2174 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2175 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2177 if (gst_data_queue_is_full (oq->queue)) {
2178 GstDataQueueSize size;
2180 gst_data_queue_get_level (oq->queue, &size);
2181 if (IS_FILLED (oq, visible, size.visible)) {
2182 oq->max_size.visible = size.visible + 1;
2183 GST_DEBUG_OBJECT (mq,
2184 "queue %d is filled, bumping its max visible to %d", oq->id,
2185 oq->max_size.visible);
2186 gst_data_queue_limits_changed (oq->queue);
2189 if (!gst_data_queue_is_empty (oq->queue))
2192 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2195 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2196 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2201 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2202 guint64 time, GstSingleQueue * sq)
2205 GstMultiQueue *mq = sq->mqueue;
2207 GST_DEBUG_OBJECT (mq,
2208 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2209 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2210 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2212 /* we are always filled on EOS */
2216 /* we never go past the max visible items unless we are in buffering mode */
2217 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2220 /* check time or bytes */
2221 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
2227 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2229 GstDataQueueItem *sitem;
2230 GstMultiQueueItem *mitem;
2231 gboolean was_flushing = FALSE;
2233 while (!gst_data_queue_is_empty (sq->queue)) {
2234 GstMiniObject *data;
2236 /* FIXME: If this fails here although the queue is not empty,
2237 * we're flushing... but we want to rescue all sticky
2238 * events nonetheless.
2240 if (!gst_data_queue_pop (sq->queue, &sitem)) {
2241 was_flushing = TRUE;
2242 gst_data_queue_set_flushing (sq->queue, FALSE);
2246 mitem = (GstMultiQueueItem *) sitem;
2248 data = sitem->object;
2250 if (!full && !mitem->is_query && GST_IS_EVENT (data)
2251 && GST_EVENT_IS_STICKY (data)
2252 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2253 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2254 gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2257 sitem->destroy (sitem);
2260 gst_data_queue_flush (sq->queue);
2262 gst_data_queue_set_flushing (sq->queue, TRUE);
2264 GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2265 update_buffering (sq->mqueue, sq);
2266 GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2267 gst_multi_queue_post_buffering (sq->mqueue);
2271 gst_single_queue_free (GstSingleQueue * sq)
2274 gst_data_queue_flush (sq->queue);
2275 g_object_unref (sq->queue);
2276 g_cond_clear (&sq->turn);
2277 g_cond_clear (&sq->query_handled);
2281 static GstSingleQueue *
2282 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2287 guint temp_id = (id == -1) ? 0 : id;
2289 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2291 /* Find an unused queue ID, if possible the passed one */
2292 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2293 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2294 /* This works because the IDs are sorted in ascending order */
2295 if (sq2->id == temp_id) {
2296 /* If this ID was requested by the caller return NULL,
2297 * otherwise just get us the next one */
2299 temp_id = sq2->id + 1;
2302 } else if (sq2->id > temp_id) {
2307 sq = g_new0 (GstSingleQueue, 1);
2311 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2312 mqueue->queues_cookie++;
2314 /* copy over max_size and extra_size so we don't need to take the lock
2315 * any longer when checking if the queue is full. */
2316 sq->max_size.visible = mqueue->max_size.visible;
2317 sq->max_size.bytes = mqueue->max_size.bytes;
2318 sq->max_size.time = mqueue->max_size.time;
2320 sq->extra_size.visible = mqueue->extra_size.visible;
2321 sq->extra_size.bytes = mqueue->extra_size.bytes;
2322 sq->extra_size.time = mqueue->extra_size.time;
2324 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2326 sq->mqueue = mqueue;
2327 sq->srcresult = GST_FLOW_FLUSHING;
2329 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2330 single_queue_check_full,
2331 (GstDataQueueFullCallback) single_queue_overrun_cb,
2332 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2334 sq->flushing = FALSE;
2335 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2336 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2340 sq->next_time = GST_CLOCK_TIME_NONE;
2341 sq->last_time = GST_CLOCK_TIME_NONE;
2342 g_cond_init (&sq->turn);
2343 g_cond_init (&sq->query_handled);
2345 sq->sinktime = GST_CLOCK_TIME_NONE;
2346 sq->srctime = GST_CLOCK_TIME_NONE;
2347 sq->sink_tainted = TRUE;
2348 sq->src_tainted = TRUE;
2350 name = g_strdup_printf ("sink_%u", sq->id);
2351 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
2354 gst_pad_set_chain_function (sq->sinkpad,
2355 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2356 gst_pad_set_activatemode_function (sq->sinkpad,
2357 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2358 gst_pad_set_event_function (sq->sinkpad,
2359 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2360 gst_pad_set_query_function (sq->sinkpad,
2361 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2362 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2363 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2364 GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2366 name = g_strdup_printf ("src_%u", sq->id);
2367 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2370 gst_pad_set_activatemode_function (sq->srcpad,
2371 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2372 gst_pad_set_event_function (sq->srcpad,
2373 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2374 gst_pad_set_query_function (sq->srcpad,
2375 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2376 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2377 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2378 GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2380 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2381 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2383 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2385 /* only activate the pads when we are not in the NULL state
2386 * and add the pad under the state_lock to prevend state changes
2387 * between activating and adding */
2388 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2389 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2390 gst_pad_set_active (sq->srcpad, TRUE);
2391 gst_pad_set_active (sq->sinkpad, TRUE);
2393 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2394 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2395 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2397 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",