@@ -71,6 +71,7 @@ private static int getDefaultUsageAttribute() {
71
71
72
72
private ByteBuffer byteBuffer ;
73
73
74
+ private @ Nullable final AudioAttributes audioAttributes ;
74
75
private @ Nullable AudioTrack audioTrack ;
75
76
private @ Nullable AudioTrackThread audioThread ;
76
77
private final VolumeLogger volumeLogger ;
@@ -162,15 +163,17 @@ public void stopThread() {
162
163
163
164
@ CalledByNative
164
165
WebRtcAudioTrack (Context context , AudioManager audioManager ) {
165
- this (context , audioManager , null /* errorCallback */ , null /* stateCallback */ );
166
+ this (context , audioManager , null /* audioAttributes */ , null /* errorCallback */ ,
167
+ null /* stateCallback */ );
166
168
}
167
169
168
170
WebRtcAudioTrack (Context context , AudioManager audioManager ,
169
- @ Nullable AudioTrackErrorCallback errorCallback ,
171
+ @ Nullable AudioAttributes audioAttributes , @ Nullable AudioTrackErrorCallback errorCallback ,
170
172
@ Nullable AudioTrackStateCallback stateCallback ) {
171
173
threadChecker .detachThread ();
172
174
this .context = context ;
173
175
this .audioManager = audioManager ;
176
+ this .audioAttributes = audioAttributes ;
174
177
this .errorCallback = errorCallback ;
175
178
this .stateCallback = stateCallback ;
176
179
this .volumeLogger = new VolumeLogger (audioManager );
@@ -231,8 +234,8 @@ private int initPlayout(int sampleRate, int channels, double bufferSizeFactor) {
231
234
// supersede the notion of stream types for defining the behavior of audio playback,
232
235
// and to allow certain platforms or routing policies to use this information for more
233
236
// refined volume or routing decisions.
234
- audioTrack =
235
- createAudioTrackOnLollipopOrHigher ( sampleRate , channelConfig , minBufferSizeInBytes );
237
+ audioTrack = createAudioTrackOnLollipopOrHigher (
238
+ sampleRate , channelConfig , minBufferSizeInBytes , audioAttributes );
236
239
} else {
237
240
// Use default constructor for API levels below 21.
238
241
audioTrack =
@@ -383,8 +386,8 @@ private void logMainParameters() {
383
386
// It allows certain platforms or routing policies to use this information for more
384
387
// refined volume or routing decisions.
385
388
@ 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 ) {
388
391
Logging .d (TAG , "createAudioTrackOnLollipopOrHigher" );
389
392
// TODO(henrika): use setPerformanceMode(int) with PERFORMANCE_MODE_LOW_LATENCY to control
390
393
// performance when Android O is supported. Add some logging in the mean time.
@@ -394,11 +397,26 @@ private static AudioTrack createAudioTrackOnLollipopOrHigher(
394
397
if (sampleRateInHz != nativeOutputSampleRate ) {
395
398
Logging .w (TAG , "Unable to use fast mode since requested sample rate is not native" );
396
399
}
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
+
397
418
// 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 (),
402
420
new AudioFormat .Builder ()
403
421
.setEncoding (AudioFormat .ENCODING_PCM_16BIT )
404
422
.setSampleRate (sampleRateInHz )
0 commit comments