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
28 * @see_also: #GstQueue
30 * Multiqueue is similar to a normal #GstQueue with the following additional
33 * 1) Multiple streamhandling
35 * * The element handles queueing data on more than one stream at once. To
36 * achieve such a feature it has request sink pads (sink%u) and
37 * 'sometimes' src pads (src%u). When requesting a given sinkpad with gst_element_request_pad(),
38 * the associated srcpad for that stream will be created.
39 * Example: requesting sink1 will generate src1.
41 * 2) Non-starvation on multiple stream
43 * * If more than one stream is used with the element, the streams' queues
44 * will be dynamically grown (up to a limit), in order to ensure that no
45 * stream is risking data starvation. This guarantees that at any given
46 * time there are at least N bytes queued and available for each individual
47 * stream. If an EOS event comes through a srcpad, the associated queue will be
48 * considered as 'not-empty' in the queue-size-growing algorithm.
50 * 3) Non-linked srcpads graceful handling
52 * * In order to better support dynamic switching between streams, the multiqueue
53 * (unlike the current GStreamer queue) continues to push buffers on non-linked
54 * pads rather than shutting down. In addition, to prevent a non-linked stream from very quickly consuming all
55 * available buffers and thus 'racing ahead' of the other streams, the element
56 * must ensure that buffers and inlined events for a non-linked stream are pushed
57 * in the same order as they were received, relative to the other streams
58 * controlled by the element. This means that a buffer cannot be pushed to a
59 * non-linked pad any sooner than buffers in any other stream which were received
62 * Data is queued until one of the limits specified by the
63 * #GstMultiQueue:max-size-buffers, #GstMultiQueue:max-size-bytes and/or
64 * #GstMultiQueue:max-size-time properties has been reached. Any attempt to push
65 * more buffers into the queue will block the pushing thread until more space
66 * becomes available. #GstMultiQueue:extra-size-buffers,
69 * #GstMultiQueue:extra-size-bytes and #GstMultiQueue:extra-size-time are
72 * The default queue size limits are 5 buffers, 10MB of data, or
73 * two second worth of data, whichever is reached first. Note that the number
74 * of buffers will dynamically grow depending on the fill level of
77 * The #GstMultiQueue::underrun signal is emitted when all of the queues
78 * are empty. The #GstMultiQueue::overrun signal is emitted when one of the
80 * Both signals are emitted from the context of the streaming thread.
82 * When using #GstMultiQueue:sync-by-running-time the unlinked streams will
83 * be throttled by the highest running-time of linked streams. This allows
84 * further relinking of those unlinked streams without them being in the
85 * future (i.e. to achieve gapless playback).
86 * When dealing with streams which have got different consumption requirements
87 * downstream (ex: video decoders which will consume more buffer (in time) than
88 * audio decoders), it is recommended to group streams of the same type
89 * by using the pad "group-id" property. This will further throttle streams
90 * in time within that group.
98 #include <gst/glib-compat-private.h>
101 #include "gstmultiqueue.h"
102 #include "gstcoreelementselements.h"
105 * @sinkpad: associated sink #GstPad
106 * @srcpad: associated source #GstPad
108 * Structure containing all information and properties about
111 typedef struct _GstSingleQueue GstSingleQueue;
113 struct _GstSingleQueue
117 /* unique identifier of the queue */
119 /* group of streams to which this queue belongs to */
121 GstClockTimeDiff group_high_time;
127 /* flowreturn of previous srcpad push */
128 GstFlowReturn srcresult;
129 /* If something was actually pushed on
130 * this pad after flushing/pad activation
131 * and the srcresult corresponds to something
137 GstSegment sink_segment;
138 GstSegment src_segment;
139 gboolean has_src_segment; /* preferred over initializing the src_segment to
140 * UNDEFINED as this doesn't requires adding ifs
141 * in every segment usage */
143 /* position of src/sink */
144 GstClockTimeDiff sinktime, srctime;
145 /* cached input value, used for interleave */
146 GstClockTimeDiff cached_sinktime;
147 /* TRUE if either position needs to be recalculated */
148 gboolean sink_tainted, src_tainted;
150 /* stream group id */
151 guint32 sink_stream_gid;
152 guint32 src_stream_gid;
154 /* TRUE if the stream group-id changed. Resetted to FALSE the next time the
155 * segment is calculated */
156 gboolean sink_stream_gid_changed;
157 gboolean src_stream_gid_changed;
161 GstDataQueueSize max_size, extra_size;
162 GstClockTime cur_time;
164 gboolean is_segment_done;
169 /* Protected by global lock */
170 guint32 nextid; /* ID of the next object waiting to be pushed */
171 guint32 oldid; /* ID of the last object pushed (last in a series) */
172 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
173 GstClockTimeDiff next_time; /* End running time of next buffer to be pushed */
174 GstClockTimeDiff last_time; /* Start running time of last pushed buffer */
175 GCond turn; /* SingleQueue turn waiting conditional */
177 /* for serialized queries */
180 GstQuery *last_handled_query;
182 /* For interleave calculation */
183 GThread *thread; /* Streaming thread of SingleQueue */
184 GstClockTime interleave; /* Calculated interleve within the thread */
187 /* Extension of GstDataQueueItem structure for our usage */
188 typedef struct _GstMultiQueueItem GstMultiQueueItem;
190 struct _GstMultiQueueItem
192 GstMiniObject *object;
197 GDestroyNotify destroy;
203 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
204 static void gst_single_queue_unref (GstSingleQueue * squeue);
205 static GstSingleQueue *gst_single_queue_ref (GstSingleQueue * squeue);
207 static void wake_up_next_non_linked (GstMultiQueue * mq);
208 static void compute_high_id (GstMultiQueue * mq);
209 static void compute_high_time (GstMultiQueue * mq, guint groupid);
210 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
211 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
213 static void update_buffering (GstMultiQueue * mq, GstSingleQueue * sq);
214 static void gst_multi_queue_post_buffering (GstMultiQueue * mq);
215 static void recheck_buffering_status (GstMultiQueue * mq);
217 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
219 static void calculate_interleave (GstMultiQueue * mq, GstSingleQueue * sq);
221 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
224 GST_STATIC_CAPS_ANY);
226 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
229 GST_STATIC_CAPS_ANY);
231 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
232 #define GST_CAT_DEFAULT (multi_queue_debug)
234 /* Signals and args */
242 /* default limits, we try to keep up to 2 seconds of data and if there is not
243 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
244 * there is data in the queues. Normally, the byte and time limits are not hit
245 * in theses conditions. */
246 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
247 #define DEFAULT_MAX_SIZE_BUFFERS 5
248 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
250 /* second limits. When we hit one of the above limits we are probably dealing
251 * with a badly muxed file and we scale the limits to these emergency values.
252 * This is currently not yet implemented.
253 * Since we dynamically scale the queue buffer size up to the limits but avoid
254 * going above the max-size-buffers when we can, we don't really need this
255 * additional extra size. */
256 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
257 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
258 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
260 #define DEFAULT_USE_BUFFERING FALSE
261 #define DEFAULT_LOW_WATERMARK 0.01
262 #define DEFAULT_HIGH_WATERMARK 0.99
263 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
264 #define DEFAULT_USE_INTERLEAVE FALSE
265 #define DEFAULT_UNLINKED_CACHE_TIME 250 * GST_MSECOND
267 #define DEFAULT_MINIMUM_INTERLEAVE (250 * GST_MSECOND)
272 PROP_EXTRA_SIZE_BYTES,
273 PROP_EXTRA_SIZE_BUFFERS,
274 PROP_EXTRA_SIZE_TIME,
276 PROP_MAX_SIZE_BUFFERS,
283 PROP_SYNC_BY_RUNNING_TIME,
285 PROP_UNLINKED_CACHE_TIME,
286 PROP_MINIMUM_INTERLEAVE,
291 /* Explanation for buffer levels and percentages:
293 * The buffering_level functions here return a value in a normalized range
294 * that specifies the current fill level of a queue. The range goes from 0 to
295 * MAX_BUFFERING_LEVEL. The low/high watermarks also use this same range.
297 * This is not to be confused with the buffering_percent value, which is
298 * a *relative* quantity - relative to the low/high watermarks.
299 * buffering_percent = 0% means overall buffering_level is at the low watermark.
300 * buffering_percent = 100% means overall buffering_level is at the high watermark.
301 * buffering_percent is used for determining if the fill level has reached
302 * the high watermark, and for producing BUFFERING messages. This value
303 * always uses a 0..100 range (since it is a percentage).
305 * To avoid future confusions, whenever "buffering level" is mentioned, it
306 * refers to the absolute level which is in the 0..MAX_BUFFERING_LEVEL
307 * range. Whenever "buffering_percent" is mentioned, it refers to the
308 * percentage value that is relative to the low/high watermark. */
310 /* Using a buffering level range of 0..1000000 to allow for a
311 * resolution in ppm (1 ppm = 0.0001%) */
312 #define MAX_BUFFERING_LEVEL 1000000
314 /* How much 1% makes up in the buffer level range */
315 #define BUF_LEVEL_PERCENT_FACTOR ((MAX_BUFFERING_LEVEL) / 100)
317 /* GstMultiQueuePad */
319 #define DEFAULT_PAD_GROUP_ID 0
325 PROP_CURRENT_LEVEL_BUFFERS,
326 PROP_CURRENT_LEVEL_BYTES,
327 PROP_CURRENT_LEVEL_TIME,
330 #define GST_TYPE_MULTIQUEUE_PAD (gst_multiqueue_pad_get_type())
331 #define GST_MULTIQUEUE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePad))
332 #define GST_IS_MULTIQUEUE_PAD(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTIQUEUE_PAD))
333 #define GST_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
334 #define GST_IS_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_MULTIQUEUE_PAD))
335 #define GST_MULTIQUEUE_PAD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
337 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
338 g_mutex_lock (&q->qlock); \
341 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
342 g_mutex_unlock (&q->qlock); \
345 #define SET_PERCENT(mq, perc) G_STMT_START { \
346 if (perc != mq->buffering_percent) { \
347 mq->buffering_percent = perc; \
348 mq->buffering_percent_changed = TRUE; \
349 GST_DEBUG_OBJECT (mq, "buffering %d percent", perc); \
353 struct _GstMultiQueuePad
360 struct _GstMultiQueuePadClass
362 GstPadClass parent_class;
365 GType gst_multiqueue_pad_get_type (void);
367 G_DEFINE_TYPE (GstMultiQueuePad, gst_multiqueue_pad, GST_TYPE_PAD);
370 gst_multiqueue_pad_get_group_id (GstMultiQueuePad * pad)
378 mq = g_weak_ref_get (&pad->sq->mqueue);
381 GST_OBJECT_LOCK (mq);
384 ret = pad->sq->groupid;
387 GST_OBJECT_UNLOCK (mq);
388 gst_object_unref (mq);
395 gst_multiqueue_pad_get_current_level_buffers (GstMultiQueuePad * pad)
397 GstSingleQueue *sq = pad->sq;
398 GstDataQueueSize level;
404 mq = g_weak_ref_get (&pad->sq->mqueue);
407 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
410 gst_data_queue_get_level (sq->queue, &level);
413 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
414 gst_object_unref (mq);
417 return level.visible;
421 gst_multiqueue_pad_get_current_level_bytes (GstMultiQueuePad * pad)
423 GstSingleQueue *sq = pad->sq;
424 GstDataQueueSize level;
430 mq = g_weak_ref_get (&pad->sq->mqueue);
433 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
436 gst_data_queue_get_level (sq->queue, &level);
439 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
440 gst_object_unref (mq);
447 gst_multiqueue_pad_get_current_level_time (GstMultiQueuePad * pad)
449 GstSingleQueue *sq = pad->sq;
456 mq = g_weak_ref_get (&pad->sq->mqueue);
459 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
465 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
466 gst_object_unref (mq);
473 gst_multiqueue_pad_get_property (GObject * object, guint prop_id,
474 GValue * value, GParamSpec * pspec)
476 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
479 case PROP_PAD_GROUP_ID:
480 g_value_set_uint (value, gst_multiqueue_pad_get_group_id (pad));
482 case PROP_CURRENT_LEVEL_BUFFERS:{
483 g_value_set_uint (value,
484 gst_multiqueue_pad_get_current_level_buffers (pad));
487 case PROP_CURRENT_LEVEL_BYTES:{
488 g_value_set_uint (value,
489 gst_multiqueue_pad_get_current_level_bytes (pad));
492 case PROP_CURRENT_LEVEL_TIME:{
493 g_value_set_uint64 (value,
494 gst_multiqueue_pad_get_current_level_time (pad));
498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504 gst_multiqueue_pad_set_property (GObject * object, guint prop_id,
505 const GValue * value, GParamSpec * pspec)
507 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
510 case PROP_PAD_GROUP_ID:
512 GstMultiQueue *mqueue = g_weak_ref_get (&pad->sq->mqueue);
515 GST_OBJECT_LOCK (mqueue);
517 pad->sq->groupid = g_value_get_uint (value);
520 GST_OBJECT_UNLOCK (mqueue);
521 gst_object_unref (mqueue);
526 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
532 gst_multiqueue_pad_finalize (GObject * object)
534 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
537 gst_single_queue_unref (pad->sq);
539 G_OBJECT_CLASS (gst_multiqueue_pad_parent_class)->finalize (object);
543 gst_multiqueue_pad_class_init (GstMultiQueuePadClass * klass)
545 GObjectClass *gobject_class = (GObjectClass *) klass;
547 gobject_class->set_property = gst_multiqueue_pad_set_property;
548 gobject_class->get_property = gst_multiqueue_pad_get_property;
549 gobject_class->finalize = gst_multiqueue_pad_finalize;
552 * GstMultiQueuePad:group-id:
554 * Group to which this pad belongs.
558 g_object_class_install_property (gobject_class, PROP_PAD_GROUP_ID,
559 g_param_spec_uint ("group-id", "Group ID",
560 "Group to which this pad belongs", 0, G_MAXUINT32,
561 DEFAULT_PAD_GROUP_ID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
564 * GstMultiQueuePad:current-level-buffers:
566 * The corresponding queue's current level of buffers.
570 g_object_class_install_property (gobject_class, PROP_CURRENT_LEVEL_BUFFERS,
571 g_param_spec_uint ("current-level-buffers", "Current level buffers",
572 "Current level buffers", 0, G_MAXUINT32,
573 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
576 * GstMultiQueuePad:current-level-bytes:
578 * The corresponding queue's current level of bytes.
582 g_object_class_install_property (gobject_class, PROP_CURRENT_LEVEL_BYTES,
583 g_param_spec_uint ("current-level-bytes", "Current level bytes",
584 "Current level bytes", 0, G_MAXUINT32,
585 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
588 * GstMultiQueuePad:current-level-time:
590 * The corresponding queue's current level of time.
594 g_object_class_install_property (gobject_class, PROP_CURRENT_LEVEL_TIME,
595 g_param_spec_uint64 ("current-level-time", "Current level time",
596 "Current level time", 0, G_MAXUINT64,
597 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
601 gst_multiqueue_pad_init (GstMultiQueuePad * pad)
607 /* Convenience function */
608 static inline GstClockTimeDiff
609 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
611 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
613 if (GST_CLOCK_TIME_IS_VALID (val)) {
615 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
624 static void gst_multi_queue_finalize (GObject * object);
625 static void gst_multi_queue_set_property (GObject * object,
626 guint prop_id, const GValue * value, GParamSpec * pspec);
627 static void gst_multi_queue_get_property (GObject * object,
628 guint prop_id, GValue * value, GParamSpec * pspec);
630 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
631 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
632 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
633 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
634 element, GstStateChange transition);
636 static void gst_multi_queue_loop (GstPad * pad);
639 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
640 #define gst_multi_queue_parent_class parent_class
641 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
643 GST_ELEMENT_REGISTER_DEFINE (multiqueue, "multiqueue", GST_RANK_NONE,
644 GST_TYPE_MULTI_QUEUE);
646 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
649 gst_multi_queue_class_init (GstMultiQueueClass * klass)
651 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
652 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
654 gobject_class->set_property = gst_multi_queue_set_property;
655 gobject_class->get_property = gst_multi_queue_get_property;
660 * GstMultiQueue::underrun:
661 * @multiqueue: the multiqueue instance
663 * This signal is emitted from the streaming thread when there is
664 * no data in any of the queues inside the multiqueue instance (underrun).
666 * This indicates either starvation or EOS from the upstream data sources.
668 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
669 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
670 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
671 NULL, G_TYPE_NONE, 0);
674 * GstMultiQueue::overrun:
675 * @multiqueue: the multiqueue instance
677 * Reports that one of the queues in the multiqueue is full (overrun).
678 * A queue is full if the total amount of data inside it (num-buffers, time,
679 * size) is higher than the boundary values which can be set through the
680 * GObject properties.
682 * This can be used as an indicator of pre-roll.
684 gst_multi_queue_signals[SIGNAL_OVERRUN] =
685 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
686 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
687 NULL, G_TYPE_NONE, 0);
691 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
692 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
693 "Max. amount of data in the queue (bytes, 0=disable)",
694 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
695 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
696 G_PARAM_STATIC_STRINGS));
697 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
698 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
699 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
700 DEFAULT_MAX_SIZE_BUFFERS,
701 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
702 G_PARAM_STATIC_STRINGS));
703 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
704 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
705 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
706 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
707 G_PARAM_STATIC_STRINGS));
709 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
710 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
711 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
712 " (NOT IMPLEMENTED)",
713 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
714 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
715 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
716 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
717 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
718 " (NOT IMPLEMENTED)",
719 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
720 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
721 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
722 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
723 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
724 " (NOT IMPLEMENTED)",
725 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
726 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
729 * GstMultiQueue:use-buffering:
731 * Enable the buffering option in multiqueue so that BUFFERING messages are
732 * emitted based on low-/high-percent thresholds.
734 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
735 g_param_spec_boolean ("use-buffering", "Use buffering",
736 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds "
737 "(0% = low-watermark, 100% = high-watermark)",
738 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
739 G_PARAM_STATIC_STRINGS));
741 * GstMultiQueue:low-percent:
743 * Low threshold percent for buffering to start.
745 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
746 g_param_spec_int ("low-percent", "Low percent",
747 "Low threshold for buffering to start. Only used if use-buffering is True "
748 "(Deprecated: use low-watermark instead)",
749 0, 100, DEFAULT_LOW_WATERMARK * 100,
750 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
752 * GstMultiQueue:high-percent:
754 * High threshold percent for buffering to finish.
756 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
757 g_param_spec_int ("high-percent", "High percent",
758 "High threshold for buffering to finish. Only used if use-buffering is True "
759 "(Deprecated: use high-watermark instead)",
760 0, 100, DEFAULT_HIGH_WATERMARK * 100,
761 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
763 * GstMultiQueue:low-watermark:
765 * Low threshold watermark for buffering to start.
769 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
770 g_param_spec_double ("low-watermark", "Low watermark",
771 "Low threshold for buffering to start. Only used if use-buffering is True",
772 0.0, 1.0, DEFAULT_LOW_WATERMARK,
773 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
775 * GstMultiQueue:high-watermark:
777 * High threshold watermark for buffering to finish.
781 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
782 g_param_spec_double ("high-watermark", "High watermark",
783 "High threshold for buffering to finish. Only used if use-buffering is True",
784 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
785 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
788 * GstMultiQueue:sync-by-running-time:
790 * If enabled multiqueue will synchronize deactivated or not-linked streams
791 * to the activated and linked streams by taking the running time.
792 * Otherwise multiqueue will synchronize the deactivated or not-linked
793 * streams by keeping the order in which buffers and events arrived compared
794 * to active and linked streams.
796 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
797 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
798 "Synchronize deactivated or not-linked streams by running time",
799 DEFAULT_SYNC_BY_RUNNING_TIME,
800 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
802 g_object_class_install_property (gobject_class, PROP_USE_INTERLEAVE,
803 g_param_spec_boolean ("use-interleave", "Use interleave",
804 "Adjust time limits based on input interleave",
805 DEFAULT_USE_INTERLEAVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
807 g_object_class_install_property (gobject_class, PROP_UNLINKED_CACHE_TIME,
808 g_param_spec_uint64 ("unlinked-cache-time", "Unlinked cache time (ns)",
809 "Extra buffering in time for unlinked streams (if 'sync-by-running-time')",
810 0, G_MAXUINT64, DEFAULT_UNLINKED_CACHE_TIME,
811 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
812 G_PARAM_STATIC_STRINGS));
814 g_object_class_install_property (gobject_class, PROP_MINIMUM_INTERLEAVE,
815 g_param_spec_uint64 ("min-interleave-time", "Minimum interleave time",
816 "Minimum extra buffering for deinterleaving (size of the queues) when use-interleave=true",
817 0, G_MAXUINT64, DEFAULT_MINIMUM_INTERLEAVE,
818 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
819 G_PARAM_STATIC_STRINGS));
822 * GstMultiQueue:stats:
824 * Various #GstMultiQueue statistics. This property returns a #GstStructure
825 * with name "application/x-gst-multi-queue-stats" with the following fields:
827 * - "queues" GST_TYPE_ARRAY Contains one GstStructure named "queue_%d"
828 * (where \%d is the queue's ID) per internal queue:
829 * - "buffers" G_TYPE_UINT The queue's current level of buffers
830 * - "bytes" G_TYPE_UINT The queue's current level of bytes
831 * - "time" G_TYPE_UINT64 The queue's current level of time
835 g_object_class_install_property (gobject_class, PROP_STATS,
836 g_param_spec_boxed ("stats", "Stats",
837 "Multiqueue Statistics",
838 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
840 gobject_class->finalize = gst_multi_queue_finalize;
842 gst_element_class_set_static_metadata (gstelement_class,
844 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
845 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
846 &sinktemplate, GST_TYPE_MULTIQUEUE_PAD);
847 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
848 &srctemplate, GST_TYPE_MULTIQUEUE_PAD);
850 gstelement_class->request_new_pad =
851 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
852 gstelement_class->release_pad =
853 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
854 gstelement_class->change_state =
855 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
857 gst_type_mark_as_plugin_api (GST_TYPE_MULTIQUEUE_PAD, 0);
861 gst_multi_queue_init (GstMultiQueue * mqueue)
863 mqueue->nbqueues = 0;
864 mqueue->queues = NULL;
866 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
867 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
868 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
870 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
871 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
872 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
874 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
875 mqueue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
876 mqueue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
878 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
879 mqueue->use_interleave = DEFAULT_USE_INTERLEAVE;
880 mqueue->min_interleave_time = DEFAULT_MINIMUM_INTERLEAVE;
881 mqueue->unlinked_cache_time = DEFAULT_UNLINKED_CACHE_TIME;
885 mqueue->high_time = GST_CLOCK_STIME_NONE;
887 g_mutex_init (&mqueue->qlock);
888 g_mutex_init (&mqueue->reconf_lock);
889 g_mutex_init (&mqueue->buffering_post_lock);
893 gst_multi_queue_finalize (GObject * object)
895 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
897 g_list_free_full (mqueue->queues, (GDestroyNotify) gst_single_queue_unref);
898 mqueue->queues = NULL;
899 mqueue->queues_cookie++;
901 /* free/unref instance data */
902 g_mutex_clear (&mqueue->qlock);
903 g_mutex_clear (&mqueue->reconf_lock);
904 g_mutex_clear (&mqueue->buffering_post_lock);
906 G_OBJECT_CLASS (parent_class)->finalize (object);
909 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
910 GList * tmp = mq->queues; \
912 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
913 q->max_size.format = mq->max_size.format; \
914 update_buffering (mq, q); \
915 gst_data_queue_limits_changed (q->queue); \
916 tmp = g_list_next(tmp); \
921 gst_multi_queue_set_property (GObject * object, guint prop_id,
922 const GValue * value, GParamSpec * pspec)
924 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
927 case PROP_MAX_SIZE_BYTES:
928 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
929 mq->max_size.bytes = g_value_get_uint (value);
930 SET_CHILD_PROPERTY (mq, bytes);
931 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
932 gst_multi_queue_post_buffering (mq);
934 case PROP_MAX_SIZE_BUFFERS:
937 gint new_size = g_value_get_uint (value);
939 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
941 mq->max_size.visible = new_size;
945 GstDataQueueSize size;
946 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
947 gst_data_queue_get_level (q->queue, &size);
949 GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
950 " current: %d, current max %d", q->id, new_size, size.visible,
951 q->max_size.visible);
953 /* do not reduce max size below current level if the single queue
954 * has grown because of empty queue */
956 q->max_size.visible = new_size;
957 } else if (q->max_size.visible == 0) {
958 q->max_size.visible = MAX (new_size, size.visible);
959 } else if (new_size > size.visible) {
960 q->max_size.visible = new_size;
962 update_buffering (mq, q);
963 gst_data_queue_limits_changed (q->queue);
964 tmp = g_list_next (tmp);
967 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
968 gst_multi_queue_post_buffering (mq);
972 case PROP_MAX_SIZE_TIME:
973 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
974 mq->max_size.time = g_value_get_uint64 (value);
975 SET_CHILD_PROPERTY (mq, time);
976 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
977 gst_multi_queue_post_buffering (mq);
979 case PROP_EXTRA_SIZE_BYTES:
980 mq->extra_size.bytes = g_value_get_uint (value);
982 case PROP_EXTRA_SIZE_BUFFERS:
983 mq->extra_size.visible = g_value_get_uint (value);
985 case PROP_EXTRA_SIZE_TIME:
986 mq->extra_size.time = g_value_get_uint64 (value);
988 case PROP_USE_BUFFERING:
989 mq->use_buffering = g_value_get_boolean (value);
990 recheck_buffering_status (mq);
992 case PROP_LOW_PERCENT:
993 mq->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
994 /* Recheck buffering status - the new low_watermark value might
995 * be above the current fill level. If the old low_watermark one
996 * was below the current level, this means that mq->buffering is
997 * disabled and needs to be re-enabled. */
998 recheck_buffering_status (mq);
1000 case PROP_HIGH_PERCENT:
1001 mq->high_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
1002 recheck_buffering_status (mq);
1004 case PROP_LOW_WATERMARK:
1005 mq->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
1006 recheck_buffering_status (mq);
1008 case PROP_HIGH_WATERMARK:
1009 mq->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
1010 recheck_buffering_status (mq);
1012 case PROP_SYNC_BY_RUNNING_TIME:
1013 mq->sync_by_running_time = g_value_get_boolean (value);
1015 case PROP_USE_INTERLEAVE:
1016 mq->use_interleave = g_value_get_boolean (value);
1018 case PROP_UNLINKED_CACHE_TIME:
1019 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1020 mq->unlinked_cache_time = g_value_get_uint64 (value);
1021 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1022 gst_multi_queue_post_buffering (mq);
1024 case PROP_MINIMUM_INTERLEAVE:
1025 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1026 mq->min_interleave_time = g_value_get_uint64 (value);
1027 if (mq->use_interleave)
1028 calculate_interleave (mq, NULL);
1029 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1032 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1037 /* Called with mutex held */
1038 static GstStructure *
1039 gst_multi_queue_get_stats (GstMultiQueue * mq)
1042 gst_structure_new_empty ("application/x-gst-multi-queue-stats");
1046 if (mq->queues != NULL) {
1047 GValue queues = G_VALUE_INIT;
1048 GValue v = G_VALUE_INIT;
1050 g_value_init (&queues, GST_TYPE_ARRAY);
1052 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1053 GstDataQueueSize level;
1056 g_value_init (&v, GST_TYPE_STRUCTURE);
1058 sq = (GstSingleQueue *) tmp->data;
1059 gst_data_queue_get_level (sq->queue, &level);
1060 id = g_strdup_printf ("queue_%d", sq->id);
1061 s = gst_structure_new (id,
1062 "buffers", G_TYPE_UINT, level.visible,
1063 "bytes", G_TYPE_UINT, level.bytes,
1064 "time", G_TYPE_UINT64, sq->cur_time, NULL);
1065 g_value_take_boxed (&v, s);
1066 gst_value_array_append_and_take_value (&queues, &v);
1069 gst_structure_take_value (ret, "queues", &queues);
1076 gst_multi_queue_get_property (GObject * object, guint prop_id,
1077 GValue * value, GParamSpec * pspec)
1079 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
1081 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1084 case PROP_EXTRA_SIZE_BYTES:
1085 g_value_set_uint (value, mq->extra_size.bytes);
1087 case PROP_EXTRA_SIZE_BUFFERS:
1088 g_value_set_uint (value, mq->extra_size.visible);
1090 case PROP_EXTRA_SIZE_TIME:
1091 g_value_set_uint64 (value, mq->extra_size.time);
1093 case PROP_MAX_SIZE_BYTES:
1094 g_value_set_uint (value, mq->max_size.bytes);
1096 case PROP_MAX_SIZE_BUFFERS:
1097 g_value_set_uint (value, mq->max_size.visible);
1099 case PROP_MAX_SIZE_TIME:
1100 g_value_set_uint64 (value, mq->max_size.time);
1102 case PROP_USE_BUFFERING:
1103 g_value_set_boolean (value, mq->use_buffering);
1105 case PROP_LOW_PERCENT:
1106 g_value_set_int (value, mq->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
1108 case PROP_HIGH_PERCENT:
1109 g_value_set_int (value, mq->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
1111 case PROP_LOW_WATERMARK:
1112 g_value_set_double (value, mq->low_watermark /
1113 (gdouble) MAX_BUFFERING_LEVEL);
1115 case PROP_HIGH_WATERMARK:
1116 g_value_set_double (value, mq->high_watermark /
1117 (gdouble) MAX_BUFFERING_LEVEL);
1119 case PROP_SYNC_BY_RUNNING_TIME:
1120 g_value_set_boolean (value, mq->sync_by_running_time);
1122 case PROP_USE_INTERLEAVE:
1123 g_value_set_boolean (value, mq->use_interleave);
1125 case PROP_UNLINKED_CACHE_TIME:
1126 g_value_set_uint64 (value, mq->unlinked_cache_time);
1128 case PROP_MINIMUM_INTERLEAVE:
1129 g_value_set_uint64 (value, mq->min_interleave_time);
1132 g_value_take_boxed (value, gst_multi_queue_get_stats (mq));
1135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1139 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1142 static GstIterator *
1143 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
1145 GstIterator *it = NULL;
1146 GstPad *opad, *sinkpad, *srcpad;
1147 GstSingleQueue *squeue;
1148 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
1149 GValue val = { 0, };
1151 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1152 squeue = GST_MULTIQUEUE_PAD (pad)->sq;
1156 srcpad = g_weak_ref_get (&squeue->srcpad);
1157 sinkpad = g_weak_ref_get (&squeue->sinkpad);
1158 if (sinkpad == pad && srcpad) {
1160 gst_clear_object (&sinkpad);
1162 } else if (srcpad == pad && sinkpad) {
1164 gst_clear_object (&srcpad);
1167 gst_clear_object (&srcpad);
1168 gst_clear_object (&sinkpad);
1172 g_value_init (&val, GST_TYPE_PAD);
1173 g_value_set_object (&val, opad);
1174 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1175 g_value_unset (&val);
1177 gst_object_unref (opad);
1180 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1187 * GstElement methods
1191 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
1192 const gchar * name, const GstCaps * caps)
1194 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
1195 GstSingleQueue *squeue;
1200 sscanf (name + 4, "_%u", &temp_id);
1201 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
1204 g_mutex_lock (&mqueue->reconf_lock);
1205 /* Create a new single queue, add the sink and source pad and return the sink pad */
1206 squeue = gst_single_queue_new (mqueue, temp_id);
1207 g_mutex_unlock (&mqueue->reconf_lock);
1209 new_pad = squeue ? g_weak_ref_get (&squeue->sinkpad) : NULL;
1210 /* request pad assumes the element is owning the ref of the pad it returns */
1212 gst_object_unref (new_pad);
1214 GST_DEBUG_OBJECT (mqueue, "Returning pad %" GST_PTR_FORMAT, new_pad);
1220 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
1222 GstPad *sinkpad = NULL, *srcpad = NULL;
1223 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
1224 GstSingleQueue *sq = NULL;
1227 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1229 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1230 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
1231 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1232 sq = (GstSingleQueue *) tmp->data;
1233 sinkpad = g_weak_ref_get (&sq->sinkpad);
1235 if (sinkpad == pad) {
1236 srcpad = g_weak_ref_get (&sq->srcpad);
1240 gst_object_unref (sinkpad);
1244 gst_clear_object (&sinkpad);
1245 gst_clear_object (&srcpad);
1246 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
1247 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1251 /* FIXME: The removal of the singlequeue should probably not happen until it
1252 * finishes draining */
1254 /* Take the reconfiguration lock before removing the singlequeue from
1255 * the list, to prevent overlapping release/request from causing
1257 g_mutex_lock (&mqueue->reconf_lock);
1259 /* remove it from the list */
1260 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
1261 mqueue->queues_cookie++;
1263 /* FIXME : recompute next-non-linked */
1264 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1266 /* delete SingleQueue */
1267 gst_data_queue_set_flushing (sq->queue, TRUE);
1269 gst_pad_set_active (srcpad, FALSE);
1270 gst_pad_set_active (sinkpad, FALSE);
1271 gst_element_remove_pad (element, srcpad);
1272 gst_element_remove_pad (element, sinkpad);
1273 gst_object_unref (srcpad);
1274 gst_object_unref (sinkpad);
1276 g_mutex_unlock (&mqueue->reconf_lock);
1279 static GstStateChangeReturn
1280 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
1282 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
1283 GstSingleQueue *sq = NULL;
1284 GstStateChangeReturn result;
1286 switch (transition) {
1287 case GST_STATE_CHANGE_READY_TO_PAUSED:{
1290 /* Set all pads to non-flushing */
1291 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1292 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1293 sq = (GstSingleQueue *) tmp->data;
1294 sq->flushing = FALSE;
1295 sq->sink_stream_gid = sq->src_stream_gid = GST_GROUP_ID_INVALID;
1298 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
1299 SET_CHILD_PROPERTY (mqueue, visible);
1301 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1302 gst_multi_queue_post_buffering (mqueue);
1306 case GST_STATE_CHANGE_PAUSED_TO_READY:{
1309 /* Un-wait all waiting pads */
1310 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1311 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1312 sq = (GstSingleQueue *) tmp->data;
1313 sq->flushing = TRUE;
1314 g_cond_signal (&sq->turn);
1316 sq->last_query = FALSE;
1317 g_cond_signal (&sq->query_handled);
1319 mqueue->interleave_incomplete = FALSE;
1320 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1327 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1329 switch (transition) {
1338 gst_single_queue_start (GstMultiQueue * mq, GstSingleQueue * sq)
1340 gboolean res = FALSE;
1341 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
1343 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
1346 res = gst_pad_start_task (srcpad,
1347 (GstTaskFunction) gst_multi_queue_loop, srcpad, NULL);
1348 gst_object_unref (srcpad);
1355 gst_single_queue_pause (GstMultiQueue * mq, GstSingleQueue * sq)
1357 gboolean result = FALSE;
1358 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
1360 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
1362 result = gst_pad_pause_task (srcpad);
1363 gst_object_unref (srcpad);
1366 sq->sink_tainted = sq->src_tainted = TRUE;
1372 gst_single_queue_stop (GstMultiQueue * mq, GstSingleQueue * sq)
1374 gboolean result = FALSE;
1375 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
1377 GST_LOG_OBJECT (mq, "SingleQueue %d : stopping task", sq->id);
1379 result = gst_pad_stop_task (srcpad);
1380 gst_object_unref (srcpad);
1382 sq->sink_tainted = sq->src_tainted = TRUE;
1388 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
1391 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
1395 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1396 sq->srcresult = GST_FLOW_FLUSHING;
1397 gst_data_queue_set_flushing (sq->queue, TRUE);
1399 sq->flushing = TRUE;
1401 /* wake up non-linked task */
1402 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
1404 g_cond_signal (&sq->turn);
1405 sq->last_query = FALSE;
1406 g_cond_signal (&sq->query_handled);
1407 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1409 gst_single_queue_flush_queue (sq, full);
1411 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1412 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1413 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1414 sq->has_src_segment = FALSE;
1415 /* All pads start off OK for a smooth kick-off */
1416 sq->srcresult = GST_FLOW_OK;
1419 sq->max_size.visible = mq->max_size.visible;
1421 sq->is_segment_done = FALSE;
1424 sq->last_oldid = G_MAXUINT32;
1425 sq->next_time = GST_CLOCK_STIME_NONE;
1426 sq->last_time = GST_CLOCK_STIME_NONE;
1427 sq->cached_sinktime = GST_CLOCK_STIME_NONE;
1428 sq->group_high_time = GST_CLOCK_STIME_NONE;
1429 gst_data_queue_set_flushing (sq->queue, FALSE);
1431 /* We will become active again on the next buffer/gap */
1434 /* Reset high time to be recomputed next */
1435 mq->high_time = GST_CLOCK_STIME_NONE;
1437 sq->flushing = FALSE;
1438 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1442 /* WITH LOCK TAKEN */
1444 get_buffering_level (GstMultiQueue * mq, GstSingleQueue * sq)
1446 GstDataQueueSize size;
1447 gint buffering_level, tmp;
1449 gst_data_queue_get_level (sq->queue, &size);
1451 GST_DEBUG_OBJECT (mq,
1452 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1453 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
1454 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1456 /* get bytes and time buffer levels and take the max */
1457 if (sq->is_eos || sq->is_segment_done || sq->srcresult == GST_FLOW_NOT_LINKED
1459 buffering_level = MAX_BUFFERING_LEVEL;
1461 buffering_level = 0;
1462 if (sq->max_size.time > 0) {
1464 gst_util_uint64_scale (sq->cur_time,
1465 MAX_BUFFERING_LEVEL, sq->max_size.time);
1466 buffering_level = MAX (buffering_level, tmp);
1468 if (sq->max_size.bytes > 0) {
1470 gst_util_uint64_scale_int (size.bytes,
1471 MAX_BUFFERING_LEVEL, sq->max_size.bytes);
1472 buffering_level = MAX (buffering_level, tmp);
1476 return buffering_level;
1479 /* WITH LOCK TAKEN */
1481 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
1483 gint buffering_level, percent;
1485 /* nothing to dowhen we are not in buffering mode */
1486 if (!mq->use_buffering)
1489 buffering_level = get_buffering_level (mq, sq);
1491 /* scale so that if buffering_level equals the high watermark,
1492 * the percentage is 100% */
1493 percent = gst_util_uint64_scale (buffering_level, 100, mq->high_watermark);
1498 if (mq->buffering) {
1499 if (buffering_level >= mq->high_watermark) {
1500 mq->buffering = FALSE;
1502 /* make sure it increases */
1503 percent = MAX (mq->buffering_percent, percent);
1505 SET_PERCENT (mq, percent);
1508 gboolean is_buffering = TRUE;
1510 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
1511 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
1513 if (get_buffering_level (mq, oq) >= mq->high_watermark) {
1514 is_buffering = FALSE;
1520 if (is_buffering && buffering_level < mq->low_watermark) {
1521 mq->buffering = TRUE;
1522 SET_PERCENT (mq, percent);
1528 gst_multi_queue_post_buffering (GstMultiQueue * mq)
1530 GstMessage *msg = NULL;
1532 g_mutex_lock (&mq->buffering_post_lock);
1533 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1534 if (mq->buffering_percent_changed) {
1535 gint percent = mq->buffering_percent;
1537 mq->buffering_percent_changed = FALSE;
1539 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
1540 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
1542 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1545 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
1547 g_mutex_unlock (&mq->buffering_post_lock);
1551 recheck_buffering_status (GstMultiQueue * mq)
1553 if (!mq->use_buffering && mq->buffering) {
1554 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1555 mq->buffering = FALSE;
1556 GST_DEBUG_OBJECT (mq,
1557 "Buffering property disabled, but queue was still buffering; "
1558 "setting buffering percentage to 100%%");
1559 SET_PERCENT (mq, 100);
1560 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1563 if (mq->use_buffering) {
1567 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1569 /* force buffering percentage to be recalculated */
1570 old_perc = mq->buffering_percent;
1571 mq->buffering_percent = 0;
1575 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
1576 update_buffering (mq, q);
1577 gst_data_queue_limits_changed (q->queue);
1578 tmp = g_list_next (tmp);
1581 GST_DEBUG_OBJECT (mq,
1582 "Recalculated buffering percentage: old: %d%% new: %d%%",
1583 old_perc, mq->buffering_percent);
1585 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1588 gst_multi_queue_post_buffering (mq);
1592 calculate_interleave (GstMultiQueue * mq, GstSingleQueue * sq)
1594 GstClockTimeDiff low, high;
1595 GstClockTime interleave, other_interleave = 0;
1596 gboolean some_inactive = FALSE;
1599 low = high = GST_CLOCK_STIME_NONE;
1600 interleave = mq->interleave;
1601 /* Go over all single queues and calculate lowest/highest value */
1602 for (tmp = mq->queues; tmp; tmp = tmp->next) {
1603 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1604 /* Ignore sparse streams for interleave calculation */
1608 /* If some streams aren't active yet (haven't received any buffers), we will
1609 * grow interleave accordingly */
1611 some_inactive = TRUE;
1615 /* Calculate within each streaming thread */
1616 if (sq && sq->thread != oq->thread) {
1617 if (oq->interleave > other_interleave)
1618 other_interleave = oq->interleave;
1622 /* If the stream isn't EOS, update the low/high input value */
1623 if (GST_CLOCK_STIME_IS_VALID (oq->cached_sinktime) && !oq->is_eos) {
1624 if (low == GST_CLOCK_STIME_NONE || oq->cached_sinktime < low)
1625 low = oq->cached_sinktime;
1626 if (high == GST_CLOCK_STIME_NONE || oq->cached_sinktime > high)
1627 high = oq->cached_sinktime;
1629 /* If the input is before the segment start, consider as inactive to allow
1630 * the interleave to grow until *all* streams have data within the segment.
1632 * The reason for this is that there is no requirements for data before
1633 * the segment start to be "aligned" and therefore interleave calculation
1634 * can't reliably be done. For example a demuxer could provide video data
1635 * from the previous keyframe but audio only from just before the segment
1637 if (oq->cached_sinktime < 0)
1638 some_inactive = TRUE;
1641 "queue %d , sinktime:%" GST_STIME_FORMAT " low:%" GST_STIME_FORMAT
1642 " high:%" GST_STIME_FORMAT, oq->id,
1643 GST_STIME_ARGS (oq->cached_sinktime), GST_STIME_ARGS (low),
1644 GST_STIME_ARGS (high));
1647 if (GST_CLOCK_STIME_IS_VALID (low) && GST_CLOCK_STIME_IS_VALID (high)) {
1648 gboolean do_update = high == low;
1649 interleave = high - low;
1650 /* Padding of interleave and minimum value */
1651 interleave = (150 * interleave / 100) + mq->min_interleave_time;
1653 sq->interleave = interleave;
1655 interleave = MAX (interleave, other_interleave);
1657 /* Progressively grow up the interleave up to 5s if some streams were inactive */
1658 if (some_inactive && interleave <= mq->interleave) {
1659 interleave = MIN (5 * GST_SECOND, mq->interleave + 500 * GST_MSECOND);
1663 /* We force the interleave update if:
1664 * * the interleave was previously set while some streams were not active
1665 * yet but they now all are
1666 * * OR the interleave was previously based on all streams being active
1667 * whereas some now aren't
1669 if (mq->interleave_incomplete != some_inactive)
1672 mq->interleave_incomplete = some_inactive;
1674 /* Update the stored interleave if:
1675 * * No data has arrived yet (high == low)
1676 * * Or it went higher
1677 * * Or it went lower and we've gone past the previous interleave needed */
1678 if (do_update || interleave > mq->interleave ||
1679 ((mq->last_interleave_update + (2 * MIN (GST_SECOND,
1680 mq->interleave)) < low)
1681 && interleave < (mq->interleave * 3 / 4))) {
1682 /* Update the interleave */
1683 mq->interleave = interleave;
1684 mq->last_interleave_update = high;
1685 /* Update max-size time */
1686 mq->max_size.time = mq->interleave;
1687 SET_CHILD_PROPERTY (mq, time);
1691 GST_DEBUG_OBJECT (mq,
1692 "low:%" GST_STIME_FORMAT " high:%" GST_STIME_FORMAT " interleave:%"
1693 GST_TIME_FORMAT " mq->interleave:%" GST_TIME_FORMAT
1694 " last_interleave_update:%" GST_STIME_FORMAT, GST_STIME_ARGS (low),
1695 GST_STIME_ARGS (high), GST_TIME_ARGS (interleave),
1696 GST_TIME_ARGS (mq->interleave),
1697 GST_STIME_ARGS (mq->last_interleave_update));
1701 /* calculate the diff between running time on the sink and src of the queue.
1702 * This is the total amount of time in the queue.
1703 * WITH LOCK TAKEN */
1705 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
1707 GstClockTimeDiff sink_time, src_time;
1709 if (sq->sink_tainted) {
1710 sink_time = sq->sinktime = my_segment_to_running_time (&sq->sink_segment,
1711 sq->sink_segment.position);
1713 GST_DEBUG_OBJECT (mq,
1714 "queue %d sink_segment.position:%" GST_TIME_FORMAT ", sink_time:%"
1715 GST_STIME_FORMAT, sq->id, GST_TIME_ARGS (sq->sink_segment.position),
1716 GST_STIME_ARGS (sink_time));
1718 if (G_UNLIKELY (sq->last_time == GST_CLOCK_STIME_NONE)) {
1719 /* If the single queue still doesn't have a last_time set, this means
1720 * that nothing has been pushed out yet.
1721 * In order for the high_time computation to be as efficient as possible,
1722 * we set the last_time */
1723 sq->last_time = sink_time;
1725 if (G_UNLIKELY (sink_time != GST_CLOCK_STIME_NONE)) {
1726 /* if we have a time, we become untainted and use the time */
1727 sq->sink_tainted = FALSE;
1728 if (mq->use_interleave) {
1729 sq->cached_sinktime = sink_time;
1730 calculate_interleave (mq, sq);
1734 sink_time = sq->sinktime;
1736 if (sq->src_tainted) {
1737 GstSegment *segment;
1740 if (sq->has_src_segment) {
1741 segment = &sq->src_segment;
1742 position = sq->src_segment.position;
1745 * If the src pad had no segment yet, use the sink segment
1746 * to avoid signalling overrun if the received sink segment has a
1747 * a position > max-size-time while the src pad time would be the default=0
1749 * This can happen when switching pads on chained/adaptive streams and the
1750 * new chain has a segment with a much larger position
1752 segment = &sq->sink_segment;
1753 position = sq->sink_segment.position;
1756 src_time = sq->srctime = my_segment_to_running_time (segment, position);
1757 /* if we have a time, we become untainted and use the time */
1758 if (G_UNLIKELY (src_time != GST_CLOCK_STIME_NONE)) {
1759 sq->src_tainted = FALSE;
1762 src_time = sq->srctime;
1764 GST_DEBUG_OBJECT (mq,
1765 "queue %d, sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT, sq->id,
1766 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
1768 /* This allows for streams with out of order timestamping - sometimes the
1769 * emerging timestamp is later than the arriving one(s) */
1770 if (G_LIKELY (GST_CLOCK_STIME_IS_VALID (sink_time) &&
1771 GST_CLOCK_STIME_IS_VALID (src_time) && sink_time > src_time))
1772 sq->cur_time = sink_time - src_time;
1776 /* updating the time level can change the buffering state */
1777 update_buffering (mq, sq);
1782 /* take a SEGMENT event and apply the values to segment, updating the time
1783 * level of queue. */
1785 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1786 GstSegment * segment)
1788 GstClockTimeDiff ppos = 0;
1790 /* If we switched groups, grab the previous position */
1791 if (segment->rate > 0.0) {
1792 if (segment == &sq->sink_segment && sq->sink_stream_gid_changed) {
1794 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
1796 sq->sink_stream_gid_changed = FALSE;
1797 } else if (segment == &sq->src_segment && sq->src_stream_gid_changed) {
1799 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
1801 sq->src_stream_gid_changed = FALSE;
1805 gst_event_copy_segment (event, segment);
1807 /* now configure the values, we use these to track timestamps on the
1809 if (segment->format != GST_FORMAT_TIME) {
1810 /* non-time format, pretent the current time segment is closed with a
1811 * 0 start and unknown stop time. */
1812 segment->format = GST_FORMAT_TIME;
1817 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1820 GST_DEBUG_OBJECT (mq, "queue %d, Applying base of %" GST_TIME_FORMAT,
1821 sq->id, GST_TIME_ARGS (ppos));
1822 segment->base = ppos;
1825 /* Make sure we have a valid initial segment position (and not garbage
1827 if (segment->rate > 0.0)
1828 segment->position = segment->start;
1830 segment->position = segment->stop;
1832 if (segment == &sq->sink_segment)
1833 sq->sink_tainted = TRUE;
1835 sq->has_src_segment = TRUE;
1836 sq->src_tainted = TRUE;
1839 GST_DEBUG_OBJECT (mq,
1840 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1842 /* segment can update the time level of the queue */
1843 update_time_level (mq, sq);
1845 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1846 gst_multi_queue_post_buffering (mq);
1849 /* take a buffer and update segment, updating the time level of the queue. */
1851 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1852 GstClockTime duration, GstSegment * segment)
1854 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1856 /* if no timestamp is set, assume it's continuous with the previous
1858 if (timestamp == GST_CLOCK_TIME_NONE)
1859 timestamp = segment->position;
1862 if (duration != GST_CLOCK_TIME_NONE)
1863 timestamp += duration;
1865 GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1866 sq->id, segment == &sq->sink_segment ? "sink" : "src",
1867 GST_TIME_ARGS (timestamp));
1869 segment->position = timestamp;
1871 if (segment == &sq->sink_segment)
1872 sq->sink_tainted = TRUE;
1874 sq->src_tainted = TRUE;
1876 /* calc diff with other end */
1877 update_time_level (mq, sq);
1878 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1879 gst_multi_queue_post_buffering (mq);
1883 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1884 GstSegment * segment)
1886 GstClockTime timestamp;
1887 GstClockTime duration;
1889 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1891 gst_event_parse_gap (event, ×tamp, &duration);
1893 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1895 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1896 timestamp += duration;
1899 GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1900 sq->id, segment == &sq->sink_segment ? "sink" : "src",
1901 GST_TIME_ARGS (timestamp));
1903 segment->position = timestamp;
1905 if (segment == &sq->sink_segment)
1906 sq->sink_tainted = TRUE;
1908 sq->src_tainted = TRUE;
1910 /* calc diff with other end */
1911 update_time_level (mq, sq);
1914 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1915 gst_multi_queue_post_buffering (mq);
1918 static GstClockTimeDiff
1919 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1921 GstClockTimeDiff time = GST_CLOCK_STIME_NONE;
1923 if (GST_IS_BUFFER (object)) {
1924 GstBuffer *buf = GST_BUFFER_CAST (object);
1925 GstClockTime btime = GST_BUFFER_DTS_OR_PTS (buf);
1927 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1928 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1929 btime += GST_BUFFER_DURATION (buf);
1930 time = my_segment_to_running_time (segment, btime);
1932 } else if (GST_IS_BUFFER_LIST (object)) {
1933 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1937 n = gst_buffer_list_length (list);
1938 for (i = 0; i < n; i++) {
1940 buf = gst_buffer_list_get (list, i);
1941 btime = GST_BUFFER_DTS_OR_PTS (buf);
1942 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1943 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1944 btime += GST_BUFFER_DURATION (buf);
1945 time = my_segment_to_running_time (segment, btime);
1952 } else if (GST_IS_EVENT (object)) {
1953 GstEvent *event = GST_EVENT_CAST (object);
1955 /* For newsegment events return the running time of the start position */
1956 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1957 const GstSegment *new_segment;
1959 gst_event_parse_segment (event, &new_segment);
1960 if (new_segment->format == GST_FORMAT_TIME) {
1962 my_segment_to_running_time ((GstSegment *) new_segment,
1963 new_segment->start);
1965 } else if (GST_EVENT_TYPE (event) == GST_EVENT_GAP) {
1966 GstClockTime ts, dur;
1967 gst_event_parse_gap (event, &ts, &dur);
1968 if (GST_CLOCK_TIME_IS_VALID (ts)) {
1969 if (GST_CLOCK_TIME_IS_VALID (dur))
1971 time = my_segment_to_running_time (segment, ts);
1980 static GstFlowReturn
1981 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1982 GstMiniObject * object, gboolean * allow_drop)
1984 GstFlowReturn result = sq->srcresult;
1985 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
1988 GST_INFO_OBJECT (mq,
1989 "Pushing while corresponding sourcepad has been cleared");
1990 return GST_FLOW_FLUSHING;
1993 if (GST_IS_BUFFER (object)) {
1995 GstClockTime timestamp, duration;
1997 buffer = GST_BUFFER_CAST (object);
1998 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1999 duration = GST_BUFFER_DURATION (buffer);
2001 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
2003 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
2004 gst_data_queue_limits_changed (sq->queue);
2006 if (G_UNLIKELY (*allow_drop)) {
2007 GST_DEBUG_OBJECT (mq,
2008 "SingleQueue %d : Dropping EOS buffer %p with ts %" GST_TIME_FORMAT,
2009 sq->id, buffer, GST_TIME_ARGS (timestamp));
2010 gst_buffer_unref (buffer);
2012 GST_DEBUG_OBJECT (mq,
2013 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
2014 sq->id, buffer, GST_TIME_ARGS (timestamp));
2015 result = gst_pad_push (srcpad, buffer);
2017 } else if (GST_IS_EVENT (object)) {
2020 event = GST_EVENT_CAST (object);
2022 switch (GST_EVENT_TYPE (event)) {
2023 case GST_EVENT_SEGMENT_DONE:
2024 *allow_drop = FALSE;
2027 result = GST_FLOW_EOS;
2028 if (G_UNLIKELY (*allow_drop))
2029 *allow_drop = FALSE;
2031 case GST_EVENT_STREAM_START:
2034 if (gst_event_parse_group_id (event, &group_id)) {
2035 if (sq->src_stream_gid == GST_GROUP_ID_INVALID) {
2036 sq->src_stream_gid = group_id;
2037 } else if (group_id != sq->src_stream_gid) {
2038 sq->src_stream_gid = group_id;
2039 sq->src_stream_gid_changed = TRUE;
2042 result = GST_FLOW_OK;
2043 if (G_UNLIKELY (*allow_drop))
2044 *allow_drop = FALSE;
2047 case GST_EVENT_SEGMENT:
2048 apply_segment (mq, sq, event, &sq->src_segment);
2049 /* Applying the segment may have made the queue non-full again, unblock it if needed */
2050 gst_data_queue_limits_changed (sq->queue);
2051 if (G_UNLIKELY (*allow_drop)) {
2052 result = GST_FLOW_OK;
2053 *allow_drop = FALSE;
2057 apply_gap (mq, sq, event, &sq->src_segment);
2058 /* Applying the gap may have made the queue non-full again, unblock it if needed */
2059 gst_data_queue_limits_changed (sq->queue);
2065 if (G_UNLIKELY (*allow_drop)) {
2066 GST_DEBUG_OBJECT (mq,
2067 "SingleQueue %d : Dropping EOS event %p of type %s",
2068 sq->id, event, GST_EVENT_TYPE_NAME (event));
2069 gst_event_unref (event);
2071 GST_DEBUG_OBJECT (mq,
2072 "SingleQueue %d : Pushing event %p of type %s",
2073 sq->id, event, GST_EVENT_TYPE_NAME (event));
2075 gst_pad_push_event (srcpad, event);
2077 } else if (GST_IS_QUERY (object)) {
2081 query = GST_QUERY_CAST (object);
2083 if (G_UNLIKELY (*allow_drop)) {
2084 GST_DEBUG_OBJECT (mq,
2085 "SingleQueue %d : Dropping EOS query %p", sq->id, query);
2086 gst_query_unref (query);
2089 res = gst_pad_peer_query (srcpad, query);
2092 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2093 sq->last_query = res;
2094 sq->last_handled_query = query;
2095 g_cond_signal (&sq->query_handled);
2096 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2098 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
2102 gst_object_unref (srcpad);
2108 static GstMiniObject *
2109 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
2114 item->object = NULL;
2120 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
2122 if (!item->is_query && item->object)
2123 gst_mini_object_unref (item->object);
2124 g_slice_free (GstMultiQueueItem, item);
2127 /* takes ownership of passed mini object! */
2128 static GstMultiQueueItem *
2129 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
2131 GstMultiQueueItem *item;
2133 item = g_slice_new (GstMultiQueueItem);
2134 item->object = object;
2135 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
2136 item->posid = curid;
2137 item->is_query = GST_IS_QUERY (object);
2139 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
2140 item->duration = GST_BUFFER_DURATION (object);
2141 if (item->duration == GST_CLOCK_TIME_NONE)
2143 item->visible = TRUE;
2147 static GstMultiQueueItem *
2148 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
2150 GstMultiQueueItem *item;
2152 item = g_slice_new (GstMultiQueueItem);
2153 item->object = object;
2154 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
2155 item->posid = curid;
2156 item->is_query = GST_IS_QUERY (object);
2160 item->visible = FALSE;
2164 /* Each main loop attempts to push buffers until the return value
2165 * is not-linked. not-linked pads are not allowed to push data beyond
2166 * any linked pads, so they don't 'rush ahead of the pack'.
2169 gst_multi_queue_loop (GstPad * pad)
2172 GstMultiQueueItem *item;
2173 GstDataQueueItem *sitem;
2175 GstMiniObject *object = NULL;
2177 GstFlowReturn result;
2178 GstClockTimeDiff next_time;
2180 gboolean is_query = FALSE;
2181 gboolean do_update_buffering = FALSE;
2182 gboolean dropping = FALSE;
2183 GstPad *srcpad = NULL;
2185 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2186 mq = g_weak_ref_get (&sq->mqueue);
2187 srcpad = g_weak_ref_get (&sq->srcpad);
2193 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
2198 /* Get something from the queue, blocking until that happens, or we get
2200 if (!(gst_data_queue_pop (sq->queue, &sitem)))
2203 item = (GstMultiQueueItem *) sitem;
2204 newid = item->posid;
2206 is_query = item->is_query;
2208 /* steal the object and destroy the item */
2209 object = gst_multi_queue_item_steal_object (item);
2210 gst_multi_queue_item_destroy (item);
2212 is_buffer = GST_IS_BUFFER (object);
2214 /* Get running time of the item. Events will have GST_CLOCK_STIME_NONE */
2215 next_time = get_running_time (&sq->src_segment, object, FALSE);
2217 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
2218 sq->id, newid, sq->last_oldid);
2220 /* If we're not-linked, we do some extra work because we might need to
2221 * wait before pushing. If we're linked but there's a gap in the IDs,
2222 * or it's the first loop, or we just passed the previous highid,
2223 * we might need to wake some sleeping pad up, so there's extra work
2225 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2226 if (sq->srcresult == GST_FLOW_NOT_LINKED
2227 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
2228 || sq->last_oldid > mq->highid) {
2229 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
2230 gst_flow_get_name (sq->srcresult));
2232 /* Check again if we're flushing after the lock is taken,
2233 * the flush flag might have been changed in the meantime */
2235 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2239 /* Update the nextid so other threads know when to wake us up */
2241 /* Take into account the extra cache time since we're unlinked */
2242 if (GST_CLOCK_STIME_IS_VALID (next_time))
2243 next_time += mq->unlinked_cache_time;
2244 sq->next_time = next_time;
2246 /* Update the oldid (the last ID we output) for highid tracking */
2247 if (sq->last_oldid != G_MAXUINT32)
2248 sq->oldid = sq->last_oldid;
2250 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2251 gboolean should_wait;
2252 /* Go to sleep until it's time to push this buffer */
2254 /* Recompute the highid */
2255 compute_high_id (mq);
2256 /* Recompute the high time */
2257 compute_high_time (mq, sq->groupid);
2259 GST_DEBUG_OBJECT (mq,
2260 "groupid %d high_time %" GST_STIME_FORMAT " next_time %"
2261 GST_STIME_FORMAT, sq->groupid, GST_STIME_ARGS (sq->group_high_time),
2262 GST_STIME_ARGS (next_time));
2264 if (mq->sync_by_running_time) {
2265 if (sq->group_high_time == GST_CLOCK_STIME_NONE) {
2266 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
2267 (mq->high_time == GST_CLOCK_STIME_NONE
2268 || next_time > mq->high_time);
2270 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
2271 next_time > sq->group_high_time;
2274 should_wait = newid > mq->highid;
2276 while (should_wait && sq->srcresult == GST_FLOW_NOT_LINKED) {
2278 GST_DEBUG_OBJECT (mq,
2279 "queue %d sleeping for not-linked wakeup with "
2280 "newid %u, highid %u, next_time %" GST_STIME_FORMAT
2281 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
2282 GST_STIME_ARGS (next_time), GST_STIME_ARGS (sq->group_high_time));
2284 /* Wake up all non-linked pads before we sleep */
2285 wake_up_next_non_linked (mq);
2288 g_cond_wait (&sq->turn, &mq->qlock);
2292 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2296 /* Recompute the high time and ID */
2297 compute_high_time (mq, sq->groupid);
2298 compute_high_id (mq);
2300 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
2301 "wakeup with newid %u, highid %u, next_time %" GST_STIME_FORMAT
2302 ", high_time %" GST_STIME_FORMAT " mq high_time %" GST_STIME_FORMAT,
2303 sq->id, newid, mq->highid,
2304 GST_STIME_ARGS (next_time), GST_STIME_ARGS (sq->group_high_time),
2305 GST_STIME_ARGS (mq->high_time));
2307 if (mq->sync_by_running_time) {
2308 if (sq->group_high_time == GST_CLOCK_STIME_NONE) {
2309 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
2310 (mq->high_time == GST_CLOCK_STIME_NONE
2311 || next_time > mq->high_time);
2313 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
2314 next_time > sq->group_high_time;
2317 should_wait = newid > mq->highid;
2320 /* Re-compute the high_id in case someone else pushed */
2321 compute_high_id (mq);
2322 compute_high_time (mq, sq->groupid);
2324 compute_high_id (mq);
2325 compute_high_time (mq, sq->groupid);
2326 /* Wake up all non-linked pads */
2327 wake_up_next_non_linked (mq);
2329 /* We're done waiting, we can clear the nextid and nexttime */
2331 sq->next_time = GST_CLOCK_STIME_NONE;
2333 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2338 GST_LOG_OBJECT (mq, "sq:%d BEFORE PUSHING sq->srcresult: %s", sq->id,
2339 gst_flow_get_name (sq->srcresult));
2341 /* Update time stats */
2342 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2343 next_time = get_running_time (&sq->src_segment, object, TRUE);
2344 if (GST_CLOCK_STIME_IS_VALID (next_time)) {
2345 if (sq->last_time == GST_CLOCK_STIME_NONE || sq->last_time < next_time)
2346 sq->last_time = next_time;
2347 if (mq->high_time == GST_CLOCK_STIME_NONE || mq->high_time <= next_time) {
2348 /* Wake up all non-linked pads now that we advanced the high time */
2349 mq->high_time = next_time;
2350 wake_up_next_non_linked (mq);
2353 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2355 /* Try to push out the new object */
2356 result = gst_single_queue_push_one (mq, sq, object, &dropping);
2359 /* Check if we pushed something already and if this is
2360 * now a switch from an active to a non-active stream.
2362 * If it is, we reset all the waiting streams, let them
2363 * push another buffer to see if they're now active again.
2364 * This allows faster switching between streams and prevents
2365 * deadlocks if downstream does any waiting too.
2367 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2368 if (sq->pushed && sq->srcresult == GST_FLOW_OK
2369 && result == GST_FLOW_NOT_LINKED) {
2372 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
2375 compute_high_id (mq);
2376 compute_high_time (mq, sq->groupid);
2377 do_update_buffering = TRUE;
2379 /* maybe no-one is waiting */
2380 if (mq->numwaiting > 0) {
2381 /* Else figure out which singlequeue(s) need waking up */
2382 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2383 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2385 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
2386 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
2387 sq2->pushed = FALSE;
2388 sq2->srcresult = GST_FLOW_OK;
2389 g_cond_signal (&sq2->turn);
2398 /* now hold on a bit;
2399 * can not simply throw this result to upstream, because
2400 * that might already be onto another segment, so we have to make
2401 * sure we are relaying the correct info wrt proper segment */
2402 if (result == GST_FLOW_EOS && !dropping &&
2403 sq->srcresult != GST_FLOW_NOT_LINKED) {
2404 GST_DEBUG_OBJECT (mq, "starting EOS drop on sq %d", sq->id);
2406 /* pretend we have not seen EOS yet for upstream's sake */
2407 result = sq->srcresult;
2408 } else if (dropping && gst_data_queue_is_empty (sq->queue)) {
2409 /* queue empty, so stop dropping
2410 * we can commit the result we have now,
2411 * which is either OK after a segment, or EOS */
2412 GST_DEBUG_OBJECT (mq, "committed EOS drop on sq %d", sq->id);
2414 result = GST_FLOW_EOS;
2416 sq->srcresult = result;
2417 sq->last_oldid = newid;
2419 if (do_update_buffering)
2420 update_buffering (mq, sq);
2422 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2423 gst_multi_queue_post_buffering (mq);
2425 GST_LOG_OBJECT (mq, "sq:%d AFTER PUSHING sq->srcresult: %s (is_eos:%d)",
2426 sq->id, gst_flow_get_name (sq->srcresult), GST_PAD_IS_EOS (srcpad));
2428 /* Need to make sure wake up any sleeping pads when we exit */
2429 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2430 if (mq->numwaiting > 0 && (GST_PAD_IS_EOS (srcpad)
2431 || sq->srcresult == GST_FLOW_EOS)) {
2432 compute_high_time (mq, sq->groupid);
2433 compute_high_id (mq);
2434 wake_up_next_non_linked (mq);
2436 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2441 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
2442 && result != GST_FLOW_EOS)
2446 gst_clear_object (&mq);
2447 gst_clear_object (&srcpad);
2453 if (object && !is_query)
2454 gst_mini_object_unref (object);
2456 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2457 sq->last_query = FALSE;
2458 g_cond_signal (&sq->query_handled);
2460 /* Post an error message if we got EOS while downstream
2461 * has returned an error flow return. After EOS there
2462 * will be no further buffer which could propagate the
2464 if ((sq->is_eos || sq->is_segment_done) && sq->srcresult < GST_FLOW_EOS) {
2465 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2466 GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
2468 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2471 /* upstream needs to see fatal result ASAP to shut things down,
2472 * but might be stuck in one of our other full queues;
2473 * so empty this one and trigger dynamic queue growth. At
2474 * this point the srcresult is not OK, NOT_LINKED
2475 * or EOS, i.e. a real failure */
2476 gst_single_queue_flush_queue (sq, FALSE);
2477 single_queue_underrun_cb (sq->queue, sq);
2478 gst_data_queue_set_flushing (sq->queue, TRUE);
2479 gst_pad_pause_task (srcpad);
2480 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
2481 "SingleQueue[%d] task paused, reason:%s",
2482 sq->id, gst_flow_get_name (sq->srcresult));
2488 * gst_multi_queue_chain:
2490 * This is similar to GstQueue's chain function, except:
2491 * _ we don't have leak behaviours,
2492 * _ we push with a unique id (curid)
2494 static GstFlowReturn
2495 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2499 GstMultiQueueItem *item = NULL;
2501 GstClockTime timestamp, duration;
2503 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2504 mq = g_weak_ref_get (&sq->mqueue);
2509 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2515 /* Get a unique incrementing id */
2516 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2518 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
2519 duration = GST_BUFFER_DURATION (buffer);
2522 "SingleQueue %d : about to enqueue buffer %p with id %d (pts:%"
2523 GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT " dur:%" GST_TIME_FORMAT ")",
2524 sq->id, buffer, curid, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2525 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (duration));
2527 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
2529 /* Update interleave before pushing data into queue */
2530 if (mq->use_interleave) {
2531 GstClockTime val = timestamp;
2532 GstClockTimeDiff dval;
2534 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2535 if (val == GST_CLOCK_TIME_NONE)
2536 val = sq->sink_segment.position;
2537 if (duration != GST_CLOCK_TIME_NONE)
2540 dval = my_segment_to_running_time (&sq->sink_segment, val);
2541 if (GST_CLOCK_STIME_IS_VALID (dval)) {
2542 sq->cached_sinktime = dval;
2543 GST_DEBUG_OBJECT (mq,
2544 "Queue %d cached sink time now %" G_GINT64_FORMAT " %"
2545 GST_STIME_FORMAT, sq->id, sq->cached_sinktime,
2546 GST_STIME_ARGS (sq->cached_sinktime));
2547 calculate_interleave (mq, sq);
2549 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2552 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
2555 /* update time level, we must do this after pushing the data in the queue so
2556 * that we never end up filling the queue first. */
2557 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
2560 gst_clear_object (&mq);
2561 return sq->srcresult;
2566 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2567 sq->id, gst_flow_get_name (sq->srcresult));
2569 gst_multi_queue_item_destroy (item);
2574 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
2575 gst_buffer_unref (buffer);
2576 gst_object_unref (mq);
2577 return GST_FLOW_EOS;
2582 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
2583 GstPadMode mode, gboolean active)
2589 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2590 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
2592 /* mq is NULL if the pad is activated/deactivated before being
2593 * added to the multiqueue */
2595 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2598 case GST_PAD_MODE_PUSH:
2600 /* All pads start off linked until they push one buffer */
2601 sq->srcresult = GST_FLOW_OK;
2603 gst_data_queue_set_flushing (sq->queue, FALSE);
2605 sq->srcresult = GST_FLOW_FLUSHING;
2606 sq->last_query = FALSE;
2607 g_cond_signal (&sq->query_handled);
2608 gst_data_queue_set_flushing (sq->queue, TRUE);
2610 /* Wait until streaming thread has finished */
2612 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2613 GST_PAD_STREAM_LOCK (pad);
2615 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2616 gst_data_queue_flush (sq->queue);
2618 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2619 GST_PAD_STREAM_UNLOCK (pad);
2621 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2631 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2632 gst_object_unref (mq);
2638 static GstFlowReturn
2639 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2644 GstMultiQueueItem *item;
2645 gboolean res = TRUE;
2646 GstFlowReturn flowret = GST_FLOW_OK;
2648 GstEvent *sref = NULL;
2652 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2653 mq = (GstMultiQueue *) parent;
2654 srcpad = g_weak_ref_get (&sq->srcpad);
2657 GST_INFO_OBJECT (pad, "Pushing while corresponding sourcepad has been"
2658 " removed already");
2660 return GST_FLOW_FLUSHING;
2663 type = GST_EVENT_TYPE (event);
2666 case GST_EVENT_STREAM_START:
2669 if (gst_event_parse_group_id (event, &group_id)) {
2670 if (sq->sink_stream_gid == GST_GROUP_ID_INVALID) {
2671 sq->sink_stream_gid = group_id;
2672 } else if (group_id != sq->sink_stream_gid) {
2673 sq->sink_stream_gid = group_id;
2674 sq->sink_stream_gid_changed = TRUE;
2677 if (mq->sync_by_running_time) {
2678 GstStreamFlags stream_flags;
2679 gst_event_parse_stream_flags (event, &stream_flags);
2680 if ((stream_flags & GST_STREAM_FLAG_SPARSE)) {
2681 GST_INFO_OBJECT (mq, "SingleQueue %d is a sparse stream", sq->id);
2682 sq->is_sparse = TRUE;
2686 sq->thread = g_thread_self ();
2688 /* Remove EOS flag */
2692 case GST_EVENT_FLUSH_START:
2693 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
2696 res = gst_pad_push_event (srcpad, event);
2698 gst_single_queue_flush (mq, sq, TRUE, FALSE);
2699 gst_single_queue_pause (mq, sq);
2702 case GST_EVENT_FLUSH_STOP:
2703 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
2706 res = gst_pad_push_event (srcpad, event);
2708 gst_single_queue_flush (mq, sq, FALSE, FALSE);
2709 gst_single_queue_start (mq, sq);
2712 case GST_EVENT_SEGMENT:
2713 sq->is_segment_done = FALSE;
2714 sref = gst_event_ref (event);
2717 /* take ref because the queue will take ownership and we need the event
2718 * afterwards to update the segment */
2719 sref = gst_event_ref (event);
2720 if (mq->use_interleave) {
2721 GstClockTime val, dur;
2723 gst_event_parse_gap (event, &val, &dur);
2724 if (GST_CLOCK_TIME_IS_VALID (val)) {
2725 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2726 if (GST_CLOCK_TIME_IS_VALID (dur))
2728 stime = my_segment_to_running_time (&sq->sink_segment, val);
2729 if (GST_CLOCK_STIME_IS_VALID (stime)) {
2730 sq->cached_sinktime = stime;
2731 calculate_interleave (mq, sq);
2733 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2739 if (!(GST_EVENT_IS_SERIALIZED (event))) {
2740 res = gst_pad_push_event (srcpad, event);
2746 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2750 /* Get an unique incrementing id. */
2751 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2753 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
2755 GST_DEBUG_OBJECT (mq,
2756 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
2757 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
2759 if (!gst_data_queue_push (sq->queue, (GstDataQueueItem *) item))
2762 /* mark EOS when we received one, we must do that after putting the
2763 * buffer in the queue because EOS marks the buffer as filled. */
2765 case GST_EVENT_SEGMENT_DONE:
2766 sq->is_segment_done = TRUE;
2767 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2768 update_buffering (mq, sq);
2769 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2770 single_queue_overrun_cb (sq->queue, sq);
2771 gst_multi_queue_post_buffering (mq);
2774 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2777 /* Post an error message if we got EOS while downstream
2778 * has returned an error flow return. After EOS there
2779 * will be no further buffer which could propagate the
2781 if (sq->srcresult < GST_FLOW_EOS) {
2782 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2783 GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
2785 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2788 /* EOS affects the buffering state */
2789 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2790 update_buffering (mq, sq);
2791 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2792 single_queue_overrun_cb (sq->queue, sq);
2793 gst_multi_queue_post_buffering (mq);
2795 case GST_EVENT_SEGMENT:
2796 apply_segment (mq, sq, sref, &sq->sink_segment);
2797 gst_event_unref (sref);
2798 /* a new segment allows us to accept more buffers if we got EOS
2799 * from downstream */
2800 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2801 if (sq->srcresult == GST_FLOW_EOS)
2802 sq->srcresult = GST_FLOW_OK;
2803 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2807 apply_gap (mq, sq, sref, &sq->sink_segment);
2808 gst_event_unref (sref);
2815 gst_object_unref (srcpad);
2817 flowret = GST_FLOW_ERROR;
2818 GST_DEBUG_OBJECT (mq, "SingleQueue %d : returning %s", sq->id,
2819 gst_flow_get_name (flowret));
2824 gst_object_unref (srcpad);
2825 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2826 sq->id, gst_flow_get_name (sq->srcresult));
2828 gst_event_unref (sref);
2829 gst_multi_queue_item_destroy (item);
2830 return sq->srcresult;
2834 gst_object_unref (srcpad);
2835 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return GST_FLOW_EOS");
2836 gst_event_unref (event);
2837 return GST_FLOW_EOS;
2842 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
2848 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2849 mq = (GstMultiQueue *) parent;
2851 switch (GST_QUERY_TYPE (query)) {
2853 if (GST_QUERY_IS_SERIALIZED (query)) {
2855 GstMultiQueueItem *item;
2857 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2858 if (sq->srcresult != GST_FLOW_OK)
2861 /* serialized events go in the queue. We need to be certain that we
2862 * don't cause deadlocks waiting for the query return value. We check if
2863 * the queue is empty (nothing is blocking downstream and the query can
2864 * be pushed for sure) or we are not buffering. If we are buffering,
2865 * the pipeline waits to unblock downstream until our queue fills up
2866 * completely, which can not happen if we block on the query..
2867 * Therefore we only potentially block when we are not buffering. */
2868 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
2869 /* Get an unique incrementing id. */
2870 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2872 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
2874 GST_DEBUG_OBJECT (mq,
2875 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
2876 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
2877 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2878 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
2879 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2880 if (!res || sq->flushing)
2882 /* it might be that the query has been taken out of the queue
2883 * while we were unlocked. So, we need to check if the last
2884 * handled query is the same one than the one we just
2885 * pushed. If it is, we don't need to wait for the condition
2886 * variable, otherwise we wait for the condition variable to
2888 while (!sq->flushing && sq->srcresult == GST_FLOW_OK
2889 && sq->last_handled_query != query)
2890 g_cond_wait (&sq->query_handled, &mq->qlock);
2891 res = sq->last_query;
2892 sq->last_handled_query = NULL;
2894 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
2895 "queue is not empty");
2898 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2900 /* default handling */
2901 res = gst_pad_query_default (pad, parent, query);
2909 GST_DEBUG_OBJECT (mq, "Flushing");
2910 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2916 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2917 GstPadMode mode, gboolean active)
2923 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2924 mq = g_weak_ref_get (&sq->mqueue);
2927 GST_ERROR_OBJECT (pad, "No multique set anymore, can't activate pad");
2932 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2935 case GST_PAD_MODE_PUSH:
2937 gst_single_queue_flush (mq, sq, FALSE, TRUE);
2938 result = parent ? gst_single_queue_start (mq, sq) : TRUE;
2940 gst_single_queue_flush (mq, sq, TRUE, TRUE);
2941 result = gst_single_queue_stop (mq, sq);
2948 gst_object_unref (mq);
2953 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2955 GstSingleQueue *sq = GST_MULTIQUEUE_PAD (pad)->sq;
2956 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
2958 GstPad *sinkpad = g_weak_ref_get (&sq->sinkpad);
2960 if (!mq || !sinkpad) {
2961 gst_clear_object (&sinkpad);
2962 gst_clear_object (&mq);
2963 GST_INFO_OBJECT (pad, "No multique/sinkpad set anymore, flushing");
2968 switch (GST_EVENT_TYPE (event)) {
2969 case GST_EVENT_RECONFIGURE:
2970 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2971 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2972 sq->srcresult = GST_FLOW_OK;
2973 g_cond_signal (&sq->turn);
2975 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2977 ret = gst_pad_push_event (sinkpad, event);
2980 ret = gst_pad_push_event (sinkpad, event);
2984 gst_object_unref (sinkpad);
2985 gst_object_unref (mq);
2991 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2995 /* FIXME, Handle position offset depending on queue size */
2996 switch (GST_QUERY_TYPE (query)) {
2998 /* default handling */
2999 res = gst_pad_query_default (pad, parent, query);
3006 * Next-non-linked functions
3009 /* WITH LOCK TAKEN */
3011 wake_up_next_non_linked (GstMultiQueue * mq)
3015 /* maybe no-one is waiting */
3016 if (mq->numwaiting < 1)
3019 if (mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (mq->high_time)) {
3020 /* Else figure out which singlequeue(s) need waking up */
3021 for (tmp = mq->queues; tmp; tmp = tmp->next) {
3022 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3023 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
3024 GstClockTimeDiff high_time;
3026 if (GST_CLOCK_STIME_IS_VALID (sq->group_high_time))
3027 high_time = sq->group_high_time;
3029 high_time = mq->high_time;
3031 if (GST_CLOCK_STIME_IS_VALID (sq->next_time) &&
3032 GST_CLOCK_STIME_IS_VALID (high_time)
3033 && sq->next_time <= high_time) {
3034 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
3035 g_cond_signal (&sq->turn);
3040 /* Else figure out which singlequeue(s) need waking up */
3041 for (tmp = mq->queues; tmp; tmp = tmp->next) {
3042 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3043 if (sq->srcresult == GST_FLOW_NOT_LINKED &&
3044 sq->nextid != 0 && sq->nextid <= mq->highid) {
3045 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
3046 g_cond_signal (&sq->turn);
3052 /* WITH LOCK TAKEN */
3054 compute_high_id (GstMultiQueue * mq)
3056 /* The high-id is either the highest id among the linked pads, or if all
3057 * pads are not-linked, it's the lowest not-linked pad */
3059 guint32 lowest = G_MAXUINT32;
3060 guint32 highid = G_MAXUINT32;
3062 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
3063 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3064 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
3067 GST_INFO_OBJECT (mq,
3068 "srcpad has been removed already... ignoring single queue");
3073 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
3074 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
3076 /* No need to consider queues which are not waiting */
3077 if (sq->nextid == 0) {
3078 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
3079 gst_object_unref (srcpad);
3083 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
3084 if (sq->nextid < lowest)
3085 lowest = sq->nextid;
3086 } else if (!GST_PAD_IS_EOS (srcpad) && sq->srcresult != GST_FLOW_EOS) {
3087 /* If we don't have a global highid, or the global highid is lower than
3088 * this single queue's last outputted id, store the queue's one,
3089 * unless the singlequeue output is at EOS */
3090 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
3093 gst_object_unref (srcpad);
3096 if (highid == G_MAXUINT32 || lowest < highid)
3097 mq->highid = lowest;
3099 mq->highid = highid;
3101 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
3105 /* WITH LOCK TAKEN */
3107 compute_high_time (GstMultiQueue * mq, guint groupid)
3109 /* The high-time is either the highest last time among the linked
3110 * pads, or if all pads are not-linked, it's the lowest nex time of
3113 GstClockTimeDiff highest = GST_CLOCK_STIME_NONE;
3114 GstClockTimeDiff lowest = GST_CLOCK_STIME_NONE;
3115 GstClockTimeDiff group_high = GST_CLOCK_STIME_NONE;
3116 GstClockTimeDiff group_low = GST_CLOCK_STIME_NONE;
3117 GstClockTimeDiff res;
3118 /* Number of streams which belong to groupid */
3119 guint group_count = 0;
3121 if (!mq->sync_by_running_time)
3122 /* return GST_CLOCK_STIME_NONE; */
3125 for (tmp = mq->queues; tmp; tmp = tmp->next) {
3126 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3127 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
3130 GST_INFO_OBJECT (mq,
3131 "srcpad has been removed already... ignoring single queue");
3137 "inspecting sq:%d (group:%d) , next_time:%" GST_STIME_FORMAT
3138 ", last_time:%" GST_STIME_FORMAT ", srcresult:%s", sq->id, sq->groupid,
3139 GST_STIME_ARGS (sq->next_time), GST_STIME_ARGS (sq->last_time),
3140 gst_flow_get_name (sq->srcresult));
3142 if (sq->groupid == groupid)
3145 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
3146 /* No need to consider queues which are not waiting */
3147 if (!GST_CLOCK_STIME_IS_VALID (sq->next_time)) {
3148 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
3149 gst_object_unref (srcpad);
3153 if (lowest == GST_CLOCK_STIME_NONE || sq->next_time < lowest)
3154 lowest = sq->next_time;
3155 if (sq->groupid == groupid && (group_low == GST_CLOCK_STIME_NONE
3156 || sq->next_time < group_low))
3157 group_low = sq->next_time;
3158 } else if (!GST_PAD_IS_EOS (srcpad) && sq->srcresult != GST_FLOW_EOS) {
3159 /* If we don't have a global high time, or the global high time
3160 * is lower than this single queue's last outputted time, store
3161 * the queue's one, unless the singlequeue output is at EOS. */
3162 if (highest == GST_CLOCK_STIME_NONE
3163 || (sq->last_time != GST_CLOCK_STIME_NONE && sq->last_time > highest))
3164 highest = sq->last_time;
3165 if (sq->groupid == groupid && (group_high == GST_CLOCK_STIME_NONE
3166 || (sq->last_time != GST_CLOCK_STIME_NONE
3167 && sq->last_time > group_high)))
3168 group_high = sq->last_time;
3171 "highest now %" GST_STIME_FORMAT " lowest %" GST_STIME_FORMAT,
3172 GST_STIME_ARGS (highest), GST_STIME_ARGS (lowest));
3173 if (sq->groupid == groupid)
3175 "grouphigh %" GST_STIME_FORMAT " grouplow %" GST_STIME_FORMAT,
3176 GST_STIME_ARGS (group_high), GST_STIME_ARGS (group_low));
3178 gst_object_unref (srcpad);
3181 if (highest == GST_CLOCK_STIME_NONE)
3182 mq->high_time = lowest;
3184 mq->high_time = highest;
3186 /* If there's only one stream of a given type, use the global high */
3187 if (group_count < 2)
3188 res = GST_CLOCK_STIME_NONE;
3189 else if (group_high == GST_CLOCK_STIME_NONE)
3194 GST_LOG_OBJECT (mq, "group count %d for groupid %u", group_count, groupid);
3196 "MQ High time is now : %" GST_STIME_FORMAT ", group %d high time %"
3197 GST_STIME_FORMAT ", lowest non-linked %" GST_STIME_FORMAT,
3198 GST_STIME_ARGS (mq->high_time), groupid, GST_STIME_ARGS (mq->high_time),
3199 GST_STIME_ARGS (lowest));
3201 for (tmp = mq->queues; tmp; tmp = tmp->next) {
3202 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3203 if (groupid == sq->groupid)
3204 sq->group_high_time = res;
3208 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
3209 ((q)->max_size.format) <= (value))
3212 * GstSingleQueue functions
3215 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
3218 GstDataQueueSize size;
3219 gboolean filled = TRUE;
3220 gboolean empty_found = FALSE;
3221 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
3224 GST_ERROR ("No multique set anymore, not doing anything");
3229 gst_data_queue_get_level (sq->queue, &size);
3232 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
3233 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
3234 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
3237 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
3239 /* check if we reached the hard time/bytes limits;
3240 time limit is only taken into account for non-sparse streams */
3241 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
3242 (!sq->is_sparse && IS_FILLED (sq, time, sq->cur_time))) {
3246 /* Search for empty queues */
3247 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
3248 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
3253 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
3254 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
3258 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
3259 if (gst_data_queue_is_empty (oq->queue) && !oq->is_sparse) {
3260 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
3266 /* if hard limits are not reached then we allow one more buffer in the full
3267 * queue, but only if any of the other singelqueues are empty */
3269 if (IS_FILLED (sq, visible, size.visible)) {
3270 sq->max_size.visible = size.visible + 1;
3271 GST_DEBUG_OBJECT (mq,
3272 "Bumping single queue %d max visible to %d",
3273 sq->id, sq->max_size.visible);
3279 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
3280 gst_object_unref (mq);
3282 /* Overrun is always forwarded, since this is blocking the upstream element */
3284 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
3285 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
3290 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
3292 gboolean empty = TRUE;
3293 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
3297 GST_ERROR ("No multique set anymore, not doing anything");
3302 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
3303 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
3304 gst_object_unref (mq);
3308 "Single Queue %d is empty, Checking other single queues", sq->id);
3311 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
3312 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
3313 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
3315 if (gst_data_queue_is_full (oq->queue)) {
3316 GstDataQueueSize size;
3318 gst_data_queue_get_level (oq->queue, &size);
3319 if (IS_FILLED (oq, visible, size.visible)) {
3320 oq->max_size.visible = size.visible + 1;
3321 GST_DEBUG_OBJECT (mq,
3322 "queue %d is filled, bumping its max visible to %d", oq->id,
3323 oq->max_size.visible);
3324 gst_data_queue_limits_changed (oq->queue);
3327 if (!gst_data_queue_is_empty (oq->queue) || oq->is_sparse)
3330 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
3331 gst_object_unref (mq);
3334 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
3335 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
3340 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
3341 guint64 time, GstSingleQueue * sq)
3344 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
3347 GST_ERROR ("No multique set anymore, let's say we are full");
3352 GST_DEBUG_OBJECT (mq,
3353 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
3354 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
3355 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
3357 /* we are always filled on EOS */
3358 if (sq->is_eos || sq->is_segment_done) {
3363 /* we never go past the max visible items unless we are in buffering mode */
3364 if (!mq->use_buffering && IS_FILLED (sq, visible, visible)) {
3369 /* check time or bytes */
3370 res = IS_FILLED (sq, bytes, bytes);
3371 /* We only care about limits in time if we're not a sparse stream or
3372 * we're not syncing by running time */
3373 if (!sq->is_sparse || !mq->sync_by_running_time) {
3374 /* If unlinked, take into account the extra unlinked cache time */
3375 if (mq->sync_by_running_time && sq->srcresult == GST_FLOW_NOT_LINKED) {
3376 if (sq->cur_time > mq->unlinked_cache_time)
3377 res |= IS_FILLED (sq, time, sq->cur_time - mq->unlinked_cache_time);
3381 res |= IS_FILLED (sq, time, sq->cur_time);
3384 gst_object_unref (mq);
3390 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
3392 GstDataQueueItem *sitem;
3393 GstMultiQueueItem *mitem;
3394 gboolean was_flushing = FALSE;
3395 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
3396 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
3398 while (!gst_data_queue_is_empty (sq->queue)) {
3399 GstMiniObject *data;
3401 /* FIXME: If this fails here although the queue is not empty,
3402 * we're flushing... but we want to rescue all sticky
3403 * events nonetheless.
3405 if (!gst_data_queue_pop (sq->queue, &sitem)) {
3406 was_flushing = TRUE;
3407 gst_data_queue_set_flushing (sq->queue, FALSE);
3411 mitem = (GstMultiQueueItem *) sitem;
3413 data = sitem->object;
3415 if (!full && !mitem->is_query && GST_IS_EVENT (data)
3416 && srcpad && GST_EVENT_IS_STICKY (data)
3417 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
3418 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
3419 gst_pad_store_sticky_event (srcpad, GST_EVENT_CAST (data));
3422 sitem->destroy (sitem);
3424 gst_clear_object (&srcpad);
3426 gst_data_queue_flush (sq->queue);
3428 gst_data_queue_set_flushing (sq->queue, TRUE);
3431 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
3432 update_buffering (mq, sq);
3433 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
3434 gst_multi_queue_post_buffering (mq);
3435 gst_object_unref (mq);
3440 gst_single_queue_unref (GstSingleQueue * sq)
3442 if (g_atomic_int_dec_and_test (&sq->refcount)) {
3444 gst_data_queue_flush (sq->queue);
3445 g_object_unref (sq->queue);
3446 g_cond_clear (&sq->turn);
3447 g_cond_clear (&sq->query_handled);
3448 g_weak_ref_clear (&sq->sinkpad);
3449 g_weak_ref_clear (&sq->srcpad);
3450 g_weak_ref_clear (&sq->mqueue);
3456 static GstSingleQueue *
3457 gst_single_queue_ref (GstSingleQueue * squeue)
3459 g_atomic_int_inc (&squeue->refcount);
3464 static GstSingleQueue *
3465 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
3467 GstPad *srcpad, *sinkpad;
3469 GstPadTemplate *templ;
3472 guint temp_id = (id == -1) ? 0 : id;
3474 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
3476 /* Find an unused queue ID, if possible the passed one */
3477 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
3478 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
3479 /* This works because the IDs are sorted in ascending order */
3480 if (sq2->id == temp_id) {
3481 /* If this ID was requested by the caller return NULL,
3482 * otherwise just get us the next one */
3484 temp_id = sq2->id + 1;
3486 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
3489 } else if (sq2->id > temp_id) {
3494 sq = g_new0 (GstSingleQueue, 1);
3495 g_atomic_int_set (&sq->refcount, 1);
3499 sq->groupid = DEFAULT_PAD_GROUP_ID;
3500 sq->group_high_time = GST_CLOCK_STIME_NONE;
3502 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
3503 mqueue->queues_cookie++;
3505 /* copy over max_size and extra_size so we don't need to take the lock
3506 * any longer when checking if the queue is full. */
3507 sq->max_size.visible = mqueue->max_size.visible;
3508 sq->max_size.bytes = mqueue->max_size.bytes;
3509 sq->max_size.time = mqueue->max_size.time;
3511 sq->extra_size.visible = mqueue->extra_size.visible;
3512 sq->extra_size.bytes = mqueue->extra_size.bytes;
3513 sq->extra_size.time = mqueue->extra_size.time;
3515 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
3517 g_weak_ref_init (&sq->mqueue, mqueue);
3518 sq->srcresult = GST_FLOW_FLUSHING;
3520 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
3521 single_queue_check_full,
3522 (GstDataQueueFullCallback) single_queue_overrun_cb,
3523 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
3525 sq->is_sparse = FALSE;
3526 sq->flushing = FALSE;
3528 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
3529 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
3533 sq->next_time = GST_CLOCK_STIME_NONE;
3534 sq->last_time = GST_CLOCK_STIME_NONE;
3535 g_cond_init (&sq->turn);
3536 g_cond_init (&sq->query_handled);
3538 sq->sinktime = GST_CLOCK_STIME_NONE;
3539 sq->srctime = GST_CLOCK_STIME_NONE;
3540 sq->sink_tainted = TRUE;
3541 sq->src_tainted = TRUE;
3543 sq->sink_stream_gid = sq->src_stream_gid = GST_GROUP_ID_INVALID;
3544 sq->sink_stream_gid_changed = FALSE;
3545 sq->src_stream_gid_changed = FALSE;
3547 name = g_strdup_printf ("sink_%u", sq->id);
3548 templ = gst_static_pad_template_get (&sinktemplate);
3549 sinkpad = g_object_new (GST_TYPE_MULTIQUEUE_PAD, "name", name,
3550 "direction", templ->direction, "template", templ, NULL);
3551 g_weak_ref_init (&sq->sinkpad, sinkpad);
3552 gst_object_unref (templ);
3555 GST_MULTIQUEUE_PAD (sinkpad)->sq = sq;
3557 gst_pad_set_chain_function (sinkpad,
3558 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
3559 gst_pad_set_activatemode_function (sinkpad,
3560 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
3561 gst_pad_set_event_full_function (sinkpad,
3562 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
3563 gst_pad_set_query_function (sinkpad,
3564 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
3565 gst_pad_set_iterate_internal_links_function (sinkpad,
3566 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
3567 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
3569 name = g_strdup_printf ("src_%u", sq->id);
3570 templ = gst_static_pad_template_get (&srctemplate);
3571 srcpad = g_object_new (GST_TYPE_MULTIQUEUE_PAD, "name", name,
3572 "direction", templ->direction, "template", templ, NULL);
3573 g_weak_ref_init (&sq->srcpad, srcpad);
3574 gst_object_unref (templ);
3577 GST_MULTIQUEUE_PAD (srcpad)->sq = gst_single_queue_ref (sq);
3579 gst_pad_set_activatemode_function (srcpad,
3580 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
3581 gst_pad_set_event_function (srcpad,
3582 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
3583 gst_pad_set_query_function (srcpad,
3584 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
3585 gst_pad_set_iterate_internal_links_function (srcpad,
3586 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
3587 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PROXY_CAPS);
3589 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
3591 /* only activate the pads when we are not in the NULL state
3592 * and add the pad under the state_lock to prevent state changes
3593 * between activating and adding */
3594 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
3595 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
3596 gst_pad_set_active (srcpad, TRUE);
3597 gst_pad_set_active (sinkpad, TRUE);
3599 gst_element_add_pad (GST_ELEMENT (mqueue), srcpad);
3600 gst_element_add_pad (GST_ELEMENT (mqueue), sinkpad);
3601 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
3602 gst_single_queue_start (mqueue, sq);
3604 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
3606 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",