cleanup: remove writer/reader booleans, just signal everytime bugfix: signal not_full...
[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
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 GstPadLinkReturn
177 gst_queue_link (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_link (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 = GST_PAD_PEER (queue->sinkpad);
198   else
199     otherpad = GST_PAD_PEER (queue->srcpad);
200   
201   if (otherpad)
202     return gst_pad_get_caps (otherpad);
203
204   return NULL;
205 }
206
207 static void
208 gst_queue_init (GstQueue *queue)
209 {
210   /* scheduling on this kind of element is, well, interesting */
211   GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
212   GST_FLAG_SET (queue, GST_ELEMENT_EVENT_AWARE);
213
214   queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
215   gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_chain));
216   gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
217   gst_pad_set_bufferpool_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_get_bufferpool));
218   gst_pad_set_link_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_link));
219   gst_pad_set_getcaps_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
220
221   queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
222   gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_get));
223   gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
224   gst_pad_set_link_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_link));
225   gst_pad_set_getcaps_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
226   gst_pad_set_event_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_handle_src_event));
227
228   queue->leaky = GST_QUEUE_NO_LEAK;
229   queue->queue = NULL;
230   queue->level_buffers = 0;
231   queue->level_bytes = 0;
232   queue->level_time = G_GINT64_CONSTANT (0);
233   queue->size_buffers = 100;                            /* 100 buffers */
234   queue->size_bytes = 100 * 1024;                       /* 100KB */
235   queue->size_time = G_GINT64_CONSTANT (1000000000);    /* 1sec */
236   queue->may_deadlock = TRUE;
237   queue->block_timeout = -1;
238   queue->interrupt = FALSE;
239   queue->flush = FALSE;
240
241   queue->qlock = g_mutex_new ();
242   queue->not_empty = g_cond_new ();
243   queue->not_full = g_cond_new ();
244   queue->events = g_async_queue_new();
245   queue->queue = g_queue_new ();
246   GST_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   gst_queue_locked_flush (queue);
255
256   g_mutex_free (queue->qlock);
257   g_cond_free (queue->not_empty);
258   g_cond_free (queue->not_full);
259   g_queue_free (queue->queue);
260
261   g_async_queue_unref(queue->events);
262
263   G_OBJECT_CLASS (parent_class)->dispose (object);
264 }
265
266 static GstBufferPool*
267 gst_queue_get_bufferpool (GstPad *pad)
268 {
269   GstQueue *queue;
270
271   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
272
273   return gst_pad_get_bufferpool (queue->srcpad);
274 }
275
276 static void
277 gst_queue_cleanup_data (gpointer data, const gpointer user_data)
278 {
279   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, user_data, "cleaning buffer %p", data);
280
281   gst_data_unref (GST_DATA (data));
282 }
283
284 static void
285 gst_queue_locked_flush (GstQueue *queue)
286 {
287   gpointer data;
288   
289   while ((data = g_queue_pop_head (queue->queue))) {
290     gst_queue_cleanup_data (data, (gpointer) queue);
291   }
292   queue->timeval = NULL;
293   queue->level_buffers = 0;
294   queue->level_bytes = 0;
295   queue->level_time = G_GINT64_CONSTANT (0);
296   /* make sure any pending buffers to be added are flushed too */
297   queue->flush = TRUE;
298   /* signal not_full, since we apparently aren't full anymore */
299   g_cond_signal (queue->not_full);
300 }
301
302 static void
303 gst_queue_chain (GstPad *pad, GstBuffer *buf)
304 {
305   GstQueue *queue;
306
307   g_return_if_fail (pad != NULL);
308   g_return_if_fail (GST_IS_PAD (pad));
309   g_return_if_fail (buf != NULL);
310
311   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
312   
313   /* check for events to send upstream */
314   g_async_queue_lock(queue->events);
315   while (g_async_queue_length_unlocked(queue->events) > 0){
316     GstEvent *event = (GstEvent*)g_async_queue_pop_unlocked(queue->events);
317     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "sending event upstream\n");
318     gst_pad_event_default (pad, event);
319     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "event sent\n");
320   }
321   g_async_queue_unlock(queue->events);
322
323 restart:
324   /* we have to lock the queue since we span threads */
325   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%p", g_thread_self ());
326   g_mutex_lock (queue->qlock);
327   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%p", g_thread_self ());
328
329   /* assume don't need to flush this buffer when the queue is filled */
330   queue->flush = FALSE;
331
332   if (GST_IS_EVENT (buf)) {
333     switch (GST_EVENT_TYPE (buf)) {
334       case GST_EVENT_FLUSH:
335         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
336         gst_queue_locked_flush (queue);
337         break;
338       case GST_EVENT_EOS:
339         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "eos in on %s %d\n", 
340                            GST_ELEMENT_NAME (queue), queue->level_buffers);
341         break;
342       default:
343         /* we put the event in the queue, we don't have to act ourselves */
344         break;
345     }
346   }
347
348   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding buffer %p of size %d",buf,GST_BUFFER_SIZE(buf));
349
350   if (queue->level_buffers == queue->size_buffers) {
351     /* if this is a leaky queue... */
352     if (queue->leaky) {
353       /* FIXME don't want to leak events! */
354       /* if we leak on the upstream side, drop the current buffer */
355       if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
356         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end");
357         if (GST_IS_EVENT (buf))
358           fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
359               GST_ELEMENT_NAME(GST_ELEMENT(queue)),
360               GST_EVENT_TYPE(GST_EVENT(buf)));
361           GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end");
362         /* now we have to clean up and exit right away */
363         g_mutex_unlock (queue->qlock);
364         goto out_unref;
365       }
366       /* otherwise we have to push a buffer off the other end */
367       else {
368         gpointer front;
369         GstBuffer *leakbuf;
370
371         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end");
372
373         front = g_queue_pop_head (queue->queue);
374         leakbuf = (GstBuffer *)(front);
375
376         if (GST_IS_EVENT (leakbuf)) {
377           fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
378               GST_ELEMENT_NAME(GST_ELEMENT(queue)),
379               GST_EVENT_TYPE(GST_EVENT(leakbuf)));
380         }
381         queue->level_buffers--;
382         queue->level_bytes -= GST_BUFFER_SIZE(leakbuf);
383         gst_data_unref (GST_DATA (leakbuf));
384       }
385     }
386
387     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre full wait, level:%d/%d",
388                         queue->level_buffers, queue->size_buffers);
389
390     while (queue->level_buffers == queue->size_buffers) {
391       /* if there's a pending state change for this queue or its manager, switch */
392       /* back to iterator so bottom half of state change executes */
393       if (queue->interrupt) {
394         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!");
395         g_mutex_unlock (queue->qlock);
396         if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->sinkpad), GST_ELEMENT (queue)))
397           goto out_unref;
398         /* if we got here bacause we were unlocked after a flush, we don't need
399          * to add the buffer to the queue again */
400         if (queue->flush) {
401           GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "not adding pending buffer after flush");
402           goto out_unref;
403         }
404         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding pending buffer after interrupt");
405         goto restart;
406       }
407       if (GST_STATE (queue) != GST_STATE_PLAYING) {
408         /* this means the other end is shut down */
409         /* try to signal to resolve the error */
410         if (!queue->may_deadlock) {
411           g_mutex_unlock (queue->qlock);
412           gst_data_unref (GST_DATA (buf));
413           gst_element_error (GST_ELEMENT (queue), "deadlock found, source pad elements are shut down");
414           /* we don't want to goto out_unref here, since we want to clean up before calling gst_element_error */
415           return;
416         }
417         else {
418           g_print ("%s: waiting for the app to restart source pad elements\n", GST_ELEMENT_NAME (queue));
419         }
420       }
421
422       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_full, level:%d/%d", 
423                       queue->level_buffers, queue->size_buffers);
424       g_cond_wait (queue->not_full, queue->qlock);
425       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_full signal");
426     }
427     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post full wait, level:%d/%d",
428         queue->level_buffers, queue->size_buffers);
429   }
430
431   /* put the buffer on the tail of the list */
432   g_queue_push_tail (queue->queue, buf);
433
434   queue->level_buffers++;
435   queue->level_bytes += GST_BUFFER_SIZE(buf);
436
437   /* this assertion _has_ to hold */
438   g_assert (queue->queue->length == queue->level_buffers);
439
440   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)+ level:%d/%d",
441       GST_DEBUG_PAD_NAME(pad),
442       queue->level_buffers, queue->size_buffers);
443
444   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_empty");
445   g_cond_signal (queue->not_empty);
446   g_mutex_unlock (queue->qlock);
447
448   return;
449
450 out_unref:
451   gst_data_unref (GST_DATA (buf));
452   return;
453 }
454
455 static GstBuffer *
456 gst_queue_get (GstPad *pad)
457 {
458   GstQueue *queue;
459   GstBuffer *buf = NULL;
460   gpointer front;
461
462   g_assert(pad != NULL);
463   g_assert(GST_IS_PAD(pad));
464   g_return_val_if_fail (pad != NULL, NULL);
465   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
466
467   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
468
469 restart:
470   /* have to lock for thread-safety */
471   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%p", g_thread_self ());
472   g_mutex_lock (queue->qlock);
473   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%p %p", g_thread_self (), queue->not_empty);
474
475   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre empty wait, level:%d/%d", queue->level_buffers, queue->size_buffers);
476   while (queue->level_buffers == 0) {
477     /* if there's a pending state change for this queue or its manager, switch
478      * back to iterator so bottom half of state change executes
479      */ 
480     //while (GST_STATE_PENDING (queue) != GST_STATE_VOID_PENDING) {
481     if (queue->interrupt) {
482       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!");
483       g_mutex_unlock (queue->qlock);
484       if (gst_scheduler_interrupt (gst_pad_get_scheduler (queue->srcpad), GST_ELEMENT (queue)))
485         return GST_BUFFER (gst_event_new (GST_EVENT_INTERRUPT));
486       goto restart;
487     }
488     if (GST_STATE (queue) != GST_STATE_PLAYING) {
489       /* this means the other end is shut down */
490       if (!queue->may_deadlock) {
491         g_mutex_unlock (queue->qlock);
492         gst_element_error (GST_ELEMENT (queue), "deadlock found, sink pad elements are shut down");
493         goto restart;
494       }
495       else {
496         g_print ("%s: waiting for the app to restart source pad elements\n", GST_ELEMENT_NAME (queue));
497       }
498     }
499
500     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_empty, level:%d/%d", queue->level_buffers, queue->size_buffers);
501     
502     /* if (queue->block_timeout > -1){ */
503     if (FALSE) {
504       GTimeVal timeout;
505       g_get_current_time(&timeout);
506       g_time_val_add(&timeout, queue->block_timeout);
507       if (!g_cond_timed_wait (queue->not_empty, queue->qlock, &timeout)){
508         g_mutex_unlock (queue->qlock);
509         g_warning ("filler");
510         return GST_BUFFER(gst_event_new_filler());
511       }
512     }
513     else {
514       g_cond_wait (queue->not_empty, queue->qlock);
515     }
516     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_empty signal");
517   }
518   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post empty wait, level:%d/%d", queue->level_buffers, queue->size_buffers);
519
520   front = g_queue_pop_head (queue->queue);
521   buf = (GstBuffer *)(front);
522   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "retrieved buffer %p from queue", buf);
523
524   queue->level_buffers--;
525   queue->level_bytes -= GST_BUFFER_SIZE(buf);
526
527   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)- level:%d/%d",
528       GST_DEBUG_PAD_NAME(pad),
529       queue->level_buffers, queue->size_buffers);
530
531   /* this assertion _has_ to hold */
532   g_assert (queue->queue->length == queue->level_buffers);
533
534   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_full");
535   g_cond_signal (queue->not_full);
536
537   g_mutex_unlock (queue->qlock);
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   gint event_type;
562   gint flag_flush = 0;
563
564   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
565
566   g_mutex_lock (queue->qlock);
567
568   if (gst_element_get_state (GST_ELEMENT (queue)) == GST_STATE_PLAYING) {
569     /* push the event to the queue for upstream consumption */
570     g_async_queue_push(queue->events, event);
571     g_warning ("FIXME: sending event in a running queue");
572     /* FIXME wait for delivery of the event here, then return the result
573      * instead of FALSE */
574     res = FALSE;
575     goto done;
576   }
577
578   event_type = GST_EVENT_TYPE (event);
579   if (event_type == GST_EVENT_SEEK)
580     flag_flush = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH;
581
582   res = gst_pad_event_default (pad, event); 
583
584   switch (event_type) {
585     case GST_EVENT_FLUSH:
586       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
587       gst_queue_locked_flush (queue);
588       break;
589     case GST_EVENT_SEEK:
590       if (flag_flush) {
591         gst_queue_locked_flush (queue);
592       }
593     default:
594       break;
595   }
596
597 done:
598   g_mutex_unlock (queue->qlock);
599
600   /* we have to claim success, but we don't really know */
601   return res;
602 }
603
604 static gboolean
605 gst_queue_release_locks (GstElement *element)
606 {
607   GstQueue *queue;
608
609   queue = GST_QUEUE (element);
610
611   g_mutex_lock (queue->qlock);
612   queue->interrupt = TRUE;
613   g_cond_signal (queue->not_full);
614   g_cond_signal (queue->not_empty); 
615   g_mutex_unlock (queue->qlock);
616
617   return TRUE;
618 }
619
620 static GstElementStateReturn
621 gst_queue_change_state (GstElement *element)
622 {
623   GstQueue *queue;
624   GstElementStateReturn ret;
625
626   queue = GST_QUEUE (element);
627
628   GST_DEBUG_ENTER("('%s')", GST_ELEMENT_NAME (element));
629
630   /* lock the queue so another thread (not in sync with this thread's state)
631    * can't call this queue's _get (or whatever)
632    */
633   g_mutex_lock (queue->qlock);
634
635   switch (GST_STATE_TRANSITION (element)) {
636     case GST_STATE_NULL_TO_READY:
637       gst_queue_locked_flush (queue);
638       break;
639     case GST_STATE_READY_TO_PAUSED:
640       break;
641     case GST_STATE_PAUSED_TO_PLAYING:
642       if (!GST_PAD_IS_LINKED (queue->sinkpad)) {
643         GST_DEBUG_ELEMENT (GST_CAT_STATES, queue, "queue %s is not linked", GST_ELEMENT_NAME (queue));
644         /* FIXME can this be? */
645         g_cond_signal (queue->not_empty);
646
647         ret = GST_STATE_FAILURE;
648         goto error;
649       }
650       else {
651         GstScheduler *src_sched, *sink_sched;
652             
653         src_sched = gst_pad_get_scheduler (GST_PAD_CAST (queue->srcpad));
654         sink_sched = gst_pad_get_scheduler (GST_PAD_CAST (queue->sinkpad));
655
656         if (src_sched == sink_sched) {
657           GST_DEBUG_ELEMENT (GST_CAT_STATES, queue, "queue %s does not connect different schedulers", 
658                         GST_ELEMENT_NAME (queue));
659
660           g_warning ("queue %s does not connect different schedulers",
661                         GST_ELEMENT_NAME (queue));
662
663           ret = GST_STATE_FAILURE;
664           goto error;
665         }
666       }
667       queue->interrupt = FALSE;
668       break;
669     case GST_STATE_PLAYING_TO_PAUSED:
670       break;
671     case GST_STATE_PAUSED_TO_READY:
672       gst_queue_locked_flush (queue);
673       break;
674     case GST_STATE_READY_TO_NULL:
675       break;
676   }
677
678   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
679
680 error:
681   g_mutex_unlock (queue->qlock);
682
683   GST_DEBUG_LEAVE("('%s')", GST_ELEMENT_NAME (element));
684   return ret;
685 }
686
687
688 static void
689 gst_queue_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
690 {
691   GstQueue *queue;
692
693   /* it's not null if we got it, but it might not be ours */
694   g_return_if_fail (GST_IS_QUEUE (object));
695
696   queue = GST_QUEUE (object);
697
698   switch (prop_id) {
699     case ARG_LEAKY:
700       queue->leaky = g_value_get_enum (value);
701       break;
702     case ARG_MAX_LEVEL:
703       queue->size_buffers = g_value_get_int (value);
704       break;
705     case ARG_MAY_DEADLOCK:
706       queue->may_deadlock = g_value_get_boolean (value);
707       break;
708     case ARG_BLOCK_TIMEOUT:
709       queue->block_timeout = g_value_get_int (value);
710       break;
711     default:
712       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
713       break;
714   }
715 }
716
717 static void
718 gst_queue_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
719 {
720   GstQueue *queue;
721
722   /* it's not null if we got it, but it might not be ours */
723   g_return_if_fail (GST_IS_QUEUE (object));
724
725   queue = GST_QUEUE (object);
726
727   switch (prop_id) {
728     case ARG_LEAKY:
729       g_value_set_enum (value, queue->leaky);
730       break;
731     case ARG_LEVEL:
732       g_value_set_int (value, queue->level_buffers);
733       break;
734     case ARG_MAX_LEVEL:
735       g_value_set_int (value, queue->size_buffers);
736       break;
737     case ARG_MAY_DEADLOCK:
738       g_value_set_boolean (value, queue->may_deadlock);
739       break;
740     case ARG_BLOCK_TIMEOUT:
741       g_value_set_int (value, queue->block_timeout);
742       break;
743     default:
744       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
745       break;
746   }
747 }