aye ladie, no more ugly // comments here, even if Taaz gets upset about it
[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
39 GstElementDetails gst_queue_details = {
40   "Queue",
41   "Connection",
42   "Simple data queue",
43   VERSION,
44   "Erik Walthinsen <omega@cse.ogi.edu>",
45   "(C) 1999",
46 };
47
48
49 /* Queue signals and args */
50 enum {
51   LOW_WATERMARK,
52   HIGH_WATERMARK,
53   LAST_SIGNAL
54 };
55
56 enum {
57   ARG_0,
58   ARG_LEVEL_BUFFERS,
59   ARG_LEVEL_BYTES,
60   ARG_LEVEL_TIME,
61   ARG_SIZE_BUFFERS,
62   ARG_SIZE_BYTES,
63   ARG_SIZE_TIME,
64   ARG_LEAKY,
65   ARG_LEVEL,
66   ARG_MAX_LEVEL,
67 };
68
69
70 static void                     gst_queue_class_init            (GstQueueClass *klass);
71 static void                     gst_queue_init                  (GstQueue *queue);
72
73 static void                     gst_queue_set_property          (GObject *object, guint prop_id, 
74                                                                  const GValue *value, GParamSpec *pspec);
75 static void                     gst_queue_get_property          (GObject *object, guint prop_id, 
76                                                                  GValue *value, GParamSpec *pspec);
77
78 static GstPadNegotiateReturn    gst_queue_handle_negotiate_src  (GstPad *pad, GstCaps **caps, gpointer *data);
79 static GstPadNegotiateReturn    gst_queue_handle_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data);
80 static void                     gst_queue_chain                 (GstPad *pad, GstBuffer *buf);
81 static GstBuffer *              gst_queue_get                   (GstPad *pad);
82 static GstBufferPool*           gst_queue_get_bufferpool        (GstPad *pad);
83         
84 static void                     gst_queue_locked_flush                  (GstQueue *queue);
85 static void                     gst_queue_flush                 (GstQueue *queue);
86
87 static GstElementStateReturn    gst_queue_change_state          (GstElement *element);
88
89   
90 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type())
91 static GType
92 queue_leaky_get_type(void) {
93   static GType queue_leaky_type = 0;
94   static GEnumValue queue_leaky[] = {
95     { GST_QUEUE_NO_LEAK,                "0", "Not Leaky" },
96     { GST_QUEUE_LEAK_UPSTREAM,          "1", "Leaky on Upstream" },
97     { GST_QUEUE_LEAK_DOWNSTREAM,        "2", "Leaky on Downstream" },
98     { 0, NULL, NULL },
99   };
100   if (!queue_leaky_type) {
101     queue_leaky_type = g_enum_register_static("GstQueueLeaky", queue_leaky);
102   }
103   return queue_leaky_type;
104 }
105
106 static GstElementClass *parent_class = NULL;
107 /* static guint gst_queue_signals[LAST_SIGNAL] = { 0 }; */
108
109 GType
110 gst_queue_get_type(void) 
111 {
112   static GType queue_type = 0;
113
114   if (!queue_type) {
115     static const GTypeInfo queue_info = {
116       sizeof(GstQueueClass),
117       NULL,
118       NULL,
119       (GClassInitFunc)gst_queue_class_init,
120       NULL,
121       NULL,
122       sizeof(GstQueue),
123       4,
124       (GInstanceInitFunc)gst_queue_init,
125       NULL
126     };
127     queue_type = g_type_register_static (GST_TYPE_ELEMENT, "GstQueue", &queue_info, 0);
128   }
129   return queue_type;
130 }
131
132 static void
133 gst_queue_class_init (GstQueueClass *klass)
134 {
135   GObjectClass *gobject_class;
136   GstElementClass *gstelement_class;
137
138   gobject_class = (GObjectClass*)klass;
139   gstelement_class = (GstElementClass*)klass;
140
141   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
142
143   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LEAKY,
144     g_param_spec_enum("leaky","Leaky","Where the queue leaks, if at all.",
145                       GST_TYPE_QUEUE_LEAKY,GST_QUEUE_NO_LEAK,G_PARAM_READWRITE));
146   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LEVEL,
147     g_param_spec_int("level","Level","How many buffers are in the queue.",
148                      0,G_MAXINT,0,G_PARAM_READABLE));
149   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MAX_LEVEL,
150     g_param_spec_int("max_level","Maximum Level","How many buffers the queue holds.",
151                      0,G_MAXINT,100,G_PARAM_READWRITE));
152
153   gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_queue_set_property);
154   gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_queue_get_property);
155
156   gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_queue_change_state);
157 }
158
159 static void
160 gst_queue_init (GstQueue *queue)
161 {
162   /* scheduling on this kind of element is, well, interesting */
163   GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
164   GST_FLAG_SET (queue, GST_ELEMENT_EVENT_AWARE);
165
166   queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
167   gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_chain));
168   gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
169   gst_pad_set_negotiate_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_handle_negotiate_sink));
170   gst_pad_set_bufferpool_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_get_bufferpool));
171
172   queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
173   gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_get));
174   gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
175   gst_pad_set_negotiate_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_handle_negotiate_src));
176
177   queue->queue = NULL;
178   queue->level_buffers = 0;
179   queue->level_bytes = 0;
180   queue->level_time = 0LL;
181   queue->size_buffers = 100;            /* 100 buffers */
182   queue->size_bytes = 100 * 1024;       /* 100KB */
183   queue->size_time = 1000000000LL;      /* 1sec */
184
185   queue->qlock = g_mutex_new ();
186   queue->reader = FALSE;
187   queue->writer = FALSE;
188   queue->not_empty = g_cond_new ();
189   queue->not_full = g_cond_new ();
190   GST_DEBUG_ELEMENT (GST_CAT_THREAD, queue, "initialized queue's not_empty & not_full conditions\n");
191 }
192
193 static GstBufferPool*
194 gst_queue_get_bufferpool (GstPad *pad)
195 {
196   GstQueue *queue;
197
198   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
199
200   return gst_pad_get_bufferpool (queue->srcpad);
201 }
202
203 static GstPadNegotiateReturn
204 gst_queue_handle_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data)
205 {
206   GstQueue *queue;
207
208   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
209
210   return gst_pad_negotiate_proxy (pad, queue->sinkpad, caps);
211 }
212
213 static GstPadNegotiateReturn
214 gst_queue_handle_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data)
215 {
216   GstQueue *queue;
217
218   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
219
220   return gst_pad_negotiate_proxy (pad, queue->srcpad, caps);
221 }
222
223 static void
224 gst_queue_cleanup_buffers (gpointer data, const gpointer user_data)
225 {
226   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, user_data, "cleaning buffer %p\n", data);
227
228   if (GST_IS_BUFFER (data)) {
229     gst_buffer_unref (GST_BUFFER (data));
230   }
231 }
232
233 static void
234 gst_queue_locked_flush (GstQueue *queue)
235 {
236   g_slist_foreach (queue->queue, gst_queue_cleanup_buffers,
237                   (gpointer) queue);
238   g_slist_free (queue->queue);
239
240   queue->queue = NULL;
241   queue->level_buffers = 0;
242   queue->timeval = NULL;
243 }
244
245 static void
246 gst_queue_flush (GstQueue *queue)
247 {
248   g_mutex_lock (queue->qlock);
249   gst_queue_locked_flush (queue);
250   g_mutex_unlock (queue->qlock);
251 }
252
253
254 static void
255 gst_queue_chain (GstPad *pad, GstBuffer *buf)
256 {
257   GstQueue *queue;
258   gboolean reader;
259
260   g_return_if_fail (pad != NULL);
261   g_return_if_fail (GST_IS_PAD (pad));
262   g_return_if_fail (buf != NULL);
263
264   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
265
266   reader = FALSE;
267
268 restart:
269   /* we have to lock the queue since we span threads */
270   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%ld\n", pthread_self ());
271   g_mutex_lock (queue->qlock);
272   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%ld\n", pthread_self ());
273
274   if (GST_IS_EVENT (buf)) {
275     switch (GST_EVENT_TYPE (buf)) {
276       case GST_EVENT_FLUSH:
277         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
278         gst_queue_locked_flush (queue);
279         break;
280       case GST_EVENT_EOS:
281         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "eos in on %s %d\n", 
282                            GST_ELEMENT_NAME (queue), queue->level_buffers);
283         break;
284       default:
285         gst_pad_event_default (pad, GST_EVENT (buf));
286         break;
287     }
288   }
289
290   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding buffer %p of size %d\n",buf,GST_BUFFER_SIZE(buf));
291
292   if (queue->level_buffers == queue->size_buffers) {
293     /* if this is a leaky queue... */
294     if (queue->leaky) {
295       /* FIXME don't want to leak events! */
296       /* if we leak on the upstream side, drop the current buffer */
297       if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
298         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end\n");
299         if (GST_IS_EVENT (buf))
300           fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
301               GST_ELEMENT_NAME(GST_ELEMENT(queue)),
302               GST_EVENT_TYPE(GST_EVENT(buf)));
303           GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end\n");
304         gst_buffer_unref(buf);
305         /* now we have to clean up and exit right away */
306         g_mutex_unlock (queue->qlock);
307         return;
308       }
309       /* otherwise we have to push a buffer off the other end */
310       else {
311         GSList *front;
312         GstBuffer *leakbuf;
313         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end\n");
314         front = queue->queue;
315         leakbuf = (GstBuffer *)(front->data);
316         if (GST_IS_EVENT (leakbuf))
317           fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
318               GST_ELEMENT_NAME(GST_ELEMENT(queue)),
319               GST_EVENT_TYPE(GST_EVENT(leakbuf)));
320         queue->level_buffers--;
321         queue->level_bytes -= GST_BUFFER_SIZE(leakbuf);
322         gst_buffer_unref(leakbuf);
323         queue->queue = g_slist_remove_link (queue->queue, front);
324         g_slist_free (front);
325       }
326     }
327
328     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre full wait, level:%d/%d\n",
329         queue->level_buffers, queue->size_buffers);
330     while (queue->level_buffers == queue->size_buffers) {
331       /* if there's a pending state change for this queue or its manager, switch */
332       /* back to iterator so bottom half of state change executes */
333       while (GST_STATE (queue) != GST_STATE_PLAYING) {
334         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!\n");
335         g_mutex_unlock (queue->qlock);
336         cothread_switch(cothread_current_main());
337         goto restart;
338       }
339       g_assert (GST_STATE (queue) == GST_STATE_PLAYING);
340
341       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_full, level:%d/%d\n", queue->level_buffers, queue->size_buffers);
342       if (queue->writer)
343         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "WARNING: multiple writers on queue!\n");
344       queue->writer = TRUE;
345       g_cond_wait (queue->not_full, queue->qlock);
346       queue->writer = FALSE;
347       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_full signal\n");
348     }
349     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post full wait, level:%d/%d\n",
350         queue->level_buffers, queue->size_buffers);
351   }
352
353   /* put the buffer on the tail of the list */
354   queue->queue = g_slist_append (queue->queue, buf);
355   queue->level_buffers++;
356   queue->level_bytes += GST_BUFFER_SIZE(buf);
357   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)+ level:%d/%d\n",
358       GST_DEBUG_PAD_NAME(pad),
359       queue->level_buffers, queue->size_buffers);
360
361   /* reader waiting on an empty queue */
362   reader = queue->reader;
363
364   g_mutex_unlock (queue->qlock);
365
366   if (reader)
367   {
368     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_empty\n");
369     g_cond_signal (queue->not_empty);
370   }
371 }
372
373 static GstBuffer *
374 gst_queue_get (GstPad *pad)
375 {
376   GstQueue *queue;
377   GstBuffer *buf = NULL;
378   GSList *front;
379   gboolean writer;
380
381   g_assert(pad != NULL);
382   g_assert(GST_IS_PAD(pad));
383   g_return_val_if_fail (pad != NULL, NULL);
384   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
385
386   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
387
388   writer = FALSE;
389
390 restart:
391   /* have to lock for thread-safety */
392   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%ld\n", pthread_self ());
393   g_mutex_lock (queue->qlock);
394   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%ld %p\n", pthread_self (), queue->not_empty);
395
396   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre empty wait, level:%d/%d\n", queue->level_buffers, queue->size_buffers);
397   while (queue->level_buffers == 0) {
398     /* if there's a pending state change for this queue or its manager, switch
399      * back to iterator so bottom half of state change executes
400      */ 
401     while (GST_STATE (queue) != GST_STATE_PLAYING) {
402       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!\n");
403       g_mutex_unlock (queue->qlock);
404       cothread_switch(cothread_current_main());
405       goto restart;
406     }
407     g_assert (GST_STATE (queue) == GST_STATE_PLAYING);
408
409     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_empty, level:%d/%d\n", queue->level_buffers, queue->size_buffers);
410     if (queue->reader)
411       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "WARNING: multiple readers on queue!\n");
412     queue->reader = TRUE;
413     g_cond_wait (queue->not_empty, queue->qlock);
414     queue->reader = FALSE;
415     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_empty signal\n");
416   }
417   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post empty wait, level:%d/%d\n", queue->level_buffers, queue->size_buffers);
418
419   front = queue->queue;
420   buf = (GstBuffer *)(front->data);
421   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "retrieved buffer %p from queue\n", buf);
422   queue->queue = g_slist_remove_link (queue->queue, front);
423   g_slist_free (front);
424
425   queue->level_buffers--;
426   queue->level_bytes -= GST_BUFFER_SIZE(buf);
427   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)- level:%d/%d\n",
428       GST_DEBUG_PAD_NAME(pad),
429       queue->level_buffers, queue->size_buffers);
430
431   /* writer waiting on a full queue */
432   writer = queue->writer;
433
434   g_mutex_unlock (queue->qlock);
435
436   if (writer)
437   {
438     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_full\n");
439     g_cond_signal (queue->not_full);
440   }
441
442   /* FIXME where should this be? locked? */
443   if (GST_IS_EVENT(buf)) {
444     GstEvent *event = GST_EVENT(buf);
445     switch (GST_EVENT_TYPE(event)) {
446       case GST_EVENT_EOS:
447         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue \"%s\" eos\n", GST_ELEMENT_NAME (queue));
448         gst_element_set_state (GST_ELEMENT (queue), GST_STATE_PAUSED);
449         break;
450       default:
451         break;
452     }
453   }
454
455   return buf;
456 }
457
458 static GstElementStateReturn
459 gst_queue_change_state (GstElement *element)
460 {
461   GstQueue *queue;
462   GstElementStateReturn ret;
463   GstElementState new_state;
464   g_return_val_if_fail (GST_IS_QUEUE (element), GST_STATE_FAILURE);
465
466   queue = GST_QUEUE (element);
467
468   GST_DEBUG_ENTER("('%s')", GST_ELEMENT_NAME (element));
469
470   /* lock the queue so another thread (not in sync with this thread's state)
471    * can't call this queue's _get (or whatever)
472    */
473   g_mutex_lock (queue->qlock);
474
475   new_state = GST_STATE_PENDING (element);
476
477   if (new_state == GST_STATE_PAUSED) {
478     g_cond_signal (queue->not_full);
479     g_cond_signal (queue->not_empty);
480   }
481   else if (new_state == GST_STATE_READY) {
482     gst_queue_locked_flush (queue);
483   }
484   else if (new_state == GST_STATE_PLAYING) {
485     if (!GST_PAD_CONNECTED (queue->sinkpad)) {
486       /* FIXME can this be? */
487       if (queue->reader)
488         g_cond_signal (queue->not_empty);
489       g_mutex_unlock (queue->qlock);
490
491       return GST_STATE_FAILURE;
492     }
493   }
494
495   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
496   g_mutex_unlock (queue->qlock);
497
498   GST_DEBUG_LEAVE("('%s')", GST_ELEMENT_NAME (element));
499   return ret;
500 }
501
502
503 static void
504 gst_queue_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
505 {
506   GstQueue *queue;
507
508   /* it's not null if we got it, but it might not be ours */
509   g_return_if_fail (GST_IS_QUEUE (object));
510
511   queue = GST_QUEUE (object);
512
513   switch (prop_id) {
514     case ARG_LEAKY:
515       queue->leaky = g_value_get_int(value);
516       break;
517     case ARG_MAX_LEVEL:
518       queue->size_buffers = g_value_get_int(value);
519       break;
520     default:
521       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
522       break;
523   }
524 }
525
526 static void
527 gst_queue_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
528 {
529   GstQueue *queue;
530
531   /* it's not null if we got it, but it might not be ours */
532   g_return_if_fail (GST_IS_QUEUE (object));
533
534   queue = GST_QUEUE (object);
535
536   switch (prop_id) {
537     case ARG_LEAKY:
538       g_value_set_int(value, queue->leaky);
539       break;
540     case ARG_LEVEL:
541       g_value_set_int(value, queue->level_buffers);
542       break;
543     case ARG_MAX_LEVEL:
544       g_value_set_int(value, queue->size_buffers);
545       break;
546     default:
547       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
548       break;
549   }
550 }