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