1
0

AudioDecoderSLES.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /****************************************************************************
  2. Copyright (c) 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. #pragma once
  21. #include "audio/android/AudioDecoder.h"
  22. #include <mutex>
  23. #include <condition_variable>
  24. namespace cocos2d { namespace experimental {
  25. class AudioDecoderSLES : public AudioDecoder
  26. {
  27. protected:
  28. AudioDecoderSLES();
  29. virtual ~AudioDecoderSLES();
  30. bool init(SLEngineItf engineItf, const std::string &url, int bufferSizeInFrames, int sampleRate, const FdGetterCallback &fdGetterCallback);
  31. virtual bool decodeToPcm() override;
  32. private:
  33. void queryAudioInfo();
  34. void signalEos();
  35. void decodeToPcmCallback(SLAndroidSimpleBufferQueueItf queueItf);
  36. void prefetchCallback(SLPrefetchStatusItf caller, SLuint32 event);
  37. void decodeProgressCallback(SLPlayItf caller, SLuint32 event);
  38. SLEngineItf _engineItf;
  39. SLObjectItf _playObj;
  40. /* Local storage for decoded audio data */
  41. char* _pcmData;
  42. /* we only want to query / display the PCM format once */
  43. bool _formatQueried;
  44. /* Used to signal prefetching failures */
  45. bool _prefetchError;
  46. /* to display the number of decode iterations */
  47. int _counter;
  48. /* metadata key index for the PCM format information we want to retrieve */
  49. int _numChannelsKeyIndex;
  50. int _sampleRateKeyIndex;
  51. int _bitsPerSampleKeyIndex;
  52. int _containerSizeKeyIndex;
  53. int _channelMaskKeyIndex;
  54. int _endiannessKeyIndex;
  55. /* to signal to the test app the end of the stream to decode has been reached */
  56. bool _eos;
  57. std::mutex _eosLock;
  58. std::condition_variable _eosCondition;
  59. /* Structure for passing information to callback function */
  60. typedef struct CallbackCntxt_
  61. {
  62. SLPlayItf playItf;
  63. SLMetadataExtractionItf metaItf;
  64. SLuint32 size;
  65. SLint8 *pDataBase; // Base address of local audio data storage
  66. SLint8 *pData; // Current address of local audio data storage
  67. } CallbackCntxt;
  68. CallbackCntxt _decContext;
  69. int _bufferSizeInFrames;
  70. int _assetFd;
  71. FdGetterCallback _fdGetterCallback;
  72. bool _isDecodingCallbackInvoked;
  73. friend class SLAudioDecoderCallbackProxy;
  74. friend class AudioDecoderProvider;
  75. };
  76. }} // namespace cocos2d { namespace experimental {