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>
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.
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.
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.
26 * SECTION:element-queue
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
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
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.
45 * The default queue size limits are 200 buffers, 10MB of data, or
46 * one second worth of data, whichever is reached first.
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.
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.
59 #include "gst/gst_private.h"
63 #include "gstcoreelementselements.h"
65 #include "../../gst/gst-i18n-lib.h"
66 #include "../../gst/glib-compat-private.h"
68 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
73 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
78 GST_DEBUG_CATEGORY_STATIC (queue_debug);
79 #define GST_CAT_DEFAULT (queue_debug)
80 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
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))
99 /* Queue signals and args */
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,
117 PROP_MAX_SIZE_BUFFERS,
120 PROP_MIN_THRESHOLD_BUFFERS,
121 PROP_MIN_THRESHOLD_BYTES,
122 PROP_MIN_THRESHOLD_TIME,
125 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
127 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
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 */
136 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
137 g_mutex_lock (&q->qlock); \
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) \
146 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
147 g_mutex_unlock (&q->qlock); \
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"); \
159 STATUS (q, q->sinkpad, "received DEL"); \
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"); \
171 STATUS (q, q->srcpad, "received ADD"); \
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); \
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); \
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);
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);
202 static GstFlowReturn gst_queue_chain (GstPad * pad, GstObject * parent,
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);
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,
214 static gboolean gst_queue_handle_src_event (GstPad * pad, GstObject * parent,
216 static gboolean gst_queue_handle_src_query (GstPad * pad, GstObject * parent,
219 static void gst_queue_locked_flush (GstQueue * queue, gboolean full);
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);
226 static gboolean gst_queue_is_empty (GstQueue * queue);
227 static gboolean gst_queue_is_filled (GstQueue * queue);
237 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
240 queue_leaky_get_type (void)
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)",
251 if (!queue_leaky_type) {
252 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
254 return queue_leaky_type;
257 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
260 gst_queue_class_init (GstQueueClass * klass)
262 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
263 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
265 gobject_class->set_property = gst_queue_set_property;
266 gobject_class->get_property = gst_queue_get_property;
270 * GstQueue::underrun:
271 * @queue: the queue instance
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.
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);
284 * @queue: the queue instance
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.
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);
296 * @queue: the queue instance
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.
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);
309 * @queue: the queue instance
311 * Reports when the queue has enough data to start pushing data again on the
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);
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));
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));
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)",
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,
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)",
368 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
369 G_PARAM_STATIC_STRINGS));
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));
381 * Don't emit queue signals. Makes queues more lightweight if no signals are
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));
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 */
398 * queue:flush-on-eos:
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).
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).
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));
418 gobject_class->finalize = gst_queue_finalize;
420 gst_element_class_set_static_metadata (gstelement_class,
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);
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);
435 gst_type_mark_as_plugin_api (GST_TYPE_QUEUE_LEAKY, 0);
439 gst_queue_init (GstQueue * queue)
441 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
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);
452 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
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);
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;
471 queue->leaky = GST_QUEUE_NO_LEAK;
472 queue->srcresult = GST_FLOW_FLUSHING;
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);
480 gst_queue_array_new_for_struct (sizeof (GstQueueItem),
481 DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
483 queue->sinktime = GST_CLOCK_STIME_NONE;
484 queue->srctime = GST_CLOCK_STIME_NONE;
486 queue->sink_tainted = TRUE;
487 queue->src_tainted = TRUE;
489 queue->newseg_applied_to_src = FALSE;
491 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
492 queue->empty_buffers = FALSE;
493 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
495 GST_DEBUG_OBJECT (queue,
496 "initialized queue's not_empty & not_full conditions");
499 /* called only once, as opposed to dispose */
501 gst_queue_finalize (GObject * object)
503 GstQueue *queue = GST_QUEUE (object);
506 GST_DEBUG_OBJECT (queue, "finalizing queue");
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);
513 gst_queue_array_free (queue->queue);
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);
520 G_OBJECT_CLASS (parent_class)->finalize (object);
523 /* Convenience function */
524 static inline GstClockTimeDiff
525 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
527 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
529 if (GST_CLOCK_TIME_IS_VALID (val)) {
531 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
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. */
543 update_time_level (GstQueue * queue)
545 gint64 sink_time, src_time;
547 if (queue->sink_tainted) {
548 GST_LOG_OBJECT (queue, "update sink time");
550 my_segment_to_running_time (&queue->sink_segment,
551 queue->sink_segment.position);
552 queue->sink_tainted = FALSE;
554 sink_time = queue->sinktime;
556 if (queue->src_tainted) {
557 GST_LOG_OBJECT (queue, "update src time");
559 my_segment_to_running_time (&queue->src_segment,
560 queue->src_segment.position);
561 queue->src_tainted = FALSE;
563 src_time = queue->srctime;
565 GST_LOG_OBJECT (queue, "sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT,
566 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
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;
572 queue->cur_level.time = 0;
575 /* take a SEGMENT event and apply the values to segment, updating the time
578 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
581 gst_event_copy_segment (event, segment);
583 /* now configure the values, we use these to track timestamps on the
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;
594 queue->sink_tainted = TRUE;
596 queue->src_tainted = TRUE;
598 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
600 /* segment can update the time level of the queue */
601 update_time_level (queue);
605 apply_gap (GstQueue * queue, GstEvent * event,
606 GstSegment * segment, gboolean is_sink)
608 GstClockTime timestamp;
609 GstClockTime duration;
611 gst_event_parse_gap (event, ×tamp, &duration);
613 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
615 if (GST_CLOCK_TIME_IS_VALID (duration)) {
616 timestamp += duration;
619 segment->position = timestamp;
622 queue->sink_tainted = TRUE;
624 queue->src_tainted = TRUE;
626 /* calc diff with other end */
627 update_time_level (queue);
632 /* take a buffer and update segment, updating the time level of the queue. */
634 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
637 GstClockTime duration, timestamp;
639 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
640 duration = GST_BUFFER_DURATION (buffer);
642 /* if no timestamp is set, assume it's continuous with the previous
644 if (timestamp == GST_CLOCK_TIME_NONE)
645 timestamp = segment->position;
648 if (duration != GST_CLOCK_TIME_NONE)
649 timestamp += duration;
651 GST_LOG_OBJECT (queue, "%s position updated to %" GST_TIME_FORMAT,
652 segment == &queue->sink_segment ? "sink" : "src",
653 GST_TIME_ARGS (timestamp));
655 segment->position = timestamp;
657 queue->sink_tainted = TRUE;
659 queue->src_tainted = TRUE;
662 /* calc diff with other end */
663 update_time_level (queue);
667 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer user_data)
669 GstClockTime *timestamp = user_data;
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)));
677 btime = GST_BUFFER_DTS_OR_PTS (*buf);
678 if (GST_CLOCK_TIME_IS_VALID (btime))
681 if (GST_BUFFER_DURATION_IS_VALID (*buf))
682 *timestamp += GST_BUFFER_DURATION (*buf);
684 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
689 /* take a buffer list and update segment, updating the time level of the queue */
691 apply_buffer_list (GstQueue * queue, GstBufferList * buffer_list,
692 GstSegment * segment, gboolean sink)
694 GstClockTime timestamp;
696 /* if no timestamp is set, assume it's continuous with the previous time */
697 timestamp = segment->position;
699 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
701 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
702 GST_TIME_ARGS (timestamp));
704 segment->position = timestamp;
707 queue->sink_tainted = TRUE;
709 queue->src_tainted = TRUE;
711 /* calc diff with other end */
712 update_time_level (queue);
716 gst_queue_locked_flush (GstQueue * queue, gboolean full)
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));
729 if (!qitem->is_query)
730 gst_mini_object_unref (qitem->item);
731 memset (qitem, 0, sizeof (GstQueueItem));
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;
743 queue->sinktime = queue->srctime = GST_CLOCK_STIME_NONE;
744 queue->sink_tainted = queue->src_tainted = TRUE;
746 /* we deleted a lot of something */
747 GST_QUEUE_SIGNAL_DEL (queue);
750 /* enqueue an item an update the level stats, with QUEUE_LOCK */
752 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
755 GstBuffer *buffer = GST_BUFFER_CAST (item);
756 gsize bsize = gst_buffer_get_size (buffer);
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);
764 qitem.is_query = FALSE;
766 gst_queue_array_push_tail_struct (queue->queue, &qitem);
767 GST_QUEUE_SIGNAL_ADD (queue);
771 gst_queue_locked_enqueue_buffer_list (GstQueue * queue, gpointer item)
774 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
777 bsize = gst_buffer_list_calculate_size (buffer_list);
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);
785 qitem.is_query = FALSE;
787 gst_queue_array_push_tail_struct (queue->queue, &qitem);
788 GST_QUEUE_SIGNAL_ADD (queue);
792 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
795 GstEvent *event = GST_EVENT_CAST (item);
797 switch (GST_EVENT_TYPE (event)) {
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);
805 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
806 /* mark the queue as EOS. This prevents us from accepting more data. */
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;
817 /* a new segment allows us to accept more buffers if we got EOS
819 queue->unexpected = FALSE;
822 apply_gap (queue, event, &queue->sink_segment, TRUE);
829 qitem.is_query = FALSE;
831 gst_queue_array_push_tail_struct (queue->queue, &qitem);
832 GST_QUEUE_SIGNAL_ADD (queue);
835 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
836 static GstMiniObject *
837 gst_queue_locked_dequeue (GstQueue * queue)
843 qitem = gst_queue_array_pop_head_struct (queue->queue);
848 bufsize = qitem->size;
850 if (GST_IS_BUFFER (item)) {
851 GstBuffer *buffer = GST_BUFFER_CAST (item);
853 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
854 "retrieved buffer %p from queue", buffer);
856 queue->cur_level.buffers--;
857 queue->cur_level.bytes -= bufsize;
858 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
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);
866 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
867 "retrieved buffer list %p from queue", buffer_list);
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);
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);
879 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
880 "retrieved event %p from queue", event);
882 switch (GST_EVENT_TYPE (event)) {
884 /* queue is empty now that we dequeued the EOS */
885 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
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);
892 queue->newseg_applied_to_src = FALSE;
896 apply_gap (queue, event, &queue->src_segment, FALSE);
901 } else if (GST_IS_QUERY (item)) {
902 GstQuery *query = GST_QUERY_CAST (item);
904 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
905 "retrieved query %p from queue", query);
908 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
909 item, GST_OBJECT_NAME (queue));
912 GST_QUEUE_SIGNAL_DEL (queue);
919 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
925 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
930 queue = GST_QUEUE (parent);
932 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Received event '%s'",
933 GST_EVENT_TYPE_NAME (event));
935 switch (GST_EVENT_TYPE (event)) {
936 case GST_EVENT_FLUSH_START:
938 ret = gst_pad_push_event (queue->srcpad, event);
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);
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");
953 /* unblock query handler after the streaming thread is shut down.
954 * Otherwise downstream might have a query that is already unreffed
956 GST_QUEUE_MUTEX_LOCK (queue);
957 queue->last_query = FALSE;
958 g_cond_signal (&queue->query_handled);
959 GST_QUEUE_MUTEX_UNLOCK (queue);
961 case GST_EVENT_FLUSH_STOP:
963 ret = gst_pad_push_event (queue->srcpad, event);
965 GST_QUEUE_MUTEX_LOCK (queue);
966 gst_queue_locked_flush (queue, FALSE);
967 queue->srcresult = GST_FLOW_OK;
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);
974 GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
975 "pad not active any longer");
977 GST_QUEUE_MUTEX_UNLOCK (queue);
979 STATUS (queue, pad, "after flush");
982 if (GST_EVENT_IS_SERIALIZED (event)) {
983 /* serialized events go in the queue */
984 GST_QUEUE_MUTEX_LOCK (queue);
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;
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.
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);
1008 GST_QUEUE_MUTEX_UNLOCK (queue);
1010 goto out_flow_error;
1014 /* refuse more events on EOS unless they unset the EOS status */
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;
1023 queue->unexpected = FALSE;
1024 gst_pad_start_task (queue->srcpad,
1025 (GstTaskFunction) gst_queue_loop, queue->srcpad, NULL);
1028 queue->unexpected = FALSE;
1037 gst_queue_locked_enqueue_event (queue, event);
1038 GST_QUEUE_MUTEX_UNLOCK (queue);
1040 /* non-serialized events are forwarded downstream immediately */
1041 ret = gst_pad_push_event (queue->srcpad, event);
1046 GST_ERROR_OBJECT (queue, "Failed to push event");
1047 return GST_FLOW_ERROR;
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;
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;
1070 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1072 GstQueue *queue = GST_QUEUE_CAST (parent);
1075 switch (GST_QUERY_TYPE (query)) {
1077 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
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;
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)
1094 res = queue->last_query;
1095 GST_QUEUE_MUTEX_UNLOCK (queue);
1097 res = gst_pad_query_default (pad, parent, query);
1106 GST_DEBUG_OBJECT (queue, "we are flushing");
1107 GST_QUEUE_MUTEX_UNLOCK (queue);
1113 gst_queue_is_empty (GstQueue * queue)
1117 tail = gst_queue_array_peek_tail_struct (queue->queue);
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.
1126 if (!GST_IS_BUFFER (tail->item) && !GST_IS_BUFFER_LIST (tail->item))
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);
1141 gst_queue_is_filled (GstQueue * queue)
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)));
1152 gst_queue_leak_downstream (GstQueue * queue)
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;
1158 leak = gst_queue_locked_dequeue (queue);
1159 /* there is nothing to dequeue and the queue is still filled.. This should
1161 g_assert (leak != NULL);
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));
1171 if (!GST_IS_QUERY (leak))
1172 gst_mini_object_unref (leak);
1174 /* last buffer needs to get a DISCONT flag */
1175 queue->head_needs_discont = TRUE;
1180 discont_first_buffer (GstBuffer ** buffer, guint i, gpointer user_data)
1182 GstQueue *queue = user_data;
1183 GstBuffer *subbuffer = gst_buffer_make_writable (*buffer);
1186 *buffer = subbuffer;
1187 GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
1189 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1195 static GstFlowReturn
1196 gst_queue_chain_buffer_or_list (GstPad * pad, GstObject * parent,
1197 GstMiniObject * obj, gboolean is_list)
1201 queue = GST_QUEUE_CAST (parent);
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 */
1208 if (queue->unexpected)
1209 goto out_unexpected;
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);
1219 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
1222 GstClockTime duration, timestamp;
1223 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1225 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1226 duration = GST_BUFFER_DURATION (buffer);
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));
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)));
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))
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 */
1261 case GST_QUEUE_LEAK_DOWNSTREAM:
1262 gst_queue_leak_downstream (queue);
1265 g_warning ("Unknown leaky type, using default");
1267 case GST_QUEUE_NO_LEAK:
1269 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1270 "queue is full, waiting for free space");
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);
1278 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
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);
1290 if (queue->tail_needs_discont) {
1292 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1293 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1297 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1299 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1302 obj = GST_MINI_OBJECT_CAST (buffer);
1304 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
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);
1310 queue->tail_needs_discont = FALSE;
1313 /* put buffer in queue now */
1315 gst_queue_locked_enqueue_buffer_list (queue, obj);
1317 gst_queue_locked_enqueue_buffer (queue, obj);
1318 GST_QUEUE_MUTEX_UNLOCK (queue);
1322 /* special conditions */
1325 GST_QUEUE_MUTEX_UNLOCK (queue);
1327 gst_mini_object_unref (obj);
1333 GstFlowReturn ret = queue->srcresult;
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);
1344 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1345 GST_QUEUE_MUTEX_UNLOCK (queue);
1347 gst_mini_object_unref (obj);
1349 return GST_FLOW_EOS;
1353 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1354 GST_QUEUE_MUTEX_UNLOCK (queue);
1356 gst_mini_object_unref (obj);
1358 return GST_FLOW_EOS;
1362 static GstFlowReturn
1363 gst_queue_chain_list (GstPad * pad, GstObject * parent,
1364 GstBufferList * buffer_list)
1366 return gst_queue_chain_buffer_or_list (pad, parent,
1367 GST_MINI_OBJECT_CAST (buffer_list), TRUE);
1370 static GstFlowReturn
1371 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1373 return gst_queue_chain_buffer_or_list (pad, parent,
1374 GST_MINI_OBJECT_CAST (buffer), FALSE);
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)
1382 GstFlowReturn result = queue->srcresult;
1383 GstMiniObject *data;
1386 data = gst_queue_locked_dequeue (queue);
1391 is_list = GST_IS_BUFFER_LIST (data);
1393 if (GST_IS_BUFFER (data) || is_list) {
1397 buffer = GST_BUFFER_CAST (data);
1399 if (queue->head_needs_discont) {
1400 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1404 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1406 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1408 queue->head_needs_discont = FALSE;
1411 GST_QUEUE_MUTEX_UNLOCK (queue);
1412 result = gst_pad_push (queue->srcpad, buffer);
1414 GstBufferList *buffer_list;
1416 buffer_list = GST_BUFFER_LIST_CAST (data);
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;
1424 GST_QUEUE_MUTEX_UNLOCK (queue);
1425 result = gst_pad_push_list (queue->srcpad, buffer_list);
1428 /* need to check for srcresult here as well */
1429 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
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);
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));
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);
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);
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;
1477 } else if (GST_IS_EVENT (data)) {
1478 GstEvent *event = GST_EVENT_CAST (data);
1479 GstEventType type = GST_EVENT_TYPE (event);
1481 GST_QUEUE_MUTEX_UNLOCK (queue);
1483 gst_pad_push_event (queue->srcpad, event);
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;
1492 } else if (GST_IS_QUERY (data)) {
1493 GstQuery *query = GST_QUERY_CAST (data);
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);
1510 GST_CAT_ERROR_OBJECT (queue_dataflow, queue,
1511 "exit because we have no item in the queue");
1512 return GST_FLOW_ERROR;
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));
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));
1533 gst_queue_loop (GstPad * pad)
1538 queue = (GstQueue *) GST_PAD_PARENT (pad);
1540 /* have to lock for thread-safety */
1541 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
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);
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);
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);
1565 ret = gst_queue_push_one (queue);
1566 queue->srcresult = ret;
1567 if (ret != GST_FLOW_OK)
1570 GST_QUEUE_MUTEX_UNLOCK (queue);
1577 gboolean eos = queue->eos;
1578 GstFlowReturn ret = queue->srcresult;
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);
1586 GST_QUEUE_SIGNAL_DEL (queue);
1587 queue->last_query = FALSE;
1588 g_cond_signal (&queue->query_handled);
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 ());
1602 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1604 gboolean res = TRUE;
1605 GstQueue *queue = GST_QUEUE (parent);
1607 #ifndef GST_DISABLE_GST_DEBUG
1608 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1609 event, GST_EVENT_TYPE (event));
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);
1621 GST_QUEUE_MUTEX_UNLOCK (queue);
1623 res = gst_pad_push_event (queue->sinkpad, event);
1626 res = gst_pad_event_default (pad, parent, event);
1635 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1637 GstQueue *queue = GST_QUEUE (parent);
1640 switch (GST_QUERY_TYPE (query)) {
1641 case GST_QUERY_SCHEDULING:{
1642 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1647 res = gst_pad_query_default (pad, parent, query);
1654 /* Adjust peer response for data contained in queue */
1655 switch (GST_QUERY_TYPE (query)) {
1656 case GST_QUERY_POSITION:
1661 /* get peer position */
1662 gst_query_parse_position (query, &format, &peer_pos);
1664 /* FIXME: this code assumes that there's no discont in the queue */
1666 case GST_FORMAT_BYTES:
1667 peer_pos -= queue->cur_level.bytes;
1668 if (peer_pos < 0) /* Clamp result to 0 */
1671 case GST_FORMAT_TIME:
1672 peer_pos -= queue->cur_level.time;
1673 if (peer_pos < 0) /* Clamp result to 0 */
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));
1681 /* set updated position */
1682 gst_query_set_position (query, format, peer_pos);
1685 case GST_QUERY_LATENCY:
1688 GstClockTime min, max;
1690 gst_query_parse_latency (query, &live, &min, &max);
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);
1705 /* adjust for min-threshold */
1706 if (queue->min_threshold.time > 0)
1707 min += queue->min_threshold.time;
1709 gst_query_set_latency (query, live, min, max);
1713 /* peer handled other queries */
1721 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1727 queue = GST_QUEUE (parent);
1730 case GST_PAD_MODE_PUSH:
1732 GST_QUEUE_MUTEX_LOCK (queue);
1733 queue->srcresult = GST_FLOW_OK;
1735 queue->unexpected = FALSE;
1736 GST_QUEUE_MUTEX_UNLOCK (queue);
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);
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);
1762 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1768 queue = GST_QUEUE (parent);
1771 case GST_PAD_MODE_PUSH:
1773 GST_QUEUE_MUTEX_LOCK (queue);
1774 queue->srcresult = GST_FLOW_OK;
1776 queue->unexpected = FALSE;
1778 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1780 GST_QUEUE_MUTEX_UNLOCK (queue);
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);
1789 /* step 2, make sure streaming finishes */
1790 result = gst_pad_stop_task (pad);
1792 GST_QUEUE_MUTEX_LOCK (queue);
1793 gst_queue_locked_flush (queue, FALSE);
1794 GST_QUEUE_MUTEX_UNLOCK (queue);
1805 queue_capacity_change (GstQueue * queue)
1807 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1808 gst_queue_leak_downstream (queue);
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);
1817 /* Changing the minimum required fill level must
1818 * wake up the _loop function as it might now
1819 * be able to preceed.
1821 #define QUEUE_THRESHOLD_CHANGE(q)\
1822 GST_QUEUE_SIGNAL_ADD (q);
1825 gst_queue_set_property (GObject * object,
1826 guint prop_id, const GValue * value, GParamSpec * pspec)
1828 GstQueue *queue = GST_QUEUE (object);
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);
1835 case PROP_MAX_SIZE_BYTES:
1836 queue->max_size.bytes = g_value_get_uint (value);
1837 queue_capacity_change (queue);
1839 case PROP_MAX_SIZE_BUFFERS:
1840 queue->max_size.buffers = g_value_get_uint (value);
1841 queue_capacity_change (queue);
1843 case PROP_MAX_SIZE_TIME:
1844 queue->max_size.time = g_value_get_uint64 (value);
1845 queue_capacity_change (queue);
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);
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);
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);
1863 queue->leaky = g_value_get_enum (value);
1866 queue->silent = g_value_get_boolean (value);
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);
1875 GST_INFO_OBJECT(queue, "done");
1877 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
1878 case PROP_FLUSH_ON_EOS:
1879 queue->flush_on_eos = g_value_get_boolean (value);
1882 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1886 GST_QUEUE_MUTEX_UNLOCK (queue);
1890 gst_queue_get_property (GObject * object,
1891 guint prop_id, GValue * value, GParamSpec * pspec)
1893 GstQueue *queue = GST_QUEUE (object);
1895 GST_QUEUE_MUTEX_LOCK (queue);
1898 case PROP_CUR_LEVEL_BYTES:
1899 g_value_set_uint (value, queue->cur_level.bytes);
1901 case PROP_CUR_LEVEL_BUFFERS:
1902 g_value_set_uint (value, queue->cur_level.buffers);
1904 case PROP_CUR_LEVEL_TIME:
1905 g_value_set_uint64 (value, queue->cur_level.time);
1907 case PROP_MAX_SIZE_BYTES:
1908 g_value_set_uint (value, queue->max_size.bytes);
1910 case PROP_MAX_SIZE_BUFFERS:
1911 g_value_set_uint (value, queue->max_size.buffers);
1913 case PROP_MAX_SIZE_TIME:
1914 g_value_set_uint64 (value, queue->max_size.time);
1916 case PROP_MIN_THRESHOLD_BYTES:
1917 g_value_set_uint (value, queue->min_threshold.bytes);
1919 case PROP_MIN_THRESHOLD_BUFFERS:
1920 g_value_set_uint (value, queue->min_threshold.buffers);
1922 case PROP_MIN_THRESHOLD_TIME:
1923 g_value_set_uint64 (value, queue->min_threshold.time);
1926 g_value_set_enum (value, queue->leaky);
1929 g_value_set_boolean (value, queue->silent);
1931 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
1932 case PROP_EMPTY_BUFFERS:
1933 g_value_set_boolean(value, queue->empty_buffers);
1935 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
1936 case PROP_FLUSH_ON_EOS:
1937 g_value_set_boolean (value, queue->flush_on_eos);
1940 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1944 GST_QUEUE_MUTEX_UNLOCK (queue);