Package com.amazonaws.ivs.player
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static enum
Enum representing the internal states of the decoder. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
protected final ArrayDeque
<Integer> protected static final int
protected long
protected android.media.MediaCodec
protected android.media.MediaFormat
protected final ArrayDeque
<Long> protected String
protected android.os.Handler
protected final com.amazonaws.ivs.player.Decoder.Listener
protected android.media.MediaCrypto
protected static final int
protected com.amazonaws.ivs.player.MediaSample
protected static final int
Constants for buffer management and timingprotected final com.amazonaws.ivs.player.Fsm
<Decoder.InternalState> protected final Map
<Decoder.InternalState, Runnable> protected final com.amazonaws.ivs.player.MediaSample.TrackID
protected com.amazonaws.ivs.player.Pullable
<com.amazonaws.ivs.player.MediaSample> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Decoder()
Default constructor for backward compatibility.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. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
awaitState
(Decoder.InternalState state) Waits for the decoder to reach the specified state.protected boolean
Changes the decoder state if the transition is valid.protected void
Clears all buffer queues.protected abstract void
Configures the decoder with the current format.protected android.media.MediaCodec
createCodec
(android.media.MediaFormat format, boolean useSoftwareDecoder) Creates a codec for the specified format.void
Signals end of stream.void
flush()
Flushes the decoder.android.media.MediaCodec
getCodec()
Gets the MediaCodec instance used by this decoder.protected Decoder.InternalState
Gets the current state of the decoder.protected void
Handles reconfiguration of the decoder when needed.protected String
hashMediaFormat
(android.media.MediaFormat format) Creates a hash of the media format for comparison.protected void
initialize
(android.media.MediaFormat format, boolean useSoftwareDecoder) Initializes the decoder with the specified format.protected boolean
isExpected
(long pts) protected void
Submits buffers to the decoder if available.void
onError
(android.media.MediaCodec codec, android.media.MediaCodec.CodecException e) void
onInputBufferAvailable
(android.media.MediaCodec codec, int index) void
onOutputFormatChanged
(android.media.MediaCodec codec, android.media.MediaFormat format) void
pause()
Pauses playback.void
play()
Starts playback.protected void
removeExpected
(long pts) void
reset()
Resets the decoder.void
seekTo
(long timestampUs) Seeks to the specified timestamp.protected void
setExpected
(long pts) protected boolean
shouldReconfigureForFormat
(String currentMime, String nextMime) Determines if the decoder should be reconfigured based on format changes.void
stop()
Stops playback.protected abstract String
tag()
Returns the tag to be used for logging.Methods inherited from class android.media.MediaCodec.Callback
onCryptoError, onOutputBufferAvailable, onOutputBuffersAvailable
-
Field Details
-
RETRY_DELAY_MS
protected static final int RETRY_DELAY_MSConstants 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
-
expectedTimestamps
-
listener
protected final com.amazonaws.ivs.player.Decoder.Listener listener -
formatHash
-
trackId
protected final com.amazonaws.ivs.player.MediaSample.TrackID trackId -
state
-
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 eventstrackId
- The track ID this decoder is responsible forthreadName
- The name for the decoder threadthreadPriority
- The priority for the decoder thread
-
Decoder
protected Decoder()Default constructor for backward compatibility. Subclasses should prefer the parameterized constructor.
-
-
Method Details
-
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
Gets the current state of the decoder.- Returns:
- The current internal state
-
awaitState
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
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 withuseSoftwareDecoder
- 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 formatuseSoftwareDecoder
- 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
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 typenextMime
- 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 classandroid.media.MediaCodec.Callback
-
onError
public void onError(@NonNull android.media.MediaCodec codec, @NonNull android.media.MediaCodec.CodecException e) - Specified by:
onError
in classandroid.media.MediaCodec.Callback
-
onOutputFormatChanged
public void onOutputFormatChanged(@NonNull android.media.MediaCodec codec, @NonNull android.media.MediaFormat format) - Specified by:
onOutputFormatChanged
in classandroid.media.MediaCodec.Callback
-
hashMediaFormat
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
-