opensles_common.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_DEVICE_ANDROID_OPENSLES_COMMON_H_
  11. #define MODULES_AUDIO_DEVICE_ANDROID_OPENSLES_COMMON_H_
  12. #include <SLES/OpenSLES.h>
  13. #include <stddef.h>
  14. #include "rtc_base/checks.h"
  15. namespace webrtc {
  16. // Returns a string representation given an integer SL_RESULT_XXX code.
  17. // The mapping can be found in <SLES/OpenSLES.h>.
  18. const char* GetSLErrorString(size_t code);
  19. // Configures an SL_DATAFORMAT_PCM structure based on native audio parameters.
  20. SLDataFormat_PCM CreatePCMConfiguration(size_t channels,
  21. int sample_rate,
  22. size_t bits_per_sample);
  23. // Helper class for using SLObjectItf interfaces.
  24. template <typename SLType, typename SLDerefType>
  25. class ScopedSLObject {
  26. public:
  27. ScopedSLObject() : obj_(nullptr) {}
  28. ~ScopedSLObject() { Reset(); }
  29. SLType* Receive() {
  30. RTC_DCHECK(!obj_);
  31. return &obj_;
  32. }
  33. SLDerefType operator->() { return *obj_; }
  34. SLType Get() const { return obj_; }
  35. void Reset() {
  36. if (obj_) {
  37. (*obj_)->Destroy(obj_);
  38. obj_ = nullptr;
  39. }
  40. }
  41. private:
  42. SLType obj_;
  43. };
  44. typedef ScopedSLObject<SLObjectItf, const SLObjectItf_*> ScopedSLObjectItf;
  45. } // namespace webrtc
  46. #endif // MODULES_AUDIO_DEVICE_ANDROID_OPENSLES_COMMON_H_