blob: 808417ca9d8439fac5adc0f0e8c2e1e0a76ffc7b [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;
104
henrika1f0ad102016-05-25 05:15:10 -0700105 // Returns true if the device supports pro-audio features in combination with
106 // OpenSL ES.
107 bool IsProAudioSupported() const;
108
henrikab2619892015-05-18 16:49:16 +0200109 // Returns the estimated total delay of this device. Unit is in milliseconds.
110 // The vaule is set once at construction and never changes after that.
111 // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and
112 // webrtc::kHighLatencyModeDelayEstimateInMilliseconds.
113 int GetDelayEstimateInMilliseconds() const;
henrika8324b522015-03-27 10:56:23 +0100114
115 private:
116 // Called from Java side so we can cache the native audio parameters.
117 // This method will be called by the WebRtcAudioManager constructor, i.e.
118 // on the same thread that this object is created on.
henrikab2619892015-05-18 16:49:16 +0200119 static void JNICALL CacheAudioParameters(JNIEnv* env,
120 jobject obj,
121 jint sample_rate,
122 jint channels,
123 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200124 jboolean hardware_agc,
125 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200126 jboolean low_latency_output,
henrika1f0ad102016-05-25 05:15:10 -0700127 jboolean pro_audio,
henrikab2619892015-05-18 16:49:16 +0200128 jint output_buffer_size,
129 jint input_buffer_size,
130 jlong native_audio_manager);
131 void OnCacheAudioParameters(JNIEnv* env,
132 jint sample_rate,
133 jint channels,
134 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200135 jboolean hardware_agc,
136 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200137 jboolean low_latency_output,
henrika1f0ad102016-05-25 05:15:10 -0700138 jboolean pro_audio,
henrikab2619892015-05-18 16:49:16 +0200139 jint output_buffer_size,
henrika1ba936a2015-11-03 04:27:58 -0800140 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100141
142 // Stores thread ID in the constructor.
143 // We can then use ThreadChecker::CalledOnValidThread() to ensure that
144 // other methods are called from the same thread.
145 rtc::ThreadChecker thread_checker_;
146
henrikab2619892015-05-18 16:49:16 +0200147 // Calls AttachCurrentThread() if this thread is not attached at construction.
148 // Also ensures that DetachCurrentThread() is called at destruction.
149 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
150
henrikaee369e42015-05-25 10:11:27 +0200151 // Wraps the JNI interface pointer and methods associated with it.
kwibergf01633e2016-02-24 05:00:36 -0800152 std::unique_ptr<JNIEnvironment> j_environment_;
henrikab2619892015-05-18 16:49:16 +0200153
henrikaee369e42015-05-25 10:11:27 +0200154 // Contains factory method for creating the Java object.
kwibergf01633e2016-02-24 05:00:36 -0800155 std::unique_ptr<NativeRegistration> j_native_registration_;
henrikab2619892015-05-18 16:49:16 +0200156
henrikaee369e42015-05-25 10:11:27 +0200157 // Wraps the Java specific parts of the AudioManager.
kwibergf01633e2016-02-24 05:00:36 -0800158 std::unique_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
henrikab2619892015-05-18 16:49:16 +0200159
henrika521f7a82016-05-31 07:03:17 -0700160 // Contains the selected audio layer specified by the AudioLayer enumerator
161 // in the AudioDeviceModule class.
henrikab2619892015-05-18 16:49:16 +0200162 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100163
henrika521f7a82016-05-31 07:03:17 -0700164 // This object is the global entry point of the OpenSL ES API.
165 // After creating the engine object, the application can obtain this object‘s
166 // SLEngineItf interface. This interface contains creation methods for all
167 // the other object types in the API. None of these interface are realized
168 // by this class. It only provides access to the global engine object.
169 webrtc::ScopedSLObjectItf engine_object_;
170
henrika8324b522015-03-27 10:56:23 +0100171 // Set to true by Init() and false by Close().
172 bool initialized_;
173
henrikab2619892015-05-18 16:49:16 +0200174 // True if device supports hardware (or built-in) AEC.
175 bool hardware_aec_;
henrikac14f5ff2015-09-23 14:08:33 +0200176 // True if device supports hardware (or built-in) AGC.
177 bool hardware_agc_;
178 // True if device supports hardware (or built-in) NS.
179 bool hardware_ns_;
henrikab2619892015-05-18 16:49:16 +0200180
181 // True if device supports the low-latency OpenSL ES audio path.
182 bool low_latency_playout_;
183
henrika1f0ad102016-05-25 05:15:10 -0700184 // True if device supports the low-latency OpenSL ES pro-audio path.
185 bool pro_audio_;
186
henrikab2619892015-05-18 16:49:16 +0200187 // The delay estimate can take one of two fixed values depending on if the
188 // device supports low-latency output or not.
189 int delay_estimate_in_milliseconds_;
190
henrika8324b522015-03-27 10:56:23 +0100191 // Contains native parameters (e.g. sample rate, channel configuration).
192 // Set at construction in OnCacheAudioParameters() which is called from
193 // Java on the same thread as this object is created on.
194 AudioParameters playout_parameters_;
195 AudioParameters record_parameters_;
196};
197
198} // namespace webrtc
199
200#endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_