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