123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- /****************************************************************************
- Copyright (c) 2016-2017 Chukong Technologies Inc.
- 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 "AudioPlayerProvider"
- #include "audio/android/AudioPlayerProvider.h"
- #include "audio/android/UrlAudioPlayer.h"
- #include "audio/android/PcmAudioPlayer.h"
- #include "audio/android/AudioDecoder.h"
- #include "audio/android/AudioDecoderProvider.h"
- #include "audio/android/AudioMixerController.h"
- #include "audio/android/PcmAudioService.h"
- #include "audio/android/CCThreadPool.h"
- #include "audio/android/ICallerThreadUtils.h"
- #include "audio/android/utils/Utils.h"
- #include <sys/system_properties.h>
- #include <stdlib.h>
- #include <algorithm> // for std::find_if
- namespace cocos2d { namespace experimental {
- static int getSystemAPILevel()
- {
- static int __systemApiLevel = -1;
- if (__systemApiLevel > 0)
- {
- return __systemApiLevel;
- }
- int apiLevel = getSDKVersion();
- if (apiLevel > 0)
- {
- ALOGD("Android API level: %d", apiLevel);
- }
- else
- {
- ALOGE("Fail to get Android API level!");
- }
- __systemApiLevel = apiLevel;
- return apiLevel;
- }
- struct AudioFileIndicator
- {
- std::string extension;
- int smallSizeIndicator;
- };
- static AudioFileIndicator __audioFileIndicator[] = {
- {"default", 128000}, // If we could not handle the audio format, return default value, the position should be first.
- {".wav", 1024000},
- {".ogg", 128000},
- {".mp3", 160000}
- };
- AudioPlayerProvider::AudioPlayerProvider(SLEngineItf engineItf, SLObjectItf outputMixObject,
- int deviceSampleRate, int bufferSizeInFrames,
- const FdGetterCallback &fdGetterCallback,
- ICallerThreadUtils* callerThreadUtils)
- : _engineItf(engineItf), _outputMixObject(outputMixObject),
- _deviceSampleRate(deviceSampleRate), _bufferSizeInFrames(bufferSizeInFrames),
- _fdGetterCallback(fdGetterCallback), _callerThreadUtils(callerThreadUtils),
- _pcmAudioService(nullptr), _mixController(nullptr),
- _threadPool(ThreadPool::newCachedThreadPool(1, 8, 5, 2, 2))
- {
- ALOGI("deviceSampleRate: %d, bufferSizeInFrames: %d", _deviceSampleRate, _bufferSizeInFrames);
- if (getSystemAPILevel() >= 17)
- {
- _mixController = new (std::nothrow) AudioMixerController(_bufferSizeInFrames, _deviceSampleRate, 2);
- _mixController->init();
- _pcmAudioService = new (std::nothrow) PcmAudioService(engineItf, outputMixObject);
- _pcmAudioService->init(_mixController, 2, deviceSampleRate, bufferSizeInFrames * 2);
- }
- ALOG_ASSERT(callerThreadUtils != nullptr, "Caller thread utils parameter should not be nullptr!");
- }
- AudioPlayerProvider::~AudioPlayerProvider()
- {
- ALOGV("~AudioPlayerProvider()");
- UrlAudioPlayer::stopAll();
- SL_SAFE_DELETE(_pcmAudioService);
- SL_SAFE_DELETE(_mixController);
- SL_SAFE_DELETE(_threadPool);
- }
- IAudioPlayer *AudioPlayerProvider::getAudioPlayer(const std::string &audioFilePath)
- {
- // Pcm data decoding by OpenSLES API only supports in API level 17 and later.
- if (getSystemAPILevel() < 17)
- {
- AudioFileInfo info = getFileInfo(audioFilePath);
- if (info.isValid())
- {
- return createUrlAudioPlayer(info);
- }
- return nullptr;
- }
- IAudioPlayer *player = nullptr;
- _pcmCacheMutex.lock();
- auto iter = _pcmCache.find(audioFilePath);
- if (iter != _pcmCache.end())
- {// Found pcm cache means it was used to be a PcmAudioService
- PcmData pcmData = iter->second;
- _pcmCacheMutex.unlock();
- player = obtainPcmAudioPlayer(audioFilePath, pcmData);
- ALOGV_IF(player == nullptr, "%s, %d: player is nullptr, path: %s", __FUNCTION__, __LINE__, audioFilePath.c_str());
- }
- else
- {
- _pcmCacheMutex.unlock();
- // Check audio file size to determine to use a PcmAudioService or UrlAudioPlayer,
- // generally PcmAudioService is used for playing short audio like game effects while
- // playing background music uses UrlAudioPlayer
- AudioFileInfo info = getFileInfo(audioFilePath);
- if (info.isValid())
- {
- if (isSmallFile(info))
- {
- // Put an empty lambda to preloadEffect since we only want the future object to get PcmData
- auto pcmData = std::make_shared<PcmData>();
- auto isSucceed = std::make_shared<bool>(false);
- auto isReturnFromCache = std::make_shared<bool>(false);
- auto isPreloadFinished = std::make_shared<bool>(false);
- std::thread::id threadId = std::this_thread::get_id();
- void* infoPtr = &info;
- std::string url = info.url;
- preloadEffect(info, [infoPtr, url, threadId, pcmData, isSucceed, isReturnFromCache, isPreloadFinished](bool succeed, PcmData data){
- // If the callback is in the same thread as caller's, it means that we found it
- // in the cache
- *isReturnFromCache = std::this_thread::get_id() == threadId;
- *pcmData = data;
- *isSucceed = succeed;
- *isPreloadFinished = true;
- ALOGV("FileInfo (%p), Set isSucceed flag: %d, path: %s", infoPtr, succeed, url.c_str());
- }, true);
- if (!*isReturnFromCache && !*isPreloadFinished)
- {
- std::unique_lock<std::mutex> lk(_preloadWaitMutex);
- // Wait for 2 seconds for the decoding in sub thread finishes.
- ALOGV("FileInfo (%p), Waiting preload (%s) to finish ...", &info, audioFilePath.c_str());
- _preloadWaitCond.wait_for(lk, std::chrono::seconds(2));
- ALOGV("FileInfo (%p), Waitup preload (%s) ...", &info, audioFilePath.c_str());
- }
- if (*isSucceed)
- {
- if (pcmData->isValid())
- {
- player = obtainPcmAudioPlayer(info.url, *pcmData);
- ALOGV_IF(player == nullptr, "%s, %d: player is nullptr, path: %s", __FUNCTION__, __LINE__, audioFilePath.c_str());
- }
- else
- {
- ALOGE("pcm data is invalid, path: %s", audioFilePath.c_str());
- }
- }
- else
- {
- ALOGE("FileInfo (%p), preloadEffect (%s) failed", &info, audioFilePath.c_str());
- }
- }
- else
- {
- player = createUrlAudioPlayer(info);
- ALOGV_IF(player == nullptr, "%s, %d: player is nullptr, path: %s", __FUNCTION__, __LINE__, audioFilePath.c_str());
- }
- }
- else
- {
- ALOGE("File info is invalid, path: %s", audioFilePath.c_str());
- }
- }
- ALOGV_IF(player == nullptr, "%s, %d return nullptr", __FUNCTION__, __LINE__);
- return player;
- }
- void AudioPlayerProvider::preloadEffect(const std::string &audioFilePath, const PreloadCallback& cb)
- {
- // Pcm data decoding by OpenSLES API only supports in API level 17 and later.
- if (getSystemAPILevel() < 17)
- {
- PcmData data;
- cb(true, data);
- return;
- }
- _pcmCacheMutex.lock();
- auto&& iter = _pcmCache.find(audioFilePath);
- if (iter != _pcmCache.end())
- {
- ALOGV("preload return from cache: (%s)", audioFilePath.c_str());
- _pcmCacheMutex.unlock();
- cb(true, iter->second);
- return;
- }
- _pcmCacheMutex.unlock();
- auto info = getFileInfo(audioFilePath);
- preloadEffect(info, [this, cb, audioFilePath](bool succeed, PcmData data){
- _callerThreadUtils->performFunctionInCallerThread([this, succeed, data, cb](){
- cb(succeed, data);
- });
- }, false);
- }
- // Used internally
- void AudioPlayerProvider::preloadEffect(const AudioFileInfo &info, const PreloadCallback& cb, bool isPreloadInPlay2d)
- {
- PcmData pcmData;
- if (!info.isValid())
- {
- cb(false, pcmData);
- return;
- }
- if (isSmallFile(info))
- {
- std::string audioFilePath = info.url;
- // 1. First time check, if it wasn't in the cache, goto 2 step
- _pcmCacheMutex.lock();
- auto&& iter = _pcmCache.find(audioFilePath);
- if (iter != _pcmCache.end())
- {
- ALOGV("1. Return pcm data from cache, url: %s", info.url.c_str());
- _pcmCacheMutex.unlock();
- cb(true, iter->second);
- return;
- }
- _pcmCacheMutex.unlock();
- {
- // 2. Check whether the audio file is being preloaded, if it has been removed from map just now,
- // goto step 3
- std::lock_guard<std::mutex> lk(_preloadCallbackMutex);
- auto&& preloadIter = _preloadCallbackMap.find(audioFilePath);
- if (preloadIter != _preloadCallbackMap.end())
- {
- ALOGV("audio (%s) is being preloaded, add to callback vector!", audioFilePath.c_str());
- PreloadCallbackParam param;
- param.callback = cb;
- param.isPreloadInPlay2d = isPreloadInPlay2d;
- preloadIter->second.push_back(std::move(param));
- return;
- }
- // 3. Check it in cache again. If it has been removed from map just now, the file is in
- // the cache absolutely.
- _pcmCacheMutex.lock();
- auto&& iter = _pcmCache.find(audioFilePath);
- if (iter != _pcmCache.end())
- {
- ALOGV("2. Return pcm data from cache, url: %s", info.url.c_str());
- _pcmCacheMutex.unlock();
- cb(true, iter->second);
- return;
- }
- _pcmCacheMutex.unlock();
- PreloadCallbackParam param;
- param.callback = cb;
- param.isPreloadInPlay2d = isPreloadInPlay2d;
- std::vector<PreloadCallbackParam> callbacks;
- callbacks.push_back(std::move(param));
- _preloadCallbackMap.insert(std::make_pair(audioFilePath, std::move(callbacks)));
- }
- _threadPool->pushTask([this, audioFilePath](int tid) {
- ALOGV("AudioPlayerProvider::preloadEffect: (%s)", audioFilePath.c_str());
- PcmData d;
- AudioDecoder* decoder = AudioDecoderProvider::createAudioDecoder(_engineItf, audioFilePath, _bufferSizeInFrames, _deviceSampleRate, _fdGetterCallback);
- bool ret = decoder != nullptr && decoder->start();
- if (ret)
- {
- d = decoder->getResult();
- std::lock_guard<std::mutex> lk(_pcmCacheMutex);
- _pcmCache.insert(std::make_pair(audioFilePath, d));
- }
- else
- {
- ALOGE("decode (%s) failed!", audioFilePath.c_str());
- }
- ALOGV("decode %s", (ret ? "succeed" : "failed"));
- std::lock_guard<std::mutex> lk(_preloadCallbackMutex);
- auto&& preloadIter = _preloadCallbackMap.find(audioFilePath);
- if (preloadIter != _preloadCallbackMap.end())
- {
- auto&& params = preloadIter->second;
- ALOGV("preload (%s) callback count: %d", audioFilePath.c_str(), (int)params.size());
- PcmData result = decoder->getResult();
- for (auto&& param : params)
- {
- param.callback(ret, result);
- if (param.isPreloadInPlay2d)
- {
- _preloadWaitCond.notify_one();
- }
- }
- _preloadCallbackMap.erase(preloadIter);
- }
- AudioDecoderProvider::destroyAudioDecoder(&decoder);
- });
- }
- else
- {
- ALOGV("File (%s) is too large, ignore preload!", info.url.c_str());
- cb(true, pcmData);
- }
- }
- AudioPlayerProvider::AudioFileInfo AudioPlayerProvider::getFileInfo(
- const std::string &audioFilePath)
- {
- AudioFileInfo info;
- long fileSize = 0;
- off_t start = 0, length = 0;
- int assetFd = -1;
- if (audioFilePath[0] != '/')
- {
- std::string relativePath;
- size_t position = audioFilePath.find("assets/");
- if (0 == position)
- {
- // "assets/" is at the beginning of the path and we don't want it
- relativePath = audioFilePath.substr(strlen("assets/"));
- }
- else
- {
- relativePath = audioFilePath;
- }
- assetFd = _fdGetterCallback(relativePath, &start, &length);
- if (assetFd <= 0)
- {
- ALOGE("Failed to open file descriptor for '%s'", audioFilePath.c_str());
- return info;
- }
- fileSize = length;
- }
- else
- {
- FILE *fp = fopen(audioFilePath.c_str(), "rb");
- if (fp != nullptr)
- {
- fseek(fp, 0, SEEK_END);
- fileSize = ftell(fp);
- fclose(fp);
- }
- else
- {
- return info;
- }
- }
- info.url = audioFilePath;
- info.assetFd = std::make_shared<AssetFd>(assetFd);
- info.start = start;
- info.length = fileSize;
- ALOGV("(%s) file size: %ld", audioFilePath.c_str(), fileSize);
- return info;
- }
- bool AudioPlayerProvider::isSmallFile(const AudioFileInfo &info)
- {
- //TODO: If file size is smaller than 100k, we think it's a small file. This value should be set by developers.
- AudioFileInfo &audioFileInfo = const_cast<AudioFileInfo &>(info);
- size_t judgeCount = sizeof(__audioFileIndicator) / sizeof(__audioFileIndicator[0]);
- size_t pos = audioFileInfo.url.rfind(".");
- std::string extension;
- if (pos != std::string::npos)
- {
- extension = audioFileInfo.url.substr(pos);
- }
- auto iter = std::find_if(std::begin(__audioFileIndicator), std::end(__audioFileIndicator),
- [&extension](const AudioFileIndicator &judge) -> bool {
- return judge.extension == extension;
- });
- if (iter != std::end(__audioFileIndicator))
- {
- // ALOGV("isSmallFile: found: %s: ", iter->extension.c_str());
- return info.length < iter->smallSizeIndicator;
- }
- // ALOGV("isSmallFile: not found return default value");
- return info.length < __audioFileIndicator[0].smallSizeIndicator;
- }
- void AudioPlayerProvider::clearPcmCache(const std::string &audioFilePath)
- {
- std::lock_guard<std::mutex> lk(_pcmCacheMutex);
- auto iter = _pcmCache.find(audioFilePath);
- if (iter != _pcmCache.end())
- {
- ALOGV("clear pcm cache: (%s)", audioFilePath.c_str());
- _pcmCache.erase(iter);
- }
- else
- {
- ALOGW("Couldn't find the pcm cache: (%s)", audioFilePath.c_str());
- }
- }
- void AudioPlayerProvider::clearAllPcmCaches()
- {
- std::lock_guard<std::mutex> lk(_pcmCacheMutex);
- _pcmCache.clear();
- }
- PcmAudioPlayer *AudioPlayerProvider::obtainPcmAudioPlayer(const std::string &url,
- const PcmData &pcmData)
- {
- PcmAudioPlayer *pcmPlayer = nullptr;
- if (pcmData.isValid())
- {
- pcmPlayer = new(std::nothrow) PcmAudioPlayer(_mixController, _callerThreadUtils);
- if (pcmPlayer != nullptr)
- {
- pcmPlayer->prepare(url, pcmData);
- }
- }
- else
- {
- ALOGE("obtainPcmAudioPlayer failed, pcmData isn't valid!");
- }
- return pcmPlayer;
- }
- UrlAudioPlayer *AudioPlayerProvider::createUrlAudioPlayer(
- const AudioPlayerProvider::AudioFileInfo &info)
- {
- if (info.url.empty())
- {
- ALOGE("createUrlAudioPlayer failed, url is empty!");
- return nullptr;
- }
- SLuint32 locatorType = info.assetFd->getFd() > 0 ? SL_DATALOCATOR_ANDROIDFD : SL_DATALOCATOR_URI;
- auto urlPlayer = new (std::nothrow) UrlAudioPlayer(_engineItf, _outputMixObject, _callerThreadUtils);
- bool ret = urlPlayer->prepare(info.url, locatorType, info.assetFd, info.start, info.length);
- if (!ret)
- {
- SL_SAFE_DELETE(urlPlayer);
- }
- return urlPlayer;
- }
- void AudioPlayerProvider::pause()
- {
- if (_mixController != nullptr)
- {
- _mixController->pause();
- }
- if (_pcmAudioService != nullptr)
- {
- _pcmAudioService->pause();
- }
- }
- void AudioPlayerProvider::resume()
- {
- if (_mixController != nullptr)
- {
- _mixController->resume();
- }
- if (_pcmAudioService != nullptr)
- {
- _pcmAudioService->resume();
- }
- }
- }} // namespace cocos2d { namespace experimental {
|