From 384cfa8b6b2ff6484473576a6855763321c153f9 Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Thu, 1 Dec 2016 09:58:33 +0800 Subject: [PATCH 1/5] fix CocosDenshion namespace compile error --- .../frameworks/runtime-src/Classes/AppDelegate.cpp | 1 - .../frameworks/runtime-src/Classes/AppDelegate.cpp | 1 - .../frameworks/runtime-src/Classes/AppDelegate.cpp | 1 - .../simulator/frameworks/runtime-src/Classes/AppDelegate.cpp | 2 -- 4 files changed, 5 deletions(-) diff --git a/native/templates/js-template-binary/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-binary/frameworks/runtime-src/Classes/AppDelegate.cpp index 7ffb2b7b98..50ffb7aabc 100755 --- a/native/templates/js-template-binary/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-binary/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -28,7 +28,6 @@ using namespace anysdk::framework; #endif USING_NS_CC; -using namespace CocosDenshion; AppDelegate::AppDelegate() { diff --git a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp index 0076993a43..32bd013d4e 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -28,7 +28,6 @@ using namespace anysdk::framework; #endif USING_NS_CC; -using namespace CocosDenshion; AppDelegate::AppDelegate() { diff --git a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp index cdd69c3a8d..630e5af505 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -28,7 +28,6 @@ using namespace anysdk::framework; #endif USING_NS_CC; -using namespace CocosDenshion; AppDelegate::AppDelegate() { diff --git a/native/tools/simulator/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/tools/simulator/frameworks/runtime-src/Classes/AppDelegate.cpp index 6b80d7ef7c..0d2ab4a774 100755 --- a/native/tools/simulator/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/tools/simulator/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -22,8 +22,6 @@ #include "runtime/Runtime.h" #include "ide-support/RuntimeJsImpl.h" -using namespace CocosDenshion; - USING_NS_CC; using namespace std; From 51a5cda2b76c5c340e6518d3914b6b49dbfa89fe Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 2 Dec 2016 10:06:22 +0800 Subject: [PATCH 2/5] Sync Pull Request https://github.com/cocos2d/cocos2d-x/pull/16946 --- .../cocos2d_libs.xcodeproj/project.pbxproj | 12 + native/cocos/audio/apple/AudioCache.h | 59 +--- native/cocos/audio/apple/AudioCache.mm | 276 ++++++++---------- native/cocos/audio/apple/AudioDecoder.h | 107 +++++++ native/cocos/audio/apple/AudioDecoder.mm | 207 +++++++++++++ native/cocos/audio/apple/AudioEngine-inl.h | 20 +- native/cocos/audio/apple/AudioEngine-inl.mm | 94 +++--- native/cocos/audio/apple/AudioMacros.h | 68 +++++ native/cocos/audio/apple/AudioPlayer.h | 20 +- native/cocos/audio/apple/AudioPlayer.mm | 172 +++++------ 10 files changed, 676 insertions(+), 359 deletions(-) create mode 100644 native/cocos/audio/apple/AudioDecoder.h create mode 100644 native/cocos/audio/apple/AudioDecoder.mm create mode 100644 native/cocos/audio/apple/AudioMacros.h diff --git a/native/build/cocos2d_libs.xcodeproj/project.pbxproj b/native/build/cocos2d_libs.xcodeproj/project.pbxproj index 0df3db07b4..38b069d063 100644 --- a/native/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/native/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -552,6 +552,9 @@ 1A570311180BCF190088DEC7 /* CCComponentContainer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A57030A180BCF190088DEC7 /* CCComponentContainer.cpp */; }; 1A570312180BCF190088DEC7 /* CCComponentContainer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A57030B180BCF190088DEC7 /* CCComponentContainer.h */; }; 1A570313180BCF190088DEC7 /* CCComponentContainer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A57030B180BCF190088DEC7 /* CCComponentContainer.h */; }; + 1A5FB7CA1DF10E3500C918C1 /* AudioDecoder.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A5FB7C71DF10E3500C918C1 /* AudioDecoder.mm */; }; + 1A5FB7CB1DF10E3500C918C1 /* AudioDecoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A5FB7C81DF10E3500C918C1 /* AudioDecoder.h */; }; + 1A5FB7CC1DF10E3500C918C1 /* AudioMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A5FB7C91DF10E3500C918C1 /* AudioMacros.h */; }; 1A9DCA27180E6955007A3AD4 /* CCGLBufferedNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A9DCA02180E6955007A3AD4 /* CCGLBufferedNode.cpp */; }; 1A9DCA28180E6955007A3AD4 /* CCGLBufferedNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A9DCA02180E6955007A3AD4 /* CCGLBufferedNode.cpp */; }; 1A9DCA29180E6955007A3AD4 /* CCGLBufferedNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A9DCA03180E6955007A3AD4 /* CCGLBufferedNode.h */; }; @@ -1823,6 +1826,9 @@ 1A570309180BCF190088DEC7 /* CCComponent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCComponent.h; sourceTree = ""; }; 1A57030A180BCF190088DEC7 /* CCComponentContainer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCComponentContainer.cpp; sourceTree = ""; }; 1A57030B180BCF190088DEC7 /* CCComponentContainer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCComponentContainer.h; sourceTree = ""; }; + 1A5FB7C71DF10E3500C918C1 /* AudioDecoder.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = AudioDecoder.mm; sourceTree = ""; }; + 1A5FB7C81DF10E3500C918C1 /* AudioDecoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioDecoder.h; sourceTree = ""; }; + 1A5FB7C91DF10E3500C918C1 /* AudioMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioMacros.h; sourceTree = ""; }; 1A97ABFC1A1D962A0076D9CC /* MathUtilNeon64.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = MathUtilNeon64.inl; sourceTree = ""; }; 1A97ABFD1A1D962A0076D9CC /* MathUtilSSE.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = MathUtilSSE.inl; sourceTree = ""; }; 1A9DCA02180E6955007A3AD4 /* CCGLBufferedNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCGLBufferedNode.cpp; sourceTree = ""; }; @@ -3936,8 +3942,11 @@ children = ( 50CB246F19D9C5A100687767 /* AudioCache.h */, 50CB247019D9C5A100687767 /* AudioCache.mm */, + 1A5FB7C81DF10E3500C918C1 /* AudioDecoder.h */, + 1A5FB7C71DF10E3500C918C1 /* AudioDecoder.mm */, 50CB247119D9C5A100687767 /* AudioEngine-inl.h */, 50CB247219D9C5A100687767 /* AudioEngine-inl.mm */, + 1A5FB7C91DF10E3500C918C1 /* AudioMacros.h */, 50CB247319D9C5A100687767 /* AudioPlayer.h */, 50CB247419D9C5A100687767 /* AudioPlayer.mm */, ); @@ -4471,6 +4480,7 @@ BAFF7C781D59E0DA0051B92F /* CCActionFrameEasing.h in Headers */, 1A5701FD180BCBAD0088DEC7 /* CCMenuItem.h in Headers */, A63CF00E1CD9CF3500A6971D /* CCUITextInput.h in Headers */, + 1A5FB7CB1DF10E3500C918C1 /* AudioDecoder.h in Headers */, ED3057831BEC76C90083C3ED /* unzip.h in Headers */, 1A570204180BCBD40088DEC7 /* CCClippingNode.h in Headers */, BAFF7CE61D59E0DB0051B92F /* CCUtilMath.h in Headers */, @@ -4577,6 +4587,7 @@ 50ABBDA11925AB4100A911A9 /* CCGroupCommand.h in Headers */, BAFF7D6C1D5C1CF80051B92F /* BoneData.h in Headers */, 29394CF019B01DBA00D2DE1A /* UIWebView.h in Headers */, + 1A5FB7CC1DF10E3500C918C1 /* AudioMacros.h in Headers */, BAFF7DBC1D5C1CF80051B92F /* SkeletonRenderer.h in Headers */, 15AE186519AAD31D00C27E9E /* CDOpenALSupport.h in Headers */, 15AE1B5C19AADA9900C27E9E /* UITextAtlas.h in Headers */, @@ -5506,6 +5517,7 @@ 1A5702F2180BCE750088DEC7 /* CCTMXObjectGroup.cpp in Sources */, BAFF7CE41D59E0DB0051B92F /* CCUtilMath.cpp in Sources */, 15AE189419AAD33D00C27E9E /* CCLayerLoader.cpp in Sources */, + 1A5FB7CA1DF10E3500C918C1 /* AudioDecoder.mm in Sources */, 826294351AAF004C00CB7CF7 /* HttpCookie.cpp in Sources */, BAFF7DD41D5C1CF80051B92F /* TransformConstraintData.c in Sources */, 1A5702FA180BCE750088DEC7 /* CCTMXXMLParser.cpp in Sources */, diff --git a/native/cocos/audio/apple/AudioCache.h b/native/cocos/audio/apple/AudioCache.h index 232dbe6ace..422de1736a 100644 --- a/native/cocos/audio/apple/AudioCache.h +++ b/native/cocos/audio/apple/AudioCache.h @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2014-2016 Chukong Technologies Inc. + Copyright (c) 2014 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -28,41 +28,13 @@ #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC #import -#import #include #include #include #include "platform/CCPlatformMacros.h" - -#define QUEUEBUFFER_NUM 3 -#define QUEUEBUFFER_TIME_STEP 0.1 - -#define QUOTEME_(x) #x -#define QUOTEME(x) QUOTEME_(x) - -#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0 -#define ALOGV(fmt, ...) printf("V/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) -#else -#define ALOGV(fmt, ...) do {} while(false) -#endif -#define ALOGD(fmt, ...) printf("D/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) -#define ALOGI(fmt, ...) printf("I/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) -#define ALOGW(fmt, ...) printf("W/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) -#define ALOGE(fmt, ...) printf("E/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) - -#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0 -#define CHECK_AL_ERROR_DEBUG() \ -do { \ - GLenum __error = alGetError(); \ - if (__error) { \ - ALOGE("OpenAL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \ - } \ -} while (false) -#else -#define CHECK_AL_ERROR_DEBUG() -#endif +#include "audio/apple/AudioMacros.h" NS_CC_BEGIN namespace experimental{ @@ -73,7 +45,7 @@ class AudioPlayer; class AudioCache { public: - + enum class State { INITIAL, @@ -81,14 +53,14 @@ public: READY, FAILED }; - + AudioCache(); ~AudioCache(); void addPlayCallback(const std::function& callback); void addLoadCallback(const std::function& callback); - + protected: void setSkipReadDataTask(bool isSkip) { _isSkipReadDataTask = isSkip; }; void readDataTask(unsigned int selfId); @@ -96,46 +68,43 @@ protected: void invokingPlayCallbacks(); void invokingLoadCallbacks(); - + //pcm data related stuff - ALsizei _dataSize; ALenum _format; ALsizei _sampleRate; float _duration; - int _bytesPerFrame; - AudioStreamBasicDescription _outputFormat; + uint32_t _totalFrames; + uint32_t _framesRead; /*Cache related stuff; * Cache pcm data when sizeInBytes less than PCMDATA_CACHEMAXSIZE */ ALuint _alBufferId; char* _pcmData; - SInt64 _bytesOfRead; /*Queue buffer related stuff * Streaming in openal when sizeInBytes greater then PCMDATA_CACHEMAXSIZE */ char* _queBuffers[QUEUEBUFFER_NUM]; ALsizei _queBufferSize[QUEUEBUFFER_NUM]; - UInt32 _queBufferFrames; - UInt32 _queBufferBytes; + uint32_t _queBufferFrames; std::mutex _playCallbackMutex; std::vector< std::function > _playCallbacks; - + // loadCallbacks doesn't need mutex since it's invoked only in Cocos thread. std::vector< std::function > _loadCallbacks; - + std::mutex _readDataTaskMutex; - + State _state; - + std::shared_ptr _isDestroyed; std::string _fileFullPath; unsigned int _id; bool _isLoadingFinished; bool _isSkipReadDataTask; - + friend class AudioEngineImpl; friend class AudioPlayer; } ; diff --git a/native/cocos/audio/apple/AudioCache.mm b/native/cocos/audio/apple/AudioCache.mm index 2eaac01a7d..d4ca3a95b5 100644 --- a/native/cocos/audio/apple/AudioCache.mm +++ b/native/cocos/audio/apple/AudioCache.mm @@ -1,18 +1,18 @@ /**************************************************************************** - Copyright (c) 2014-2016 Chukong Technologies Inc. - + Copyright (c) 2014 Chukong Technologies Inc. + 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 @@ -31,11 +31,12 @@ #import #import -#import #include #include "base/CCDirector.h" #include "base/CCScheduler.h" +#include "audio/apple/AudioDecoder.h" + #define VERY_VERY_VERBOSE_LOGGING #ifdef VERY_VERY_VERBOSE_LOGGING #define ALOGVV ALOGV @@ -54,30 +55,28 @@ typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr) (const ALint bid, ALenum static ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq) { static alBufferDataStaticProcPtr proc = nullptr; - + if (proc == nullptr) { proc = (alBufferDataStaticProcPtr) alcGetProcAddress(nullptr, (const ALCchar*) "alBufferDataStatic"); } - + if (proc){ proc(bid, format, data, size, freq); } - + return; } using namespace cocos2d; using namespace cocos2d::experimental; AudioCache::AudioCache() -: _dataSize(0) +: _totalFrames(0) +, _framesRead(0) , _format(-1) , _duration(0.0f) -, _bytesPerFrame(0) , _alBufferId(INVALID_AL_BUFFER_ID) , _pcmData(nullptr) -, _bytesOfRead(0) , _queBufferFrames(0) -, _queBufferBytes(0) , _state(State::INITIAL) , _isDestroyed(std::make_shared(false)) , _id(++__idIndex) @@ -90,8 +89,6 @@ AudioCache::AudioCache() _queBuffers[i] = nullptr; _queBufferSize[i] = 0; } - - memset(&_outputFormat, 0, sizeof(_outputFormat)); } AudioCache::~AudioCache() @@ -148,94 +145,73 @@ void AudioCache::readDataTask(unsigned int selfId) _readDataTaskMutex.lock(); _state = State::LOADING; - - CFURLRef fileURL = nil; - ExtAudioFileRef extRef = nullptr; - + + AudioDecoder decoder; do { - if (*_isDestroyed) break; - - AudioStreamBasicDescription theFileFormat; - UInt32 thePropertySize = sizeof(theFileFormat); - - SInt64 theFileLengthInFrames; - SInt64 readInFrames; - SInt64 dataSize; - SInt64 frames; - AudioBufferList theDataBuffer; - - NSString *fileFullPath = [[NSString alloc] initWithCString:_fileFullPath.c_str() encoding:NSUTF8StringEncoding]; - fileURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:fileFullPath]; - [fileFullPath release]; - - if (*_isDestroyed) break; + if (!decoder.open(_fileFullPath.c_str())) + break; - auto error = ExtAudioFileOpenURL(fileURL, &extRef); - if (error) { - ALOGE("%s: ExtAudioFileOpenURL FAILED, Error = %ld", __PRETTY_FUNCTION__, (long)error); - break; - } + const uint32_t originalTotalFrames = decoder.getTotalFrames(); + const uint32_t bytesPerFrame = decoder.getBytesPerFrame(); + const uint32_t sampleRate = decoder.getSampleRate(); + const uint32_t channelCount = decoder.getChannelCount(); + + uint32_t totalFrames = originalTotalFrames; + uint32_t dataSize = totalFrames * bytesPerFrame; + uint32_t remainingFrames = totalFrames; + uint32_t adjustFrames = 0; + + _format = channelCount > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; + _sampleRate = (ALsizei)sampleRate; + _duration = 1.0f * totalFrames / sampleRate; + _totalFrames = totalFrames; - if (*_isDestroyed) break; - - // Get the audio data format - error = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &theFileFormat); - if(error) + if (dataSize <= PCMDATA_CACHEMAXSIZE) { - ALOGE("%s: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld", __PRETTY_FUNCTION__, (long)error); - break; - } - - if (theFileFormat.mChannelsPerFrame > 2) - { - ALOGE("%s: Unsupported Format, channel count is greater than stereo",__PRETTY_FUNCTION__); - break; - } - - if (*_isDestroyed) break; - - // Set the client format to 16 bit signed integer (native-endian) data - // Maintain the channel count and sample rate of the original source format - _outputFormat.mSampleRate = theFileFormat.mSampleRate; - _outputFormat.mChannelsPerFrame = theFileFormat.mChannelsPerFrame; - - _bytesPerFrame = 2 * _outputFormat.mChannelsPerFrame; - _outputFormat.mFormatID = kAudioFormatLinearPCM; - _outputFormat.mBytesPerPacket = _bytesPerFrame; - _outputFormat.mFramesPerPacket = 1; - _outputFormat.mBytesPerFrame = _bytesPerFrame; - _outputFormat.mBitsPerChannel = 16; - _outputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger; - - error = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(_outputFormat), &_outputFormat); - if(error) - { - ALOGE("%s: ExtAudioFileSetProperty FAILED, Error = %ld", __PRETTY_FUNCTION__, (long)error); - break; - } - - if (*_isDestroyed) break; - - // Get the total frame count - thePropertySize = sizeof(theFileLengthInFrames); - error = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames); - if(error) - { - ALOGE("%s: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld", __PRETTY_FUNCTION__, (long)error); - break; - } - - if (*_isDestroyed) break; - - _dataSize = (ALsizei)(theFileLengthInFrames * _outputFormat.mBytesPerFrame); - _format = (_outputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; - _sampleRate = (ALsizei)_outputFormat.mSampleRate; - _duration = 1.0f * theFileLengthInFrames / _outputFormat.mSampleRate; - - if (_dataSize <= PCMDATA_CACHEMAXSIZE) - { - _pcmData = (char*)malloc(_dataSize); + uint32_t framesRead = 0; + const uint32_t framesToReadOnce = std::min(totalFrames, static_cast(sampleRate * QUEUEBUFFER_TIME_STEP * QUEUEBUFFER_NUM)); + + BREAK_IF_ERR_LOG(!decoder.seek(totalFrames), "AudioDecoder::seek(%u) error", totalFrames); + + char* tmpBuf = (char*)malloc(framesToReadOnce * bytesPerFrame); + std::vector adjustFrameBuf; + adjustFrameBuf.reserve(framesToReadOnce * bytesPerFrame); + + do + { + framesRead = decoder.read(framesToReadOnce, tmpBuf); + if (framesRead > 0) + { + adjustFrames += framesRead; + adjustFrameBuf.insert(adjustFrameBuf.end(), tmpBuf, tmpBuf + framesRead * bytesPerFrame); + } + + } while (framesRead > 0); + + if (adjustFrames > 0) + { + ALOGV("Orignal total frames: %u, adjust frames: %u, current total frames: %u", totalFrames, adjustFrames, totalFrames + adjustFrames); + totalFrames += adjustFrames; + _totalFrames = remainingFrames = totalFrames; + } + + // Reset dataSize + dataSize = totalFrames * bytesPerFrame; + + free(tmpBuf); + + // Reset to frame 0 + BREAK_IF_ERR_LOG(!decoder.seek(0), "AudioDecoder::seek(0) failed!"); + + _pcmData = (char*)malloc(dataSize); + memset(_pcmData, 0, dataSize); + + if (adjustFrames > 0) + { + memcpy(_pcmData + (dataSize - adjustFrameBuf.size()), adjustFrameBuf.data(), adjustFrameBuf.size()); + } + alGenBuffers(1, &_alBufferId); auto alError = alGetError(); if (alError != AL_NO_ERROR) { @@ -243,67 +219,60 @@ void AudioCache::readDataTask(unsigned int selfId) break; } - if (*_isDestroyed) break; + if (*_isDestroyed) + break; - alBufferDataStaticProc(_alBufferId, _format, _pcmData, _dataSize, _sampleRate); + 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); - readInFrames = theFileFormat.mSampleRate * QUEUEBUFFER_TIME_STEP * QUEUEBUFFER_NUM; - dataSize = _outputFormat.mBytesPerFrame * readInFrames; - if (dataSize > _dataSize) { - dataSize = _dataSize; - readInFrames = theFileLengthInFrames; - } - theDataBuffer.mNumberBuffers = 1; - theDataBuffer.mBuffers[0].mDataByteSize = (UInt32)dataSize; - theDataBuffer.mBuffers[0].mNumberChannels = _outputFormat.mChannelsPerFrame; - - theDataBuffer.mBuffers[0].mData = _pcmData; - frames = readInFrames; - ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer); - - if (*_isDestroyed) break; + if (*_isDestroyed) + break; _state = State::READY; - - _bytesOfRead += dataSize; + invokingPlayCallbacks(); - - while (!*_isDestroyed && _bytesOfRead + dataSize < _dataSize) { - theDataBuffer.mBuffers[0].mData = _pcmData + _bytesOfRead; - frames = readInFrames; - ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer); - _bytesOfRead += dataSize; //FIXME: the buffer size of read frames may be fewer than dataSize. + + uint32_t frames = 0; + while (!*_isDestroyed && _framesRead < originalTotalFrames) + { + frames = std::min(framesToReadOnce, remainingFrames); + if (_framesRead + frames > originalTotalFrames) + { + frames = originalTotalFrames - _framesRead; + } + framesRead = decoder.read(frames, _pcmData + _framesRead * bytesPerFrame); + if (framesRead == 0) + break; + _framesRead += framesRead; + remainingFrames -= framesRead; } - - dataSize = _dataSize - _bytesOfRead; - if (!*_isDestroyed && dataSize > 0) { - theDataBuffer.mBuffers[0].mDataByteSize = (UInt32)dataSize; - theDataBuffer.mBuffers[0].mData = _pcmData + _bytesOfRead; - frames = readInFrames; - ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer); - } - - _bytesOfRead = _dataSize; + + CCASSERT(_framesRead == originalTotalFrames, "The read frame count isn't equal to orignal frame count!"); + ALOGV("pcm buffer was loaded successfully, total frames: %u, total read frames: %u, adjust frames: %u, remainingFrames: %u", totalFrames, _framesRead, adjustFrames, remainingFrames); + + _framesRead += adjustFrames; } else { - _queBufferFrames = theFileFormat.mSampleRate * QUEUEBUFFER_TIME_STEP; - if (_queBufferFrames == 0) - break; - - _queBufferBytes = _queBufferFrames * _outputFormat.mBytesPerFrame; - - theDataBuffer.mNumberBuffers = 1; - theDataBuffer.mBuffers[0].mNumberChannels = _outputFormat.mChannelsPerFrame; - for (int index = 0; index < QUEUEBUFFER_NUM; ++index) { - _queBuffers[index] = (char*)malloc(_queBufferBytes); - - theDataBuffer.mBuffers[0].mDataByteSize = _queBufferBytes; - theDataBuffer.mBuffers[0].mData = _queBuffers[index]; - frames = _queBufferFrames; - ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer); - - _queBufferSize[index] = theDataBuffer.mBuffers[0].mDataByteSize; + _queBufferFrames = sampleRate * QUEUEBUFFER_TIME_STEP; + BREAK_IF_ERR_LOG(_queBufferFrames == 0, "_queBufferFrames == 0"); + + const uint32_t queBufferBytes = _queBufferFrames * bytesPerFrame; + + for (int index = 0; index < QUEUEBUFFER_NUM; ++index) + { + _queBuffers[index] = (char*)malloc(queBufferBytes); + _queBufferSize[index] = queBufferBytes; + + decoder.read(_queBufferFrames, _queBuffers[index]); } _state = State::READY; @@ -311,11 +280,7 @@ void AudioCache::readDataTask(unsigned int selfId) } while (false); - if (fileURL != nil) - CFRelease(fileURL); - - if (extRef != nullptr) - ExtAudioFileDispose(extRef); + decoder.close(); //FIXME: Why to invoke play callback first? Should it be after 'load' callback? invokingPlayCallbacks(); @@ -414,6 +379,7 @@ void AudioCache::invokingLoadCallbacks() { cb(_state == State::READY); } + _loadCallbacks.clear(); }); } diff --git a/native/cocos/audio/apple/AudioDecoder.h b/native/cocos/audio/apple/AudioDecoder.h new file mode 100644 index 0000000000..6597425f28 --- /dev/null +++ b/native/cocos/audio/apple/AudioDecoder.h @@ -0,0 +1,107 @@ +/**************************************************************************** + Copyright (c) 2016 Chukong Technologies Inc. + + 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 +#import + +namespace cocos2d { namespace experimental { + +/** + * @brief The class for decoding compressed audio file to PCM buffer. + */ +class AudioDecoder +{ +public: + static const uint32_t INVALID_FRAME_INDEX = UINT32_MAX; + + AudioDecoder(); + ~AudioDecoder(); + + /** + * @brief Opens an audio file specified by a file path. + * @return true if succeed, otherwise false. + */ + bool open(const char* path); + + /** + * @brief Checks whether decoder has opened file successfully. + * @return true if succeed, otherwise false. + */ + bool isOpened() const; + + /** + * @brief Closes opened audio file. + * @note The method will also be automatically invoked in the destructor. + */ + void close(); + + /** + * @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. + * @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 Sets frame offest to be read. + * @param frameOffset The frame offest to be set. + * @return true if succeed, otherwise false + */ + bool seek(uint32_t frameOffset); + + /** + * @brief Tells the current frame offset. + * @return The current frame offset. + */ + uint32_t tell() const; + + /** Gets total frames of current audio.*/ + uint32_t getTotalFrames() const; + + /** Gets bytes per frame of current audio.*/ + uint32_t getBytesPerFrame() const; + + /** Gets sample rate of current audio.*/ + uint32_t getSampleRate() const; + + /** Gets the channel count of current audio. + * @note Currently we only support 1 or 2 channels. + */ + uint32_t getChannelCount() const; + +private: + bool _isOpened; + ExtAudioFileRef _extRef; + uint32_t _totalFrames; + uint32_t _bytesPerFrame; + uint32_t _sampleRate; + uint32_t _channelCount; + + AudioStreamBasicDescription _outputFormat; +}; + +}} // namespace cocos2d { namespace experimental { diff --git a/native/cocos/audio/apple/AudioDecoder.mm b/native/cocos/audio/apple/AudioDecoder.mm new file mode 100644 index 0000000000..810542f70c --- /dev/null +++ b/native/cocos/audio/apple/AudioDecoder.mm @@ -0,0 +1,207 @@ +/**************************************************************************** + Copyright (c) 2016 Chukong Technologies Inc. + + 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. + ****************************************************************************/ + +#include "audio/apple/AudioDecoder.h" +#include "audio/apple/AudioMacros.h" + +#define LOG_TAG "AudioDecoder" + +namespace cocos2d { namespace experimental { + + AudioDecoder::AudioDecoder() + : _isOpened(false) + , _extRef(nullptr) + , _totalFrames(0) + , _bytesPerFrame(0) + , _sampleRate(0) + , _channelCount(0) + { + memset(&_outputFormat, 0, sizeof(_outputFormat)); + } + + AudioDecoder::~AudioDecoder() + { + close(); + } + + bool AudioDecoder::open(const char* path) + { + bool ret = false; + CFURLRef fileURL = nil; + do + { + BREAK_IF_ERR_LOG(path == nullptr || strlen(path) == 0, "Invalid path!"); + + NSString *fileFullPath = [[NSString alloc] initWithCString:path encoding:NSUTF8StringEncoding]; + fileURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:fileFullPath]; + [fileFullPath release]; + BREAK_IF_ERR_LOG(fileURL == nil, "Converting path to CFURLRef failed!"); + + OSStatus status = ExtAudioFileOpenURL(fileURL, &_extRef); + BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileOpenURL FAILED, Error = %ld", (long)ret); + + AudioStreamBasicDescription fileFormat; + UInt32 propertySize = sizeof(fileFormat); + + // Get the audio data format + ret = ExtAudioFileGetProperty(_extRef, kExtAudioFileProperty_FileDataFormat, &propertySize, &fileFormat); + BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld", (long)ret); + BREAK_IF_ERR_LOG(fileFormat.mChannelsPerFrame > 2, "Unsupported Format, channel count is greater than stereo!"); + + // Set the client format to 16 bit signed integer (native-endian) data + // Maintain the channel count and sample rate of the original source format + _outputFormat.mSampleRate = fileFormat.mSampleRate; + _outputFormat.mChannelsPerFrame = fileFormat.mChannelsPerFrame; + _outputFormat.mFormatID = kAudioFormatLinearPCM; + _outputFormat.mFramesPerPacket = 1; + _outputFormat.mBitsPerChannel = 16; + _outputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger; + + _sampleRate = _outputFormat.mSampleRate; + _channelCount = _outputFormat.mChannelsPerFrame; + _bytesPerFrame = 2 * _outputFormat.mChannelsPerFrame; + + _outputFormat.mBytesPerPacket = _bytesPerFrame; + _outputFormat.mBytesPerFrame = _bytesPerFrame; + + ret = ExtAudioFileSetProperty(_extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(_outputFormat), &_outputFormat); + BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileSetProperty FAILED, Error = %ld", (long)ret); + + // Get the total frame count + SInt64 totalFrames = 0; + propertySize = sizeof(totalFrames); + ret = ExtAudioFileGetProperty(_extRef, kExtAudioFileProperty_FileLengthFrames, &propertySize, &totalFrames); + BREAK_IF_ERR_LOG(status != noErr, "ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld", (long)ret); + _totalFrames = static_cast(totalFrames); + _isOpened = true; + + ret = true; + } while (false); + + if (fileURL != nil) + CFRelease(fileURL); + + if (!ret) + { + close(); + } + + return ret; + } + + void AudioDecoder::close() + { + if (_extRef != nullptr) + { + ExtAudioFileDispose(_extRef); + _extRef = nullptr; + + _totalFrames = 0; + _bytesPerFrame = 0; + _sampleRate = 0; + _channelCount = 0; + } + } + + uint32_t AudioDecoder::read(uint32_t framesToRead, char* pcmBuf) + { + uint32_t ret = 0; + do + { + BREAK_IF_ERR_LOG(!isOpened(), "decoder isn't openned"); + BREAK_IF_ERR_LOG(framesToRead == INVALID_FRAME_INDEX, "frameToRead is INVALID_FRAME_INDEX"); + BREAK_IF_ERR_LOG(framesToRead == 0, "frameToRead is 0"); + BREAK_IF_ERR_LOG(pcmBuf == nullptr, "pcmBuf is nullptr"); + + AudioBufferList bufferList; + bufferList.mNumberBuffers = 1; + bufferList.mBuffers[0].mDataByteSize = framesToRead * _bytesPerFrame; + bufferList.mBuffers[0].mNumberChannels = _outputFormat.mChannelsPerFrame; + bufferList.mBuffers[0].mData = pcmBuf; + + UInt32 frames = framesToRead; + OSStatus status = ExtAudioFileRead(_extRef, &frames, &bufferList); + BREAK_IF(status != noErr); + ret = frames; + } while (false); + + return ret; + } + + bool AudioDecoder::seek(uint32_t frameOffset) + { + bool ret = false; + do + { + BREAK_IF_ERR_LOG(!isOpened(), "decoder isn't openned"); + BREAK_IF_ERR_LOG(frameOffset == INVALID_FRAME_INDEX, "frameIndex is INVALID_FRAME_INDEX"); + + OSStatus status = ExtAudioFileSeek(_extRef, frameOffset); + BREAK_IF(status != noErr); + ret = true; + } while(false); + return ret; + } + + uint32_t AudioDecoder::tell() const + { + uint32_t ret = INVALID_FRAME_INDEX; + do + { + BREAK_IF_ERR_LOG(!isOpened(), "decoder isn't openned"); + SInt64 frameIndex = INVALID_FRAME_INDEX; + OSStatus status = ExtAudioFileTell(_extRef, &frameIndex); + BREAK_IF(status != noErr); + ret = static_cast(frameIndex); + } while(false); + + return ret; + } + + uint32_t AudioDecoder::getTotalFrames() const + { + return _totalFrames; + } + + uint32_t AudioDecoder::getBytesPerFrame() const + { + return _bytesPerFrame; + } + + uint32_t AudioDecoder::getSampleRate() const + { + return _sampleRate; + } + + uint32_t AudioDecoder::getChannelCount() const + { + return _channelCount; + } + + bool AudioDecoder::isOpened() const + { + return _isOpened; + } + +}} // namespace cocos2d { namespace experimental { diff --git a/native/cocos/audio/apple/AudioEngine-inl.h b/native/cocos/audio/apple/AudioEngine-inl.h index dad37a0d5c..6a8c9257c1 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.h +++ b/native/cocos/audio/apple/AudioEngine-inl.h @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2014-2016 Chukong Technologies Inc. + Copyright (c) 2014 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -45,7 +45,7 @@ class AudioEngineImpl : public cocos2d::Ref public: AudioEngineImpl(); ~AudioEngineImpl(); - + bool init(); int play2d(const std::string &fileFullPath ,bool loop ,float volume); void setVolume(int audioID,float volume); @@ -58,29 +58,29 @@ public: float getCurrentTime(int audioID); bool setCurrentTime(int audioID, float time); void setFinishCallback(int audioID, const std::function &callback); - + void uncache(const std::string& filePath); void uncacheAll(); AudioCache* preload(const std::string& filePath, std::function callback); void update(float dt); - + private: void _play2d(AudioCache *cache, int audioID); - + ALuint _alSources[MAX_AUDIOINSTANCES]; - + //source,used std::unordered_map _alSourceUsed; - + //filePath,bufferInfo std::unordered_map _audioCaches; - + //audioID,AudioInfo std::unordered_map _audioPlayers; std::mutex _threadMutex; - + bool _lazyInitLoop; - + int _currentAudioID; Scheduler* _scheduler; }; diff --git a/native/cocos/audio/apple/AudioEngine-inl.mm b/native/cocos/audio/apple/AudioEngine-inl.mm index 6e63cd6edd..52ad4d00ab 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.mm +++ b/native/cocos/audio/apple/AudioEngine-inl.mm @@ -1,18 +1,18 @@ /**************************************************************************** - Copyright (c) 2014-2016 Chukong Technologies Inc. - + Copyright (c) 2014 Chukong Technologies Inc. + 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 @@ -67,7 +67,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio else if (kAudioSessionEndInterruption == interruption_state) { OSStatus result = AudioSessionSetActive(true); - if (result) NSLog(@"Error setting audio session active! %d\n", result); + if (result) NSLog(@"Error setting audio session active! %d\n", static_cast(result)); alcMakeContextCurrent(s_ALContext); } @@ -96,13 +96,13 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio -(void)handleInterruption:(NSNotification*)notification { static bool resumeOnBecomingActive = false; - + if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) { NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] integerValue]; if (reason == AVAudioSessionInterruptionTypeBegan) { alcMakeContextCurrent(NULL); } - + if (reason == AVAudioSessionInterruptionTypeEnded) { if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { NSError *error = nil; @@ -113,7 +113,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio } } } - + if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification] && resumeOnBecomingActive) { resumeOnBecomingActive = false; NSError *error = nil; @@ -133,7 +133,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio { [[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; - + [super dealloc]; } @end @@ -145,16 +145,16 @@ AudioEngineImpl::AudioEngineImpl() : _lazyInitLoop(true) , _currentAudioID(0) { - + } AudioEngineImpl::~AudioEngineImpl() { if (s_ALContext) { alDeleteSources(MAX_AUDIOINSTANCES, _alSources); - + _audioCaches.clear(); - + alcMakeContextCurrent(nullptr); alcDestroyContext(s_ALContext); } @@ -174,13 +174,13 @@ bool AudioEngineImpl::init() #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS s_AudioEngineSessionHandler = [[AudioEngineSessionHandler alloc] init]; #endif - + s_ALDevice = alcOpenDevice(nullptr); - + if (s_ALDevice) { s_ALContext = alcCreateContext(s_ALDevice, nullptr); alcMakeContextCurrent(s_ALContext); - + alGenSources(MAX_AUDIOINSTANCES, _alSources); auto alError = alGetError(); if(alError != AL_NO_ERROR) @@ -188,7 +188,7 @@ bool AudioEngineImpl::init() ALOGE("%s:generating sources failed! error = %x", __PRETTY_FUNCTION__, alError); break; } - + for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) { _alSourceUsed[_alSources[i]] = false; } @@ -260,14 +260,14 @@ bool AudioEngineImpl::init() ALOGI("OpenAL was initialized successfully!"); } }while (false); - + return ret; } AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function callback) { AudioCache* audioCache = nullptr; - + auto it = _audioCaches.find(filePath); if (it == _audioCaches.end()) { audioCache = &_audioCaches[filePath]; @@ -287,8 +287,8 @@ AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function< else { audioCache = &it->second; } - - if(audioCache && callback) + + if (audioCache && callback) { audioCache->addLoadCallback(callback); } @@ -300,12 +300,12 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume if (s_ALDevice == nullptr) { 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; @@ -314,7 +314,7 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume if(!sourceFlag){ return AudioEngine::INVALID_AUDIO_ID; } - + auto player = new (std::nothrow) AudioPlayer; if (player == nullptr) { return AudioEngine::INVALID_AUDIO_ID; @@ -323,7 +323,7 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume player->_alSource = alSource; player->_loop = loop; player->_volume = volume; - + auto audioCache = preload(filePath, nullptr); if (audioCache == nullptr) { delete player; @@ -343,7 +343,7 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume _lazyInitLoop = false; _scheduler->schedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this, 0.05f, false); } - + return _currentAudioID++; } @@ -379,10 +379,10 @@ void AudioEngineImpl::setVolume(int audioID,float volume) { auto player = _audioPlayers[audioID]; player->_volume = volume; - + if (player->_ready) { alSourcef(_audioPlayers[audioID]->_alSource, AL_GAIN, volume); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); @@ -393,7 +393,7 @@ void AudioEngineImpl::setVolume(int audioID,float volume) void AudioEngineImpl::setLoop(int audioID, bool loop) { auto player = _audioPlayers[audioID]; - + if (player->_ready) { if (player->_streamingSource) { player->setLoop(loop); @@ -403,7 +403,7 @@ void AudioEngineImpl::setLoop(int audioID, bool loop) } else { alSourcei(player->_alSource, AL_LOOPING, AL_FALSE); } - + auto error = alGetError(); if (error != AL_NO_ERROR) { ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); @@ -419,13 +419,13 @@ bool AudioEngineImpl::pause(int audioID) { bool ret = true; alSourcePause(_audioPlayers[audioID]->_alSource); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ret = false; ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); } - + return ret; } @@ -433,13 +433,13 @@ bool AudioEngineImpl::resume(int audioID) { bool ret = true; alSourcePlay(_audioPlayers[audioID]->_alSource); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ret = false; ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); } - + return ret; } @@ -485,14 +485,14 @@ float AudioEngineImpl::getCurrentTime(int audioID) ret = player->getTime(); } else { alGetSourcef(player->_alSource, AL_SEC_OFFSET, &ret); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ALOGE("%s, audio id:%d,error code:%x", __PRETTY_FUNCTION__,audioID,error); } } } - + return ret; } @@ -500,25 +500,25 @@ bool AudioEngineImpl::setCurrentTime(int audioID, float time) { bool ret = false; auto player = _audioPlayers[audioID]; - + do { if (!player->_ready) { break; } - + if (player->_streamingSource) { ret = player->setTime(time); break; } else { - if (player->_audioCache->_bytesOfRead != player->_audioCache->_dataSize && - (time * player->_audioCache->_sampleRate * player->_audioCache->_bytesPerFrame) > player->_audioCache->_bytesOfRead) { + if (player->_audioCache->_framesRead != player->_audioCache->_totalFrames && + (time * player->_audioCache->_sampleRate) > player->_audioCache->_framesRead) { ALOGE("%s: audio id = %d", __PRETTY_FUNCTION__,audioID); break; } - + alSourcef(player->_alSource, AL_SEC_OFFSET, time); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); @@ -526,7 +526,7 @@ bool AudioEngineImpl::setCurrentTime(int audioID, float time) ret = true; } } while (0); - + return ret; } @@ -547,8 +547,8 @@ void AudioEngineImpl::update(float dt) audioID = it->first; player = it->second; alGetSourcei(player->_alSource, AL_SOURCE_STATE, &sourceState); - - if(player->_removeByAudioEngine) + + if (player->_removeByAudioEngine) { _alSourceUsed[player->_alSource] = false; @@ -565,7 +565,7 @@ void AudioEngineImpl::update(float dt) auto& audioInfo = AudioEngine::_audioIDInfoMap[audioID]; player->_finishCallbak(audioID, *audioInfo.filePath); //FIXME: callback will delay 50ms } - + AudioEngine::remove(audioID); delete player; _threadMutex.lock(); @@ -576,7 +576,7 @@ void AudioEngineImpl::update(float dt) ++it; } } - + if(_audioPlayers.empty()){ _lazyInitLoop = true; _scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this); diff --git a/native/cocos/audio/apple/AudioMacros.h b/native/cocos/audio/apple/AudioMacros.h new file mode 100644 index 0000000000..fa3a062799 --- /dev/null +++ b/native/cocos/audio/apple/AudioMacros.h @@ -0,0 +1,68 @@ +/**************************************************************************** + Copyright (c) 2016 Chukong Technologies Inc. + + 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 + +#define QUEUEBUFFER_NUM (3) +#define QUEUEBUFFER_TIME_STEP (0.1f) + +#define QUOTEME_(x) #x +#define QUOTEME(x) QUOTEME_(x) + +#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0 +#define ALOGV(fmt, ...) printf("V/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) +#else +#define ALOGV(fmt, ...) do {} while(false) +#endif +#define ALOGD(fmt, ...) printf("D/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) +#define ALOGI(fmt, ...) printf("I/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) +#define ALOGW(fmt, ...) printf("W/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) +#define ALOGE(fmt, ...) printf("E/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__) + +#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0 +#define CHECK_AL_ERROR_DEBUG() \ +do { \ + GLenum __error = alGetError(); \ + if (__error) { \ + ALOGE("OpenAL error 0x%04X in %s %s %d\n", __error, __FILE__, __FUNCTION__, __LINE__); \ + } \ +} while (false) +#else +#define CHECK_AL_ERROR_DEBUG() +#endif + +#define BREAK_IF(condition) \ +do {\ + if (!!(condition)) { \ + break; \ + } \ +} while(false) + +#define BREAK_IF_ERR_LOG(condition, fmt, ...) \ +do {\ + if (!!(condition)) { \ + ALOGE("(" QUOTEME(condition) ") failed, message: " fmt, ##__VA_ARGS__); \ + break; \ + } \ +} while(false) diff --git a/native/cocos/audio/apple/AudioPlayer.h b/native/cocos/audio/apple/AudioPlayer.h index 2215e7d980..2049079bcc 100644 --- a/native/cocos/audio/apple/AudioPlayer.h +++ b/native/cocos/audio/apple/AudioPlayer.h @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2014-2016 Chukong Technologies Inc. + Copyright (c) 2014 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -45,30 +45,30 @@ class AudioPlayer public: AudioPlayer(); ~AudioPlayer(); - + void destroy(); - + //queue buffer related stuff bool setTime(float time); float getTime() { return _currTime;} bool setLoop(bool loop); - + protected: void setCache(AudioCache* cache); void rotateBufferThread(int offsetFrame); bool play2d(); AudioCache* _audioCache; - + float _volume; bool _loop; std::function _finishCallbak; - + bool _isDestroyed; bool _removeByAudioEngine; bool _ready; ALuint _alSource; - + //play by circular buffer float _currTime; bool _streamingSource; @@ -78,11 +78,11 @@ protected: std::mutex _sleepMutex; bool _timeDirty; bool _isRotateThreadExited; - + std::mutex _play2dMutex; - + unsigned int _id; - + friend class AudioEngineImpl; }; diff --git a/native/cocos/audio/apple/AudioPlayer.mm b/native/cocos/audio/apple/AudioPlayer.mm index 0bf53218c6..4bdd571c39 100644 --- a/native/cocos/audio/apple/AudioPlayer.mm +++ b/native/cocos/audio/apple/AudioPlayer.mm @@ -1,18 +1,18 @@ /**************************************************************************** - Copyright (c) 2014-2016 Chukong Technologies Inc. - + Copyright (c) 2014 Chukong Technologies Inc. + 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 @@ -32,7 +32,7 @@ #include "audio/apple/AudioPlayer.h" #include "audio/apple/AudioCache.h" #include "platform/CCFileUtils.h" -#import +#include "audio/apple/AudioDecoder.h" #define VERY_VERY_VERBOSE_LOGGING #ifdef VERY_VERY_VERBOSE_LOGGING @@ -68,7 +68,7 @@ AudioPlayer::~AudioPlayer() { ALOGVV("~AudioPlayer() (%p), id=%u", this, _id); destroy(); - + if (_streamingSource) { alDeleteBuffers(3, _bufferIds); @@ -79,11 +79,11 @@ void AudioPlayer::destroy() { if (_isDestroyed) return; - + ALOGVV("AudioPlayer::destroy begin, id=%u", _id); _isDestroyed = true; - + do { if (_audioCache != nullptr) @@ -93,17 +93,17 @@ void AudioPlayer::destroy() ALOGV("AudioPlayer::destroy, id=%u, cache isn't ready!", _id); break; } - + while (!_audioCache->_isLoadingFinished) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); } } - + // Wait for play2d to be finished. _play2dMutex.lock(); _play2dMutex.unlock(); - + if (_streamingSource) { if (_rotateBufferThread != nullptr) @@ -113,25 +113,25 @@ void AudioPlayer::destroy() _sleepCondition.notify_one(); std::this_thread::sleep_for(std::chrono::milliseconds(5)); } - + if (_rotateBufferThread->joinable()) { _rotateBufferThread->join(); } - + delete _rotateBufferThread; _rotateBufferThread = nullptr; ALOGVV("rotateBufferThread exited!"); } } } while(false); - + ALOGVV("Before alSourceStop"); alSourceStop(_alSource); CHECK_AL_ERROR_DEBUG(); ALOGVV("Before alSourcei"); alSourcei(_alSource, AL_BUFFER, NULL); CHECK_AL_ERROR_DEBUG(); - + _removeByAudioEngine = true; - + _ready = false; ALOGVV("AudioPlayer::destroy end, id=%u", _id); } @@ -236,91 +236,79 @@ bool AudioPlayer::play2d() void AudioPlayer::rotateBufferThread(int offsetFrame) { - ALint sourceState; - ALint bufferProcessed = 0; - ExtAudioFileRef extRef = nullptr; + char* tmpBuffer = nullptr; + AudioDecoder decoder; + do + { + BREAK_IF(!decoder.open(_audioCache->_fileFullPath.c_str())); - NSString *fileFullPath = [[NSString alloc] initWithCString:_audioCache->_fileFullPath.c_str() encoding:NSUTF8StringEncoding]; - auto fileURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:fileFullPath]; - [fileFullPath release]; - char* tmpBuffer = (char*)malloc(_audioCache->_queBufferBytes); - auto frames = _audioCache->_queBufferFrames; + uint32_t framesRead = 0; + const uint32_t framesToRead = _audioCache->_queBufferFrames; + const uint32_t bufferSize = framesToRead * decoder.getBytesPerFrame(); + tmpBuffer = (char*)malloc(bufferSize); + memset(tmpBuffer, 0, bufferSize); - auto error = ExtAudioFileOpenURL(fileURL, &extRef); - if(error) { - ALOGE("%s: ExtAudioFileOpenURL FAILED, Error = %ld", __PRETTY_FUNCTION__,(long) error); - goto ExitBufferThread; - } + if (offsetFrame != 0) { + decoder.seek(offsetFrame); + } - error = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(_audioCache->_outputFormat), &_audioCache->_outputFormat); - AudioBufferList theDataBuffer; - theDataBuffer.mNumberBuffers = 1; - theDataBuffer.mBuffers[0].mData = tmpBuffer; - theDataBuffer.mBuffers[0].mDataByteSize = _audioCache->_queBufferBytes; - theDataBuffer.mBuffers[0].mNumberChannels = _audioCache->_outputFormat.mChannelsPerFrame; + ALint sourceState; + ALint bufferProcessed = 0; + bool needToExitThread = false; - if (offsetFrame != 0) { - ExtAudioFileSeek(extRef, offsetFrame); - } - - 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; - offsetFrame = _currTime * _audioCache->_outputFormat.mSampleRate; - ExtAudioFileSeek(extRef, offsetFrame); - } - else { - _currTime += QUEUEBUFFER_TIME_STEP; - if (_currTime > _audioCache->_duration) { - if (_loop) { - _currTime = 0.0f; - } else { - _currTime = _audioCache->_duration; + 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; + offsetFrame = _currTime * decoder.getSampleRate(); + decoder.seek(offsetFrame); + } + else { + _currTime += QUEUEBUFFER_TIME_STEP; + if (_currTime > _audioCache->_duration) { + if (_loop) { + _currTime = 0.0f; + } else { + _currTime = _audioCache->_duration; + } } } - } - frames = _audioCache->_queBufferFrames; - ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer); - if (frames <= 0) { - if (_loop) { - ExtAudioFileSeek(extRef, 0); - frames = _audioCache->_queBufferFrames; - theDataBuffer.mBuffers[0].mDataByteSize = _audioCache->_queBufferBytes; - ExtAudioFileRead(extRef, (UInt32*)&frames, &theDataBuffer); - } else { - _isDestroyed = true; - break; + framesRead = decoder.read(framesToRead, tmpBuffer); + + if (framesRead == 0) { + if (_loop) { + decoder.seek(0); + framesRead = decoder.read(framesToRead, tmpBuffer); + } else { + needToExitThread = true; + break; + } } + + ALuint bid; + alSourceUnqueueBuffers(_alSource, 1, &bid); + alBufferData(bid, _audioCache->_format, tmpBuffer, framesRead * decoder.getBytesPerFrame(), decoder.getSampleRate()); + alSourceQueueBuffers(_alSource, 1, &bid); } - - ALuint bid; - alSourceUnqueueBuffers(_alSource, 1, &bid); - alBufferData(bid, _audioCache->_format, tmpBuffer, frames * _audioCache->_outputFormat.mBytesPerFrame, _audioCache->_sampleRate); - alSourceQueueBuffers(_alSource, 1, &bid); } + + std::unique_lock lk(_sleepMutex); + if (_isDestroyed || needToExitThread) { + break; + } + + _sleepCondition.wait_for(lk,std::chrono::milliseconds(75)); } + + } while(false); - std::unique_lock lk(_sleepMutex); - if (_isDestroyed) { - break; - } - - _sleepCondition.wait_for(lk,std::chrono::milliseconds(75)); - } - -ExitBufferThread: ALOGV("Exit rotate buffer thread ..."); - CFRelease(fileURL); - // Dispose the ExtAudioFileRef, it is no longer needed - if (extRef){ - ExtAudioFileDispose(extRef); - } + decoder.close(); free(tmpBuffer); _isRotateThreadExited = true; } @@ -331,17 +319,17 @@ bool AudioPlayer::setLoop(bool loop) _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; From f33abbaa744548c0073864419c098f56041d89cf Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 2 Dec 2016 10:22:49 +0800 Subject: [PATCH 3/5] Updates license years and deletes tailing white spaces. --- native/cocos/audio/apple/AudioCache.h | 23 ++-- native/cocos/audio/apple/AudioCache.mm | 62 +++++----- native/cocos/audio/apple/AudioDecoder.h | 4 +- native/cocos/audio/apple/AudioEngine-inl.h | 21 ++-- native/cocos/audio/apple/AudioEngine-inl.mm | 124 ++++++++++---------- native/cocos/audio/apple/AudioPlayer.h | 23 ++-- native/cocos/audio/apple/AudioPlayer.mm | 62 +++++----- 7 files changed, 158 insertions(+), 161 deletions(-) diff --git a/native/cocos/audio/apple/AudioCache.h b/native/cocos/audio/apple/AudioCache.h index 422de1736a..4c313c096b 100644 --- a/native/cocos/audio/apple/AudioCache.h +++ b/native/cocos/audio/apple/AudioCache.h @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2014 Chukong Technologies Inc. + Copyright (c) 2014-2016 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -45,7 +45,7 @@ class AudioPlayer; class AudioCache { public: - + enum class State { INITIAL, @@ -53,14 +53,14 @@ public: READY, FAILED }; - + AudioCache(); ~AudioCache(); void addPlayCallback(const std::function& callback); void addLoadCallback(const std::function& callback); - + protected: void setSkipReadDataTask(bool isSkip) { _isSkipReadDataTask = isSkip; }; void readDataTask(unsigned int selfId); @@ -68,7 +68,7 @@ protected: void invokingPlayCallbacks(); void invokingLoadCallbacks(); - + //pcm data related stuff ALenum _format; ALsizei _sampleRate; @@ -91,26 +91,25 @@ protected: std::mutex _playCallbackMutex; std::vector< std::function > _playCallbacks; - + // loadCallbacks doesn't need mutex since it's invoked only in Cocos thread. std::vector< std::function > _loadCallbacks; - + std::mutex _readDataTaskMutex; - + State _state; - + std::shared_ptr _isDestroyed; std::string _fileFullPath; unsigned int _id; bool _isLoadingFinished; bool _isSkipReadDataTask; - + friend class AudioEngineImpl; friend class AudioPlayer; -} ; +}; } NS_CC_END #endif - diff --git a/native/cocos/audio/apple/AudioCache.mm b/native/cocos/audio/apple/AudioCache.mm index d4ca3a95b5..7953dab3d1 100644 --- a/native/cocos/audio/apple/AudioCache.mm +++ b/native/cocos/audio/apple/AudioCache.mm @@ -1,18 +1,18 @@ /**************************************************************************** - Copyright (c) 2014 Chukong Technologies Inc. - + Copyright (c) 2014-2016 Chukong Technologies Inc. + 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 @@ -55,15 +55,15 @@ typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr) (const ALint bid, ALenum static ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq) { static alBufferDataStaticProcPtr proc = nullptr; - + if (proc == nullptr) { proc = (alBufferDataStaticProcPtr) alcGetProcAddress(nullptr, (const ALCchar*) "alBufferDataStatic"); } - + if (proc){ proc(bid, format, data, size, freq); } - + return; } using namespace cocos2d; @@ -108,7 +108,7 @@ AudioCache::~AudioCache() //wait for the 'readDataTask' task to exit _readDataTaskMutex.lock(); _readDataTaskMutex.unlock(); - + if (_pcmData) { if (_state == State::READY) @@ -124,10 +124,10 @@ AudioCache::~AudioCache() { ALOGW("AudioCache (%p), id=%u, buffer isn't ready, state=%d", this, _id, _state); } - + free(_pcmData); } - + if (_queBufferFrames > 0) { for (int index = 0; index < QUEUEBUFFER_NUM; ++index) @@ -142,7 +142,7 @@ void AudioCache::readDataTask(unsigned int selfId) { //Note: It's in sub thread ALOGVV("readDataTask, cache id=%u", selfId); - + _readDataTaskMutex.lock(); _state = State::LOADING; @@ -166,7 +166,7 @@ void AudioCache::readDataTask(unsigned int selfId) _sampleRate = (ALsizei)sampleRate; _duration = 1.0f * totalFrames / sampleRate; _totalFrames = totalFrames; - + if (dataSize <= PCMDATA_CACHEMAXSIZE) { uint32_t framesRead = 0; @@ -218,10 +218,10 @@ void AudioCache::readDataTask(unsigned int selfId) ALOGE("%s: attaching audio to buffer fail: %x", __PRETTY_FUNCTION__, alError); break; } - + if (*_isDestroyed) break; - + alBufferDataStaticProc(_alBufferId, _format, _pcmData, (ALsizei)dataSize, (ALsizei)sampleRate); do @@ -232,10 +232,10 @@ void AudioCache::readDataTask(unsigned int selfId) _framesRead += framesRead; remainingFrames -= framesRead; } while (_framesRead < framesToReadOnce); - + if (*_isDestroyed) break; - + _state = State::READY; invokingPlayCallbacks(); @@ -274,18 +274,18 @@ void AudioCache::readDataTask(unsigned int selfId) decoder.read(_queBufferFrames, _queBuffers[index]); } - + _state = State::READY; } - + } while (false); - + decoder.close(); - + //FIXME: Why to invoke play callback first? Should it be after 'load' callback? invokingPlayCallbacks(); invokingLoadCallbacks(); - + _isLoadingFinished = true; if (_state != State::READY) { @@ -297,7 +297,7 @@ void AudioCache::readDataTask(unsigned int selfId) _alBufferId = INVALID_AL_BUFFER_ID; } } - + _readDataTaskMutex.unlock(); } @@ -310,14 +310,14 @@ void AudioCache::addPlayCallback(const std::function& callback) case State::LOADING: _playCallbacks.push_back(callback); break; - + case State::READY: // If state is failure, we still need to invoke the callback // since the callback will set the 'AudioPlayer::_removeByAudioEngine' flag to true. case State::FAILED: callback(); break; - + default: ALOGE("Invalid state: %d", _state); break; @@ -327,12 +327,12 @@ void AudioCache::addPlayCallback(const std::function& callback) void AudioCache::invokingPlayCallbacks() { std::lock_guard lk(_playCallbackMutex); - + for (auto&& cb : _playCallbacks) { cb(); } - + _playCallbacks.clear(); } @@ -344,14 +344,14 @@ void AudioCache::addLoadCallback(const std::function& callback) case State::LOADING: _loadCallbacks.push_back(callback); break; - + case State::READY: callback(true); break; case State::FAILED: callback(false); break; - + default: ALOGE("Invalid state: %d", _state); break; @@ -365,7 +365,7 @@ void AudioCache::invokingLoadCallbacks() ALOGV("AudioCache (%p) was destroyed, don't invoke preload callback ...", this); return; } - + auto isDestroyed = _isDestroyed; auto scheduler = Director::getInstance()->getScheduler(); scheduler->performFunctionInCocosThread([&, isDestroyed](){ @@ -374,7 +374,7 @@ void AudioCache::invokingLoadCallbacks() ALOGV("invokingLoadCallbacks perform in cocos thread, AudioCache (%p) was destroyed!", this); return; } - + for (auto&& cb : _loadCallbacks) { cb(_state == State::READY); diff --git a/native/cocos/audio/apple/AudioDecoder.h b/native/cocos/audio/apple/AudioDecoder.h index 6597425f28..90b4dcd224 100644 --- a/native/cocos/audio/apple/AudioDecoder.h +++ b/native/cocos/audio/apple/AudioDecoder.h @@ -40,7 +40,7 @@ public: AudioDecoder(); ~AudioDecoder(); - /** + /** * @brief Opens an audio file specified by a file path. * @return true if succeed, otherwise false. */ @@ -58,7 +58,7 @@ public: */ void close(); - /** + /** * @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. diff --git a/native/cocos/audio/apple/AudioEngine-inl.h b/native/cocos/audio/apple/AudioEngine-inl.h index 6a8c9257c1..5bde49a43f 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.h +++ b/native/cocos/audio/apple/AudioEngine-inl.h @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2014 Chukong Technologies Inc. + Copyright (c) 2014-2016 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -45,7 +45,7 @@ class AudioEngineImpl : public cocos2d::Ref public: AudioEngineImpl(); ~AudioEngineImpl(); - + bool init(); int play2d(const std::string &fileFullPath ,bool loop ,float volume); void setVolume(int audioID,float volume); @@ -58,29 +58,29 @@ public: float getCurrentTime(int audioID); bool setCurrentTime(int audioID, float time); void setFinishCallback(int audioID, const std::function &callback); - + void uncache(const std::string& filePath); void uncacheAll(); AudioCache* preload(const std::string& filePath, std::function callback); void update(float dt); - + private: void _play2d(AudioCache *cache, int audioID); - + ALuint _alSources[MAX_AUDIOINSTANCES]; - + //source,used std::unordered_map _alSourceUsed; - + //filePath,bufferInfo std::unordered_map _audioCaches; - + //audioID,AudioInfo std::unordered_map _audioPlayers; std::mutex _threadMutex; - + bool _lazyInitLoop; - + int _currentAudioID; Scheduler* _scheduler; }; @@ -88,4 +88,3 @@ private: NS_CC_END #endif // __AUDIO_ENGINE_INL_H_ #endif - diff --git a/native/cocos/audio/apple/AudioEngine-inl.mm b/native/cocos/audio/apple/AudioEngine-inl.mm index 52ad4d00ab..22899e2c36 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.mm +++ b/native/cocos/audio/apple/AudioEngine-inl.mm @@ -1,18 +1,18 @@ /**************************************************************************** - Copyright (c) 2014 Chukong Technologies Inc. - + Copyright (c) 2014-2016 Chukong Technologies Inc. + 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 @@ -96,13 +96,13 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio -(void)handleInterruption:(NSNotification*)notification { static bool resumeOnBecomingActive = false; - + if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) { NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] integerValue]; if (reason == AVAudioSessionInterruptionTypeBegan) { alcMakeContextCurrent(NULL); } - + if (reason == AVAudioSessionInterruptionTypeEnded) { if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { NSError *error = nil; @@ -113,7 +113,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio } } } - + if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification] && resumeOnBecomingActive) { resumeOnBecomingActive = false; NSError *error = nil; @@ -133,7 +133,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio { [[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; - + [super dealloc]; } @end @@ -145,16 +145,16 @@ AudioEngineImpl::AudioEngineImpl() : _lazyInitLoop(true) , _currentAudioID(0) { - + } AudioEngineImpl::~AudioEngineImpl() { if (s_ALContext) { alDeleteSources(MAX_AUDIOINSTANCES, _alSources); - + _audioCaches.clear(); - + alcMakeContextCurrent(nullptr); alcDestroyContext(s_ALContext); } @@ -174,13 +174,13 @@ bool AudioEngineImpl::init() #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS s_AudioEngineSessionHandler = [[AudioEngineSessionHandler alloc] init]; #endif - + s_ALDevice = alcOpenDevice(nullptr); - + if (s_ALDevice) { s_ALContext = alcCreateContext(s_ALDevice, nullptr); alcMakeContextCurrent(s_ALContext); - + alGenSources(MAX_AUDIOINSTANCES, _alSources); auto alError = alGetError(); if(alError != AL_NO_ERROR) @@ -188,11 +188,11 @@ bool AudioEngineImpl::init() ALOGE("%s:generating sources failed! error = %x", __PRETTY_FUNCTION__, alError); break; } - + for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) { _alSourceUsed[_alSources[i]] = false; } - + // 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', @@ -200,15 +200,15 @@ bool AudioEngineImpl::init() // Why? OpenAL is opensource by Apple and could be found at // http://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp . /* - + void InitializeBufferMap() { if (gOALBufferMap == NULL) // Position 1 { gOALBufferMap = new OALBufferMap (); // Position 2 - + // Position Gap - + gBufferMapLock = new CAGuard("OAL:BufferMapLock"); // Position 3 gDeadOALBufferMap = new OALBufferMap (); @@ -233,7 +233,7 @@ bool AudioEngineImpl::init() ... ... } - + */ // 'gBufferMapLock' will be initialized in the 'InitializeBufferMap' function, // that's the problem. It means that 'InitializeBufferMap' may be invoked in different threads. @@ -243,31 +243,31 @@ bool AudioEngineImpl::init() // 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? - + // Workaround is do an unused invocation in the mainthread right after OpenAL is initialized successfully // as bellow. // ================ Workaround begin ================ // - + ALuint unusedAlBufferId = 0; alGenBuffers(1, &unusedAlBufferId); alDeleteBuffers(1, &unusedAlBufferId); - + // ================ Workaround end ================ // - - + + _scheduler = Director::getInstance()->getScheduler(); ret = true; ALOGI("OpenAL was initialized successfully!"); } }while (false); - + return ret; } AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function callback) { AudioCache* audioCache = nullptr; - + auto it = _audioCaches.find(filePath); if (it == _audioCaches.end()) { audioCache = &_audioCaches[filePath]; @@ -287,7 +287,7 @@ AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function< else { audioCache = &it->second; } - + if (audioCache && callback) { audioCache->addLoadCallback(callback); @@ -300,12 +300,12 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume if (s_ALDevice == nullptr) { 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; @@ -314,36 +314,36 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume if(!sourceFlag){ return AudioEngine::INVALID_AUDIO_ID; } - + auto player = new (std::nothrow) AudioPlayer; if (player == nullptr) { return AudioEngine::INVALID_AUDIO_ID; } - + player->_alSource = alSource; player->_loop = loop; player->_volume = volume; - + auto audioCache = preload(filePath, nullptr); if (audioCache == nullptr) { delete player; return AudioEngine::INVALID_AUDIO_ID; } - + player->setCache(audioCache); _threadMutex.lock(); _audioPlayers[_currentAudioID] = player; _threadMutex.unlock(); - + _alSourceUsed[alSource] = true; - + audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID)); - + if (_lazyInitLoop) { _lazyInitLoop = false; _scheduler->schedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this, 0.05f, false); } - + return _currentAudioID++; } @@ -356,7 +356,7 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID) auto playerIt = _audioPlayers.find(audioID); if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) { _scheduler->performFunctionInCocosThread([audioID](){ - + if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) { AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING; } @@ -379,10 +379,10 @@ void AudioEngineImpl::setVolume(int audioID,float volume) { auto player = _audioPlayers[audioID]; player->_volume = volume; - + if (player->_ready) { alSourcef(_audioPlayers[audioID]->_alSource, AL_GAIN, volume); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); @@ -393,7 +393,7 @@ void AudioEngineImpl::setVolume(int audioID,float volume) void AudioEngineImpl::setLoop(int audioID, bool loop) { auto player = _audioPlayers[audioID]; - + if (player->_ready) { if (player->_streamingSource) { player->setLoop(loop); @@ -403,7 +403,7 @@ void AudioEngineImpl::setLoop(int audioID, bool loop) } else { alSourcei(player->_alSource, AL_LOOPING, AL_FALSE); } - + auto error = alGetError(); if (error != AL_NO_ERROR) { ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); @@ -419,13 +419,13 @@ bool AudioEngineImpl::pause(int audioID) { bool ret = true; alSourcePause(_audioPlayers[audioID]->_alSource); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ret = false; ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); } - + return ret; } @@ -433,13 +433,13 @@ bool AudioEngineImpl::resume(int audioID) { bool ret = true; alSourcePlay(_audioPlayers[audioID]->_alSource); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ret = false; ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); } - + return ret; } @@ -485,14 +485,14 @@ float AudioEngineImpl::getCurrentTime(int audioID) ret = player->getTime(); } else { alGetSourcef(player->_alSource, AL_SEC_OFFSET, &ret); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ALOGE("%s, audio id:%d,error code:%x", __PRETTY_FUNCTION__,audioID,error); } } } - + return ret; } @@ -500,12 +500,12 @@ bool AudioEngineImpl::setCurrentTime(int audioID, float time) { bool ret = false; auto player = _audioPlayers[audioID]; - + do { if (!player->_ready) { break; } - + if (player->_streamingSource) { ret = player->setTime(time); break; @@ -516,9 +516,9 @@ bool AudioEngineImpl::setCurrentTime(int audioID, float time) ALOGE("%s: audio id = %d", __PRETTY_FUNCTION__,audioID); break; } - + alSourcef(player->_alSource, AL_SEC_OFFSET, time); - + auto error = alGetError(); if (error != AL_NO_ERROR) { ALOGE("%s: audio id = %d, error = %x", __PRETTY_FUNCTION__,audioID,error); @@ -526,7 +526,7 @@ bool AudioEngineImpl::setCurrentTime(int audioID, float time) ret = true; } } while (0); - + return ret; } @@ -540,18 +540,18 @@ void AudioEngineImpl::update(float dt) ALint sourceState; int audioID; AudioPlayer* player; - + // ALOGV("AudioPlayer count: %d", (int)_audioPlayers.size()); - + for (auto it = _audioPlayers.begin(); it != _audioPlayers.end(); ) { audioID = it->first; player = it->second; alGetSourcei(player->_alSource, AL_SOURCE_STATE, &sourceState); - + if (player->_removeByAudioEngine) { _alSourceUsed[player->_alSource] = false; - + AudioEngine::remove(audioID); _threadMutex.lock(); it = _audioPlayers.erase(it); @@ -559,13 +559,13 @@ void AudioEngineImpl::update(float dt) delete player; } else if (player->_ready && sourceState == AL_STOPPED) { - + _alSourceUsed[player->_alSource] = false; if (player->_finishCallbak) { auto& audioInfo = AudioEngine::_audioIDInfoMap[audioID]; player->_finishCallbak(audioID, *audioInfo.filePath); //FIXME: callback will delay 50ms } - + AudioEngine::remove(audioID); delete player; _threadMutex.lock(); @@ -576,7 +576,7 @@ void AudioEngineImpl::update(float dt) ++it; } } - + if(_audioPlayers.empty()){ _lazyInitLoop = true; _scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this); diff --git a/native/cocos/audio/apple/AudioPlayer.h b/native/cocos/audio/apple/AudioPlayer.h index 2049079bcc..f79be14f7a 100644 --- a/native/cocos/audio/apple/AudioPlayer.h +++ b/native/cocos/audio/apple/AudioPlayer.h @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2014 Chukong Technologies Inc. + Copyright (c) 2014-2016 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -45,30 +45,30 @@ class AudioPlayer public: AudioPlayer(); ~AudioPlayer(); - + void destroy(); - + //queue buffer related stuff bool setTime(float time); float getTime() { return _currTime;} bool setLoop(bool loop); - + protected: void setCache(AudioCache* cache); void rotateBufferThread(int offsetFrame); bool play2d(); - + AudioCache* _audioCache; - + float _volume; bool _loop; std::function _finishCallbak; - + bool _isDestroyed; bool _removeByAudioEngine; bool _ready; ALuint _alSource; - + //play by circular buffer float _currTime; bool _streamingSource; @@ -78,11 +78,11 @@ protected: std::mutex _sleepMutex; bool _timeDirty; bool _isRotateThreadExited; - + std::mutex _play2dMutex; - + unsigned int _id; - + friend class AudioEngineImpl; }; @@ -90,4 +90,3 @@ protected: NS_CC_END #endif - diff --git a/native/cocos/audio/apple/AudioPlayer.mm b/native/cocos/audio/apple/AudioPlayer.mm index 4bdd571c39..6730dc026f 100644 --- a/native/cocos/audio/apple/AudioPlayer.mm +++ b/native/cocos/audio/apple/AudioPlayer.mm @@ -1,18 +1,18 @@ /**************************************************************************** - Copyright (c) 2014 Chukong Technologies Inc. - + Copyright (c) 2014-2016 Chukong Technologies Inc. + 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 @@ -68,7 +68,7 @@ AudioPlayer::~AudioPlayer() { ALOGVV("~AudioPlayer() (%p), id=%u", this, _id); destroy(); - + if (_streamingSource) { alDeleteBuffers(3, _bufferIds); @@ -79,11 +79,11 @@ void AudioPlayer::destroy() { if (_isDestroyed) return; - + ALOGVV("AudioPlayer::destroy begin, id=%u", _id); _isDestroyed = true; - + do { if (_audioCache != nullptr) @@ -93,17 +93,17 @@ void AudioPlayer::destroy() ALOGV("AudioPlayer::destroy, id=%u, cache isn't ready!", _id); break; } - + while (!_audioCache->_isLoadingFinished) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); } } - + // Wait for play2d to be finished. _play2dMutex.lock(); _play2dMutex.unlock(); - + if (_streamingSource) { if (_rotateBufferThread != nullptr) @@ -113,25 +113,25 @@ void AudioPlayer::destroy() _sleepCondition.notify_one(); std::this_thread::sleep_for(std::chrono::milliseconds(5)); } - + if (_rotateBufferThread->joinable()) { _rotateBufferThread->join(); } - + delete _rotateBufferThread; _rotateBufferThread = nullptr; ALOGVV("rotateBufferThread exited!"); } } } while(false); - + ALOGVV("Before alSourceStop"); alSourceStop(_alSource); CHECK_AL_ERROR_DEBUG(); ALOGVV("Before alSourcei"); alSourcei(_alSource, AL_BUFFER, NULL); CHECK_AL_ERROR_DEBUG(); - + _removeByAudioEngine = true; - + _ready = false; ALOGVV("AudioPlayer::destroy end, id=%u", _id); } @@ -157,12 +157,12 @@ bool AudioPlayer::play2d() 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) { @@ -173,7 +173,7 @@ bool AudioPlayer::play2d() else { alGenBuffers(3, _bufferIds); - + auto alError = alGetError(); if (alError == AL_NO_ERROR) { @@ -190,12 +190,12 @@ bool AudioPlayer::play2d() } _streamingSource = true; } - + { std::unique_lock lk(_sleepMutex); if (_isDestroyed) break; - + if (_streamingSource) { alSourceQueueBuffers(_alSource, QUEUEBUFFER_NUM, _bufferIds); @@ -210,7 +210,7 @@ bool AudioPlayer::play2d() alSourcePlay(_alSource); } - + auto alError = alGetError(); if (alError != AL_NO_ERROR) { @@ -224,12 +224,12 @@ bool AudioPlayer::play2d() _ready = true; ret = true; } while (false); - + if (!ret) { _removeByAudioEngine = true; } - + _play2dMutex.unlock(); return ret; } @@ -289,22 +289,22 @@ void AudioPlayer::rotateBufferThread(int offsetFrame) break; } } - + ALuint bid; alSourceUnqueueBuffers(_alSource, 1, &bid); alBufferData(bid, _audioCache->_format, tmpBuffer, framesRead * decoder.getBytesPerFrame(), decoder.getSampleRate()); alSourceQueueBuffers(_alSource, 1, &bid); } } - + std::unique_lock lk(_sleepMutex); if (_isDestroyed || needToExitThread) { break; } - + _sleepCondition.wait_for(lk,std::chrono::milliseconds(75)); } - + } while(false); ALOGV("Exit rotate buffer thread ..."); @@ -319,17 +319,17 @@ bool AudioPlayer::setLoop(bool loop) _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; From a180aa28d094a332f01a0d7859162d43c975ce0a Mon Sep 17 00:00:00 2001 From: CocosRobot Date: Tue, 6 Dec 2016 08:57:16 +0000 Subject: [PATCH 4/5] [AUTO][ci skip]: updating cocos2dx_files.json --- native/templates/cocos2dx_files.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/native/templates/cocos2dx_files.json b/native/templates/cocos2dx_files.json index 6b81c846a8..798413eaa2 100644 --- a/native/templates/cocos2dx_files.json +++ b/native/templates/cocos2dx_files.json @@ -203,8 +203,11 @@ "cocos/audio/android/utils/Utils.h", "cocos/audio/apple/AudioCache.h", "cocos/audio/apple/AudioCache.mm", + "cocos/audio/apple/AudioDecoder.h", + "cocos/audio/apple/AudioDecoder.mm", "cocos/audio/apple/AudioEngine-inl.h", "cocos/audio/apple/AudioEngine-inl.mm", + "cocos/audio/apple/AudioMacros.h", "cocos/audio/apple/AudioPlayer.h", "cocos/audio/apple/AudioPlayer.mm", "cocos/audio/include/AudioEngine.h", From 86817f5d510c2966c38c5a839b2526872d630b47 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Wed, 7 Dec 2016 13:48:36 +0800 Subject: [PATCH 5/5] add hasEventListener for CCEventDispatcher --- native/cocos/base/CCEventDispatcher.cpp | 5 +++++ native/cocos/base/CCEventDispatcher.h | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/native/cocos/base/CCEventDispatcher.cpp b/native/cocos/base/CCEventDispatcher.cpp index 2284413507..a7b19c6abe 100644 --- a/native/cocos/base/CCEventDispatcher.cpp +++ b/native/cocos/base/CCEventDispatcher.cpp @@ -857,6 +857,11 @@ void EventDispatcher::dispatchCustomEvent(const std::string &eventName, void *op dispatchEvent(&ev); } +bool EventDispatcher::hasEventListener(const EventListener::ListenerID& listenerID) const +{ + return getListeners(listenerID) != nullptr; +} + void EventDispatcher::dispatchTouchEvent(EventTouch* event) { sortEventListeners(EventListenerTouchOneByOne::LISTENER_ID); diff --git a/native/cocos/base/CCEventDispatcher.h b/native/cocos/base/CCEventDispatcher.h index 548aafd97a..4bc8f6c87f 100644 --- a/native/cocos/base/CCEventDispatcher.h +++ b/native/cocos/base/CCEventDispatcher.h @@ -177,6 +177,14 @@ public: */ void dispatchCustomEvent(const std::string &eventName, void *optionalUserData = nullptr); + /** Query whether the specified event listener id has been added. + * + * @param listenerID The listenerID of the event listener id. + * + * @return True if dispatching events is exist + */ + bool hasEventListener(const EventListener::ListenerID& listenerID) const; + ///////////////////////////////////////////// /** Constructor of EventDispatcher.