From e060ea3e7796197a97f995fd6a17b5ed7d7e374a Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 20 Jun 2017 10:31:19 +0800 Subject: [PATCH] fixed #17800: [iOS] Streaming audio (normally a background music) maybe cut and may not be looped. Sync PR https://github.com/cocos2d/cocos2d-x/pull/17947 --- native/cocos/audio/apple/AudioEngine-inl.h | 1 + native/cocos/audio/apple/AudioEngine-inl.mm | 45 ++++++++++++++++++-- native/cocos/audio/apple/AudioPlayer.h | 10 +++-- native/cocos/audio/apple/AudioPlayer.mm | 18 ++++++-- native/cocos/audio/win32/AudioDecoderMp3.cpp | 2 +- native/cocos/audio/win32/AudioDecoderOgg.cpp | 2 +- 6 files changed, 66 insertions(+), 12 deletions(-) diff --git a/native/cocos/audio/apple/AudioEngine-inl.h b/native/cocos/audio/apple/AudioEngine-inl.h index 3c88ee7071..2add9ebf42 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.h +++ b/native/cocos/audio/apple/AudioEngine-inl.h @@ -66,6 +66,7 @@ public: private: void _play2d(AudioCache *cache, int audioID); + static ALvoid myAlSourceNotificationCallback(ALuint sid, ALuint notificationID, ALvoid* userData); ALuint _alSources[MAX_AUDIOINSTANCES]; diff --git a/native/cocos/audio/apple/AudioEngine-inl.mm b/native/cocos/audio/apple/AudioEngine-inl.mm index cd29c0b54f..06c9992d2e 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.mm +++ b/native/cocos/audio/apple/AudioEngine-inl.mm @@ -41,8 +41,27 @@ using namespace cocos2d; using namespace cocos2d::experimental; -static ALCdevice *s_ALDevice = nullptr; -static ALCcontext *s_ALContext = nullptr; +static ALCdevice* s_ALDevice = nullptr; +static ALCcontext* s_ALContext = nullptr; +static AudioEngineImpl* s_instance = nullptr; + +typedef ALvoid (*alSourceNotificationProc)(ALuint sid, ALuint notificationID, ALvoid* userData); +typedef ALenum (*alSourceAddNotificationProcPtr)(ALuint sid, ALuint notificationID, alSourceNotificationProc notifyProc, ALvoid* userData); +static ALenum alSourceAddNotificationExt(ALuint sid, ALuint notificationID, alSourceNotificationProc notifyProc, ALvoid* userData) +{ + static alSourceAddNotificationProcPtr proc = nullptr; + + if (proc == nullptr) + { + proc = (alSourceAddNotificationProcPtr)alcGetProcAddress(nullptr, "alSourceAddNotification"); + } + + if (proc) + { + return proc(sid, notificationID, notifyProc, userData); + } + return AL_INVALID_VALUE; +} #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS @interface AudioEngineSessionHandler : NSObject @@ -189,12 +208,29 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio static id s_AudioEngineSessionHandler = nullptr; #endif +ALvoid AudioEngineImpl::myAlSourceNotificationCallback(ALuint sid, ALuint notificationID, ALvoid* userData) +{ + // Currently, we only care about AL_BUFFERS_PROCESSED event + if (notificationID != AL_BUFFERS_PROCESSED) + return; + + AudioPlayer* player = nullptr; + for (const auto& e : s_instance->_audioPlayers) + { + player = e.second; + if (player->_alSource == sid && player->_streamingSource) + { + player->wakeupRotateThread(); + } + } +} + AudioEngineImpl::AudioEngineImpl() : _lazyInitLoop(true) , _currentAudioID(0) , _scheduler(nullptr) { - + s_instance = this; } AudioEngineImpl::~AudioEngineImpl() @@ -219,6 +255,7 @@ AudioEngineImpl::~AudioEngineImpl() #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS [s_AudioEngineSessionHandler release]; #endif + s_instance = nullptr; } bool AudioEngineImpl::init() @@ -245,6 +282,7 @@ bool AudioEngineImpl::init() for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) { _alSourceUsed[_alSources[i]] = false; + alSourceAddNotificationExt(_alSources[i], AL_BUFFERS_PROCESSED, myAlSourceNotificationCallback, nullptr); } // fixed #16170: Random crash in alGenBuffers(AudioCache::readDataTask) at startup @@ -308,7 +346,6 @@ bool AudioEngineImpl::init() // ================ Workaround end ================ // - _scheduler = Director::getInstance()->getScheduler(); ret = true; ALOGI("OpenAL was initialized successfully!"); diff --git a/native/cocos/audio/apple/AudioPlayer.h b/native/cocos/audio/apple/AudioPlayer.h index b21a4ccfb8..369540d5e2 100644 --- a/native/cocos/audio/apple/AudioPlayer.h +++ b/native/cocos/audio/apple/AudioPlayer.h @@ -27,12 +27,14 @@ #include "platform/CCPlatformConfig.h" #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC +#include "platform/CCPlatformMacros.h" +#include "audio/apple/AudioMacros.h" + #include #include #include #include -#import -#include "platform/CCPlatformMacros.h" +#include NS_CC_BEGIN namespace experimental{ @@ -57,6 +59,7 @@ protected: void setCache(AudioCache* cache); void rotateBufferThread(int offsetFrame); bool play2d(); + void wakeupRotateThread(); AudioCache* _audioCache; @@ -72,12 +75,13 @@ 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; bool _timeDirty; bool _isRotateThreadExited; + std::atomic_bool _needWakeupRotateThread; std::mutex _play2dMutex; diff --git a/native/cocos/audio/apple/AudioPlayer.mm b/native/cocos/audio/apple/AudioPlayer.mm index 22409143ea..22c6b05aa5 100644 --- a/native/cocos/audio/apple/AudioPlayer.mm +++ b/native/cocos/audio/apple/AudioPlayer.mm @@ -59,6 +59,7 @@ AudioPlayer::AudioPlayer() , _rotateBufferThread(nullptr) , _timeDirty(false) , _isRotateThreadExited(false) +, _needWakeupRotateThread(false) , _id(++__idIndex) { memset(_bufferIds, 0, sizeof(_bufferIds)); @@ -71,7 +72,7 @@ AudioPlayer::~AudioPlayer() if (_streamingSource) { - alDeleteBuffers(3, _bufferIds); + alDeleteBuffers(QUEUEBUFFER_NUM, _bufferIds); } } @@ -172,7 +173,7 @@ bool AudioPlayer::play2d() } else { - alGenBuffers(3, _bufferIds); + alGenBuffers(QUEUEBUFFER_NUM, _bufferIds); auto alError = alGetError(); if (alError == AL_NO_ERROR) @@ -302,7 +303,12 @@ void AudioPlayer::rotateBufferThread(int offsetFrame) break; } - _sleepCondition.wait_for(lk,std::chrono::milliseconds(75)); + if (!_needWakeupRotateThread) + { + _sleepCondition.wait_for(lk,std::chrono::milliseconds(75)); + } + + _needWakeupRotateThread = false; } } while(false); @@ -313,6 +319,12 @@ void AudioPlayer::rotateBufferThread(int offsetFrame) _isRotateThreadExited = true; } +void AudioPlayer::wakeupRotateThread() +{ + _needWakeupRotateThread = true; + _sleepCondition.notify_all(); +} + bool AudioPlayer::setLoop(bool loop) { if (!_isDestroyed ) { diff --git a/native/cocos/audio/win32/AudioDecoderMp3.cpp b/native/cocos/audio/win32/AudioDecoderMp3.cpp index add65bf987..63e9a5fcbb 100644 --- a/native/cocos/audio/win32/AudioDecoderMp3.cpp +++ b/native/cocos/audio/win32/AudioDecoderMp3.cpp @@ -91,7 +91,7 @@ namespace cocos2d { namespace experimental { break; } - if (mpg123_open(_mpg123handle, fullPath.c_str()) != MPG123_OK + if (mpg123_open(_mpg123handle, FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str()) != MPG123_OK || mpg123_getformat(_mpg123handle, &rate, &channel, &mp3Encoding) != MPG123_OK) { ALOGE("Trouble with mpg123: %s\n", mpg123_strerror(_mpg123handle) ); diff --git a/native/cocos/audio/win32/AudioDecoderOgg.cpp b/native/cocos/audio/win32/AudioDecoderOgg.cpp index edfa82b5f2..822c73004e 100644 --- a/native/cocos/audio/win32/AudioDecoderOgg.cpp +++ b/native/cocos/audio/win32/AudioDecoderOgg.cpp @@ -42,7 +42,7 @@ namespace cocos2d { namespace experimental { bool AudioDecoderOgg::open(const char* path) { std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path); - if (0 == ov_fopen(fullPath.c_str(), &_vf)) + if (0 == ov_fopen(FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str(), &_vf)) { // header vorbis_info* vi = ov_info(&_vf, -1);