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