OpenSLHelper.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #pragma once
  21. #include "audio/android/cutils/log.h"
  22. #include <SLES/OpenSLES.h>
  23. #include <SLES/OpenSLES_Android.h>
  24. #include <functional>
  25. #include <string>
  26. #define SL_SAFE_DELETE(obj) \
  27. if ((obj) != nullptr) { delete (obj); (obj) = nullptr; }
  28. #define SL_DESTROY_OBJ(OBJ) \
  29. if ((OBJ) != nullptr) { \
  30. (*(OBJ))->Destroy(OBJ); \
  31. (OBJ) = nullptr; \
  32. }
  33. #define SL_RETURN_VAL_IF_FAILED(r, rval, ...) \
  34. if (r != SL_RESULT_SUCCESS) {\
  35. ALOGE(__VA_ARGS__); \
  36. return rval; \
  37. }
  38. #define SL_RETURN_IF_FAILED(r, ...) \
  39. if (r != SL_RESULT_SUCCESS) {\
  40. ALOGE(__VA_ARGS__); \
  41. return; \
  42. }
  43. #define SL_PRINT_ERROR_IF_FAILED(r, ...) \
  44. if (r != SL_RESULT_SUCCESS) {\
  45. ALOGE(__VA_ARGS__); \
  46. }
  47. typedef std::function<int(const std::string&, off_t* start, off_t* length)> FdGetterCallback;
  48. // Copied from OpenSLES_AndroidMetadata.h in android-21
  49. // It's because android-10 doesn't contain this header file
  50. /**
  51. * Additional metadata keys to be used in SLMetadataExtractionItf:
  52. * the ANDROID_KEY_PCMFORMAT_* keys follow the fields of the SLDataFormat_PCM struct, and as such
  53. * all values corresponding to these keys are of SLuint32 type, and are defined as the fields
  54. * of the same name in SLDataFormat_PCM. The exception is that sample rate is expressed here
  55. * in Hz units, rather than in milliHz units.
  56. */
  57. #ifndef ANDROID_KEY_PCMFORMAT_NUMCHANNELS
  58. #define ANDROID_KEY_PCMFORMAT_NUMCHANNELS "AndroidPcmFormatNumChannels"
  59. #endif
  60. #ifndef ANDROID_KEY_PCMFORMAT_SAMPLERATE
  61. #define ANDROID_KEY_PCMFORMAT_SAMPLERATE "AndroidPcmFormatSampleRate"
  62. #endif
  63. #ifndef ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE
  64. #define ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE "AndroidPcmFormatBitsPerSample"
  65. #endif
  66. #ifndef ANDROID_KEY_PCMFORMAT_CONTAINERSIZE
  67. #define ANDROID_KEY_PCMFORMAT_CONTAINERSIZE "AndroidPcmFormatContainerSize"
  68. #endif
  69. #ifndef ANDROID_KEY_PCMFORMAT_CHANNELMASK
  70. #define ANDROID_KEY_PCMFORMAT_CHANNELMASK "AndroidPcmFormatChannelMask"
  71. #endif
  72. #ifndef ANDROID_KEY_PCMFORMAT_ENDIANNESS
  73. #define ANDROID_KEY_PCMFORMAT_ENDIANNESS "AndroidPcmFormatEndianness"
  74. #endif
  75. #define clockNow() std::chrono::high_resolution_clock::now()
  76. #define intervalInMS(oldTime, newTime) (static_cast<long>(std::chrono::duration_cast<std::chrono::microseconds>((newTime) - (oldTime)).count()) / 1000.f)
  77. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))