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_push_one (GstQueue * queue);
199 static void gst_queue_loop (GstPad * pad);
201 static gboolean gst_queue_handle_sink_event (GstPad * pad, GstObject * parent,
203 static gboolean gst_queue_handle_sink_query (GstPad * pad, GstObject * parent,
206 static gboolean gst_queue_handle_src_event (GstPad * pad, GstObject * parent,
208 static gboolean gst_queue_handle_src_query (GstPad * pad, GstObject * parent,
211 static void gst_queue_locked_flush (GstQueue * queue, gboolean full);
213 static gboolean gst_queue_src_activate_mode (GstPad * pad, GstObject * parent,
214 GstPadMode mode, gboolean active);
215 static gboolean gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
216 GstPadMode mode, gboolean active);
218 static gboolean gst_queue_is_empty (GstQueue * queue);
219 static gboolean gst_queue_is_filled (GstQueue * queue);
229 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
232 queue_leaky_get_type (void)
234 static GType queue_leaky_type = 0;
235 static const GEnumValue queue_leaky[] = {
236 {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
237 {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
238 {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
243 if (!queue_leaky_type) {
244 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
246 return queue_leaky_type;
249 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
252 gst_queue_class_init (GstQueueClass * klass)
254 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
255 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
257 gobject_class->set_property = gst_queue_set_property;
258 gobject_class->get_property = gst_queue_get_property;
262 * GstQueue::underrun:
263 * @queue: the queue instance
265 * Reports that the buffer became empty (underrun).
266 * A buffer is empty if the total amount of data inside it (num-buffers, time,
267 * size) is lower than the boundary values which can be set through the
268 * GObject properties.
270 gst_queue_signals[SIGNAL_UNDERRUN] =
271 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
272 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
273 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
276 * @queue: the queue instance
278 * Reports that enough (min-threshold) data is in the queue. Use this signal
279 * together with the underrun signal to pause the pipeline on underrun and
280 * wait for the queue to fill-up before resume playback.
282 gst_queue_signals[SIGNAL_RUNNING] =
283 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
284 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
285 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
288 * @queue: the queue instance
290 * Reports that the buffer became full (overrun).
291 * A buffer is full if the total amount of data inside it (num-buffers, time,
292 * size) is higher than the boundary values which can be set through the
293 * GObject properties.
295 gst_queue_signals[SIGNAL_OVERRUN] =
296 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
297 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
298 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
301 * @queue: the queue instance
303 * Reports when the queue has enough data to start pushing data again on the
306 gst_queue_signals[SIGNAL_PUSHING] =
307 g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
308 G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
309 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
312 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
313 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
314 "Current amount of data in the queue (bytes)",
315 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
317 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
318 "Current number of buffers in the queue",
319 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
321 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
322 "Current amount of data in the queue (in ns)",
323 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
325 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
326 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
327 "Max. amount of data in the queue (bytes, 0=disable)",
328 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
329 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
331 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
332 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
333 DEFAULT_MAX_SIZE_BUFFERS,
334 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
336 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
337 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
338 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
341 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
342 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
343 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
345 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
346 "Min. number of buffers in the queue to allow reading (0=disable)",
347 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
349 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
350 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
351 0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_LEAKY,
354 g_param_spec_enum ("leaky", "Leaky",
355 "Where the queue leaks, if at all",
356 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362 * Don't emit queue signals. Makes queues more lightweight if no signals are
365 g_object_class_install_property (gobject_class, PROP_SILENT,
366 g_param_spec_boolean ("silent", "Silent",
367 "Don't emit queue signals", FALSE,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 * GstQueue:flush-on-eos
373 * Discard all data in the queue when an EOS event is received, and pass
374 * on the EOS event as soon as possible (instead of waiting until all
375 * buffers in the queue have been processed, which is the default behaviour).
377 * Flushing the queue on EOS might be useful when capturing and encoding
378 * from a live source, to finish up the recording quickly in cases when
379 * the encoder is slow. Note that this might mean some data from the end of
380 * the recording data might be lost though (never more than the configured
381 * max. sizes though).
385 g_object_class_install_property (gobject_class, PROP_FLUSH_ON_EOS,
386 g_param_spec_boolean ("flush-on-eos", "Flush on EOS",
387 "Discard all data in the queue when an EOS event is received", FALSE,
388 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 gobject_class->finalize = gst_queue_finalize;
392 gst_element_class_set_static_metadata (gstelement_class,
394 "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
395 gst_element_class_add_pad_template (gstelement_class,
396 gst_static_pad_template_get (&srctemplate));
397 gst_element_class_add_pad_template (gstelement_class,
398 gst_static_pad_template_get (&sinktemplate));
400 /* Registering debug symbols for function pointers */
401 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
402 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
403 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
404 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
405 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
406 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
410 gst_queue_init (GstQueue * queue)
412 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
414 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
415 gst_pad_set_activatemode_function (queue->sinkpad,
416 gst_queue_sink_activate_mode);
417 gst_pad_set_event_function (queue->sinkpad, gst_queue_handle_sink_event);
418 gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
419 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
420 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
422 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
424 gst_pad_set_activatemode_function (queue->srcpad,
425 gst_queue_src_activate_mode);
426 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
427 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
428 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
429 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
431 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
432 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
433 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
434 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
435 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
436 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
437 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
438 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
439 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
441 queue->leaky = GST_QUEUE_NO_LEAK;
442 queue->srcresult = GST_FLOW_FLUSHING;
444 g_mutex_init (&queue->qlock);
445 g_cond_init (&queue->item_add);
446 g_cond_init (&queue->item_del);
447 g_cond_init (&queue->query_handled);
449 queue->queue = gst_queue_array_new (DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
451 queue->sinktime = GST_CLOCK_TIME_NONE;
452 queue->srctime = GST_CLOCK_TIME_NONE;
454 queue->sink_tainted = TRUE;
455 queue->src_tainted = TRUE;
457 queue->newseg_applied_to_src = FALSE;
459 GST_DEBUG_OBJECT (queue,
460 "initialized queue's not_empty & not_full conditions");
463 /* called only once, as opposed to dispose */
465 gst_queue_finalize (GObject * object)
467 GstQueue *queue = GST_QUEUE (object);
469 GST_DEBUG_OBJECT (queue, "finalizing queue");
471 while (!gst_queue_array_is_empty (queue->queue)) {
472 GstQueueItem *qitem = gst_queue_array_pop_head (queue->queue);
473 /* FIXME: if it's a query, shouldn't we unref that too? */
474 if (!qitem->is_query)
475 gst_mini_object_unref (qitem->item);
476 g_slice_free (GstQueueItem, qitem);
478 gst_queue_array_free (queue->queue);
480 g_mutex_clear (&queue->qlock);
481 g_cond_clear (&queue->item_add);
482 g_cond_clear (&queue->item_del);
483 g_cond_clear (&queue->query_handled);
485 G_OBJECT_CLASS (parent_class)->finalize (object);
488 /* calculate the diff between running time on the sink and src of the queue.
489 * This is the total amount of time in the queue. */
491 update_time_level (GstQueue * queue)
493 gint64 sink_time, src_time;
495 if (queue->sink_tainted) {
496 GST_LOG_OBJECT (queue, "update sink time");
498 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
499 queue->sink_segment.position);
500 queue->sink_tainted = FALSE;
502 sink_time = queue->sinktime;
504 if (queue->src_tainted) {
505 GST_LOG_OBJECT (queue, "update src time");
507 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
508 queue->src_segment.position);
509 queue->src_tainted = FALSE;
511 src_time = queue->srctime;
513 GST_LOG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
514 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
516 if (sink_time >= src_time)
517 queue->cur_level.time = sink_time - src_time;
519 queue->cur_level.time = 0;
522 /* take a SEGMENT event and apply the values to segment, updating the time
525 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
528 gst_event_copy_segment (event, segment);
530 /* now configure the values, we use these to track timestamps on the
532 if (segment->format != GST_FORMAT_TIME) {
533 /* non-time format, pretent the current time segment is closed with a
534 * 0 start and unknown stop time. */
535 segment->format = GST_FORMAT_TIME;
541 queue->sink_tainted = TRUE;
543 queue->src_tainted = TRUE;
545 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
547 /* segment can update the time level of the queue */
548 update_time_level (queue);
551 /* take a buffer and update segment, updating the time level of the queue. */
553 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
554 gboolean with_duration, gboolean sink)
556 GstClockTime duration, timestamp;
558 timestamp = GST_BUFFER_TIMESTAMP (buffer);
559 duration = GST_BUFFER_DURATION (buffer);
561 /* if no timestamp is set, assume it's continuous with the previous
563 if (timestamp == GST_CLOCK_TIME_NONE)
564 timestamp = segment->position;
567 if (with_duration && duration != GST_CLOCK_TIME_NONE)
568 timestamp += duration;
570 GST_LOG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
571 GST_TIME_ARGS (timestamp));
573 segment->position = timestamp;
575 queue->sink_tainted = TRUE;
577 queue->src_tainted = TRUE;
580 /* calc diff with other end */
581 update_time_level (queue);
585 gst_queue_locked_flush (GstQueue * queue, gboolean full)
587 while (!gst_queue_array_is_empty (queue->queue)) {
588 GstQueueItem *qitem = gst_queue_array_pop_head (queue->queue);
590 /* Then lose another reference because we are supposed to destroy that
591 data when flushing */
592 if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
593 && GST_EVENT_IS_STICKY (qitem->item)
594 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
595 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
596 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
598 if (!qitem->is_query)
599 gst_mini_object_unref (qitem->item);
600 g_slice_free (GstQueueItem, qitem);
602 queue->last_query = FALSE;
603 g_cond_signal (&queue->query_handled);
604 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
605 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
606 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
607 queue->min_threshold.time = queue->orig_min_threshold.time;
608 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
609 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
610 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
612 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
613 queue->sink_tainted = queue->src_tainted = TRUE;
615 /* we deleted a lot of something */
616 GST_QUEUE_SIGNAL_DEL (queue);
619 /* enqueue an item an update the level stats, with QUEUE_LOCK */
621 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
623 GstBuffer *buffer = GST_BUFFER_CAST (item);
624 gsize bsize = gst_buffer_get_size (buffer);
626 /* add buffer to the statistics */
627 queue->cur_level.buffers++;
628 queue->cur_level.bytes += bsize;
629 apply_buffer (queue, buffer, &queue->sink_segment, TRUE, TRUE);
632 GstQueueItem *qitem = g_slice_new (GstQueueItem);
634 qitem->is_query = FALSE;
636 gst_queue_array_push_tail (queue->queue, qitem);
638 GST_QUEUE_SIGNAL_ADD (queue);
642 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
644 GstEvent *event = GST_EVENT_CAST (item);
646 switch (GST_EVENT_TYPE (event)) {
648 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
649 /* Zero the thresholds, this makes sure the queue is completely
650 * filled and we can read all data from the queue. */
651 if (queue->flush_on_eos)
652 gst_queue_locked_flush (queue, FALSE);
654 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
655 /* mark the queue as EOS. This prevents us from accepting more data. */
658 case GST_EVENT_SEGMENT:
659 apply_segment (queue, event, &queue->sink_segment, TRUE);
660 /* if the queue is empty, apply sink segment on the source */
661 if (gst_queue_array_is_empty (queue->queue)) {
662 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
663 apply_segment (queue, event, &queue->src_segment, FALSE);
664 queue->newseg_applied_to_src = TRUE;
666 /* a new segment allows us to accept more buffers if we got EOS
668 queue->unexpected = FALSE;
675 GstQueueItem *qitem = g_slice_new (GstQueueItem);
677 qitem->is_query = FALSE;
678 gst_queue_array_push_tail (queue->queue, qitem);
680 GST_QUEUE_SIGNAL_ADD (queue);
683 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
684 static GstMiniObject *
685 gst_queue_locked_dequeue (GstQueue * queue)
691 qitem = gst_queue_array_pop_head (queue->queue);
696 bufsize = qitem->size;
697 g_slice_free (GstQueueItem, qitem);
699 if (GST_IS_BUFFER (item)) {
700 GstBuffer *buffer = GST_BUFFER_CAST (item);
702 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
703 "retrieved buffer %p from queue", buffer);
705 queue->cur_level.buffers--;
706 queue->cur_level.bytes -= bufsize;
707 apply_buffer (queue, buffer, &queue->src_segment, TRUE, FALSE);
709 /* if the queue is empty now, update the other side */
710 if (queue->cur_level.buffers == 0)
711 queue->cur_level.time = 0;
713 } else if (GST_IS_EVENT (item)) {
714 GstEvent *event = GST_EVENT_CAST (item);
716 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
717 "retrieved event %p from queue", event);
719 switch (GST_EVENT_TYPE (event)) {
721 /* queue is empty now that we dequeued the EOS */
722 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
724 case GST_EVENT_SEGMENT:
725 /* apply newsegment if it has not already been applied */
726 if (G_LIKELY (!queue->newseg_applied_to_src)) {
727 apply_segment (queue, event, &queue->src_segment, FALSE);
729 queue->newseg_applied_to_src = FALSE;
735 } else if (GST_IS_QUERY (item)) {
736 GstQuery *query = GST_QUERY_CAST (item);
738 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
739 "retrieved query %p from queue", query);
742 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
743 item, GST_OBJECT_NAME (queue));
746 GST_QUEUE_SIGNAL_DEL (queue);
753 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
759 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
763 queue = GST_QUEUE (parent);
765 switch (GST_EVENT_TYPE (event)) {
766 case GST_EVENT_FLUSH_START:
768 STATUS (queue, pad, "received flush start event");
770 gst_pad_push_event (queue->srcpad, event);
772 /* now unblock the chain function */
773 GST_QUEUE_MUTEX_LOCK (queue);
774 queue->srcresult = GST_FLOW_FLUSHING;
775 /* unblock the loop and chain functions */
776 GST_QUEUE_SIGNAL_ADD (queue);
777 GST_QUEUE_SIGNAL_DEL (queue);
778 queue->last_query = FALSE;
779 g_cond_signal (&queue->query_handled);
780 GST_QUEUE_MUTEX_UNLOCK (queue);
782 /* make sure it pauses, this should happen since we sent
783 * flush_start downstream. */
784 gst_pad_pause_task (queue->srcpad);
785 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
788 case GST_EVENT_FLUSH_STOP:
790 STATUS (queue, pad, "received flush stop event");
792 gst_pad_push_event (queue->srcpad, event);
794 GST_QUEUE_MUTEX_LOCK (queue);
795 gst_queue_locked_flush (queue, FALSE);
796 queue->srcresult = GST_FLOW_OK;
798 queue->unexpected = FALSE;
799 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
800 queue->srcpad, NULL);
801 GST_QUEUE_MUTEX_UNLOCK (queue);
803 STATUS (queue, pad, "after flush");
807 if (GST_EVENT_IS_SERIALIZED (event)) {
808 /* serialized events go in the queue */
809 GST_QUEUE_MUTEX_LOCK (queue);
810 if (queue->srcresult != GST_FLOW_OK) {
811 /* Errors in sticky event pushing are no problem and ignored here
812 * as they will cause more meaningful errors during data flow.
813 * For EOS events, that are not followed by data flow, we still
814 * return FALSE here though.
816 if (!GST_EVENT_IS_STICKY (event) ||
817 GST_EVENT_TYPE (event) == GST_EVENT_EOS)
820 /* refuse more events on EOS */
823 gst_queue_locked_enqueue_event (queue, event);
824 GST_QUEUE_MUTEX_UNLOCK (queue);
826 /* non-serialized events are forwarded downstream immediately */
827 gst_pad_push_event (queue->srcpad, event);
837 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
838 GST_QUEUE_MUTEX_UNLOCK (queue);
839 gst_event_unref (event);
844 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
845 "refusing event, we have a downstream flow error: %s",
846 gst_flow_get_name (queue->srcresult));
847 GST_QUEUE_MUTEX_UNLOCK (queue);
848 gst_event_unref (event);
854 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
856 GstQueue *queue = GST_QUEUE_CAST (parent);
859 switch (GST_QUERY_TYPE (query)) {
861 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
864 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
865 GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
866 GST_QUERY_TYPE_NAME (query));
867 qitem = g_slice_new (GstQueueItem);
868 qitem->item = GST_MINI_OBJECT_CAST (query);
869 qitem->is_query = TRUE;
870 gst_queue_array_push_tail (queue->queue, qitem);
871 GST_QUEUE_SIGNAL_ADD (queue);
872 g_cond_wait (&queue->query_handled, &queue->qlock);
873 if (queue->srcresult != GST_FLOW_OK)
875 res = queue->last_query;
876 GST_QUEUE_MUTEX_UNLOCK (queue);
878 res = gst_pad_query_default (pad, parent, query);
887 GST_DEBUG_OBJECT (queue, "we are flushing");
888 GST_QUEUE_MUTEX_UNLOCK (queue);
894 gst_queue_is_empty (GstQueue * queue)
898 if (gst_queue_array_is_empty (queue->queue))
901 /* Only consider the queue empty if the minimum thresholds
902 * are not reached and data is at the queue head. Otherwise
903 * we would block forever on serialized queries.
905 head = gst_queue_array_peek_head (queue->queue);
906 if (!GST_IS_BUFFER (head->item) && !GST_IS_BUFFER_LIST (head->item))
909 /* It is possible that a max size is reached before all min thresholds are.
910 * Therefore, only consider it empty if it is not filled. */
911 return ((queue->min_threshold.buffers > 0 &&
912 queue->cur_level.buffers < queue->min_threshold.buffers) ||
913 (queue->min_threshold.bytes > 0 &&
914 queue->cur_level.bytes < queue->min_threshold.bytes) ||
915 (queue->min_threshold.time > 0 &&
916 queue->cur_level.time < queue->min_threshold.time)) &&
917 !gst_queue_is_filled (queue);
921 gst_queue_is_filled (GstQueue * queue)
923 return (((queue->max_size.buffers > 0 &&
924 queue->cur_level.buffers >= queue->max_size.buffers) ||
925 (queue->max_size.bytes > 0 &&
926 queue->cur_level.bytes >= queue->max_size.bytes) ||
927 (queue->max_size.time > 0 &&
928 queue->cur_level.time >= queue->max_size.time)));
932 gst_queue_leak_downstream (GstQueue * queue)
934 /* for as long as the queue is filled, dequeue an item and discard it */
935 while (gst_queue_is_filled (queue)) {
938 leak = gst_queue_locked_dequeue (queue);
939 /* there is nothing to dequeue and the queue is still filled.. This should
941 g_assert (leak != NULL);
943 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
944 "queue is full, leaking item %p on downstream end", leak);
945 if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
946 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
947 "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
948 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
951 if (!GST_IS_QUERY (leak))
952 gst_mini_object_unref (leak);
954 /* last buffer needs to get a DISCONT flag */
955 queue->head_needs_discont = TRUE;
960 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
963 GstClockTime duration, timestamp;
965 queue = GST_QUEUE_CAST (parent);
967 /* we have to lock the queue since we span threads */
968 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
969 /* when we received EOS, we refuse any more data */
972 if (queue->unexpected)
975 timestamp = GST_BUFFER_TIMESTAMP (buffer);
976 duration = GST_BUFFER_DURATION (buffer);
978 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
979 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
980 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
981 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
983 /* We make space available if we're "full" according to whatever
984 * the user defined as "full". Note that this only applies to buffers.
985 * We always handle events and they don't count in our statistics. */
986 while (gst_queue_is_filled (queue)) {
987 if (!queue->silent) {
988 GST_QUEUE_MUTEX_UNLOCK (queue);
989 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
990 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
991 /* we recheck, the signal could have changed the thresholds */
992 if (!gst_queue_is_filled (queue))
996 /* how are we going to make space for this buffer? */
997 switch (queue->leaky) {
998 case GST_QUEUE_LEAK_UPSTREAM:
999 /* next buffer needs to get a DISCONT flag */
1000 queue->tail_needs_discont = TRUE;
1001 /* leak current buffer */
1002 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1003 "queue is full, leaking buffer on upstream end");
1004 /* now we can clean up and exit right away */
1006 case GST_QUEUE_LEAK_DOWNSTREAM:
1007 gst_queue_leak_downstream (queue);
1010 g_warning ("Unknown leaky type, using default");
1012 case GST_QUEUE_NO_LEAK:
1014 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1015 "queue is full, waiting for free space");
1017 /* don't leak. Instead, wait for space to be available */
1019 /* for as long as the queue is filled, wait till an item was deleted. */
1020 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1021 } while (gst_queue_is_filled (queue));
1023 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1025 if (!queue->silent) {
1026 GST_QUEUE_MUTEX_UNLOCK (queue);
1027 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1028 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1035 if (queue->tail_needs_discont) {
1036 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1040 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1042 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1044 queue->tail_needs_discont = FALSE;
1047 /* put buffer in queue now */
1048 gst_queue_locked_enqueue_buffer (queue, buffer);
1049 GST_QUEUE_MUTEX_UNLOCK (queue);
1053 /* special conditions */
1056 GST_QUEUE_MUTEX_UNLOCK (queue);
1058 gst_buffer_unref (buffer);
1064 GstFlowReturn ret = queue->srcresult;
1066 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1067 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1068 GST_QUEUE_MUTEX_UNLOCK (queue);
1069 gst_buffer_unref (buffer);
1075 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1076 GST_QUEUE_MUTEX_UNLOCK (queue);
1078 gst_buffer_unref (buffer);
1080 return GST_FLOW_EOS;
1084 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1085 GST_QUEUE_MUTEX_UNLOCK (queue);
1087 gst_buffer_unref (buffer);
1089 return GST_FLOW_EOS;
1093 /* dequeue an item from the queue an push it downstream. This functions returns
1094 * the result of the push. */
1095 static GstFlowReturn
1096 gst_queue_push_one (GstQueue * queue)
1098 GstFlowReturn result = GST_FLOW_OK;
1099 GstMiniObject *data;
1101 data = gst_queue_locked_dequeue (queue);
1106 if (GST_IS_BUFFER (data)) {
1109 buffer = GST_BUFFER_CAST (data);
1111 if (queue->head_needs_discont) {
1112 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1116 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1118 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1120 queue->head_needs_discont = FALSE;
1123 GST_QUEUE_MUTEX_UNLOCK (queue);
1124 result = gst_pad_push (queue->srcpad, buffer);
1126 /* need to check for srcresult here as well */
1127 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1129 if (result == GST_FLOW_EOS) {
1130 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1131 /* stop pushing buffers, we dequeue all items until we see an item that we
1132 * can push again, which is EOS or SEGMENT. If there is nothing in the
1133 * queue we can push, we set a flag to make the sinkpad refuse more
1134 * buffers with an EOS return value. */
1135 while ((data = gst_queue_locked_dequeue (queue))) {
1136 if (GST_IS_BUFFER (data)) {
1137 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1138 "dropping EOS buffer %p", data);
1139 gst_buffer_unref (GST_BUFFER_CAST (data));
1140 } else if (GST_IS_EVENT (data)) {
1141 GstEvent *event = GST_EVENT_CAST (data);
1142 GstEventType type = GST_EVENT_TYPE (event);
1144 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
1145 /* we found a pushable item in the queue, push it out */
1146 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1147 "pushing pushable event %s after EOS",
1148 GST_EVENT_TYPE_NAME (event));
1151 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1152 "dropping EOS event %p", event);
1153 gst_event_unref (event);
1154 } else if (GST_IS_QUERY (data)) {
1155 GstQuery *query = GST_QUERY_CAST (data);
1157 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1158 "dropping query %p because of EOS", query);
1159 queue->last_query = FALSE;
1160 g_cond_signal (&queue->query_handled);
1163 /* no more items in the queue. Set the unexpected flag so that upstream
1164 * make us refuse any more buffers on the sinkpad. Since we will still
1165 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1166 * task function does not shut down. */
1167 queue->unexpected = TRUE;
1168 result = GST_FLOW_OK;
1170 } else if (GST_IS_EVENT (data)) {
1171 GstEvent *event = GST_EVENT_CAST (data);
1172 GstEventType type = GST_EVENT_TYPE (event);
1174 GST_QUEUE_MUTEX_UNLOCK (queue);
1176 gst_pad_push_event (queue->srcpad, event);
1178 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1179 /* if we're EOS, return EOS so that the task pauses. */
1180 if (type == GST_EVENT_EOS) {
1181 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1182 "pushed EOS event %p, return EOS", event);
1183 result = GST_FLOW_EOS;
1185 } else if (GST_IS_QUERY (data)) {
1186 GstQuery *query = GST_QUERY_CAST (data);
1189 GST_QUEUE_MUTEX_UNLOCK (queue);
1190 ret = gst_pad_peer_query (queue->srcpad, query);
1191 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1192 queue->last_query = ret;
1193 g_cond_signal (&queue->query_handled);
1194 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1195 "did query %p, return %d", query, queue->last_query);
1202 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1203 "exit because we have no item in the queue");
1204 return GST_FLOW_ERROR;
1208 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1209 return GST_FLOW_FLUSHING;
1213 queue->last_query = FALSE;
1214 g_cond_signal (&queue->query_handled);
1215 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1216 return GST_FLOW_FLUSHING;
1221 gst_queue_loop (GstPad * pad)
1226 queue = (GstQueue *) GST_PAD_PARENT (pad);
1228 /* have to lock for thread-safety */
1229 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1231 while (gst_queue_is_empty (queue)) {
1232 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1233 if (!queue->silent) {
1234 GST_QUEUE_MUTEX_UNLOCK (queue);
1235 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1236 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1239 /* we recheck, the signal could have changed the thresholds */
1240 while (gst_queue_is_empty (queue)) {
1241 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1244 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1245 if (!queue->silent) {
1246 GST_QUEUE_MUTEX_UNLOCK (queue);
1247 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1248 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1249 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1253 ret = gst_queue_push_one (queue);
1254 queue->srcresult = ret;
1255 if (ret != GST_FLOW_OK)
1258 GST_QUEUE_MUTEX_UNLOCK (queue);
1265 gboolean eos = queue->eos;
1266 GstFlowReturn ret = queue->srcresult;
1268 gst_pad_pause_task (queue->srcpad);
1269 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1270 "pause task, reason: %s", gst_flow_get_name (ret));
1271 if (ret == GST_FLOW_FLUSHING)
1272 gst_queue_locked_flush (queue, FALSE);
1274 GST_QUEUE_SIGNAL_DEL (queue);
1275 GST_QUEUE_MUTEX_UNLOCK (queue);
1276 /* let app know about us giving up if upstream is not expected to do so */
1277 /* EOS is already taken care of elsewhere */
1278 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1279 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
1280 (_("Internal data flow error.")),
1281 ("streaming task paused, reason %s (%d)",
1282 gst_flow_get_name (ret), ret));
1283 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1290 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1292 gboolean res = TRUE;
1293 GstQueue *queue = GST_QUEUE (parent);
1295 #ifndef GST_DISABLE_GST_DEBUG
1296 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1297 event, GST_EVENT_TYPE (event));
1300 switch (GST_EVENT_TYPE (event)) {
1301 case GST_EVENT_RECONFIGURE:
1302 GST_QUEUE_MUTEX_LOCK (queue);
1303 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1304 /* when we got not linked, assume downstream is linked again now and we
1305 * can try to start pushing again */
1306 queue->srcresult = GST_FLOW_OK;
1307 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1309 GST_QUEUE_MUTEX_UNLOCK (queue);
1311 res = gst_pad_push_event (queue->sinkpad, event);
1314 res = gst_pad_event_default (pad, parent, event);
1323 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1325 GstQueue *queue = GST_QUEUE (parent);
1328 switch (GST_QUERY_TYPE (query)) {
1329 case GST_QUERY_SCHEDULING:{
1330 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1335 res = gst_pad_query_default (pad, parent, query);
1342 /* Adjust peer response for data contained in queue */
1343 switch (GST_QUERY_TYPE (query)) {
1344 case GST_QUERY_POSITION:
1349 /* get peer position */
1350 gst_query_parse_position (query, &format, &peer_pos);
1352 /* FIXME: this code assumes that there's no discont in the queue */
1354 case GST_FORMAT_BYTES:
1355 peer_pos -= queue->cur_level.bytes;
1357 case GST_FORMAT_TIME:
1358 peer_pos -= queue->cur_level.time;
1361 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1362 "know how to adjust value", gst_format_get_name (format));
1365 /* set updated position */
1366 gst_query_set_position (query, format, peer_pos);
1369 case GST_QUERY_LATENCY:
1372 GstClockTime min, max;
1374 gst_query_parse_latency (query, &live, &min, &max);
1376 /* we can delay up to the limit of the queue in time. If we have no time
1377 * limit, the best thing we can do is to return an infinite delay. In
1378 * reality a better estimate would be the byte/buffer rate but that is not
1379 * possible right now. */
1380 if (queue->max_size.time > 0 && max != -1)
1381 max += queue->max_size.time;
1385 /* adjust for min-threshold */
1386 if (queue->min_threshold.time > 0 && min != -1)
1387 min += queue->min_threshold.time;
1389 gst_query_set_latency (query, live, min, max);
1393 /* peer handled other queries */
1401 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1407 queue = GST_QUEUE (parent);
1410 case GST_PAD_MODE_PUSH:
1412 GST_QUEUE_MUTEX_LOCK (queue);
1413 queue->srcresult = GST_FLOW_OK;
1415 queue->unexpected = FALSE;
1416 GST_QUEUE_MUTEX_UNLOCK (queue);
1418 /* step 1, unblock chain function */
1419 GST_QUEUE_MUTEX_LOCK (queue);
1420 queue->srcresult = GST_FLOW_FLUSHING;
1421 /* the item del signal will unblock */
1422 g_cond_signal (&queue->item_del);
1423 /* unblock query handler */
1424 queue->last_query = FALSE;
1425 g_cond_signal (&queue->query_handled);
1426 GST_QUEUE_MUTEX_UNLOCK (queue);
1428 /* step 2, wait until streaming thread stopped and flush queue */
1429 GST_PAD_STREAM_LOCK (pad);
1430 GST_QUEUE_MUTEX_LOCK (queue);
1431 gst_queue_locked_flush (queue, TRUE);
1432 GST_QUEUE_MUTEX_UNLOCK (queue);
1433 GST_PAD_STREAM_UNLOCK (pad);
1445 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1451 queue = GST_QUEUE (parent);
1454 case GST_PAD_MODE_PUSH:
1456 GST_QUEUE_MUTEX_LOCK (queue);
1457 queue->srcresult = GST_FLOW_OK;
1459 queue->unexpected = FALSE;
1461 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1463 GST_QUEUE_MUTEX_UNLOCK (queue);
1465 /* step 1, unblock loop function */
1466 GST_QUEUE_MUTEX_LOCK (queue);
1467 queue->srcresult = GST_FLOW_FLUSHING;
1468 /* the item add signal will unblock */
1469 g_cond_signal (&queue->item_add);
1470 GST_QUEUE_MUTEX_UNLOCK (queue);
1472 /* step 2, make sure streaming finishes */
1473 result = gst_pad_stop_task (pad);
1484 queue_capacity_change (GstQueue * queue)
1486 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1487 gst_queue_leak_downstream (queue);
1490 /* changing the capacity of the queue must wake up
1491 * the _chain function, it might have more room now
1492 * to store the buffer/event in the queue */
1493 GST_QUEUE_SIGNAL_DEL (queue);
1496 /* Changing the minimum required fill level must
1497 * wake up the _loop function as it might now
1498 * be able to preceed.
1500 #define QUEUE_THRESHOLD_CHANGE(q)\
1501 GST_QUEUE_SIGNAL_ADD (q);
1504 gst_queue_set_property (GObject * object,
1505 guint prop_id, const GValue * value, GParamSpec * pspec)
1507 GstQueue *queue = GST_QUEUE (object);
1509 /* someone could change levels here, and since this
1510 * affects the get/put funcs, we need to lock for safety. */
1511 GST_QUEUE_MUTEX_LOCK (queue);
1514 case PROP_MAX_SIZE_BYTES:
1515 queue->max_size.bytes = g_value_get_uint (value);
1516 queue_capacity_change (queue);
1518 case PROP_MAX_SIZE_BUFFERS:
1519 queue->max_size.buffers = g_value_get_uint (value);
1520 queue_capacity_change (queue);
1522 case PROP_MAX_SIZE_TIME:
1523 queue->max_size.time = g_value_get_uint64 (value);
1524 queue_capacity_change (queue);
1526 case PROP_MIN_THRESHOLD_BYTES:
1527 queue->min_threshold.bytes = g_value_get_uint (value);
1528 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1529 QUEUE_THRESHOLD_CHANGE (queue);
1531 case PROP_MIN_THRESHOLD_BUFFERS:
1532 queue->min_threshold.buffers = g_value_get_uint (value);
1533 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1534 QUEUE_THRESHOLD_CHANGE (queue);
1536 case PROP_MIN_THRESHOLD_TIME:
1537 queue->min_threshold.time = g_value_get_uint64 (value);
1538 queue->orig_min_threshold.time = queue->min_threshold.time;
1539 QUEUE_THRESHOLD_CHANGE (queue);
1542 queue->leaky = g_value_get_enum (value);
1545 queue->silent = g_value_get_boolean (value);
1547 case PROP_FLUSH_ON_EOS:
1548 queue->flush_on_eos = g_value_get_boolean (value);
1551 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1555 GST_QUEUE_MUTEX_UNLOCK (queue);
1559 gst_queue_get_property (GObject * object,
1560 guint prop_id, GValue * value, GParamSpec * pspec)
1562 GstQueue *queue = GST_QUEUE (object);
1564 GST_QUEUE_MUTEX_LOCK (queue);
1567 case PROP_CUR_LEVEL_BYTES:
1568 g_value_set_uint (value, queue->cur_level.bytes);
1570 case PROP_CUR_LEVEL_BUFFERS:
1571 g_value_set_uint (value, queue->cur_level.buffers);
1573 case PROP_CUR_LEVEL_TIME:
1574 g_value_set_uint64 (value, queue->cur_level.time);
1576 case PROP_MAX_SIZE_BYTES:
1577 g_value_set_uint (value, queue->max_size.bytes);
1579 case PROP_MAX_SIZE_BUFFERS:
1580 g_value_set_uint (value, queue->max_size.buffers);
1582 case PROP_MAX_SIZE_TIME:
1583 g_value_set_uint64 (value, queue->max_size.time);
1585 case PROP_MIN_THRESHOLD_BYTES:
1586 g_value_set_uint (value, queue->min_threshold.bytes);
1588 case PROP_MIN_THRESHOLD_BUFFERS:
1589 g_value_set_uint (value, queue->min_threshold.buffers);
1591 case PROP_MIN_THRESHOLD_TIME:
1592 g_value_set_uint64 (value, queue->min_threshold.time);
1595 g_value_set_enum (value, queue->leaky);
1598 g_value_set_boolean (value, queue->silent);
1600 case PROP_FLUSH_ON_EOS:
1601 g_value_set_boolean (value, queue->flush_on_eos);
1604 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1608 GST_QUEUE_MUTEX_UNLOCK (queue);