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 GstFlowReturn gst_queue_handle_sink_event (GstPad * pad,
204 GstObject * parent, GstEvent * event);
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_static_pad_template (gstelement_class, &srctemplate);
411 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
413 /* Registering debug symbols for function pointers */
414 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
415 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
416 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
417 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
418 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
419 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
420 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain_list);
424 gst_queue_init (GstQueue * queue)
426 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
428 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
429 gst_pad_set_chain_list_function (queue->sinkpad, gst_queue_chain_list);
430 gst_pad_set_activatemode_function (queue->sinkpad,
431 gst_queue_sink_activate_mode);
432 gst_pad_set_event_full_function (queue->sinkpad, gst_queue_handle_sink_event);
433 gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
434 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
435 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
437 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
439 gst_pad_set_activatemode_function (queue->srcpad,
440 gst_queue_src_activate_mode);
441 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
442 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
443 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
444 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
446 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
447 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
448 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
449 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
450 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
451 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
452 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
453 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
454 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
456 queue->leaky = GST_QUEUE_NO_LEAK;
457 queue->srcresult = GST_FLOW_FLUSHING;
459 g_mutex_init (&queue->qlock);
460 g_cond_init (&queue->item_add);
461 g_cond_init (&queue->item_del);
462 g_cond_init (&queue->query_handled);
465 gst_queue_array_new_for_struct (sizeof (GstQueueItem),
466 DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
468 queue->sinktime = GST_CLOCK_STIME_NONE;
469 queue->srctime = GST_CLOCK_STIME_NONE;
471 queue->sink_tainted = TRUE;
472 queue->src_tainted = TRUE;
474 queue->newseg_applied_to_src = FALSE;
476 GST_DEBUG_OBJECT (queue,
477 "initialized queue's not_empty & not_full conditions");
480 /* called only once, as opposed to dispose */
482 gst_queue_finalize (GObject * object)
484 GstQueue *queue = GST_QUEUE (object);
487 GST_DEBUG_OBJECT (queue, "finalizing queue");
489 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
490 /* FIXME: if it's a query, shouldn't we unref that too? */
491 if (!qitem->is_query)
492 gst_mini_object_unref (qitem->item);
494 gst_queue_array_free (queue->queue);
496 g_mutex_clear (&queue->qlock);
497 g_cond_clear (&queue->item_add);
498 g_cond_clear (&queue->item_del);
499 g_cond_clear (&queue->query_handled);
501 G_OBJECT_CLASS (parent_class)->finalize (object);
504 /* Convenience function */
505 static inline GstClockTimeDiff
506 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
508 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
510 if (GST_CLOCK_TIME_IS_VALID (val)) {
512 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
521 /* calculate the diff between running time on the sink and src of the queue.
522 * This is the total amount of time in the queue. */
524 update_time_level (GstQueue * queue)
526 gint64 sink_time, src_time;
528 if (queue->sink_tainted) {
529 GST_LOG_OBJECT (queue, "update sink time");
531 my_segment_to_running_time (&queue->sink_segment,
532 queue->sink_segment.position);
533 queue->sink_tainted = FALSE;
535 sink_time = queue->sinktime;
537 if (queue->src_tainted) {
538 GST_LOG_OBJECT (queue, "update src time");
540 my_segment_to_running_time (&queue->src_segment,
541 queue->src_segment.position);
542 queue->src_tainted = FALSE;
544 src_time = queue->srctime;
546 GST_LOG_OBJECT (queue, "sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT,
547 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
549 if (sink_time >= src_time)
550 queue->cur_level.time = sink_time - src_time;
552 queue->cur_level.time = 0;
555 /* take a SEGMENT event and apply the values to segment, updating the time
558 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
561 gst_event_copy_segment (event, segment);
563 /* now configure the values, we use these to track timestamps on the
565 if (segment->format != GST_FORMAT_TIME) {
566 /* non-time format, pretent the current time segment is closed with a
567 * 0 start and unknown stop time. */
568 segment->format = GST_FORMAT_TIME;
574 queue->sink_tainted = TRUE;
576 queue->src_tainted = TRUE;
578 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
580 /* segment can update the time level of the queue */
581 update_time_level (queue);
585 apply_gap (GstQueue * queue, GstEvent * event,
586 GstSegment * segment, gboolean is_sink)
588 GstClockTime timestamp;
589 GstClockTime duration;
591 gst_event_parse_gap (event, ×tamp, &duration);
593 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
595 if (GST_CLOCK_TIME_IS_VALID (duration)) {
596 timestamp += duration;
599 segment->position = timestamp;
602 queue->sink_tainted = TRUE;
604 queue->src_tainted = TRUE;
606 /* calc diff with other end */
607 update_time_level (queue);
612 /* take a buffer and update segment, updating the time level of the queue. */
614 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
617 GstClockTime duration, timestamp;
619 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
620 duration = GST_BUFFER_DURATION (buffer);
622 /* if no timestamp is set, assume it's continuous with the previous
624 if (timestamp == GST_CLOCK_TIME_NONE)
625 timestamp = segment->position;
628 if (duration != GST_CLOCK_TIME_NONE)
629 timestamp += duration;
631 GST_LOG_OBJECT (queue, "%s position updated to %" GST_TIME_FORMAT,
632 segment == &queue->sink_segment ? "sink" : "src",
633 GST_TIME_ARGS (timestamp));
635 segment->position = timestamp;
637 queue->sink_tainted = TRUE;
639 queue->src_tainted = TRUE;
642 /* calc diff with other end */
643 update_time_level (queue);
647 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer user_data)
649 GstClockTime *timestamp = user_data;
652 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
653 " duration %" GST_TIME_FORMAT, idx, GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
654 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
655 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
657 btime = GST_BUFFER_DTS_OR_PTS (*buf);
658 if (GST_CLOCK_TIME_IS_VALID (btime))
661 if (GST_BUFFER_DURATION_IS_VALID (*buf))
662 *timestamp += GST_BUFFER_DURATION (*buf);
664 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
669 /* take a buffer list and update segment, updating the time level of the queue */
671 apply_buffer_list (GstQueue * queue, GstBufferList * buffer_list,
672 GstSegment * segment, gboolean sink)
674 GstClockTime timestamp;
676 /* if no timestamp is set, assume it's continuous with the previous time */
677 timestamp = segment->position;
679 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
681 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
682 GST_TIME_ARGS (timestamp));
684 segment->position = timestamp;
687 queue->sink_tainted = TRUE;
689 queue->src_tainted = TRUE;
691 /* calc diff with other end */
692 update_time_level (queue);
696 gst_queue_locked_flush (GstQueue * queue, gboolean full)
700 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
701 /* Then lose another reference because we are supposed to destroy that
702 data when flushing */
703 if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
704 && GST_EVENT_IS_STICKY (qitem->item)
705 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
706 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
707 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
709 if (!qitem->is_query)
710 gst_mini_object_unref (qitem->item);
711 memset (qitem, 0, sizeof (GstQueueItem));
713 queue->last_query = FALSE;
714 g_cond_signal (&queue->query_handled);
715 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
716 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
717 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
718 queue->min_threshold.time = queue->orig_min_threshold.time;
719 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
720 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
721 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
723 queue->sinktime = queue->srctime = GST_CLOCK_STIME_NONE;
724 queue->sink_tainted = queue->src_tainted = TRUE;
726 /* we deleted a lot of something */
727 GST_QUEUE_SIGNAL_DEL (queue);
730 /* enqueue an item an update the level stats, with QUEUE_LOCK */
732 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
735 GstBuffer *buffer = GST_BUFFER_CAST (item);
736 gsize bsize = gst_buffer_get_size (buffer);
738 /* add buffer to the statistics */
739 queue->cur_level.buffers++;
740 queue->cur_level.bytes += bsize;
741 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
744 qitem.is_query = FALSE;
746 gst_queue_array_push_tail_struct (queue->queue, &qitem);
747 GST_QUEUE_SIGNAL_ADD (queue);
751 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
753 guint *p_size = data;
756 buf_size = gst_buffer_get_size (*buf);
757 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
763 gst_queue_locked_enqueue_buffer_list (GstQueue * queue, gpointer item)
766 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
769 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &bsize);
771 /* add buffer to the statistics */
772 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
773 queue->cur_level.bytes += bsize;
774 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
777 qitem.is_query = FALSE;
779 gst_queue_array_push_tail_struct (queue->queue, &qitem);
780 GST_QUEUE_SIGNAL_ADD (queue);
784 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
787 GstEvent *event = GST_EVENT_CAST (item);
789 switch (GST_EVENT_TYPE (event)) {
791 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
792 /* Zero the thresholds, this makes sure the queue is completely
793 * filled and we can read all data from the queue. */
794 if (queue->flush_on_eos)
795 gst_queue_locked_flush (queue, FALSE);
797 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
798 /* mark the queue as EOS. This prevents us from accepting more data. */
801 case GST_EVENT_SEGMENT:
802 apply_segment (queue, event, &queue->sink_segment, TRUE);
803 /* if the queue is empty, apply sink segment on the source */
804 if (gst_queue_array_is_empty (queue->queue)) {
805 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
806 apply_segment (queue, event, &queue->src_segment, FALSE);
807 queue->newseg_applied_to_src = TRUE;
809 /* a new segment allows us to accept more buffers if we got EOS
811 queue->unexpected = FALSE;
814 apply_gap (queue, event, &queue->sink_segment, TRUE);
821 qitem.is_query = FALSE;
823 gst_queue_array_push_tail_struct (queue->queue, &qitem);
824 GST_QUEUE_SIGNAL_ADD (queue);
827 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
828 static GstMiniObject *
829 gst_queue_locked_dequeue (GstQueue * queue)
835 qitem = gst_queue_array_pop_head_struct (queue->queue);
840 bufsize = qitem->size;
842 if (GST_IS_BUFFER (item)) {
843 GstBuffer *buffer = GST_BUFFER_CAST (item);
845 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
846 "retrieved buffer %p from queue", buffer);
848 queue->cur_level.buffers--;
849 queue->cur_level.bytes -= bufsize;
850 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
852 /* if the queue is empty now, update the other side */
853 if (queue->cur_level.buffers == 0)
854 queue->cur_level.time = 0;
855 } else if (GST_IS_BUFFER_LIST (item)) {
856 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
858 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
859 "retrieved buffer list %p from queue", buffer_list);
861 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
862 queue->cur_level.bytes -= bufsize;
863 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
865 /* if the queue is empty now, update the other side */
866 if (queue->cur_level.buffers == 0)
867 queue->cur_level.time = 0;
868 } else if (GST_IS_EVENT (item)) {
869 GstEvent *event = GST_EVENT_CAST (item);
871 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
872 "retrieved event %p from queue", event);
874 switch (GST_EVENT_TYPE (event)) {
876 /* queue is empty now that we dequeued the EOS */
877 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
879 case GST_EVENT_SEGMENT:
880 /* apply newsegment if it has not already been applied */
881 if (G_LIKELY (!queue->newseg_applied_to_src)) {
882 apply_segment (queue, event, &queue->src_segment, FALSE);
884 queue->newseg_applied_to_src = FALSE;
888 apply_gap (queue, event, &queue->src_segment, FALSE);
893 } else if (GST_IS_QUERY (item)) {
894 GstQuery *query = GST_QUERY_CAST (item);
896 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
897 "retrieved query %p from queue", query);
900 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
901 item, GST_OBJECT_NAME (queue));
904 GST_QUEUE_SIGNAL_DEL (queue);
911 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
917 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
922 queue = GST_QUEUE (parent);
924 switch (GST_EVENT_TYPE (event)) {
925 case GST_EVENT_FLUSH_START:
926 STATUS (queue, pad, "received flush start event");
928 ret = gst_pad_push_event (queue->srcpad, event);
930 /* now unblock the chain function */
931 GST_QUEUE_MUTEX_LOCK (queue);
932 queue->srcresult = GST_FLOW_FLUSHING;
933 /* unblock the loop and chain functions */
934 GST_QUEUE_SIGNAL_ADD (queue);
935 GST_QUEUE_SIGNAL_DEL (queue);
936 GST_QUEUE_MUTEX_UNLOCK (queue);
938 /* make sure it pauses, this should happen since we sent
939 * flush_start downstream. */
940 gst_pad_pause_task (queue->srcpad);
941 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
943 /* unblock query handler after the streaming thread is shut down.
944 * Otherwise downstream might have a query that is already unreffed
946 GST_QUEUE_MUTEX_LOCK (queue);
947 queue->last_query = FALSE;
948 g_cond_signal (&queue->query_handled);
949 GST_QUEUE_MUTEX_UNLOCK (queue);
951 case GST_EVENT_FLUSH_STOP:
952 STATUS (queue, pad, "received flush stop event");
954 ret = gst_pad_push_event (queue->srcpad, event);
956 GST_QUEUE_MUTEX_LOCK (queue);
957 gst_queue_locked_flush (queue, FALSE);
958 queue->srcresult = GST_FLOW_OK;
960 queue->unexpected = FALSE;
961 if (gst_pad_is_active (queue->srcpad)) {
962 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
963 queue->srcpad, NULL);
965 GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
966 "pad not active any longer");
968 GST_QUEUE_MUTEX_UNLOCK (queue);
970 STATUS (queue, pad, "after flush");
973 if (GST_EVENT_IS_SERIALIZED (event)) {
974 /* serialized events go in the queue */
975 GST_QUEUE_MUTEX_LOCK (queue);
976 if (queue->srcresult != GST_FLOW_OK) {
977 /* Errors in sticky event pushing are no problem and ignored here
978 * as they will cause more meaningful errors during data flow.
979 * For EOS events, that are not followed by data flow, we still
980 * return FALSE here though and report an error.
982 if (!GST_EVENT_IS_STICKY (event)) {
983 GST_QUEUE_MUTEX_UNLOCK (queue);
985 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
986 if (queue->srcresult == GST_FLOW_NOT_LINKED
987 || queue->srcresult < GST_FLOW_EOS) {
988 GST_QUEUE_MUTEX_UNLOCK (queue);
989 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
991 GST_QUEUE_MUTEX_UNLOCK (queue);
996 /* refuse more events on EOS */
999 gst_queue_locked_enqueue_event (queue, event);
1000 GST_QUEUE_MUTEX_UNLOCK (queue);
1002 /* non-serialized events are forwarded downstream immediately */
1003 ret = gst_pad_push_event (queue->srcpad, event);
1008 return GST_FLOW_ERROR;
1014 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
1015 GST_QUEUE_MUTEX_UNLOCK (queue);
1016 gst_event_unref (event);
1017 return GST_FLOW_EOS;
1021 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1022 "refusing event, we have a downstream flow error: %s",
1023 gst_flow_get_name (queue->srcresult));
1024 gst_event_unref (event);
1025 return queue->srcresult;
1030 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1032 GstQueue *queue = GST_QUEUE_CAST (parent);
1035 switch (GST_QUERY_TYPE (query)) {
1037 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
1040 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1041 GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
1042 GST_QUERY_TYPE_NAME (query));
1043 qitem.item = GST_MINI_OBJECT_CAST (query);
1044 qitem.is_query = TRUE;
1046 gst_queue_array_push_tail_struct (queue->queue, &qitem);
1047 GST_QUEUE_SIGNAL_ADD (queue);
1048 g_cond_wait (&queue->query_handled, &queue->qlock);
1049 if (queue->srcresult != GST_FLOW_OK)
1051 res = queue->last_query;
1052 GST_QUEUE_MUTEX_UNLOCK (queue);
1054 res = gst_pad_query_default (pad, parent, query);
1063 GST_DEBUG_OBJECT (queue, "we are flushing");
1064 GST_QUEUE_MUTEX_UNLOCK (queue);
1070 gst_queue_is_empty (GstQueue * queue)
1074 head = gst_queue_array_peek_head_struct (queue->queue);
1079 /* Only consider the queue empty if the minimum thresholds
1080 * are not reached and data is at the queue head. Otherwise
1081 * we would block forever on serialized queries.
1083 if (!GST_IS_BUFFER (head->item) && !GST_IS_BUFFER_LIST (head->item))
1086 /* It is possible that a max size is reached before all min thresholds are.
1087 * Therefore, only consider it empty if it is not filled. */
1088 return ((queue->min_threshold.buffers > 0 &&
1089 queue->cur_level.buffers < queue->min_threshold.buffers) ||
1090 (queue->min_threshold.bytes > 0 &&
1091 queue->cur_level.bytes < queue->min_threshold.bytes) ||
1092 (queue->min_threshold.time > 0 &&
1093 queue->cur_level.time < queue->min_threshold.time)) &&
1094 !gst_queue_is_filled (queue);
1098 gst_queue_is_filled (GstQueue * queue)
1100 return (((queue->max_size.buffers > 0 &&
1101 queue->cur_level.buffers >= queue->max_size.buffers) ||
1102 (queue->max_size.bytes > 0 &&
1103 queue->cur_level.bytes >= queue->max_size.bytes) ||
1104 (queue->max_size.time > 0 &&
1105 queue->cur_level.time >= queue->max_size.time)));
1109 gst_queue_leak_downstream (GstQueue * queue)
1111 /* for as long as the queue is filled, dequeue an item and discard it */
1112 while (gst_queue_is_filled (queue)) {
1113 GstMiniObject *leak;
1115 leak = gst_queue_locked_dequeue (queue);
1116 /* there is nothing to dequeue and the queue is still filled.. This should
1118 g_assert (leak != NULL);
1120 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1121 "queue is full, leaking item %p on downstream end", leak);
1122 if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
1123 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1124 "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
1125 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
1128 if (!GST_IS_QUERY (leak))
1129 gst_mini_object_unref (leak);
1131 /* last buffer needs to get a DISCONT flag */
1132 queue->head_needs_discont = TRUE;
1137 discont_first_buffer (GstBuffer ** buffer, guint i, gpointer user_data)
1139 GstQueue *queue = user_data;
1140 GstBuffer *subbuffer = gst_buffer_make_writable (*buffer);
1143 *buffer = subbuffer;
1144 GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
1146 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1152 static GstFlowReturn
1153 gst_queue_chain_buffer_or_list (GstPad * pad, GstObject * parent,
1154 GstMiniObject * obj, gboolean is_list)
1158 queue = GST_QUEUE_CAST (parent);
1160 /* we have to lock the queue since we span threads */
1161 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1162 /* when we received EOS, we refuse any more data */
1165 if (queue->unexpected)
1166 goto out_unexpected;
1169 GstClockTime duration, timestamp;
1170 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1172 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1173 duration = GST_BUFFER_DURATION (buffer);
1175 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
1176 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1177 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1178 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1180 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1181 "received buffer list %p with %u buffers", obj,
1182 gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)));
1185 /* We make space available if we're "full" according to whatever
1186 * the user defined as "full". Note that this only applies to buffers.
1187 * We always handle events and they don't count in our statistics. */
1188 while (gst_queue_is_filled (queue)) {
1189 if (!queue->silent) {
1190 GST_QUEUE_MUTEX_UNLOCK (queue);
1191 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
1192 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1193 /* we recheck, the signal could have changed the thresholds */
1194 if (!gst_queue_is_filled (queue))
1198 /* how are we going to make space for this buffer? */
1199 switch (queue->leaky) {
1200 case GST_QUEUE_LEAK_UPSTREAM:
1201 /* next buffer needs to get a DISCONT flag */
1202 queue->tail_needs_discont = TRUE;
1203 /* leak current buffer */
1204 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1205 "queue is full, leaking buffer on upstream end");
1206 /* now we can clean up and exit right away */
1208 case GST_QUEUE_LEAK_DOWNSTREAM:
1209 gst_queue_leak_downstream (queue);
1212 g_warning ("Unknown leaky type, using default");
1214 case GST_QUEUE_NO_LEAK:
1216 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1217 "queue is full, waiting for free space");
1219 /* don't leak. Instead, wait for space to be available */
1221 /* for as long as the queue is filled, wait till an item was deleted. */
1222 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1223 } while (gst_queue_is_filled (queue));
1225 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1227 if (!queue->silent) {
1228 GST_QUEUE_MUTEX_UNLOCK (queue);
1229 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1230 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1237 if (queue->tail_needs_discont) {
1239 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1240 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1244 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1246 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1249 obj = GST_MINI_OBJECT_CAST (buffer);
1251 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
1253 buffer_list = gst_buffer_list_make_writable (buffer_list);
1254 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1255 obj = GST_MINI_OBJECT_CAST (buffer_list);
1257 queue->tail_needs_discont = FALSE;
1260 /* put buffer in queue now */
1262 gst_queue_locked_enqueue_buffer_list (queue, obj);
1264 gst_queue_locked_enqueue_buffer (queue, obj);
1265 GST_QUEUE_MUTEX_UNLOCK (queue);
1269 /* special conditions */
1272 GST_QUEUE_MUTEX_UNLOCK (queue);
1274 gst_mini_object_unref (obj);
1280 GstFlowReturn ret = queue->srcresult;
1282 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1283 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1284 GST_QUEUE_MUTEX_UNLOCK (queue);
1285 gst_mini_object_unref (obj);
1291 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1292 GST_QUEUE_MUTEX_UNLOCK (queue);
1294 gst_mini_object_unref (obj);
1296 return GST_FLOW_EOS;
1300 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1301 GST_QUEUE_MUTEX_UNLOCK (queue);
1303 gst_mini_object_unref (obj);
1305 return GST_FLOW_EOS;
1309 static GstFlowReturn
1310 gst_queue_chain_list (GstPad * pad, GstObject * parent,
1311 GstBufferList * buffer_list)
1313 return gst_queue_chain_buffer_or_list (pad, parent,
1314 GST_MINI_OBJECT_CAST (buffer_list), TRUE);
1317 static GstFlowReturn
1318 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1320 return gst_queue_chain_buffer_or_list (pad, parent,
1321 GST_MINI_OBJECT_CAST (buffer), FALSE);
1324 /* dequeue an item from the queue an push it downstream. This functions returns
1325 * the result of the push. */
1326 static GstFlowReturn
1327 gst_queue_push_one (GstQueue * queue)
1329 GstFlowReturn result = queue->srcresult;
1330 GstMiniObject *data;
1333 data = gst_queue_locked_dequeue (queue);
1338 is_list = GST_IS_BUFFER_LIST (data);
1340 if (GST_IS_BUFFER (data) || is_list) {
1344 buffer = GST_BUFFER_CAST (data);
1346 if (queue->head_needs_discont) {
1347 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1351 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1353 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1355 queue->head_needs_discont = FALSE;
1358 GST_QUEUE_MUTEX_UNLOCK (queue);
1359 result = gst_pad_push (queue->srcpad, buffer);
1361 GstBufferList *buffer_list;
1363 buffer_list = GST_BUFFER_LIST_CAST (data);
1365 if (queue->head_needs_discont) {
1366 buffer_list = gst_buffer_list_make_writable (buffer_list);
1367 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1368 queue->head_needs_discont = FALSE;
1371 GST_QUEUE_MUTEX_UNLOCK (queue);
1372 result = gst_pad_push_list (queue->srcpad, buffer_list);
1375 /* need to check for srcresult here as well */
1376 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1378 if (result == GST_FLOW_EOS) {
1379 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1380 /* stop pushing buffers, we dequeue all items until we see an item that we
1381 * can push again, which is EOS or SEGMENT. If there is nothing in the
1382 * queue we can push, we set a flag to make the sinkpad refuse more
1383 * buffers with an EOS return value. */
1384 while ((data = gst_queue_locked_dequeue (queue))) {
1385 if (GST_IS_BUFFER (data)) {
1386 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1387 "dropping EOS buffer %p", data);
1388 gst_buffer_unref (GST_BUFFER_CAST (data));
1389 } else if (GST_IS_BUFFER_LIST (data)) {
1390 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1391 "dropping EOS buffer list %p", data);
1392 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
1393 } else if (GST_IS_EVENT (data)) {
1394 GstEvent *event = GST_EVENT_CAST (data);
1395 GstEventType type = GST_EVENT_TYPE (event);
1397 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
1398 /* we found a pushable item in the queue, push it out */
1399 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1400 "pushing pushable event %s after EOS",
1401 GST_EVENT_TYPE_NAME (event));
1404 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1405 "dropping EOS event %p", event);
1406 gst_event_unref (event);
1407 } else if (GST_IS_QUERY (data)) {
1408 GstQuery *query = GST_QUERY_CAST (data);
1410 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1411 "dropping query %p because of EOS", query);
1412 queue->last_query = FALSE;
1413 g_cond_signal (&queue->query_handled);
1416 /* no more items in the queue. Set the unexpected flag so that upstream
1417 * make us refuse any more buffers on the sinkpad. Since we will still
1418 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1419 * task function does not shut down. */
1420 queue->unexpected = TRUE;
1421 result = GST_FLOW_OK;
1423 } else if (GST_IS_EVENT (data)) {
1424 GstEvent *event = GST_EVENT_CAST (data);
1425 GstEventType type = GST_EVENT_TYPE (event);
1427 GST_QUEUE_MUTEX_UNLOCK (queue);
1429 gst_pad_push_event (queue->srcpad, event);
1431 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1432 /* if we're EOS, return EOS so that the task pauses. */
1433 if (type == GST_EVENT_EOS) {
1434 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1435 "pushed EOS event %p, return EOS", event);
1436 result = GST_FLOW_EOS;
1438 } else if (GST_IS_QUERY (data)) {
1439 GstQuery *query = GST_QUERY_CAST (data);
1442 GST_QUEUE_MUTEX_UNLOCK (queue);
1443 ret = gst_pad_peer_query (queue->srcpad, query);
1444 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1445 queue->last_query = ret;
1446 g_cond_signal (&queue->query_handled);
1447 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1448 "did query %p, return %d", query, queue->last_query);
1455 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1456 "exit because we have no item in the queue");
1457 return GST_FLOW_ERROR;
1461 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1462 return GST_FLOW_FLUSHING;
1466 queue->last_query = FALSE;
1467 g_cond_signal (&queue->query_handled);
1468 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1469 return GST_FLOW_FLUSHING;
1474 gst_queue_loop (GstPad * pad)
1479 queue = (GstQueue *) GST_PAD_PARENT (pad);
1481 /* have to lock for thread-safety */
1482 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1484 while (gst_queue_is_empty (queue)) {
1485 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1486 if (!queue->silent) {
1487 GST_QUEUE_MUTEX_UNLOCK (queue);
1488 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1489 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1492 /* we recheck, the signal could have changed the thresholds */
1493 while (gst_queue_is_empty (queue)) {
1494 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1497 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1498 if (!queue->silent) {
1499 GST_QUEUE_MUTEX_UNLOCK (queue);
1500 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1501 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1502 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1506 ret = gst_queue_push_one (queue);
1507 queue->srcresult = ret;
1508 if (ret != GST_FLOW_OK)
1511 GST_QUEUE_MUTEX_UNLOCK (queue);
1518 gboolean eos = queue->eos;
1519 GstFlowReturn ret = queue->srcresult;
1521 gst_pad_pause_task (queue->srcpad);
1522 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1523 "pause task, reason: %s", gst_flow_get_name (ret));
1524 if (ret == GST_FLOW_FLUSHING) {
1525 gst_queue_locked_flush (queue, FALSE);
1527 GST_QUEUE_SIGNAL_DEL (queue);
1528 queue->last_query = FALSE;
1529 g_cond_signal (&queue->query_handled);
1531 GST_QUEUE_MUTEX_UNLOCK (queue);
1532 /* let app know about us giving up if upstream is not expected to do so */
1533 /* EOS is already taken care of elsewhere */
1534 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1535 GST_ELEMENT_FLOW_ERROR (queue, ret);
1536 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1543 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1545 gboolean res = TRUE;
1546 GstQueue *queue = GST_QUEUE (parent);
1548 #ifndef GST_DISABLE_GST_DEBUG
1549 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1550 event, GST_EVENT_TYPE (event));
1553 switch (GST_EVENT_TYPE (event)) {
1554 case GST_EVENT_RECONFIGURE:
1555 GST_QUEUE_MUTEX_LOCK (queue);
1556 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1557 /* when we got not linked, assume downstream is linked again now and we
1558 * can try to start pushing again */
1559 queue->srcresult = GST_FLOW_OK;
1560 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1562 GST_QUEUE_MUTEX_UNLOCK (queue);
1564 res = gst_pad_push_event (queue->sinkpad, event);
1567 res = gst_pad_event_default (pad, parent, event);
1576 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1578 GstQueue *queue = GST_QUEUE (parent);
1581 switch (GST_QUERY_TYPE (query)) {
1582 case GST_QUERY_SCHEDULING:{
1583 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1588 res = gst_pad_query_default (pad, parent, query);
1595 /* Adjust peer response for data contained in queue */
1596 switch (GST_QUERY_TYPE (query)) {
1597 case GST_QUERY_POSITION:
1602 /* get peer position */
1603 gst_query_parse_position (query, &format, &peer_pos);
1605 /* FIXME: this code assumes that there's no discont in the queue */
1607 case GST_FORMAT_BYTES:
1608 peer_pos -= queue->cur_level.bytes;
1609 if (peer_pos < 0) /* Clamp result to 0 */
1612 case GST_FORMAT_TIME:
1613 peer_pos -= queue->cur_level.time;
1614 if (peer_pos < 0) /* Clamp result to 0 */
1618 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1619 "know how to adjust value", gst_format_get_name (format));
1622 /* set updated position */
1623 gst_query_set_position (query, format, peer_pos);
1626 case GST_QUERY_LATENCY:
1629 GstClockTime min, max;
1631 gst_query_parse_latency (query, &live, &min, &max);
1633 /* we can delay up to the limit of the queue in time. If we have no time
1634 * limit, the best thing we can do is to return an infinite delay. In
1635 * reality a better estimate would be the byte/buffer rate but that is not
1636 * possible right now. */
1637 /* TODO: Use CONVERT query? */
1638 if (queue->max_size.time > 0 && max != -1
1639 && queue->leaky == GST_QUEUE_NO_LEAK)
1640 max += queue->max_size.time;
1641 else if (queue->max_size.time > 0 && queue->leaky != GST_QUEUE_NO_LEAK)
1642 max = MIN (queue->max_size.time, max);
1646 /* adjust for min-threshold */
1647 if (queue->min_threshold.time > 0)
1648 min += queue->min_threshold.time;
1650 gst_query_set_latency (query, live, min, max);
1654 /* peer handled other queries */
1662 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1668 queue = GST_QUEUE (parent);
1671 case GST_PAD_MODE_PUSH:
1673 GST_QUEUE_MUTEX_LOCK (queue);
1674 queue->srcresult = GST_FLOW_OK;
1676 queue->unexpected = FALSE;
1677 GST_QUEUE_MUTEX_UNLOCK (queue);
1679 /* step 1, unblock chain function */
1680 GST_QUEUE_MUTEX_LOCK (queue);
1681 queue->srcresult = GST_FLOW_FLUSHING;
1682 /* the item del signal will unblock */
1683 GST_QUEUE_SIGNAL_DEL (queue);
1684 GST_QUEUE_MUTEX_UNLOCK (queue);
1686 /* step 2, wait until streaming thread stopped and flush queue */
1687 GST_PAD_STREAM_LOCK (pad);
1688 GST_QUEUE_MUTEX_LOCK (queue);
1689 gst_queue_locked_flush (queue, TRUE);
1690 GST_QUEUE_MUTEX_UNLOCK (queue);
1691 GST_PAD_STREAM_UNLOCK (pad);
1703 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1709 queue = GST_QUEUE (parent);
1712 case GST_PAD_MODE_PUSH:
1714 GST_QUEUE_MUTEX_LOCK (queue);
1715 queue->srcresult = GST_FLOW_OK;
1717 queue->unexpected = FALSE;
1719 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1721 GST_QUEUE_MUTEX_UNLOCK (queue);
1723 /* step 1, unblock loop function */
1724 GST_QUEUE_MUTEX_LOCK (queue);
1725 queue->srcresult = GST_FLOW_FLUSHING;
1726 /* the item add signal will unblock */
1727 g_cond_signal (&queue->item_add);
1728 GST_QUEUE_MUTEX_UNLOCK (queue);
1730 /* step 2, make sure streaming finishes */
1731 result = gst_pad_stop_task (pad);
1742 queue_capacity_change (GstQueue * queue)
1744 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1745 gst_queue_leak_downstream (queue);
1748 /* changing the capacity of the queue must wake up
1749 * the _chain function, it might have more room now
1750 * to store the buffer/event in the queue */
1751 GST_QUEUE_SIGNAL_DEL (queue);
1754 /* Changing the minimum required fill level must
1755 * wake up the _loop function as it might now
1756 * be able to preceed.
1758 #define QUEUE_THRESHOLD_CHANGE(q)\
1759 GST_QUEUE_SIGNAL_ADD (q);
1762 gst_queue_set_property (GObject * object,
1763 guint prop_id, const GValue * value, GParamSpec * pspec)
1765 GstQueue *queue = GST_QUEUE (object);
1767 /* someone could change levels here, and since this
1768 * affects the get/put funcs, we need to lock for safety. */
1769 GST_QUEUE_MUTEX_LOCK (queue);
1772 case PROP_MAX_SIZE_BYTES:
1773 queue->max_size.bytes = g_value_get_uint (value);
1774 queue_capacity_change (queue);
1776 case PROP_MAX_SIZE_BUFFERS:
1777 queue->max_size.buffers = g_value_get_uint (value);
1778 queue_capacity_change (queue);
1780 case PROP_MAX_SIZE_TIME:
1781 queue->max_size.time = g_value_get_uint64 (value);
1782 queue_capacity_change (queue);
1784 case PROP_MIN_THRESHOLD_BYTES:
1785 queue->min_threshold.bytes = g_value_get_uint (value);
1786 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1787 QUEUE_THRESHOLD_CHANGE (queue);
1789 case PROP_MIN_THRESHOLD_BUFFERS:
1790 queue->min_threshold.buffers = g_value_get_uint (value);
1791 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1792 QUEUE_THRESHOLD_CHANGE (queue);
1794 case PROP_MIN_THRESHOLD_TIME:
1795 queue->min_threshold.time = g_value_get_uint64 (value);
1796 queue->orig_min_threshold.time = queue->min_threshold.time;
1797 QUEUE_THRESHOLD_CHANGE (queue);
1800 queue->leaky = g_value_get_enum (value);
1803 queue->silent = g_value_get_boolean (value);
1805 case PROP_FLUSH_ON_EOS:
1806 queue->flush_on_eos = g_value_get_boolean (value);
1809 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1813 GST_QUEUE_MUTEX_UNLOCK (queue);
1817 gst_queue_get_property (GObject * object,
1818 guint prop_id, GValue * value, GParamSpec * pspec)
1820 GstQueue *queue = GST_QUEUE (object);
1822 GST_QUEUE_MUTEX_LOCK (queue);
1825 case PROP_CUR_LEVEL_BYTES:
1826 g_value_set_uint (value, queue->cur_level.bytes);
1828 case PROP_CUR_LEVEL_BUFFERS:
1829 g_value_set_uint (value, queue->cur_level.buffers);
1831 case PROP_CUR_LEVEL_TIME:
1832 g_value_set_uint64 (value, queue->cur_level.time);
1834 case PROP_MAX_SIZE_BYTES:
1835 g_value_set_uint (value, queue->max_size.bytes);
1837 case PROP_MAX_SIZE_BUFFERS:
1838 g_value_set_uint (value, queue->max_size.buffers);
1840 case PROP_MAX_SIZE_TIME:
1841 g_value_set_uint64 (value, queue->max_size.time);
1843 case PROP_MIN_THRESHOLD_BYTES:
1844 g_value_set_uint (value, queue->min_threshold.bytes);
1846 case PROP_MIN_THRESHOLD_BUFFERS:
1847 g_value_set_uint (value, queue->min_threshold.buffers);
1849 case PROP_MIN_THRESHOLD_TIME:
1850 g_value_set_uint64 (value, queue->min_threshold.time);
1853 g_value_set_enum (value, queue->leaky);
1856 g_value_set_boolean (value, queue->silent);
1858 case PROP_FLUSH_ON_EOS:
1859 g_value_set_boolean (value, queue->flush_on_eos);
1862 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1866 GST_QUEUE_MUTEX_UNLOCK (queue);