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);
192 static void gst_queue_set_property (GObject * object,
193 guint prop_id, const GValue * value, GParamSpec * pspec);
194 static void gst_queue_get_property (GObject * object,
195 guint prop_id, GValue * value, GParamSpec * pspec);
197 static GstFlowReturn gst_queue_chain (GstPad * pad, GstObject * parent,
199 static GstFlowReturn gst_queue_push_one (GstQueue * queue);
200 static void gst_queue_loop (GstPad * pad);
202 static gboolean gst_queue_handle_sink_event (GstPad * pad, GstObject * parent,
204 static gboolean gst_queue_handle_sink_query (GstPad * pad, GstObject * parent,
207 static gboolean gst_queue_handle_src_event (GstPad * pad, GstObject * parent,
209 static gboolean gst_queue_handle_src_query (GstPad * pad, GstObject * parent,
212 static void gst_queue_locked_flush (GstQueue * queue, gboolean full);
214 static gboolean gst_queue_src_activate_mode (GstPad * pad, GstObject * parent,
215 GstPadMode mode, gboolean active);
216 static gboolean gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
217 GstPadMode mode, gboolean active);
219 static gboolean gst_queue_is_empty (GstQueue * queue);
220 static gboolean gst_queue_is_filled (GstQueue * queue);
222 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
225 queue_leaky_get_type (void)
227 static GType queue_leaky_type = 0;
228 static const GEnumValue queue_leaky[] = {
229 {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
230 {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
231 {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
236 if (!queue_leaky_type) {
237 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
239 return queue_leaky_type;
242 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
245 gst_queue_class_init (GstQueueClass * klass)
247 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
248 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
250 gobject_class->set_property = gst_queue_set_property;
251 gobject_class->get_property = gst_queue_get_property;
255 * GstQueue::underrun:
256 * @queue: the queue instance
258 * Reports that the buffer became empty (underrun).
259 * A buffer is empty if the total amount of data inside it (num-buffers, time,
260 * size) is lower than the boundary values which can be set through the
261 * GObject properties.
263 gst_queue_signals[SIGNAL_UNDERRUN] =
264 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
265 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
266 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
269 * @queue: the queue instance
271 * Reports that enough (min-threshold) data is in the queue. Use this signal
272 * together with the underrun signal to pause the pipeline on underrun and
273 * wait for the queue to fill-up before resume playback.
275 gst_queue_signals[SIGNAL_RUNNING] =
276 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
277 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
278 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
281 * @queue: the queue instance
283 * Reports that the buffer became full (overrun).
284 * A buffer is full if the total amount of data inside it (num-buffers, time,
285 * size) is higher than the boundary values which can be set through the
286 * GObject properties.
288 gst_queue_signals[SIGNAL_OVERRUN] =
289 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
290 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
291 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
294 * @queue: the queue instance
296 * Reports when the queue has enough data to start pushing data again on the
299 gst_queue_signals[SIGNAL_PUSHING] =
300 g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
301 G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
302 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
305 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
306 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
307 "Current amount of data in the queue (bytes)",
308 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
310 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
311 "Current number of buffers in the queue",
312 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
314 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
315 "Current amount of data in the queue (in ns)",
316 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
319 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
320 "Max. amount of data in the queue (bytes, 0=disable)",
321 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
322 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
324 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
325 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
326 DEFAULT_MAX_SIZE_BUFFERS,
327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
329 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
330 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
331 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
334 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
335 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
336 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
338 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
339 "Min. number of buffers in the queue to allow reading (0=disable)",
340 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
342 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
343 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
344 0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_LEAKY,
347 g_param_spec_enum ("leaky", "Leaky",
348 "Where the queue leaks, if at all",
349 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 * Don't emit queue signals. Makes queues more lightweight if no signals are
358 g_object_class_install_property (gobject_class, PROP_SILENT,
359 g_param_spec_boolean ("silent", "Silent",
360 "Don't emit queue signals", FALSE,
361 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 * GstQueue:flush-on-eos
366 * Discard all data in the queue when an EOS event is received, and pass
367 * on the EOS event as soon as possible (instead of waiting until all
368 * buffers in the queue have been processed, which is the default behaviour).
370 * Flushing the queue on EOS might be useful when capturing and encoding
371 * from a live source, to finish up the recording quickly in cases when
372 * the encoder is slow. Note that this might mean some data from the end of
373 * the recoding data might be lost though (never more than the configured
374 * max. sizes though).
378 g_object_class_install_property (gobject_class, PROP_FLUSH_ON_EOS,
379 g_param_spec_boolean ("flush-on-eos", "Flush on EOS",
380 "Discard all data in the queue when an EOS event is received", FALSE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 gobject_class->finalize = gst_queue_finalize;
385 gst_element_class_set_static_metadata (gstelement_class,
387 "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
388 gst_element_class_add_pad_template (gstelement_class,
389 gst_static_pad_template_get (&srctemplate));
390 gst_element_class_add_pad_template (gstelement_class,
391 gst_static_pad_template_get (&sinktemplate));
393 /* Registering debug symbols for function pointers */
394 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
395 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
396 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
397 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
398 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
399 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
403 gst_queue_init (GstQueue * queue)
405 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
407 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
408 gst_pad_set_activatemode_function (queue->sinkpad,
409 gst_queue_sink_activate_mode);
410 gst_pad_set_event_function (queue->sinkpad, gst_queue_handle_sink_event);
411 gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
412 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
413 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
415 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
417 gst_pad_set_activatemode_function (queue->srcpad,
418 gst_queue_src_activate_mode);
419 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
420 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
421 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
422 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
424 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
425 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
426 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
427 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
428 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
429 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
430 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
431 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
432 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
434 queue->leaky = GST_QUEUE_NO_LEAK;
435 queue->srcresult = GST_FLOW_FLUSHING;
437 g_mutex_init (&queue->qlock);
438 g_cond_init (&queue->item_add);
439 g_cond_init (&queue->item_del);
441 queue->queue = gst_queue_array_new (DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
443 queue->sinktime = GST_CLOCK_TIME_NONE;
444 queue->srctime = GST_CLOCK_TIME_NONE;
446 queue->sink_tainted = TRUE;
447 queue->src_tainted = TRUE;
449 queue->newseg_applied_to_src = FALSE;
451 GST_DEBUG_OBJECT (queue,
452 "initialized queue's not_empty & not_full conditions");
455 /* called only once, as opposed to dispose */
457 gst_queue_finalize (GObject * object)
460 GstQueue *queue = GST_QUEUE (object);
462 GST_DEBUG_OBJECT (queue, "finalizing queue");
464 while (!gst_queue_array_is_empty (queue->queue)) {
465 data = gst_queue_array_pop_head (queue->queue);
466 /* FIXME: if it's a query, shouldn't we unref that too? */
467 if (!GST_IS_QUERY (data))
468 gst_mini_object_unref (data);
470 gst_queue_array_free (queue->queue);
472 g_mutex_clear (&queue->qlock);
473 g_cond_clear (&queue->item_add);
474 g_cond_clear (&queue->item_del);
476 G_OBJECT_CLASS (parent_class)->finalize (object);
479 /* calculate the diff between running time on the sink and src of the queue.
480 * This is the total amount of time in the queue. */
482 update_time_level (GstQueue * queue)
484 gint64 sink_time, src_time;
486 if (queue->sink_tainted) {
487 GST_LOG_OBJECT (queue, "update sink time");
489 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
490 queue->sink_segment.position);
491 queue->sink_tainted = FALSE;
493 sink_time = queue->sinktime;
495 if (queue->src_tainted) {
496 GST_LOG_OBJECT (queue, "update src time");
498 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
499 queue->src_segment.position);
500 queue->src_tainted = FALSE;
502 src_time = queue->srctime;
504 GST_LOG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
505 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
507 if (sink_time >= src_time)
508 queue->cur_level.time = sink_time - src_time;
510 queue->cur_level.time = 0;
513 /* take a SEGMENT event and apply the values to segment, updating the time
516 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
519 gst_event_copy_segment (event, segment);
521 /* now configure the values, we use these to track timestamps on the
523 if (segment->format != GST_FORMAT_TIME) {
524 /* non-time format, pretent the current time segment is closed with a
525 * 0 start and unknown stop time. */
526 segment->format = GST_FORMAT_TIME;
532 queue->sink_tainted = TRUE;
534 queue->src_tainted = TRUE;
536 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
538 /* segment can update the time level of the queue */
539 update_time_level (queue);
542 /* take a buffer and update segment, updating the time level of the queue. */
544 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
545 gboolean with_duration, gboolean sink)
547 GstClockTime duration, timestamp;
549 timestamp = GST_BUFFER_TIMESTAMP (buffer);
550 duration = GST_BUFFER_DURATION (buffer);
552 /* if no timestamp is set, assume it's continuous with the previous
554 if (timestamp == GST_CLOCK_TIME_NONE)
555 timestamp = segment->position;
558 if (with_duration && duration != GST_CLOCK_TIME_NONE)
559 timestamp += duration;
561 GST_LOG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
562 GST_TIME_ARGS (timestamp));
564 segment->position = timestamp;
566 queue->sink_tainted = TRUE;
568 queue->src_tainted = TRUE;
571 /* calc diff with other end */
572 update_time_level (queue);
576 gst_queue_locked_flush (GstQueue * queue, gboolean full)
580 while (!gst_queue_array_is_empty (queue->queue)) {
581 data = gst_queue_array_pop_head (queue->queue);
582 /* Then lose another reference because we are supposed to destroy that
583 data when flushing */
584 if (!full && GST_IS_EVENT (data) && GST_EVENT_IS_STICKY (data) &&
585 GST_EVENT_TYPE (data) != GST_EVENT_SEGMENT
586 && GST_EVENT_TYPE (data) != GST_EVENT_EOS) {
587 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (data));
589 if (!GST_IS_QUERY (data))
590 gst_mini_object_unref (data);
592 queue->last_query = FALSE;
593 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
594 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
595 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
596 queue->min_threshold.time = queue->orig_min_threshold.time;
597 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
598 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
599 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
601 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
602 queue->sink_tainted = queue->src_tainted = TRUE;
604 /* we deleted a lot of something */
605 GST_QUEUE_SIGNAL_DEL (queue);
608 /* enqueue an item an update the level stats, with QUEUE_LOCK */
610 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
612 GstBuffer *buffer = GST_BUFFER_CAST (item);
614 /* add buffer to the statistics */
615 queue->cur_level.buffers++;
616 queue->cur_level.bytes += gst_buffer_get_size (buffer);
617 apply_buffer (queue, buffer, &queue->sink_segment, TRUE, TRUE);
620 gst_queue_array_push_tail (queue->queue, item);
621 GST_QUEUE_SIGNAL_ADD (queue);
625 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
627 GstEvent *event = GST_EVENT_CAST (item);
629 switch (GST_EVENT_TYPE (event)) {
631 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
632 /* Zero the thresholds, this makes sure the queue is completely
633 * filled and we can read all data from the queue. */
634 if (queue->flush_on_eos)
635 gst_queue_locked_flush (queue, FALSE);
637 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
638 /* mark the queue as EOS. This prevents us from accepting more data. */
641 case GST_EVENT_SEGMENT:
642 apply_segment (queue, event, &queue->sink_segment, TRUE);
643 /* if the queue is empty, apply sink segment on the source */
644 if (gst_queue_array_is_empty (queue->queue)) {
645 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
646 apply_segment (queue, event, &queue->src_segment, FALSE);
647 queue->newseg_applied_to_src = TRUE;
649 /* a new segment allows us to accept more buffers if we got EOS
651 queue->unexpected = FALSE;
658 gst_queue_array_push_tail (queue->queue, item);
659 GST_QUEUE_SIGNAL_ADD (queue);
662 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
663 static GstMiniObject *
664 gst_queue_locked_dequeue (GstQueue * queue)
668 item = gst_queue_array_pop_head (queue->queue);
672 if (GST_IS_BUFFER (item)) {
673 GstBuffer *buffer = GST_BUFFER_CAST (item);
675 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
676 "retrieved buffer %p from queue", buffer);
678 queue->cur_level.buffers--;
679 queue->cur_level.bytes -= gst_buffer_get_size (buffer);
680 apply_buffer (queue, buffer, &queue->src_segment, TRUE, FALSE);
682 /* if the queue is empty now, update the other side */
683 if (queue->cur_level.buffers == 0)
684 queue->cur_level.time = 0;
686 } else if (GST_IS_EVENT (item)) {
687 GstEvent *event = GST_EVENT_CAST (item);
689 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
690 "retrieved event %p from queue", event);
692 switch (GST_EVENT_TYPE (event)) {
694 /* queue is empty now that we dequeued the EOS */
695 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
697 case GST_EVENT_SEGMENT:
698 /* apply newsegment if it has not already been applied */
699 if (G_LIKELY (!queue->newseg_applied_to_src)) {
700 apply_segment (queue, event, &queue->src_segment, FALSE);
702 queue->newseg_applied_to_src = FALSE;
708 } else if (GST_IS_QUERY (item)) {
709 GstQuery *query = GST_QUERY_CAST (item);
711 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
712 "retrieved query %p from queue", query);
715 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
716 item, GST_OBJECT_NAME (queue));
719 GST_QUEUE_SIGNAL_DEL (queue);
726 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
732 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
736 queue = GST_QUEUE (parent);
738 switch (GST_EVENT_TYPE (event)) {
739 case GST_EVENT_FLUSH_START:
741 STATUS (queue, pad, "received flush start event");
743 gst_pad_push_event (queue->srcpad, event);
745 /* now unblock the chain function */
746 GST_QUEUE_MUTEX_LOCK (queue);
747 queue->srcresult = GST_FLOW_FLUSHING;
748 /* unblock the loop and chain functions */
749 GST_QUEUE_SIGNAL_ADD (queue);
750 GST_QUEUE_SIGNAL_DEL (queue);
751 GST_QUEUE_MUTEX_UNLOCK (queue);
753 /* make sure it pauses, this should happen since we sent
754 * flush_start downstream. */
755 gst_pad_pause_task (queue->srcpad);
756 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
759 case GST_EVENT_FLUSH_STOP:
761 STATUS (queue, pad, "received flush stop event");
763 gst_pad_push_event (queue->srcpad, event);
765 GST_QUEUE_MUTEX_LOCK (queue);
766 gst_queue_locked_flush (queue, FALSE);
767 queue->srcresult = GST_FLOW_OK;
769 queue->unexpected = FALSE;
770 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
771 queue->srcpad, NULL);
772 GST_QUEUE_MUTEX_UNLOCK (queue);
774 STATUS (queue, pad, "after flush");
778 if (GST_EVENT_IS_SERIALIZED (event)) {
779 /* serialized events go in the queue */
780 GST_QUEUE_MUTEX_LOCK (queue);
781 if (queue->srcresult != GST_FLOW_OK) {
782 /* Errors in sticky event pushing are no problem and ignored here
783 * as they will cause more meaningful errors during data flow.
784 * For EOS events, that are not followed by data flow, we still
785 * return FALSE here though.
787 if (!GST_EVENT_IS_STICKY (event) ||
788 GST_EVENT_TYPE (event) == GST_EVENT_EOS)
791 /* refuse more events on EOS */
794 gst_queue_locked_enqueue_event (queue, event);
795 GST_QUEUE_MUTEX_UNLOCK (queue);
797 /* non-serialized events are forwarded downstream immediately */
798 gst_pad_push_event (queue->srcpad, event);
808 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
809 GST_QUEUE_MUTEX_UNLOCK (queue);
810 gst_event_unref (event);
815 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
816 "refusing event, we have a downstream flow error: %s",
817 gst_flow_get_name (queue->srcresult));
818 GST_QUEUE_MUTEX_UNLOCK (queue);
819 gst_event_unref (event);
825 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
827 GstQueue *queue = GST_QUEUE_CAST (parent);
830 switch (GST_QUERY_TYPE (query)) {
832 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
833 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
834 GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
835 GST_QUERY_TYPE_NAME (query));
836 gst_queue_array_push_tail (queue->queue, query);
837 GST_QUEUE_SIGNAL_ADD (queue);
838 while (!gst_queue_array_is_empty (queue->queue)) {
839 /* for as long as the queue has items, we know the query is
841 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
843 res = queue->last_query;
844 GST_QUEUE_MUTEX_UNLOCK (queue);
846 res = gst_pad_query_default (pad, parent, query);
857 GST_DEBUG_OBJECT (queue, "we are flushing");
859 /* Remove query from queue if still there, since we hold no ref to it */
860 index = gst_queue_array_find (queue->queue, NULL, query);
863 gst_queue_array_drop_element (queue->queue, index);
865 GST_QUEUE_MUTEX_UNLOCK (queue);
871 gst_queue_is_empty (GstQueue * queue)
875 if (gst_queue_array_is_empty (queue->queue))
878 /* Only consider the queue empty if the minimum thresholds
879 * are not reached and data is at the queue head. Otherwise
880 * we would block forever on serialized queries.
882 head = gst_queue_array_peek_head (queue->queue);
883 if (!GST_IS_BUFFER (head) && !GST_IS_BUFFER_LIST (head))
886 /* It is possible that a max size is reached before all min thresholds are.
887 * Therefore, only consider it empty if it is not filled. */
888 return ((queue->min_threshold.buffers > 0 &&
889 queue->cur_level.buffers < queue->min_threshold.buffers) ||
890 (queue->min_threshold.bytes > 0 &&
891 queue->cur_level.bytes < queue->min_threshold.bytes) ||
892 (queue->min_threshold.time > 0 &&
893 queue->cur_level.time < queue->min_threshold.time)) &&
894 !gst_queue_is_filled (queue);
898 gst_queue_is_filled (GstQueue * queue)
900 return (((queue->max_size.buffers > 0 &&
901 queue->cur_level.buffers >= queue->max_size.buffers) ||
902 (queue->max_size.bytes > 0 &&
903 queue->cur_level.bytes >= queue->max_size.bytes) ||
904 (queue->max_size.time > 0 &&
905 queue->cur_level.time >= queue->max_size.time)));
909 gst_queue_leak_downstream (GstQueue * queue)
911 /* for as long as the queue is filled, dequeue an item and discard it */
912 while (gst_queue_is_filled (queue)) {
915 leak = gst_queue_locked_dequeue (queue);
916 /* there is nothing to dequeue and the queue is still filled.. This should
918 g_assert (leak != NULL);
920 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
921 "queue is full, leaking item %p on downstream end", leak);
922 if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
923 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
924 "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
925 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
928 if (!GST_IS_QUERY (leak))
929 gst_mini_object_unref (leak);
931 /* last buffer needs to get a DISCONT flag */
932 queue->head_needs_discont = TRUE;
937 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
940 GstClockTime duration, timestamp;
942 queue = GST_QUEUE_CAST (parent);
944 /* we have to lock the queue since we span threads */
945 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
946 /* when we received EOS, we refuse any more data */
949 if (queue->unexpected)
952 timestamp = GST_BUFFER_TIMESTAMP (buffer);
953 duration = GST_BUFFER_DURATION (buffer);
955 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
956 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
957 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
958 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
960 /* We make space available if we're "full" according to whatever
961 * the user defined as "full". Note that this only applies to buffers.
962 * We always handle events and they don't count in our statistics. */
963 while (gst_queue_is_filled (queue)) {
964 if (!queue->silent) {
965 GST_QUEUE_MUTEX_UNLOCK (queue);
966 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
967 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
968 /* we recheck, the signal could have changed the thresholds */
969 if (!gst_queue_is_filled (queue))
973 /* how are we going to make space for this buffer? */
974 switch (queue->leaky) {
975 case GST_QUEUE_LEAK_UPSTREAM:
976 /* next buffer needs to get a DISCONT flag */
977 queue->tail_needs_discont = TRUE;
978 /* leak current buffer */
979 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
980 "queue is full, leaking buffer on upstream end");
981 /* now we can clean up and exit right away */
983 case GST_QUEUE_LEAK_DOWNSTREAM:
984 gst_queue_leak_downstream (queue);
987 g_warning ("Unknown leaky type, using default");
989 case GST_QUEUE_NO_LEAK:
991 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
992 "queue is full, waiting for free space");
994 /* don't leak. Instead, wait for space to be available */
996 /* for as long as the queue is filled, wait till an item was deleted. */
997 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
998 } while (gst_queue_is_filled (queue));
1000 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1002 if (!queue->silent) {
1003 GST_QUEUE_MUTEX_UNLOCK (queue);
1004 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1005 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1012 if (queue->tail_needs_discont) {
1013 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1017 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1019 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1021 queue->tail_needs_discont = FALSE;
1024 /* put buffer in queue now */
1025 gst_queue_locked_enqueue_buffer (queue, buffer);
1026 GST_QUEUE_MUTEX_UNLOCK (queue);
1030 /* special conditions */
1033 GST_QUEUE_MUTEX_UNLOCK (queue);
1035 gst_buffer_unref (buffer);
1041 GstFlowReturn ret = queue->srcresult;
1043 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1044 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1045 GST_QUEUE_MUTEX_UNLOCK (queue);
1046 gst_buffer_unref (buffer);
1052 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1053 GST_QUEUE_MUTEX_UNLOCK (queue);
1055 gst_buffer_unref (buffer);
1057 return GST_FLOW_EOS;
1061 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1062 GST_QUEUE_MUTEX_UNLOCK (queue);
1064 gst_buffer_unref (buffer);
1066 return GST_FLOW_EOS;
1070 /* dequeue an item from the queue an push it downstream. This functions returns
1071 * the result of the push. */
1072 static GstFlowReturn
1073 gst_queue_push_one (GstQueue * queue)
1075 GstFlowReturn result = GST_FLOW_OK;
1076 GstMiniObject *data;
1078 data = gst_queue_locked_dequeue (queue);
1083 if (GST_IS_BUFFER (data)) {
1086 buffer = GST_BUFFER_CAST (data);
1088 if (queue->head_needs_discont) {
1089 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1093 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1095 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1097 queue->head_needs_discont = FALSE;
1100 GST_QUEUE_MUTEX_UNLOCK (queue);
1101 result = gst_pad_push (queue->srcpad, buffer);
1103 /* need to check for srcresult here as well */
1104 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1106 if (result == GST_FLOW_EOS) {
1107 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1108 /* stop pushing buffers, we dequeue all items until we see an item that we
1109 * can push again, which is EOS or SEGMENT. If there is nothing in the
1110 * queue we can push, we set a flag to make the sinkpad refuse more
1111 * buffers with an EOS return value. */
1112 while ((data = gst_queue_locked_dequeue (queue))) {
1113 if (GST_IS_BUFFER (data)) {
1114 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1115 "dropping EOS buffer %p", data);
1116 gst_buffer_unref (GST_BUFFER_CAST (data));
1117 } else if (GST_IS_EVENT (data)) {
1118 GstEvent *event = GST_EVENT_CAST (data);
1119 GstEventType type = GST_EVENT_TYPE (event);
1121 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
1122 /* we found a pushable item in the queue, push it out */
1123 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1124 "pushing pushable event %s after EOS",
1125 GST_EVENT_TYPE_NAME (event));
1128 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1129 "dropping EOS event %p", event);
1130 gst_event_unref (event);
1131 } else if (GST_IS_QUERY (data)) {
1132 GstQuery *query = GST_QUERY_CAST (data);
1134 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1135 "dropping query %p because of EOS", query);
1136 queue->last_query = FALSE;
1139 /* no more items in the queue. Set the unexpected flag so that upstream
1140 * make us refuse any more buffers on the sinkpad. Since we will still
1141 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1142 * task function does not shut down. */
1143 queue->unexpected = TRUE;
1144 result = GST_FLOW_OK;
1146 } else if (GST_IS_EVENT (data)) {
1147 GstEvent *event = GST_EVENT_CAST (data);
1148 GstEventType type = GST_EVENT_TYPE (event);
1150 GST_QUEUE_MUTEX_UNLOCK (queue);
1152 gst_pad_push_event (queue->srcpad, event);
1154 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1155 /* if we're EOS, return EOS so that the task pauses. */
1156 if (type == GST_EVENT_EOS) {
1157 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1158 "pushed EOS event %p, return EOS", event);
1159 result = GST_FLOW_EOS;
1161 } else if (GST_IS_QUERY (data)) {
1162 GstQuery *query = GST_QUERY_CAST (data);
1164 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
1165 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1166 "did query %p, return %d", query, queue->last_query);
1173 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1174 "exit because we have no item in the queue");
1175 return GST_FLOW_ERROR;
1179 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1180 return GST_FLOW_FLUSHING;
1185 gst_queue_loop (GstPad * pad)
1190 queue = (GstQueue *) GST_PAD_PARENT (pad);
1192 /* have to lock for thread-safety */
1193 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1195 while (gst_queue_is_empty (queue)) {
1196 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1197 if (!queue->silent) {
1198 GST_QUEUE_MUTEX_UNLOCK (queue);
1199 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1200 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1203 /* we recheck, the signal could have changed the thresholds */
1204 while (gst_queue_is_empty (queue)) {
1205 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1208 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1209 if (!queue->silent) {
1210 GST_QUEUE_MUTEX_UNLOCK (queue);
1211 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1212 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1213 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1217 ret = gst_queue_push_one (queue);
1218 queue->srcresult = ret;
1219 if (ret != GST_FLOW_OK)
1222 GST_QUEUE_MUTEX_UNLOCK (queue);
1229 gboolean eos = queue->eos;
1230 GstFlowReturn ret = queue->srcresult;
1232 gst_pad_pause_task (queue->srcpad);
1233 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1234 "pause task, reason: %s", gst_flow_get_name (ret));
1235 if (ret == GST_FLOW_FLUSHING)
1236 gst_queue_locked_flush (queue, FALSE);
1238 GST_QUEUE_SIGNAL_DEL (queue);
1239 GST_QUEUE_MUTEX_UNLOCK (queue);
1240 /* let app know about us giving up if upstream is not expected to do so */
1241 /* EOS is already taken care of elsewhere */
1242 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1243 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
1244 (_("Internal data flow error.")),
1245 ("streaming task paused, reason %s (%d)",
1246 gst_flow_get_name (ret), ret));
1247 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1254 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1256 gboolean res = TRUE;
1257 GstQueue *queue = GST_QUEUE (parent);
1259 #ifndef GST_DISABLE_GST_DEBUG
1260 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1261 event, GST_EVENT_TYPE (event));
1264 switch (GST_EVENT_TYPE (event)) {
1265 case GST_EVENT_RECONFIGURE:
1266 GST_QUEUE_MUTEX_LOCK (queue);
1267 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1268 /* when we got not linked, assume downstream is linked again now and we
1269 * can try to start pushing again */
1270 queue->srcresult = GST_FLOW_OK;
1271 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1273 GST_QUEUE_MUTEX_UNLOCK (queue);
1275 res = gst_pad_push_event (queue->sinkpad, event);
1278 res = gst_pad_event_default (pad, parent, event);
1287 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1289 GstQueue *queue = GST_QUEUE (parent);
1292 switch (GST_QUERY_TYPE (query)) {
1293 case GST_QUERY_SCHEDULING:{
1294 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1299 res = gst_pad_query_default (pad, parent, query);
1306 /* Adjust peer response for data contained in queue */
1307 switch (GST_QUERY_TYPE (query)) {
1308 case GST_QUERY_POSITION:
1313 /* get peer position */
1314 gst_query_parse_position (query, &format, &peer_pos);
1316 /* FIXME: this code assumes that there's no discont in the queue */
1318 case GST_FORMAT_BYTES:
1319 peer_pos -= queue->cur_level.bytes;
1321 case GST_FORMAT_TIME:
1322 peer_pos -= queue->cur_level.time;
1325 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1326 "know how to adjust value", gst_format_get_name (format));
1329 /* set updated position */
1330 gst_query_set_position (query, format, peer_pos);
1333 case GST_QUERY_LATENCY:
1336 GstClockTime min, max;
1338 gst_query_parse_latency (query, &live, &min, &max);
1340 /* we can delay up to the limit of the queue in time. If we have no time
1341 * limit, the best thing we can do is to return an infinite delay. In
1342 * reality a better estimate would be the byte/buffer rate but that is not
1343 * possible right now. */
1344 if (queue->max_size.time > 0 && max != -1)
1345 max += queue->max_size.time;
1349 /* adjust for min-threshold */
1350 if (queue->min_threshold.time > 0 && min != -1)
1351 min += queue->min_threshold.time;
1353 gst_query_set_latency (query, live, min, max);
1357 /* peer handled other queries */
1365 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1371 queue = GST_QUEUE (parent);
1374 case GST_PAD_MODE_PUSH:
1376 GST_QUEUE_MUTEX_LOCK (queue);
1377 queue->srcresult = GST_FLOW_OK;
1379 queue->unexpected = FALSE;
1380 GST_QUEUE_MUTEX_UNLOCK (queue);
1382 /* step 1, unblock chain function */
1383 GST_QUEUE_MUTEX_LOCK (queue);
1384 queue->srcresult = GST_FLOW_FLUSHING;
1385 gst_queue_locked_flush (queue, TRUE);
1386 GST_QUEUE_MUTEX_UNLOCK (queue);
1398 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1404 queue = GST_QUEUE (parent);
1407 case GST_PAD_MODE_PUSH:
1409 GST_QUEUE_MUTEX_LOCK (queue);
1410 queue->srcresult = GST_FLOW_OK;
1412 queue->unexpected = FALSE;
1414 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1416 GST_QUEUE_MUTEX_UNLOCK (queue);
1418 /* step 1, unblock loop function */
1419 GST_QUEUE_MUTEX_LOCK (queue);
1420 queue->srcresult = GST_FLOW_FLUSHING;
1421 /* the item add signal will unblock */
1422 g_cond_signal (&queue->item_add);
1423 GST_QUEUE_MUTEX_UNLOCK (queue);
1425 /* step 2, make sure streaming finishes */
1426 result = gst_pad_stop_task (pad);
1437 queue_capacity_change (GstQueue * queue)
1439 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1440 gst_queue_leak_downstream (queue);
1443 /* changing the capacity of the queue must wake up
1444 * the _chain function, it might have more room now
1445 * to store the buffer/event in the queue */
1446 GST_QUEUE_SIGNAL_DEL (queue);
1449 /* Changing the minimum required fill level must
1450 * wake up the _loop function as it might now
1451 * be able to preceed.
1453 #define QUEUE_THRESHOLD_CHANGE(q)\
1454 GST_QUEUE_SIGNAL_ADD (q);
1457 gst_queue_set_property (GObject * object,
1458 guint prop_id, const GValue * value, GParamSpec * pspec)
1460 GstQueue *queue = GST_QUEUE (object);
1462 /* someone could change levels here, and since this
1463 * affects the get/put funcs, we need to lock for safety. */
1464 GST_QUEUE_MUTEX_LOCK (queue);
1467 case PROP_MAX_SIZE_BYTES:
1468 queue->max_size.bytes = g_value_get_uint (value);
1469 queue_capacity_change (queue);
1471 case PROP_MAX_SIZE_BUFFERS:
1472 queue->max_size.buffers = g_value_get_uint (value);
1473 queue_capacity_change (queue);
1475 case PROP_MAX_SIZE_TIME:
1476 queue->max_size.time = g_value_get_uint64 (value);
1477 queue_capacity_change (queue);
1479 case PROP_MIN_THRESHOLD_BYTES:
1480 queue->min_threshold.bytes = g_value_get_uint (value);
1481 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1482 QUEUE_THRESHOLD_CHANGE (queue);
1484 case PROP_MIN_THRESHOLD_BUFFERS:
1485 queue->min_threshold.buffers = g_value_get_uint (value);
1486 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1487 QUEUE_THRESHOLD_CHANGE (queue);
1489 case PROP_MIN_THRESHOLD_TIME:
1490 queue->min_threshold.time = g_value_get_uint64 (value);
1491 queue->orig_min_threshold.time = queue->min_threshold.time;
1492 QUEUE_THRESHOLD_CHANGE (queue);
1495 queue->leaky = g_value_get_enum (value);
1498 queue->silent = g_value_get_boolean (value);
1500 case PROP_FLUSH_ON_EOS:
1501 queue->flush_on_eos = g_value_get_boolean (value);
1504 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1508 GST_QUEUE_MUTEX_UNLOCK (queue);
1512 gst_queue_get_property (GObject * object,
1513 guint prop_id, GValue * value, GParamSpec * pspec)
1515 GstQueue *queue = GST_QUEUE (object);
1517 GST_QUEUE_MUTEX_LOCK (queue);
1520 case PROP_CUR_LEVEL_BYTES:
1521 g_value_set_uint (value, queue->cur_level.bytes);
1523 case PROP_CUR_LEVEL_BUFFERS:
1524 g_value_set_uint (value, queue->cur_level.buffers);
1526 case PROP_CUR_LEVEL_TIME:
1527 g_value_set_uint64 (value, queue->cur_level.time);
1529 case PROP_MAX_SIZE_BYTES:
1530 g_value_set_uint (value, queue->max_size.bytes);
1532 case PROP_MAX_SIZE_BUFFERS:
1533 g_value_set_uint (value, queue->max_size.buffers);
1535 case PROP_MAX_SIZE_TIME:
1536 g_value_set_uint64 (value, queue->max_size.time);
1538 case PROP_MIN_THRESHOLD_BYTES:
1539 g_value_set_uint (value, queue->min_threshold.bytes);
1541 case PROP_MIN_THRESHOLD_BUFFERS:
1542 g_value_set_uint (value, queue->min_threshold.buffers);
1544 case PROP_MIN_THRESHOLD_TIME:
1545 g_value_set_uint64 (value, queue->min_threshold.time);
1548 g_value_set_enum (value, queue->leaky);
1551 g_value_set_boolean (value, queue->silent);
1553 case PROP_FLUSH_ON_EOS:
1554 g_value_set_boolean (value, queue->flush_on_eos);
1557 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1561 GST_QUEUE_MUTEX_UNLOCK (queue);