Commit 235ea598 authored by Wim Taymans's avatar Wim Taymans
Browse files

Make ringbuffer faster and more simple by removing the locks in the playback thread.

Original commit message from CVS:
Make ringbuffer faster and more simple by removing the locks
in the playback thread.
Add sample accurate playback based on buffer sample offsets.
Make the baseaudiosink provide a clock.
Parse caps in the base class.
Correctly handle seeking, flushing and state changes.
parent 37822dc3
2005-04-28 Wim Taymans <wim@fluendo.com>
* gst-libs/gst/audio/Makefile.am:
* gst-libs/gst/audio/audio.h:
* gst-libs/gst/audio/audioclock.c:
* gst-libs/gst/audio/audioclock.h:
* gst-libs/gst/audio/gstaudioclock.c: (gst_audio_clock_get_type),
(gst_audio_clock_class_init), (gst_audio_clock_init),
(gst_audio_clock_new), (gst_audio_clock_get_internal_time):
* gst-libs/gst/audio/gstaudioclock.h:
* gst-libs/gst/audio/gstaudiosink.c:
(gst_audioringbuffer_get_type), (gst_audioringbuffer_class_init),
(audioringbuffer_thread_func), (gst_audioringbuffer_init),
(gst_audioringbuffer_acquire), (gst_audioringbuffer_release),
(gst_audioringbuffer_play), (gst_audioringbuffer_stop),
(gst_audioringbuffer_delay), (gst_audiosink_class_init),
(gst_audiosink_create_ringbuffer):
* gst-libs/gst/audio/gstbaseaudiosink.c:
(gst_baseaudiosink_class_init), (gst_baseaudiosink_init),
(gst_baseaudiosink_get_clock), (gst_baseaudiosink_get_time),
(gst_baseaudiosink_set_property), (gst_baseaudiosink_get_property),
(build_linear_format), (debug_spec_caps), (debug_spec_buffer),
(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_init), (gst_ringbuffer_finalize),
(gst_ringbuffer_set_callback), (gst_ringbuffer_acquire),
(gst_ringbuffer_release), (gst_ringbuffer_play),
(gst_ringbuffer_pause), (gst_ringbuffer_stop),
(gst_ringbuffer_delay), (gst_ringbuffer_played_samples),
(gst_ringbuffer_set_sample), (wait_segment),
(gst_ringbuffer_commit), (gst_ringbuffer_prepare_read),
(gst_ringbuffer_advance), (gst_ringbuffer_clear):
* gst-libs/gst/audio/gstringbuffer.h:
Make ringbuffer faster and more simple by removing the locks
in the playback thread.
Add sample accurate playback based on buffer sample offsets.
Make the baseaudiosink provide a clock.
Parse caps in the base class.
Correctly handle seeking, flushing and state changes.
2005-04-25 Thomas Vander Stichele <thomas at apestaart dot org>
 
