2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
5 * gstaudiosink.c: simple audio sink base class
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.
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.
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.
24 * SECTION:gstaudiosink
25 * @short_description: Simple base class for audio sinks
26 * @see_also: #GstBaseAudioSink, #GstRingBuffer, #GstAudioSink.
28 * This is the most simple base class for audio sinks that only requires
29 * subclasses to implement a set of simple functions:
34 * <listitem><para>Open the device.</para></listitem>
37 * <term>prepare()</term>
38 * <listitem><para>Configure the device with the specified format.</para></listitem>
41 * <term>write()</term>
42 * <listitem><para>Write samples to the device.</para></listitem>
45 * <term>reset()</term>
46 * <listitem><para>Unblock writes and flush the device.</para></listitem>
49 * <term>delay()</term>
50 * <listitem><para>Get the number of samples written but not yet played
51 * by the device.</para></listitem>
54 * <term>unprepare()</term>
55 * <listitem><para>Undo operations done by prepare.</para></listitem>
58 * <term>close()</term>
59 * <listitem><para>Close the device.</para></listitem>
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.
67 * Last reviewed on 2006-09-27 (0.10.12)
72 #include "gstaudiosink.h"
74 #include "gst/glib-compat-private.h"
76 GST_DEBUG_CATEGORY_STATIC (gst_audio_sink_debug);
77 #define GST_CAT_DEFAULT gst_audio_sink_debug
79 #define GST_TYPE_AUDIORING_BUFFER \
80 (gst_audioringbuffer_get_type())
81 #define GST_AUDIORING_BUFFER(obj) \
82 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIORING_BUFFER,GstAudioRingBuffer))
83 #define GST_AUDIORING_BUFFER_CLASS(klass) \
84 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIORING_BUFFER,GstAudioRingBufferClass))
85 #define GST_AUDIORING_BUFFER_GET_CLASS(obj) \
86 (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AUDIORING_BUFFER, GstAudioRingBufferClass))
87 #define GST_AUDIORING_BUFFER_CAST(obj) \
88 ((GstAudioRingBuffer *)obj)
89 #define GST_IS_AUDIORING_BUFFER(obj) \
90 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIORING_BUFFER))
91 #define GST_IS_AUDIORING_BUFFER_CLASS(klass)\
92 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIORING_BUFFER))
94 typedef struct _GstAudioRingBuffer GstAudioRingBuffer;
95 typedef struct _GstAudioRingBufferClass GstAudioRingBufferClass;
97 #define GST_AUDIORING_BUFFER_GET_COND(buf) (((GstAudioRingBuffer *)buf)->cond)
98 #define GST_AUDIORING_BUFFER_WAIT(buf) (g_cond_wait (GST_AUDIORING_BUFFER_GET_COND (buf), GST_OBJECT_GET_LOCK (buf)))
99 #define GST_AUDIORING_BUFFER_SIGNAL(buf) (g_cond_signal (GST_AUDIORING_BUFFER_GET_COND (buf)))
100 #define GST_AUDIORING_BUFFER_BROADCAST(buf)(g_cond_broadcast (GST_AUDIORING_BUFFER_GET_COND (buf)))
102 struct _GstAudioRingBuffer
104 GstRingBuffer object;
112 struct _GstAudioRingBufferClass
114 GstRingBufferClass parent_class;
117 static void gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass);
118 static void gst_audioringbuffer_init (GstAudioRingBuffer * ringbuffer,
119 GstAudioRingBufferClass * klass);
120 static void gst_audioringbuffer_dispose (GObject * object);
121 static void gst_audioringbuffer_finalize (GObject * object);
123 static GstRingBufferClass *ring_parent_class = NULL;
125 static gboolean gst_audioringbuffer_open_device (GstRingBuffer * buf);
126 static gboolean gst_audioringbuffer_close_device (GstRingBuffer * buf);
127 static gboolean gst_audioringbuffer_acquire (GstRingBuffer * buf,
128 GstRingBufferSpec * spec);
129 static gboolean gst_audioringbuffer_release (GstRingBuffer * buf);
130 static gboolean gst_audioringbuffer_start (GstRingBuffer * buf);
131 static gboolean gst_audioringbuffer_pause (GstRingBuffer * buf);
132 static gboolean gst_audioringbuffer_stop (GstRingBuffer * buf);
133 static guint gst_audioringbuffer_delay (GstRingBuffer * buf);
134 static gboolean gst_audioringbuffer_activate (GstRingBuffer * buf,
137 /* ringbuffer abstract base class */
139 gst_audioringbuffer_get_type (void)
141 static GType ringbuffer_type = 0;
143 if (!ringbuffer_type) {
144 static const GTypeInfo ringbuffer_info = {
145 sizeof (GstAudioRingBufferClass),
148 (GClassInitFunc) gst_audioringbuffer_class_init,
151 sizeof (GstAudioRingBuffer),
153 (GInstanceInitFunc) gst_audioringbuffer_init,
158 g_type_register_static (GST_TYPE_RING_BUFFER, "GstAudioSinkRingBuffer",
159 &ringbuffer_info, 0);
161 return ringbuffer_type;
165 gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass)
167 GObjectClass *gobject_class;
168 GstRingBufferClass *gstringbuffer_class;
170 gobject_class = (GObjectClass *) klass;
171 gstringbuffer_class = (GstRingBufferClass *) klass;
173 ring_parent_class = g_type_class_peek_parent (klass);
175 gobject_class->dispose = gst_audioringbuffer_dispose;
176 gobject_class->finalize = gst_audioringbuffer_finalize;
178 gstringbuffer_class->open_device =
179 GST_DEBUG_FUNCPTR (gst_audioringbuffer_open_device);
180 gstringbuffer_class->close_device =
181 GST_DEBUG_FUNCPTR (gst_audioringbuffer_close_device);
182 gstringbuffer_class->acquire =
183 GST_DEBUG_FUNCPTR (gst_audioringbuffer_acquire);
184 gstringbuffer_class->release =
185 GST_DEBUG_FUNCPTR (gst_audioringbuffer_release);
186 gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_audioringbuffer_start);
187 gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_audioringbuffer_pause);
188 gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_audioringbuffer_start);
189 gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_audioringbuffer_stop);
191 gstringbuffer_class->delay = GST_DEBUG_FUNCPTR (gst_audioringbuffer_delay);
192 gstringbuffer_class->activate =
193 GST_DEBUG_FUNCPTR (gst_audioringbuffer_activate);
196 typedef guint (*WriteFunc) (GstAudioSink * sink, gpointer data, guint length);
198 /* this internal thread does nothing else but write samples to the audio device.
199 * It will write each segment in the ringbuffer and will update the play
201 * The start/stop methods control the thread.
204 audioringbuffer_thread_func (GstRingBuffer * buf)
207 GstAudioSinkClass *csink;
208 GstAudioRingBuffer *abuf = GST_AUDIORING_BUFFER_CAST (buf);
213 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
214 csink = GST_AUDIO_SINK_GET_CLASS (sink);
216 GST_DEBUG_OBJECT (sink, "enter thread");
218 GST_OBJECT_LOCK (abuf);
219 GST_DEBUG_OBJECT (sink, "signal wait");
220 GST_AUDIORING_BUFFER_SIGNAL (buf);
221 GST_OBJECT_UNLOCK (abuf);
223 writefunc = csink->write;
224 if (writefunc == NULL)
227 g_value_init (&val, G_TYPE_POINTER);
228 g_value_set_pointer (&val, sink->thread);
229 message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
230 GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (sink));
231 gst_message_set_stream_status_object (message, &val);
232 GST_DEBUG_OBJECT (sink, "posting ENTER stream status");
233 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
240 /* buffer must be started */
241 if (gst_ring_buffer_prepare_read (buf, &readseg, &readptr, &len)) {
246 written = writefunc (sink, readptr, left);
247 GST_LOG_OBJECT (sink, "transfered %d bytes of %d from segment %d",
248 written, left, readseg);
249 if (written < 0 || written > left) {
250 /* might not be critical, it e.g. happens when aborting playback */
251 GST_WARNING_OBJECT (sink,
252 "error writing data in %s (reason: %s), skipping segment (left: %d, written: %d)",
253 GST_DEBUG_FUNCPTR_NAME (writefunc),
254 (errno > 1 ? g_strerror (errno) : "unknown"), left, written);
261 /* clear written samples */
262 gst_ring_buffer_clear (buf, readseg);
264 /* we wrote one segment */
265 gst_ring_buffer_advance (buf, 1);
267 GST_OBJECT_LOCK (abuf);
270 if (G_UNLIKELY (g_atomic_int_get (&buf->state) ==
271 GST_RING_BUFFER_STATE_STARTED)) {
272 GST_OBJECT_UNLOCK (abuf);
275 GST_DEBUG_OBJECT (sink, "signal wait");
276 GST_AUDIORING_BUFFER_SIGNAL (buf);
277 GST_DEBUG_OBJECT (sink, "wait for action");
278 GST_AUDIORING_BUFFER_WAIT (buf);
279 GST_DEBUG_OBJECT (sink, "got signal");
282 GST_DEBUG_OBJECT (sink, "continue running");
283 GST_OBJECT_UNLOCK (abuf);
287 /* Will never be reached */
288 g_assert_not_reached ();
294 GST_DEBUG_OBJECT (sink, "no write function, exit thread");
299 GST_OBJECT_UNLOCK (abuf);
300 GST_DEBUG_OBJECT (sink, "stop running, exit thread");
301 message = gst_message_new_stream_status (GST_OBJECT_CAST (buf),
302 GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT_CAST (sink));
303 gst_message_set_stream_status_object (message, &val);
304 GST_DEBUG_OBJECT (sink, "posting LEAVE stream status");
305 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
311 gst_audioringbuffer_init (GstAudioRingBuffer * ringbuffer,
312 GstAudioRingBufferClass * g_class)
314 ringbuffer->running = FALSE;
315 ringbuffer->queuedseg = 0;
317 ringbuffer->cond = g_cond_new ();
321 gst_audioringbuffer_dispose (GObject * object)
323 G_OBJECT_CLASS (ring_parent_class)->dispose (object);
327 gst_audioringbuffer_finalize (GObject * object)
329 GstAudioRingBuffer *ringbuffer = GST_AUDIORING_BUFFER_CAST (object);
331 g_cond_free (ringbuffer->cond);
333 G_OBJECT_CLASS (ring_parent_class)->finalize (object);
337 gst_audioringbuffer_open_device (GstRingBuffer * buf)
340 GstAudioSinkClass *csink;
341 gboolean result = TRUE;
343 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
344 csink = GST_AUDIO_SINK_GET_CLASS (sink);
347 result = csink->open (sink);
356 GST_DEBUG_OBJECT (sink, "could not open device");
362 gst_audioringbuffer_close_device (GstRingBuffer * buf)
365 GstAudioSinkClass *csink;
366 gboolean result = TRUE;
368 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
369 csink = GST_AUDIO_SINK_GET_CLASS (sink);
372 result = csink->close (sink);
375 goto could_not_close;
381 GST_DEBUG_OBJECT (sink, "could not close device");
387 gst_audioringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
390 GstAudioSinkClass *csink;
391 gboolean result = FALSE;
393 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
394 csink = GST_AUDIO_SINK_GET_CLASS (sink);
397 result = csink->prepare (sink, spec);
399 goto could_not_prepare;
401 /* set latency to one more segment as we need some headroom */
402 spec->seglatency = spec->segtotal + 1;
404 buf->data = gst_buffer_new_and_alloc (spec->segtotal * spec->segsize);
405 memset (GST_BUFFER_DATA (buf->data), 0, GST_BUFFER_SIZE (buf->data));
412 GST_DEBUG_OBJECT (sink, "could not prepare device");
418 gst_audioringbuffer_activate (GstRingBuffer * buf, gboolean active)
421 GstAudioRingBuffer *abuf;
422 GError *error = NULL;
424 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
425 abuf = GST_AUDIORING_BUFFER_CAST (buf);
428 abuf->running = TRUE;
430 GST_DEBUG_OBJECT (sink, "starting thread");
432 #if !GLIB_CHECK_VERSION (2, 31, 0)
434 g_thread_create ((GThreadFunc) audioringbuffer_thread_func, buf, TRUE,
437 sink->thread = g_thread_try_new ("audiosink-ringbuffer",
438 (GThreadFunc) audioringbuffer_thread_func, buf, &error);
441 if (!sink->thread || error != NULL)
444 GST_DEBUG_OBJECT (sink, "waiting for thread");
445 /* the object lock is taken */
446 GST_AUDIORING_BUFFER_WAIT (buf);
447 GST_DEBUG_OBJECT (sink, "thread is started");
449 abuf->running = FALSE;
450 GST_DEBUG_OBJECT (sink, "signal wait");
451 GST_AUDIORING_BUFFER_SIGNAL (buf);
453 GST_OBJECT_UNLOCK (buf);
455 /* join the thread */
456 g_thread_join (sink->thread);
458 GST_OBJECT_LOCK (buf);
466 GST_ERROR_OBJECT (sink, "could not create thread %s", error->message);
468 GST_ERROR_OBJECT (sink, "could not create thread for unknown reason");
473 /* function is called with LOCK */
475 gst_audioringbuffer_release (GstRingBuffer * buf)
478 GstAudioSinkClass *csink;
479 gboolean result = FALSE;
481 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
482 csink = GST_AUDIO_SINK_GET_CLASS (sink);
484 /* free the buffer */
485 gst_buffer_unref (buf->data);
488 if (csink->unprepare)
489 result = csink->unprepare (sink);
492 goto could_not_unprepare;
494 GST_DEBUG_OBJECT (sink, "unprepared");
500 GST_DEBUG_OBJECT (sink, "could not unprepare device");
506 gst_audioringbuffer_start (GstRingBuffer * buf)
510 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
512 GST_DEBUG_OBJECT (sink, "start, sending signal");
513 GST_AUDIORING_BUFFER_SIGNAL (buf);
519 gst_audioringbuffer_pause (GstRingBuffer * buf)
522 GstAudioSinkClass *csink;
524 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
525 csink = GST_AUDIO_SINK_GET_CLASS (sink);
527 /* unblock any pending writes to the audio device */
529 GST_DEBUG_OBJECT (sink, "reset...");
531 GST_DEBUG_OBJECT (sink, "reset done");
538 gst_audioringbuffer_stop (GstRingBuffer * buf)
541 GstAudioSinkClass *csink;
543 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
544 csink = GST_AUDIO_SINK_GET_CLASS (sink);
546 /* unblock any pending writes to the audio device */
548 GST_DEBUG_OBJECT (sink, "reset...");
550 GST_DEBUG_OBJECT (sink, "reset done");
554 GST_DEBUG_OBJECT (sink, "stop, waiting...");
555 GST_AUDIORING_BUFFER_WAIT (buf);
556 GST_DEBUG_OBJECT (sink, "stopped");
564 gst_audioringbuffer_delay (GstRingBuffer * buf)
567 GstAudioSinkClass *csink;
570 sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
571 csink = GST_AUDIO_SINK_GET_CLASS (sink);
574 res = csink->delay (sink);
579 /* AudioSink signals and args */
591 #define _do_init(bla) \
592 GST_DEBUG_CATEGORY_INIT (gst_audio_sink_debug, "audiosink", 0, "audiosink element");
594 GST_BOILERPLATE_FULL (GstAudioSink, gst_audio_sink, GstBaseAudioSink,
595 GST_TYPE_BASE_AUDIO_SINK, _do_init);
597 static GstRingBuffer *gst_audio_sink_create_ringbuffer (GstBaseAudioSink *
601 gst_audio_sink_base_init (gpointer g_class)
606 gst_audio_sink_class_init (GstAudioSinkClass * klass)
608 GstBaseAudioSinkClass *gstbaseaudiosink_class;
610 gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
612 gstbaseaudiosink_class->create_ringbuffer =
613 GST_DEBUG_FUNCPTR (gst_audio_sink_create_ringbuffer);
615 g_type_class_ref (GST_TYPE_AUDIORING_BUFFER);
619 gst_audio_sink_init (GstAudioSink * audiosink, GstAudioSinkClass * g_class)
623 static GstRingBuffer *
624 gst_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
626 GstRingBuffer *buffer;
628 GST_DEBUG_OBJECT (sink, "creating ringbuffer");
629 buffer = g_object_new (GST_TYPE_AUDIORING_BUFFER, NULL);
630 GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);