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 GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
38 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
43 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
44 #define GST_CAT_DEFAULT (queue_dataflow)
46 #define STATUS(queue, msg) \
47 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
48 "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
49 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
50 "-%" G_GUINT64_FORMAT " ns, %u elements", \
51 GST_DEBUG_PAD_NAME (pad), \
52 queue->cur_level.buffers, \
53 queue->min_threshold.buffers, \
54 queue->max_size.buffers, \
55 queue->cur_level.bytes, \
56 queue->min_threshold.bytes, \
57 queue->max_size.bytes, \
58 queue->cur_level.time, \
59 queue->min_threshold.time, \
60 queue->max_size.time, \
63 static GstElementDetails gst_queue_details = GST_ELEMENT_DETAILS ("Queue",
66 "Erik Walthinsen <omega@cse.ogi.edu>");
69 /* Queue signals and args */
81 /* FIXME: don't we have another way of doing this
82 * "Gstreamer format" (frame/byte/time) queries? */
83 ARG_CUR_LEVEL_BUFFERS,
89 ARG_MIN_THRESHOLD_BUFFERS,
90 ARG_MIN_THRESHOLD_BYTES,
91 ARG_MIN_THRESHOLD_TIME,
98 #define GST_QUEUE_MUTEX_LOCK G_STMT_START { \
99 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
100 "locking qlock from thread %p", \
102 g_mutex_lock (queue->qlock); \
103 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
104 "locked qlock from thread %p", \
108 #define GST_QUEUE_MUTEX_UNLOCK G_STMT_START { \
109 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
110 "unlocking qlock from thread %p", \
112 g_mutex_unlock (queue->qlock); \
116 typedef struct _GstQueueEventResponse
119 gboolean ret, handled;
121 GstQueueEventResponse;
123 static void gst_queue_base_init (GstQueueClass * klass);
124 static void gst_queue_class_init (GstQueueClass * klass);
125 static void gst_queue_init (GstQueue * queue);
126 static void gst_queue_finalize (GObject * object);
128 static void gst_queue_set_property (GObject * object,
129 guint prop_id, const GValue * value, GParamSpec * pspec);
130 static void gst_queue_get_property (GObject * object,
131 guint prop_id, GValue * value, GParamSpec * pspec);
133 static void gst_queue_chain (GstPad * pad, GstData * data);
134 static GstData *gst_queue_get (GstPad * pad);
136 static gboolean gst_queue_handle_src_event (GstPad * pad, GstEvent * event);
137 static gboolean gst_queue_handle_src_query (GstPad * pad,
138 GstQueryType type, GstFormat * fmt, gint64 * value);
140 static GstCaps *gst_queue_getcaps (GstPad * pad);
141 static GstPadLinkReturn
142 gst_queue_link_sink (GstPad * pad, const GstCaps * caps);
143 static GstPadLinkReturn gst_queue_link_src (GstPad * pad, const GstCaps * caps);
144 static void gst_queue_locked_flush (GstQueue * queue);
146 static GstElementStateReturn gst_queue_change_state (GstElement * element);
147 static gboolean gst_queue_release_locks (GstElement * element);
150 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
153 queue_leaky_get_type (void)
155 static GType queue_leaky_type = 0;
156 static GEnumValue queue_leaky[] = {
157 {GST_QUEUE_NO_LEAK, "0", "Not Leaky"},
158 {GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream"},
159 {GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream"},
163 if (!queue_leaky_type) {
164 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
166 return queue_leaky_type;
169 static GstElementClass *parent_class = NULL;
170 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
173 gst_queue_get_type (void)
175 static GType queue_type = 0;
178 static const GTypeInfo queue_info = {
179 sizeof (GstQueueClass),
180 (GBaseInitFunc) gst_queue_base_init,
182 (GClassInitFunc) gst_queue_class_init,
187 (GInstanceInitFunc) gst_queue_init,
191 queue_type = g_type_register_static (GST_TYPE_ELEMENT,
192 "GstQueue", &queue_info, 0);
193 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0,
194 "dataflow inside the queue element");
201 gst_queue_base_init (GstQueueClass * klass)
203 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
205 gst_element_class_add_pad_template (gstelement_class,
206 gst_static_pad_template_get (&srctemplate));
207 gst_element_class_add_pad_template (gstelement_class,
208 gst_static_pad_template_get (&sinktemplate));
209 gst_element_class_set_details (gstelement_class, &gst_queue_details);
213 gst_queue_class_init (GstQueueClass * klass)
215 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
216 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
218 parent_class = g_type_class_peek_parent (klass);
221 gst_queue_signals[SIGNAL_UNDERRUN] =
222 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
223 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
224 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
225 gst_queue_signals[SIGNAL_RUNNING] =
226 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
227 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
228 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
229 gst_queue_signals[SIGNAL_OVERRUN] =
230 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
231 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
232 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
235 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_BYTES,
236 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
237 "Current amount of data in the queue (bytes)",
238 0, G_MAXUINT, 0, G_PARAM_READABLE));
239 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_BUFFERS,
240 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
241 "Current number of buffers in the queue",
242 0, G_MAXUINT, 0, G_PARAM_READABLE));
243 g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_TIME,
244 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
245 "Current amount of data in the queue (in ns)",
246 0, G_MAXUINT64, 0, G_PARAM_READABLE));
248 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BYTES,
249 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
250 "Max. amount of data in the queue (bytes, 0=disable)",
251 0, G_MAXUINT, 0, G_PARAM_READWRITE));
252 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BUFFERS,
253 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
254 "Max. number of buffers in the queue (0=disable)",
255 0, G_MAXUINT, 0, G_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_TIME,
257 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
258 "Max. amount of data in the queue (in ns, 0=disable)",
259 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
261 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_BYTES,
262 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
263 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
264 0, G_MAXUINT, 0, G_PARAM_READWRITE));
265 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_BUFFERS,
266 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
267 "Min. number of buffers in the queue to allow reading (0=disable)",
268 0, G_MAXUINT, 0, G_PARAM_READWRITE));
269 g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_TIME,
270 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
271 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
272 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
274 g_object_class_install_property (gobject_class, ARG_LEAKY,
275 g_param_spec_enum ("leaky", "Leaky",
276 "Where the queue leaks, if at all",
277 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK, G_PARAM_READWRITE));
278 g_object_class_install_property (gobject_class, ARG_MAY_DEADLOCK,
279 g_param_spec_boolean ("may_deadlock", "May Deadlock",
280 "The queue may deadlock if it's full and not PLAYING",
281 TRUE, G_PARAM_READWRITE));
282 g_object_class_install_property (gobject_class, ARG_BLOCK_TIMEOUT,
283 g_param_spec_uint64 ("block_timeout", "Timeout for Block",
284 "Nanoseconds until blocked queue times out and returns filler event. "
285 "Value of -1 disables timeout",
286 0, G_MAXUINT64, -1, G_PARAM_READWRITE));
288 /* set several parent class virtual functions */
289 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_queue_finalize);
290 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_queue_set_property);
291 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_queue_get_property);
293 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue_change_state);
294 gstelement_class->release_locks = GST_DEBUG_FUNCPTR (gst_queue_release_locks);
298 gst_queue_init (GstQueue * queue)
300 /* scheduling on this kind of element is, well, interesting */
301 GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
302 GST_FLAG_SET (queue, GST_ELEMENT_EVENT_AWARE);
305 gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate),
307 gst_pad_set_chain_function (queue->sinkpad,
308 GST_DEBUG_FUNCPTR (gst_queue_chain));
309 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
310 gst_pad_set_link_function (queue->sinkpad,
311 GST_DEBUG_FUNCPTR (gst_queue_link_sink));
312 gst_pad_set_getcaps_function (queue->sinkpad,
313 GST_DEBUG_FUNCPTR (gst_queue_getcaps));
314 gst_pad_set_active (queue->sinkpad, TRUE);
317 gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate),
319 gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_get));
320 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
321 gst_pad_set_link_function (queue->srcpad,
322 GST_DEBUG_FUNCPTR (gst_queue_link_src));
323 gst_pad_set_getcaps_function (queue->srcpad,
324 GST_DEBUG_FUNCPTR (gst_queue_getcaps));
325 gst_pad_set_event_function (queue->srcpad,
326 GST_DEBUG_FUNCPTR (gst_queue_handle_src_event));
327 gst_pad_set_query_function (queue->srcpad,
328 GST_DEBUG_FUNCPTR (gst_queue_handle_src_query));
329 gst_pad_set_active (queue->srcpad, TRUE);
331 queue->cur_level.buffers = 0; /* no content */
332 queue->cur_level.bytes = 0; /* no content */
333 queue->cur_level.time = 0; /* no content */
334 queue->max_size.buffers = 100; /* 100 buffers */
335 queue->max_size.bytes = 10 * 1024 * 1024; /* 10 MB */
336 queue->max_size.time = GST_SECOND; /* 1 s. */
337 queue->min_threshold.buffers = 0; /* no threshold */
338 queue->min_threshold.bytes = 0; /* no threshold */
339 queue->min_threshold.time = 0; /* no threshold */
341 queue->leaky = GST_QUEUE_NO_LEAK;
342 queue->may_deadlock = TRUE;
343 queue->block_timeout = GST_CLOCK_TIME_NONE;
344 queue->interrupt = FALSE;
345 queue->flush = FALSE;
347 queue->qlock = g_mutex_new ();
348 queue->item_add = g_cond_new ();
349 queue->item_del = g_cond_new ();
350 queue->event_done = g_cond_new ();
351 queue->events = g_queue_new ();
352 queue->event_lock = g_mutex_new ();
353 queue->queue = g_queue_new ();
355 GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue,
356 "initialized queue's not_empty & not_full conditions");
359 /* called only once, as opposed to dispose */
361 gst_queue_finalize (GObject * object)
363 GstQueue *queue = GST_QUEUE (object);
365 GST_DEBUG_OBJECT (queue, "finalizing queue");
367 while (!g_queue_is_empty (queue->queue)) {
368 GstData *data = g_queue_pop_head (queue->queue);
370 gst_data_unref (data);
372 g_queue_free (queue->queue);
373 g_mutex_free (queue->qlock);
374 g_cond_free (queue->item_add);
375 g_cond_free (queue->item_del);
376 g_cond_free (queue->event_done);
377 g_mutex_lock (queue->event_lock);
378 while (!g_queue_is_empty (queue->events)) {
379 GstQueueEventResponse *er = g_queue_pop_head (queue->events);
381 gst_event_unref (er->event);
383 g_mutex_unlock (queue->event_lock);
384 g_mutex_free (queue->event_lock);
385 g_queue_free (queue->events);
387 if (G_OBJECT_CLASS (parent_class)->finalize)
388 G_OBJECT_CLASS (parent_class)->finalize (object);
392 gst_queue_getcaps (GstPad * pad)
396 queue = GST_QUEUE (gst_pad_get_parent (pad));
398 if (pad == queue->srcpad && queue->cur_level.bytes > 0) {
399 return gst_caps_copy (queue->negotiated_caps);
402 return gst_pad_proxy_getcaps (pad);
405 static GstPadLinkReturn
406 gst_queue_link_sink (GstPad * pad, const GstCaps * caps)
409 GstPadLinkReturn link_ret;
411 queue = GST_QUEUE (gst_pad_get_parent (pad));
413 if (queue->cur_level.bytes > 0) {
414 if (gst_caps_is_equal (caps, queue->negotiated_caps)) {
415 GST_QUEUE_MUTEX_UNLOCK;
416 return GST_PAD_LINK_OK;
419 /* Wait until the queue is empty before attempting the pad
421 GST_QUEUE_MUTEX_LOCK;
423 STATUS (queue, "waiting for queue to get empty");
424 while (queue->cur_level.bytes > 0) {
425 g_cond_wait (queue->item_del, queue->qlock);
427 STATUS (queue, "queue is now empty");
429 GST_QUEUE_MUTEX_UNLOCK;
432 link_ret = gst_pad_proxy_pad_link (pad, caps);
434 if (GST_PAD_LINK_SUCCESSFUL (link_ret)) {
435 /* we store an extra copy of the negotiated caps, just in case
436 * the pads become unnegotiated while we have buffers */
437 gst_caps_replace (&queue->negotiated_caps, gst_caps_copy (caps));
443 static GstPadLinkReturn
444 gst_queue_link_src (GstPad * pad, const GstCaps * caps)
447 GstPadLinkReturn link_ret;
449 queue = GST_QUEUE (gst_pad_get_parent (pad));
451 if (queue->cur_level.bytes > 0) {
452 if (gst_caps_is_equal (caps, queue->negotiated_caps)) {
453 return GST_PAD_LINK_OK;
455 return GST_PAD_LINK_REFUSED;
458 link_ret = gst_pad_proxy_pad_link (pad, caps);
460 if (GST_PAD_LINK_SUCCESSFUL (link_ret)) {
461 /* we store an extra copy of the negotiated caps, just in case
462 * the pads become unnegotiated while we have buffers */
463 gst_caps_replace (&queue->negotiated_caps, gst_caps_copy (caps));
470 gst_queue_locked_flush (GstQueue * queue)
472 while (!g_queue_is_empty (queue->queue)) {
473 GstData *data = g_queue_pop_head (queue->queue);
475 /* First loose the reference we added when putting that data in the queue */
476 gst_data_unref (data);
477 /* Then loose another reference because we are supposed to destroy that
478 data when flushing */
479 gst_data_unref (data);
481 queue->timeval = NULL;
482 queue->cur_level.buffers = 0;
483 queue->cur_level.bytes = 0;
484 queue->cur_level.time = 0;
486 /* make sure any pending buffers to be added are flushed too */
489 /* we deleted something... */
490 g_cond_signal (queue->item_del);
494 gst_queue_handle_pending_events (GstQueue * queue)
496 /* check for events to send upstream */
497 /* g_queue_get_length is glib 2.4, so don't depend on it yet, use ->length */
498 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
499 "handling pending events, events queue of size %d",
500 queue->events->length);
501 g_mutex_lock (queue->event_lock);
502 while (!g_queue_is_empty (queue->events)) {
503 GstQueueEventResponse *er;
505 er = g_queue_pop_head (queue->events);
507 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
508 "sending event %p (%d) from event response %p upstream",
509 er->event, GST_EVENT_TYPE (er->event), er);
511 /* change this to an assert when this file gets reviewed properly. */
512 GST_ELEMENT_ERROR (queue, CORE, EVENT, (NULL),
513 ("already handled event %p (%d) from event response %p upstream",
514 er->event, GST_EVENT_TYPE (er->event), er));
517 g_mutex_unlock (queue->event_lock);
518 er->ret = gst_pad_event_default (queue->srcpad, er->event);
520 g_cond_signal (queue->event_done);
521 g_mutex_lock (queue->event_lock);
522 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "event sent");
524 g_mutex_unlock (queue->event_lock);
528 gst_queue_chain (GstPad * pad, GstData * data)
532 g_return_if_fail (pad != NULL);
533 g_return_if_fail (GST_IS_PAD (pad));
534 g_return_if_fail (data != NULL);
536 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
539 /* we have to lock the queue since we span threads */
540 GST_QUEUE_MUTEX_LOCK;
542 gst_queue_handle_pending_events (queue);
544 /* assume don't need to flush this buffer when the queue is filled */
545 queue->flush = FALSE;
547 if (GST_IS_EVENT (data)) {
548 switch (GST_EVENT_TYPE (data)) {
549 case GST_EVENT_FLUSH:
550 STATUS (queue, "received flush event");
551 gst_queue_locked_flush (queue);
552 STATUS (queue, "after flush");
555 STATUS (queue, "received EOS");
558 /* we put the event in the queue, we don't have to act ourselves */
559 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
560 "adding event %p of type %d", data, GST_EVENT_TYPE (data));
565 if (GST_IS_BUFFER (data))
566 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
567 "adding buffer %p of size %d", data, GST_BUFFER_SIZE (data));
569 /* We make space available if we're "full" according to whatever
570 * the user defined as "full". Note that this only applies to buffers.
571 * We always handle events and they don't count in our statistics. */
572 if (GST_IS_BUFFER (data) &&
573 ((queue->max_size.buffers > 0 &&
574 queue->cur_level.buffers >= queue->max_size.buffers) ||
575 (queue->max_size.bytes > 0 &&
576 queue->cur_level.bytes >= queue->max_size.bytes) ||
577 (queue->max_size.time > 0 &&
578 queue->cur_level.time >= queue->max_size.time))) {
579 GST_QUEUE_MUTEX_UNLOCK;
580 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_OVERRUN], 0);
581 GST_QUEUE_MUTEX_LOCK;
583 /* how are we going to make space for this buffer? */
584 switch (queue->leaky) {
585 /* leak current buffer */
586 case GST_QUEUE_LEAK_UPSTREAM:
587 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
588 "queue is full, leaking buffer on upstream end");
589 /* now we can clean up and exit right away */
590 GST_QUEUE_MUTEX_UNLOCK;
593 /* leak first buffer in the queue */
594 case GST_QUEUE_LEAK_DOWNSTREAM:{
595 /* this is a bit hacky. We'll manually iterate the list
596 * and find the first buffer from the head on. We'll
597 * unref that and "fix up" the GQueue object... */
599 GstData *leak = NULL;
601 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
602 "queue is full, leaking buffer on downstream end");
604 for (item = queue->queue->head; item != NULL; item = item->next) {
605 if (GST_IS_BUFFER (item->data)) {
611 /* if we didn't find anything, it means we have no buffers
612 * in here. That cannot happen, since we had >= 1 bufs */
615 /* Now remove it from the list, fixing up the GQueue
616 * CHECKME: is a queue->head the first or the last item? */
617 item = g_list_delete_link (queue->queue->head, item);
618 queue->queue->head = g_list_first (item);
619 queue->queue->tail = g_list_last (item);
620 queue->queue->length--;
622 /* and unref the data at the end. Twice, because we keep a ref
623 * to make things read-only. Also keep our list uptodate. */
624 queue->cur_level.bytes -= GST_BUFFER_SIZE (data);
625 queue->cur_level.buffers--;
626 if (GST_BUFFER_DURATION (data) != GST_CLOCK_TIME_NONE)
627 queue->cur_level.time -= GST_BUFFER_DURATION (data);
629 gst_data_unref (data);
630 gst_data_unref (data);
635 g_warning ("Unknown leaky type, using default");
638 /* don't leak. Instead, wait for space to be available */
639 case GST_QUEUE_NO_LEAK:
640 STATUS (queue, "pre-full wait");
642 while ((queue->max_size.buffers > 0 &&
643 queue->cur_level.buffers >= queue->max_size.buffers) ||
644 (queue->max_size.bytes > 0 &&
645 queue->cur_level.bytes >= queue->max_size.bytes) ||
646 (queue->max_size.time > 0 &&
647 queue->cur_level.time >= queue->max_size.time)) {
648 /* if there's a pending state change for this queue
649 * or its manager, switch back to iterator so bottom
650 * half of state change executes */
651 if (queue->interrupt) {
652 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "interrupted");
653 GST_QUEUE_MUTEX_UNLOCK;
654 if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->sinkpad),
655 GST_ELEMENT (queue))) {
658 /* if we got here because we were unlocked after a
659 * flush, we don't need to add the buffer to the
662 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
663 "not adding pending buffer after flush");
666 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
667 "adding pending buffer after interrupt");
671 if (GST_STATE (queue) != GST_STATE_PLAYING) {
672 /* this means the other end is shut down. Try to
673 * signal to resolve the error */
674 if (!queue->may_deadlock) {
675 GST_QUEUE_MUTEX_UNLOCK;
676 gst_data_unref (data);
677 GST_ELEMENT_ERROR (queue, CORE, THREAD, (NULL),
678 ("deadlock found, shutting down source pad elements"));
679 /* we don't go to out_unref here, since we want to
680 * unref the buffer *before* calling GST_ELEMENT_ERROR */
683 GST_CAT_WARNING_OBJECT (queue_dataflow, queue,
684 "%s: waiting for the app to restart "
685 "source pad elements", GST_ELEMENT_NAME (queue));
689 /* OK, we've got a serious issue here. Imagine the situation
690 * where the puller (next element) is sending an event here,
691 * so it cannot pull events from the queue, and we cannot
692 * push data further because the queue is 'full' and therefore,
693 * we wait here (and do not handle events): deadlock! to solve
694 * that, we handle pending upstream events here, too. */
695 gst_queue_handle_pending_events (queue);
697 STATUS (queue, "waiting for item_del signal from thread using qlock");
698 g_cond_wait (queue->item_del, queue->qlock);
699 STATUS (queue, "received item_del signal from thread using qlock");
702 STATUS (queue, "post-full wait");
703 GST_QUEUE_MUTEX_UNLOCK;
704 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
705 GST_QUEUE_MUTEX_LOCK;
710 /* put the buffer on the tail of the list. We keep a reference,
711 * so that the data is read-only while in here. There's a good
712 * reason to do so: we have a size and time counter, and any
713 * modification to the content could change any of the two. */
715 g_queue_push_tail (queue->queue, data);
717 /* Note that we only add buffers (not events) to the statistics */
718 if (GST_IS_BUFFER (data)) {
719 queue->cur_level.buffers++;
720 queue->cur_level.bytes += GST_BUFFER_SIZE (data);
721 if (GST_BUFFER_DURATION (data) != GST_CLOCK_TIME_NONE)
722 queue->cur_level.time += GST_BUFFER_DURATION (data);
725 STATUS (queue, "+ level");
727 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "signalling item_add");
728 g_cond_signal (queue->item_add);
729 GST_QUEUE_MUTEX_UNLOCK;
734 gst_data_unref (data);
739 gst_queue_get (GstPad * pad)
744 g_return_val_if_fail (pad != NULL, NULL);
745 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
747 queue = GST_QUEUE (gst_pad_get_parent (pad));
750 /* have to lock for thread-safety */
751 GST_QUEUE_MUTEX_LOCK;
753 if (queue->queue->length == 0 ||
754 (queue->min_threshold.buffers > 0 &&
755 queue->cur_level.buffers < queue->min_threshold.buffers) ||
756 (queue->min_threshold.bytes > 0 &&
757 queue->cur_level.bytes < queue->min_threshold.bytes) ||
758 (queue->min_threshold.time > 0 &&
759 queue->cur_level.time < queue->min_threshold.time)) {
760 GST_QUEUE_MUTEX_UNLOCK;
761 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_UNDERRUN], 0);
762 GST_QUEUE_MUTEX_LOCK;
764 STATUS (queue, "pre-empty wait");
765 while (queue->queue->length == 0 ||
766 (queue->min_threshold.buffers > 0 &&
767 queue->cur_level.buffers < queue->min_threshold.buffers) ||
768 (queue->min_threshold.bytes > 0 &&
769 queue->cur_level.bytes < queue->min_threshold.bytes) ||
770 (queue->min_threshold.time > 0 &&
771 queue->cur_level.time < queue->min_threshold.time)) {
772 /* if there's a pending state change for this queue or its
773 * manager, switch back to iterator so bottom half of state
774 * change executes. */
775 if (queue->interrupt) {
776 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "interrupted");
777 GST_QUEUE_MUTEX_UNLOCK;
778 if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->srcpad),
779 GST_ELEMENT (queue)))
780 return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
783 if (GST_STATE (queue) != GST_STATE_PLAYING) {
784 /* this means the other end is shut down */
785 if (!queue->may_deadlock) {
786 GST_QUEUE_MUTEX_UNLOCK;
787 GST_ELEMENT_ERROR (queue, CORE, THREAD, (NULL),
788 ("deadlock found, shutting down sink pad elements"));
791 GST_CAT_WARNING_OBJECT (queue_dataflow, queue,
792 "%s: waiting for the app to restart "
793 "source pad elements", GST_ELEMENT_NAME (queue));
797 STATUS (queue, "waiting for item_add");
799 if (queue->block_timeout != GST_CLOCK_TIME_NONE) {
802 g_get_current_time (&timeout);
803 g_time_val_add (&timeout, queue->block_timeout / 1000);
804 GST_LOG_OBJECT (queue, "g_cond_time_wait using qlock from thread %p",
806 if (!g_cond_timed_wait (queue->item_add, queue->qlock, &timeout)) {
807 GST_QUEUE_MUTEX_UNLOCK;
808 GST_CAT_WARNING_OBJECT (queue_dataflow, queue,
809 "Sending filler event");
810 return GST_DATA (gst_event_new_filler ());
813 GST_LOG_OBJECT (queue, "doing g_cond_wait using qlock from thread %p",
815 g_cond_wait (queue->item_add, queue->qlock);
816 GST_LOG_OBJECT (queue, "done g_cond_wait using qlock from thread %p",
819 STATUS (queue, "got item_add signal");
822 STATUS (queue, "post-empty wait");
823 GST_QUEUE_MUTEX_UNLOCK;
824 g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
825 GST_QUEUE_MUTEX_LOCK;
828 /* There's something in the list now, whatever it is */
829 data = g_queue_pop_head (queue->queue);
830 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
831 "retrieved data %p from queue", data);
836 if (GST_IS_BUFFER (data)) {
837 /* Update statistics */
838 queue->cur_level.buffers--;
839 queue->cur_level.bytes -= GST_BUFFER_SIZE (data);
840 if (GST_BUFFER_DURATION (data) != GST_CLOCK_TIME_NONE)
841 queue->cur_level.time -= GST_BUFFER_DURATION (data);
844 /* Now that we're done, we can lose our own reference to
845 * the item, since we're no longer in danger. */
846 gst_data_unref (data);
848 STATUS (queue, "after _get()");
850 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "signalling item_del");
851 g_cond_signal (queue->item_del);
852 GST_QUEUE_MUTEX_UNLOCK;
854 /* FIXME: I suppose this needs to be locked, since the EOS
855 * bit affects the pipeline state. However, that bit is
856 * locked too so it'd cause a deadlock. */
857 if (GST_IS_EVENT (data)) {
858 GstEvent *event = GST_EVENT (data);
860 switch (GST_EVENT_TYPE (event)) {
862 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
863 "queue \"%s\" eos", GST_ELEMENT_NAME (queue));
864 gst_element_set_eos (GST_ELEMENT (queue));
876 gst_queue_handle_src_event (GstPad * pad, GstEvent * event)
878 GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
881 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
882 event, GST_EVENT_TYPE (event));
883 GST_QUEUE_MUTEX_LOCK;
885 if (gst_element_get_state (GST_ELEMENT (queue)) == GST_STATE_PLAYING) {
886 GstQueueEventResponse er;
888 /* push the event to the queue and wait for upstream consumption */
891 g_mutex_lock (queue->event_lock);
892 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
893 "putting event %p (%d) on internal queue", event,
894 GST_EVENT_TYPE (event));
895 g_queue_push_tail (queue->events, &er);
896 g_mutex_unlock (queue->event_lock);
897 GST_CAT_WARNING_OBJECT (queue_dataflow, queue,
898 "Preparing for loop for event handler");
899 /* see the chain function on why this is here - it prevents a deadlock */
900 g_cond_signal (queue->item_del);
901 while (!er.handled) {
904 g_get_current_time (&timeout);
905 g_time_val_add (&timeout, 500 * 1000); /* half a second */
906 GST_LOG_OBJECT (queue, "doing g_cond_wait using qlock from thread %p",
908 if (!g_cond_timed_wait (queue->event_done, queue->qlock, &timeout) &&
910 GST_CAT_WARNING_OBJECT (queue_dataflow, queue,
911 "timeout in upstream event handling, dropping event %p (%d)",
912 er.event, GST_EVENT_TYPE (er.event));
913 g_mutex_lock (queue->event_lock);
914 /* since this queue is for src events (ie upstream), this thread is
915 * the only one that is pushing stuff on it, so we're sure that
916 * it's still the tail element. FIXME: But in practice, we should use
917 * GList instead of GQueue for this so we can remove any element in
919 g_queue_pop_tail (queue->events);
920 g_mutex_unlock (queue->event_lock);
921 gst_event_unref (er.event);
926 GST_CAT_WARNING_OBJECT (queue_dataflow, queue, "Event handled");
929 res = gst_pad_event_default (pad, event);
931 switch (GST_EVENT_TYPE (event)) {
932 case GST_EVENT_FLUSH:
933 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
934 "FLUSH event, flushing queue\n");
935 gst_queue_locked_flush (queue);
938 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH) {
939 gst_queue_locked_flush (queue);
946 GST_QUEUE_MUTEX_UNLOCK;
952 gst_queue_handle_src_query (GstPad * pad,
953 GstQueryType type, GstFormat * fmt, gint64 * value)
955 GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
958 res = gst_pad_query (GST_PAD_PEER (queue->sinkpad), type, fmt, value);
962 if (type == GST_QUERY_POSITION) {
963 /* FIXME: this code assumes that there's no discont in the queue */
965 case GST_FORMAT_BYTES:
966 *value -= queue->cur_level.bytes;
968 case GST_FORMAT_TIME:
969 *value -= queue->cur_level.time;
981 gst_queue_release_locks (GstElement * element)
985 queue = GST_QUEUE (element);
987 GST_QUEUE_MUTEX_LOCK;
988 queue->interrupt = TRUE;
989 g_cond_signal (queue->item_add);
990 g_cond_signal (queue->item_del);
991 GST_QUEUE_MUTEX_UNLOCK;
996 static GstElementStateReturn
997 gst_queue_change_state (GstElement * element)
1000 GstElementStateReturn ret = GST_STATE_SUCCESS;
1002 queue = GST_QUEUE (element);
1004 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "starting state change");
1006 /* lock the queue so another thread (not in sync with this thread's state)
1007 * can't call this queue's _get (or whatever)
1009 GST_QUEUE_MUTEX_LOCK;
1011 switch (GST_STATE_TRANSITION (element)) {
1012 case GST_STATE_NULL_TO_READY:
1013 gst_queue_locked_flush (queue);
1015 case GST_STATE_PAUSED_TO_PLAYING:
1016 if (!GST_PAD_IS_LINKED (queue->sinkpad)) {
1017 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, queue,
1018 "queue %s is not linked", GST_ELEMENT_NAME (queue));
1019 /* FIXME can this be? */
1020 g_cond_signal (queue->item_add);
1022 ret = GST_STATE_FAILURE;
1025 GstScheduler *src_sched, *sink_sched;
1027 src_sched = gst_pad_get_scheduler (GST_PAD (queue->srcpad));
1028 sink_sched = gst_pad_get_scheduler (GST_PAD (queue->sinkpad));
1030 if (src_sched == sink_sched) {
1031 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, queue,
1032 "queue %s does not connect different schedulers",
1033 GST_ELEMENT_NAME (queue));
1035 g_warning ("queue %s does not connect different schedulers",
1036 GST_ELEMENT_NAME (queue));
1038 ret = GST_STATE_FAILURE;
1042 queue->interrupt = FALSE;
1044 case GST_STATE_PAUSED_TO_READY:
1045 gst_queue_locked_flush (queue);
1046 gst_caps_replace (&queue->negotiated_caps, NULL);
1052 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1053 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
1055 /* this is an ugly hack to make sure our pads are always active.
1056 * Reason for this is that pad activation for the queue element
1057 * depends on 2 schedulers (ugh) */
1058 gst_pad_set_active (queue->sinkpad, TRUE);
1059 gst_pad_set_active (queue->srcpad, TRUE);
1062 GST_QUEUE_MUTEX_UNLOCK;
1064 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "done with state change");
1071 gst_queue_set_property (GObject * object,
1072 guint prop_id, const GValue * value, GParamSpec * pspec)
1074 GstQueue *queue = GST_QUEUE (object);
1076 /* someone could change levels here, and since this
1077 * affects the get/put funcs, we need to lock for safety. */
1078 GST_QUEUE_MUTEX_LOCK;
1081 case ARG_MAX_SIZE_BYTES:
1082 queue->max_size.bytes = g_value_get_uint (value);
1084 case ARG_MAX_SIZE_BUFFERS:
1085 queue->max_size.buffers = g_value_get_uint (value);
1087 case ARG_MAX_SIZE_TIME:
1088 queue->max_size.time = g_value_get_uint64 (value);
1090 case ARG_MIN_THRESHOLD_BYTES:
1091 queue->min_threshold.bytes = g_value_get_uint (value);
1093 case ARG_MIN_THRESHOLD_BUFFERS:
1094 queue->min_threshold.buffers = g_value_get_uint (value);
1096 case ARG_MIN_THRESHOLD_TIME:
1097 queue->min_threshold.time = g_value_get_uint64 (value);
1100 queue->leaky = g_value_get_enum (value);
1102 case ARG_MAY_DEADLOCK:
1103 queue->may_deadlock = g_value_get_boolean (value);
1105 case ARG_BLOCK_TIMEOUT:
1106 queue->block_timeout = g_value_get_uint64 (value);
1109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1113 GST_QUEUE_MUTEX_UNLOCK;
1117 gst_queue_get_property (GObject * object,
1118 guint prop_id, GValue * value, GParamSpec * pspec)
1120 GstQueue *queue = GST_QUEUE (object);
1123 case ARG_CUR_LEVEL_BYTES:
1124 g_value_set_uint (value, queue->cur_level.bytes);
1126 case ARG_CUR_LEVEL_BUFFERS:
1127 g_value_set_uint (value, queue->cur_level.buffers);
1129 case ARG_CUR_LEVEL_TIME:
1130 g_value_set_uint64 (value, queue->cur_level.time);
1132 case ARG_MAX_SIZE_BYTES:
1133 g_value_set_uint (value, queue->max_size.bytes);
1135 case ARG_MAX_SIZE_BUFFERS:
1136 g_value_set_uint (value, queue->max_size.buffers);
1138 case ARG_MAX_SIZE_TIME:
1139 g_value_set_uint64 (value, queue->max_size.time);
1141 case ARG_MIN_THRESHOLD_BYTES:
1142 g_value_set_uint (value, queue->min_threshold.bytes);
1144 case ARG_MIN_THRESHOLD_BUFFERS:
1145 g_value_set_uint (value, queue->min_threshold.buffers);
1147 case ARG_MIN_THRESHOLD_TIME:
1148 g_value_set_uint64 (value, queue->min_threshold.time);
1151 g_value_set_enum (value, queue->leaky);
1153 case ARG_MAY_DEADLOCK:
1154 g_value_set_boolean (value, queue->may_deadlock);
1156 case ARG_BLOCK_TIMEOUT:
1157 g_value_set_uint64 (value, queue->block_timeout);
1160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);