Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit b249d0a

Browse files
Gaurav VaishCommit Bot
authored andcommitted
Allow AudioAttributes to be app/client configurable
WebRtcAudioTrack is hardcoded to configure AudioAttributes with 1. usage=USAGE_VOICE_COMMUNICATIOON 2. contentType=CONTENT_TYPE_SPEECH This change allows AudioAttributes to be configured via the JavaAudioDeviceModule. Bug: webrtc:12153 Change-Id: I67c7f6e572c5a9f3a8fde674b6600d2adaf17895 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/191941 Commit-Queue: Gaurav Vaish <[email protected]> Reviewed-by: Henrik Andreassson <[email protected]> Reviewed-by: Paulina Hensman <[email protected]> Cr-Commit-Position: refs/heads/master@{#32583}
1 parent 0bfdbc3 commit b249d0a

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

sdk/android/api/org/webrtc/audio/JavaAudioDeviceModule.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package org.webrtc.audio;
1212

1313
import android.content.Context;
14+
import android.media.AudioAttributes;
1415
import android.media.AudioDeviceInfo;
1516
import android.media.AudioManager;
1617
import android.os.Build;
@@ -47,6 +48,7 @@ public static class Builder {
4748
private boolean useHardwareNoiseSuppressor = isBuiltInNoiseSuppressorSupported();
4849
private boolean useStereoInput;
4950
private boolean useStereoOutput;
51+
private AudioAttributes audioAttributes;
5052

5153
private Builder(Context context) {
5254
this.context = context;
@@ -193,6 +195,14 @@ public Builder setUseStereoOutput(boolean useStereoOutput) {
193195
return this;
194196
}
195197

198+
/**
199+
* Set custom {@link AudioAttributes} to use.
200+
*/
201+
public Builder setAudioAttributes(AudioAttributes audioAttributes) {
202+
this.audioAttributes = audioAttributes;
203+
return this;
204+
}
205+
196206
/**
197207
* Construct an AudioDeviceModule based on the supplied arguments. The caller takes ownership
198208
* and is responsible for calling release().
@@ -223,7 +233,7 @@ public JavaAudioDeviceModule createAudioDeviceModule() {
223233
audioSource, audioFormat, audioRecordErrorCallback, audioRecordStateCallback,
224234
samplesReadyCallback, useHardwareAcousticEchoCanceler, useHardwareNoiseSuppressor);
225235
final WebRtcAudioTrack audioOutput = new WebRtcAudioTrack(
226-
context, audioManager, audioTrackErrorCallback, audioTrackStateCallback);
236+
context, audioManager, audioAttributes, audioTrackErrorCallback, audioTrackStateCallback);
227237
return new JavaAudioDeviceModule(context, audioManager, audioInput, audioOutput,
228238
inputSampleRate, outputSampleRate, useStereoInput, useStereoOutput);
229239
}

sdk/android/src/java/org/webrtc/audio/WebRtcAudioTrack.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private static int getDefaultUsageAttribute() {
7171

7272
private ByteBuffer byteBuffer;
7373

74+
private @Nullable final AudioAttributes audioAttributes;
7475
private @Nullable AudioTrack audioTrack;
7576
private @Nullable AudioTrackThread audioThread;
7677
private final VolumeLogger volumeLogger;
@@ -162,15 +163,17 @@ public void stopThread() {
162163

163164
@CalledByNative
164165
WebRtcAudioTrack(Context context, AudioManager audioManager) {
165-
this(context, audioManager, null /* errorCallback */, null /* stateCallback */);
166+
this(context, audioManager, null /* audioAttributes */, null /* errorCallback */,
167+
null /* stateCallback */);
166168
}
167169

168170
WebRtcAudioTrack(Context context, AudioManager audioManager,
169-
@Nullable AudioTrackErrorCallback errorCallback,
171+
@Nullable AudioAttributes audioAttributes, @Nullable AudioTrackErrorCallback errorCallback,
170172
@Nullable AudioTrackStateCallback stateCallback) {
171173
threadChecker.detachThread();
172174
this.context = context;
173175
this.audioManager = audioManager;
176+
this.audioAttributes = audioAttributes;
174177
this.errorCallback = errorCallback;
175178
this.stateCallback = stateCallback;
176179
this.volumeLogger = new VolumeLogger(audioManager);
@@ -231,8 +234,8 @@ private int initPlayout(int sampleRate, int channels, double bufferSizeFactor) {
231234
// supersede the notion of stream types for defining the behavior of audio playback,
232235
// and to allow certain platforms or routing policies to use this information for more
233236
// refined volume or routing decisions.
234-
audioTrack =
235-
createAudioTrackOnLollipopOrHigher(sampleRate, channelConfig, minBufferSizeInBytes);
237+
audioTrack = createAudioTrackOnLollipopOrHigher(
238+
sampleRate, channelConfig, minBufferSizeInBytes, audioAttributes);
236239
} else {
237240
// Use default constructor for API levels below 21.
238241
audioTrack =
@@ -383,8 +386,8 @@ private void logMainParameters() {
383386
// It allows certain platforms or routing policies to use this information for more
384387
// refined volume or routing decisions.
385388
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
386-
private static AudioTrack createAudioTrackOnLollipopOrHigher(
387-
int sampleRateInHz, int channelConfig, int bufferSizeInBytes) {
389+
private static AudioTrack createAudioTrackOnLollipopOrHigher(int sampleRateInHz,
390+
int channelConfig, int bufferSizeInBytes, @Nullable AudioAttributes overrideAttributes) {
388391
Logging.d(TAG, "createAudioTrackOnLollipopOrHigher");
389392
// TODO(henrika): use setPerformanceMode(int) with PERFORMANCE_MODE_LOW_LATENCY to control
390393
// performance when Android O is supported. Add some logging in the mean time.
@@ -394,11 +397,26 @@ private static AudioTrack createAudioTrackOnLollipopOrHigher(
394397
if (sampleRateInHz != nativeOutputSampleRate) {
395398
Logging.w(TAG, "Unable to use fast mode since requested sample rate is not native");
396399
}
400+
401+
AudioAttributes.Builder attributesBuilder =
402+
new AudioAttributes.Builder()
403+
.setUsage(DEFAULT_USAGE)
404+
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH);
405+
406+
if (overrideAttributes != null) {
407+
if (overrideAttributes.getUsage() != AudioAttributes.USAGE_UNKNOWN) {
408+
attributesBuilder.setUsage(overrideAttributes.getUsage());
409+
}
410+
if (overrideAttributes.getContentType() != AudioAttributes.CONTENT_TYPE_UNKNOWN) {
411+
attributesBuilder.setContentType(overrideAttributes.getContentType());
412+
}
413+
414+
attributesBuilder.setAllowedCapturePolicy(overrideAttributes.getAllowedCapturePolicy())
415+
.setFlags(overrideAttributes.getFlags());
416+
}
417+
397418
// Create an audio track where the audio usage is for VoIP and the content type is speech.
398-
return new AudioTrack(new AudioAttributes.Builder()
399-
.setUsage(DEFAULT_USAGE)
400-
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
401-
.build(),
419+
return new AudioTrack(attributesBuilder.build(),
402420
new AudioFormat.Builder()
403421
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
404422
.setSampleRate(sampleRateInHz)

0 commit comments

Comments
 (0)