Workaround for getting AL_STOPPTED state after alSourcePlay(sourceId). (#18228)

This commit is contained in:
James Chen 2017-09-07 20:29:39 -05:00 committed by minggo
parent ce1c207aee
commit ddb3c49eec
2 changed files with 21 additions and 26 deletions

View File

@ -66,12 +66,14 @@ public:
private:
void _play2d(AudioCache *cache, int audioID);
ALuint findValidSource();
static ALvoid myAlSourceNotificationCallback(ALuint sid, ALuint notificationID, ALvoid* userData);
ALuint _alSources[MAX_AUDIOINSTANCES];
//source,used
std::unordered_map<ALuint, bool> _alSourceUsed;
std::list<ALuint> _unusedSourcesPool;
//filePath,bufferInfo
std::unordered_map<std::string, AudioCache> _audioCaches;

View File

@ -289,7 +289,7 @@ bool AudioEngineImpl::init()
}
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
_alSourceUsed[_alSources[i]] = false;
_unusedSourcesPool.push_back(_alSources[i]);
alSourceAddNotificationExt(_alSources[i], AL_BUFFERS_PROCESSED, myAlSourceNotificationCallback, nullptr);
}
@ -400,17 +400,9 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
return AudioEngine::INVALID_AUDIO_ID;
}
bool sourceFlag = false;
ALuint alSource = 0;
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
alSource = _alSources[i];
if ( !_alSourceUsed[alSource]) {
sourceFlag = true;
break;
}
}
if(!sourceFlag){
ALuint alSource = findValidSource();
if (alSource == AL_INVALID)
{
return AudioEngine::INVALID_AUDIO_ID;
}
@ -434,8 +426,6 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
_audioPlayers[_currentAudioID] = player;
_threadMutex.unlock();
_alSourceUsed[alSource] = true;
audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
if (_lazyInitLoop) {
@ -474,6 +464,18 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
}
}
ALuint AudioEngineImpl::findValidSource()
{
ALuint sourceId = AL_INVALID;
if (!_unusedSourcesPool.empty())
{
sourceId = _unusedSourcesPool.front();
_unusedSourcesPool.pop_front();
}
return sourceId;
}
void AudioEngineImpl::setVolume(int audioID,float volume)
{
auto player = _audioPlayers[audioID];
@ -546,9 +548,6 @@ void AudioEngineImpl::stop(int audioID)
{
auto player = _audioPlayers[audioID];
player->destroy();
//Note: Don't set the flag to false here, it should be set in 'update' function.
// Otherwise, the state got from alSourceState may be wrong
// _alSourceUsed[player->_alSource] = false;
// Call 'update' method to cleanup immediately since the schedule may be cancelled without any notification.
update(0.0f);
@ -560,12 +559,6 @@ void AudioEngineImpl::stopAll()
{
player.second->destroy();
}
//Note: Don't set the flag to false here, it should be set in 'update' function.
// Otherwise, the state got from alSourceState may be wrong
// for(int index = 0; index < MAX_AUDIOINSTANCES; ++index)
// {
// _alSourceUsed[_alSources[index]] = false;
// }
// Call 'update' method to cleanup immediately since the schedule may be cancelled without any notification.
update(0.0f);
@ -662,7 +655,7 @@ void AudioEngineImpl::update(float dt)
it = _audioPlayers.erase(it);
_threadMutex.unlock();
delete player;
_alSourceUsed[alSource] = false;
_unusedSourcesPool.push_back(alSource);
}
else if (player->_ready && sourceState == AL_STOPPED) {
@ -682,7 +675,7 @@ void AudioEngineImpl::update(float dt)
}
delete player;
_alSourceUsed[alSource] = false;
_unusedSourcesPool.push_back(alSource);
}
else{
++it;