1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
media / audio / android / audio_manager_android.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/audio/android/audio_manager_android.h"
#include <memory>
#include "base/android/build_info.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "build/android_buildflags.h"
#include "media/audio/android/aaudio_input.h"
#include "media/audio/android/aaudio_output.h"
#include "media/audio/android/audio_track_output_stream.h"
#include "media/audio/android/opensles_input.h"
#include "media/audio/android/opensles_output.h"
#include "media/audio/audio_device_description.h"
#include "media/audio/audio_features.h"
#include "media/audio/audio_manager.h"
#include "media/audio/fake_audio_input_stream.h"
#include "media/base/audio_parameters.h"
#include "media/base/channel_layout.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "media/base/android/media_jni_headers/AudioManagerAndroid_jni.h"
using base::android::AppendJavaStringArrayToStringVector;
using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF8ToJavaString;
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
namespace media {
namespace {
// Maximum number of output streams that can be open simultaneously.
const int kMaxOutputStreams = 10;
const int kDefaultInputBufferSize = 1024;
const int kDefaultOutputBufferSize = 2048;
void AddDefaultDevice(AudioDeviceNames* device_names) {
DCHECK(device_names->empty());
device_names->push_front(AudioDeviceName::CreateDefault());
}
// Returns whether the currently connected device is an audio sink.
bool IsAudioSinkConnected() {
return Java_AudioManagerAndroid_isAudioSinkConnected(
base::android::AttachCurrentThread());
}
bool UseAAudioOutput() {
return base::FeatureList::IsEnabled(features::kUseAAudioDriver);
}
bool UseAAudioInput() {
if (!base::FeatureList::IsEnabled(features::kUseAAudioInput)) {
return false;
}
if (auto* info = base::android::BuildInfo::GetInstance()) {
// Disable AAudio input on Unisoc devices running Android 11 and below due
// to missing/broken echo cancellation. See https://crbug.com/344607452.
if (base::StartsWith(info->board(), "ums",
base::CompareCase::INSENSITIVE_ASCII) &&
info->sdk_int() < base::android::SDK_VERSION_S) {
return false;
}
}
return true;
}
} // namespace
std::unique_ptr<AudioManager> CreateAudioManager(
std::unique_ptr<AudioThread> audio_thread,
AudioLogFactory* audio_log_factory) {
return std::make_unique<AudioManagerAndroid>(std::move(audio_thread),
audio_log_factory);
}
AudioManagerAndroid::AudioManagerAndroid(
std::unique_ptr<AudioThread> audio_thread,
AudioLogFactory* audio_log_factory)
: AudioManagerBase(std::move(audio_thread), audio_log_factory),
communication_mode_is_on_(false),
output_volume_override_set_(false),
output_volume_override_(0) {
SetMaxOutputStreamsAllowed(kMaxOutputStreams);
}
AudioManagerAndroid::~AudioManagerAndroid() = default;
void AudioManagerAndroid::InitializeIfNeeded() {
GetTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(base::IgnoreResult(
&AudioManagerAndroid::GetJavaAudioManager),
base::Unretained(this)));
}
void AudioManagerAndroid::ShutdownOnAudioThread() {
AudioManagerBase::ShutdownOnAudioThread();
// Destroy java android manager here because it can only be accessed on the
// audio thread.
if (!j_audio_manager_.is_null()) {
DVLOG(2) << "Destroying Java part of the audio manager";
Java_AudioManagerAndroid_close(base::android::AttachCurrentThread(),
j_audio_manager_);
j_audio_manager_.Reset();
}
}
bool AudioManagerAndroid::HasAudioOutputDevices() {
return true;
}
bool AudioManagerAndroid::HasAudioInputDevices() {
return true;
}
void AudioManagerAndroid::GetAudioInputDeviceNames(
AudioDeviceNames* device_names) {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
// Always add default device parameters as first element.
DCHECK(device_names->empty());
AddDefaultDevice(device_names);
// Get list of available audio devices.
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> j_device_array =
Java_AudioManagerAndroid_getAudioInputDeviceNames(env,
GetJavaAudioManager());
if (j_device_array.is_null()) {
// Most probable reason for a NULL result here is that the process lacks
// MODIFY_AUDIO_SETTINGS or RECORD_AUDIO permissions.
return;
}
AudioDeviceName device;
for (auto j_device : j_device_array.ReadElements<jobject>()) {
ScopedJavaLocalRef<jstring> j_device_name =
Java_AudioDeviceName_name(env, j_device);
ConvertJavaStringToUTF8(env, j_device_name.obj(), &device.device_name);
ScopedJavaLocalRef<jstring> j_device_id =
Java_AudioDeviceName_id(env, j_device);
ConvertJavaStringToUTF8(env, j_device_id.obj(), &device.unique_id);
device_names->push_back(device);
}
for (auto d : *device_names) {
DVLOG(1) << "device_name: " << d.device_name;
DVLOG(1) << "unique_id: " << d.unique_id;
}
}
void AudioManagerAndroid::GetAudioOutputDeviceNames(
AudioDeviceNames* device_names) {
// TODO(henrika): enumerate using GetAudioInputDeviceNames().
AddDefaultDevice(device_names);
}
AudioParameters AudioManagerAndroid::GetInputStreamParameters(
const std::string& device_id) {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
// Use mono as preferred number of input channels on Android to save
// resources. Using mono also avoids a driver issue seen on Samsung
// Galaxy S3 and S4 devices. See http://crbug.com/256851 for details.
JNIEnv* env = AttachCurrentThread();
constexpr ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO;
int buffer_size = Java_AudioManagerAndroid_getMinInputFrameSize(
env, GetNativeOutputSampleRate(),
ChannelLayoutToChannelCount(channel_layout));
buffer_size = buffer_size <= 0 ? kDefaultInputBufferSize : buffer_size;
int effects = AudioParameters::NO_EFFECTS;
effects |= Java_AudioManagerAndroid_acousticEchoCancelerIsAvailable(env)
? AudioParameters::ECHO_CANCELLER
: AudioParameters::NO_EFFECTS;
int user_buffer_size = GetUserBufferSize();
if (user_buffer_size)
buffer_size = user_buffer_size;
AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
ChannelLayoutConfig::FromLayout<channel_layout>(),
GetNativeOutputSampleRate(), buffer_size);
params.set_effects(effects);
DVLOG(1) << params.AsHumanReadableString();
return params;
}
const char* AudioManagerAndroid::GetName() {
return "Android";
}
AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream(
const AudioParameters& params,
const std::string& device_id,
const LogCallback& log_callback) {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
AudioOutputStream* stream = AudioManagerBase::MakeAudioOutputStream(
params, device_id, AudioManager::LogCallback());
if (stream)
streams_.insert(static_cast<MuteableAudioOutputStream*>(stream));
return stream;
}
AudioInputStream* AudioManagerAndroid::MakeAudioInputStream(
const AudioParameters& params,
const std::string& device_id,
const LogCallback& log_callback) {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
bool has_input_streams = !HasNoAudioInputStreams();
bool force_communication_mode = false;
AudioInputStream* stream = AudioManagerBase::MakeAudioInputStream(
params, device_id, AudioManager::LogCallback());
// Avoid changing the communication mode if there are existing input streams.
if (!stream || has_input_streams) {
return stream;
}
// By default, the audio manager for Android creates streams intended for
// real-time VoIP sessions and therefore sets the audio mode to
// MODE_IN_COMMUNICATION. However, the user might have asked for a special
// mode where all audio input processing is disabled, and if that is the case
// we avoid changing the mode.
// To ensure proper audio routing when a Bluetooth microphone is in use,
// Android's audio manager must switch the output from TYPE_BLUETOOTH_A2DP to
// TYPE_BLUETOOTH_SCO. This switch is triggered by setting the audio mode to
// MODE_IN_COMMUNICATION. Failing to activate communication mode can result
// in audio being routed incorrectly, leading to no sound output from the
// Bluetooth headset.
#if BUILDFLAG(IS_DESKTOP_ANDROID)
force_communication_mode = IsBluetoothMicrophoneOn();
#endif
if (params.effects() != AudioParameters::NO_EFFECTS ||
force_communication_mode) {
communication_mode_is_on_ = true;
SetCommunicationAudioModeOn(true);
}
return stream;
}
void AudioManagerAndroid::ReleaseOutputStream(AudioOutputStream* stream) {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
streams_.erase(static_cast<MuteableAudioOutputStream*>(stream));
AudioManagerBase::ReleaseOutputStream(stream);
}
void AudioManagerAndroid::ReleaseInputStream(AudioInputStream* stream) {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
AudioManagerBase::ReleaseInputStream(stream);
// Restore the audio mode which was used before the first communication-
// mode stream was created.
if (HasNoAudioInputStreams() && communication_mode_is_on_) {
communication_mode_is_on_ = false;
SetCommunicationAudioModeOn(false);
}
}
AudioOutputStream* AudioManagerAndroid::MakeLinearOutputStream(
const AudioParameters& params,
const LogCallback& log_callback) {
DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
if (__builtin_available(android AAUDIO_MIN_API, *)) {
if (UseAAudioOutput()) {
return new AAudioOutputStream(this, params, AAUDIO_USAGE_MEDIA);
}
}
return new OpenSLESOutputStream(this, params, SL_ANDROID_STREAM_MEDIA);
}
AudioOutputStream* AudioManagerAndroid::MakeLowLatencyOutputStream(
const AudioParameters& params,
const std::string& device_id,
const LogCallback& log_callback) {
DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
if (__builtin_available(android AAUDIO_MIN_API, *)) {
if (UseAAudioOutput()) {
const aaudio_usage_t usage = communication_mode_is_on_
? AAUDIO_USAGE_VOICE_COMMUNICATION
: AAUDIO_USAGE_MEDIA;
return new AAudioOutputStream(this, params, usage);
}
}
// Set stream type which matches the current system-wide audio mode used by
// the Android audio manager.
const SLint32 stream_type = communication_mode_is_on_
? SL_ANDROID_STREAM_VOICE
: SL_ANDROID_STREAM_MEDIA;
return new OpenSLESOutputStream(this, params, stream_type);
}
AudioOutputStream* AudioManagerAndroid::MakeBitstreamOutputStream(
const AudioParameters& params,
const std::string& device_id,
const LogCallback& log_callback) {
DCHECK(params.IsBitstreamFormat());
return new AudioTrackOutputStream(this, params);
}
AudioInputStream* AudioManagerAndroid::MakeLinearInputStream(
const AudioParameters& params,
const std::string& device_id,
const LogCallback& log_callback) {
// TODO(henrika): add support for device selection if/when any client
// needs it.
DLOG_IF(ERROR, !device_id.empty()) << "Not implemented!";
DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
if (__builtin_available(android AAUDIO_MIN_API, *)) {
if (UseAAudioInput()) {
return new AAudioInputStream(this, params);
}
}
return new OpenSLESInputStream(this, params);
}
AudioInputStream* AudioManagerAndroid::MakeLowLatencyInputStream(
const AudioParameters& params,
const std::string& device_id,
const LogCallback& log_callback) {
DVLOG(1) << "MakeLowLatencyInputStream: " << params.effects();
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
DLOG_IF(ERROR, device_id.empty()) << "Invalid device ID!";
// Use the device ID to select the correct input device.
// Note that the input device is always associated with a certain output
// device, i.e., this selection does also switch the output device.
// All input and output streams will be affected by the device selection.
if (!SetAudioDevice(device_id)) {
LOG(ERROR) << "Unable to select audio device!";
return NULL;
}
if (__builtin_available(android AAUDIO_MIN_API, *)) {
if (UseAAudioInput()) {
return new AAudioInputStream(this, params);
}
}
// Create a new audio input stream and enable or disable all audio effects
// given |params.effects()|.
return new OpenSLESInputStream(this, params);
}
void AudioManagerAndroid::SetMute(JNIEnv* env,
const JavaParamRef<jobject>& obj,
jboolean muted) {
GetTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&AudioManagerAndroid::DoSetMuteOnAudioThread,
base::Unretained(this), muted));
}
void AudioManagerAndroid::SetOutputVolumeOverride(double volume) {
GetTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&AudioManagerAndroid::DoSetVolumeOnAudioThread,
base::Unretained(this), volume));
}
bool AudioManagerAndroid::HasOutputVolumeOverride(double* out_volume) const {
if (output_volume_override_set_) {
*out_volume = output_volume_override_;
}
return output_volume_override_set_;
}
base::TimeDelta AudioManagerAndroid::GetOutputLatency() {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
JNIEnv* env = AttachCurrentThread();
return base::Milliseconds(
Java_AudioManagerAndroid_getOutputLatency(env, GetJavaAudioManager()));
}
AudioParameters AudioManagerAndroid::GetPreferredOutputStreamParameters(
const std::string& output_device_id,
const AudioParameters& input_params) {
DVLOG(1) << __FUNCTION__;
// TODO(tommi): Support |output_device_id|.
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
DLOG_IF(ERROR, !output_device_id.empty()) << "Not implemented!";
ChannelLayoutConfig channel_layout_config = ChannelLayoutConfig::Stereo();
int sample_rate = GetNativeOutputSampleRate();
int buffer_size = GetOptimalOutputFrameSize(sample_rate, 2);
if (input_params.IsValid()) {
// Use the client's input parameters if they are valid.
sample_rate = input_params.sample_rate();
// AudioManager APIs for GetOptimalOutputFrameSize() don't support channel
// layouts greater than stereo unless low latency audio is supported.
if (input_params.channels() <= 2 || IsAudioLowLatencySupported())
channel_layout_config = input_params.channel_layout_config();
// For high latency playback on supported platforms, pass through the
// requested buffer size; this provides significant power savings (~25%) and
// reduces the potential for glitches under load.
if (input_params.latency_tag() == AudioLatency::Type::kPlayback) {
buffer_size = input_params.frames_per_buffer();
} else {
buffer_size = GetOptimalOutputFrameSize(sample_rate,
channel_layout_config.channels());
}
}
int user_buffer_size = GetUserBufferSize();
if (user_buffer_size)
buffer_size = user_buffer_size;
// Check if device supports additional audio encodings.
if (IsAudioSinkConnected()) {
return GetAudioFormatsSupportedBySinkDevice(
output_device_id, channel_layout_config, sample_rate, buffer_size);
}
return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
channel_layout_config, sample_rate, buffer_size);
}
bool AudioManagerAndroid::HasNoAudioInputStreams() {
return input_stream_count() == 0;
}
const JavaRef<jobject>& AudioManagerAndroid::GetJavaAudioManager() {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
if (j_audio_manager_.is_null()) {
// Create the Android audio manager on the audio thread.
DVLOG(2) << "Creating Java part of the audio manager";
j_audio_manager_.Reset(Java_AudioManagerAndroid_createAudioManagerAndroid(
base::android::AttachCurrentThread(),
reinterpret_cast<intptr_t>(this)));
// Prepare the list of audio devices and register receivers for device
// notifications.
Java_AudioManagerAndroid_init(base::android::AttachCurrentThread(),
j_audio_manager_);
}
return j_audio_manager_;
}
void AudioManagerAndroid::SetCommunicationAudioModeOn(bool on) {
DVLOG(1) << __FUNCTION__ << ": " << on;
Java_AudioManagerAndroid_setCommunicationAudioModeOn(
base::android::AttachCurrentThread(), GetJavaAudioManager(), on);
}
bool AudioManagerAndroid::SetAudioDevice(const std::string& device_id) {
DVLOG(1) << __FUNCTION__ << ": " << device_id;
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
// Send the unique device ID to the Java audio manager and make the
// device switch. Provide an empty string to the Java audio manager
// if the default device is selected.
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> j_device_id = ConvertUTF8ToJavaString(
env, device_id == AudioDeviceDescription::kDefaultDeviceId ? std::string()
: device_id);
return Java_AudioManagerAndroid_setDevice(env, GetJavaAudioManager(),
j_device_id);
}
bool AudioManagerAndroid::IsBluetoothMicrophoneOn() {
return Java_AudioManagerAndroid_isBluetoothMicrophoneOn(
base::android::AttachCurrentThread(), GetJavaAudioManager());
}
int AudioManagerAndroid::GetNativeOutputSampleRate() {
return Java_AudioManagerAndroid_getNativeOutputSampleRate(
base::android::AttachCurrentThread(), GetJavaAudioManager());
}
bool AudioManagerAndroid::IsAudioLowLatencySupported() {
return Java_AudioManagerAndroid_isAudioLowLatencySupported(
base::android::AttachCurrentThread(), GetJavaAudioManager());
}
int AudioManagerAndroid::GetAudioLowLatencyOutputFrameSize() {
return Java_AudioManagerAndroid_getAudioLowLatencyOutputFrameSize(
base::android::AttachCurrentThread(), GetJavaAudioManager());
}
int AudioManagerAndroid::GetOptimalOutputFrameSize(int sample_rate,
int channels) {
if (IsAudioLowLatencySupported())
return GetAudioLowLatencyOutputFrameSize();
return std::max(kDefaultOutputBufferSize,
Java_AudioManagerAndroid_getMinOutputFrameSize(
base::android::AttachCurrentThread(),
sample_rate, channels));
}
// Returns a bit mask of AudioParameters::Format enum values sink device
// supports.
int AudioManagerAndroid::GetSinkAudioEncodingFormats() {
JNIEnv* env = AttachCurrentThread();
return Java_AudioManagerAndroid_getAudioEncodingFormatsSupported(env);
}
// Returns encoding bitstream formats supported by Sink device. Returns
// AudioParameters structure.
AudioParameters AudioManagerAndroid::GetAudioFormatsSupportedBySinkDevice(
const std::string& output_device_id,
const ChannelLayoutConfig& channel_layout_config,
int sample_rate,
int buffer_size) {
int formats = GetSinkAudioEncodingFormats();
DVLOG(1) << __func__ << ": IsAudioSinkConnected()==true, output_device_id="
<< output_device_id << ", Supported Encodings=" << formats;
return AudioParameters(
AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout_config,
sample_rate, buffer_size,
AudioParameters::HardwareCapabilities(formats,
/*require_encapsulation=*/false));
}
void AudioManagerAndroid::DoSetMuteOnAudioThread(bool muted) {
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
for (OutputStreams::iterator it = streams_.begin();
it != streams_.end(); ++it) {
(*it)->SetMute(muted);
}
}
void AudioManagerAndroid::DoSetVolumeOnAudioThread(double volume) {
output_volume_override_set_ = true;
output_volume_override_ = volume;
DCHECK(GetTaskRunner()->BelongsToCurrentThread());
for (OutputStreams::iterator it = streams_.begin(); it != streams_.end();
++it) {
(*it)->SetVolume(volume);
}
}
} // namespace media