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 16 #include "webrtc/base/scoped_ptr.h" 17 #include "webrtc/base/thread_checker.h" 18 #include "webrtc/modules/audio_device/android/audio_common.h" 19 #include "webrtc/modules/audio_device/audio_device_config.h" 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/include/helpers_android.h" 23 #include "webrtc/modules/utility/include/jvm_android.h" 24 25 namespace webrtc { 26 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. 33 class AudioManager { 34 public: 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); 43 ~JavaAudioManager(); 44 45 bool Init(); 46 void Close(); 47 bool IsCommunicationModeEnabled(); 48 bool IsDeviceBlacklistedForOpenSLESUsage(); 49 50 private: 51 rtc::scoped_ptr<GlobalRef> audio_manager_; 52 jmethodID init_; 53 jmethodID dispose_; 54 jmethodID is_communication_mode_enabled_; 55 jmethodID is_device_blacklisted_for_open_sles_usage_; 56 }; 57 58 AudioManager(); 59 ~AudioManager(); 60 61 // Sets the currently active audio layer combination. Must be called before 62 // Init(). 63 void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer); 64 65 // Initializes the audio manager and stores the current audio mode. 66 bool Init(); 67 // Revert any setting done by Init(). 68 bool Close(); 69 70 // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION. 71 bool IsCommunicationModeEnabled() const; 72 73 // Native audio parameters stored during construction. 74 const AudioParameters& GetPlayoutAudioParameters(); 75 const AudioParameters& GetRecordAudioParameters(); 76 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 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; 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. 100 static void JNICALL CacheAudioParameters(JNIEnv* env, 101 jobject obj, 102 jint sample_rate, 103 jint channels, 104 jboolean hardware_aec, 105 jboolean hardware_agc, 106 jboolean hardware_ns, 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, 115 jboolean hardware_agc, 116 jboolean hardware_ns, 117 jboolean low_latency_output, 118 jint output_buffer_size, 119 jint input_buffer_size); 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 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 130 // Wraps the JNI interface pointer and methods associated with it. 131 rtc::scoped_ptr<JNIEnvironment> j_environment_; 132 133 // Contains factory method for creating the Java object. 134 rtc::scoped_ptr<NativeRegistration> j_native_registration_; 135 136 // Wraps the Java specific parts of the AudioManager. 137 rtc::scoped_ptr<AudioManager::JavaAudioManager> j_audio_manager_; 138 139 AudioDeviceModule::AudioLayer audio_layer_; 140 141 // Set to true by Init() and false by Close(). 142 bool initialized_; 143 144 // True if device supports hardware (or built-in) AEC. 145 bool hardware_aec_; 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_; 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 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_ 168