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_arg (GtkObject *object, GtkArg *arg, guint id);
74 static void gst_queue_get_arg (GtkObject *object, GtkArg *arg, guint id);
76 static gboolean gst_queue_handle_eos (GstPad *pad);
77 static GstPadNegotiateReturn gst_queue_handle_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data);
78 static GstPadNegotiateReturn gst_queue_handle_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data);
79 static void gst_queue_chain (GstPad *pad, GstBuffer *buf);
80 static GstBuffer * gst_queue_get (GstPad *pad);
81 static GstBufferPool* gst_queue_get_bufferpool (GstPad *pad);
83 static void gst_queue_flush (GstQueue *queue);
85 static GstElementStateReturn gst_queue_change_state (GstElement *element);
89 queue_leaky_get_type(void) {
90 static GtkType queue_leaky_type = 0;
91 static GtkEnumValue queue_leaky[] = {
92 { GST_QUEUE_NO_LEAK, "0", "Not Leaky" },
93 { GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream" },
94 { GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream" },
97 if (!queue_leaky_type) {
98 queue_leaky_type = gtk_type_register_enum("GstQueueLeaky", queue_leaky);
100 return queue_leaky_type;
102 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type())
105 static GstElementClass *parent_class = NULL;
106 //static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
109 gst_queue_get_type(void) {
110 static GtkType queue_type = 0;
113 static const GtkTypeInfo queue_info = {
116 sizeof(GstQueueClass),
117 (GtkClassInitFunc)gst_queue_class_init,
118 (GtkObjectInitFunc)gst_queue_init,
119 (GtkArgSetFunc)gst_queue_set_arg,
120 (GtkArgGetFunc)gst_queue_get_arg,
121 (GtkClassInitFunc)NULL,
123 queue_type = gtk_type_unique (GST_TYPE_ELEMENT, &queue_info);
129 gst_queue_class_init (GstQueueClass *klass)
131 GtkObjectClass *gtkobject_class;
132 GstElementClass *gstelement_class;
134 gtkobject_class = (GtkObjectClass*)klass;
135 gstelement_class = (GstElementClass*)klass;
137 parent_class = gtk_type_class (GST_TYPE_ELEMENT);
139 gtk_object_add_arg_type ("GstQueue::leaky", GST_TYPE_QUEUE_LEAKY,
140 GTK_ARG_READWRITE, ARG_LEAKY);
141 gtk_object_add_arg_type ("GstQueue::level", GTK_TYPE_INT,
142 GTK_ARG_READABLE, ARG_LEVEL);
143 gtk_object_add_arg_type ("GstQueue::max_level", GTK_TYPE_INT,
144 GTK_ARG_READWRITE, ARG_MAX_LEVEL);
146 gtkobject_class->set_arg = gst_queue_set_arg;
147 gtkobject_class->get_arg = gst_queue_get_arg;
149 gstelement_class->change_state = gst_queue_change_state;
153 gst_queue_init (GstQueue *queue)
155 // scheduling on this kind of element is, well, interesting
156 GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
158 queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
159 gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_chain));
160 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
161 gst_pad_set_eos_function (queue->sinkpad, gst_queue_handle_eos);
162 gst_pad_set_negotiate_function (queue->sinkpad, gst_queue_handle_negotiate_sink);
163 gst_pad_set_bufferpool_function (queue->sinkpad, gst_queue_get_bufferpool);
165 queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
166 gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_get));
167 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
168 gst_pad_set_negotiate_function (queue->srcpad, gst_queue_handle_negotiate_src);
171 queue->level_buffers = 0;
172 queue->level_bytes = 0;
173 queue->level_time = 0LL;
174 queue->size_buffers = 100; // 100 buffers
175 queue->size_bytes = 100 * 1024; // 100KB
176 queue->size_time = 1000000000LL; // 1sec
178 queue->emptycond = g_cond_new ();
179 queue->fullcond = g_cond_new ();
180 GST_DEBUG(GST_CAT_THREAD, "initialized queue's emptycond and fullcond\n");
183 static GstBufferPool*
184 gst_queue_get_bufferpool (GstPad *pad)
188 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
190 return gst_pad_get_bufferpool (queue->srcpad);
193 static GstPadNegotiateReturn
194 gst_queue_handle_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data)
198 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
200 return gst_pad_negotiate_proxy (pad, queue->sinkpad, caps);
203 static GstPadNegotiateReturn
204 gst_queue_handle_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data)
208 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
210 return gst_pad_negotiate_proxy (pad, queue->srcpad, caps);
214 gst_queue_handle_eos (GstPad *pad)
218 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
220 GST_DEBUG (GST_CAT_DATAFLOW,"%s received eos\n", GST_ELEMENT_NAME (queue));
223 GST_DEBUG (GST_CAT_DATAFLOW,"%s has %d buffers left\n", GST_ELEMENT_NAME (queue),
224 queue->level_buffers);
226 GST_FLAG_SET (pad, GST_PAD_EOS);
228 g_cond_signal (queue->emptycond);
236 gst_queue_cleanup_buffers (gpointer data, const gpointer user_data)
238 GST_DEBUG (GST_CAT_DATAFLOW,"%s cleaning buffer %p\n", (gchar *)user_data, data);
240 gst_buffer_unref (GST_BUFFER (data));
244 gst_queue_flush (GstQueue *queue)
246 g_slist_foreach (queue->queue, gst_queue_cleanup_buffers,
247 (char *) GST_ELEMENT_NAME (queue));
248 g_slist_free (queue->queue);
251 queue->level_buffers = 0;
252 queue->timeval = NULL;
256 gst_queue_chain (GstPad *pad, GstBuffer *buf)
261 g_return_if_fail (pad != NULL);
262 g_return_if_fail (GST_IS_PAD (pad));
263 g_return_if_fail (buf != NULL);
265 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
266 name = GST_ELEMENT_NAME (queue);
268 /* we have to lock the queue since we span threads */
270 // GST_DEBUG (GST_CAT_DATAFLOW,"trying to get lock on queue \"%s\"\n",name);
273 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLUSH)) {
274 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "buffer has FLUSH bit set, flushing queue\n");
275 gst_queue_flush (queue);
278 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding buffer %p of size %d\n",buf,GST_BUFFER_SIZE(buf));
280 if (queue->level_buffers >= queue->size_buffers) {
281 // if this is a leaky queue...
283 // if we leak on the upstream side, drop the current buffer
284 if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
285 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end\n");
286 gst_buffer_unref(buf);
287 // now we have to clean up and exit right away
291 // otherwise we have to push a buffer off the other end
295 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end\n");
296 front = queue->queue;
297 leakbuf = (GstBuffer *)(front->data);
298 queue->level_buffers--;
299 queue->level_bytes -= GST_BUFFER_SIZE(leakbuf);
300 gst_buffer_unref(leakbuf);
301 queue->queue = g_slist_remove_link (queue->queue, front);
302 g_slist_free (front);
306 while (queue->level_buffers >= queue->size_buffers) {
307 // if there's a pending state change for this queue or its manager, switch
308 // back to iterator so bottom half of state change executes
309 if (GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING ||
310 // GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING)
311 GST_STATE_PENDING(GST_SCHED_PARENT(GST_ELEMENT_SCHED(GST_PAD_PARENT(GST_PAD_PEER(queue->sinkpad))))) !=
312 GST_STATE_NONE_PENDING)
314 GST_DEBUG(GST_CAT_DATAFLOW,"interrupted!!\n");
315 if (GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING)
316 GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING)\n");
317 if (GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING)
318 GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING\n");
320 cothread_switch(cothread_current_main());
323 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for space, level is %d\n", queue->level_buffers);
324 g_cond_signal (queue->emptycond);
325 g_cond_wait (queue->fullcond, GST_OBJECT(queue)->lock);
326 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "done waiting, level is now %d\n", queue->level_buffers);
330 /* put the buffer on the tail of the list */
331 queue->queue = g_slist_append (queue->queue, buf);
332 queue->level_buffers++;
333 queue->level_bytes += GST_BUFFER_SIZE(buf);
334 // GST_DEBUG (GST_CAT_DATAFLOW, "(%s:%s)+\n",GST_DEBUG_PAD_NAME(pad));
336 /* if we were empty, but aren't any more, signal a condition */
337 if (queue->level_buffers == 1)
339 GST_DEBUG (GST_CAT_DATAFLOW,"%s signalling emptycond\n", name);
340 g_cond_signal (queue->emptycond);
347 gst_queue_get (GstPad *pad)
350 GstBuffer *buf = NULL;
354 g_assert(pad != NULL);
355 g_assert(GST_IS_PAD(pad));
356 g_return_val_if_fail (pad != NULL, NULL);
357 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
359 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
360 name = GST_ELEMENT_NAME (queue);
362 /* have to lock for thread-safety */
363 GST_DEBUG (GST_CAT_DATAFLOW,"%s try have queue lock\n", name);
365 GST_DEBUG (GST_CAT_DATAFLOW,"%s push %d %ld %p\n", name, queue->level_buffers, pthread_self (), queue->emptycond);
366 GST_DEBUG (GST_CAT_DATAFLOW,"%s have queue lock\n", name);
368 while (!queue->level_buffers) {
369 if (GST_FLAG_IS_SET (queue->sinkpad, GST_PAD_EOS)) {
370 GST_DEBUG (GST_CAT_DATAFLOW, "%s U released lock\n", name);
372 gst_pad_set_eos (queue->srcpad);
373 // this return NULL shouldn't hurt anything...
377 // if there's a pending state change for this queue or its manager, switch
378 // back to iterator so bottom half of state change executes
379 if (GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING ||
380 // GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING)
381 GST_STATE_PENDING(GST_SCHED_PARENT(GST_ELEMENT_SCHED(GST_PAD_PARENT(GST_PAD_PEER(queue->srcpad))))) !=
382 GST_STATE_NONE_PENDING)
384 GST_DEBUG(GST_CAT_DATAFLOW,"interrupted!!\n");
385 if (GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING)
386 GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING)\n");
387 if (GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING)
388 GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING\n");
390 cothread_switch(cothread_current_main());
393 g_cond_signal (queue->fullcond);
394 g_cond_wait (queue->emptycond, GST_OBJECT(queue)->lock);
397 front = queue->queue;
398 buf = (GstBuffer *)(front->data);
399 GST_DEBUG (GST_CAT_DATAFLOW,"retrieved buffer %p from queue\n",buf);
400 queue->queue = g_slist_remove_link (queue->queue, front);
401 g_slist_free (front);
403 // if (queue->level_buffers < queue->size_buffers)
404 if (queue->level_buffers == queue->size_buffers)
406 GST_DEBUG (GST_CAT_DATAFLOW,"%s signalling fullcond\n", name);
407 g_cond_signal (queue->fullcond);
410 queue->level_buffers--;
411 queue->level_bytes -= GST_BUFFER_SIZE(buf);
412 GST_DEBUG (GST_CAT_DATAFLOW,"(%s:%s)- ",GST_DEBUG_PAD_NAME(pad));
419 static GstElementStateReturn
420 gst_queue_change_state (GstElement *element)
423 GstElementStateReturn ret;
424 g_return_val_if_fail (GST_IS_QUEUE (element), GST_STATE_FAILURE);
426 queue = GST_QUEUE (element);
428 // lock the queue so another thread (not in sync with this thread's state)
429 // can't call this queue's _get (or whatever)
432 /* if going down into NULL state, clear out buffers*/
433 if (GST_STATE_PENDING (element) == GST_STATE_READY) {
434 /* otherwise (READY or higher) we need to open the file */
435 gst_queue_flush (queue);
438 /* if we haven't failed already, give the parent class a chance to ;-) */
439 if (GST_ELEMENT_CLASS (parent_class)->change_state)
441 gboolean valid_handler = FALSE;
442 guint state_change_id = gtk_signal_lookup("state_change", GTK_OBJECT_TYPE(element));
444 // determine whether we need to block the parent (element) class'
445 // STATE_CHANGE signal so we can UNLOCK before returning. we block
446 // it if we could find the state_change signal AND there's a signal
447 // handler attached to it.
449 // note: this assumes that change_state() *only* emits state_change signal.
450 // if element change_state() emits other signals, they need to be blocked
452 if (state_change_id &&
453 gtk_signal_handler_pending(GTK_OBJECT(element), state_change_id, FALSE))
454 valid_handler = TRUE;
456 gtk_signal_handler_block(GTK_OBJECT(element), state_change_id);
458 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
461 gtk_signal_handler_unblock(GTK_OBJECT(element), state_change_id);
463 // UNLOCK, *then* emit signal (if there's one there)
466 gtk_signal_emit(GTK_OBJECT (element), state_change_id, GST_STATE(element));
470 ret = GST_STATE_SUCCESS;
479 gst_queue_set_arg (GtkObject *object, GtkArg *arg, guint id)
483 /* it's not null if we got it, but it might not be ours */
484 g_return_if_fail (GST_IS_QUEUE (object));
486 queue = GST_QUEUE (object);
490 queue->leaky = GTK_VALUE_INT (*arg);
493 queue->size_buffers = GTK_VALUE_INT (*arg);
501 gst_queue_get_arg (GtkObject *object, GtkArg *arg, guint id)
505 /* it's not null if we got it, but it might not be ours */
506 g_return_if_fail (GST_IS_QUEUE (object));
508 queue = GST_QUEUE (object);
512 GTK_VALUE_INT (*arg) = queue->leaky;
515 GTK_VALUE_INT (*arg) = queue->level_buffers;
518 GTK_VALUE_INT (*arg) = queue->size_buffers;
521 arg->type = GTK_TYPE_INVALID;