blob: d2d0eb623b431020110fbbd45594d4369e6a4130 [file] [log] [blame]
henrika8324b522015-03-27 10:56:23 +01001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_
12#define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_
henrika8324b522015-03-27 10:56:23 +010013
henrika521f7a82016-05-31 07:03:17 -070014#include <SLES/OpenSLES.h>
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <jni.h>
henrika8324b522015-03-27 10:56:23 +010016
henrika883d00f2018-03-16 10:09:49 +010017#include <memory>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_device/android/audio_common.h"
20#include "modules/audio_device/android/opensles_common.h"
21#include "modules/audio_device/audio_device_config.h"
22#include "modules/audio_device/audio_device_generic.h"
23#include "modules/audio_device/include/audio_device_defines.h"
24#include "modules/utility/include/helpers_android.h"
25#include "modules/utility/include/jvm_android.h"
26#include "rtc_base/thread_checker.h"
henrika8324b522015-03-27 10:56:23 +010027
28namespace webrtc {
29
henrika8324b522015-03-27 10:56:23 +010030// 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.
henrika8324b522015-03-27 10:56:23 +010036class AudioManager {
37 public:
henrikab2619892015-05-18 16:49:16 +020038 // 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,
kwibergf01633e2016-02-24 05:00:36 -080045 std::unique_ptr<GlobalRef> audio_manager);
henrika796e1722015-05-28 14:18:33 +020046 ~JavaAudioManager();
henrikab2619892015-05-18 16:49:16 +020047
henrika796e1722015-05-28 14:18:33 +020048 bool Init();
49 void Close();
henrikafe55c382015-06-05 11:45:56 +020050 bool IsCommunicationModeEnabled();
henrika8a897182015-06-09 10:45:09 +020051 bool IsDeviceBlacklistedForOpenSLESUsage();
henrikab2619892015-05-18 16:49:16 +020052
henrika796e1722015-05-28 14:18:33 +020053 private:
kwibergf01633e2016-02-24 05:00:36 -080054 std::unique_ptr<GlobalRef> audio_manager_;
henrika796e1722015-05-28 14:18:33 +020055 jmethodID init_;
56 jmethodID dispose_;
henrikafe55c382015-06-05 11:45:56 +020057 jmethodID is_communication_mode_enabled_;
henrika8a897182015-06-09 10:45:09 +020058 jmethodID is_device_blacklisted_for_open_sles_usage_;
henrikab2619892015-05-18 16:49:16 +020059 };
henrika8324b522015-03-27 10:56:23 +010060
61 AudioManager();
62 ~AudioManager();
63
henrikab2619892015-05-18 16:49:16 +020064 // Sets the currently active audio layer combination. Must be called before
65 // Init().
66 void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer);
67
henrika521f7a82016-05-31 07:03:17 -070068 // 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
henrika09bf1a12015-04-10 11:46:55 +020080 // Initializes the audio manager and stores the current audio mode.
henrika8324b522015-03-27 10:56:23 +010081 bool Init();
82 // Revert any setting done by Init().
83 bool Close();
84
henrikafe55c382015-06-05 11:45:56 +020085 // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION.
86 bool IsCommunicationModeEnabled() const;
henrika09bf1a12015-04-10 11:46:55 +020087
henrika8324b522015-03-27 10:56:23 +010088 // Native audio parameters stored during construction.
henrikab2619892015-05-18 16:49:16 +020089 const AudioParameters& GetPlayoutAudioParameters();
90 const AudioParameters& GetRecordAudioParameters();
henrika8324b522015-03-27 10:56:23 +010091
henrikac14f5ff2015-09-23 14:08:33 +020092 // 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.
henrikab2619892015-05-18 16:49:16 +020095 // 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;
henrikac14f5ff2015-09-23 14:08:33 +020098 bool IsAutomaticGainControlSupported() const;
99 bool IsNoiseSuppressorSupported() const;
henrikab2619892015-05-18 16:49:16 +0200100
101 // Returns true if the device supports the low-latency audio paths in
102 // combination with OpenSL ES.
103 bool IsLowLatencyPlayoutSupported() const;
henrika918b5542016-09-19 15:44:09 +0200104 bool IsLowLatencyRecordSupported() const;
henrikab2619892015-05-18 16:49:16 +0200105
henrika76535de2017-09-11 01:25:55 -0700106 // 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
henrika1f0ad102016-05-25 05:15:10 -0700114 // Returns true if the device supports pro-audio features in combination with
115 // OpenSL ES.
116 bool IsProAudioSupported() const;
117
henrika883d00f2018-03-16 10:09:49 +0100118 // Returns true if the device supports AAudio.
119 bool IsAAudioSupported() const;
120
henrikab2619892015-05-18 16:49:16 +0200121 // 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;
henrika8324b522015-03-27 10:56:23 +0100126
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.
henrikab2619892015-05-18 16:49:16 +0200131 static void JNICALL CacheAudioParameters(JNIEnv* env,
132 jobject obj,
133 jint sample_rate,
henrika779017d2016-11-16 06:30:46 -0800134 jint output_channels,
135 jint input_channels,
henrikab2619892015-05-18 16:49:16 +0200136 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200137 jboolean hardware_agc,
138 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200139 jboolean low_latency_output,
henrika918b5542016-09-19 15:44:09 +0200140 jboolean low_latency_input,
henrika1f0ad102016-05-25 05:15:10 -0700141 jboolean pro_audio,
henrika883d00f2018-03-16 10:09:49 +0100142 jboolean a_audio,
henrikab2619892015-05-18 16:49:16 +0200143 jint output_buffer_size,
144 jint input_buffer_size,
145 jlong native_audio_manager);
146 void OnCacheAudioParameters(JNIEnv* env,
147 jint sample_rate,
henrika779017d2016-11-16 06:30:46 -0800148 jint output_channels,
149 jint input_channels,
henrikab2619892015-05-18 16:49:16 +0200150 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200151 jboolean hardware_agc,
152 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200153 jboolean low_latency_output,
henrika918b5542016-09-19 15:44:09 +0200154 jboolean low_latency_input,
henrika1f0ad102016-05-25 05:15:10 -0700155 jboolean pro_audio,
henrika883d00f2018-03-16 10:09:49 +0100156 jboolean a_audio,
henrikab2619892015-05-18 16:49:16 +0200157 jint output_buffer_size,
henrika1ba936a2015-11-03 04:27:58 -0800158 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100159
160 // Stores thread ID in the constructor.
161 // We can then use ThreadChecker::CalledOnValidThread() to ensure that
162 // other methods are called from the same thread.
163 rtc::ThreadChecker thread_checker_;
164
henrikab2619892015-05-18 16:49:16 +0200165 // Calls AttachCurrentThread() if this thread is not attached at construction.
166 // Also ensures that DetachCurrentThread() is called at destruction.
167 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
168
henrikaee369e42015-05-25 10:11:27 +0200169 // Wraps the JNI interface pointer and methods associated with it.
kwibergf01633e2016-02-24 05:00:36 -0800170 std::unique_ptr<JNIEnvironment> j_environment_;
henrikab2619892015-05-18 16:49:16 +0200171
henrikaee369e42015-05-25 10:11:27 +0200172 // Contains factory method for creating the Java object.
kwibergf01633e2016-02-24 05:00:36 -0800173 std::unique_ptr<NativeRegistration> j_native_registration_;
henrikab2619892015-05-18 16:49:16 +0200174
henrikaee369e42015-05-25 10:11:27 +0200175 // Wraps the Java specific parts of the AudioManager.
kwibergf01633e2016-02-24 05:00:36 -0800176 std::unique_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
henrikab2619892015-05-18 16:49:16 +0200177
henrika521f7a82016-05-31 07:03:17 -0700178 // Contains the selected audio layer specified by the AudioLayer enumerator
179 // in the AudioDeviceModule class.
henrikab2619892015-05-18 16:49:16 +0200180 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100181
henrika521f7a82016-05-31 07:03:17 -0700182 // This object is the global entry point of the OpenSL ES API.
183 // After creating the engine object, the application can obtain this object‘s
184 // SLEngineItf interface. This interface contains creation methods for all
185 // the other object types in the API. None of these interface are realized
186 // by this class. It only provides access to the global engine object.
187 webrtc::ScopedSLObjectItf engine_object_;
188
henrika8324b522015-03-27 10:56:23 +0100189 // Set to true by Init() and false by Close().
190 bool initialized_;
191
henrikab2619892015-05-18 16:49:16 +0200192 // True if device supports hardware (or built-in) AEC.
193 bool hardware_aec_;
henrikac14f5ff2015-09-23 14:08:33 +0200194 // True if device supports hardware (or built-in) AGC.
195 bool hardware_agc_;
196 // True if device supports hardware (or built-in) NS.
197 bool hardware_ns_;
henrikab2619892015-05-18 16:49:16 +0200198
henrika918b5542016-09-19 15:44:09 +0200199 // True if device supports the low-latency OpenSL ES audio path for output.
henrikab2619892015-05-18 16:49:16 +0200200 bool low_latency_playout_;
201
henrika918b5542016-09-19 15:44:09 +0200202 // True if device supports the low-latency OpenSL ES audio path for input.
203 bool low_latency_record_;
204
henrika1f0ad102016-05-25 05:15:10 -0700205 // True if device supports the low-latency OpenSL ES pro-audio path.
206 bool pro_audio_;
207
henrika883d00f2018-03-16 10:09:49 +0100208 // True if device supports the low-latency AAudio audio path.
209 bool a_audio_;
210
henrikab2619892015-05-18 16:49:16 +0200211 // The delay estimate can take one of two fixed values depending on if the
212 // device supports low-latency output or not.
213 int delay_estimate_in_milliseconds_;
214
henrika8324b522015-03-27 10:56:23 +0100215 // Contains native parameters (e.g. sample rate, channel configuration).
216 // Set at construction in OnCacheAudioParameters() which is called from
217 // Java on the same thread as this object is created on.
218 AudioParameters playout_parameters_;
219 AudioParameters record_parameters_;
220};
221
222} // namespace webrtc
223
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200224#endif // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_