plugins: use new gst_buffer_list_calculate_size()
[platform/upstream/gstreamer.git] / plugins / elements / gstqueue.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2003 Colin Walters <cwalters@gnome.org>
5  *                    2005 Wim Taymans <wim@fluendo.com>
6  *
7  * gstqueue.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-queue
27  * @title: queue
28  *
29  * Data is queued until one of the limits specified by the
30  * #GstQueue:max-size-buffers, #GstQueue:max-size-bytes and/or
31  * #GstQueue:max-size-time properties has been reached. Any attempt to push
32  * more buffers into the queue will block the pushing thread until more space
33  * becomes available.
34  *
35  * The queue will create a new thread on the source pad to decouple the
36  * processing on sink and source pad.
37  *
38  * You can query how many buffers are queued by reading the
39  * #GstQueue:current-level-buffers property. You can track changes
40  * by connecting to the notify::current-level-buffers signal (which
41  * like all signals will be emitted from the streaming thread). The same
42  * applies to the #GstQueue:current-level-time and
43  * #GstQueue:current-level-bytes properties.
44  *
45  * The default queue size limits are 200 buffers, 10MB of data, or
46  * one second worth of data, whichever is reached first.
47  *
48  * As said earlier, the queue blocks by default when one of the specified
49  * maximums (bytes, time, buffers) has been reached. You can set the
50  * #GstQueue:leaky property to specify that instead of blocking it should
51  * leak (drop) new or old buffers.
52  *
53  * The #GstQueue::underrun signal is emitted when the queue has less data than
54  * the specified minimum thresholds require (by default: when the queue is
55  * empty). The #GstQueue::overrun signal is emitted when the queue is filled
56  * up. Both signals are emitted from the context of the streaming thread.
57  */
58
59 #include "gst/gst_private.h"
60
61 #include <gst/gst.h>
62 #include "gstqueue.h"
63
64 #include "../../gst/gst-i18n-lib.h"
65 #include "../../gst/glib-compat-private.h"
66
67 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
68     GST_PAD_SINK,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS_ANY);
71
72 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
73     GST_PAD_SRC,
74     GST_PAD_ALWAYS,
75     GST_STATIC_CAPS_ANY);
76
77 GST_DEBUG_CATEGORY_STATIC (queue_debug);
78 #define GST_CAT_DEFAULT (queue_debug)
79 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
80
81 #define STATUS(queue, pad, msg) \
82   GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
83                       "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
84                       "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
85                       "-%" G_GUINT64_FORMAT " ns, %u items", \
86                       GST_DEBUG_PAD_NAME (pad), \
87                       queue->cur_level.buffers, \
88                       queue->min_threshold.buffers, \
89                       queue->max_size.buffers, \
90                       queue->cur_level.bytes, \
91                       queue->min_threshold.bytes, \
92                       queue->max_size.bytes, \
93                       queue->cur_level.time, \
94                       queue->min_threshold.time, \
95                       queue->max_size.time, \
96                       gst_queue_array_get_length (queue->queue))
97
98 /* Queue signals and args */
99 enum
100 {
101   SIGNAL_UNDERRUN,
102   SIGNAL_RUNNING,
103   SIGNAL_OVERRUN,
104   SIGNAL_PUSHING,
105   LAST_SIGNAL
106 };
107
108 enum
109 {
110   PROP_0,
111   /* FIXME: don't we have another way of doing this
112    * "Gstreamer format" (frame/byte/time) queries? */
113   PROP_CUR_LEVEL_BUFFERS,
114   PROP_CUR_LEVEL_BYTES,
115   PROP_CUR_LEVEL_TIME,
116   PROP_MAX_SIZE_BUFFERS,
117   PROP_MAX_SIZE_BYTES,
118   PROP_MAX_SIZE_TIME,
119   PROP_MIN_THRESHOLD_BUFFERS,
120   PROP_MIN_THRESHOLD_BYTES,
121   PROP_MIN_THRESHOLD_TIME,
122   PROP_LEAKY,
123   PROP_SILENT,
124   PROP_FLUSH_ON_EOS
125 };
126
127 /* default property values */
128 #define DEFAULT_MAX_SIZE_BUFFERS  200   /* 200 buffers */
129 #define DEFAULT_MAX_SIZE_BYTES    (10 * 1024 * 1024)    /* 10 MB       */
130 #define DEFAULT_MAX_SIZE_TIME     GST_SECOND    /* 1 second    */
131
132 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START {                          \
133   g_mutex_lock (&q->qlock);                                              \
134 } G_STMT_END
135
136 #define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START {              \
137   GST_QUEUE_MUTEX_LOCK (q);                                             \
138   if (q->srcresult != GST_FLOW_OK)                                      \
139     goto label;                                                         \
140 } G_STMT_END
141
142 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START {                        \
143   g_mutex_unlock (&q->qlock);                                            \
144 } G_STMT_END
145
146 #define GST_QUEUE_WAIT_DEL_CHECK(q, label) G_STMT_START {               \
147   STATUS (q, q->sinkpad, "wait for DEL");                               \
148   q->waiting_del = TRUE;                                                \
149   g_cond_wait (&q->item_del, &q->qlock);                                  \
150   q->waiting_del = FALSE;                                               \
151   if (q->srcresult != GST_FLOW_OK) {                                    \
152     STATUS (q, q->srcpad, "received DEL wakeup");                       \
153     goto label;                                                         \
154   }                                                                     \
155   STATUS (q, q->sinkpad, "received DEL");                               \
156 } G_STMT_END
157
158 #define GST_QUEUE_WAIT_ADD_CHECK(q, label) G_STMT_START {               \
159   STATUS (q, q->srcpad, "wait for ADD");                                \
160   q->waiting_add = TRUE;                                                \
161   g_cond_wait (&q->item_add, &q->qlock);                                  \
162   q->waiting_add = FALSE;                                               \
163   if (q->srcresult != GST_FLOW_OK) {                                    \
164     STATUS (q, q->srcpad, "received ADD wakeup");                       \
165     goto label;                                                         \
166   }                                                                     \
167   STATUS (q, q->srcpad, "received ADD");                                \
168 } G_STMT_END
169
170 #define GST_QUEUE_SIGNAL_DEL(q) G_STMT_START {                          \
171   if (q->waiting_del) {                                                 \
172     STATUS (q, q->srcpad, "signal DEL");                                \
173     g_cond_signal (&q->item_del);                                        \
174   }                                                                     \
175 } G_STMT_END
176
177 #define GST_QUEUE_SIGNAL_ADD(q) G_STMT_START {                          \
178   if (q->waiting_add) {                                                 \
179     STATUS (q, q->sinkpad, "signal ADD");                               \
180     g_cond_signal (&q->item_add);                                        \
181   }                                                                     \
182 } G_STMT_END
183
184 #define _do_init \
185     GST_DEBUG_CATEGORY_INIT (queue_debug, "queue", 0, "queue element"); \
186     GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0, \
187         "dataflow inside the queue element");
188 #define gst_queue_parent_class parent_class
189 G_DEFINE_TYPE_WITH_CODE (GstQueue, gst_queue, GST_TYPE_ELEMENT, _do_init);
190
191 static void gst_queue_finalize (GObject * object);
192 static void gst_queue_set_property (GObject * object,
193     guint prop_id, const GValue * value, GParamSpec * pspec);
194 static void gst_queue_get_property (GObject * object,
195     guint prop_id, GValue * value, GParamSpec * pspec);
196
197 static GstFlowReturn gst_queue_chain (GstPad * pad, GstObject * parent,
198     GstBuffer * buffer);
199 static GstFlowReturn gst_queue_chain_list (GstPad * pad, GstObject * parent,
200     GstBufferList * buffer_list);
201 static GstFlowReturn gst_queue_push_one (GstQueue * queue);
202 static void gst_queue_loop (GstPad * pad);
203
204 static GstFlowReturn gst_queue_handle_sink_event (GstPad * pad,
205     GstObject * parent, GstEvent * event);
206 static gboolean gst_queue_handle_sink_query (GstPad * pad, GstObject * parent,
207     GstQuery * query);
208
209 static gboolean gst_queue_handle_src_event (GstPad * pad, GstObject * parent,
210     GstEvent * event);
211 static gboolean gst_queue_handle_src_query (GstPad * pad, GstObject * parent,
212     GstQuery * query);
213
214 static void gst_queue_locked_flush (GstQueue * queue, gboolean full);
215
216 static gboolean gst_queue_src_activate_mode (GstPad * pad, GstObject * parent,
217     GstPadMode mode, gboolean active);
218 static gboolean gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
219     GstPadMode mode, gboolean active);
220
221 static gboolean gst_queue_is_empty (GstQueue * queue);
222 static gboolean gst_queue_is_filled (GstQueue * queue);
223
224
225 typedef struct
226 {
227   GstMiniObject *item;
228   gsize size;
229   gboolean is_query;
230 } GstQueueItem;
231
232 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
233
234 static GType
235 queue_leaky_get_type (void)
236 {
237   static GType queue_leaky_type = 0;
238   static const GEnumValue queue_leaky[] = {
239     {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
240     {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
241     {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
242         "downstream"},
243     {0, NULL, NULL},
244   };
245
246   if (!queue_leaky_type) {
247     queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
248   }
249   return queue_leaky_type;
250 }
251
252 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
253
254 static void
255 gst_queue_class_init (GstQueueClass * klass)
256 {
257   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
258   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
259
260   gobject_class->set_property = gst_queue_set_property;
261   gobject_class->get_property = gst_queue_get_property;
262
263   /* signals */
264   /**
265    * GstQueue::underrun:
266    * @queue: the queue instance
267    *
268    * Reports that the buffer became empty (underrun).
269    * A buffer is empty if the total amount of data inside it (num-buffers, time,
270    * size) is lower than the boundary values which can be set through the
271    * GObject properties.
272    */
273   gst_queue_signals[SIGNAL_UNDERRUN] =
274       g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
275       G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
276       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
277   /**
278    * GstQueue::running:
279    * @queue: the queue instance
280    *
281    * Reports that enough (min-threshold) data is in the queue. Use this signal
282    * together with the underrun signal to pause the pipeline on underrun and
283    * wait for the queue to fill-up before resume playback.
284    */
285   gst_queue_signals[SIGNAL_RUNNING] =
286       g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
287       G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
288       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
289   /**
290    * GstQueue::overrun:
291    * @queue: the queue instance
292    *
293    * Reports that the buffer became full (overrun).
294    * A buffer is full if the total amount of data inside it (num-buffers, time,
295    * size) is higher than the boundary values which can be set through the
296    * GObject properties.
297    */
298   gst_queue_signals[SIGNAL_OVERRUN] =
299       g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
300       G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
301       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
302   /**
303    * GstQueue::pushing:
304    * @queue: the queue instance
305    *
306    * Reports when the queue has enough data to start pushing data again on the
307    * source pad.
308    */
309   gst_queue_signals[SIGNAL_PUSHING] =
310       g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
311       G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
312       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
313
314   /* properties */
315   g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
316       g_param_spec_uint ("current-level-bytes", "Current level (kB)",
317           "Current amount of data in the queue (bytes)",
318           0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
319   g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
320       g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
321           "Current number of buffers in the queue",
322           0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
323   g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
324       g_param_spec_uint64 ("current-level-time", "Current level (ns)",
325           "Current amount of data in the queue (in ns)",
326           0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
327
328   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
329       g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
330           "Max. amount of data in the queue (bytes, 0=disable)",
331           0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
332           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
333           G_PARAM_STATIC_STRINGS));
334   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
335       g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
336           "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
337           DEFAULT_MAX_SIZE_BUFFERS,
338           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
339           G_PARAM_STATIC_STRINGS));
340   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
341       g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
342           "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
343           DEFAULT_MAX_SIZE_TIME,
344           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
345           G_PARAM_STATIC_STRINGS));
346
347   g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
348       g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
349           "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
350           0, G_MAXUINT, 0,
351           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
352           G_PARAM_STATIC_STRINGS));
353   g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
354       g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
355           "Min. number of buffers in the queue to allow reading (0=disable)", 0,
356           G_MAXUINT, 0,
357           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
358           G_PARAM_STATIC_STRINGS));
359   g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
360       g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
361           "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
362           0, G_MAXUINT64, 0,
363           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
364           G_PARAM_STATIC_STRINGS));
365
366   g_object_class_install_property (gobject_class, PROP_LEAKY,
367       g_param_spec_enum ("leaky", "Leaky",
368           "Where the queue leaks, if at all",
369           GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
370           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
371           G_PARAM_STATIC_STRINGS));
372
373   /**
374    * GstQueue:silent
375    *
376    * Don't emit queue signals. Makes queues more lightweight if no signals are
377    * needed.
378    */
379   g_object_class_install_property (gobject_class, PROP_SILENT,
380       g_param_spec_boolean ("silent", "Silent",
381           "Don't emit queue signals", FALSE,
382           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
383           G_PARAM_STATIC_STRINGS));
384
385   /**
386    * GstQueue:flush-on-eos
387    *
388    * Discard all data in the queue when an EOS event is received, and pass
389    * on the EOS event as soon as possible (instead of waiting until all
390    * buffers in the queue have been processed, which is the default behaviour).
391    *
392    * Flushing the queue on EOS might be useful when capturing and encoding
393    * from a live source, to finish up the recording quickly in cases when
394    * the encoder is slow. Note that this might mean some data from the end of
395    * the recording data might be lost though (never more than the configured
396    * max. sizes though).
397    *
398    * Since: 1.2
399    */
400   g_object_class_install_property (gobject_class, PROP_FLUSH_ON_EOS,
401       g_param_spec_boolean ("flush-on-eos", "Flush on EOS",
402           "Discard all data in the queue when an EOS event is received", FALSE,
403           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
404           G_PARAM_STATIC_STRINGS));
405
406   gobject_class->finalize = gst_queue_finalize;
407
408   gst_element_class_set_static_metadata (gstelement_class,
409       "Queue",
410       "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
411   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
412   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
413
414   /* Registering debug symbols for function pointers */
415   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
416   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
417   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
418   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
419   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
420   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
421   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain_list);
422 }
423
424 static void
425 gst_queue_init (GstQueue * queue)
426 {
427   queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
428
429   gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
430   gst_pad_set_chain_list_function (queue->sinkpad, gst_queue_chain_list);
431   gst_pad_set_activatemode_function (queue->sinkpad,
432       gst_queue_sink_activate_mode);
433   gst_pad_set_event_full_function (queue->sinkpad, gst_queue_handle_sink_event);
434   gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
435   GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
436   gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
437
438   queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
439
440   gst_pad_set_activatemode_function (queue->srcpad,
441       gst_queue_src_activate_mode);
442   gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
443   gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
444   GST_PAD_SET_PROXY_CAPS (queue->srcpad);
445   gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
446
447   GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
448   queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
449   queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
450   queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
451   GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
452   GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
453   gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
454   gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
455   queue->head_needs_discont = queue->tail_needs_discont = FALSE;
456
457   queue->leaky = GST_QUEUE_NO_LEAK;
458   queue->srcresult = GST_FLOW_FLUSHING;
459
460   g_mutex_init (&queue->qlock);
461   g_cond_init (&queue->item_add);
462   g_cond_init (&queue->item_del);
463   g_cond_init (&queue->query_handled);
464
465   queue->queue =
466       gst_queue_array_new_for_struct (sizeof (GstQueueItem),
467       DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
468
469   queue->sinktime = GST_CLOCK_STIME_NONE;
470   queue->srctime = GST_CLOCK_STIME_NONE;
471
472   queue->sink_tainted = TRUE;
473   queue->src_tainted = TRUE;
474
475   queue->newseg_applied_to_src = FALSE;
476
477   GST_DEBUG_OBJECT (queue,
478       "initialized queue's not_empty & not_full conditions");
479 }
480
481 /* called only once, as opposed to dispose */
482 static void
483 gst_queue_finalize (GObject * object)
484 {
485   GstQueue *queue = GST_QUEUE (object);
486   GstQueueItem *qitem;
487
488   GST_DEBUG_OBJECT (queue, "finalizing queue");
489
490   while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
491     /* FIXME: if it's a query, shouldn't we unref that too? */
492     if (!qitem->is_query)
493       gst_mini_object_unref (qitem->item);
494   }
495   gst_queue_array_free (queue->queue);
496
497   g_mutex_clear (&queue->qlock);
498   g_cond_clear (&queue->item_add);
499   g_cond_clear (&queue->item_del);
500   g_cond_clear (&queue->query_handled);
501
502   G_OBJECT_CLASS (parent_class)->finalize (object);
503 }
504
505 /* Convenience function */
506 static inline GstClockTimeDiff
507 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
508 {
509   GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
510
511   if (GST_CLOCK_TIME_IS_VALID (val)) {
512     gboolean sign =
513         gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
514     if (sign > 0)
515       res = val;
516     else if (sign < 0)
517       res = -val;
518   }
519   return res;
520 }
521
522 /* calculate the diff between running time on the sink and src of the queue.
523  * This is the total amount of time in the queue. */
524 static void
525 update_time_level (GstQueue * queue)
526 {
527   gint64 sink_time, src_time;
528
529   if (queue->sink_tainted) {
530     GST_LOG_OBJECT (queue, "update sink time");
531     queue->sinktime =
532         my_segment_to_running_time (&queue->sink_segment,
533         queue->sink_segment.position);
534     queue->sink_tainted = FALSE;
535   }
536   sink_time = queue->sinktime;
537
538   if (queue->src_tainted) {
539     GST_LOG_OBJECT (queue, "update src time");
540     queue->srctime =
541         my_segment_to_running_time (&queue->src_segment,
542         queue->src_segment.position);
543     queue->src_tainted = FALSE;
544   }
545   src_time = queue->srctime;
546
547   GST_LOG_OBJECT (queue, "sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT,
548       GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
549
550   if (sink_time >= src_time)
551     queue->cur_level.time = sink_time - src_time;
552   else
553     queue->cur_level.time = 0;
554 }
555
556 /* take a SEGMENT event and apply the values to segment, updating the time
557  * level of queue. */
558 static void
559 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
560     gboolean sink)
561 {
562   gst_event_copy_segment (event, segment);
563
564   /* now configure the values, we use these to track timestamps on the
565    * sinkpad. */
566   if (segment->format != GST_FORMAT_TIME) {
567     /* non-time format, pretent the current time segment is closed with a
568      * 0 start and unknown stop time. */
569     segment->format = GST_FORMAT_TIME;
570     segment->start = 0;
571     segment->stop = -1;
572     segment->time = 0;
573   }
574   if (sink)
575     queue->sink_tainted = TRUE;
576   else
577     queue->src_tainted = TRUE;
578
579   GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
580
581   /* segment can update the time level of the queue */
582   update_time_level (queue);
583 }
584
585 static void
586 apply_gap (GstQueue * queue, GstEvent * event,
587     GstSegment * segment, gboolean is_sink)
588 {
589   GstClockTime timestamp;
590   GstClockTime duration;
591
592   gst_event_parse_gap (event, &timestamp, &duration);
593
594   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
595
596     if (GST_CLOCK_TIME_IS_VALID (duration)) {
597       timestamp += duration;
598     }
599
600     segment->position = timestamp;
601
602     if (is_sink)
603       queue->sink_tainted = TRUE;
604     else
605       queue->src_tainted = TRUE;
606
607     /* calc diff with other end */
608     update_time_level (queue);
609   }
610 }
611
612
613 /* take a buffer and update segment, updating the time level of the queue. */
614 static void
615 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
616     gboolean sink)
617 {
618   GstClockTime duration, timestamp;
619
620   timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
621   duration = GST_BUFFER_DURATION (buffer);
622
623   /* if no timestamp is set, assume it's continuous with the previous
624    * time */
625   if (timestamp == GST_CLOCK_TIME_NONE)
626     timestamp = segment->position;
627
628   /* add duration */
629   if (duration != GST_CLOCK_TIME_NONE)
630     timestamp += duration;
631
632   GST_LOG_OBJECT (queue, "%s position updated to %" GST_TIME_FORMAT,
633       segment == &queue->sink_segment ? "sink" : "src",
634       GST_TIME_ARGS (timestamp));
635
636   segment->position = timestamp;
637   if (sink)
638     queue->sink_tainted = TRUE;
639   else
640     queue->src_tainted = TRUE;
641
642
643   /* calc diff with other end */
644   update_time_level (queue);
645 }
646
647 static gboolean
648 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer user_data)
649 {
650   GstClockTime *timestamp = user_data;
651   GstClockTime btime;
652
653   GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
654       " duration %" GST_TIME_FORMAT, idx, GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
655       GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
656       GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
657
658   btime = GST_BUFFER_DTS_OR_PTS (*buf);
659   if (GST_CLOCK_TIME_IS_VALID (btime))
660     *timestamp = btime;
661
662   if (GST_BUFFER_DURATION_IS_VALID (*buf))
663     *timestamp += GST_BUFFER_DURATION (*buf);
664
665   GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
666
667   return TRUE;
668 }
669
670 /* take a buffer list and update segment, updating the time level of the queue */
671 static void
672 apply_buffer_list (GstQueue * queue, GstBufferList * buffer_list,
673     GstSegment * segment, gboolean sink)
674 {
675   GstClockTime timestamp;
676
677   /* if no timestamp is set, assume it's continuous with the previous time */
678   timestamp = segment->position;
679
680   gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &timestamp);
681
682   GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
683       GST_TIME_ARGS (timestamp));
684
685   segment->position = timestamp;
686
687   if (sink)
688     queue->sink_tainted = TRUE;
689   else
690     queue->src_tainted = TRUE;
691
692   /* calc diff with other end */
693   update_time_level (queue);
694 }
695
696 static void
697 gst_queue_locked_flush (GstQueue * queue, gboolean full)
698 {
699   GstQueueItem *qitem;
700
701   while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
702     /* Then lose another reference because we are supposed to destroy that
703        data when flushing */
704     if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
705         && GST_EVENT_IS_STICKY (qitem->item)
706         && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
707         && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
708       gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
709     }
710     if (!qitem->is_query)
711       gst_mini_object_unref (qitem->item);
712     memset (qitem, 0, sizeof (GstQueueItem));
713   }
714   queue->last_query = FALSE;
715   g_cond_signal (&queue->query_handled);
716   GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
717   queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
718   queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
719   queue->min_threshold.time = queue->orig_min_threshold.time;
720   gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
721   gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
722   queue->head_needs_discont = queue->tail_needs_discont = FALSE;
723
724   queue->sinktime = queue->srctime = GST_CLOCK_STIME_NONE;
725   queue->sink_tainted = queue->src_tainted = TRUE;
726
727   /* we deleted a lot of something */
728   GST_QUEUE_SIGNAL_DEL (queue);
729 }
730
731 /* enqueue an item an update the level stats, with QUEUE_LOCK */
732 static inline void
733 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
734 {
735   GstQueueItem qitem;
736   GstBuffer *buffer = GST_BUFFER_CAST (item);
737   gsize bsize = gst_buffer_get_size (buffer);
738
739   /* add buffer to the statistics */
740   queue->cur_level.buffers++;
741   queue->cur_level.bytes += bsize;
742   apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
743
744   qitem.item = item;
745   qitem.is_query = FALSE;
746   qitem.size = bsize;
747   gst_queue_array_push_tail_struct (queue->queue, &qitem);
748   GST_QUEUE_SIGNAL_ADD (queue);
749 }
750
751 static inline void
752 gst_queue_locked_enqueue_buffer_list (GstQueue * queue, gpointer item)
753 {
754   GstQueueItem qitem;
755   GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
756   gsize bsize;
757
758   bsize = gst_buffer_list_calculate_size (buffer_list);
759
760   /* add buffer to the statistics */
761   queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
762   queue->cur_level.bytes += bsize;
763   apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
764
765   qitem.item = item;
766   qitem.is_query = FALSE;
767   qitem.size = bsize;
768   gst_queue_array_push_tail_struct (queue->queue, &qitem);
769   GST_QUEUE_SIGNAL_ADD (queue);
770 }
771
772 static inline void
773 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
774 {
775   GstQueueItem qitem;
776   GstEvent *event = GST_EVENT_CAST (item);
777
778   switch (GST_EVENT_TYPE (event)) {
779     case GST_EVENT_EOS:
780       GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
781       /* Zero the thresholds, this makes sure the queue is completely
782        * filled and we can read all data from the queue. */
783       if (queue->flush_on_eos)
784         gst_queue_locked_flush (queue, FALSE);
785       else
786         GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
787       /* mark the queue as EOS. This prevents us from accepting more data. */
788       queue->eos = TRUE;
789       break;
790     case GST_EVENT_SEGMENT:
791       apply_segment (queue, event, &queue->sink_segment, TRUE);
792       /* if the queue is empty, apply sink segment on the source */
793       if (gst_queue_array_is_empty (queue->queue)) {
794         GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
795         apply_segment (queue, event, &queue->src_segment, FALSE);
796         queue->newseg_applied_to_src = TRUE;
797       }
798       /* a new segment allows us to accept more buffers if we got EOS
799        * from downstream */
800       queue->unexpected = FALSE;
801       break;
802     case GST_EVENT_GAP:
803       apply_gap (queue, event, &queue->sink_segment, TRUE);
804       break;
805     default:
806       break;
807   }
808
809   qitem.item = item;
810   qitem.is_query = FALSE;
811   qitem.size = 0;
812   gst_queue_array_push_tail_struct (queue->queue, &qitem);
813   GST_QUEUE_SIGNAL_ADD (queue);
814 }
815
816 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
817 static GstMiniObject *
818 gst_queue_locked_dequeue (GstQueue * queue)
819 {
820   GstQueueItem *qitem;
821   GstMiniObject *item;
822   gsize bufsize;
823
824   qitem = gst_queue_array_pop_head_struct (queue->queue);
825   if (qitem == NULL)
826     goto no_item;
827
828   item = qitem->item;
829   bufsize = qitem->size;
830
831   if (GST_IS_BUFFER (item)) {
832     GstBuffer *buffer = GST_BUFFER_CAST (item);
833
834     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
835         "retrieved buffer %p from queue", buffer);
836
837     queue->cur_level.buffers--;
838     queue->cur_level.bytes -= bufsize;
839     apply_buffer (queue, buffer, &queue->src_segment, FALSE);
840
841     /* if the queue is empty now, update the other side */
842     if (queue->cur_level.buffers == 0)
843       queue->cur_level.time = 0;
844   } else if (GST_IS_BUFFER_LIST (item)) {
845     GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
846
847     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
848         "retrieved buffer list %p from queue", buffer_list);
849
850     queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
851     queue->cur_level.bytes -= bufsize;
852     apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
853
854     /* if the queue is empty now, update the other side */
855     if (queue->cur_level.buffers == 0)
856       queue->cur_level.time = 0;
857   } else if (GST_IS_EVENT (item)) {
858     GstEvent *event = GST_EVENT_CAST (item);
859
860     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
861         "retrieved event %p from queue", event);
862
863     switch (GST_EVENT_TYPE (event)) {
864       case GST_EVENT_EOS:
865         /* queue is empty now that we dequeued the EOS */
866         GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
867         break;
868       case GST_EVENT_SEGMENT:
869         /* apply newsegment if it has not already been applied */
870         if (G_LIKELY (!queue->newseg_applied_to_src)) {
871           apply_segment (queue, event, &queue->src_segment, FALSE);
872         } else {
873           queue->newseg_applied_to_src = FALSE;
874         }
875         break;
876       case GST_EVENT_GAP:
877         apply_gap (queue, event, &queue->src_segment, FALSE);
878         break;
879       default:
880         break;
881     }
882   } else if (GST_IS_QUERY (item)) {
883     GstQuery *query = GST_QUERY_CAST (item);
884
885     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
886         "retrieved query %p from queue", query);
887   } else {
888     g_warning
889         ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
890         item, GST_OBJECT_NAME (queue));
891     item = NULL;
892   }
893   GST_QUEUE_SIGNAL_DEL (queue);
894
895   return item;
896
897   /* ERRORS */
898 no_item:
899   {
900     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
901     return NULL;
902   }
903 }
904
905 static GstFlowReturn
906 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
907 {
908   gboolean ret = TRUE;
909   GstQueue *queue;
910
911   queue = GST_QUEUE (parent);
912
913   GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Received event '%s'",
914       GST_EVENT_TYPE_NAME (event));
915
916   switch (GST_EVENT_TYPE (event)) {
917     case GST_EVENT_FLUSH_START:
918       /* forward event */
919       ret = gst_pad_push_event (queue->srcpad, event);
920
921       /* now unblock the chain function */
922       GST_QUEUE_MUTEX_LOCK (queue);
923       queue->srcresult = GST_FLOW_FLUSHING;
924       /* unblock the loop and chain functions */
925       GST_QUEUE_SIGNAL_ADD (queue);
926       GST_QUEUE_SIGNAL_DEL (queue);
927       GST_QUEUE_MUTEX_UNLOCK (queue);
928
929       /* make sure it pauses, this should happen since we sent
930        * flush_start downstream. */
931       gst_pad_pause_task (queue->srcpad);
932       GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
933
934       /* unblock query handler after the streaming thread is shut down.
935        * Otherwise downstream might have a query that is already unreffed
936        * upstream */
937       GST_QUEUE_MUTEX_LOCK (queue);
938       queue->last_query = FALSE;
939       g_cond_signal (&queue->query_handled);
940       GST_QUEUE_MUTEX_UNLOCK (queue);
941       break;
942     case GST_EVENT_FLUSH_STOP:
943       /* forward event */
944       ret = gst_pad_push_event (queue->srcpad, event);
945
946       GST_QUEUE_MUTEX_LOCK (queue);
947       gst_queue_locked_flush (queue, FALSE);
948       queue->srcresult = GST_FLOW_OK;
949       queue->eos = FALSE;
950       queue->unexpected = FALSE;
951       if (gst_pad_is_active (queue->srcpad)) {
952         gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
953             queue->srcpad, NULL);
954       } else {
955         GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
956             "pad not active any longer");
957       }
958       GST_QUEUE_MUTEX_UNLOCK (queue);
959
960       STATUS (queue, pad, "after flush");
961       break;
962     default:
963       if (GST_EVENT_IS_SERIALIZED (event)) {
964         /* serialized events go in the queue */
965         GST_QUEUE_MUTEX_LOCK (queue);
966
967         /* STREAM_START and SEGMENT reset the EOS status of a
968          * pad. Change the cached sinkpad flow result accordingly */
969         if (queue->srcresult == GST_FLOW_EOS
970             && (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START
971                 || GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT))
972           queue->srcresult = GST_FLOW_OK;
973
974         if (queue->srcresult != GST_FLOW_OK) {
975           /* Errors in sticky event pushing are no problem and ignored here
976            * as they will cause more meaningful errors during data flow.
977            * For EOS events, that are not followed by data flow, we still
978            * return FALSE here though and report an error.
979            */
980           if (!GST_EVENT_IS_STICKY (event)) {
981             GST_QUEUE_MUTEX_UNLOCK (queue);
982             goto out_flow_error;
983           } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
984             if (queue->srcresult == GST_FLOW_NOT_LINKED
985                 || queue->srcresult < GST_FLOW_EOS) {
986               GST_QUEUE_MUTEX_UNLOCK (queue);
987               GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
988             } else {
989               GST_QUEUE_MUTEX_UNLOCK (queue);
990             }
991             goto out_flow_error;
992           }
993         }
994
995         /* refuse more events on EOS unless they unset the EOS status */
996         if (queue->eos) {
997           switch (GST_EVENT_TYPE (event)) {
998             case GST_EVENT_STREAM_START:
999             case GST_EVENT_SEGMENT:
1000               /* Restart the loop */
1001               if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
1002                 queue->srcresult = GST_FLOW_OK;
1003                 queue->eos = FALSE;
1004                 queue->unexpected = FALSE;
1005                 gst_pad_start_task (queue->srcpad,
1006                     (GstTaskFunction) gst_queue_loop, queue->srcpad, NULL);
1007               } else {
1008                 queue->eos = FALSE;
1009                 queue->unexpected = FALSE;
1010               }
1011
1012               break;
1013             default:
1014               goto out_eos;
1015           }
1016         }
1017
1018         gst_queue_locked_enqueue_event (queue, event);
1019         GST_QUEUE_MUTEX_UNLOCK (queue);
1020       } else {
1021         /* non-serialized events are forwarded downstream immediately */
1022         ret = gst_pad_push_event (queue->srcpad, event);
1023       }
1024       break;
1025   }
1026   if (ret == FALSE) {
1027     GST_ERROR_OBJECT (queue, "Failed to push event");
1028     return GST_FLOW_ERROR;
1029   }
1030   return GST_FLOW_OK;
1031
1032   /* ERRORS */
1033 out_eos:
1034   {
1035     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
1036     GST_QUEUE_MUTEX_UNLOCK (queue);
1037     gst_event_unref (event);
1038     return GST_FLOW_EOS;
1039   }
1040 out_flow_error:
1041   {
1042     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1043         "refusing event, we have a downstream flow error: %s",
1044         gst_flow_get_name (queue->srcresult));
1045     gst_event_unref (event);
1046     return queue->srcresult;
1047   }
1048 }
1049
1050 static gboolean
1051 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1052 {
1053   GstQueue *queue = GST_QUEUE_CAST (parent);
1054   gboolean res;
1055
1056   switch (GST_QUERY_TYPE (query)) {
1057     default:
1058       if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
1059         GstQueueItem qitem;
1060
1061         GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1062         GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
1063             GST_QUERY_TYPE_NAME (query));
1064         qitem.item = GST_MINI_OBJECT_CAST (query);
1065         qitem.is_query = TRUE;
1066         qitem.size = 0;
1067         gst_queue_array_push_tail_struct (queue->queue, &qitem);
1068         GST_QUEUE_SIGNAL_ADD (queue);
1069         while (queue->srcresult == GST_FLOW_OK &&
1070             queue->last_handled_query != query)
1071           g_cond_wait (&queue->query_handled, &queue->qlock);
1072         queue->last_handled_query = NULL;
1073         if (queue->srcresult != GST_FLOW_OK)
1074           goto out_flushing;
1075         res = queue->last_query;
1076         GST_QUEUE_MUTEX_UNLOCK (queue);
1077       } else {
1078         res = gst_pad_query_default (pad, parent, query);
1079       }
1080       break;
1081   }
1082   return res;
1083
1084   /* ERRORS */
1085 out_flushing:
1086   {
1087     GST_DEBUG_OBJECT (queue, "we are flushing");
1088     GST_QUEUE_MUTEX_UNLOCK (queue);
1089     return FALSE;
1090   }
1091 }
1092
1093 static gboolean
1094 gst_queue_is_empty (GstQueue * queue)
1095 {
1096   GstQueueItem *head;
1097
1098   head = gst_queue_array_peek_head_struct (queue->queue);
1099
1100   if (head == NULL)
1101     return TRUE;
1102
1103   /* Only consider the queue empty if the minimum thresholds
1104    * are not reached and data is at the queue head. Otherwise
1105    * we would block forever on serialized queries.
1106    */
1107   if (!GST_IS_BUFFER (head->item) && !GST_IS_BUFFER_LIST (head->item))
1108     return FALSE;
1109
1110   /* It is possible that a max size is reached before all min thresholds are.
1111    * Therefore, only consider it empty if it is not filled. */
1112   return ((queue->min_threshold.buffers > 0 &&
1113           queue->cur_level.buffers < queue->min_threshold.buffers) ||
1114       (queue->min_threshold.bytes > 0 &&
1115           queue->cur_level.bytes < queue->min_threshold.bytes) ||
1116       (queue->min_threshold.time > 0 &&
1117           queue->cur_level.time < queue->min_threshold.time)) &&
1118       !gst_queue_is_filled (queue);
1119 }
1120
1121 static gboolean
1122 gst_queue_is_filled (GstQueue * queue)
1123 {
1124   return (((queue->max_size.buffers > 0 &&
1125               queue->cur_level.buffers >= queue->max_size.buffers) ||
1126           (queue->max_size.bytes > 0 &&
1127               queue->cur_level.bytes >= queue->max_size.bytes) ||
1128           (queue->max_size.time > 0 &&
1129               queue->cur_level.time >= queue->max_size.time)));
1130 }
1131
1132 static void
1133 gst_queue_leak_downstream (GstQueue * queue)
1134 {
1135   /* for as long as the queue is filled, dequeue an item and discard it */
1136   while (gst_queue_is_filled (queue)) {
1137     GstMiniObject *leak;
1138
1139     leak = gst_queue_locked_dequeue (queue);
1140     /* there is nothing to dequeue and the queue is still filled.. This should
1141      * not happen */
1142     g_assert (leak != NULL);
1143
1144     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1145         "queue is full, leaking item %p on downstream end", leak);
1146     if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
1147       GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1148           "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
1149       gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
1150     }
1151
1152     if (!GST_IS_QUERY (leak))
1153       gst_mini_object_unref (leak);
1154
1155     /* last buffer needs to get a DISCONT flag */
1156     queue->head_needs_discont = TRUE;
1157   }
1158 }
1159
1160 static gboolean
1161 discont_first_buffer (GstBuffer ** buffer, guint i, gpointer user_data)
1162 {
1163   GstQueue *queue = user_data;
1164   GstBuffer *subbuffer = gst_buffer_make_writable (*buffer);
1165
1166   if (subbuffer) {
1167     *buffer = subbuffer;
1168     GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
1169   } else {
1170     GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1171   }
1172
1173   return FALSE;
1174 }
1175
1176 static GstFlowReturn
1177 gst_queue_chain_buffer_or_list (GstPad * pad, GstObject * parent,
1178     GstMiniObject * obj, gboolean is_list)
1179 {
1180   GstQueue *queue;
1181
1182   queue = GST_QUEUE_CAST (parent);
1183
1184   /* we have to lock the queue since we span threads */
1185   GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1186   /* when we received EOS, we refuse any more data */
1187   if (queue->eos)
1188     goto out_eos;
1189   if (queue->unexpected)
1190     goto out_unexpected;
1191
1192   if (!is_list) {
1193     GstClockTime duration, timestamp;
1194     GstBuffer *buffer = GST_BUFFER_CAST (obj);
1195
1196     timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1197     duration = GST_BUFFER_DURATION (buffer);
1198
1199     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
1200         G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1201         GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1202         GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1203   } else {
1204     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1205         "received buffer list %p with %u buffers", obj,
1206         gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)));
1207   }
1208
1209   /* We make space available if we're "full" according to whatever
1210    * the user defined as "full". Note that this only applies to buffers.
1211    * We always handle events and they don't count in our statistics. */
1212   while (gst_queue_is_filled (queue)) {
1213     if (!queue->silent) {
1214       GST_QUEUE_MUTEX_UNLOCK (queue);
1215       g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
1216       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1217       /* we recheck, the signal could have changed the thresholds */
1218       if (!gst_queue_is_filled (queue))
1219         break;
1220     }
1221
1222     /* how are we going to make space for this buffer? */
1223     switch (queue->leaky) {
1224       case GST_QUEUE_LEAK_UPSTREAM:
1225         /* next buffer needs to get a DISCONT flag */
1226         queue->tail_needs_discont = TRUE;
1227         /* leak current buffer */
1228         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1229             "queue is full, leaking buffer on upstream end");
1230         /* now we can clean up and exit right away */
1231         goto out_unref;
1232       case GST_QUEUE_LEAK_DOWNSTREAM:
1233         gst_queue_leak_downstream (queue);
1234         break;
1235       default:
1236         g_warning ("Unknown leaky type, using default");
1237         /* fall-through */
1238       case GST_QUEUE_NO_LEAK:
1239       {
1240         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1241             "queue is full, waiting for free space");
1242
1243         /* don't leak. Instead, wait for space to be available */
1244         do {
1245           /* for as long as the queue is filled, wait till an item was deleted. */
1246           GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1247         } while (gst_queue_is_filled (queue));
1248
1249         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1250
1251         if (!queue->silent) {
1252           GST_QUEUE_MUTEX_UNLOCK (queue);
1253           g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1254           GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1255         }
1256         break;
1257       }
1258     }
1259   }
1260
1261   if (queue->tail_needs_discont) {
1262     if (!is_list) {
1263       GstBuffer *buffer = GST_BUFFER_CAST (obj);
1264       GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1265
1266       if (subbuffer) {
1267         buffer = subbuffer;
1268         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1269       } else {
1270         GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1271       }
1272
1273       obj = GST_MINI_OBJECT_CAST (buffer);
1274     } else {
1275       GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
1276
1277       buffer_list = gst_buffer_list_make_writable (buffer_list);
1278       gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1279       obj = GST_MINI_OBJECT_CAST (buffer_list);
1280     }
1281     queue->tail_needs_discont = FALSE;
1282   }
1283
1284   /* put buffer in queue now */
1285   if (is_list)
1286     gst_queue_locked_enqueue_buffer_list (queue, obj);
1287   else
1288     gst_queue_locked_enqueue_buffer (queue, obj);
1289   GST_QUEUE_MUTEX_UNLOCK (queue);
1290
1291   return GST_FLOW_OK;
1292
1293   /* special conditions */
1294 out_unref:
1295   {
1296     GST_QUEUE_MUTEX_UNLOCK (queue);
1297
1298     gst_mini_object_unref (obj);
1299
1300     return GST_FLOW_OK;
1301   }
1302 out_flushing:
1303   {
1304     GstFlowReturn ret = queue->srcresult;
1305
1306     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1307         "exit because task paused, reason: %s", gst_flow_get_name (ret));
1308     GST_QUEUE_MUTEX_UNLOCK (queue);
1309     gst_mini_object_unref (obj);
1310
1311     return ret;
1312   }
1313 out_eos:
1314   {
1315     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1316     GST_QUEUE_MUTEX_UNLOCK (queue);
1317
1318     gst_mini_object_unref (obj);
1319
1320     return GST_FLOW_EOS;
1321   }
1322 out_unexpected:
1323   {
1324     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1325     GST_QUEUE_MUTEX_UNLOCK (queue);
1326
1327     gst_mini_object_unref (obj);
1328
1329     return GST_FLOW_EOS;
1330   }
1331 }
1332
1333 static GstFlowReturn
1334 gst_queue_chain_list (GstPad * pad, GstObject * parent,
1335     GstBufferList * buffer_list)
1336 {
1337   return gst_queue_chain_buffer_or_list (pad, parent,
1338       GST_MINI_OBJECT_CAST (buffer_list), TRUE);
1339 }
1340
1341 static GstFlowReturn
1342 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1343 {
1344   return gst_queue_chain_buffer_or_list (pad, parent,
1345       GST_MINI_OBJECT_CAST (buffer), FALSE);
1346 }
1347
1348 /* dequeue an item from the queue an push it downstream. This functions returns
1349  * the result of the push. */
1350 static GstFlowReturn
1351 gst_queue_push_one (GstQueue * queue)
1352 {
1353   GstFlowReturn result = queue->srcresult;
1354   GstMiniObject *data;
1355   gboolean is_list;
1356
1357   data = gst_queue_locked_dequeue (queue);
1358   if (data == NULL)
1359     goto no_item;
1360
1361 next:
1362   is_list = GST_IS_BUFFER_LIST (data);
1363
1364   if (GST_IS_BUFFER (data) || is_list) {
1365     if (!is_list) {
1366       GstBuffer *buffer;
1367
1368       buffer = GST_BUFFER_CAST (data);
1369
1370       if (queue->head_needs_discont) {
1371         GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1372
1373         if (subbuffer) {
1374           buffer = subbuffer;
1375           GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1376         } else {
1377           GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1378         }
1379         queue->head_needs_discont = FALSE;
1380       }
1381
1382       GST_QUEUE_MUTEX_UNLOCK (queue);
1383       result = gst_pad_push (queue->srcpad, buffer);
1384     } else {
1385       GstBufferList *buffer_list;
1386
1387       buffer_list = GST_BUFFER_LIST_CAST (data);
1388
1389       if (queue->head_needs_discont) {
1390         buffer_list = gst_buffer_list_make_writable (buffer_list);
1391         gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1392         queue->head_needs_discont = FALSE;
1393       }
1394
1395       GST_QUEUE_MUTEX_UNLOCK (queue);
1396       result = gst_pad_push_list (queue->srcpad, buffer_list);
1397     }
1398
1399     /* need to check for srcresult here as well */
1400     GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1401
1402     if (result == GST_FLOW_EOS) {
1403       GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1404       /* stop pushing buffers, we dequeue all items until we see an item that we
1405        * can push again, which is EOS or SEGMENT. If there is nothing in the
1406        * queue we can push, we set a flag to make the sinkpad refuse more
1407        * buffers with an EOS return value. */
1408       while ((data = gst_queue_locked_dequeue (queue))) {
1409         if (GST_IS_BUFFER (data)) {
1410           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1411               "dropping EOS buffer %p", data);
1412           gst_buffer_unref (GST_BUFFER_CAST (data));
1413         } else if (GST_IS_BUFFER_LIST (data)) {
1414           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1415               "dropping EOS buffer list %p", data);
1416           gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
1417         } else if (GST_IS_EVENT (data)) {
1418           GstEvent *event = GST_EVENT_CAST (data);
1419           GstEventType type = GST_EVENT_TYPE (event);
1420
1421           if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT
1422               || type == GST_EVENT_STREAM_START) {
1423             /* we found a pushable item in the queue, push it out */
1424             GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1425                 "pushing pushable event %s after EOS",
1426                 GST_EVENT_TYPE_NAME (event));
1427             goto next;
1428           }
1429           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1430               "dropping EOS event %p", event);
1431           gst_event_unref (event);
1432         } else if (GST_IS_QUERY (data)) {
1433           GstQuery *query = GST_QUERY_CAST (data);
1434
1435           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1436               "dropping query %p because of EOS", query);
1437           queue->last_query = FALSE;
1438           g_cond_signal (&queue->query_handled);
1439         }
1440       }
1441       /* no more items in the queue. Set the unexpected flag so that upstream
1442        * make us refuse any more buffers on the sinkpad. Since we will still
1443        * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1444        * task function does not shut down. */
1445       queue->unexpected = TRUE;
1446       result = GST_FLOW_OK;
1447     }
1448   } else if (GST_IS_EVENT (data)) {
1449     GstEvent *event = GST_EVENT_CAST (data);
1450     GstEventType type = GST_EVENT_TYPE (event);
1451
1452     GST_QUEUE_MUTEX_UNLOCK (queue);
1453
1454     gst_pad_push_event (queue->srcpad, event);
1455
1456     GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1457     /* if we're EOS, return EOS so that the task pauses. */
1458     if (type == GST_EVENT_EOS) {
1459       GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1460           "pushed EOS event %p, return EOS", event);
1461       result = GST_FLOW_EOS;
1462     }
1463   } else if (GST_IS_QUERY (data)) {
1464     GstQuery *query = GST_QUERY_CAST (data);
1465     gboolean ret;
1466
1467     GST_QUEUE_MUTEX_UNLOCK (queue);
1468     ret = gst_pad_peer_query (queue->srcpad, query);
1469     GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1470     queue->last_query = ret;
1471     queue->last_handled_query = query;
1472     g_cond_signal (&queue->query_handled);
1473     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1474         "did query %p, return %d", query, queue->last_query);
1475   }
1476   return result;
1477
1478   /* ERRORS */
1479 no_item:
1480   {
1481     GST_CAT_ERROR_OBJECT (queue_dataflow, queue,
1482         "exit because we have no item in the queue");
1483     return GST_FLOW_ERROR;
1484   }
1485 out_flushing:
1486   {
1487     GstFlowReturn ret = queue->srcresult;
1488     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1489         "exit because task paused, reason: %s", gst_flow_get_name (ret));
1490     return ret;
1491   }
1492 out_flushing_query:
1493   {
1494     GstFlowReturn ret = queue->srcresult;
1495     queue->last_query = FALSE;
1496     g_cond_signal (&queue->query_handled);
1497     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1498         "exit because task paused, reason: %s", gst_flow_get_name (ret));
1499     return ret;
1500   }
1501 }
1502
1503 static void
1504 gst_queue_loop (GstPad * pad)
1505 {
1506   GstQueue *queue;
1507   GstFlowReturn ret;
1508
1509   queue = (GstQueue *) GST_PAD_PARENT (pad);
1510
1511   /* have to lock for thread-safety */
1512   GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1513
1514   while (gst_queue_is_empty (queue)) {
1515     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1516     if (!queue->silent) {
1517       GST_QUEUE_MUTEX_UNLOCK (queue);
1518       g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1519       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1520     }
1521
1522     /* we recheck, the signal could have changed the thresholds */
1523     while (gst_queue_is_empty (queue)) {
1524       GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1525     }
1526
1527     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1528     if (!queue->silent) {
1529       GST_QUEUE_MUTEX_UNLOCK (queue);
1530       g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1531       g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1532       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1533     }
1534   }
1535
1536   ret = gst_queue_push_one (queue);
1537   queue->srcresult = ret;
1538   if (ret != GST_FLOW_OK)
1539     goto out_flushing;
1540
1541   GST_QUEUE_MUTEX_UNLOCK (queue);
1542
1543   return;
1544
1545   /* ERRORS */
1546 out_flushing:
1547   {
1548     gboolean eos = queue->eos;
1549     GstFlowReturn ret = queue->srcresult;
1550
1551     gst_pad_pause_task (queue->srcpad);
1552     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1553         "pause task, reason:  %s", gst_flow_get_name (ret));
1554     if (ret == GST_FLOW_FLUSHING) {
1555       gst_queue_locked_flush (queue, FALSE);
1556     } else {
1557       GST_QUEUE_SIGNAL_DEL (queue);
1558       queue->last_query = FALSE;
1559       g_cond_signal (&queue->query_handled);
1560     }
1561     GST_QUEUE_MUTEX_UNLOCK (queue);
1562     /* let app know about us giving up if upstream is not expected to do so */
1563     /* EOS is already taken care of elsewhere */
1564     if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1565       GST_ELEMENT_FLOW_ERROR (queue, ret);
1566       gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1567     }
1568     return;
1569   }
1570 }
1571
1572 static gboolean
1573 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1574 {
1575   gboolean res = TRUE;
1576   GstQueue *queue = GST_QUEUE (parent);
1577
1578 #ifndef GST_DISABLE_GST_DEBUG
1579   GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1580       event, GST_EVENT_TYPE (event));
1581 #endif
1582
1583   switch (GST_EVENT_TYPE (event)) {
1584     case GST_EVENT_RECONFIGURE:
1585       GST_QUEUE_MUTEX_LOCK (queue);
1586       if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1587         /* when we got not linked, assume downstream is linked again now and we
1588          * can try to start pushing again */
1589         queue->srcresult = GST_FLOW_OK;
1590         gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1591       }
1592       GST_QUEUE_MUTEX_UNLOCK (queue);
1593
1594       res = gst_pad_push_event (queue->sinkpad, event);
1595       break;
1596     default:
1597       res = gst_pad_event_default (pad, parent, event);
1598       break;
1599   }
1600
1601
1602   return res;
1603 }
1604
1605 static gboolean
1606 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1607 {
1608   GstQueue *queue = GST_QUEUE (parent);
1609   gboolean res;
1610
1611   switch (GST_QUERY_TYPE (query)) {
1612     case GST_QUERY_SCHEDULING:{
1613       gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1614       res = TRUE;
1615       break;
1616     }
1617     default:
1618       res = gst_pad_query_default (pad, parent, query);
1619       break;
1620   }
1621
1622   if (!res)
1623     return FALSE;
1624
1625   /* Adjust peer response for data contained in queue */
1626   switch (GST_QUERY_TYPE (query)) {
1627     case GST_QUERY_POSITION:
1628     {
1629       gint64 peer_pos;
1630       GstFormat format;
1631
1632       /* get peer position */
1633       gst_query_parse_position (query, &format, &peer_pos);
1634
1635       /* FIXME: this code assumes that there's no discont in the queue */
1636       switch (format) {
1637         case GST_FORMAT_BYTES:
1638           peer_pos -= queue->cur_level.bytes;
1639           if (peer_pos < 0)     /* Clamp result to 0 */
1640             peer_pos = 0;
1641           break;
1642         case GST_FORMAT_TIME:
1643           peer_pos -= queue->cur_level.time;
1644           if (peer_pos < 0)     /* Clamp result to 0 */
1645             peer_pos = 0;
1646           break;
1647         default:
1648           GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1649               "know how to adjust value", gst_format_get_name (format));
1650           return TRUE;
1651       }
1652       /* set updated position */
1653       gst_query_set_position (query, format, peer_pos);
1654       break;
1655     }
1656     case GST_QUERY_LATENCY:
1657     {
1658       gboolean live;
1659       GstClockTime min, max;
1660
1661       gst_query_parse_latency (query, &live, &min, &max);
1662
1663       /* we can delay up to the limit of the queue in time. If we have no time
1664        * limit, the best thing we can do is to return an infinite delay. In
1665        * reality a better estimate would be the byte/buffer rate but that is not
1666        * possible right now. */
1667       /* TODO: Use CONVERT query? */
1668       if (queue->max_size.time > 0 && max != -1
1669           && queue->leaky == GST_QUEUE_NO_LEAK)
1670         max += queue->max_size.time;
1671       else if (queue->max_size.time > 0 && queue->leaky != GST_QUEUE_NO_LEAK)
1672         max = MIN (queue->max_size.time, max);
1673       else
1674         max = -1;
1675
1676       /* adjust for min-threshold */
1677       if (queue->min_threshold.time > 0)
1678         min += queue->min_threshold.time;
1679
1680       gst_query_set_latency (query, live, min, max);
1681       break;
1682     }
1683     default:
1684       /* peer handled other queries */
1685       break;
1686   }
1687
1688   return TRUE;
1689 }
1690
1691 static gboolean
1692 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1693     gboolean active)
1694 {
1695   gboolean result;
1696   GstQueue *queue;
1697
1698   queue = GST_QUEUE (parent);
1699
1700   switch (mode) {
1701     case GST_PAD_MODE_PUSH:
1702       if (active) {
1703         GST_QUEUE_MUTEX_LOCK (queue);
1704         queue->srcresult = GST_FLOW_OK;
1705         queue->eos = FALSE;
1706         queue->unexpected = FALSE;
1707         GST_QUEUE_MUTEX_UNLOCK (queue);
1708       } else {
1709         /* step 1, unblock chain function */
1710         GST_QUEUE_MUTEX_LOCK (queue);
1711         queue->srcresult = GST_FLOW_FLUSHING;
1712         /* the item del signal will unblock */
1713         GST_QUEUE_SIGNAL_DEL (queue);
1714         GST_QUEUE_MUTEX_UNLOCK (queue);
1715
1716         /* step 2, wait until streaming thread stopped and flush queue */
1717         GST_PAD_STREAM_LOCK (pad);
1718         GST_QUEUE_MUTEX_LOCK (queue);
1719         gst_queue_locked_flush (queue, TRUE);
1720         GST_QUEUE_MUTEX_UNLOCK (queue);
1721         GST_PAD_STREAM_UNLOCK (pad);
1722       }
1723       result = TRUE;
1724       break;
1725     default:
1726       result = FALSE;
1727       break;
1728   }
1729   return result;
1730 }
1731
1732 static gboolean
1733 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1734     gboolean active)
1735 {
1736   gboolean result;
1737   GstQueue *queue;
1738
1739   queue = GST_QUEUE (parent);
1740
1741   switch (mode) {
1742     case GST_PAD_MODE_PUSH:
1743       if (active) {
1744         GST_QUEUE_MUTEX_LOCK (queue);
1745         queue->srcresult = GST_FLOW_OK;
1746         queue->eos = FALSE;
1747         queue->unexpected = FALSE;
1748         result =
1749             gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1750             NULL);
1751         GST_QUEUE_MUTEX_UNLOCK (queue);
1752       } else {
1753         /* step 1, unblock loop function */
1754         GST_QUEUE_MUTEX_LOCK (queue);
1755         queue->srcresult = GST_FLOW_FLUSHING;
1756         /* the item add signal will unblock */
1757         g_cond_signal (&queue->item_add);
1758         GST_QUEUE_MUTEX_UNLOCK (queue);
1759
1760         /* step 2, make sure streaming finishes */
1761         result = gst_pad_stop_task (pad);
1762
1763         GST_QUEUE_MUTEX_LOCK (queue);
1764         gst_queue_locked_flush (queue, FALSE);
1765         GST_QUEUE_MUTEX_UNLOCK (queue);
1766       }
1767       break;
1768     default:
1769       result = FALSE;
1770       break;
1771   }
1772   return result;
1773 }
1774
1775 static void
1776 queue_capacity_change (GstQueue * queue)
1777 {
1778   if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1779     gst_queue_leak_downstream (queue);
1780   }
1781
1782   /* changing the capacity of the queue must wake up
1783    * the _chain function, it might have more room now
1784    * to store the buffer/event in the queue */
1785   GST_QUEUE_SIGNAL_DEL (queue);
1786 }
1787
1788 /* Changing the minimum required fill level must
1789  * wake up the _loop function as it might now
1790  * be able to preceed.
1791  */
1792 #define QUEUE_THRESHOLD_CHANGE(q)\
1793   GST_QUEUE_SIGNAL_ADD (q);
1794
1795 static void
1796 gst_queue_set_property (GObject * object,
1797     guint prop_id, const GValue * value, GParamSpec * pspec)
1798 {
1799   GstQueue *queue = GST_QUEUE (object);
1800
1801   /* someone could change levels here, and since this
1802    * affects the get/put funcs, we need to lock for safety. */
1803   GST_QUEUE_MUTEX_LOCK (queue);
1804
1805   switch (prop_id) {
1806     case PROP_MAX_SIZE_BYTES:
1807       queue->max_size.bytes = g_value_get_uint (value);
1808       queue_capacity_change (queue);
1809       break;
1810     case PROP_MAX_SIZE_BUFFERS:
1811       queue->max_size.buffers = g_value_get_uint (value);
1812       queue_capacity_change (queue);
1813       break;
1814     case PROP_MAX_SIZE_TIME:
1815       queue->max_size.time = g_value_get_uint64 (value);
1816       queue_capacity_change (queue);
1817       break;
1818     case PROP_MIN_THRESHOLD_BYTES:
1819       queue->min_threshold.bytes = g_value_get_uint (value);
1820       queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1821       QUEUE_THRESHOLD_CHANGE (queue);
1822       break;
1823     case PROP_MIN_THRESHOLD_BUFFERS:
1824       queue->min_threshold.buffers = g_value_get_uint (value);
1825       queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1826       QUEUE_THRESHOLD_CHANGE (queue);
1827       break;
1828     case PROP_MIN_THRESHOLD_TIME:
1829       queue->min_threshold.time = g_value_get_uint64 (value);
1830       queue->orig_min_threshold.time = queue->min_threshold.time;
1831       QUEUE_THRESHOLD_CHANGE (queue);
1832       break;
1833     case PROP_LEAKY:
1834       queue->leaky = g_value_get_enum (value);
1835       break;
1836     case PROP_SILENT:
1837       queue->silent = g_value_get_boolean (value);
1838       break;
1839     case PROP_FLUSH_ON_EOS:
1840       queue->flush_on_eos = g_value_get_boolean (value);
1841       break;
1842     default:
1843       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1844       break;
1845   }
1846
1847   GST_QUEUE_MUTEX_UNLOCK (queue);
1848 }
1849
1850 static void
1851 gst_queue_get_property (GObject * object,
1852     guint prop_id, GValue * value, GParamSpec * pspec)
1853 {
1854   GstQueue *queue = GST_QUEUE (object);
1855
1856   GST_QUEUE_MUTEX_LOCK (queue);
1857
1858   switch (prop_id) {
1859     case PROP_CUR_LEVEL_BYTES:
1860       g_value_set_uint (value, queue->cur_level.bytes);
1861       break;
1862     case PROP_CUR_LEVEL_BUFFERS:
1863       g_value_set_uint (value, queue->cur_level.buffers);
1864       break;
1865     case PROP_CUR_LEVEL_TIME:
1866       g_value_set_uint64 (value, queue->cur_level.time);
1867       break;
1868     case PROP_MAX_SIZE_BYTES:
1869       g_value_set_uint (value, queue->max_size.bytes);
1870       break;
1871     case PROP_MAX_SIZE_BUFFERS:
1872       g_value_set_uint (value, queue->max_size.buffers);
1873       break;
1874     case PROP_MAX_SIZE_TIME:
1875       g_value_set_uint64 (value, queue->max_size.time);
1876       break;
1877     case PROP_MIN_THRESHOLD_BYTES:
1878       g_value_set_uint (value, queue->min_threshold.bytes);
1879       break;
1880     case PROP_MIN_THRESHOLD_BUFFERS:
1881       g_value_set_uint (value, queue->min_threshold.buffers);
1882       break;
1883     case PROP_MIN_THRESHOLD_TIME:
1884       g_value_set_uint64 (value, queue->min_threshold.time);
1885       break;
1886     case PROP_LEAKY:
1887       g_value_set_enum (value, queue->leaky);
1888       break;
1889     case PROP_SILENT:
1890       g_value_set_boolean (value, queue->silent);
1891       break;
1892     case PROP_FLUSH_ON_EOS:
1893       g_value_set_boolean (value, queue->flush_on_eos);
1894       break;
1895     default:
1896       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1897       break;
1898   }
1899
1900   GST_QUEUE_MUTEX_UNLOCK (queue);
1901 }