path: root/audio/out/push.c
diff options
authorwm4 <wm4@nowhere>2014-03-10 01:13:40 +0100
committerwm4 <wm4@nowhere>2014-03-10 01:13:40 +0100
commitd842b017e41cd6c70ecb6c5dfb78b0d568448a33 (patch)
tree6e6010d867926f6b85d7a53f741f0835c21dfe6c /audio/out/push.c
parent4c19c71b85d7c52648feaa6dee599d84f9810da4 (diff)
audio/out: reduce amount of audio buffering
Since the addition of the AO feed thread, 200ms of latency (MIN_BUFFER) was added to all push-based AOs. This is not so nice, because even AOs with relatively small buffering (e.g. ao_alsa on my system with ~170ms of buffer size), the additional latency becomes noticable when e.g. toggling mute with softvol. Fix this by trying to keep not only 200ms minimum buffer, but also 200ms maximum buffer. In other words, never buffer beyond 200ms in total. Do this by estimating the AO's buffer fill status using get_space and the initially known AO buffer size (the get_space return value on initialization, before any audio was played). We limit the maximum amount of data written to the soft buffer so that soft buffer size and audio buffer size equal to 200ms (MIN_BUFFER). To avoid weird problems with weird AOs, we buffer beyond MIN_BUFFER if the AO's get_space requests more data than that, and as long as the soft buffer is large enough. Note that this is just a hack to improve the latency. When the audio chain gains the ability to refilter data, this won't be needed anymore, and instead we can introduce some sort of buffer replacement function in order to update data in the soft buffer.
Diffstat (limited to 'audio/out/push.c')
1 files changed, 16 insertions, 2 deletions
diff --git a/audio/out/push.c b/audio/out/push.c
index f0608dd3a4..228fa25ba4 100644
--- a/audio/out/push.c
+++ b/audio/out/push.c
@@ -122,9 +122,23 @@ static int get_space(struct ao *ao)
struct ao_push_state *p = ao->api_priv;
- int s = mp_audio_buffer_get_write_available(p->buffer);
+ int space = mp_audio_buffer_get_write_available(p->buffer);
+ if (ao->driver->get_space) {
+ // The following code attempts to keep the total buffered audio to
+ // MIN_BUFFER in order to improve latency.
+ int device_space = ao->driver->get_space(ao);
+ int device_buffered = ao->device_buffer - device_space;
+ int soft_buffered = mp_audio_buffer_samples(p->buffer);
+ int min_buffer = MIN_BUFFER * ao->samplerate;
+ int missing = min_buffer - device_buffered - soft_buffered;
+ // But always keep the device's buffer filled as much as we can.
+ int device_missing = device_space - soft_buffered;
+ missing = MPMAX(missing, device_missing);
+ space = MPMIN(space, missing);
+ space = MPMAX(0, space);
+ }
- return s;
+ return space;
static int play(struct ao *ao, void **data, int samples, int flags)