* configure.ac:
......
......@@ -14,7 +14,7 @@ EXTRA_DIST = gstaudiofiltertemplate.c make_filter
CLEANFILES = gstaudiofilterexample.c \
$(BUILT_SOURCES)
libgstaudio_@GST_MAJORMINOR@_la_SOURCES = audio.c audioclock.c \
libgstaudio_@GST_MAJORMINOR@_la_SOURCES = audio.c gstaudioclock.c \
multichannel.c \
gstaudiosink.c \
gstbaseaudiosink.c \
......@@ -24,7 +24,7 @@ nodist_libgstaudio_@GST_MAJORMINOR@_la_SOURCES = $(built_sources) $(built_header
libgstaudio_@GST_MAJORMINOR@includedir = $(includedir)/gstreamer-@GST_MAJORMINOR@/gst/audio
libgstaudio_@GST_MAJORMINOR@include_HEADERS = \
audio.h \
audioclock.h \
gstaudioclock.h \
gstaudiofilter.h \
gstaudiosink.h \
gstbaseaudiosink.h \
......
......@@ -20,8 +20,6 @@
#include <gst/gst.h>
#include <gst/audio/audioclock.h>
#ifndef __GST_AUDIO_AUDIO_H__
#define __GST_AUDIO_AUDIO_H__
......
......@@ -24,16 +24,12 @@
#include "config.h"
#endif
#include "audioclock.h"
#include "gstaudioclock.h"
static void gst_audio_clock_class_init (GstAudioClockClass * klass);
static void gst_audio_clock_init (GstAudioClock * clock);
static GstClockTime gst_audio_clock_get_internal_time (GstClock * clock);
static GstClockReturn gst_audio_clock_id_wait_async (GstClock * clock,
GstClockEntry * entry);
static void gst_audio_clock_id_unschedule (GstClock * clock,
GstClockEntry * entry);
static GstSystemClockClass *parent_class = NULL;
......@@ -79,17 +75,12 @@ gst_audio_clock_class_init (GstAudioClockClass * klass)
parent_class = g_type_class_ref (GST_TYPE_SYSTEM_CLOCK);
gstclock_class->get_internal_time = gst_audio_clock_get_internal_time;
gstclock_class->wait_async = gst_audio_clock_id_wait_async;
gstclock_class->unschedule = gst_audio_clock_id_unschedule;
}
static void
gst_audio_clock_init (GstAudioClock * clock)
{
gst_object_set_name (GST_OBJECT (clock), "GstAudioClock");
clock->prev1 = 0;
clock->prev2 = 0;
}
GstClock *
......@@ -101,105 +92,14 @@ gst_audio_clock_new (gchar * name, GstAudioClockGetTimeFunc func,
aclock->func = func;
aclock->user_data = user_data;
aclock->adjust = 0;
return (GstClock *) aclock;
}
void
gst_audio_clock_set_active (GstAudioClock * aclock, gboolean active)
{
GstClockTime audio_time, system_time;
GstClock *clock;
GTimeVal timeval;
g_return_if_fail (GST_IS_AUDIO_CLOCK (aclock));
clock = GST_CLOCK (aclock);
if (active == aclock->active) {
/* Nothing to do. */
return;
}
audio_time = aclock->func (clock, aclock->user_data);
g_get_current_time (&timeval);
system_time = GST_TIMEVAL_TO_TIME (timeval);
/* Set the new adjust value in such a way that there's no abrupt
discontinuity, i.e. if gst_audio_clock_get_internal_time is
invoked right before and right after (de)activating the clock,
the values returned will be close to each other, and the second
value will be greater than or equal than the first. */
if (active) {
aclock->adjust = aclock->adjust + system_time - audio_time;
} else {
aclock->adjust = aclock->adjust + audio_time - system_time;
}
aclock->active = active;
}
static GstClockTime
gst_audio_clock_get_internal_time (GstClock * clock)
{
GstAudioClock *aclock = GST_AUDIO_CLOCK (clock);
if (aclock->active) {
return aclock->func (clock, aclock->user_data) + aclock->adjust;
} else {
GTimeVal timeval;
g_get_current_time (&timeval);
return GST_TIMEVAL_TO_TIME (timeval) + aclock->adjust;
}
}
void
gst_audio_clock_update_time (GstAudioClock * aclock, GstClockTime time)
{
/* I don't know of a purpose in updating these; perhaps they can be removed */
aclock->prev2 = aclock->prev1;
aclock->prev1 = time;
/* FIXME: the wait_async subsystem should be made threadsafe, but I don't want
* to lock and unlock a mutex on every iteration... */
while (aclock->async_entries) {
GstClockEntry *entry = (GstClockEntry *) aclock->async_entries->data;
if (entry->time > time)
break;
entry->func ((GstClock *) aclock, time, entry, entry->user_data);
aclock->async_entries = g_slist_delete_link (aclock->async_entries,
aclock->async_entries);
/* do I need to free the entry? */
}
}
static gint
compare_clock_entries (GstClockEntry * entry1, GstClockEntry * entry2)
{
return entry1->time - entry2->time;
}
static GstClockReturn
gst_audio_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
{
GstAudioClock *aclock = (GstAudioClock *) clock;
aclock->async_entries = g_slist_insert_sorted (aclock->async_entries,
entry, (GCompareFunc) compare_clock_entries);
/* is this the proper return val? */
return GST_CLOCK_EARLY;
}
static void
gst_audio_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
{
GstAudioClock *aclock = (GstAudioClock *) clock;
aclock->async_entries = g_slist_remove (aclock->async_entries, entry);
return aclock->func (clock, aclock->user_data);
}
/* GStreamer
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
* 2000 Wim Taymans <wtay@chello.be>
* 2005 Wim Taymans <wim@fluendo.com>
*
* audioclock.h: Clock for use by audio plugins
* gstaudioclock.h: Clock for use by audio plugins
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -44,22 +44,13 @@ typedef struct _GstAudioClockClass GstAudioClockClass;
typedef GstClockTime (*GstAudioClockGetTimeFunc) (GstClock *clock, gpointer user_data);
struct _GstAudioClock {
GstSystemClock clock;
GstClockTime prev1, prev2;
/* --- protected --- */
GstAudioClockGetTimeFunc func;
gpointer user_data;
GstClockTimeDiff adjust;
GSList *async_entries;
gboolean active;
gpointer _gst_reserved[GST_PADDING];
};
......@@ -72,9 +63,6 @@ struct _GstAudioClockClass {
GType gst_audio_clock_get_type (void);
GstClock* gst_audio_clock_new (gchar *name, GstAudioClockGetTimeFunc func,
gpointer user_data);
void gst_audio_clock_set_active (GstAudioClock *aclock, gboolean active);
void gst_audio_clock_update_time (GstAudioClock *aclock, GstClockTime time);
G_END_DECLS
......
......@@ -125,6 +125,7 @@ gst_audioringbuffer_class_init (GstAudioRingBufferClass * klass)
gstringbuffer_class->release =
GST_DEBUG_FUNCPTR (gst_audioringbuffer_release);
gstringbuffer_class->play = GST_DEBUG_FUNCPTR (gst_audioringbuffer_play);
gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_audioringbuffer_play);
gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_audioringbuffer_stop);
gstringbuffer_class->delay = GST_DEBUG_FUNCPTR (gst_audioringbuffer_delay);
......@@ -144,7 +145,6 @@ audioringbuffer_thread_func (GstRingBuffer * buf)
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);
......@@ -155,53 +155,48 @@ audioringbuffer_thread_func (GstRingBuffer * buf)
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);
gint left, len;
guint8 *readptr;
gint readseg;
if (gst_ringbuffer_prepare_read (buf, &readseg, &readptr, &len)) {
gint written = 0;
left = len;
do {
written = writefunc (sink, readptr + written, to_write);
if (written < 0 || written > to_write) {
perror ("error writing data\n");
GST_DEBUG ("transfer %d bytes from segment %d", left, readseg);
written = writefunc (sink, readptr + written, left);
GST_DEBUG ("transfered %d bytes", written);
if (written < 0 || written > left) {
GST_WARNING ("error writing data (reason: %s), skipping segment\n",
strerror (errno));
break;
}
to_write -= written;
} while (to_write > 0);
left -= written;
} while (left > 0);
/* clear written samples */
gst_ringbuffer_clear (buf, readseg);
/* we wrote one segment */
gst_ringbuffer_callback (buf, 1);
gst_ringbuffer_advance (buf, 1);
} else {
GST_LOCK (abuf);
GST_DEBUG ("signal wait");
GST_AUDIORINGBUFFER_SIGNAL (buf);
GST_DEBUG ("wait for play");
GST_DEBUG ("wait for action");
GST_AUDIORINGBUFFER_WAIT (buf);
GST_DEBUG ("got signal");
if (!abuf->running) {
GST_UNLOCK (abuf);
GST_DEBUG ("stop running");
goto done;
break;
}
GST_DEBUG ("continue running");
GST_UNLOCK (abuf);
}
}
done:
GST_DEBUG ("exit thread");
return;
......@@ -305,7 +300,7 @@ gst_audioringbuffer_play (GstRingBuffer * buf)
sink = GST_AUDIOSINK (GST_OBJECT_PARENT (buf));
GST_DEBUG ("play");
GST_DEBUG ("play, sending signal");
GST_AUDIORINGBUFFER_SIGNAL (buf);
return TRUE;
......@@ -321,11 +316,15 @@ gst_audioringbuffer_stop (GstRingBuffer * buf)
csink = GST_AUDIOSINK_GET_CLASS (sink);
/* unblock any pending writes to the audio device */
if (csink->reset)
if (csink->reset) {
GST_DEBUG ("reset...");
csink->reset (sink);
GST_DEBUG ("reset done");
}
GST_DEBUG ("stop");
GST_DEBUG ("stop, waiting...");
GST_AUDIORINGBUFFER_WAIT (buf);
GST_DEBUG ("stoped");
return TRUE;
}
......@@ -398,7 +397,9 @@ gst_audiosink_create_ringbuffer (GstBaseAudioSink * sink)
{
GstRingBuffer *buffer;
GST_DEBUG ("creating ringbuffer");
buffer = g_object_new (GST_TYPE_AUDIORINGBUFFER, NULL);
GST_DEBUG ("created ringbuffer @%p", buffer);
return buffer;
}
......@@ -20,6 +20,8 @@
* Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include "gstbaseaudiosink.h"
GST_DEBUG_CATEGORY_STATIC (gst_baseaudiosink_debug);
......@@ -32,13 +34,13 @@ enum
LAST_SIGNAL
};
#define DEFAULT_BUFFER -1
#define DEFAULT_LATENCY -1
#define DEFAULT_BUFFER_TIME 500 * GST_USECOND
#define DEFAULT_LATENCY_TIME 10 * GST_USECOND
enum
{
PROP_0,
PROP_BUFFER,
PROP_LATENCY,
PROP_BUFFER_TIME,
PROP_LATENCY_TIME,
};
#define _do_init(bla) \
......@@ -55,6 +57,10 @@ static void gst_baseaudiosink_get_property (GObject * object, guint prop_id,
static GstElementStateReturn gst_baseaudiosink_change_state (GstElement *
element);
static GstClock *gst_baseaudiosink_get_clock (GstElement * elem);
static GstClockTime gst_baseaudiosink_get_time (GstClock * clock,
GstBaseAudioSink * sink);
static GstFlowReturn gst_baseaudiosink_preroll (GstBaseSink * bsink,
GstBuffer * buffer);
static GstFlowReturn gst_baseaudiosink_render (GstBaseSink * bsink,
......@@ -87,17 +93,18 @@ gst_baseaudiosink_class_init (GstBaseAudioSinkClass * klass)
gobject_class->get_property =
GST_DEBUG_FUNCPTR (gst_baseaudiosink_get_property);
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER,
g_param_spec_uint64 ("buffer", "Buffer",
"Size of audio buffer in nanoseconds (-1 = default)",
0, G_MAXUINT64, DEFAULT_BUFFER, G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LATENCY,
g_param_spec_uint64 ("latency", "Latency",
"Audio latency in nanoseconds (-1 = default)",
0, G_MAXUINT64, DEFAULT_LATENCY, G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_TIME,
g_param_spec_int64 ("buffer-time", "Buffer Time",
"Size of audio buffer in milliseconds (-1 = default)",
-1, G_MAXINT64, DEFAULT_BUFFER_TIME, G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LATENCY_TIME,
g_param_spec_int64 ("latency-time", "Latency Time",
"Audio latency in milliseconds (-1 = default)",
-1, G_MAXINT64, DEFAULT_LATENCY_TIME, G_PARAM_READWRITE));
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_baseaudiosink_change_state);
gstelement_class->get_clock = GST_DEBUG_FUNCPTR (gst_baseaudiosink_get_clock);
gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_baseaudiosink_event);
gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_baseaudiosink_preroll);
......@@ -110,8 +117,38 @@ gst_baseaudiosink_class_init (GstBaseAudioSinkClass * klass)
static void
gst_baseaudiosink_init (GstBaseAudioSink * baseaudiosink)
{
baseaudiosink->buffer = DEFAULT_BUFFER;
baseaudiosink->latency = DEFAULT_LATENCY;
baseaudiosink->buffer_time = DEFAULT_BUFFER_TIME;
baseaudiosink->latency_time = DEFAULT_LATENCY_TIME;
baseaudiosink->clock = gst_audio_clock_new ("clock",
(GstAudioClockGetTimeFunc) gst_baseaudiosink_get_time, baseaudiosink);
}
static GstClock *
gst_baseaudiosink_get_clock (GstElement * elem)
{
GstBaseAudioSink *sink;
sink = GST_BASEAUDIOSINK (elem);
return GST_CLOCK (gst_object_ref (GST_OBJECT (sink->clock)));
}
static GstClockTime
gst_baseaudiosink_get_time (GstClock * clock, GstBaseAudioSink * sink)
{
guint64 samples;
GstClockTime result;
if (sink->ringbuffer == NULL || sink->ringbuffer->spec.rate == 0)
return 0;
samples = gst_ringbuffer_played_samples (sink->ringbuffer);
result = samples * GST_SECOND / sink->ringbuffer->spec.rate;
result += GST_ELEMENT (sink)->base_time;
return result;
}
static void
......@@ -123,9 +160,11 @@ gst_baseaudiosink_set_property (GObject * object, guint prop_id,
sink = GST_BASEAUDIOSINK (object);
switch (prop_id) {
case PROP_BUFFER:
case PROP_BUFFER_TIME:
sink->buffer_time = g_value_get_int64 (value);
break;
case PROP_LATENCY:
case PROP_LATENCY_TIME:
sink->latency_time = g_value_get_int64 (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
......@@ -142,9 +181,11 @@ gst_baseaudiosink_get_property (GObject * object, guint prop_id, GValue * value,
sink = GST_BASEAUDIOSINK (object);
switch (prop_id) {
case PROP_BUFFER:
case PROP_BUFFER_TIME:
g_value_set_int64 (value, sink->buffer_time);
break;
case PROP_LATENCY:
case PROP_LATENCY_TIME:
g_value_set_int64 (value, sink->latency_time);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
......@@ -152,25 +193,228 @@ gst_baseaudiosink_get_property (GObject * object, guint prop_id, GValue * value,
}
}
static int linear_formats[4 * 2 * 2] = {
GST_S8,
GST_S8,
GST_U8,
GST_U8,
GST_S16_LE,
GST_S16_BE,
GST_U16_LE,
GST_U16_BE,
GST_S24_LE,
GST_S24_BE,
GST_U24_LE,
GST_U24_BE,
GST_S32_LE,
GST_S32_BE,
GST_U32_LE,
GST_U32_BE
};
static int linear24_formats[3 * 2 * 2] = {
GST_S24_3LE,
GST_S24_3BE,
GST_U24_3LE,
GST_U24_3BE,
GST_S20_3LE,
GST_S20_3BE,
GST_U20_3LE,
GST_U20_3BE,
GST_S18_3LE,
GST_S18_3BE,
GST_U18_3LE,
GST_U18_3BE,
};
static GstBufferFormat
build_linear_format (int depth, int width, int unsignd, int big_endian)
{
if (width == 24) {
switch (depth) {
case 24:
depth = 0;
break;
case 20:
depth = 1;
break;
case 18:
depth = 2;
break;
default:
return GST_UNKNOWN;
}
return ((int (*)[2][2]) linear24_formats)[depth][!!unsignd][!!big_endian];
} else {
switch (depth) {
case 8:
depth = 0;
break;
case 16:
depth = 1;
break;
case 24:
depth = 2;
break;
case 32:
depth = 3;
break;
default:
return GST_UNKNOWN;
}
}
return ((int (*)[2][2]) linear_formats)[depth][!!unsignd][!!big_endian];
}
static void
debug_spec_caps (GstBaseAudioSink * sink, GstRingBufferSpec * spec)
{
GST_DEBUG ("spec caps: %p %" GST_PTR_FORMAT, spec->caps, spec->caps);
GST_DEBUG ("parsed caps: type: %d", spec->type);
GST_DEBUG ("parsed caps: format: %d", spec->format);
GST_DEBUG ("parsed caps: width: %d", spec->width);
GST_DEBUG ("parsed caps: depth: %d", spec->depth);
GST_DEBUG ("parsed caps: sign: %d", spec->sign);
GST_DEBUG ("parsed caps: bigend: %d", spec->bigend);
GST_DEBUG ("parsed caps: rate: %d", spec->rate);
GST_DEBUG ("parsed caps: channels: %d", spec->channels);
GST_DEBUG ("parsed caps: sample bytes: %d", spec->bytes_per_sample);
}
static void
debug_spec_buffer (GstBaseAudioSink * sink, GstRingBufferSpec * spec)
{
GST_DEBUG ("acquire ringbuffer: buffer time: %" G_GINT64_FORMAT " usec",
spec->buffer_time);
GST_DEBUG ("acquire ringbuffer: latency time: %" G_GINT64_FORMAT " usec",
spec->latency_time);
GST_DEBUG ("acquire ringbuffer: total segments: %d", spec->segtotal);
GST_DEBUG ("acquire ringbuffer: segment size: %d bytes = %d samples",
spec->segsize, spec->segsize / spec->bytes_per_sample);
GST_DEBUG ("acquire ringbuffer: buffer size: %d bytes = %d samples",
spec->segsize * spec->segtotal,
spec->segsize * spec->segtotal / spec->bytes_per_sample);
}
static gboolean
gst_baseaudiosink_setcaps (GstBaseSink * bsink, GstCaps * caps)
{
GstBaseAudioSink *sink = GST_BASEAUDIOSINK (bsink);
GstRingBufferSpec *spec;