diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-01-16 21:18:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-16 21:18:05 +0100 |
commit | 49b5776e8bd62b4bfb6511cca770611b6a3062e4 (patch) | |
tree | ad737ba05b4a6ee81b2616137ac2d746a0c58987 | |
parent | 3f66d8b953c89d366f83a71ec8404f47c1824d5c (diff) | |
parent | 17ac0127284145d366a4611960525ecbdac8ab10 (diff) |
Merge pull request #45036 from vix597/issue-45025
Fix `AudioServer.get_time_since_last_mix()` returning incorrect value when accessing from another thread
-rw-r--r-- | servers/audio_server.cpp | 17 | ||||
-rw-r--r-- | servers/audio_server.h | 4 |
2 files changed, 14 insertions, 7 deletions
diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 295059a1ea..d4f7876b4b 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -71,13 +71,20 @@ void AudioDriver::update_mix_time(int p_frames) { } } -double AudioDriver::get_time_since_last_mix() const { - return (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0; +double AudioDriver::get_time_since_last_mix() { + lock(); + uint64_t last_mix_time = _last_mix_time; + unlock(); + return (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0; } -double AudioDriver::get_time_to_next_mix() const { - double total = (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0; - double mix_buffer = _last_mix_frames / (double)get_mix_rate(); +double AudioDriver::get_time_to_next_mix() { + lock(); + uint64_t last_mix_time = _last_mix_time; + uint64_t last_mix_frames = _last_mix_frames; + unlock(); + double total = (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0; + double mix_buffer = last_mix_frames / (double)get_mix_rate(); return mix_buffer - total; } diff --git a/servers/audio_server.h b/servers/audio_server.h index 631fef0f65..51fbc59851 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -70,8 +70,8 @@ protected: #endif public: - double get_time_since_last_mix() const; //useful for video -> audio sync - double get_time_to_next_mix() const; + double get_time_since_last_mix(); //useful for video -> audio sync + double get_time_to_next_mix(); enum SpeakerMode { SPEAKER_MODE_STEREO, |