blob: 9cceaacfca7236f64bd4662a68ac5910018c8ba3 [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
14#include <jni.h>
15
henrikab2619892015-05-18 16:49:16 +020016#include "webrtc/base/scoped_ptr.h"
henrika8324b522015-03-27 10:56:23 +010017#include "webrtc/base/thread_checker.h"
18#include "webrtc/modules/audio_device/android/audio_common.h"
henrikaba35d052015-07-14 17:04:08 +020019#include "webrtc/modules/audio_device/audio_device_config.h"
henrika8324b522015-03-27 10:56:23 +010020#include "webrtc/modules/audio_device/include/audio_device_defines.h"
21#include "webrtc/modules/audio_device/audio_device_generic.h"
22#include "webrtc/modules/utility/interface/helpers_android.h"
henrikab2619892015-05-18 16:49:16 +020023#include "webrtc/modules/utility/interface/jvm_android.h"
henrika8324b522015-03-27 10:56:23 +010024
25namespace webrtc {
26
henrika8324b522015-03-27 10:56:23 +010027// Implements support for functions in the WebRTC audio stack for Android that
28// relies on the AudioManager in android.media. It also populates an
29// AudioParameter structure with native audio parameters detected at
30// construction. This class does not make any audio-related modifications
31// unless Init() is called. Caching audio parameters makes no changes but only
32// reads data from the Java side.
henrika8324b522015-03-27 10:56:23 +010033class AudioManager {
34 public:
henrikab2619892015-05-18 16:49:16 +020035 // Wraps the Java specific parts of the AudioManager into one helper class.
36 // Stores method IDs for all supported methods at construction and then
37 // allows calls like JavaAudioManager::Close() while hiding the Java/JNI
38 // parts that are associated with this call.
39 class JavaAudioManager {
40 public:
41 JavaAudioManager(NativeRegistration* native_registration,
42 rtc::scoped_ptr<GlobalRef> audio_manager);
henrika796e1722015-05-28 14:18:33 +020043 ~JavaAudioManager();
henrikab2619892015-05-18 16:49:16 +020044
henrika796e1722015-05-28 14:18:33 +020045 bool Init();
46 void Close();
henrikafe55c382015-06-05 11:45:56 +020047 bool IsCommunicationModeEnabled();
henrika8a897182015-06-09 10:45:09 +020048 bool IsDeviceBlacklistedForOpenSLESUsage();
henrikab2619892015-05-18 16:49:16 +020049
henrika796e1722015-05-28 14:18:33 +020050 private:
51 rtc::scoped_ptr<GlobalRef> audio_manager_;
52 jmethodID init_;
53 jmethodID dispose_;
henrikafe55c382015-06-05 11:45:56 +020054 jmethodID is_communication_mode_enabled_;
henrika8a897182015-06-09 10:45:09 +020055 jmethodID is_device_blacklisted_for_open_sles_usage_;
henrikab2619892015-05-18 16:49:16 +020056 };
henrika8324b522015-03-27 10:56:23 +010057
58 AudioManager();
59 ~AudioManager();
60
henrikab2619892015-05-18 16:49:16 +020061 // Sets the currently active audio layer combination. Must be called before
62 // Init().
63 void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer);
64
henrika09bf1a12015-04-10 11:46:55 +020065 // Initializes the audio manager and stores the current audio mode.
henrika8324b522015-03-27 10:56:23 +010066 bool Init();
67 // Revert any setting done by Init().
68 bool Close();
69
henrikafe55c382015-06-05 11:45:56 +020070 // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION.
71 bool IsCommunicationModeEnabled() const;
henrika09bf1a12015-04-10 11:46:55 +020072
henrika8324b522015-03-27 10:56:23 +010073 // Native audio parameters stored during construction.
henrikab2619892015-05-18 16:49:16 +020074 const AudioParameters& GetPlayoutAudioParameters();
75 const AudioParameters& GetRecordAudioParameters();
henrika8324b522015-03-27 10:56:23 +010076
henrikac14f5ff2015-09-23 14:08:33 +020077 // Returns true if the device supports built-in audio effects for AEC, AGC
78 // and NS. Some devices can also be blacklisted for use in combination with
79 // platform effects and these devices will return false.
henrikab2619892015-05-18 16:49:16 +020080 // Can currently only be used in combination with a Java based audio backend
81 // for the recoring side (i.e. using the android.media.AudioRecord API).
82 bool IsAcousticEchoCancelerSupported() const;
henrikac14f5ff2015-09-23 14:08:33 +020083 bool IsAutomaticGainControlSupported() const;
84 bool IsNoiseSuppressorSupported() const;
henrikab2619892015-05-18 16:49:16 +020085
86 // Returns true if the device supports the low-latency audio paths in
87 // combination with OpenSL ES.
88 bool IsLowLatencyPlayoutSupported() const;
89
90 // Returns the estimated total delay of this device. Unit is in milliseconds.
91 // The vaule is set once at construction and never changes after that.
92 // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and
93 // webrtc::kHighLatencyModeDelayEstimateInMilliseconds.
94 int GetDelayEstimateInMilliseconds() const;
henrika8324b522015-03-27 10:56:23 +010095
96 private:
97 // Called from Java side so we can cache the native audio parameters.
98 // This method will be called by the WebRtcAudioManager constructor, i.e.
99 // on the same thread that this object is created on.
henrikab2619892015-05-18 16:49:16 +0200100 static void JNICALL CacheAudioParameters(JNIEnv* env,
101 jobject obj,
102 jint sample_rate,
103 jint channels,
104 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200105 jboolean hardware_agc,
106 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200107 jboolean low_latency_output,
108 jint output_buffer_size,
109 jint input_buffer_size,
110 jlong native_audio_manager);
111 void OnCacheAudioParameters(JNIEnv* env,
112 jint sample_rate,
113 jint channels,
114 jboolean hardware_aec,
henrikac14f5ff2015-09-23 14:08:33 +0200115 jboolean hardware_agc,
116 jboolean hardware_ns,
henrikab2619892015-05-18 16:49:16 +0200117 jboolean low_latency_output,
118 jint output_buffer_size,
119 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100120
121 // Stores thread ID in the constructor.
122 // We can then use ThreadChecker::CalledOnValidThread() to ensure that
123 // other methods are called from the same thread.
124 rtc::ThreadChecker thread_checker_;
125
henrikab2619892015-05-18 16:49:16 +0200126 // Calls AttachCurrentThread() if this thread is not attached at construction.
127 // Also ensures that DetachCurrentThread() is called at destruction.
128 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
129
henrikaee369e42015-05-25 10:11:27 +0200130 // Wraps the JNI interface pointer and methods associated with it.
henrikab2619892015-05-18 16:49:16 +0200131 rtc::scoped_ptr<JNIEnvironment> j_environment_;
132
henrikaee369e42015-05-25 10:11:27 +0200133 // Contains factory method for creating the Java object.
henrikab2619892015-05-18 16:49:16 +0200134 rtc::scoped_ptr<NativeRegistration> j_native_registration_;
135
henrikaee369e42015-05-25 10:11:27 +0200136 // Wraps the Java specific parts of the AudioManager.
henrikab2619892015-05-18 16:49:16 +0200137 rtc::scoped_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
138
139 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100140
141 // Set to true by Init() and false by Close().
142 bool initialized_;
143
henrikab2619892015-05-18 16:49:16 +0200144 // True if device supports hardware (or built-in) AEC.
145 bool hardware_aec_;
henrikac14f5ff2015-09-23 14:08:33 +0200146 // True if device supports hardware (or built-in) AGC.
147 bool hardware_agc_;
148 // True if device supports hardware (or built-in) NS.
149 bool hardware_ns_;
henrikab2619892015-05-18 16:49:16 +0200150
151 // True if device supports the low-latency OpenSL ES audio path.
152 bool low_latency_playout_;
153
154 // The delay estimate can take one of two fixed values depending on if the
155 // device supports low-latency output or not.
156 int delay_estimate_in_milliseconds_;
157
henrika8324b522015-03-27 10:56:23 +0100158 // Contains native parameters (e.g. sample rate, channel configuration).
159 // Set at construction in OnCacheAudioParameters() which is called from
160 // Java on the same thread as this object is created on.
161 AudioParameters playout_parameters_;
162 AudioParameters record_parameters_;
163};
164
165} // namespace webrtc
166
167#endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_