blob: a2f192ca00bdf18ee6554d9cef9aeb5d4e3344d1 [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();
henrikab2619892015-05-18 16:49:16 +0200101
henrika796e1722015-05-28 14:18:33 +0200102 private:
103 rtc::scoped_ptr<GlobalRef> audio_manager_;
104 jmethodID init_;
105 jmethodID dispose_;
henrikafe55c382015-06-05 11:45:56 +0200106 jmethodID is_communication_mode_enabled_;
henrikab2619892015-05-18 16:49:16 +0200107 };
henrika8324b522015-03-27 10:56:23 +0100108
109 AudioManager();
110 ~AudioManager();
111
henrikab2619892015-05-18 16:49:16 +0200112 // Sets the currently active audio layer combination. Must be called before
113 // Init().
114 void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer);
115
henrika09bf1a12015-04-10 11:46:55 +0200116 // Initializes the audio manager and stores the current audio mode.
henrika8324b522015-03-27 10:56:23 +0100117 bool Init();
118 // Revert any setting done by Init().
119 bool Close();
120
henrikafe55c382015-06-05 11:45:56 +0200121 // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION.
122 bool IsCommunicationModeEnabled() const;
henrika09bf1a12015-04-10 11:46:55 +0200123
henrika8324b522015-03-27 10:56:23 +0100124 // Native audio parameters stored during construction.
henrikab2619892015-05-18 16:49:16 +0200125 const AudioParameters& GetPlayoutAudioParameters();
126 const AudioParameters& GetRecordAudioParameters();
henrika8324b522015-03-27 10:56:23 +0100127
henrikab2619892015-05-18 16:49:16 +0200128 // Returns true if the device supports a built-in Acoustic Echo Canceler.
129 // Some devices can also be blacklisted for use in combination with an AEC
130 // and these devices will return false.
131 // Can currently only be used in combination with a Java based audio backend
132 // for the recoring side (i.e. using the android.media.AudioRecord API).
133 bool IsAcousticEchoCancelerSupported() const;
134
135 // Returns true if the device supports the low-latency audio paths in
136 // combination with OpenSL ES.
137 bool IsLowLatencyPlayoutSupported() const;
138
139 // Returns the estimated total delay of this device. Unit is in milliseconds.
140 // The vaule is set once at construction and never changes after that.
141 // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and
142 // webrtc::kHighLatencyModeDelayEstimateInMilliseconds.
143 int GetDelayEstimateInMilliseconds() const;
henrika8324b522015-03-27 10:56:23 +0100144
145 private:
146 // Called from Java side so we can cache the native audio parameters.
147 // This method will be called by the WebRtcAudioManager constructor, i.e.
148 // on the same thread that this object is created on.
henrikab2619892015-05-18 16:49:16 +0200149 static void JNICALL CacheAudioParameters(JNIEnv* env,
150 jobject obj,
151 jint sample_rate,
152 jint channels,
153 jboolean hardware_aec,
154 jboolean low_latency_output,
155 jint output_buffer_size,
156 jint input_buffer_size,
157 jlong native_audio_manager);
158 void OnCacheAudioParameters(JNIEnv* env,
159 jint sample_rate,
160 jint channels,
161 jboolean hardware_aec,
162 jboolean low_latency_output,
163 jint output_buffer_size,
164 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100165
166 // Stores thread ID in the constructor.
167 // We can then use ThreadChecker::CalledOnValidThread() to ensure that
168 // other methods are called from the same thread.
169 rtc::ThreadChecker thread_checker_;
170
henrikab2619892015-05-18 16:49:16 +0200171 // Calls AttachCurrentThread() if this thread is not attached at construction.
172 // Also ensures that DetachCurrentThread() is called at destruction.
173 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
174
henrikaee369e42015-05-25 10:11:27 +0200175 // Wraps the JNI interface pointer and methods associated with it.
henrikab2619892015-05-18 16:49:16 +0200176 rtc::scoped_ptr<JNIEnvironment> j_environment_;
177
henrikaee369e42015-05-25 10:11:27 +0200178 // Contains factory method for creating the Java object.
henrikab2619892015-05-18 16:49:16 +0200179 rtc::scoped_ptr<NativeRegistration> j_native_registration_;
180
henrikaee369e42015-05-25 10:11:27 +0200181 // Wraps the Java specific parts of the AudioManager.
henrikab2619892015-05-18 16:49:16 +0200182 rtc::scoped_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
183
184 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100185
186 // Set to true by Init() and false by Close().
187 bool initialized_;
188
henrikab2619892015-05-18 16:49:16 +0200189 // True if device supports hardware (or built-in) AEC.
190 bool hardware_aec_;
191
192 // True if device supports the low-latency OpenSL ES audio path.
193 bool low_latency_playout_;
194
195 // 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_