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