Merge pull request #445 from dumganhar/fix/apple-audio-assert

[mac, ios] Sync bug fix from https://github.com/cocos2d/cocos2d-x/pull/17011
This commit is contained in:
Wang Nan
2016-12-20 15:20:57 +08:00
committed by GitHub
5 changed files with 48 additions and 21 deletions
+11 -11
View File
@@ -178,6 +178,8 @@ void AudioCache::readDataTask(unsigned int selfId)
std::vector<char> adjustFrameBuf;
adjustFrameBuf.reserve(framesToReadOnce * bytesPerFrame);
// Adjust total frames by setting position to the end of frames and try to read more data.
// This is a workaround for https://github.com/cocos2d/cocos2d-x/issues/16938
do
{
framesRead = decoder.read(framesToReadOnce, tmpBuf);
@@ -205,7 +207,7 @@ void AudioCache::readDataTask(unsigned int selfId)
BREAK_IF_ERR_LOG(!decoder.seek(0), "AudioDecoder::seek(0) failed!");
_pcmData = (char*)malloc(dataSize);
memset(_pcmData, 0, dataSize);
memset(_pcmData, 0x00, dataSize);
if (adjustFrames > 0)
{
@@ -224,14 +226,9 @@ void AudioCache::readDataTask(unsigned int selfId)
alBufferDataStaticProc(_alBufferId, _format, _pcmData, (ALsizei)dataSize, (ALsizei)sampleRate);
do
{
framesRead = decoder.read(std::min(framesToReadOnce, remainingFrames), _pcmData + _framesRead * bytesPerFrame);
if (framesRead == 0)
break;
_framesRead += framesRead;
remainingFrames -= framesRead;
} while (_framesRead < framesToReadOnce);
framesRead = decoder.readFixedFrames(std::min(framesToReadOnce, remainingFrames), _pcmData + _framesRead * bytesPerFrame);
_framesRead += framesRead;
remainingFrames -= framesRead;
if (*_isDestroyed)
break;
@@ -255,7 +252,10 @@ void AudioCache::readDataTask(unsigned int selfId)
remainingFrames -= framesRead;
}
CCASSERT(_framesRead == originalTotalFrames, "The read frame count isn't equal to orignal frame count!");
if (_framesRead < originalTotalFrames)
{
memset(_pcmData + _framesRead * bytesPerFrame, 0x00, (totalFrames - _framesRead) * bytesPerFrame);
}
ALOGV("pcm buffer was loaded successfully, total frames: %u, total read frames: %u, adjust frames: %u, remainingFrames: %u", totalFrames, _framesRead, adjustFrames, remainingFrames);
_framesRead += adjustFrames;
@@ -272,7 +272,7 @@ void AudioCache::readDataTask(unsigned int selfId)
_queBuffers[index] = (char*)malloc(queBufferBytes);
_queBufferSize[index] = queBufferBytes;
decoder.read(_queBufferFrames, _queBuffers[index]);
decoder.readFixedFrames(_queBufferFrames, _queBuffers[index]);
}
_state = State::READY;
+13 -1
View File
@@ -61,11 +61,23 @@ public:
/**
* @brief Reads audio frames of PCM format.
* @param framesToRead The number of frames excepted to be read.
* @param pcmBuf The buffer to hold the frames to be read, its size should be >= framesToRead * _bytesPerFrame.
* @param pcmBuf The buffer to hold the frames to be read, its size should be >= |framesToRead| * _bytesPerFrame.
* @return The number of frames actually read, it's probably less than 'framesToRead'. Returns 0 means reach the end of file.
*/
uint32_t read(uint32_t framesToRead, char* pcmBuf);
/**
* @brief Reads fixed audio frames of PCM format.
* @param framesToRead The number of frames excepted to be read.
* @param pcmBuf The buffer to hold the frames to be read, its size should be >= |framesToRead| * _bytesPerFrame.
* @return The number of frames actually read, it's probably less than |framesToRead|. Returns 0 means reach the end of file.
* @note The different between |read| and |readFixedFrames| is |readFixedFrames| will do multiple reading operations if |framesToRead| frames
* isn't filled entirely, while |read| just does reading operation once whatever |framesToRead| is or isn't filled entirely.
* If current position reaches the end of frames, the return value may smaller than |framesToRead| and the remaining
* buffer in |pcmBuf| will be set with silence data (0x00).
*/
uint32_t readFixedFrames(uint32_t framesToRead, char* pcmBuf);
/**
* @brief Sets frame offest to be read.
* @param frameOffset The frame offest to be set.
+19
View File
@@ -93,6 +93,7 @@ namespace cocos2d { namespace experimental {
propertySize = sizeof(totalFrames);
ret = ExtAudioFileGetProperty(_extRef, kExtAudioFileProperty_FileLengthFrames, &propertySize, &totalFrames);
BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld", (long)ret);
BREAK_IF_ERR_LOG(totalFrames <= 0, "Total frames is 0, it's an invalid audio file: %s", path);
_totalFrames = static_cast<uint32_t>(totalFrames);
_isOpened = true;
@@ -149,6 +150,24 @@ namespace cocos2d { namespace experimental {
return ret;
}
uint32_t AudioDecoder::readFixedFrames(uint32_t framesToRead, char* pcmBuf)
{
uint32_t framesRead = 0;
uint32_t framesReadOnce = 0;
do
{
framesReadOnce = read(framesToRead - framesRead, pcmBuf + framesRead * _bytesPerFrame);
framesRead += framesReadOnce;
} while (framesReadOnce != 0 && framesRead < framesToRead);
if (framesRead < framesToRead)
{
memset(pcmBuf + framesRead * _bytesPerFrame, 0x00, (framesToRead - framesRead) * _bytesPerFrame);
}
return framesRead;
}
bool AudioDecoder::seek(uint32_t frameOffset)
{
bool ret = false;
+3 -7
View File
@@ -53,16 +53,12 @@ do { \
#endif
#define BREAK_IF(condition) \
do {\
if (!!(condition)) { \
break; \
} \
} while(false)
break; \
}
#define BREAK_IF_ERR_LOG(condition, fmt, ...) \
do {\
if (!!(condition)) { \
ALOGE("(" QUOTEME(condition) ") failed, message: " fmt, ##__VA_ARGS__); \
break; \
} \
} while(false)
}
+2 -2
View File
@@ -278,12 +278,12 @@ void AudioPlayer::rotateBufferThread(int offsetFrame)
}
}
framesRead = decoder.read(framesToRead, tmpBuffer);
framesRead = decoder.readFixedFrames(framesToRead, tmpBuffer);
if (framesRead == 0) {
if (_loop) {
decoder.seek(0);
framesRead = decoder.read(framesToRead, tmpBuffer);
framesRead = decoder.readFixedFrames(framesToRead, tmpBuffer);
} else {
needToExitThread = true;
break;