Skip to content

Commit

Permalink
Mutex protect _saudio_backend_latency() under AAudio
Browse files Browse the repository at this point in the history
This is necessary as the error callback might be called at the same time as this function
  • Loading branch information
oviano committed Mar 6, 2021
1 parent 283f1fc commit 0569b2d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions sokol_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -1803,18 +1803,24 @@ _SOKOL_PRIVATE bool _saudio_backend_init(void) {
}

_SOKOL_PRIVATE int _saudio_backend_latency(void) {
if (_saudio.backend.stream) {
int64_t frame_position;
int64_t time_nanoseconds;
if (AAudioStream_getTimestamp(_saudio.backend.stream, CLOCK_MONOTONIC, &frame_position, &time_nanoseconds) == AAUDIO_OK) {
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
int64_t frames_played = frame_position + (now.tv_sec * 1000000000LL + now.tv_nsec - time_nanoseconds) * _saudio.sample_rate / 1000000000LL;
int64_t frames_written = AAudioStream_getFramesWritten(_saudio.backend.stream);
return (frames_written - frames_played) * 1000 / _saudio.sample_rate;
if (_saudio.valid) {
pthread_mutex_lock(&_saudio.backend.mutex);
if (_saudio.backend.stream) {
int64_t frame_position;
int64_t time_nanoseconds;
if (AAudioStream_getTimestamp(_saudio.backend.stream, CLOCK_MONOTONIC, &frame_position, &time_nanoseconds) == AAUDIO_OK) {
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
int64_t frames_played = frame_position + (now.tv_sec * 1000000000LL + now.tv_nsec - time_nanoseconds) * _saudio.sample_rate / 1000000000LL;
int64_t frames_written = AAudioStream_getFramesWritten(_saudio.backend.stream);
pthread_mutex_unlock(&_saudio.backend.mutex);
return (frames_written - frames_played) * 1000 / _saudio.sample_rate;
}
}
pthread_mutex_unlock(&_saudio.backend.mutex);
return _saudio_default_latency();
}
return _saudio_default_latency();
return 0;
}

#ifdef __cplusplus
Expand Down

0 comments on commit 0569b2d

Please sign in to comment.