Class Decoder

java.lang.Object
android.media.MediaCodec.Callback
com.amazonaws.ivs.player.Decoder

@RequiresApi(api=23) public abstract class Decoder extends android.media.MediaCodec.Callback
Base class for media decoders that handles common functionality. This class provides the foundation for both audio and video decoding, implementing shared functionality like buffer management, state transitions, and MediaCodec interactions.
  • Field Details

    • RETRY_DELAY_MS

      protected static final int RETRY_DELAY_MS
      Constants for buffer management and timing
      See Also:
    • MICROSECONDS_PER_SECOND

      protected static final int MICROSECONDS_PER_SECOND
      See Also:
    • BYTES_PER_SAMPLE

      protected static final int BYTES_PER_SAMPLE
      See Also:
    • awaitingReconfigure

      protected boolean awaitingReconfigure
    • decoder

      protected android.media.MediaCodec decoder
    • mediaCrypto

      protected android.media.MediaCrypto mediaCrypto
    • decoderFormat

      protected android.media.MediaFormat decoderFormat
    • handler

      protected android.os.Handler handler
    • upstream

      protected com.amazonaws.ivs.player.Pullable<com.amazonaws.ivs.player.MediaSample> upstream
    • nextSample

      protected com.amazonaws.ivs.player.MediaSample nextSample
    • currentPlayhead

      protected long currentPlayhead
    • bufferQueue

      protected final ArrayDeque<Integer> bufferQueue
    • expectedTimestamps

      protected final ArrayDeque<Long> expectedTimestamps
    • listener

      protected final com.amazonaws.ivs.player.Decoder.Listener listener
    • formatHash

      protected String formatHash
    • trackId

      protected final com.amazonaws.ivs.player.MediaSample.TrackID trackId
    • state

      protected final com.amazonaws.ivs.player.Fsm<Decoder.InternalState> state
    • stateCallbacks

      protected final Map<Decoder.InternalState,Runnable> stateCallbacks
  • Constructor Details

    • Decoder

      protected Decoder(com.amazonaws.ivs.player.Decoder.Listener listener, com.amazonaws.ivs.player.MediaSample.TrackID trackId, String threadName, int threadPriority)
      Creates a new Decoder with the specified listener and track ID.
      Parameters:
      listener - The listener to notify of decoder events
      trackId - The track ID this decoder is responsible for
      threadName - The name for the decoder thread
      threadPriority - The priority for the decoder thread
    • Decoder

      protected Decoder()
      Default constructor for backward compatibility. Subclasses should prefer the parameterized constructor.
  • Method Details

    • tag

      protected abstract String tag()
      Returns the tag to be used for logging. This allows subclasses to provide their own tag for better log identification.
      Returns:
      The tag string for logging
    • getCodec

      public android.media.MediaCodec getCodec()
      Gets the MediaCodec instance used by this decoder.
      Returns:
      The MediaCodec instance
    • getCurrentState

      protected Decoder.InternalState getCurrentState()
      Gets the current state of the decoder.
      Returns:
      The current internal state
    • awaitState

      protected void awaitState(Decoder.InternalState state)
      Waits for the decoder to reach the specified state. This is useful for synchronizing operations with state transitions.
      Parameters:
      state - The state to wait for
    • changeState

      protected boolean changeState(Decoder.InternalState next)
      Changes the decoder state if the transition is valid.
      Parameters:
      next - The next state to transition to
      Returns:
      true if the state change was successful, false otherwise
    • initialize

      protected void initialize(android.media.MediaFormat format, boolean useSoftwareDecoder)
      Initializes the decoder with the specified format.
      Parameters:
      format - The media format to configure the decoder with
      useSoftwareDecoder - Whether to use a software decoder
    • createCodec

      protected android.media.MediaCodec createCodec(android.media.MediaFormat format, boolean useSoftwareDecoder)
      Creates a codec for the specified format.
      Parameters:
      format - The media format
      useSoftwareDecoder - Whether to use a software decoder
      Returns:
      The created MediaCodec instance
    • configure

      protected abstract void configure()
      Configures the decoder with the current format. Subclasses should override this to provide specific configuration.
    • play

      public void play()
      Starts playback.
    • pause

      public void pause()
      Pauses playback.
    • stop

      public void stop()
      Stops playback.
    • flush

      public void flush()
      Flushes the decoder.
    • reset

      public void reset()
      Resets the decoder.
    • endOfStream

      public void endOfStream()
      Signals end of stream.
    • seekTo

      public void seekTo(long timestampUs)
      Seeks to the specified timestamp.
      Parameters:
      timestampUs - The timestamp to seek to in microseconds
    • clearBufferQueue

      protected void clearBufferQueue()
      Clears all buffer queues.
    • isExpected

      protected boolean isExpected(long pts)
    • setExpected

      protected void setExpected(long pts)
    • removeExpected

      protected void removeExpected(long pts)
    • shouldReconfigureForFormat

      protected boolean shouldReconfigureForFormat(String currentMime, String nextMime)
      Determines if the decoder should be reconfigured based on format changes. Subclasses can override this to provide specific reconfiguration logic.
      Parameters:
      currentMime - The current MIME type
      nextMime - The new MIME type
      Returns:
      true if the decoder should be reconfigured, false otherwise
    • handleReconfiguration

      protected void handleReconfiguration()
      Handles reconfiguration of the decoder when needed. This method should be called when an end-of-stream is detected and awaitingReconfigure is true.
    • maybeSubmitBuffers

      protected void maybeSubmitBuffers()
      Submits buffers to the decoder if available. This method is called when new input buffers become available or when the decoder state changes to RUNNING.
    • onInputBufferAvailable

      public void onInputBufferAvailable(@NonNull android.media.MediaCodec codec, int index)
      Specified by:
      onInputBufferAvailable in class android.media.MediaCodec.Callback
    • onError

      public void onError(@NonNull android.media.MediaCodec codec, @NonNull android.media.MediaCodec.CodecException e)
      Specified by:
      onError in class android.media.MediaCodec.Callback
    • onOutputFormatChanged

      public void onOutputFormatChanged(@NonNull android.media.MediaCodec codec, @NonNull android.media.MediaFormat format)
      Specified by:
      onOutputFormatChanged in class android.media.MediaCodec.Callback
    • hashMediaFormat

      protected String hashMediaFormat(android.media.MediaFormat format)
      Creates a hash of the media format for comparison. This is used to detect format changes.
      Parameters:
      format - The media format to hash
      Returns:
      A string hash of the format