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