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