OpenSLHelper.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. #include "audio/android/cutils/log.h"
  23. #include <SLES/OpenSLES.h>
  24. #include <SLES/OpenSLES_Android.h>
  25. #include <functional>
  26. #include <string>
  27. #define SL_SAFE_DELETE(obj) \
  28. if ((obj) != nullptr) { delete (obj); (obj) = nullptr; }
  29. #define SL_DESTROY_OBJ(OBJ) \
  30. if ((OBJ) != nullptr) { \
  31. (*(OBJ))->Destroy(OBJ); \
  32. (OBJ) = nullptr; \
  33. }
  34. #define SL_RETURN_VAL_IF_FAILED(r, rval, ...) \
  35. if (r != SL_RESULT_SUCCESS) {\
  36. ALOGE(__VA_ARGS__); \
  37. return rval; \
  38. }
  39. #define SL_RETURN_IF_FAILED(r, ...) \
  40. if (r != SL_RESULT_SUCCESS) {\
  41. ALOGE(__VA_ARGS__); \
  42. return; \
  43. }
  44. #define SL_PRINT_ERROR_IF_FAILED(r, ...) \
  45. if (r != SL_RESULT_SUCCESS) {\
  46. ALOGE(__VA_ARGS__); \
  47. }
  48. typedef std::function<int(const std::string&, off_t* start, off_t* length)> FdGetterCallback;
  49. // Copied from OpenSLES_AndroidMetadata.h in android-21
  50. // It's because android-10 doesn't contain this header file
  51. /**
  52. * Additional metadata keys to be used in SLMetadataExtractionItf:
  53. * the ANDROID_KEY_PCMFORMAT_* keys follow the fields of the SLDataFormat_PCM struct, and as such
  54. * all values corresponding to these keys are of SLuint32 type, and are defined as the fields
  55. * of the same name in SLDataFormat_PCM. The exception is that sample rate is expressed here
  56. * in Hz units, rather than in milliHz units.
  57. */
  58. #ifndef ANDROID_KEY_PCMFORMAT_NUMCHANNELS
  59. #define ANDROID_KEY_PCMFORMAT_NUMCHANNELS "AndroidPcmFormatNumChannels"
  60. #endif
  61. #ifndef ANDROID_KEY_PCMFORMAT_SAMPLERATE
  62. #define ANDROID_KEY_PCMFORMAT_SAMPLERATE "AndroidPcmFormatSampleRate"
  63. #endif
  64. #ifndef ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE
  65. #define ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE "AndroidPcmFormatBitsPerSample"
  66. #endif
  67. #ifndef ANDROID_KEY_PCMFORMAT_CONTAINERSIZE
  68. #define ANDROID_KEY_PCMFORMAT_CONTAINERSIZE "AndroidPcmFormatContainerSize"
  69. #endif
  70. #ifndef ANDROID_KEY_PCMFORMAT_CHANNELMASK
  71. #define ANDROID_KEY_PCMFORMAT_CHANNELMASK "AndroidPcmFormatChannelMask"
  72. #endif
  73. #ifndef ANDROID_KEY_PCMFORMAT_ENDIANNESS
  74. #define ANDROID_KEY_PCMFORMAT_ENDIANNESS "AndroidPcmFormatEndianness"
  75. #endif
  76. #define clockNow() std::chrono::high_resolution_clock::now()
  77. #define intervalInMS(oldTime, newTime) (static_cast<long>(std::chrono::duration_cast<std::chrono::microseconds>((newTime) - (oldTime)).count()) / 1000.f)
  78. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))