2019-11-23 20:27:39 +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.
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 "AudioPlayer"
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
2019-11-23 20:27:39 +08:00
#import <Foundation/Foundation.h>
#include "audio/apple/AudioPlayer.h"
2019-11-26 02:33:54 +08:00
#include "audio/include/AudioCache.h"
2019-11-23 20:27:39 +08:00
#include "platform/CCFileUtils.h"
2019-11-30 06:11:17 +08:00
#include "audio/include/AudioDecoder.h"
#include "audio/include/AudioDecoderManager.h"
2019-11-23 20:27:39 +08:00
#ifdef VERY_VERY_VERBOSE_LOGGING
#define ALOGVV ALOGV
#else
#define ALOGVV(...) do{} while(false)
#endif
using namespace cocos2d;
namespace {
unsigned int __idIndex = 0;
}
AudioPlayer::AudioPlayer()
: _audioCache(nullptr)
, _finishCallbak(nullptr)
, _isDestroyed(false)
, _removeByAudioEngine(false)
, _ready(false)
, _currTime(0.0f)
, _streamingSource(false)
, _rotateBufferThread(nullptr)
, _timeDirty(false)
, _isRotateThreadExited(false)
, _needWakeupRotateThread(false)
, _id(++__idIndex)
{
memset(_bufferIds, 0, sizeof(_bufferIds));
}
AudioPlayer::~AudioPlayer()
{
ALOGVV("~AudioPlayer() (%p), id=%u", this, _id);
destroy();
if (_streamingSource)
{
alDeleteBuffers(QUEUEBUFFER_NUM, _bufferIds);
}
}
void AudioPlayer::destroy()
{
2020-06-04 23:11:24 +08:00
std::unique_lock<std::mutex> lck(_play2dMutex);
2019-11-23 20:27:39 +08:00
if (_isDestroyed)
return;
ALOGVV("AudioPlayer::destroy begin, id=%u", _id);
_isDestroyed = true;
do
{
if (_audioCache != nullptr)
{
if (_audioCache->_state == AudioCache::State::INITIAL)
{
ALOGV("AudioPlayer::destroy, id=%u, cache isn't ready!", _id);
break;
}
while (!_audioCache->_isLoadingFinished)
{
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}
if (_streamingSource)
{
if (_rotateBufferThread != nullptr)
{
while (!_isRotateThreadExited)
{
_sleepCondition.notify_one();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
if (_rotateBufferThread->joinable()) {
_rotateBufferThread->join();
}
delete _rotateBufferThread;
_rotateBufferThread = nullptr;
ALOGVV("rotateBufferThread exited!");
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
// some specific OpenAL implement defects existed on iOS platform
// refer to: https://github.com/cocos2d/cocos2d-x/issues/18597
ALint sourceState;
ALint bufferProcessed = 0;
alGetSourcei(_alSource, AL_SOURCE_STATE, &sourceState);
if (sourceState == AL_PLAYING) {
alGetSourcei(_alSource, AL_BUFFERS_PROCESSED, &bufferProcessed);
while (bufferProcessed < QUEUEBUFFER_NUM) {
std::this_thread::sleep_for(std::chrono::milliseconds(2));
alGetSourcei(_alSource, AL_BUFFERS_PROCESSED, &bufferProcessed);
}
alSourceUnqueueBuffers(_alSource, QUEUEBUFFER_NUM, _bufferIds); CHECK_AL_ERROR_DEBUG();
}
ALOGVV("UnqueueBuffers Before alSourceStop");
#endif
}
}
} while(false);
ALOGVV("Before alSourceStop");
alSourceStop(_alSource); CHECK_AL_ERROR_DEBUG();
ALOGVV("Before alSourcei");
alSourcei(_alSource, AL_BUFFER, 0); CHECK_AL_ERROR_DEBUG();
_removeByAudioEngine = true;
_ready = false;
ALOGVV("AudioPlayer::destroy end, id=%u", _id);
}
void AudioPlayer::setCache(AudioCache* cache)
{
_audioCache = cache;
}
bool AudioPlayer::play2d()
{
2019-11-24 14:54:45 +08:00
std::unique_lock<std::mutex> lck(_play2dMutex);
ALOGV("AudioPlayer::play2d, _alSource: %u", _alSource);
2019-11-23 20:27:39 +08:00
2019-11-24 14:54:45 +08:00
if (_isDestroyed)
return false;
2019-11-23 20:27:39 +08:00
/*********************************************************************/
/* Note that it may be in sub thread or in main thread. **/
/*********************************************************************/
bool ret = false;
do
{
if (_audioCache->_state != AudioCache::State::READY)
{
ALOGE("alBuffer isn't ready for play!");
break;
}
alSourcei(_alSource, AL_BUFFER, 0);CHECK_AL_ERROR_DEBUG();
alSourcef(_alSource, AL_PITCH, 1.0f);CHECK_AL_ERROR_DEBUG();
alSourcef(_alSource, AL_GAIN, _volume);CHECK_AL_ERROR_DEBUG();
alSourcei(_alSource, AL_LOOPING, AL_FALSE);CHECK_AL_ERROR_DEBUG();
if (_audioCache->_queBufferFrames == 0)
{
if (_loop) {
alSourcei(_alSource, AL_LOOPING, AL_TRUE);
CHECK_AL_ERROR_DEBUG();
}
}
else
{
alGenBuffers(QUEUEBUFFER_NUM, _bufferIds);
auto alError = alGetError();
if (alError == AL_NO_ERROR)
{
for (int index = 0; index < QUEUEBUFFER_NUM; ++index)
{
alBufferData(_bufferIds[index], _audioCache->_format, _audioCache->_queBuffers[index], _audioCache->_queBufferSize[index], _audioCache->_sampleRate);
}
CHECK_AL_ERROR_DEBUG();
}
else
{
ALOGE("%s:alGenBuffers error code:%x", __PRETTY_FUNCTION__,alError);
break;
}
_streamingSource = true;
}
{
std::unique_lock<std::mutex> lk(_sleepMutex);
if (_isDestroyed)
break;
if (_streamingSource)
{
// To continuously stream audio from a source without interruption, buffer queuing is required.
alSourceQueueBuffers(_alSource, QUEUEBUFFER_NUM, _bufferIds);
CHECK_AL_ERROR_DEBUG();
_rotateBufferThread = new std::thread(&AudioPlayer::rotateBufferThread, this, _audioCache->_queBufferFrames * QUEUEBUFFER_NUM + 1);
}
else
{
alSourcei(_alSource, AL_BUFFER, _audioCache->_alBufferId);
CHECK_AL_ERROR_DEBUG();
}
alSourcePlay(_alSource);
}
auto alError = alGetError();
if (alError != AL_NO_ERROR)
{
ALOGE("%s:alSourcePlay error code:%x", __PRETTY_FUNCTION__,alError);
break;
}
ALint state;
alGetSourcei(_alSource, AL_SOURCE_STATE, &state);
2019-11-24 14:54:45 +08:00
// assert(state == AL_PLAYING); // sometime when switch audio too fast, the result state will error, but there is no any alError, so just skip for workaround.
2019-11-23 20:27:39 +08:00
_ready = true;
ret = true;
} while (false);
if (!ret)
{
_removeByAudioEngine = true;
}
2019-11-24 14:54:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
// rotateBufferThread is used to rotate alBufferData for _alSource when playing big audio file
void AudioPlayer::rotateBufferThread(int offsetFrame)
{
2019-11-24 14:54:45 +08:00
pthread_setname_np("ALStreaming");
2019-11-23 20:27:39 +08:00
char* tmpBuffer = nullptr;
2019-11-30 06:11:17 +08:00
auto& fullPath = _audioCache->_fileFullPath;
AudioDecoder* decoder = AudioDecoderManager::createDecoder(fullPath);
2019-11-23 20:27:39 +08:00
long long rotateSleepTime = static_cast<long long>(QUEUEBUFFER_TIME_STEP * 1000) / 2;
do
{
2019-11-30 06:11:17 +08:00
BREAK_IF(decoder == nullptr || !decoder->open(fullPath));
2019-11-23 20:27:39 +08:00
uint32_t framesRead = 0;
const uint32_t framesToRead = _audioCache->_queBufferFrames;
2020-06-04 12:26:25 +08:00
const uint32_t bufferSize = decoder->framesToBytes(framesToRead);
2019-11-23 20:27:39 +08:00
tmpBuffer = (char*)malloc(bufferSize);
memset(tmpBuffer, 0, bufferSize);
if (offsetFrame != 0) {
2019-11-30 06:11:17 +08:00
decoder->seek(offsetFrame);
2019-11-23 20:27:39 +08:00
}
ALint sourceState;
ALint bufferProcessed = 0;
bool needToExitThread = false;
while (!_isDestroyed) {
alGetSourcei(_alSource, AL_SOURCE_STATE, &sourceState);
if (sourceState == AL_PLAYING) {
alGetSourcei(_alSource, AL_BUFFERS_PROCESSED, &bufferProcessed);
while (bufferProcessed > 0) {
bufferProcessed--;
if (_timeDirty) {
_timeDirty = false;
2019-11-30 06:11:17 +08:00
offsetFrame = _currTime * decoder->getSampleRate();
decoder->seek(offsetFrame);
2019-11-23 20:27:39 +08:00
}
else {
_currTime += QUEUEBUFFER_TIME_STEP;
if (_currTime > _audioCache->_duration) {
if (_loop) {
_currTime = 0.0f;
} else {
_currTime = _audioCache->_duration;
}
}
}
2019-11-30 06:11:17 +08:00
framesRead = decoder->readFixedFrames(framesToRead, tmpBuffer);
2019-11-23 20:27:39 +08:00
if (framesRead == 0) {
if (_loop) {
2019-11-30 06:11:17 +08:00
decoder->seek(0);
framesRead = decoder->readFixedFrames(framesToRead, tmpBuffer);
2019-11-23 20:27:39 +08:00
} else {
needToExitThread = true;
break;
}
}
/*
While the source is playing, alSourceUnqueueBuffers can be called to remove buffers which have
already played. Those buffers can then be filled with new data or discarded. New or refilled
buffers can then be attached to the playing source using alSourceQueueBuffers. As long as there is
always a new buffer to play in the queue, the source will continue to play.
*/
ALuint bid;
alSourceUnqueueBuffers(_alSource, 1, &bid);
2020-06-04 12:26:25 +08:00
alBufferData(bid, _audioCache->_format, tmpBuffer, decoder->framesToBytes(framesRead), decoder->getSampleRate());
2019-11-23 20:27:39 +08:00
alSourceQueueBuffers(_alSource, 1, &bid);
}
}
2019-11-30 06:55:32 +08:00
/* Make sure the source hasn't underrun */
else if (sourceState != AL_PAUSED)
{
ALint queued;
/* If no buffers are queued, playback is finished */
alGetSourcei(_alSource, AL_BUFFERS_QUEUED, &queued);
if (queued == 0) {
needToExitThread = true;
}
else {
alSourcePlay(_alSource);
if (alGetError() != AL_NO_ERROR)
{
ALOGE("Error restarting playback!");
needToExitThread = true;
}
}
}
2019-11-23 20:27:39 +08:00
std::unique_lock<std::mutex> lk(_sleepMutex);
if (_isDestroyed || needToExitThread) {
break;
}
if (!_needWakeupRotateThread)
{
_sleepCondition.wait_for(lk,std::chrono::milliseconds(rotateSleepTime));
}
_needWakeupRotateThread = false;
}
} while(false);
ALOGVV("Exit rotate buffer thread ...");
2019-11-30 06:11:17 +08:00
if(decoder)
decoder->close();
AudioDecoderManager::destroyDecoder(decoder);
2019-11-23 20:27:39 +08:00
free(tmpBuffer);
_isRotateThreadExited = true;
}
void AudioPlayer::wakeupRotateThread()
{
_needWakeupRotateThread = true;
_sleepCondition.notify_all();
}
2019-11-30 06:55:32 +08:00
bool AudioPlayer::isFinished() const
{
if(_streamingSource) return _isRotateThreadExited;
else {
ALint sourceState;
alGetSourcei(_alSource, AL_SOURCE_STATE, &sourceState);
return sourceState == AL_STOPPED;
}
}
2019-11-23 20:27:39 +08:00
bool AudioPlayer::setLoop(bool loop)
{
if (!_isDestroyed ) {
_loop = loop;
return true;
}
return false;
}
bool AudioPlayer::setTime(float time)
{
if (!_isDestroyed && time >= 0.0f && time < _audioCache->_duration) {
_currTime = time;
_timeDirty = true;
return true;
}
return false;
}
2019-11-24 14:54:45 +08:00
#endif