2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 //#define DEBUG_ENABLED
24 //#define STATUS_ENABLED
26 #define STATUS(A) GST_DEBUG(GST_CAT_DATAFLOW, A, GST_ELEMENT_NAME(queue))
34 #include "gst_private.h"
37 #include "gstscheduler.h"
39 GstElementDetails gst_queue_details = {
44 "Erik Walthinsen <omega@cse.ogi.edu>",
49 /* Queue signals and args */
70 static void gst_queue_class_init (GstQueueClass *klass);
71 static void gst_queue_init (GstQueue *queue);
73 static void gst_queue_set_property (GObject *object, guint prop_id,
74 const GValue *value, GParamSpec *pspec);
75 static void gst_queue_get_property (GObject *object, guint prop_id,
76 GValue *value, GParamSpec *pspec);
78 static GstPadNegotiateReturn gst_queue_handle_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data);
79 static GstPadNegotiateReturn gst_queue_handle_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data);
80 static void gst_queue_chain (GstPad *pad, GstBuffer *buf);
81 static GstBuffer * gst_queue_get (GstPad *pad);
82 static GstBufferPool* gst_queue_get_bufferpool (GstPad *pad);
84 static void gst_queue_locked_flush (GstQueue *queue);
85 static void gst_queue_flush (GstQueue *queue);
87 static GstElementStateReturn gst_queue_change_state (GstElement *element);
90 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type())
92 queue_leaky_get_type(void) {
93 static GType queue_leaky_type = 0;
94 static GEnumValue queue_leaky[] = {
95 { GST_QUEUE_NO_LEAK, "0", "Not Leaky" },
96 { GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream" },
97 { GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream" },
100 if (!queue_leaky_type) {
101 queue_leaky_type = g_enum_register_static("GstQueueLeaky", queue_leaky);
103 return queue_leaky_type;
106 static GstElementClass *parent_class = NULL;
107 //static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
110 gst_queue_get_type(void)
112 static GType queue_type = 0;
115 static const GTypeInfo queue_info = {
116 sizeof(GstQueueClass),
119 (GClassInitFunc)gst_queue_class_init,
124 (GInstanceInitFunc)gst_queue_init,
127 queue_type = g_type_register_static (GST_TYPE_ELEMENT, "GstQueue", &queue_info, 0);
133 gst_queue_class_init (GstQueueClass *klass)
135 GObjectClass *gobject_class;
136 GstElementClass *gstelement_class;
138 gobject_class = (GObjectClass*)klass;
139 gstelement_class = (GstElementClass*)klass;
141 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
143 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LEAKY,
144 g_param_spec_enum("leaky","Leaky","Where the queue leaks, if at all.",
145 GST_TYPE_QUEUE_LEAKY,GST_QUEUE_NO_LEAK,G_PARAM_READWRITE));
146 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LEVEL,
147 g_param_spec_int("level","Level","How many buffers are in the queue.",
148 0,G_MAXINT,0,G_PARAM_READABLE));
149 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MAX_LEVEL,
150 g_param_spec_int("max_level","Maximum Level","How many buffers the queue holds.",
151 0,G_MAXINT,100,G_PARAM_READWRITE));
153 gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_queue_set_property);
154 gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_queue_get_property);
156 gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_queue_change_state);
160 gst_queue_init (GstQueue *queue)
162 // scheduling on this kind of element is, well, interesting
163 GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
164 GST_FLAG_SET (queue, GST_ELEMENT_EVENT_AWARE);
166 queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
167 gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_chain));
168 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
169 gst_pad_set_negotiate_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_handle_negotiate_sink));
170 gst_pad_set_bufferpool_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_get_bufferpool));
172 queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
173 gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_get));
174 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
175 gst_pad_set_negotiate_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_handle_negotiate_src));
178 queue->level_buffers = 0;
179 queue->level_bytes = 0;
180 queue->level_time = 0LL;
181 queue->size_buffers = 100; // 100 buffers
182 queue->size_bytes = 100 * 1024; // 100KB
183 queue->size_time = 1000000000LL; // 1sec
185 queue->qlock = g_mutex_new ();
186 queue->reader = FALSE;
187 queue->writer = FALSE;
188 queue->not_empty = g_cond_new ();
189 queue->not_full = g_cond_new ();
190 GST_DEBUG_ELEMENT (GST_CAT_THREAD, queue, "initialized queue's not_empty & not_full conditions\n");
193 static GstBufferPool*
194 gst_queue_get_bufferpool (GstPad *pad)
198 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
200 return gst_pad_get_bufferpool (queue->srcpad);
203 static GstPadNegotiateReturn
204 gst_queue_handle_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data)
208 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
210 return gst_pad_negotiate_proxy (pad, queue->sinkpad, caps);
213 static GstPadNegotiateReturn
214 gst_queue_handle_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data)
218 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
220 return gst_pad_negotiate_proxy (pad, queue->srcpad, caps);
224 gst_queue_cleanup_buffers (gpointer data, const gpointer user_data)
226 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, user_data, "cleaning buffer %p\n", data);
228 if (GST_IS_BUFFER (data)) {
229 gst_buffer_unref (GST_BUFFER (data));
234 gst_queue_locked_flush (GstQueue *queue)
236 g_slist_foreach (queue->queue, gst_queue_cleanup_buffers,
238 g_slist_free (queue->queue);
241 queue->level_buffers = 0;
242 queue->timeval = NULL;
246 gst_queue_flush (GstQueue *queue)
248 g_mutex_lock (queue->qlock);
249 gst_queue_locked_flush (queue);
250 g_mutex_unlock (queue->qlock);
255 gst_queue_chain (GstPad *pad, GstBuffer *buf)
260 g_return_if_fail (pad != NULL);
261 g_return_if_fail (GST_IS_PAD (pad));
262 g_return_if_fail (buf != NULL);
264 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
268 /* we have to lock the queue since we span threads */
269 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%ld\n", pthread_self ());
270 g_mutex_lock (queue->qlock);
271 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%ld\n", pthread_self ());
273 if (GST_IS_EVENT (buf)) {
274 switch (GST_EVENT_TYPE (buf)) {
275 case GST_EVENT_FLUSH:
276 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
277 gst_queue_locked_flush (queue);
282 gst_pad_event_default (pad, GST_EVENT (buf));
287 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding buffer %p of size %d\n",buf,GST_BUFFER_SIZE(buf));
289 if (queue->level_buffers == queue->size_buffers) {
290 // if this is a leaky queue...
292 // FIXME don't want to leak events!
293 // if we leak on the upstream side, drop the current buffer
294 if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
295 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end\n");
296 if (GST_IS_EVENT (buf))
297 fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
298 GST_ELEMENT_NAME(GST_ELEMENT(queue)),
299 GST_EVENT_TYPE(GST_EVENT(buf)));
300 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end\n");
301 gst_buffer_unref(buf);
302 // now we have to clean up and exit right away
303 g_mutex_unlock (queue->qlock);
306 // otherwise we have to push a buffer off the other end
310 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end\n");
311 front = queue->queue;
312 leakbuf = (GstBuffer *)(front->data);
313 if (GST_IS_EVENT (leakbuf))
314 fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
315 GST_ELEMENT_NAME(GST_ELEMENT(queue)),
316 GST_EVENT_TYPE(GST_EVENT(leakbuf)));
317 queue->level_buffers--;
318 queue->level_bytes -= GST_BUFFER_SIZE(leakbuf);
319 gst_buffer_unref(leakbuf);
320 queue->queue = g_slist_remove_link (queue->queue, front);
321 g_slist_free (front);
325 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre full wait, level:%d/%d\n",
326 queue->level_buffers, queue->size_buffers);
327 while (queue->level_buffers == queue->size_buffers) {
328 // if there's a pending state change for this queue or its manager, switch
329 // back to iterator so bottom half of state change executes
330 if (GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING ||
331 // GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING)
332 GST_STATE_PENDING(GST_SCHED_PARENT(GST_ELEMENT_SCHED(GST_PAD_PARENT(GST_PAD_PEER(queue->sinkpad))))) !=
333 GST_STATE_VOID_PENDING)
335 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!\n");
336 if (GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING)
337 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING)\n");
338 if (GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING)
339 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING\n");
340 g_mutex_unlock (queue->qlock);
341 cothread_switch(cothread_current_main());
344 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_full, level:%d/%d\n", queue->level_buffers, queue->size_buffers);
346 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "WARNING: multiple writers on queue!\n");
347 queue->writer = TRUE;
348 g_cond_wait (queue->not_full, queue->qlock);
349 queue->writer = FALSE;
350 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_full signal\n");
352 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post full wait, level:%d/%d\n",
353 queue->level_buffers, queue->size_buffers);
356 /* put the buffer on the tail of the list */
357 queue->queue = g_slist_append (queue->queue, buf);
358 queue->level_buffers++;
359 queue->level_bytes += GST_BUFFER_SIZE(buf);
360 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)+ level:%d/%d\n",
361 GST_DEBUG_PAD_NAME(pad),
362 queue->level_buffers, queue->size_buffers);
364 /* reader waiting on an empty queue */
365 reader = queue->reader;
367 g_mutex_unlock (queue->qlock);
371 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_empty\n");
372 g_cond_signal (queue->not_empty);
377 gst_queue_get (GstPad *pad)
380 GstBuffer *buf = NULL;
384 g_assert(pad != NULL);
385 g_assert(GST_IS_PAD(pad));
386 g_return_val_if_fail (pad != NULL, NULL);
387 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
389 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
393 /* have to lock for thread-safety */
394 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%ld\n", pthread_self ());
395 g_mutex_lock (queue->qlock);
396 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%ld %p\n", pthread_self (), queue->not_empty);
398 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre empty wait, level:%d/%d\n", queue->level_buffers, queue->size_buffers);
399 while (queue->level_buffers == 0) {
400 // if there's a pending state change for this queue or its manager, switch
401 // back to iterator so bottom half of state change executes
402 if (GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING ||
403 // GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING)
404 GST_STATE_PENDING(GST_SCHED_PARENT(GST_ELEMENT_SCHED(GST_PAD_PARENT(GST_PAD_PEER(queue->srcpad))))) !=
405 GST_STATE_VOID_PENDING)
407 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!\n");
408 if (GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING)
409 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING)\n");
410 if (GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING)
411 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING\n");
412 g_mutex_unlock (queue->qlock);
413 cothread_switch(cothread_current_main());
416 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_empty, level:%d/%d\n", queue->level_buffers, queue->size_buffers);
418 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "WARNING: multiple readers on queue!\n");
419 queue->reader = TRUE;
420 g_cond_wait (queue->not_empty, queue->qlock);
421 queue->reader = FALSE;
422 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_empty signal\n");
424 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post empty wait, level:%d/%d\n", queue->level_buffers, queue->size_buffers);
426 front = queue->queue;
427 buf = (GstBuffer *)(front->data);
428 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "retrieved buffer %p from queue\n", buf);
429 queue->queue = g_slist_remove_link (queue->queue, front);
430 g_slist_free (front);
432 queue->level_buffers--;
433 queue->level_bytes -= GST_BUFFER_SIZE(buf);
434 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)- level:%d/%d\n",
435 GST_DEBUG_PAD_NAME(pad),
436 queue->level_buffers, queue->size_buffers);
438 /* writer waiting on a full queue */
439 writer = queue->writer;
441 g_mutex_unlock (queue->qlock);
445 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_full\n");
446 g_cond_signal (queue->not_full);
449 // FIXME where should this be? locked?
450 if (GST_IS_EVENT(buf)) {
451 GstEvent *event = GST_EVENT(buf);
452 switch (GST_EVENT_TYPE(event)) {
454 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue eos\n");
455 gst_element_set_state (GST_ELEMENT (queue), GST_STATE_PAUSED);
465 static GstElementStateReturn
466 gst_queue_change_state (GstElement *element)
469 GstElementStateReturn ret;
470 g_return_val_if_fail (GST_IS_QUEUE (element), GST_STATE_FAILURE);
472 queue = GST_QUEUE (element);
474 // lock the queue so another thread (not in sync with this thread's state)
475 // can't call this queue's _get (or whatever)
478 /* if going down into NULL state, clear out buffers*/
479 if (GST_STATE_PENDING (element) == GST_STATE_READY) {
480 /* otherwise (READY or higher) we need to open the file */
481 gst_queue_flush (queue);
484 // if we haven't failed already, give the parent class a chance to ;-)
485 if (GST_ELEMENT_CLASS (parent_class)->change_state)
487 gboolean valid_handler = FALSE;
488 guint state_change_id = g_signal_lookup("state_change", G_OBJECT_TYPE(element));
490 // determine whether we need to block the parent (element) class'
491 // STATE_CHANGE signal so we can UNLOCK before returning. we block
492 // it if we could find the state_change signal AND there's a signal
493 // handler attached to it.
495 // note: this assumes that change_state() *only* emits state_change signal.
496 // if element change_state() emits other signals, they need to be blocked
498 if (state_change_id &&
499 g_signal_has_handler_pending(G_OBJECT(element), state_change_id, 0, FALSE))
500 valid_handler = TRUE;
502 g_signal_handler_block(G_OBJECT(element), state_change_id);
504 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
507 g_signal_handler_unblock(G_OBJECT(element), state_change_id);
509 // UNLOCK, *then* emit signal (if there's one there)
512 g_signal_emit(G_OBJECT (element), state_change_id, 0, GST_STATE(element));
516 ret = GST_STATE_SUCCESS;
525 gst_queue_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
529 /* it's not null if we got it, but it might not be ours */
530 g_return_if_fail (GST_IS_QUEUE (object));
532 queue = GST_QUEUE (object);
536 queue->leaky = g_value_get_int(value);
539 queue->size_buffers = g_value_get_int(value);
542 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
548 gst_queue_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
552 /* it's not null if we got it, but it might not be ours */
553 g_return_if_fail (GST_IS_QUEUE (object));
555 queue = GST_QUEUE (object);
559 g_value_set_int(value, queue->leaky);
562 g_value_set_int(value, queue->level_buffers);
565 g_value_set_int(value, queue->size_buffers);
568 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);