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.
27 * @short_description: Simple asynchronous data queue.
29 * Data is queued till max_level buffers have been stored. Any subsequent
30 * buffers sent to this filter will block until free space becomes available in
31 * the buffer. The queue is typically used in conjunction with a thread.
33 * You can query how many buffers are queued with the level argument.
35 * The default queue length is set to 100.
37 * The queue blocks by default.
40 #include "gst_private.h"
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
53 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
59 #define GST_CAT_DEFAULT (queue_dataflow)
61 #define STATUS(queue, msg) \
62 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
63 "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
64 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
65 "-%" G_GUINT64_FORMAT " ns, %u elements", \
66 GST_DEBUG_PAD_NAME (pad), \
67 queue->cur_level.buffers, \
68 queue->min_threshold.buffers, \
69 queue->max_size.buffers, \
70 queue->cur_level.bytes, \
71 queue->min_threshold.bytes, \
72 queue->max_size.bytes, \
73 queue->cur_level.time, \
74 queue->min_threshold.time, \
75 queue->max_size.time, \
78 static GstElementDetails gst_queue_details = GST_ELEMENT_DETAILS ("Queue",
81 "Erik Walthinsen <omega@cse.ogi.edu>");
84 /* Queue signals and args */
96 /* FIXME: don't we have another way of doing this
97 * "Gstreamer format" (frame/byte/time) queries? */
98 ARG_CUR_LEVEL_BUFFERS,
101 ARG_MAX_SIZE_BUFFERS,
104 ARG_MIN_THRESHOLD_BUFFERS,
105 ARG_MIN_THRESHOLD_BYTES,
106 ARG_MIN_THRESHOLD_TIME,
111 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
112 GST_CAT_LOG_OBJECT (queue_dataflow, q, \
113 "locking qlock from thread %p", \
115 g_mutex_lock (q->qlock); \
116 GST_CAT_LOG_OBJECT (queue_dataflow, q, \
117 "locked qlock from thread %p", \
121 #define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START { \
122 GST_QUEUE_MUTEX_LOCK (q); \
123 if (q->srcresult != GST_FLOW_OK) \
127 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
128 GST_CAT_LOG_OBJECT (queue_dataflow, q, \
129 "unlocking qlock from thread %p", \
131 g_mutex_unlock (q->qlock); \
135 static void gst_queue_base_init (GstQueueClass * klass);
136 static void gst_queue_class_init (GstQueueClass * klass);
137 static void gst_queue_init (GstQueue * queue);
138 static void gst_queue_finalize (GObject * object);
140 static void gst_queue_set_property (GObject * object,
141 guint prop_id, const GValue * value, GParamSpec * pspec);
142 static void gst_queue_get_property (GObject * object,
143 guint prop_id, GValue * value, GParamSpec * pspec);
145 static GstFlowReturn gst_queue_chain (GstPad * pad, GstBuffer * buffer);
146 static GstFlowReturn gst_queue_bufferalloc (GstPad * pad, guint64 offset,
147 guint size, GstCaps * caps, GstBuffer ** buf);
148 static void gst_queue_loop (GstPad * pad);
150 static gboolean gst_queue_handle_sink_event (GstPad * pad, GstEvent * event);
152 static gboolean gst_queue_handle_src_event (GstPad * pad, GstEvent * event);
153 static gboolean gst_queue_handle_src_query (GstPad * pad, GstQuery * query);
155 static GstCaps *gst_queue_getcaps (GstPad * pad);
156 static GstPadLinkReturn gst_queue_link_sink (GstPad * pad, GstPad * peer);
157 static GstPadLinkReturn gst_queue_link_src (GstPad * pad, GstPad * peer);
158 static void gst_queue_locked_flush (GstQueue * queue);
160 static gboolean gst_queue_src_activate_push (GstPad * pad, gboolean active);
161 static gboolean gst_queue_sink_activate_push (GstPad * pad, gboolean active);
162 static GstStateChangeReturn gst_queue_change_state (GstElement * element,
163 GstStateChange transition);
166 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
169 queue_leaky_get_type (void)
171 static GType queue_leaky_type = 0;
172 static GEnumValue queue_leaky[] = {
173 {GST_QUEUE_NO_LEAK, "0", "Not Leaky"},
174 {GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream"},
175 {GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream"},
179 if (!queue_leaky_type) {
180 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
182 return queue_leaky_type;
185 static GstElementClass *parent_class = NULL;
186 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
189 gst_queue_get_type (void)
191 static GType queue_type = 0;
194 static const GTypeInfo queue_info = {
195 sizeof (GstQueueClass),
196 (GBaseInitFunc) gst_queue_base_init,
198 (GClassInitFunc) gst_queue_class_init,
203 (GInstanceInitFunc) gst_queue_init,
207 queue_type = g_type_register_static (GST_TYPE_ELEMENT,
208 "GstQueue", &queue_info, 0);
209 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0,
210 "dataflow inside the queue element");
217 gst_queue_base_init (GstQueueClass * klass)
219 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
221 gst_element_class_add_pad_template (gstelement_class,
222 gst_static_pad_template_get (&srctemplate));
223 gst_element_class_add_pad_template (gstelement_class,
224 gst_static_pad_template_get (&sinktemplate));
225 gst_element_class_set_details (gstelement_class, &gst_queue_details);
229 gst_queue_class_init (GstQueueClass * klass)
231 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
232 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
234 parent_class = g_type_class_peek_parent (klass);
236 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_queue_set_property);
237 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_queue_get_property);
241 * GstQueue::underrun:
242 * @queue: the queue instance
244 * Reports that the buffer became empty (underrun).
245 * A buffer is empty if the total amount of data inside it (num-buffers, time,
246 * size) is lower than the boundary values which can be set through the GObject
249 gst_queue_signals[SIGNAL_UNDERRUN] =
250 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
251 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
252 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
255 * @queue: the queue instance
257 * Reports that enough (min-threshold) data is in the queue. Use this signal
258 * together with the underrun signal to pause the pipeline on underrun and wait
259 * for the queue to fill-up before resume playback.
261 gst_queue_signals[SIGNAL_RUNNING] =
262 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
263 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
264 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
267 * @queue: the queue instance
269 * Reports that the buffer became full (overrun).
270 * A buffer is full if the total amount of data inside it (num-buffers, time,
271 * size) is higher than the boundary values which can be set through the GObject
274 gst_queue_signals[SIGNAL_OVERRUN] =
275 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
276 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
277 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
280 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_BYTES,
281 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
282 "Current amount of data in the queue (bytes)",
283 0, G_MAXUINT, 0, G_PARAM_READABLE));
284 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_BUFFERS,
285 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
286 "Current number of buffers in the queue",
287 0, G_MAXUINT, 0, G_PARAM_READABLE));
288 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_TIME,
289 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
290 "Current amount of data in the queue (in ns)",
291 0, G_MAXUINT64, 0, G_PARAM_READABLE));
293 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BYTES,
294 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
295 "Max. amount of data in the queue (bytes, 0=disable)",
296 0, G_MAXUINT, 0, G_PARAM_READWRITE));
297 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BUFFERS,
298 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
299 "Max. number of buffers in the queue (0=disable)",
300 0, G_MAXUINT, 0, G_PARAM_READWRITE));
301 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_TIME,
302 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
303 "Max. amount of data in the queue (in ns, 0=disable)",
304 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
306 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_BYTES,
307 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
308 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
309 0, G_MAXUINT, 0, G_PARAM_READWRITE));
310 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_BUFFERS,
311 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
312 "Min. number of buffers in the queue to allow reading (0=disable)",
313 0, G_MAXUINT, 0, G_PARAM_READWRITE));
314 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_TIME,
315 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
316 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
317 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
319 g_object_class_install_property (gobject_class, ARG_LEAKY,
320 g_param_spec_enum ("leaky", "Leaky",
321 "Where the queue leaks, if at all",
322 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK, G_PARAM_READWRITE));
324 /* set several parent class virtual functions */
325 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_queue_finalize);
327 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue_change_state);
331 gst_queue_init (GstQueue * queue)
334 gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate),
336 gst_pad_set_chain_function (queue->sinkpad,
337 GST_DEBUG_FUNCPTR (gst_queue_chain));
338 gst_pad_set_activatepush_function (queue->sinkpad,
339 GST_DEBUG_FUNCPTR (gst_queue_sink_activate_push));
340 gst_pad_set_event_function (queue->sinkpad,
341 GST_DEBUG_FUNCPTR (gst_queue_handle_sink_event));
342 gst_pad_set_link_function (queue->sinkpad,
343 GST_DEBUG_FUNCPTR (gst_queue_link_sink));
344 gst_pad_set_getcaps_function (queue->sinkpad,
345 GST_DEBUG_FUNCPTR (gst_queue_getcaps));
346 gst_pad_set_bufferalloc_function (queue->sinkpad,
347 GST_DEBUG_FUNCPTR (gst_queue_bufferalloc));
348 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
351 gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate),
353 gst_pad_set_activatepush_function (queue->srcpad,
354 GST_DEBUG_FUNCPTR (gst_queue_src_activate_push));
355 gst_pad_set_link_function (queue->srcpad,
356 GST_DEBUG_FUNCPTR (gst_queue_link_src));
357 gst_pad_set_getcaps_function (queue->srcpad,
358 GST_DEBUG_FUNCPTR (gst_queue_getcaps));
359 gst_pad_set_event_function (queue->srcpad,
360 GST_DEBUG_FUNCPTR (gst_queue_handle_src_event));
361 gst_pad_set_query_function (queue->srcpad,
362 GST_DEBUG_FUNCPTR (gst_queue_handle_src_query));
363 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
365 queue->cur_level.buffers = 0; /* no content */
366 queue->cur_level.bytes = 0; /* no content */
367 queue->cur_level.time = 0; /* no content */
368 queue->max_size.buffers = 200; /* 200 buffers */
369 queue->max_size.bytes = 10 * 1024 * 1024; /* 10 MB */
370 queue->max_size.time = GST_SECOND; /* 1 s. */
371 queue->min_threshold.buffers = 0; /* no threshold */
372 queue->min_threshold.bytes = 0; /* no threshold */
373 queue->min_threshold.time = 0; /* no threshold */
375 queue->leaky = GST_QUEUE_NO_LEAK;
376 queue->srcresult = GST_FLOW_WRONG_STATE;
378 queue->qlock = g_mutex_new ();
379 queue->item_add = g_cond_new ();
380 queue->item_del = g_cond_new ();
381 queue->queue = g_queue_new ();
383 GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue,
384 "initialized queue's not_empty & not_full conditions");
387 /* called only once, as opposed to dispose */
389 gst_queue_finalize (GObject * object)
391 GstQueue *queue = GST_QUEUE (object);
393 GST_DEBUG_OBJECT (queue, "finalizing queue");
395 while (!g_queue_is_empty (queue->queue)) {
396 GstMiniObject *data = g_queue_pop_head (queue->queue);
398 gst_mini_object_unref (data);
400 g_queue_free (queue->queue);
401 GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue, "free mutex");
402 g_mutex_free (queue->qlock);
403 GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue, "done free mutex");
404 g_cond_free (queue->item_add);
405 g_cond_free (queue->item_del);
407 if (G_OBJECT_CLASS (parent_class)->finalize)
408 G_OBJECT_CLASS (parent_class)->finalize (object);
412 gst_queue_getcaps (GstPad * pad)
418 queue = GST_QUEUE (GST_PAD_PARENT (pad));
420 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
421 result = gst_pad_peer_get_caps (otherpad);
423 result = gst_caps_new_any ();
428 static GstPadLinkReturn
429 gst_queue_link_sink (GstPad * pad, GstPad * peer)
431 return GST_PAD_LINK_OK;
434 static GstPadLinkReturn
435 gst_queue_link_src (GstPad * pad, GstPad * peer)
437 GstPadLinkReturn result = GST_PAD_LINK_OK;
440 queue = GST_QUEUE (gst_pad_get_parent (pad));
442 GST_DEBUG ("queue linking source pad");
444 if (GST_PAD_LINKFUNC (peer)) {
445 result = GST_PAD_LINKFUNC (peer) (peer, pad);
448 if (GST_PAD_LINK_SUCCESSFUL (result)) {
449 GST_QUEUE_MUTEX_LOCK (queue);
450 if (queue->srcresult == GST_FLOW_OK) {
451 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad);
452 GST_DEBUG ("starting task as pad is linked");
454 GST_DEBUG ("not starting task reason %s",
455 gst_flow_get_name (queue->srcresult));
457 GST_QUEUE_MUTEX_UNLOCK (queue);
459 gst_object_unref (queue);
465 gst_queue_bufferalloc (GstPad * pad, guint64 offset, guint size, GstCaps * caps,
469 GstFlowReturn result;
471 queue = GST_QUEUE (GST_PAD_PARENT (pad));
473 result = gst_pad_alloc_buffer (queue->srcpad, offset, size, caps, buf);
480 gst_queue_locked_flush (GstQueue * queue)
482 while (!g_queue_is_empty (queue->queue)) {
483 GstMiniObject *data = g_queue_pop_head (queue->queue);
485 /* Then loose another reference because we are supposed to destroy that
486 data when flushing */
487 gst_mini_object_unref (data);
489 queue->cur_level.buffers = 0;
490 queue->cur_level.bytes = 0;
491 queue->cur_level.time = 0;
493 /* we deleted something... */
494 g_cond_signal (queue->item_del);
497 #define STATUS(queue, msg) \
498 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
499 "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
500 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
501 "-%" G_GUINT64_FORMAT " ns, %u elements", \
502 GST_DEBUG_PAD_NAME (pad), \
503 queue->cur_level.buffers, \
504 queue->min_threshold.buffers, \
505 queue->max_size.buffers, \
506 queue->cur_level.bytes, \
507 queue->min_threshold.bytes, \
508 queue->max_size.bytes, \
509 queue->cur_level.time, \
510 queue->min_threshold.time, \
511 queue->max_size.time, \
512 queue->queue->length)
515 gst_queue_handle_sink_event (GstPad * pad, GstEvent * event)
519 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
521 switch (GST_EVENT_TYPE (event)) {
522 case GST_EVENT_FLUSH_START:
523 STATUS (queue, "received flush start event");
524 /* forward event, re first as we're going to use it still */
525 gst_event_ref (event);
526 gst_pad_push_event (queue->srcpad, event);
528 /* now unblock the chain function */
529 GST_QUEUE_MUTEX_LOCK (queue);
530 queue->srcresult = GST_FLOW_WRONG_STATE;
531 /* unblock the loop and chain functions */
532 g_cond_signal (queue->item_add);
533 g_cond_signal (queue->item_del);
534 GST_QUEUE_MUTEX_UNLOCK (queue);
536 /* make sure it pauses */
537 gst_pad_pause_task (queue->srcpad);
538 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
539 gst_event_unref (event);
541 case GST_EVENT_FLUSH_STOP:
542 STATUS (queue, "received flush stop event");
543 /* forward event, re first as we're going to use it still */
544 gst_event_ref (event);
545 gst_pad_push_event (queue->srcpad, event);
547 GST_QUEUE_MUTEX_LOCK (queue);
548 gst_queue_locked_flush (queue);
549 queue->srcresult = GST_FLOW_OK;
550 if (gst_pad_is_linked (queue->srcpad)) {
551 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
554 GST_DEBUG_OBJECT (queue, "not re-starting task as pad is not linked");
556 GST_QUEUE_MUTEX_UNLOCK (queue);
558 STATUS (queue, "after flush");
559 gst_event_unref (event);
562 STATUS (queue, "received EOS");
565 if (GST_EVENT_IS_SERIALIZED (event)) {
566 /* we put the event in the queue, we don't have to act ourselves */
567 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
568 "adding event %p of type %d", event, GST_EVENT_TYPE (event));
570 gst_event_ref (event);
571 gst_pad_push_event (queue->srcpad, event);
572 gst_event_unref (event);
578 GST_QUEUE_MUTEX_LOCK (queue);
579 g_queue_push_tail (queue->queue, event);
580 g_cond_signal (queue->item_add);
581 GST_QUEUE_MUTEX_UNLOCK (queue);
589 gst_queue_is_empty (GstQueue * queue)
591 return (queue->queue->length == 0 ||
592 (queue->min_threshold.buffers > 0 &&
593 queue->cur_level.buffers < queue->min_threshold.buffers) ||
594 (queue->min_threshold.bytes > 0 &&
595 queue->cur_level.bytes < queue->min_threshold.bytes) ||
596 (queue->min_threshold.time > 0 &&
597 queue->cur_level.time < queue->min_threshold.time));
601 gst_queue_is_filled (GstQueue * queue)
603 return (((queue->max_size.buffers > 0 &&
604 queue->cur_level.buffers >= queue->max_size.buffers) ||
605 (queue->max_size.bytes > 0 &&
606 queue->cur_level.bytes >= queue->max_size.bytes) ||
607 (queue->max_size.time > 0 &&
608 queue->cur_level.time >= queue->max_size.time)));
613 gst_queue_chain (GstPad * pad, GstBuffer * buffer)
617 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
619 /* we have to lock the queue since we span threads */
620 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
622 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
623 "adding buffer %p of size %d", buffer, GST_BUFFER_SIZE (buffer));
625 /* We make space available if we're "full" according to whatever
626 * the user defined as "full". Note that this only applies to buffers.
627 * We always handle events and they don't count in our statistics. */
628 while (gst_queue_is_filled (queue)) {
629 GST_QUEUE_MUTEX_UNLOCK (queue);
630 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_OVERRUN], 0);
631 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
633 /* how are we going to make space for this buffer? */
634 switch (queue->leaky) {
635 /* leak current buffer */
636 case GST_QUEUE_LEAK_UPSTREAM:
637 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
638 "queue is full, leaking buffer on upstream end");
639 /* now we can clean up and exit right away */
642 /* leak first buffer in the queue */
643 case GST_QUEUE_LEAK_DOWNSTREAM:{
644 /* this is a bit hacky. We'll manually iterate the list
645 * and find the first buffer from the head on. We'll
646 * unref that and "fix up" the GQueue object... */
648 GstMiniObject *leak = NULL;
650 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
651 "queue is full, leaking buffer on downstream end");
653 for (item = queue->queue->head; item != NULL; item = item->next) {
654 if (GST_IS_BUFFER (item->data)) {
660 /* if we didn't find anything, it means we have no buffers
661 * in here. That cannot happen, since we had >= 1 bufs */
664 /* Now remove it from the list, fixing up the GQueue
665 * CHECKME: is a queue->head the first or the last item? */
666 item = g_list_delete_link (queue->queue->head, item);
667 queue->queue->head = g_list_first (item);
668 queue->queue->tail = g_list_last (item);
669 queue->queue->length--;
671 /* and unref the buffer at the end. Twice, because we keep a ref
672 * to make things read-only. Also keep our list uptodate. */
673 queue->cur_level.bytes -= GST_BUFFER_SIZE (buffer);
674 queue->cur_level.buffers--;
675 if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE)
676 queue->cur_level.time -= GST_BUFFER_DURATION (buffer);
678 gst_buffer_unref (buffer);
679 gst_buffer_unref (buffer);
684 g_warning ("Unknown leaky type, using default");
687 /* don't leak. Instead, wait for space to be available */
688 case GST_QUEUE_NO_LEAK:
689 STATUS (queue, "pre-full wait");
691 while (gst_queue_is_filled (queue)) {
692 STATUS (queue, "waiting for item_del signal from thread using qlock");
693 g_cond_wait (queue->item_del, queue->qlock);
695 if (queue->srcresult != GST_FLOW_OK)
698 /* if there's a pending state change for this queue
699 * or its manager, switch back to iterator so bottom
700 * half of state change executes */
701 STATUS (queue, "received item_del signal from thread using qlock");
704 STATUS (queue, "post-full wait");
705 GST_QUEUE_MUTEX_UNLOCK (queue);
706 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
707 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
713 g_queue_push_tail (queue->queue, buffer);
715 /* add buffer to the statistics */
716 queue->cur_level.buffers++;
717 queue->cur_level.bytes += GST_BUFFER_SIZE (buffer);
718 if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE)
719 queue->cur_level.time += GST_BUFFER_DURATION (buffer);
721 STATUS (queue, "+ level");
723 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "signalling item_add");
724 g_cond_signal (queue->item_add);
725 GST_QUEUE_MUTEX_UNLOCK (queue);
729 /* special conditions */
732 GST_QUEUE_MUTEX_UNLOCK (queue);
734 gst_buffer_unref (buffer);
740 GstFlowReturn ret = queue->srcresult;
741 const gchar *flowname = gst_flow_get_name (ret);
743 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
744 "exit because task paused, reason: %s", flowname);
745 GST_QUEUE_MUTEX_UNLOCK (queue);
747 gst_buffer_unref (buffer);
754 gst_queue_loop (GstPad * pad)
758 gboolean restart = TRUE;
760 queue = GST_QUEUE (GST_PAD_PARENT (pad));
762 /* have to lock for thread-safety */
763 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
766 while (gst_queue_is_empty (queue)) {
767 GST_QUEUE_MUTEX_UNLOCK (queue);
768 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_UNDERRUN], 0);
769 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
771 STATUS (queue, "pre-empty wait");
772 while (gst_queue_is_empty (queue)) {
773 STATUS (queue, "waiting for item_add");
775 GST_LOG_OBJECT (queue, "doing g_cond_wait using qlock from thread %p",
777 g_cond_wait (queue->item_add, queue->qlock);
779 /* we released the lock in the g_cond above so we might be
781 if (queue->srcresult != GST_FLOW_OK)
784 GST_LOG_OBJECT (queue, "done g_cond_wait using qlock from thread %p",
786 STATUS (queue, "got item_add signal");
789 STATUS (queue, "post-empty wait");
790 GST_QUEUE_MUTEX_UNLOCK (queue);
791 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
792 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
795 /* There's something in the list now, whatever it is */
796 data = g_queue_pop_head (queue->queue);
797 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
798 "retrieved data %p from queue", data);
800 if (GST_IS_BUFFER (data)) {
801 GstFlowReturn result;
803 /* Update statistics */
804 queue->cur_level.buffers--;
805 queue->cur_level.bytes -= GST_BUFFER_SIZE (data);
806 if (GST_BUFFER_DURATION (data) != GST_CLOCK_TIME_NONE)
807 queue->cur_level.time -= GST_BUFFER_DURATION (data);
809 GST_QUEUE_MUTEX_UNLOCK (queue);
810 result = gst_pad_push (pad, GST_BUFFER (data));
811 GST_QUEUE_MUTEX_LOCK (queue);
812 /* can opt to check for srcresult here but the push should
813 * return an error value that is more accurate */
814 if (result != GST_FLOW_OK) {
815 const gchar *flowname;
817 flowname = gst_flow_get_name (result);
819 queue->srcresult = result;
820 if (GST_FLOW_IS_FATAL (result)) {
821 GST_ELEMENT_ERROR (queue, STREAM, STOPPED,
822 ("streaming stopped, reason %s", flowname),
823 ("streaming stopped, reason %s", flowname));
824 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
826 GST_DEBUG ("pausing queue, reason %s", flowname);
827 gst_pad_pause_task (queue->srcpad);
830 if (GST_EVENT_TYPE (data) == GST_EVENT_EOS) {
831 /* all incomming data is now unexpected */
832 queue->srcresult = GST_FLOW_UNEXPECTED;
833 /* and we don't need to process anymore */
834 GST_DEBUG ("pausing queue, we're EOS now");
835 gst_pad_pause_task (queue->srcpad);
838 GST_QUEUE_MUTEX_UNLOCK (queue);
839 gst_pad_push_event (queue->srcpad, GST_EVENT (data));
840 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
845 STATUS (queue, "after _get()");
847 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "signalling item_del");
848 g_cond_signal (queue->item_del);
849 GST_QUEUE_MUTEX_UNLOCK (queue);
855 const gchar *flowname = gst_flow_get_name (queue->srcresult);
857 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
858 "exit because task paused, reason: %s", flowname);
859 GST_QUEUE_MUTEX_UNLOCK (queue);
867 gst_queue_handle_src_event (GstPad * pad, GstEvent * event)
870 GstQueue *queue = GST_QUEUE (GST_PAD_PARENT (pad));
872 #ifndef GST_DISABLE_GST_DEBUG
873 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
874 event, GST_EVENT_TYPE (event));
877 res = gst_pad_push_event (queue->sinkpad, event);
883 gst_queue_handle_src_query (GstPad * pad, GstQuery * query)
885 GstQueue *queue = GST_QUEUE (GST_PAD_PARENT (pad));
889 if (!(peer = gst_pad_get_peer (queue->sinkpad)))
892 res = gst_pad_query (peer, query);
893 gst_object_unref (peer);
897 switch (GST_QUERY_TYPE (query)) {
898 case GST_QUERY_POSITION:
900 gint64 peer_pos, peer_total;
903 /* get peer position */
904 gst_query_parse_position (query, &format, &peer_pos, &peer_total);
906 /* FIXME: this code assumes that there's no discont in the queue */
908 case GST_FORMAT_BYTES:
909 peer_pos -= queue->cur_level.bytes;
911 case GST_FORMAT_TIME:
912 peer_pos -= queue->cur_level.time;
918 /* set updated positions */
919 gst_query_set_position (query, format, peer_pos, peer_total);
930 gst_queue_sink_activate_push (GstPad * pad, gboolean active)
932 gboolean result = TRUE;
935 queue = GST_QUEUE (gst_pad_get_parent (pad));
938 GST_QUEUE_MUTEX_LOCK (queue);
939 queue->srcresult = GST_FLOW_OK;
940 GST_QUEUE_MUTEX_UNLOCK (queue);
942 /* step 1, unblock chain function */
943 GST_QUEUE_MUTEX_LOCK (queue);
944 queue->srcresult = GST_FLOW_WRONG_STATE;
945 gst_queue_locked_flush (queue);
946 GST_QUEUE_MUTEX_UNLOCK (queue);
948 /* and make sure the chain function finishes */
949 GST_STREAM_LOCK (pad);
950 GST_STREAM_UNLOCK (pad);
953 gst_object_unref (queue);
959 gst_queue_src_activate_push (GstPad * pad, gboolean active)
961 gboolean result = FALSE;
964 queue = GST_QUEUE (gst_pad_get_parent (pad));
967 GST_QUEUE_MUTEX_LOCK (queue);
968 queue->srcresult = GST_FLOW_OK;
969 /* we do not start the task yet if the pad is not connected */
970 if (gst_pad_is_linked (pad))
971 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad);
973 GST_DEBUG_OBJECT (queue, "not starting task as pad is not linked");
976 GST_QUEUE_MUTEX_UNLOCK (queue);
978 /* step 1, unblock loop function */
979 GST_QUEUE_MUTEX_LOCK (queue);
980 queue->srcresult = GST_FLOW_WRONG_STATE;
981 /* the item add signal will unblock */
982 g_cond_signal (queue->item_add);
983 GST_QUEUE_MUTEX_UNLOCK (queue);
985 /* step 2, make sure streaming finishes */
986 result = gst_pad_stop_task (pad);
989 gst_object_unref (queue);
994 static GstStateChangeReturn
995 gst_queue_change_state (GstElement * element, GstStateChange transition)
998 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1000 queue = GST_QUEUE (element);
1002 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "starting state change");
1004 switch (transition) {
1005 case GST_STATE_CHANGE_NULL_TO_READY:
1007 case GST_STATE_CHANGE_READY_TO_PAUSED:
1009 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1015 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1017 switch (transition) {
1018 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1020 case GST_STATE_CHANGE_PAUSED_TO_READY:
1022 case GST_STATE_CHANGE_READY_TO_NULL:
1032 gst_queue_set_property (GObject * object,
1033 guint prop_id, const GValue * value, GParamSpec * pspec)
1035 GstQueue *queue = GST_QUEUE (object);
1037 /* someone could change levels here, and since this
1038 * affects the get/put funcs, we need to lock for safety. */
1039 GST_QUEUE_MUTEX_LOCK (queue);
1042 case ARG_MAX_SIZE_BYTES:
1043 queue->max_size.bytes = g_value_get_uint (value);
1045 case ARG_MAX_SIZE_BUFFERS:
1046 queue->max_size.buffers = g_value_get_uint (value);
1048 case ARG_MAX_SIZE_TIME:
1049 queue->max_size.time = g_value_get_uint64 (value);
1051 case ARG_MIN_THRESHOLD_BYTES:
1052 queue->min_threshold.bytes = g_value_get_uint (value);
1054 case ARG_MIN_THRESHOLD_BUFFERS:
1055 queue->min_threshold.buffers = g_value_get_uint (value);
1057 case ARG_MIN_THRESHOLD_TIME:
1058 queue->min_threshold.time = g_value_get_uint64 (value);
1061 queue->leaky = g_value_get_enum (value);
1064 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1068 GST_QUEUE_MUTEX_UNLOCK (queue);
1072 gst_queue_get_property (GObject * object,
1073 guint prop_id, GValue * value, GParamSpec * pspec)
1075 GstQueue *queue = GST_QUEUE (object);
1077 GST_QUEUE_MUTEX_LOCK (queue);
1080 case ARG_CUR_LEVEL_BYTES:
1081 g_value_set_uint (value, queue->cur_level.bytes);
1083 case ARG_CUR_LEVEL_BUFFERS:
1084 g_value_set_uint (value, queue->cur_level.buffers);
1086 case ARG_CUR_LEVEL_TIME:
1087 g_value_set_uint64 (value, queue->cur_level.time);
1089 case ARG_MAX_SIZE_BYTES:
1090 g_value_set_uint (value, queue->max_size.bytes);
1092 case ARG_MAX_SIZE_BUFFERS:
1093 g_value_set_uint (value, queue->max_size.buffers);
1095 case ARG_MAX_SIZE_TIME:
1096 g_value_set_uint64 (value, queue->max_size.time);
1098 case ARG_MIN_THRESHOLD_BYTES:
1099 g_value_set_uint (value, queue->min_threshold.bytes);
1101 case ARG_MIN_THRESHOLD_BUFFERS:
1102 g_value_set_uint (value, queue->min_threshold.buffers);
1104 case ARG_MIN_THRESHOLD_TIME:
1105 g_value_set_uint64 (value, queue->min_threshold.time);
1108 g_value_set_enum (value, queue->leaky);
1111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1115 GST_QUEUE_MUTEX_UNLOCK (queue);