Skip to content

Commit

Permalink
Revert "[android] Remove AudioTrack creation loop" (youtube#1830)
Browse files Browse the repository at this point in the history
Reverts youtube#1770

b/175822425
  • Loading branch information
jasonzhangxx authored Oct 23, 2023
1 parent 301a02e commit 7b71cf4
Showing 1 changed file with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,33 +135,42 @@ public AudioTrackBridge(
.setChannelMask(channelConfig)
.build();

try {
audioTrack =
new AudioTrack(
attributes,
format,
preferredBufferSizeInBytes,
AudioTrack.MODE_STREAM,
tunnelModeEnabled
? tunnelModeAudioSessionId
: AudioManager.AUDIO_SESSION_ID_GENERATE);
} catch (Exception e) {
audioTrack = null;
Log.i(TAG, "Failed to create AudioTrack: " + e.getMessage());
}
// AudioTrack ctor can fail in multiple, platform specific ways, so do a thorough check
// before proceed.
if (audioTrack != null && audioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
audioTrack = null;
int audioTrackBufferSize = preferredBufferSizeInBytes;
// TODO: Investigate if this implementation could be refined.
// It is not necessary to loop until 0 since there is new implementation based on
// AudioTrack.getMinBufferSize(). Especially for tunnel mode, it would fail if audio HAL does
// not support tunnel mode and then it is not helpful to retry.
while (audioTrackBufferSize > 0) {
try {
audioTrack =
new AudioTrack(
attributes,
format,
audioTrackBufferSize,
AudioTrack.MODE_STREAM,
tunnelModeEnabled
? tunnelModeAudioSessionId
: AudioManager.AUDIO_SESSION_ID_GENERATE);
} catch (Exception e) {
audioTrack = null;
}
// AudioTrack ctor can fail in multiple, platform specific ways, so do a thorough check
// before proceed.
if (audioTrack != null) {
if (audioTrack.getState() == AudioTrack.STATE_INITIALIZED) {
break;
}
audioTrack = null;
}
audioTrackBufferSize /= 2;
}

Log.i(
TAG,
"AudioTrack created with preferred size %d (in bytes). The minimum buffer size is %d. The"
+ " buffer size is %d (in frames).",
"AudioTrack created with buffer size %d (preferred: %d). The minimum buffer size is"
+ " %d.",
audioTrackBufferSize,
preferredBufferSizeInBytes,
AudioTrack.getMinBufferSize(sampleRate, channelConfig, sampleType),
audioTrack.getBufferSizeInFrames());
AudioTrack.getMinBufferSize(sampleRate, channelConfig, sampleType));
}

public Boolean isAudioTrackValid() {
Expand Down

0 comments on commit 7b71cf4

Please sign in to comment.