From 7b71cf454a52cf4f0d7e401395628f386c876953 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 23 Oct 2023 15:44:07 -0700 Subject: [PATCH] Revert "[android] Remove AudioTrack creation loop" (#1830) Reverts youtube/cobalt#1770 b/175822425 --- .../dev/cobalt/media/AudioTrackBridge.java | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/starboard/android/apk/app/src/main/java/dev/cobalt/media/AudioTrackBridge.java b/starboard/android/apk/app/src/main/java/dev/cobalt/media/AudioTrackBridge.java index c7f6c2d3faed..8d2d7f808a48 100644 --- a/starboard/android/apk/app/src/main/java/dev/cobalt/media/AudioTrackBridge.java +++ b/starboard/android/apk/app/src/main/java/dev/cobalt/media/AudioTrackBridge.java @@ -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() {