s/GstBuffer/GstData/ in the API where you can pass events. Fix the plugins to deal...
[platform/upstream/gstreamer.git] / gst / gstqueue.c
1 /* GStreamer
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>
5  *
6  * gstqueue.c:
7  *
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.
12  *
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.
17  *
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.
22  */
23
24
25 #include "gst_private.h"
26
27 #include "gstqueue.h"
28 #include "gstscheduler.h"
29 #include "gstevent.h"
30 #include "gstinfo.h"
31
32 GstElementDetails gst_queue_details = {
33   "Queue",
34   "Generic",
35   "LGPL",
36   "Simple data queue",
37   VERSION,
38   "Erik Walthinsen <omega@cse.ogi.edu>",
39   "(C) 1999",
40 };
41
42
43 /* Queue signals and args */
44 enum {
45   FULL,
46   LAST_SIGNAL
47 };
48
49 enum {
50   ARG_0,
51   ARG_LEVEL_BUFFERS,
52   ARG_LEVEL_BYTES,
53   ARG_LEVEL_TIME,
54   ARG_SIZE_BUFFERS,
55   ARG_SIZE_BYTES,
56   ARG_SIZE_TIME,
57   ARG_LEAKY,
58   ARG_LEVEL,
59   ARG_MAX_LEVEL,
60   ARG_MIN_THRESHOLD_BYTES, 
61   ARG_MAY_DEADLOCK,
62   ARG_BLOCK_TIMEOUT,
63 };
64
65
66 static void                     gst_queue_class_init            (GstQueueClass *klass);
67 static void                     gst_queue_init                  (GstQueue *queue);
68 static void                     gst_queue_dispose               (GObject *object);
69
70 static void                     gst_queue_set_property          (GObject *object, guint prop_id, 
71                                                                  const GValue *value, GParamSpec *pspec);
72 static void                     gst_queue_get_property          (GObject *object, guint prop_id, 
73                                                                  GValue *value, GParamSpec *pspec);
74
75 static void                     gst_queue_chain                 (GstPad *pad, GstData *data);
76 static GstData *                gst_queue_get                   (GstPad *pad);
77 static GstBufferPool*           gst_queue_get_bufferpool        (GstPad *pad);
78         
79 static gboolean                 gst_queue_handle_src_event      (GstPad *pad, GstEvent *event);
80
81
82 static void                     gst_queue_locked_flush          (GstQueue *queue);
83
84 static GstElementStateReturn    gst_queue_change_state          (GstElement *element);
85 static gboolean                 gst_queue_release_locks         (GstElement *element);
86
87   
88 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type())
89 static GType
90 queue_leaky_get_type(void) {
91   static GType queue_leaky_type = 0;
92   static GEnumValue queue_leaky[] = {
93     { GST_QUEUE_NO_LEAK,                "0", "Not Leaky" },
94     { GST_QUEUE_LEAK_UPSTREAM,          "1", "Leaky on Upstream" },
95     { GST_QUEUE_LEAK_DOWNSTREAM,        "2", "Leaky on Downstream" },
96     { 0, NULL, NULL },
97   };
98   if (!queue_leaky_type) {
99     queue_leaky_type = g_enum_register_static("GstQueueLeaky", queue_leaky);
100   }
101   return queue_leaky_type;
102 }
103
104 static GstElementClass *parent_class = NULL;
105 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
106
107 GType
108 gst_queue_get_type(void) 
109 {
110   static GType queue_type = 0;
111
112   if (!queue_type) {
113     static const GTypeInfo queue_info = {
114       sizeof(GstQueueClass),
115       NULL,
116       NULL,
117       (GClassInitFunc)gst_queue_class_init,
118       NULL,
119       NULL,
120       sizeof(GstQueue),
121       4,
122       (GInstanceInitFunc)gst_queue_init,
123       NULL
124     };
125     queue_type = g_type_register_static (GST_TYPE_ELEMENT, "GstQueue", &queue_info, 0);
126   }
127   return queue_type;
128 }
129
130 static void
131 gst_queue_class_init (GstQueueClass *klass)
132 {
133   GObjectClass *gobject_class;
134   GstElementClass *gstelement_class;
135
136   gobject_class = (GObjectClass*)klass;
137   gstelement_class = (GstElementClass*)klass;
138
139   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
140
141   gst_queue_signals[FULL] =
142     g_signal_new ("full", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
143                   G_STRUCT_OFFSET (GstQueueClass, full), NULL, NULL,
144                   g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
145   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEAKY,
146     g_param_spec_enum ("leaky", "Leaky", "Where the queue leaks, if at all.",
147                        GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK, G_PARAM_READWRITE));
148   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEVEL,
149     g_param_spec_int ("level", "Level", "How many buffers are in the queue.",
150                       0, G_MAXINT, 0, G_PARAM_READABLE));
151   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_LEVEL,
152     g_param_spec_int ("max_level", "Maximum Level", "How many buffers the queue holds.",
153                       0, G_MAXINT, 100, G_PARAM_READWRITE));
154   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MIN_THRESHOLD_BYTES,
155     g_param_spec_int ("min_threshold_bytes", "Minimum Threshold",
156                       "Minimum bytes required before signalling not_empty to reader.",
157                       0, G_MAXINT, 0, G_PARAM_READWRITE));
158   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAY_DEADLOCK,
159     g_param_spec_boolean ("may_deadlock", "May Deadlock", "The queue may deadlock if it's full and not PLAYING",
160                       TRUE, G_PARAM_READWRITE));
161   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCK_TIMEOUT,
162     g_param_spec_int ("block_timeout", "Timeout for Block", 
163                       "Microseconds until blocked queue times out and returns filler event. "
164                       "Value of -1 disables timeout",
165                       -1, G_MAXINT, -1, G_PARAM_READWRITE));
166
167   gobject_class->dispose                = GST_DEBUG_FUNCPTR (gst_queue_dispose);
168   gobject_class->set_property           = GST_DEBUG_FUNCPTR (gst_queue_set_property);
169   gobject_class->get_property           = GST_DEBUG_FUNCPTR (gst_queue_get_property);
170
171   gstelement_class->change_state  = GST_DEBUG_FUNCPTR(gst_queue_change_state);
172   gstelement_class->release_locks = GST_DEBUG_FUNCPTR(gst_queue_release_locks);
173 }
174
175 static GstPadLinkReturn
176 gst_queue_link (GstPad *pad, GstCaps *caps)
177 {
178   GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
179   GstPad *otherpad;
180
181   if (pad == queue->srcpad) 
182     otherpad = queue->sinkpad;
183   else
184     otherpad = queue->srcpad;
185
186   return gst_pad_proxy_link (otherpad, caps);
187 }
188
189 static GstCaps*
190 gst_queue_getcaps (GstPad *pad, GstCaps *caps)
191 {
192   GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
193   GstPad *otherpad;
194
195   if (pad == queue->srcpad) 
196     otherpad = GST_PAD_PEER (queue->sinkpad);
197   else
198     otherpad = GST_PAD_PEER (queue->srcpad);
199   
200   if (otherpad)
201     return gst_pad_get_caps (otherpad);
202
203   return NULL;
204 }
205
206 static void
207 gst_queue_init (GstQueue *queue)
208 {
209   /* scheduling on this kind of element is, well, interesting */
210   GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
211   GST_FLAG_SET (queue, GST_ELEMENT_EVENT_AWARE);
212
213   queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
214   gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_chain));
215   gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
216   gst_pad_set_bufferpool_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_get_bufferpool));
217   gst_pad_set_link_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_link));
218   gst_pad_set_getcaps_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
219
220   queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
221   gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_get));
222   gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
223   gst_pad_set_link_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_link));
224   gst_pad_set_getcaps_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
225   gst_pad_set_event_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_handle_src_event));
226
227   queue->leaky = GST_QUEUE_NO_LEAK;
228   queue->queue = NULL;
229   queue->level_buffers = 0;
230   queue->level_bytes = 0;
231   queue->level_time = G_GINT64_CONSTANT (0);
232   queue->size_buffers = 100;                            /* 100 buffers */
233   queue->size_bytes = 100 * 1024;       /* 100KB */
234   queue->size_time = GST_SECOND;        /* 1sec */
235   queue->min_threshold_bytes = 0;
236   queue->may_deadlock = TRUE;
237   queue->block_timeout = -1;
238   queue->interrupt = FALSE;
239   queue->flush = FALSE;
240
241   queue->qlock = g_mutex_new ();
242   queue->not_empty = g_cond_new ();
243   queue->not_full = g_cond_new ();
244   queue->events = g_async_queue_new();
245   queue->queue = g_queue_new ();
246   GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue, "initialized queue's not_empty & not_full conditions");
247 }
248
249 static void
250 gst_queue_dispose (GObject *object)
251 {
252   GstQueue *queue = GST_QUEUE (object);
253
254   gst_element_set_state (GST_ELEMENT (queue), GST_STATE_NULL);
255
256   g_mutex_free (queue->qlock);
257   g_cond_free (queue->not_empty);
258   g_cond_free (queue->not_full);
259   g_queue_free (queue->queue);
260
261   g_async_queue_unref(queue->events);
262
263   G_OBJECT_CLASS (parent_class)->dispose (object);
264 }
265
266 static GstBufferPool*
267 gst_queue_get_bufferpool (GstPad *pad)
268 {
269   GstQueue *queue;
270
271   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
272
273   return gst_pad_get_bufferpool (queue->srcpad);
274 }
275
276 static void
277 gst_queue_cleanup_data (gpointer data, const gpointer user_data)
278 {
279   GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, user_data, "cleaning buffer %p", data);
280
281   gst_data_unref (GST_DATA (data));
282 }
283
284 static void
285 gst_queue_locked_flush (GstQueue *queue)
286 {
287   gpointer data;
288   
289   while ((data = g_queue_pop_head (queue->queue))) {
290     gst_queue_cleanup_data (data, (gpointer) queue);
291   }
292   queue->timeval = NULL;
293   queue->level_buffers = 0;
294   queue->level_bytes = 0;
295   queue->level_time = G_GINT64_CONSTANT (0);
296   /* make sure any pending buffers to be added are flushed too */
297   queue->flush = TRUE;
298   /* signal not_full, since we apparently aren't full anymore */
299   g_cond_signal (queue->not_full);
300 }
301
302 static void
303 gst_queue_chain (GstPad *pad, GstData *data)
304 {
305   GstQueue *queue;
306
307   g_return_if_fail (pad != NULL);
308   g_return_if_fail (GST_IS_PAD (pad));
309   g_return_if_fail (data != NULL);
310
311   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
312   
313   /* check for events to send upstream */
314   g_async_queue_lock(queue->events);
315   while (g_async_queue_length_unlocked(queue->events) > 0){
316     GstEvent *event = (GstEvent*)g_async_queue_pop_unlocked(queue->events);
317     GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "sending event upstream\n");
318     gst_pad_event_default (pad, event);
319     GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "event sent\n");
320   }
321   g_async_queue_unlock(queue->events);
322   
323 restart:
324   /* we have to lock the queue since we span threads */
325   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "locking t:%p", g_thread_self ());
326   g_mutex_lock (queue->qlock);
327   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "locked t:%p", g_thread_self ());
328   
329   /* assume don't need to flush this buffer when the queue is filled */
330   queue->flush = FALSE;
331   
332   if (GST_IS_EVENT (data)) {
333     switch (GST_EVENT_TYPE (data)) {
334       case GST_EVENT_FLUSH:
335         GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
336         gst_queue_locked_flush (queue);
337         break;
338       case GST_EVENT_EOS:
339         GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "eos in on %s %d\n", 
340                               GST_ELEMENT_NAME (queue), queue->level_buffers);
341         break;
342       default:
343         /* we put the event in the queue, we don't have to act ourselves */
344         break;
345     }
346   }
347   
348   if (GST_IS_BUFFER (data))
349     GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, 
350                         "adding buffer %p of size %d", data, GST_BUFFER_SIZE (data));
351
352   if (queue->level_buffers == queue->size_buffers) {
353     g_mutex_unlock (queue->qlock);
354     g_signal_emit (G_OBJECT (queue), gst_queue_signals[FULL], 0);
355     g_mutex_lock (queue->qlock);
356
357     /* if this is a leaky queue... */
358     if (queue->leaky) {
359       /* FIXME don't want to leak events! */
360       /* if we leak on the upstream side, drop the current buffer */
361       if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
362         GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end");
363         if (GST_IS_EVENT (data))
364           fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
365               GST_ELEMENT_NAME(GST_ELEMENT(queue)),
366               GST_EVENT_TYPE(GST_EVENT(data)));
367         /* now we have to clean up and exit right away */
368         g_mutex_unlock (queue->qlock);
369         goto out_unref;
370       }
371       /* otherwise we have to push a buffer off the other end */
372       else {
373         gpointer front;
374         GstData *leak;
375
376         GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end");
377
378         front = g_queue_pop_head (queue->queue);
379         leak = GST_DATA (front);
380
381         queue->level_buffers--;
382         if (GST_IS_EVENT (leak)) {
383           fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
384               GST_ELEMENT_NAME(GST_ELEMENT(queue)),
385               GST_EVENT_TYPE(GST_EVENT(leak)));
386         } else {
387           queue->level_bytes -= GST_BUFFER_SIZE(leak);
388         }
389
390         gst_data_unref (leak);
391       }
392     }
393
394     GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "pre full wait, level:%d/%d buffers, %d bytes",
395                         queue->level_buffers, queue->size_buffers, queue->level_bytes);
396
397     while (queue->level_buffers == queue->size_buffers) {
398       /* if there's a pending state change for this queue or its manager, switch */
399       /* back to iterator so bottom half of state change executes */
400       if (queue->interrupt) {
401         GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "interrupted!!");
402         g_mutex_unlock (queue->qlock);
403         if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->sinkpad), GST_ELEMENT (queue)))
404           goto out_unref;
405         /* if we got here because we were unlocked after a flush, we don't need
406          * to add the buffer to the queue again */
407         if (queue->flush) {
408           GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "not adding pending buffer after flush");
409           goto out_unref;
410         }
411         GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "adding pending buffer after interrupt");
412         goto restart;
413       }
414       if (GST_STATE (queue) != GST_STATE_PLAYING) {
415         /* this means the other end is shut down */
416         /* try to signal to resolve the error */
417         if (!queue->may_deadlock) {
418           g_mutex_unlock (queue->qlock);
419           gst_data_unref (data);
420           gst_element_error (GST_ELEMENT (queue), "deadlock found, source pad elements are shut down");
421           /* we don't want to goto out_unref here, since we want to clean up before calling gst_element_error */
422           return;
423         }
424         else {
425           g_print ("%s: waiting for the app to restart source pad elements\n", GST_ELEMENT_NAME (queue));
426         }
427       }
428
429       GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "waiting for not_full, level:%d/%d buffers, %d bytes", 
430                       queue->level_buffers, queue->size_buffers, queue->level_bytes);
431       g_cond_wait (queue->not_full, queue->qlock);
432       GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "got not_full signal");
433     }
434     GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "post full wait, level:%d/%d buffers, %d bytes",
435                         queue->level_buffers, queue->size_buffers, queue->level_bytes);
436   }
437
438   /* put the buffer on the tail of the list */
439   g_queue_push_tail (queue->queue, data);
440
441   queue->level_buffers++;
442   if (GST_IS_BUFFER (data))
443     queue->level_bytes += GST_BUFFER_SIZE (data);
444
445   /* this assertion _has_ to hold */
446   g_assert (queue->queue->length == queue->level_buffers);
447
448   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "(%s:%s)+ level:%d/%d buffers, %d bytes",
449       GST_DEBUG_PAD_NAME(pad),
450       queue->level_buffers, queue->size_buffers, queue->level_bytes);
451
452   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "signalling not_empty");
453   g_cond_signal (queue->not_empty);
454   g_mutex_unlock (queue->qlock);
455
456   return;
457
458 out_unref:
459   gst_data_unref (data);
460   return;
461 }
462
463 static GstData *
464 gst_queue_get (GstPad *pad)
465 {
466   GstQueue *queue;
467   GstData *data = NULL;
468   gpointer front;
469
470   g_assert(pad != NULL);
471   g_assert(GST_IS_PAD(pad));
472   g_return_val_if_fail (pad != NULL, NULL);
473   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
474
475   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
476
477 restart:
478   /* have to lock for thread-safety */
479   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "locking t:%p", g_thread_self ());
480   g_mutex_lock (queue->qlock);
481   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "locked t:%p %p", g_thread_self (), queue->not_empty);
482
483   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "pre empty wait, level:%d/%d buffers, %d bytes",
484     queue->level_buffers, queue->size_buffers, queue->level_bytes);
485   while (queue->level_buffers == 0) {
486     /* if there's a pending state change for this queue or its manager, switch
487      * back to iterator so bottom half of state change executes
488      */ 
489     //while (GST_STATE_PENDING (queue) != GST_STATE_VOID_PENDING) {
490     if (queue->interrupt) {
491       GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "interrupted!!");
492       g_mutex_unlock (queue->qlock);
493       if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->srcpad), GST_ELEMENT (queue)))
494         return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
495       goto restart;
496     }
497     if (GST_STATE (queue) != GST_STATE_PLAYING) {
498       /* this means the other end is shut down */
499       if (!queue->may_deadlock) {
500         g_mutex_unlock (queue->qlock);
501         gst_element_error (GST_ELEMENT (queue), "deadlock found, sink pad elements are shut down");
502         goto restart;
503       }
504       else {
505         g_print ("%s: waiting for the app to restart source pad elements\n", GST_ELEMENT_NAME (queue));
506       }
507     }
508
509     GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "waiting for not_empty, level:%d/%d buffers, %d bytes",
510                        queue->level_buffers, queue->size_buffers, queue->level_bytes);
511     
512     /* if (queue->block_timeout > -1){ */
513     if (FALSE) {
514       GTimeVal timeout;
515       g_get_current_time(&timeout);
516       g_time_val_add(&timeout, queue->block_timeout);
517       if (!g_cond_timed_wait (queue->not_empty, queue->qlock, &timeout)){
518         g_mutex_unlock (queue->qlock);
519         g_warning ("filler");
520         return GST_DATA (gst_event_new_filler());
521       }
522     }
523     else {
524       g_cond_wait (queue->not_empty, queue->qlock);
525     }
526     GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "got not_empty signal");
527   }
528   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "post empty wait, level:%d/%d buffers, %d bytes",
529     queue->level_buffers, queue->size_buffers, queue->level_bytes);
530
531   front = g_queue_pop_head (queue->queue);
532   data = GST_DATA (front);
533   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "retrieved data %p from queue", data);
534
535   queue->level_buffers--;
536   if (GST_IS_BUFFER (data))
537     queue->level_bytes -= GST_BUFFER_SIZE (data);
538
539   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "(%s:%s)- level:%d/%d buffers, %d bytes",
540       GST_DEBUG_PAD_NAME(pad),
541       queue->level_buffers, queue->size_buffers, queue->level_bytes);
542
543   /* this assertion _has_ to hold */
544   g_assert (queue->queue->length == queue->level_buffers);
545
546   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, queue, "signalling not_full");
547   g_cond_signal (queue->not_full);
548
549   g_mutex_unlock (queue->qlock);
550
551   /* FIXME where should this be? locked? */
552   if (GST_IS_EVENT (data)) {
553     GstEvent *event = GST_EVENT (data);
554     switch (GST_EVENT_TYPE(event)) {
555       case GST_EVENT_EOS:
556         GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "queue \"%s\" eos", GST_ELEMENT_NAME (queue));
557         gst_element_set_eos (GST_ELEMENT (queue));
558         break;
559       default:
560         break;
561     }
562   }
563
564   return data;
565 }
566
567
568 static gboolean
569 gst_queue_handle_src_event (GstPad *pad, GstEvent *event)
570 {
571   GstQueue *queue;
572   gboolean res;
573   gint event_type;
574   gint flag_flush = 0;
575
576   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
577
578   g_mutex_lock (queue->qlock);
579
580   if (gst_element_get_state (GST_ELEMENT (queue)) == GST_STATE_PLAYING) {
581     /* push the event to the queue for upstream consumption */
582     g_async_queue_push(queue->events, event);
583     g_warning ("FIXME: sending event in a running queue");
584     /* FIXME wait for delivery of the event here, then return the result
585      * instead of FALSE */
586     res = FALSE;
587     goto done;
588   }
589
590   event_type = GST_EVENT_TYPE (event);
591   if (event_type == GST_EVENT_SEEK)
592     flag_flush = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH;
593
594   res = gst_pad_event_default (pad, event); 
595
596   switch (event_type) {
597     case GST_EVENT_FLUSH:
598       GST_CAT_DEBUG_OBJECT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
599       gst_queue_locked_flush (queue);
600       break;
601     case GST_EVENT_SEEK:
602       if (flag_flush) {
603         gst_queue_locked_flush (queue);
604       }
605     default:
606       break;
607   }
608
609 done:
610   g_mutex_unlock (queue->qlock);
611
612   /* we have to claim success, but we don't really know */
613   return res;
614 }
615
616 static gboolean
617 gst_queue_release_locks (GstElement *element)
618 {
619   GstQueue *queue;
620
621   queue = GST_QUEUE (element);
622
623   g_mutex_lock (queue->qlock);
624   queue->interrupt = TRUE;
625   g_cond_signal (queue->not_full);
626   g_cond_signal (queue->not_empty); 
627   g_mutex_unlock (queue->qlock);
628
629   return TRUE;
630 }
631
632 static GstElementStateReturn
633 gst_queue_change_state (GstElement *element)
634 {
635   GstQueue *queue;
636   GstElementStateReturn ret;
637
638   queue = GST_QUEUE (element);
639
640   GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "starting state change");
641
642   /* lock the queue so another thread (not in sync with this thread's state)
643    * can't call this queue's _get (or whatever)
644    */
645   g_mutex_lock (queue->qlock);
646
647   switch (GST_STATE_TRANSITION (element)) {
648     case GST_STATE_NULL_TO_READY:
649       gst_queue_locked_flush (queue);
650       break;
651     case GST_STATE_READY_TO_PAUSED:
652       break;
653     case GST_STATE_PAUSED_TO_PLAYING:
654       if (!GST_PAD_IS_LINKED (queue->sinkpad)) {
655         GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, queue, "queue %s is not linked", GST_ELEMENT_NAME (queue));
656         /* FIXME can this be? */
657         g_cond_signal (queue->not_empty);
658
659         ret = GST_STATE_FAILURE;
660         goto error;
661       }
662       else {
663         GstScheduler *src_sched, *sink_sched;
664             
665         src_sched = gst_pad_get_scheduler (GST_PAD_CAST (queue->srcpad));
666         sink_sched = gst_pad_get_scheduler (GST_PAD_CAST (queue->sinkpad));
667
668         if (src_sched == sink_sched) {
669           GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, queue, "queue %s does not connect different schedulers", 
670                         GST_ELEMENT_NAME (queue));
671
672           g_warning ("queue %s does not connect different schedulers",
673                         GST_ELEMENT_NAME (queue));
674
675           ret = GST_STATE_FAILURE;
676           goto error;
677         }
678       }
679       queue->interrupt = FALSE;
680       break;
681     case GST_STATE_PLAYING_TO_PAUSED:
682       break;
683     case GST_STATE_PAUSED_TO_READY:
684       gst_queue_locked_flush (queue);
685       break;
686     case GST_STATE_READY_TO_NULL:
687       break;
688   }
689
690   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
691
692 error:
693   g_mutex_unlock (queue->qlock);
694
695   GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "done with state change");
696   return ret;
697 }
698
699
700 static void
701 gst_queue_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
702 {
703   GstQueue *queue;
704
705   /* it's not null if we got it, but it might not be ours */
706   g_return_if_fail (GST_IS_QUEUE (object));
707
708   queue = GST_QUEUE (object);
709
710   switch (prop_id) {
711     case ARG_LEAKY:
712       queue->leaky = g_value_get_enum (value);
713       break;
714     case ARG_MAX_LEVEL:
715       queue->size_buffers = g_value_get_int (value);
716       break;
717     case ARG_MIN_THRESHOLD_BYTES:
718       queue->min_threshold_bytes = g_value_get_int (value);
719       break;
720     case ARG_MAY_DEADLOCK:
721       queue->may_deadlock = g_value_get_boolean (value);
722       break;
723     case ARG_BLOCK_TIMEOUT:
724       queue->block_timeout = g_value_get_int (value);
725       break;
726     default:
727       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
728       break;
729   }
730 }
731
732 static void
733 gst_queue_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
734 {
735   GstQueue *queue;
736
737   /* it's not null if we got it, but it might not be ours */
738   g_return_if_fail (GST_IS_QUEUE (object));
739
740   queue = GST_QUEUE (object);
741
742   switch (prop_id) {
743     case ARG_LEAKY:
744       g_value_set_enum (value, queue->leaky);
745       break;
746     case ARG_LEVEL:
747       g_value_set_int (value, queue->level_buffers);
748       break;
749     case ARG_MAX_LEVEL:
750       g_value_set_int (value, queue->size_buffers);
751       break;
752     case ARG_MIN_THRESHOLD_BYTES:
753       g_value_set_int (value, queue->min_threshold_bytes);
754       break;
755     case ARG_MAY_DEADLOCK:
756       g_value_set_boolean (value, queue->may_deadlock);
757       break;
758     case ARG_BLOCK_TIMEOUT:
759       g_value_set_int (value, queue->block_timeout);
760       break;
761     default:
762       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
763       break;
764   }
765 }