2014-09-03 18:19:21 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2019-11-24 14:54:45 +08:00
|
|
|
Copyright (c) 2018 HALX99.
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
http://www.cocos2d-x.org
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
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:
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-09-30 01:07:11 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
#define LOG_TAG "AudioEngineImpl.mm"
|
2016-07-21 14:57:59 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|
|
|
|
|
|
|
|
#include "audio/apple/AudioEngineImpl.h"
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
#import <OpenAL/alc.h>
|
2015-02-11 15:34:08 +08:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
|
|
|
#include "audio/include/AudioEngine.h"
|
2014-09-03 18:19:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
|
2018-02-08 09:24:33 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
2017-06-20 09:04:04 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2015-02-11 15:34:08 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
@interface AudioEngineSessionHandler : NSObject
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
-(id) init;
|
|
|
|
-(void)handleInterruption:(NSNotification*)notification;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AudioEngineSessionHandler
|
|
|
|
|
2015-12-29 07:59:36 +08:00
|
|
|
// only enable it on iOS. Disable it on tvOS
|
|
|
|
#if !defined(CC_TARGET_OS_TVOS)
|
2015-04-04 06:57:47 +08:00
|
|
|
void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruption_state)
|
|
|
|
{
|
|
|
|
if (kAudioSessionBeginInterruption == interruption_state)
|
|
|
|
{
|
|
|
|
alcMakeContextCurrent(nullptr);
|
|
|
|
}
|
|
|
|
else if (kAudioSessionEndInterruption == interruption_state)
|
|
|
|
{
|
|
|
|
OSStatus result = AudioSessionSetActive(true);
|
2016-08-04 09:54:27 +08:00
|
|
|
if (result) NSLog(@"Error setting audio session active! %d\n", static_cast<int>(result));
|
2015-04-04 06:57:47 +08:00
|
|
|
|
|
|
|
alcMakeContextCurrent(s_ALContext);
|
|
|
|
}
|
|
|
|
}
|
2015-12-29 07:59:36 +08:00
|
|
|
#endif
|
2015-04-04 06:57:47 +08:00
|
|
|
|
2015-02-11 15:34:08 +08:00
|
|
|
-(id) init
|
|
|
|
{
|
2015-05-28 11:49:27 +08:00
|
|
|
if (self = [super init])
|
2015-02-11 15:34:08 +08:00
|
|
|
{
|
2015-04-04 06:57:47 +08:00
|
|
|
if ([[[UIDevice currentDevice] systemVersion] intValue] > 5) {
|
2017-03-14 13:56:12 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:UIApplicationDidBecomeActiveNotification object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:UIApplicationWillResignActiveNotification object:nil];
|
2015-04-04 06:57:47 +08:00
|
|
|
}
|
2015-12-29 07:59:36 +08:00
|
|
|
// only enable it on iOS. Disable it on tvOS
|
|
|
|
// AudioSessionInitialize removed from tvOS
|
|
|
|
#if !defined(CC_TARGET_OS_TVOS)
|
2015-04-04 06:57:47 +08:00
|
|
|
else {
|
|
|
|
AudioSessionInitialize(NULL, NULL, AudioEngineInterruptionListenerCallback, self);
|
|
|
|
}
|
2015-12-29 07:59:36 +08:00
|
|
|
#endif
|
2017-08-22 09:46:15 +08:00
|
|
|
|
|
|
|
BOOL success = [[AVAudioSession sharedInstance]
|
|
|
|
setCategory: AVAudioSessionCategoryAmbient
|
|
|
|
error: nil];
|
|
|
|
if (!success)
|
|
|
|
ALOGE("Fail to set audio session.");
|
2015-02-11 15:34:08 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)handleInterruption:(NSNotification*)notification
|
|
|
|
{
|
2017-03-14 13:56:12 +08:00
|
|
|
static bool isAudioSessionInterrupted = false;
|
2015-05-29 21:41:54 +08:00
|
|
|
static bool resumeOnBecomingActive = false;
|
2017-03-14 13:56:12 +08:00
|
|
|
static bool pauseOnResignActive = false;
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2017-03-14 13:56:12 +08:00
|
|
|
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification])
|
|
|
|
{
|
2015-02-11 15:34:08 +08:00
|
|
|
NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] integerValue];
|
2017-03-14 13:56:12 +08:00
|
|
|
if (reason == AVAudioSessionInterruptionTypeBegan)
|
|
|
|
{
|
|
|
|
isAudioSessionInterrupted = true;
|
|
|
|
|
|
|
|
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive)
|
|
|
|
{
|
|
|
|
ALOGD("AVAudioSessionInterruptionTypeBegan, application != UIApplicationStateActive, alcMakeContextCurrent(nullptr)");
|
|
|
|
alcMakeContextCurrent(nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ALOGD("AVAudioSessionInterruptionTypeBegan, application == UIApplicationStateActive, pauseOnResignActive = true");
|
|
|
|
pauseOnResignActive = true;
|
|
|
|
}
|
2015-02-11 15:34:08 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2017-03-14 13:56:12 +08:00
|
|
|
if (reason == AVAudioSessionInterruptionTypeEnded)
|
|
|
|
{
|
|
|
|
isAudioSessionInterrupted = false;
|
|
|
|
|
|
|
|
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
|
|
|
|
{
|
|
|
|
ALOGD("AVAudioSessionInterruptionTypeEnded, application == UIApplicationStateActive, alcMakeContextCurrent(s_ALContext)");
|
2015-05-29 21:41:54 +08:00
|
|
|
NSError *error = nil;
|
|
|
|
[[AVAudioSession sharedInstance] setActive:YES error:&error];
|
2019-11-24 14:54:45 +08:00
|
|
|
if (alcGetCurrentContext() != nullptr)
|
|
|
|
alcMakeContextCurrent(nullptr);
|
2015-05-29 21:41:54 +08:00
|
|
|
alcMakeContextCurrent(s_ALContext);
|
2017-03-14 13:56:12 +08:00
|
|
|
if (Director::getInstance()->isPaused())
|
|
|
|
{
|
|
|
|
ALOGD("AVAudioSessionInterruptionTypeEnded, director was paused, try to resume it.");
|
|
|
|
Director::getInstance()->resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ALOGD("AVAudioSessionInterruptionTypeEnded, application != UIApplicationStateActive, resumeOnBecomingActive = true");
|
2015-05-29 21:41:54 +08:00
|
|
|
resumeOnBecomingActive = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 13:56:12 +08:00
|
|
|
else if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification])
|
|
|
|
{
|
|
|
|
ALOGD("UIApplicationWillResignActiveNotification");
|
|
|
|
if (pauseOnResignActive)
|
|
|
|
{
|
|
|
|
pauseOnResignActive = false;
|
|
|
|
ALOGD("UIApplicationWillResignActiveNotification, alcMakeContextCurrent(nullptr)");
|
|
|
|
alcMakeContextCurrent(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification])
|
|
|
|
{
|
|
|
|
ALOGD("UIApplicationDidBecomeActiveNotification");
|
|
|
|
if (resumeOnBecomingActive)
|
|
|
|
{
|
|
|
|
resumeOnBecomingActive = false;
|
|
|
|
ALOGD("UIApplicationDidBecomeActiveNotification, alcMakeContextCurrent(s_ALContext)");
|
|
|
|
NSError *error = nil;
|
|
|
|
BOOL success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];
|
|
|
|
if (!success) {
|
|
|
|
ALOGE("Fail to set audio session.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[[AVAudioSession sharedInstance] setActive:YES error:&error];
|
2019-11-24 14:54:45 +08:00
|
|
|
|
|
|
|
if (alcGetCurrentContext() != nullptr)
|
|
|
|
alcMakeContextCurrent(nullptr);
|
2017-03-14 13:56:12 +08:00
|
|
|
alcMakeContextCurrent(s_ALContext);
|
|
|
|
}
|
|
|
|
else if (isAudioSessionInterrupted)
|
|
|
|
{
|
|
|
|
ALOGD("Audio session is still interrupted, pause director!");
|
2019-11-24 14:54:45 +08:00
|
|
|
// Director::getInstance()->pause();
|
2015-02-11 15:34:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil];
|
2015-05-29 21:41:54 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
|
2017-03-14 13:56:12 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-02-11 15:34:08 +08:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
static id s_AudioEngineSessionHandler = nullptr;
|
|
|
|
#endif
|
|
|
|
|
2017-06-20 09:04:04 +08:00
|
|
|
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;
|
2017-07-18 11:12:20 +08:00
|
|
|
s_instance->_threadMutex.lock();
|
2017-06-20 09:04:04 +08:00
|
|
|
for (const auto& e : s_instance->_audioPlayers)
|
|
|
|
{
|
|
|
|
player = e.second;
|
|
|
|
if (player->_alSource == sid && player->_streamingSource)
|
|
|
|
{
|
|
|
|
player->wakeupRotateThread();
|
|
|
|
}
|
|
|
|
}
|
2017-07-18 11:12:20 +08:00
|
|
|
s_instance->_threadMutex.unlock();
|
2017-06-20 09:04:04 +08:00
|
|
|
}
|
|
|
|
|
2014-09-05 14:34:30 +08:00
|
|
|
AudioEngineImpl::AudioEngineImpl()
|
2015-07-09 14:31:03 +08:00
|
|
|
: _lazyInitLoop(true)
|
2014-11-15 05:07:34 +08:00
|
|
|
, _currentAudioID(0)
|
2017-04-13 17:58:49 +08:00
|
|
|
, _scheduler(nullptr)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2017-06-20 09:04:04 +08:00
|
|
|
s_instance = this;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioEngineImpl::~AudioEngineImpl()
|
|
|
|
{
|
2017-04-13 17:58:49 +08:00
|
|
|
if (_scheduler != nullptr)
|
|
|
|
{
|
|
|
|
_scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this);
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
if (s_ALContext) {
|
2014-09-10 17:12:46 +08:00
|
|
|
alDeleteSources(MAX_AUDIOINSTANCES, _alSources);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioCaches.clear();
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-22 15:38:12 +08:00
|
|
|
alcMakeContextCurrent(nullptr);
|
2014-09-03 18:19:21 +08:00
|
|
|
alcDestroyContext(s_ALContext);
|
|
|
|
}
|
|
|
|
if (s_ALDevice) {
|
|
|
|
alcCloseDevice(s_ALDevice);
|
|
|
|
}
|
2015-07-09 14:31:03 +08:00
|
|
|
|
2015-02-11 15:34:08 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
[s_AudioEngineSessionHandler release];
|
|
|
|
#endif
|
2017-06-20 09:04:04 +08:00
|
|
|
s_instance = nullptr;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngineImpl::init()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do{
|
2015-02-11 15:34:08 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
s_AudioEngineSessionHandler = [[AudioEngineSessionHandler alloc] init];
|
|
|
|
#endif
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
s_ALDevice = alcOpenDevice(nullptr);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
if (s_ALDevice) {
|
|
|
|
s_ALContext = alcCreateContext(s_ALDevice, nullptr);
|
|
|
|
alcMakeContextCurrent(s_ALContext);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
alGenSources(MAX_AUDIOINSTANCES, _alSources);
|
2015-05-28 11:49:27 +08:00
|
|
|
auto alError = alGetError();
|
2014-09-03 18:19:21 +08:00
|
|
|
if(alError != AL_NO_ERROR)
|
|
|
|
{
|
2016-07-21 14:57:59 +08:00
|
|
|
ALOGE("%s:generating sources failed! error = %x", __PRETTY_FUNCTION__, alError);
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
|
2017-09-08 09:29:39 +08:00
|
|
|
_unusedSourcesPool.push_back(_alSources[i]);
|
2017-06-20 09:04:04 +08:00
|
|
|
alSourceAddNotificationExt(_alSources[i], AL_BUFFERS_PROCESSED, myAlSourceNotificationCallback, nullptr);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 16:51:04 +08:00
|
|
|
// fixed #16170: Random crash in alGenBuffers(AudioCache::readDataTask) at startup
|
|
|
|
// Please note that, as we know the OpenAL operation is atomic (threadsafe),
|
|
|
|
// 'alGenBuffers' may be invoked by different threads. But in current implementation of 'alGenBuffers',
|
|
|
|
// When the first time it's invoked, application may crash!!!
|
|
|
|
// Why? OpenAL is opensource by Apple and could be found at
|
|
|
|
// http://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp .
|
|
|
|
/*
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 16:51:04 +08:00
|
|
|
void InitializeBufferMap()
|
|
|
|
{
|
|
|
|
if (gOALBufferMap == NULL) // Position 1
|
|
|
|
{
|
|
|
|
gOALBufferMap = new OALBufferMap (); // Position 2
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 16:51:04 +08:00
|
|
|
// Position Gap
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 16:51:04 +08:00
|
|
|
gBufferMapLock = new CAGuard("OAL:BufferMapLock"); // Position 3
|
|
|
|
gDeadOALBufferMap = new OALBufferMap ();
|
|
|
|
|
|
|
|
OALBuffer *newBuffer = new OALBuffer (AL_NONE);
|
|
|
|
gOALBufferMap->Add(AL_NONE, &newBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *bids)
|
|
|
|
{
|
|
|
|
...
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (n < 0)
|
|
|
|
throw ((OSStatus) AL_INVALID_VALUE);
|
|
|
|
|
|
|
|
InitializeBufferMap();
|
|
|
|
if (gOALBufferMap == NULL)
|
|
|
|
throw ((OSStatus) AL_INVALID_OPERATION);
|
|
|
|
|
|
|
|
CAGuard::Locker locked(*gBufferMapLock); // Position 4
|
|
|
|
...
|
|
|
|
...
|
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 16:51:04 +08:00
|
|
|
*/
|
|
|
|
// 'gBufferMapLock' will be initialized in the 'InitializeBufferMap' function,
|
|
|
|
// that's the problem. It means that 'InitializeBufferMap' may be invoked in different threads.
|
|
|
|
// It will be very dangerous in multi-threads environment.
|
|
|
|
// Imagine there're two threads (Thread A, Thread B), they call 'alGenBuffers' simultaneously.
|
|
|
|
// While A goto 'Position Gap', 'gOALBufferMap' was assigned, then B goto 'Position 1' and find
|
|
|
|
// that 'gOALBufferMap' isn't NULL, B just jump over 'InitialBufferMap' and goto 'Position 4'.
|
|
|
|
// Meanwhile, A is still at 'Position Gap', B will crash at '*gBufferMapLock' since 'gBufferMapLock'
|
|
|
|
// is still a null pointer. Oops, how could Apple implemented this method in this fucking way?
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 16:51:04 +08:00
|
|
|
// Workaround is do an unused invocation in the mainthread right after OpenAL is initialized successfully
|
|
|
|
// as bellow.
|
|
|
|
// ================ Workaround begin ================ //
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 16:51:04 +08:00
|
|
|
ALuint unusedAlBufferId = 0;
|
|
|
|
alGenBuffers(1, &unusedAlBufferId);
|
|
|
|
alDeleteBuffers(1, &unusedAlBufferId);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 16:51:04 +08:00
|
|
|
// ================ Workaround end ================ //
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
_scheduler = Director::getInstance()->getScheduler();
|
2014-09-03 18:19:21 +08:00
|
|
|
ret = true;
|
2016-07-21 14:57:59 +08:00
|
|
|
ALOGI("OpenAL was initialized successfully!");
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}while (false);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-28 15:27:07 +08:00
|
|
|
AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function<void(bool)> callback)
|
2015-07-08 18:04:43 +08:00
|
|
|
{
|
|
|
|
AudioCache* audioCache = nullptr;
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-07-08 18:04:43 +08:00
|
|
|
auto it = _audioCaches.find(filePath);
|
|
|
|
if (it == _audioCaches.end()) {
|
|
|
|
audioCache = &_audioCaches[filePath];
|
|
|
|
audioCache->_fileFullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
|
2016-07-21 14:57:59 +08:00
|
|
|
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);
|
|
|
|
});
|
2015-07-08 18:04:43 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
audioCache = &it->second;
|
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 14:57:59 +08:00
|
|
|
if (audioCache && callback)
|
2015-07-28 15:27:07 +08:00
|
|
|
{
|
|
|
|
audioCache->addLoadCallback(callback);
|
|
|
|
}
|
2015-07-08 18:04:43 +08:00
|
|
|
return audioCache;
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
AUDIO_ID AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
if (s_ALDevice == nullptr) {
|
2015-01-01 08:51:45 +08:00
|
|
|
return AudioEngine::INVALID_AUDIO_ID;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2017-09-08 09:29:39 +08:00
|
|
|
ALuint alSource = findValidSource();
|
|
|
|
if (alSource == AL_INVALID)
|
|
|
|
{
|
2015-01-01 08:51:45 +08:00
|
|
|
return AudioEngine::INVALID_AUDIO_ID;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
auto player = new (std::nothrow) AudioPlayer;
|
|
|
|
if (player == nullptr) {
|
2015-07-08 18:04:43 +08:00
|
|
|
return AudioEngine::INVALID_AUDIO_ID;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
player->_alSource = alSource;
|
|
|
|
player->_loop = loop;
|
|
|
|
player->_volume = volume;
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
auto audioCache = preload(filePath, nullptr);
|
|
|
|
if (audioCache == nullptr) {
|
|
|
|
delete player;
|
|
|
|
return AudioEngine::INVALID_AUDIO_ID;
|
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 14:57:59 +08:00
|
|
|
player->setCache(audioCache);
|
2015-08-11 11:40:36 +08:00
|
|
|
_threadMutex.lock();
|
2019-11-24 14:54:45 +08:00
|
|
|
_audioPlayers.emplace(++_currentAudioID, player);
|
2015-08-11 11:40:36 +08:00
|
|
|
_threadMutex.unlock();
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 14:57:59 +08:00
|
|
|
audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
if (_lazyInitLoop) {
|
|
|
|
_lazyInitLoop = false;
|
2015-08-11 11:40:36 +08:00
|
|
|
_scheduler->schedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this, 0.05f, false);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
return _currentAudioID;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
void AudioEngineImpl::_play2d(AudioCache *cache, AUDIO_ID audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
_threadMutex.lock();
|
2018-07-11 09:36:57 +08:00
|
|
|
//Note: It maybe in sub thread or main thread :(
|
2016-07-21 14:57:59 +08:00
|
|
|
if (!*cache->_isDestroyed && cache->_state == AudioCache::State::READY)
|
|
|
|
{
|
2014-09-03 18:19:21 +08:00
|
|
|
auto playerIt = _audioPlayers.find(audioID);
|
2016-07-21 14:57:59 +08:00
|
|
|
if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) {
|
2015-08-11 11:40:36 +08:00
|
|
|
_scheduler->performFunctionInCocosThread([audioID](){
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) {
|
|
|
|
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
|
|
|
|
}
|
|
|
|
});
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 14:57:59 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ALOGD("AudioEngineImpl::_play2d, cache was destroyed or not ready!");
|
|
|
|
auto iter = _audioPlayers.find(audioID);
|
|
|
|
if (iter != _audioPlayers.end())
|
|
|
|
{
|
|
|
|
iter->second->_removeByAudioEngine = true;
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 14:54:45 +08:00
|
|
|
_threadMutex.unlock();
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2017-09-08 09:29:39 +08:00
|
|
|
ALuint AudioEngineImpl::findValidSource()
|
|
|
|
{
|
|
|
|
ALuint sourceId = AL_INVALID;
|
|
|
|
if (!_unusedSourcesPool.empty())
|
|
|
|
{
|
|
|
|
sourceId = _unusedSourcesPool.front();
|
|
|
|
_unusedSourcesPool.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
return sourceId;
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
void AudioEngineImpl::setVolume(AUDIO_ID audioID,float volume)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
std::unique_lock<std::mutex> lck(_threadMutex);
|
|
|
|
auto iter = _audioPlayers.find(audioID);
|
|
|
|
if(iter == _audioPlayers.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto player = iter->second;
|
|
|
|
lck.unlock();
|
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
player->_volume = volume;
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
if (player->_ready) {
|
2019-11-24 14:54:45 +08:00
|
|
|
alSourcef(player->_alSource, AL_GAIN, volume);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
2019-11-24 14:54:45 +08:00
|
|
|
ALOGE("%s: audio id = %zu, error = %x", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
void AudioEngineImpl::setLoop(AUDIO_ID audioID, bool loop)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
std::unique_lock<std::mutex> lck(_threadMutex);
|
|
|
|
auto iter = _audioPlayers.find(audioID);
|
|
|
|
if(iter == _audioPlayers.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto player = iter->second;
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
lck.unlock();
|
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
if (player->_ready) {
|
|
|
|
if (player->_streamingSource) {
|
|
|
|
player->setLoop(loop);
|
2014-09-03 18:19:21 +08:00
|
|
|
} else {
|
|
|
|
if (loop) {
|
2015-08-11 11:40:36 +08:00
|
|
|
alSourcei(player->_alSource, AL_LOOPING, AL_TRUE);
|
2014-09-03 18:19:21 +08:00
|
|
|
} else {
|
2015-08-11 11:40:36 +08:00
|
|
|
alSourcei(player->_alSource, AL_LOOPING, AL_FALSE);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
2019-11-24 14:54:45 +08:00
|
|
|
ALOGE("%s: audio id = %zu, error = %x", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-08-11 11:40:36 +08:00
|
|
|
player->_loop = loop;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
bool AudioEngineImpl::pause(AUDIO_ID audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
std::unique_lock<std::mutex> lck(_threadMutex);
|
|
|
|
auto iter = _audioPlayers.find(audioID);
|
|
|
|
if(iter == _audioPlayers.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto player = iter->second;
|
|
|
|
|
|
|
|
lck.unlock();
|
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
bool ret = true;
|
2019-11-24 14:54:45 +08:00
|
|
|
alSourcePause(player->_alSource);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
|
|
|
ret = false;
|
2019-11-24 14:54:45 +08:00
|
|
|
ALOGE("%s: audio id = %zu, error = %x", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
bool AudioEngineImpl::resume(AUDIO_ID audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
bool ret = true;
|
2019-11-24 14:54:45 +08:00
|
|
|
std::unique_lock<std::mutex> lck(_threadMutex);
|
|
|
|
auto iter = _audioPlayers.find(audioID);
|
|
|
|
if(iter == _audioPlayers.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto player = iter->second;
|
|
|
|
lck.unlock();
|
|
|
|
|
|
|
|
alSourcePlay(player->_alSource);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
|
|
|
ret = false;
|
2019-11-24 14:54:45 +08:00
|
|
|
ALOGE("%s: audio id = %zu, error = %x", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
void AudioEngineImpl::stop(AUDIO_ID audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
std::unique_lock<std::mutex> lck(_threadMutex);
|
|
|
|
auto iter = _audioPlayers.find(audioID);
|
|
|
|
if(iter == _audioPlayers.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto player = iter->second;
|
|
|
|
lck.unlock();
|
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
player->destroy();
|
2017-05-22 11:12:17 +08:00
|
|
|
|
|
|
|
// Call 'update' method to cleanup immediately since the schedule may be cancelled without any notification.
|
|
|
|
update(0.0f);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::stopAll()
|
|
|
|
{
|
2015-08-11 11:40:36 +08:00
|
|
|
for(auto&& player : _audioPlayers)
|
|
|
|
{
|
|
|
|
player.second->destroy();
|
|
|
|
}
|
2017-05-22 11:12:17 +08:00
|
|
|
|
|
|
|
// Call 'update' method to cleanup immediately since the schedule may be cancelled without any notification.
|
|
|
|
update(0.0f);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
float AudioEngineImpl::getDuration(AUDIO_ID audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2015-08-11 11:40:36 +08:00
|
|
|
auto player = _audioPlayers[audioID];
|
|
|
|
if(player->_ready){
|
|
|
|
return player->_audioCache->_duration;
|
2014-09-03 18:19:21 +08:00
|
|
|
} else {
|
|
|
|
return AudioEngine::TIME_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
float AudioEngineImpl::getCurrentTime(AUDIO_ID audioID)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
float ret = 0.0f;
|
2015-08-11 11:40:36 +08:00
|
|
|
auto player = _audioPlayers[audioID];
|
|
|
|
if(player->_ready){
|
|
|
|
if (player->_streamingSource) {
|
|
|
|
ret = player->getTime();
|
2014-09-03 18:19:21 +08:00
|
|
|
} else {
|
2015-08-11 11:40:36 +08:00
|
|
|
alGetSourcef(player->_alSource, AL_SEC_OFFSET, &ret);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
2019-11-24 14:54:45 +08:00
|
|
|
ALOGE("%s, audio id:%zu,error code:%x", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
bool AudioEngineImpl::setCurrentTime(AUDIO_ID audioID, float time)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
2015-08-11 11:40:36 +08:00
|
|
|
auto player = _audioPlayers[audioID];
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
do {
|
2015-08-11 11:40:36 +08:00
|
|
|
if (!player->_ready) {
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
if (player->_streamingSource) {
|
|
|
|
ret = player->setTime(time);
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2016-12-02 13:52:37 +08:00
|
|
|
if (player->_audioCache->_framesRead != player->_audioCache->_totalFrames &&
|
|
|
|
(time * player->_audioCache->_sampleRate) > player->_audioCache->_framesRead) {
|
2019-11-24 14:54:45 +08:00
|
|
|
ALOGE("%s: audio id = %zu", __PRETTY_FUNCTION__,audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2015-08-11 11:40:36 +08:00
|
|
|
alSourcef(player->_alSource, AL_SEC_OFFSET, time);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
2019-11-24 14:54:45 +08:00
|
|
|
ALOGE("%s: audio id = %zu, error = %x", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
} while (0);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:54:45 +08:00
|
|
|
void AudioEngineImpl::setFinishCallback(AUDIO_ID audioID, const std::function<void (AUDIO_ID, const std::string &)> &callback)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
std::unique_lock<std::mutex> lck(_threadMutex);
|
|
|
|
auto iter = _audioPlayers.find(audioID);
|
|
|
|
if(iter == _audioPlayers.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto player = iter->second;
|
|
|
|
lck.unlock();
|
|
|
|
|
|
|
|
player->_finishCallbak = callback;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::update(float dt)
|
|
|
|
{
|
|
|
|
ALint sourceState;
|
2019-11-24 14:54:45 +08:00
|
|
|
AUDIO_ID audioID;
|
2015-08-11 11:40:36 +08:00
|
|
|
AudioPlayer* player;
|
2017-03-27 17:28:39 +08:00
|
|
|
ALuint alSource;
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 14:57:59 +08:00
|
|
|
// ALOGV("AudioPlayer count: %d", (int)_audioPlayers.size());
|
2019-11-24 14:54:45 +08:00
|
|
|
_threadMutex.lock();
|
2014-09-03 18:19:21 +08:00
|
|
|
for (auto it = _audioPlayers.begin(); it != _audioPlayers.end(); ) {
|
|
|
|
audioID = it->first;
|
2015-08-11 11:40:36 +08:00
|
|
|
player = it->second;
|
2017-03-27 17:28:39 +08:00
|
|
|
alSource = player->_alSource;
|
|
|
|
alGetSourcei(alSource, AL_SOURCE_STATE, &sourceState);
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2016-07-21 14:57:59 +08:00
|
|
|
if (player->_removeByAudioEngine)
|
2015-08-11 11:40:36 +08:00
|
|
|
{
|
|
|
|
AudioEngine::remove(audioID);
|
|
|
|
it = _audioPlayers.erase(it);
|
|
|
|
delete player;
|
2017-09-08 09:29:39 +08:00
|
|
|
_unusedSourcesPool.push_back(alSource);
|
2015-08-11 11:40:36 +08:00
|
|
|
}
|
|
|
|
else if (player->_ready && sourceState == AL_STOPPED) {
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2017-03-27 17:28:39 +08:00
|
|
|
std::string filePath;
|
2015-08-11 11:40:36 +08:00
|
|
|
if (player->_finishCallbak) {
|
2014-09-22 15:38:12 +08:00
|
|
|
auto& audioInfo = AudioEngine::_audioIDInfoMap[audioID];
|
2019-11-26 09:54:25 +08:00
|
|
|
filePath = audioInfo.filePath;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-05 14:34:30 +08:00
|
|
|
AudioEngine::remove(audioID);
|
2019-11-24 14:54:45 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
it = _audioPlayers.erase(it);
|
2017-03-27 17:28:39 +08:00
|
|
|
|
|
|
|
if (player->_finishCallbak) {
|
2019-11-24 14:54:45 +08:00
|
|
|
/// ###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
|
|
|
|
});
|
2017-03-27 17:28:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
delete player;
|
2017-09-08 09:29:39 +08:00
|
|
|
_unusedSourcesPool.push_back(alSource);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 13:52:37 +08:00
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
if(_audioPlayers.empty()){
|
|
|
|
_lazyInitLoop = true;
|
2015-08-11 11:40:36 +08:00
|
|
|
_scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this);
|
2014-09-10 17:12:46 +08:00
|
|
|
}
|
2019-11-24 14:54:45 +08:00
|
|
|
_threadMutex.unlock();
|
|
|
|
|
|
|
|
if (!_finishCallbacks.empty()) {
|
|
|
|
for (auto& finishCallback : _finishCallbacks)
|
|
|
|
finishCallback();
|
|
|
|
_finishCallbacks.clear();
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::uncache(const std::string &filePath)
|
|
|
|
{
|
2014-09-29 10:15:41 +08:00
|
|
|
_audioCaches.erase(filePath);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::uncacheAll()
|
|
|
|
{
|
|
|
|
_audioCaches.clear();
|
|
|
|
}
|
2019-11-24 14:54:45 +08:00
|
|
|
|
|
|
|
#endif
|