Commit 5a3941c7 authored by Wim Taymans's avatar Wim Taymans
Browse files

An attempt at a set of audio base classes together with some design docs.

Original commit message from CVS:
* docs/design-audiosinks.txt:
* gst-libs/gst/audio/Makefile.am:
* gst-libs/gst/audio/TODO:
* gst-libs/gst/audio/gstaudiosink.c:
(gst_audioringbuffer_get_type), (gst_audioringbuffer_class_init),
(audioringbuffer_thread_func), (gst_audioringbuffer_init),
(gst_audioringbuffer_dispose), (gst_audioringbuffer_finalize),
(gst_audioringbuffer_acquire), (gst_audioringbuffer_release),
(gst_audioringbuffer_play), (gst_audioringbuffer_stop),
(gst_audioringbuffer_delay), (gst_audiosink_base_init),
(gst_audiosink_class_init), (gst_audiosink_init),
(gst_audiosink_create_ringbuffer):
* gst-libs/gst/audio/gstaudiosink.h:
* gst-libs/gst/audio/gstbaseaudiosink.c:
(gst_baseaudiosink_base_init), (gst_baseaudiosink_class_init),
(gst_baseaudiosink_init), (gst_baseaudiosink_set_property),
(gst_baseaudiosink_get_property), (gst_baseaudiosink_setcaps),
(gst_baseaudiosink_get_times), (gst_baseaudiosink_event),
(gst_baseaudiosink_preroll), (gst_baseaudiosink_render),
(gst_baseaudiosink_create_ringbuffer),
(gst_baseaudiosink_callback), (gst_baseaudiosink_change_state):
* gst-libs/gst/audio/gstbaseaudiosink.h:
* gst-libs/gst/audio/gstringbuffer.c: (gst_ringbuffer_get_type),
(gst_ringbuffer_class_init), (gst_ringbuffer_init),
(gst_ringbuffer_dispose), (gst_ringbuffer_finalize),
(gst_ringbuffer_set_callback), (gst_ringbuffer_acquire),
(gst_ringbuffer_release), (gst_ringbuffer_play_unlocked),
(gst_ringbuffer_play), (gst_ringbuffer_pause),
(gst_ringbuffer_resume), (gst_ringbuffer_stop),
(gst_ringbuffer_callback), (gst_ringbuffer_delay),
(gst_ringbuffer_played_samples), (gst_ringbuffer_commit),
(gst_ringbuffer_prepare_read), (gst_ringbuffer_clear):
* gst-libs/gst/audio/gstringbuffer.h:
An attempt at a set of audio base classes together with some
design docs.
parent 468d6d43
2005-04-20 Wim Taymans <wim@fluendo.com>
* docs/design-audiosinks.txt:
* gst-libs/gst/audio/Makefile.am:
* gst-libs/gst/audio/TODO:
* gst-libs/gst/audio/gstaudiosink.c:
(gst_audioringbuffer_get_type), (gst_audioringbuffer_class_init),
(audioringbuffer_thread_func), (gst_audioringbuffer_init),
(gst_audioringbuffer_dispose), (gst_audioringbuffer_finalize),
(gst_audioringbuffer_acquire), (gst_audioringbuffer_release),
(gst_audioringbuffer_play), (gst_audioringbuffer_stop),
(gst_audioringbuffer_delay), (gst_audiosink_base_init),
(gst_audiosink_class_init), (gst_audiosink_init),
(gst_audiosink_create_ringbuffer):
* gst-libs/gst/audio/gstaudiosink.h:
* gst-libs/gst/audio/gstbaseaudiosink.c:
(gst_baseaudiosink_base_init), (gst_baseaudiosink_class_init),
(gst_baseaudiosink_init), (gst_baseaudiosink_set_property),
(gst_baseaudiosink_get_property), (gst_baseaudiosink_setcaps),
(gst_baseaudiosink_get_times), (gst_baseaudiosink_event),
(gst_baseaudiosink_preroll), (gst_baseaudiosink_render),
(gst_baseaudiosink_create_ringbuffer),
(gst_baseaudiosink_callback), (gst_baseaudiosink_change_state):
* gst-libs/gst/audio/gstbaseaudiosink.h:
* gst-libs/gst/audio/gstringbuffer.c: (gst_ringbuffer_get_type),
(gst_ringbuffer_class_init), (gst_ringbuffer_init),
(gst_ringbuffer_dispose), (gst_ringbuffer_finalize),
(gst_ringbuffer_set_callback), (gst_ringbuffer_acquire),
(gst_ringbuffer_release), (gst_ringbuffer_play_unlocked),
(gst_ringbuffer_play), (gst_ringbuffer_pause),
(gst_ringbuffer_resume), (gst_ringbuffer_stop),
(gst_ringbuffer_callback), (gst_ringbuffer_delay),
(gst_ringbuffer_played_samples), (gst_ringbuffer_commit),
(gst_ringbuffer_prepare_read), (gst_ringbuffer_clear):
* gst-libs/gst/audio/gstringbuffer.h:
An attempt at a set of audio base classes together with some
design docs.
2005-04-20 Wim Taymans <wim@fluendo.com>
 
