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 | GST_PARAM_MUTABLE_PLAYING |
330 G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
332 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
333 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
334 DEFAULT_MAX_SIZE_BUFFERS,
335 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
336 G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
338 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
339 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
340 DEFAULT_MAX_SIZE_TIME,
341 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
342 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)",
348 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
349 G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
351 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
352 "Min. number of buffers in the queue to allow reading (0=disable)", 0,
354 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
355 G_PARAM_STATIC_STRINGS));
356 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
357 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
358 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
360 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
361 G_PARAM_STATIC_STRINGS));
363 g_object_class_install_property (gobject_class, PROP_LEAKY,
364 g_param_spec_enum ("leaky", "Leaky",
365 "Where the queue leaks, if at all",
366 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
367 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
368 G_PARAM_STATIC_STRINGS));
373 * Don't emit queue signals. Makes queues more lightweight if no signals are
376 g_object_class_install_property (gobject_class, PROP_SILENT,
377 g_param_spec_boolean ("silent", "Silent",
378 "Don't emit queue signals", FALSE,
379 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
380 G_PARAM_STATIC_STRINGS));
383 * GstQueue:flush-on-eos
385 * Discard all data in the queue when an EOS event is received, and pass
386 * on the EOS event as soon as possible (instead of waiting until all
387 * buffers in the queue have been processed, which is the default behaviour).
389 * Flushing the queue on EOS might be useful when capturing and encoding
390 * from a live source, to finish up the recording quickly in cases when
391 * the encoder is slow. Note that this might mean some data from the end of
392 * the recording data might be lost though (never more than the configured
393 * max. sizes though).
397 g_object_class_install_property (gobject_class, PROP_FLUSH_ON_EOS,
398 g_param_spec_boolean ("flush-on-eos", "Flush on EOS",
399 "Discard all data in the queue when an EOS event is received", FALSE,
400 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
401 G_PARAM_STATIC_STRINGS));
403 gobject_class->finalize = gst_queue_finalize;
405 gst_element_class_set_static_metadata (gstelement_class,
407 "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
408 gst_element_class_add_pad_template (gstelement_class,
409 gst_static_pad_template_get (&srctemplate));
410 gst_element_class_add_pad_template (gstelement_class,
411 gst_static_pad_template_get (&sinktemplate));
413 /* Registering debug symbols for function pointers */
414 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
415 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
416 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
417 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
418 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
419 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
423 gst_queue_init (GstQueue * queue)
425 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
427 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
428 gst_pad_set_activatemode_function (queue->sinkpad,
429 gst_queue_sink_activate_mode);
430 gst_pad_set_event_function (queue->sinkpad, gst_queue_handle_sink_event);
431 gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
432 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
433 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
435 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
437 gst_pad_set_activatemode_function (queue->srcpad,
438 gst_queue_src_activate_mode);
439 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
440 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
441 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
442 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
444 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
445 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
446 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
447 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
448 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
449 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
450 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
451 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
452 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
454 queue->leaky = GST_QUEUE_NO_LEAK;
455 queue->srcresult = GST_FLOW_FLUSHING;
457 g_mutex_init (&queue->qlock);
458 g_cond_init (&queue->item_add);
459 g_cond_init (&queue->item_del);
460 g_cond_init (&queue->query_handled);
462 queue->queue = gst_queue_array_new (DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
464 queue->sinktime = GST_CLOCK_TIME_NONE;
465 queue->srctime = GST_CLOCK_TIME_NONE;
467 queue->sink_tainted = TRUE;
468 queue->src_tainted = TRUE;
470 queue->newseg_applied_to_src = FALSE;
472 GST_DEBUG_OBJECT (queue,
473 "initialized queue's not_empty & not_full conditions");
476 /* called only once, as opposed to dispose */
478 gst_queue_finalize (GObject * object)
480 GstQueue *queue = GST_QUEUE (object);
482 GST_DEBUG_OBJECT (queue, "finalizing queue");
484 while (!gst_queue_array_is_empty (queue->queue)) {
485 GstQueueItem *qitem = gst_queue_array_pop_head (queue->queue);
486 /* FIXME: if it's a query, shouldn't we unref that too? */
487 if (!qitem->is_query)
488 gst_mini_object_unref (qitem->item);
489 g_slice_free (GstQueueItem, qitem);
491 gst_queue_array_free (queue->queue);
493 g_mutex_clear (&queue->qlock);
494 g_cond_clear (&queue->item_add);
495 g_cond_clear (&queue->item_del);
496 g_cond_clear (&queue->query_handled);
498 G_OBJECT_CLASS (parent_class)->finalize (object);
501 /* calculate the diff between running time on the sink and src of the queue.
502 * This is the total amount of time in the queue. */
504 update_time_level (GstQueue * queue)
506 gint64 sink_time, src_time;
508 if (queue->sink_tainted) {
509 GST_LOG_OBJECT (queue, "update sink time");
511 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
512 queue->sink_segment.position);
513 queue->sink_tainted = FALSE;
515 sink_time = queue->sinktime;
517 if (queue->src_tainted) {
518 GST_LOG_OBJECT (queue, "update src time");
520 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
521 queue->src_segment.position);
522 queue->src_tainted = FALSE;
524 src_time = queue->srctime;
526 GST_LOG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
527 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
529 if (sink_time >= src_time)
530 queue->cur_level.time = sink_time - src_time;
532 queue->cur_level.time = 0;
535 /* take a SEGMENT event and apply the values to segment, updating the time
538 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
541 gst_event_copy_segment (event, segment);
543 /* now configure the values, we use these to track timestamps on the
545 if (segment->format != GST_FORMAT_TIME) {
546 /* non-time format, pretent the current time segment is closed with a
547 * 0 start and unknown stop time. */
548 segment->format = GST_FORMAT_TIME;
554 queue->sink_tainted = TRUE;
556 queue->src_tainted = TRUE;
558 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
560 /* segment can update the time level of the queue */
561 update_time_level (queue);
565 apply_gap (GstQueue * queue, GstEvent * event,
566 GstSegment * segment, gboolean is_sink)
568 GstClockTime timestamp;
569 GstClockTime duration;
571 gst_event_parse_gap (event, ×tamp, &duration);
573 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
575 if (GST_CLOCK_TIME_IS_VALID (duration)) {
576 timestamp += duration;
579 segment->position = timestamp;
582 queue->sink_tainted = TRUE;
584 queue->src_tainted = TRUE;
586 /* calc diff with other end */
587 update_time_level (queue);
592 /* take a buffer and update segment, updating the time level of the queue. */
594 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
595 gboolean with_duration, gboolean sink)
597 GstClockTime duration, timestamp;
599 timestamp = GST_BUFFER_TIMESTAMP (buffer);
600 duration = GST_BUFFER_DURATION (buffer);
602 /* if no timestamp is set, assume it's continuous with the previous
604 if (timestamp == GST_CLOCK_TIME_NONE)
605 timestamp = segment->position;
608 if (with_duration && duration != GST_CLOCK_TIME_NONE)
609 timestamp += duration;
611 GST_LOG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
612 GST_TIME_ARGS (timestamp));
614 segment->position = timestamp;
616 queue->sink_tainted = TRUE;
618 queue->src_tainted = TRUE;
621 /* calc diff with other end */
622 update_time_level (queue);
626 gst_queue_locked_flush (GstQueue * queue, gboolean full)
628 while (!gst_queue_array_is_empty (queue->queue)) {
629 GstQueueItem *qitem = gst_queue_array_pop_head (queue->queue);
631 /* Then lose another reference because we are supposed to destroy that
632 data when flushing */
633 if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
634 && GST_EVENT_IS_STICKY (qitem->item)
635 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
636 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
637 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
639 if (!qitem->is_query)
640 gst_mini_object_unref (qitem->item);
641 g_slice_free (GstQueueItem, qitem);
643 queue->last_query = FALSE;
644 g_cond_signal (&queue->query_handled);
645 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
646 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
647 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
648 queue->min_threshold.time = queue->orig_min_threshold.time;
649 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
650 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
651 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
653 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
654 queue->sink_tainted = queue->src_tainted = TRUE;
656 /* we deleted a lot of something */
657 GST_QUEUE_SIGNAL_DEL (queue);
660 /* enqueue an item an update the level stats, with QUEUE_LOCK */
662 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
665 GstBuffer *buffer = GST_BUFFER_CAST (item);
666 gsize bsize = gst_buffer_get_size (buffer);
668 /* add buffer to the statistics */
669 queue->cur_level.buffers++;
670 queue->cur_level.bytes += bsize;
671 apply_buffer (queue, buffer, &queue->sink_segment, TRUE, TRUE);
673 qitem = g_slice_new (GstQueueItem);
675 qitem->is_query = FALSE;
677 gst_queue_array_push_tail (queue->queue, qitem);
678 GST_QUEUE_SIGNAL_ADD (queue);
682 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
685 GstEvent *event = GST_EVENT_CAST (item);
687 switch (GST_EVENT_TYPE (event)) {
689 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
690 /* Zero the thresholds, this makes sure the queue is completely
691 * filled and we can read all data from the queue. */
692 if (queue->flush_on_eos)
693 gst_queue_locked_flush (queue, FALSE);
695 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
696 /* mark the queue as EOS. This prevents us from accepting more data. */
699 case GST_EVENT_SEGMENT:
700 apply_segment (queue, event, &queue->sink_segment, TRUE);
701 /* if the queue is empty, apply sink segment on the source */
702 if (gst_queue_array_is_empty (queue->queue)) {
703 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
704 apply_segment (queue, event, &queue->src_segment, FALSE);
705 queue->newseg_applied_to_src = TRUE;
707 /* a new segment allows us to accept more buffers if we got EOS
709 queue->unexpected = FALSE;
711 apply_gap (queue, event, &queue->sink_segment, TRUE);
717 qitem = g_slice_new (GstQueueItem);
719 qitem->is_query = FALSE;
720 gst_queue_array_push_tail (queue->queue, qitem);
721 GST_QUEUE_SIGNAL_ADD (queue);
724 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
725 static GstMiniObject *
726 gst_queue_locked_dequeue (GstQueue * queue)
732 qitem = gst_queue_array_pop_head (queue->queue);
737 bufsize = qitem->size;
738 g_slice_free (GstQueueItem, qitem);
740 if (GST_IS_BUFFER (item)) {
741 GstBuffer *buffer = GST_BUFFER_CAST (item);
743 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
744 "retrieved buffer %p from queue", buffer);
746 queue->cur_level.buffers--;
747 queue->cur_level.bytes -= bufsize;
748 apply_buffer (queue, buffer, &queue->src_segment, TRUE, FALSE);
750 /* if the queue is empty now, update the other side */
751 if (queue->cur_level.buffers == 0)
752 queue->cur_level.time = 0;
754 } else if (GST_IS_EVENT (item)) {
755 GstEvent *event = GST_EVENT_CAST (item);
757 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
758 "retrieved event %p from queue", event);
760 switch (GST_EVENT_TYPE (event)) {
762 /* queue is empty now that we dequeued the EOS */
763 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
765 case GST_EVENT_SEGMENT:
766 /* apply newsegment if it has not already been applied */
767 if (G_LIKELY (!queue->newseg_applied_to_src)) {
768 apply_segment (queue, event, &queue->src_segment, FALSE);
770 queue->newseg_applied_to_src = FALSE;
774 apply_gap (queue, event, &queue->src_segment, FALSE);
779 } else if (GST_IS_QUERY (item)) {
780 GstQuery *query = GST_QUERY_CAST (item);
782 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
783 "retrieved query %p from queue", query);
786 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
787 item, GST_OBJECT_NAME (queue));
790 GST_QUEUE_SIGNAL_DEL (queue);
797 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
803 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
808 queue = GST_QUEUE (parent);
810 switch (GST_EVENT_TYPE (event)) {
811 case GST_EVENT_FLUSH_START:
812 STATUS (queue, pad, "received flush start event");
814 ret = gst_pad_push_event (queue->srcpad, event);
816 /* now unblock the chain function */
817 GST_QUEUE_MUTEX_LOCK (queue);
818 queue->srcresult = GST_FLOW_FLUSHING;
819 /* unblock the loop and chain functions */
820 GST_QUEUE_SIGNAL_ADD (queue);
821 GST_QUEUE_SIGNAL_DEL (queue);
822 queue->last_query = FALSE;
823 g_cond_signal (&queue->query_handled);
824 GST_QUEUE_MUTEX_UNLOCK (queue);
826 /* make sure it pauses, this should happen since we sent
827 * flush_start downstream. */
828 gst_pad_pause_task (queue->srcpad);
829 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
831 case GST_EVENT_FLUSH_STOP:
832 STATUS (queue, pad, "received flush stop event");
834 ret = gst_pad_push_event (queue->srcpad, event);
836 GST_QUEUE_MUTEX_LOCK (queue);
837 gst_queue_locked_flush (queue, FALSE);
838 queue->srcresult = GST_FLOW_OK;
840 queue->unexpected = FALSE;
841 if (gst_pad_is_active (queue->srcpad)) {
842 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
843 queue->srcpad, NULL);
845 GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
846 "pad not active any longer");
848 GST_QUEUE_MUTEX_UNLOCK (queue);
850 STATUS (queue, pad, "after flush");
853 if (GST_EVENT_IS_SERIALIZED (event)) {
854 /* serialized events go in the queue */
855 GST_QUEUE_MUTEX_LOCK (queue);
856 if (queue->srcresult != GST_FLOW_OK) {
857 /* Errors in sticky event pushing are no problem and ignored here
858 * as they will cause more meaningful errors during data flow.
859 * For EOS events, that are not followed by data flow, we still
860 * return FALSE here though and report an error.
862 if (!GST_EVENT_IS_STICKY (event)) {
863 GST_QUEUE_MUTEX_UNLOCK (queue);
865 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
866 if (queue->srcresult == GST_FLOW_NOT_LINKED
867 || queue->srcresult < GST_FLOW_EOS) {
868 GST_QUEUE_MUTEX_UNLOCK (queue);
869 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
870 (_("Internal data flow error.")),
871 ("streaming task paused, reason %s (%d)",
872 gst_flow_get_name (queue->srcresult), queue->srcresult));
874 GST_QUEUE_MUTEX_UNLOCK (queue);
879 /* refuse more events on EOS */
882 gst_queue_locked_enqueue_event (queue, event);
883 GST_QUEUE_MUTEX_UNLOCK (queue);
885 /* non-serialized events are forwarded downstream immediately */
886 ret = gst_pad_push_event (queue->srcpad, event);
895 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
896 GST_QUEUE_MUTEX_UNLOCK (queue);
897 gst_event_unref (event);
902 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
903 "refusing event, we have a downstream flow error: %s",
904 gst_flow_get_name (queue->srcresult));
905 gst_event_unref (event);
911 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
913 GstQueue *queue = GST_QUEUE_CAST (parent);
916 switch (GST_QUERY_TYPE (query)) {
918 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
921 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
922 GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
923 GST_QUERY_TYPE_NAME (query));
924 qitem = g_slice_new (GstQueueItem);
925 qitem->item = GST_MINI_OBJECT_CAST (query);
926 qitem->is_query = TRUE;
927 gst_queue_array_push_tail (queue->queue, qitem);
928 GST_QUEUE_SIGNAL_ADD (queue);
929 g_cond_wait (&queue->query_handled, &queue->qlock);
930 if (queue->srcresult != GST_FLOW_OK)
932 res = queue->last_query;
933 GST_QUEUE_MUTEX_UNLOCK (queue);
935 res = gst_pad_query_default (pad, parent, query);
944 GST_DEBUG_OBJECT (queue, "we are flushing");
945 GST_QUEUE_MUTEX_UNLOCK (queue);
951 gst_queue_is_empty (GstQueue * queue)
955 if (gst_queue_array_is_empty (queue->queue))
958 /* Only consider the queue empty if the minimum thresholds
959 * are not reached and data is at the queue head. Otherwise
960 * we would block forever on serialized queries.
962 head = gst_queue_array_peek_head (queue->queue);
963 if (!GST_IS_BUFFER (head->item) && !GST_IS_BUFFER_LIST (head->item))
966 /* It is possible that a max size is reached before all min thresholds are.
967 * Therefore, only consider it empty if it is not filled. */
968 return ((queue->min_threshold.buffers > 0 &&
969 queue->cur_level.buffers < queue->min_threshold.buffers) ||
970 (queue->min_threshold.bytes > 0 &&
971 queue->cur_level.bytes < queue->min_threshold.bytes) ||
972 (queue->min_threshold.time > 0 &&
973 queue->cur_level.time < queue->min_threshold.time)) &&
974 !gst_queue_is_filled (queue);
978 gst_queue_is_filled (GstQueue * queue)
980 return (((queue->max_size.buffers > 0 &&
981 queue->cur_level.buffers >= queue->max_size.buffers) ||
982 (queue->max_size.bytes > 0 &&
983 queue->cur_level.bytes >= queue->max_size.bytes) ||
984 (queue->max_size.time > 0 &&
985 queue->cur_level.time >= queue->max_size.time)));
989 gst_queue_leak_downstream (GstQueue * queue)
991 /* for as long as the queue is filled, dequeue an item and discard it */
992 while (gst_queue_is_filled (queue)) {
995 leak = gst_queue_locked_dequeue (queue);
996 /* there is nothing to dequeue and the queue is still filled.. This should
998 g_assert (leak != NULL);
1000 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1001 "queue is full, leaking item %p on downstream end", leak);
1002 if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
1003 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1004 "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
1005 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
1008 if (!GST_IS_QUERY (leak))
1009 gst_mini_object_unref (leak);
1011 /* last buffer needs to get a DISCONT flag */
1012 queue->head_needs_discont = TRUE;
1016 static GstFlowReturn
1017 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1020 GstClockTime duration, timestamp;
1022 queue = GST_QUEUE_CAST (parent);
1024 /* we have to lock the queue since we span threads */
1025 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1026 /* when we received EOS, we refuse any more data */
1029 if (queue->unexpected)
1030 goto out_unexpected;
1032 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1033 duration = GST_BUFFER_DURATION (buffer);
1035 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
1036 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1037 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1038 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1040 /* We make space available if we're "full" according to whatever
1041 * the user defined as "full". Note that this only applies to buffers.
1042 * We always handle events and they don't count in our statistics. */
1043 while (gst_queue_is_filled (queue)) {
1044 if (!queue->silent) {
1045 GST_QUEUE_MUTEX_UNLOCK (queue);
1046 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
1047 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1048 /* we recheck, the signal could have changed the thresholds */
1049 if (!gst_queue_is_filled (queue))
1053 /* how are we going to make space for this buffer? */
1054 switch (queue->leaky) {
1055 case GST_QUEUE_LEAK_UPSTREAM:
1056 /* next buffer needs to get a DISCONT flag */
1057 queue->tail_needs_discont = TRUE;
1058 /* leak current buffer */
1059 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1060 "queue is full, leaking buffer on upstream end");
1061 /* now we can clean up and exit right away */
1063 case GST_QUEUE_LEAK_DOWNSTREAM:
1064 gst_queue_leak_downstream (queue);
1067 g_warning ("Unknown leaky type, using default");
1069 case GST_QUEUE_NO_LEAK:
1071 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1072 "queue is full, waiting for free space");
1074 /* don't leak. Instead, wait for space to be available */
1076 /* for as long as the queue is filled, wait till an item was deleted. */
1077 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1078 } while (gst_queue_is_filled (queue));
1080 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1082 if (!queue->silent) {
1083 GST_QUEUE_MUTEX_UNLOCK (queue);
1084 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1085 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1092 if (queue->tail_needs_discont) {
1093 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1097 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1099 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1101 queue->tail_needs_discont = FALSE;
1104 /* put buffer in queue now */
1105 gst_queue_locked_enqueue_buffer (queue, buffer);
1106 GST_QUEUE_MUTEX_UNLOCK (queue);
1110 /* special conditions */
1113 GST_QUEUE_MUTEX_UNLOCK (queue);
1115 gst_buffer_unref (buffer);
1121 GstFlowReturn ret = queue->srcresult;
1123 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1124 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1125 GST_QUEUE_MUTEX_UNLOCK (queue);
1126 gst_buffer_unref (buffer);
1132 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1133 GST_QUEUE_MUTEX_UNLOCK (queue);
1135 gst_buffer_unref (buffer);
1137 return GST_FLOW_EOS;
1141 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1142 GST_QUEUE_MUTEX_UNLOCK (queue);
1144 gst_buffer_unref (buffer);
1146 return GST_FLOW_EOS;
1150 /* dequeue an item from the queue an push it downstream. This functions returns
1151 * the result of the push. */
1152 static GstFlowReturn
1153 gst_queue_push_one (GstQueue * queue)
1155 GstFlowReturn result = queue->srcresult;
1156 GstMiniObject *data;
1158 data = gst_queue_locked_dequeue (queue);
1163 if (GST_IS_BUFFER (data)) {
1166 buffer = GST_BUFFER_CAST (data);
1168 if (queue->head_needs_discont) {
1169 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1173 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1175 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1177 queue->head_needs_discont = FALSE;
1180 GST_QUEUE_MUTEX_UNLOCK (queue);
1181 result = gst_pad_push (queue->srcpad, buffer);
1183 /* need to check for srcresult here as well */
1184 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1186 if (result == GST_FLOW_EOS) {
1187 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1188 /* stop pushing buffers, we dequeue all items until we see an item that we
1189 * can push again, which is EOS or SEGMENT. If there is nothing in the
1190 * queue we can push, we set a flag to make the sinkpad refuse more
1191 * buffers with an EOS return value. */
1192 while ((data = gst_queue_locked_dequeue (queue))) {
1193 if (GST_IS_BUFFER (data)) {
1194 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1195 "dropping EOS buffer %p", data);
1196 gst_buffer_unref (GST_BUFFER_CAST (data));
1197 } else if (GST_IS_EVENT (data)) {
1198 GstEvent *event = GST_EVENT_CAST (data);
1199 GstEventType type = GST_EVENT_TYPE (event);
1201 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
1202 /* we found a pushable item in the queue, push it out */
1203 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1204 "pushing pushable event %s after EOS",
1205 GST_EVENT_TYPE_NAME (event));
1208 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1209 "dropping EOS event %p", event);
1210 gst_event_unref (event);
1211 } else if (GST_IS_QUERY (data)) {
1212 GstQuery *query = GST_QUERY_CAST (data);
1214 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1215 "dropping query %p because of EOS", query);
1216 queue->last_query = FALSE;
1217 g_cond_signal (&queue->query_handled);
1220 /* no more items in the queue. Set the unexpected flag so that upstream
1221 * make us refuse any more buffers on the sinkpad. Since we will still
1222 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1223 * task function does not shut down. */
1224 queue->unexpected = TRUE;
1225 result = GST_FLOW_OK;
1227 } else if (GST_IS_EVENT (data)) {
1228 GstEvent *event = GST_EVENT_CAST (data);
1229 GstEventType type = GST_EVENT_TYPE (event);
1231 GST_QUEUE_MUTEX_UNLOCK (queue);
1233 gst_pad_push_event (queue->srcpad, event);
1235 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1236 /* if we're EOS, return EOS so that the task pauses. */
1237 if (type == GST_EVENT_EOS) {
1238 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1239 "pushed EOS event %p, return EOS", event);
1240 result = GST_FLOW_EOS;
1242 } else if (GST_IS_QUERY (data)) {
1243 GstQuery *query = GST_QUERY_CAST (data);
1246 GST_QUEUE_MUTEX_UNLOCK (queue);
1247 ret = gst_pad_peer_query (queue->srcpad, query);
1248 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1249 queue->last_query = ret;
1250 g_cond_signal (&queue->query_handled);
1251 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1252 "did query %p, return %d", query, queue->last_query);
1259 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1260 "exit because we have no item in the queue");
1261 return GST_FLOW_ERROR;
1265 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1266 return GST_FLOW_FLUSHING;
1270 queue->last_query = FALSE;
1271 g_cond_signal (&queue->query_handled);
1272 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1273 return GST_FLOW_FLUSHING;
1278 gst_queue_loop (GstPad * pad)
1283 queue = (GstQueue *) GST_PAD_PARENT (pad);
1285 /* have to lock for thread-safety */
1286 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1288 while (gst_queue_is_empty (queue)) {
1289 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1290 if (!queue->silent) {
1291 GST_QUEUE_MUTEX_UNLOCK (queue);
1292 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1293 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1296 /* we recheck, the signal could have changed the thresholds */
1297 while (gst_queue_is_empty (queue)) {
1298 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1301 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1302 if (!queue->silent) {
1303 GST_QUEUE_MUTEX_UNLOCK (queue);
1304 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1305 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1306 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1310 ret = gst_queue_push_one (queue);
1311 queue->srcresult = ret;
1312 if (ret != GST_FLOW_OK)
1315 GST_QUEUE_MUTEX_UNLOCK (queue);
1322 gboolean eos = queue->eos;
1323 GstFlowReturn ret = queue->srcresult;
1325 gst_pad_pause_task (queue->srcpad);
1326 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1327 "pause task, reason: %s", gst_flow_get_name (ret));
1328 if (ret == GST_FLOW_FLUSHING)
1329 gst_queue_locked_flush (queue, FALSE);
1331 GST_QUEUE_SIGNAL_DEL (queue);
1332 GST_QUEUE_MUTEX_UNLOCK (queue);
1333 /* let app know about us giving up if upstream is not expected to do so */
1334 /* EOS is already taken care of elsewhere */
1335 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1336 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
1337 (_("Internal data flow error.")),
1338 ("streaming task paused, reason %s (%d)",
1339 gst_flow_get_name (ret), ret));
1340 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1347 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1349 gboolean res = TRUE;
1350 GstQueue *queue = GST_QUEUE (parent);
1352 #ifndef GST_DISABLE_GST_DEBUG
1353 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1354 event, GST_EVENT_TYPE (event));
1357 switch (GST_EVENT_TYPE (event)) {
1358 case GST_EVENT_RECONFIGURE:
1359 GST_QUEUE_MUTEX_LOCK (queue);
1360 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1361 /* when we got not linked, assume downstream is linked again now and we
1362 * can try to start pushing again */
1363 queue->srcresult = GST_FLOW_OK;
1364 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1366 GST_QUEUE_MUTEX_UNLOCK (queue);
1368 res = gst_pad_push_event (queue->sinkpad, event);
1371 res = gst_pad_event_default (pad, parent, event);
1380 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1382 GstQueue *queue = GST_QUEUE (parent);
1385 switch (GST_QUERY_TYPE (query)) {
1386 case GST_QUERY_SCHEDULING:{
1387 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1392 res = gst_pad_query_default (pad, parent, query);
1399 /* Adjust peer response for data contained in queue */
1400 switch (GST_QUERY_TYPE (query)) {
1401 case GST_QUERY_POSITION:
1406 /* get peer position */
1407 gst_query_parse_position (query, &format, &peer_pos);
1409 /* FIXME: this code assumes that there's no discont in the queue */
1411 case GST_FORMAT_BYTES:
1412 peer_pos -= queue->cur_level.bytes;
1414 case GST_FORMAT_TIME:
1415 peer_pos -= queue->cur_level.time;
1418 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1419 "know how to adjust value", gst_format_get_name (format));
1422 /* set updated position */
1423 gst_query_set_position (query, format, peer_pos);
1426 case GST_QUERY_LATENCY:
1429 GstClockTime min, max;
1431 gst_query_parse_latency (query, &live, &min, &max);
1433 /* we can delay up to the limit of the queue in time. If we have no time
1434 * limit, the best thing we can do is to return an infinite delay. In
1435 * reality a better estimate would be the byte/buffer rate but that is not
1436 * possible right now. */
1437 if (queue->max_size.time > 0 && max != -1)
1438 max += queue->max_size.time;
1442 /* adjust for min-threshold */
1443 if (queue->min_threshold.time > 0 && min != -1)
1444 min += queue->min_threshold.time;
1446 gst_query_set_latency (query, live, min, max);
1450 /* peer handled other queries */
1458 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1464 queue = GST_QUEUE (parent);
1467 case GST_PAD_MODE_PUSH:
1469 GST_QUEUE_MUTEX_LOCK (queue);
1470 queue->srcresult = GST_FLOW_OK;
1472 queue->unexpected = FALSE;
1473 GST_QUEUE_MUTEX_UNLOCK (queue);
1475 /* step 1, unblock chain function */
1476 GST_QUEUE_MUTEX_LOCK (queue);
1477 queue->srcresult = GST_FLOW_FLUSHING;
1478 /* the item del signal will unblock */
1479 g_cond_signal (&queue->item_del);
1480 /* unblock query handler */
1481 queue->last_query = FALSE;
1482 g_cond_signal (&queue->query_handled);
1483 GST_QUEUE_MUTEX_UNLOCK (queue);
1485 /* step 2, wait until streaming thread stopped and flush queue */
1486 GST_PAD_STREAM_LOCK (pad);
1487 GST_QUEUE_MUTEX_LOCK (queue);
1488 gst_queue_locked_flush (queue, TRUE);
1489 GST_QUEUE_MUTEX_UNLOCK (queue);
1490 GST_PAD_STREAM_UNLOCK (pad);
1502 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1508 queue = GST_QUEUE (parent);
1511 case GST_PAD_MODE_PUSH:
1513 GST_QUEUE_MUTEX_LOCK (queue);
1514 queue->srcresult = GST_FLOW_OK;
1516 queue->unexpected = FALSE;
1518 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1520 GST_QUEUE_MUTEX_UNLOCK (queue);
1522 /* step 1, unblock loop function */
1523 GST_QUEUE_MUTEX_LOCK (queue);
1524 queue->srcresult = GST_FLOW_FLUSHING;
1525 /* the item add signal will unblock */
1526 g_cond_signal (&queue->item_add);
1527 GST_QUEUE_MUTEX_UNLOCK (queue);
1529 /* step 2, make sure streaming finishes */
1530 result = gst_pad_stop_task (pad);
1541 queue_capacity_change (GstQueue * queue)
1543 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1544 gst_queue_leak_downstream (queue);
1547 /* changing the capacity of the queue must wake up
1548 * the _chain function, it might have more room now
1549 * to store the buffer/event in the queue */
1550 GST_QUEUE_SIGNAL_DEL (queue);
1553 /* Changing the minimum required fill level must
1554 * wake up the _loop function as it might now
1555 * be able to preceed.
1557 #define QUEUE_THRESHOLD_CHANGE(q)\
1558 GST_QUEUE_SIGNAL_ADD (q);
1561 gst_queue_set_property (GObject * object,
1562 guint prop_id, const GValue * value, GParamSpec * pspec)
1564 GstQueue *queue = GST_QUEUE (object);
1566 /* someone could change levels here, and since this
1567 * affects the get/put funcs, we need to lock for safety. */
1568 GST_QUEUE_MUTEX_LOCK (queue);
1571 case PROP_MAX_SIZE_BYTES:
1572 queue->max_size.bytes = g_value_get_uint (value);
1573 queue_capacity_change (queue);
1575 case PROP_MAX_SIZE_BUFFERS:
1576 queue->max_size.buffers = g_value_get_uint (value);
1577 queue_capacity_change (queue);
1579 case PROP_MAX_SIZE_TIME:
1580 queue->max_size.time = g_value_get_uint64 (value);
1581 queue_capacity_change (queue);
1583 case PROP_MIN_THRESHOLD_BYTES:
1584 queue->min_threshold.bytes = g_value_get_uint (value);
1585 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1586 QUEUE_THRESHOLD_CHANGE (queue);
1588 case PROP_MIN_THRESHOLD_BUFFERS:
1589 queue->min_threshold.buffers = g_value_get_uint (value);
1590 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1591 QUEUE_THRESHOLD_CHANGE (queue);
1593 case PROP_MIN_THRESHOLD_TIME:
1594 queue->min_threshold.time = g_value_get_uint64 (value);
1595 queue->orig_min_threshold.time = queue->min_threshold.time;
1596 QUEUE_THRESHOLD_CHANGE (queue);
1599 queue->leaky = g_value_get_enum (value);
1602 queue->silent = g_value_get_boolean (value);
1604 case PROP_FLUSH_ON_EOS:
1605 queue->flush_on_eos = g_value_get_boolean (value);
1608 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1612 GST_QUEUE_MUTEX_UNLOCK (queue);
1616 gst_queue_get_property (GObject * object,
1617 guint prop_id, GValue * value, GParamSpec * pspec)
1619 GstQueue *queue = GST_QUEUE (object);
1621 GST_QUEUE_MUTEX_LOCK (queue);
1624 case PROP_CUR_LEVEL_BYTES:
1625 g_value_set_uint (value, queue->cur_level.bytes);
1627 case PROP_CUR_LEVEL_BUFFERS:
1628 g_value_set_uint (value, queue->cur_level.buffers);
1630 case PROP_CUR_LEVEL_TIME:
1631 g_value_set_uint64 (value, queue->cur_level.time);
1633 case PROP_MAX_SIZE_BYTES:
1634 g_value_set_uint (value, queue->max_size.bytes);
1636 case PROP_MAX_SIZE_BUFFERS:
1637 g_value_set_uint (value, queue->max_size.buffers);
1639 case PROP_MAX_SIZE_TIME:
1640 g_value_set_uint64 (value, queue->max_size.time);
1642 case PROP_MIN_THRESHOLD_BYTES:
1643 g_value_set_uint (value, queue->min_threshold.bytes);
1645 case PROP_MIN_THRESHOLD_BUFFERS:
1646 g_value_set_uint (value, queue->min_threshold.buffers);
1648 case PROP_MIN_THRESHOLD_TIME:
1649 g_value_set_uint64 (value, queue->min_threshold.time);
1652 g_value_set_enum (value, queue->leaky);
1655 g_value_set_boolean (value, queue->silent);
1657 case PROP_FLUSH_ON_EOS:
1658 g_value_set_boolean (value, queue->flush_on_eos);
1661 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1665 GST_QUEUE_MUTEX_UNLOCK (queue);