Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst-libs / gst / audio / gstaudiosink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstaudiosink.c: simple audio sink base class
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 /**
24  * SECTION:gstaudiosink
25  * @short_description: Simple base class for audio sinks
26  * @see_also: #GstBaseAudioSink, #GstRingBuffer, #GstAudioSink.
27  *
28  * This is the most simple base class for audio sinks that only requires
29  * subclasses to implement a set of simple functions:
30  *
31  * <variablelist>
32  *   <varlistentry>
33  *     <term>open()</term>
34  *     <listitem><para>Open the device.</para></listitem>
35  *   </varlistentry>
36  *   <varlistentry>
37  *     <term>prepare()</term>
38  *     <listitem><para>Configure the device with the specified format.</para></listitem>
39  *   </varlistentry>
40  *   <varlistentry>
41  *     <term>write()</term>
42  *     <listitem><para>Write samples to the device.</para></listitem>
43  *   </varlistentry>
44  *   <varlistentry>
45  *     <term>reset()</term>
46  *     <listitem><para>Unblock writes and flush the device.</para></listitem>
47  *   </varlistentry>
48  *   <varlistentry>
49  *     <term>delay()</term>
50  *     <listitem><para>Get the number of samples written but not yet played 
51  *     by the device.</para></listitem>
52  *   </varlistentry>
53  *   <varlistentry>
54  *     <term>unprepare()</term>
55  *     <listitem><para>Undo operations done by prepare.</para></listitem>
56  *   </varlistentry>
57  *   <varlistentry>
58  *     <term>close()</term>
59  *     <listitem><para>Close the device.</para></listitem>
60  *   </varlistentry>
61  * </variablelist>
62  *
63  * All scheduling of samples and timestamps is done in this base class
64  * together with #GstBaseAudioSink using a default implementation of a
65  * #GstRingBuffer that uses threads.
66  *
67  * Last reviewed on 2006-09-27 (0.10.12)
68  */
69
70 #include <string.h>
71
72 #include "gstaudiosink.h"
73
74 GST_DEBUG_CATEGORY_STATIC (gst_audio_sink_debug);
75 #define GST_CAT_DEFAULT gst_audio_sink_debug
76
77 #define GST_TYPE_AUDIORING_BUFFER        \
78         (gst_audioringbuffer_get_type())
79 #define GST_AUDIORING_BUFFER(obj)        \
80         (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIORING_BUFFER,GstAudioRingBuffer))
81 #define GST_AUDIORING_BUFFER_CLASS(klass) \
82         (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIORING_BUFFER,GstAudioRingBufferClass))
83 #define GST_AUDIORING_BUFFER_GET_CLASS(obj) \
84         (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AUDIORING_BUFFER, GstAudioRingBufferClass))
85 #define GST_AUDIORING_BUFFER_CAST(obj)        \
86         ((GstAudioRingBuffer *)obj)
87 #define GST_IS_AUDIORING_BUFFER(obj)     \
88         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIORING_BUFFER))
89 #define GST_IS_AUDIORING_BUFFER_CLASS(klass)\
90         (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIORING_BUFFER))
91
92 typedef struct _GstAudioRingBuffer GstAudioRingBuffer;
93 typedef struct _GstAudioRingBufferClass GstAudioRingBufferClass;
94
95 #define GST_AUDIORING_BUFFER_GET_COND(buf) (((GstAudioRingBuffer *)buf)->cond)
96 #define GST_AUDIORING_BUFFER_WAIT(buf)     (g_cond_wait (GST_AUDIORING_BUFFER_GET_COND (buf), GST_OBJECT_GET_LOCK (buf)))
97 #define GST_AUDIORING_BUFFER_SIGNAL(buf)   (g_cond_signal (GST_AUDIORING_BUFFER_GET_COND (buf)))
98 #define GST_AUDIORING_BUFFER_BROADCAST(buf)(g_cond_broadcast (GST_AUDIORING_BUFFER_GET_COND (buf)))
99
100 struct _GstAudioRingBuffer
101 {
102   GstRingBuffer object;
103
104   gboolean running;
105   gint queuedseg;
106
107   GCond *cond;
108 };
109
110 struct _GstAudioRingBufferClass
111 {
112   GstRingBufferClass parent_class;
113 };
114
115 static void gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass);
116 static void gst_audioringbuffer_init (GstAudioRingBuffer * ringbuffer,
117     GstAudioRingBufferClass * klass);
118 static void gst_audioringbuffer_dispose (GObject * object);
119 static void gst_audioringbuffer_finalize (GObject * object);
120
121 static GstRingBufferClass *ring_parent_class = NULL;
122
123 static gboolean gst_audioringbuffer_open_device (GstRingBuffer * buf);
124 static gboolean gst_audioringbuffer_close_device (GstRingBuffer * buf);
125 static gboolean gst_audioringbuffer_acquire (GstRingBuffer * buf,
126     GstRingBufferSpec * spec);
127 static gboolean gst_audioringbuffer_release (GstRingBuffer * buf);
128 static gboolean gst_audioringbuffer_start (GstRingBuffer * buf);
129 static gboolean gst_audioringbuffer_pause (GstRingBuffer * buf);
130 static gboolean gst_audioringbuffer_stop (GstRingBuffer * buf);
131 static guint gst_audioringbuffer_delay (GstRingBuffer * buf);
132 static gboolean gst_audioringbuffer_activate (GstRingBuffer * buf,
133     gboolean active);
134
135 /* ringbuffer abstract base class */
136 static GType
137 gst_audioringbuffer_get_type (void)
138 {
139   static GType ringbuffer_type = 0;
140
141   if (!ringbuffer_type) {
142     static const GTypeInfo ringbuffer_info = {
143       sizeof (GstAudioRingBufferClass),
144       NULL,
145       NULL,
146       (GClassInitFunc) gst_audioringbuffer_class_init,
147       NULL,
148       NULL,
149       sizeof (GstAudioRingBuffer),
150       0,
151       (GInstanceInitFunc) gst_audioringbuffer_init,
152       NULL
153     };
154
155     ringbuffer_type =
156         g_type_register_static (GST_TYPE_RING_BUFFER, "GstAudioSinkRingBuffer",
157         &ringbuffer_info, 0);
158   }
159   return ringbuffer_type;
160 }
161
162 static void
163 gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass)
164 {
165   GObjectClass *gobject_class;
166   GstRingBufferClass *gstringbuffer_class;
167
168   gobject_class = (GObjectClass *) klass;
169   gstringbuffer_class = (GstRingBufferClass *) klass;
170
171   ring_parent_class = g_type_class_peek_parent (klass);
172
173   gobject_class->dispose = gst_audioringbuffer_dispose;
174   gobject_class->finalize = gst_audioringbuffer_finalize;
175
176   gstringbuffer_class->open_device =
177       GST_DEBUG_FUNCPTR (gst_audioringbuffer_open_device);
178   gstringbuffer_class->close_device =
179       GST_DEBUG_FUNCPTR (gst_audioringbuffer_close_device);
180   gstringbuffer_class->acquire =
181       GST_DEBUG_FUNCPTR (gst_audioringbuffer_acquire);
182   gstringbuffer_class->release =
183       GST_DEBUG_FUNCPTR (gst_audioringbuffer_release);
184   gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_audioringbuffer_start);
185   gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_audioringbuffer_pause);
186   gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_audioringbuffer_start);
187   gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_audioringbuffer_stop);
188
189   gstringbuffer_class->delay = GST_DEBUG_FUNCPTR (gst_audioringbuffer_delay);
190   gstringbuffer_class->activate =
191       GST_DEBUG_FUNCPTR (gst_audioringbuffer_activate);
192 }
193
194 typedef guint (*WriteFunc) (GstAudioSink * sink, gpointer data, guint length);
195
196 /* this internal thread does nothing else but write samples to the audio device.
197  * It will write each segment in the ringbuffer and will update the play
198  * pointer. 
199  * The start/stop methods control the thread.
200  */
201 static void
202 audioringbuffer_thread_func (GstRingBuffer * buf)
203 {
204   GstAudioSink *sink;
205   GstAudioSinkClass *csink;
206   GstAudioRingBuffer *abuf = GST_AUDIORING_BUFFER_CAST (buf);
207   WriteFunc writefunc;
208   GstMessage *message;
209   GValue val = { 0 };
210
211   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
212   csink = GST_AUDIO_SINK_GET_CLASS (sink);
213
214   GST_DEBUG_OBJECT (sink, "enter thread");
215
216   GST_OBJECT_LOCK (abuf);
217   GST_DEBUG_OBJECT (sink, "signal wait");
218   GST_AUDIORING_BUFFER_SIGNAL (buf);
219   GST_OBJECT_UNLOCK (abuf);
220
221   writefunc = csink->write;
222   if (writefunc == NULL)
223     goto no_function;
224
225   g_value_init (&val, G_TYPE_POINTER);
226   g_value_set_pointer (&val, sink->thread);
227   message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
228       GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (sink));
229   gst_message_set_stream_status_object (message, &val);
230   GST_DEBUG_OBJECT (sink, "posting ENTER stream status");
231   gst_element_post_message (GST_ELEMENT_CAST (sink), message);
232
233   while (TRUE) {
234     gint left, len;
235     guint8 *readptr;
236     gint readseg;
237
238     /* buffer must be started */
239     if (gst_ring_buffer_prepare_read (buf, &readseg, &readptr, &len)) {
240       gint written;
241
242       left = len;
243       do {
244         written = writefunc (sink, readptr, left);
245         GST_LOG_OBJECT (sink, "transfered %d bytes of %d from segment %d",
246             written, left, readseg);
247         if (written < 0 || written > left) {
248           /* might not be critical, it e.g. happens when aborting playback */
249           GST_WARNING_OBJECT (sink,
250               "error writing data in %s (reason: %s), skipping segment (left: %d, written: %d)",
251               GST_DEBUG_FUNCPTR_NAME (writefunc),
252               (errno > 1 ? g_strerror (errno) : "unknown"), left, written);
253           break;
254         }
255         left -= written;
256         readptr += written;
257       } while (left > 0);
258
259       /* clear written samples */
260       gst_ring_buffer_clear (buf, readseg);
261
262       /* we wrote one segment */
263       gst_ring_buffer_advance (buf, 1);
264     } else {
265       GST_OBJECT_LOCK (abuf);
266       if (!abuf->running)
267         goto stop_running;
268       GST_DEBUG_OBJECT (sink, "signal wait");
269       GST_AUDIORING_BUFFER_SIGNAL (buf);
270       GST_DEBUG_OBJECT (sink, "wait for action");
271       GST_AUDIORING_BUFFER_WAIT (buf);
272       GST_DEBUG_OBJECT (sink, "got signal");
273       if (!abuf->running)
274         goto stop_running;
275       GST_DEBUG_OBJECT (sink, "continue running");
276       GST_OBJECT_UNLOCK (abuf);
277     }
278   }
279
280   /* Will never be reached */
281   g_assert_not_reached ();
282   return;
283
284   /* ERROR */
285 no_function:
286   {
287     GST_DEBUG_OBJECT (sink, "no write function, exit thread");
288     return;
289   }
290 stop_running:
291   {
292     GST_OBJECT_UNLOCK (abuf);
293     GST_DEBUG_OBJECT (sink, "stop running, exit thread");
294     message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
295         GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT_CAST (sink));
296     gst_message_set_stream_status_object (message, &val);
297     GST_DEBUG_OBJECT (sink, "posting LEAVE stream status");
298     gst_element_post_message (GST_ELEMENT_CAST (sink), message);
299     return;
300   }
301 }
302
303 static void
304 gst_audioringbuffer_init (GstAudioRingBuffer * ringbuffer,
305     GstAudioRingBufferClass * g_class)
306 {
307   ringbuffer->running = FALSE;
308   ringbuffer->queuedseg = 0;
309
310   ringbuffer->cond = g_cond_new ();
311 }
312
313 static void
314 gst_audioringbuffer_dispose (GObject * object)
315 {
316   G_OBJECT_CLASS (ring_parent_class)->dispose (object);
317 }
318
319 static void
320 gst_audioringbuffer_finalize (GObject * object)
321 {
322   GstAudioRingBuffer *ringbuffer = GST_AUDIORING_BUFFER_CAST (object);
323
324   g_cond_free (ringbuffer->cond);
325
326   G_OBJECT_CLASS (ring_parent_class)->finalize (object);
327 }
328
329 static gboolean
330 gst_audioringbuffer_open_device (GstRingBuffer * buf)
331 {
332   GstAudioSink *sink;
333   GstAudioSinkClass *csink;
334   gboolean result = TRUE;
335
336   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
337   csink = GST_AUDIO_SINK_GET_CLASS (sink);
338
339   if (csink->open)
340     result = csink->open (sink);
341
342   if (!result)
343     goto could_not_open;
344
345   return result;
346
347 could_not_open:
348   {
349     GST_DEBUG_OBJECT (sink, "could not open device");
350     return FALSE;
351   }
352 }
353
354 static gboolean
355 gst_audioringbuffer_close_device (GstRingBuffer * buf)
356 {
357   GstAudioSink *sink;
358   GstAudioSinkClass *csink;
359   gboolean result = TRUE;
360
361   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
362   csink = GST_AUDIO_SINK_GET_CLASS (sink);
363
364   if (csink->close)
365     result = csink->close (sink);
366
367   if (!result)
368     goto could_not_close;
369
370   return result;
371
372 could_not_close:
373   {
374     GST_DEBUG_OBJECT (sink, "could not close device");
375     return FALSE;
376   }
377 }
378
379 static gboolean
380 gst_audioringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
381 {
382   GstAudioSink *sink;
383   GstAudioSinkClass *csink;
384   gboolean result = FALSE;
385
386   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
387   csink = GST_AUDIO_SINK_GET_CLASS (sink);
388
389   if (csink->prepare)
390     result = csink->prepare (sink, spec);
391   if (!result)
392     goto could_not_prepare;
393
394   /* set latency to one more segment as we need some headroom */
395   spec->seglatency = spec->segtotal + 1;
396
397   buf->data = gst_buffer_new_and_alloc (spec->segtotal * spec->segsize);
398   memset (GST_BUFFER_DATA (buf->data), 0, GST_BUFFER_SIZE (buf->data));
399
400   return TRUE;
401
402   /* ERRORS */
403 could_not_prepare:
404   {
405     GST_DEBUG_OBJECT (sink, "could not prepare device");
406     return FALSE;
407   }
408 }
409
410 static gboolean
411 gst_audioringbuffer_activate (GstRingBuffer * buf, gboolean active)
412 {
413   GstAudioSink *sink;
414   GstAudioRingBuffer *abuf;
415   GError *error = NULL;
416
417   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
418   abuf = GST_AUDIORING_BUFFER_CAST (buf);
419
420   if (active) {
421     abuf->running = TRUE;
422
423     GST_DEBUG_OBJECT (sink, "starting thread");
424     sink->thread =
425         g_thread_create ((GThreadFunc) audioringbuffer_thread_func, buf, TRUE,
426         &error);
427     if (!sink->thread || error != NULL)
428       goto thread_failed;
429
430     GST_DEBUG_OBJECT (sink, "waiting for thread");
431     /* the object lock is taken */
432     GST_AUDIORING_BUFFER_WAIT (buf);
433     GST_DEBUG_OBJECT (sink, "thread is started");
434   } else {
435     abuf->running = FALSE;
436     GST_DEBUG_OBJECT (sink, "signal wait");
437     GST_AUDIORING_BUFFER_SIGNAL (buf);
438
439     GST_OBJECT_UNLOCK (buf);
440
441     /* join the thread */
442     g_thread_join (sink->thread);
443
444     GST_OBJECT_LOCK (buf);
445   }
446   return TRUE;
447
448   /* ERRORS */
449 thread_failed:
450   {
451     if (error)
452       GST_ERROR_OBJECT (sink, "could not create thread %s", error->message);
453     else
454       GST_ERROR_OBJECT (sink, "could not create thread for unknown reason");
455     return FALSE;
456   }
457 }
458
459 /* function is called with LOCK */
460 static gboolean
461 gst_audioringbuffer_release (GstRingBuffer * buf)
462 {
463   GstAudioSink *sink;
464   GstAudioSinkClass *csink;
465   gboolean result = FALSE;
466
467   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
468   csink = GST_AUDIO_SINK_GET_CLASS (sink);
469
470   /* free the buffer */
471   gst_buffer_unref (buf->data);
472   buf->data = NULL;
473
474   if (csink->unprepare)
475     result = csink->unprepare (sink);
476
477   if (!result)
478     goto could_not_unprepare;
479
480   GST_DEBUG_OBJECT (sink, "unprepared");
481
482   return result;
483
484 could_not_unprepare:
485   {
486     GST_DEBUG_OBJECT (sink, "could not unprepare device");
487     return FALSE;
488   }
489 }
490
491 static gboolean
492 gst_audioringbuffer_start (GstRingBuffer * buf)
493 {
494   GstAudioSink *sink;
495
496   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
497
498   GST_DEBUG_OBJECT (sink, "start, sending signal");
499   GST_AUDIORING_BUFFER_SIGNAL (buf);
500
501   return TRUE;
502 }
503
504 static gboolean
505 gst_audioringbuffer_pause (GstRingBuffer * buf)
506 {
507   GstAudioSink *sink;
508   GstAudioSinkClass *csink;
509
510   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
511   csink = GST_AUDIO_SINK_GET_CLASS (sink);
512
513   /* unblock any pending writes to the audio device */
514   if (csink->reset) {
515     GST_DEBUG_OBJECT (sink, "reset...");
516     csink->reset (sink);
517     GST_DEBUG_OBJECT (sink, "reset done");
518   }
519
520   return TRUE;
521 }
522
523 static gboolean
524 gst_audioringbuffer_stop (GstRingBuffer * buf)
525 {
526   GstAudioSink *sink;
527   GstAudioSinkClass *csink;
528
529   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
530   csink = GST_AUDIO_SINK_GET_CLASS (sink);
531
532   /* unblock any pending writes to the audio device */
533   if (csink->reset) {
534     GST_DEBUG_OBJECT (sink, "reset...");
535     csink->reset (sink);
536     GST_DEBUG_OBJECT (sink, "reset done");
537   }
538 #if 0
539   if (abuf->running) {
540     GST_DEBUG_OBJECT (sink, "stop, waiting...");
541     GST_AUDIORING_BUFFER_WAIT (buf);
542     GST_DEBUG_OBJECT (sink, "stopped");
543   }
544 #endif
545
546   return TRUE;
547 }
548
549 static guint
550 gst_audioringbuffer_delay (GstRingBuffer * buf)
551 {
552   GstAudioSink *sink;
553   GstAudioSinkClass *csink;
554   guint res = 0;
555
556   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
557   csink = GST_AUDIO_SINK_GET_CLASS (sink);
558
559   if (csink->delay)
560     res = csink->delay (sink);
561
562   return res;
563 }
564
565 /* AudioSink signals and args */
566 enum
567 {
568   /* FILL ME */
569   LAST_SIGNAL
570 };
571
572 enum
573 {
574   ARG_0,
575 };
576
577 #define _do_init(bla) \
578     GST_DEBUG_CATEGORY_INIT (gst_audio_sink_debug, "audiosink", 0, "audiosink element");
579
580 GST_BOILERPLATE_FULL (GstAudioSink, gst_audio_sink, GstBaseAudioSink,
581     GST_TYPE_BASE_AUDIO_SINK, _do_init);
582
583 static GstRingBuffer *gst_audio_sink_create_ringbuffer (GstBaseAudioSink *
584     sink);
585
586 static void
587 gst_audio_sink_base_init (gpointer g_class)
588 {
589 }
590
591 static void
592 gst_audio_sink_class_init (GstAudioSinkClass * klass)
593 {
594   GstBaseAudioSinkClass *gstbaseaudiosink_class;
595
596   gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
597
598   gstbaseaudiosink_class->create_ringbuffer =
599       GST_DEBUG_FUNCPTR (gst_audio_sink_create_ringbuffer);
600
601   g_type_class_ref (GST_TYPE_AUDIORING_BUFFER);
602 }
603
604 static void
605 gst_audio_sink_init (GstAudioSink * audiosink, GstAudioSinkClass * g_class)
606 {
607 }
608
609 static GstRingBuffer *
610 gst_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
611 {
612   GstRingBuffer *buffer;
613
614   GST_DEBUG_OBJECT (sink, "creating ringbuffer");
615   buffer = g_object_new (GST_TYPE_AUDIORING_BUFFER, NULL);
616   GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
617
618   return buffer;
619 }