From e250fe3707b2b8f09e3a8c540b78be705966144d Mon Sep 17 00:00:00 2001 From: halx99 Date: Thu, 30 Jul 2020 17:26:54 +0800 Subject: [PATCH] Fix dead-lock when play/stopAll immidiately --- cocos/audio/AudioCache.cpp | 4 +++- cocos/audio/AudioEngineImpl.cpp | 3 ++- cocos/audio/apple/AudioCache.mm | 4 +++- cocos/audio/apple/AudioEngineImpl.mm | 4 ++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cocos/audio/AudioCache.cpp b/cocos/audio/AudioCache.cpp index 5f71742d39..8cf1d4adeb 100644 --- a/cocos/audio/AudioCache.cpp +++ b/cocos/audio/AudioCache.cpp @@ -276,11 +276,13 @@ void AudioCache::readDataTask(unsigned int selfId) } } + // Set before invokingPlayCallbacks, otherwise, may cause dead-lock + _isLoadingFinished = true; + //FIXME: Why to invoke play callback first? Should it be after 'load' callback? invokingPlayCallbacks(); invokingLoadCallbacks(); - _isLoadingFinished = true; _readDataTaskMutex.unlock(); ALOGVV("readDataTask end, cache id=%u", selfId); } diff --git a/cocos/audio/AudioEngineImpl.cpp b/cocos/audio/AudioEngineImpl.cpp index 538a127e4f..592e95dbc2 100644 --- a/cocos/audio/AudioEngineImpl.cpp +++ b/cocos/audio/AudioEngineImpl.cpp @@ -495,8 +495,9 @@ void AudioEngineImpl::_updateLocked(float dt) finishCallback(audioID, filePath); //FIXME: callback will delay 50ms }); #endif - player->setCache(nullptr); // it's safe for player didn't free audio cache } + // clear cache when audio player finsihed properly + player->setCache(nullptr); delete player; _alSourceUsed[alSource] = false; } diff --git a/cocos/audio/apple/AudioCache.mm b/cocos/audio/apple/AudioCache.mm index 177543a11a..ed42c863f8 100644 --- a/cocos/audio/apple/AudioCache.mm +++ b/cocos/audio/apple/AudioCache.mm @@ -306,11 +306,13 @@ void AudioCache::readDataTask(unsigned int selfId) decoder->close(); AudioDecoderManager::destroyDecoder(decoder); + // Set before invokingPlayCallbacks, otherwise, may cause dead-lock + _isLoadingFinished = true; + //FIXME: Why to invoke play callback first? Should it be after 'load' callback? invokingPlayCallbacks(); invokingLoadCallbacks(); - _isLoadingFinished = true; if (_state != State::READY) { _state = State::FAILED; diff --git a/cocos/audio/apple/AudioEngineImpl.mm b/cocos/audio/apple/AudioEngineImpl.mm index 0a4136d8bc..3185a23212 100644 --- a/cocos/audio/apple/AudioEngineImpl.mm +++ b/cocos/audio/apple/AudioEngineImpl.mm @@ -751,9 +751,9 @@ void AudioEngineImpl::_updateLocked(float dt) _finishCallbacks.push_back([finishCallback = std::move(player->_finishCallbak), audioID, filePath = std::move(filePath)](){ finishCallback(audioID, filePath); // FIXME: callback will delay 50ms }); - player->setCache(nullptr); // it's safe for player didn't free audio cache } - + // clear cache when audio player finsihed properly + player->setCache(nullptr); delete player; _unusedSourcesPool.push_back(alSource); }