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: private:
void _play2d(AudioCache *cache, int audioID); void _play2d(AudioCache *cache, int audioID);
ALuint findValidSource();
static ALvoid myAlSourceNotificationCallback(ALuint sid, ALuint notificationID, ALvoid* userData); static ALvoid myAlSourceNotificationCallback(ALuint sid, ALuint notificationID, ALvoid* userData);
ALuint _alSources[MAX_AUDIOINSTANCES]; ALuint _alSources[MAX_AUDIOINSTANCES];
//source,used //source,used
std::unordered_map<ALuint, bool> _alSourceUsed; std::list<ALuint> _unusedSourcesPool;
//filePath,bufferInfo //filePath,bufferInfo
std::unordered_map<std::string, AudioCache> _audioCaches; std::unordered_map<std::string, AudioCache> _audioCaches;

View File

@ -289,7 +289,7 @@ bool AudioEngineImpl::init()
} }
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) { 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); 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; return AudioEngine::INVALID_AUDIO_ID;
} }
bool sourceFlag = false; ALuint alSource = findValidSource();
ALuint alSource = 0; if (alSource == AL_INVALID)
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) { {
alSource = _alSources[i];
if ( !_alSourceUsed[alSource]) {
sourceFlag = true;
break;
}
}
if(!sourceFlag){
return AudioEngine::INVALID_AUDIO_ID; return AudioEngine::INVALID_AUDIO_ID;
} }
@ -434,8 +426,6 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
_audioPlayers[_currentAudioID] = player; _audioPlayers[_currentAudioID] = player;
_threadMutex.unlock(); _threadMutex.unlock();
_alSourceUsed[alSource] = true;
audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID)); audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
if (_lazyInitLoop) { 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) void AudioEngineImpl::setVolume(int audioID,float volume)
{ {
auto player = _audioPlayers[audioID]; auto player = _audioPlayers[audioID];
@ -546,9 +548,6 @@ void AudioEngineImpl::stop(int audioID)
{ {
auto player = _audioPlayers[audioID]; auto player = _audioPlayers[audioID];
player->destroy(); 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. // Call 'update' method to cleanup immediately since the schedule may be cancelled without any notification.
update(0.0f); update(0.0f);
@ -560,12 +559,6 @@ void AudioEngineImpl::stopAll()
{ {
player.second->destroy(); 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. // Call 'update' method to cleanup immediately since the schedule may be cancelled without any notification.
update(0.0f); update(0.0f);
@ -662,7 +655,7 @@ void AudioEngineImpl::update(float dt)
it = _audioPlayers.erase(it); it = _audioPlayers.erase(it);
_threadMutex.unlock(); _threadMutex.unlock();
delete player; delete player;
_alSourceUsed[alSource] = false; _unusedSourcesPool.push_back(alSource);
} }
else if (player->_ready && sourceState == AL_STOPPED) { else if (player->_ready && sourceState == AL_STOPPED) {
@ -682,7 +675,7 @@ void AudioEngineImpl::update(float dt)
} }
delete player; delete player;
_alSourceUsed[alSource] = false; _unusedSourcesPool.push_back(alSource);
} }
else{ else{
++it; ++it;