audio_manager.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2015 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_AUDIO_MANAGER_H_
  11. #define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_
  12. #include <SLES/OpenSLES.h>
  13. #include <jni.h>
  14. #include <memory>
  15. #include "modules/audio_device/android/audio_common.h"
  16. #include "modules/audio_device/android/opensles_common.h"
  17. #include "modules/audio_device/audio_device_config.h"
  18. #include "modules/audio_device/audio_device_generic.h"
  19. #include "modules/audio_device/include/audio_device_defines.h"
  20. #include "modules/utility/include/helpers_android.h"
  21. #include "modules/utility/include/jvm_android.h"
  22. #include "rtc_base/thread_checker.h"
  23. namespace webrtc {
  24. // Implements support for functions in the WebRTC audio stack for Android that
  25. // relies on the AudioManager in android.media. It also populates an
  26. // AudioParameter structure with native audio parameters detected at
  27. // construction. This class does not make any audio-related modifications
  28. // unless Init() is called. Caching audio parameters makes no changes but only
  29. // reads data from the Java side.
  30. class AudioManager {
  31. public:
  32. // Wraps the Java specific parts of the AudioManager into one helper class.
  33. // Stores method IDs for all supported methods at construction and then
  34. // allows calls like JavaAudioManager::Close() while hiding the Java/JNI
  35. // parts that are associated with this call.
  36. class JavaAudioManager {
  37. public:
  38. JavaAudioManager(NativeRegistration* native_registration,
  39. std::unique_ptr<GlobalRef> audio_manager);
  40. ~JavaAudioManager();
  41. bool Init();
  42. void Close();
  43. bool IsCommunicationModeEnabled();
  44. bool IsDeviceBlacklistedForOpenSLESUsage();
  45. private:
  46. std::unique_ptr<GlobalRef> audio_manager_;
  47. jmethodID init_;
  48. jmethodID dispose_;
  49. jmethodID is_communication_mode_enabled_;
  50. jmethodID is_device_blacklisted_for_open_sles_usage_;
  51. };
  52. AudioManager();
  53. ~AudioManager();
  54. // Sets the currently active audio layer combination. Must be called before
  55. // Init().
  56. void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer);
  57. // Creates and realizes the main (global) Open SL engine object and returns
  58. // a reference to it. The engine object is only created at the first call
  59. // since OpenSL ES for Android only supports a single engine per application.
  60. // Subsequent calls returns the already created engine. The SL engine object
  61. // is destroyed when the AudioManager object is deleted. It means that the
  62. // engine object will be the first OpenSL ES object to be created and last
  63. // object to be destroyed.
  64. // Note that NULL will be returned unless the audio layer is specified as
  65. // AudioDeviceModule::kAndroidOpenSLESAudio or
  66. // AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio.
  67. SLObjectItf GetOpenSLEngine();
  68. // Initializes the audio manager and stores the current audio mode.
  69. bool Init();
  70. // Revert any setting done by Init().
  71. bool Close();
  72. // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION.
  73. bool IsCommunicationModeEnabled() const;
  74. // Native audio parameters stored during construction.
  75. const AudioParameters& GetPlayoutAudioParameters();
  76. const AudioParameters& GetRecordAudioParameters();
  77. // Returns true if the device supports built-in audio effects for AEC, AGC
  78. // and NS. Some devices can also be blacklisted for use in combination with
  79. // platform effects and these devices will return false.
  80. // Can currently only be used in combination with a Java based audio backend
  81. // for the recoring side (i.e. using the android.media.AudioRecord API).
  82. bool IsAcousticEchoCancelerSupported() const;
  83. bool IsAutomaticGainControlSupported() const;
  84. bool IsNoiseSuppressorSupported() const;
  85. // Returns true if the device supports the low-latency audio paths in
  86. // combination with OpenSL ES.
  87. bool IsLowLatencyPlayoutSupported() const;
  88. bool IsLowLatencyRecordSupported() const;
  89. // Returns true if the device supports (and has been configured for) stereo.
  90. // Call the Java API WebRtcAudioManager.setStereoOutput/Input() with true as
  91. // paramter to enable stereo. Default is mono in both directions and the
  92. // setting is set once and for all when the audio manager object is created.
  93. // TODO(henrika): stereo is not supported in combination with OpenSL ES.
  94. bool IsStereoPlayoutSupported() const;
  95. bool IsStereoRecordSupported() const;
  96. // Returns true if the device supports pro-audio features in combination with
  97. // OpenSL ES.
  98. bool IsProAudioSupported() const;
  99. // Returns true if the device supports AAudio.
  100. bool IsAAudioSupported() const;
  101. // Returns the estimated total delay of this device. Unit is in milliseconds.
  102. // The vaule is set once at construction and never changes after that.
  103. // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and
  104. // webrtc::kHighLatencyModeDelayEstimateInMilliseconds.
  105. int GetDelayEstimateInMilliseconds() const;
  106. private:
  107. // Called from Java side so we can cache the native audio parameters.
  108. // This method will be called by the WebRtcAudioManager constructor, i.e.
  109. // on the same thread that this object is created on.
  110. static void JNICALL CacheAudioParameters(JNIEnv* env,
  111. jobject obj,
  112. jint sample_rate,
  113. jint output_channels,
  114. jint input_channels,
  115. jboolean hardware_aec,
  116. jboolean hardware_agc,
  117. jboolean hardware_ns,
  118. jboolean low_latency_output,
  119. jboolean low_latency_input,
  120. jboolean pro_audio,
  121. jboolean a_audio,
  122. jint output_buffer_size,
  123. jint input_buffer_size,
  124. jlong native_audio_manager);
  125. void OnCacheAudioParameters(JNIEnv* env,
  126. jint sample_rate,
  127. jint output_channels,
  128. jint input_channels,
  129. jboolean hardware_aec,
  130. jboolean hardware_agc,
  131. jboolean hardware_ns,
  132. jboolean low_latency_output,
  133. jboolean low_latency_input,
  134. jboolean pro_audio,
  135. jboolean a_audio,
  136. jint output_buffer_size,
  137. jint input_buffer_size);
  138. // Stores thread ID in the constructor.
  139. // We can then use ThreadChecker::IsCurrent() to ensure that
  140. // other methods are called from the same thread.
  141. rtc::ThreadChecker thread_checker_;
  142. // Calls JavaVM::AttachCurrentThread() if this thread is not attached at
  143. // construction.
  144. // Also ensures that DetachCurrentThread() is called at destruction.
  145. JvmThreadConnector attach_thread_if_needed_;
  146. // Wraps the JNI interface pointer and methods associated with it.
  147. std::unique_ptr<JNIEnvironment> j_environment_;
  148. // Contains factory method for creating the Java object.
  149. std::unique_ptr<NativeRegistration> j_native_registration_;
  150. // Wraps the Java specific parts of the AudioManager.
  151. std::unique_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
  152. // Contains the selected audio layer specified by the AudioLayer enumerator
  153. // in the AudioDeviceModule class.
  154. AudioDeviceModule::AudioLayer audio_layer_;
  155. // This object is the global entry point of the OpenSL ES API.
  156. // After creating the engine object, the application can obtain this object‘s
  157. // SLEngineItf interface. This interface contains creation methods for all
  158. // the other object types in the API. None of these interface are realized
  159. // by this class. It only provides access to the global engine object.
  160. webrtc::ScopedSLObjectItf engine_object_;
  161. // Set to true by Init() and false by Close().
  162. bool initialized_;
  163. // True if device supports hardware (or built-in) AEC.
  164. bool hardware_aec_;
  165. // True if device supports hardware (or built-in) AGC.
  166. bool hardware_agc_;
  167. // True if device supports hardware (or built-in) NS.
  168. bool hardware_ns_;
  169. // True if device supports the low-latency OpenSL ES audio path for output.
  170. bool low_latency_playout_;
  171. // True if device supports the low-latency OpenSL ES audio path for input.
  172. bool low_latency_record_;
  173. // True if device supports the low-latency OpenSL ES pro-audio path.
  174. bool pro_audio_;
  175. // True if device supports the low-latency AAudio audio path.
  176. bool a_audio_;
  177. // The delay estimate can take one of two fixed values depending on if the
  178. // device supports low-latency output or not.
  179. int delay_estimate_in_milliseconds_;
  180. // Contains native parameters (e.g. sample rate, channel configuration).
  181. // Set at construction in OnCacheAudioParameters() which is called from
  182. // Java on the same thread as this object is created on.
  183. AudioParameters playout_parameters_;
  184. AudioParameters record_parameters_;
  185. };
  186. } // namespace webrtc
  187. #endif // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_