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