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,
127 /* default property values */
128 #define DEFAULT_MAX_SIZE_BUFFERS 200 /* 200 buffers */
129 #define DEFAULT_MAX_SIZE_BYTES (10 * 1024 * 1024) /* 10 MB */
130 #define DEFAULT_MAX_SIZE_TIME GST_SECOND /* 1 second */
132 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
133 g_mutex_lock (&q->qlock); \
136 #define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START { \
137 GST_QUEUE_MUTEX_LOCK (q); \
138 if (q->srcresult != GST_FLOW_OK) \
142 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
143 g_mutex_unlock (&q->qlock); \
146 #define GST_QUEUE_WAIT_DEL_CHECK(q, label) G_STMT_START { \
147 STATUS (q, q->sinkpad, "wait for DEL"); \
148 q->waiting_del = TRUE; \
149 g_cond_wait (&q->item_del, &q->qlock); \
150 q->waiting_del = FALSE; \
151 if (q->srcresult != GST_FLOW_OK) { \
152 STATUS (q, q->srcpad, "received DEL wakeup"); \
155 STATUS (q, q->sinkpad, "received DEL"); \
158 #define GST_QUEUE_WAIT_ADD_CHECK(q, label) G_STMT_START { \
159 STATUS (q, q->srcpad, "wait for ADD"); \
160 q->waiting_add = TRUE; \
161 g_cond_wait (&q->item_add, &q->qlock); \
162 q->waiting_add = FALSE; \
163 if (q->srcresult != GST_FLOW_OK) { \
164 STATUS (q, q->srcpad, "received ADD wakeup"); \
167 STATUS (q, q->srcpad, "received ADD"); \
170 #define GST_QUEUE_SIGNAL_DEL(q) G_STMT_START { \
171 if (q->waiting_del) { \
172 STATUS (q, q->srcpad, "signal DEL"); \
173 g_cond_signal (&q->item_del); \
177 #define GST_QUEUE_SIGNAL_ADD(q) G_STMT_START { \
178 if (q->waiting_add) { \
179 STATUS (q, q->sinkpad, "signal ADD"); \
180 g_cond_signal (&q->item_add); \
185 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue", 0, "queue element"); \
186 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0, \
187 "dataflow inside the queue element");
188 #define gst_queue_parent_class parent_class
189 G_DEFINE_TYPE_WITH_CODE (GstQueue, gst_queue, GST_TYPE_ELEMENT, _do_init);
191 static void gst_queue_finalize (GObject * object);
192 static void gst_queue_set_property (GObject * object,
193 guint prop_id, const GValue * value, GParamSpec * pspec);
194 static void gst_queue_get_property (GObject * object,
195 guint prop_id, GValue * value, GParamSpec * pspec);
197 static GstFlowReturn gst_queue_chain (GstPad * pad, GstObject * parent,
199 static GstFlowReturn gst_queue_chain_list (GstPad * pad, GstObject * parent,
200 GstBufferList * buffer_list);
201 static GstFlowReturn gst_queue_push_one (GstQueue * queue);
202 static void gst_queue_loop (GstPad * pad);
204 static GstFlowReturn gst_queue_handle_sink_event (GstPad * pad,
205 GstObject * parent, GstEvent * event);
206 static gboolean gst_queue_handle_sink_query (GstPad * pad, GstObject * parent,
209 static gboolean gst_queue_handle_src_event (GstPad * pad, GstObject * parent,
211 static gboolean gst_queue_handle_src_query (GstPad * pad, GstObject * parent,
214 static void gst_queue_locked_flush (GstQueue * queue, gboolean full);
216 static gboolean gst_queue_src_activate_mode (GstPad * pad, GstObject * parent,
217 GstPadMode mode, gboolean active);
218 static gboolean gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
219 GstPadMode mode, gboolean active);
221 static gboolean gst_queue_is_empty (GstQueue * queue);
222 static gboolean gst_queue_is_filled (GstQueue * queue);
232 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
235 queue_leaky_get_type (void)
237 static GType queue_leaky_type = 0;
238 static const GEnumValue queue_leaky[] = {
239 {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
240 {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
241 {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
246 if (!queue_leaky_type) {
247 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
249 return queue_leaky_type;
252 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
255 gst_queue_class_init (GstQueueClass * klass)
257 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
258 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
260 gobject_class->set_property = gst_queue_set_property;
261 gobject_class->get_property = gst_queue_get_property;
265 * GstQueue::underrun:
266 * @queue: the queue instance
268 * Reports that the buffer became empty (underrun).
269 * A buffer is empty if the total amount of data inside it (num-buffers, time,
270 * size) is lower than the boundary values which can be set through the
271 * GObject properties.
273 gst_queue_signals[SIGNAL_UNDERRUN] =
274 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
275 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
276 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
279 * @queue: the queue instance
281 * Reports that enough (min-threshold) data is in the queue. Use this signal
282 * together with the underrun signal to pause the pipeline on underrun and
283 * wait for the queue to fill-up before resume playback.
285 gst_queue_signals[SIGNAL_RUNNING] =
286 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
287 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
288 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
291 * @queue: the queue instance
293 * Reports that the buffer became full (overrun).
294 * A buffer is full if the total amount of data inside it (num-buffers, time,
295 * size) is higher than the boundary values which can be set through the
296 * GObject properties.
298 gst_queue_signals[SIGNAL_OVERRUN] =
299 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
300 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
301 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
304 * @queue: the queue instance
306 * Reports when the queue has enough data to start pushing data again on the
309 gst_queue_signals[SIGNAL_PUSHING] =
310 g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
311 G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
312 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
315 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
316 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
317 "Current amount of data in the queue (bytes)",
318 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
320 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
321 "Current number of buffers in the queue",
322 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
324 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
325 "Current amount of data in the queue (in ns)",
326 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
329 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
330 "Max. amount of data in the queue (bytes, 0=disable)",
331 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
332 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
333 G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
335 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
336 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
337 DEFAULT_MAX_SIZE_BUFFERS,
338 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
339 G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
341 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
342 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
343 DEFAULT_MAX_SIZE_TIME,
344 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
345 G_PARAM_STATIC_STRINGS));
347 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
348 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
349 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
351 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
352 G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
354 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
355 "Min. number of buffers in the queue to allow reading (0=disable)", 0,
357 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
358 G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
360 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
361 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
363 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
364 G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (gobject_class, PROP_LEAKY,
367 g_param_spec_enum ("leaky", "Leaky",
368 "Where the queue leaks, if at all",
369 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
370 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
371 G_PARAM_STATIC_STRINGS));
376 * Don't emit queue signals. Makes queues more lightweight if no signals are
379 g_object_class_install_property (gobject_class, PROP_SILENT,
380 g_param_spec_boolean ("silent", "Silent",
381 "Don't emit queue signals", FALSE,
382 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
383 G_PARAM_STATIC_STRINGS));
386 * GstQueue:flush-on-eos
388 * Discard all data in the queue when an EOS event is received, and pass
389 * on the EOS event as soon as possible (instead of waiting until all
390 * buffers in the queue have been processed, which is the default behaviour).
392 * Flushing the queue on EOS might be useful when capturing and encoding
393 * from a live source, to finish up the recording quickly in cases when
394 * the encoder is slow. Note that this might mean some data from the end of
395 * the recording data might be lost though (never more than the configured
396 * max. sizes though).
400 g_object_class_install_property (gobject_class, PROP_FLUSH_ON_EOS,
401 g_param_spec_boolean ("flush-on-eos", "Flush on EOS",
402 "Discard all data in the queue when an EOS event is received", FALSE,
403 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
404 G_PARAM_STATIC_STRINGS));
406 gobject_class->finalize = gst_queue_finalize;
408 gst_element_class_set_static_metadata (gstelement_class,
410 "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
411 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
412 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
414 /* Registering debug symbols for function pointers */
415 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
416 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
417 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
418 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
419 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
420 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
421 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain_list);
425 gst_queue_init (GstQueue * queue)
427 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
429 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
430 gst_pad_set_chain_list_function (queue->sinkpad, gst_queue_chain_list);
431 gst_pad_set_activatemode_function (queue->sinkpad,
432 gst_queue_sink_activate_mode);
433 gst_pad_set_event_full_function (queue->sinkpad, gst_queue_handle_sink_event);
434 gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
435 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
436 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
438 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
440 gst_pad_set_activatemode_function (queue->srcpad,
441 gst_queue_src_activate_mode);
442 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
443 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
444 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
445 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
447 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
448 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
449 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
450 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
451 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
452 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
453 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
454 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
455 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
457 queue->leaky = GST_QUEUE_NO_LEAK;
458 queue->srcresult = GST_FLOW_FLUSHING;
460 g_mutex_init (&queue->qlock);
461 g_cond_init (&queue->item_add);
462 g_cond_init (&queue->item_del);
463 g_cond_init (&queue->query_handled);
466 gst_queue_array_new_for_struct (sizeof (GstQueueItem),
467 DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
469 queue->sinktime = GST_CLOCK_STIME_NONE;
470 queue->srctime = GST_CLOCK_STIME_NONE;
472 queue->sink_tainted = TRUE;
473 queue->src_tainted = TRUE;
475 queue->newseg_applied_to_src = FALSE;
477 GST_DEBUG_OBJECT (queue,
478 "initialized queue's not_empty & not_full conditions");
481 /* called only once, as opposed to dispose */
483 gst_queue_finalize (GObject * object)
485 GstQueue *queue = GST_QUEUE (object);
488 GST_DEBUG_OBJECT (queue, "finalizing queue");
490 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
491 /* FIXME: if it's a query, shouldn't we unref that too? */
492 if (!qitem->is_query)
493 gst_mini_object_unref (qitem->item);
495 gst_queue_array_free (queue->queue);
497 g_mutex_clear (&queue->qlock);
498 g_cond_clear (&queue->item_add);
499 g_cond_clear (&queue->item_del);
500 g_cond_clear (&queue->query_handled);
502 G_OBJECT_CLASS (parent_class)->finalize (object);
505 /* Convenience function */
506 static inline GstClockTimeDiff
507 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
509 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
511 if (GST_CLOCK_TIME_IS_VALID (val)) {
513 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
522 /* calculate the diff between running time on the sink and src of the queue.
523 * This is the total amount of time in the queue. */
525 update_time_level (GstQueue * queue)
527 gint64 sink_time, src_time;
529 if (queue->sink_tainted) {
530 GST_LOG_OBJECT (queue, "update sink time");
532 my_segment_to_running_time (&queue->sink_segment,
533 queue->sink_segment.position);
534 queue->sink_tainted = FALSE;
536 sink_time = queue->sinktime;
538 if (queue->src_tainted) {
539 GST_LOG_OBJECT (queue, "update src time");
541 my_segment_to_running_time (&queue->src_segment,
542 queue->src_segment.position);
543 queue->src_tainted = FALSE;
545 src_time = queue->srctime;
547 GST_LOG_OBJECT (queue, "sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT,
548 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
550 if (sink_time >= src_time)
551 queue->cur_level.time = sink_time - src_time;
553 queue->cur_level.time = 0;
556 /* take a SEGMENT event and apply the values to segment, updating the time
559 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
562 gst_event_copy_segment (event, segment);
564 /* now configure the values, we use these to track timestamps on the
566 if (segment->format != GST_FORMAT_TIME) {
567 /* non-time format, pretent the current time segment is closed with a
568 * 0 start and unknown stop time. */
569 segment->format = GST_FORMAT_TIME;
575 queue->sink_tainted = TRUE;
577 queue->src_tainted = TRUE;
579 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
581 /* segment can update the time level of the queue */
582 update_time_level (queue);
586 apply_gap (GstQueue * queue, GstEvent * event,
587 GstSegment * segment, gboolean is_sink)
589 GstClockTime timestamp;
590 GstClockTime duration;
592 gst_event_parse_gap (event, ×tamp, &duration);
594 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
596 if (GST_CLOCK_TIME_IS_VALID (duration)) {
597 timestamp += duration;
600 segment->position = timestamp;
603 queue->sink_tainted = TRUE;
605 queue->src_tainted = TRUE;
607 /* calc diff with other end */
608 update_time_level (queue);
613 /* take a buffer and update segment, updating the time level of the queue. */
615 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
618 GstClockTime duration, timestamp;
620 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
621 duration = GST_BUFFER_DURATION (buffer);
623 /* if no timestamp is set, assume it's continuous with the previous
625 if (timestamp == GST_CLOCK_TIME_NONE)
626 timestamp = segment->position;
629 if (duration != GST_CLOCK_TIME_NONE)
630 timestamp += duration;
632 GST_LOG_OBJECT (queue, "%s position updated to %" GST_TIME_FORMAT,
633 segment == &queue->sink_segment ? "sink" : "src",
634 GST_TIME_ARGS (timestamp));
636 segment->position = timestamp;
638 queue->sink_tainted = TRUE;
640 queue->src_tainted = TRUE;
643 /* calc diff with other end */
644 update_time_level (queue);
648 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer user_data)
650 GstClockTime *timestamp = user_data;
653 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
654 " duration %" GST_TIME_FORMAT, idx, GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
655 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
656 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
658 btime = GST_BUFFER_DTS_OR_PTS (*buf);
659 if (GST_CLOCK_TIME_IS_VALID (btime))
662 if (GST_BUFFER_DURATION_IS_VALID (*buf))
663 *timestamp += GST_BUFFER_DURATION (*buf);
665 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
670 /* take a buffer list and update segment, updating the time level of the queue */
672 apply_buffer_list (GstQueue * queue, GstBufferList * buffer_list,
673 GstSegment * segment, gboolean sink)
675 GstClockTime timestamp;
677 /* if no timestamp is set, assume it's continuous with the previous time */
678 timestamp = segment->position;
680 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
682 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
683 GST_TIME_ARGS (timestamp));
685 segment->position = timestamp;
688 queue->sink_tainted = TRUE;
690 queue->src_tainted = TRUE;
692 /* calc diff with other end */
693 update_time_level (queue);
697 gst_queue_locked_flush (GstQueue * queue, gboolean full)
701 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
702 /* Then lose another reference because we are supposed to destroy that
703 data when flushing */
704 if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
705 && GST_EVENT_IS_STICKY (qitem->item)
706 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
707 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
708 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
710 if (!qitem->is_query)
711 gst_mini_object_unref (qitem->item);
712 memset (qitem, 0, sizeof (GstQueueItem));
714 queue->last_query = FALSE;
715 g_cond_signal (&queue->query_handled);
716 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
717 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
718 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
719 queue->min_threshold.time = queue->orig_min_threshold.time;
720 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
721 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
722 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
724 queue->sinktime = queue->srctime = GST_CLOCK_STIME_NONE;
725 queue->sink_tainted = queue->src_tainted = TRUE;
727 /* we deleted a lot of something */
728 GST_QUEUE_SIGNAL_DEL (queue);
731 /* enqueue an item an update the level stats, with QUEUE_LOCK */
733 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
736 GstBuffer *buffer = GST_BUFFER_CAST (item);
737 gsize bsize = gst_buffer_get_size (buffer);
739 /* add buffer to the statistics */
740 queue->cur_level.buffers++;
741 queue->cur_level.bytes += bsize;
742 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
745 qitem.is_query = FALSE;
747 gst_queue_array_push_tail_struct (queue->queue, &qitem);
748 GST_QUEUE_SIGNAL_ADD (queue);
752 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
754 guint *p_size = data;
757 buf_size = gst_buffer_get_size (*buf);
758 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
764 gst_queue_locked_enqueue_buffer_list (GstQueue * queue, gpointer item)
767 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
770 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &bsize);
772 /* add buffer to the statistics */
773 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
774 queue->cur_level.bytes += bsize;
775 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
778 qitem.is_query = FALSE;
780 gst_queue_array_push_tail_struct (queue->queue, &qitem);
781 GST_QUEUE_SIGNAL_ADD (queue);
785 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
788 GstEvent *event = GST_EVENT_CAST (item);
790 switch (GST_EVENT_TYPE (event)) {
792 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
793 /* Zero the thresholds, this makes sure the queue is completely
794 * filled and we can read all data from the queue. */
795 if (queue->flush_on_eos)
796 gst_queue_locked_flush (queue, FALSE);
798 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
799 /* mark the queue as EOS. This prevents us from accepting more data. */
802 case GST_EVENT_SEGMENT:
803 apply_segment (queue, event, &queue->sink_segment, TRUE);
804 /* if the queue is empty, apply sink segment on the source */
805 if (gst_queue_array_is_empty (queue->queue)) {
806 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
807 apply_segment (queue, event, &queue->src_segment, FALSE);
808 queue->newseg_applied_to_src = TRUE;
810 /* a new segment allows us to accept more buffers if we got EOS
812 queue->unexpected = FALSE;
815 apply_gap (queue, event, &queue->sink_segment, TRUE);
822 qitem.is_query = FALSE;
824 gst_queue_array_push_tail_struct (queue->queue, &qitem);
825 GST_QUEUE_SIGNAL_ADD (queue);
828 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
829 static GstMiniObject *
830 gst_queue_locked_dequeue (GstQueue * queue)
836 qitem = gst_queue_array_pop_head_struct (queue->queue);
841 bufsize = qitem->size;
843 if (GST_IS_BUFFER (item)) {
844 GstBuffer *buffer = GST_BUFFER_CAST (item);
846 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
847 "retrieved buffer %p from queue", buffer);
849 queue->cur_level.buffers--;
850 queue->cur_level.bytes -= bufsize;
851 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
853 /* if the queue is empty now, update the other side */
854 if (queue->cur_level.buffers == 0)
855 queue->cur_level.time = 0;
856 } else if (GST_IS_BUFFER_LIST (item)) {
857 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
859 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
860 "retrieved buffer list %p from queue", buffer_list);
862 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
863 queue->cur_level.bytes -= bufsize;
864 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
866 /* if the queue is empty now, update the other side */
867 if (queue->cur_level.buffers == 0)
868 queue->cur_level.time = 0;
869 } else if (GST_IS_EVENT (item)) {
870 GstEvent *event = GST_EVENT_CAST (item);
872 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
873 "retrieved event %p from queue", event);
875 switch (GST_EVENT_TYPE (event)) {
877 /* queue is empty now that we dequeued the EOS */
878 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
880 case GST_EVENT_SEGMENT:
881 /* apply newsegment if it has not already been applied */
882 if (G_LIKELY (!queue->newseg_applied_to_src)) {
883 apply_segment (queue, event, &queue->src_segment, FALSE);
885 queue->newseg_applied_to_src = FALSE;
889 apply_gap (queue, event, &queue->src_segment, FALSE);
894 } else if (GST_IS_QUERY (item)) {
895 GstQuery *query = GST_QUERY_CAST (item);
897 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
898 "retrieved query %p from queue", query);
901 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
902 item, GST_OBJECT_NAME (queue));
905 GST_QUEUE_SIGNAL_DEL (queue);
912 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
918 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
923 queue = GST_QUEUE (parent);
925 switch (GST_EVENT_TYPE (event)) {
926 case GST_EVENT_FLUSH_START:
927 STATUS (queue, pad, "received flush start event");
929 ret = gst_pad_push_event (queue->srcpad, event);
931 /* now unblock the chain function */
932 GST_QUEUE_MUTEX_LOCK (queue);
933 queue->srcresult = GST_FLOW_FLUSHING;
934 /* unblock the loop and chain functions */
935 GST_QUEUE_SIGNAL_ADD (queue);
936 GST_QUEUE_SIGNAL_DEL (queue);
937 GST_QUEUE_MUTEX_UNLOCK (queue);
939 /* make sure it pauses, this should happen since we sent
940 * flush_start downstream. */
941 gst_pad_pause_task (queue->srcpad);
942 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
944 /* unblock query handler after the streaming thread is shut down.
945 * Otherwise downstream might have a query that is already unreffed
947 GST_QUEUE_MUTEX_LOCK (queue);
948 queue->last_query = FALSE;
949 g_cond_signal (&queue->query_handled);
950 GST_QUEUE_MUTEX_UNLOCK (queue);
952 case GST_EVENT_FLUSH_STOP:
953 STATUS (queue, pad, "received flush stop event");
955 ret = gst_pad_push_event (queue->srcpad, event);
957 GST_QUEUE_MUTEX_LOCK (queue);
958 gst_queue_locked_flush (queue, FALSE);
959 queue->srcresult = GST_FLOW_OK;
961 queue->unexpected = FALSE;
962 if (gst_pad_is_active (queue->srcpad)) {
963 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
964 queue->srcpad, NULL);
966 GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
967 "pad not active any longer");
969 GST_QUEUE_MUTEX_UNLOCK (queue);
971 STATUS (queue, pad, "after flush");
974 if (GST_EVENT_IS_SERIALIZED (event)) {
975 /* serialized events go in the queue */
976 GST_QUEUE_MUTEX_LOCK (queue);
977 if (queue->srcresult != GST_FLOW_OK) {
978 /* Errors in sticky event pushing are no problem and ignored here
979 * as they will cause more meaningful errors during data flow.
980 * For EOS events, that are not followed by data flow, we still
981 * return FALSE here though and report an error.
983 if (!GST_EVENT_IS_STICKY (event)) {
984 GST_QUEUE_MUTEX_UNLOCK (queue);
986 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
987 if (queue->srcresult == GST_FLOW_NOT_LINKED
988 || queue->srcresult < GST_FLOW_EOS) {
989 GST_QUEUE_MUTEX_UNLOCK (queue);
990 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
992 GST_QUEUE_MUTEX_UNLOCK (queue);
997 /* refuse more events on EOS */
1000 gst_queue_locked_enqueue_event (queue, event);
1001 GST_QUEUE_MUTEX_UNLOCK (queue);
1003 /* non-serialized events are forwarded downstream immediately */
1004 ret = gst_pad_push_event (queue->srcpad, event);
1009 GST_ERROR_OBJECT (queue, "Failed to push event");
1010 return GST_FLOW_ERROR;
1017 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
1018 GST_QUEUE_MUTEX_UNLOCK (queue);
1019 gst_event_unref (event);
1020 return GST_FLOW_EOS;
1024 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1025 "refusing event, we have a downstream flow error: %s",
1026 gst_flow_get_name (queue->srcresult));
1027 gst_event_unref (event);
1028 return queue->srcresult;
1033 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1035 GstQueue *queue = GST_QUEUE_CAST (parent);
1038 switch (GST_QUERY_TYPE (query)) {
1040 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
1043 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1044 GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
1045 GST_QUERY_TYPE_NAME (query));
1046 qitem.item = GST_MINI_OBJECT_CAST (query);
1047 qitem.is_query = TRUE;
1049 gst_queue_array_push_tail_struct (queue->queue, &qitem);
1050 GST_QUEUE_SIGNAL_ADD (queue);
1051 while (queue->srcresult == GST_FLOW_OK &&
1052 queue->last_handled_query != query)
1053 g_cond_wait (&queue->query_handled, &queue->qlock);
1054 queue->last_handled_query = NULL;
1055 if (queue->srcresult != GST_FLOW_OK)
1057 res = queue->last_query;
1058 GST_QUEUE_MUTEX_UNLOCK (queue);
1060 res = gst_pad_query_default (pad, parent, query);
1069 GST_DEBUG_OBJECT (queue, "we are flushing");
1070 GST_QUEUE_MUTEX_UNLOCK (queue);
1076 gst_queue_is_empty (GstQueue * queue)
1080 head = gst_queue_array_peek_head_struct (queue->queue);
1085 /* Only consider the queue empty if the minimum thresholds
1086 * are not reached and data is at the queue head. Otherwise
1087 * we would block forever on serialized queries.
1089 if (!GST_IS_BUFFER (head->item) && !GST_IS_BUFFER_LIST (head->item))
1092 /* It is possible that a max size is reached before all min thresholds are.
1093 * Therefore, only consider it empty if it is not filled. */
1094 return ((queue->min_threshold.buffers > 0 &&
1095 queue->cur_level.buffers < queue->min_threshold.buffers) ||
1096 (queue->min_threshold.bytes > 0 &&
1097 queue->cur_level.bytes < queue->min_threshold.bytes) ||
1098 (queue->min_threshold.time > 0 &&
1099 queue->cur_level.time < queue->min_threshold.time)) &&
1100 !gst_queue_is_filled (queue);
1104 gst_queue_is_filled (GstQueue * queue)
1106 return (((queue->max_size.buffers > 0 &&
1107 queue->cur_level.buffers >= queue->max_size.buffers) ||
1108 (queue->max_size.bytes > 0 &&
1109 queue->cur_level.bytes >= queue->max_size.bytes) ||
1110 (queue->max_size.time > 0 &&
1111 queue->cur_level.time >= queue->max_size.time)));
1115 gst_queue_leak_downstream (GstQueue * queue)
1117 /* for as long as the queue is filled, dequeue an item and discard it */
1118 while (gst_queue_is_filled (queue)) {
1119 GstMiniObject *leak;
1121 leak = gst_queue_locked_dequeue (queue);
1122 /* there is nothing to dequeue and the queue is still filled.. This should
1124 g_assert (leak != NULL);
1126 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1127 "queue is full, leaking item %p on downstream end", leak);
1128 if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
1129 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1130 "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
1131 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
1134 if (!GST_IS_QUERY (leak))
1135 gst_mini_object_unref (leak);
1137 /* last buffer needs to get a DISCONT flag */
1138 queue->head_needs_discont = TRUE;
1143 discont_first_buffer (GstBuffer ** buffer, guint i, gpointer user_data)
1145 GstQueue *queue = user_data;
1146 GstBuffer *subbuffer = gst_buffer_make_writable (*buffer);
1149 *buffer = subbuffer;
1150 GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
1152 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1158 static GstFlowReturn
1159 gst_queue_chain_buffer_or_list (GstPad * pad, GstObject * parent,
1160 GstMiniObject * obj, gboolean is_list)
1164 queue = GST_QUEUE_CAST (parent);
1166 /* we have to lock the queue since we span threads */
1167 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1168 /* when we received EOS, we refuse any more data */
1171 if (queue->unexpected)
1172 goto out_unexpected;
1175 GstClockTime duration, timestamp;
1176 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1178 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1179 duration = GST_BUFFER_DURATION (buffer);
1181 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
1182 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1183 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1184 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1186 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1187 "received buffer list %p with %u buffers", obj,
1188 gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)));
1191 /* We make space available if we're "full" according to whatever
1192 * the user defined as "full". Note that this only applies to buffers.
1193 * We always handle events and they don't count in our statistics. */
1194 while (gst_queue_is_filled (queue)) {
1195 if (!queue->silent) {
1196 GST_QUEUE_MUTEX_UNLOCK (queue);
1197 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
1198 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1199 /* we recheck, the signal could have changed the thresholds */
1200 if (!gst_queue_is_filled (queue))
1204 /* how are we going to make space for this buffer? */
1205 switch (queue->leaky) {
1206 case GST_QUEUE_LEAK_UPSTREAM:
1207 /* next buffer needs to get a DISCONT flag */
1208 queue->tail_needs_discont = TRUE;
1209 /* leak current buffer */
1210 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1211 "queue is full, leaking buffer on upstream end");
1212 /* now we can clean up and exit right away */
1214 case GST_QUEUE_LEAK_DOWNSTREAM:
1215 gst_queue_leak_downstream (queue);
1218 g_warning ("Unknown leaky type, using default");
1220 case GST_QUEUE_NO_LEAK:
1222 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1223 "queue is full, waiting for free space");
1225 /* don't leak. Instead, wait for space to be available */
1227 /* for as long as the queue is filled, wait till an item was deleted. */
1228 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1229 } while (gst_queue_is_filled (queue));
1231 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1233 if (!queue->silent) {
1234 GST_QUEUE_MUTEX_UNLOCK (queue);
1235 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1236 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1243 if (queue->tail_needs_discont) {
1245 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1246 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1250 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1252 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1255 obj = GST_MINI_OBJECT_CAST (buffer);
1257 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
1259 buffer_list = gst_buffer_list_make_writable (buffer_list);
1260 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1261 obj = GST_MINI_OBJECT_CAST (buffer_list);
1263 queue->tail_needs_discont = FALSE;
1266 /* put buffer in queue now */
1268 gst_queue_locked_enqueue_buffer_list (queue, obj);
1270 gst_queue_locked_enqueue_buffer (queue, obj);
1271 GST_QUEUE_MUTEX_UNLOCK (queue);
1275 /* special conditions */
1278 GST_QUEUE_MUTEX_UNLOCK (queue);
1280 gst_mini_object_unref (obj);
1286 GstFlowReturn ret = queue->srcresult;
1288 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1289 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1290 GST_QUEUE_MUTEX_UNLOCK (queue);
1291 gst_mini_object_unref (obj);
1297 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1298 GST_QUEUE_MUTEX_UNLOCK (queue);
1300 gst_mini_object_unref (obj);
1302 return GST_FLOW_EOS;
1306 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1307 GST_QUEUE_MUTEX_UNLOCK (queue);
1309 gst_mini_object_unref (obj);
1311 return GST_FLOW_EOS;
1315 static GstFlowReturn
1316 gst_queue_chain_list (GstPad * pad, GstObject * parent,
1317 GstBufferList * buffer_list)
1319 return gst_queue_chain_buffer_or_list (pad, parent,
1320 GST_MINI_OBJECT_CAST (buffer_list), TRUE);
1323 static GstFlowReturn
1324 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1326 return gst_queue_chain_buffer_or_list (pad, parent,
1327 GST_MINI_OBJECT_CAST (buffer), FALSE);
1330 /* dequeue an item from the queue an push it downstream. This functions returns
1331 * the result of the push. */
1332 static GstFlowReturn
1333 gst_queue_push_one (GstQueue * queue)
1335 GstFlowReturn result = queue->srcresult;
1336 GstMiniObject *data;
1339 data = gst_queue_locked_dequeue (queue);
1344 is_list = GST_IS_BUFFER_LIST (data);
1346 if (GST_IS_BUFFER (data) || is_list) {
1350 buffer = GST_BUFFER_CAST (data);
1352 if (queue->head_needs_discont) {
1353 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1357 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1359 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1361 queue->head_needs_discont = FALSE;
1364 GST_QUEUE_MUTEX_UNLOCK (queue);
1365 result = gst_pad_push (queue->srcpad, buffer);
1367 GstBufferList *buffer_list;
1369 buffer_list = GST_BUFFER_LIST_CAST (data);
1371 if (queue->head_needs_discont) {
1372 buffer_list = gst_buffer_list_make_writable (buffer_list);
1373 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1374 queue->head_needs_discont = FALSE;
1377 GST_QUEUE_MUTEX_UNLOCK (queue);
1378 result = gst_pad_push_list (queue->srcpad, buffer_list);
1381 /* need to check for srcresult here as well */
1382 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1384 if (result == GST_FLOW_EOS) {
1385 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1386 /* stop pushing buffers, we dequeue all items until we see an item that we
1387 * can push again, which is EOS or SEGMENT. If there is nothing in the
1388 * queue we can push, we set a flag to make the sinkpad refuse more
1389 * buffers with an EOS return value. */
1390 while ((data = gst_queue_locked_dequeue (queue))) {
1391 if (GST_IS_BUFFER (data)) {
1392 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1393 "dropping EOS buffer %p", data);
1394 gst_buffer_unref (GST_BUFFER_CAST (data));
1395 } else if (GST_IS_BUFFER_LIST (data)) {
1396 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1397 "dropping EOS buffer list %p", data);
1398 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
1399 } else if (GST_IS_EVENT (data)) {
1400 GstEvent *event = GST_EVENT_CAST (data);
1401 GstEventType type = GST_EVENT_TYPE (event);
1403 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
1404 /* we found a pushable item in the queue, push it out */
1405 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1406 "pushing pushable event %s after EOS",
1407 GST_EVENT_TYPE_NAME (event));
1410 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1411 "dropping EOS event %p", event);
1412 gst_event_unref (event);
1413 } else if (GST_IS_QUERY (data)) {
1414 GstQuery *query = GST_QUERY_CAST (data);
1416 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1417 "dropping query %p because of EOS", query);
1418 queue->last_query = FALSE;
1419 g_cond_signal (&queue->query_handled);
1422 /* no more items in the queue. Set the unexpected flag so that upstream
1423 * make us refuse any more buffers on the sinkpad. Since we will still
1424 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1425 * task function does not shut down. */
1426 queue->unexpected = TRUE;
1427 result = GST_FLOW_OK;
1429 } else if (GST_IS_EVENT (data)) {
1430 GstEvent *event = GST_EVENT_CAST (data);
1431 GstEventType type = GST_EVENT_TYPE (event);
1433 GST_QUEUE_MUTEX_UNLOCK (queue);
1435 gst_pad_push_event (queue->srcpad, event);
1437 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1438 /* if we're EOS, return EOS so that the task pauses. */
1439 if (type == GST_EVENT_EOS) {
1440 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1441 "pushed EOS event %p, return EOS", event);
1442 result = GST_FLOW_EOS;
1444 } else if (GST_IS_QUERY (data)) {
1445 GstQuery *query = GST_QUERY_CAST (data);
1448 GST_QUEUE_MUTEX_UNLOCK (queue);
1449 ret = gst_pad_peer_query (queue->srcpad, query);
1450 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1451 queue->last_query = ret;
1452 queue->last_handled_query = query;
1453 g_cond_signal (&queue->query_handled);
1454 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1455 "did query %p, return %d", query, queue->last_query);
1462 GST_CAT_ERROR_OBJECT (queue_dataflow, queue,
1463 "exit because we have no item in the queue");
1464 return GST_FLOW_ERROR;
1468 GstFlowReturn ret = queue->srcresult;
1469 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1470 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1475 GstFlowReturn ret = queue->srcresult;
1476 queue->last_query = FALSE;
1477 g_cond_signal (&queue->query_handled);
1478 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1479 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1485 gst_queue_loop (GstPad * pad)
1490 queue = (GstQueue *) GST_PAD_PARENT (pad);
1492 /* have to lock for thread-safety */
1493 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1495 while (gst_queue_is_empty (queue)) {
1496 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1497 if (!queue->silent) {
1498 GST_QUEUE_MUTEX_UNLOCK (queue);
1499 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1500 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1503 /* we recheck, the signal could have changed the thresholds */
1504 while (gst_queue_is_empty (queue)) {
1505 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1508 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1509 if (!queue->silent) {
1510 GST_QUEUE_MUTEX_UNLOCK (queue);
1511 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1512 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1513 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1517 ret = gst_queue_push_one (queue);
1518 queue->srcresult = ret;
1519 if (ret != GST_FLOW_OK)
1522 GST_QUEUE_MUTEX_UNLOCK (queue);
1529 gboolean eos = queue->eos;
1530 GstFlowReturn ret = queue->srcresult;
1532 gst_pad_pause_task (queue->srcpad);
1533 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1534 "pause task, reason: %s", gst_flow_get_name (ret));
1535 if (ret == GST_FLOW_FLUSHING) {
1536 gst_queue_locked_flush (queue, FALSE);
1538 GST_QUEUE_SIGNAL_DEL (queue);
1539 queue->last_query = FALSE;
1540 g_cond_signal (&queue->query_handled);
1542 GST_QUEUE_MUTEX_UNLOCK (queue);
1543 /* let app know about us giving up if upstream is not expected to do so */
1544 /* EOS is already taken care of elsewhere */
1545 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1546 GST_ELEMENT_FLOW_ERROR (queue, ret);
1547 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1554 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1556 gboolean res = TRUE;
1557 GstQueue *queue = GST_QUEUE (parent);
1559 #ifndef GST_DISABLE_GST_DEBUG
1560 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1561 event, GST_EVENT_TYPE (event));
1564 switch (GST_EVENT_TYPE (event)) {
1565 case GST_EVENT_RECONFIGURE:
1566 GST_QUEUE_MUTEX_LOCK (queue);
1567 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1568 /* when we got not linked, assume downstream is linked again now and we
1569 * can try to start pushing again */
1570 queue->srcresult = GST_FLOW_OK;
1571 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1573 GST_QUEUE_MUTEX_UNLOCK (queue);
1575 res = gst_pad_push_event (queue->sinkpad, event);
1578 res = gst_pad_event_default (pad, parent, event);
1587 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1589 GstQueue *queue = GST_QUEUE (parent);
1592 switch (GST_QUERY_TYPE (query)) {
1593 case GST_QUERY_SCHEDULING:{
1594 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1599 res = gst_pad_query_default (pad, parent, query);
1606 /* Adjust peer response for data contained in queue */
1607 switch (GST_QUERY_TYPE (query)) {
1608 case GST_QUERY_POSITION:
1613 /* get peer position */
1614 gst_query_parse_position (query, &format, &peer_pos);
1616 /* FIXME: this code assumes that there's no discont in the queue */
1618 case GST_FORMAT_BYTES:
1619 peer_pos -= queue->cur_level.bytes;
1620 if (peer_pos < 0) /* Clamp result to 0 */
1623 case GST_FORMAT_TIME:
1624 peer_pos -= queue->cur_level.time;
1625 if (peer_pos < 0) /* Clamp result to 0 */
1629 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1630 "know how to adjust value", gst_format_get_name (format));
1633 /* set updated position */
1634 gst_query_set_position (query, format, peer_pos);
1637 case GST_QUERY_LATENCY:
1640 GstClockTime min, max;
1642 gst_query_parse_latency (query, &live, &min, &max);
1644 /* we can delay up to the limit of the queue in time. If we have no time
1645 * limit, the best thing we can do is to return an infinite delay. In
1646 * reality a better estimate would be the byte/buffer rate but that is not
1647 * possible right now. */
1648 /* TODO: Use CONVERT query? */
1649 if (queue->max_size.time > 0 && max != -1
1650 && queue->leaky == GST_QUEUE_NO_LEAK)
1651 max += queue->max_size.time;
1652 else if (queue->max_size.time > 0 && queue->leaky != GST_QUEUE_NO_LEAK)
1653 max = MIN (queue->max_size.time, max);
1657 /* adjust for min-threshold */
1658 if (queue->min_threshold.time > 0)
1659 min += queue->min_threshold.time;
1661 gst_query_set_latency (query, live, min, max);
1665 /* peer handled other queries */
1673 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1679 queue = GST_QUEUE (parent);
1682 case GST_PAD_MODE_PUSH:
1684 GST_QUEUE_MUTEX_LOCK (queue);
1685 queue->srcresult = GST_FLOW_OK;
1687 queue->unexpected = FALSE;
1688 GST_QUEUE_MUTEX_UNLOCK (queue);
1690 /* step 1, unblock chain function */
1691 GST_QUEUE_MUTEX_LOCK (queue);
1692 queue->srcresult = GST_FLOW_FLUSHING;
1693 /* the item del signal will unblock */
1694 GST_QUEUE_SIGNAL_DEL (queue);
1695 GST_QUEUE_MUTEX_UNLOCK (queue);
1697 /* step 2, wait until streaming thread stopped and flush queue */
1698 GST_PAD_STREAM_LOCK (pad);
1699 GST_QUEUE_MUTEX_LOCK (queue);
1700 gst_queue_locked_flush (queue, TRUE);
1701 GST_QUEUE_MUTEX_UNLOCK (queue);
1702 GST_PAD_STREAM_UNLOCK (pad);
1714 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1720 queue = GST_QUEUE (parent);
1723 case GST_PAD_MODE_PUSH:
1725 GST_QUEUE_MUTEX_LOCK (queue);
1726 queue->srcresult = GST_FLOW_OK;
1728 queue->unexpected = FALSE;
1730 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1732 GST_QUEUE_MUTEX_UNLOCK (queue);
1734 /* step 1, unblock loop function */
1735 GST_QUEUE_MUTEX_LOCK (queue);
1736 queue->srcresult = GST_FLOW_FLUSHING;
1737 /* the item add signal will unblock */
1738 g_cond_signal (&queue->item_add);
1739 GST_QUEUE_MUTEX_UNLOCK (queue);
1741 /* step 2, make sure streaming finishes */
1742 result = gst_pad_stop_task (pad);
1744 GST_QUEUE_MUTEX_LOCK (queue);
1745 gst_queue_locked_flush (queue, FALSE);
1746 GST_QUEUE_MUTEX_UNLOCK (queue);
1757 queue_capacity_change (GstQueue * queue)
1759 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1760 gst_queue_leak_downstream (queue);
1763 /* changing the capacity of the queue must wake up
1764 * the _chain function, it might have more room now
1765 * to store the buffer/event in the queue */
1766 GST_QUEUE_SIGNAL_DEL (queue);
1769 /* Changing the minimum required fill level must
1770 * wake up the _loop function as it might now
1771 * be able to preceed.
1773 #define QUEUE_THRESHOLD_CHANGE(q)\
1774 GST_QUEUE_SIGNAL_ADD (q);
1777 gst_queue_set_property (GObject * object,
1778 guint prop_id, const GValue * value, GParamSpec * pspec)
1780 GstQueue *queue = GST_QUEUE (object);
1782 /* someone could change levels here, and since this
1783 * affects the get/put funcs, we need to lock for safety. */
1784 GST_QUEUE_MUTEX_LOCK (queue);
1787 case PROP_MAX_SIZE_BYTES:
1788 queue->max_size.bytes = g_value_get_uint (value);
1789 queue_capacity_change (queue);
1791 case PROP_MAX_SIZE_BUFFERS:
1792 queue->max_size.buffers = g_value_get_uint (value);
1793 queue_capacity_change (queue);
1795 case PROP_MAX_SIZE_TIME:
1796 queue->max_size.time = g_value_get_uint64 (value);
1797 queue_capacity_change (queue);
1799 case PROP_MIN_THRESHOLD_BYTES:
1800 queue->min_threshold.bytes = g_value_get_uint (value);
1801 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1802 QUEUE_THRESHOLD_CHANGE (queue);
1804 case PROP_MIN_THRESHOLD_BUFFERS:
1805 queue->min_threshold.buffers = g_value_get_uint (value);
1806 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1807 QUEUE_THRESHOLD_CHANGE (queue);
1809 case PROP_MIN_THRESHOLD_TIME:
1810 queue->min_threshold.time = g_value_get_uint64 (value);
1811 queue->orig_min_threshold.time = queue->min_threshold.time;
1812 QUEUE_THRESHOLD_CHANGE (queue);
1815 queue->leaky = g_value_get_enum (value);
1818 queue->silent = g_value_get_boolean (value);
1820 case PROP_FLUSH_ON_EOS:
1821 queue->flush_on_eos = g_value_get_boolean (value);
1824 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1828 GST_QUEUE_MUTEX_UNLOCK (queue);
1832 gst_queue_get_property (GObject * object,
1833 guint prop_id, GValue * value, GParamSpec * pspec)
1835 GstQueue *queue = GST_QUEUE (object);
1837 GST_QUEUE_MUTEX_LOCK (queue);
1840 case PROP_CUR_LEVEL_BYTES:
1841 g_value_set_uint (value, queue->cur_level.bytes);
1843 case PROP_CUR_LEVEL_BUFFERS:
1844 g_value_set_uint (value, queue->cur_level.buffers);
1846 case PROP_CUR_LEVEL_TIME:
1847 g_value_set_uint64 (value, queue->cur_level.time);
1849 case PROP_MAX_SIZE_BYTES:
1850 g_value_set_uint (value, queue->max_size.bytes);
1852 case PROP_MAX_SIZE_BUFFERS:
1853 g_value_set_uint (value, queue->max_size.buffers);
1855 case PROP_MAX_SIZE_TIME:
1856 g_value_set_uint64 (value, queue->max_size.time);
1858 case PROP_MIN_THRESHOLD_BYTES:
1859 g_value_set_uint (value, queue->min_threshold.bytes);
1861 case PROP_MIN_THRESHOLD_BUFFERS:
1862 g_value_set_uint (value, queue->min_threshold.buffers);
1864 case PROP_MIN_THRESHOLD_TIME:
1865 g_value_set_uint64 (value, queue->min_threshold.time);
1868 g_value_set_enum (value, queue->leaky);
1871 g_value_set_boolean (value, queue->silent);
1873 case PROP_FLUSH_ON_EOS:
1874 g_value_set_boolean (value, queue->flush_on_eos);
1877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1881 GST_QUEUE_MUTEX_UNLOCK (queue);