blob: 341d426e418bc99d7ed4c7297f1759f3d036c14a [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
11#ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_
13
kwibergf01633e2016-02-24 05:00:36 -080014#include <memory>
15
henrika8324b522015-03-27 10:56:23 +010016#include <jni.h>
henrika521f7a82016-05-31 07:03:17 -070017#include <SLES/OpenSLES.h>
henrika8324b522015-03-27 10:56:23 +010018
19#include "webrtc/base/thread_checker.h"
20#include "webrtc/modules/audio_device/android/audio_common.h"
henrikaba35d052015-07-14 17:04:08 +020021#include "webrtc/modules/audio_device/audio_device_config.h"
henrika8324b522015-03-27 10:56:23 +010022#include "webrtc/modules/audio_device/include/audio_device_defines.h"
henrika521f7a82016-05-31 07:03:17 -070023#include "webrtc/modules/audio_device/android/opensles_common.h"
henrika8324b522015-03-27 10:56:23 +010024#include "webrtc/modules/audio_device/audio_device_generic.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/utility/include/helpers_android.h"
26#include "webrtc/modules/utility/include/jvm_android.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
henrika1f0ad102016-05-25 05:15:10 -0700106 // Returns true if the device supports pro-audio features in combination with
107 // OpenSL ES.
108 bool IsProAudioSupported() const;
109
henrikab2619892015-05-18 16:49:16 +0200110 // 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;
henrika8324b522015-03-27 10:56:23 +0100115
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.
henrikab2619892015-05-18 16:49:16 +0200120 static void JNICALL CacheAudioParameters(JNIEnv* env,
121 jobject obj,
122 jint sample_rate,
123 jint channels,
124 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200125 jboolean hardware_agc,
126 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200127 jboolean low_latency_output,
henrika918b5542016-09-19 15:44:09 +0200128 jboolean low_latency_input,
henrika1f0ad102016-05-25 05:15:10 -0700129 jboolean pro_audio,
henrikab2619892015-05-18 16:49:16 +0200130 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,
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,
henrikab2619892015-05-18 16:49:16 +0200142 jint output_buffer_size,
henrika1ba936a2015-11-03 04:27:58 -0800143 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100144
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
henrikab2619892015-05-18 16:49:16 +0200150 // 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
henrikaee369e42015-05-25 10:11:27 +0200154 // Wraps the JNI interface pointer and methods associated with it.
kwibergf01633e2016-02-24 05:00:36 -0800155 std::unique_ptr<JNIEnvironment> j_environment_;
henrikab2619892015-05-18 16:49:16 +0200156
henrikaee369e42015-05-25 10:11:27 +0200157 // Contains factory method for creating the Java object.
kwibergf01633e2016-02-24 05:00:36 -0800158 std::unique_ptr<NativeRegistration> j_native_registration_;
henrikab2619892015-05-18 16:49:16 +0200159
henrikaee369e42015-05-25 10:11:27 +0200160 // Wraps the Java specific parts of the AudioManager.
kwibergf01633e2016-02-24 05:00:36 -0800161 std::unique_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
henrikab2619892015-05-18 16:49:16 +0200162
henrika521f7a82016-05-31 07:03:17 -0700163 // Contains the selected audio layer specified by the AudioLayer enumerator
164 // in the AudioDeviceModule class.
henrikab2619892015-05-18 16:49:16 +0200165 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100166
henrika521f7a82016-05-31 07:03:17 -0700167 // 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
henrika8324b522015-03-27 10:56:23 +0100174 // Set to true by Init() and false by Close().
175 bool initialized_;
176
henrikab2619892015-05-18 16:49:16 +0200177 // True if device supports hardware (or built-in) AEC.
178 bool hardware_aec_;
henrikac14f5ff2015-09-23 14:08:33 +0200179 // 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_;
henrikab2619892015-05-18 16:49:16 +0200183
henrika918b5542016-09-19 15:44:09 +0200184 // True if device supports the low-latency OpenSL ES audio path for output.
henrikab2619892015-05-18 16:49:16 +0200185 bool low_latency_playout_;
186
henrika918b5542016-09-19 15:44:09 +0200187 // True if device supports the low-latency OpenSL ES audio path for input.
188 bool low_latency_record_;
189
henrika1f0ad102016-05-25 05:15:10 -0700190 // True if device supports the low-latency OpenSL ES pro-audio path.
191 bool pro_audio_;
192
henrikab2619892015-05-18 16:49:16 +0200193 // 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
henrika8324b522015-03-27 10:56:23 +0100197 // 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_