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
This commit is contained in:
minggo 2019-07-22 09:37:07 +08:00 committed by GitHub
parent 77a72052c8
commit 3e6b1ffe92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 15 deletions

View File

@ -344,7 +344,7 @@ void AudioEngine::remove(int audioID)
it->second.profileHelper->audioIDs.remove(audioID); it->second.profileHelper->audioIDs.remove(audioID);
} }
_audioPathIDMap[*it->second.filePath].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); itInfo->second.profileHelper->audioIDs.remove(audioID);
} }
_audioIDInfoMap.erase(audioID); _audioIDInfoMap.erase(itInfo);
} }
} }
_audioPathIDMap.erase(filePath); _audioPathIDMap.erase(filePath);
} }
if (_audioEngineImpl) _audioEngineImpl->uncache(filePath);
{
_audioEngineImpl->uncache(filePath);
}
} }
void AudioEngine::uncacheAll() void AudioEngine::uncacheAll()

View File

@ -72,8 +72,6 @@ AudioEngineImpl::AudioEngineImpl()
AudioEngineImpl::~AudioEngineImpl() AudioEngineImpl::~AudioEngineImpl()
{ {
FMOD_RESULT result; FMOD_RESULT result;
result = pSystem->close();
ERRCHECKWITHEXIT(result);
result = pSystem->release(); result = pSystem->release();
ERRCHECKWITHEXIT(result); ERRCHECKWITHEXIT(result);
} }
@ -109,7 +107,8 @@ int AudioEngineImpl::play2d(const std::string &fileFullPath, bool loop, float vo
int id = preload(fileFullPath, nullptr); int id = preload(fileFullPath, nullptr);
if (id >= 0) { if (id >= 0) {
mapChannelInfo[id].loop=loop; 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; mapChannelInfo[id].volume = volume;
AudioEngine::_audioIDInfoMap[id].state = AudioEngine::AudioState::PAUSED; AudioEngine::_audioIDInfoMap[id].state = AudioEngine::AudioState::PAUSED;
resume(id); resume(id);
@ -287,8 +286,7 @@ void AudioEngineImpl::uncache(const std::string& path)
} }
mapSound.erase(it); mapSound.erase(it);
} }
if (mapId.find(path) != mapId.end()) mapId.erase(path);
mapId.erase(path);
} }
void AudioEngineImpl::uncacheAll() void AudioEngineImpl::uncacheAll()
@ -320,10 +318,9 @@ int AudioEngineImpl::preload(const std::string& filePath, std::function<void(boo
} }
int id = static_cast<int>(mapChannelInfo.size()) + 1; int id = static_cast<int>(mapChannelInfo.size()) + 1;
if (mapId.find(filePath) == mapId.end()) // std::map::insert returns std::pair<iter, bool>
mapId.insert({filePath, id}); auto channelInfoIter = mapId.insert({filePath, id});
else id = channelInfoIter.first->second;
id = mapId.at(filePath);
auto& chanelInfo = mapChannelInfo[id]; auto& chanelInfo = mapChannelInfo[id];
chanelInfo.sound = sound; chanelInfo.sound = sound;