diff options
-rw-r--r-- | audio/out/ao.c | 3 | ||||
-rw-r--r-- | audio/out/internal.h | 3 | ||||
-rw-r--r-- | audio/out/push.c | 18 |
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) |