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