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