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