Skip to content

Commit

Permalink
refactor AudioEngine-Linux (#19822)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JohnCoconut authored and minggo committed Jun 19, 2019
1 parent bf90996 commit d09918b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
9 changes: 3 additions & 6 deletions cocos/audio/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void AudioEngine::remove(int audioID)
it->second.profileHelper->audioIDs.remove(audioID);
}
_audioPathIDMap[*it->second.filePath].remove(audioID);
_audioIDInfoMap.erase(audioID);
_audioIDInfoMap.erase(it);
}
}

Expand Down Expand Up @@ -394,16 +394,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()
Expand Down
15 changes: 6 additions & 9 deletions cocos/audio/linux/AudioEngine-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ AudioEngineImpl::AudioEngineImpl()
AudioEngineImpl::~AudioEngineImpl()
{
FMOD_RESULT result;
result = pSystem->close();
ERRCHECKWITHEXIT(result);
result = pSystem->release();
ERRCHECKWITHEXIT(result);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -320,10 +318,9 @@ int AudioEngineImpl::preload(const std::string& filePath, std::function<void(boo
}

int id = static_cast<int>(mapChannelInfo.size()) + 1;
if (mapId.find(filePath) == mapId.end())
mapId.insert({filePath, id});
else
id = mapId.at(filePath);
// std::map::insert returns std::pair<iter, bool>
auto channelInfoIter = mapId.insert({filePath, id});
id = channelInfoIter.first->second;

auto& chanelInfo = mapChannelInfo[id];
chanelInfo.sound = sound;
Expand Down

0 comments on commit d09918b

Please sign in to comment.