Make use of the new GST_ELEMENT_FLOW_ERROR API all around.
[platform/upstream/gstreamer.git] / plugins / elements / gstmultiqueue.c
1 /* GStreamer
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>
6  *
7  * gstmultiqueue.c:
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /**
26  * SECTION:element-multiqueue
27  * @see_also: #GstQueue
28  *
29  * <refsect2>
30  * <para>
31  * Multiqueue is similar to a normal #GstQueue with the following additional
32  * features:
33  * <orderedlist>
34  * <listitem>
35  *   <itemizedlist><title>Multiple streamhandling</title>
36  *   <listitem><para>
37  *     The element handles queueing data on more than one stream at once. To
38  *     achieve such a feature it has request sink pads (sink&percnt;u) and
39  *     'sometimes' src pads (src&percnt;u).
40  *   </para><para>
41  *     When requesting a given sinkpad with gst_element_request_pad(),
42  *     the associated srcpad for that stream will be created.
43  *     Example: requesting sink1 will generate src1.
44  *   </para></listitem>
45  *   </itemizedlist>
46  * </listitem>
47  * <listitem>
48  *   <itemizedlist><title>Non-starvation on multiple streams</title>
49  *   <listitem><para>
50  *     If more than one stream is used with the element, the streams' queues
51  *     will be dynamically grown (up to a limit), in order to ensure that no
52  *     stream is risking data starvation. This guarantees that at any given
53  *     time there are at least N bytes queued and available for each individual
54  *     stream.
55  *   </para><para>
56  *     If an EOS event comes through a srcpad, the associated queue will be
57  *     considered as 'not-empty' in the queue-size-growing algorithm.
58  *   </para></listitem>
59  *   </itemizedlist>
60  * </listitem>
61  * <listitem>
62  *   <itemizedlist><title>Non-linked srcpads graceful handling</title>
63  *   <listitem><para>
64  *     In order to better support dynamic switching between streams, the multiqueue
65  *     (unlike the current GStreamer queue) continues to push buffers on non-linked
66  *     pads rather than shutting down.
67  *   </para><para>
68  *     In addition, to prevent a non-linked stream from very quickly consuming all
69  *     available buffers and thus 'racing ahead' of the other streams, the element
70  *     must ensure that buffers and inlined events for a non-linked stream are pushed
71  *     in the same order as they were received, relative to the other streams
72  *     controlled by the element. This means that a buffer cannot be pushed to a
73  *     non-linked pad any sooner than buffers in any other stream which were received
74  *     before it.
75  *   </para></listitem>
76  *   </itemizedlist>
77  * </listitem>
78  * </orderedlist>
79  * </para>
80  * <para>
81  *   Data is queued until one of the limits specified by the
82  *   #GstMultiQueue:max-size-buffers, #GstMultiQueue:max-size-bytes and/or
83  *   #GstMultiQueue:max-size-time properties has been reached. Any attempt to push
84  *   more buffers into the queue will block the pushing thread until more space
85  *   becomes available. #GstMultiQueue:extra-size-buffers,
86  * </para>
87  * <para>
88  *   #GstMultiQueue:extra-size-bytes and #GstMultiQueue:extra-size-time are
89  *   currently unused.
90  * </para>
91  * <para>
92  *   The default queue size limits are 5 buffers, 10MB of data, or
93  *   two second worth of data, whichever is reached first. Note that the number
94  *   of buffers will dynamically grow depending on the fill level of 
95  *   other queues.
96  * </para>
97  * <para>
98  *   The #GstMultiQueue::underrun signal is emitted when all of the queues
99  *   are empty. The #GstMultiQueue::overrun signal is emitted when one of the
100  *   queues is filled.
101  *   Both signals are emitted from the context of the streaming thread.
102  * </para>
103  * <para>
104  *   When using #GstMultiQueue:sync-by-running-time the unlinked streams will
105  *   be throttled by the highest running-time of linked streams. This allows
106  *   further relinking of those unlinked streams without them being in the
107  *   future (i.e. to achieve gapless playback).
108  *   When dealing with streams which have got different consumption requirements
109  *   downstream (ex: video decoders which will consume more buffer (in time) than
110  *   audio decoders), it is recommended to group streams of the same type
111  *   by using the pad "group-id" property. This will further throttle streams
112  *   in time within that group.
113  * </para>
114  * </refsect2>
115  */
116
117 #ifdef HAVE_CONFIG_H
118 #  include "config.h"
119 #endif
120
121 #include <gst/gst.h>
122 #include <stdio.h>
123 #include "gstmultiqueue.h"
124 #include <gst/glib-compat-private.h>
125
126 /**
127  * GstSingleQueue:
128  * @sinkpad: associated sink #GstPad
129  * @srcpad: associated source #GstPad
130  *
131  * Structure containing all information and properties about
132  * a single queue.
133  */
134 typedef struct _GstSingleQueue GstSingleQueue;
135
136 struct _GstSingleQueue
137 {
138   /* unique identifier of the queue */
139   guint id;
140   /* group of streams to which this queue belongs to */
141   guint groupid;
142
143   GstMultiQueue *mqueue;
144
145   GstPad *sinkpad;
146   GstPad *srcpad;
147
148   /* flowreturn of previous srcpad push */
149   GstFlowReturn srcresult;
150   /* If something was actually pushed on
151    * this pad after flushing/pad activation
152    * and the srcresult corresponds to something
153    * real
154    */
155   gboolean pushed;
156
157   /* segments */
158   GstSegment sink_segment;
159   GstSegment src_segment;
160   gboolean has_src_segment;     /* preferred over initializing the src_segment to
161                                  * UNDEFINED as this doesn't requires adding ifs
162                                  * in every segment usage */
163
164   /* position of src/sink */
165   GstClockTimeDiff sinktime, srctime;
166   /* cached input value, used for interleave */
167   GstClockTimeDiff cached_sinktime;
168   /* TRUE if either position needs to be recalculated */
169   gboolean sink_tainted, src_tainted;
170
171   /* queue of data */
172   GstDataQueue *queue;
173   GstDataQueueSize max_size, extra_size;
174   GstClockTime cur_time;
175   gboolean is_eos;
176   gboolean is_sparse;
177   gboolean flushing;
178   gboolean active;
179
180   /* Protected by global lock */
181   guint32 nextid;               /* ID of the next object waiting to be pushed */
182   guint32 oldid;                /* ID of the last object pushed (last in a series) */
183   guint32 last_oldid;           /* Previously observed old_id, reset to MAXUINT32 on flush */
184   GstClockTimeDiff next_time;   /* End running time of next buffer to be pushed */
185   GstClockTimeDiff last_time;   /* Start running time of last pushed buffer */
186   GCond turn;                   /* SingleQueue turn waiting conditional */
187
188   /* for serialized queries */
189   GCond query_handled;
190   gboolean last_query;
191   GstQuery *last_handled_query;
192
193   /* For interleave calculation */
194   GThread *thread;
195 };
196
197
198 /* Extension of GstDataQueueItem structure for our usage */
199 typedef struct _GstMultiQueueItem GstMultiQueueItem;
200
201 struct _GstMultiQueueItem
202 {
203   GstMiniObject *object;
204   guint size;
205   guint64 duration;
206   gboolean visible;
207
208   GDestroyNotify destroy;
209   guint32 posid;
210
211   gboolean is_query;
212 };
213
214 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, guint id);
215 static void gst_single_queue_free (GstSingleQueue * squeue);
216
217 static void wake_up_next_non_linked (GstMultiQueue * mq);
218 static void compute_high_id (GstMultiQueue * mq);
219 static GstClockTimeDiff compute_high_time (GstMultiQueue * mq, guint groupid);
220 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
221 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
222
223 static void update_buffering (GstMultiQueue * mq, GstSingleQueue * sq);
224 static void gst_multi_queue_post_buffering (GstMultiQueue * mq);
225 static void recheck_buffering_status (GstMultiQueue * mq);
226
227 static void gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full);
228
229 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
230     GST_PAD_SINK,
231     GST_PAD_REQUEST,
232     GST_STATIC_CAPS_ANY);
233
234 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
235     GST_PAD_SRC,
236     GST_PAD_SOMETIMES,
237     GST_STATIC_CAPS_ANY);
238
239 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
240 #define GST_CAT_DEFAULT (multi_queue_debug)
241
242 /* Signals and args */
243 enum
244 {
245   SIGNAL_UNDERRUN,
246   SIGNAL_OVERRUN,
247   LAST_SIGNAL
248 };
249
250 /* default limits, we try to keep up to 2 seconds of data and if there is not
251  * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
252  * there is data in the queues. Normally, the byte and time limits are not hit
253  * in theses conditions. */
254 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
255 #define DEFAULT_MAX_SIZE_BUFFERS 5
256 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
257
258 /* second limits. When we hit one of the above limits we are probably dealing
259  * with a badly muxed file and we scale the limits to these emergency values.
260  * This is currently not yet implemented.
261  * Since we dynamically scale the queue buffer size up to the limits but avoid
262  * going above the max-size-buffers when we can, we don't really need this
263  * aditional extra size. */
264 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024       /* 10 MB */
265 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
266 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
267
268 #define DEFAULT_USE_BUFFERING FALSE
269 #define DEFAULT_LOW_PERCENT   10
270 #define DEFAULT_HIGH_PERCENT  99
271 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
272 #define DEFAULT_USE_INTERLEAVE FALSE
273 #define DEFAULT_UNLINKED_CACHE_TIME 250 * GST_MSECOND
274
275 enum
276 {
277   PROP_0,
278   PROP_EXTRA_SIZE_BYTES,
279   PROP_EXTRA_SIZE_BUFFERS,
280   PROP_EXTRA_SIZE_TIME,
281   PROP_MAX_SIZE_BYTES,
282   PROP_MAX_SIZE_BUFFERS,
283   PROP_MAX_SIZE_TIME,
284   PROP_USE_BUFFERING,
285   PROP_LOW_PERCENT,
286   PROP_HIGH_PERCENT,
287   PROP_SYNC_BY_RUNNING_TIME,
288   PROP_USE_INTERLEAVE,
289   PROP_UNLINKED_CACHE_TIME,
290   PROP_LAST
291 };
292
293 /* GstMultiQueuePad */
294
295 #define DEFAULT_PAD_GROUP_ID 0
296
297 enum
298 {
299   PROP_PAD_0,
300   PROP_PAD_GROUP_ID,
301 };
302
303 #define GST_TYPE_MULTIQUEUE_PAD            (gst_multiqueue_pad_get_type())
304 #define GST_MULTIQUEUE_PAD(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePad))
305 #define GST_IS_MULTIQUEUE_PAD(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTIQUEUE_PAD))
306 #define GST_MULTIQUEUE_PAD_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
307 #define GST_IS_MULTIQUEUE_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_MULTIQUEUE_PAD))
308 #define GST_MULTIQUEUE_PAD_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_MULTIQUEUE_PAD,GstMultiQueuePadClass))
309
310 struct _GstMultiQueuePad
311 {
312   GstPad parent;
313
314   GstSingleQueue *sq;
315 };
316
317 struct _GstMultiQueuePadClass
318 {
319   GstPadClass parent_class;
320 };
321
322 GType gst_multiqueue_pad_get_type (void);
323
324 G_DEFINE_TYPE (GstMultiQueuePad, gst_multiqueue_pad, GST_TYPE_PAD);
325 static void
326 gst_multiqueue_pad_get_property (GObject * object, guint prop_id,
327     GValue * value, GParamSpec * pspec)
328 {
329   GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
330
331   switch (prop_id) {
332     case PROP_PAD_GROUP_ID:
333       if (pad->sq)
334         g_value_set_uint (value, pad->sq->groupid);
335       break;
336     default:
337       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338       break;
339   }
340 }
341
342 static void
343 gst_multiqueue_pad_set_property (GObject * object, guint prop_id,
344     const GValue * value, GParamSpec * pspec)
345 {
346   GstMultiQueuePad *pad = GST_MULTIQUEUE_PAD (object);
347
348   switch (prop_id) {
349     case PROP_PAD_GROUP_ID:
350       GST_OBJECT_LOCK (pad);
351       if (pad->sq)
352         pad->sq->groupid = g_value_get_uint (value);
353       GST_OBJECT_UNLOCK (pad);
354       break;
355     default:
356       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357       break;
358   }
359 }
360
361 static void
362 gst_multiqueue_pad_class_init (GstMultiQueuePadClass * klass)
363 {
364   GObjectClass *gobject_class = (GObjectClass *) klass;
365
366   gobject_class->set_property = gst_multiqueue_pad_set_property;
367   gobject_class->get_property = gst_multiqueue_pad_get_property;
368
369   /**
370    * GstMultiQueuePad:group-id:
371    *
372    * Group to which this pad belongs.
373    *
374    * Since: 1.10
375    */
376   g_object_class_install_property (gobject_class, PROP_PAD_GROUP_ID,
377       g_param_spec_uint ("group-id", "Group ID",
378           "Group to which this pad belongs", 0, G_MAXUINT32,
379           DEFAULT_PAD_GROUP_ID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 }
381
382 static void
383 gst_multiqueue_pad_init (GstMultiQueuePad * pad)
384 {
385
386 }
387
388
389 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START {                          \
390   g_mutex_lock (&q->qlock);                                              \
391 } G_STMT_END
392
393 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START {                        \
394   g_mutex_unlock (&q->qlock);                                            \
395 } G_STMT_END
396
397 #define SET_PERCENT(mq, perc) G_STMT_START {                             \
398   if (perc != mq->percent) {                                             \
399     mq->percent = perc;                                                  \
400     mq->percent_changed = TRUE;                                          \
401     GST_DEBUG_OBJECT (mq, "buffering %d percent", perc);                 \
402   }                                                                      \
403 } G_STMT_END
404
405 /* Convenience function */
406 static inline GstClockTimeDiff
407 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
408 {
409   GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
410
411   if (GST_CLOCK_TIME_IS_VALID (val)) {
412     gboolean sign =
413         gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
414     if (sign > 0)
415       res = val;
416     else if (sign < 0)
417       res = -val;
418   }
419   return res;
420 }
421
422 static void gst_multi_queue_finalize (GObject * object);
423 static void gst_multi_queue_set_property (GObject * object,
424     guint prop_id, const GValue * value, GParamSpec * pspec);
425 static void gst_multi_queue_get_property (GObject * object,
426     guint prop_id, GValue * value, GParamSpec * pspec);
427
428 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
429     GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
430 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
431 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
432     element, GstStateChange transition);
433
434 static void gst_multi_queue_loop (GstPad * pad);
435
436 #define _do_init \
437   GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
438 #define gst_multi_queue_parent_class parent_class
439 G_DEFINE_TYPE_WITH_CODE (GstMultiQueue, gst_multi_queue, GST_TYPE_ELEMENT,
440     _do_init);
441
442 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
443
444 static void
445 gst_multi_queue_class_init (GstMultiQueueClass * klass)
446 {
447   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
448   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
449
450   gobject_class->set_property = gst_multi_queue_set_property;
451   gobject_class->get_property = gst_multi_queue_get_property;
452
453   /* SIGNALS */
454
455   /**
456    * GstMultiQueue::underrun:
457    * @multiqueue: the multiqueue instance
458    *
459    * This signal is emitted from the streaming thread when there is
460    * no data in any of the queues inside the multiqueue instance (underrun).
461    *
462    * This indicates either starvation or EOS from the upstream data sources.
463    */
464   gst_multi_queue_signals[SIGNAL_UNDERRUN] =
465       g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
466       G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
467       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
468
469   /**
470    * GstMultiQueue::overrun:
471    * @multiqueue: the multiqueue instance
472    *
473    * Reports that one of the queues in the multiqueue is full (overrun).
474    * A queue is full if the total amount of data inside it (num-buffers, time,
475    * size) is higher than the boundary values which can be set through the
476    * GObject properties.
477    *
478    * This can be used as an indicator of pre-roll. 
479    */
480   gst_multi_queue_signals[SIGNAL_OVERRUN] =
481       g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
482       G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
483       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
484
485   /* PROPERTIES */
486
487   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
488       g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
489           "Max. amount of data in the queue (bytes, 0=disable)",
490           0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
491           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
492           G_PARAM_STATIC_STRINGS));
493   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
494       g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
495           "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
496           DEFAULT_MAX_SIZE_BUFFERS,
497           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
498           G_PARAM_STATIC_STRINGS));
499   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
500       g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
501           "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
502           DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
503           G_PARAM_STATIC_STRINGS));
504
505   g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
506       g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
507           "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
508           " (NOT IMPLEMENTED)",
509           0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
510           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
511   g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
512       g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
513           "Amount of buffers the queues can grow if one of them is empty (0=disable)"
514           " (NOT IMPLEMENTED)",
515           0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
516           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
517   g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
518       g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
519           "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
520           " (NOT IMPLEMENTED)",
521           0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
522           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
523
524   /**
525    * GstMultiQueue:use-buffering
526    * 
527    * Enable the buffering option in multiqueue so that BUFFERING messages are
528    * emitted based on low-/high-percent thresholds.
529    */
530   g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
531       g_param_spec_boolean ("use-buffering", "Use buffering",
532           "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
533           DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
534           G_PARAM_STATIC_STRINGS));
535   /**
536    * GstMultiQueue:low-percent
537    * 
538    * Low threshold percent for buffering to start.
539    */
540   g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
541       g_param_spec_int ("low-percent", "Low percent",
542           "Low threshold for buffering to start", 0, 100,
543           DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
544   /**
545    * GstMultiQueue:high-percent
546    * 
547    * High threshold percent for buffering to finish.
548    */
549   g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
550       g_param_spec_int ("high-percent", "High percent",
551           "High threshold for buffering to finish", 0, 100,
552           DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
553
554   /**
555    * GstMultiQueue:sync-by-running-time
556    * 
557    * If enabled multiqueue will synchronize deactivated or not-linked streams
558    * to the activated and linked streams by taking the running time.
559    * Otherwise multiqueue will synchronize the deactivated or not-linked
560    * streams by keeping the order in which buffers and events arrived compared
561    * to active and linked streams.
562    */
563   g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
564       g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
565           "Synchronize deactivated or not-linked streams by running time",
566           DEFAULT_SYNC_BY_RUNNING_TIME,
567           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
568
569   g_object_class_install_property (gobject_class, PROP_USE_INTERLEAVE,
570       g_param_spec_boolean ("use-interleave", "Use interleave",
571           "Adjust time limits based on input interleave",
572           DEFAULT_USE_INTERLEAVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
573
574   g_object_class_install_property (gobject_class, PROP_UNLINKED_CACHE_TIME,
575       g_param_spec_uint64 ("unlinked-cache-time", "Unlinked cache time (ns)",
576           "Extra buffering in time for unlinked streams (if 'sync-by-running-time')",
577           0, G_MAXUINT64, DEFAULT_UNLINKED_CACHE_TIME,
578           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
579           G_PARAM_STATIC_STRINGS));
580
581
582   gobject_class->finalize = gst_multi_queue_finalize;
583
584   gst_element_class_set_static_metadata (gstelement_class,
585       "MultiQueue",
586       "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
587   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
588   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
589
590   gstelement_class->request_new_pad =
591       GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
592   gstelement_class->release_pad =
593       GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
594   gstelement_class->change_state =
595       GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
596 }
597
598 static void
599 gst_multi_queue_init (GstMultiQueue * mqueue)
600 {
601   mqueue->nbqueues = 0;
602   mqueue->queues = NULL;
603
604   mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
605   mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
606   mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
607
608   mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
609   mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
610   mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
611
612   mqueue->use_buffering = DEFAULT_USE_BUFFERING;
613   mqueue->low_percent = DEFAULT_LOW_PERCENT;
614   mqueue->high_percent = DEFAULT_HIGH_PERCENT;
615
616   mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
617   mqueue->use_interleave = DEFAULT_USE_INTERLEAVE;
618   mqueue->unlinked_cache_time = DEFAULT_UNLINKED_CACHE_TIME;
619
620   mqueue->counter = 1;
621   mqueue->highid = -1;
622   mqueue->high_time = GST_CLOCK_STIME_NONE;
623
624   g_mutex_init (&mqueue->qlock);
625   g_mutex_init (&mqueue->buffering_post_lock);
626 }
627
628 static void
629 gst_multi_queue_finalize (GObject * object)
630 {
631   GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
632
633   g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
634   g_list_free (mqueue->queues);
635   mqueue->queues = NULL;
636   mqueue->queues_cookie++;
637
638   /* free/unref instance data */
639   g_mutex_clear (&mqueue->qlock);
640   g_mutex_clear (&mqueue->buffering_post_lock);
641
642   G_OBJECT_CLASS (parent_class)->finalize (object);
643 }
644
645 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START {            \
646     GList * tmp = mq->queues;                                   \
647     while (tmp) {                                               \
648       GstSingleQueue *q = (GstSingleQueue*)tmp->data;           \
649       q->max_size.format = mq->max_size.format;                 \
650       update_buffering (mq, q);                                 \
651       gst_data_queue_limits_changed (q->queue);                 \
652       tmp = g_list_next(tmp);                                   \
653     };                                                          \
654 } G_STMT_END
655
656 static void
657 gst_multi_queue_set_property (GObject * object, guint prop_id,
658     const GValue * value, GParamSpec * pspec)
659 {
660   GstMultiQueue *mq = GST_MULTI_QUEUE (object);
661
662   switch (prop_id) {
663     case PROP_MAX_SIZE_BYTES:
664       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
665       mq->max_size.bytes = g_value_get_uint (value);
666       SET_CHILD_PROPERTY (mq, bytes);
667       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
668       gst_multi_queue_post_buffering (mq);
669       break;
670     case PROP_MAX_SIZE_BUFFERS:
671     {
672       GList *tmp;
673       gint new_size = g_value_get_uint (value);
674
675       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
676
677       mq->max_size.visible = new_size;
678
679       tmp = mq->queues;
680       while (tmp) {
681         GstDataQueueSize size;
682         GstSingleQueue *q = (GstSingleQueue *) tmp->data;
683         gst_data_queue_get_level (q->queue, &size);
684
685         GST_DEBUG_OBJECT (mq, "Queue %d: Requested buffers size: %d,"
686             " current: %d, current max %d", q->id, new_size, size.visible,
687             q->max_size.visible);
688
689         /* do not reduce max size below current level if the single queue
690          * has grown because of empty queue */
691         if (new_size == 0) {
692           q->max_size.visible = new_size;
693         } else if (q->max_size.visible == 0) {
694           q->max_size.visible = MAX (new_size, size.visible);
695         } else if (new_size > size.visible) {
696           q->max_size.visible = new_size;
697         }
698         update_buffering (mq, q);
699         gst_data_queue_limits_changed (q->queue);
700         tmp = g_list_next (tmp);
701       }
702
703       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
704       gst_multi_queue_post_buffering (mq);
705
706       break;
707     }
708     case PROP_MAX_SIZE_TIME:
709       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
710       mq->max_size.time = g_value_get_uint64 (value);
711       SET_CHILD_PROPERTY (mq, time);
712       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
713       gst_multi_queue_post_buffering (mq);
714       break;
715     case PROP_EXTRA_SIZE_BYTES:
716       mq->extra_size.bytes = g_value_get_uint (value);
717       break;
718     case PROP_EXTRA_SIZE_BUFFERS:
719       mq->extra_size.visible = g_value_get_uint (value);
720       break;
721     case PROP_EXTRA_SIZE_TIME:
722       mq->extra_size.time = g_value_get_uint64 (value);
723       break;
724     case PROP_USE_BUFFERING:
725       mq->use_buffering = g_value_get_boolean (value);
726       recheck_buffering_status (mq);
727       break;
728     case PROP_LOW_PERCENT:
729       mq->low_percent = g_value_get_int (value);
730       /* Recheck buffering status - the new low-percent value might
731        * be above the current fill level. If the old low-percent one
732        * was below the current level, this means that mq->buffering is
733        * disabled and needs to be re-enabled. */
734       recheck_buffering_status (mq);
735       break;
736     case PROP_HIGH_PERCENT:
737       mq->high_percent = g_value_get_int (value);
738       recheck_buffering_status (mq);
739       break;
740     case PROP_SYNC_BY_RUNNING_TIME:
741       mq->sync_by_running_time = g_value_get_boolean (value);
742       break;
743     case PROP_USE_INTERLEAVE:
744       mq->use_interleave = g_value_get_boolean (value);
745       break;
746     case PROP_UNLINKED_CACHE_TIME:
747       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
748       mq->unlinked_cache_time = g_value_get_uint64 (value);
749       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
750       gst_multi_queue_post_buffering (mq);
751       break;
752     default:
753       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
754       break;
755   }
756 }
757
758 static void
759 gst_multi_queue_get_property (GObject * object, guint prop_id,
760     GValue * value, GParamSpec * pspec)
761 {
762   GstMultiQueue *mq = GST_MULTI_QUEUE (object);
763
764   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
765
766   switch (prop_id) {
767     case PROP_EXTRA_SIZE_BYTES:
768       g_value_set_uint (value, mq->extra_size.bytes);
769       break;
770     case PROP_EXTRA_SIZE_BUFFERS:
771       g_value_set_uint (value, mq->extra_size.visible);
772       break;
773     case PROP_EXTRA_SIZE_TIME:
774       g_value_set_uint64 (value, mq->extra_size.time);
775       break;
776     case PROP_MAX_SIZE_BYTES:
777       g_value_set_uint (value, mq->max_size.bytes);
778       break;
779     case PROP_MAX_SIZE_BUFFERS:
780       g_value_set_uint (value, mq->max_size.visible);
781       break;
782     case PROP_MAX_SIZE_TIME:
783       g_value_set_uint64 (value, mq->max_size.time);
784       break;
785     case PROP_USE_BUFFERING:
786       g_value_set_boolean (value, mq->use_buffering);
787       break;
788     case PROP_LOW_PERCENT:
789       g_value_set_int (value, mq->low_percent);
790       break;
791     case PROP_HIGH_PERCENT:
792       g_value_set_int (value, mq->high_percent);
793       break;
794     case PROP_SYNC_BY_RUNNING_TIME:
795       g_value_set_boolean (value, mq->sync_by_running_time);
796       break;
797     case PROP_USE_INTERLEAVE:
798       g_value_set_boolean (value, mq->use_interleave);
799       break;
800     case PROP_UNLINKED_CACHE_TIME:
801       g_value_set_uint64 (value, mq->unlinked_cache_time);
802       break;
803     default:
804       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
805       break;
806   }
807
808   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
809 }
810
811 static GstIterator *
812 gst_multi_queue_iterate_internal_links (GstPad * pad, GstObject * parent)
813 {
814   GstIterator *it = NULL;
815   GstPad *opad;
816   GstSingleQueue *squeue;
817   GstMultiQueue *mq = GST_MULTI_QUEUE (parent);
818   GValue val = { 0, };
819
820   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
821   squeue = gst_pad_get_element_private (pad);
822   if (!squeue)
823     goto out;
824
825   if (squeue->sinkpad == pad)
826     opad = gst_object_ref (squeue->srcpad);
827   else if (squeue->srcpad == pad)
828     opad = gst_object_ref (squeue->sinkpad);
829   else
830     goto out;
831
832   g_value_init (&val, GST_TYPE_PAD);
833   g_value_set_object (&val, opad);
834   it = gst_iterator_new_single (GST_TYPE_PAD, &val);
835   g_value_unset (&val);
836
837   gst_object_unref (opad);
838
839 out:
840   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
841
842   return it;
843 }
844
845
846 /*
847  * GstElement methods
848  */
849
850 static GstPad *
851 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
852     const gchar * name, const GstCaps * caps)
853 {
854   GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
855   GstSingleQueue *squeue;
856   GstPad *new_pad;
857   guint temp_id = -1;
858
859   if (name) {
860     sscanf (name + 4, "_%u", &temp_id);
861     GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
862   }
863
864   /* Create a new single queue, add the sink and source pad and return the sink pad */
865   squeue = gst_single_queue_new (mqueue, temp_id);
866
867   new_pad = squeue ? squeue->sinkpad : NULL;
868
869   GST_DEBUG_OBJECT (mqueue, "Returning pad %" GST_PTR_FORMAT, new_pad);
870
871   return new_pad;
872 }
873
874 static void
875 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
876 {
877   GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
878   GstSingleQueue *sq = NULL;
879   GList *tmp;
880
881   GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
882
883   GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
884   /* Find which single queue it belongs to, knowing that it should be a sinkpad */
885   for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
886     sq = (GstSingleQueue *) tmp->data;
887
888     if (sq->sinkpad == pad)
889       break;
890   }
891
892   if (!tmp) {
893     GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
894     GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
895     return;
896   }
897
898   /* FIXME: The removal of the singlequeue should probably not happen until it
899    * finishes draining */
900
901   /* remove it from the list */
902   mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
903   mqueue->queues_cookie++;
904
905   /* FIXME : recompute next-non-linked */
906   GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
907
908   /* delete SingleQueue */
909   gst_data_queue_set_flushing (sq->queue, TRUE);
910
911   gst_pad_set_active (sq->srcpad, FALSE);
912   gst_pad_set_active (sq->sinkpad, FALSE);
913   gst_pad_set_element_private (sq->srcpad, NULL);
914   gst_pad_set_element_private (sq->sinkpad, NULL);
915   gst_element_remove_pad (element, sq->srcpad);
916   gst_element_remove_pad (element, sq->sinkpad);
917   gst_single_queue_free (sq);
918 }
919
920 static GstStateChangeReturn
921 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
922 {
923   GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
924   GstSingleQueue *sq = NULL;
925   GstStateChangeReturn result;
926
927   switch (transition) {
928     case GST_STATE_CHANGE_READY_TO_PAUSED:{
929       GList *tmp;
930
931       /* Set all pads to non-flushing */
932       GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
933       for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
934         sq = (GstSingleQueue *) tmp->data;
935         sq->flushing = FALSE;
936       }
937
938       /* the visible limit might not have been set on single queues that have grown because of other queueus were empty */
939       SET_CHILD_PROPERTY (mqueue, visible);
940
941       GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
942       gst_multi_queue_post_buffering (mqueue);
943
944       break;
945     }
946     case GST_STATE_CHANGE_PAUSED_TO_READY:{
947       GList *tmp;
948
949       /* Un-wait all waiting pads */
950       GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
951       for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
952         sq = (GstSingleQueue *) tmp->data;
953         sq->flushing = TRUE;
954         g_cond_signal (&sq->turn);
955
956         sq->last_query = FALSE;
957         g_cond_signal (&sq->query_handled);
958       }
959       GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
960       break;
961     }
962     default:
963       break;
964   }
965
966   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
967
968   switch (transition) {
969     default:
970       break;
971   }
972
973   return result;
974 }
975
976 static gboolean
977 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush,
978     gboolean full)
979 {
980   gboolean result;
981
982   GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
983       sq->id);
984
985   if (flush) {
986     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
987     sq->srcresult = GST_FLOW_FLUSHING;
988     gst_data_queue_set_flushing (sq->queue, TRUE);
989
990     sq->flushing = TRUE;
991
992     /* wake up non-linked task */
993     GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
994         sq->id);
995     g_cond_signal (&sq->turn);
996     sq->last_query = FALSE;
997     g_cond_signal (&sq->query_handled);
998     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
999
1000     GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
1001     result = gst_pad_pause_task (sq->srcpad);
1002     sq->sink_tainted = sq->src_tainted = TRUE;
1003   } else {
1004     gst_single_queue_flush_queue (sq, full);
1005
1006     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1007     gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1008     gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1009     sq->has_src_segment = FALSE;
1010     /* All pads start off not-linked for a smooth kick-off */
1011     sq->srcresult = GST_FLOW_OK;
1012     sq->pushed = FALSE;
1013     sq->cur_time = 0;
1014     sq->max_size.visible = mq->max_size.visible;
1015     sq->is_eos = FALSE;
1016     sq->nextid = 0;
1017     sq->oldid = 0;
1018     sq->last_oldid = G_MAXUINT32;
1019     sq->next_time = GST_CLOCK_STIME_NONE;
1020     sq->last_time = GST_CLOCK_STIME_NONE;
1021     sq->cached_sinktime = GST_CLOCK_STIME_NONE;
1022     gst_data_queue_set_flushing (sq->queue, FALSE);
1023
1024     /* We will become active again on the next buffer/gap */
1025     sq->active = FALSE;
1026
1027     /* Reset high time to be recomputed next */
1028     mq->high_time = GST_CLOCK_STIME_NONE;
1029
1030     sq->flushing = FALSE;
1031     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1032
1033     GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
1034     result =
1035         gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
1036         sq->srcpad, NULL);
1037   }
1038   return result;
1039 }
1040
1041 /* WITH LOCK TAKEN */
1042 static gint
1043 get_percentage (GstSingleQueue * sq)
1044 {
1045   GstDataQueueSize size;
1046   gint percent, tmp;
1047
1048   gst_data_queue_get_level (sq->queue, &size);
1049
1050   GST_DEBUG_OBJECT (sq->mqueue,
1051       "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1052       G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
1053       size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1054
1055   /* get bytes and time percentages and take the max */
1056   if (sq->is_eos || sq->srcresult == GST_FLOW_NOT_LINKED || sq->is_sparse) {
1057     percent = 100;
1058   } else {
1059     percent = 0;
1060     if (sq->max_size.time > 0) {
1061       tmp = (sq->cur_time * 100) / sq->max_size.time;
1062       percent = MAX (percent, tmp);
1063     }
1064     if (sq->max_size.bytes > 0) {
1065       tmp = (size.bytes * 100) / sq->max_size.bytes;
1066       percent = MAX (percent, tmp);
1067     }
1068   }
1069
1070   return percent;
1071 }
1072
1073 /* WITH LOCK TAKEN */
1074 static void
1075 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
1076 {
1077   gint percent;
1078
1079   /* nothing to dowhen we are not in buffering mode */
1080   if (!mq->use_buffering)
1081     return;
1082
1083   percent = get_percentage (sq);
1084
1085   if (mq->buffering) {
1086     if (percent >= mq->high_percent) {
1087       mq->buffering = FALSE;
1088     }
1089     /* make sure it increases */
1090     percent = MAX (mq->percent, percent);
1091
1092     SET_PERCENT (mq, percent);
1093   } else {
1094     GList *iter;
1095     gboolean is_buffering = TRUE;
1096
1097     for (iter = mq->queues; iter; iter = g_list_next (iter)) {
1098       GstSingleQueue *oq = (GstSingleQueue *) iter->data;
1099
1100       if (get_percentage (oq) >= mq->high_percent) {
1101         is_buffering = FALSE;
1102
1103         break;
1104       }
1105     }
1106
1107     if (is_buffering && percent < mq->low_percent) {
1108       mq->buffering = TRUE;
1109       SET_PERCENT (mq, percent);
1110     }
1111   }
1112 }
1113
1114 static void
1115 gst_multi_queue_post_buffering (GstMultiQueue * mq)
1116 {
1117   GstMessage *msg = NULL;
1118
1119   g_mutex_lock (&mq->buffering_post_lock);
1120   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1121   if (mq->percent_changed) {
1122     gint percent = mq->percent;
1123
1124     mq->percent_changed = FALSE;
1125
1126     percent = percent * 100 / mq->high_percent;
1127     /* clip */
1128     if (percent > 100)
1129       percent = 100;
1130
1131     GST_DEBUG_OBJECT (mq, "Going to post buffering: %d%%", percent);
1132     msg = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
1133   }
1134   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1135
1136   if (msg != NULL)
1137     gst_element_post_message (GST_ELEMENT_CAST (mq), msg);
1138
1139   g_mutex_unlock (&mq->buffering_post_lock);
1140 }
1141
1142 static void
1143 recheck_buffering_status (GstMultiQueue * mq)
1144 {
1145   if (!mq->use_buffering && mq->buffering) {
1146     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1147     mq->buffering = FALSE;
1148     GST_DEBUG_OBJECT (mq,
1149         "Buffering property disabled, but queue was still buffering; setting percentage to 100%%");
1150     SET_PERCENT (mq, 100);
1151     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1152   }
1153
1154   if (mq->use_buffering) {
1155     GList *tmp;
1156     gint old_perc;
1157
1158     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1159
1160     /* force fill level percentage to be recalculated */
1161     old_perc = mq->percent;
1162     mq->percent = 0;
1163
1164     tmp = mq->queues;
1165     while (tmp) {
1166       GstSingleQueue *q = (GstSingleQueue *) tmp->data;
1167       update_buffering (mq, q);
1168       gst_data_queue_limits_changed (q->queue);
1169       tmp = g_list_next (tmp);
1170     }
1171
1172     GST_DEBUG_OBJECT (mq, "Recalculated fill level: old: %d%% new: %d%%",
1173         old_perc, mq->percent);
1174
1175     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1176   }
1177
1178   gst_multi_queue_post_buffering (mq);
1179 }
1180
1181 static void
1182 calculate_interleave (GstMultiQueue * mq)
1183 {
1184   GstClockTimeDiff low, high;
1185   GstClockTime interleave;
1186   GList *tmp;
1187
1188   low = high = GST_CLOCK_STIME_NONE;
1189   interleave = mq->interleave;
1190   /* Go over all single queues and calculate lowest/highest value */
1191   for (tmp = mq->queues; tmp; tmp = tmp->next) {
1192     GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1193     /* Ignore sparse streams for interleave calculation */
1194     if (sq->is_sparse)
1195       continue;
1196     /* If a stream is not active yet (hasn't received any buffers), set
1197      * a maximum interleave to allow it to receive more data */
1198     if (!sq->active) {
1199       GST_LOG_OBJECT (mq,
1200           "queue %d is not active yet, forcing interleave to 5s", sq->id);
1201       mq->interleave = 5 * GST_SECOND;
1202       /* Update max-size time */
1203       mq->max_size.time = mq->interleave;
1204       SET_CHILD_PROPERTY (mq, time);
1205       goto beach;
1206     }
1207     if (GST_CLOCK_STIME_IS_VALID (sq->cached_sinktime)) {
1208       if (low == GST_CLOCK_STIME_NONE || sq->cached_sinktime < low)
1209         low = sq->cached_sinktime;
1210       if (high == GST_CLOCK_STIME_NONE || sq->cached_sinktime > high)
1211         high = sq->cached_sinktime;
1212     }
1213     GST_LOG_OBJECT (mq,
1214         "queue %d , sinktime:%" GST_STIME_FORMAT " low:%" GST_STIME_FORMAT
1215         " high:%" GST_STIME_FORMAT, sq->id,
1216         GST_STIME_ARGS (sq->cached_sinktime), GST_STIME_ARGS (low),
1217         GST_STIME_ARGS (high));
1218   }
1219
1220   if (GST_CLOCK_STIME_IS_VALID (low) && GST_CLOCK_STIME_IS_VALID (high)) {
1221     interleave = high - low;
1222     /* Padding of interleave and minimum value */
1223     /* FIXME : Make the minimum time interleave a property */
1224     interleave = (150 * interleave / 100) + 250 * GST_MSECOND;
1225
1226     /* Update the stored interleave if:
1227      * * No data has arrived yet (high == low)
1228      * * Or it went higher
1229      * * Or it went lower and we've gone past the previous interleave needed */
1230     if (high == low || interleave > mq->interleave ||
1231         ((mq->last_interleave_update + (2 * MIN (GST_SECOND,
1232                         mq->interleave)) < low)
1233             && interleave < (mq->interleave * 3 / 4))) {
1234       /* Update the interleave */
1235       mq->interleave = interleave;
1236       mq->last_interleave_update = high;
1237       /* Update max-size time */
1238       mq->max_size.time = mq->interleave;
1239       SET_CHILD_PROPERTY (mq, time);
1240     }
1241   }
1242
1243 beach:
1244   GST_DEBUG_OBJECT (mq,
1245       "low:%" GST_STIME_FORMAT " high:%" GST_STIME_FORMAT " interleave:%"
1246       GST_TIME_FORMAT " mq->interleave:%" GST_TIME_FORMAT
1247       " last_interleave_update:%" GST_STIME_FORMAT, GST_STIME_ARGS (low),
1248       GST_STIME_ARGS (high), GST_TIME_ARGS (interleave),
1249       GST_TIME_ARGS (mq->interleave),
1250       GST_STIME_ARGS (mq->last_interleave_update));
1251 }
1252
1253
1254 /* calculate the diff between running time on the sink and src of the queue.
1255  * This is the total amount of time in the queue. 
1256  * WITH LOCK TAKEN */
1257 static void
1258 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
1259 {
1260   GstClockTimeDiff sink_time, src_time;
1261
1262   if (sq->sink_tainted) {
1263     sink_time = sq->sinktime = my_segment_to_running_time (&sq->sink_segment,
1264         sq->sink_segment.position);
1265
1266     GST_DEBUG_OBJECT (mq,
1267         "queue %d sink_segment.position:%" GST_TIME_FORMAT ", sink_time:%"
1268         GST_STIME_FORMAT, sq->id, GST_TIME_ARGS (sq->sink_segment.position),
1269         GST_STIME_ARGS (sink_time));
1270
1271     if (G_UNLIKELY (sq->last_time == GST_CLOCK_STIME_NONE)) {
1272       /* If the single queue still doesn't have a last_time set, this means
1273        * that nothing has been pushed out yet.
1274        * In order for the high_time computation to be as efficient as possible,
1275        * we set the last_time */
1276       sq->last_time = sink_time;
1277     }
1278     if (G_UNLIKELY (sink_time != GST_CLOCK_STIME_NONE)) {
1279       /* if we have a time, we become untainted and use the time */
1280       sq->sink_tainted = FALSE;
1281       if (mq->use_interleave) {
1282         sq->cached_sinktime = sink_time;
1283         calculate_interleave (mq);
1284       }
1285     }
1286   } else
1287     sink_time = sq->sinktime;
1288
1289   if (sq->src_tainted) {
1290     GstSegment *segment;
1291     gint64 position;
1292
1293     if (sq->has_src_segment) {
1294       segment = &sq->src_segment;
1295       position = sq->src_segment.position;
1296     } else {
1297       /*
1298        * If the src pad had no segment yet, use the sink segment
1299        * to avoid signalling overrun if the received sink segment has a
1300        * a position > max-size-time while the src pad time would be the default=0
1301        *
1302        * This can happen when switching pads on chained/adaptive streams and the
1303        * new chain has a segment with a much larger position
1304        */
1305       segment = &sq->sink_segment;
1306       position = sq->sink_segment.position;
1307     }
1308
1309     src_time = sq->srctime = my_segment_to_running_time (segment, position);
1310     /* if we have a time, we become untainted and use the time */
1311     if (G_UNLIKELY (src_time != GST_CLOCK_STIME_NONE)) {
1312       sq->src_tainted = FALSE;
1313     }
1314   } else
1315     src_time = sq->srctime;
1316
1317   GST_DEBUG_OBJECT (mq,
1318       "queue %d, sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT, sq->id,
1319       GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
1320
1321   /* This allows for streams with out of order timestamping - sometimes the
1322    * emerging timestamp is later than the arriving one(s) */
1323   if (G_LIKELY (GST_CLOCK_STIME_IS_VALID (sink_time) &&
1324           GST_CLOCK_STIME_IS_VALID (src_time) && sink_time > src_time))
1325     sq->cur_time = sink_time - src_time;
1326   else
1327     sq->cur_time = 0;
1328
1329   /* updating the time level can change the buffering state */
1330   update_buffering (mq, sq);
1331
1332   return;
1333 }
1334
1335 /* take a SEGMENT event and apply the values to segment, updating the time
1336  * level of queue. */
1337 static void
1338 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1339     GstSegment * segment)
1340 {
1341   gst_event_copy_segment (event, segment);
1342
1343   /* now configure the values, we use these to track timestamps on the
1344    * sinkpad. */
1345   if (segment->format != GST_FORMAT_TIME) {
1346     /* non-time format, pretent the current time segment is closed with a
1347      * 0 start and unknown stop time. */
1348     segment->format = GST_FORMAT_TIME;
1349     segment->start = 0;
1350     segment->stop = -1;
1351     segment->time = 0;
1352   }
1353   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1354
1355   /* Make sure we have a valid initial segment position (and not garbage
1356    * from upstream) */
1357   if (segment->rate > 0.0)
1358     segment->position = segment->start;
1359   else
1360     segment->position = segment->stop;
1361   if (segment == &sq->sink_segment)
1362     sq->sink_tainted = TRUE;
1363   else {
1364     sq->has_src_segment = TRUE;
1365     sq->src_tainted = TRUE;
1366   }
1367
1368   GST_DEBUG_OBJECT (mq,
1369       "queue %d, configured SEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
1370
1371   /* segment can update the time level of the queue */
1372   update_time_level (mq, sq);
1373
1374   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1375   gst_multi_queue_post_buffering (mq);
1376 }
1377
1378 /* take a buffer and update segment, updating the time level of the queue. */
1379 static void
1380 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
1381     GstClockTime duration, GstSegment * segment)
1382 {
1383   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1384
1385   /* if no timestamp is set, assume it's continuous with the previous 
1386    * time */
1387   if (timestamp == GST_CLOCK_TIME_NONE)
1388     timestamp = segment->position;
1389
1390   /* add duration */
1391   if (duration != GST_CLOCK_TIME_NONE)
1392     timestamp += duration;
1393
1394   GST_DEBUG_OBJECT (mq, "queue %d, %s position updated to %" GST_TIME_FORMAT,
1395       sq->id, segment == &sq->sink_segment ? "sink" : "src",
1396       GST_TIME_ARGS (timestamp));
1397
1398   segment->position = timestamp;
1399
1400   if (segment == &sq->sink_segment)
1401     sq->sink_tainted = TRUE;
1402   else
1403     sq->src_tainted = TRUE;
1404
1405   /* calc diff with other end */
1406   update_time_level (mq, sq);
1407   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1408   gst_multi_queue_post_buffering (mq);
1409 }
1410
1411 static void
1412 apply_gap (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
1413     GstSegment * segment)
1414 {
1415   GstClockTime timestamp;
1416   GstClockTime duration;
1417
1418   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1419
1420   gst_event_parse_gap (event, &timestamp, &duration);
1421
1422   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1423
1424     if (GST_CLOCK_TIME_IS_VALID (duration)) {
1425       timestamp += duration;
1426     }
1427
1428     segment->position = timestamp;
1429
1430     if (segment == &sq->sink_segment)
1431       sq->sink_tainted = TRUE;
1432     else
1433       sq->src_tainted = TRUE;
1434
1435     /* calc diff with other end */
1436     update_time_level (mq, sq);
1437   }
1438
1439   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1440   gst_multi_queue_post_buffering (mq);
1441 }
1442
1443 static GstClockTimeDiff
1444 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
1445 {
1446   GstClockTimeDiff time = GST_CLOCK_STIME_NONE;
1447
1448   if (GST_IS_BUFFER (object)) {
1449     GstBuffer *buf = GST_BUFFER_CAST (object);
1450     GstClockTime btime = GST_BUFFER_DTS_OR_PTS (buf);
1451
1452     if (GST_CLOCK_TIME_IS_VALID (btime)) {
1453       if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1454         btime += GST_BUFFER_DURATION (buf);
1455       if (btime > segment->stop)
1456         btime = segment->stop;
1457       time = my_segment_to_running_time (segment, btime);
1458     }
1459   } else if (GST_IS_BUFFER_LIST (object)) {
1460     GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1461     gint i, n;
1462     GstBuffer *buf;
1463
1464     n = gst_buffer_list_length (list);
1465     for (i = 0; i < n; i++) {
1466       GstClockTime btime;
1467       buf = gst_buffer_list_get (list, i);
1468       btime = GST_BUFFER_DTS_OR_PTS (buf);
1469       if (GST_CLOCK_TIME_IS_VALID (btime)) {
1470         if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1471           btime += GST_BUFFER_DURATION (buf);
1472         if (btime > segment->stop)
1473           btime = segment->stop;
1474         time = my_segment_to_running_time (segment, btime);
1475         if (!end)
1476           goto done;
1477       } else if (!end) {
1478         goto done;
1479       }
1480     }
1481   } else if (GST_IS_EVENT (object)) {
1482     GstEvent *event = GST_EVENT_CAST (object);
1483
1484     /* For newsegment events return the running time of the start position */
1485     if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1486       const GstSegment *new_segment;
1487
1488       gst_event_parse_segment (event, &new_segment);
1489       if (new_segment->format == GST_FORMAT_TIME) {
1490         time =
1491             my_segment_to_running_time ((GstSegment *) new_segment,
1492             new_segment->start);
1493       }
1494     }
1495   }
1496
1497 done:
1498   return time;
1499 }
1500
1501 static GstFlowReturn
1502 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1503     GstMiniObject * object, gboolean * allow_drop)
1504 {
1505   GstFlowReturn result = sq->srcresult;
1506
1507   if (GST_IS_BUFFER (object)) {
1508     GstBuffer *buffer;
1509     GstClockTime timestamp, duration;
1510
1511     buffer = GST_BUFFER_CAST (object);
1512     timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1513     duration = GST_BUFFER_DURATION (buffer);
1514
1515     apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1516
1517     /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1518     gst_data_queue_limits_changed (sq->queue);
1519
1520     if (G_UNLIKELY (*allow_drop)) {
1521       GST_DEBUG_OBJECT (mq,
1522           "SingleQueue %d : Dropping EOS buffer %p with ts %" GST_TIME_FORMAT,
1523           sq->id, buffer, GST_TIME_ARGS (timestamp));
1524       gst_buffer_unref (buffer);
1525     } else {
1526       GST_DEBUG_OBJECT (mq,
1527           "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1528           sq->id, buffer, GST_TIME_ARGS (timestamp));
1529       result = gst_pad_push (sq->srcpad, buffer);
1530     }
1531   } else if (GST_IS_EVENT (object)) {
1532     GstEvent *event;
1533
1534     event = GST_EVENT_CAST (object);
1535
1536     switch (GST_EVENT_TYPE (event)) {
1537       case GST_EVENT_EOS:
1538         result = GST_FLOW_EOS;
1539         if (G_UNLIKELY (*allow_drop))
1540           *allow_drop = FALSE;
1541         break;
1542       case GST_EVENT_SEGMENT:
1543         apply_segment (mq, sq, event, &sq->src_segment);
1544         /* Applying the segment may have made the queue non-full again, unblock it if needed */
1545         gst_data_queue_limits_changed (sq->queue);
1546         if (G_UNLIKELY (*allow_drop)) {
1547           result = GST_FLOW_OK;
1548           *allow_drop = FALSE;
1549         }
1550         break;
1551       case GST_EVENT_GAP:
1552         apply_gap (mq, sq, event, &sq->src_segment);
1553         /* Applying the gap may have made the queue non-full again, unblock it if needed */
1554         gst_data_queue_limits_changed (sq->queue);
1555         break;
1556       default:
1557         break;
1558     }
1559
1560     if (G_UNLIKELY (*allow_drop)) {
1561       GST_DEBUG_OBJECT (mq,
1562           "SingleQueue %d : Dropping EOS event %p of type %s",
1563           sq->id, event, GST_EVENT_TYPE_NAME (event));
1564       gst_event_unref (event);
1565     } else {
1566       GST_DEBUG_OBJECT (mq,
1567           "SingleQueue %d : Pushing event %p of type %s",
1568           sq->id, event, GST_EVENT_TYPE_NAME (event));
1569
1570       gst_pad_push_event (sq->srcpad, event);
1571     }
1572   } else if (GST_IS_QUERY (object)) {
1573     GstQuery *query;
1574     gboolean res;
1575
1576     query = GST_QUERY_CAST (object);
1577
1578     if (G_UNLIKELY (*allow_drop)) {
1579       GST_DEBUG_OBJECT (mq,
1580           "SingleQueue %d : Dropping EOS query %p", sq->id, query);
1581       gst_query_unref (query);
1582       res = FALSE;
1583     } else {
1584       res = gst_pad_peer_query (sq->srcpad, query);
1585     }
1586
1587     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1588     sq->last_query = res;
1589     sq->last_handled_query = query;
1590     g_cond_signal (&sq->query_handled);
1591     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1592   } else {
1593     g_warning ("Unexpected object in singlequeue %u (refcounting problem?)",
1594         sq->id);
1595   }
1596   return result;
1597
1598   /* ERRORS */
1599 }
1600
1601 static GstMiniObject *
1602 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1603 {
1604   GstMiniObject *res;
1605
1606   res = item->object;
1607   item->object = NULL;
1608
1609   return res;
1610 }
1611
1612 static void
1613 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1614 {
1615   if (!item->is_query && item->object)
1616     gst_mini_object_unref (item->object);
1617   g_slice_free (GstMultiQueueItem, item);
1618 }
1619
1620 /* takes ownership of passed mini object! */
1621 static GstMultiQueueItem *
1622 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1623 {
1624   GstMultiQueueItem *item;
1625
1626   item = g_slice_new (GstMultiQueueItem);
1627   item->object = object;
1628   item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1629   item->posid = curid;
1630   item->is_query = GST_IS_QUERY (object);
1631
1632   item->size = gst_buffer_get_size (GST_BUFFER_CAST (object));
1633   item->duration = GST_BUFFER_DURATION (object);
1634   if (item->duration == GST_CLOCK_TIME_NONE)
1635     item->duration = 0;
1636   item->visible = TRUE;
1637   return item;
1638 }
1639
1640 static GstMultiQueueItem *
1641 gst_multi_queue_mo_item_new (GstMiniObject * object, guint32 curid)
1642 {
1643   GstMultiQueueItem *item;
1644
1645   item = g_slice_new (GstMultiQueueItem);
1646   item->object = object;
1647   item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1648   item->posid = curid;
1649   item->is_query = GST_IS_QUERY (object);
1650
1651   item->size = 0;
1652   item->duration = 0;
1653   item->visible = FALSE;
1654   return item;
1655 }
1656
1657 /* Each main loop attempts to push buffers until the return value
1658  * is not-linked. not-linked pads are not allowed to push data beyond
1659  * any linked pads, so they don't 'rush ahead of the pack'.
1660  */
1661 static void
1662 gst_multi_queue_loop (GstPad * pad)
1663 {
1664   GstSingleQueue *sq;
1665   GstMultiQueueItem *item;
1666   GstDataQueueItem *sitem;
1667   GstMultiQueue *mq;
1668   GstMiniObject *object = NULL;
1669   guint32 newid;
1670   GstFlowReturn result;
1671   GstClockTimeDiff next_time;
1672   gboolean is_buffer;
1673   gboolean do_update_buffering = FALSE;
1674   gboolean dropping = FALSE;
1675
1676   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1677   mq = sq->mqueue;
1678
1679 next:
1680   GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1681
1682   if (sq->flushing)
1683     goto out_flushing;
1684
1685   /* Get something from the queue, blocking until that happens, or we get
1686    * flushed */
1687   if (!(gst_data_queue_pop (sq->queue, &sitem)))
1688     goto out_flushing;
1689
1690   item = (GstMultiQueueItem *) sitem;
1691   newid = item->posid;
1692
1693   /* steal the object and destroy the item */
1694   object = gst_multi_queue_item_steal_object (item);
1695   gst_multi_queue_item_destroy (item);
1696
1697   is_buffer = GST_IS_BUFFER (object);
1698
1699   /* Get running time of the item. Events will have GST_CLOCK_STIME_NONE */
1700   next_time = get_running_time (&sq->src_segment, object, FALSE);
1701
1702   GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1703       sq->id, newid, sq->last_oldid);
1704
1705   /* If we're not-linked, we do some extra work because we might need to
1706    * wait before pushing. If we're linked but there's a gap in the IDs,
1707    * or it's the first loop, or we just passed the previous highid,
1708    * we might need to wake some sleeping pad up, so there's extra work
1709    * there too */
1710   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1711   if (sq->srcresult == GST_FLOW_NOT_LINKED
1712       || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1713       || sq->last_oldid > mq->highid) {
1714     GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1715         gst_flow_get_name (sq->srcresult));
1716
1717     /* Check again if we're flushing after the lock is taken,
1718      * the flush flag might have been changed in the meantime */
1719     if (sq->flushing) {
1720       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1721       goto out_flushing;
1722     }
1723
1724     /* Update the nextid so other threads know when to wake us up */
1725     sq->nextid = newid;
1726     /* Take into account the extra cache time since we're unlinked */
1727     if (GST_CLOCK_STIME_IS_VALID (next_time))
1728       next_time += mq->unlinked_cache_time;
1729     sq->next_time = next_time;
1730
1731     /* Update the oldid (the last ID we output) for highid tracking */
1732     if (sq->last_oldid != G_MAXUINT32)
1733       sq->oldid = sq->last_oldid;
1734
1735     if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1736       GstClockTimeDiff high_time;
1737       /* Go to sleep until it's time to push this buffer */
1738
1739       /* Recompute the highid */
1740       compute_high_id (mq);
1741       /* Recompute the high time */
1742       high_time = compute_high_time (mq, sq->groupid);
1743
1744       GST_DEBUG_OBJECT (mq,
1745           "groupid %d high_time %" GST_STIME_FORMAT " next_time %"
1746           GST_STIME_FORMAT, sq->groupid, GST_STIME_ARGS (high_time),
1747           GST_STIME_ARGS (next_time));
1748
1749       while (((mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (next_time)
1750                   && (high_time == GST_CLOCK_STIME_NONE
1751                       || next_time > high_time))
1752               || (!mq->sync_by_running_time && newid > mq->highid))
1753           && sq->srcresult == GST_FLOW_NOT_LINKED) {
1754
1755         GST_DEBUG_OBJECT (mq,
1756             "queue %d sleeping for not-linked wakeup with "
1757             "newid %u, highid %u, next_time %" GST_STIME_FORMAT
1758             ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1759             GST_STIME_ARGS (next_time), GST_STIME_ARGS (high_time));
1760
1761         /* Wake up all non-linked pads before we sleep */
1762         wake_up_next_non_linked (mq);
1763
1764         mq->numwaiting++;
1765         g_cond_wait (&sq->turn, &mq->qlock);
1766         mq->numwaiting--;
1767
1768         if (sq->flushing) {
1769           GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1770           goto out_flushing;
1771         }
1772
1773         /* Recompute the high time and ID */
1774         high_time = compute_high_time (mq, sq->groupid);
1775         compute_high_id (mq);
1776
1777         GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1778             "wakeup with newid %u, highid %u, next_time %" GST_STIME_FORMAT
1779             ", high_time %" GST_STIME_FORMAT, sq->id, newid, mq->highid,
1780             GST_STIME_ARGS (next_time), GST_STIME_ARGS (high_time));
1781       }
1782
1783       /* Re-compute the high_id in case someone else pushed */
1784       compute_high_id (mq);
1785       compute_high_time (mq, sq->groupid);
1786     } else {
1787       compute_high_id (mq);
1788       compute_high_time (mq, sq->groupid);
1789       /* Wake up all non-linked pads */
1790       wake_up_next_non_linked (mq);
1791     }
1792     /* We're done waiting, we can clear the nextid and nexttime */
1793     sq->nextid = 0;
1794     sq->next_time = GST_CLOCK_STIME_NONE;
1795   }
1796   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1797
1798   if (sq->flushing)
1799     goto out_flushing;
1800
1801   GST_LOG_OBJECT (mq, "sq:%d BEFORE PUSHING sq->srcresult: %s", sq->id,
1802       gst_flow_get_name (sq->srcresult));
1803
1804   /* Update time stats */
1805   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1806   next_time = get_running_time (&sq->src_segment, object, TRUE);
1807   if (GST_CLOCK_STIME_IS_VALID (next_time)) {
1808     if (sq->last_time == GST_CLOCK_STIME_NONE || sq->last_time < next_time)
1809       sq->last_time = next_time;
1810     if (mq->high_time == GST_CLOCK_STIME_NONE || mq->high_time <= next_time) {
1811       /* Wake up all non-linked pads now that we advanced the high time */
1812       mq->high_time = next_time;
1813       wake_up_next_non_linked (mq);
1814     }
1815   }
1816   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1817
1818   /* Try to push out the new object */
1819   result = gst_single_queue_push_one (mq, sq, object, &dropping);
1820   object = NULL;
1821
1822   /* Check if we pushed something already and if this is
1823    * now a switch from an active to a non-active stream.
1824    *
1825    * If it is, we reset all the waiting streams, let them
1826    * push another buffer to see if they're now active again.
1827    * This allows faster switching between streams and prevents
1828    * deadlocks if downstream does any waiting too.
1829    */
1830   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1831   if (sq->pushed && sq->srcresult == GST_FLOW_OK
1832       && result == GST_FLOW_NOT_LINKED) {
1833     GList *tmp;
1834
1835     GST_LOG_OBJECT (mq, "SingleQueue %d : Changed from active to non-active",
1836         sq->id);
1837
1838     compute_high_id (mq);
1839     do_update_buffering = TRUE;
1840
1841     /* maybe no-one is waiting */
1842     if (mq->numwaiting > 0) {
1843       /* Else figure out which singlequeue(s) need waking up */
1844       for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1845         GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1846
1847         if (sq2->srcresult == GST_FLOW_NOT_LINKED) {
1848           GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq2->id);
1849           sq2->pushed = FALSE;
1850           sq2->srcresult = GST_FLOW_OK;
1851           g_cond_signal (&sq2->turn);
1852         }
1853       }
1854     }
1855   }
1856
1857   if (is_buffer)
1858     sq->pushed = TRUE;
1859
1860   /* now hold on a bit;
1861    * can not simply throw this result to upstream, because
1862    * that might already be onto another segment, so we have to make
1863    * sure we are relaying the correct info wrt proper segment */
1864   if (result == GST_FLOW_EOS && !dropping &&
1865       sq->srcresult != GST_FLOW_NOT_LINKED) {
1866     GST_DEBUG_OBJECT (mq, "starting EOS drop on sq %d", sq->id);
1867     dropping = TRUE;
1868     /* pretend we have not seen EOS yet for upstream's sake */
1869     result = sq->srcresult;
1870   } else if (dropping && gst_data_queue_is_empty (sq->queue)) {
1871     /* queue empty, so stop dropping
1872      * we can commit the result we have now,
1873      * which is either OK after a segment, or EOS */
1874     GST_DEBUG_OBJECT (mq, "committed EOS drop on sq %d", sq->id);
1875     dropping = FALSE;
1876     result = GST_FLOW_EOS;
1877   }
1878   sq->srcresult = result;
1879   sq->last_oldid = newid;
1880
1881   if (do_update_buffering)
1882     update_buffering (mq, sq);
1883
1884   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1885   gst_multi_queue_post_buffering (mq);
1886
1887   GST_LOG_OBJECT (mq, "sq:%d AFTER PUSHING sq->srcresult: %s (is_eos:%d)",
1888       sq->id, gst_flow_get_name (sq->srcresult), GST_PAD_IS_EOS (sq->srcpad));
1889
1890   /* Need to make sure wake up any sleeping pads when we exit */
1891   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1892   if (mq->numwaiting > 0 && (GST_PAD_IS_EOS (sq->srcpad)
1893           || sq->srcresult == GST_FLOW_EOS)) {
1894     compute_high_time (mq, sq->groupid);
1895     compute_high_id (mq);
1896     wake_up_next_non_linked (mq);
1897   }
1898   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1899
1900   if (dropping)
1901     goto next;
1902
1903   if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1904       && result != GST_FLOW_EOS)
1905     goto out_flushing;
1906
1907   return;
1908
1909 out_flushing:
1910   {
1911     if (object)
1912       gst_mini_object_unref (object);
1913
1914     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1915     sq->last_query = FALSE;
1916     g_cond_signal (&sq->query_handled);
1917
1918     /* Post an error message if we got EOS while downstream
1919      * has returned an error flow return. After EOS there
1920      * will be no further buffer which could propagate the
1921      * error upstream */
1922     if (sq->is_eos && sq->srcresult < GST_FLOW_EOS) {
1923       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1924       GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
1925     } else {
1926       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1927     }
1928
1929     /* upstream needs to see fatal result ASAP to shut things down,
1930      * but might be stuck in one of our other full queues;
1931      * so empty this one and trigger dynamic queue growth. At
1932      * this point the srcresult is not OK, NOT_LINKED
1933      * or EOS, i.e. a real failure */
1934     gst_single_queue_flush_queue (sq, FALSE);
1935     single_queue_underrun_cb (sq->queue, sq);
1936     gst_data_queue_set_flushing (sq->queue, TRUE);
1937     gst_pad_pause_task (sq->srcpad);
1938     GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1939         "SingleQueue[%d] task paused, reason:%s",
1940         sq->id, gst_flow_get_name (sq->srcresult));
1941     return;
1942   }
1943 }
1944
1945 /**
1946  * gst_multi_queue_chain:
1947  *
1948  * This is similar to GstQueue's chain function, except:
1949  * _ we don't have leak behaviours,
1950  * _ we push with a unique id (curid)
1951  */
1952 static GstFlowReturn
1953 gst_multi_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1954 {
1955   GstSingleQueue *sq;
1956   GstMultiQueue *mq;
1957   GstMultiQueueItem *item;
1958   guint32 curid;
1959   GstClockTime timestamp, duration;
1960
1961   sq = gst_pad_get_element_private (pad);
1962   mq = sq->mqueue;
1963
1964   /* if eos, we are always full, so avoid hanging incoming indefinitely */
1965   if (sq->is_eos)
1966     goto was_eos;
1967
1968   sq->active = TRUE;
1969
1970   /* Get a unique incrementing id */
1971   curid = g_atomic_int_add ((gint *) & mq->counter, 1);
1972
1973   timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1974   duration = GST_BUFFER_DURATION (buffer);
1975
1976   GST_LOG_OBJECT (mq,
1977       "SingleQueue %d : about to enqueue buffer %p with id %d (pts:%"
1978       GST_TIME_FORMAT " dts:%" GST_TIME_FORMAT " dur:%" GST_TIME_FORMAT ")",
1979       sq->id, buffer, curid, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1980       GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), GST_TIME_ARGS (duration));
1981
1982   item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1983
1984   /* Update interleave before pushing data into queue */
1985   if (mq->use_interleave) {
1986     GstClockTime val = timestamp;
1987     GstClockTimeDiff dval;
1988
1989     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1990     if (val == GST_CLOCK_TIME_NONE)
1991       val = sq->sink_segment.position;
1992     if (duration != GST_CLOCK_TIME_NONE)
1993       val += duration;
1994
1995     dval = my_segment_to_running_time (&sq->sink_segment, val);
1996     if (GST_CLOCK_STIME_IS_VALID (dval)) {
1997       sq->cached_sinktime = dval;
1998       GST_DEBUG_OBJECT (mq,
1999           "Queue %d cached sink time now %" G_GINT64_FORMAT " %"
2000           GST_STIME_FORMAT, sq->id, sq->cached_sinktime,
2001           GST_STIME_ARGS (sq->cached_sinktime));
2002       calculate_interleave (mq);
2003     }
2004     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2005   }
2006
2007   if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
2008     goto flushing;
2009
2010   /* update time level, we must do this after pushing the data in the queue so
2011    * that we never end up filling the queue first. */
2012   apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
2013
2014 done:
2015   return sq->srcresult;
2016
2017   /* ERRORS */
2018 flushing:
2019   {
2020     GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2021         sq->id, gst_flow_get_name (sq->srcresult));
2022     gst_multi_queue_item_destroy (item);
2023     goto done;
2024   }
2025 was_eos:
2026   {
2027     GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return EOS");
2028     gst_buffer_unref (buffer);
2029     return GST_FLOW_EOS;
2030   }
2031 }
2032
2033 static gboolean
2034 gst_multi_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
2035     GstPadMode mode, gboolean active)
2036 {
2037   gboolean res;
2038   GstSingleQueue *sq;
2039   GstMultiQueue *mq;
2040
2041   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2042   mq = (GstMultiQueue *) gst_pad_get_parent (pad);
2043
2044   /* mq is NULL if the pad is activated/deactivated before being
2045    * added to the multiqueue */
2046   if (mq)
2047     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2048
2049   switch (mode) {
2050     case GST_PAD_MODE_PUSH:
2051       if (active) {
2052         /* All pads start off linked until they push one buffer */
2053         sq->srcresult = GST_FLOW_OK;
2054         sq->pushed = FALSE;
2055         gst_data_queue_set_flushing (sq->queue, FALSE);
2056       } else {
2057         sq->srcresult = GST_FLOW_FLUSHING;
2058         sq->last_query = FALSE;
2059         g_cond_signal (&sq->query_handled);
2060         gst_data_queue_set_flushing (sq->queue, TRUE);
2061
2062         /* Wait until streaming thread has finished */
2063         if (mq)
2064           GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2065         GST_PAD_STREAM_LOCK (pad);
2066         if (mq)
2067           GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2068         gst_data_queue_flush (sq->queue);
2069         if (mq)
2070           GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2071         GST_PAD_STREAM_UNLOCK (pad);
2072         if (mq)
2073           GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2074       }
2075       res = TRUE;
2076       break;
2077     default:
2078       res = FALSE;
2079       break;
2080   }
2081
2082   if (mq) {
2083     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2084     gst_object_unref (mq);
2085   }
2086
2087   return res;
2088 }
2089
2090 static GstFlowReturn
2091 gst_multi_queue_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2092 {
2093   GstSingleQueue *sq;
2094   GstMultiQueue *mq;
2095   guint32 curid;
2096   GstMultiQueueItem *item;
2097   gboolean res = TRUE;
2098   GstFlowReturn flowret = GST_FLOW_OK;
2099   GstEventType type;
2100   GstEvent *sref = NULL;
2101
2102   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2103   mq = (GstMultiQueue *) parent;
2104
2105   type = GST_EVENT_TYPE (event);
2106
2107   switch (type) {
2108     case GST_EVENT_STREAM_START:
2109     {
2110       if (mq->sync_by_running_time) {
2111         GstStreamFlags stream_flags;
2112         gst_event_parse_stream_flags (event, &stream_flags);
2113         if ((stream_flags & GST_STREAM_FLAG_SPARSE)) {
2114           GST_INFO_OBJECT (mq, "SingleQueue %d is a sparse stream", sq->id);
2115           sq->is_sparse = TRUE;
2116         }
2117       }
2118
2119       sq->thread = g_thread_self ();
2120
2121       /* Remove EOS flag */
2122       sq->is_eos = FALSE;
2123       break;
2124     }
2125     case GST_EVENT_FLUSH_START:
2126       GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
2127           sq->id);
2128
2129       res = gst_pad_push_event (sq->srcpad, event);
2130
2131       gst_single_queue_flush (mq, sq, TRUE, FALSE);
2132       goto done;
2133
2134     case GST_EVENT_FLUSH_STOP:
2135       GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
2136           sq->id);
2137
2138       res = gst_pad_push_event (sq->srcpad, event);
2139
2140       gst_single_queue_flush (mq, sq, FALSE, FALSE);
2141       goto done;
2142
2143     case GST_EVENT_SEGMENT:
2144       sref = gst_event_ref (event);
2145       break;
2146     case GST_EVENT_GAP:
2147       /* take ref because the queue will take ownership and we need the event
2148        * afterwards to update the segment */
2149       sref = gst_event_ref (event);
2150       if (mq->use_interleave) {
2151         GstClockTime val, dur;
2152         GstClockTime stime;
2153         gst_event_parse_gap (event, &val, &dur);
2154         if (GST_CLOCK_TIME_IS_VALID (val)) {
2155           GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2156           if (GST_CLOCK_TIME_IS_VALID (dur))
2157             val += dur;
2158           stime = my_segment_to_running_time (&sq->sink_segment, val);
2159           if (GST_CLOCK_STIME_IS_VALID (stime)) {
2160             sq->cached_sinktime = stime;
2161             calculate_interleave (mq);
2162           }
2163           GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2164         }
2165       }
2166       break;
2167
2168     default:
2169       if (!(GST_EVENT_IS_SERIALIZED (event))) {
2170         res = gst_pad_push_event (sq->srcpad, event);
2171         goto done;
2172       }
2173       break;
2174   }
2175
2176   /* if eos, we are always full, so avoid hanging incoming indefinitely */
2177   if (sq->is_eos)
2178     goto was_eos;
2179
2180   /* Get an unique incrementing id. */
2181   curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2182
2183   item = gst_multi_queue_mo_item_new ((GstMiniObject *) event, curid);
2184
2185   GST_DEBUG_OBJECT (mq,
2186       "SingleQueue %d : Enqueuing event %p of type %s with id %d",
2187       sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
2188
2189   if (!gst_data_queue_push (sq->queue, (GstDataQueueItem *) item))
2190     goto flushing;
2191
2192   /* mark EOS when we received one, we must do that after putting the
2193    * buffer in the queue because EOS marks the buffer as filled. */
2194   switch (type) {
2195     case GST_EVENT_EOS:
2196       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2197       sq->is_eos = TRUE;
2198
2199       /* Post an error message if we got EOS while downstream
2200        * has returned an error flow return. After EOS there
2201        * will be no further buffer which could propagate the
2202        * error upstream */
2203       if (sq->srcresult < GST_FLOW_EOS) {
2204         GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2205         GST_ELEMENT_FLOW_ERROR (mq, sq->srcresult);
2206       } else {
2207         GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2208       }
2209
2210       /* EOS affects the buffering state */
2211       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2212       update_buffering (mq, sq);
2213       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2214       single_queue_overrun_cb (sq->queue, sq);
2215       gst_multi_queue_post_buffering (mq);
2216       break;
2217     case GST_EVENT_SEGMENT:
2218       apply_segment (mq, sq, sref, &sq->sink_segment);
2219       gst_event_unref (sref);
2220       /* a new segment allows us to accept more buffers if we got EOS
2221        * from downstream */
2222       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2223       if (sq->srcresult == GST_FLOW_EOS)
2224         sq->srcresult = GST_FLOW_OK;
2225       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2226       break;
2227     case GST_EVENT_GAP:
2228       sq->active = TRUE;
2229       apply_gap (mq, sq, sref, &sq->sink_segment);
2230       gst_event_unref (sref);
2231     default:
2232       break;
2233   }
2234
2235 done:
2236   if (res == FALSE)
2237     flowret = GST_FLOW_ERROR;
2238   GST_DEBUG_OBJECT (mq, "SingleQueue %d : returning %s", sq->id,
2239       gst_flow_get_name (flowret));
2240   return flowret;
2241
2242 flushing:
2243   {
2244     GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
2245         sq->id, gst_flow_get_name (sq->srcresult));
2246     if (sref)
2247       gst_event_unref (sref);
2248     gst_multi_queue_item_destroy (item);
2249     return sq->srcresult;
2250   }
2251 was_eos:
2252   {
2253     GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return GST_FLOW_EOS");
2254     gst_event_unref (event);
2255     return GST_FLOW_EOS;
2256   }
2257 }
2258
2259 static gboolean
2260 gst_multi_queue_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
2261 {
2262   gboolean res;
2263   GstSingleQueue *sq;
2264   GstMultiQueue *mq;
2265
2266   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2267   mq = (GstMultiQueue *) parent;
2268
2269   switch (GST_QUERY_TYPE (query)) {
2270     default:
2271       if (GST_QUERY_IS_SERIALIZED (query)) {
2272         guint32 curid;
2273         GstMultiQueueItem *item;
2274
2275         GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2276         if (sq->srcresult != GST_FLOW_OK)
2277           goto out_flushing;
2278
2279         /* serialized events go in the queue. We need to be certain that we
2280          * don't cause deadlocks waiting for the query return value. We check if
2281          * the queue is empty (nothing is blocking downstream and the query can
2282          * be pushed for sure) or we are not buffering. If we are buffering,
2283          * the pipeline waits to unblock downstream until our queue fills up
2284          * completely, which can not happen if we block on the query..
2285          * Therefore we only potentially block when we are not buffering. */
2286         if (!mq->use_buffering || gst_data_queue_is_empty (sq->queue)) {
2287           /* Get an unique incrementing id. */
2288           curid = g_atomic_int_add ((gint *) & mq->counter, 1);
2289
2290           item = gst_multi_queue_mo_item_new ((GstMiniObject *) query, curid);
2291
2292           GST_DEBUG_OBJECT (mq,
2293               "SingleQueue %d : Enqueuing query %p of type %s with id %d",
2294               sq->id, query, GST_QUERY_TYPE_NAME (query), curid);
2295           GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2296           res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item);
2297           GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2298           if (!res || sq->flushing)
2299             goto out_flushing;
2300           /* it might be that the query has been taken out of the queue
2301            * while we were unlocked. So, we need to check if the last
2302            * handled query is the same one than the one we just
2303            * pushed. If it is, we don't need to wait for the condition
2304            * variable, otherwise we wait for the condition variable to
2305            * be signaled. */
2306           if (sq->last_handled_query != query)
2307             g_cond_wait (&sq->query_handled, &mq->qlock);
2308           res = sq->last_query;
2309           sq->last_handled_query = NULL;
2310         } else {
2311           GST_DEBUG_OBJECT (mq, "refusing query, we are buffering and the "
2312               "queue is not empty");
2313           res = FALSE;
2314         }
2315         GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2316       } else {
2317         /* default handling */
2318         res = gst_pad_query_default (pad, parent, query);
2319       }
2320       break;
2321   }
2322   return res;
2323
2324 out_flushing:
2325   {
2326     GST_DEBUG_OBJECT (mq, "Flushing");
2327     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2328     return FALSE;
2329   }
2330 }
2331
2332 static gboolean
2333 gst_multi_queue_src_activate_mode (GstPad * pad, GstObject * parent,
2334     GstPadMode mode, gboolean active)
2335 {
2336   GstMultiQueue *mq;
2337   GstSingleQueue *sq;
2338   gboolean result;
2339
2340   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
2341   mq = sq->mqueue;
2342
2343   GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
2344
2345   switch (mode) {
2346     case GST_PAD_MODE_PUSH:
2347       if (active) {
2348         result = gst_single_queue_flush (mq, sq, FALSE, TRUE);
2349       } else {
2350         result = gst_single_queue_flush (mq, sq, TRUE, TRUE);
2351         /* make sure streaming finishes */
2352         result |= gst_pad_stop_task (pad);
2353       }
2354       break;
2355     default:
2356       result = FALSE;
2357       break;
2358   }
2359   return result;
2360 }
2361
2362 static gboolean
2363 gst_multi_queue_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2364 {
2365   GstSingleQueue *sq = gst_pad_get_element_private (pad);
2366   GstMultiQueue *mq = sq->mqueue;
2367   gboolean ret;
2368
2369   switch (GST_EVENT_TYPE (event)) {
2370     case GST_EVENT_RECONFIGURE:
2371       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2372       if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2373         sq->srcresult = GST_FLOW_OK;
2374         g_cond_signal (&sq->turn);
2375       }
2376       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2377
2378       ret = gst_pad_push_event (sq->sinkpad, event);
2379       break;
2380     default:
2381       ret = gst_pad_push_event (sq->sinkpad, event);
2382       break;
2383   }
2384
2385   return ret;
2386 }
2387
2388 static gboolean
2389 gst_multi_queue_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2390 {
2391   gboolean res;
2392
2393   /* FIXME, Handle position offset depending on queue size */
2394   switch (GST_QUERY_TYPE (query)) {
2395     default:
2396       /* default handling */
2397       res = gst_pad_query_default (pad, parent, query);
2398       break;
2399   }
2400   return res;
2401 }
2402
2403 /*
2404  * Next-non-linked functions
2405  */
2406
2407 /* WITH LOCK TAKEN */
2408 static void
2409 wake_up_next_non_linked (GstMultiQueue * mq)
2410 {
2411   GList *tmp;
2412
2413   /* maybe no-one is waiting */
2414   if (mq->numwaiting < 1)
2415     return;
2416
2417   if (mq->sync_by_running_time && GST_CLOCK_STIME_IS_VALID (mq->high_time)) {
2418     /* Else figure out which singlequeue(s) need waking up */
2419     for (tmp = mq->queues; tmp; tmp = tmp->next) {
2420       GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2421       if (sq->srcresult == GST_FLOW_NOT_LINKED
2422           && GST_CLOCK_STIME_IS_VALID (sq->next_time)
2423           && sq->next_time <= mq->high_time) {
2424         GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2425         g_cond_signal (&sq->turn);
2426       }
2427     }
2428   } else {
2429     /* Else figure out which singlequeue(s) need waking up */
2430     for (tmp = mq->queues; tmp; tmp = tmp->next) {
2431       GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2432       if (sq->srcresult == GST_FLOW_NOT_LINKED &&
2433           sq->nextid != 0 && sq->nextid <= mq->highid) {
2434         GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
2435         g_cond_signal (&sq->turn);
2436       }
2437     }
2438   }
2439 }
2440
2441 /* WITH LOCK TAKEN */
2442 static void
2443 compute_high_id (GstMultiQueue * mq)
2444 {
2445   /* The high-id is either the highest id among the linked pads, or if all
2446    * pads are not-linked, it's the lowest not-linked pad */
2447   GList *tmp;
2448   guint32 lowest = G_MAXUINT32;
2449   guint32 highid = G_MAXUINT32;
2450
2451   for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2452     GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2453
2454     GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
2455         sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
2456
2457     if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2458       /* No need to consider queues which are not waiting */
2459       if (sq->nextid == 0) {
2460         GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2461         continue;
2462       }
2463
2464       if (sq->nextid < lowest)
2465         lowest = sq->nextid;
2466     } else if (!GST_PAD_IS_EOS (sq->srcpad) && sq->srcresult != GST_FLOW_EOS) {
2467       /* If we don't have a global highid, or the global highid is lower than
2468        * this single queue's last outputted id, store the queue's one, 
2469        * unless the singlequeue output is at EOS */
2470       if ((highid == G_MAXUINT32) || (sq->oldid > highid))
2471         highid = sq->oldid;
2472     }
2473   }
2474
2475   if (highid == G_MAXUINT32 || lowest < highid)
2476     mq->highid = lowest;
2477   else
2478     mq->highid = highid;
2479
2480   GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
2481       lowest);
2482 }
2483
2484 /* WITH LOCK TAKEN */
2485 static GstClockTimeDiff
2486 compute_high_time (GstMultiQueue * mq, guint groupid)
2487 {
2488   /* The high-time is either the highest last time among the linked
2489    * pads, or if all pads are not-linked, it's the lowest nex time of
2490    * not-linked pad */
2491   GList *tmp;
2492   GstClockTimeDiff highest = GST_CLOCK_STIME_NONE;
2493   GstClockTimeDiff lowest = GST_CLOCK_STIME_NONE;
2494   GstClockTimeDiff group_high = GST_CLOCK_STIME_NONE;
2495   GstClockTimeDiff group_low = GST_CLOCK_STIME_NONE;
2496   /* Number of streams which belong to groupid */
2497   guint group_count = 0;
2498
2499   if (!mq->sync_by_running_time)
2500     return GST_CLOCK_STIME_NONE;
2501
2502   for (tmp = mq->queues; tmp; tmp = tmp->next) {
2503     GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
2504
2505     GST_LOG_OBJECT (mq,
2506         "inspecting sq:%d (group:%d) , next_time:%" GST_STIME_FORMAT
2507         ", last_time:%" GST_STIME_FORMAT ", srcresult:%s", sq->id, sq->groupid,
2508         GST_STIME_ARGS (sq->next_time), GST_STIME_ARGS (sq->last_time),
2509         gst_flow_get_name (sq->srcresult));
2510
2511     if (sq->groupid == groupid)
2512       group_count++;
2513
2514     if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2515       /* No need to consider queues which are not waiting */
2516       if (!GST_CLOCK_STIME_IS_VALID (sq->next_time)) {
2517         GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
2518         continue;
2519       }
2520
2521       if (lowest == GST_CLOCK_STIME_NONE || sq->next_time < lowest)
2522         lowest = sq->next_time;
2523       if (sq->groupid == groupid && (group_low == GST_CLOCK_STIME_NONE
2524               || sq->next_time < group_low))
2525         group_low = sq->next_time;
2526     } else if (!GST_PAD_IS_EOS (sq->srcpad) && sq->srcresult != GST_FLOW_EOS) {
2527       /* If we don't have a global high time, or the global high time
2528        * is lower than this single queue's last outputted time, store
2529        * the queue's one, unless the singlequeue output is at EOS. */
2530       if (highest == GST_CLOCK_STIME_NONE
2531           || (sq->last_time != GST_CLOCK_STIME_NONE && sq->last_time > highest))
2532         highest = sq->last_time;
2533       if (sq->groupid == groupid && (group_high == GST_CLOCK_STIME_NONE
2534               || (sq->last_time != GST_CLOCK_STIME_NONE
2535                   && sq->last_time > group_high)))
2536         group_high = sq->last_time;
2537     }
2538     GST_LOG_OBJECT (mq,
2539         "highest now %" GST_STIME_FORMAT " lowest %" GST_STIME_FORMAT,
2540         GST_STIME_ARGS (highest), GST_STIME_ARGS (lowest));
2541     if (sq->groupid == groupid)
2542       GST_LOG_OBJECT (mq,
2543           "grouphigh %" GST_STIME_FORMAT " grouplow %" GST_STIME_FORMAT,
2544           GST_STIME_ARGS (group_high), GST_STIME_ARGS (group_low));
2545   }
2546
2547   if (highest == GST_CLOCK_STIME_NONE)
2548     mq->high_time = lowest;
2549   else
2550     mq->high_time = highest;
2551
2552   GST_LOG_OBJECT (mq, "group count %d for groupid %u", group_count, groupid);
2553   GST_LOG_OBJECT (mq,
2554       "High time is now : %" GST_STIME_FORMAT ", lowest non-linked %"
2555       GST_STIME_FORMAT, GST_STIME_ARGS (mq->high_time),
2556       GST_STIME_ARGS (lowest));
2557
2558   /* If there's only one stream of a given type, use the global high */
2559   if (group_count < 2)
2560     return mq->high_time;
2561
2562   if (group_high == GST_CLOCK_STIME_NONE)
2563     return group_low;
2564   return group_high;
2565 }
2566
2567 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
2568      ((q)->max_size.format) <= (value))
2569
2570 /*
2571  * GstSingleQueue functions
2572  */
2573 static void
2574 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2575 {
2576   GstMultiQueue *mq = sq->mqueue;
2577   GList *tmp;
2578   GstDataQueueSize size;
2579   gboolean filled = TRUE;
2580   gboolean empty_found = FALSE;
2581
2582   gst_data_queue_get_level (sq->queue, &size);
2583
2584   GST_LOG_OBJECT (mq,
2585       "Single Queue %d: EOS %d, visible %u/%u, bytes %u/%u, time %"
2586       G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, sq->id, sq->is_eos, size.visible,
2587       sq->max_size.visible, size.bytes, sq->max_size.bytes, sq->cur_time,
2588       sq->max_size.time);
2589
2590   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2591
2592   /* check if we reached the hard time/bytes limits;
2593      time limit is only taken into account for non-sparse streams */
2594   if (sq->is_eos || IS_FILLED (sq, bytes, size.bytes) ||
2595       (!sq->is_sparse && IS_FILLED (sq, time, sq->cur_time))) {
2596     goto done;
2597   }
2598
2599   /* Search for empty queues */
2600   for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2601     GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2602
2603     if (oq == sq)
2604       continue;
2605
2606     if (oq->srcresult == GST_FLOW_NOT_LINKED) {
2607       GST_LOG_OBJECT (mq, "Queue %d is not-linked", oq->id);
2608       continue;
2609     }
2610
2611     GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
2612     if (gst_data_queue_is_empty (oq->queue) && !oq->is_sparse) {
2613       GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
2614       empty_found = TRUE;
2615       break;
2616     }
2617   }
2618
2619   /* if hard limits are not reached then we allow one more buffer in the full
2620    * queue, but only if any of the other singelqueues are empty */
2621   if (empty_found) {
2622     if (IS_FILLED (sq, visible, size.visible)) {
2623       sq->max_size.visible = size.visible + 1;
2624       GST_DEBUG_OBJECT (mq,
2625           "Bumping single queue %d max visible to %d",
2626           sq->id, sq->max_size.visible);
2627       filled = FALSE;
2628     }
2629   }
2630
2631 done:
2632   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2633
2634   /* Overrun is always forwarded, since this is blocking the upstream element */
2635   if (filled) {
2636     GST_DEBUG_OBJECT (mq, "Queue %d is filled, signalling overrun", sq->id);
2637     g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
2638   }
2639 }
2640
2641 static void
2642 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
2643 {
2644   gboolean empty = TRUE;
2645   GstMultiQueue *mq = sq->mqueue;
2646   GList *tmp;
2647
2648   if (sq->srcresult == GST_FLOW_NOT_LINKED) {
2649     GST_LOG_OBJECT (mq, "Single Queue %d is empty but not-linked", sq->id);
2650     return;
2651   } else {
2652     GST_LOG_OBJECT (mq,
2653         "Single Queue %d is empty, Checking other single queues", sq->id);
2654   }
2655
2656   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
2657   for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
2658     GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
2659
2660     if (gst_data_queue_is_full (oq->queue)) {
2661       GstDataQueueSize size;
2662
2663       gst_data_queue_get_level (oq->queue, &size);
2664       if (IS_FILLED (oq, visible, size.visible)) {
2665         oq->max_size.visible = size.visible + 1;
2666         GST_DEBUG_OBJECT (mq,
2667             "queue %d is filled, bumping its max visible to %d", oq->id,
2668             oq->max_size.visible);
2669         gst_data_queue_limits_changed (oq->queue);
2670       }
2671     }
2672     if (!gst_data_queue_is_empty (oq->queue) || oq->is_sparse)
2673       empty = FALSE;
2674   }
2675   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
2676
2677   if (empty) {
2678     GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
2679     g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
2680   }
2681 }
2682
2683 static gboolean
2684 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
2685     guint64 time, GstSingleQueue * sq)
2686 {
2687   gboolean res;
2688   GstMultiQueue *mq = sq->mqueue;
2689
2690   GST_DEBUG_OBJECT (mq,
2691       "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
2692       G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
2693       sq->max_size.bytes, sq->cur_time, sq->max_size.time);
2694
2695   /* we are always filled on EOS */
2696   if (sq->is_eos)
2697     return TRUE;
2698
2699   /* we never go past the max visible items unless we are in buffering mode */
2700   if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
2701     return TRUE;
2702
2703   /* check time or bytes */
2704   res = IS_FILLED (sq, bytes, bytes);
2705   /* We only care about limits in time if we're not a sparse stream or
2706    * we're not syncing by running time */
2707   if (!sq->is_sparse || !mq->sync_by_running_time) {
2708     /* If unlinked, take into account the extra unlinked cache time */
2709     if (mq->sync_by_running_time && sq->srcresult == GST_FLOW_NOT_LINKED) {
2710       if (sq->cur_time > mq->unlinked_cache_time)
2711         res |= IS_FILLED (sq, time, sq->cur_time - mq->unlinked_cache_time);
2712       else
2713         res = FALSE;
2714     } else
2715       res |= IS_FILLED (sq, time, sq->cur_time);
2716   }
2717
2718   return res;
2719 }
2720
2721 static void
2722 gst_single_queue_flush_queue (GstSingleQueue * sq, gboolean full)
2723 {
2724   GstDataQueueItem *sitem;
2725   GstMultiQueueItem *mitem;
2726   gboolean was_flushing = FALSE;
2727
2728   while (!gst_data_queue_is_empty (sq->queue)) {
2729     GstMiniObject *data;
2730
2731     /* FIXME: If this fails here although the queue is not empty,
2732      * we're flushing... but we want to rescue all sticky
2733      * events nonetheless.
2734      */
2735     if (!gst_data_queue_pop (sq->queue, &sitem)) {
2736       was_flushing = TRUE;
2737       gst_data_queue_set_flushing (sq->queue, FALSE);
2738       continue;
2739     }
2740
2741     mitem = (GstMultiQueueItem *) sitem;
2742
2743     data = sitem->object;
2744
2745     if (!full && !mitem->is_query && GST_IS_EVENT (data)
2746         && GST_EVENT_IS_STICKY (data)
2747         && GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
2748         && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
2749       gst_pad_store_sticky_event (sq->srcpad, GST_EVENT_CAST (data));
2750     }
2751
2752     sitem->destroy (sitem);
2753   }
2754
2755   gst_data_queue_flush (sq->queue);
2756   if (was_flushing)
2757     gst_data_queue_set_flushing (sq->queue, TRUE);
2758
2759   GST_MULTI_QUEUE_MUTEX_LOCK (sq->mqueue);
2760   update_buffering (sq->mqueue, sq);
2761   GST_MULTI_QUEUE_MUTEX_UNLOCK (sq->mqueue);
2762   gst_multi_queue_post_buffering (sq->mqueue);
2763 }
2764
2765 static void
2766 gst_single_queue_free (GstSingleQueue * sq)
2767 {
2768   /* DRAIN QUEUE */
2769   gst_data_queue_flush (sq->queue);
2770   g_object_unref (sq->queue);
2771   g_cond_clear (&sq->turn);
2772   g_cond_clear (&sq->query_handled);
2773   g_free (sq);
2774 }
2775
2776 static GstSingleQueue *
2777 gst_single_queue_new (GstMultiQueue * mqueue, guint id)
2778 {
2779   GstSingleQueue *sq;
2780   GstMultiQueuePad *mqpad;
2781   GstPadTemplate *templ;
2782   gchar *name;
2783   GList *tmp;
2784   guint temp_id = (id == -1) ? 0 : id;
2785
2786   GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
2787
2788   /* Find an unused queue ID, if possible the passed one */
2789   for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
2790     GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
2791     /* This works because the IDs are sorted in ascending order */
2792     if (sq2->id == temp_id) {
2793       /* If this ID was requested by the caller return NULL,
2794        * otherwise just get us the next one */
2795       if (id == -1) {
2796         temp_id = sq2->id + 1;
2797       } else {
2798         GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2799         return NULL;
2800       }
2801     } else if (sq2->id > temp_id) {
2802       break;
2803     }
2804   }
2805
2806   sq = g_new0 (GstSingleQueue, 1);
2807   mqueue->nbqueues++;
2808   sq->id = temp_id;
2809   sq->groupid = DEFAULT_PAD_GROUP_ID;
2810
2811   mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
2812   mqueue->queues_cookie++;
2813
2814   /* copy over max_size and extra_size so we don't need to take the lock
2815    * any longer when checking if the queue is full. */
2816   sq->max_size.visible = mqueue->max_size.visible;
2817   sq->max_size.bytes = mqueue->max_size.bytes;
2818   sq->max_size.time = mqueue->max_size.time;
2819
2820   sq->extra_size.visible = mqueue->extra_size.visible;
2821   sq->extra_size.bytes = mqueue->extra_size.bytes;
2822   sq->extra_size.time = mqueue->extra_size.time;
2823
2824   GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
2825
2826   sq->mqueue = mqueue;
2827   sq->srcresult = GST_FLOW_FLUSHING;
2828   sq->pushed = FALSE;
2829   sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
2830       single_queue_check_full,
2831       (GstDataQueueFullCallback) single_queue_overrun_cb,
2832       (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
2833   sq->is_eos = FALSE;
2834   sq->is_sparse = FALSE;
2835   sq->flushing = FALSE;
2836   sq->active = FALSE;
2837   gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
2838   gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
2839
2840   sq->nextid = 0;
2841   sq->oldid = 0;
2842   sq->next_time = GST_CLOCK_STIME_NONE;
2843   sq->last_time = GST_CLOCK_STIME_NONE;
2844   g_cond_init (&sq->turn);
2845   g_cond_init (&sq->query_handled);
2846
2847   sq->sinktime = GST_CLOCK_STIME_NONE;
2848   sq->srctime = GST_CLOCK_STIME_NONE;
2849   sq->sink_tainted = TRUE;
2850   sq->src_tainted = TRUE;
2851
2852   name = g_strdup_printf ("sink_%u", sq->id);
2853   templ = gst_static_pad_template_get (&sinktemplate);
2854   sq->sinkpad = g_object_new (GST_TYPE_MULTIQUEUE_PAD, "name", name,
2855       "direction", templ->direction, "template", templ, NULL);
2856   gst_object_unref (templ);
2857   g_free (name);
2858
2859   mqpad = (GstMultiQueuePad *) sq->sinkpad;
2860   mqpad->sq = sq;
2861
2862   gst_pad_set_chain_function (sq->sinkpad,
2863       GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
2864   gst_pad_set_activatemode_function (sq->sinkpad,
2865       GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_mode));
2866   gst_pad_set_event_full_function (sq->sinkpad,
2867       GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
2868   gst_pad_set_query_function (sq->sinkpad,
2869       GST_DEBUG_FUNCPTR (gst_multi_queue_sink_query));
2870   gst_pad_set_iterate_internal_links_function (sq->sinkpad,
2871       GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2872   GST_OBJECT_FLAG_SET (sq->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
2873
2874   name = g_strdup_printf ("src_%u", sq->id);
2875   sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
2876   g_free (name);
2877
2878   gst_pad_set_activatemode_function (sq->srcpad,
2879       GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_mode));
2880   gst_pad_set_event_function (sq->srcpad,
2881       GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
2882   gst_pad_set_query_function (sq->srcpad,
2883       GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
2884   gst_pad_set_iterate_internal_links_function (sq->srcpad,
2885       GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2886   GST_OBJECT_FLAG_SET (sq->srcpad, GST_PAD_FLAG_PROXY_CAPS);
2887
2888   gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2889   gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2890
2891   GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2892
2893   /* only activate the pads when we are not in the NULL state
2894    * and add the pad under the state_lock to prevend state changes
2895    * between activating and adding */
2896   g_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2897   if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2898     gst_pad_set_active (sq->srcpad, TRUE);
2899     gst_pad_set_active (sq->sinkpad, TRUE);
2900   }
2901   gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2902   gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2903   g_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2904
2905   GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",
2906       sq->id);
2907
2908   return sq;
2909 }