fixed #16849: Preload many audios may cause crash on devices those use Samsung Exynos CPU. (#17049)

* fixed #16849: Preload many audios may cause crash on devices those use Samsung Exynos CPU.

* Adds comment for __SLPlayerMutex variable.

* Fixes wrong comments in AudioDecoder.cpp
This commit is contained in:
James Chen 2016-12-29 15:41:47 +08:00 committed by minggo
parent ce4685c64f
commit e4e872a065
1 changed files with 25 additions and 13 deletions

View File

@ -34,7 +34,7 @@ THE SOFTWARE.
namespace cocos2d { namespace experimental {
/* Explicitly requesting SL_IID_ANDROIDSIMPLEBUFFERQUEUE and SL_IID_PREFETCHSTATUS
* on the UrlAudioPlayer object for decoding, SL_IID_METADATAEXTRACTION for retrieving the
* on the AudioPlayer object for decoding, SL_IID_METADATAEXTRACTION for retrieving the
* format of the decoded audio */
#define NUM_EXPLICIT_INTERFACES_FOR_PLAYER 3
@ -54,6 +54,9 @@ namespace cocos2d { namespace experimental {
//-----------------------------------------------------------------
// __SLPlayerMutex is a workaround for https://github.com/cocos2d/cocos2d-x/issues/16849
static std::mutex __SLPlayerMutex;
static int toBufferSizeInBytes(int bufferSizeInFrames, int sampleSize, int channelCount)
{
return bufferSizeInFrames * sampleSize * channelCount;
@ -115,7 +118,10 @@ AudioDecoder::AudioDecoder(SLEngineItf engineItf, const std::string &url, int bu
AudioDecoder::~AudioDecoder()
{
ALOGV("~AudioDecoder() %p", this);
SL_DESTROY_OBJ(_playObj);
{
std::lock_guard<std::mutex> lk(__SLPlayerMutex);
SL_DESTROY_OBJ(_playObj);
}
ALOGV("After destroying SL play object");
if (_assetFd > 0)
{
@ -264,16 +270,19 @@ bool AudioDecoder::decodeToPcm()
decDest.pLocator = (void *) &decBuffQueue;
decDest.pFormat = (void *) &pcm;
/* Create the audio player */
result = (*_engineItf)->CreateAudioPlayer(_engineItf, &player, &decSource, &decDest,
NUM_EXPLICIT_INTERFACES_FOR_PLAYER, iidArray,
required);
SL_RETURN_VAL_IF_FAILED(result, false, "CreateAudioPlayer failed");
{
std::lock_guard<std::mutex> lk(__SLPlayerMutex);
/* Create the audio player */
result = (*_engineItf)->CreateAudioPlayer(_engineItf, &player, &decSource, &decDest,
NUM_EXPLICIT_INTERFACES_FOR_PLAYER, iidArray,
required);
SL_RETURN_VAL_IF_FAILED(result, false, "CreateAudioPlayer failed");
_playObj = player;
/* Realize the player in synchronous mode. */
result = (*player)->Realize(player, SL_BOOLEAN_FALSE);
SL_RETURN_VAL_IF_FAILED(result, false, "Realize failed");
_playObj = player;
/* Realize the player in synchronous mode. */
result = (*player)->Realize(player, SL_BOOLEAN_FALSE);
SL_RETURN_VAL_IF_FAILED(result, false, "Realize failed");
}
/* Get the play interface which is implicit */
result = (*player)->GetInterface(player, SL_IID_PLAY, (void *) &playItf);
@ -468,8 +477,11 @@ bool AudioDecoder::decodeToPcm()
ALOGV("Stopped decoding");
/* Destroy the UrlAudioPlayer object */
SL_DESTROY_OBJ(_playObj);
/* Destroy the AudioPlayer object */
{
std::lock_guard<std::mutex> lk(__SLPlayerMutex);
SL_DESTROY_OBJ(_playObj);
}
ALOGV("After destroy player ...");