Fixed some warnings.
[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_arg       (GtkObject *object, GtkArg *arg, guint id);
74 static void                     gst_queue_get_arg       (GtkObject *object, GtkArg *arg, guint id);
75
76 static gboolean                 gst_queue_handle_eos    (GstPad *pad);
77 static GstPadNegotiateReturn    gst_queue_handle_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data);
78 static GstPadNegotiateReturn    gst_queue_handle_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data);
79 static void                     gst_queue_chain         (GstPad *pad, GstBuffer *buf);
80 static GstBuffer *              gst_queue_get           (GstPad *pad);
81 static GstBufferPool*           gst_queue_get_bufferpool (GstPad *pad);
82         
83 static void                     gst_queue_flush         (GstQueue *queue);
84
85 static GstElementStateReturn    gst_queue_change_state  (GstElement *element);
86
87   
88 static GtkType
89 queue_leaky_get_type(void) {
90   static GtkType queue_leaky_type = 0;
91   static GtkEnumValue queue_leaky[] = {
92     { GST_QUEUE_NO_LEAK, "0", "Not Leaky" },
93     { GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream" },
94     { GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream" },
95     { 0, NULL, NULL },
96   };
97   if (!queue_leaky_type) {
98     queue_leaky_type = gtk_type_register_enum("GstQueueLeaky", queue_leaky);
99   }
100   return queue_leaky_type;
101 }
102 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type())
103
104
105 static GstElementClass *parent_class = NULL;
106 //static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
107
108 GtkType
109 gst_queue_get_type(void) {
110   static GtkType queue_type = 0;
111
112   if (!queue_type) {
113     static const GtkTypeInfo queue_info = {
114       "GstQueue",
115       sizeof(GstQueue),
116       sizeof(GstQueueClass),
117       (GtkClassInitFunc)gst_queue_class_init,
118       (GtkObjectInitFunc)gst_queue_init,
119       (GtkArgSetFunc)gst_queue_set_arg,
120       (GtkArgGetFunc)gst_queue_get_arg,
121       (GtkClassInitFunc)NULL,
122     };
123     queue_type = gtk_type_unique (GST_TYPE_ELEMENT, &queue_info);
124   }
125   return queue_type;
126 }
127
128 static void
129 gst_queue_class_init (GstQueueClass *klass)
130 {
131   GtkObjectClass *gtkobject_class;
132   GstElementClass *gstelement_class;
133
134   gtkobject_class = (GtkObjectClass*)klass;
135   gstelement_class = (GstElementClass*)klass;
136
137   parent_class = gtk_type_class (GST_TYPE_ELEMENT);
138
139   gtk_object_add_arg_type ("GstQueue::leaky", GST_TYPE_QUEUE_LEAKY,
140                            GTK_ARG_READWRITE, ARG_LEAKY);
141   gtk_object_add_arg_type ("GstQueue::level", GTK_TYPE_INT,
142                            GTK_ARG_READABLE, ARG_LEVEL);
143   gtk_object_add_arg_type ("GstQueue::max_level", GTK_TYPE_INT,
144                            GTK_ARG_READWRITE, ARG_MAX_LEVEL);
145
146   gtkobject_class->set_arg = gst_queue_set_arg;
147   gtkobject_class->get_arg = gst_queue_get_arg;
148
149   gstelement_class->change_state = gst_queue_change_state;
150 }
151
152 static void
153 gst_queue_init (GstQueue *queue)
154 {
155   // scheduling on this kind of element is, well, interesting
156   GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
157
158   queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
159   gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR(gst_queue_chain));
160   gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
161   gst_pad_set_eos_function (queue->sinkpad, gst_queue_handle_eos);
162   gst_pad_set_negotiate_function (queue->sinkpad, gst_queue_handle_negotiate_sink);
163   gst_pad_set_bufferpool_function (queue->sinkpad, gst_queue_get_bufferpool);
164
165   queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
166   gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR(gst_queue_get));
167   gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
168   gst_pad_set_negotiate_function (queue->srcpad, gst_queue_handle_negotiate_src);
169
170   queue->queue = NULL;
171   queue->level_buffers = 0;
172   queue->level_bytes = 0;
173   queue->level_time = 0LL;
174   queue->size_buffers = 100;            // 100 buffers
175   queue->size_bytes = 100 * 1024;       // 100KB
176   queue->size_time = 1000000000LL;      // 1sec
177
178   queue->emptycond = g_cond_new ();
179   queue->fullcond = g_cond_new ();
180   GST_DEBUG(GST_CAT_THREAD, "initialized queue's emptycond and fullcond\n");
181 }
182
183 static GstBufferPool*
184 gst_queue_get_bufferpool (GstPad *pad)
185 {
186   GstQueue *queue;
187
188   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
189
190   return gst_pad_get_bufferpool (queue->srcpad);
191 }
192
193 static GstPadNegotiateReturn
194 gst_queue_handle_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data)
195 {
196   GstQueue *queue;
197
198   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
199
200   return gst_pad_negotiate_proxy (pad, queue->sinkpad, caps);
201 }
202
203 static GstPadNegotiateReturn
204 gst_queue_handle_negotiate_sink (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->srcpad, caps);
211 }
212
213 static gboolean
214 gst_queue_handle_eos (GstPad *pad)
215 {
216   GstQueue *queue;
217
218   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
219
220   GST_DEBUG (GST_CAT_DATAFLOW,"%s received eos\n", GST_ELEMENT_NAME (queue));
221
222   GST_LOCK (queue);
223   GST_DEBUG (GST_CAT_DATAFLOW,"%s has %d buffers left\n", GST_ELEMENT_NAME (queue),
224                   queue->level_buffers);
225
226   GST_FLAG_SET (pad, GST_PAD_EOS);
227
228   g_cond_signal (queue->emptycond);
229
230   GST_UNLOCK (queue);
231
232   return TRUE;
233 }
234
235 static void
236 gst_queue_cleanup_buffers (gpointer data, const gpointer user_data)
237 {
238   GST_DEBUG (GST_CAT_DATAFLOW,"%s cleaning buffer %p\n", (gchar *)user_data, data);
239
240   gst_buffer_unref (GST_BUFFER (data));
241 }
242
243 static void
244 gst_queue_flush (GstQueue *queue)
245 {
246   g_slist_foreach (queue->queue, gst_queue_cleanup_buffers,
247                   (char *) GST_ELEMENT_NAME (queue));
248   g_slist_free (queue->queue);
249
250   queue->queue = NULL;
251   queue->level_buffers = 0;
252   queue->timeval = NULL;
253 }
254
255 static void
256 gst_queue_chain (GstPad *pad, GstBuffer *buf)
257 {
258   GstQueue *queue;
259   const guchar *name;
260
261   g_return_if_fail (pad != NULL);
262   g_return_if_fail (GST_IS_PAD (pad));
263   g_return_if_fail (buf != NULL);
264
265   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
266   name = GST_ELEMENT_NAME (queue);
267
268   /* we have to lock the queue since we span threads */
269
270 //  GST_DEBUG (GST_CAT_DATAFLOW,"trying to get lock on queue \"%s\"\n",name);
271   GST_LOCK (queue);
272
273   if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLUSH)) {
274     GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "buffer has FLUSH bit set, flushing queue\n");
275     gst_queue_flush (queue);
276   }
277
278   GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding buffer %p of size %d\n",buf,GST_BUFFER_SIZE(buf));
279
280   if (queue->level_buffers >= queue->size_buffers) {
281     // if this is a leaky queue...
282     if (queue->leaky) {
283       // if we leak on the upstream side, drop the current buffer
284       if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
285         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end\n");
286         gst_buffer_unref(buf);
287         // now we have to clean up and exit right away
288         GST_UNLOCK (queue);
289         return;
290       }
291       // otherwise we have to push a buffer off the other end
292       else {
293         GSList *front;
294         GstBuffer *leakbuf;
295         GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end\n");
296         front = queue->queue;
297         leakbuf = (GstBuffer *)(front->data);
298         queue->level_buffers--;
299         queue->level_bytes -= GST_BUFFER_SIZE(leakbuf);
300         gst_buffer_unref(leakbuf);
301         queue->queue = g_slist_remove_link (queue->queue, front);
302         g_slist_free (front);
303       }
304     }
305
306     while (queue->level_buffers >= queue->size_buffers) {
307       // if there's a pending state change for this queue or its manager, switch
308       // back to iterator so bottom half of state change executes
309       if (GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING ||
310 //          GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING)
311 GST_STATE_PENDING(GST_SCHED_PARENT(GST_ELEMENT_SCHED(GST_PAD_PARENT(GST_PAD_PEER(queue->sinkpad))))) != 
312 GST_STATE_NONE_PENDING)
313       {
314         GST_DEBUG(GST_CAT_DATAFLOW,"interrupted!!\n");
315         if (GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING)
316           GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING)\n");
317         if (GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING)
318           GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING\n");
319         GST_UNLOCK(queue);
320         cothread_switch(cothread_current_main());
321       }
322
323       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for space, level is %d\n", queue->level_buffers);
324       g_cond_signal (queue->emptycond);
325       g_cond_wait (queue->fullcond, GST_OBJECT(queue)->lock);
326       GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "done waiting, level is now %d\n", queue->level_buffers);
327     }
328   }
329
330   /* put the buffer on the tail of the list */
331   queue->queue = g_slist_append (queue->queue, buf);
332   queue->level_buffers++;
333   queue->level_bytes += GST_BUFFER_SIZE(buf);
334 //  GST_DEBUG (GST_CAT_DATAFLOW, "(%s:%s)+\n",GST_DEBUG_PAD_NAME(pad));
335
336   /* if we were empty, but aren't any more, signal a condition */
337   if (queue->level_buffers == 1)
338   {
339     GST_DEBUG (GST_CAT_DATAFLOW,"%s signalling emptycond\n", name);
340     g_cond_signal (queue->emptycond);
341   }
342
343   GST_UNLOCK (queue);
344 }
345
346 static GstBuffer *
347 gst_queue_get (GstPad *pad)
348 {
349   GstQueue *queue;
350   GstBuffer *buf = NULL;
351   GSList *front;
352   const guchar *name;
353
354   g_assert(pad != NULL);
355   g_assert(GST_IS_PAD(pad));
356   g_return_val_if_fail (pad != NULL, NULL);
357   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
358
359   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
360   name = GST_ELEMENT_NAME (queue);
361
362   /* have to lock for thread-safety */
363   GST_DEBUG (GST_CAT_DATAFLOW,"%s try have queue lock\n", name);
364   GST_LOCK (queue);
365   GST_DEBUG (GST_CAT_DATAFLOW,"%s push %d %ld %p\n", name, queue->level_buffers, pthread_self (), queue->emptycond);
366   GST_DEBUG (GST_CAT_DATAFLOW,"%s have queue lock\n", name);
367
368   while (!queue->level_buffers) {
369     if (GST_FLAG_IS_SET (queue->sinkpad, GST_PAD_EOS)) {
370       GST_DEBUG (GST_CAT_DATAFLOW, "%s U released lock\n", name);
371       GST_UNLOCK(queue);
372       gst_pad_set_eos (queue->srcpad);
373       // this return NULL shouldn't hurt anything...
374       return NULL;
375     }
376
377     // if there's a pending state change for this queue or its manager, switch
378     // back to iterator so bottom half of state change executes
379     if (GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING ||
380 //        GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING)
381 GST_STATE_PENDING(GST_SCHED_PARENT(GST_ELEMENT_SCHED(GST_PAD_PARENT(GST_PAD_PEER(queue->srcpad))))) != 
382 GST_STATE_NONE_PENDING)
383     {
384       GST_DEBUG(GST_CAT_DATAFLOW,"interrupted!!\n");
385       if (GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING)
386         GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(queue) != GST_STATE_NONE_PENDING)\n");
387       if (GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING)
388         GST_DEBUG(GST_CAT_DATAFLOW,"GST_STATE_PENDING(GST_SCHEDULE(GST_ELEMENT(queue)->sched)->parent) != GST_STATE_NONE_PENDING\n");
389       GST_UNLOCK(queue);
390       cothread_switch(cothread_current_main());
391     }
392
393     g_cond_signal (queue->fullcond);
394     g_cond_wait (queue->emptycond, GST_OBJECT(queue)->lock);
395   }
396
397   front = queue->queue;
398   buf = (GstBuffer *)(front->data);
399   GST_DEBUG (GST_CAT_DATAFLOW,"retrieved buffer %p from queue\n",buf);
400   queue->queue = g_slist_remove_link (queue->queue, front);
401   g_slist_free (front);
402
403 //  if (queue->level_buffers < queue->size_buffers)
404   if (queue->level_buffers == queue->size_buffers)
405   {
406     GST_DEBUG (GST_CAT_DATAFLOW,"%s signalling fullcond\n", name);
407     g_cond_signal (queue->fullcond);
408   }
409
410   queue->level_buffers--;
411   queue->level_bytes -= GST_BUFFER_SIZE(buf);
412   GST_DEBUG (GST_CAT_DATAFLOW,"(%s:%s)- ",GST_DEBUG_PAD_NAME(pad));
413
414   GST_UNLOCK(queue);
415
416   return buf;
417 }
418
419 static GstElementStateReturn
420 gst_queue_change_state (GstElement *element)
421 {
422   GstQueue *queue;
423   GstElementStateReturn ret;
424   g_return_val_if_fail (GST_IS_QUEUE (element), GST_STATE_FAILURE);
425
426   queue = GST_QUEUE (element);
427
428   // lock the queue so another thread (not in sync with this thread's state)
429   // can't call this queue's _get (or whatever)
430   GST_LOCK (queue);
431
432   /* if going down into NULL state, clear out buffers*/
433   if (GST_STATE_PENDING (element) == GST_STATE_READY) {
434     /* otherwise (READY or higher) we need to open the file */
435     gst_queue_flush (queue);
436   }
437
438   /* if we haven't failed already, give the parent class a chance to ;-) */
439   if (GST_ELEMENT_CLASS (parent_class)->change_state)
440   {
441     gboolean valid_handler = FALSE;
442     guint state_change_id = gtk_signal_lookup("state_change", GTK_OBJECT_TYPE(element));
443
444     // determine whether we need to block the parent (element) class'
445     // STATE_CHANGE signal so we can UNLOCK before returning.  we block
446     // it if we could find the state_change signal AND there's a signal
447     // handler attached to it.
448     //
449     // note: this assumes that change_state() *only* emits state_change signal.
450     // if element change_state() emits other signals, they need to be blocked
451     // as well.
452     if (state_change_id &&
453         gtk_signal_handler_pending(GTK_OBJECT(element), state_change_id, FALSE))
454       valid_handler = TRUE;
455     if (valid_handler)
456       gtk_signal_handler_block(GTK_OBJECT(element), state_change_id);
457
458     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
459
460     if (valid_handler)
461       gtk_signal_handler_unblock(GTK_OBJECT(element), state_change_id);
462
463     // UNLOCK, *then* emit signal (if there's one there)
464     GST_UNLOCK(queue);
465     if (valid_handler)
466       gtk_signal_emit(GTK_OBJECT (element), state_change_id, GST_STATE(element));
467   }
468   else
469   {
470     ret = GST_STATE_SUCCESS;
471     GST_UNLOCK(queue);
472   }
473
474   return ret;
475 }
476
477
478 static void
479 gst_queue_set_arg (GtkObject *object, GtkArg *arg, guint id)
480 {
481   GstQueue *queue;
482
483   /* it's not null if we got it, but it might not be ours */
484   g_return_if_fail (GST_IS_QUEUE (object));
485
486   queue = GST_QUEUE (object);
487
488   switch(id) {
489     case ARG_LEAKY:
490       queue->leaky = GTK_VALUE_INT (*arg);
491       break;
492     case ARG_MAX_LEVEL:
493       queue->size_buffers = GTK_VALUE_INT (*arg);
494       break;
495     default:
496       break;
497   }
498 }
499
500 static void
501 gst_queue_get_arg (GtkObject *object, GtkArg *arg, guint id)
502 {
503   GstQueue *queue;
504
505   /* it's not null if we got it, but it might not be ours */
506   g_return_if_fail (GST_IS_QUEUE (object));
507
508   queue = GST_QUEUE (object);
509
510   switch (id) {
511     case ARG_LEAKY:
512       GTK_VALUE_INT (*arg) = queue->leaky;
513       break;
514     case ARG_LEVEL:
515       GTK_VALUE_INT (*arg) = queue->level_buffers;
516       break;
517     case ARG_MAX_LEVEL:
518       GTK_VALUE_INT (*arg) = queue->size_buffers;
519       break;
520     default:
521       arg->type = GTK_TYPE_INVALID;
522       break;
523   }
524 }