VideoPlayer: fix possible race condition

In set_stream() we write to 'playback' while accessing the same object
in _mix_audio() in audio thread. Protect the 'write' part in
set_stream() to avoid possible crash in _mix_audio() function.

(cherry picked from commit e435d57758)
This commit is contained in:
Mark Kuo 2020-06-29 12:56:10 +10:00 committed by Rémi Verschelde
parent 850f07a4d9
commit 46590fa3b0
1 changed files with 2 additions and 2 deletions

View File

@ -212,10 +212,9 @@ bool VideoPlayer::has_expand() const {
void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) { void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) {
stop(); stop();
AudioServer::get_singleton()->lock(); AudioServer::get_singleton()->lock();
mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size()); mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size());
AudioServer::get_singleton()->unlock();
stream = p_stream; stream = p_stream;
if (stream.is_valid()) { if (stream.is_valid()) {
stream->set_audio_track(audio_track); stream->set_audio_track(audio_track);
@ -223,6 +222,7 @@ void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) {
} else { } else {
playback = Ref<VideoStreamPlayback>(); playback = Ref<VideoStreamPlayback>();
} }
AudioServer::get_singleton()->unlock();
if (!playback.is_null()) { if (!playback.is_null()) {
playback->set_loop(loops); playback->set_loop(loops);