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"
64 #include "../../gst/gst-i18n-lib.h"
65 #include "../../gst/glib-compat-private.h"
67 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
72 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
77 GST_DEBUG_CATEGORY_STATIC (queue_debug);
78 #define GST_CAT_DEFAULT (queue_debug)
79 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
81 #define STATUS(queue, pad, msg) \
82 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
83 "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
84 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
85 "-%" G_GUINT64_FORMAT " ns, %u items", \
86 GST_DEBUG_PAD_NAME (pad), \
87 queue->cur_level.buffers, \
88 queue->min_threshold.buffers, \
89 queue->max_size.buffers, \
90 queue->cur_level.bytes, \
91 queue->min_threshold.bytes, \
92 queue->max_size.bytes, \
93 queue->cur_level.time, \
94 queue->min_threshold.time, \
95 queue->max_size.time, \
96 gst_queue_array_get_length (queue->queue))
98 /* Queue signals and args */
111 /* FIXME: don't we have another way of doing this
112 * "Gstreamer format" (frame/byte/time) queries? */
113 PROP_CUR_LEVEL_BUFFERS,
114 PROP_CUR_LEVEL_BYTES,
116 PROP_MAX_SIZE_BUFFERS,
119 PROP_MIN_THRESHOLD_BUFFERS,
120 PROP_MIN_THRESHOLD_BYTES,
121 PROP_MIN_THRESHOLD_TIME,
124 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
126 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
130 /* default property values */
131 #define DEFAULT_MAX_SIZE_BUFFERS 200 /* 200 buffers */
132 #define DEFAULT_MAX_SIZE_BYTES (10 * 1024 * 1024) /* 10 MB */
133 #define DEFAULT_MAX_SIZE_TIME GST_SECOND /* 1 second */
135 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
136 g_mutex_lock (&q->qlock); \
139 #define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START { \
140 GST_QUEUE_MUTEX_LOCK (q); \
141 if (q->srcresult != GST_FLOW_OK) \
145 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
146 g_mutex_unlock (&q->qlock); \
149 #define GST_QUEUE_WAIT_DEL_CHECK(q, label) G_STMT_START { \
150 STATUS (q, q->sinkpad, "wait for DEL"); \
151 q->waiting_del = TRUE; \
152 g_cond_wait (&q->item_del, &q->qlock); \
153 q->waiting_del = FALSE; \
154 if (q->srcresult != GST_FLOW_OK) { \
155 STATUS (q, q->srcpad, "received DEL wakeup"); \
158 STATUS (q, q->sinkpad, "received DEL"); \
161 #define GST_QUEUE_WAIT_ADD_CHECK(q, label) G_STMT_START { \
162 STATUS (q, q->srcpad, "wait for ADD"); \
163 q->waiting_add = TRUE; \
164 g_cond_wait (&q->item_add, &q->qlock); \
165 q->waiting_add = FALSE; \
166 if (q->srcresult != GST_FLOW_OK) { \
167 STATUS (q, q->srcpad, "received ADD wakeup"); \
170 STATUS (q, q->srcpad, "received ADD"); \
173 #define GST_QUEUE_SIGNAL_DEL(q) G_STMT_START { \
174 if (q->waiting_del) { \
175 STATUS (q, q->srcpad, "signal DEL"); \
176 g_cond_signal (&q->item_del); \
180 #define GST_QUEUE_SIGNAL_ADD(q) G_STMT_START { \
181 if (q->waiting_add) { \
182 STATUS (q, q->sinkpad, "signal ADD"); \
183 g_cond_signal (&q->item_add); \
188 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue", 0, "queue element"); \
189 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0, \
190 "dataflow inside the queue element");
191 #define gst_queue_parent_class parent_class
192 G_DEFINE_TYPE_WITH_CODE (GstQueue, gst_queue, GST_TYPE_ELEMENT, _do_init);
194 static void gst_queue_finalize (GObject * object);
195 static void gst_queue_set_property (GObject * object,
196 guint prop_id, const GValue * value, GParamSpec * pspec);
197 static void gst_queue_get_property (GObject * object,
198 guint prop_id, GValue * value, GParamSpec * pspec);
200 static GstFlowReturn gst_queue_chain (GstPad * pad, GstObject * parent,
202 static GstFlowReturn gst_queue_chain_list (GstPad * pad, GstObject * parent,
203 GstBufferList * buffer_list);
204 static GstFlowReturn gst_queue_push_one (GstQueue * queue);
205 static void gst_queue_loop (GstPad * pad);
207 static GstFlowReturn gst_queue_handle_sink_event (GstPad * pad,
208 GstObject * parent, GstEvent * event);
209 static gboolean gst_queue_handle_sink_query (GstPad * pad, GstObject * parent,
212 static gboolean gst_queue_handle_src_event (GstPad * pad, GstObject * parent,
214 static gboolean gst_queue_handle_src_query (GstPad * pad, GstObject * parent,
217 static void gst_queue_locked_flush (GstQueue * queue, gboolean full);
219 static gboolean gst_queue_src_activate_mode (GstPad * pad, GstObject * parent,
220 GstPadMode mode, gboolean active);
221 static gboolean gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
222 GstPadMode mode, gboolean active);
224 static gboolean gst_queue_is_empty (GstQueue * queue);
225 static gboolean gst_queue_is_filled (GstQueue * queue);
235 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
238 queue_leaky_get_type (void)
240 static GType queue_leaky_type = 0;
241 static const GEnumValue queue_leaky[] = {
242 {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
243 {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
244 {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
249 if (!queue_leaky_type) {
250 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
252 return queue_leaky_type;
255 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
258 gst_queue_class_init (GstQueueClass * klass)
260 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
261 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
263 gobject_class->set_property = gst_queue_set_property;
264 gobject_class->get_property = gst_queue_get_property;
268 * GstQueue::underrun:
269 * @queue: the queue instance
271 * Reports that the buffer became empty (underrun).
272 * A buffer is empty if the total amount of data inside it (num-buffers, time,
273 * size) is lower than the boundary values which can be set through the
274 * GObject properties.
276 gst_queue_signals[SIGNAL_UNDERRUN] =
277 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
278 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
279 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
282 * @queue: the queue instance
284 * Reports that enough (min-threshold) data is in the queue. Use this signal
285 * together with the underrun signal to pause the pipeline on underrun and
286 * wait for the queue to fill-up before resume playback.
288 gst_queue_signals[SIGNAL_RUNNING] =
289 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
290 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
291 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
294 * @queue: the queue instance
296 * Reports that the buffer became full (overrun).
297 * A buffer is full if the total amount of data inside it (num-buffers, time,
298 * size) is higher than the boundary values which can be set through the
299 * GObject properties.
301 gst_queue_signals[SIGNAL_OVERRUN] =
302 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
303 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
304 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
307 * @queue: the queue instance
309 * Reports when the queue has enough data to start pushing data again on the
312 gst_queue_signals[SIGNAL_PUSHING] =
313 g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
314 G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
315 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
318 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
319 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
320 "Current amount of data in the queue (bytes)",
321 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
323 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
324 "Current number of buffers in the queue",
325 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
327 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
328 "Current amount of data in the queue (in ns)",
329 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
332 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
333 "Max. amount of data in the queue (bytes, 0=disable)",
334 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
335 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
336 G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
338 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
339 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
340 DEFAULT_MAX_SIZE_BUFFERS,
341 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
342 G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
344 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
345 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
346 DEFAULT_MAX_SIZE_TIME,
347 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
348 G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
351 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
352 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
354 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
355 G_PARAM_STATIC_STRINGS));
356 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
357 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
358 "Min. number of buffers in the queue to allow reading (0=disable)", 0,
360 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
361 G_PARAM_STATIC_STRINGS));
362 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
363 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
364 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
366 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
367 G_PARAM_STATIC_STRINGS));
369 g_object_class_install_property (gobject_class, PROP_LEAKY,
370 g_param_spec_enum ("leaky", "Leaky",
371 "Where the queue leaks, if at all",
372 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
373 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
374 G_PARAM_STATIC_STRINGS));
379 * Don't emit queue signals. Makes queues more lightweight if no signals are
382 g_object_class_install_property (gobject_class, PROP_SILENT,
383 g_param_spec_boolean ("silent", "Silent",
384 "Don't emit queue signals", FALSE,
385 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
386 G_PARAM_STATIC_STRINGS));
388 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
389 g_object_class_install_property (gobject_class, PROP_EMPTY_BUFFERS,
390 g_param_spec_boolean ("empty-buffers", "empty_buffers",
391 "Drop all the incomming buffers and flush buffers in queue",
392 FALSE, G_PARAM_READWRITE));
393 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
396 * GstQueue:flush-on-eos
398 * Discard all data in the queue when an EOS event is received, and pass
399 * on the EOS event as soon as possible (instead of waiting until all
400 * buffers in the queue have been processed, which is the default behaviour).
402 * Flushing the queue on EOS might be useful when capturing and encoding
403 * from a live source, to finish up the recording quickly in cases when
404 * the encoder is slow. Note that this might mean some data from the end of
405 * the recording data might be lost though (never more than the configured
406 * max. sizes though).
410 g_object_class_install_property (gobject_class, PROP_FLUSH_ON_EOS,
411 g_param_spec_boolean ("flush-on-eos", "Flush on EOS",
412 "Discard all data in the queue when an EOS event is received", FALSE,
413 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
414 G_PARAM_STATIC_STRINGS));
416 gobject_class->finalize = gst_queue_finalize;
418 gst_element_class_set_static_metadata (gstelement_class,
420 "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
421 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
422 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
424 /* Registering debug symbols for function pointers */
425 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
426 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
427 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
428 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
429 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
430 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
431 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain_list);
435 gst_queue_init (GstQueue * queue)
437 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
439 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
440 gst_pad_set_chain_list_function (queue->sinkpad, gst_queue_chain_list);
441 gst_pad_set_activatemode_function (queue->sinkpad,
442 gst_queue_sink_activate_mode);
443 gst_pad_set_event_full_function (queue->sinkpad, gst_queue_handle_sink_event);
444 gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
445 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
446 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
448 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
450 gst_pad_set_activatemode_function (queue->srcpad,
451 gst_queue_src_activate_mode);
452 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
453 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
454 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
455 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
457 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
458 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
459 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
460 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
461 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
462 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
463 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
464 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
465 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
467 queue->leaky = GST_QUEUE_NO_LEAK;
468 queue->srcresult = GST_FLOW_FLUSHING;
470 g_mutex_init (&queue->qlock);
471 g_cond_init (&queue->item_add);
472 g_cond_init (&queue->item_del);
473 g_cond_init (&queue->query_handled);
476 gst_queue_array_new_for_struct (sizeof (GstQueueItem),
477 DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
479 queue->sinktime = GST_CLOCK_STIME_NONE;
480 queue->srctime = GST_CLOCK_STIME_NONE;
482 queue->sink_tainted = TRUE;
483 queue->src_tainted = TRUE;
485 queue->newseg_applied_to_src = FALSE;
487 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
488 queue->empty_buffers = FALSE;
489 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
491 GST_DEBUG_OBJECT (queue,
492 "initialized queue's not_empty & not_full conditions");
495 /* called only once, as opposed to dispose */
497 gst_queue_finalize (GObject * object)
499 GstQueue *queue = GST_QUEUE (object);
502 GST_DEBUG_OBJECT (queue, "finalizing queue");
504 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
505 /* FIXME: if it's a query, shouldn't we unref that too? */
506 if (!qitem->is_query)
507 gst_mini_object_unref (qitem->item);
509 gst_queue_array_free (queue->queue);
511 g_mutex_clear (&queue->qlock);
512 g_cond_clear (&queue->item_add);
513 g_cond_clear (&queue->item_del);
514 g_cond_clear (&queue->query_handled);
516 G_OBJECT_CLASS (parent_class)->finalize (object);
519 /* Convenience function */
520 static inline GstClockTimeDiff
521 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
523 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
525 if (GST_CLOCK_TIME_IS_VALID (val)) {
527 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
536 /* calculate the diff between running time on the sink and src of the queue.
537 * This is the total amount of time in the queue. */
539 update_time_level (GstQueue * queue)
541 gint64 sink_time, src_time;
543 if (queue->sink_tainted) {
544 GST_LOG_OBJECT (queue, "update sink time");
546 my_segment_to_running_time (&queue->sink_segment,
547 queue->sink_segment.position);
548 queue->sink_tainted = FALSE;
550 sink_time = queue->sinktime;
552 if (queue->src_tainted) {
553 GST_LOG_OBJECT (queue, "update src time");
555 my_segment_to_running_time (&queue->src_segment,
556 queue->src_segment.position);
557 queue->src_tainted = FALSE;
559 src_time = queue->srctime;
561 GST_LOG_OBJECT (queue, "sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT,
562 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
564 if (GST_CLOCK_STIME_IS_VALID (src_time)
565 && GST_CLOCK_STIME_IS_VALID (sink_time) && sink_time >= src_time)
566 queue->cur_level.time = sink_time - src_time;
568 queue->cur_level.time = 0;
571 /* take a SEGMENT event and apply the values to segment, updating the time
574 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
577 gst_event_copy_segment (event, segment);
579 /* now configure the values, we use these to track timestamps on the
581 if (segment->format != GST_FORMAT_TIME) {
582 /* non-time format, pretent the current time segment is closed with a
583 * 0 start and unknown stop time. */
584 segment->format = GST_FORMAT_TIME;
590 queue->sink_tainted = TRUE;
592 queue->src_tainted = TRUE;
594 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
596 /* segment can update the time level of the queue */
597 update_time_level (queue);
601 apply_gap (GstQueue * queue, GstEvent * event,
602 GstSegment * segment, gboolean is_sink)
604 GstClockTime timestamp;
605 GstClockTime duration;
607 gst_event_parse_gap (event, ×tamp, &duration);
609 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
611 if (GST_CLOCK_TIME_IS_VALID (duration)) {
612 timestamp += duration;
615 segment->position = timestamp;
618 queue->sink_tainted = TRUE;
620 queue->src_tainted = TRUE;
622 /* calc diff with other end */
623 update_time_level (queue);
628 /* take a buffer and update segment, updating the time level of the queue. */
630 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
633 GstClockTime duration, timestamp;
635 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
636 duration = GST_BUFFER_DURATION (buffer);
638 /* if no timestamp is set, assume it's continuous with the previous
640 if (timestamp == GST_CLOCK_TIME_NONE)
641 timestamp = segment->position;
644 if (duration != GST_CLOCK_TIME_NONE)
645 timestamp += duration;
647 GST_LOG_OBJECT (queue, "%s position updated to %" GST_TIME_FORMAT,
648 segment == &queue->sink_segment ? "sink" : "src",
649 GST_TIME_ARGS (timestamp));
651 segment->position = timestamp;
653 queue->sink_tainted = TRUE;
655 queue->src_tainted = TRUE;
658 /* calc diff with other end */
659 update_time_level (queue);
663 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer user_data)
665 GstClockTime *timestamp = user_data;
668 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
669 " duration %" GST_TIME_FORMAT, idx, GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
670 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
671 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
673 btime = GST_BUFFER_DTS_OR_PTS (*buf);
674 if (GST_CLOCK_TIME_IS_VALID (btime))
677 if (GST_BUFFER_DURATION_IS_VALID (*buf))
678 *timestamp += GST_BUFFER_DURATION (*buf);
680 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
685 /* take a buffer list and update segment, updating the time level of the queue */
687 apply_buffer_list (GstQueue * queue, GstBufferList * buffer_list,
688 GstSegment * segment, gboolean sink)
690 GstClockTime timestamp;
692 /* if no timestamp is set, assume it's continuous with the previous time */
693 timestamp = segment->position;
695 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
697 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
698 GST_TIME_ARGS (timestamp));
700 segment->position = timestamp;
703 queue->sink_tainted = TRUE;
705 queue->src_tainted = TRUE;
707 /* calc diff with other end */
708 update_time_level (queue);
712 gst_queue_locked_flush (GstQueue * queue, gboolean full)
716 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
717 /* Then lose another reference because we are supposed to destroy that
718 data when flushing */
719 if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
720 && GST_EVENT_IS_STICKY (qitem->item)
721 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
722 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
723 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
725 if (!qitem->is_query)
726 gst_mini_object_unref (qitem->item);
727 memset (qitem, 0, sizeof (GstQueueItem));
729 queue->last_query = FALSE;
730 g_cond_signal (&queue->query_handled);
731 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
732 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
733 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
734 queue->min_threshold.time = queue->orig_min_threshold.time;
735 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
736 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
737 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
739 queue->sinktime = queue->srctime = GST_CLOCK_STIME_NONE;
740 queue->sink_tainted = queue->src_tainted = TRUE;
742 /* we deleted a lot of something */
743 GST_QUEUE_SIGNAL_DEL (queue);
746 /* enqueue an item an update the level stats, with QUEUE_LOCK */
748 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
751 GstBuffer *buffer = GST_BUFFER_CAST (item);
752 gsize bsize = gst_buffer_get_size (buffer);
754 /* add buffer to the statistics */
755 queue->cur_level.buffers++;
756 queue->cur_level.bytes += bsize;
757 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
760 qitem.is_query = FALSE;
762 gst_queue_array_push_tail_struct (queue->queue, &qitem);
763 GST_QUEUE_SIGNAL_ADD (queue);
767 gst_queue_locked_enqueue_buffer_list (GstQueue * queue, gpointer item)
770 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
773 bsize = gst_buffer_list_calculate_size (buffer_list);
775 /* add buffer to the statistics */
776 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
777 queue->cur_level.bytes += bsize;
778 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
781 qitem.is_query = FALSE;
783 gst_queue_array_push_tail_struct (queue->queue, &qitem);
784 GST_QUEUE_SIGNAL_ADD (queue);
788 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
791 GstEvent *event = GST_EVENT_CAST (item);
793 switch (GST_EVENT_TYPE (event)) {
795 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
796 /* Zero the thresholds, this makes sure the queue is completely
797 * filled and we can read all data from the queue. */
798 if (queue->flush_on_eos)
799 gst_queue_locked_flush (queue, FALSE);
801 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
802 /* mark the queue as EOS. This prevents us from accepting more data. */
805 case GST_EVENT_SEGMENT:
806 apply_segment (queue, event, &queue->sink_segment, TRUE);
807 /* if the queue is empty, apply sink segment on the source */
808 if (gst_queue_array_is_empty (queue->queue)) {
809 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
810 apply_segment (queue, event, &queue->src_segment, FALSE);
811 queue->newseg_applied_to_src = TRUE;
813 /* a new segment allows us to accept more buffers if we got EOS
815 queue->unexpected = FALSE;
818 apply_gap (queue, event, &queue->sink_segment, TRUE);
825 qitem.is_query = FALSE;
827 gst_queue_array_push_tail_struct (queue->queue, &qitem);
828 GST_QUEUE_SIGNAL_ADD (queue);
831 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
832 static GstMiniObject *
833 gst_queue_locked_dequeue (GstQueue * queue)
839 qitem = gst_queue_array_pop_head_struct (queue->queue);
844 bufsize = qitem->size;
846 if (GST_IS_BUFFER (item)) {
847 GstBuffer *buffer = GST_BUFFER_CAST (item);
849 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
850 "retrieved buffer %p from queue", buffer);
852 queue->cur_level.buffers--;
853 queue->cur_level.bytes -= bufsize;
854 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
856 /* if the queue is empty now, update the other side */
857 if (queue->cur_level.buffers == 0)
858 queue->cur_level.time = 0;
859 } else if (GST_IS_BUFFER_LIST (item)) {
860 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
862 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
863 "retrieved buffer list %p from queue", buffer_list);
865 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
866 queue->cur_level.bytes -= bufsize;
867 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
869 /* if the queue is empty now, update the other side */
870 if (queue->cur_level.buffers == 0)
871 queue->cur_level.time = 0;
872 } else if (GST_IS_EVENT (item)) {
873 GstEvent *event = GST_EVENT_CAST (item);
875 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
876 "retrieved event %p from queue", event);
878 switch (GST_EVENT_TYPE (event)) {
880 /* queue is empty now that we dequeued the EOS */
881 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
883 case GST_EVENT_SEGMENT:
884 /* apply newsegment if it has not already been applied */
885 if (G_LIKELY (!queue->newseg_applied_to_src)) {
886 apply_segment (queue, event, &queue->src_segment, FALSE);
888 queue->newseg_applied_to_src = FALSE;
892 apply_gap (queue, event, &queue->src_segment, FALSE);
897 } else if (GST_IS_QUERY (item)) {
898 GstQuery *query = GST_QUERY_CAST (item);
900 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
901 "retrieved query %p from queue", query);
904 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
905 item, GST_OBJECT_NAME (queue));
908 GST_QUEUE_SIGNAL_DEL (queue);
915 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
921 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
926 queue = GST_QUEUE (parent);
928 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Received event '%s'",
929 GST_EVENT_TYPE_NAME (event));
931 switch (GST_EVENT_TYPE (event)) {
932 case GST_EVENT_FLUSH_START:
934 ret = gst_pad_push_event (queue->srcpad, event);
936 /* now unblock the chain function */
937 GST_QUEUE_MUTEX_LOCK (queue);
938 queue->srcresult = GST_FLOW_FLUSHING;
939 /* unblock the loop and chain functions */
940 GST_QUEUE_SIGNAL_ADD (queue);
941 GST_QUEUE_SIGNAL_DEL (queue);
942 GST_QUEUE_MUTEX_UNLOCK (queue);
944 /* make sure it pauses, this should happen since we sent
945 * flush_start downstream. */
946 gst_pad_pause_task (queue->srcpad);
947 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
949 /* unblock query handler after the streaming thread is shut down.
950 * Otherwise downstream might have a query that is already unreffed
952 GST_QUEUE_MUTEX_LOCK (queue);
953 queue->last_query = FALSE;
954 g_cond_signal (&queue->query_handled);
955 GST_QUEUE_MUTEX_UNLOCK (queue);
957 case GST_EVENT_FLUSH_STOP:
959 ret = gst_pad_push_event (queue->srcpad, event);
961 GST_QUEUE_MUTEX_LOCK (queue);
962 gst_queue_locked_flush (queue, FALSE);
963 queue->srcresult = GST_FLOW_OK;
965 queue->unexpected = FALSE;
966 if (gst_pad_is_active (queue->srcpad)) {
967 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
968 queue->srcpad, NULL);
970 GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
971 "pad not active any longer");
973 GST_QUEUE_MUTEX_UNLOCK (queue);
975 STATUS (queue, pad, "after flush");
978 if (GST_EVENT_IS_SERIALIZED (event)) {
979 /* serialized events go in the queue */
980 GST_QUEUE_MUTEX_LOCK (queue);
982 /* STREAM_START and SEGMENT reset the EOS status of a
983 * pad. Change the cached sinkpad flow result accordingly */
984 if (queue->srcresult == GST_FLOW_EOS
985 && (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START
986 || GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT))
987 queue->srcresult = GST_FLOW_OK;
989 if (queue->srcresult != GST_FLOW_OK) {
990 /* Errors in sticky event pushing are no problem and ignored here
991 * as they will cause more meaningful errors during data flow.
992 * For EOS events, that are not followed by data flow, we still
993 * return FALSE here though and report an error.
995 if (!GST_EVENT_IS_STICKY (event)) {
996 GST_QUEUE_MUTEX_UNLOCK (queue);
998 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
999 if (queue->srcresult == GST_FLOW_NOT_LINKED
1000 || queue->srcresult < GST_FLOW_EOS) {
1001 GST_QUEUE_MUTEX_UNLOCK (queue);
1002 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
1004 GST_QUEUE_MUTEX_UNLOCK (queue);
1006 goto out_flow_error;
1010 /* refuse more events on EOS unless they unset the EOS status */
1012 switch (GST_EVENT_TYPE (event)) {
1013 case GST_EVENT_STREAM_START:
1014 case GST_EVENT_SEGMENT:
1015 /* Restart the loop */
1016 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
1017 queue->srcresult = GST_FLOW_OK;
1019 queue->unexpected = FALSE;
1020 gst_pad_start_task (queue->srcpad,
1021 (GstTaskFunction) gst_queue_loop, queue->srcpad, NULL);
1024 queue->unexpected = FALSE;
1033 gst_queue_locked_enqueue_event (queue, event);
1034 GST_QUEUE_MUTEX_UNLOCK (queue);
1036 /* non-serialized events are forwarded downstream immediately */
1037 ret = gst_pad_push_event (queue->srcpad, event);
1042 GST_ERROR_OBJECT (queue, "Failed to push event");
1043 return GST_FLOW_ERROR;
1050 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
1051 GST_QUEUE_MUTEX_UNLOCK (queue);
1052 gst_event_unref (event);
1053 return GST_FLOW_EOS;
1057 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1058 "refusing event, we have a downstream flow error: %s",
1059 gst_flow_get_name (queue->srcresult));
1060 gst_event_unref (event);
1061 return queue->srcresult;
1066 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1068 GstQueue *queue = GST_QUEUE_CAST (parent);
1071 switch (GST_QUERY_TYPE (query)) {
1073 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
1076 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1077 GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
1078 GST_QUERY_TYPE_NAME (query));
1079 qitem.item = GST_MINI_OBJECT_CAST (query);
1080 qitem.is_query = TRUE;
1082 gst_queue_array_push_tail_struct (queue->queue, &qitem);
1083 GST_QUEUE_SIGNAL_ADD (queue);
1084 while (queue->srcresult == GST_FLOW_OK &&
1085 queue->last_handled_query != query)
1086 g_cond_wait (&queue->query_handled, &queue->qlock);
1087 queue->last_handled_query = NULL;
1088 if (queue->srcresult != GST_FLOW_OK)
1090 res = queue->last_query;
1091 GST_QUEUE_MUTEX_UNLOCK (queue);
1093 res = gst_pad_query_default (pad, parent, query);
1102 GST_DEBUG_OBJECT (queue, "we are flushing");
1103 GST_QUEUE_MUTEX_UNLOCK (queue);
1109 gst_queue_is_empty (GstQueue * queue)
1113 tail = gst_queue_array_peek_tail_struct (queue->queue);
1118 /* Only consider the queue empty if the minimum thresholds
1119 * are not reached and data is at the queue tail. Otherwise
1120 * we would block forever on serialized queries.
1122 if (!GST_IS_BUFFER (tail->item) && !GST_IS_BUFFER_LIST (tail->item))
1125 /* It is possible that a max size is reached before all min thresholds are.
1126 * Therefore, only consider it empty if it is not filled. */
1127 return ((queue->min_threshold.buffers > 0 &&
1128 queue->cur_level.buffers < queue->min_threshold.buffers) ||
1129 (queue->min_threshold.bytes > 0 &&
1130 queue->cur_level.bytes < queue->min_threshold.bytes) ||
1131 (queue->min_threshold.time > 0 &&
1132 queue->cur_level.time < queue->min_threshold.time)) &&
1133 !gst_queue_is_filled (queue);
1137 gst_queue_is_filled (GstQueue * queue)
1139 return (((queue->max_size.buffers > 0 &&
1140 queue->cur_level.buffers >= queue->max_size.buffers) ||
1141 (queue->max_size.bytes > 0 &&
1142 queue->cur_level.bytes >= queue->max_size.bytes) ||
1143 (queue->max_size.time > 0 &&
1144 queue->cur_level.time >= queue->max_size.time)));
1148 gst_queue_leak_downstream (GstQueue * queue)
1150 /* for as long as the queue is filled, dequeue an item and discard it */
1151 while (gst_queue_is_filled (queue)) {
1152 GstMiniObject *leak;
1154 leak = gst_queue_locked_dequeue (queue);
1155 /* there is nothing to dequeue and the queue is still filled.. This should
1157 g_assert (leak != NULL);
1159 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1160 "queue is full, leaking item %p on downstream end", leak);
1161 if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
1162 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1163 "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
1164 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
1167 if (!GST_IS_QUERY (leak))
1168 gst_mini_object_unref (leak);
1170 /* last buffer needs to get a DISCONT flag */
1171 queue->head_needs_discont = TRUE;
1176 discont_first_buffer (GstBuffer ** buffer, guint i, gpointer user_data)
1178 GstQueue *queue = user_data;
1179 GstBuffer *subbuffer = gst_buffer_make_writable (*buffer);
1182 *buffer = subbuffer;
1183 GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
1185 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1191 static GstFlowReturn
1192 gst_queue_chain_buffer_or_list (GstPad * pad, GstObject * parent,
1193 GstMiniObject * obj, gboolean is_list)
1197 queue = GST_QUEUE_CAST (parent);
1199 /* we have to lock the queue since we span threads */
1200 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1201 /* when we received EOS, we refuse any more data */
1204 if (queue->unexpected)
1205 goto out_unexpected;
1207 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
1208 /* Added to not enqueue buffers in the queue while paused */
1209 if (queue->empty_buffers) {
1210 GST_CAT_LOG_OBJECT(queue_dataflow, queue, "drop buffer %p", obj);
1211 gst_mini_object_unref(obj);
1212 GST_QUEUE_MUTEX_UNLOCK(queue);
1215 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
1218 GstClockTime duration, timestamp;
1219 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1221 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1222 duration = GST_BUFFER_DURATION (buffer);
1224 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
1225 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1226 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1227 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1229 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1230 "received buffer list %p with %u buffers", obj,
1231 gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)));
1234 /* We make space available if we're "full" according to whatever
1235 * the user defined as "full". Note that this only applies to buffers.
1236 * We always handle events and they don't count in our statistics. */
1237 while (gst_queue_is_filled (queue)) {
1238 if (!queue->silent) {
1239 GST_QUEUE_MUTEX_UNLOCK (queue);
1240 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
1241 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1242 /* we recheck, the signal could have changed the thresholds */
1243 if (!gst_queue_is_filled (queue))
1247 /* how are we going to make space for this buffer? */
1248 switch (queue->leaky) {
1249 case GST_QUEUE_LEAK_UPSTREAM:
1250 /* next buffer needs to get a DISCONT flag */
1251 queue->tail_needs_discont = TRUE;
1252 /* leak current buffer */
1253 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1254 "queue is full, leaking buffer on upstream end");
1255 /* now we can clean up and exit right away */
1257 case GST_QUEUE_LEAK_DOWNSTREAM:
1258 gst_queue_leak_downstream (queue);
1261 g_warning ("Unknown leaky type, using default");
1263 case GST_QUEUE_NO_LEAK:
1265 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1266 "queue is full, waiting for free space");
1268 /* don't leak. Instead, wait for space to be available */
1270 /* for as long as the queue is filled, wait till an item was deleted. */
1271 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1272 } while (gst_queue_is_filled (queue));
1274 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1276 if (!queue->silent) {
1277 GST_QUEUE_MUTEX_UNLOCK (queue);
1278 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1279 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1286 if (queue->tail_needs_discont) {
1288 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1289 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1293 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1295 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1298 obj = GST_MINI_OBJECT_CAST (buffer);
1300 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
1302 buffer_list = gst_buffer_list_make_writable (buffer_list);
1303 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1304 obj = GST_MINI_OBJECT_CAST (buffer_list);
1306 queue->tail_needs_discont = FALSE;
1309 /* put buffer in queue now */
1311 gst_queue_locked_enqueue_buffer_list (queue, obj);
1313 gst_queue_locked_enqueue_buffer (queue, obj);
1314 GST_QUEUE_MUTEX_UNLOCK (queue);
1318 /* special conditions */
1321 GST_QUEUE_MUTEX_UNLOCK (queue);
1323 gst_mini_object_unref (obj);
1329 GstFlowReturn ret = queue->srcresult;
1331 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1332 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1333 GST_QUEUE_MUTEX_UNLOCK (queue);
1334 gst_mini_object_unref (obj);
1340 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1341 GST_QUEUE_MUTEX_UNLOCK (queue);
1343 gst_mini_object_unref (obj);
1345 return GST_FLOW_EOS;
1349 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1350 GST_QUEUE_MUTEX_UNLOCK (queue);
1352 gst_mini_object_unref (obj);
1354 return GST_FLOW_EOS;
1358 static GstFlowReturn
1359 gst_queue_chain_list (GstPad * pad, GstObject * parent,
1360 GstBufferList * buffer_list)
1362 return gst_queue_chain_buffer_or_list (pad, parent,
1363 GST_MINI_OBJECT_CAST (buffer_list), TRUE);
1366 static GstFlowReturn
1367 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1369 return gst_queue_chain_buffer_or_list (pad, parent,
1370 GST_MINI_OBJECT_CAST (buffer), FALSE);
1373 /* dequeue an item from the queue an push it downstream. This functions returns
1374 * the result of the push. */
1375 static GstFlowReturn
1376 gst_queue_push_one (GstQueue * queue)
1378 GstFlowReturn result = queue->srcresult;
1379 GstMiniObject *data;
1382 data = gst_queue_locked_dequeue (queue);
1387 is_list = GST_IS_BUFFER_LIST (data);
1389 if (GST_IS_BUFFER (data) || is_list) {
1393 buffer = GST_BUFFER_CAST (data);
1395 if (queue->head_needs_discont) {
1396 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1400 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1402 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1404 queue->head_needs_discont = FALSE;
1407 GST_QUEUE_MUTEX_UNLOCK (queue);
1408 result = gst_pad_push (queue->srcpad, buffer);
1410 GstBufferList *buffer_list;
1412 buffer_list = GST_BUFFER_LIST_CAST (data);
1414 if (queue->head_needs_discont) {
1415 buffer_list = gst_buffer_list_make_writable (buffer_list);
1416 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1417 queue->head_needs_discont = FALSE;
1420 GST_QUEUE_MUTEX_UNLOCK (queue);
1421 result = gst_pad_push_list (queue->srcpad, buffer_list);
1424 /* need to check for srcresult here as well */
1425 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1427 if (result == GST_FLOW_EOS) {
1428 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1429 /* stop pushing buffers, we dequeue all items until we see an item that we
1430 * can push again, which is EOS or SEGMENT. If there is nothing in the
1431 * queue we can push, we set a flag to make the sinkpad refuse more
1432 * buffers with an EOS return value. */
1433 while ((data = gst_queue_locked_dequeue (queue))) {
1434 if (GST_IS_BUFFER (data)) {
1435 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1436 "dropping EOS buffer %p", data);
1437 gst_buffer_unref (GST_BUFFER_CAST (data));
1438 } else if (GST_IS_BUFFER_LIST (data)) {
1439 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1440 "dropping EOS buffer list %p", data);
1441 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
1442 } else if (GST_IS_EVENT (data)) {
1443 GstEvent *event = GST_EVENT_CAST (data);
1444 GstEventType type = GST_EVENT_TYPE (event);
1446 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT
1447 || type == GST_EVENT_STREAM_START) {
1448 /* we found a pushable item in the queue, push it out */
1449 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1450 "pushing pushable event %s after EOS",
1451 GST_EVENT_TYPE_NAME (event));
1454 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1455 "dropping EOS event %p", event);
1456 gst_event_unref (event);
1457 } else if (GST_IS_QUERY (data)) {
1458 GstQuery *query = GST_QUERY_CAST (data);
1460 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1461 "dropping query %p because of EOS", query);
1462 queue->last_query = FALSE;
1463 g_cond_signal (&queue->query_handled);
1466 /* no more items in the queue. Set the unexpected flag so that upstream
1467 * make us refuse any more buffers on the sinkpad. Since we will still
1468 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1469 * task function does not shut down. */
1470 queue->unexpected = TRUE;
1471 result = GST_FLOW_OK;
1473 } else if (GST_IS_EVENT (data)) {
1474 GstEvent *event = GST_EVENT_CAST (data);
1475 GstEventType type = GST_EVENT_TYPE (event);
1477 GST_QUEUE_MUTEX_UNLOCK (queue);
1479 gst_pad_push_event (queue->srcpad, event);
1481 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1482 /* if we're EOS, return EOS so that the task pauses. */
1483 if (type == GST_EVENT_EOS) {
1484 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1485 "pushed EOS event %p, return EOS", event);
1486 result = GST_FLOW_EOS;
1488 } else if (GST_IS_QUERY (data)) {
1489 GstQuery *query = GST_QUERY_CAST (data);
1492 GST_QUEUE_MUTEX_UNLOCK (queue);
1493 ret = gst_pad_peer_query (queue->srcpad, query);
1494 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1495 queue->last_query = ret;
1496 queue->last_handled_query = query;
1497 g_cond_signal (&queue->query_handled);
1498 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1499 "did query %p, return %d", query, queue->last_query);
1506 GST_CAT_ERROR_OBJECT (queue_dataflow, queue,
1507 "exit because we have no item in the queue");
1508 return GST_FLOW_ERROR;
1512 GstFlowReturn ret = queue->srcresult;
1513 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1514 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1519 GstFlowReturn ret = queue->srcresult;
1520 queue->last_query = FALSE;
1521 g_cond_signal (&queue->query_handled);
1522 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1523 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1529 gst_queue_loop (GstPad * pad)
1534 queue = (GstQueue *) GST_PAD_PARENT (pad);
1536 /* have to lock for thread-safety */
1537 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1539 while (gst_queue_is_empty (queue)) {
1540 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1541 if (!queue->silent) {
1542 GST_QUEUE_MUTEX_UNLOCK (queue);
1543 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1544 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1547 /* we recheck, the signal could have changed the thresholds */
1548 while (gst_queue_is_empty (queue)) {
1549 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1552 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1553 if (!queue->silent) {
1554 GST_QUEUE_MUTEX_UNLOCK (queue);
1555 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1556 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1557 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1561 ret = gst_queue_push_one (queue);
1562 queue->srcresult = ret;
1563 if (ret != GST_FLOW_OK)
1566 GST_QUEUE_MUTEX_UNLOCK (queue);
1573 gboolean eos = queue->eos;
1574 GstFlowReturn ret = queue->srcresult;
1576 gst_pad_pause_task (queue->srcpad);
1577 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1578 "pause task, reason: %s", gst_flow_get_name (ret));
1579 if (ret == GST_FLOW_FLUSHING) {
1580 gst_queue_locked_flush (queue, FALSE);
1582 GST_QUEUE_SIGNAL_DEL (queue);
1583 queue->last_query = FALSE;
1584 g_cond_signal (&queue->query_handled);
1586 GST_QUEUE_MUTEX_UNLOCK (queue);
1587 /* let app know about us giving up if upstream is not expected to do so */
1588 /* EOS is already taken care of elsewhere */
1589 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1590 GST_ELEMENT_FLOW_ERROR (queue, ret);
1591 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1598 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1600 gboolean res = TRUE;
1601 GstQueue *queue = GST_QUEUE (parent);
1603 #ifndef GST_DISABLE_GST_DEBUG
1604 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1605 event, GST_EVENT_TYPE (event));
1608 switch (GST_EVENT_TYPE (event)) {
1609 case GST_EVENT_RECONFIGURE:
1610 GST_QUEUE_MUTEX_LOCK (queue);
1611 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1612 /* when we got not linked, assume downstream is linked again now and we
1613 * can try to start pushing again */
1614 queue->srcresult = GST_FLOW_OK;
1615 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1617 GST_QUEUE_MUTEX_UNLOCK (queue);
1619 res = gst_pad_push_event (queue->sinkpad, event);
1622 res = gst_pad_event_default (pad, parent, event);
1631 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1633 GstQueue *queue = GST_QUEUE (parent);
1636 switch (GST_QUERY_TYPE (query)) {
1637 case GST_QUERY_SCHEDULING:{
1638 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1643 res = gst_pad_query_default (pad, parent, query);
1650 /* Adjust peer response for data contained in queue */
1651 switch (GST_QUERY_TYPE (query)) {
1652 case GST_QUERY_POSITION:
1657 /* get peer position */
1658 gst_query_parse_position (query, &format, &peer_pos);
1660 /* FIXME: this code assumes that there's no discont in the queue */
1662 case GST_FORMAT_BYTES:
1663 peer_pos -= queue->cur_level.bytes;
1664 if (peer_pos < 0) /* Clamp result to 0 */
1667 case GST_FORMAT_TIME:
1668 peer_pos -= queue->cur_level.time;
1669 if (peer_pos < 0) /* Clamp result to 0 */
1673 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1674 "know how to adjust value", gst_format_get_name (format));
1677 /* set updated position */
1678 gst_query_set_position (query, format, peer_pos);
1681 case GST_QUERY_LATENCY:
1684 GstClockTime min, max;
1686 gst_query_parse_latency (query, &live, &min, &max);
1688 /* we can delay up to the limit of the queue in time. If we have no time
1689 * limit, the best thing we can do is to return an infinite delay. In
1690 * reality a better estimate would be the byte/buffer rate but that is not
1691 * possible right now. */
1692 /* TODO: Use CONVERT query? */
1693 if (queue->max_size.time > 0 && max != -1
1694 && queue->leaky == GST_QUEUE_NO_LEAK)
1695 max += queue->max_size.time;
1696 else if (queue->max_size.time > 0 && queue->leaky != GST_QUEUE_NO_LEAK)
1697 max = MIN (queue->max_size.time, max);
1701 /* adjust for min-threshold */
1702 if (queue->min_threshold.time > 0)
1703 min += queue->min_threshold.time;
1705 gst_query_set_latency (query, live, min, max);
1709 /* peer handled other queries */
1717 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1723 queue = GST_QUEUE (parent);
1726 case GST_PAD_MODE_PUSH:
1728 GST_QUEUE_MUTEX_LOCK (queue);
1729 queue->srcresult = GST_FLOW_OK;
1731 queue->unexpected = FALSE;
1732 GST_QUEUE_MUTEX_UNLOCK (queue);
1734 /* step 1, unblock chain function */
1735 GST_QUEUE_MUTEX_LOCK (queue);
1736 queue->srcresult = GST_FLOW_FLUSHING;
1737 /* the item del signal will unblock */
1738 GST_QUEUE_SIGNAL_DEL (queue);
1739 GST_QUEUE_MUTEX_UNLOCK (queue);
1741 /* step 2, wait until streaming thread stopped and flush queue */
1742 GST_PAD_STREAM_LOCK (pad);
1743 GST_QUEUE_MUTEX_LOCK (queue);
1744 gst_queue_locked_flush (queue, TRUE);
1745 GST_QUEUE_MUTEX_UNLOCK (queue);
1746 GST_PAD_STREAM_UNLOCK (pad);
1758 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1764 queue = GST_QUEUE (parent);
1767 case GST_PAD_MODE_PUSH:
1769 GST_QUEUE_MUTEX_LOCK (queue);
1770 queue->srcresult = GST_FLOW_OK;
1772 queue->unexpected = FALSE;
1774 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1776 GST_QUEUE_MUTEX_UNLOCK (queue);
1778 /* step 1, unblock loop function */
1779 GST_QUEUE_MUTEX_LOCK (queue);
1780 queue->srcresult = GST_FLOW_FLUSHING;
1781 /* the item add signal will unblock */
1782 g_cond_signal (&queue->item_add);
1783 GST_QUEUE_MUTEX_UNLOCK (queue);
1785 /* step 2, make sure streaming finishes */
1786 result = gst_pad_stop_task (pad);
1788 GST_QUEUE_MUTEX_LOCK (queue);
1789 gst_queue_locked_flush (queue, FALSE);
1790 GST_QUEUE_MUTEX_UNLOCK (queue);
1801 queue_capacity_change (GstQueue * queue)
1803 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1804 gst_queue_leak_downstream (queue);
1807 /* changing the capacity of the queue must wake up
1808 * the _chain function, it might have more room now
1809 * to store the buffer/event in the queue */
1810 GST_QUEUE_SIGNAL_DEL (queue);
1813 /* Changing the minimum required fill level must
1814 * wake up the _loop function as it might now
1815 * be able to preceed.
1817 #define QUEUE_THRESHOLD_CHANGE(q)\
1818 GST_QUEUE_SIGNAL_ADD (q);
1821 gst_queue_set_property (GObject * object,
1822 guint prop_id, const GValue * value, GParamSpec * pspec)
1824 GstQueue *queue = GST_QUEUE (object);
1826 /* someone could change levels here, and since this
1827 * affects the get/put funcs, we need to lock for safety. */
1828 GST_QUEUE_MUTEX_LOCK (queue);
1831 case PROP_MAX_SIZE_BYTES:
1832 queue->max_size.bytes = g_value_get_uint (value);
1833 queue_capacity_change (queue);
1835 case PROP_MAX_SIZE_BUFFERS:
1836 queue->max_size.buffers = g_value_get_uint (value);
1837 queue_capacity_change (queue);
1839 case PROP_MAX_SIZE_TIME:
1840 queue->max_size.time = g_value_get_uint64 (value);
1841 queue_capacity_change (queue);
1843 case PROP_MIN_THRESHOLD_BYTES:
1844 queue->min_threshold.bytes = g_value_get_uint (value);
1845 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1846 QUEUE_THRESHOLD_CHANGE (queue);
1848 case PROP_MIN_THRESHOLD_BUFFERS:
1849 queue->min_threshold.buffers = g_value_get_uint (value);
1850 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1851 QUEUE_THRESHOLD_CHANGE (queue);
1853 case PROP_MIN_THRESHOLD_TIME:
1854 queue->min_threshold.time = g_value_get_uint64 (value);
1855 queue->orig_min_threshold.time = queue->min_threshold.time;
1856 QUEUE_THRESHOLD_CHANGE (queue);
1859 queue->leaky = g_value_get_enum (value);
1862 queue->silent = g_value_get_boolean (value);
1864 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
1865 case PROP_EMPTY_BUFFERS:
1866 queue->empty_buffers = g_value_get_boolean (value);
1867 GST_INFO_OBJECT(queue, "set empty buffer : %d", queue->empty_buffers);
1868 if (queue->empty_buffers) {
1869 gst_queue_locked_flush(queue, FALSE);
1871 GST_INFO_OBJECT(queue, "done");
1873 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
1874 case PROP_FLUSH_ON_EOS:
1875 queue->flush_on_eos = g_value_get_boolean (value);
1878 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1882 GST_QUEUE_MUTEX_UNLOCK (queue);
1886 gst_queue_get_property (GObject * object,
1887 guint prop_id, GValue * value, GParamSpec * pspec)
1889 GstQueue *queue = GST_QUEUE (object);
1891 GST_QUEUE_MUTEX_LOCK (queue);
1894 case PROP_CUR_LEVEL_BYTES:
1895 g_value_set_uint (value, queue->cur_level.bytes);
1897 case PROP_CUR_LEVEL_BUFFERS:
1898 g_value_set_uint (value, queue->cur_level.buffers);
1900 case PROP_CUR_LEVEL_TIME:
1901 g_value_set_uint64 (value, queue->cur_level.time);
1903 case PROP_MAX_SIZE_BYTES:
1904 g_value_set_uint (value, queue->max_size.bytes);
1906 case PROP_MAX_SIZE_BUFFERS:
1907 g_value_set_uint (value, queue->max_size.buffers);
1909 case PROP_MAX_SIZE_TIME:
1910 g_value_set_uint64 (value, queue->max_size.time);
1912 case PROP_MIN_THRESHOLD_BYTES:
1913 g_value_set_uint (value, queue->min_threshold.bytes);
1915 case PROP_MIN_THRESHOLD_BUFFERS:
1916 g_value_set_uint (value, queue->min_threshold.buffers);
1918 case PROP_MIN_THRESHOLD_TIME:
1919 g_value_set_uint64 (value, queue->min_threshold.time);
1922 g_value_set_enum (value, queue->leaky);
1925 g_value_set_boolean (value, queue->silent);
1927 #ifdef TIZEN_FEATURE_QUEUE_MODIFICATION
1928 case PROP_EMPTY_BUFFERS:
1929 g_value_set_boolean(value, queue->empty_buffers);
1931 #endif /* TIZEN_FEATURE_QUEUE_MODIFICATION */
1932 case PROP_FLUSH_ON_EOS:
1933 g_value_set_boolean (value, queue->flush_on_eos);
1936 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1940 GST_QUEUE_MUTEX_UNLOCK (queue);