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