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