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 | |
| 11 | #ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_ |
| 13 | |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 16 | #include <jni.h> |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 17 | #include <SLES/OpenSLES.h> |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 18 | |
| 19 | #include "webrtc/base/thread_checker.h" |
| 20 | #include "webrtc/modules/audio_device/android/audio_common.h" |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 21 | #include "webrtc/modules/audio_device/audio_device_config.h" |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 22 | #include "webrtc/modules/audio_device/include/audio_device_defines.h" |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 23 | #include "webrtc/modules/audio_device/android/opensles_common.h" |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 24 | #include "webrtc/modules/audio_device/audio_device_generic.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 25 | #include "webrtc/modules/utility/include/helpers_android.h" |
| 26 | #include "webrtc/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 | 1f0ad10 | 2016-05-25 05:15:10 -0700 | [diff] [blame] | 106 | // Returns true if the device supports pro-audio features in combination with |
| 107 | // OpenSL ES. |
| 108 | bool IsProAudioSupported() const; |
| 109 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 110 | // Returns the estimated total delay of this device. Unit is in milliseconds. |
| 111 | // The vaule is set once at construction and never changes after that. |
| 112 | // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and |
| 113 | // webrtc::kHighLatencyModeDelayEstimateInMilliseconds. |
| 114 | int GetDelayEstimateInMilliseconds() const; |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | // Called from Java side so we can cache the native audio parameters. |
| 118 | // This method will be called by the WebRtcAudioManager constructor, i.e. |
| 119 | // on the same thread that this object is created on. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 120 | static void JNICALL CacheAudioParameters(JNIEnv* env, |
| 121 | jobject obj, |
| 122 | jint sample_rate, |
| 123 | jint channels, |
| 124 | jboolean hardware_aec, |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 125 | jboolean hardware_agc, |
| 126 | jboolean hardware_ns, |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 127 | jboolean low_latency_output, |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 128 | jboolean low_latency_input, |
henrika | 1f0ad10 | 2016-05-25 05:15:10 -0700 | [diff] [blame] | 129 | jboolean pro_audio, |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 130 | jint output_buffer_size, |
| 131 | jint input_buffer_size, |
| 132 | jlong native_audio_manager); |
| 133 | void OnCacheAudioParameters(JNIEnv* env, |
| 134 | jint sample_rate, |
| 135 | jint channels, |
| 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 | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 142 | jint output_buffer_size, |
henrika | 1ba936a | 2015-11-03 04:27:58 -0800 | [diff] [blame] | 143 | jint input_buffer_size); |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 144 | |
| 145 | // Stores thread ID in the constructor. |
| 146 | // We can then use ThreadChecker::CalledOnValidThread() to ensure that |
| 147 | // other methods are called from the same thread. |
| 148 | rtc::ThreadChecker thread_checker_; |
| 149 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 150 | // Calls AttachCurrentThread() if this thread is not attached at construction. |
| 151 | // Also ensures that DetachCurrentThread() is called at destruction. |
| 152 | AttachCurrentThreadIfNeeded attach_thread_if_needed_; |
| 153 | |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 154 | // Wraps the JNI interface pointer and methods associated with it. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 155 | std::unique_ptr<JNIEnvironment> j_environment_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 156 | |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 157 | // Contains factory method for creating the Java object. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 158 | std::unique_ptr<NativeRegistration> j_native_registration_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 159 | |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 160 | // Wraps the Java specific parts of the AudioManager. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 161 | std::unique_ptr<AudioManager::JavaAudioManager> j_audio_manager_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 162 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 163 | // Contains the selected audio layer specified by the AudioLayer enumerator |
| 164 | // in the AudioDeviceModule class. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 165 | AudioDeviceModule::AudioLayer audio_layer_; |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 166 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 167 | // This object is the global entry point of the OpenSL ES API. |
| 168 | // After creating the engine object, the application can obtain this object‘s |
| 169 | // SLEngineItf interface. This interface contains creation methods for all |
| 170 | // the other object types in the API. None of these interface are realized |
| 171 | // by this class. It only provides access to the global engine object. |
| 172 | webrtc::ScopedSLObjectItf engine_object_; |
| 173 | |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 174 | // Set to true by Init() and false by Close(). |
| 175 | bool initialized_; |
| 176 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 177 | // True if device supports hardware (or built-in) AEC. |
| 178 | bool hardware_aec_; |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 179 | // True if device supports hardware (or built-in) AGC. |
| 180 | bool hardware_agc_; |
| 181 | // True if device supports hardware (or built-in) NS. |
| 182 | bool hardware_ns_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 183 | |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 184 | // True if device supports the low-latency OpenSL ES audio path for output. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 185 | bool low_latency_playout_; |
| 186 | |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 187 | // True if device supports the low-latency OpenSL ES audio path for input. |
| 188 | bool low_latency_record_; |
| 189 | |
henrika | 1f0ad10 | 2016-05-25 05:15:10 -0700 | [diff] [blame] | 190 | // True if device supports the low-latency OpenSL ES pro-audio path. |
| 191 | bool pro_audio_; |
| 192 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 193 | // The delay estimate can take one of two fixed values depending on if the |
| 194 | // device supports low-latency output or not. |
| 195 | int delay_estimate_in_milliseconds_; |
| 196 | |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 197 | // Contains native parameters (e.g. sample rate, channel configuration). |
| 198 | // Set at construction in OnCacheAudioParameters() which is called from |
| 199 | // Java on the same thread as this object is created on. |
| 200 | AudioParameters playout_parameters_; |
| 201 | AudioParameters record_parameters_; |
| 202 | }; |
| 203 | |
| 204 | } // namespace webrtc |
| 205 | |
| 206 | #endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_ |