blob: e15e4aaf7708438cec4b23b28ddb69028bfb994f [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
henrika8324b522015-03-27 10:56:23 +010019#include "webrtc/modules/audio_device/android/audio_common.h"
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020020#include "webrtc/modules/audio_device/android/opensles_common.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/modules/audio_device/audio_device_config.h"
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020022#include "webrtc/modules/audio_device/audio_device_generic.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020023#include "webrtc/modules/audio_device/include/audio_device_defines.h"
solenberg71b9b582016-11-25 11:45:05 -080024#include "webrtc/modules/utility/include/helpers_android.h"
25#include "webrtc/modules/utility/include/jvm_android.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020026#include "webrtc/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
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,
henrika779017d2016-11-16 06:30:46 -0800123 jint output_channels,
124 jint input_channels,
henrikab2619892015-05-18 16:49:16 +0200125 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200126 jboolean hardware_agc,
127 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200128 jboolean low_latency_output,
henrika918b5542016-09-19 15:44:09 +0200129 jboolean low_latency_input,
henrika1f0ad102016-05-25 05:15:10 -0700130 jboolean pro_audio,
henrikab2619892015-05-18 16:49:16 +0200131 jint output_buffer_size,
132 jint input_buffer_size,
133 jlong native_audio_manager);
134 void OnCacheAudioParameters(JNIEnv* env,
135 jint sample_rate,
henrika779017d2016-11-16 06:30:46 -0800136 jint output_channels,
137 jint input_channels,
henrikab2619892015-05-18 16:49:16 +0200138 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200139 jboolean hardware_agc,
140 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200141 jboolean low_latency_output,
henrika918b5542016-09-19 15:44:09 +0200142 jboolean low_latency_input,
henrika1f0ad102016-05-25 05:15:10 -0700143 jboolean pro_audio,
henrikab2619892015-05-18 16:49:16 +0200144 jint output_buffer_size,
henrika1ba936a2015-11-03 04:27:58 -0800145 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100146
147 // Stores thread ID in the constructor.
148 // We can then use ThreadChecker::CalledOnValidThread() to ensure that
149 // other methods are called from the same thread.
150 rtc::ThreadChecker thread_checker_;
151
henrikab2619892015-05-18 16:49:16 +0200152 // Calls AttachCurrentThread() if this thread is not attached at construction.
153 // Also ensures that DetachCurrentThread() is called at destruction.
154 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
155
henrikaee369e42015-05-25 10:11:27 +0200156 // Wraps the JNI interface pointer and methods associated with it.
kwibergf01633e2016-02-24 05:00:36 -0800157 std::unique_ptr<JNIEnvironment> j_environment_;
henrikab2619892015-05-18 16:49:16 +0200158
henrikaee369e42015-05-25 10:11:27 +0200159 // Contains factory method for creating the Java object.
kwibergf01633e2016-02-24 05:00:36 -0800160 std::unique_ptr<NativeRegistration> j_native_registration_;
henrikab2619892015-05-18 16:49:16 +0200161
henrikaee369e42015-05-25 10:11:27 +0200162 // Wraps the Java specific parts of the AudioManager.
kwibergf01633e2016-02-24 05:00:36 -0800163 std::unique_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
henrikab2619892015-05-18 16:49:16 +0200164
henrika521f7a82016-05-31 07:03:17 -0700165 // Contains the selected audio layer specified by the AudioLayer enumerator
166 // in the AudioDeviceModule class.
henrikab2619892015-05-18 16:49:16 +0200167 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100168
henrika521f7a82016-05-31 07:03:17 -0700169 // This object is the global entry point of the OpenSL ES API.
170 // After creating the engine object, the application can obtain this object‘s
171 // SLEngineItf interface. This interface contains creation methods for all
172 // the other object types in the API. None of these interface are realized
173 // by this class. It only provides access to the global engine object.
174 webrtc::ScopedSLObjectItf engine_object_;
175
henrika8324b522015-03-27 10:56:23 +0100176 // Set to true by Init() and false by Close().
177 bool initialized_;
178
henrikab2619892015-05-18 16:49:16 +0200179 // True if device supports hardware (or built-in) AEC.
180 bool hardware_aec_;
henrikac14f5ff2015-09-23 14:08:33 +0200181 // True if device supports hardware (or built-in) AGC.
182 bool hardware_agc_;
183 // True if device supports hardware (or built-in) NS.
184 bool hardware_ns_;
henrikab2619892015-05-18 16:49:16 +0200185
henrika918b5542016-09-19 15:44:09 +0200186 // True if device supports the low-latency OpenSL ES audio path for output.
henrikab2619892015-05-18 16:49:16 +0200187 bool low_latency_playout_;
188
henrika918b5542016-09-19 15:44:09 +0200189 // True if device supports the low-latency OpenSL ES audio path for input.
190 bool low_latency_record_;
191
henrika1f0ad102016-05-25 05:15:10 -0700192 // True if device supports the low-latency OpenSL ES pro-audio path.
193 bool pro_audio_;
194
henrikab2619892015-05-18 16:49:16 +0200195 // The delay estimate can take one of two fixed values depending on if the
196 // device supports low-latency output or not.
197 int delay_estimate_in_milliseconds_;
198
henrika8324b522015-03-27 10:56:23 +0100199 // Contains native parameters (e.g. sample rate, channel configuration).
200 // Set at construction in OnCacheAudioParameters() which is called from
201 // Java on the same thread as this object is created on.
202 AudioParameters playout_parameters_;
203 AudioParameters record_parameters_;
204};
205
206} // namespace webrtc
207
208#endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_