PcmAudioService.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /****************************************************************************
  2. Copyright (c) 2016-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #define LOG_TAG "PcmAudioService"
  21. #include "audio/android/PcmAudioService.h"
  22. #include "audio/android/AudioMixerController.h"
  23. namespace cocos2d { namespace experimental {
  24. static std::vector<char> __silenceData;
  25. #define AUDIO_PLAYER_BUFFER_COUNT (2)
  26. class SLPcmAudioPlayerCallbackProxy
  27. {
  28. public:
  29. static void samplePlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
  30. {
  31. PcmAudioService *thiz = reinterpret_cast<PcmAudioService *>(context);
  32. thiz->bqFetchBufferCallback(bq);
  33. }
  34. };
  35. PcmAudioService::PcmAudioService(SLEngineItf engineItf, SLObjectItf outputMixObject)
  36. : _engineItf(engineItf), _outputMixObj(outputMixObject), _playObj(nullptr),
  37. _playItf(nullptr), _volumeItf(nullptr), _bufferQueueItf(nullptr), _numChannels(-1),
  38. _sampleRate(-1), _bufferSizeInBytes(0), _controller(nullptr)
  39. {
  40. }
  41. PcmAudioService::~PcmAudioService()
  42. {
  43. ALOGV("PcmAudioServicee() (%p), before destroy play object", this);
  44. SL_DESTROY_OBJ(_playObj);
  45. ALOGV("PcmAudioServicee() end");
  46. }
  47. bool PcmAudioService::enqueue()
  48. {
  49. if (_controller->hasPlayingTacks())
  50. {
  51. if (_controller->isPaused())
  52. {
  53. SLresult r = (*_bufferQueueItf)->Enqueue(_bufferQueueItf, __silenceData.data(), __silenceData.size());
  54. SL_RETURN_VAL_IF_FAILED(r, false, "enqueue silent data failed!");
  55. }
  56. else
  57. {
  58. _controller->mixOneFrame();
  59. auto current = _controller->current();
  60. ALOG_ASSERT(current != nullptr, "current buffer is nullptr ...");
  61. SLresult r = (*_bufferQueueItf)->Enqueue(_bufferQueueItf, current->buf, current->size);
  62. SL_RETURN_VAL_IF_FAILED(r, false, "enqueue failed!");
  63. }
  64. }
  65. else
  66. {
  67. SLresult r = (*_bufferQueueItf)->Enqueue(_bufferQueueItf, __silenceData.data(), __silenceData.size());
  68. SL_RETURN_VAL_IF_FAILED(r, false, "enqueue silent data failed!");
  69. }
  70. return true;
  71. }
  72. void PcmAudioService::bqFetchBufferCallback(SLAndroidSimpleBufferQueueItf bq)
  73. {
  74. // FIXME: PcmAudioService instance may be destroyed, we need to find a way to wait...
  75. // It's in sub thread
  76. enqueue();
  77. }
  78. bool PcmAudioService::init(AudioMixerController* controller, int numChannels, int sampleRate, int bufferSizeInBytes)
  79. {
  80. _controller = controller;
  81. _numChannels = numChannels;
  82. _sampleRate = sampleRate;
  83. _bufferSizeInBytes = bufferSizeInBytes;
  84. SLuint32 channelMask = SL_SPEAKER_FRONT_CENTER;
  85. if (numChannels > 1)
  86. {
  87. channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  88. }
  89. SLDataFormat_PCM formatPcm = {
  90. SL_DATAFORMAT_PCM,
  91. (SLuint32) numChannels,
  92. (SLuint32) sampleRate * 1000,
  93. SL_PCMSAMPLEFORMAT_FIXED_16,
  94. SL_PCMSAMPLEFORMAT_FIXED_16,
  95. channelMask,
  96. SL_BYTEORDER_LITTLEENDIAN
  97. };
  98. SLDataLocator_AndroidSimpleBufferQueue locBufQueue = {
  99. SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
  100. AUDIO_PLAYER_BUFFER_COUNT
  101. };
  102. SLDataSource source = {&locBufQueue, &formatPcm};
  103. SLDataLocator_OutputMix locOutmix = {
  104. SL_DATALOCATOR_OUTPUTMIX,
  105. _outputMixObj
  106. };
  107. SLDataSink sink = {&locOutmix, nullptr};
  108. const SLInterfaceID ids[] = {
  109. SL_IID_PLAY,
  110. SL_IID_VOLUME,
  111. SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
  112. };
  113. const SLboolean req[] = {
  114. SL_BOOLEAN_TRUE,
  115. SL_BOOLEAN_TRUE,
  116. SL_BOOLEAN_TRUE,
  117. };
  118. SLresult r;
  119. r = (*_engineItf)->CreateAudioPlayer(_engineItf, &_playObj, &source, &sink,
  120. sizeof(ids) / sizeof(ids[0]), ids, req);
  121. SL_RETURN_VAL_IF_FAILED(r, false, "CreateAudioPlayer failed");
  122. r = (*_playObj)->Realize(_playObj, SL_BOOLEAN_FALSE);
  123. SL_RETURN_VAL_IF_FAILED(r, false, "Realize failed");
  124. r = (*_playObj)->GetInterface(_playObj, SL_IID_PLAY, &_playItf);
  125. SL_RETURN_VAL_IF_FAILED(r, false, "GetInterface SL_IID_PLAY failed");
  126. r = (*_playObj)->GetInterface(_playObj, SL_IID_VOLUME, &_volumeItf);
  127. SL_RETURN_VAL_IF_FAILED(r, false, "GetInterface SL_IID_VOLUME failed");
  128. r = (*_playObj)->GetInterface(_playObj, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &_bufferQueueItf);
  129. SL_RETURN_VAL_IF_FAILED(r, false, "GetInterface SL_IID_ANDROIDSIMPLEBUFFERQUEUE failed");
  130. r = (*_bufferQueueItf)->RegisterCallback(_bufferQueueItf,
  131. SLPcmAudioPlayerCallbackProxy::samplePlayerCallback,
  132. this);
  133. SL_RETURN_VAL_IF_FAILED(r, false, "_bufferQueueItf RegisterCallback failed");
  134. if (__silenceData.empty())
  135. {
  136. __silenceData.resize(_numChannels * _bufferSizeInBytes, 0x00);
  137. }
  138. r = (*_bufferQueueItf)->Enqueue(_bufferQueueItf, __silenceData.data(), __silenceData.size());
  139. SL_RETURN_VAL_IF_FAILED(r, false, "_bufferQueueItf Enqueue failed");
  140. r = (*_playItf)->SetPlayState(_playItf, SL_PLAYSTATE_PLAYING);
  141. SL_RETURN_VAL_IF_FAILED(r, false, "SetPlayState failed");
  142. return true;
  143. }
  144. void PcmAudioService::pause()
  145. {
  146. SLresult r = (*_playItf)->SetPlayState(_playItf, SL_PLAYSTATE_PAUSED);
  147. SL_RETURN_IF_FAILED(r, "PcmAudioService::pause failed");
  148. }
  149. void PcmAudioService::resume()
  150. {
  151. SLresult r = (*_playItf)->SetPlayState(_playItf, SL_PLAYSTATE_PLAYING);
  152. SL_RETURN_IF_FAILED(r, "PcmAudioService::resume failed");
  153. }
  154. }} // namespace cocos2d { namespace experimental {