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>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 #include "gst_private.h"
28 #include "gstscheduler.h"
33 static GstElementDetails gst_queue_details = GST_ELEMENT_DETAILS (
37 "Erik Walthinsen <omega@cse.ogi.edu>"
41 /* Queue signals and args */
51 /* FIXME: don't we have another way of doing this
52 * "Gstreamer format" (frame/byte/time) queries? */
53 ARG_CUR_LEVEL_BUFFERS,
59 ARG_MIN_THRESHOLD_BUFFERS,
60 ARG_MIN_THRESHOLD_BYTES,
61 ARG_MIN_THRESHOLD_TIME,
68 typedef struct _GstQueueEventResponse {
70 gboolean ret, handled;
71 } GstQueueEventResponse;
73 static void gst_queue_base_init (GstQueueClass *klass);
74 static void gst_queue_class_init (GstQueueClass *klass);
75 static void gst_queue_init (GstQueue *queue);
76 static void gst_queue_dispose (GObject *object);
78 static void gst_queue_set_property (GObject *object,
82 static void gst_queue_get_property (GObject *object,
87 static void gst_queue_chain (GstPad *pad,
89 static GstData *gst_queue_get (GstPad *pad);
91 static gboolean gst_queue_handle_src_event (GstPad *pad,
94 static GstCaps *gst_queue_getcaps (GstPad *pad);
95 static GstPadLinkReturn
96 gst_queue_link (GstPad *pad,
98 static void gst_queue_locked_flush (GstQueue *queue);
100 static GstElementStateReturn
101 gst_queue_change_state (GstElement *element);
102 static gboolean gst_queue_release_locks (GstElement *element);
105 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
108 queue_leaky_get_type (void)
110 static GType queue_leaky_type = 0;
111 static GEnumValue queue_leaky[] = {
112 { GST_QUEUE_NO_LEAK, "0", "Not Leaky" },
113 { GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream" },
114 { GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream" },
117 if (!queue_leaky_type) {
118 queue_leaky_type = g_enum_register_static("GstQueueLeaky", queue_leaky);
120 return queue_leaky_type;
123 static GstElementClass *parent_class = NULL;
124 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
127 gst_queue_get_type (void)
129 static GType queue_type = 0;
132 static const GTypeInfo queue_info = {
133 sizeof (GstQueueClass),
134 (GBaseInitFunc) gst_queue_base_init,
136 (GClassInitFunc) gst_queue_class_init,
141 (GInstanceInitFunc) gst_queue_init,
145 queue_type = g_type_register_static (GST_TYPE_ELEMENT,
146 "GstQueue", &queue_info, 0);
153 gst_queue_base_init (GstQueueClass *klass)
155 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
157 gst_element_class_set_details (gstelement_class, &gst_queue_details);
161 gst_queue_class_init (GstQueueClass *klass)
163 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
164 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
166 parent_class = g_type_class_peek_parent (klass);
169 gst_queue_signals[SIGNAL_UNDERRUN] =
170 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
171 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
172 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
173 gst_queue_signals[SIGNAL_RUNNING] =
174 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
175 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
176 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
177 gst_queue_signals[SIGNAL_OVERRUN] =
178 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
179 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
180 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
183 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_BYTES,
184 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
185 "Current amount of data in the queue (bytes)",
186 0, G_MAXUINT, 0, G_PARAM_READABLE));
187 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_BUFFERS,
188 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
189 "Current number of buffers in the queue",
190 0, G_MAXUINT, 0, G_PARAM_READABLE));
191 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_TIME,
192 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
193 "Current amount of data in the queue (in ns)",
194 0, G_MAXUINT64, 0, G_PARAM_READABLE));
196 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BYTES,
197 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
198 "Max. amount of data in the queue (bytes, 0=disable)",
199 0, G_MAXUINT, 0, G_PARAM_READWRITE));
200 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BUFFERS,
201 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
202 "Max. number of buffers in the queue (0=disable)",
203 0, G_MAXUINT, 0, G_PARAM_READWRITE));
204 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_TIME,
205 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
206 "Max. amount of data in the queue (in ns, 0=disable)",
207 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
209 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_BYTES,
210 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
211 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
212 0, G_MAXUINT, 0, G_PARAM_READWRITE));
213 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_BUFFERS,
214 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
215 "Min. number of buffers in the queue to allow reading (0=disable)",
216 0, G_MAXUINT, 0, G_PARAM_READWRITE));
217 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_TIME,
218 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
219 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
220 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
222 g_object_class_install_property (gobject_class, ARG_LEAKY,
223 g_param_spec_enum ("leaky", "Leaky",
224 "Where the queue leaks, if at all",
225 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK, G_PARAM_READWRITE));
226 g_object_class_install_property (gobject_class, ARG_MAY_DEADLOCK,
227 g_param_spec_boolean ("may_deadlock", "May Deadlock",
228 "The queue may deadlock if it's full and not PLAYING",
229 TRUE, G_PARAM_READWRITE));
230 g_object_class_install_property (gobject_class, ARG_BLOCK_TIMEOUT,
231 g_param_spec_uint64 ("block_timeout", "Timeout for Block",
232 "Nanoseconds until blocked queue times out and returns filler event. "
233 "Value of -1 disables timeout",
234 0, G_MAXUINT64, -1, G_PARAM_READWRITE));
236 /* set several parent class virtual functions */
237 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_queue_dispose);
238 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_queue_set_property);
239 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_queue_get_property);
241 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue_change_state);
242 gstelement_class->release_locks = GST_DEBUG_FUNCPTR (gst_queue_release_locks);
246 gst_queue_init (GstQueue *queue)
248 /* scheduling on this kind of element is, well, interesting */
249 GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
250 GST_FLAG_SET (queue, GST_ELEMENT_EVENT_AWARE);
252 queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
253 gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_chain));
254 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
255 gst_pad_set_link_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_link));
256 gst_pad_set_getcaps_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
257 gst_pad_set_active (queue->sinkpad, TRUE);
259 queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
260 gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_get));
261 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
262 gst_pad_set_link_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_link));
263 gst_pad_set_getcaps_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
264 gst_pad_set_event_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_handle_src_event));
265 gst_pad_set_active (queue->srcpad, TRUE);
267 queue->cur_level.buffers = 0; /* no content */
268 queue->cur_level.bytes = 0; /* no content */
269 queue->cur_level.time = 0; /* no content */
270 queue->max_size.buffers = 100; /* 100 buffers */
271 queue->max_size.bytes = 10 * 1024 * 1024; /* 10 MB */
272 queue->max_size.time = GST_SECOND; /* 1 s. */
273 queue->min_threshold.buffers = 0; /* no threshold */
274 queue->min_threshold.bytes = 0; /* no threshold */
275 queue->min_threshold.time = 0; /* no threshold */
277 queue->leaky = GST_QUEUE_NO_LEAK;
278 queue->may_deadlock = TRUE;
279 queue->block_timeout = GST_CLOCK_TIME_NONE;
280 queue->interrupt = FALSE;
281 queue->flush = FALSE;
283 queue->qlock = g_mutex_new ();
284 queue->item_add = g_cond_new ();
285 queue->item_del = g_cond_new ();
286 queue->event_done = g_cond_new ();
287 queue->events = g_queue_new ();
288 queue->queue = g_queue_new ();
290 GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue,
291 "initialized queue's not_empty & not_full conditions");
295 gst_queue_dispose (GObject *object)
297 GstQueue *queue = GST_QUEUE (object);
299 gst_element_set_state (GST_ELEMENT (queue), GST_STATE_NULL);
301 while (!g_queue_is_empty (queue->queue)) {
302 GstData *data = g_queue_pop_head (queue->queue);
303 gst_data_unref (data);
305 g_queue_free (queue->queue);
306 g_mutex_free (queue->qlock);
307 g_cond_free (queue->item_add);
308 g_cond_free (queue->item_del);
309 g_cond_free (queue->event_done);
310 while (!g_queue_is_empty (queue->events)) {
311 GstEvent *event = g_queue_pop_head (queue->events);
312 gst_event_unref (event);
315 if (G_OBJECT_CLASS (parent_class)->dispose)
316 G_OBJECT_CLASS (parent_class)->dispose (object);
320 gst_queue_getcaps (GstPad *pad)
324 queue = GST_QUEUE (gst_pad_get_parent (pad));
326 if (queue->cur_level.bytes > 0) {
327 return gst_caps_copy (queue->negotiated_caps);
330 return gst_pad_proxy_getcaps (pad);
333 static GstPadLinkReturn
334 gst_queue_link (GstPad *pad, const GstCaps *caps)
337 GstPadLinkReturn link_ret;
339 queue = GST_QUEUE (gst_pad_get_parent (pad));
341 if (queue->cur_level.bytes > 0) {
342 if (gst_caps_is_equal_fixed (caps, queue->negotiated_caps)) {
343 return GST_PAD_LINK_OK;
345 return GST_PAD_LINK_REFUSED;
348 link_ret = gst_pad_proxy_pad_link (pad, caps);
350 if (GST_PAD_LINK_SUCCESSFUL (link_ret)) {
351 /* we store an extra copy of the negotiated caps, just in case
352 * the pads become unnegotiated while we have buffers */
353 gst_caps_replace (&queue->negotiated_caps, gst_caps_copy (caps));
360 gst_queue_locked_flush (GstQueue *queue)
362 while (!g_queue_is_empty (queue->queue)) {
363 GstData *data = g_queue_pop_head (queue->queue);
364 /* First loose the reference we added when putting that data in the queue */
365 gst_data_unref (data);
366 /* Then loose another reference because we are supposed to destroy that
367 data when flushing */
368 gst_data_unref (data);
370 queue->timeval = NULL;
371 queue->cur_level.buffers = 0;
372 queue->cur_level.bytes = 0;
373 queue->cur_level.time = 0;
375 /* make sure any pending buffers to be added are flushed too */
378 /* we deleted something... */
379 g_cond_signal (queue->item_del);
383 gst_queue_handle_pending_events (GstQueue *queue)
385 /* check for events to send upstream */
386 while (!g_queue_is_empty (queue->events)){
387 GstQueueEventResponse *er = g_queue_pop_head (queue->events);
388 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "sending event upstream");
389 er->ret = gst_pad_event_default (queue->srcpad, er->event);
391 g_cond_signal (queue->event_done);
392 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "event sent");
396 #define STATUS(queue, msg) \
397 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, \
398 "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
399 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
400 "-%" G_GUINT64_FORMAT " ns, %u elements", \
401 GST_DEBUG_PAD_NAME (pad), \
402 queue->cur_level.buffers, \
403 queue->min_threshold.buffers, \
404 queue->max_size.buffers, \
405 queue->cur_level.bytes, \
406 queue->min_threshold.bytes, \
407 queue->max_size.bytes, \
408 queue->cur_level.time, \
409 queue->min_threshold.time, \
410 queue->max_size.time, \
411 queue->queue->length)
414 gst_queue_chain (GstPad *pad,
419 g_return_if_fail (pad != NULL);
420 g_return_if_fail (GST_IS_PAD (pad));
421 g_return_if_fail (data != NULL);
423 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
426 /* we have to lock the queue since we span threads */
427 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "locking t:%p", g_thread_self ());
428 g_mutex_lock (queue->qlock);
429 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "locked t:%p", g_thread_self ());
431 gst_queue_handle_pending_events (queue);
433 /* assume don't need to flush this buffer when the queue is filled */
434 queue->flush = FALSE;
436 if (GST_IS_EVENT (data)) {
437 switch (GST_EVENT_TYPE (data)) {
438 case GST_EVENT_FLUSH:
439 STATUS (queue, "received flush event");
440 gst_queue_locked_flush (queue);
441 STATUS (queue, "after flush");
444 STATUS (queue, "received EOS");
447 /* we put the event in the queue, we don't have to act ourselves */
448 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue,
449 "adding event %p of type %d",
450 data, GST_EVENT_TYPE (data));
455 if (GST_IS_BUFFER (data))
456 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue,
457 "adding buffer %p of size %d",
458 data, GST_BUFFER_SIZE (data));
460 /* We make space available if we're "full" according to whatever
461 * the user defined as "full". Note that this only applies to buffers.
462 * We always handle events and they don't count in our statistics. */
463 if (GST_IS_BUFFER (data) &&
464 ((queue->max_size.buffers > 0 &&
465 queue->cur_level.buffers >= queue->max_size.buffers) ||
466 (queue->max_size.bytes > 0 &&
467 queue->cur_level.bytes >= queue->max_size.bytes) ||
468 (queue->max_size.time > 0 &&
469 queue->cur_level.time >= queue->max_size.time))) {
470 g_mutex_unlock (queue->qlock);
471 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_OVERRUN], 0);
472 g_mutex_lock (queue->qlock);
474 /* how are we going to make space for this buffer? */
475 switch (queue->leaky) {
476 /* leak current buffer */
477 case GST_QUEUE_LEAK_UPSTREAM:
478 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue,
479 "queue is full, leaking buffer on upstream end");
480 /* now we can clean up and exit right away */
481 g_mutex_unlock (queue->qlock);
484 /* leak first buffer in the queue */
485 case GST_QUEUE_LEAK_DOWNSTREAM: {
486 /* this is a bit hacky. We'll manually iterate the list
487 * and find the first buffer from the head on. We'll
488 * unref that and "fix up" the GQueue object... */
490 GstData *leak = NULL;
492 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue,
493 "queue is full, leaking buffer on downstream end");
495 for (item = queue->queue->head; item != NULL; item = item->next) {
496 if (GST_IS_BUFFER (item->data)) {
502 /* if we didn't find anything, it means we have no buffers
503 * in here. That cannot happen, since we had >= 1 bufs */
506 /* Now remove it from the list, fixing up the GQueue
507 * CHECKME: is a queue->head the first or the last item? */
508 item = g_list_delete_link (queue->queue->head, item);
509 queue->queue->head = g_list_first (item);
510 queue->queue->tail = g_list_last (item);
511 queue->queue->length--;
513 /* and unref the data at the end. Twice, because we keep a ref
514 * to make things read-only. Also keep our list uptodate. */
515 queue->cur_level.bytes -= GST_BUFFER_SIZE (data);
516 queue->cur_level.buffers --;
517 if (GST_BUFFER_DURATION (data) != GST_CLOCK_TIME_NONE)
518 queue->cur_level.time -= GST_BUFFER_DURATION (data);
520 gst_data_unref (data);
521 gst_data_unref (data);
526 g_warning ("Unknown leaky type, using default");
529 /* don't leak. Instead, wait for space to be available */
530 case GST_QUEUE_NO_LEAK:
531 STATUS (queue, "pre-full wait");
533 while ((queue->max_size.buffers > 0 &&
534 queue->cur_level.buffers >= queue->max_size.buffers) ||
535 (queue->max_size.bytes > 0 &&
536 queue->cur_level.bytes >= queue->max_size.bytes) ||
537 (queue->max_size.time > 0 &&
538 queue->cur_level.time >= queue->max_size.time)) {
539 /* if there's a pending state change for this queue
540 * or its manager, switch back to iterator so bottom
541 * half of state change executes */
542 if (queue->interrupt) {
543 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "interrupted");
544 g_mutex_unlock (queue->qlock);
545 if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->sinkpad),
546 GST_ELEMENT (queue))) {
549 /* if we got here because we were unlocked after a
550 * flush, we don't need to add the buffer to the
553 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue,
554 "not adding pending buffer after flush");
557 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue,
558 "adding pending buffer after interrupt");
562 if (GST_STATE (queue) != GST_STATE_PLAYING) {
563 /* this means the other end is shut down. Try to
564 * signal to resolve the error */
565 if (!queue->may_deadlock) {
566 g_mutex_unlock (queue->qlock);
567 gst_data_unref (data);
568 GST_ELEMENT_ERROR (queue, CORE, THREAD, (NULL),
569 ("deadlock found, shutting down source pad elements"));
570 /* we don't go to out_unref here, since we want to
571 * unref the buffer *before* calling GST_ELEMENT_ERROR */
574 GST_CAT_WARNING_OBJECT (GST_CAT_DATAFLOW, queue,
575 "%s: waiting for the app to restart "
576 "source pad elements",
577 GST_ELEMENT_NAME (queue));
581 /* OK, we've got a serious issue here. Imagine the situation
582 * where the puller (next element) is sending an event here,
583 * so it cannot pull events from the queue, and we cannot
584 * push data further because the queue is 'full' and therefore,
585 * we wait here (and do not handle events): deadlock! to solve
586 * that, we handle pending upstream events here, too. */
587 gst_queue_handle_pending_events (queue);
589 STATUS (queue, "waiting for item_del signal");
590 g_cond_wait (queue->item_del, queue->qlock);
591 STATUS (queue, "received item_del signal");
594 STATUS (queue, "post-full wait");
595 g_mutex_unlock (queue->qlock);
596 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
597 g_mutex_lock (queue->qlock);
602 /* put the buffer on the tail of the list. We keep a reference,
603 * so that the data is read-only while in here. There's a good
604 * reason to do so: we have a size and time counter, and any
605 * modification to the content could change any of the two. */
607 g_queue_push_tail (queue->queue, data);
609 /* Note that we only add buffers (not events) to the statistics */
610 if (GST_IS_BUFFER (data)) {
611 queue->cur_level.buffers++;
612 queue->cur_level.bytes += GST_BUFFER_SIZE (data);
613 if (GST_BUFFER_DURATION (data) != GST_CLOCK_TIME_NONE)
614 queue->cur_level.time += GST_BUFFER_DURATION (data);
617 STATUS (queue, "+ level");
619 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "signalling item_add");
620 g_cond_signal (queue->item_add);
621 g_mutex_unlock (queue->qlock);
626 gst_data_unref (data);
631 gst_queue_get (GstPad *pad)
636 g_return_val_if_fail (pad != NULL, NULL);
637 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
639 queue = GST_QUEUE (gst_pad_get_parent (pad));
642 /* have to lock for thread-safety */
643 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue,
644 "locking t:%p", g_thread_self ());
645 g_mutex_lock (queue->qlock);
646 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue,
647 "locked t:%p", g_thread_self ());
649 if (queue->queue->length == 0 ||
650 (queue->min_threshold.buffers > 0 &&
651 queue->cur_level.buffers < queue->min_threshold.buffers) ||
652 (queue->min_threshold.bytes > 0 &&
653 queue->cur_level.bytes < queue->min_threshold.bytes) ||
654 (queue->min_threshold.time > 0 &&
655 queue->cur_level.time < queue->min_threshold.time)) {
656 g_mutex_unlock (queue->qlock);
657 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_UNDERRUN], 0);
658 g_mutex_lock (queue->qlock);
660 STATUS (queue, "pre-empty wait");
661 while (queue->queue->length == 0 ||
662 (queue->min_threshold.buffers > 0 &&
663 queue->cur_level.buffers < queue->min_threshold.buffers) ||
664 (queue->min_threshold.bytes > 0 &&
665 queue->cur_level.bytes < queue->min_threshold.bytes) ||
666 (queue->min_threshold.time > 0 &&
667 queue->cur_level.time < queue->min_threshold.time)) {
668 /* if there's a pending state change for this queue or its
669 * manager, switch back to iterator so bottom half of state
670 * change executes. */
671 if (queue->interrupt) {
672 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "interrupted");
673 g_mutex_unlock (queue->qlock);
674 if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->srcpad),
675 GST_ELEMENT (queue)))
676 return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
679 if (GST_STATE (queue) != GST_STATE_PLAYING) {
680 /* this means the other end is shut down */
681 if (!queue->may_deadlock) {
682 g_mutex_unlock (queue->qlock);
683 GST_ELEMENT_ERROR (queue, CORE, THREAD, (NULL),
684 ("deadlock found, shutting down sink pad elements"));
687 GST_CAT_WARNING_OBJECT (GST_CAT_DATAFLOW, queue,
688 "%s: waiting for the app to restart "
689 "source pad elements",
690 GST_ELEMENT_NAME (queue));
694 STATUS (queue, "waiting for item_add");
696 if (queue->block_timeout != GST_CLOCK_TIME_NONE) {
698 g_get_current_time (&timeout);
699 g_time_val_add (&timeout, queue->block_timeout / 1000);
700 if (!g_cond_timed_wait (queue->item_add, queue->qlock, &timeout)){
701 g_mutex_unlock (queue->qlock);
702 GST_CAT_WARNING_OBJECT (GST_CAT_DATAFLOW, queue,
703 "Sending filler event");
704 return GST_DATA (gst_event_new_filler ());
707 g_cond_wait (queue->item_add, queue->qlock);
709 STATUS (queue, "got item_add signal");
712 STATUS (queue, "post-empty wait");
713 g_mutex_unlock (queue->qlock);
714 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
715 g_mutex_lock (queue->qlock);
718 /* There's something in the list now, whatever it is */
719 data = g_queue_pop_head (queue->queue);
720 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue,
721 "retrieved data %p from queue", data);
726 if (GST_IS_BUFFER (data)) {
727 /* Update statistics */
728 queue->cur_level.buffers--;
729 queue->cur_level.bytes -= GST_BUFFER_SIZE (data);
730 if (GST_BUFFER_DURATION (data) != GST_CLOCK_TIME_NONE)
731 queue->cur_level.time -= GST_BUFFER_DURATION (data);
734 /* Now that we're done, we can lose our own reference to
735 * the item, since we're no longer in danger. */
736 gst_data_unref (data);
738 STATUS (queue, "after _get()");
740 GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "signalling item_del");
741 g_cond_signal (queue->item_del);
742 g_mutex_unlock (queue->qlock);
744 /* FIXME: I suppose this needs to be locked, since the EOS
745 * bit affects the pipeline state. However, that bit is
746 * locked too so it'd cause a deadlock. */
747 if (GST_IS_EVENT (data)) {
748 GstEvent *event = GST_EVENT (data);
749 switch (GST_EVENT_TYPE (event)) {
751 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue,
753 GST_ELEMENT_NAME (queue));
754 gst_element_set_eos (GST_ELEMENT (queue));
766 gst_queue_handle_src_event (GstPad *pad,
769 GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
772 g_mutex_lock (queue->qlock);
774 if (gst_element_get_state (GST_ELEMENT (queue)) == GST_STATE_PLAYING) {
775 GstQueueEventResponse er;
777 /* push the event to the queue and wait for upstream consumption */
780 g_queue_push_tail (queue->events, &er);
781 GST_CAT_WARNING_OBJECT (GST_CAT_DATAFLOW, queue,
782 "Preparing for loop for event handler");
783 /* see the chain function on why this is here - it prevents a deadlock */
784 g_cond_signal (queue->item_del);
785 while (!er.handled) {
787 g_get_current_time (&timeout);
788 g_time_val_add (&timeout, 500 * 1000); /* half a second */
789 if (!g_cond_timed_wait (queue->event_done, queue->qlock, &timeout) &&
791 GST_CAT_WARNING_OBJECT (GST_CAT_DATAFLOW, queue,
792 "timeout in upstream event handling");
793 /* remove ourselves from the pending list. Since we're
794 * locked, others cannot reference this anymore. */
795 queue->queue->head = g_list_remove (queue->queue->head, &er);
796 queue->queue->head = g_list_first (queue->queue->head);
797 queue->queue->tail = g_list_last (queue->queue->head);
798 queue->queue->length--;
803 GST_CAT_WARNING_OBJECT (GST_CAT_DATAFLOW, queue,
807 res = gst_pad_event_default (pad, event);
809 switch (GST_EVENT_TYPE (event)) {
810 case GST_EVENT_FLUSH:
811 GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue,
812 "FLUSH event, flushing queue\n");
813 gst_queue_locked_flush (queue);
816 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH) {
817 gst_queue_locked_flush (queue);
824 g_mutex_unlock (queue->qlock);
830 gst_queue_release_locks (GstElement *element)
834 queue = GST_QUEUE (element);
836 g_mutex_lock (queue->qlock);
837 queue->interrupt = TRUE;
838 g_cond_signal (queue->item_add);
839 g_cond_signal (queue->item_del);
840 g_mutex_unlock (queue->qlock);
845 static GstElementStateReturn
846 gst_queue_change_state (GstElement *element)
849 GstElementStateReturn ret = GST_STATE_SUCCESS;
851 queue = GST_QUEUE (element);
853 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "starting state change");
855 /* lock the queue so another thread (not in sync with this thread's state)
856 * can't call this queue's _get (or whatever)
858 g_mutex_lock (queue->qlock);
860 switch (GST_STATE_TRANSITION (element)) {
861 case GST_STATE_NULL_TO_READY:
862 gst_queue_locked_flush (queue);
864 case GST_STATE_PAUSED_TO_PLAYING:
865 if (!GST_PAD_IS_LINKED (queue->sinkpad)) {
866 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, queue,
867 "queue %s is not linked",
868 GST_ELEMENT_NAME (queue));
869 /* FIXME can this be? */
870 g_cond_signal (queue->item_add);
872 ret = GST_STATE_FAILURE;
875 GstScheduler *src_sched, *sink_sched;
877 src_sched = gst_pad_get_scheduler (GST_PAD (queue->srcpad));
878 sink_sched = gst_pad_get_scheduler (GST_PAD (queue->sinkpad));
880 if (src_sched == sink_sched) {
881 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, queue,
882 "queue %s does not connect different schedulers",
883 GST_ELEMENT_NAME (queue));
885 g_warning ("queue %s does not connect different schedulers",
886 GST_ELEMENT_NAME (queue));
888 ret = GST_STATE_FAILURE;
892 queue->interrupt = FALSE;
894 case GST_STATE_PAUSED_TO_READY:
895 gst_queue_locked_flush (queue);
901 if (GST_ELEMENT_CLASS (parent_class)->change_state)
902 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
904 /* this is an ugly hack to make sure our pads are always active.
905 * Reason for this is that pad activation for the queue element
906 * depends on 2 schedulers (ugh) */
907 gst_pad_set_active (queue->sinkpad, TRUE);
908 gst_pad_set_active (queue->srcpad, TRUE);
911 g_mutex_unlock (queue->qlock);
913 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "done with state change");
920 gst_queue_set_property (GObject *object,
925 GstQueue *queue = GST_QUEUE (object);
927 /* someone could change levels here, and since this
928 * affects the get/put funcs, we need to lock for safety. */
929 g_mutex_lock (queue->qlock);
932 case ARG_MAX_SIZE_BYTES:
933 queue->max_size.bytes = g_value_get_uint (value);
935 case ARG_MAX_SIZE_BUFFERS:
936 queue->max_size.buffers = g_value_get_uint (value);
938 case ARG_MAX_SIZE_TIME:
939 queue->max_size.time = g_value_get_uint64 (value);
941 case ARG_MIN_THRESHOLD_BYTES:
942 queue->min_threshold.bytes = g_value_get_uint (value);
944 case ARG_MIN_THRESHOLD_BUFFERS:
945 queue->min_threshold.buffers = g_value_get_uint (value);
947 case ARG_MIN_THRESHOLD_TIME:
948 queue->min_threshold.time = g_value_get_uint64 (value);
951 queue->leaky = g_value_get_enum (value);
953 case ARG_MAY_DEADLOCK:
954 queue->may_deadlock = g_value_get_boolean (value);
956 case ARG_BLOCK_TIMEOUT:
957 queue->block_timeout = g_value_get_uint64 (value);
960 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
964 g_mutex_unlock (queue->qlock);
968 gst_queue_get_property (GObject *object,
973 GstQueue *queue = GST_QUEUE (object);
976 case ARG_CUR_LEVEL_BYTES:
977 g_value_set_uint (value, queue->cur_level.bytes);
979 case ARG_CUR_LEVEL_BUFFERS:
980 g_value_set_uint (value, queue->cur_level.buffers);
982 case ARG_CUR_LEVEL_TIME:
983 g_value_set_uint64 (value, queue->cur_level.time);
985 case ARG_MAX_SIZE_BYTES:
986 g_value_set_uint (value, queue->max_size.bytes);
988 case ARG_MAX_SIZE_BUFFERS:
989 g_value_set_uint (value, queue->max_size.buffers);
991 case ARG_MAX_SIZE_TIME:
992 g_value_set_uint64 (value, queue->max_size.time);
994 case ARG_MIN_THRESHOLD_BYTES:
995 g_value_set_uint (value, queue->min_threshold.bytes);
997 case ARG_MIN_THRESHOLD_BUFFERS:
998 g_value_set_uint (value, queue->min_threshold.buffers);
1000 case ARG_MIN_THRESHOLD_TIME:
1001 g_value_set_uint64 (value, queue->min_threshold.time);
1004 g_value_set_enum (value, queue->leaky);
1006 case ARG_MAY_DEADLOCK:
1007 g_value_set_boolean (value, queue->may_deadlock);
1009 case ARG_BLOCK_TIMEOUT:
1010 g_value_set_uint64 (value, queue->block_timeout);
1013 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);