summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-03-10 01:13:40 +0100
committerwm4 <wm4@nowhere>2014-03-10 01:13:40 +0100
commitd842b017e41cd6c70ecb6c5dfb78b0d568448a33 (patch)
tree6e6010d867926f6b85d7a53f741f0835c21dfe6c
parent4c19c71b85d7c52648feaa6dee599d84f9810da4 (diff)
downloadmpv-d842b017e41cd6c70ecb6c5dfb78b0d568448a33.tar.bz2
mpv-d842b017e41cd6c70ecb6c5dfb78b0d568448a33.tar.xz
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.
-rw-r--r--audio/out/ao.c3
-rw-r--r--audio/out/internal.h3
-rw-r--r--audio/out/push.c18
3 files changed, 19 insertions, 5 deletions
diff --git a/audio/out/ao.c b/audio/out/ao.c
index ee8bc2a254..d48b266122 100644
--- a/audio/out/ao.c
+++ b/audio/out/ao.c
@@ -36,9 +36,6 @@
#include "common/common.h"
#include "common/global.h"
-// Minimum buffer size in seconds.
-#define MIN_BUFFER 0.2
-
extern const struct ao_driver audio_out_oss;
extern const struct ao_driver audio_out_coreaudio;
extern const struct ao_driver audio_out_rsound;
diff --git a/audio/out/internal.h b/audio/out/internal.h
index c14cdf3c68..ea655f65f2 100644
--- a/audio/out/internal.h
+++ b/audio/out/internal.h
@@ -24,6 +24,9 @@
#include "audio/chmap.h"
#include "audio/chmap_sel.h"
+// Minimum buffer size in seconds.
+#define MIN_BUFFER 0.2
+
/* global data used by ao.c and ao drivers */
struct ao {
int samplerate;
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;
pthread_mutex_lock(&p->lock);
- 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);
+ }
pthread_mutex_unlock(&p->lock);
- return s;
+ return space;
}
static int play(struct ao *ao, void **data, int samples, int flags)