blob: 8d96d27e3913662c538f99788aba1fb026edfd48 [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"
19#include "webrtc/modules/audio_device/include/audio_device_defines.h"
20#include "webrtc/modules/audio_device/audio_device_generic.h"
21#include "webrtc/modules/utility/interface/helpers_android.h"
henrikab2619892015-05-18 16:49:16 +020022#include "webrtc/modules/utility/interface/jvm_android.h"
henrika8324b522015-03-27 10:56:23 +010023
24namespace webrtc {
25
26class AudioParameters {
27 public:
28 enum { kBitsPerSample = 16 };
29 AudioParameters()
30 : sample_rate_(0),
31 channels_(0),
32 frames_per_buffer_(0),
henrikab2619892015-05-18 16:49:16 +020033 frames_per_10ms_buffer_(0),
henrika8324b522015-03-27 10:56:23 +010034 bits_per_sample_(kBitsPerSample) {}
henrikab2619892015-05-18 16:49:16 +020035 AudioParameters(int sample_rate, int channels, int frames_per_buffer)
henrika8324b522015-03-27 10:56:23 +010036 : sample_rate_(sample_rate),
37 channels_(channels),
henrikab2619892015-05-18 16:49:16 +020038 frames_per_buffer_(frames_per_buffer),
39 frames_per_10ms_buffer_(sample_rate / 100),
henrika8324b522015-03-27 10:56:23 +010040 bits_per_sample_(kBitsPerSample) {}
henrikab2619892015-05-18 16:49:16 +020041 void reset(int sample_rate, int channels, int frames_per_buffer) {
henrika8324b522015-03-27 10:56:23 +010042 sample_rate_ = sample_rate;
43 channels_ = channels;
henrikab2619892015-05-18 16:49:16 +020044 frames_per_buffer_ = frames_per_buffer;
45 frames_per_10ms_buffer_ = (sample_rate / 100);
henrika8324b522015-03-27 10:56:23 +010046 }
47 int sample_rate() const { return sample_rate_; }
48 int channels() const { return channels_; }
49 int frames_per_buffer() const { return frames_per_buffer_; }
henrikab2619892015-05-18 16:49:16 +020050 int frames_per_10ms_buffer() const { return frames_per_10ms_buffer_; }
51 int bits_per_sample() const { return bits_per_sample_; }
henrika8324b522015-03-27 10:56:23 +010052 bool is_valid() const {
53 return ((sample_rate_ > 0) && (channels_ > 0) && (frames_per_buffer_ > 0));
54 }
55 int GetBytesPerFrame() const { return channels_ * bits_per_sample_ / 8; }
56 int GetBytesPerBuffer() const {
57 return frames_per_buffer_ * GetBytesPerFrame();
58 }
henrikab2619892015-05-18 16:49:16 +020059 int GetBytesPer10msBuffer() const {
60 return frames_per_10ms_buffer_ * GetBytesPerFrame();
61 }
62 float GetBufferSizeInMilliseconds() const {
63 if (sample_rate_ == 0)
64 return 0.0f;
65 return frames_per_buffer_ / (sample_rate_ / 1000.0f);
66 }
henrika8324b522015-03-27 10:56:23 +010067
68 private:
69 int sample_rate_;
70 int channels_;
henrikab2619892015-05-18 16:49:16 +020071 // Lowest possible size of native audio buffer. Measured in number of frames.
72 // This size is injected into the OpenSL ES output (since it does not "talk
73 // Java") implementation but is currently not utilized by the Java
74 // implementation since it aquires the same value internally.
henrika8324b522015-03-27 10:56:23 +010075 int frames_per_buffer_;
henrikab2619892015-05-18 16:49:16 +020076 int frames_per_10ms_buffer_;
77 int bits_per_sample_;
henrika8324b522015-03-27 10:56:23 +010078};
79
80// Implements support for functions in the WebRTC audio stack for Android that
81// relies on the AudioManager in android.media. It also populates an
82// AudioParameter structure with native audio parameters detected at
83// construction. This class does not make any audio-related modifications
84// unless Init() is called. Caching audio parameters makes no changes but only
85// reads data from the Java side.
henrika8324b522015-03-27 10:56:23 +010086class AudioManager {
87 public:
henrikab2619892015-05-18 16:49:16 +020088 // Wraps the Java specific parts of the AudioManager into one helper class.
89 // Stores method IDs for all supported methods at construction and then
90 // allows calls like JavaAudioManager::Close() while hiding the Java/JNI
91 // parts that are associated with this call.
92 class JavaAudioManager {
93 public:
94 JavaAudioManager(NativeRegistration* native_registration,
95 rtc::scoped_ptr<GlobalRef> audio_manager);
henrika796e1722015-05-28 14:18:33 +020096 ~JavaAudioManager();
henrikab2619892015-05-18 16:49:16 +020097
henrika796e1722015-05-28 14:18:33 +020098 bool Init();
99 void Close();
henrikafe55c382015-06-05 11:45:56 +0200100 bool IsCommunicationModeEnabled();
henrika8a897182015-06-09 10:45:09 +0200101 bool IsDeviceBlacklistedForOpenSLESUsage();
henrikab2619892015-05-18 16:49:16 +0200102
henrika796e1722015-05-28 14:18:33 +0200103 private:
104 rtc::scoped_ptr<GlobalRef> audio_manager_;
105 jmethodID init_;
106 jmethodID dispose_;
henrikafe55c382015-06-05 11:45:56 +0200107 jmethodID is_communication_mode_enabled_;
henrika8a897182015-06-09 10:45:09 +0200108 jmethodID is_device_blacklisted_for_open_sles_usage_;
henrikab2619892015-05-18 16:49:16 +0200109 };
henrika8324b522015-03-27 10:56:23 +0100110
111 AudioManager();
112 ~AudioManager();
113
henrikab2619892015-05-18 16:49:16 +0200114 // Sets the currently active audio layer combination. Must be called before
115 // Init().
116 void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer);
117
henrika09bf1a12015-04-10 11:46:55 +0200118 // Initializes the audio manager and stores the current audio mode.
henrika8324b522015-03-27 10:56:23 +0100119 bool Init();
120 // Revert any setting done by Init().
121 bool Close();
122
henrikafe55c382015-06-05 11:45:56 +0200123 // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION.
124 bool IsCommunicationModeEnabled() const;
henrika09bf1a12015-04-10 11:46:55 +0200125
henrika8324b522015-03-27 10:56:23 +0100126 // Native audio parameters stored during construction.
henrikab2619892015-05-18 16:49:16 +0200127 const AudioParameters& GetPlayoutAudioParameters();
128 const AudioParameters& GetRecordAudioParameters();
henrika8324b522015-03-27 10:56:23 +0100129
henrikab2619892015-05-18 16:49:16 +0200130 // Returns true if the device supports a built-in Acoustic Echo Canceler.
131 // Some devices can also be blacklisted for use in combination with an AEC
132 // and these devices will return false.
133 // Can currently only be used in combination with a Java based audio backend
134 // for the recoring side (i.e. using the android.media.AudioRecord API).
135 bool IsAcousticEchoCancelerSupported() const;
136
137 // Returns true if the device supports the low-latency audio paths in
138 // combination with OpenSL ES.
139 bool IsLowLatencyPlayoutSupported() const;
140
141 // Returns the estimated total delay of this device. Unit is in milliseconds.
142 // The vaule is set once at construction and never changes after that.
143 // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and
144 // webrtc::kHighLatencyModeDelayEstimateInMilliseconds.
145 int GetDelayEstimateInMilliseconds() const;
henrika8324b522015-03-27 10:56:23 +0100146
147 private:
148 // Called from Java side so we can cache the native audio parameters.
149 // This method will be called by the WebRtcAudioManager constructor, i.e.
150 // on the same thread that this object is created on.
henrikab2619892015-05-18 16:49:16 +0200151 static void JNICALL CacheAudioParameters(JNIEnv* env,
152 jobject obj,
153 jint sample_rate,
154 jint channels,
155 jboolean hardware_aec,
156 jboolean low_latency_output,
157 jint output_buffer_size,
158 jint input_buffer_size,
159 jlong native_audio_manager);
160 void OnCacheAudioParameters(JNIEnv* env,
161 jint sample_rate,
162 jint channels,
163 jboolean hardware_aec,
164 jboolean low_latency_output,
165 jint output_buffer_size,
166 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100167
168 // Stores thread ID in the constructor.
169 // We can then use ThreadChecker::CalledOnValidThread() to ensure that
170 // other methods are called from the same thread.
171 rtc::ThreadChecker thread_checker_;
172
henrikab2619892015-05-18 16:49:16 +0200173 // Calls AttachCurrentThread() if this thread is not attached at construction.
174 // Also ensures that DetachCurrentThread() is called at destruction.
175 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
176
henrikaee369e42015-05-25 10:11:27 +0200177 // Wraps the JNI interface pointer and methods associated with it.
henrikab2619892015-05-18 16:49:16 +0200178 rtc::scoped_ptr<JNIEnvironment> j_environment_;
179
henrikaee369e42015-05-25 10:11:27 +0200180 // Contains factory method for creating the Java object.
henrikab2619892015-05-18 16:49:16 +0200181 rtc::scoped_ptr<NativeRegistration> j_native_registration_;
182
henrikaee369e42015-05-25 10:11:27 +0200183 // Wraps the Java specific parts of the AudioManager.
henrikab2619892015-05-18 16:49:16 +0200184 rtc::scoped_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
185
186 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100187
188 // Set to true by Init() and false by Close().
189 bool initialized_;
190
henrikab2619892015-05-18 16:49:16 +0200191 // True if device supports hardware (or built-in) AEC.
192 bool hardware_aec_;
193
194 // True if device supports the low-latency OpenSL ES audio path.
195 bool low_latency_playout_;
196
197 // The delay estimate can take one of two fixed values depending on if the
198 // device supports low-latency output or not.
199 int delay_estimate_in_milliseconds_;
200
henrika8324b522015-03-27 10:56:23 +0100201 // Contains native parameters (e.g. sample rate, channel configuration).
202 // Set at construction in OnCacheAudioParameters() which is called from
203 // Java on the same thread as this object is created on.
204 AudioParameters playout_parameters_;
205 AudioParameters record_parameters_;
206};
207
208} // namespace webrtc
209
210#endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_