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 enumEnum representing the internal states of the decoder. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected final ArrayDeque<Integer> protected static final intprotected longprotected android.media.MediaCodecprotected android.media.MediaFormatprotected final ArrayDeque<Long> protected Stringprotected android.os.Handlerprotected final com.amazonaws.ivs.player.Decoder.Listenerprotected android.media.MediaCryptoprotected static final intprotected com.amazonaws.ivs.player.MediaSampleprotected static final intConstants 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.TrackIDprotected com.amazonaws.ivs.player.Pullable<com.amazonaws.ivs.player.MediaSample> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDecoder()Default constructor for backward compatibility.protectedDecoder(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 voidawaitState(Decoder.InternalState state) Waits for the decoder to reach the specified state.protected booleanChanges the decoder state if the transition is valid.protected voidClears all buffer queues.protected abstract voidConfigures the decoder with the current format.protected android.media.MediaCodeccreateCodec(android.media.MediaFormat format, boolean useSoftwareDecoder) Creates a codec for the specified format.voidSignals end of stream.voidflush()Flushes the decoder.android.media.MediaCodecgetCodec()Gets the MediaCodec instance used by this decoder.protected Decoder.InternalStateGets the current state of the decoder.protected voidHandles reconfiguration of the decoder when needed.protected StringhashMediaFormat(android.media.MediaFormat format) Creates a hash of the media format for comparison.protected voidinitialize(android.media.MediaFormat format, boolean useSoftwareDecoder) Initializes the decoder with the specified format.protected booleanisExpected(long pts) protected voidSubmits buffers to the decoder if available.voidonError(android.media.MediaCodec codec, android.media.MediaCodec.CodecException e) voidonInputBufferAvailable(android.media.MediaCodec codec, int index) voidonOutputFormatChanged(android.media.MediaCodec codec, android.media.MediaFormat format) voidpause()Pauses playback.voidplay()Starts playback.protected voidremoveExpected(long pts) voidreset()Resets the decoder.voidseekTo(long timestampUs) Seeks to the specified timestamp.protected voidsetExpected(long pts) protected booleanshouldReconfigureForFormat(String currentMime, String nextMime) Determines if the decoder should be reconfigured based on format changes.voidstop()Stops playback.protected abstract Stringtag()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:
onInputBufferAvailablein classandroid.media.MediaCodec.Callback
-
onError
public void onError(@NonNull android.media.MediaCodec codec, @NonNull android.media.MediaCodec.CodecException e) - Specified by:
onErrorin classandroid.media.MediaCodec.Callback
-
onOutputFormatChanged
public void onOutputFormatChanged(@NonNull android.media.MediaCodec codec, @NonNull android.media.MediaFormat format) - Specified by:
onOutputFormatChangedin 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
-