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;
152 GstDataQueueSize max_size, extra_size;
153 GstClockTime cur_time;
155 gboolean is_segment_done;
160 /* Protected by global lock */
161 guint32 nextid; /* ID of the next object waiting to be pushed */
162 guint32 oldid; /* ID of the last object pushed (last in a series) */
163 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
164 GstClockTimeDiff next_time; /* End running time of next buffer to be pushed */
165 GstClockTimeDiff last_time; /* Start running time of last pushed buffer */
166 GCond turn; /* SingleQueue turn waiting conditional */
168 /* for serialized queries */
171 GstQuery *last_handled_query;
173 /* For interleave calculation */
174 GThread *thread; /* Streaming thread of SingleQueue */
175 GstClockTime interleave; /* Calculated interleve within the thread */
178 /* Extension of GstDataQueueItem structure for our usage */
179 typedef struct _GstMultiQueueItem GstMultiQueueItem;
181 struct _GstMultiQueueItem
183 GstMiniObject *object;
188 GDestroyNotify destroy;
194 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
195 static void gst_single_queue_unref (GstSingleQueue * squeue);
196 static GstSingleQueue *gst_single_queue_ref (GstSingleQueue * squeue);
198 static void wake_up_next_non_linked (GstMultiQueue * mq);
199 static void compute_high_id (GstMultiQueue * mq);
200 static void compute_high_time (GstMultiQueue * mq, guint groupid);
201 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
202 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
204 static void update_buffering (GstMultiQueue * mq, GstSingleQueue * sq);
205 static void gst_multi_queue_post_buffering (GstMultiQueue * mq);
206 static void recheck_buffering_status (GstMultiQueue * mq);
208 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
210 static void calculate_interleave (GstMultiQueue * mq, GstSingleQueue * sq);
212 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
215 GST_STATIC_CAPS_ANY);
217 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
220 GST_STATIC_CAPS_ANY);
222 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
223 #define GST_CAT_DEFAULT (multi_queue_debug)
225 /* Signals and args */
233 /* default limits, we try to keep up to 2 seconds of data and if there is not
234 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
235 * there is data in the queues. Normally, the byte and time limits are not hit
236 * in theses conditions. */
237 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
238 #define DEFAULT_MAX_SIZE_BUFFERS 5
239 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
241 /* second limits. When we hit one of the above limits we are probably dealing
242 * with a badly muxed file and we scale the limits to these emergency values.
243 * This is currently not yet implemented.
244 * Since we dynamically scale the queue buffer size up to the limits but avoid
245 * going above the max-size-buffers when we can, we don't really need this
246 * additional extra size. */
247 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
248 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
249 #ifdef TIZEN_FEATURE_MQ_MODIFICATION_EXTRA_SIZE_TIME
250 #define DEFAULT_EXTRA_SIZE_TIME 10 * GST_SECOND
252 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
255 #define DEFAULT_USE_BUFFERING FALSE
256 #define DEFAULT_LOW_WATERMARK 0.01
257 #define DEFAULT_HIGH_WATERMARK 0.99
258 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
259 #define DEFAULT_USE_INTERLEAVE FALSE
260 #define DEFAULT_UNLINKED_CACHE_TIME 250 * GST_MSECOND
262 #define DEFAULT_MINIMUM_INTERLEAVE (250 * GST_MSECOND)
267 PROP_EXTRA_SIZE_BYTES,
268 PROP_EXTRA_SIZE_BUFFERS,
269 PROP_EXTRA_SIZE_TIME,
271 PROP_MAX_SIZE_BUFFERS,
273 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
274 PROP_CURR_SIZE_BYTES,
281 PROP_SYNC_BY_RUNNING_TIME,
283 PROP_UNLINKED_CACHE_TIME,
284 PROP_MINIMUM_INTERLEAVE,
289 /* Explanation for buffer levels and percentages:
291 * The buffering_level functions here return a value in a normalized range
292 * that specifies the current fill level of a queue. The range goes from 0 to
293 * MAX_BUFFERING_LEVEL. The low/high watermarks also use this same range.
295 * This is not to be confused with the buffering_percent value, which is
296 * a *relative* quantity - relative to the low/high watermarks.
297 * buffering_percent = 0% means overall buffering_level is at the low watermark.
298 * buffering_percent = 100% means overall buffering_level is at the high watermark.
299 * buffering_percent is used for determining if the fill level has reached
300 * the high watermark, and for producing BUFFERING messages. This value
301 * always uses a 0..100 range (since it is a percentage).
303 * To avoid future confusions, whenever "buffering level" is mentioned, it
304 * refers to the absolute level which is in the 0..MAX_BUFFERING_LEVEL
305 * range. Whenever "buffering_percent" is mentioned, it refers to the
306 * percentage value that is relative to the low/high watermark. */
308 /* Using a buffering level range of 0..1000000 to allow for a
309 * resolution in ppm (1 ppm = 0.0001%) */
310 #define MAX_BUFFERING_LEVEL 1000000
312 /* How much 1% makes up in the buffer level range */
313 #define BUF_LEVEL_PERCENT_FACTOR ((MAX_BUFFERING_LEVEL) / 100)
315 /* GstMultiQueuePad */
317 #define DEFAULT_PAD_GROUP_ID 0
323 PROP_CURRENT_LEVEL_BUFFERS,
324 PROP_CURRENT_LEVEL_BYTES,
325 PROP_CURRENT_LEVEL_TIME,
328 #define GST_TYPE_MULTIQUEUE_PAD (gst_multiqueue_pad_get_type())
329 #define GST_MULTIQUEUE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePad))
330 #define GST_IS_MULTIQUEUE_PAD(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTIQUEUE_PAD))
331 #define GST_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
332 #define GST_IS_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_MULTIQUEUE_PAD))
333 #define GST_MULTIQUEUE_PAD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
335 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
336 g_mutex_lock (&q->qlock); \
339 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
340 g_mutex_unlock (&q->qlock); \
343 #define SET_PERCENT(mq, perc) G_STMT_START { \
344 if (perc != mq->buffering_percent) { \
345 mq->buffering_percent = perc; \
346 mq->buffering_percent_changed = TRUE; \
347 GST_DEBUG_OBJECT (mq, "buffering %d percent", perc); \
351 struct _GstMultiQueuePad
358 struct _GstMultiQueuePadClass
360 GstPadClass parent_class;
363 GType gst_multiqueue_pad_get_type (void);
365 G_DEFINE_TYPE (GstMultiQueuePad, gst_multiqueue_pad, GST_TYPE_PAD);
368 gst_multiqueue_pad_get_group_id (GstMultiQueuePad * pad)
376 mq = g_weak_ref_get (&pad->sq->mqueue);
379 GST_OBJECT_LOCK (mq);
382 ret = pad->sq->groupid;
385 GST_OBJECT_UNLOCK (mq);
386 gst_object_unref (mq);
393 gst_multiqueue_pad_get_current_level_buffers (GstMultiQueuePad * pad)
395 GstSingleQueue *sq = pad->sq;
396 GstDataQueueSize level;
402 mq = g_weak_ref_get (&pad->sq->mqueue);
405 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
408 gst_data_queue_get_level (sq->queue, &level);
411 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
412 gst_object_unref (mq);
415 return level.visible;
419 gst_multiqueue_pad_get_current_level_bytes (GstMultiQueuePad * pad)
421 GstSingleQueue *sq = pad->sq;
422 GstDataQueueSize level;
428 mq = g_weak_ref_get (&pad->sq->mqueue);
431 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
434 gst_data_queue_get_level (sq->queue, &level);
437 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
438 gst_object_unref (mq);
445 gst_multiqueue_pad_get_current_level_time (GstMultiQueuePad * pad)
447 GstSingleQueue *sq = pad->sq;
454 mq = g_weak_ref_get (&pad->sq->mqueue);
457 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
463 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
464 gst_object_unref (mq);
471 gst_multiqueue_pad_get_property (GObject * object, guint prop_id,
472 GValue * value, GParamSpec * pspec)
474 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
477 case PROP_PAD_GROUP_ID:
478 g_value_set_uint (value, gst_multiqueue_pad_get_group_id (pad));
480 case PROP_CURRENT_LEVEL_BUFFERS:{
481 g_value_set_uint (value,
482 gst_multiqueue_pad_get_current_level_buffers (pad));
485 case PROP_CURRENT_LEVEL_BYTES:{
486 g_value_set_uint (value,
487 gst_multiqueue_pad_get_current_level_bytes (pad));
490 case PROP_CURRENT_LEVEL_TIME:{
491 g_value_set_uint64 (value,
492 gst_multiqueue_pad_get_current_level_time (pad));
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502 gst_multiqueue_pad_set_property (GObject * object, guint prop_id,
503 const GValue * value, GParamSpec * pspec)
505 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
508 case PROP_PAD_GROUP_ID:
510 GstMultiQueue *mqueue = g_weak_ref_get (&pad->sq->mqueue);
513 GST_OBJECT_LOCK (mqueue);
515 pad->sq->groupid = g_value_get_uint (value);
518 GST_OBJECT_UNLOCK (mqueue);
519 gst_object_unref (mqueue);
524 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530 gst_multiqueue_pad_finalize (GObject * object)
532 GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
535 gst_single_queue_unref (pad->sq);
537 G_OBJECT_CLASS (gst_multiqueue_pad_parent_class)->finalize (object);
541 gst_multiqueue_pad_class_init (GstMultiQueuePadClass * klass)
543 GObjectClass *gobject_class = (GObjectClass *) klass;
545 gobject_class->set_property = gst_multiqueue_pad_set_property;
546 gobject_class->get_property = gst_multiqueue_pad_get_property;
547 gobject_class->finalize = gst_multiqueue_pad_finalize;
550 * GstMultiQueuePad:group-id:
552 * Group to which this pad belongs.
556 g_object_class_install_property (gobject_class, PROP_PAD_GROUP_ID,
557 g_param_spec_uint ("group-id", "Group ID",
558 "Group to which this pad belongs", 0, G_MAXUINT32,
559 DEFAULT_PAD_GROUP_ID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
562 * GstMultiQueuePad:current-level-buffers:
564 * The corresponding queue's current level of buffers.
568 g_object_class_install_property (gobject_class, PROP_CURRENT_LEVEL_BUFFERS,
569 g_param_spec_uint ("current-level-buffers", "Current level buffers",
570 "Current level buffers", 0, G_MAXUINT32,
571 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
574 * GstMultiQueuePad:current-level-bytes:
576 * The corresponding queue's current level of bytes.
580 g_object_class_install_property (gobject_class, PROP_CURRENT_LEVEL_BYTES,
581 g_param_spec_uint ("current-level-bytes", "Current level bytes",
582 "Current level bytes", 0, G_MAXUINT32,
583 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
586 * GstMultiQueuePad:current-level-time:
588 * The corresponding queue's current level of time.
592 g_object_class_install_property (gobject_class, PROP_CURRENT_LEVEL_TIME,
593 g_param_spec_uint64 ("current-level-time", "Current level time",
594 "Current level time", 0, G_MAXUINT64,
595 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
599 gst_multiqueue_pad_init (GstMultiQueuePad * pad)
605 /* Convenience function */
606 static inline GstClockTimeDiff
607 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
609 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
611 if (GST_CLOCK_TIME_IS_VALID (val)) {
613 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
622 static void gst_multi_queue_finalize (GObject * object);
623 static void gst_multi_queue_set_property (GObject * object,
624 guint prop_id, const GValue * value, GParamSpec * pspec);
625 static void gst_multi_queue_get_property (GObject * object,
626 guint prop_id, GValue * value, GParamSpec * pspec);
628 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
629 GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
630 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
631 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
632 element, GstStateChange transition);
634 static void gst_multi_queue_loop (GstPad * pad);
637 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
638 #define gst_multi_queue_parent_class parent_class
639 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
641 GST_ELEMENT_REGISTER_DEFINE (multiqueue, "multiqueue", GST_RANK_NONE,
642 GST_TYPE_MULTI_QUEUE);
644 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
647 gst_multi_queue_class_init (GstMultiQueueClass * klass)
649 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
650 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
652 gobject_class->set_property = gst_multi_queue_set_property;
653 gobject_class->get_property = gst_multi_queue_get_property;
658 * GstMultiQueue::underrun:
659 * @multiqueue: the multiqueue instance
661 * This signal is emitted from the streaming thread when there is
662 * no data in any of the queues inside the multiqueue instance (underrun).
664 * This indicates either starvation or EOS from the upstream data sources.
666 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
667 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
668 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
669 NULL, G_TYPE_NONE, 0);
672 * GstMultiQueue::overrun:
673 * @multiqueue: the multiqueue instance
675 * Reports that one of the queues in the multiqueue is full (overrun).
676 * A queue is full if the total amount of data inside it (num-buffers, time,
677 * size) is higher than the boundary values which can be set through the
678 * GObject properties.
680 * This can be used as an indicator of pre-roll.
682 gst_multi_queue_signals[SIGNAL_OVERRUN] =
683 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
684 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
685 NULL, G_TYPE_NONE, 0);
689 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
690 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
691 "Max. amount of data in the queue (bytes, 0=disable)",
692 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
693 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
694 G_PARAM_STATIC_STRINGS));
695 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
696 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
697 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
698 DEFAULT_MAX_SIZE_BUFFERS,
699 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
700 G_PARAM_STATIC_STRINGS));
701 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
702 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
703 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
704 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
705 G_PARAM_STATIC_STRINGS));
706 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
707 g_object_class_install_property (gobject_class, PROP_CURR_SIZE_BYTES,
708 g_param_spec_uint ("curr-size-bytes", "Current buffered size (kB)",
709 "buffered amount of data in the queue (bytes)", 0, G_MAXUINT,
710 0, G_PARAM_READABLE | GST_PARAM_MUTABLE_PLAYING |
711 G_PARAM_STATIC_STRINGS));
713 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
714 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
715 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
716 " (NOT IMPLEMENTED)",
717 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
718 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
719 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
720 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
721 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
722 " (NOT IMPLEMENTED)",
723 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
724 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
725 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
726 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
727 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
728 " (NOT IMPLEMENTED)",
729 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
730 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
733 * GstMultiQueue:use-buffering:
735 * Enable the buffering option in multiqueue so that BUFFERING messages are
736 * emitted based on low-/high-percent thresholds.
738 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
739 g_param_spec_boolean ("use-buffering", "Use buffering",
740 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds "
741 "(0% = low-watermark, 100% = high-watermark)",
742 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
743 G_PARAM_STATIC_STRINGS));
745 * GstMultiQueue:low-percent:
747 * Low threshold percent for buffering to start.
749 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
750 g_param_spec_int ("low-percent", "Low percent",
751 "Low threshold for buffering to start. Only used if use-buffering is True "
752 "(Deprecated: use low-watermark instead)",
753 0, 100, DEFAULT_LOW_WATERMARK * 100,
754 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
756 * GstMultiQueue:high-percent:
758 * High threshold percent for buffering to finish.
760 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
761 g_param_spec_int ("high-percent", "High percent",
762 "High threshold for buffering to finish. Only used if use-buffering is True "
763 "(Deprecated: use high-watermark instead)",
764 0, 100, DEFAULT_HIGH_WATERMARK * 100,
765 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
767 * GstMultiQueue:low-watermark:
769 * Low threshold watermark for buffering to start.
773 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
774 g_param_spec_double ("low-watermark", "Low watermark",
775 "Low threshold for buffering to start. Only used if use-buffering is True",
776 0.0, 1.0, DEFAULT_LOW_WATERMARK,
777 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
779 * GstMultiQueue:high-watermark:
781 * High threshold watermark for buffering to finish.
785 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
786 g_param_spec_double ("high-watermark", "High watermark",
787 "High threshold for buffering to finish. Only used if use-buffering is True",
788 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
789 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
792 * GstMultiQueue:sync-by-running-time:
794 * If enabled multiqueue will synchronize deactivated or not-linked streams
795 * to the activated and linked streams by taking the running time.
796 * Otherwise multiqueue will synchronize the deactivated or not-linked
797 * streams by keeping the order in which buffers and events arrived compared
798 * to active and linked streams.
800 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
801 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
802 "Synchronize deactivated or not-linked streams by running time",
803 DEFAULT_SYNC_BY_RUNNING_TIME,
804 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
806 g_object_class_install_property (gobject_class, PROP_USE_INTERLEAVE,
807 g_param_spec_boolean ("use-interleave", "Use interleave",
808 "Adjust time limits based on input interleave",
809 DEFAULT_USE_INTERLEAVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
811 g_object_class_install_property (gobject_class, PROP_UNLINKED_CACHE_TIME,
812 g_param_spec_uint64 ("unlinked-cache-time", "Unlinked cache time (ns)",
813 "Extra buffering in time for unlinked streams (if 'sync-by-running-time')",
814 0, G_MAXUINT64, DEFAULT_UNLINKED_CACHE_TIME,
815 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
816 G_PARAM_STATIC_STRINGS));
818 g_object_class_install_property (gobject_class, PROP_MINIMUM_INTERLEAVE,
819 g_param_spec_uint64 ("min-interleave-time", "Minimum interleave time",
820 "Minimum extra buffering for deinterleaving (size of the queues) when use-interleave=true",
821 0, G_MAXUINT64, DEFAULT_MINIMUM_INTERLEAVE,
822 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
823 G_PARAM_STATIC_STRINGS));
826 * GstMultiQueue:stats:
828 * Various #GstMultiQueue statistics. This property returns a #GstStructure
829 * with name "application/x-gst-multi-queue-stats" with the following fields:
831 * - "queues" GST_TYPE_ARRAY Contains one GstStructure named "queue_%d"
832 * (where \%d is the queue's ID) per internal queue:
833 * - "buffers" G_TYPE_UINT The queue's current level of buffers
834 * - "bytes" G_TYPE_UINT The queue's current level of bytes
835 * - "time" G_TYPE_UINT64 The queue's current level of time
839 g_object_class_install_property (gobject_class, PROP_STATS,
840 g_param_spec_boxed ("stats", "Stats",
841 "Multiqueue Statistics",
842 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
844 gobject_class->finalize = gst_multi_queue_finalize;
846 gst_element_class_set_static_metadata (gstelement_class,
848 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
849 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
850 &sinktemplate, GST_TYPE_MULTIQUEUE_PAD);
851 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
852 &srctemplate, GST_TYPE_MULTIQUEUE_PAD);
854 gstelement_class->request_new_pad =
855 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
856 gstelement_class->release_pad =
857 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
858 gstelement_class->change_state =
859 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
861 gst_type_mark_as_plugin_api (GST_TYPE_MULTIQUEUE_PAD, 0);
865 gst_multi_queue_init (GstMultiQueue * mqueue)
867 mqueue->nbqueues = 0;
868 mqueue->queues = NULL;
870 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
871 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
872 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
874 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
875 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
876 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
878 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
879 mqueue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
880 mqueue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
882 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
883 mqueue->use_interleave = DEFAULT_USE_INTERLEAVE;
884 mqueue->min_interleave_time = DEFAULT_MINIMUM_INTERLEAVE;
885 mqueue->unlinked_cache_time = DEFAULT_UNLINKED_CACHE_TIME;
889 mqueue->high_time = GST_CLOCK_STIME_NONE;
891 g_mutex_init (&mqueue->qlock);
892 g_mutex_init (&mqueue->buffering_post_lock);
896 gst_multi_queue_finalize (GObject * object)
898 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
900 g_list_free_full (mqueue->queues, (GDestroyNotify) gst_single_queue_unref);
901 mqueue->queues = NULL;
902 mqueue->queues_cookie++;
904 /* free/unref instance data */
905 g_mutex_clear (&mqueue->qlock);
906 g_mutex_clear (&mqueue->buffering_post_lock);
908 G_OBJECT_CLASS (parent_class)->finalize (object);
911 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
912 GList * tmp = mq->queues; \
914 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
915 q->max_size.format = mq->max_size.format; \
916 update_buffering (mq, q); \
917 gst_data_queue_limits_changed (q->queue); \
918 tmp = g_list_next(tmp); \
923 gst_multi_queue_set_property (GObject * object, guint prop_id,
924 const GValue * value, GParamSpec * pspec)
926 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
929 case PROP_MAX_SIZE_BYTES:
930 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
931 mq->max_size.bytes = g_value_get_uint (value);
932 SET_CHILD_PROPERTY (mq, bytes);
933 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
934 gst_multi_queue_post_buffering (mq);
936 case PROP_MAX_SIZE_BUFFERS:
939 gint new_size = g_value_get_uint (value);
941 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
943 mq->max_size.visible = new_size;
947 GstDataQueueSize size;
948 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
949 gst_data_queue_get_level (q->queue, &size);
951 GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
952 " current: %d, current max %d", q->id, new_size, size.visible,
953 q->max_size.visible);
955 /* do not reduce max size below current level if the single queue
956 * has grown because of empty queue */
958 q->max_size.visible = new_size;
959 } else if (q->max_size.visible == 0) {
960 q->max_size.visible = MAX (new_size, size.visible);
961 } else if (new_size > size.visible) {
962 q->max_size.visible = new_size;
964 update_buffering (mq, q);
965 gst_data_queue_limits_changed (q->queue);
966 tmp = g_list_next (tmp);
969 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
970 gst_multi_queue_post_buffering (mq);
974 case PROP_MAX_SIZE_TIME:
975 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
976 mq->max_size.time = g_value_get_uint64 (value);
977 SET_CHILD_PROPERTY (mq, time);
978 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
979 gst_multi_queue_post_buffering (mq);
981 case PROP_EXTRA_SIZE_BYTES:
982 mq->extra_size.bytes = g_value_get_uint (value);
984 case PROP_EXTRA_SIZE_BUFFERS:
985 mq->extra_size.visible = g_value_get_uint (value);
987 case PROP_EXTRA_SIZE_TIME:
988 mq->extra_size.time = g_value_get_uint64 (value);
990 case PROP_USE_BUFFERING:
991 mq->use_buffering = g_value_get_boolean (value);
992 recheck_buffering_status (mq);
994 case PROP_LOW_PERCENT:
995 mq->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
996 /* Recheck buffering status - the new low_watermark value might
997 * be above the current fill level. If the old low_watermark one
998 * was below the current level, this means that mq->buffering is
999 * disabled and needs to be re-enabled. */
1000 recheck_buffering_status (mq);
1002 case PROP_HIGH_PERCENT:
1003 mq->high_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
1004 recheck_buffering_status (mq);
1006 case PROP_LOW_WATERMARK:
1007 mq->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
1008 recheck_buffering_status (mq);
1010 case PROP_HIGH_WATERMARK:
1011 mq->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
1012 recheck_buffering_status (mq);
1014 case PROP_SYNC_BY_RUNNING_TIME:
1015 mq->sync_by_running_time = g_value_get_boolean (value);
1017 case PROP_USE_INTERLEAVE:
1018 mq->use_interleave = g_value_get_boolean (value);
1020 case PROP_UNLINKED_CACHE_TIME:
1021 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1022 mq->unlinked_cache_time = g_value_get_uint64 (value);
1023 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1024 gst_multi_queue_post_buffering (mq);
1026 case PROP_MINIMUM_INTERLEAVE:
1027 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1028 mq->min_interleave_time = g_value_get_uint64 (value);
1029 if (mq->use_interleave)
1030 calculate_interleave (mq, NULL);
1031 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1034 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1039 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
1041 get_current_size_bytes (GstMultiQueue * mq)
1044 guint current_size_bytes = 0;
1046 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1047 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1048 GstDataQueueSize size;
1050 gst_data_queue_get_level (sq->queue, &size);
1052 current_size_bytes += size.bytes;
1054 GST_DEBUG_OBJECT (mq,
1055 "queue %d: bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1056 G_GUINT64_FORMAT, sq->id, size.bytes, sq->max_size.bytes,
1057 sq->cur_time, sq->max_size.time);
1060 GST_INFO_OBJECT (mq, "current_size_bytes : %u", current_size_bytes);
1062 return current_size_bytes;
1066 /* Called with mutex held */
1067 static GstStructure *
1068 gst_multi_queue_get_stats (GstMultiQueue * mq)
1071 gst_structure_new_empty ("application/x-gst-multi-queue-stats");
1075 if (mq->queues != NULL) {
1076 GValue queues = G_VALUE_INIT;
1077 GValue v = G_VALUE_INIT;
1079 g_value_init (&queues, GST_TYPE_ARRAY);
1081 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1082 GstDataQueueSize level;
1085 g_value_init (&v, GST_TYPE_STRUCTURE);
1087 sq = (GstSingleQueue *) tmp->data;
1088 gst_data_queue_get_level (sq->queue, &level);
1089 id = g_strdup_printf ("queue_%d", sq->id);
1090 s = gst_structure_new (id,
1091 "buffers", G_TYPE_UINT, level.visible,
1092 "bytes", G_TYPE_UINT, level.bytes,
1093 "time", G_TYPE_UINT64, sq->cur_time, NULL);
1094 g_value_take_boxed (&v, s);
1095 gst_value_array_append_and_take_value (&queues, &v);
1098 gst_structure_take_value (ret, "queues", &queues);
1105 gst_multi_queue_get_property (GObject * object, guint prop_id,
1106 GValue * value, GParamSpec * pspec)
1108 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
1110 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1113 case PROP_EXTRA_SIZE_BYTES:
1114 g_value_set_uint (value, mq->extra_size.bytes);
1116 case PROP_EXTRA_SIZE_BUFFERS:
1117 g_value_set_uint (value, mq->extra_size.visible);
1119 case PROP_EXTRA_SIZE_TIME:
1120 g_value_set_uint64 (value, mq->extra_size.time);
1122 case PROP_MAX_SIZE_BYTES:
1123 g_value_set_uint (value, mq->max_size.bytes);
1125 case PROP_MAX_SIZE_BUFFERS:
1126 g_value_set_uint (value, mq->max_size.visible);
1128 case PROP_MAX_SIZE_TIME:
1129 g_value_set_uint64 (value, mq->max_size.time);
1131 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
1132 case PROP_CURR_SIZE_BYTES:
1133 g_value_set_uint (value, get_current_size_bytes(mq));
1136 case PROP_USE_BUFFERING:
1137 g_value_set_boolean (value, mq->use_buffering);
1139 case PROP_LOW_PERCENT:
1140 g_value_set_int (value, mq->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
1142 case PROP_HIGH_PERCENT:
1143 g_value_set_int (value, mq->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
1145 case PROP_LOW_WATERMARK:
1146 g_value_set_double (value, mq->low_watermark /
1147 (gdouble) MAX_BUFFERING_LEVEL);
1149 case PROP_HIGH_WATERMARK:
1150 g_value_set_double (value, mq->high_watermark /
1151 (gdouble) MAX_BUFFERING_LEVEL);
1153 case PROP_SYNC_BY_RUNNING_TIME:
1154 g_value_set_boolean (value, mq->sync_by_running_time);
1156 case PROP_USE_INTERLEAVE:
1157 g_value_set_boolean (value, mq->use_interleave);
1159 case PROP_UNLINKED_CACHE_TIME:
1160 g_value_set_uint64 (value, mq->unlinked_cache_time);
1162 case PROP_MINIMUM_INTERLEAVE:
1163 g_value_set_uint64 (value, mq->min_interleave_time);
1166 g_value_take_boxed (value, gst_multi_queue_get_stats (mq));
1169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1173 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1176 static GstIterator *
1177 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
1179 GstIterator *it = NULL;
1180 GstPad *opad, *sinkpad, *srcpad;
1181 GstSingleQueue *squeue;
1182 GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
1183 GValue val = { 0, };
1185 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1186 squeue = GST_MULTIQUEUE_PAD (pad)->sq;
1190 srcpad = g_weak_ref_get (&squeue->srcpad);
1191 sinkpad = g_weak_ref_get (&squeue->sinkpad);
1192 if (sinkpad == pad && srcpad) {
1194 gst_clear_object (&sinkpad);
1196 } else if (srcpad == pad && sinkpad) {
1198 gst_clear_object (&srcpad);
1201 gst_clear_object (&srcpad);
1202 gst_clear_object (&sinkpad);
1206 g_value_init (&val, GST_TYPE_PAD);
1207 g_value_set_object (&val, opad);
1208 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1209 g_value_unset (&val);
1211 gst_object_unref (opad);
1214 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1221 * GstElement methods
1225 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
1226 const gchar * name, const GstCaps * caps)
1228 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
1229 GstSingleQueue *squeue;
1234 sscanf (name + 4, "_%u", &temp_id);
1235 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
1238 /* Create a new single queue, add the sink and source pad and return the sink pad */
1239 squeue = gst_single_queue_new (mqueue, temp_id);
1241 new_pad = squeue ? g_weak_ref_get (&squeue->sinkpad) : NULL;
1242 /* request pad assumes the element is owning the ref of the pad it returns */
1244 gst_object_unref (new_pad);
1246 GST_DEBUG_OBJECT (mqueue, "Returning pad %" GST_PTR_FORMAT, new_pad);
1252 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
1254 GstPad *sinkpad = NULL, *srcpad = NULL;
1255 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
1256 GstSingleQueue *sq = NULL;
1259 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1261 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1262 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
1263 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1264 sq = (GstSingleQueue *) tmp->data;
1265 sinkpad = g_weak_ref_get (&sq->sinkpad);
1267 if (sinkpad == pad) {
1268 srcpad = g_weak_ref_get (&sq->srcpad);
1272 gst_object_unref (sinkpad);
1276 gst_clear_object (&sinkpad);
1277 gst_clear_object (&srcpad);
1278 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
1279 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1283 /* FIXME: The removal of the singlequeue should probably not happen until it
1284 * finishes draining */
1286 /* remove it from the list */
1287 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
1288 mqueue->queues_cookie++;
1290 /* FIXME : recompute next-non-linked */
1291 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1293 /* delete SingleQueue */
1294 gst_data_queue_set_flushing (sq->queue, TRUE);
1296 gst_pad_set_active (srcpad, FALSE);
1297 gst_pad_set_active (sinkpad, FALSE);
1298 gst_element_remove_pad (element, srcpad);
1299 gst_element_remove_pad (element, sinkpad);
1300 gst_object_unref (srcpad);
1301 gst_object_unref (sinkpad);
1304 static GstStateChangeReturn
1305 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
1307 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
1308 GstSingleQueue *sq = NULL;
1309 GstStateChangeReturn result;
1311 switch (transition) {
1312 case GST_STATE_CHANGE_READY_TO_PAUSED:{
1315 /* Set all pads to non-flushing */
1316 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1317 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1318 sq = (GstSingleQueue *) tmp->data;
1319 sq->flushing = FALSE;
1322 /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
1323 SET_CHILD_PROPERTY (mqueue, visible);
1325 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1326 gst_multi_queue_post_buffering (mqueue);
1330 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
1331 /* to stop buffering during playing state */
1332 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:{
1333 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1334 mqueue->buffering = FALSE;
1335 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1336 gst_multi_queue_post_buffering (mqueue);
1340 case GST_STATE_CHANGE_PAUSED_TO_READY:{
1343 /* Un-wait all waiting pads */
1344 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1345 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1346 sq = (GstSingleQueue *) tmp->data;
1347 sq->flushing = TRUE;
1348 g_cond_signal (&sq->turn);
1350 sq->last_query = FALSE;
1351 g_cond_signal (&sq->query_handled);
1353 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1360 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1362 switch (transition) {
1371 gst_single_queue_start (GstMultiQueue * mq, GstSingleQueue * sq)
1373 gboolean res = FALSE;
1374 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
1376 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
1379 res = gst_pad_start_task (srcpad,
1380 (GstTaskFunction) gst_multi_queue_loop, srcpad, NULL);
1381 gst_object_unref (srcpad);
1388 gst_single_queue_pause (GstMultiQueue * mq, GstSingleQueue * sq)
1390 gboolean result = FALSE;
1391 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
1393 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
1395 result = gst_pad_pause_task (srcpad);
1396 gst_object_unref (srcpad);
1399 sq->sink_tainted = sq->src_tainted = TRUE;
1404 gst_single_queue_stop (GstMultiQueue * mq, GstSingleQueue * sq)
1406 gboolean result = FALSE;
1407 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
1409 GST_LOG_OBJECT (mq, "SingleQueue %d : stopping task", sq->id);
1411 result = gst_pad_stop_task (srcpad);
1412 gst_object_unref (srcpad);
1414 sq->sink_tainted = sq->src_tainted = TRUE;
1419 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
1422 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
1426 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1427 sq->srcresult = GST_FLOW_FLUSHING;
1428 gst_data_queue_set_flushing (sq->queue, TRUE);
1430 sq->flushing = TRUE;
1432 /* wake up non-linked task */
1433 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
1435 g_cond_signal (&sq->turn);
1436 sq->last_query = FALSE;
1437 g_cond_signal (&sq->query_handled);
1438 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1440 gst_single_queue_flush_queue (sq, full);
1442 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1443 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1444 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1445 sq->has_src_segment = FALSE;
1446 /* All pads start off OK for a smooth kick-off */
1447 sq->srcresult = GST_FLOW_OK;
1450 sq->max_size.visible = mq->max_size.visible;
1452 sq->is_segment_done = FALSE;
1455 sq->last_oldid = G_MAXUINT32;
1456 sq->next_time = GST_CLOCK_STIME_NONE;
1457 sq->last_time = GST_CLOCK_STIME_NONE;
1458 sq->cached_sinktime = GST_CLOCK_STIME_NONE;
1459 sq->group_high_time = GST_CLOCK_STIME_NONE;
1460 gst_data_queue_set_flushing (sq->queue, FALSE);
1462 /* We will become active again on the next buffer/gap */
1465 /* Reset high time to be recomputed next */
1466 mq->high_time = GST_CLOCK_STIME_NONE;
1468 sq->flushing = FALSE;
1469 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1473 /* WITH LOCK TAKEN */
1475 get_buffering_level (GstMultiQueue * mq, GstSingleQueue * sq)
1477 GstDataQueueSize size;
1478 gint buffering_level, tmp;
1480 gst_data_queue_get_level (sq->queue, &size);
1482 GST_DEBUG_OBJECT (mq,
1483 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1484 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
1485 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1487 /* get bytes and time buffer levels and take the max */
1488 if (sq->is_eos || sq->is_segment_done || sq->srcresult == GST_FLOW_NOT_LINKED
1490 buffering_level = MAX_BUFFERING_LEVEL;
1492 buffering_level = 0;
1493 if (sq->max_size.time > 0) {
1495 gst_util_uint64_scale (sq->cur_time,
1496 MAX_BUFFERING_LEVEL, sq->max_size.time);
1497 buffering_level = MAX (buffering_level, tmp);
1499 if (sq->max_size.bytes > 0) {
1501 gst_util_uint64_scale_int (size.bytes,
1502 MAX_BUFFERING_LEVEL, sq->max_size.bytes);
1503 buffering_level = MAX (buffering_level, tmp);
1507 return buffering_level;
1510 /* WITH LOCK TAKEN */
1512 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
1514 gint buffering_level, percent;
1516 /* nothing to dowhen we are not in buffering mode */
1517 if (!mq->use_buffering)
1520 #ifdef TIZEN_FEATURE_MQ_SKIP_BUFFERING
1521 GstPad *sinkpad = g_weak_ref_get (&sq->sinkpad);
1523 GstCaps *caps = gst_pad_get_current_caps (sinkpad);
1527 /* skip buffering except audio and video */
1528 if ((s = gst_caps_get_structure (caps, 0)) &&
1529 (!g_strrstr (gst_structure_get_name (s), "video")) &&
1530 (!g_strrstr (gst_structure_get_name (s), "audio"))) {
1531 gst_caps_unref (caps);
1532 gst_clear_object (&sinkpad);
1535 gst_caps_unref (caps);
1538 gst_clear_object (&sinkpad);
1541 buffering_level = get_buffering_level (mq, sq);
1543 /* scale so that if buffering_level equals the high watermark,
1544 * the percentage is 100% */
1545 percent = gst_util_uint64_scale (buffering_level, 100, mq->high_watermark);
1550 if (mq->buffering) {
1551 if (buffering_level >= mq->high_watermark) {
1552 mq->buffering = FALSE;
1554 /* make sure it increases */
1555 percent = MAX (mq->buffering_percent, percent);
1557 SET_PERCENT (mq, percent);
1560 gboolean is_buffering = TRUE;
1562 for (iter = mq->queues; iter; iter = g_list_next (iter)) {
1563 GstSingleQueue *oq = (GstSingleQueue *) iter->data;
1565 if (get_buffering_level (mq, oq) >= mq->high_watermark) {
1566 is_buffering = FALSE;
1572 if (is_buffering && buffering_level < mq->low_watermark) {
1573 mq->buffering = TRUE;
1574 SET_PERCENT (mq, percent);
1580 gst_multi_queue_post_buffering (GstMultiQueue * mq)
1582 GstMessage *msg = NULL;
1584 g_mutex_lock (&mq->buffering_post_lock);
1585 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1586 if (mq->buffering_percent_changed) {
1587 gint percent = mq->buffering_percent;
1589 mq->buffering_percent_changed = FALSE;
1591 GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
1592 msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
1594 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1597 gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
1599 g_mutex_unlock (&mq->buffering_post_lock);
1603 recheck_buffering_status (GstMultiQueue * mq)
1605 if (!mq->use_buffering && mq->buffering) {
1606 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1607 mq->buffering = FALSE;
1608 GST_DEBUG_OBJECT (mq,
1609 "Buffering property disabled, but queue was still buffering; "
1610 "setting buffering percentage to 100%%");
1611 SET_PERCENT (mq, 100);
1612 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1615 if (mq->use_buffering) {
1619 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1621 /* force buffering percentage to be recalculated */
1622 old_perc = mq->buffering_percent;
1623 mq->buffering_percent = 0;
1627 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
1628 update_buffering (mq, q);
1629 gst_data_queue_limits_changed (q->queue);
1630 tmp = g_list_next (tmp);
1633 GST_DEBUG_OBJECT (mq,
1634 "Recalculated buffering percentage: old: %d%% new: %d%%",
1635 old_perc, mq->buffering_percent);
1637 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1640 gst_multi_queue_post_buffering (mq);
1644 calculate_interleave (GstMultiQueue * mq, GstSingleQueue * sq)
1646 GstClockTimeDiff low, high;
1647 GstClockTime interleave, other_interleave = 0;
1650 low = high = GST_CLOCK_STIME_NONE;
1651 interleave = mq->interleave;
1652 /* Go over all single queues and calculate lowest/highest value */
1653 for (tmp = mq->queues; tmp; tmp = tmp->next) {
1654 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1655 /* Ignore sparse streams for interleave calculation */
1658 /* If a stream is not active yet (hasn't received any buffers), set
1659 * a maximum interleave to allow it to receive more data */
1662 "queue %d is not active yet, forcing interleave to 5s", oq->id);
1663 mq->interleave = 5 * GST_SECOND;
1664 /* Update max-size time */
1665 mq->max_size.time = mq->interleave;
1666 SET_CHILD_PROPERTY (mq, time);
1670 /* Calculate within each streaming thread */
1671 if (sq && sq->thread != oq->thread) {
1672 if (oq->interleave > other_interleave)
1673 other_interleave = oq->interleave;
1677 if (GST_CLOCK_STIME_IS_VALID (oq->cached_sinktime)) {
1678 if (low == GST_CLOCK_STIME_NONE || oq->cached_sinktime < low)
1679 low = oq->cached_sinktime;
1680 if (high == GST_CLOCK_STIME_NONE || oq->cached_sinktime > high)
1681 high = oq->cached_sinktime;
1684 "queue %d , sinktime:%" GST_STIME_FORMAT " low:%" GST_STIME_FORMAT
1685 " high:%" GST_STIME_FORMAT, oq->id,
1686 GST_STIME_ARGS (oq->cached_sinktime), GST_STIME_ARGS (low),
1687 GST_STIME_ARGS (high));
1690 if (GST_CLOCK_STIME_IS_VALID (low) && GST_CLOCK_STIME_IS_VALID (high)) {
1691 interleave = high - low;
1692 /* Padding of interleave and minimum value */
1693 interleave = (150 * interleave / 100) + mq->min_interleave_time;
1695 sq->interleave = interleave;
1697 interleave = MAX (interleave, other_interleave);
1699 /* Update the stored interleave if:
1700 * * No data has arrived yet (high == low)
1701 * * Or it went higher
1702 * * Or it went lower and we've gone past the previous interleave needed */
1703 if (high == low || interleave > mq->interleave ||
1704 ((mq->last_interleave_update + (2 * MIN (GST_SECOND,
1705 mq->interleave)) < low)
1706 && interleave < (mq->interleave * 3 / 4))) {
1707 /* Update the interleave */
1708 mq->interleave = interleave;
1709 mq->last_interleave_update = high;
1710 /* Update max-size time */
1711 mq->max_size.time = mq->interleave;
1712 SET_CHILD_PROPERTY (mq, time);
1717 GST_DEBUG_OBJECT (mq,
1718 "low:%" GST_STIME_FORMAT " high:%" GST_STIME_FORMAT " interleave:%"
1719 GST_TIME_FORMAT " mq->interleave:%" GST_TIME_FORMAT
1720 " last_interleave_update:%" GST_STIME_FORMAT, GST_STIME_ARGS (low),
1721 GST_STIME_ARGS (high), GST_TIME_ARGS (interleave),
1722 GST_TIME_ARGS (mq->interleave),
1723 GST_STIME_ARGS (mq->last_interleave_update));
1727 /* calculate the diff between running time on the sink and src of the queue.
1728 * This is the total amount of time in the queue.
1729 * WITH LOCK TAKEN */
1731 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
1733 GstClockTimeDiff sink_time, src_time;
1735 if (sq->sink_tainted) {
1736 sink_time = sq->sinktime = my_segment_to_running_time (&sq->sink_segment,
1737 sq->sink_segment.position);
1739 GST_DEBUG_OBJECT (mq,
1740 "queue %d sink_segment.position:%" GST_TIME_FORMAT ", sink_time:%"
1741 GST_STIME_FORMAT, sq->id, GST_TIME_ARGS (sq->sink_segment.position),
1742 GST_STIME_ARGS (sink_time));
1744 if (G_UNLIKELY (sq->last_time == GST_CLOCK_STIME_NONE)) {
1745 /* If the single queue still doesn't have a last_time set, this means
1746 * that nothing has been pushed out yet.
1747 * In order for the high_time computation to be as efficient as possible,
1748 * we set the last_time */
1749 sq->last_time = sink_time;
1751 if (G_UNLIKELY (sink_time != GST_CLOCK_STIME_NONE)) {
1752 /* if we have a time, we become untainted and use the time */
1753 sq->sink_tainted = FALSE;
1754 if (mq->use_interleave) {
1755 sq->cached_sinktime = sink_time;
1756 calculate_interleave (mq, sq);
1760 sink_time = sq->sinktime;
1762 if (sq->src_tainted) {
1763 GstSegment *segment;
1766 if (sq->has_src_segment) {
1767 segment = &sq->src_segment;
1768 position = sq->src_segment.position;
1771 * If the src pad had no segment yet, use the sink segment
1772 * to avoid signalling overrun if the received sink segment has a
1773 * a position > max-size-time while the src pad time would be the default=0
1775 * This can happen when switching pads on chained/adaptive streams and the
1776 * new chain has a segment with a much larger position
1778 segment = &sq->sink_segment;
1779 position = sq->sink_segment.position;
1782 src_time = sq->srctime = my_segment_to_running_time (segment, position);
1783 /* if we have a time, we become untainted and use the time */
1784 if (G_UNLIKELY (src_time != GST_CLOCK_STIME_NONE)) {
1785 sq->src_tainted = FALSE;
1788 src_time = sq->srctime;
1790 GST_DEBUG_OBJECT (mq,
1791 "queue %d, sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT, sq->id,
1792 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
1794 /* This allows for streams with out of order timestamping - sometimes the
1795 * emerging timestamp is later than the arriving one(s) */
1796 if (G_LIKELY (GST_CLOCK_STIME_IS_VALID (sink_time) &&
1797 GST_CLOCK_STIME_IS_VALID (src_time) && sink_time > src_time))
1798 sq->cur_time = sink_time - src_time;
1802 /* updating the time level can change the buffering state */
1803 update_buffering (mq, sq);
1808 /* take a SEGMENT event and apply the values to segment, updating the time
1809 * level of queue. */
1811 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1812 GstSegment * segment)
1814 gst_event_copy_segment (event, segment);
1816 /* now configure the values, we use these to track timestamps on the
1818 if (segment->format != GST_FORMAT_TIME) {
1819 /* non-time format, pretent the current time segment is closed with a
1820 * 0 start and unknown stop time. */
1821 segment->format = GST_FORMAT_TIME;
1826 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1828 /* Make sure we have a valid initial segment position (and not garbage
1830 if (segment->rate > 0.0)
1831 segment->position = segment->start;
1833 segment->position = segment->stop;
1834 if (segment == &sq->sink_segment)
1835 sq->sink_tainted = TRUE;
1837 sq->has_src_segment = TRUE;
1838 sq->src_tainted = TRUE;
1841 GST_DEBUG_OBJECT (mq,
1842 "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1844 /* segment can update the time level of the queue */
1845 update_time_level (mq, sq);
1847 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1848 gst_multi_queue_post_buffering (mq);
1851 /* take a buffer and update segment, updating the time level of the queue. */
1853 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1854 GstClockTime duration, GstSegment * segment)
1856 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1858 /* if no timestamp is set, assume it's continuous with the previous
1860 if (timestamp == GST_CLOCK_TIME_NONE)
1861 timestamp = segment->position;
1864 if (duration != GST_CLOCK_TIME_NONE)
1865 timestamp += duration;
1867 GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1868 sq->id, segment == &sq->sink_segment ? "sink" : "src",
1869 GST_TIME_ARGS (timestamp));
1871 segment->position = timestamp;
1873 if (segment == &sq->sink_segment)
1874 sq->sink_tainted = TRUE;
1876 sq->src_tainted = TRUE;
1878 /* calc diff with other end */
1879 update_time_level (mq, sq);
1880 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1881 gst_multi_queue_post_buffering (mq);
1885 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1886 GstSegment * segment)
1888 GstClockTime timestamp;
1889 GstClockTime duration;
1891 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1893 gst_event_parse_gap (event, ×tamp, &duration);
1895 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1897 if (GST_CLOCK_TIME_IS_VALID (duration)) {
1898 timestamp += duration;
1901 GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1902 sq->id, segment == &sq->sink_segment ? "sink" : "src",
1903 GST_TIME_ARGS (timestamp));
1905 segment->position = timestamp;
1907 if (segment == &sq->sink_segment)
1908 sq->sink_tainted = TRUE;
1910 sq->src_tainted = TRUE;
1912 /* calc diff with other end */
1913 update_time_level (mq, sq);
1916 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1917 gst_multi_queue_post_buffering (mq);
1920 static GstClockTimeDiff
1921 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1923 GstClockTimeDiff time = GST_CLOCK_STIME_NONE;
1925 if (GST_IS_BUFFER (object)) {
1926 GstBuffer *buf = GST_BUFFER_CAST (object);
1927 GstClockTime btime = GST_BUFFER_DTS_OR_PTS (buf);
1929 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1930 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1931 btime += GST_BUFFER_DURATION (buf);
1932 time = my_segment_to_running_time (segment, btime);
1934 } else if (GST_IS_BUFFER_LIST (object)) {
1935 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1939 n = gst_buffer_list_length (list);
1940 for (i = 0; i < n; i++) {
1942 buf = gst_buffer_list_get (list, i);
1943 btime = GST_BUFFER_DTS_OR_PTS (buf);
1944 if (GST_CLOCK_TIME_IS_VALID (btime)) {
1945 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1946 btime += GST_BUFFER_DURATION (buf);
1947 time = my_segment_to_running_time (segment, btime);
1954 } else if (GST_IS_EVENT (object)) {
1955 GstEvent *event = GST_EVENT_CAST (object);
1957 /* For newsegment events return the running time of the start position */
1958 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1959 const GstSegment *new_segment;
1961 gst_event_parse_segment (event, &new_segment);
1962 if (new_segment->format == GST_FORMAT_TIME) {
1964 my_segment_to_running_time ((GstSegment *) new_segment,
1965 new_segment->start);
1967 } else if (GST_EVENT_TYPE (event) == GST_EVENT_GAP) {
1968 GstClockTime ts, dur;
1969 gst_event_parse_gap (event, &ts, &dur);
1970 if (GST_CLOCK_TIME_IS_VALID (ts)) {
1971 if (GST_CLOCK_TIME_IS_VALID (dur))
1973 time = my_segment_to_running_time (segment, ts);
1982 static GstFlowReturn
1983 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1984 GstMiniObject * object, gboolean * allow_drop)
1986 GstFlowReturn result = sq->srcresult;
1987 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
1990 GST_INFO_OBJECT (mq,
1991 "Pushing while corresponding sourcepad has been cleared");
1992 return GST_FLOW_FLUSHING;
1995 if (GST_IS_BUFFER (object)) {
1997 GstClockTime timestamp, duration;
1999 buffer = GST_BUFFER_CAST (object);
2000 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
2001 duration = GST_BUFFER_DURATION (buffer);
2003 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
2005 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
2006 gst_data_queue_limits_changed (sq->queue);
2008 if (G_UNLIKELY (*allow_drop)) {
2009 GST_DEBUG_OBJECT (mq,
2010 "SingleQueue %d : Dropping EOS buffer %p with ts %" GST_TIME_FORMAT,
2011 sq->id, buffer, GST_TIME_ARGS (timestamp));
2012 gst_buffer_unref (buffer);
2014 GST_DEBUG_OBJECT (mq,
2015 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
2016 sq->id, buffer, GST_TIME_ARGS (timestamp));
2017 result = gst_pad_push (srcpad, buffer);
2019 } else if (GST_IS_EVENT (object)) {
2022 event = GST_EVENT_CAST (object);
2024 switch (GST_EVENT_TYPE (event)) {
2025 case GST_EVENT_SEGMENT_DONE:
2026 *allow_drop = FALSE;
2029 result = GST_FLOW_EOS;
2030 if (G_UNLIKELY (*allow_drop))
2031 *allow_drop = FALSE;
2033 case GST_EVENT_STREAM_START:
2034 result = GST_FLOW_OK;
2035 if (G_UNLIKELY (*allow_drop))
2036 *allow_drop = FALSE;
2038 case GST_EVENT_SEGMENT:
2039 apply_segment (mq, sq, event, &sq->src_segment);
2040 /* Applying the segment may have made the queue non-full again, unblock it if needed */
2041 gst_data_queue_limits_changed (sq->queue);
2042 if (G_UNLIKELY (*allow_drop)) {
2043 result = GST_FLOW_OK;
2044 *allow_drop = FALSE;
2048 apply_gap (mq, sq, event, &sq->src_segment);
2049 /* Applying the gap may have made the queue non-full again, unblock it if needed */
2050 gst_data_queue_limits_changed (sq->queue);
2056 if (G_UNLIKELY (*allow_drop)) {
2057 GST_DEBUG_OBJECT (mq,
2058 "SingleQueue %d : Dropping EOS event %p of type %s",
2059 sq->id, event, GST_EVENT_TYPE_NAME (event));
2060 gst_event_unref (event);
2062 GST_DEBUG_OBJECT (mq,
2063 "SingleQueue %d : Pushing event %p of type %s",
2064 sq->id, event, GST_EVENT_TYPE_NAME (event));
2066 gst_pad_push_event (srcpad, event);
2068 } else if (GST_IS_QUERY (object)) {
2072 query = GST_QUERY_CAST (object);
2074 if (G_UNLIKELY (*allow_drop)) {
2075 GST_DEBUG_OBJECT (mq,
2076 "SingleQueue %d : Dropping EOS query %p", sq->id, query);
2077 gst_query_unref (query);
2080 res = gst_pad_peer_query (srcpad, query);
2083 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2084 sq->last_query = res;
2085 sq->last_handled_query = query;
2086 g_cond_signal (&sq->query_handled);
2087 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2089 g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
2093 gst_object_unref (srcpad);
2099 static GstMiniObject *
2100 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
2105 item->object = NULL;
2111 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
2113 if (!item->is_query && item->object)
2114 gst_mini_object_unref (item->object);
2115 g_slice_free (GstMultiQueueItem, item);
2118 /* takes ownership of passed mini object! */
2119 static GstMultiQueueItem *
2120 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
2122 GstMultiQueueItem *item;
2124 item = g_slice_new (GstMultiQueueItem);
2125 item->object = object;
2126 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
2127 item->posid = curid;
2128 item->is_query = GST_IS_QUERY (object);
2130 item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
2131 item->duration = GST_BUFFER_DURATION (object);
2132 if (item->duration == GST_CLOCK_TIME_NONE)
2134 item->visible = TRUE;
2138 static GstMultiQueueItem *
2139 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
2141 GstMultiQueueItem *item;
2143 item = g_slice_new (GstMultiQueueItem);
2144 item->object = object;
2145 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
2146 item->posid = curid;
2147 item->is_query = GST_IS_QUERY (object);
2151 item->visible = FALSE;
2155 /* Each main loop attempts to push buffers until the return value
2156 * is not-linked. not-linked pads are not allowed to push data beyond
2157 * any linked pads, so they don't 'rush ahead of the pack'.
2160 gst_multi_queue_loop (GstPad * pad)
2163 GstMultiQueueItem *item;
2164 GstDataQueueItem *sitem;
2166 GstMiniObject *object = NULL;
2168 GstFlowReturn result;
2169 GstClockTimeDiff next_time;
2171 gboolean do_update_buffering = FALSE;
2172 gboolean dropping = FALSE;
2173 GstPad *srcpad = NULL;
2175 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2176 mq = g_weak_ref_get (&sq->mqueue);
2177 srcpad = g_weak_ref_get (&sq->srcpad);
2183 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
2188 /* Get something from the queue, blocking until that happens, or we get
2190 if (!(gst_data_queue_pop (sq->queue, &sitem)))
2193 item = (GstMultiQueueItem *) sitem;
2194 newid = item->posid;
2196 /* steal the object and destroy the item */
2197 object = gst_multi_queue_item_steal_object (item);
2198 gst_multi_queue_item_destroy (item);
2200 is_buffer = GST_IS_BUFFER (object);
2202 /* Get running time of the item. Events will have GST_CLOCK_STIME_NONE */
2203 next_time = get_running_time (&sq->src_segment, object, FALSE);
2205 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
2206 sq->id, newid, sq->last_oldid);
2208 /* If we're not-linked, we do some extra work because we might need to
2209 * wait before pushing. If we're linked but there's a gap in the IDs,
2210 * or it's the first loop, or we just passed the previous highid,
2211 * we might need to wake some sleeping pad up, so there's extra work
2213 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2214 if (sq->srcresult == GST_FLOW_NOT_LINKED
2215 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
2216 || sq->last_oldid > mq->highid) {
2217 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
2218 gst_flow_get_name (sq->srcresult));
2220 /* Check again if we're flushing after the lock is taken,
2221 * the flush flag might have been changed in the meantime */
2223 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2227 /* Update the nextid so other threads know when to wake us up */
2229 /* Take into account the extra cache time since we're unlinked */
2230 if (GST_CLOCK_STIME_IS_VALID (next_time))
2231 next_time += mq->unlinked_cache_time;
2232 sq->next_time = next_time;
2234 /* Update the oldid (the last ID we output) for highid tracking */
2235 if (sq->last_oldid != G_MAXUINT32)
2236 sq->oldid = sq->last_oldid;
2238 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2239 gboolean should_wait;
2240 /* Go to sleep until it's time to push this buffer */
2242 /* Recompute the highid */
2243 compute_high_id (mq);
2244 /* Recompute the high time */
2245 compute_high_time (mq, sq->groupid);
2247 GST_DEBUG_OBJECT (mq,
2248 "groupid %d high_time %" GST_STIME_FORMAT " next_time %"
2249 GST_STIME_FORMAT, sq->groupid, GST_STIME_ARGS (sq->group_high_time),
2250 GST_STIME_ARGS (next_time));
2252 if (mq->sync_by_running_time) {
2253 if (sq->group_high_time == GST_CLOCK_STIME_NONE) {
2254 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
2255 (mq->high_time == GST_CLOCK_STIME_NONE
2256 || next_time > mq->high_time);
2258 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
2259 next_time > sq->group_high_time;
2262 should_wait = newid > mq->highid;
2264 while (should_wait && sq->srcresult == GST_FLOW_NOT_LINKED) {
2266 GST_DEBUG_OBJECT (mq,
2267 "queue %d sleeping for not-linked wakeup with "
2268 "newid %u, highid %u, next_time %" GST_STIME_FORMAT
2269 ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
2270 GST_STIME_ARGS (next_time), GST_STIME_ARGS (sq->group_high_time));
2272 /* Wake up all non-linked pads before we sleep */
2273 wake_up_next_non_linked (mq);
2276 g_cond_wait (&sq->turn, &mq->qlock);
2280 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2284 /* Recompute the high time and ID */
2285 compute_high_time (mq, sq->groupid);
2286 compute_high_id (mq);
2288 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
2289 "wakeup with newid %u, highid %u, next_time %" GST_STIME_FORMAT
2290 ", high_time %" GST_STIME_FORMAT " mq high_time %" GST_STIME_FORMAT,
2291 sq->id, newid, mq->highid,
2292 GST_STIME_ARGS (next_time), GST_STIME_ARGS (sq->group_high_time),
2293 GST_STIME_ARGS (mq->high_time));
2295 if (mq->sync_by_running_time) {
2296 if (sq->group_high_time == GST_CLOCK_STIME_NONE) {
2297 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
2298 (mq->high_time == GST_CLOCK_STIME_NONE
2299 || next_time > mq->high_time);
2301 should_wait = GST_CLOCK_STIME_IS_VALID (next_time) &&
2302 next_time > sq->group_high_time;
2305 should_wait = newid > mq->highid;
2308 /* Re-compute the high_id in case someone else pushed */
2309 compute_high_id (mq);
2310 compute_high_time (mq, sq->groupid);
2312 compute_high_id (mq);
2313 compute_high_time (mq, sq->groupid);
2314 /* Wake up all non-linked pads */
2315 wake_up_next_non_linked (mq);
2317 /* We're done waiting, we can clear the nextid and nexttime */
2319 sq->next_time = GST_CLOCK_STIME_NONE;
2321 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2326 GST_LOG_OBJECT (mq, "sq:%d BEFORE PUSHING sq->srcresult: %s", sq->id,
2327 gst_flow_get_name (sq->srcresult));
2329 /* Update time stats */
2330 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2331 next_time = get_running_time (&sq->src_segment, object, TRUE);
2332 if (GST_CLOCK_STIME_IS_VALID (next_time)) {
2333 if (sq->last_time == GST_CLOCK_STIME_NONE || sq->last_time < next_time)
2334 sq->last_time = next_time;
2335 if (mq->high_time == GST_CLOCK_STIME_NONE || mq->high_time <= next_time) {
2336 /* Wake up all non-linked pads now that we advanced the high time */
2337 mq->high_time = next_time;
2338 wake_up_next_non_linked (mq);
2341 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2343 /* Try to push out the new object */
2344 result = gst_single_queue_push_one (mq, sq, object, &dropping);
2347 /* Check if we pushed something already and if this is
2348 * now a switch from an active to a non-active stream.
2350 * If it is, we reset all the waiting streams, let them
2351 * push another buffer to see if they're now active again.
2352 * This allows faster switching between streams and prevents
2353 * deadlocks if downstream does any waiting too.
2355 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2356 if (sq->pushed && sq->srcresult == GST_FLOW_OK
2357 && result == GST_FLOW_NOT_LINKED) {
2360 GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
2363 compute_high_id (mq);
2364 compute_high_time (mq, sq->groupid);
2365 do_update_buffering = TRUE;
2367 /* maybe no-one is waiting */
2368 if (mq->numwaiting > 0) {
2369 /* Else figure out which singlequeue(s) need waking up */
2370 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2371 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2373 if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
2374 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
2375 sq2->pushed = FALSE;
2376 sq2->srcresult = GST_FLOW_OK;
2377 g_cond_signal (&sq2->turn);
2386 /* now hold on a bit;
2387 * can not simply throw this result to upstream, because
2388 * that might already be onto another segment, so we have to make
2389 * sure we are relaying the correct info wrt proper segment */
2390 if (result == GST_FLOW_EOS && !dropping &&
2391 sq->srcresult != GST_FLOW_NOT_LINKED) {
2392 GST_DEBUG_OBJECT (mq, "starting EOS drop on sq %d", sq->id);
2394 /* pretend we have not seen EOS yet for upstream's sake */
2395 result = sq->srcresult;
2396 } else if (dropping && gst_data_queue_is_empty (sq->queue)) {
2397 /* queue empty, so stop dropping
2398 * we can commit the result we have now,
2399 * which is either OK after a segment, or EOS */
2400 GST_DEBUG_OBJECT (mq, "committed EOS drop on sq %d", sq->id);
2402 result = GST_FLOW_EOS;
2404 sq->srcresult = result;
2405 sq->last_oldid = newid;
2407 if (do_update_buffering)
2408 update_buffering (mq, sq);
2410 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2411 gst_multi_queue_post_buffering (mq);
2413 GST_LOG_OBJECT (mq, "sq:%d AFTER PUSHING sq->srcresult: %s (is_eos:%d)",
2414 sq->id, gst_flow_get_name (sq->srcresult), GST_PAD_IS_EOS (srcpad));
2416 /* Need to make sure wake up any sleeping pads when we exit */
2417 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2418 if (mq->numwaiting > 0 && (GST_PAD_IS_EOS (srcpad)
2419 || sq->srcresult == GST_FLOW_EOS)) {
2420 compute_high_time (mq, sq->groupid);
2421 compute_high_id (mq);
2422 wake_up_next_non_linked (mq);
2424 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2429 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
2430 && result != GST_FLOW_EOS)
2434 gst_clear_object (&mq);
2435 gst_clear_object (&srcpad);
2441 if (object && !GST_IS_QUERY (object))
2442 gst_mini_object_unref (object);
2444 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2445 sq->last_query = FALSE;
2446 g_cond_signal (&sq->query_handled);
2448 /* Post an error message if we got EOS while downstream
2449 * has returned an error flow return. After EOS there
2450 * will be no further buffer which could propagate the
2452 if ((sq->is_eos || sq->is_segment_done) && sq->srcresult < GST_FLOW_EOS) {
2453 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2454 GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
2456 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2459 /* upstream needs to see fatal result ASAP to shut things down,
2460 * but might be stuck in one of our other full queues;
2461 * so empty this one and trigger dynamic queue growth. At
2462 * this point the srcresult is not OK, NOT_LINKED
2463 * or EOS, i.e. a real failure */
2464 gst_single_queue_flush_queue (sq, FALSE);
2465 single_queue_underrun_cb (sq->queue, sq);
2466 gst_data_queue_set_flushing (sq->queue, TRUE);
2467 gst_pad_pause_task (srcpad);
2468 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
2469 "SingleQueue[%d] task paused, reason:%s",
2470 sq->id, gst_flow_get_name (sq->srcresult));
2476 * gst_multi_queue_chain:
2478 * This is similar to GstQueue's chain function, except:
2479 * _ we don't have leak behaviours,
2480 * _ we push with a unique id (curid)
2482 static GstFlowReturn
2483 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2487 GstMultiQueueItem *item = NULL;
2489 GstClockTime timestamp, duration;
2491 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2492 mq = g_weak_ref_get (&sq->mqueue);
2497 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2503 /* Get a unique incrementing id */
2504 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2506 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
2507 duration = GST_BUFFER_DURATION (buffer);
2510 "SingleQueue %d : about to enqueue buffer %p with id %d (pts:%"
2511 GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT " dur:%" GST_TIME_FORMAT ")",
2512 sq->id, buffer, curid, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2513 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (duration));
2515 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
2517 /* Update interleave before pushing data into queue */
2518 if (mq->use_interleave) {
2519 GstClockTime val = timestamp;
2520 GstClockTimeDiff dval;
2522 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2523 if (val == GST_CLOCK_TIME_NONE)
2524 val = sq->sink_segment.position;
2525 if (duration != GST_CLOCK_TIME_NONE)
2528 dval = my_segment_to_running_time (&sq->sink_segment, val);
2529 if (GST_CLOCK_STIME_IS_VALID (dval)) {
2530 sq->cached_sinktime = dval;
2531 GST_DEBUG_OBJECT (mq,
2532 "Queue %d cached sink time now %" G_GINT64_FORMAT " %"
2533 GST_STIME_FORMAT, sq->id, sq->cached_sinktime,
2534 GST_STIME_ARGS (sq->cached_sinktime));
2535 calculate_interleave (mq, sq);
2537 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2540 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
2543 /* update time level, we must do this after pushing the data in the queue so
2544 * that we never end up filling the queue first. */
2545 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
2548 gst_clear_object (&mq);
2549 return sq->srcresult;
2554 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2555 sq->id, gst_flow_get_name (sq->srcresult));
2557 gst_multi_queue_item_destroy (item);
2562 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
2563 gst_buffer_unref (buffer);
2564 gst_object_unref (mq);
2565 return GST_FLOW_EOS;
2570 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
2571 GstPadMode mode, gboolean active)
2577 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2578 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
2580 /* mq is NULL if the pad is activated/deactivated before being
2581 * added to the multiqueue */
2583 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2586 case GST_PAD_MODE_PUSH:
2588 /* All pads start off linked until they push one buffer */
2589 sq->srcresult = GST_FLOW_OK;
2591 gst_data_queue_set_flushing (sq->queue, FALSE);
2593 sq->srcresult = GST_FLOW_FLUSHING;
2594 sq->last_query = FALSE;
2595 g_cond_signal (&sq->query_handled);
2596 gst_data_queue_set_flushing (sq->queue, TRUE);
2598 /* Wait until streaming thread has finished */
2600 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2601 GST_PAD_STREAM_LOCK (pad);
2603 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2604 gst_data_queue_flush (sq->queue);
2606 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2607 GST_PAD_STREAM_UNLOCK (pad);
2609 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2619 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2620 gst_object_unref (mq);
2626 static GstFlowReturn
2627 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2632 GstMultiQueueItem *item;
2633 gboolean res = TRUE;
2634 GstFlowReturn flowret = GST_FLOW_OK;
2636 GstEvent *sref = NULL;
2640 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2641 mq = (GstMultiQueue *) parent;
2642 srcpad = g_weak_ref_get (&sq->srcpad);
2645 GST_INFO_OBJECT (pad, "Pushing while corresponding sourcepad has been"
2646 " removed already");
2648 return GST_FLOW_FLUSHING;
2651 type = GST_EVENT_TYPE (event);
2654 case GST_EVENT_STREAM_START:
2656 if (mq->sync_by_running_time) {
2657 GstStreamFlags stream_flags;
2658 gst_event_parse_stream_flags (event, &stream_flags);
2659 if ((stream_flags & GST_STREAM_FLAG_SPARSE)) {
2660 GST_INFO_OBJECT (mq, "SingleQueue %d is a sparse stream", sq->id);
2661 sq->is_sparse = TRUE;
2665 sq->thread = g_thread_self ();
2667 /* Remove EOS flag */
2671 case GST_EVENT_FLUSH_START:
2672 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
2675 res = gst_pad_push_event (srcpad, event);
2677 gst_single_queue_flush (mq, sq, TRUE, FALSE);
2678 gst_single_queue_pause (mq, sq);
2681 case GST_EVENT_FLUSH_STOP:
2682 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
2685 res = gst_pad_push_event (srcpad, event);
2687 gst_single_queue_flush (mq, sq, FALSE, FALSE);
2688 gst_single_queue_start (mq, sq);
2689 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
2690 /* need to reset the buffering data after seeking */
2694 GstSingleQueue *q = (GstSingleQueue *) tmp->data;
2697 tmp = g_list_next (tmp);
2699 recheck_buffering_status (mq);
2703 case GST_EVENT_SEGMENT:
2704 sq->is_segment_done = FALSE;
2705 sref = gst_event_ref (event);
2708 /* take ref because the queue will take ownership and we need the event
2709 * afterwards to update the segment */
2710 sref = gst_event_ref (event);
2711 if (mq->use_interleave) {
2712 GstClockTime val, dur;
2714 gst_event_parse_gap (event, &val, &dur);
2715 if (GST_CLOCK_TIME_IS_VALID (val)) {
2716 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2717 if (GST_CLOCK_TIME_IS_VALID (dur))
2719 stime = my_segment_to_running_time (&sq->sink_segment, val);
2720 if (GST_CLOCK_STIME_IS_VALID (stime)) {
2721 sq->cached_sinktime = stime;
2722 calculate_interleave (mq, sq);
2724 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2730 if (!(GST_EVENT_IS_SERIALIZED (event))) {
2731 res = gst_pad_push_event (srcpad, event);
2737 /* if eos, we are always full, so avoid hanging incoming indefinitely */
2741 /* Get an unique incrementing id. */
2742 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2744 item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
2746 GST_DEBUG_OBJECT (mq,
2747 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
2748 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
2750 if (!gst_data_queue_push (sq->queue, (GstDataQueueItem *) item))
2753 /* mark EOS when we received one, we must do that after putting the
2754 * buffer in the queue because EOS marks the buffer as filled. */
2756 case GST_EVENT_SEGMENT_DONE:
2757 sq->is_segment_done = TRUE;
2758 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2759 update_buffering (mq, sq);
2760 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2761 single_queue_overrun_cb (sq->queue, sq);
2762 gst_multi_queue_post_buffering (mq);
2765 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2768 /* Post an error message if we got EOS while downstream
2769 * has returned an error flow return. After EOS there
2770 * will be no further buffer which could propagate the
2772 if (sq->srcresult < GST_FLOW_EOS) {
2773 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2774 GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
2776 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2779 /* EOS affects the buffering state */
2780 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2781 update_buffering (mq, sq);
2782 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2783 single_queue_overrun_cb (sq->queue, sq);
2784 gst_multi_queue_post_buffering (mq);
2786 case GST_EVENT_SEGMENT:
2787 apply_segment (mq, sq, sref, &sq->sink_segment);
2788 gst_event_unref (sref);
2789 /* a new segment allows us to accept more buffers if we got EOS
2790 * from downstream */
2791 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2792 if (sq->srcresult == GST_FLOW_EOS)
2793 sq->srcresult = GST_FLOW_OK;
2794 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2798 apply_gap (mq, sq, sref, &sq->sink_segment);
2799 gst_event_unref (sref);
2806 gst_object_unref (srcpad);
2808 flowret = GST_FLOW_ERROR;
2809 GST_DEBUG_OBJECT (mq, "SingleQueue %d : returning %s", sq->id,
2810 gst_flow_get_name (flowret));
2815 gst_object_unref (srcpad);
2816 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2817 sq->id, gst_flow_get_name (sq->srcresult));
2819 gst_event_unref (sref);
2820 gst_multi_queue_item_destroy (item);
2821 return sq->srcresult;
2825 gst_object_unref (srcpad);
2826 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return GST_FLOW_EOS");
2827 gst_event_unref (event);
2828 return GST_FLOW_EOS;
2833 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
2839 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2840 mq = (GstMultiQueue *) parent;
2842 switch (GST_QUERY_TYPE (query)) {
2844 if (GST_QUERY_IS_SERIALIZED (query)) {
2846 GstMultiQueueItem *item;
2848 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2849 if (sq->srcresult != GST_FLOW_OK)
2852 /* serialized events go in the queue. We need to be certain that we
2853 * don't cause deadlocks waiting for the query return value. We check if
2854 * the queue is empty (nothing is blocking downstream and the query can
2855 * be pushed for sure) or we are not buffering. If we are buffering,
2856 * the pipeline waits to unblock downstream until our queue fills up
2857 * completely, which can not happen if we block on the query..
2858 * Therefore we only potentially block when we are not buffering. */
2859 if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
2860 /* Get an unique incrementing id. */
2861 curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2863 item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
2865 GST_DEBUG_OBJECT (mq,
2866 "SingleQueue %d : Enqueuing query %p of type %s with id %d",
2867 sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
2868 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2869 res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
2870 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2871 #ifdef TIZEN_FEATURE_MQ_MODIFICATION
2872 if (!res || sq->flushing) {
2873 gst_multi_queue_item_destroy (item);
2877 if (!res || sq->flushing)
2880 /* it might be that the query has been taken out of the queue
2881 * while we were unlocked. So, we need to check if the last
2882 * handled query is the same one than the one we just
2883 * pushed. If it is, we don't need to wait for the condition
2884 * variable, otherwise we wait for the condition variable to
2886 while (!sq->flushing && sq->srcresult == GST_FLOW_OK
2887 && sq->last_handled_query != query)
2888 g_cond_wait (&sq->query_handled, &mq->qlock);
2889 res = sq->last_query;
2890 sq->last_handled_query = NULL;
2892 GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
2893 "queue is not empty");
2896 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2898 /* default handling */
2899 res = gst_pad_query_default (pad, parent, query);
2907 GST_DEBUG_OBJECT (mq, "Flushing");
2908 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2914 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2915 GstPadMode mode, gboolean active)
2921 sq = GST_MULTIQUEUE_PAD (pad)->sq;
2922 mq = g_weak_ref_get (&sq->mqueue);
2925 GST_ERROR_OBJECT (pad, "No multique set anymore, can't activate pad");
2930 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2933 case GST_PAD_MODE_PUSH:
2935 gst_single_queue_flush (mq, sq, FALSE, TRUE);
2936 result = parent ? gst_single_queue_start (mq, sq) : TRUE;
2938 gst_single_queue_flush (mq, sq, TRUE, TRUE);
2939 result = gst_single_queue_stop (mq, sq);
2946 gst_object_unref (mq);
2951 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2953 GstSingleQueue *sq = GST_MULTIQUEUE_PAD (pad)->sq;
2954 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
2956 GstPad *sinkpad = g_weak_ref_get (&sq->sinkpad);
2958 if (!mq || !sinkpad) {
2959 gst_clear_object (&sinkpad);
2960 gst_clear_object (&mq);
2961 GST_INFO_OBJECT (pad, "No multique/sinkpad set anymore, flushing");
2966 switch (GST_EVENT_TYPE (event)) {
2967 case GST_EVENT_RECONFIGURE:
2968 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2969 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2970 sq->srcresult = GST_FLOW_OK;
2971 g_cond_signal (&sq->turn);
2973 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2975 ret = gst_pad_push_event (sinkpad, event);
2978 ret = gst_pad_push_event (sinkpad, event);
2982 gst_object_unref (sinkpad);
2983 gst_object_unref (mq);
2989 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2993 /* FIXME, Handle position offset depending on queue size */
2994 switch (GST_QUERY_TYPE (query)) {
2996 /* default handling */
2997 res = gst_pad_query_default (pad, parent, query);
3004 * Next-non-linked functions
3007 /* WITH LOCK TAKEN */
3009 wake_up_next_non_linked (GstMultiQueue * mq)
3013 /* maybe no-one is waiting */
3014 if (mq->numwaiting < 1)
3017 if (mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (mq->high_time)) {
3018 /* Else figure out which singlequeue(s) need waking up */
3019 for (tmp = mq->queues; tmp; tmp = tmp->next) {
3020 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3021 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
3022 GstClockTimeDiff high_time;
3024 if (GST_CLOCK_STIME_IS_VALID (sq->group_high_time))
3025 high_time = sq->group_high_time;
3027 high_time = mq->high_time;
3029 if (GST_CLOCK_STIME_IS_VALID (sq->next_time) &&
3030 GST_CLOCK_STIME_IS_VALID (high_time)
3031 && sq->next_time <= high_time) {
3032 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
3033 g_cond_signal (&sq->turn);
3038 /* Else figure out which singlequeue(s) need waking up */
3039 for (tmp = mq->queues; tmp; tmp = tmp->next) {
3040 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3041 if (sq->srcresult == GST_FLOW_NOT_LINKED &&
3042 sq->nextid != 0 && sq->nextid <= mq->highid) {
3043 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
3044 g_cond_signal (&sq->turn);
3050 /* WITH LOCK TAKEN */
3052 compute_high_id (GstMultiQueue * mq)
3054 /* The high-id is either the highest id among the linked pads, or if all
3055 * pads are not-linked, it's the lowest not-linked pad */
3057 guint32 lowest = G_MAXUINT32;
3058 guint32 highid = G_MAXUINT32;
3060 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
3061 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3062 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
3065 GST_INFO_OBJECT (mq,
3066 "srcpad has been removed already... ignoring single queue");
3071 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
3072 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
3074 /* No need to consider queues which are not waiting */
3075 if (sq->nextid == 0) {
3076 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
3077 gst_object_unref (srcpad);
3081 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
3082 if (sq->nextid < lowest)
3083 lowest = sq->nextid;
3084 } else if (!GST_PAD_IS_EOS (srcpad) && sq->srcresult != GST_FLOW_EOS) {
3085 /* If we don't have a global highid, or the global highid is lower than
3086 * this single queue's last outputted id, store the queue's one,
3087 * unless the singlequeue output is at EOS */
3088 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
3091 gst_object_unref (srcpad);
3094 if (highid == G_MAXUINT32 || lowest < highid)
3095 mq->highid = lowest;
3097 mq->highid = highid;
3099 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
3103 /* WITH LOCK TAKEN */
3105 compute_high_time (GstMultiQueue * mq, guint groupid)
3107 /* The high-time is either the highest last time among the linked
3108 * pads, or if all pads are not-linked, it's the lowest nex time of
3111 GstClockTimeDiff highest = GST_CLOCK_STIME_NONE;
3112 GstClockTimeDiff lowest = GST_CLOCK_STIME_NONE;
3113 GstClockTimeDiff group_high = GST_CLOCK_STIME_NONE;
3114 GstClockTimeDiff group_low = GST_CLOCK_STIME_NONE;
3115 GstClockTimeDiff res;
3116 /* Number of streams which belong to groupid */
3117 guint group_count = 0;
3119 if (!mq->sync_by_running_time)
3120 /* return GST_CLOCK_STIME_NONE; */
3123 for (tmp = mq->queues; tmp; tmp = tmp->next) {
3124 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3125 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
3128 GST_INFO_OBJECT (mq,
3129 "srcpad has been removed already... ignoring single queue");
3135 "inspecting sq:%d (group:%d) , next_time:%" GST_STIME_FORMAT
3136 ", last_time:%" GST_STIME_FORMAT ", srcresult:%s", sq->id, sq->groupid,
3137 GST_STIME_ARGS (sq->next_time), GST_STIME_ARGS (sq->last_time),
3138 gst_flow_get_name (sq->srcresult));
3140 if (sq->groupid == groupid)
3143 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
3144 /* No need to consider queues which are not waiting */
3145 if (!GST_CLOCK_STIME_IS_VALID (sq->next_time)) {
3146 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
3147 gst_object_unref (srcpad);
3151 if (lowest == GST_CLOCK_STIME_NONE || sq->next_time < lowest)
3152 lowest = sq->next_time;
3153 if (sq->groupid == groupid && (group_low == GST_CLOCK_STIME_NONE
3154 || sq->next_time < group_low))
3155 group_low = sq->next_time;
3156 } else if (!GST_PAD_IS_EOS (srcpad) && sq->srcresult != GST_FLOW_EOS) {
3157 /* If we don't have a global high time, or the global high time
3158 * is lower than this single queue's last outputted time, store
3159 * the queue's one, unless the singlequeue output is at EOS. */
3160 if (highest == GST_CLOCK_STIME_NONE
3161 || (sq->last_time != GST_CLOCK_STIME_NONE && sq->last_time > highest))
3162 highest = sq->last_time;
3163 if (sq->groupid == groupid && (group_high == GST_CLOCK_STIME_NONE
3164 || (sq->last_time != GST_CLOCK_STIME_NONE
3165 && sq->last_time > group_high)))
3166 group_high = sq->last_time;
3169 "highest now %" GST_STIME_FORMAT " lowest %" GST_STIME_FORMAT,
3170 GST_STIME_ARGS (highest), GST_STIME_ARGS (lowest));
3171 if (sq->groupid == groupid)
3173 "grouphigh %" GST_STIME_FORMAT " grouplow %" GST_STIME_FORMAT,
3174 GST_STIME_ARGS (group_high), GST_STIME_ARGS (group_low));
3176 gst_object_unref (srcpad);
3179 if (highest == GST_CLOCK_STIME_NONE)
3180 mq->high_time = lowest;
3182 mq->high_time = highest;
3184 /* If there's only one stream of a given type, use the global high */
3185 if (group_count < 2)
3186 res = GST_CLOCK_STIME_NONE;
3187 else if (group_high == GST_CLOCK_STIME_NONE)
3192 GST_LOG_OBJECT (mq, "group count %d for groupid %u", group_count, groupid);
3194 "MQ High time is now : %" GST_STIME_FORMAT ", group %d high time %"
3195 GST_STIME_FORMAT ", lowest non-linked %" GST_STIME_FORMAT,
3196 GST_STIME_ARGS (mq->high_time), groupid, GST_STIME_ARGS (mq->high_time),
3197 GST_STIME_ARGS (lowest));
3199 for (tmp = mq->queues; tmp; tmp = tmp->next) {
3200 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
3201 if (groupid == sq->groupid)
3202 sq->group_high_time = res;
3206 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
3207 ((q)->max_size.format) <= (value))
3209 #ifdef TIZEN_FEATURE_MQ_MODIFICATION_EXTRA_SIZE_TIME
3210 #define IS_FILLED_EXTRA(q, format, value) ((((q)->extra_size.format) != 0) && (((q)->max_size.format) != 0) && \
3211 (((q)->extra_size.format)+((q)->max_size.format)) <= (value))
3214 * GstSingleQueue functions
3217 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
3220 GstDataQueueSize size;
3221 gboolean filled = TRUE;
3222 gboolean empty_found = FALSE;
3223 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
3226 GST_ERROR ("No multique set anymore, not doing anything");
3231 gst_data_queue_get_level (sq->queue, &size);
3234 "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
3235 G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
3236 sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
3239 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
3241 /* check if we reached the hard time/bytes limits;
3242 time limit is only taken into account for non-sparse streams */
3243 if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
3244 (!sq->is_sparse && IS_FILLED (sq, time, sq->cur_time))) {
3248 /* Search for empty queues */
3249 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
3250 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
3255 if (oq->srcresult == GST_FLOW_NOT_LINKED) {
3256 GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
3260 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
3261 if (gst_data_queue_is_empty (oq->queue) && !oq->is_sparse) {
3262 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
3268 /* if hard limits are not reached then we allow one more buffer in the full
3269 * queue, but only if any of the other singelqueues are empty */
3271 if (IS_FILLED (sq, visible, size.visible)) {
3272 sq->max_size.visible = size.visible + 1;
3273 GST_DEBUG_OBJECT (mq,
3274 "Bumping single queue %d max visible to %d",
3275 sq->id, sq->max_size.visible);
3281 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
3282 gst_object_unref (mq);
3284 /* Overrun is always forwarded, since this is blocking the upstream element */
3286 GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
3287 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
3292 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
3294 gboolean empty = TRUE;
3295 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
3299 GST_ERROR ("No multique set anymore, not doing anything");
3304 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
3305 GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
3306 gst_object_unref (mq);
3310 "Single Queue %d is empty, Checking other single queues", sq->id);
3313 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
3314 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
3315 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
3317 if (gst_data_queue_is_full (oq->queue)) {
3318 GstDataQueueSize size;
3320 gst_data_queue_get_level (oq->queue, &size);
3321 if (IS_FILLED (oq, visible, size.visible)) {
3322 oq->max_size.visible = size.visible + 1;
3323 GST_DEBUG_OBJECT (mq,
3324 "queue %d is filled, bumping its max visible to %d", oq->id,
3325 oq->max_size.visible);
3326 gst_data_queue_limits_changed (oq->queue);
3329 if (!gst_data_queue_is_empty (oq->queue) || oq->is_sparse)
3332 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
3333 gst_object_unref (mq);
3336 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
3337 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
3342 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
3343 guint64 time, GstSingleQueue * sq)
3346 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
3349 GST_ERROR ("No multique set anymore, let's say we are full");
3354 GST_DEBUG_OBJECT (mq,
3355 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
3356 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
3357 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
3359 /* we are always filled on EOS */
3360 if (sq->is_eos || sq->is_segment_done) {
3365 /* we never go past the max visible items unless we are in buffering mode */
3366 if (!mq->use_buffering && IS_FILLED (sq, visible, visible)) {
3371 /* check time or bytes */
3372 #ifdef TIZEN_FEATURE_MQ_MODIFICATION_EXTRA_SIZE_TIME
3373 res = IS_FILLED_EXTRA (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
3375 res = IS_FILLED (sq, bytes, bytes);
3377 /* We only care about limits in time if we're not a sparse stream or
3378 * we're not syncing by running time */
3379 if (!sq->is_sparse || !mq->sync_by_running_time) {
3380 /* If unlinked, take into account the extra unlinked cache time */
3381 if (mq->sync_by_running_time && sq->srcresult == GST_FLOW_NOT_LINKED) {
3382 if (sq->cur_time > mq->unlinked_cache_time)
3383 res |= IS_FILLED (sq, time, sq->cur_time - mq->unlinked_cache_time);
3387 res |= IS_FILLED (sq, time, sq->cur_time);
3390 gst_object_unref (mq);
3396 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
3398 GstDataQueueItem *sitem;
3399 GstMultiQueueItem *mitem;
3400 gboolean was_flushing = FALSE;
3401 GstPad *srcpad = g_weak_ref_get (&sq->srcpad);
3402 GstMultiQueue *mq = g_weak_ref_get (&sq->mqueue);
3404 while (!gst_data_queue_is_empty (sq->queue)) {
3405 GstMiniObject *data;
3407 /* FIXME: If this fails here although the queue is not empty,
3408 * we're flushing... but we want to rescue all sticky
3409 * events nonetheless.
3411 if (!gst_data_queue_pop (sq->queue, &sitem)) {
3412 was_flushing = TRUE;
3413 gst_data_queue_set_flushing (sq->queue, FALSE);
3417 mitem = (GstMultiQueueItem *) sitem;
3419 data = sitem->object;
3421 if (!full && !mitem->is_query && GST_IS_EVENT (data)
3422 && srcpad && GST_EVENT_IS_STICKY (data)
3423 && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
3424 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
3425 gst_pad_store_sticky_event (srcpad, GST_EVENT_CAST (data));
3428 sitem->destroy (sitem);
3430 gst_clear_object (&srcpad);
3432 gst_data_queue_flush (sq->queue);
3434 gst_data_queue_set_flushing (sq->queue, TRUE);
3437 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
3438 update_buffering (mq, sq);
3439 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
3440 gst_multi_queue_post_buffering (mq);
3441 gst_object_unref (mq);
3446 gst_single_queue_unref (GstSingleQueue * sq)
3448 if (g_atomic_int_dec_and_test (&sq->refcount)) {
3450 gst_data_queue_flush (sq->queue);
3451 g_object_unref (sq->queue);
3452 g_cond_clear (&sq->turn);
3453 g_cond_clear (&sq->query_handled);
3454 g_weak_ref_clear (&sq->sinkpad);
3455 g_weak_ref_clear (&sq->srcpad);
3456 g_weak_ref_clear (&sq->mqueue);
3462 static GstSingleQueue *
3463 gst_single_queue_ref (GstSingleQueue * squeue)
3465 g_atomic_int_inc (&squeue->refcount);
3470 static GstSingleQueue *
3471 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
3473 GstPad *srcpad, *sinkpad;
3475 GstPadTemplate *templ;
3478 guint temp_id = (id == -1) ? 0 : id;
3480 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
3482 /* Find an unused queue ID, if possible the passed one */
3483 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
3484 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
3485 /* This works because the IDs are sorted in ascending order */
3486 if (sq2->id == temp_id) {
3487 /* If this ID was requested by the caller return NULL,
3488 * otherwise just get us the next one */
3490 temp_id = sq2->id + 1;
3492 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
3495 } else if (sq2->id > temp_id) {
3500 sq = g_new0 (GstSingleQueue, 1);
3501 g_atomic_int_set (&sq->refcount, 1);
3505 sq->groupid = DEFAULT_PAD_GROUP_ID;
3506 sq->group_high_time = GST_CLOCK_STIME_NONE;
3508 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
3509 mqueue->queues_cookie++;
3511 /* copy over max_size and extra_size so we don't need to take the lock
3512 * any longer when checking if the queue is full. */
3513 sq->max_size.visible = mqueue->max_size.visible;
3514 sq->max_size.bytes = mqueue->max_size.bytes;
3515 sq->max_size.time = mqueue->max_size.time;
3517 sq->extra_size.visible = mqueue->extra_size.visible;
3518 sq->extra_size.bytes = mqueue->extra_size.bytes;
3519 sq->extra_size.time = mqueue->extra_size.time;
3521 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
3523 g_weak_ref_init (&sq->mqueue, mqueue);
3524 sq->srcresult = GST_FLOW_FLUSHING;
3526 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
3527 single_queue_check_full,
3528 (GstDataQueueFullCallback) single_queue_overrun_cb,
3529 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
3531 sq->is_sparse = FALSE;
3532 sq->flushing = FALSE;
3534 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
3535 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
3539 sq->next_time = GST_CLOCK_STIME_NONE;
3540 sq->last_time = GST_CLOCK_STIME_NONE;
3541 g_cond_init (&sq->turn);
3542 g_cond_init (&sq->query_handled);
3544 sq->sinktime = GST_CLOCK_STIME_NONE;
3545 sq->srctime = GST_CLOCK_STIME_NONE;
3546 sq->sink_tainted = TRUE;
3547 sq->src_tainted = TRUE;
3549 name = g_strdup_printf ("sink_%u", sq->id);
3550 templ = gst_static_pad_template_get (&sinktemplate);
3551 sinkpad = g_object_new (GST_TYPE_MULTIQUEUE_PAD, "name", name,
3552 "direction", templ->direction, "template", templ, NULL);
3553 g_weak_ref_init (&sq->sinkpad, sinkpad);
3554 gst_object_unref (templ);
3557 GST_MULTIQUEUE_PAD (sinkpad)->sq = sq;
3559 gst_pad_set_chain_function (sinkpad,
3560 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
3561 gst_pad_set_activatemode_function (sinkpad,
3562 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
3563 gst_pad_set_event_full_function (sinkpad,
3564 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
3565 gst_pad_set_query_function (sinkpad,
3566 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
3567 gst_pad_set_iterate_internal_links_function (sinkpad,
3568 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
3569 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
3571 name = g_strdup_printf ("src_%u", sq->id);
3572 templ = gst_static_pad_template_get (&srctemplate);
3573 srcpad = g_object_new (GST_TYPE_MULTIQUEUE_PAD, "name", name,
3574 "direction", templ->direction, "template", templ, NULL);
3575 g_weak_ref_init (&sq->srcpad, srcpad);
3576 gst_object_unref (templ);
3579 GST_MULTIQUEUE_PAD (srcpad)->sq = gst_single_queue_ref (sq);
3581 gst_pad_set_activatemode_function (srcpad,
3582 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
3583 gst_pad_set_event_function (srcpad,
3584 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
3585 gst_pad_set_query_function (srcpad,
3586 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
3587 gst_pad_set_iterate_internal_links_function (srcpad,
3588 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
3589 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PROXY_CAPS);
3591 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
3593 /* only activate the pads when we are not in the NULL state
3594 * and add the pad under the state_lock to prevent state changes
3595 * between activating and adding */
3596 g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
3597 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
3598 gst_pad_set_active (srcpad, TRUE);
3599 gst_pad_set_active (sinkpad, TRUE);
3601 gst_element_add_pad (GST_ELEMENT (mqueue), srcpad);
3602 gst_element_add_pad (GST_ELEMENT (mqueue), sinkpad);
3603 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
3604 gst_single_queue_start (mqueue, sq);
3606 g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
3608 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",