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_flush (GstQueue *queue);
86 static GstElementStateReturn gst_queue_change_state (GstElement *element);
89 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type())
91 queue_leaky_get_type(void) {
92 static GType queue_leaky_type = 0;
93 static GEnumValue queue_leaky[] = {
94 { GST_QUEUE_NO_LEAK, "0", "Not Leaky" },
95 { GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream" },
96 { GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream" },
99 if (!queue_leaky_type) {
100 queue_leaky_type = g_enum_register_static("GstQueueLeaky", queue_leaky);
102 return queue_leaky_type;
105 static GstElementClass *parent_class = NULL;
106 //static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
109 gst_queue_get_type(void)
111 static GType queue_type = 0;
114 static const GTypeInfo queue_info = {
115 sizeof(GstQueueClass),
118 (GClassInitFunc)gst_queue_class_init,
123 (GInstanceInitFunc)gst_queue_init,
125 queue_type = g_type_register_static (GST_TYPE_ELEMENT, "GstQueue", &queue_info, 0);
131 gst_queue_class_init (GstQueueClass *klass)
133 GObjectClass *gobject_class;
134 GstElementClass *gstelement_class;
136 gobject_class = (GObjectClass*)klass;
137 gstelement_class = (GstElementClass*)klass;
139 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
141 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LEAKY,
142 g_param_spec_enum("leaky","Leaky","Where the queue leaks, if at all.",
143 GST_TYPE_QUEUE_LEAKY,GST_QUEUE_NO_LEAK,G_PARAM_READWRITE));
144 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LEVEL,
145 g_param_spec_int("level","Level","How many buffers are in the queue.",
146 0,G_MAXINT,0,G_PARAM_READABLE));
147 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MAX_LEVEL,
148 g_param_spec_int("max_level","Maximum Level","How many buffers the queue holds.",
149 0,G_MAXINT,100,G_PARAM_READWRITE));
151 gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_queue_set_property);
152 gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_queue_get_property);
154 gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_queue_change_state);
158 gst_queue_init (GstQueue *queue)
160 // scheduling on this kind of element is, well, interesting
161 GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
163 queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
164 gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_chain));
165 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
166 gst_pad_set_negotiate_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_handle_negotiate_sink));
167 gst_pad_set_bufferpool_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_get_bufferpool));
169 queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
170 gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_get));
171 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
172 gst_pad_set_negotiate_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_handle_negotiate_src));
175 queue->level_buffers = 0;
176 queue->level_bytes = 0;
177 queue->level_time = 0LL;
178 queue->size_buffers = 100; // 100 buffers
179 queue->size_bytes = 100 * 1024; // 100KB
180 queue->size_time = 1000000000LL; // 1sec
182 queue->emptycond = g_cond_new ();
183 queue->fullcond = g_cond_new ();
184 GST_DEBUG(GST_CAT_THREAD, "initialized queue's emptycond and fullcond\n");
187 static GstBufferPool*
188 gst_queue_get_bufferpool (GstPad *pad)
192 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
194 return gst_pad_get_bufferpool (queue->srcpad);
197 static GstPadNegotiateReturn
198 gst_queue_handle_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data)
202 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
204 return gst_pad_negotiate_proxy (pad, queue->sinkpad, caps);
207 static GstPadNegotiateReturn
208 gst_queue_handle_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data)
212 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
214 return gst_pad_negotiate_proxy (pad, queue->srcpad, caps);
218 gst_queue_handle_event (GstPad *pad)
222 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
224 GST_DEBUG (GST_CAT_DATAFLOW,"%s received event\n", GST_ELEMENT_NAME (queue));
227 GST_DEBUG (GST_CAT_DATAFLOW,"%s has %d buffers left\n", GST_ELEMENT_NAME (queue),
228 queue->level_buffers);
230 GST_FLAG_SET (pad, GST_PAD_EOS);
232 g_cond_signal (queue->emptycond);
240 gst_queue_cleanup_buffers (gpointer data, const gpointer user_data)
242 GST_DEBUG (GST_CAT_DATAFLOW,"%s cleaning buffer %p\n", (gchar *)user_data, data);
244 gst_buffer_unref (GST_BUFFER (data));
248 gst_queue_flush (GstQueue *queue)
250 g_slist_foreach (queue->queue, gst_queue_cleanup_buffers,
251 (char *) GST_ELEMENT_NAME (queue));
252 g_slist_free (queue->queue);
255 queue->level_buffers = 0;
256 queue->timeval = NULL;
260 gst_queue_chain (GstPad *pad, GstBuffer *buf)
265 g_return_if_fail (pad != NULL);
266 g_return_if_fail (GST_IS_PAD (pad));
267 g_return_if_fail (buf != NULL);
269 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
270 name = GST_ELEMENT_NAME (queue);
272 /* we have to lock the queue since we span threads */
274 // GST_DEBUG (GST_CAT_DATAFLOW,"trying to get lock on queue \"%s\"\n",name);
277 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLUSH)) {
278 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "buffer has FLUSH bit set, flushing queue\n");
279 gst_queue_flush (queue);
282 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding buffer %p of size %d\n",buf,GST_BUFFER_SIZE(buf));
284 if (queue->level_buffers >= queue->size_buffers) {
285 // if this is a leaky queue...
287 // if we leak on the upstream side, drop the current buffer
288 if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
289 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end\n");
290 gst_buffer_unref(buf);
291 // now we have to clean up and exit right away
295 // otherwise we have to push a buffer off the other end
299 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end\n");
300 front = queue->queue;
301 leakbuf = (GstBuffer *)(front->data);
302 queue->level_buffers--;
303 queue->level_bytes -= GST_BUFFER_SIZE(leakbuf);
304 gst_buffer_unref(leakbuf);
305 queue->queue = g_slist_remove_link (queue->queue, front);
306 g_slist_free (front);
310 while (queue->level_buffers >= queue->size_buffers) {
311 // if there's a pending state change for this queue or its manager, switch
312 // back to iterator so bottom half of state change executes
313 if (GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING ||
314 // GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING)
315 GST_STATE_PENDING(GST_SCHED_PARENT(GST_ELEMENT_SCHED(GST_PAD_PARENT(GST_PAD_PEER(queue->sinkpad))))) !=
316 GST_STATE_VOID_PENDING)
318 GST_DEBUG(GST_CAT_DATAFLOW,"interrupted!!\n");
319 if (GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING)
320 GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING)\n");
321 if (GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING)
322 GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING\n");
324 cothread_switch(cothread_current_main());
327 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for space, level is %d\n", queue->level_buffers);
328 g_cond_signal (queue->emptycond);
329 g_cond_wait (queue->fullcond, GST_OBJECT(queue)->lock);
330 GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "done waiting, level is now %d\n", queue->level_buffers);
334 /* put the buffer on the tail of the list */
335 queue->queue = g_slist_append (queue->queue, buf);
336 queue->level_buffers++;
337 queue->level_bytes += GST_BUFFER_SIZE(buf);
338 // GST_DEBUG (GST_CAT_DATAFLOW, "(%s:%s)+\n",GST_DEBUG_PAD_NAME(pad));
340 /* if we were empty, but aren't any more, signal a condition */
341 if (queue->level_buffers == 1)
343 GST_DEBUG (GST_CAT_DATAFLOW,"%s signalling emptycond\n", name);
344 g_cond_signal (queue->emptycond);
351 gst_queue_get (GstPad *pad)
354 GstBuffer *buf = NULL;
358 g_assert(pad != NULL);
359 g_assert(GST_IS_PAD(pad));
360 g_return_val_if_fail (pad != NULL, NULL);
361 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
363 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
364 name = GST_ELEMENT_NAME (queue);
366 /* have to lock for thread-safety */
367 GST_DEBUG (GST_CAT_DATAFLOW,"%s try have queue lock\n", name);
369 GST_DEBUG (GST_CAT_DATAFLOW,"%s push %d %ld %p\n", name, queue->level_buffers, pthread_self (), queue->emptycond);
370 GST_DEBUG (GST_CAT_DATAFLOW,"%s have queue lock\n", name);
372 while (!queue->level_buffers) {
373 if (GST_FLAG_IS_SET (queue->sinkpad, GST_PAD_EOS)) {
374 GST_DEBUG (GST_CAT_DATAFLOW, "%s U released lock\n", name);
376 // this return NULL shouldn't hurt anything...
380 // if there's a pending state change for this queue or its manager, switch
381 // back to iterator so bottom half of state change executes
382 if (GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING ||
383 // GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING)
384 GST_STATE_PENDING(GST_SCHED_PARENT(GST_ELEMENT_SCHED(GST_PAD_PARENT(GST_PAD_PEER(queue->srcpad))))) !=
385 GST_STATE_VOID_PENDING)
387 GST_DEBUG(GST_CAT_DATAFLOW,"interrupted!!\n");
388 if (GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING)
389 GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(queue) != GST_STATE_VOID_PENDING)\n");
390 if (GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING)
391 GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_VOID_PENDING\n");
393 cothread_switch(cothread_current_main());
396 g_cond_signal (queue->fullcond);
397 g_cond_wait (queue->emptycond, GST_OBJECT(queue)->lock);
400 front = queue->queue;
401 buf = (GstBuffer *)(front->data);
402 GST_DEBUG (GST_CAT_DATAFLOW,"retrieved buffer %p from queue\n",buf);
403 queue->queue = g_slist_remove_link (queue->queue, front);
404 g_slist_free (front);
406 // if (queue->level_buffers < queue->size_buffers)
407 if (queue->level_buffers == queue->size_buffers)
409 GST_DEBUG (GST_CAT_DATAFLOW,"%s signalling fullcond\n", name);
410 g_cond_signal (queue->fullcond);
413 queue->level_buffers--;
414 queue->level_bytes -= GST_BUFFER_SIZE(buf);
415 GST_DEBUG (GST_CAT_DATAFLOW,"(%s:%s)- ",GST_DEBUG_PAD_NAME(pad));
422 static GstElementStateReturn
423 gst_queue_change_state (GstElement *element)
426 GstElementStateReturn ret;
427 g_return_val_if_fail (GST_IS_QUEUE (element), GST_STATE_FAILURE);
429 queue = GST_QUEUE (element);
431 // lock the queue so another thread (not in sync with this thread's state)
432 // can't call this queue's _get (or whatever)
435 /* if going down into NULL state, clear out buffers*/
436 if (GST_STATE_PENDING (element) == GST_STATE_READY) {
437 /* otherwise (READY or higher) we need to open the file */
438 gst_queue_flush (queue);
441 // if we haven't failed already, give the parent class a chance to ;-)
442 if (GST_ELEMENT_CLASS (parent_class)->change_state)
444 gboolean valid_handler = FALSE;
445 guint state_change_id = g_signal_lookup("state_change", G_OBJECT_TYPE(element));
447 // determine whether we need to block the parent (element) class'
448 // STATE_CHANGE signal so we can UNLOCK before returning. we block
449 // it if we could find the state_change signal AND there's a signal
450 // handler attached to it.
452 // note: this assumes that change_state() *only* emits state_change signal.
453 // if element change_state() emits other signals, they need to be blocked
455 if (state_change_id &&
456 g_signal_has_handler_pending(G_OBJECT(element), state_change_id, 0, FALSE))
457 valid_handler = TRUE;
459 g_signal_handler_block(G_OBJECT(element), state_change_id);
461 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
464 g_signal_handler_unblock(G_OBJECT(element), state_change_id);
466 // UNLOCK, *then* emit signal (if there's one there)
469 g_signal_emit(G_OBJECT (element), state_change_id, 0, GST_STATE(element));
473 ret = GST_STATE_SUCCESS;
482 gst_queue_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
486 /* it's not null if we got it, but it might not be ours */
487 g_return_if_fail (GST_IS_QUEUE (object));
489 queue = GST_QUEUE (object);
493 queue->leaky = g_value_get_int(value);
496 queue->size_buffers = g_value_get_int(value);
499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505 gst_queue_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
509 /* it's not null if we got it, but it might not be ours */
510 g_return_if_fail (GST_IS_QUEUE (object));
512 queue = GST_QUEUE (object);
516 g_value_set_int(value, queue->leaky);
519 g_value_set_int(value, queue->level_buffers);
522 g_value_set_int(value, queue->size_buffers);
525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);