axmol/cocos/audio/AudioEngineImpl.cpp

531 lines
15 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2020-06-04 23:11:24 +08:00
Copyright (c) 2018-2020 HALX99.
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#define LOG_TAG "AudioEngineImpl"
#include "platform/CCPlatformConfig.h"
#include "audio/include/AudioEngineImpl.h"
2019-11-23 20:27:39 +08:00
#ifdef OPENAL_PLAIN_INCLUDES
#include "alc.h"
#include "alext.h"
#else
#include "AL/alc.h"
#include "AL/alext.h"
#endif
#include "audio/include/AudioEngine.h"
#include "base/CCDirector.h"
#include "base/CCScheduler.h"
#include "platform/CCFileUtils.h"
#include "audio/include/AudioDecoderManager.h"
#include "audio/include/AudioPlayer.h"
2019-11-23 20:27:39 +08:00
using namespace cocos2d;
static ALCdevice *s_ALDevice = nullptr;
static ALCcontext *s_ALContext = nullptr;
AudioEngineImpl::AudioEngineImpl()
: _lazyInitLoop(true)
, _currentAudioID(0)
, _scheduler(nullptr)
{
}
AudioEngineImpl::~AudioEngineImpl()
{
if (_scheduler != nullptr)
{
_scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this);
}
if (s_ALContext) {
alDeleteSources(MAX_AUDIOINSTANCES, _alSources);
_audioCaches.clear();
alcMakeContextCurrent(nullptr);
alcDestroyContext(s_ALContext);
s_ALContext = nullptr;
}
if (s_ALDevice) {
alcCloseDevice(s_ALDevice);
s_ALDevice = nullptr;
}
AudioDecoderManager::destroy();
}
bool AudioEngineImpl::init()
{
bool ret = false;
do{
s_ALDevice = alcOpenDevice(nullptr);
if (s_ALDevice) {
alGetError();
s_ALContext = alcCreateContext(s_ALDevice, nullptr);
alcMakeContextCurrent(s_ALContext);
alGenSources(MAX_AUDIOINSTANCES, _alSources);
auto alError = alGetError();
if(alError != AL_NO_ERROR)
{
ALOGE("%s:generating sources failed! error = %x\n", __FUNCTION__, alError);
break;
}
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
_unusedSourcesPool.push(_alSources[i]);
2019-11-23 20:27:39 +08:00
}
_scheduler = Director::getInstance()->getScheduler();
ret = AudioDecoderManager::init();
ALOGI("OpenAL was initialized successfully!");
}
}while (false);
return ret;
}
AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function<void(bool)> callback)
{
AudioCache* audioCache = nullptr;
auto it = _audioCaches.find(filePath);
if (it == _audioCaches.end()) {
audioCache = &_audioCaches[filePath];
audioCache->_fileFullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
unsigned int cacheId = audioCache->_id;
auto isCacheDestroyed = audioCache->_isDestroyed;
AudioEngine::addTask([audioCache, cacheId, isCacheDestroyed](){
if (*isCacheDestroyed)
{
ALOGV("AudioCache (id=%u) was destroyed, no need to launch readDataTask.", cacheId);
audioCache->setSkipReadDataTask(true);
return;
}
audioCache->readDataTask(cacheId);
});
}
else {
audioCache = &it->second;
}
if (audioCache && callback)
{
audioCache->addLoadCallback(callback);
}
return audioCache;
}
AUDIO_ID AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume)
2019-11-23 20:27:39 +08:00
{
if (s_ALDevice == nullptr) {
return AudioEngine::INVALID_AUDIO_ID;
}
ALuint alSource = findValidSource();
if (alSource == AL_INVALID)
{
2019-11-23 20:27:39 +08:00
return AudioEngine::INVALID_AUDIO_ID;
}
auto player = new (std::nothrow) AudioPlayer;
if (player == nullptr) {
return AudioEngine::INVALID_AUDIO_ID;
}
player->_alSource = alSource;
player->_loop = loop;
player->_volume = volume;
auto audioCache = preload(filePath, nullptr);
if (audioCache == nullptr) {
delete player;
return AudioEngine::INVALID_AUDIO_ID;
}
player->setCache(audioCache);
_threadMutex.lock();
_audioPlayers.emplace(++_currentAudioID, player);
2019-11-23 20:27:39 +08:00
_threadMutex.unlock();
audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
if (_lazyInitLoop) {
_lazyInitLoop = false;
_scheduler->schedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this, 0.05f, false);
}
return _currentAudioID;
2019-11-23 20:27:39 +08:00
}
void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID)
2019-11-23 20:27:39 +08:00
{
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if (iter == _audioPlayers.end())
return;
auto player = iter->second;
//Note: It maybe in sub thread or main thread :(
2019-11-23 20:27:39 +08:00
if (!*cache->_isDestroyed && cache->_state == AudioCache::State::READY)
{
2020-06-04 23:11:24 +08:00
if (player->play2d()) {
2019-11-23 20:27:39 +08:00
_scheduler->performFunctionInCocosThread([audioID](){
if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) {
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
}
});
}
}
else
{
ALOGD("AudioEngineImpl::_play2d, cache was destroyed or not ready!");
2020-06-04 23:11:24 +08:00
player->_removeByAudioEngine = true;
2019-11-23 20:27:39 +08:00
}
}
ALuint AudioEngineImpl::findValidSource()
{
ALuint sourceId = AL_INVALID;
if (!_unusedSourcesPool.empty())
{
sourceId = _unusedSourcesPool.front();
_unusedSourcesPool.pop();
}
return sourceId;
}
void AudioEngineImpl::setVolume(AUDIO_ID audioID,float volume)
2019-11-23 20:27:39 +08:00
{
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if(iter == _audioPlayers.end())
return;
auto player = iter->second;
lck.unlock();
2019-11-23 20:27:39 +08:00
player->_volume = volume;
if (player->_ready) {
2020-06-04 23:11:24 +08:00
alSourcef(player->_alSource, AL_GAIN, volume);
2019-11-23 20:27:39 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
2020-06-04 23:11:24 +08:00
ALOGE("%s: audio id = " AUDIO_ID_PRID ", error = %x", __FUNCTION__, audioID, error);
2019-11-23 20:27:39 +08:00
}
}
}
void AudioEngineImpl::setLoop(AUDIO_ID audioID, bool loop)
2019-11-23 20:27:39 +08:00
{
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if(iter == _audioPlayers.end())
return;
auto player = iter->second;
lck.unlock();
2019-11-23 20:27:39 +08:00
if (player->_ready) {
if (player->_streamingSource) {
player->setLoop(loop);
} else {
if (loop) {
alSourcei(player->_alSource, AL_LOOPING, AL_TRUE);
} else {
alSourcei(player->_alSource, AL_LOOPING, AL_FALSE);
}
auto error = alGetError();
if (error != AL_NO_ERROR) {
2020-06-04 23:11:24 +08:00
ALOGE("%s: audio id = " AUDIO_ID_PRID ", error = %x", __FUNCTION__, audioID, error);
2019-11-23 20:27:39 +08:00
}
}
}
else {
player->_loop = loop;
}
}
bool AudioEngineImpl::pause(AUDIO_ID audioID)
2019-11-23 20:27:39 +08:00
{
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if(iter == _audioPlayers.end())
return false;
auto player = iter->second;
lck.unlock();
2019-11-23 20:27:39 +08:00
bool ret = true;
2020-06-04 23:11:24 +08:00
alSourcePause(player->_alSource);
2019-11-23 20:27:39 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
ret = false;
2020-06-04 23:11:24 +08:00
ALOGE("%s: audio id = " AUDIO_ID_PRID ", error = %x\n", __FUNCTION__, audioID, error);
2019-11-23 20:27:39 +08:00
}
return ret;
}
bool AudioEngineImpl::resume(AUDIO_ID audioID)
2019-11-23 20:27:39 +08:00
{
bool ret = true;
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if(iter == _audioPlayers.end())
return false;
auto player = iter->second;
lck.unlock();
alSourcePlay(player->_alSource);
2019-11-23 20:27:39 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
ret = false;
2020-06-04 23:11:24 +08:00
ALOGE("%s: audio id = " AUDIO_ID_PRID ", error = %x\n", __FUNCTION__, audioID, error);
2019-11-23 20:27:39 +08:00
}
return ret;
}
void AudioEngineImpl::stop(AUDIO_ID audioID)
2019-11-23 20:27:39 +08:00
{
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if(iter == _audioPlayers.end())
return;
auto player = iter->second;
2019-11-23 20:27:39 +08:00
player->destroy();
// Call 'update' method to cleanup immediately since the schedule may be cancelled without any notification.
2020-06-04 23:11:24 +08:00
_updateLocked(0.0f);
2019-11-23 20:27:39 +08:00
}
void AudioEngineImpl::stopAll()
{
2020-06-04 23:11:24 +08:00
std::lock_guard<std::recursive_mutex> lck(_threadMutex);
2019-11-23 20:27:39 +08:00
for(auto&& player : _audioPlayers)
{
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.
2020-06-04 23:11:24 +08:00
_updateLocked(0.0f);
2019-11-23 20:27:39 +08:00
}
float AudioEngineImpl::getDuration(AUDIO_ID audioID)
2019-11-23 20:27:39 +08:00
{
2020-06-04 23:11:24 +08:00
std::lock_guard<std::recursive_mutex> lck(_threadMutex);
auto it = _audioPlayers.find(audioID);
if (it != _audioPlayers.end()) {
auto player = it->second;
if (player->_ready) {
return player->_audioCache->_duration;
}
2019-11-23 20:27:39 +08:00
}
2020-06-04 23:11:24 +08:00
return AudioEngine::TIME_UNKNOWN;
2019-11-23 20:27:39 +08:00
}
float AudioEngineImpl::getCurrentTime(AUDIO_ID audioID)
2019-11-23 20:27:39 +08:00
{
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto it = _audioPlayers.find(audioID);
if (it == _audioPlayers.end())
return 0.0f;
2019-11-23 20:27:39 +08:00
float ret = 0.0f;
2020-06-04 23:11:24 +08:00
auto player = it->second;
if (player->_ready) {
2019-11-23 20:27:39 +08:00
if (player->_streamingSource) {
ret = player->getTime();
2020-06-04 23:11:24 +08:00
}
else {
2019-11-23 20:27:39 +08:00
alGetSourcef(player->_alSource, AL_SEC_OFFSET, &ret);
auto error = alGetError();
if (error != AL_NO_ERROR) {
2020-06-04 23:11:24 +08:00
ALOGE("%s, audio id:" AUDIO_ID_PRID ",error code:%x", __FUNCTION__, audioID, error);
2019-11-23 20:27:39 +08:00
}
}
}
return ret;
}
bool AudioEngineImpl::setCurrentTime(AUDIO_ID audioID, float time)
2019-11-23 20:27:39 +08:00
{
bool ret = false;
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if(iter == _audioPlayers.end())
return false;
auto player = iter->second;
2019-11-23 20:27:39 +08:00
do {
if (!player->_ready) {
break;
}
if (player->_streamingSource) {
ret = player->setTime(time);
break;
}
else {
if (player->_audioCache->_framesRead != player->_audioCache->_totalFrames &&
(time * player->_audioCache->_sampleRate) > player->_audioCache->_framesRead) {
2020-06-04 23:11:24 +08:00
ALOGE("%s: audio id = " AUDIO_ID_PRID, __FUNCTION__, audioID);
2019-11-23 20:27:39 +08:00
break;
}
alSourcef(player->_alSource, AL_SEC_OFFSET, time);
auto error = alGetError();
if (error != AL_NO_ERROR) {
2020-06-04 23:11:24 +08:00
ALOGE("%s: audio id = " AUDIO_ID_PRID ", error = %x", __FUNCTION__, audioID, error);
2019-11-23 20:27:39 +08:00
}
ret = true;
}
} while (0);
return ret;
}
void AudioEngineImpl::setFinishCallback(AUDIO_ID audioID, const std::function<void (AUDIO_ID, const std::string &)> &callback)
2019-11-23 20:27:39 +08:00
{
2020-06-04 23:11:24 +08:00
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if(iter == _audioPlayers.end())
return;
auto player = iter->second;
lck.unlock();
player->_finishCallbak = callback;
2019-11-23 20:27:39 +08:00
}
void AudioEngineImpl::update(float dt)
2020-06-04 23:11:24 +08:00
{
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
_updateLocked(dt);
}
void AudioEngineImpl::_updateLocked(float dt)
2019-11-23 20:27:39 +08:00
{
AUDIO_ID audioID;
2019-11-23 20:27:39 +08:00
AudioPlayer* player;
ALuint alSource;
// ALOGV("AudioPlayer count: %d", (int)_audioPlayers.size());
for (auto it = _audioPlayers.begin(); it != _audioPlayers.end(); ) {
audioID = it->first;
player = it->second;
alSource = player->_alSource;
if (player->_removeByAudioEngine)
{
AudioEngine::remove(audioID);
2019-11-23 20:27:39 +08:00
it = _audioPlayers.erase(it);
delete player;
_unusedSourcesPool.push(alSource);
2019-11-23 20:27:39 +08:00
}
else if (player->_ready && player->isFinished()) {
2019-11-23 20:27:39 +08:00
std::string filePath;
if (player->_finishCallbak) {
auto& audioInfo = AudioEngine::_audioIDInfoMap[audioID];
filePath = audioInfo.filePath;
2019-11-23 20:27:39 +08:00
}
AudioEngine::remove(audioID);
it = _audioPlayers.erase(it);
if (player->_finishCallbak) {
/// ###IMPORTANT: don't call immidiately, because at callback, user-end may play a new audio
/// cause _audioPlayers' iterator goan to invalid.
_finishCallbacks.push_back([finishCallback = std::move(player->_finishCallbak), audioID, filePath = std::move(filePath)](){
finishCallback(audioID, filePath); // FIXME: callback will delay 50ms
});
2019-11-23 20:27:39 +08:00
}
// clear cache when audio player finsihed properly
player->setCache(nullptr);
2019-11-23 20:27:39 +08:00
delete player;
_unusedSourcesPool.push(alSource);
2019-11-23 20:27:39 +08:00
}
else{
++it;
}
}
if(_audioPlayers.empty()) {
2019-11-23 20:27:39 +08:00
_lazyInitLoop = true;
_scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this);
}
if (!_finishCallbacks.empty()) {
for (auto& finishCallback : _finishCallbacks)
finishCallback();
_finishCallbacks.clear();
}
2019-11-23 20:27:39 +08:00
}
void AudioEngineImpl::uncache(const std::string &filePath)
{
_audioCaches.erase(filePath);
}
void AudioEngineImpl::uncacheAll()
{
// prevent player hold invalid AudioCache* pointer, since all audio caches purged
for (auto& player : _audioPlayers)
player.second->setCache(nullptr);
2019-11-23 20:27:39 +08:00
_audioCaches.clear();
}