Merge pull request #126 from halx99/simplify-audio-sources

Simplify audio sources
This commit is contained in:
HALX99 2020-07-31 00:52:22 -07:00 committed by GitHub
commit d7858db30d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 65 additions and 265 deletions

View File

@ -32,11 +32,7 @@
#include "platform/CCFileUtils.h"
#include "base/ccUtils.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) && !CC_USE_ALSOFT_ON_APPLE
#include "audio/apple/AudioEngineImpl.h"
#else
#include "audio/include/AudioEngineImpl.h"
#endif
#define TIME_DELAY_PRECISION 0.0001

View File

@ -101,7 +101,7 @@ bool AudioEngineImpl::init()
}
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
_alSourceUsed[_alSources[i]] = false;
_unusedSourcesPool.push(_alSources[i]);
}
_scheduler = Director::getInstance()->getScheduler();
@ -150,17 +150,9 @@ AUDIO_ID AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float v
return AudioEngine::INVALID_AUDIO_ID;
}
bool sourceFlag = false;
ALuint alSource = 0;
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
alSource = _alSources[i];
if ( !_alSourceUsed[alSource]) {
sourceFlag = true;
break;
}
}
if(!sourceFlag){
ALuint alSource = findValidSource();
if (alSource == AL_INVALID)
{
return AudioEngine::INVALID_AUDIO_ID;
}
@ -184,8 +176,6 @@ AUDIO_ID AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float v
_audioPlayers.emplace(++_currentAudioID, player);
_threadMutex.unlock();
_alSourceUsed[alSource] = true;
audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
if (_lazyInitLoop) {
@ -223,6 +213,18 @@ void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID)
}
}
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)
{
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
@ -470,7 +472,7 @@ void AudioEngineImpl::_updateLocked(float dt)
it = _audioPlayers.erase(it);
delete player;
_alSourceUsed[alSource] = false;
_unusedSourcesPool.push(alSource);
}
else if (player->_ready && player->isFinished()) {
@ -485,21 +487,16 @@ void AudioEngineImpl::_updateLocked(float dt)
it = _audioPlayers.erase(it);
if (player->_finishCallbak) {
#if !defined(_MSC_VER) || _MSC_VER >= 1900
_finishCallbacks.push_back([finishCallback = std::move(player->_finishCallbak), audioID, filePath = std::move(filePath)] {
finishCallback(audioID, filePath); //FIXME: callback will delay 50ms
/// ###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
});
#else
auto finishCallback = std::move(player->_finishCallbak);
_finishCallbacks.push_back([=]{
finishCallback(audioID, filePath); //FIXME: callback will delay 50ms
});
#endif
}
// clear cache when audio player finsihed properly
player->setCache(nullptr);
delete player;
_alSourceUsed[alSource] = false;
_unusedSourcesPool.push(alSource);
}
else{
++it;

View File

@ -142,6 +142,9 @@ bool AudioPlayer::play2d()
std::unique_lock<std::mutex> lck(_play2dMutex);
ALOGV("AudioPlayer::play2d, _alSource: %u, player id=%u", _alSource, _id);
if (_isDestroyed)
return false;
/*********************************************************************/
/* Note that it may be in sub thread or in main thread. **/
/*********************************************************************/

View File

@ -84,6 +84,8 @@ elseif(APPLE)
audio/include/AudioDecoderOgg.h
audio/include/AudioDecoderWav.h
audio/include/AudioCache.h
audio/include/AudioPlayer.h
audio/include/AudioEngineImpl.h
audio/apple/AudioDecoderEXT.h
)
@ -96,20 +98,12 @@ elseif(APPLE)
)
if(CC_USE_ALSOFT_ON_APPLE)
set(COCOS_AUDIO_PLATFORM_HEADER ${COCOS_AUDIO_PLATFORM_HEADER}
audio/include/AudioPlayer.h
audio/include/AudioEngineImpl.h
)
set(COCOS_AUDIO_PLATFORM_SRC ${COCOS_AUDIO_PLATFORM_SRC}
audio/AudioCache.cpp
audio/AudioPlayer.cpp
audio/AudioEngineImpl.cpp
)
else()
set(COCOS_AUDIO_PLATFORM_HEADER ${COCOS_AUDIO_PLATFORM_HEADER}
audio/apple/AudioPlayer.h
audio/apple/AudioEngineImpl.h
)
set(COCOS_AUDIO_PLATFORM_SRC ${COCOS_AUDIO_PLATFORM_SRC}
audio/apple/AudioCache.mm
audio/apple/AudioPlayer.mm

View File

@ -1,99 +0,0 @@
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2018-2020 HALX99.
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.
****************************************************************************/
#pragma once
#ifndef __AUDIO_ENGINE_IMPL_H_
#define __AUDIO_ENGINE_IMPL_H_
#include "platform/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
#include <unordered_map>
#include <list>
#include "base/CCRef.h"
#include "audio/include/AudioMacros.h"
#include "audio/include/AudioCache.h"
#include "audio/apple/AudioPlayer.h"
NS_CC_BEGIN
class Scheduler;
#define MAX_AUDIOINSTANCES 24
class AudioEngineImpl : public cocos2d::Ref
{
public:
AudioEngineImpl();
~AudioEngineImpl();
bool init();
AUDIO_ID play2d(const std::string &fileFullPath ,bool loop ,float volume);
void setVolume(AUDIO_ID audioID,float volume);
void setLoop(AUDIO_ID audioID, bool loop);
bool pause(AUDIO_ID audioID);
bool resume(AUDIO_ID audioID);
void stop(AUDIO_ID audioID);
void stopAll();
float getDuration(AUDIO_ID audioID);
float getCurrentTime(AUDIO_ID audioID);
bool setCurrentTime(AUDIO_ID audioID, float time);
void setFinishCallback(AUDIO_ID audioID, const std::function<void (AUDIO_ID, const std::string &)> &callback);
void uncache(const std::string& filePath);
void uncacheAll();
AudioCache* preload(const std::string& filePath, std::function<void(bool)> callback);
void update(float dt);
private:
void _updateLocked(float dt);
void _play2d(AudioCache *cache, AUDIO_ID audioID);
ALuint findValidSource();
static ALvoid myAlSourceNotificationCallback(ALuint sid, ALuint notificationID, ALvoid* userData);
ALuint _alSources[MAX_AUDIOINSTANCES];
//source,used
std::list<ALuint> _unusedSourcesPool;
//filePath,bufferInfo
std::unordered_map<std::string, AudioCache> _audioCaches;
//audioID,AudioInfo
std::unordered_map<AUDIO_ID, AudioPlayer*> _audioPlayers;
std::recursive_mutex _threadMutex;
//finish callbacks
std::vector<std::function<void()>> _finishCallbacks;
bool _lazyInitLoop;
AUDIO_ID _currentAudioID;
Scheduler* _scheduler;
};
NS_CC_END
#endif // __AUDIO_ENGINE_INL_H_
#endif

View File

@ -28,7 +28,7 @@
#include "platform/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
#include "audio/apple/AudioEngineImpl.h"
#include "audio/include/AudioEngineImpl.h"
#import <OpenAL/alc.h>
#import <AVFoundation/AVFoundation.h>
@ -298,7 +298,7 @@ bool AudioEngineImpl::init()
}
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
_unusedSourcesPool.push_back(_alSources[i]);
_unusedSourcesPool.push(_alSources[i]);
alSourceAddNotificationExt(_alSources[i], AL_BUFFERS_PROCESSED, myAlSourceNotificationCallback, nullptr);
}
@ -478,7 +478,7 @@ ALuint AudioEngineImpl::findValidSource()
if (!_unusedSourcesPool.empty())
{
sourceId = _unusedSourcesPool.front();
_unusedSourcesPool.pop_front();
_unusedSourcesPool.pop();
}
return sourceId;
@ -731,7 +731,7 @@ void AudioEngineImpl::_updateLocked(float dt)
it = _audioPlayers.erase(it);
delete player;
_unusedSourcesPool.push_back(alSource);
_unusedSourcesPool.push(alSource);
}
else if (player->_ready && player->isFinished()) {
@ -755,7 +755,7 @@ void AudioEngineImpl::_updateLocked(float dt)
// clear cache when audio player finsihed properly
player->setCache(nullptr);
delete player;
_unusedSourcesPool.push_back(alSource);
_unusedSourcesPool.push(alSource);
}
else{
++it;
@ -779,9 +779,9 @@ void AudioEngineImpl::uncache(const std::string &filePath)
}
void AudioEngineImpl::uncacheAll()
{
{
// prevent player hold invalid AudioCache* pointer, since all audio caches purged
for(auto& player : _audioPlayers)
for (auto& player : _audioPlayers)
player.second->setCache(nullptr);
_audioCaches.clear();

View File

@ -1,97 +0,0 @@
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2018 HALX99.
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.
****************************************************************************/
#pragma once
#include "platform/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
#include "platform/CCPlatformMacros.h"
#include "audio/include/AudioMacros.h"
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
#include <OpenAL/al.h>
NS_CC_BEGIN
class AudioCache;
class AudioEngineImpl;
class AudioPlayer
{
public:
AudioPlayer();
~AudioPlayer();
void destroy();
//queue buffer related stuff
bool setTime(float time);
float getTime() { return _currTime;}
bool setLoop(bool loop);
bool isFinished() const;
protected:
void setCache(AudioCache* cache);
void rotateBufferThread(int offsetFrame);
bool play2d();
void wakeupRotateThread();
AudioCache* _audioCache;
float _volume;
bool _loop;
std::function<void (AUDIO_ID, const std::string &)> _finishCallbak;
bool _isDestroyed;
bool _removeByAudioEngine;
bool _ready;
ALuint _alSource;
//play by circular buffer
float _currTime;
bool _streamingSource;
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;
unsigned int _id;
friend class AudioEngineImpl;
};
NS_CC_END
#endif

View File

@ -30,7 +30,7 @@
#import <Foundation/Foundation.h>
#include "audio/apple/AudioPlayer.h"
#include "audio/include/AudioPlayer.h"
#include "audio/include/AudioCache.h"
#include "platform/CCFileUtils.h"
#include "audio/include/AudioDecoder.h"

View File

@ -33,18 +33,6 @@
#include <vector>
#include <memory>
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) && !CC_USE_ALSOFT_ON_APPLE
#import <OpenAL/al.h>
#else
#ifdef OPENAL_PLAIN_INCLUDES
#include <al.h>
#include <alext.h>
#else
#include <AL/al.h>
#include <AL/alext.h>
#endif
#endif
#include "platform/CCPlatformMacros.h"
#include "audio/include/AudioMacros.h"

View File

@ -29,6 +29,7 @@
#include "platform/CCPlatformConfig.h"
#include <unordered_map>
#include <queue>
#include "base/CCRef.h"
#include "audio/include/AudioMacros.h"
@ -39,8 +40,6 @@ NS_CC_BEGIN
class Scheduler;
#define MAX_AUDIOINSTANCES 32
class CC_DLL AudioEngineImpl : public cocos2d::Ref
{
public:
@ -68,11 +67,14 @@ public:
private:
void _updateLocked(float dt);
void _play2d(AudioCache *cache, AUDIO_ID audioID);
ALuint findValidSource();
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
static ALvoid myAlSourceNotificationCallback(ALuint sid, ALuint notificationID, ALvoid* userData);
#endif
ALuint _alSources[MAX_AUDIOINSTANCES];
//source,used
std::unordered_map<ALuint, bool> _alSourceUsed;
//available sources
std::queue<ALuint> _unusedSourcesPool;
//filePath,bufferInfo
std::unordered_map<std::string, AudioCache> _audioCaches;

View File

@ -84,3 +84,17 @@ do { \
#define AUDIO_ID int
#define AUDIO_ID_PRID "%d"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) && !CC_USE_ALSOFT_ON_APPLE
#import <OpenAL/al.h>
#define MAX_AUDIOINSTANCES 24
#else
#ifdef OPENAL_PLAIN_INCLUDES
#include <al.h>
#include <alext.h>
#else
#include <AL/al.h>
#include <AL/alext.h>
#endif
#define MAX_AUDIOINSTANCES 32
#endif

View File

@ -31,11 +31,7 @@
#include <condition_variable>
#include <mutex>
#include <thread>
#ifdef OPENAL_PLAIN_INCLUDES
#include <al.h>
#else
#include <AL/al.h>
#endif
#include "audio/include/AudioMacros.h"
#include "platform/CCPlatformMacros.h"
@ -64,6 +60,9 @@ protected:
void setCache(AudioCache* cache);
void rotateBufferThread(int offsetFrame);
bool play2d();
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
void wakeupRotateThread();
#endif
AudioCache* _audioCache;
@ -85,6 +84,9 @@ protected:
std::mutex _sleepMutex;
bool _timeDirty;
bool _isRotateThreadExited;
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
std::atomic_bool _needWakeupRotateThread;
#endif
std::mutex _play2dMutex;

View File

@ -1,5 +1,5 @@
{
"version": "v53",
"version": "v54",
"zip_file_size": "107642814",
"repo_name": "engine-x-3rd",
"repo_parent": "https://github.com/c4games/",

View File

@ -69,7 +69,7 @@ function build_ios_cmake()
cd $COCOS2DX_ROOT
mkdir -p ios_cmake_build
cd ios_cmake_build
cmake .. -GXcode -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator
cmake .. -GXcode -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCC_USE_ALSOFT_ON_APPLE=ON
# cmake .. -GXcode -DCMAKE_TOOLCHAIN_FILE=../cmake/ios.toolchain.cmake -DCMAKE_SYSTEM_NAME=iOS -DPLATFORM=OS -DENABLE_ARC=0 # too much logs on console when "cmake --build ."
cmake --build . --config Release --target cpp-tests -- -quiet -jobs $NUM_OF_CORES -destination "platform=iOS Simulator,name=iPhone Retina (4-inch)"