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
28 * Data is queued until one of the limits specified by the
29 * #GstQueue:max-size-buffers, #GstQueue:max-size-bytes and/or
30 * #GstQueue:max-size-time properties has been reached. Any attempt to push
31 * more buffers into the queue will block the pushing thread until more space
34 * The queue will create a new thread on the source pad to decouple the
35 * processing on sink and source pad.
37 * You can query how many buffers are queued by reading the
38 * #GstQueue:current-level-buffers property. You can track changes
39 * by connecting to the notify::current-level-buffers signal (which
40 * like all signals will be emitted from the streaming thread). The same
41 * applies to the #GstQueue:current-level-time and
42 * #GstQueue:current-level-bytes properties.
44 * The default queue size limits are 200 buffers, 10MB of data, or
45 * one second worth of data, whichever is reached first.
47 * As said earlier, the queue blocks by default when one of the specified
48 * maximums (bytes, time, buffers) has been reached. You can set the
49 * #GstQueue:leaky property to specify that instead of blocking it should
50 * leak (drop) new or old buffers.
52 * The #GstQueue::underrun signal is emitted when the queue has less data than
53 * the specified minimum thresholds require (by default: when the queue is
54 * empty). The #GstQueue::overrun signal is emitted when the queue is filled
55 * up. Both signals are emitted from the context of the streaming thread.
58 #include "gst/gst_private.h"
63 #include "../../gst/gst-i18n-lib.h"
64 #include "../../gst/glib-compat-private.h"
66 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
71 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
76 GST_DEBUG_CATEGORY_STATIC (queue_debug);
77 #define GST_CAT_DEFAULT (queue_debug)
78 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
80 #define STATUS(queue, pad, msg) \
81 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
82 "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
83 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
84 "-%" G_GUINT64_FORMAT " ns, %u items", \
85 GST_DEBUG_PAD_NAME (pad), \
86 queue->cur_level.buffers, \
87 queue->min_threshold.buffers, \
88 queue->max_size.buffers, \
89 queue->cur_level.bytes, \
90 queue->min_threshold.bytes, \
91 queue->max_size.bytes, \
92 queue->cur_level.time, \
93 queue->min_threshold.time, \
94 queue->max_size.time, \
95 gst_queue_array_get_length (queue->queue))
97 /* Queue signals and args */
110 /* FIXME: don't we have another way of doing this
111 * "Gstreamer format" (frame/byte/time) queries? */
112 PROP_CUR_LEVEL_BUFFERS,
113 PROP_CUR_LEVEL_BYTES,
115 PROP_MAX_SIZE_BUFFERS,
118 PROP_MIN_THRESHOLD_BUFFERS,
119 PROP_MIN_THRESHOLD_BYTES,
120 PROP_MIN_THRESHOLD_TIME,
126 /* default property values */
127 #define DEFAULT_MAX_SIZE_BUFFERS 200 /* 200 buffers */
128 #define DEFAULT_MAX_SIZE_BYTES (10 * 1024 * 1024) /* 10 MB */
129 #define DEFAULT_MAX_SIZE_TIME GST_SECOND /* 1 second */
131 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
132 g_mutex_lock (&q->qlock); \
135 #define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START { \
136 GST_QUEUE_MUTEX_LOCK (q); \
137 if (q->srcresult != GST_FLOW_OK) \
141 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
142 g_mutex_unlock (&q->qlock); \
145 #define GST_QUEUE_WAIT_DEL_CHECK(q, label) G_STMT_START { \
146 STATUS (q, q->sinkpad, "wait for DEL"); \
147 q->waiting_del = TRUE; \
148 g_cond_wait (&q->item_del, &q->qlock); \
149 q->waiting_del = FALSE; \
150 if (q->srcresult != GST_FLOW_OK) { \
151 STATUS (q, q->srcpad, "received DEL wakeup"); \
154 STATUS (q, q->sinkpad, "received DEL"); \
157 #define GST_QUEUE_WAIT_ADD_CHECK(q, label) G_STMT_START { \
158 STATUS (q, q->srcpad, "wait for ADD"); \
159 q->waiting_add = TRUE; \
160 g_cond_wait (&q->item_add, &q->qlock); \
161 q->waiting_add = FALSE; \
162 if (q->srcresult != GST_FLOW_OK) { \
163 STATUS (q, q->srcpad, "received ADD wakeup"); \
166 STATUS (q, q->srcpad, "received ADD"); \
169 #define GST_QUEUE_SIGNAL_DEL(q) G_STMT_START { \
170 if (q->waiting_del) { \
171 STATUS (q, q->srcpad, "signal DEL"); \
172 g_cond_signal (&q->item_del); \
176 #define GST_QUEUE_SIGNAL_ADD(q) G_STMT_START { \
177 if (q->waiting_add) { \
178 STATUS (q, q->sinkpad, "signal ADD"); \
179 g_cond_signal (&q->item_add); \
184 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue", 0, "queue element"); \
185 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0, \
186 "dataflow inside the queue element");
187 #define gst_queue_parent_class parent_class
188 G_DEFINE_TYPE_WITH_CODE (GstQueue, gst_queue, GST_TYPE_ELEMENT, _do_init);
190 static void gst_queue_finalize (GObject * object);
191 static void gst_queue_set_property (GObject * object,
192 guint prop_id, const GValue * value, GParamSpec * pspec);
193 static void gst_queue_get_property (GObject * object,
194 guint prop_id, GValue * value, GParamSpec * pspec);
196 static GstFlowReturn gst_queue_chain (GstPad * pad, GstObject * parent,
198 static GstFlowReturn gst_queue_chain_list (GstPad * pad, GstObject * parent,
199 GstBufferList * buffer_list);
200 static GstFlowReturn gst_queue_push_one (GstQueue * queue);
201 static void gst_queue_loop (GstPad * pad);
203 static gboolean gst_queue_handle_sink_event (GstPad * pad, GstObject * parent,
205 static gboolean gst_queue_handle_sink_query (GstPad * pad, GstObject * parent,
208 static gboolean gst_queue_handle_src_event (GstPad * pad, GstObject * parent,
210 static gboolean gst_queue_handle_src_query (GstPad * pad, GstObject * parent,
213 static void gst_queue_locked_flush (GstQueue * queue, gboolean full);
215 static gboolean gst_queue_src_activate_mode (GstPad * pad, GstObject * parent,
216 GstPadMode mode, gboolean active);
217 static gboolean gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
218 GstPadMode mode, gboolean active);
220 static gboolean gst_queue_is_empty (GstQueue * queue);
221 static gboolean gst_queue_is_filled (GstQueue * queue);
231 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
234 queue_leaky_get_type (void)
236 static GType queue_leaky_type = 0;
237 static const GEnumValue queue_leaky[] = {
238 {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
239 {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
240 {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
245 if (!queue_leaky_type) {
246 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
248 return queue_leaky_type;
251 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
254 gst_queue_class_init (GstQueueClass * klass)
256 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
257 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
259 gobject_class->set_property = gst_queue_set_property;
260 gobject_class->get_property = gst_queue_get_property;
264 * GstQueue::underrun:
265 * @queue: the queue instance
267 * Reports that the buffer became empty (underrun).
268 * A buffer is empty if the total amount of data inside it (num-buffers, time,
269 * size) is lower than the boundary values which can be set through the
270 * GObject properties.
272 gst_queue_signals[SIGNAL_UNDERRUN] =
273 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
274 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
275 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
278 * @queue: the queue instance
280 * Reports that enough (min-threshold) data is in the queue. Use this signal
281 * together with the underrun signal to pause the pipeline on underrun and
282 * wait for the queue to fill-up before resume playback.
284 gst_queue_signals[SIGNAL_RUNNING] =
285 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
286 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
287 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
290 * @queue: the queue instance
292 * Reports that the buffer became full (overrun).
293 * A buffer is full if the total amount of data inside it (num-buffers, time,
294 * size) is higher than the boundary values which can be set through the
295 * GObject properties.
297 gst_queue_signals[SIGNAL_OVERRUN] =
298 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
299 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
300 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
303 * @queue: the queue instance
305 * Reports when the queue has enough data to start pushing data again on the
308 gst_queue_signals[SIGNAL_PUSHING] =
309 g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
310 G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
311 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
314 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
315 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
316 "Current amount of data in the queue (bytes)",
317 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
319 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
320 "Current number of buffers in the queue",
321 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
323 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
324 "Current amount of data in the queue (in ns)",
325 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
328 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
329 "Max. amount of data in the queue (bytes, 0=disable)",
330 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
331 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
332 G_PARAM_STATIC_STRINGS));
333 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
334 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
335 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
336 DEFAULT_MAX_SIZE_BUFFERS,
337 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
338 G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
340 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
341 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
342 DEFAULT_MAX_SIZE_TIME,
343 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
344 G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
347 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
348 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
350 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
351 G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
353 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
354 "Min. number of buffers in the queue to allow reading (0=disable)", 0,
356 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
357 G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
359 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
360 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
362 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
363 G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_LEAKY,
366 g_param_spec_enum ("leaky", "Leaky",
367 "Where the queue leaks, if at all",
368 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
369 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
370 G_PARAM_STATIC_STRINGS));
375 * Don't emit queue signals. Makes queues more lightweight if no signals are
378 g_object_class_install_property (gobject_class, PROP_SILENT,
379 g_param_spec_boolean ("silent", "Silent",
380 "Don't emit queue signals", FALSE,
381 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
382 G_PARAM_STATIC_STRINGS));
385 * GstQueue:flush-on-eos
387 * Discard all data in the queue when an EOS event is received, and pass
388 * on the EOS event as soon as possible (instead of waiting until all
389 * buffers in the queue have been processed, which is the default behaviour).
391 * Flushing the queue on EOS might be useful when capturing and encoding
392 * from a live source, to finish up the recording quickly in cases when
393 * the encoder is slow. Note that this might mean some data from the end of
394 * the recording data might be lost though (never more than the configured
395 * max. sizes though).
399 g_object_class_install_property (gobject_class, PROP_FLUSH_ON_EOS,
400 g_param_spec_boolean ("flush-on-eos", "Flush on EOS",
401 "Discard all data in the queue when an EOS event is received", FALSE,
402 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
403 G_PARAM_STATIC_STRINGS));
405 gobject_class->finalize = gst_queue_finalize;
407 gst_element_class_set_static_metadata (gstelement_class,
409 "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
410 gst_element_class_add_pad_template (gstelement_class,
411 gst_static_pad_template_get (&srctemplate));
412 gst_element_class_add_pad_template (gstelement_class,
413 gst_static_pad_template_get (&sinktemplate));
415 /* Registering debug symbols for function pointers */
416 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
417 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
418 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
419 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
420 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
421 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
422 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain_list);
426 gst_queue_init (GstQueue * queue)
428 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
430 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
431 gst_pad_set_chain_list_function (queue->sinkpad, gst_queue_chain_list);
432 gst_pad_set_activatemode_function (queue->sinkpad,
433 gst_queue_sink_activate_mode);
434 gst_pad_set_event_function (queue->sinkpad, gst_queue_handle_sink_event);
435 gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
436 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
437 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
439 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
441 gst_pad_set_activatemode_function (queue->srcpad,
442 gst_queue_src_activate_mode);
443 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
444 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
445 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
446 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
448 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
449 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
450 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
451 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
452 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
453 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
454 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
455 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
456 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
458 queue->leaky = GST_QUEUE_NO_LEAK;
459 queue->srcresult = GST_FLOW_FLUSHING;
461 g_mutex_init (&queue->qlock);
462 g_cond_init (&queue->item_add);
463 g_cond_init (&queue->item_del);
464 g_cond_init (&queue->query_handled);
467 gst_queue_array_new_for_struct (sizeof (GstQueueItem),
468 DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
470 queue->sinktime = GST_CLOCK_TIME_NONE;
471 queue->srctime = GST_CLOCK_TIME_NONE;
473 queue->sink_tainted = TRUE;
474 queue->src_tainted = TRUE;
476 queue->newseg_applied_to_src = FALSE;
478 GST_DEBUG_OBJECT (queue,
479 "initialized queue's not_empty & not_full conditions");
482 /* called only once, as opposed to dispose */
484 gst_queue_finalize (GObject * object)
486 GstQueue *queue = GST_QUEUE (object);
489 GST_DEBUG_OBJECT (queue, "finalizing queue");
491 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
492 /* FIXME: if it's a query, shouldn't we unref that too? */
493 if (!qitem->is_query)
494 gst_mini_object_unref (qitem->item);
496 gst_queue_array_free (queue->queue);
498 g_mutex_clear (&queue->qlock);
499 g_cond_clear (&queue->item_add);
500 g_cond_clear (&queue->item_del);
501 g_cond_clear (&queue->query_handled);
503 G_OBJECT_CLASS (parent_class)->finalize (object);
506 /* calculate the diff between running time on the sink and src of the queue.
507 * This is the total amount of time in the queue. */
509 update_time_level (GstQueue * queue)
511 gint64 sink_time, src_time;
513 if (queue->sink_tainted) {
514 GST_LOG_OBJECT (queue, "update sink time");
516 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
517 queue->sink_segment.position);
518 queue->sink_tainted = FALSE;
520 sink_time = queue->sinktime;
522 if (queue->src_tainted) {
523 GST_LOG_OBJECT (queue, "update src time");
525 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
526 queue->src_segment.position);
527 queue->src_tainted = FALSE;
529 src_time = queue->srctime;
531 GST_LOG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
532 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
534 if (sink_time >= src_time)
535 queue->cur_level.time = sink_time - src_time;
537 queue->cur_level.time = 0;
540 /* take a SEGMENT event and apply the values to segment, updating the time
543 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
546 gst_event_copy_segment (event, segment);
548 /* now configure the values, we use these to track timestamps on the
550 if (segment->format != GST_FORMAT_TIME) {
551 /* non-time format, pretent the current time segment is closed with a
552 * 0 start and unknown stop time. */
553 segment->format = GST_FORMAT_TIME;
559 queue->sink_tainted = TRUE;
561 queue->src_tainted = TRUE;
563 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
565 /* segment can update the time level of the queue */
566 update_time_level (queue);
570 apply_gap (GstQueue * queue, GstEvent * event,
571 GstSegment * segment, gboolean is_sink)
573 GstClockTime timestamp;
574 GstClockTime duration;
576 gst_event_parse_gap (event, ×tamp, &duration);
578 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
580 if (GST_CLOCK_TIME_IS_VALID (duration)) {
581 timestamp += duration;
584 segment->position = timestamp;
587 queue->sink_tainted = TRUE;
589 queue->src_tainted = TRUE;
591 /* calc diff with other end */
592 update_time_level (queue);
597 /* take a buffer and update segment, updating the time level of the queue. */
599 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
602 GstClockTime duration, timestamp;
604 timestamp = GST_BUFFER_TIMESTAMP (buffer);
605 duration = GST_BUFFER_DURATION (buffer);
607 /* if no timestamp is set, assume it's continuous with the previous
609 if (timestamp == GST_CLOCK_TIME_NONE)
610 timestamp = segment->position;
613 if (duration != GST_CLOCK_TIME_NONE)
614 timestamp += duration;
616 GST_LOG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
617 GST_TIME_ARGS (timestamp));
619 segment->position = timestamp;
621 queue->sink_tainted = TRUE;
623 queue->src_tainted = TRUE;
626 /* calc diff with other end */
627 update_time_level (queue);
631 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer user_data)
633 GstClockTime *timestamp = user_data;
635 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
636 " duration %" GST_TIME_FORMAT, idx,
637 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
638 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
640 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
641 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
643 if (GST_BUFFER_DURATION_IS_VALID (*buf))
644 *timestamp += GST_BUFFER_DURATION (*buf);
646 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
651 /* take a buffer list and update segment, updating the time level of the queue */
653 apply_buffer_list (GstQueue * queue, GstBufferList * buffer_list,
654 GstSegment * segment, gboolean sink)
656 GstClockTime timestamp;
658 /* if no timestamp is set, assume it's continuous with the previous time */
659 timestamp = segment->position;
661 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
663 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
664 GST_TIME_ARGS (timestamp));
666 segment->position = timestamp;
669 queue->sink_tainted = TRUE;
671 queue->src_tainted = TRUE;
673 /* calc diff with other end */
674 update_time_level (queue);
678 gst_queue_locked_flush (GstQueue * queue, gboolean full)
682 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
683 /* Then lose another reference because we are supposed to destroy that
684 data when flushing */
685 if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
686 && GST_EVENT_IS_STICKY (qitem->item)
687 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
688 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
689 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
691 if (!qitem->is_query)
692 gst_mini_object_unref (qitem->item);
693 memset (qitem, 0, sizeof (GstQueueItem));
695 queue->last_query = FALSE;
696 g_cond_signal (&queue->query_handled);
697 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
698 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
699 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
700 queue->min_threshold.time = queue->orig_min_threshold.time;
701 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
702 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
703 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
705 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
706 queue->sink_tainted = queue->src_tainted = TRUE;
708 /* we deleted a lot of something */
709 GST_QUEUE_SIGNAL_DEL (queue);
712 /* enqueue an item an update the level stats, with QUEUE_LOCK */
714 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
717 GstBuffer *buffer = GST_BUFFER_CAST (item);
718 gsize bsize = gst_buffer_get_size (buffer);
720 /* add buffer to the statistics */
721 queue->cur_level.buffers++;
722 queue->cur_level.bytes += bsize;
723 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
726 qitem.is_query = FALSE;
728 gst_queue_array_push_tail_struct (queue->queue, &qitem);
729 GST_QUEUE_SIGNAL_ADD (queue);
733 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
735 guint *p_size = data;
738 buf_size = gst_buffer_get_size (*buf);
739 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
745 gst_queue_locked_enqueue_buffer_list (GstQueue * queue, gpointer item)
748 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
751 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &bsize);
753 /* add buffer to the statistics */
754 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
755 queue->cur_level.bytes += bsize;
756 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
759 qitem.is_query = FALSE;
761 gst_queue_array_push_tail_struct (queue->queue, &qitem);
762 GST_QUEUE_SIGNAL_ADD (queue);
766 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
769 GstEvent *event = GST_EVENT_CAST (item);
771 switch (GST_EVENT_TYPE (event)) {
773 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
774 /* Zero the thresholds, this makes sure the queue is completely
775 * filled and we can read all data from the queue. */
776 if (queue->flush_on_eos)
777 gst_queue_locked_flush (queue, FALSE);
779 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
780 /* mark the queue as EOS. This prevents us from accepting more data. */
783 case GST_EVENT_SEGMENT:
784 apply_segment (queue, event, &queue->sink_segment, TRUE);
785 /* if the queue is empty, apply sink segment on the source */
786 if (gst_queue_array_is_empty (queue->queue)) {
787 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
788 apply_segment (queue, event, &queue->src_segment, FALSE);
789 queue->newseg_applied_to_src = TRUE;
791 /* a new segment allows us to accept more buffers if we got EOS
793 queue->unexpected = FALSE;
796 apply_gap (queue, event, &queue->sink_segment, TRUE);
803 qitem.is_query = FALSE;
805 gst_queue_array_push_tail_struct (queue->queue, &qitem);
806 GST_QUEUE_SIGNAL_ADD (queue);
809 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
810 static GstMiniObject *
811 gst_queue_locked_dequeue (GstQueue * queue)
817 qitem = gst_queue_array_pop_head_struct (queue->queue);
822 bufsize = qitem->size;
824 if (GST_IS_BUFFER (item)) {
825 GstBuffer *buffer = GST_BUFFER_CAST (item);
827 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
828 "retrieved buffer %p from queue", buffer);
830 queue->cur_level.buffers--;
831 queue->cur_level.bytes -= bufsize;
832 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
834 /* if the queue is empty now, update the other side */
835 if (queue->cur_level.buffers == 0)
836 queue->cur_level.time = 0;
837 } else if (GST_IS_BUFFER_LIST (item)) {
838 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
840 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
841 "retrieved buffer list %p from queue", buffer_list);
843 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
844 queue->cur_level.bytes -= bufsize;
845 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
847 /* if the queue is empty now, update the other side */
848 if (queue->cur_level.buffers == 0)
849 queue->cur_level.time = 0;
850 } else if (GST_IS_EVENT (item)) {
851 GstEvent *event = GST_EVENT_CAST (item);
853 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
854 "retrieved event %p from queue", event);
856 switch (GST_EVENT_TYPE (event)) {
858 /* queue is empty now that we dequeued the EOS */
859 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
861 case GST_EVENT_SEGMENT:
862 /* apply newsegment if it has not already been applied */
863 if (G_LIKELY (!queue->newseg_applied_to_src)) {
864 apply_segment (queue, event, &queue->src_segment, FALSE);
866 queue->newseg_applied_to_src = FALSE;
870 apply_gap (queue, event, &queue->src_segment, FALSE);
875 } else if (GST_IS_QUERY (item)) {
876 GstQuery *query = GST_QUERY_CAST (item);
878 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
879 "retrieved query %p from queue", query);
882 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
883 item, GST_OBJECT_NAME (queue));
886 GST_QUEUE_SIGNAL_DEL (queue);
893 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
899 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
904 queue = GST_QUEUE (parent);
906 switch (GST_EVENT_TYPE (event)) {
907 case GST_EVENT_FLUSH_START:
908 STATUS (queue, pad, "received flush start event");
910 ret = gst_pad_push_event (queue->srcpad, event);
912 /* now unblock the chain function */
913 GST_QUEUE_MUTEX_LOCK (queue);
914 queue->srcresult = GST_FLOW_FLUSHING;
915 /* unblock the loop and chain functions */
916 GST_QUEUE_SIGNAL_ADD (queue);
917 GST_QUEUE_SIGNAL_DEL (queue);
918 queue->last_query = FALSE;
919 g_cond_signal (&queue->query_handled);
920 GST_QUEUE_MUTEX_UNLOCK (queue);
922 /* make sure it pauses, this should happen since we sent
923 * flush_start downstream. */
924 gst_pad_pause_task (queue->srcpad);
925 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
927 case GST_EVENT_FLUSH_STOP:
928 STATUS (queue, pad, "received flush stop event");
930 ret = gst_pad_push_event (queue->srcpad, event);
932 GST_QUEUE_MUTEX_LOCK (queue);
933 gst_queue_locked_flush (queue, FALSE);
934 queue->srcresult = GST_FLOW_OK;
936 queue->unexpected = FALSE;
937 if (gst_pad_is_active (queue->srcpad)) {
938 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
939 queue->srcpad, NULL);
941 GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
942 "pad not active any longer");
944 GST_QUEUE_MUTEX_UNLOCK (queue);
946 STATUS (queue, pad, "after flush");
949 if (GST_EVENT_IS_SERIALIZED (event)) {
950 /* serialized events go in the queue */
951 GST_QUEUE_MUTEX_LOCK (queue);
952 if (queue->srcresult != GST_FLOW_OK) {
953 /* Errors in sticky event pushing are no problem and ignored here
954 * as they will cause more meaningful errors during data flow.
955 * For EOS events, that are not followed by data flow, we still
956 * return FALSE here though and report an error.
958 if (!GST_EVENT_IS_STICKY (event)) {
959 GST_QUEUE_MUTEX_UNLOCK (queue);
961 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
962 if (queue->srcresult == GST_FLOW_NOT_LINKED
963 || queue->srcresult < GST_FLOW_EOS) {
964 GST_QUEUE_MUTEX_UNLOCK (queue);
965 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
966 (_("Internal data flow error.")),
967 ("streaming task paused, reason %s (%d)",
968 gst_flow_get_name (queue->srcresult), queue->srcresult));
970 GST_QUEUE_MUTEX_UNLOCK (queue);
975 /* refuse more events on EOS */
978 gst_queue_locked_enqueue_event (queue, event);
979 GST_QUEUE_MUTEX_UNLOCK (queue);
981 /* non-serialized events are forwarded downstream immediately */
982 ret = gst_pad_push_event (queue->srcpad, event);
991 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
992 GST_QUEUE_MUTEX_UNLOCK (queue);
993 gst_event_unref (event);
998 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
999 "refusing event, we have a downstream flow error: %s",
1000 gst_flow_get_name (queue->srcresult));
1001 gst_event_unref (event);
1007 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1009 GstQueue *queue = GST_QUEUE_CAST (parent);
1012 switch (GST_QUERY_TYPE (query)) {
1014 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
1017 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1018 GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
1019 GST_QUERY_TYPE_NAME (query));
1020 qitem.item = GST_MINI_OBJECT_CAST (query);
1021 qitem.is_query = TRUE;
1023 gst_queue_array_push_tail_struct (queue->queue, &qitem);
1024 GST_QUEUE_SIGNAL_ADD (queue);
1025 g_cond_wait (&queue->query_handled, &queue->qlock);
1026 if (queue->srcresult != GST_FLOW_OK)
1028 res = queue->last_query;
1029 GST_QUEUE_MUTEX_UNLOCK (queue);
1031 res = gst_pad_query_default (pad, parent, query);
1040 GST_DEBUG_OBJECT (queue, "we are flushing");
1041 GST_QUEUE_MUTEX_UNLOCK (queue);
1047 gst_queue_is_empty (GstQueue * queue)
1051 head = gst_queue_array_peek_head_struct (queue->queue);
1056 /* Only consider the queue empty if the minimum thresholds
1057 * are not reached and data is at the queue head. Otherwise
1058 * we would block forever on serialized queries.
1060 if (!GST_IS_BUFFER (head->item) && !GST_IS_BUFFER_LIST (head->item))
1063 /* It is possible that a max size is reached before all min thresholds are.
1064 * Therefore, only consider it empty if it is not filled. */
1065 return ((queue->min_threshold.buffers > 0 &&
1066 queue->cur_level.buffers < queue->min_threshold.buffers) ||
1067 (queue->min_threshold.bytes > 0 &&
1068 queue->cur_level.bytes < queue->min_threshold.bytes) ||
1069 (queue->min_threshold.time > 0 &&
1070 queue->cur_level.time < queue->min_threshold.time)) &&
1071 !gst_queue_is_filled (queue);
1075 gst_queue_is_filled (GstQueue * queue)
1077 return (((queue->max_size.buffers > 0 &&
1078 queue->cur_level.buffers >= queue->max_size.buffers) ||
1079 (queue->max_size.bytes > 0 &&
1080 queue->cur_level.bytes >= queue->max_size.bytes) ||
1081 (queue->max_size.time > 0 &&
1082 queue->cur_level.time >= queue->max_size.time)));
1086 gst_queue_leak_downstream (GstQueue * queue)
1088 /* for as long as the queue is filled, dequeue an item and discard it */
1089 while (gst_queue_is_filled (queue)) {
1090 GstMiniObject *leak;
1092 leak = gst_queue_locked_dequeue (queue);
1093 /* there is nothing to dequeue and the queue is still filled.. This should
1095 g_assert (leak != NULL);
1097 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1098 "queue is full, leaking item %p on downstream end", leak);
1099 if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
1100 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1101 "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
1102 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
1105 if (!GST_IS_QUERY (leak))
1106 gst_mini_object_unref (leak);
1108 /* last buffer needs to get a DISCONT flag */
1109 queue->head_needs_discont = TRUE;
1114 discont_first_buffer (GstBuffer ** buffer, guint i, gpointer user_data)
1116 GstQueue *queue = user_data;
1117 GstBuffer *subbuffer = gst_buffer_make_writable (*buffer);
1120 *buffer = subbuffer;
1121 GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
1123 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1129 static GstFlowReturn
1130 gst_queue_chain_buffer_or_list (GstPad * pad, GstObject * parent,
1131 GstMiniObject * obj, gboolean is_list)
1135 queue = GST_QUEUE_CAST (parent);
1137 /* we have to lock the queue since we span threads */
1138 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1139 /* when we received EOS, we refuse any more data */
1142 if (queue->unexpected)
1143 goto out_unexpected;
1146 GstClockTime duration, timestamp;
1147 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1149 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1150 duration = GST_BUFFER_DURATION (buffer);
1152 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
1153 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1154 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1155 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1157 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1158 "received buffer list %p with %u buffers", obj,
1159 gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)));
1162 /* We make space available if we're "full" according to whatever
1163 * the user defined as "full". Note that this only applies to buffers.
1164 * We always handle events and they don't count in our statistics. */
1165 while (gst_queue_is_filled (queue)) {
1166 if (!queue->silent) {
1167 GST_QUEUE_MUTEX_UNLOCK (queue);
1168 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
1169 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1170 /* we recheck, the signal could have changed the thresholds */
1171 if (!gst_queue_is_filled (queue))
1175 /* how are we going to make space for this buffer? */
1176 switch (queue->leaky) {
1177 case GST_QUEUE_LEAK_UPSTREAM:
1178 /* next buffer needs to get a DISCONT flag */
1179 queue->tail_needs_discont = TRUE;
1180 /* leak current buffer */
1181 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1182 "queue is full, leaking buffer on upstream end");
1183 /* now we can clean up and exit right away */
1185 case GST_QUEUE_LEAK_DOWNSTREAM:
1186 gst_queue_leak_downstream (queue);
1189 g_warning ("Unknown leaky type, using default");
1191 case GST_QUEUE_NO_LEAK:
1193 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1194 "queue is full, waiting for free space");
1196 /* don't leak. Instead, wait for space to be available */
1198 /* for as long as the queue is filled, wait till an item was deleted. */
1199 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1200 } while (gst_queue_is_filled (queue));
1202 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1204 if (!queue->silent) {
1205 GST_QUEUE_MUTEX_UNLOCK (queue);
1206 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1207 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1214 if (queue->tail_needs_discont) {
1216 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1217 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1221 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1223 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1226 obj = GST_MINI_OBJECT_CAST (buffer);
1228 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
1230 buffer_list = gst_buffer_list_make_writable (buffer_list);
1231 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1232 obj = GST_MINI_OBJECT_CAST (buffer_list);
1234 queue->tail_needs_discont = FALSE;
1237 /* put buffer in queue now */
1239 gst_queue_locked_enqueue_buffer_list (queue, obj);
1241 gst_queue_locked_enqueue_buffer (queue, obj);
1242 GST_QUEUE_MUTEX_UNLOCK (queue);
1246 /* special conditions */
1249 GST_QUEUE_MUTEX_UNLOCK (queue);
1251 gst_mini_object_unref (obj);
1257 GstFlowReturn ret = queue->srcresult;
1259 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1260 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1261 GST_QUEUE_MUTEX_UNLOCK (queue);
1262 gst_mini_object_unref (obj);
1268 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1269 GST_QUEUE_MUTEX_UNLOCK (queue);
1271 gst_mini_object_unref (obj);
1273 return GST_FLOW_EOS;
1277 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1278 GST_QUEUE_MUTEX_UNLOCK (queue);
1280 gst_mini_object_unref (obj);
1282 return GST_FLOW_EOS;
1286 static GstFlowReturn
1287 gst_queue_chain_list (GstPad * pad, GstObject * parent,
1288 GstBufferList * buffer_list)
1290 return gst_queue_chain_buffer_or_list (pad, parent,
1291 GST_MINI_OBJECT_CAST (buffer_list), TRUE);
1294 static GstFlowReturn
1295 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1297 return gst_queue_chain_buffer_or_list (pad, parent,
1298 GST_MINI_OBJECT_CAST (buffer), FALSE);
1301 /* dequeue an item from the queue an push it downstream. This functions returns
1302 * the result of the push. */
1303 static GstFlowReturn
1304 gst_queue_push_one (GstQueue * queue)
1306 GstFlowReturn result = queue->srcresult;
1307 GstMiniObject *data;
1310 data = gst_queue_locked_dequeue (queue);
1315 is_list = GST_IS_BUFFER_LIST (data);
1317 if (GST_IS_BUFFER (data) || is_list) {
1321 buffer = GST_BUFFER_CAST (data);
1323 if (queue->head_needs_discont) {
1324 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1328 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1330 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1332 queue->head_needs_discont = FALSE;
1335 GST_QUEUE_MUTEX_UNLOCK (queue);
1336 result = gst_pad_push (queue->srcpad, buffer);
1338 GstBufferList *buffer_list;
1340 buffer_list = GST_BUFFER_LIST_CAST (data);
1342 if (queue->head_needs_discont) {
1343 buffer_list = gst_buffer_list_make_writable (buffer_list);
1344 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1345 queue->head_needs_discont = FALSE;
1348 GST_QUEUE_MUTEX_UNLOCK (queue);
1349 result = gst_pad_push_list (queue->srcpad, buffer_list);
1352 /* need to check for srcresult here as well */
1353 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1355 if (result == GST_FLOW_EOS) {
1356 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1357 /* stop pushing buffers, we dequeue all items until we see an item that we
1358 * can push again, which is EOS or SEGMENT. If there is nothing in the
1359 * queue we can push, we set a flag to make the sinkpad refuse more
1360 * buffers with an EOS return value. */
1361 while ((data = gst_queue_locked_dequeue (queue))) {
1362 if (GST_IS_BUFFER (data)) {
1363 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1364 "dropping EOS buffer %p", data);
1365 gst_buffer_unref (GST_BUFFER_CAST (data));
1366 } else if (GST_IS_BUFFER_LIST (data)) {
1367 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1368 "dropping EOS buffer list %p", data);
1369 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
1370 } else if (GST_IS_EVENT (data)) {
1371 GstEvent *event = GST_EVENT_CAST (data);
1372 GstEventType type = GST_EVENT_TYPE (event);
1374 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
1375 /* we found a pushable item in the queue, push it out */
1376 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1377 "pushing pushable event %s after EOS",
1378 GST_EVENT_TYPE_NAME (event));
1381 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1382 "dropping EOS event %p", event);
1383 gst_event_unref (event);
1384 } else if (GST_IS_QUERY (data)) {
1385 GstQuery *query = GST_QUERY_CAST (data);
1387 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1388 "dropping query %p because of EOS", query);
1389 queue->last_query = FALSE;
1390 g_cond_signal (&queue->query_handled);
1393 /* no more items in the queue. Set the unexpected flag so that upstream
1394 * make us refuse any more buffers on the sinkpad. Since we will still
1395 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1396 * task function does not shut down. */
1397 queue->unexpected = TRUE;
1398 result = GST_FLOW_OK;
1400 } else if (GST_IS_EVENT (data)) {
1401 GstEvent *event = GST_EVENT_CAST (data);
1402 GstEventType type = GST_EVENT_TYPE (event);
1404 GST_QUEUE_MUTEX_UNLOCK (queue);
1406 gst_pad_push_event (queue->srcpad, event);
1408 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1409 /* if we're EOS, return EOS so that the task pauses. */
1410 if (type == GST_EVENT_EOS) {
1411 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1412 "pushed EOS event %p, return EOS", event);
1413 result = GST_FLOW_EOS;
1415 } else if (GST_IS_QUERY (data)) {
1416 GstQuery *query = GST_QUERY_CAST (data);
1419 GST_QUEUE_MUTEX_UNLOCK (queue);
1420 ret = gst_pad_peer_query (queue->srcpad, query);
1421 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1422 queue->last_query = ret;
1423 g_cond_signal (&queue->query_handled);
1424 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1425 "did query %p, return %d", query, queue->last_query);
1432 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1433 "exit because we have no item in the queue");
1434 return GST_FLOW_ERROR;
1438 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1439 return GST_FLOW_FLUSHING;
1443 queue->last_query = FALSE;
1444 g_cond_signal (&queue->query_handled);
1445 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1446 return GST_FLOW_FLUSHING;
1451 gst_queue_loop (GstPad * pad)
1456 queue = (GstQueue *) GST_PAD_PARENT (pad);
1458 /* have to lock for thread-safety */
1459 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1461 while (gst_queue_is_empty (queue)) {
1462 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1463 if (!queue->silent) {
1464 GST_QUEUE_MUTEX_UNLOCK (queue);
1465 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1466 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1469 /* we recheck, the signal could have changed the thresholds */
1470 while (gst_queue_is_empty (queue)) {
1471 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1474 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1475 if (!queue->silent) {
1476 GST_QUEUE_MUTEX_UNLOCK (queue);
1477 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1478 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1479 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1483 ret = gst_queue_push_one (queue);
1484 queue->srcresult = ret;
1485 if (ret != GST_FLOW_OK)
1488 GST_QUEUE_MUTEX_UNLOCK (queue);
1495 gboolean eos = queue->eos;
1496 GstFlowReturn ret = queue->srcresult;
1498 gst_pad_pause_task (queue->srcpad);
1499 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1500 "pause task, reason: %s", gst_flow_get_name (ret));
1501 if (ret == GST_FLOW_FLUSHING) {
1502 gst_queue_locked_flush (queue, FALSE);
1504 GST_QUEUE_SIGNAL_DEL (queue);
1505 queue->last_query = FALSE;
1506 g_cond_signal (&queue->query_handled);
1508 GST_QUEUE_MUTEX_UNLOCK (queue);
1509 /* let app know about us giving up if upstream is not expected to do so */
1510 /* EOS is already taken care of elsewhere */
1511 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1512 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
1513 (_("Internal data flow error.")),
1514 ("streaming task paused, reason %s (%d)",
1515 gst_flow_get_name (ret), ret));
1516 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1523 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1525 gboolean res = TRUE;
1526 GstQueue *queue = GST_QUEUE (parent);
1528 #ifndef GST_DISABLE_GST_DEBUG
1529 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1530 event, GST_EVENT_TYPE (event));
1533 switch (GST_EVENT_TYPE (event)) {
1534 case GST_EVENT_RECONFIGURE:
1535 GST_QUEUE_MUTEX_LOCK (queue);
1536 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1537 /* when we got not linked, assume downstream is linked again now and we
1538 * can try to start pushing again */
1539 queue->srcresult = GST_FLOW_OK;
1540 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1542 GST_QUEUE_MUTEX_UNLOCK (queue);
1544 res = gst_pad_push_event (queue->sinkpad, event);
1547 res = gst_pad_event_default (pad, parent, event);
1556 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1558 GstQueue *queue = GST_QUEUE (parent);
1561 switch (GST_QUERY_TYPE (query)) {
1562 case GST_QUERY_SCHEDULING:{
1563 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1568 res = gst_pad_query_default (pad, parent, query);
1575 /* Adjust peer response for data contained in queue */
1576 switch (GST_QUERY_TYPE (query)) {
1577 case GST_QUERY_POSITION:
1582 /* get peer position */
1583 gst_query_parse_position (query, &format, &peer_pos);
1585 /* FIXME: this code assumes that there's no discont in the queue */
1587 case GST_FORMAT_BYTES:
1588 peer_pos -= queue->cur_level.bytes;
1590 case GST_FORMAT_TIME:
1591 peer_pos -= queue->cur_level.time;
1594 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1595 "know how to adjust value", gst_format_get_name (format));
1598 /* set updated position */
1599 gst_query_set_position (query, format, peer_pos);
1602 case GST_QUERY_LATENCY:
1605 GstClockTime min, max;
1607 gst_query_parse_latency (query, &live, &min, &max);
1609 /* we can delay up to the limit of the queue in time. If we have no time
1610 * limit, the best thing we can do is to return an infinite delay. In
1611 * reality a better estimate would be the byte/buffer rate but that is not
1612 * possible right now. */
1613 /* TODO: Use CONVERT query? */
1614 if (queue->max_size.time > 0 && max != -1
1615 && queue->leaky == GST_QUEUE_NO_LEAK)
1616 max += queue->max_size.time;
1617 else if (queue->max_size.time > 0 && queue->leaky != GST_QUEUE_NO_LEAK)
1618 max = MIN (queue->max_size.time, max);
1622 /* adjust for min-threshold */
1623 if (queue->min_threshold.time > 0)
1624 min += queue->min_threshold.time;
1626 gst_query_set_latency (query, live, min, max);
1630 /* peer handled other queries */
1638 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1644 queue = GST_QUEUE (parent);
1647 case GST_PAD_MODE_PUSH:
1649 GST_QUEUE_MUTEX_LOCK (queue);
1650 queue->srcresult = GST_FLOW_OK;
1652 queue->unexpected = FALSE;
1653 GST_QUEUE_MUTEX_UNLOCK (queue);
1655 /* step 1, unblock chain function */
1656 GST_QUEUE_MUTEX_LOCK (queue);
1657 queue->srcresult = GST_FLOW_FLUSHING;
1658 /* the item del signal will unblock */
1659 g_cond_signal (&queue->item_del);
1660 /* unblock query handler */
1661 queue->last_query = FALSE;
1662 g_cond_signal (&queue->query_handled);
1663 GST_QUEUE_MUTEX_UNLOCK (queue);
1665 /* step 2, wait until streaming thread stopped and flush queue */
1666 GST_PAD_STREAM_LOCK (pad);
1667 GST_QUEUE_MUTEX_LOCK (queue);
1668 gst_queue_locked_flush (queue, TRUE);
1669 GST_QUEUE_MUTEX_UNLOCK (queue);
1670 GST_PAD_STREAM_UNLOCK (pad);
1682 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1688 queue = GST_QUEUE (parent);
1691 case GST_PAD_MODE_PUSH:
1693 GST_QUEUE_MUTEX_LOCK (queue);
1694 queue->srcresult = GST_FLOW_OK;
1696 queue->unexpected = FALSE;
1698 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1700 GST_QUEUE_MUTEX_UNLOCK (queue);
1702 /* step 1, unblock loop function */
1703 GST_QUEUE_MUTEX_LOCK (queue);
1704 queue->srcresult = GST_FLOW_FLUSHING;
1705 /* the item add signal will unblock */
1706 g_cond_signal (&queue->item_add);
1707 GST_QUEUE_MUTEX_UNLOCK (queue);
1709 /* step 2, make sure streaming finishes */
1710 result = gst_pad_stop_task (pad);
1721 queue_capacity_change (GstQueue * queue)
1723 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1724 gst_queue_leak_downstream (queue);
1727 /* changing the capacity of the queue must wake up
1728 * the _chain function, it might have more room now
1729 * to store the buffer/event in the queue */
1730 GST_QUEUE_SIGNAL_DEL (queue);
1733 /* Changing the minimum required fill level must
1734 * wake up the _loop function as it might now
1735 * be able to preceed.
1737 #define QUEUE_THRESHOLD_CHANGE(q)\
1738 GST_QUEUE_SIGNAL_ADD (q);
1741 gst_queue_set_property (GObject * object,
1742 guint prop_id, const GValue * value, GParamSpec * pspec)
1744 GstQueue *queue = GST_QUEUE (object);
1746 /* someone could change levels here, and since this
1747 * affects the get/put funcs, we need to lock for safety. */
1748 GST_QUEUE_MUTEX_LOCK (queue);
1751 case PROP_MAX_SIZE_BYTES:
1752 queue->max_size.bytes = g_value_get_uint (value);
1753 queue_capacity_change (queue);
1755 case PROP_MAX_SIZE_BUFFERS:
1756 queue->max_size.buffers = g_value_get_uint (value);
1757 queue_capacity_change (queue);
1759 case PROP_MAX_SIZE_TIME:
1760 queue->max_size.time = g_value_get_uint64 (value);
1761 queue_capacity_change (queue);
1763 case PROP_MIN_THRESHOLD_BYTES:
1764 queue->min_threshold.bytes = g_value_get_uint (value);
1765 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1766 QUEUE_THRESHOLD_CHANGE (queue);
1768 case PROP_MIN_THRESHOLD_BUFFERS:
1769 queue->min_threshold.buffers = g_value_get_uint (value);
1770 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1771 QUEUE_THRESHOLD_CHANGE (queue);
1773 case PROP_MIN_THRESHOLD_TIME:
1774 queue->min_threshold.time = g_value_get_uint64 (value);
1775 queue->orig_min_threshold.time = queue->min_threshold.time;
1776 QUEUE_THRESHOLD_CHANGE (queue);
1779 queue->leaky = g_value_get_enum (value);
1782 queue->silent = g_value_get_boolean (value);
1784 case PROP_FLUSH_ON_EOS:
1785 queue->flush_on_eos = g_value_get_boolean (value);
1788 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1792 GST_QUEUE_MUTEX_UNLOCK (queue);
1796 gst_queue_get_property (GObject * object,
1797 guint prop_id, GValue * value, GParamSpec * pspec)
1799 GstQueue *queue = GST_QUEUE (object);
1801 GST_QUEUE_MUTEX_LOCK (queue);
1804 case PROP_CUR_LEVEL_BYTES:
1805 g_value_set_uint (value, queue->cur_level.bytes);
1807 case PROP_CUR_LEVEL_BUFFERS:
1808 g_value_set_uint (value, queue->cur_level.buffers);
1810 case PROP_CUR_LEVEL_TIME:
1811 g_value_set_uint64 (value, queue->cur_level.time);
1813 case PROP_MAX_SIZE_BYTES:
1814 g_value_set_uint (value, queue->max_size.bytes);
1816 case PROP_MAX_SIZE_BUFFERS:
1817 g_value_set_uint (value, queue->max_size.buffers);
1819 case PROP_MAX_SIZE_TIME:
1820 g_value_set_uint64 (value, queue->max_size.time);
1822 case PROP_MIN_THRESHOLD_BYTES:
1823 g_value_set_uint (value, queue->min_threshold.bytes);
1825 case PROP_MIN_THRESHOLD_BUFFERS:
1826 g_value_set_uint (value, queue->min_threshold.buffers);
1828 case PROP_MIN_THRESHOLD_TIME:
1829 g_value_set_uint64 (value, queue->min_threshold.time);
1832 g_value_set_enum (value, queue->leaky);
1835 g_value_set_boolean (value, queue->silent);
1837 case PROP_FLUSH_ON_EOS:
1838 g_value_set_boolean (value, queue->flush_on_eos);
1841 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1845 GST_QUEUE_MUTEX_UNLOCK (queue);