From 9c0ef83757e435b30990e3bf9b56a2803daf20f5 Mon Sep 17 00:00:00 2001 From: halx99 Date: Sat, 30 Nov 2019 06:55:32 +0800 Subject: [PATCH] optimize --- cocos/audio/AudioPlayer.cpp | 5 +++-- cocos/audio/apple/AudioEngineImpl.mm | 8 ++++---- cocos/audio/apple/AudioPlayer.h | 4 +++- cocos/audio/apple/AudioPlayer.mm | 29 ++++++++++++++++++++++++++++ cocos/audio/include/AudioPlayer.h | 2 +- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/cocos/audio/AudioPlayer.cpp b/cocos/audio/AudioPlayer.cpp index babafdbc8e..54a4e37883 100644 --- a/cocos/audio/AudioPlayer.cpp +++ b/cocos/audio/AudioPlayer.cpp @@ -241,11 +241,12 @@ bool AudioPlayer::play2d() void AudioPlayer::rotateBufferThread(int offsetFrame) { char* tmpBuffer = nullptr; - AudioDecoder* decoder = AudioDecoderManager::createDecoder(_audioCache->_fileFullPath.c_str()); + auto& fullPath = _audioCache->_fileFullPath; + AudioDecoder* decoder = AudioDecoderManager::createDecoder(fullPath); long long rotateSleepTime = static_cast(QUEUEBUFFER_TIME_STEP * 1000) / 2; do { - BREAK_IF(decoder == nullptr || !decoder->open(_audioCache->_fileFullPath.c_str())); + BREAK_IF(decoder == nullptr || !decoder->open(fullPath)); uint32_t framesRead = 0; const uint32_t framesToRead = _audioCache->_queBufferFrames; diff --git a/cocos/audio/apple/AudioEngineImpl.mm b/cocos/audio/apple/AudioEngineImpl.mm index cd1765e0a9..d5dc8c002d 100644 --- a/cocos/audio/apple/AudioEngineImpl.mm +++ b/cocos/audio/apple/AudioEngineImpl.mm @@ -448,10 +448,10 @@ AUDIO_ID AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float v void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID) { - _threadMutex.lock(); - //Note: It maybe in sub thread or main thread :( + //Note: It may bn in sub thread or main thread :( if (!*cache->_isDestroyed && cache->_state == AudioCache::State::READY) { + _threadMutex.lock(); auto playerIt = _audioPlayers.find(audioID); if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) { _scheduler->performFunctionInCocosThread([audioID](){ @@ -461,6 +461,7 @@ void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID) } }); } + _threadMutex.unlock(); } else { @@ -471,7 +472,6 @@ void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID) iter->second->_removeByAudioEngine = true; } } - _threadMutex.unlock(); } ALuint AudioEngineImpl::findValidSource() @@ -711,7 +711,7 @@ void AudioEngineImpl::update(float dt) delete player; _unusedSourcesPool.push_back(alSource); } - else if (player->_ready && sourceState == AL_STOPPED) { + else if (player->_ready && player->isFinished()) { std::string filePath; if (player->_finishCallbak) { diff --git a/cocos/audio/apple/AudioPlayer.h b/cocos/audio/apple/AudioPlayer.h index bc365c0630..55a0ce3dbc 100644 --- a/cocos/audio/apple/AudioPlayer.h +++ b/cocos/audio/apple/AudioPlayer.h @@ -55,6 +55,8 @@ public: float getTime() { return _currTime;} bool setLoop(bool loop); + bool isFinished() const; + protected: void setCache(AudioCache* cache); void rotateBufferThread(int offsetFrame); @@ -65,7 +67,7 @@ protected: float _volume; bool _loop; - std::function _finishCallbak; + std::function _finishCallbak; bool _isDestroyed; bool _removeByAudioEngine; diff --git a/cocos/audio/apple/AudioPlayer.mm b/cocos/audio/apple/AudioPlayer.mm index 7d609d25ef..31f4fbfe96 100644 --- a/cocos/audio/apple/AudioPlayer.mm +++ b/cocos/audio/apple/AudioPlayer.mm @@ -327,6 +327,25 @@ void AudioPlayer::rotateBufferThread(int offsetFrame) alSourceQueueBuffers(_alSource, 1, &bid); } } + /* Make sure the source hasn't underrun */ + else if (sourceState != AL_PAUSED) + { + ALint queued; + + /* If no buffers are queued, playback is finished */ + alGetSourcei(_alSource, AL_BUFFERS_QUEUED, &queued); + if (queued == 0) { + needToExitThread = true; + } + else { + alSourcePlay(_alSource); + if (alGetError() != AL_NO_ERROR) + { + ALOGE("Error restarting playback!"); + needToExitThread = true; + } + } + } std::unique_lock lk(_sleepMutex); if (_isDestroyed || needToExitThread) { @@ -357,6 +376,16 @@ void AudioPlayer::wakeupRotateThread() _sleepCondition.notify_all(); } +bool AudioPlayer::isFinished() const +{ + if(_streamingSource) return _isRotateThreadExited; + else { + ALint sourceState; + alGetSourcei(_alSource, AL_SOURCE_STATE, &sourceState); + return sourceState == AL_STOPPED; + } +} + bool AudioPlayer::setLoop(bool loop) { if (!_isDestroyed ) { diff --git a/cocos/audio/include/AudioPlayer.h b/cocos/audio/include/AudioPlayer.h index 163f6d2cb2..a37eb58520 100644 --- a/cocos/audio/include/AudioPlayer.h +++ b/cocos/audio/include/AudioPlayer.h @@ -78,7 +78,7 @@ protected: //play by circular buffer float _currTime; bool _streamingSource; - ALuint _bufferIds[3]; + ALuint _bufferIds[QUEUEBUFFER_NUM]; std::thread* _rotateBufferThread; std::condition_variable _sleepCondition; std::mutex _sleepMutex;