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(0,A, gst_element_get_name(GST_ELEMENT(queue)))
34 #include "gst_private.h"
38 GstElementDetails gst_queue_details = {
43 "Erik Walthinsen <omega@cse.ogi.edu>",
48 /* Queue signals and args */
62 static void gst_queue_class_init (GstQueueClass *klass);
63 static void gst_queue_init (GstQueue *queue);
65 static void gst_queue_set_arg (GtkObject *object, GtkArg *arg, guint id);
66 static void gst_queue_get_arg (GtkObject *object, GtkArg *arg, guint id);
68 static gboolean gst_queue_handle_eos (GstPad *pad);
69 static void gst_queue_chain (GstPad *pad, GstBuffer *buf);
70 static GstBuffer * gst_queue_get (GstPad *pad);
72 static void gst_queue_flush (GstQueue *queue);
74 static GstElementStateReturn gst_queue_change_state (GstElement *element);
77 static GstElementClass *parent_class = NULL;
78 //static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
81 gst_queue_get_type(void) {
82 static GtkType queue_type = 0;
85 static const GtkTypeInfo queue_info = {
88 sizeof(GstQueueClass),
89 (GtkClassInitFunc)gst_queue_class_init,
90 (GtkObjectInitFunc)gst_queue_init,
91 (GtkArgSetFunc)gst_queue_set_arg,
92 (GtkArgGetFunc)gst_queue_get_arg,
93 (GtkClassInitFunc)NULL,
95 queue_type = gtk_type_unique (GST_TYPE_ELEMENT, &queue_info);
101 gst_queue_class_init (GstQueueClass *klass)
103 GtkObjectClass *gtkobject_class;
104 GstElementClass *gstelement_class;
106 gtkobject_class = (GtkObjectClass*)klass;
107 gstelement_class = (GstElementClass*)klass;
109 parent_class = gtk_type_class (GST_TYPE_ELEMENT);
111 gtk_object_add_arg_type ("GstQueue::level", GTK_TYPE_INT,
112 GTK_ARG_READABLE, ARG_LEVEL);
113 gtk_object_add_arg_type ("GstQueue::max_level", GTK_TYPE_INT,
114 GTK_ARG_READWRITE, ARG_MAX_LEVEL);
115 gtk_object_add_arg_type ("GstQueue::block", GTK_TYPE_BOOL,
116 GTK_ARG_READWRITE, ARG_BLOCK);
118 gtkobject_class->set_arg = gst_queue_set_arg;
119 gtkobject_class->get_arg = gst_queue_get_arg;
121 gstelement_class->change_state = gst_queue_change_state;
125 gst_queue_init (GstQueue *queue)
127 // scheduling on this kind of element is, well, interesting
128 GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
130 queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
131 gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_chain));
132 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
133 gst_pad_set_eos_function (queue->sinkpad, gst_queue_handle_eos);
135 queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
136 gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_get));
137 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
140 queue->level_buffers = 0;
141 queue->max_buffers = 100;
143 queue->level_bytes = 0;
144 queue->size_buffers = 0;
145 queue->size_bytes = 0;
147 queue->emptycond = g_cond_new ();
148 queue->fullcond = g_cond_new ();
152 gst_queue_handle_eos (GstPad *pad)
156 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
158 GST_DEBUG (0,"queue: %s received eos\n", GST_ELEMENT_NAME (queue));
161 GST_DEBUG (0,"queue: %s has %d buffers left\n", GST_ELEMENT_NAME (queue),
162 queue->level_buffers);
164 GST_FLAG_SET (pad, GST_PAD_EOS);
166 g_cond_signal (queue->emptycond);
174 gst_queue_cleanup_buffers (gpointer data, const gpointer user_data)
176 GST_DEBUG (0,"queue: %s cleaning buffer %p\n", (gchar *)user_data, data);
178 gst_buffer_unref (GST_BUFFER (data));
182 gst_queue_flush (GstQueue *queue)
184 g_slist_foreach (queue->queue, gst_queue_cleanup_buffers,
185 (char *) GST_ELEMENT_NAME (queue));
186 g_slist_free (queue->queue);
189 queue->level_buffers = 0;
190 queue->timeval = NULL;
194 gst_queue_chain (GstPad *pad, GstBuffer *buf)
197 gboolean tosignal = FALSE;
200 g_return_if_fail (pad != NULL);
201 g_return_if_fail (GST_IS_PAD (pad));
202 g_return_if_fail (buf != NULL);
204 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
205 name = GST_ELEMENT_NAME (queue);
207 /* we have to lock the queue since we span threads */
209 GST_DEBUG (0,"queue: try have queue lock\n");
211 GST_DEBUG (0,"queue: %s adding buffer %p %ld\n", name, buf, pthread_self ());
212 GST_DEBUG (0,"queue: have queue lock\n");
214 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLUSH)) {
215 gst_queue_flush (queue);
218 GST_DEBUG (0,"queue: %s: chain %d %p\n", name, queue->level_buffers, buf);
220 while (queue->level_buffers >= queue->max_buffers) {
221 GST_DEBUG (0,"queue: %s waiting %d\n", name, queue->level_buffers);
223 //g_cond_timed_wait (queue->fullcond, queue->fulllock, queue->timeval);
224 //FIXME need to signal other thread in case signals got lost?
225 g_cond_signal (queue->emptycond);
226 g_cond_wait (queue->fullcond, GST_OBJECT(queue)->lock);
228 GST_DEBUG (0,"queue: %s waiting done %d\n", name, queue->level_buffers);
231 /* put the buffer on the tail of the list */
232 queue->queue = g_slist_append (queue->queue, buf);
233 // STATUS("%s: +\n");
234 GST_DEBUG (0,"(%s:%s)+ ",GST_DEBUG_PAD_NAME(pad));
236 /* if we were empty, but aren't any more, signal a condition */
237 tosignal = (queue->level_buffers >= 0);
238 queue->level_buffers++;
240 /* we can unlock now */
241 GST_DEBUG (0,"queue: %s chain %d end signal(%d,%p)\n", name, queue->level_buffers, tosignal, queue->emptycond);
244 // STATUS("%s: >\n");
245 g_cond_signal (queue->emptycond);
246 // STATUS("%s: >>\n");
252 gst_queue_get (GstPad *pad)
255 GstBuffer *buf = NULL;
259 g_return_val_if_fail (pad != NULL, NULL);
260 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
262 queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
263 name = GST_ELEMENT_NAME (queue);
265 /* have to lock for thread-safety */
266 GST_DEBUG (0,"queue: %s try have queue lock\n", name);
268 GST_DEBUG (0,"queue: %s push %d %ld %p\n", name, queue->level_buffers, pthread_self (), queue->emptycond);
269 GST_DEBUG (0,"queue: %s have queue lock\n", name);
271 // we bail if there's nothing there
272 if (!queue->level_buffers && !queue->block) {
277 while (!queue->level_buffers) {
278 STATUS("queue: %s U released lock\n");
279 //g_cond_timed_wait (queue->emptycond, queue->emptylock, queue->timeval);
280 if (GST_FLAG_IS_SET (queue->sinkpad, GST_PAD_EOS)) {
281 gst_pad_set_eos (queue->srcpad);
284 //FIXME need to signal other thread in case signals got lost?
285 g_cond_signal (queue->fullcond);
286 g_cond_wait (queue->emptycond, GST_OBJECT(queue)->lock);
287 // STATUS("queue: %s U- getting lock\n");
290 front = queue->queue;
291 buf = (GstBuffer *)(front->data);
292 GST_DEBUG (0,"retrieved buffer %p from queue\n",buf);
293 queue->queue = g_slist_remove_link (queue->queue, front);
294 g_slist_free (front);
296 queue->level_buffers--;
297 // STATUS("%s: -\n");
298 GST_DEBUG (0,"(%s:%s)- ",GST_DEBUG_PAD_NAME(pad));
300 if (queue->level_buffers < queue->max_buffers) {
301 // STATUS("%s: < \n");
302 g_cond_signal (queue->fullcond);
303 // STATUS("%s: << \n");
307 // GST_DEBUG (0,"queue: %s pushing %d %p \n", name, queue->level_buffers, buf);
308 // gst_pad_push (queue->srcpad, buf);
309 // GST_DEBUG (0,"queue: %s pushing %d done \n", name, queue->level_buffers);
315 static GstElementStateReturn
316 gst_queue_change_state (GstElement *element)
319 g_return_val_if_fail (GST_IS_QUEUE (element), GST_STATE_FAILURE);
321 queue = GST_QUEUE (element);
322 GST_DEBUG (0,"gstqueue: state pending %d\n", GST_STATE_PENDING (element));
324 /* if going down into NULL state, clear out buffers*/
325 if (GST_STATE_PENDING (element) == GST_STATE_READY) {
326 /* otherwise (READY or higher) we need to open the file */
327 gst_queue_flush (queue);
330 /* if we haven't failed already, give the parent class a chance to ;-) */
331 if (GST_ELEMENT_CLASS (parent_class)->change_state)
332 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
334 return GST_STATE_SUCCESS;
339 gst_queue_set_arg (GtkObject *object, GtkArg *arg, guint id)
343 /* it's not null if we got it, but it might not be ours */
344 g_return_if_fail (GST_IS_QUEUE (object));
346 queue = GST_QUEUE (object);
350 queue->max_buffers = GTK_VALUE_INT (*arg);
353 queue->block = GTK_VALUE_BOOL (*arg);
361 gst_queue_get_arg (GtkObject *object, GtkArg *arg, guint id)
365 /* it's not null if we got it, but it might not be ours */
366 g_return_if_fail (GST_IS_QUEUE (object));
368 queue = GST_QUEUE (object);
372 GTK_VALUE_INT (*arg) = queue->level_buffers;
375 GTK_VALUE_INT (*arg) = queue->max_buffers;
378 GTK_VALUE_BOOL (*arg) = queue->block;
381 arg->type = GTK_TYPE_INVALID;