Fix audio output latency measurement.

This commit is contained in:
Paul Adenot 2019-07-15 18:26:21 +02:00
Родитель 0f799155fc
Коммит c64d242bec
1 изменённых файлов: 14 добавлений и 3 удалений

Просмотреть файл

@ -241,6 +241,7 @@ struct cubeb_stream {
/* Latency requested by the user. */
uint32_t latency_frames = 0;
atomic<uint32_t> current_latency_frames{ 0 };
atomic<uint32_t> total_output_latency_frames { 0 };
atomic<float> panning{ 0 };
unique_ptr<cubeb_resampler, decltype(&cubeb_resampler_destroy)> resampler;
/* This is true if a device change callback is currently running. */
@ -368,8 +369,10 @@ typedef UInt32 AudioObjectID;
#define AudioGetCurrentHostTime mach_absolute_time
#endif
uint64_t
AudioConvertHostTimeToNanos(uint64_t host_time)
ConvertHostTimeToNanos(uint64_t host_time)
{
static struct mach_timebase_info timebase_info;
static bool initialized = false;
@ -385,7 +388,6 @@ AudioConvertHostTimeToNanos(uint64_t host_time)
}
return (uint64_t)answer;
}
#endif
static void
audiounit_increment_active_streams(cubeb * ctx)
@ -583,6 +585,15 @@ audiounit_output_callback(void * user_ptr,
cubeb_stream * stm = static_cast<cubeb_stream *>(user_ptr);
uint64_t now = ConvertHostTimeToNanos(mach_absolute_time());
uint64_t audio_output_time = ConvertHostTimeToNanos(tstamp->mHostTime);
uint64_t output_latency_ns = audio_output_time - now;
const int ns2s = 1e9;
// The total output latency is the timestamp difference + the stream latency +
// the hardware latency.
stm->total_output_latency_frames = output_latency_ns * stm->output_hw_rate / ns2s + stm->current_latency_frames;
ALOGV("(%p) output: buffers %u, size %u, channels %u, frames %u, total input frames %lu.",
stm,
(unsigned int) outBufferList->mNumberBuffers,
@ -2973,7 +2984,7 @@ audiounit_stream_get_latency(cubeb_stream * stm, uint32_t * latency)
//TODO
return CUBEB_ERROR_NOT_SUPPORTED;
#else
*latency = stm->current_latency_frames;
*latency = stm->total_output_latency_frames;
return CUBEB_OK;
#endif
}