From 3e6b1ffe929f26c81d548906f1c1935e7aebb9ee Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 22 Jul 2019 09:37:07 +0800 Subject: [PATCH] refactor AudioEngine-Linux (#19822) (#19946) * refactor AudioEngine and AudioEngine-linux * map::erase() can handle case if key doesn't exist. * use map::iterator when it has already obtained. * mapChannelInfo[id].channel is nullptr befor resume(). Don't dereference it. * FMOD::System::release() calls close, so calling close before release is not necessary. * use std::map::insert properly. * remove unnecessary null check on _audioEngineImpl * add comment on nullptr dereference --- cocos/audio/AudioEngine.cpp | 9 +++------ cocos/audio/linux/AudioEngine-linux.cpp | 15 ++++++--------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/cocos/audio/AudioEngine.cpp b/cocos/audio/AudioEngine.cpp index 2ae01b4822..3f192341f0 100644 --- a/cocos/audio/AudioEngine.cpp +++ b/cocos/audio/AudioEngine.cpp @@ -344,7 +344,7 @@ void AudioEngine::remove(int audioID) it->second.profileHelper->audioIDs.remove(audioID); } _audioPathIDMap[*it->second.filePath].remove(audioID); - _audioIDInfoMap.erase(audioID); + _audioIDInfoMap.erase(it); } } @@ -389,16 +389,13 @@ void AudioEngine::uncache(const std::string &filePath) { itInfo->second.profileHelper->audioIDs.remove(audioID); } - _audioIDInfoMap.erase(audioID); + _audioIDInfoMap.erase(itInfo); } } _audioPathIDMap.erase(filePath); } - if (_audioEngineImpl) - { - _audioEngineImpl->uncache(filePath); - } + _audioEngineImpl->uncache(filePath); } void AudioEngine::uncacheAll() diff --git a/cocos/audio/linux/AudioEngine-linux.cpp b/cocos/audio/linux/AudioEngine-linux.cpp index 29df77226f..0a9e89b860 100644 --- a/cocos/audio/linux/AudioEngine-linux.cpp +++ b/cocos/audio/linux/AudioEngine-linux.cpp @@ -72,8 +72,6 @@ AudioEngineImpl::AudioEngineImpl() AudioEngineImpl::~AudioEngineImpl() { FMOD_RESULT result; - result = pSystem->close(); - ERRCHECKWITHEXIT(result); result = pSystem->release(); ERRCHECKWITHEXIT(result); } @@ -109,7 +107,8 @@ int AudioEngineImpl::play2d(const std::string &fileFullPath, bool loop, float vo int id = preload(fileFullPath, nullptr); if (id >= 0) { mapChannelInfo[id].loop=loop; - mapChannelInfo[id].channel->setPaused(true); + // channel is null here. Don't dereference it. It's only set in resume(id). + //mapChannelInfo[id].channel->setPaused(true); mapChannelInfo[id].volume = volume; AudioEngine::_audioIDInfoMap[id].state = AudioEngine::AudioState::PAUSED; resume(id); @@ -287,8 +286,7 @@ void AudioEngineImpl::uncache(const std::string& path) } mapSound.erase(it); } - if (mapId.find(path) != mapId.end()) - mapId.erase(path); + mapId.erase(path); } void AudioEngineImpl::uncacheAll() @@ -320,10 +318,9 @@ int AudioEngineImpl::preload(const std::string& filePath, std::function(mapChannelInfo.size()) + 1; - if (mapId.find(filePath) == mapId.end()) - mapId.insert({filePath, id}); - else - id = mapId.at(filePath); + // std::map::insert returns std::pair + auto channelInfoIter = mapId.insert({filePath, id}); + id = channelInfoIter.first->second; auto& chanelInfo = mapChannelInfo[id]; chanelInfo.sound = sound;