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> |
| 17 | |
| 18 | #include "webrtc/base/thread_checker.h" |
| 19 | #include "webrtc/modules/audio_device/android/audio_common.h" |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 20 | #include "webrtc/modules/audio_device/audio_device_config.h" |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 21 | #include "webrtc/modules/audio_device/include/audio_device_defines.h" |
| 22 | #include "webrtc/modules/audio_device/audio_device_generic.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 23 | #include "webrtc/modules/utility/include/helpers_android.h" |
| 24 | #include "webrtc/modules/utility/include/jvm_android.h" |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 28 | // Implements support for functions in the WebRTC audio stack for Android that |
| 29 | // relies on the AudioManager in android.media. It also populates an |
| 30 | // AudioParameter structure with native audio parameters detected at |
| 31 | // construction. This class does not make any audio-related modifications |
| 32 | // unless Init() is called. Caching audio parameters makes no changes but only |
| 33 | // reads data from the Java side. |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 34 | class AudioManager { |
| 35 | public: |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 36 | // Wraps the Java specific parts of the AudioManager into one helper class. |
| 37 | // Stores method IDs for all supported methods at construction and then |
| 38 | // allows calls like JavaAudioManager::Close() while hiding the Java/JNI |
| 39 | // parts that are associated with this call. |
| 40 | class JavaAudioManager { |
| 41 | public: |
| 42 | JavaAudioManager(NativeRegistration* native_registration, |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 43 | std::unique_ptr<GlobalRef> audio_manager); |
henrika | 796e172 | 2015-05-28 14:18:33 +0200 | [diff] [blame] | 44 | ~JavaAudioManager(); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 45 | |
henrika | 796e172 | 2015-05-28 14:18:33 +0200 | [diff] [blame] | 46 | bool Init(); |
| 47 | void Close(); |
henrika | fe55c38 | 2015-06-05 11:45:56 +0200 | [diff] [blame] | 48 | bool IsCommunicationModeEnabled(); |
henrika | 8a89718 | 2015-06-09 10:45:09 +0200 | [diff] [blame] | 49 | bool IsDeviceBlacklistedForOpenSLESUsage(); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 50 | |
henrika | 796e172 | 2015-05-28 14:18:33 +0200 | [diff] [blame] | 51 | private: |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 52 | std::unique_ptr<GlobalRef> audio_manager_; |
henrika | 796e172 | 2015-05-28 14:18:33 +0200 | [diff] [blame] | 53 | jmethodID init_; |
| 54 | jmethodID dispose_; |
henrika | fe55c38 | 2015-06-05 11:45:56 +0200 | [diff] [blame] | 55 | jmethodID is_communication_mode_enabled_; |
henrika | 8a89718 | 2015-06-09 10:45:09 +0200 | [diff] [blame] | 56 | jmethodID is_device_blacklisted_for_open_sles_usage_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 57 | }; |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 58 | |
| 59 | AudioManager(); |
| 60 | ~AudioManager(); |
| 61 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 62 | // Sets the currently active audio layer combination. Must be called before |
| 63 | // Init(). |
| 64 | void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer); |
| 65 | |
henrika | 09bf1a1 | 2015-04-10 11:46:55 +0200 | [diff] [blame] | 66 | // Initializes the audio manager and stores the current audio mode. |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 67 | bool Init(); |
| 68 | // Revert any setting done by Init(). |
| 69 | bool Close(); |
| 70 | |
henrika | fe55c38 | 2015-06-05 11:45:56 +0200 | [diff] [blame] | 71 | // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION. |
| 72 | bool IsCommunicationModeEnabled() const; |
henrika | 09bf1a1 | 2015-04-10 11:46:55 +0200 | [diff] [blame] | 73 | |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 74 | // Native audio parameters stored during construction. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 75 | const AudioParameters& GetPlayoutAudioParameters(); |
| 76 | const AudioParameters& GetRecordAudioParameters(); |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 77 | |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 78 | // Returns true if the device supports built-in audio effects for AEC, AGC |
| 79 | // and NS. Some devices can also be blacklisted for use in combination with |
| 80 | // platform effects and these devices will return false. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 81 | // Can currently only be used in combination with a Java based audio backend |
| 82 | // for the recoring side (i.e. using the android.media.AudioRecord API). |
| 83 | bool IsAcousticEchoCancelerSupported() const; |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 84 | bool IsAutomaticGainControlSupported() const; |
| 85 | bool IsNoiseSuppressorSupported() const; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 86 | |
| 87 | // Returns true if the device supports the low-latency audio paths in |
| 88 | // combination with OpenSL ES. |
| 89 | bool IsLowLatencyPlayoutSupported() const; |
| 90 | |
| 91 | // Returns the estimated total delay of this device. Unit is in milliseconds. |
| 92 | // The vaule is set once at construction and never changes after that. |
| 93 | // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and |
| 94 | // webrtc::kHighLatencyModeDelayEstimateInMilliseconds. |
| 95 | int GetDelayEstimateInMilliseconds() const; |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | // Called from Java side so we can cache the native audio parameters. |
| 99 | // This method will be called by the WebRtcAudioManager constructor, i.e. |
| 100 | // on the same thread that this object is created on. |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 101 | static void JNICALL CacheAudioParameters(JNIEnv* env, |
| 102 | jobject obj, |
| 103 | jint sample_rate, |
| 104 | jint channels, |
| 105 | jboolean hardware_aec, |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 106 | jboolean hardware_agc, |
| 107 | jboolean hardware_ns, |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 108 | jboolean low_latency_output, |
| 109 | jint output_buffer_size, |
| 110 | jint input_buffer_size, |
| 111 | jlong native_audio_manager); |
| 112 | void OnCacheAudioParameters(JNIEnv* env, |
| 113 | jint sample_rate, |
| 114 | jint channels, |
| 115 | jboolean hardware_aec, |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 116 | jboolean hardware_agc, |
| 117 | jboolean hardware_ns, |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 118 | jboolean low_latency_output, |
| 119 | jint output_buffer_size, |
henrika | 1ba936a | 2015-11-03 04:27:58 -0800 | [diff] [blame] | 120 | jint input_buffer_size); |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 121 | |
| 122 | // Stores thread ID in the constructor. |
| 123 | // We can then use ThreadChecker::CalledOnValidThread() to ensure that |
| 124 | // other methods are called from the same thread. |
| 125 | rtc::ThreadChecker thread_checker_; |
| 126 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 127 | // Calls AttachCurrentThread() if this thread is not attached at construction. |
| 128 | // Also ensures that DetachCurrentThread() is called at destruction. |
| 129 | AttachCurrentThreadIfNeeded attach_thread_if_needed_; |
| 130 | |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 131 | // Wraps the JNI interface pointer and methods associated with it. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 132 | std::unique_ptr<JNIEnvironment> j_environment_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 133 | |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 134 | // Contains factory method for creating the Java object. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 135 | std::unique_ptr<NativeRegistration> j_native_registration_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 136 | |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 137 | // Wraps the Java specific parts of the AudioManager. |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 138 | std::unique_ptr<AudioManager::JavaAudioManager> j_audio_manager_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 139 | |
| 140 | AudioDeviceModule::AudioLayer audio_layer_; |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 141 | |
| 142 | // Set to true by Init() and false by Close(). |
| 143 | bool initialized_; |
| 144 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 145 | // True if device supports hardware (or built-in) AEC. |
| 146 | bool hardware_aec_; |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 147 | // True if device supports hardware (or built-in) AGC. |
| 148 | bool hardware_agc_; |
| 149 | // True if device supports hardware (or built-in) NS. |
| 150 | bool hardware_ns_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 151 | |
| 152 | // True if device supports the low-latency OpenSL ES audio path. |
| 153 | bool low_latency_playout_; |
| 154 | |
| 155 | // The delay estimate can take one of two fixed values depending on if the |
| 156 | // device supports low-latency output or not. |
| 157 | int delay_estimate_in_milliseconds_; |
| 158 | |
henrika | 8324b52 | 2015-03-27 10:56:23 +0100 | [diff] [blame] | 159 | // Contains native parameters (e.g. sample rate, channel configuration). |
| 160 | // Set at construction in OnCacheAudioParameters() which is called from |
| 161 | // Java on the same thread as this object is created on. |
| 162 | AudioParameters playout_parameters_; |
| 163 | AudioParameters record_parameters_; |
| 164 | }; |
| 165 | |
| 166 | } // namespace webrtc |
| 167 | |
| 168 | #endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_ |