Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions sdk/android/src/java/org/webrtc/audio/WebRtcAudioTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,18 @@ public void setSpeakerMute(boolean mute) {
public boolean updateAudioTrackUsage(int usage) {
Logging.d(TAG, "updateAudioTrackUsage(usage=" + usage + ")");

// if audioTrack is not created yet
// update the current audioAttributes with audioAttributes which the user has chosen
// return true because when native calls initPlayout it will pick the updated audioAttribute
if (audioTrack == null) {
audioAttributes = getAudioAttributes(usage);
return true;
}

// Check if the usage is already the same
if (audioAttributes != null && audioAttributes.getUsage() == usage) {
Logging.d(TAG, "Usage is already set to " + usage + ", no update needed");
return true;
return false;
}

// Check if playout was active before reconfiguration
Expand All @@ -610,14 +618,7 @@ public boolean updateAudioTrackUsage(int usage) {
}

// Update audio attributes with new usage
int contentType = AudioAttributes.CONTENT_TYPE_SPEECH;
if (usage == AudioAttributes.USAGE_MEDIA) {
contentType = AudioAttributes.CONTENT_TYPE_MUSIC;
}
audioAttributes = new AudioAttributes.Builder()
.setUsage(usage)
.setContentType(contentType)
.build();
audioAttributes = getAudioAttributes(usage);

// Use cached values from native initPlayout
int sampleRate = cachedSampleRate;
Expand Down Expand Up @@ -647,6 +648,16 @@ public boolean updateAudioTrackUsage(int usage) {
return true;
}

private AudioAttributes getAudioAttributes(int usage) {
int contentType = AudioAttributes.CONTENT_TYPE_SPEECH;
if (usage == AudioAttributes.USAGE_MEDIA) {
contentType = AudioAttributes.CONTENT_TYPE_MUSIC;
}
return new AudioAttributes.Builder()
.setUsage(usage)
.setContentType(contentType)
.build();
}

// Releases the native AudioTrack resources.
private void releaseAudioResources() {
Expand Down