elements: use new gst_element_class_add_static_pad_template()
[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_TIME_NONE;
469   queue->srctime = GST_CLOCK_TIME_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 /* calculate the diff between running time on the sink and src of the queue.
505  * This is the total amount of time in the queue. */
506 static void
507 update_time_level (GstQueue * queue)
508 {
509   gint64 sink_time, src_time;
510
511   if (queue->sink_tainted) {
512     GST_LOG_OBJECT (queue, "update sink time");
513     queue->sinktime =
514         gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
515         queue->sink_segment.position);
516     queue->sink_tainted = FALSE;
517   }
518   sink_time = queue->sinktime;
519
520   if (queue->src_tainted) {
521     GST_LOG_OBJECT (queue, "update src time");
522     queue->srctime =
523         gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
524         queue->src_segment.position);
525     queue->src_tainted = FALSE;
526   }
527   src_time = queue->srctime;
528
529   GST_LOG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
530       GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
531
532   if (sink_time >= src_time)
533     queue->cur_level.time = sink_time - src_time;
534   else
535     queue->cur_level.time = 0;
536 }
537
538 /* take a SEGMENT event and apply the values to segment, updating the time
539  * level of queue. */
540 static void
541 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
542     gboolean sink)
543 {
544   gst_event_copy_segment (event, segment);
545
546   /* now configure the values, we use these to track timestamps on the
547    * sinkpad. */
548   if (segment->format != GST_FORMAT_TIME) {
549     /* non-time format, pretent the current time segment is closed with a
550      * 0 start and unknown stop time. */
551     segment->format = GST_FORMAT_TIME;
552     segment->start = 0;
553     segment->stop = -1;
554     segment->time = 0;
555   }
556   if (sink)
557     queue->sink_tainted = TRUE;
558   else
559     queue->src_tainted = TRUE;
560
561   GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
562
563   /* segment can update the time level of the queue */
564   update_time_level (queue);
565 }
566
567 static void
568 apply_gap (GstQueue * queue, GstEvent * event,
569     GstSegment * segment, gboolean is_sink)
570 {
571   GstClockTime timestamp;
572   GstClockTime duration;
573
574   gst_event_parse_gap (event, &timestamp, &duration);
575
576   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
577
578     if (GST_CLOCK_TIME_IS_VALID (duration)) {
579       timestamp += duration;
580     }
581
582     segment->position = timestamp;
583
584     if (is_sink)
585       queue->sink_tainted = TRUE;
586     else
587       queue->src_tainted = TRUE;
588
589     /* calc diff with other end */
590     update_time_level (queue);
591   }
592 }
593
594
595 /* take a buffer and update segment, updating the time level of the queue. */
596 static void
597 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
598     gboolean sink)
599 {
600   GstClockTime duration, timestamp;
601
602   timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
603   duration = GST_BUFFER_DURATION (buffer);
604
605   /* if no timestamp is set, assume it's continuous with the previous
606    * time */
607   if (timestamp == GST_CLOCK_TIME_NONE)
608     timestamp = segment->position;
609
610   /* add duration */
611   if (duration != GST_CLOCK_TIME_NONE)
612     timestamp += duration;
613
614   GST_LOG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
615       GST_TIME_ARGS (timestamp));
616
617   segment->position = timestamp;
618   if (sink)
619     queue->sink_tainted = TRUE;
620   else
621     queue->src_tainted = TRUE;
622
623
624   /* calc diff with other end */
625   update_time_level (queue);
626 }
627
628 static gboolean
629 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer user_data)
630 {
631   GstClockTime *timestamp = user_data;
632   GstClockTime btime;
633
634   GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
635       " duration %" GST_TIME_FORMAT, idx, GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
636       GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
637       GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
638
639   btime = GST_BUFFER_DTS_OR_PTS (*buf);
640   if (GST_CLOCK_TIME_IS_VALID (btime))
641     *timestamp = btime;
642
643   if (GST_BUFFER_DURATION_IS_VALID (*buf))
644     *timestamp += GST_BUFFER_DURATION (*buf);
645
646   GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
647
648   return TRUE;
649 }
650
651 /* take a buffer list and update segment, updating the time level of the queue */
652 static void
653 apply_buffer_list (GstQueue * queue, GstBufferList * buffer_list,
654     GstSegment * segment, gboolean sink)
655 {
656   GstClockTime timestamp;
657
658   /* if no timestamp is set, assume it's continuous with the previous time */
659   timestamp = segment->position;
660
661   gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &timestamp);
662
663   GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
664       GST_TIME_ARGS (timestamp));
665
666   segment->position = timestamp;
667
668   if (sink)
669     queue->sink_tainted = TRUE;
670   else
671     queue->src_tainted = TRUE;
672
673   /* calc diff with other end */
674   update_time_level (queue);
675 }
676
677 static void
678 gst_queue_locked_flush (GstQueue * queue, gboolean full)
679 {
680   GstQueueItem *qitem;
681
682   while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
683     /* Then lose another reference because we are supposed to destroy that
684        data when flushing */
685     if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
686         && GST_EVENT_IS_STICKY (qitem->item)
687         && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
688         && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
689       gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
690     }
691     if (!qitem->is_query)
692       gst_mini_object_unref (qitem->item);
693     memset (qitem, 0, sizeof (GstQueueItem));
694   }
695   queue->last_query = FALSE;
696   g_cond_signal (&queue->query_handled);
697   GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
698   queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
699   queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
700   queue->min_threshold.time = queue->orig_min_threshold.time;
701   gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
702   gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
703   queue->head_needs_discont = queue->tail_needs_discont = FALSE;
704
705   queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
706   queue->sink_tainted = queue->src_tainted = TRUE;
707
708   /* we deleted a lot of something */
709   GST_QUEUE_SIGNAL_DEL (queue);
710 }
711
712 /* enqueue an item an update the level stats, with QUEUE_LOCK */
713 static inline void
714 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
715 {
716   GstQueueItem qitem;
717   GstBuffer *buffer = GST_BUFFER_CAST (item);
718   gsize bsize = gst_buffer_get_size (buffer);
719
720   /* add buffer to the statistics */
721   queue->cur_level.buffers++;
722   queue->cur_level.bytes += bsize;
723   apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
724
725   qitem.item = item;
726   qitem.is_query = FALSE;
727   qitem.size = bsize;
728   gst_queue_array_push_tail_struct (queue->queue, &qitem);
729   GST_QUEUE_SIGNAL_ADD (queue);
730 }
731
732 static gboolean
733 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
734 {
735   guint *p_size = data;
736   gsize buf_size;
737
738   buf_size = gst_buffer_get_size (*buf);
739   GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
740   *p_size += buf_size;
741   return TRUE;
742 }
743
744 static inline void
745 gst_queue_locked_enqueue_buffer_list (GstQueue * queue, gpointer item)
746 {
747   GstQueueItem qitem;
748   GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
749   gsize bsize = 0;
750
751   gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &bsize);
752
753   /* add buffer to the statistics */
754   queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
755   queue->cur_level.bytes += bsize;
756   apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
757
758   qitem.item = item;
759   qitem.is_query = FALSE;
760   qitem.size = bsize;
761   gst_queue_array_push_tail_struct (queue->queue, &qitem);
762   GST_QUEUE_SIGNAL_ADD (queue);
763 }
764
765 static inline void
766 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
767 {
768   GstQueueItem qitem;
769   GstEvent *event = GST_EVENT_CAST (item);
770
771   switch (GST_EVENT_TYPE (event)) {
772     case GST_EVENT_EOS:
773       GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
774       /* Zero the thresholds, this makes sure the queue is completely
775        * filled and we can read all data from the queue. */
776       if (queue->flush_on_eos)
777         gst_queue_locked_flush (queue, FALSE);
778       else
779         GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
780       /* mark the queue as EOS. This prevents us from accepting more data. */
781       queue->eos = TRUE;
782       break;
783     case GST_EVENT_SEGMENT:
784       apply_segment (queue, event, &queue->sink_segment, TRUE);
785       /* if the queue is empty, apply sink segment on the source */
786       if (gst_queue_array_is_empty (queue->queue)) {
787         GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
788         apply_segment (queue, event, &queue->src_segment, FALSE);
789         queue->newseg_applied_to_src = TRUE;
790       }
791       /* a new segment allows us to accept more buffers if we got EOS
792        * from downstream */
793       queue->unexpected = FALSE;
794       break;
795     case GST_EVENT_GAP:
796       apply_gap (queue, event, &queue->sink_segment, TRUE);
797       break;
798     default:
799       break;
800   }
801
802   qitem.item = item;
803   qitem.is_query = FALSE;
804   qitem.size = 0;
805   gst_queue_array_push_tail_struct (queue->queue, &qitem);
806   GST_QUEUE_SIGNAL_ADD (queue);
807 }
808
809 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
810 static GstMiniObject *
811 gst_queue_locked_dequeue (GstQueue * queue)
812 {
813   GstQueueItem *qitem;
814   GstMiniObject *item;
815   gsize bufsize;
816
817   qitem = gst_queue_array_pop_head_struct (queue->queue);
818   if (qitem == NULL)
819     goto no_item;
820
821   item = qitem->item;
822   bufsize = qitem->size;
823
824   if (GST_IS_BUFFER (item)) {
825     GstBuffer *buffer = GST_BUFFER_CAST (item);
826
827     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
828         "retrieved buffer %p from queue", buffer);
829
830     queue->cur_level.buffers--;
831     queue->cur_level.bytes -= bufsize;
832     apply_buffer (queue, buffer, &queue->src_segment, FALSE);
833
834     /* if the queue is empty now, update the other side */
835     if (queue->cur_level.buffers == 0)
836       queue->cur_level.time = 0;
837   } else if (GST_IS_BUFFER_LIST (item)) {
838     GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
839
840     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
841         "retrieved buffer list %p from queue", buffer_list);
842
843     queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
844     queue->cur_level.bytes -= bufsize;
845     apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
846
847     /* if the queue is empty now, update the other side */
848     if (queue->cur_level.buffers == 0)
849       queue->cur_level.time = 0;
850   } else if (GST_IS_EVENT (item)) {
851     GstEvent *event = GST_EVENT_CAST (item);
852
853     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
854         "retrieved event %p from queue", event);
855
856     switch (GST_EVENT_TYPE (event)) {
857       case GST_EVENT_EOS:
858         /* queue is empty now that we dequeued the EOS */
859         GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
860         break;
861       case GST_EVENT_SEGMENT:
862         /* apply newsegment if it has not already been applied */
863         if (G_LIKELY (!queue->newseg_applied_to_src)) {
864           apply_segment (queue, event, &queue->src_segment, FALSE);
865         } else {
866           queue->newseg_applied_to_src = FALSE;
867         }
868         break;
869       case GST_EVENT_GAP:
870         apply_gap (queue, event, &queue->src_segment, FALSE);
871         break;
872       default:
873         break;
874     }
875   } else if (GST_IS_QUERY (item)) {
876     GstQuery *query = GST_QUERY_CAST (item);
877
878     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
879         "retrieved query %p from queue", query);
880   } else {
881     g_warning
882         ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
883         item, GST_OBJECT_NAME (queue));
884     item = NULL;
885   }
886   GST_QUEUE_SIGNAL_DEL (queue);
887
888   return item;
889
890   /* ERRORS */
891 no_item:
892   {
893     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
894     return NULL;
895   }
896 }
897
898 static GstFlowReturn
899 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
900 {
901   gboolean ret = TRUE;
902   GstQueue *queue;
903
904   queue = GST_QUEUE (parent);
905
906   switch (GST_EVENT_TYPE (event)) {
907     case GST_EVENT_FLUSH_START:
908       STATUS (queue, pad, "received flush start event");
909       /* forward event */
910       ret = gst_pad_push_event (queue->srcpad, event);
911
912       /* now unblock the chain function */
913       GST_QUEUE_MUTEX_LOCK (queue);
914       queue->srcresult = GST_FLOW_FLUSHING;
915       /* unblock the loop and chain functions */
916       GST_QUEUE_SIGNAL_ADD (queue);
917       GST_QUEUE_SIGNAL_DEL (queue);
918       queue->last_query = FALSE;
919       g_cond_signal (&queue->query_handled);
920       GST_QUEUE_MUTEX_UNLOCK (queue);
921
922       /* make sure it pauses, this should happen since we sent
923        * flush_start downstream. */
924       gst_pad_pause_task (queue->srcpad);
925       GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
926       break;
927     case GST_EVENT_FLUSH_STOP:
928       STATUS (queue, pad, "received flush stop event");
929       /* forward event */
930       ret = gst_pad_push_event (queue->srcpad, event);
931
932       GST_QUEUE_MUTEX_LOCK (queue);
933       gst_queue_locked_flush (queue, FALSE);
934       queue->srcresult = GST_FLOW_OK;
935       queue->eos = FALSE;
936       queue->unexpected = FALSE;
937       if (gst_pad_is_active (queue->srcpad)) {
938         gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
939             queue->srcpad, NULL);
940       } else {
941         GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
942             "pad not active any longer");
943       }
944       GST_QUEUE_MUTEX_UNLOCK (queue);
945
946       STATUS (queue, pad, "after flush");
947       break;
948     default:
949       if (GST_EVENT_IS_SERIALIZED (event)) {
950         /* serialized events go in the queue */
951         GST_QUEUE_MUTEX_LOCK (queue);
952         if (queue->srcresult != GST_FLOW_OK) {
953           /* Errors in sticky event pushing are no problem and ignored here
954            * as they will cause more meaningful errors during data flow.
955            * For EOS events, that are not followed by data flow, we still
956            * return FALSE here though and report an error.
957            */
958           if (!GST_EVENT_IS_STICKY (event)) {
959             GST_QUEUE_MUTEX_UNLOCK (queue);
960             goto out_flow_error;
961           } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
962             if (queue->srcresult == GST_FLOW_NOT_LINKED
963                 || queue->srcresult < GST_FLOW_EOS) {
964               GST_QUEUE_MUTEX_UNLOCK (queue);
965               GST_ELEMENT_ERROR (queue, STREAM, FAILED,
966                   (_("Internal data flow error.")),
967                   ("streaming task paused, reason %s (%d)",
968                       gst_flow_get_name (queue->srcresult), queue->srcresult));
969             } else {
970               GST_QUEUE_MUTEX_UNLOCK (queue);
971             }
972             goto out_flow_error;
973           }
974         }
975         /* refuse more events on EOS */
976         if (queue->eos)
977           goto out_eos;
978         gst_queue_locked_enqueue_event (queue, event);
979         GST_QUEUE_MUTEX_UNLOCK (queue);
980       } else {
981         /* non-serialized events are forwarded downstream immediately */
982         ret = gst_pad_push_event (queue->srcpad, event);
983       }
984       break;
985   }
986   if (ret == FALSE)
987     return GST_FLOW_ERROR;
988   return GST_FLOW_OK;
989
990   /* ERRORS */
991 out_eos:
992   {
993     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
994     GST_QUEUE_MUTEX_UNLOCK (queue);
995     gst_event_unref (event);
996     return GST_FLOW_EOS;
997   }
998 out_flow_error:
999   {
1000     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1001         "refusing event, we have a downstream flow error: %s",
1002         gst_flow_get_name (queue->srcresult));
1003     gst_event_unref (event);
1004     return queue->srcresult;
1005   }
1006 }
1007
1008 static gboolean
1009 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1010 {
1011   GstQueue *queue = GST_QUEUE_CAST (parent);
1012   gboolean res;
1013
1014   switch (GST_QUERY_TYPE (query)) {
1015     default:
1016       if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
1017         GstQueueItem qitem;
1018
1019         GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1020         GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
1021             GST_QUERY_TYPE_NAME (query));
1022         qitem.item = GST_MINI_OBJECT_CAST (query);
1023         qitem.is_query = TRUE;
1024         qitem.size = 0;
1025         gst_queue_array_push_tail_struct (queue->queue, &qitem);
1026         GST_QUEUE_SIGNAL_ADD (queue);
1027         g_cond_wait (&queue->query_handled, &queue->qlock);
1028         if (queue->srcresult != GST_FLOW_OK)
1029           goto out_flushing;
1030         res = queue->last_query;
1031         GST_QUEUE_MUTEX_UNLOCK (queue);
1032       } else {
1033         res = gst_pad_query_default (pad, parent, query);
1034       }
1035       break;
1036   }
1037   return res;
1038
1039   /* ERRORS */
1040 out_flushing:
1041   {
1042     GST_DEBUG_OBJECT (queue, "we are flushing");
1043     GST_QUEUE_MUTEX_UNLOCK (queue);
1044     return FALSE;
1045   }
1046 }
1047
1048 static gboolean
1049 gst_queue_is_empty (GstQueue * queue)
1050 {
1051   GstQueueItem *head;
1052
1053   head = gst_queue_array_peek_head_struct (queue->queue);
1054
1055   if (head == NULL)
1056     return TRUE;
1057
1058   /* Only consider the queue empty if the minimum thresholds
1059    * are not reached and data is at the queue head. Otherwise
1060    * we would block forever on serialized queries.
1061    */
1062   if (!GST_IS_BUFFER (head->item) && !GST_IS_BUFFER_LIST (head->item))
1063     return FALSE;
1064
1065   /* It is possible that a max size is reached before all min thresholds are.
1066    * Therefore, only consider it empty if it is not filled. */
1067   return ((queue->min_threshold.buffers > 0 &&
1068           queue->cur_level.buffers < queue->min_threshold.buffers) ||
1069       (queue->min_threshold.bytes > 0 &&
1070           queue->cur_level.bytes < queue->min_threshold.bytes) ||
1071       (queue->min_threshold.time > 0 &&
1072           queue->cur_level.time < queue->min_threshold.time)) &&
1073       !gst_queue_is_filled (queue);
1074 }
1075
1076 static gboolean
1077 gst_queue_is_filled (GstQueue * queue)
1078 {
1079   return (((queue->max_size.buffers > 0 &&
1080               queue->cur_level.buffers >= queue->max_size.buffers) ||
1081           (queue->max_size.bytes > 0 &&
1082               queue->cur_level.bytes >= queue->max_size.bytes) ||
1083           (queue->max_size.time > 0 &&
1084               queue->cur_level.time >= queue->max_size.time)));
1085 }
1086
1087 static void
1088 gst_queue_leak_downstream (GstQueue * queue)
1089 {
1090   /* for as long as the queue is filled, dequeue an item and discard it */
1091   while (gst_queue_is_filled (queue)) {
1092     GstMiniObject *leak;
1093
1094     leak = gst_queue_locked_dequeue (queue);
1095     /* there is nothing to dequeue and the queue is still filled.. This should
1096      * not happen */
1097     g_assert (leak != NULL);
1098
1099     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1100         "queue is full, leaking item %p on downstream end", leak);
1101     if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
1102       GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1103           "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
1104       gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
1105     }
1106
1107     if (!GST_IS_QUERY (leak))
1108       gst_mini_object_unref (leak);
1109
1110     /* last buffer needs to get a DISCONT flag */
1111     queue->head_needs_discont = TRUE;
1112   }
1113 }
1114
1115 static gboolean
1116 discont_first_buffer (GstBuffer ** buffer, guint i, gpointer user_data)
1117 {
1118   GstQueue *queue = user_data;
1119   GstBuffer *subbuffer = gst_buffer_make_writable (*buffer);
1120
1121   if (subbuffer) {
1122     *buffer = subbuffer;
1123     GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
1124   } else {
1125     GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1126   }
1127
1128   return FALSE;
1129 }
1130
1131 static GstFlowReturn
1132 gst_queue_chain_buffer_or_list (GstPad * pad, GstObject * parent,
1133     GstMiniObject * obj, gboolean is_list)
1134 {
1135   GstQueue *queue;
1136
1137   queue = GST_QUEUE_CAST (parent);
1138
1139   /* we have to lock the queue since we span threads */
1140   GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1141   /* when we received EOS, we refuse any more data */
1142   if (queue->eos)
1143     goto out_eos;
1144   if (queue->unexpected)
1145     goto out_unexpected;
1146
1147   if (!is_list) {
1148     GstClockTime duration, timestamp;
1149     GstBuffer *buffer = GST_BUFFER_CAST (obj);
1150
1151     timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1152     duration = GST_BUFFER_DURATION (buffer);
1153
1154     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
1155         G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1156         GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1157         GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1158   } else {
1159     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1160         "received buffer list %p with %u buffers", obj,
1161         gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)));
1162   }
1163
1164   /* We make space available if we're "full" according to whatever
1165    * the user defined as "full". Note that this only applies to buffers.
1166    * We always handle events and they don't count in our statistics. */
1167   while (gst_queue_is_filled (queue)) {
1168     if (!queue->silent) {
1169       GST_QUEUE_MUTEX_UNLOCK (queue);
1170       g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
1171       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1172       /* we recheck, the signal could have changed the thresholds */
1173       if (!gst_queue_is_filled (queue))
1174         break;
1175     }
1176
1177     /* how are we going to make space for this buffer? */
1178     switch (queue->leaky) {
1179       case GST_QUEUE_LEAK_UPSTREAM:
1180         /* next buffer needs to get a DISCONT flag */
1181         queue->tail_needs_discont = TRUE;
1182         /* leak current buffer */
1183         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1184             "queue is full, leaking buffer on upstream end");
1185         /* now we can clean up and exit right away */
1186         goto out_unref;
1187       case GST_QUEUE_LEAK_DOWNSTREAM:
1188         gst_queue_leak_downstream (queue);
1189         break;
1190       default:
1191         g_warning ("Unknown leaky type, using default");
1192         /* fall-through */
1193       case GST_QUEUE_NO_LEAK:
1194       {
1195         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1196             "queue is full, waiting for free space");
1197
1198         /* don't leak. Instead, wait for space to be available */
1199         do {
1200           /* for as long as the queue is filled, wait till an item was deleted. */
1201           GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1202         } while (gst_queue_is_filled (queue));
1203
1204         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1205
1206         if (!queue->silent) {
1207           GST_QUEUE_MUTEX_UNLOCK (queue);
1208           g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1209           GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1210         }
1211         break;
1212       }
1213     }
1214   }
1215
1216   if (queue->tail_needs_discont) {
1217     if (!is_list) {
1218       GstBuffer *buffer = GST_BUFFER_CAST (obj);
1219       GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1220
1221       if (subbuffer) {
1222         buffer = subbuffer;
1223         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1224       } else {
1225         GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1226       }
1227
1228       obj = GST_MINI_OBJECT_CAST (buffer);
1229     } else {
1230       GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
1231
1232       buffer_list = gst_buffer_list_make_writable (buffer_list);
1233       gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1234       obj = GST_MINI_OBJECT_CAST (buffer_list);
1235     }
1236     queue->tail_needs_discont = FALSE;
1237   }
1238
1239   /* put buffer in queue now */
1240   if (is_list)
1241     gst_queue_locked_enqueue_buffer_list (queue, obj);
1242   else
1243     gst_queue_locked_enqueue_buffer (queue, obj);
1244   GST_QUEUE_MUTEX_UNLOCK (queue);
1245
1246   return GST_FLOW_OK;
1247
1248   /* special conditions */
1249 out_unref:
1250   {
1251     GST_QUEUE_MUTEX_UNLOCK (queue);
1252
1253     gst_mini_object_unref (obj);
1254
1255     return GST_FLOW_OK;
1256   }
1257 out_flushing:
1258   {
1259     GstFlowReturn ret = queue->srcresult;
1260
1261     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1262         "exit because task paused, reason: %s", gst_flow_get_name (ret));
1263     GST_QUEUE_MUTEX_UNLOCK (queue);
1264     gst_mini_object_unref (obj);
1265
1266     return ret;
1267   }
1268 out_eos:
1269   {
1270     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1271     GST_QUEUE_MUTEX_UNLOCK (queue);
1272
1273     gst_mini_object_unref (obj);
1274
1275     return GST_FLOW_EOS;
1276   }
1277 out_unexpected:
1278   {
1279     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1280     GST_QUEUE_MUTEX_UNLOCK (queue);
1281
1282     gst_mini_object_unref (obj);
1283
1284     return GST_FLOW_EOS;
1285   }
1286 }
1287
1288 static GstFlowReturn
1289 gst_queue_chain_list (GstPad * pad, GstObject * parent,
1290     GstBufferList * buffer_list)
1291 {
1292   return gst_queue_chain_buffer_or_list (pad, parent,
1293       GST_MINI_OBJECT_CAST (buffer_list), TRUE);
1294 }
1295
1296 static GstFlowReturn
1297 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1298 {
1299   return gst_queue_chain_buffer_or_list (pad, parent,
1300       GST_MINI_OBJECT_CAST (buffer), FALSE);
1301 }
1302
1303 /* dequeue an item from the queue an push it downstream. This functions returns
1304  * the result of the push. */
1305 static GstFlowReturn
1306 gst_queue_push_one (GstQueue * queue)
1307 {
1308   GstFlowReturn result = queue->srcresult;
1309   GstMiniObject *data;
1310   gboolean is_list;
1311
1312   data = gst_queue_locked_dequeue (queue);
1313   if (data == NULL)
1314     goto no_item;
1315
1316 next:
1317   is_list = GST_IS_BUFFER_LIST (data);
1318
1319   if (GST_IS_BUFFER (data) || is_list) {
1320     if (!is_list) {
1321       GstBuffer *buffer;
1322
1323       buffer = GST_BUFFER_CAST (data);
1324
1325       if (queue->head_needs_discont) {
1326         GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1327
1328         if (subbuffer) {
1329           buffer = subbuffer;
1330           GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1331         } else {
1332           GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1333         }
1334         queue->head_needs_discont = FALSE;
1335       }
1336
1337       GST_QUEUE_MUTEX_UNLOCK (queue);
1338       result = gst_pad_push (queue->srcpad, buffer);
1339     } else {
1340       GstBufferList *buffer_list;
1341
1342       buffer_list = GST_BUFFER_LIST_CAST (data);
1343
1344       if (queue->head_needs_discont) {
1345         buffer_list = gst_buffer_list_make_writable (buffer_list);
1346         gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1347         queue->head_needs_discont = FALSE;
1348       }
1349
1350       GST_QUEUE_MUTEX_UNLOCK (queue);
1351       result = gst_pad_push_list (queue->srcpad, buffer_list);
1352     }
1353
1354     /* need to check for srcresult here as well */
1355     GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1356
1357     if (result == GST_FLOW_EOS) {
1358       GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1359       /* stop pushing buffers, we dequeue all items until we see an item that we
1360        * can push again, which is EOS or SEGMENT. If there is nothing in the
1361        * queue we can push, we set a flag to make the sinkpad refuse more
1362        * buffers with an EOS return value. */
1363       while ((data = gst_queue_locked_dequeue (queue))) {
1364         if (GST_IS_BUFFER (data)) {
1365           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1366               "dropping EOS buffer %p", data);
1367           gst_buffer_unref (GST_BUFFER_CAST (data));
1368         } else if (GST_IS_BUFFER_LIST (data)) {
1369           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1370               "dropping EOS buffer list %p", data);
1371           gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
1372         } else if (GST_IS_EVENT (data)) {
1373           GstEvent *event = GST_EVENT_CAST (data);
1374           GstEventType type = GST_EVENT_TYPE (event);
1375
1376           if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
1377             /* we found a pushable item in the queue, push it out */
1378             GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1379                 "pushing pushable event %s after EOS",
1380                 GST_EVENT_TYPE_NAME (event));
1381             goto next;
1382           }
1383           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1384               "dropping EOS event %p", event);
1385           gst_event_unref (event);
1386         } else if (GST_IS_QUERY (data)) {
1387           GstQuery *query = GST_QUERY_CAST (data);
1388
1389           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1390               "dropping query %p because of EOS", query);
1391           queue->last_query = FALSE;
1392           g_cond_signal (&queue->query_handled);
1393         }
1394       }
1395       /* no more items in the queue. Set the unexpected flag so that upstream
1396        * make us refuse any more buffers on the sinkpad. Since we will still
1397        * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1398        * task function does not shut down. */
1399       queue->unexpected = TRUE;
1400       result = GST_FLOW_OK;
1401     }
1402   } else if (GST_IS_EVENT (data)) {
1403     GstEvent *event = GST_EVENT_CAST (data);
1404     GstEventType type = GST_EVENT_TYPE (event);
1405
1406     GST_QUEUE_MUTEX_UNLOCK (queue);
1407
1408     gst_pad_push_event (queue->srcpad, event);
1409
1410     GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1411     /* if we're EOS, return EOS so that the task pauses. */
1412     if (type == GST_EVENT_EOS) {
1413       GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1414           "pushed EOS event %p, return EOS", event);
1415       result = GST_FLOW_EOS;
1416     }
1417   } else if (GST_IS_QUERY (data)) {
1418     GstQuery *query = GST_QUERY_CAST (data);
1419     gboolean ret;
1420
1421     GST_QUEUE_MUTEX_UNLOCK (queue);
1422     ret = gst_pad_peer_query (queue->srcpad, query);
1423     GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1424     queue->last_query = ret;
1425     g_cond_signal (&queue->query_handled);
1426     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1427         "did query %p, return %d", query, queue->last_query);
1428   }
1429   return result;
1430
1431   /* ERRORS */
1432 no_item:
1433   {
1434     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1435         "exit because we have no item in the queue");
1436     return GST_FLOW_ERROR;
1437   }
1438 out_flushing:
1439   {
1440     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1441     return GST_FLOW_FLUSHING;
1442   }
1443 out_flushing_query:
1444   {
1445     queue->last_query = FALSE;
1446     g_cond_signal (&queue->query_handled);
1447     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1448     return GST_FLOW_FLUSHING;
1449   }
1450 }
1451
1452 static void
1453 gst_queue_loop (GstPad * pad)
1454 {
1455   GstQueue *queue;
1456   GstFlowReturn ret;
1457
1458   queue = (GstQueue *) GST_PAD_PARENT (pad);
1459
1460   /* have to lock for thread-safety */
1461   GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1462
1463   while (gst_queue_is_empty (queue)) {
1464     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1465     if (!queue->silent) {
1466       GST_QUEUE_MUTEX_UNLOCK (queue);
1467       g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1468       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1469     }
1470
1471     /* we recheck, the signal could have changed the thresholds */
1472     while (gst_queue_is_empty (queue)) {
1473       GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1474     }
1475
1476     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1477     if (!queue->silent) {
1478       GST_QUEUE_MUTEX_UNLOCK (queue);
1479       g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1480       g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1481       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1482     }
1483   }
1484
1485   ret = gst_queue_push_one (queue);
1486   queue->srcresult = ret;
1487   if (ret != GST_FLOW_OK)
1488     goto out_flushing;
1489
1490   GST_QUEUE_MUTEX_UNLOCK (queue);
1491
1492   return;
1493
1494   /* ERRORS */
1495 out_flushing:
1496   {
1497     gboolean eos = queue->eos;
1498     GstFlowReturn ret = queue->srcresult;
1499
1500     gst_pad_pause_task (queue->srcpad);
1501     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1502         "pause task, reason:  %s", gst_flow_get_name (ret));
1503     if (ret == GST_FLOW_FLUSHING) {
1504       gst_queue_locked_flush (queue, FALSE);
1505     } else {
1506       GST_QUEUE_SIGNAL_DEL (queue);
1507       queue->last_query = FALSE;
1508       g_cond_signal (&queue->query_handled);
1509     }
1510     GST_QUEUE_MUTEX_UNLOCK (queue);
1511     /* let app know about us giving up if upstream is not expected to do so */
1512     /* EOS is already taken care of elsewhere */
1513     if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1514       GST_ELEMENT_ERROR (queue, STREAM, FAILED,
1515           (_("Internal data flow error.")),
1516           ("streaming task paused, reason %s (%d)",
1517               gst_flow_get_name (ret), ret));
1518       gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1519     }
1520     return;
1521   }
1522 }
1523
1524 static gboolean
1525 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1526 {
1527   gboolean res = TRUE;
1528   GstQueue *queue = GST_QUEUE (parent);
1529
1530 #ifndef GST_DISABLE_GST_DEBUG
1531   GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1532       event, GST_EVENT_TYPE (event));
1533 #endif
1534
1535   switch (GST_EVENT_TYPE (event)) {
1536     case GST_EVENT_RECONFIGURE:
1537       GST_QUEUE_MUTEX_LOCK (queue);
1538       if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1539         /* when we got not linked, assume downstream is linked again now and we
1540          * can try to start pushing again */
1541         queue->srcresult = GST_FLOW_OK;
1542         gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1543       }
1544       GST_QUEUE_MUTEX_UNLOCK (queue);
1545
1546       res = gst_pad_push_event (queue->sinkpad, event);
1547       break;
1548     default:
1549       res = gst_pad_event_default (pad, parent, event);
1550       break;
1551   }
1552
1553
1554   return res;
1555 }
1556
1557 static gboolean
1558 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1559 {
1560   GstQueue *queue = GST_QUEUE (parent);
1561   gboolean res;
1562
1563   switch (GST_QUERY_TYPE (query)) {
1564     case GST_QUERY_SCHEDULING:{
1565       gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1566       res = TRUE;
1567       break;
1568     }
1569     default:
1570       res = gst_pad_query_default (pad, parent, query);
1571       break;
1572   }
1573
1574   if (!res)
1575     return FALSE;
1576
1577   /* Adjust peer response for data contained in queue */
1578   switch (GST_QUERY_TYPE (query)) {
1579     case GST_QUERY_POSITION:
1580     {
1581       gint64 peer_pos;
1582       GstFormat format;
1583
1584       /* get peer position */
1585       gst_query_parse_position (query, &format, &peer_pos);
1586
1587       /* FIXME: this code assumes that there's no discont in the queue */
1588       switch (format) {
1589         case GST_FORMAT_BYTES:
1590           peer_pos -= queue->cur_level.bytes;
1591           break;
1592         case GST_FORMAT_TIME:
1593           peer_pos -= queue->cur_level.time;
1594           break;
1595         default:
1596           GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1597               "know how to adjust value", gst_format_get_name (format));
1598           return TRUE;
1599       }
1600       /* set updated position */
1601       gst_query_set_position (query, format, peer_pos);
1602       break;
1603     }
1604     case GST_QUERY_LATENCY:
1605     {
1606       gboolean live;
1607       GstClockTime min, max;
1608
1609       gst_query_parse_latency (query, &live, &min, &max);
1610
1611       /* we can delay up to the limit of the queue in time. If we have no time
1612        * limit, the best thing we can do is to return an infinite delay. In
1613        * reality a better estimate would be the byte/buffer rate but that is not
1614        * possible right now. */
1615       /* TODO: Use CONVERT query? */
1616       if (queue->max_size.time > 0 && max != -1
1617           && queue->leaky == GST_QUEUE_NO_LEAK)
1618         max += queue->max_size.time;
1619       else if (queue->max_size.time > 0 && queue->leaky != GST_QUEUE_NO_LEAK)
1620         max = MIN (queue->max_size.time, max);
1621       else
1622         max = -1;
1623
1624       /* adjust for min-threshold */
1625       if (queue->min_threshold.time > 0)
1626         min += queue->min_threshold.time;
1627
1628       gst_query_set_latency (query, live, min, max);
1629       break;
1630     }
1631     default:
1632       /* peer handled other queries */
1633       break;
1634   }
1635
1636   return TRUE;
1637 }
1638
1639 static gboolean
1640 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1641     gboolean active)
1642 {
1643   gboolean result;
1644   GstQueue *queue;
1645
1646   queue = GST_QUEUE (parent);
1647
1648   switch (mode) {
1649     case GST_PAD_MODE_PUSH:
1650       if (active) {
1651         GST_QUEUE_MUTEX_LOCK (queue);
1652         queue->srcresult = GST_FLOW_OK;
1653         queue->eos = FALSE;
1654         queue->unexpected = FALSE;
1655         GST_QUEUE_MUTEX_UNLOCK (queue);
1656       } else {
1657         /* step 1, unblock chain function */
1658         GST_QUEUE_MUTEX_LOCK (queue);
1659         queue->srcresult = GST_FLOW_FLUSHING;
1660         /* the item del signal will unblock */
1661         g_cond_signal (&queue->item_del);
1662         /* unblock query handler */
1663         queue->last_query = FALSE;
1664         g_cond_signal (&queue->query_handled);
1665         GST_QUEUE_MUTEX_UNLOCK (queue);
1666
1667         /* step 2, wait until streaming thread stopped and flush queue */
1668         GST_PAD_STREAM_LOCK (pad);
1669         GST_QUEUE_MUTEX_LOCK (queue);
1670         gst_queue_locked_flush (queue, TRUE);
1671         GST_QUEUE_MUTEX_UNLOCK (queue);
1672         GST_PAD_STREAM_UNLOCK (pad);
1673       }
1674       result = TRUE;
1675       break;
1676     default:
1677       result = FALSE;
1678       break;
1679   }
1680   return result;
1681 }
1682
1683 static gboolean
1684 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1685     gboolean active)
1686 {
1687   gboolean result;
1688   GstQueue *queue;
1689
1690   queue = GST_QUEUE (parent);
1691
1692   switch (mode) {
1693     case GST_PAD_MODE_PUSH:
1694       if (active) {
1695         GST_QUEUE_MUTEX_LOCK (queue);
1696         queue->srcresult = GST_FLOW_OK;
1697         queue->eos = FALSE;
1698         queue->unexpected = FALSE;
1699         result =
1700             gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1701             NULL);
1702         GST_QUEUE_MUTEX_UNLOCK (queue);
1703       } else {
1704         /* step 1, unblock loop function */
1705         GST_QUEUE_MUTEX_LOCK (queue);
1706         queue->srcresult = GST_FLOW_FLUSHING;
1707         /* the item add signal will unblock */
1708         g_cond_signal (&queue->item_add);
1709         GST_QUEUE_MUTEX_UNLOCK (queue);
1710
1711         /* step 2, make sure streaming finishes */
1712         result = gst_pad_stop_task (pad);
1713       }
1714       break;
1715     default:
1716       result = FALSE;
1717       break;
1718   }
1719   return result;
1720 }
1721
1722 static void
1723 queue_capacity_change (GstQueue * queue)
1724 {
1725   if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1726     gst_queue_leak_downstream (queue);
1727   }
1728
1729   /* changing the capacity of the queue must wake up
1730    * the _chain function, it might have more room now
1731    * to store the buffer/event in the queue */
1732   GST_QUEUE_SIGNAL_DEL (queue);
1733 }
1734
1735 /* Changing the minimum required fill level must
1736  * wake up the _loop function as it might now
1737  * be able to preceed.
1738  */
1739 #define QUEUE_THRESHOLD_CHANGE(q)\
1740   GST_QUEUE_SIGNAL_ADD (q);
1741
1742 static void
1743 gst_queue_set_property (GObject * object,
1744     guint prop_id, const GValue * value, GParamSpec * pspec)
1745 {
1746   GstQueue *queue = GST_QUEUE (object);
1747
1748   /* someone could change levels here, and since this
1749    * affects the get/put funcs, we need to lock for safety. */
1750   GST_QUEUE_MUTEX_LOCK (queue);
1751
1752   switch (prop_id) {
1753     case PROP_MAX_SIZE_BYTES:
1754       queue->max_size.bytes = g_value_get_uint (value);
1755       queue_capacity_change (queue);
1756       break;
1757     case PROP_MAX_SIZE_BUFFERS:
1758       queue->max_size.buffers = g_value_get_uint (value);
1759       queue_capacity_change (queue);
1760       break;
1761     case PROP_MAX_SIZE_TIME:
1762       queue->max_size.time = g_value_get_uint64 (value);
1763       queue_capacity_change (queue);
1764       break;
1765     case PROP_MIN_THRESHOLD_BYTES:
1766       queue->min_threshold.bytes = g_value_get_uint (value);
1767       queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1768       QUEUE_THRESHOLD_CHANGE (queue);
1769       break;
1770     case PROP_MIN_THRESHOLD_BUFFERS:
1771       queue->min_threshold.buffers = g_value_get_uint (value);
1772       queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1773       QUEUE_THRESHOLD_CHANGE (queue);
1774       break;
1775     case PROP_MIN_THRESHOLD_TIME:
1776       queue->min_threshold.time = g_value_get_uint64 (value);
1777       queue->orig_min_threshold.time = queue->min_threshold.time;
1778       QUEUE_THRESHOLD_CHANGE (queue);
1779       break;
1780     case PROP_LEAKY:
1781       queue->leaky = g_value_get_enum (value);
1782       break;
1783     case PROP_SILENT:
1784       queue->silent = g_value_get_boolean (value);
1785       break;
1786     case PROP_FLUSH_ON_EOS:
1787       queue->flush_on_eos = g_value_get_boolean (value);
1788       break;
1789     default:
1790       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1791       break;
1792   }
1793
1794   GST_QUEUE_MUTEX_UNLOCK (queue);
1795 }
1796
1797 static void
1798 gst_queue_get_property (GObject * object,
1799     guint prop_id, GValue * value, GParamSpec * pspec)
1800 {
1801   GstQueue *queue = GST_QUEUE (object);
1802
1803   GST_QUEUE_MUTEX_LOCK (queue);
1804
1805   switch (prop_id) {
1806     case PROP_CUR_LEVEL_BYTES:
1807       g_value_set_uint (value, queue->cur_level.bytes);
1808       break;
1809     case PROP_CUR_LEVEL_BUFFERS:
1810       g_value_set_uint (value, queue->cur_level.buffers);
1811       break;
1812     case PROP_CUR_LEVEL_TIME:
1813       g_value_set_uint64 (value, queue->cur_level.time);
1814       break;
1815     case PROP_MAX_SIZE_BYTES:
1816       g_value_set_uint (value, queue->max_size.bytes);
1817       break;
1818     case PROP_MAX_SIZE_BUFFERS:
1819       g_value_set_uint (value, queue->max_size.buffers);
1820       break;
1821     case PROP_MAX_SIZE_TIME:
1822       g_value_set_uint64 (value, queue->max_size.time);
1823       break;
1824     case PROP_MIN_THRESHOLD_BYTES:
1825       g_value_set_uint (value, queue->min_threshold.bytes);
1826       break;
1827     case PROP_MIN_THRESHOLD_BUFFERS:
1828       g_value_set_uint (value, queue->min_threshold.buffers);
1829       break;
1830     case PROP_MIN_THRESHOLD_TIME:
1831       g_value_set_uint64 (value, queue->min_threshold.time);
1832       break;
1833     case PROP_LEAKY:
1834       g_value_set_enum (value, queue->leaky);
1835       break;
1836     case PROP_SILENT:
1837       g_value_set_boolean (value, queue->silent);
1838       break;
1839     case PROP_FLUSH_ON_EOS:
1840       g_value_set_boolean (value, queue->flush_on_eos);
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 }