Interface AudioTrackBufferSizeProvider


public interface AudioTrackBufferSizeProvider
Provides the buffer size to use when creating an AudioTrack.
  • Field Details

    • ENCODING_INVALID

      static final int ENCODING_INVALID
      An invalid audio encoding.
      See Also:
    • ENCODING_PCM_8BIT

      static final int ENCODING_PCM_8BIT
      8-bit PCM audio.
      See Also:
    • ENCODING_PCM_16BIT

      static final int ENCODING_PCM_16BIT
      Little-endian 16-bit PCM audio.
      See Also:
    • ENCODING_PCM_16BIT_BIG_ENDIAN

      static final int ENCODING_PCM_16BIT_BIG_ENDIAN
      Big-endian 16-bit PCM audio.
      See Also:
    • ENCODING_PCM_24BIT

      static final int ENCODING_PCM_24BIT
      Little-endian 24-bit PCM audio.
      See Also:
    • ENCODING_PCM_32BIT

      static final int ENCODING_PCM_32BIT
      Little-endian 32-bit PCM audio.
      See Also:
    • ENCODING_PCM_FLOAT

      static final int ENCODING_PCM_FLOAT
      Floating-point PCM audio.
      See Also:
    • ENCODING_MP3

      static final int ENCODING_MP3
      MPEG-1 or MPEG-2 Audio Layer III audio.
      See Also:
    • ENCODING_AAC_LC

      static final int ENCODING_AAC_LC
      AAC Low Complexity audio.
      See Also:
    • ENCODING_AAC_HE_V1

      static final int ENCODING_AAC_HE_V1
      High-Efficiency AAC v1 audio.
      See Also:
    • ENCODING_AAC_HE_V2

      static final int ENCODING_AAC_HE_V2
      High-Efficiency AAC v2 audio.
      See Also:
    • ENCODING_AAC_XHE

      static final int ENCODING_AAC_XHE
      Extended High-Efficiency AAC audio.
      See Also:
    • ENCODING_AAC_ELD

      static final int ENCODING_AAC_ELD
      AAC Enhanced Low Delay audio.
      See Also:
    • ENCODING_AAC_ER_BSAC

      static final int ENCODING_AAC_ER_BSAC
      AAC Error Resilient Bit-Sliced Arithmetic Coding audio.
      See Also:
    • ENCODING_AC3

      static final int ENCODING_AC3
      Dolby Digital (AC-3) audio.
      See Also:
    • ENCODING_E_AC3

      static final int ENCODING_E_AC3
      Dolby Digital Plus (E-AC-3) audio.
      See Also:
    • ENCODING_E_AC3_JOC

      static final int ENCODING_E_AC3_JOC
      Dolby Digital Plus with Joint Object Coding audio.
      See Also:
    • ENCODING_AC4

      static final int ENCODING_AC4
      AC-4 audio.
      See Also:
    • ENCODING_DTS

      static final int ENCODING_DTS
      DTS audio.
      See Also:
    • ENCODING_DTS_HD

      static final int ENCODING_DTS_HD
      DTS-HD audio.
      See Also:
    • ENCODING_DOLBY_TRUEHD

      static final int ENCODING_DOLBY_TRUEHD
      Dolby TrueHD audio.
      See Also:
    • ENCODING_OPUS

      static final int ENCODING_OPUS
      Opus audio.
      See Also:
    • ENCODING_DTS_UHD_P2

      static final int ENCODING_DTS_UHD_P2
      DTS-UHD Profile 2 audio.
      See Also:
    • OUTPUT_MODE_PCM

      static final int OUTPUT_MODE_PCM
      The audio sink plays PCM audio.
      See Also:
    • OUTPUT_MODE_OFFLOAD

      static final int OUTPUT_MODE_OFFLOAD
      The audio sink plays encoded audio using offload.
      See Also:
    • OUTPUT_MODE_PASSTHROUGH

      static final int OUTPUT_MODE_PASSTHROUGH
      The audio sink plays encoded audio using passthrough.
      See Also:
  • Method Details

    • getBufferSizeInBytes

      int getBufferSizeInBytes(int minBufferSizeInBytes, @com.castlabs.android.player.AudioTrackBufferSizeProvider.Encoding int encoding, @com.castlabs.android.player.AudioTrackBufferSizeProvider.OutputMode int outputMode, int pcmFrameSize, int sampleRate, int bitrate, double maxAudioTrackPlaybackSpeed)
      Returns the buffer size to use for a specific format and output mode.

      To convert a target buffer duration from milliseconds to bytes, use the PCM byte rate for PCM output:

      
       long bufferSizeInBytes =
           targetBufferDurationMs * sampleRate * pcmFrameSize / 1_000L;
       

      For encoded output with a known bitrate, convert bits per second to bytes per second first:

      
       long bytesPerSecond = (bitrate + 7L) / 8L;
       long bufferSizeInBytes =
           targetBufferDurationMs * bytesPerSecond / 1_000L;
       

      If the encoded bitrate is unknown, the duration cannot be converted exactly; use a conservative codec-specific maximum byte rate or fall back to minBufferSizeInBytes. As in the default player calculation, an implementation can then scale the result by maxAudioTrackPlaybackSpeed, take the maximum of that result and minBufferSizeInBytes, and round up to a multiple of pcmFrameSize:

      
       long speedAdjustedSize = (long) (bufferSizeInBytes * maxAudioTrackPlaybackSpeed);
       long minimumSize = Math.max(minBufferSizeInBytes, speedAdjustedSize);
       long alignedSize =
           (minimumSize + pcmFrameSize - 1) / pcmFrameSize * pcmFrameSize;
       if (alignedSize > Integer.MAX_VALUE) {
           throw new IllegalArgumentException("Audio buffer size is too large");
       }
       return (int) alignedSize;
       
      The calculations use long to avoid overflow during multiplication. The final value is checked and converted to int, as required by the Android AudioTrack APIs.
      Parameters:
      minBufferSizeInBytes - The platform-reported minimum buffer size, in bytes, required to create an AudioTrack for this sample rate, channel configuration, and encoding in streaming mode. This is the value returned by AudioTrack.getMinBufferSize(int, int, int). The returned buffer size must not be smaller than this value.
      encoding - The encoding of the format.
      outputMode - How the audio will be played.
      pcmFrameSize - The PCM frame size in bytes, or 1 for encoded audio.
      sampleRate - The sample rate in Hz.
      bitrate - The compressed stream bitrate, or -1 when unknown or not applicable.
      maxAudioTrackPlaybackSpeed - The maximum playback speed used by the audio track.
      Returns:
      The buffer size in bytes. It must be at least minBufferSizeInBytes and contain an integer number of PCM frames.