- queue can change state only when not connected
[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  *
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
33 #include "config.h"
34 #include "gst_private.h"
35
36 #include "gstqueue.h"
37 #include "gstscheduler.h"
38 #include "gstevent.h"
39 #include "gstlog.h"
40
41 GstElementDetails gst_queue_details = {
42   "Queue",
43   "Generic",
44   "LGPL",
45   "Simple data queue",
46   VERSION,
47   "Erik Walthinsen <omega@cse.ogi.edu>",
48   "(C) 1999",
49 };
50
51
52 /* Queue signals and args */
53 enum {
54   LOW_WATERMARK,
55   HIGH_WATERMARK,
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   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEAKY,
151     g_param_spec_enum ("leaky", "Leaky", "Where the queue leaks, if at all.",
152                        GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK, G_PARAM_READWRITE));
153   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEVEL,
154     g_param_spec_int ("level", "Level", "How many buffers are in the queue.",
155                       0, G_MAXINT, 0, G_PARAM_READABLE));
156   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_LEVEL,
157     g_param_spec_int ("max_level", "Maximum Level", "How many buffers the queue holds.",
158                       0, G_MAXINT, 100, G_PARAM_READWRITE));
159   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAY_DEADLOCK,
160     g_param_spec_boolean ("may_deadlock", "May Deadlock", "The queue may deadlock if it's full and not PLAYING",
161                       TRUE, G_PARAM_READWRITE));
162   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCK_TIMEOUT,
163     g_param_spec_int ("block_timeout", "Timeout for Block", 
164                       "Microseconds until blocked queue times out and returns filler event. "
165                       "Value of -1 disables timeout",
166                       -1, G_MAXINT, -1, G_PARAM_READWRITE));
167
168   gobject_class->dispose                = GST_DEBUG_FUNCPTR (gst_queue_dispose);
169   gobject_class->set_property           = GST_DEBUG_FUNCPTR (gst_queue_set_property);
170   gobject_class->get_property           = GST_DEBUG_FUNCPTR (gst_queue_get_property);
171
172   gstelement_class->change_state  = GST_DEBUG_FUNCPTR(gst_queue_change_state);
173   gstelement_class->release_locks = GST_DEBUG_FUNCPTR(gst_queue_release_locks);
174 }
175
176 static GstPadConnectReturn
177 gst_queue_connect (GstPad *pad, GstCaps *caps)
178 {
179   GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
180   GstPad *otherpad;
181
182   if (pad == queue->srcpad) 
183     otherpad = queue->sinkpad;
184   else
185     otherpad = queue->srcpad;
186
187   return gst_pad_proxy_connect (otherpad, caps);
188 }
189
190 static GstCaps*
191 gst_queue_getcaps (GstPad *pad, GstCaps *caps)
192 {
193   GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
194   GstPad *otherpad;
195
196   if (pad == queue->srcpad) 
197     otherpad = queue->sinkpad;
198   else
199     otherpad = queue->srcpad;
200
201   return gst_pad_get_allowed_caps (otherpad);
202 }
203
204 static void
205 gst_queue_init (GstQueue *queue)
206 {
207   /* scheduling on this kind of element is, well, interesting */
208   GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
209   GST_FLAG_SET (queue, GST_ELEMENT_EVENT_AWARE);
210
211   queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
212   gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_chain));
213   gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
214   gst_pad_set_bufferpool_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_get_bufferpool));
215   gst_pad_set_connect_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_connect));
216   gst_pad_set_getcaps_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
217
218   queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
219   gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_get));
220   gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
221   gst_pad_set_connect_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_connect));
222   gst_pad_set_getcaps_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
223   gst_pad_set_event_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_handle_src_event));
224
225   queue->leaky = GST_QUEUE_NO_LEAK;
226   queue->queue = NULL;
227   queue->level_buffers = 0;
228   queue->level_bytes = 0;
229   queue->level_time = 0LL;
230   queue->size_buffers = 100;            /* 100 buffers */
231   queue->size_bytes = 100 * 1024;       /* 100KB */
232   queue->size_time = 1000000000LL;      /* 1sec */
233   queue->may_deadlock = TRUE;
234   queue->block_timeout = -1;
235   queue->interrupt = FALSE;
236   queue->flush = FALSE;
237
238   queue->qlock = g_mutex_new ();
239   queue->reader = FALSE;
240   queue->writer = FALSE;
241   queue->not_empty = g_cond_new ();
242   queue->not_full = g_cond_new ();
243   queue->events = g_async_queue_new();
244   queue->queue = g_queue_new ();
245   GST_DEBUG_ELEMENT (GST_CAT_THREAD, queue, "initialized queue's not_empty & not_full conditions");
246 }
247
248 static void
249 gst_queue_dispose (GObject *object)
250 {
251   GstQueue *queue = GST_QUEUE (object);
252
253   g_mutex_free (queue->qlock);
254   g_cond_free (queue->not_empty);
255   g_cond_free (queue->not_full);
256   gst_queue_locked_flush (queue);
257   g_queue_free (queue->queue);
258
259   g_async_queue_unref(queue->events);
260
261   G_OBJECT_CLASS (parent_class)->dispose (object);
262 }
263
264 static GstBufferPool*
265 gst_queue_get_bufferpool (GstPad *pad)
266 {
267   GstQueue *queue;
268
269   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
270
271   return gst_pad_get_bufferpool (queue->srcpad);
272 }
273
274 static void
275 gst_queue_cleanup_data (gpointer data, const gpointer user_data)
276 {
277   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, user_data, "cleaning buffer %p", data);
278
279   gst_data_unref (GST_DATA (data));
280 }
281
282 static void
283 gst_queue_locked_flush (GstQueue *queue)
284 {
285   gpointer data;
286   
287   while ((data = g_queue_pop_head (queue->queue))) {
288     gst_queue_cleanup_data (data, (gpointer) queue);
289   }
290   queue->timeval = NULL;
291   queue->level_buffers = 0;
292   queue->level_bytes = 0;
293   queue->level_time = 0LL;
294   /* make sure any pending buffers to be added are flushed too */
295   queue->flush = TRUE;
296 }
297
298 static void
299 gst_queue_chain (GstPad *pad, GstBuffer *buf)
300 {
301   GstQueue *queue;
302   gboolean reader;
303
304   g_return_if_fail (pad != NULL);
305   g_return_if_fail (GST_IS_PAD (pad));
306   g_return_if_fail (buf != NULL);
307
308   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
309   
310   /* check for events to send upstream */
311   g_async_queue_lock(queue->events);
312   while (g_async_queue_length_unlocked(queue->events) > 0){
313     GstEvent *event = (GstEvent*)g_async_queue_pop_unlocked(queue->events);
314     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "sending event upstream\n");
315     gst_pad_event_default (pad, event);
316     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "event sent\n");
317   }
318   g_async_queue_unlock(queue->events);
319
320 restart:
321   /* we have to lock the queue since we span threads */
322   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%p", g_thread_self ());
323   g_mutex_lock (queue->qlock);
324   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%p", g_thread_self ());
325
326   /* assume don't need to flush this buffer when the queue is filled */
327   queue->flush = FALSE;
328
329   if (GST_IS_EVENT (buf)) {
330     switch (GST_EVENT_TYPE (buf)) {
331       case GST_EVENT_FLUSH:
332         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
333         gst_queue_locked_flush (queue);
334         break;
335       case GST_EVENT_EOS:
336         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "eos in on %s %d\n", 
337                            GST_ELEMENT_NAME (queue), queue->level_buffers);
338         break;
339       default:
340         /* we put the event in the queue, we don't have to act ourselves */
341         break;
342     }
343   }
344
345   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding buffer %p of size %d",buf,GST_BUFFER_SIZE(buf));
346
347   if (queue->level_buffers == queue->size_buffers) {
348     /* if this is a leaky queue... */
349     if (queue->leaky) {
350       /* FIXME don't want to leak events! */
351       /* if we leak on the upstream side, drop the current buffer */
352       if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
353         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end");
354         if (GST_IS_EVENT (buf))
355           fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
356               GST_ELEMENT_NAME(GST_ELEMENT(queue)),
357               GST_EVENT_TYPE(GST_EVENT(buf)));
358           GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end");
359         gst_buffer_unref(buf);
360         /* now we have to clean up and exit right away */
361         g_mutex_unlock (queue->qlock);
362         return;
363       }
364       /* otherwise we have to push a buffer off the other end */
365       else {
366         gpointer front;
367         GstBuffer *leakbuf;
368
369         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end");
370
371         front = g_queue_pop_head (queue->queue);
372         leakbuf = (GstBuffer *)(front);
373
374         if (GST_IS_EVENT (leakbuf)) {
375           fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
376               GST_ELEMENT_NAME(GST_ELEMENT(queue)),
377               GST_EVENT_TYPE(GST_EVENT(leakbuf)));
378         }
379         queue->level_buffers--;
380         queue->level_bytes -= GST_BUFFER_SIZE(leakbuf);
381         gst_data_unref (GST_DATA (leakbuf));
382       }
383     }
384
385     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre full wait, level:%d/%d",
386                         queue->level_buffers, queue->size_buffers);
387
388     while (queue->level_buffers == queue->size_buffers) {
389       /* if there's a pending state change for this queue or its manager, switch */
390       /* back to iterator so bottom half of state change executes */
391       if (queue->interrupt) {
392         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!");
393         g_mutex_unlock (queue->qlock);
394         if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->sinkpad), GST_ELEMENT (queue)))
395           return;
396         /* if we got here bacause we were unlocked after a flush, we don't need
397          * to add the buffer to the queue again */
398         if (queue->flush) {
399           GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "not adding pending buffer after flush");
400           return;
401         }
402         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding pending buffer after interrupt");
403         goto restart;
404       }
405       if (GST_STATE (queue) != GST_STATE_PLAYING) {
406         /* this means the other end is shut down */
407         /* try to signal to resolve the error */
408         if (!queue->may_deadlock) {
409           gst_data_unref (GST_DATA (buf));
410           g_mutex_unlock (queue->qlock);
411           gst_element_error (GST_ELEMENT (queue), "deadlock found, source pad elements are shut down");
412           return;
413         }
414         else {
415           g_print ("%s: waiting for the app to restart source pad elements\n", GST_ELEMENT_NAME (queue));
416         }
417       }
418
419       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_full, level:%d/%d", 
420                       queue->level_buffers, queue->size_buffers);
421       if (queue->writer)
422         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "WARNING: multiple writers on queue!");
423       queue->writer = TRUE;
424       g_cond_wait (queue->not_full, queue->qlock);
425       queue->writer = FALSE;
426       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_full signal");
427     }
428     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post full wait, level:%d/%d",
429         queue->level_buffers, queue->size_buffers);
430   }
431
432   /* put the buffer on the tail of the list */
433   g_queue_push_tail (queue->queue, buf);
434
435   queue->level_buffers++;
436   queue->level_bytes += GST_BUFFER_SIZE(buf);
437
438   /* this assertion _has_ to hold */
439   g_assert (queue->queue->length == queue->level_buffers);
440
441   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)+ level:%d/%d",
442       GST_DEBUG_PAD_NAME(pad),
443       queue->level_buffers, queue->size_buffers);
444
445   /* reader waiting on an empty queue */
446   reader = queue->reader;
447
448   g_mutex_unlock (queue->qlock);
449
450   if (reader)
451   {
452     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_empty");
453     g_cond_signal (queue->not_empty);
454   }
455 }
456
457 static GstBuffer *
458 gst_queue_get (GstPad *pad)
459 {
460   GstQueue *queue;
461   GstBuffer *buf = NULL;
462   gpointer front;
463   gboolean writer;
464
465   g_assert(pad != NULL);
466   g_assert(GST_IS_PAD(pad));
467   g_return_val_if_fail (pad != NULL, NULL);
468   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
469
470   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
471
472 restart:
473   /* have to lock for thread-safety */
474   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%p", g_thread_self ());
475   g_mutex_lock (queue->qlock);
476   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%p %p", g_thread_self (), queue->not_empty);
477
478   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre empty wait, level:%d/%d", queue->level_buffers, queue->size_buffers);
479   while (queue->level_buffers == 0) {
480     /* if there's a pending state change for this queue or its manager, switch
481      * back to iterator so bottom half of state change executes
482      */ 
483     //while (GST_STATE_PENDING (queue) != GST_STATE_VOID_PENDING) {
484     if (queue->interrupt) {
485       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!");
486       g_mutex_unlock (queue->qlock);
487       if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->srcpad), GST_ELEMENT (queue)))
488         return NULL;
489       goto restart;
490     }
491     if (GST_STATE (queue) != GST_STATE_PLAYING) {
492       /* this means the other end is shut down */
493       if (!queue->may_deadlock) {
494         g_mutex_unlock (queue->qlock);
495         gst_element_error (GST_ELEMENT (queue), "deadlock found, sink pad elements are shut down");
496         goto restart;
497       }
498       else {
499         g_print ("%s: waiting for the app to restart source pad elements\n", GST_ELEMENT_NAME (queue));
500       }
501     }
502
503     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_empty, level:%d/%d", queue->level_buffers, queue->size_buffers);
504     if (queue->reader)
505       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "WARNING: multiple readers on queue!");
506     queue->reader = TRUE;
507     
508     //if (queue->block_timeout > -1){
509     if (FALSE) {
510       GTimeVal timeout;
511       g_get_current_time(&timeout);
512       g_time_val_add(&timeout, queue->block_timeout);
513       if (!g_cond_timed_wait (queue->not_empty, queue->qlock, &timeout)){
514         g_mutex_unlock (queue->qlock);
515         g_warning ("filler");
516         return GST_BUFFER(gst_event_new_filler());
517       }
518     }
519     else {
520       g_cond_wait (queue->not_empty, queue->qlock);
521     }
522     queue->reader = FALSE;
523     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_empty signal");
524   }
525   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post empty wait, level:%d/%d", queue->level_buffers, queue->size_buffers);
526
527   front = g_queue_pop_head (queue->queue);
528   buf = (GstBuffer *)(front);
529   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "retrieved buffer %p from queue", buf);
530
531   queue->level_buffers--;
532   queue->level_bytes -= GST_BUFFER_SIZE(buf);
533
534   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)- level:%d/%d",
535       GST_DEBUG_PAD_NAME(pad),
536       queue->level_buffers, queue->size_buffers);
537
538   /* this assertion _has_ to hold */
539   g_assert (queue->queue->length == queue->level_buffers);
540
541   /* writer waiting on a full queue */
542   writer = queue->writer;
543
544   g_mutex_unlock (queue->qlock);
545
546   if (writer)
547   {
548     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_full");
549     g_cond_signal (queue->not_full);
550   }
551
552   /* FIXME where should this be? locked? */
553   if (GST_IS_EVENT(buf)) {
554     GstEvent *event = GST_EVENT(buf);
555     switch (GST_EVENT_TYPE(event)) {
556       case GST_EVENT_EOS:
557         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue \"%s\" eos", GST_ELEMENT_NAME (queue));
558         gst_element_set_eos (GST_ELEMENT (queue));
559         break;
560       default:
561         break;
562     }
563   }
564
565   return buf;
566 }
567
568
569 static gboolean
570 gst_queue_handle_src_event (GstPad *pad, GstEvent *event)
571 {
572   GstQueue *queue;
573   gboolean res;
574
575   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
576
577   g_mutex_lock (queue->qlock);
578
579   if (gst_element_get_state (GST_ELEMENT (queue)) == GST_STATE_PLAYING) {
580     /* push the event to the queue for upstream consumption */
581     g_async_queue_push(queue->events, event);
582     g_mutex_unlock (queue->qlock);
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     return FALSE;
587   }
588
589   res = gst_pad_event_default (pad, event); 
590   switch (GST_EVENT_TYPE (event)) {
591     case GST_EVENT_FLUSH:
592       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
593       gst_queue_locked_flush (queue);
594       break;
595     case GST_EVENT_SEEK:
596       if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH) {
597         gst_queue_locked_flush (queue);
598       }
599     default:
600       break;
601   }
602   g_mutex_unlock (queue->qlock);
603
604   /* we have to claim success, but we don't really know */
605   return TRUE;
606 }
607
608 static gboolean
609 gst_queue_release_locks (GstElement *element)
610 {
611   GstQueue *queue;
612
613   queue = GST_QUEUE (element);
614
615   g_mutex_lock (queue->qlock);
616   queue->interrupt = TRUE;
617   g_cond_signal (queue->not_full);
618   g_cond_signal (queue->not_empty); 
619   g_mutex_unlock (queue->qlock);
620
621   return TRUE;
622 }
623
624 static GstElementStateReturn
625 gst_queue_change_state (GstElement *element)
626 {
627   GstQueue *queue;
628   GstElementStateReturn ret;
629   GstElementState new_state;
630   g_return_val_if_fail (GST_IS_QUEUE (element), GST_STATE_FAILURE);
631
632   queue = GST_QUEUE (element);
633
634   GST_DEBUG_ENTER("('%s')", GST_ELEMENT_NAME (element));
635
636   /* lock the queue so another thread (not in sync with this thread's state)
637    * can't call this queue's _get (or whatever)
638    */
639   g_mutex_lock (queue->qlock);
640
641   new_state = GST_STATE_PENDING (element);
642
643   if (new_state == GST_STATE_READY) {
644     gst_queue_locked_flush (queue);
645   }
646   else if (new_state == GST_STATE_PLAYING) {
647     if (!GST_PAD_IS_CONNECTED (queue->sinkpad)) {
648       GST_DEBUG_ELEMENT (GST_CAT_STATES, queue, "queue %s is not connected", GST_ELEMENT_NAME (queue));
649       /* FIXME can this be? */
650       if (queue->reader)
651         g_cond_signal (queue->not_empty);
652       g_mutex_unlock (queue->qlock);
653
654       return GST_STATE_FAILURE;
655     }
656     queue->interrupt = FALSE;
657   }
658
659   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
660   g_mutex_unlock (queue->qlock);
661
662   GST_DEBUG_LEAVE("('%s')", GST_ELEMENT_NAME (element));
663   return ret;
664 }
665
666
667 static void
668 gst_queue_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
669 {
670   GstQueue *queue;
671
672   /* it's not null if we got it, but it might not be ours */
673   g_return_if_fail (GST_IS_QUEUE (object));
674
675   queue = GST_QUEUE (object);
676
677   switch (prop_id) {
678     case ARG_LEAKY:
679       queue->leaky = g_value_get_enum (value);
680       break;
681     case ARG_MAX_LEVEL:
682       queue->size_buffers = g_value_get_int (value);
683       break;
684     case ARG_MAY_DEADLOCK:
685       queue->may_deadlock = g_value_get_boolean (value);
686       break;
687     case ARG_BLOCK_TIMEOUT:
688       queue->block_timeout = g_value_get_int (value);
689       break;
690     default:
691       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
692       break;
693   }
694 }
695
696 static void
697 gst_queue_get_property (GObject *object, guint prop_id, 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       g_value_set_enum (value, queue->leaky);
709       break;
710     case ARG_LEVEL:
711       g_value_set_int (value, queue->level_buffers);
712       break;
713     case ARG_MAX_LEVEL:
714       g_value_set_int (value, queue->size_buffers);
715       break;
716     case ARG_MAY_DEADLOCK:
717       g_value_set_boolean (value, queue->may_deadlock);
718       break;
719     case ARG_BLOCK_TIMEOUT:
720       g_value_set_int (value, queue->block_timeout);
721       break;
722     default:
723       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
724       break;
725   }
726 }