diff options
author | Stefano Pigozzi <stefano.pigozzi@gmail.com> | 2014-07-02 22:47:54 +0200 |
---|---|---|
committer | Stefano Pigozzi <stefano.pigozzi@gmail.com> | 2014-07-02 23:17:44 +0200 |
commit | a6a4cd2c889d1cb2d2d6779102c5430fdc1c437c (patch) | |
tree | 1aecee3093d48e0bb443f6390da1710d1bd44951 /audio/out | |
parent | 703de5d84eec087e21ce15b7eb4be9d6e2336d50 (diff) | |
download | mpv-a6a4cd2c889d1cb2d2d6779102c5430fdc1c437c.tar.bz2 mpv-a6a4cd2c889d1cb2d2d6779102c5430fdc1c437c.tar.xz |
ao_coreaudio: report latency more correctly
Previous code was completly wrong. This still doesn't report the device
latency, but we report the buffer latency (as before the AO refactoring) and
the AudioUnit's latency (this is a new 'feature').
Apparently we can also report the device actual latency and we should also
calculate the actual sample rate of the audio device instead of using the
nominal sample rate, but I'll leave this for a later commit.
Diffstat (limited to 'audio/out')
-rw-r--r-- | audio/out/ao_coreaudio.c | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/audio/out/ao_coreaudio.c b/audio/out/ao_coreaudio.c index 59846cd605..b2d78aa872 100644 --- a/audio/out/ao_coreaudio.c +++ b/audio/out/ao_coreaudio.c @@ -15,6 +15,8 @@ * with mpv. If not, see <http://www.gnu.org/licenses/>. */ +#include <CoreAudio/HostTime.h> + #include "config.h" #include "ao.h" #include "internal.h" @@ -38,13 +40,29 @@ struct priv { bool ca_layout_to_mp_chmap(struct ao *ao, AudioChannelLayout *layout, struct mp_chmap *chmap); +static int64_t ca_get_latency(const AudioTimeStamp *ts) +{ + uint64_t out = AudioConvertHostTimeToNanos(ts->mHostTime); + uint64_t now = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); + + if (now > out) + return 0; + + return (out - now) * 1e-3; +} + static OSStatus render_cb_lpcm(void *ctx, AudioUnitRenderActionFlags *aflags, const AudioTimeStamp *ts, UInt32 bus, UInt32 frames, AudioBufferList *buffer_list) { struct ao *ao = ctx; AudioBuffer buf = buffer_list->mBuffers[0]; - ao_read_data(ao, &buf.mData, frames, ts->mSampleTime); + + const int64_t playback_us = frames / (float) ao->samplerate * 1e6; + const int64_t latency_us = ca_get_latency(ts); + + const int64_t end = mp_time_us() + playback_us + latency_us; + ao_read_data(ao, &buf.mData, frames, end); return noErr; } |