blob: b4264a6f8cd9ec6a815a9483c9cefc1f6ef5463d [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>
17
18#include "webrtc/base/thread_checker.h"
19#include "webrtc/modules/audio_device/android/audio_common.h"
henrikaba35d052015-07-14 17:04:08 +020020#include "webrtc/modules/audio_device/audio_device_config.h"
henrika8324b522015-03-27 10:56:23 +010021#include "webrtc/modules/audio_device/include/audio_device_defines.h"
22#include "webrtc/modules/audio_device/audio_device_generic.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010023#include "webrtc/modules/utility/include/helpers_android.h"
24#include "webrtc/modules/utility/include/jvm_android.h"
henrika8324b522015-03-27 10:56:23 +010025
26namespace webrtc {
27
henrika8324b522015-03-27 10:56:23 +010028// Implements support for functions in the WebRTC audio stack for Android that
29// relies on the AudioManager in android.media. It also populates an
30// AudioParameter structure with native audio parameters detected at
31// construction. This class does not make any audio-related modifications
32// unless Init() is called. Caching audio parameters makes no changes but only
33// reads data from the Java side.
henrika8324b522015-03-27 10:56:23 +010034class AudioManager {
35 public:
henrikab2619892015-05-18 16:49:16 +020036 // Wraps the Java specific parts of the AudioManager into one helper class.
37 // Stores method IDs for all supported methods at construction and then
38 // allows calls like JavaAudioManager::Close() while hiding the Java/JNI
39 // parts that are associated with this call.
40 class JavaAudioManager {
41 public:
42 JavaAudioManager(NativeRegistration* native_registration,
kwibergf01633e2016-02-24 05:00:36 -080043 std::unique_ptr<GlobalRef> audio_manager);
henrika796e1722015-05-28 14:18:33 +020044 ~JavaAudioManager();
henrikab2619892015-05-18 16:49:16 +020045
henrika796e1722015-05-28 14:18:33 +020046 bool Init();
47 void Close();
henrikafe55c382015-06-05 11:45:56 +020048 bool IsCommunicationModeEnabled();
henrika8a897182015-06-09 10:45:09 +020049 bool IsDeviceBlacklistedForOpenSLESUsage();
henrikab2619892015-05-18 16:49:16 +020050
henrika796e1722015-05-28 14:18:33 +020051 private:
kwibergf01633e2016-02-24 05:00:36 -080052 std::unique_ptr<GlobalRef> audio_manager_;
henrika796e1722015-05-28 14:18:33 +020053 jmethodID init_;
54 jmethodID dispose_;
henrikafe55c382015-06-05 11:45:56 +020055 jmethodID is_communication_mode_enabled_;
henrika8a897182015-06-09 10:45:09 +020056 jmethodID is_device_blacklisted_for_open_sles_usage_;
henrikab2619892015-05-18 16:49:16 +020057 };
henrika8324b522015-03-27 10:56:23 +010058
59 AudioManager();
60 ~AudioManager();
61
henrikab2619892015-05-18 16:49:16 +020062 // Sets the currently active audio layer combination. Must be called before
63 // Init().
64 void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer);
65
henrika09bf1a12015-04-10 11:46:55 +020066 // Initializes the audio manager and stores the current audio mode.
henrika8324b522015-03-27 10:56:23 +010067 bool Init();
68 // Revert any setting done by Init().
69 bool Close();
70
henrikafe55c382015-06-05 11:45:56 +020071 // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION.
72 bool IsCommunicationModeEnabled() const;
henrika09bf1a12015-04-10 11:46:55 +020073
henrika8324b522015-03-27 10:56:23 +010074 // Native audio parameters stored during construction.
henrikab2619892015-05-18 16:49:16 +020075 const AudioParameters& GetPlayoutAudioParameters();
76 const AudioParameters& GetRecordAudioParameters();
henrika8324b522015-03-27 10:56:23 +010077
henrikac14f5ff2015-09-23 14:08:33 +020078 // Returns true if the device supports built-in audio effects for AEC, AGC
79 // and NS. Some devices can also be blacklisted for use in combination with
80 // platform effects and these devices will return false.
henrikab2619892015-05-18 16:49:16 +020081 // Can currently only be used in combination with a Java based audio backend
82 // for the recoring side (i.e. using the android.media.AudioRecord API).
83 bool IsAcousticEchoCancelerSupported() const;
henrikac14f5ff2015-09-23 14:08:33 +020084 bool IsAutomaticGainControlSupported() const;
85 bool IsNoiseSuppressorSupported() const;
henrikab2619892015-05-18 16:49:16 +020086
87 // Returns true if the device supports the low-latency audio paths in
88 // combination with OpenSL ES.
89 bool IsLowLatencyPlayoutSupported() const;
90
91 // Returns the estimated total delay of this device. Unit is in milliseconds.
92 // The vaule is set once at construction and never changes after that.
93 // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and
94 // webrtc::kHighLatencyModeDelayEstimateInMilliseconds.
95 int GetDelayEstimateInMilliseconds() const;
henrika8324b522015-03-27 10:56:23 +010096
97 private:
98 // Called from Java side so we can cache the native audio parameters.
99 // This method will be called by the WebRtcAudioManager constructor, i.e.
100 // on the same thread that this object is created on.
henrikab2619892015-05-18 16:49:16 +0200101 static void JNICALL CacheAudioParameters(JNIEnv* env,
102 jobject obj,
103 jint sample_rate,
104 jint channels,
105 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200106 jboolean hardware_agc,
107 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200108 jboolean low_latency_output,
109 jint output_buffer_size,
110 jint input_buffer_size,
111 jlong native_audio_manager);
112 void OnCacheAudioParameters(JNIEnv* env,
113 jint sample_rate,
114 jint channels,
115 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200116 jboolean hardware_agc,
117 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200118 jboolean low_latency_output,
119 jint output_buffer_size,
henrika1ba936a2015-11-03 04:27:58 -0800120 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100121
122 // Stores thread ID in the constructor.
123 // We can then use ThreadChecker::CalledOnValidThread() to ensure that
124 // other methods are called from the same thread.
125 rtc::ThreadChecker thread_checker_;
126
henrikab2619892015-05-18 16:49:16 +0200127 // Calls AttachCurrentThread() if this thread is not attached at construction.
128 // Also ensures that DetachCurrentThread() is called at destruction.
129 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
130
henrikaee369e42015-05-25 10:11:27 +0200131 // Wraps the JNI interface pointer and methods associated with it.
kwibergf01633e2016-02-24 05:00:36 -0800132 std::unique_ptr<JNIEnvironment> j_environment_;
henrikab2619892015-05-18 16:49:16 +0200133
henrikaee369e42015-05-25 10:11:27 +0200134 // Contains factory method for creating the Java object.
kwibergf01633e2016-02-24 05:00:36 -0800135 std::unique_ptr<NativeRegistration> j_native_registration_;
henrikab2619892015-05-18 16:49:16 +0200136
henrikaee369e42015-05-25 10:11:27 +0200137 // Wraps the Java specific parts of the AudioManager.
kwibergf01633e2016-02-24 05:00:36 -0800138 std::unique_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
henrikab2619892015-05-18 16:49:16 +0200139
140 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100141
142 // Set to true by Init() and false by Close().
143 bool initialized_;
144
henrikab2619892015-05-18 16:49:16 +0200145 // True if device supports hardware (or built-in) AEC.
146 bool hardware_aec_;
henrikac14f5ff2015-09-23 14:08:33 +0200147 // True if device supports hardware (or built-in) AGC.
148 bool hardware_agc_;
149 // True if device supports hardware (or built-in) NS.
150 bool hardware_ns_;
henrikab2619892015-05-18 16:49:16 +0200151
152 // True if device supports the low-latency OpenSL ES audio path.
153 bool low_latency_playout_;
154
155 // The delay estimate can take one of two fixed values depending on if the
156 // device supports low-latency output or not.
157 int delay_estimate_in_milliseconds_;
158
henrika8324b522015-03-27 10:56:23 +0100159 // Contains native parameters (e.g. sample rate, channel configuration).
160 // Set at construction in OnCacheAudioParameters() which is called from
161 // Java on the same thread as this object is created on.
162 AudioParameters playout_parameters_;
163 AudioParameters record_parameters_;
164};
165
166} // namespace webrtc
167
168#endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_