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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, 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, \
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,
125 /* default property values */
126 #define DEFAULT_MAX_SIZE_BUFFERS 200 /* 200 buffers */
127 #define DEFAULT_MAX_SIZE_BYTES (10 * 1024 * 1024) /* 10 MB */
128 #define DEFAULT_MAX_SIZE_TIME GST_SECOND /* 1 second */
130 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
131 g_mutex_lock (q->qlock); \
134 #define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START { \
135 GST_QUEUE_MUTEX_LOCK (q); \
136 if (q->srcresult != GST_FLOW_OK) \
140 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
141 g_mutex_unlock (q->qlock); \
144 #define GST_QUEUE_WAIT_DEL_CHECK(q, label) G_STMT_START { \
145 STATUS (q, q->sinkpad, "wait for DEL"); \
146 q->waiting_del = TRUE; \
147 g_cond_wait (q->item_del, q->qlock); \
148 q->waiting_del = FALSE; \
149 if (q->srcresult != GST_FLOW_OK) { \
150 STATUS (q, q->srcpad, "received DEL wakeup"); \
153 STATUS (q, q->sinkpad, "received DEL"); \
156 #define GST_QUEUE_WAIT_ADD_CHECK(q, label) G_STMT_START { \
157 STATUS (q, q->srcpad, "wait for ADD"); \
158 q->waiting_add = TRUE; \
159 g_cond_wait (q->item_add, q->qlock); \
160 q->waiting_add = FALSE; \
161 if (q->srcresult != GST_FLOW_OK) { \
162 STATUS (q, q->srcpad, "received ADD wakeup"); \
165 STATUS (q, q->srcpad, "received ADD"); \
168 #define GST_QUEUE_SIGNAL_DEL(q) G_STMT_START { \
169 if (q->waiting_del) { \
170 STATUS (q, q->srcpad, "signal DEL"); \
171 g_cond_signal (q->item_del); \
175 #define GST_QUEUE_SIGNAL_ADD(q) G_STMT_START { \
176 if (q->waiting_add) { \
177 STATUS (q, q->sinkpad, "signal ADD"); \
178 g_cond_signal (q->item_add); \
182 #define _do_init(bla) \
183 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue", 0, "queue element"); \
184 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0, \
185 "dataflow inside the queue element");
187 GST_BOILERPLATE_FULL (GstQueue, gst_queue, GstElement,
188 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, GstBuffer * buffer);
198 static GstFlowReturn gst_queue_bufferalloc (GstPad * pad, guint64 offset,
199 guint size, GstCaps * caps, GstBuffer ** buf);
200 static GstFlowReturn gst_queue_push_one (GstQueue * queue);
201 static void gst_queue_loop (GstPad * pad);
203 static gboolean gst_queue_handle_sink_event (GstPad * pad, GstEvent * event);
205 static gboolean gst_queue_handle_src_event (GstPad * pad, GstEvent * event);
206 static gboolean gst_queue_handle_src_query (GstPad * pad, GstQuery * query);
208 static gboolean gst_queue_acceptcaps (GstPad * pad, GstCaps * caps);
209 static GstCaps *gst_queue_getcaps (GstPad * pad);
210 static GstPadLinkReturn gst_queue_link_sink (GstPad * pad, GstPad * peer);
211 static GstPadLinkReturn gst_queue_link_src (GstPad * pad, GstPad * peer);
212 static void gst_queue_locked_flush (GstQueue * queue);
214 static gboolean gst_queue_src_activate_push (GstPad * pad, gboolean active);
215 static gboolean gst_queue_sink_activate_push (GstPad * pad, gboolean active);
217 static gboolean gst_queue_is_empty (GstQueue * queue);
218 static gboolean gst_queue_is_filled (GstQueue * queue);
220 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
223 queue_leaky_get_type (void)
225 static GType queue_leaky_type = 0;
226 static const GEnumValue queue_leaky[] = {
227 {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
228 {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
229 {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
234 if (!queue_leaky_type) {
235 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
237 return queue_leaky_type;
240 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
243 gst_queue_base_init (gpointer g_class)
245 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
247 gst_element_class_set_details_simple (gstelement_class,
249 "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
250 gst_element_class_add_pad_template (gstelement_class,
251 gst_static_pad_template_get (&srctemplate));
252 gst_element_class_add_pad_template (gstelement_class,
253 gst_static_pad_template_get (&sinktemplate));
257 gst_queue_class_init (GstQueueClass * klass)
259 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
261 gobject_class->set_property = gst_queue_set_property;
262 gobject_class->get_property = gst_queue_get_property;
266 * GstQueue::underrun:
267 * @queue: the queue instance
269 * Reports that the buffer became empty (underrun).
270 * A buffer is empty if the total amount of data inside it (num-buffers, time,
271 * size) is lower than the boundary values which can be set through the
272 * GObject properties.
274 gst_queue_signals[SIGNAL_UNDERRUN] =
275 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
276 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
277 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
280 * @queue: the queue instance
282 * Reports that enough (min-threshold) data is in the queue. Use this signal
283 * together with the underrun signal to pause the pipeline on underrun and
284 * wait for the queue to fill-up before resume playback.
286 gst_queue_signals[SIGNAL_RUNNING] =
287 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
288 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
289 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
292 * @queue: the queue instance
294 * Reports that the buffer became full (overrun).
295 * A buffer is full if the total amount of data inside it (num-buffers, time,
296 * size) is higher than the boundary values which can be set through the
297 * GObject properties.
299 gst_queue_signals[SIGNAL_OVERRUN] =
300 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
301 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
302 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
305 * @queue: the queue instance
307 * Reports when the queue has enough data to start pushing data again on the
310 gst_queue_signals[SIGNAL_PUSHING] =
311 g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
312 G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
313 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
316 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
317 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
318 "Current amount of data in the queue (bytes)",
319 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
321 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
322 "Current number of buffers in the queue",
323 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
324 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
325 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
326 "Current amount of data in the queue (in ns)",
327 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
330 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
331 "Max. amount of data in the queue (bytes, 0=disable)",
332 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
335 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
336 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
337 DEFAULT_MAX_SIZE_BUFFERS,
338 G_PARAM_READWRITE | 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, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
345 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
346 "Min. amount of data in the queue to allow reading (bytes, 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_BUFFERS,
349 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
350 "Min. number of buffers in the queue to allow reading (0=disable)",
351 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
353 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
354 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
355 0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (gobject_class, PROP_LEAKY,
358 g_param_spec_enum ("leaky", "Leaky",
359 "Where the queue leaks, if at all",
360 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
361 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 * Don't emit queue signals. Makes queues more lightweight if no signals are
371 g_object_class_install_property (gobject_class, PROP_SILENT,
372 g_param_spec_boolean ("silent", "Silent",
373 "Don't emit queue signals", FALSE,
374 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 gobject_class->finalize = gst_queue_finalize;
378 /* Registering debug symbols for function pointers */
379 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
380 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_sink_activate_push);
381 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
382 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_link_sink);
383 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_getcaps);
384 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_acceptcaps);
385 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_bufferalloc);
386 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_push);
387 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_link_src);
388 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
389 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
393 gst_queue_init (GstQueue * queue, GstQueueClass * g_class)
395 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
397 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
398 gst_pad_set_activatepush_function (queue->sinkpad,
399 gst_queue_sink_activate_push);
400 gst_pad_set_event_function (queue->sinkpad, gst_queue_handle_sink_event);
401 gst_pad_set_link_function (queue->sinkpad, gst_queue_link_sink);
402 gst_pad_set_getcaps_function (queue->sinkpad, gst_queue_getcaps);
403 gst_pad_set_acceptcaps_function (queue->sinkpad, gst_queue_acceptcaps);
404 gst_pad_set_bufferalloc_function (queue->sinkpad, gst_queue_bufferalloc);
405 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
407 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
409 gst_pad_set_activatepush_function (queue->srcpad,
410 gst_queue_src_activate_push);
411 gst_pad_set_link_function (queue->srcpad, gst_queue_link_src);
412 gst_pad_set_acceptcaps_function (queue->srcpad, gst_queue_acceptcaps);
413 gst_pad_set_getcaps_function (queue->srcpad, gst_queue_getcaps);
414 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
415 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
416 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
418 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
419 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
420 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
421 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
422 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
423 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
424 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
425 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
426 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
428 queue->leaky = GST_QUEUE_NO_LEAK;
429 queue->srcresult = GST_FLOW_WRONG_STATE;
431 queue->qlock = g_mutex_new ();
432 queue->item_add = g_cond_new ();
433 queue->item_del = g_cond_new ();
435 g_queue_init (&queue->queue);
437 queue->sinktime = GST_CLOCK_TIME_NONE;
438 queue->srctime = GST_CLOCK_TIME_NONE;
440 queue->sink_tainted = TRUE;
441 queue->src_tainted = TRUE;
443 queue->newseg_applied_to_src = FALSE;
445 GST_DEBUG_OBJECT (queue,
446 "initialized queue's not_empty & not_full conditions");
449 /* called only once, as opposed to dispose */
451 gst_queue_finalize (GObject * object)
454 GstQueue *queue = GST_QUEUE (object);
456 GST_DEBUG_OBJECT (queue, "finalizing queue");
458 while ((data = g_queue_pop_head (&queue->queue)))
459 gst_mini_object_unref (data);
461 g_queue_clear (&queue->queue);
462 g_mutex_free (queue->qlock);
463 g_cond_free (queue->item_add);
464 g_cond_free (queue->item_del);
466 G_OBJECT_CLASS (parent_class)->finalize (object);
470 gst_queue_acceptcaps (GstPad * pad, GstCaps * caps)
476 queue = GST_QUEUE (gst_pad_get_parent (pad));
477 if (G_UNLIKELY (queue == NULL))
480 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
481 result = gst_pad_peer_accept_caps (otherpad, caps);
483 gst_object_unref (queue);
489 gst_queue_getcaps (GstPad * pad)
495 queue = GST_QUEUE (gst_pad_get_parent (pad));
496 if (G_UNLIKELY (queue == NULL))
497 return gst_caps_new_any ();
499 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
500 result = gst_pad_peer_get_caps (otherpad);
502 result = gst_caps_new_any ();
504 gst_object_unref (queue);
509 static GstPadLinkReturn
510 gst_queue_link_sink (GstPad * pad, GstPad * peer)
512 return GST_PAD_LINK_OK;
515 static GstPadLinkReturn
516 gst_queue_link_src (GstPad * pad, GstPad * peer)
518 GstPadLinkReturn result = GST_PAD_LINK_OK;
521 queue = GST_QUEUE (gst_pad_get_parent (pad));
523 GST_DEBUG_OBJECT (queue, "queue linking source pad");
525 if (GST_PAD_LINKFUNC (peer)) {
526 result = GST_PAD_LINKFUNC (peer) (peer, pad);
529 if (GST_PAD_LINK_SUCCESSFUL (result)) {
530 GST_QUEUE_MUTEX_LOCK (queue);
531 if (queue->srcresult == GST_FLOW_OK) {
532 queue->push_newsegment = TRUE;
533 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad);
534 GST_DEBUG_OBJECT (queue, "starting task as pad is linked");
536 GST_DEBUG_OBJECT (queue, "not starting task reason %s",
537 gst_flow_get_name (queue->srcresult));
539 GST_QUEUE_MUTEX_UNLOCK (queue);
541 gst_object_unref (queue);
547 gst_queue_bufferalloc (GstPad * pad, guint64 offset, guint size, GstCaps * caps,
551 GstFlowReturn result;
553 queue = GST_QUEUE (gst_pad_get_parent (pad));
554 if (G_UNLIKELY (queue == NULL))
555 return GST_FLOW_WRONG_STATE;
557 /* Forward to src pad, without setting caps on the src pad */
558 result = gst_pad_alloc_buffer (queue->srcpad, offset, size, caps, buf);
560 gst_object_unref (queue);
564 /* calculate the diff between running time on the sink and src of the queue.
565 * This is the total amount of time in the queue. */
567 update_time_level (GstQueue * queue)
569 gint64 sink_time, src_time;
571 if (queue->sink_tainted) {
573 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
574 queue->sink_segment.last_stop);
575 queue->sink_tainted = FALSE;
577 sink_time = queue->sinktime;
579 if (queue->src_tainted) {
581 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
582 queue->src_segment.last_stop);
583 queue->src_tainted = FALSE;
585 src_time = queue->srctime;
587 GST_LOG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
588 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
590 if (sink_time >= src_time)
591 queue->cur_level.time = sink_time - src_time;
593 queue->cur_level.time = 0;
596 /* take a NEWSEGMENT event and apply the values to segment, updating the time
599 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
605 gint64 start, stop, time;
607 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
608 &format, &start, &stop, &time);
610 /* now configure the values, we use these to track timestamps on the
612 if (format != GST_FORMAT_TIME) {
613 /* non-time format, pretent the current time segment is closed with a
614 * 0 start and unknown stop time. */
616 format = GST_FORMAT_TIME;
621 gst_segment_set_newsegment_full (segment, update,
622 rate, arate, format, start, stop, time);
625 queue->sink_tainted = TRUE;
627 queue->src_tainted = TRUE;
629 GST_DEBUG_OBJECT (queue,
630 "configured NEWSEGMENT %" GST_SEGMENT_FORMAT, segment);
632 /* segment can update the time level of the queue */
633 update_time_level (queue);
636 /* take a buffer and update segment, updating the time level of the queue. */
638 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
639 gboolean with_duration, gboolean sink)
641 GstClockTime duration, timestamp;
643 timestamp = GST_BUFFER_TIMESTAMP (buffer);
644 duration = GST_BUFFER_DURATION (buffer);
646 /* if no timestamp is set, assume it's continuous with the previous
648 if (timestamp == GST_CLOCK_TIME_NONE)
649 timestamp = segment->last_stop;
652 if (with_duration && duration != GST_CLOCK_TIME_NONE)
653 timestamp += duration;
655 GST_LOG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
656 GST_TIME_ARGS (timestamp));
658 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
660 queue->sink_tainted = TRUE;
662 queue->src_tainted = TRUE;
665 /* calc diff with other end */
666 update_time_level (queue);
670 gst_queue_locked_flush (GstQueue * queue)
674 while ((data = g_queue_pop_head (&queue->queue))) {
675 /* Then lose another reference because we are supposed to destroy that
676 data when flushing */
677 gst_mini_object_unref (data);
679 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
680 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
681 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
682 queue->min_threshold.time = queue->orig_min_threshold.time;
683 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
684 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
685 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
687 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
688 queue->sink_tainted = queue->src_tainted = TRUE;
690 /* we deleted a lot of something */
691 GST_QUEUE_SIGNAL_DEL (queue);
694 /* enqueue an item an update the level stats, with QUEUE_LOCK */
696 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
698 GstBuffer *buffer = GST_BUFFER_CAST (item);
700 /* add buffer to the statistics */
701 queue->cur_level.buffers++;
702 queue->cur_level.bytes += GST_BUFFER_SIZE (buffer);
703 apply_buffer (queue, buffer, &queue->sink_segment, TRUE, TRUE);
705 g_queue_push_tail (&queue->queue, item);
706 GST_QUEUE_SIGNAL_ADD (queue);
710 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
712 GstEvent *event = GST_EVENT_CAST (item);
714 switch (GST_EVENT_TYPE (event)) {
716 /* Zero the thresholds, this makes sure the queue is completely
717 * filled and we can read all data from the queue. */
718 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
719 /* mark the queue as EOS. This prevents us from accepting more data. */
720 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
723 case GST_EVENT_NEWSEGMENT:
724 apply_segment (queue, event, &queue->sink_segment, TRUE);
725 /* if the queue is empty, apply sink segment on the source */
726 if (queue->queue.length == 0) {
727 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
728 apply_segment (queue, event, &queue->src_segment, FALSE);
729 queue->newseg_applied_to_src = TRUE;
731 /* a new segment allows us to accept more buffers if we got UNEXPECTED
733 queue->unexpected = FALSE;
739 g_queue_push_tail (&queue->queue, item);
740 GST_QUEUE_SIGNAL_ADD (queue);
743 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
744 static GstMiniObject *
745 gst_queue_locked_dequeue (GstQueue * queue, gboolean * is_buffer)
749 item = g_queue_pop_head (&queue->queue);
753 if (GST_IS_BUFFER (item)) {
754 GstBuffer *buffer = GST_BUFFER_CAST (item);
756 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
757 "retrieved buffer %p from queue", buffer);
759 queue->cur_level.buffers--;
760 queue->cur_level.bytes -= GST_BUFFER_SIZE (buffer);
761 apply_buffer (queue, buffer, &queue->src_segment, TRUE, FALSE);
763 /* if the queue is empty now, update the other side */
764 if (queue->cur_level.buffers == 0)
765 queue->cur_level.time = 0;
768 } else if (GST_IS_EVENT (item)) {
769 GstEvent *event = GST_EVENT_CAST (item);
771 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
772 "retrieved event %p from queue", event);
774 switch (GST_EVENT_TYPE (event)) {
776 /* queue is empty now that we dequeued the EOS */
777 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
779 case GST_EVENT_NEWSEGMENT:
780 /* apply newsegment if it has not already been applied */
781 if (G_LIKELY (!queue->newseg_applied_to_src)) {
782 apply_segment (queue, event, &queue->src_segment, FALSE);
784 queue->newseg_applied_to_src = FALSE;
794 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
795 item, GST_OBJECT_NAME (queue));
798 GST_QUEUE_SIGNAL_DEL (queue);
805 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
811 gst_queue_handle_sink_event (GstPad * pad, GstEvent * event)
815 queue = GST_QUEUE (gst_pad_get_parent (pad));
816 if (G_UNLIKELY (queue == NULL)) {
817 gst_event_unref (event);
821 switch (GST_EVENT_TYPE (event)) {
822 case GST_EVENT_FLUSH_START:
824 STATUS (queue, pad, "received flush start event");
826 gst_pad_push_event (queue->srcpad, event);
828 /* now unblock the chain function */
829 GST_QUEUE_MUTEX_LOCK (queue);
830 queue->srcresult = GST_FLOW_WRONG_STATE;
831 /* unblock the loop and chain functions */
832 GST_QUEUE_SIGNAL_ADD (queue);
833 GST_QUEUE_SIGNAL_DEL (queue);
834 GST_QUEUE_MUTEX_UNLOCK (queue);
836 /* make sure it pauses, this should happen since we sent
837 * flush_start downstream. */
838 gst_pad_pause_task (queue->srcpad);
839 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
842 case GST_EVENT_FLUSH_STOP:
844 STATUS (queue, pad, "received flush stop event");
846 gst_pad_push_event (queue->srcpad, event);
848 GST_QUEUE_MUTEX_LOCK (queue);
849 gst_queue_locked_flush (queue);
850 queue->srcresult = GST_FLOW_OK;
852 queue->unexpected = FALSE;
853 if (gst_pad_is_linked (queue->srcpad)) {
854 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
857 GST_INFO_OBJECT (queue, "not re-starting task as pad is not linked");
859 GST_QUEUE_MUTEX_UNLOCK (queue);
861 STATUS (queue, pad, "after flush");
865 if (GST_EVENT_IS_SERIALIZED (event)) {
866 /* serialized events go in the queue */
867 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
868 /* refuse more events on EOS */
871 gst_queue_locked_enqueue_event (queue, event);
872 GST_QUEUE_MUTEX_UNLOCK (queue);
874 /* non-serialized events are passed upstream. */
875 gst_pad_push_event (queue->srcpad, event);
880 gst_object_unref (queue);
886 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
887 "refusing event, we are flushing");
888 GST_QUEUE_MUTEX_UNLOCK (queue);
889 gst_object_unref (queue);
890 gst_event_unref (event);
895 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
896 GST_QUEUE_MUTEX_UNLOCK (queue);
897 gst_object_unref (queue);
898 gst_event_unref (event);
904 gst_queue_is_empty (GstQueue * queue)
906 if (queue->queue.length == 0)
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)) {
939 leak = gst_queue_locked_dequeue (queue, &is_buffer);
940 /* there is nothing to dequeue and the queue is still filled.. This should
942 g_assert (leak != NULL);
944 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
945 "queue is full, leaking item %p on downstream end", leak);
946 gst_mini_object_unref (leak);
948 /* last buffer needs to get a DISCONT flag */
949 queue->head_needs_discont = TRUE;
954 gst_queue_chain (GstPad * pad, GstBuffer * buffer)
957 GstClockTime duration, timestamp;
959 queue = (GstQueue *) GST_OBJECT_PARENT (pad);
961 /* we have to lock the queue since we span threads */
962 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
963 /* when we received EOS, we refuse any more data */
966 if (queue->unexpected)
969 timestamp = GST_BUFFER_TIMESTAMP (buffer);
970 duration = GST_BUFFER_DURATION (buffer);
972 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
973 "received buffer %p of size %d, time %" GST_TIME_FORMAT ", duration %"
974 GST_TIME_FORMAT, buffer, GST_BUFFER_SIZE (buffer),
975 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
977 /* We make space available if we're "full" according to whatever
978 * the user defined as "full". Note that this only applies to buffers.
979 * We always handle events and they don't count in our statistics. */
980 while (gst_queue_is_filled (queue)) {
981 if (!queue->silent) {
982 GST_QUEUE_MUTEX_UNLOCK (queue);
983 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
984 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
985 /* we recheck, the signal could have changed the thresholds */
986 if (!gst_queue_is_filled (queue))
990 /* how are we going to make space for this buffer? */
991 switch (queue->leaky) {
992 case GST_QUEUE_LEAK_UPSTREAM:
993 /* next buffer needs to get a DISCONT flag */
994 queue->tail_needs_discont = TRUE;
995 /* leak current buffer */
996 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
997 "queue is full, leaking buffer on upstream end");
998 /* now we can clean up and exit right away */
1000 case GST_QUEUE_LEAK_DOWNSTREAM:
1001 gst_queue_leak_downstream (queue);
1004 g_warning ("Unknown leaky type, using default");
1006 case GST_QUEUE_NO_LEAK:
1008 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1009 "queue is full, waiting for free space");
1011 /* don't leak. Instead, wait for space to be available */
1013 /* for as long as the queue is filled, wait till an item was deleted. */
1014 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1015 } while (gst_queue_is_filled (queue));
1017 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1019 if (!queue->silent) {
1020 GST_QUEUE_MUTEX_UNLOCK (queue);
1021 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1022 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1029 if (queue->tail_needs_discont) {
1030 GstBuffer *subbuffer = gst_buffer_make_metadata_writable (buffer);
1034 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1036 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1038 queue->tail_needs_discont = FALSE;
1041 /* put buffer in queue now */
1042 gst_queue_locked_enqueue_buffer (queue, buffer);
1043 GST_QUEUE_MUTEX_UNLOCK (queue);
1047 /* special conditions */
1050 GST_QUEUE_MUTEX_UNLOCK (queue);
1052 gst_buffer_unref (buffer);
1058 GstFlowReturn ret = queue->srcresult;
1060 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1061 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1062 GST_QUEUE_MUTEX_UNLOCK (queue);
1063 gst_buffer_unref (buffer);
1069 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1070 GST_QUEUE_MUTEX_UNLOCK (queue);
1072 gst_buffer_unref (buffer);
1074 return GST_FLOW_UNEXPECTED;
1078 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1079 "exit because we received UNEXPECTED");
1080 GST_QUEUE_MUTEX_UNLOCK (queue);
1082 gst_buffer_unref (buffer);
1084 return GST_FLOW_UNEXPECTED;
1089 gst_queue_push_newsegment (GstQueue * queue)
1094 s = &queue->src_segment;
1096 if (s->accum != 0) {
1097 event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0, s->format, 0,
1099 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1100 "pushing accum newsegment event");
1101 gst_pad_push_event (queue->srcpad, event);
1104 event = gst_event_new_new_segment_full (FALSE, s->rate, s->applied_rate,
1105 s->format, s->start, s->stop, s->time);
1106 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "pushing real newsegment event");
1107 gst_pad_push_event (queue->srcpad, event);
1110 /* dequeue an item from the queue an push it downstream. This functions returns
1111 * the result of the push. */
1112 static GstFlowReturn
1113 gst_queue_push_one (GstQueue * queue)
1115 GstFlowReturn result = GST_FLOW_OK;
1116 GstMiniObject *data;
1119 data = gst_queue_locked_dequeue (queue, &is_buffer);
1128 buffer = GST_BUFFER_CAST (data);
1130 if (queue->head_needs_discont) {
1131 GstBuffer *subbuffer = gst_buffer_make_metadata_writable (buffer);
1135 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1137 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1139 queue->head_needs_discont = FALSE;
1142 caps = GST_BUFFER_CAPS (buffer);
1144 GST_QUEUE_MUTEX_UNLOCK (queue);
1145 /* set the right caps on the pad now. We do this before pushing the buffer
1146 * because the pad_push call will check (using acceptcaps) if the buffer can
1147 * be set on the pad, which might fail because this will be propagated
1148 * upstream. Also note that if the buffer has NULL caps, it means that the
1149 * caps did not change, so we don't have to change caps on the pad. */
1150 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
1151 gst_pad_set_caps (queue->srcpad, caps);
1153 if (queue->push_newsegment) {
1154 gst_queue_push_newsegment (queue);
1156 result = gst_pad_push (queue->srcpad, buffer);
1158 /* need to check for srcresult here as well */
1159 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1161 if (result == GST_FLOW_UNEXPECTED) {
1162 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1163 "got UNEXPECTED from downstream");
1164 /* stop pushing buffers, we dequeue all items until we see an item that we
1165 * can push again, which is EOS or NEWSEGMENT. If there is nothing in the
1166 * queue we can push, we set a flag to make the sinkpad refuse more
1167 * buffers with an UNEXPECTED return value. */
1168 while ((data = gst_queue_locked_dequeue (queue, &is_buffer))) {
1170 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1171 "dropping UNEXPECTED buffer %p", data);
1172 gst_buffer_unref (GST_BUFFER_CAST (data));
1174 GstEvent *event = GST_EVENT_CAST (data);
1175 GstEventType type = GST_EVENT_TYPE (event);
1177 if (type == GST_EVENT_EOS || type == GST_EVENT_NEWSEGMENT) {
1178 /* we found a pushable item in the queue, push it out */
1179 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1180 "pushing pushable event %s after UNEXPECTED",
1181 GST_EVENT_TYPE_NAME (event));
1184 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1185 "dropping UNEXPECTED event %p", event);
1186 gst_event_unref (event);
1189 /* no more items in the queue. Set the unexpected flag so that upstream
1190 * make us refuse any more buffers on the sinkpad. Since we will still
1191 * accept EOS and NEWSEGMENT we return _FLOW_OK to the caller so that the
1192 * task function does not shut down. */
1193 queue->unexpected = TRUE;
1194 result = GST_FLOW_OK;
1197 GstEvent *event = GST_EVENT_CAST (data);
1198 GstEventType type = GST_EVENT_TYPE (event);
1200 GST_QUEUE_MUTEX_UNLOCK (queue);
1202 if (queue->push_newsegment && type != GST_EVENT_NEWSEGMENT) {
1203 gst_queue_push_newsegment (queue);
1205 gst_pad_push_event (queue->srcpad, event);
1207 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1208 /* if we're EOS, return UNEXPECTED so that the task pauses. */
1209 if (type == GST_EVENT_EOS) {
1210 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1211 "pushed EOS event %p, return UNEXPECTED", event);
1212 result = GST_FLOW_UNEXPECTED;
1220 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1221 "exit because we have no item in the queue");
1222 return GST_FLOW_ERROR;
1226 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1227 return GST_FLOW_WRONG_STATE;
1232 gst_queue_loop (GstPad * pad)
1237 queue = (GstQueue *) GST_PAD_PARENT (pad);
1239 /* have to lock for thread-safety */
1240 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1242 while (gst_queue_is_empty (queue)) {
1243 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1244 if (!queue->silent) {
1245 GST_QUEUE_MUTEX_UNLOCK (queue);
1246 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1247 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1250 /* we recheck, the signal could have changed the thresholds */
1251 while (gst_queue_is_empty (queue)) {
1252 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1255 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1256 if (!queue->silent) {
1257 GST_QUEUE_MUTEX_UNLOCK (queue);
1258 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1259 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1260 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1264 ret = gst_queue_push_one (queue);
1265 queue->push_newsegment = FALSE;
1266 queue->srcresult = ret;
1267 if (ret != GST_FLOW_OK)
1270 GST_QUEUE_MUTEX_UNLOCK (queue);
1277 gboolean eos = queue->eos;
1278 GstFlowReturn ret = queue->srcresult;
1280 gst_pad_pause_task (queue->srcpad);
1281 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1282 "pause task, reason: %s", gst_flow_get_name (ret));
1283 GST_QUEUE_SIGNAL_DEL (queue);
1284 GST_QUEUE_MUTEX_UNLOCK (queue);
1285 /* let app know about us giving up if upstream is not expected to do so */
1286 /* UNEXPECTED is already taken care of elsewhere */
1287 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED)) {
1288 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
1289 (_("Internal data flow error.")),
1290 ("streaming task paused, reason %s (%d)",
1291 gst_flow_get_name (ret), ret));
1292 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1299 gst_queue_handle_src_event (GstPad * pad, GstEvent * event)
1301 gboolean res = TRUE;
1302 GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
1304 if (G_UNLIKELY (queue == NULL)) {
1305 gst_event_unref (event);
1308 #ifndef GST_DISABLE_GST_DEBUG
1309 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1310 event, GST_EVENT_TYPE (event));
1313 res = gst_pad_push_event (queue->sinkpad, event);
1315 gst_object_unref (queue);
1320 gst_queue_handle_src_query (GstPad * pad, GstQuery * query)
1322 GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
1326 if (G_UNLIKELY (queue == NULL))
1329 if (!(peer = gst_pad_get_peer (queue->sinkpad))) {
1330 gst_object_unref (queue);
1334 res = gst_pad_query (peer, query);
1335 gst_object_unref (peer);
1337 gst_object_unref (queue);
1341 switch (GST_QUERY_TYPE (query)) {
1342 case GST_QUERY_POSITION:
1347 /* get peer position */
1348 gst_query_parse_position (query, &format, &peer_pos);
1350 /* FIXME: this code assumes that there's no discont in the queue */
1352 case GST_FORMAT_BYTES:
1353 peer_pos -= queue->cur_level.bytes;
1355 case GST_FORMAT_TIME:
1356 peer_pos -= queue->cur_level.time;
1359 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1360 "know how to adjust value", gst_format_get_name (format));
1363 /* set updated position */
1364 gst_query_set_position (query, format, peer_pos);
1367 case GST_QUERY_LATENCY:
1370 GstClockTime min, max;
1372 gst_query_parse_latency (query, &live, &min, &max);
1374 /* we can delay up to the limit of the queue in time. If we have no time
1375 * limit, the best thing we can do is to return an infinite delay. In
1376 * reality a better estimate would be the byte/buffer rate but that is not
1377 * possible right now. */
1378 if (queue->max_size.time > 0 && max != -1)
1379 max += queue->max_size.time;
1383 /* adjust for min-threshold */
1384 if (queue->min_threshold.time > 0 && min != -1)
1385 min += queue->min_threshold.time;
1387 gst_query_set_latency (query, live, min, max);
1391 /* peer handled other queries */
1395 gst_object_unref (queue);
1400 gst_queue_sink_activate_push (GstPad * pad, gboolean active)
1402 gboolean result = TRUE;
1405 queue = GST_QUEUE (gst_pad_get_parent (pad));
1408 GST_QUEUE_MUTEX_LOCK (queue);
1409 queue->srcresult = GST_FLOW_OK;
1411 queue->unexpected = FALSE;
1412 GST_QUEUE_MUTEX_UNLOCK (queue);
1414 /* step 1, unblock chain function */
1415 GST_QUEUE_MUTEX_LOCK (queue);
1416 queue->srcresult = GST_FLOW_WRONG_STATE;
1417 gst_queue_locked_flush (queue);
1418 GST_QUEUE_MUTEX_UNLOCK (queue);
1421 gst_object_unref (queue);
1427 gst_queue_src_activate_push (GstPad * pad, gboolean active)
1429 gboolean result = FALSE;
1432 queue = GST_QUEUE (gst_pad_get_parent (pad));
1435 GST_QUEUE_MUTEX_LOCK (queue);
1436 queue->srcresult = GST_FLOW_OK;
1438 queue->unexpected = FALSE;
1439 /* we do not start the task yet if the pad is not connected */
1440 if (gst_pad_is_linked (pad))
1441 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad);
1443 GST_INFO_OBJECT (queue, "not starting task as pad is not linked");
1446 GST_QUEUE_MUTEX_UNLOCK (queue);
1448 /* step 1, unblock loop function */
1449 GST_QUEUE_MUTEX_LOCK (queue);
1450 queue->srcresult = GST_FLOW_WRONG_STATE;
1451 /* the item add signal will unblock */
1452 g_cond_signal (queue->item_add);
1453 GST_QUEUE_MUTEX_UNLOCK (queue);
1455 /* step 2, make sure streaming finishes */
1456 result = gst_pad_stop_task (pad);
1459 gst_object_unref (queue);
1465 queue_capacity_change (GstQueue * queue)
1467 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1468 gst_queue_leak_downstream (queue);
1471 /* changing the capacity of the queue must wake up
1472 * the _chain function, it might have more room now
1473 * to store the buffer/event in the queue */
1474 GST_QUEUE_SIGNAL_DEL (queue);
1477 /* Changing the minimum required fill level must
1478 * wake up the _loop function as it might now
1479 * be able to preceed.
1481 #define QUEUE_THRESHOLD_CHANGE(q)\
1482 GST_QUEUE_SIGNAL_ADD (q);
1485 gst_queue_set_property (GObject * object,
1486 guint prop_id, const GValue * value, GParamSpec * pspec)
1488 GstQueue *queue = GST_QUEUE (object);
1490 /* someone could change levels here, and since this
1491 * affects the get/put funcs, we need to lock for safety. */
1492 GST_QUEUE_MUTEX_LOCK (queue);
1495 case PROP_MAX_SIZE_BYTES:
1496 queue->max_size.bytes = g_value_get_uint (value);
1497 queue_capacity_change (queue);
1499 case PROP_MAX_SIZE_BUFFERS:
1500 queue->max_size.buffers = g_value_get_uint (value);
1501 queue_capacity_change (queue);
1503 case PROP_MAX_SIZE_TIME:
1504 queue->max_size.time = g_value_get_uint64 (value);
1505 queue_capacity_change (queue);
1507 case PROP_MIN_THRESHOLD_BYTES:
1508 queue->min_threshold.bytes = g_value_get_uint (value);
1509 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1510 QUEUE_THRESHOLD_CHANGE (queue);
1512 case PROP_MIN_THRESHOLD_BUFFERS:
1513 queue->min_threshold.buffers = g_value_get_uint (value);
1514 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1515 QUEUE_THRESHOLD_CHANGE (queue);
1517 case PROP_MIN_THRESHOLD_TIME:
1518 queue->min_threshold.time = g_value_get_uint64 (value);
1519 queue->orig_min_threshold.time = queue->min_threshold.time;
1520 QUEUE_THRESHOLD_CHANGE (queue);
1523 queue->leaky = g_value_get_enum (value);
1526 queue->silent = g_value_get_boolean (value);
1529 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1533 GST_QUEUE_MUTEX_UNLOCK (queue);
1537 gst_queue_get_property (GObject * object,
1538 guint prop_id, GValue * value, GParamSpec * pspec)
1540 GstQueue *queue = GST_QUEUE (object);
1542 GST_QUEUE_MUTEX_LOCK (queue);
1545 case PROP_CUR_LEVEL_BYTES:
1546 g_value_set_uint (value, queue->cur_level.bytes);
1548 case PROP_CUR_LEVEL_BUFFERS:
1549 g_value_set_uint (value, queue->cur_level.buffers);
1551 case PROP_CUR_LEVEL_TIME:
1552 g_value_set_uint64 (value, queue->cur_level.time);
1554 case PROP_MAX_SIZE_BYTES:
1555 g_value_set_uint (value, queue->max_size.bytes);
1557 case PROP_MAX_SIZE_BUFFERS:
1558 g_value_set_uint (value, queue->max_size.buffers);
1560 case PROP_MAX_SIZE_TIME:
1561 g_value_set_uint64 (value, queue->max_size.time);
1563 case PROP_MIN_THRESHOLD_BYTES:
1564 g_value_set_uint (value, queue->min_threshold.bytes);
1566 case PROP_MIN_THRESHOLD_BUFFERS:
1567 g_value_set_uint (value, queue->min_threshold.buffers);
1569 case PROP_MIN_THRESHOLD_TIME:
1570 g_value_set_uint64 (value, queue->min_threshold.time);
1573 g_value_set_enum (value, queue->leaky);
1576 g_value_set_boolean (value, queue->silent);
1579 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1583 GST_QUEUE_MUTEX_UNLOCK (queue);