* gst/audioconvert/Makefile.am:
......
Audiosink design
----------------
Requirements:
- must operate chain based.
Most simple playback pipelines will push audio from the decoders
into the audio sink.
- must operate getrange based
Most professional audio applications will operate in a mode where
the audio sink pulls samples from the pipeline. This is typically
done in a callback from the audiosink requesting N samples. The
callback is either scheduled from a thread or from an interrupt
from the audio hardware device.
- Exact sample accurate clocks.
the audiosink must be able to provide a clock that is sample
accurate even if samples are dropped or when discontinuities are
found in the stream.
- Exact timing of playback.
The audiosink must be able to play samples at their exact times.
- use DMA access when possible.
When the hardware can do DMA we should use it. This should also
work over bufferpools to avoid data copying to/from kernel space.
Design:
The design is based on a set of base classes and the concept of a
ringbuffer of samples.
+-----------+ - provide preroll, rendering, timing
+ basesink + - caps nego
+-----+-----+
|
+-----V----------+ - manages ringbuffer
+ baseaudiosink + - manages scheduling (push/pull)
+-----+----------+ - manages clock/query/seek
| - manages scheduling of samples in the ringbuffer
| - manages caps parsing
|
+-----V------+ - default ringbuffer implementation with a GThread
+ audiosink + - subclasses provide open/read/close methods
+------------+
The ringbuffer is a contiguous piece of memory divided into segtotal
pieces of segments. Each segment has segsize bytes.
play position write position
v v
+---+---+---+-------------------------------------+----------+
+ 0 | 1 | 2 | .... | segtotal |
+---+---+---+-------------------------------------+----------+
<--->
segsize bytes = N samples * bytes_per_sample.
The ringbuffer has a play and write position, which is expressed in
segments. The play position is where the device is currently reading
samples and the write position is where new samples can be written
into the buffer.
The latency of the ringbuffer is the distance between the play and
write position. The lowest latency is the size of a segment, thus
smaller segment sizes allow for lower latency.
The ringbuffer can be put to the PLAYING or STOPPED state.
In the STOPPED state no samples are played to the device and the play
pointer does not advance.
In the PLAYING state samples are written to the device and the ringbuffer
should call a configurable callback after each segment is written to the
device. In this state the play pointer is advanced after each segment is
written.
A write operation to the ringbuffer will put new samples in the ringbuffer.
If there is not enough space in the ringbuffer, the write operation will
block. The playback of the buffer never stops, even if the buffer is
empty. When the buffer is empty, silence is played by the device.
The ringbuffer is implemented with lockfree atomic operations, especially
on the reading side so that low-latency operations are possible.
Scheduling:
- chain based mode:
In chain based mode, bytes are written into the ringbuffer. This operation
will eventually block when the ringbuffer is filled.
When no samples arrive in time, the ringbuffer will play silence. Each
buffer that arrives will be placed into the ringbuffer at the correct
times. This means that dropping samples or inserting silence is done
automatically and very accurate and independend of the play pointer.
In this mode, the ringbuffer is usually kept as full as possible. When
using a small buffer (small segsize and segtotal), the latency for audio
to start from the sink to when it is played can be kept low but at least
one context switch has to be made between read and write.
- getrange based mode
In getrange based mode, the baseaudiosink will use the callback function
of the ringbuffer to get a segsize samples from the peer element. These
samples will then be placed in the ringbuffer at the next play position.
It is assumed that the getrange function returns fast enough to fill the
ringbuffer before the play pointer reaches the write pointer.
In this mode, the ringbuffer is usually kept as empty as possible. There
is no context switch needed between the elements that create the samples
and the actual writing of the samples to the device.
DMA mode:
- Elements that can do DMA based access to the audio device have to subclass
from the GstBaseAudioSink class and wrap the DMA ringbuffer in a subclass
of GstRingBuffer.
The ringbuffer subclass should trigger a callback after writing or playing
each sample to the device. This callback can be triggered from a thread or
from a signal from the audio device.
Clocks:
The GstBaseAudioSink class will use the ringbuffer to act as a clock provider.
It can do this by using the play pointer and the delay to calculate the
clock time.
......@@ -17,7 +17,10 @@ CLEANFILES = gstaudiofilterexample.c \
$(BUILT_SOURCES)
libgstaudio_la_SOURCES = audio.c audioclock.c \
multichannel.c
multichannel.c \
gstaudiosink.c \
gstbaseaudiosink.c \
gstringbuffer.c
nodist_libgstaudio_la_SOURCES = $(built_sources)
libgstaudioincludedir = $(includedir)/gstreamer-@GST_MAJORMINOR@/gst/audio
......@@ -25,14 +28,15 @@ libgstaudioinclude_HEADERS = \
audio.h \
audioclock.h \
gstaudiofilter.h \
multichannel.h
nodist_libgstaudioinclude_HEADERS = \
gstaudiosink.h \
gstbaseaudiosink.h \
gstringbuffer.h \
multichannel.h \
multichannel-enumtypes.h
libgstaudio_la_LIBADD =
libgstaudio_la_CFLAGS = $(GST_CFLAGS)
libgstaudio_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libgstaudio_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) $(GST_BASE_LIBS)
libgstaudiofilter_la_SOURCES = gstaudiofilter.c gstaudiofilter.h
libgstaudiofilter_la_CFLAGS = $(GST_CFLAGS)
......@@ -45,4 +49,9 @@ libgstaudiofilterexample_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
gstaudiofilterexample.c: $(srcdir)/make_filter $(srcdir)/gstaudiofiltertemplate.c
$(srcdir)/make_filter AudiofilterExample $(srcdir)/gstaudiofiltertemplate.c
noinst_PROGRAMS = testchannels
testchannels_SOURCES = testchannels.c
testchannels_CFLAGS = $(GST_CFLAGS)
testchannels_LDFLAGS = $(GST_LIBS)
include $(top_srcdir)/common/glib-gen.mak
TODO
----
- audio base classes:
- GstBaseAudioSink
- parse caps into rinbuffer spec, also mase sure surround sound
is parsed correctly.
- implement seek/query/convert
- implement clocks
- implement getrange scheduling
- GstRingBuffer
- copy samples to right position in ringbuffer
/* GStreamer
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
* 2005 Wim Taymans <wim@fluendo.com>
*
* gstaudiosink.c: simple audio sink base class
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include "gstaudiosink.h"
GST_DEBUG_CATEGORY_STATIC (gst_audiosink_debug);
#define GST_CAT_DEFAULT gst_audiosink_debug
#define GST_TYPE_AUDIORINGBUFFER \
(gst_audioringbuffer_get_type())
#define GST_AUDIORINGBUFFER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIORINGBUFFER,GstAudioRingBuffer))
#define GST_AUDIORINGBUFFER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIORINGBUFFER,GstAudioRingBufferClass))
#define GST_AUDIORINGBUFFER_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AUDIORINGBUFFER, GstAudioRingBufferClass))
#define GST_IS_AUDIORINGBUFFER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIORINGBUFFER))
#define GST_IS_AUDIORINGBUFFER_CLASS(obj)\
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIORINGBUFFER))
typedef struct _GstAudioRingBuffer GstAudioRingBuffer;
typedef struct _GstAudioRingBufferClass GstAudioRingBufferClass;
#define GST_AUDIORINGBUFFER_GET_COND(buf) (((GstAudioRingBuffer *)buf)->cond)
#define GST_AUDIORINGBUFFER_WAIT(buf) (g_cond_wait (GST_AUDIORINGBUFFER_GET_COND (buf), GST_GET_LOCK (buf)))
#define GST_AUDIORINGBUFFER_SIGNAL(buf) (g_cond_signal (GST_AUDIORINGBUFFER_GET_COND (buf)))
#define GST_AUDIORINGBUFFER_BROADCAST(buf)(g_cond_broadcast (GST_AUDIORINGBUFFER_GET_COND (buf)))
struct _GstAudioRingBuffer
{
GstRingBuffer object;
gboolean running;
gint queuedseg;
GCond *cond;
};
struct _GstAudioRingBufferClass
{
GstRingBufferClass parent_class;
};
static void gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass);
static void gst_audioringbuffer_init (GstAudioRingBuffer * ringbuffer);
static void gst_audioringbuffer_dispose (GObject * object);
static void gst_audioringbuffer_finalize (GObject * object);
static GstRingBufferClass *ring_parent_class = NULL;
static gboolean gst_audioringbuffer_acquire (GstRingBuffer * buf,
GstRingBufferSpec * spec);
static gboolean gst_audioringbuffer_release (GstRingBuffer * buf);
static gboolean gst_audioringbuffer_play (GstRingBuffer * buf);
static gboolean gst_audioringbuffer_stop (GstRingBuffer * buf);
static guint gst_audioringbuffer_delay (GstRingBuffer * buf);
/* ringbuffer abstract base class */
GType
gst_audioringbuffer_get_type (void)
{
static GType ringbuffer_type = 0;
if (!ringbuffer_type) {
static const GTypeInfo ringbuffer_info = {
sizeof (GstAudioRingBufferClass),
NULL,
NULL,
(GClassInitFunc) gst_audioringbuffer_class_init,
NULL,
NULL,
sizeof (GstAudioRingBuffer),
0,
(GInstanceInitFunc) gst_audioringbuffer_init,
NULL
};
ringbuffer_type =
g_type_register_static (GST_TYPE_RINGBUFFER, "GstAudioRingBuffer",
&ringbuffer_info, 0);
}
return ringbuffer_type;
}
static void
gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass)
{
GObjectClass *gobject_class;
GstObjectClass *gstobject_class;
GstRingBufferClass *gstringbuffer_class;
gobject_class = (GObjectClass *) klass;
gstobject_class = (GstObjectClass *) klass;
gstringbuffer_class = (GstRingBufferClass *) klass;
ring_parent_class = g_type_class_ref (GST_TYPE_RINGBUFFER);
gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_audioringbuffer_dispose);
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_audioringbuffer_finalize);
gstringbuffer_class->acquire =
GST_DEBUG_FUNCPTR (gst_audioringbuffer_acquire);
gstringbuffer_class->release =
GST_DEBUG_FUNCPTR (gst_audioringbuffer_release);
gstringbuffer_class->play = GST_DEBUG_FUNCPTR (gst_audioringbuffer_play);
gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_audioringbuffer_stop);
gstringbuffer_class->delay = GST_DEBUG_FUNCPTR (gst_audioringbuffer_delay);
}
typedef guint (*WriteFunc) (GstAudioSink * sink, gpointer data, guint length);
/* this internal thread does nothing else but write samples to the audio device.
* It will write each segment in the ringbuffer and will update the play
* pointer.
* The play/stop methods control the thread.
*/
static void
audioringbuffer_thread_func (GstRingBuffer * buf)
{
GstAudioSink *sink;
GstAudioSinkClass *csink;
GstAudioRingBuffer *abuf = GST_AUDIORINGBUFFER (buf);
WriteFunc writefunc;
gint segsize, segtotal;
sink = GST_AUDIOSINK (GST_OBJECT_PARENT (buf));
csink = GST_AUDIOSINK_GET_CLASS (sink);
GST_DEBUG ("enter thread");
writefunc = csink->write;
if (writefunc == NULL)
goto no_function;
segsize = buf->spec.segsize;
segtotal = buf->spec.segtotal;
while (TRUE) {
if (g_atomic_int_get (&buf->state) == GST_RINGBUFFER_STATE_PLAYING) {
gint to_write, written;
guint8 *readptr;
gint readseg;
/* we write one segment */
to_write = segsize;
written = 0;
/* need to read and write the next segment */
readseg = (buf->playseg + 1) % segtotal;
/* get a pointer in the buffer to this segment */
readptr = gst_ringbuffer_prepare_read (buf, readseg);
do {
written = writefunc (sink, readptr + written, to_write);
if (written < 0 || written > to_write) {
perror ("error writing data\n");
break;
}
to_write -= written;
} while (to_write > 0);
/* clear written samples */
gst_ringbuffer_clear (buf, readseg);
/* we wrote one segment */
gst_ringbuffer_callback (buf, 1);
} else {
GST_LOCK (abuf);
GST_DEBUG ("signal wait");
GST_AUDIORINGBUFFER_SIGNAL (buf);
GST_DEBUG ("wait for play");
GST_AUDIORINGBUFFER_WAIT (buf);
GST_DEBUG ("got signal");
if (!abuf->running) {
GST_UNLOCK (abuf);
GST_DEBUG ("stop running");
goto done;
}
GST_UNLOCK (abuf);
}
}
done:
GST_DEBUG ("exit thread");
return;
/* ERROR */
no_function:
{
GST_DEBUG ("no write function, exit thread");
return;
}
}
static void
gst_audioringbuffer_init (GstAudioRingBuffer * ringbuffer)
{
ringbuffer->running = TRUE;
ringbuffer->queuedseg = 0;
ringbuffer->cond = g_cond_new ();
}
static void
gst_audioringbuffer_dispose (GObject * object)
{
G_OBJECT_CLASS (ring_parent_class)->dispose (object);
}
static void
gst_audioringbuffer_finalize (GObject * object)
{
G_OBJECT_CLASS (ring_parent_class)->finalize (object);
}
static gboolean
gst_audioringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
{
GstAudioSink *sink;
GstAudioSinkClass *csink;
gboolean result = FALSE;
sink = GST_AUDIOSINK (GST_OBJECT_PARENT (buf));
csink = GST_AUDIOSINK_GET_CLASS (sink);
if (csink->open)
result = csink->open (sink, spec);
if (!result)
goto could_not_open;
/* allocate one more segment as we need some headroom */
spec->segtotal++;
buf->data = gst_buffer_new_and_alloc (spec->segtotal * spec->segsize);
memset (GST_BUFFER_DATA (buf), 0, GST_BUFFER_SIZE (buf));
sink->thread =
g_thread_create ((GThreadFunc) audioringbuffer_thread_func, buf, TRUE,
NULL);
GST_AUDIORINGBUFFER_WAIT (buf);
return result;
could_not_open:
{
return FALSE;
}
}
/* function is called with LOCK */
static gboolean
gst_audioringbuffer_release (GstRingBuffer * buf)
{
GstAudioSink *sink;
GstAudioSinkClass *csink;
GstAudioRingBuffer *abuf;
gboolean result = FALSE;
sink = GST_AUDIOSINK (GST_OBJECT_PARENT (buf));
csink = GST_AUDIOSINK_GET_CLASS (sink);
abuf = GST_AUDIORINGBUFFER (buf);
abuf->running = FALSE;
GST_AUDIORINGBUFFER_SIGNAL (buf);
GST_UNLOCK (buf);
/* join the thread */
g_thread_join (sink->thread);
GST_LOCK (buf);
if (csink->close)
result = csink->close (sink);
return result;
}
static gboolean
gst_audioringbuffer_play (GstRingBuffer * buf)
{
GstAudioSink *sink;
sink = GST_AUDIOSINK (GST_OBJECT_PARENT (buf));
GST_DEBUG ("play");
GST_AUDIORINGBUFFER_SIGNAL (buf);
return TRUE;
}
static gboolean
gst_audioringbuffer_stop (GstRingBuffer * buf)
{
GstAudioSink *sink;
GstAudioSinkClass *csink;
sink = GST_AUDIOSINK (GST_OBJECT_PARENT (buf));
csink = GST_AUDIOSINK_GET_CLASS (sink);
/* unblock any pending writes to the audio device */
if (csink->reset)
csink->reset (sink);
GST_DEBUG ("stop");
GST_AUDIORINGBUFFER_WAIT (buf);
return TRUE;
}
static guint
gst_audioringbuffer_delay (GstRingBuffer * buf)
{
GstAudioSink *sink;
GstAudioSinkClass *csink;
guint res = 0;
sink = GST_AUDIOSINK (GST_OBJECT_PARENT (buf));
csink = GST_AUDIOSINK_GET_CLASS (sink);
if (csink->delay)
res = csink->delay (sink);
return res;
}
/* AudioSink signals and args */
enum
{
/* FILL ME */
LAST_SIGNAL
};
enum
{
ARG_0,
};
#define _do_init(bla) \
GST_DEBUG_CATEGORY_INIT (gst_audiosink_debug, "audiosink", 0, "audiosink element");
GST_BOILERPLATE_FULL (GstAudioSink, gst_audiosink, GstBaseAudioSink,
GST_TYPE_BASEAUDIOSINK, _do_init);
static GstRingBuffer *gst_audiosink_create_ringbuffer (GstBaseAudioSink * sink);
static void
gst_audiosink_base_init (gpointer g_class)
{
}
static void
gst_audiosink_class_init (GstAudioSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSinkClass *gstbasesink_class;
GstBaseAudioSinkClass *gstbaseaudiosink_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasesink_class = (GstBaseSinkClass *) klass;
gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
gstbaseaudiosink_class->create_ringbuffer =
GST_DEBUG_FUNCPTR (gst_audiosink_create_ringbuffer);
}