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