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