Encoder

@RequiresApi(value = 21) interface Encoder

Known direct subclasses
EncoderImpl

The encoder implementation.


The encoder interface.

An encoder could be either a video encoder or an audio encoder. The interface defines the common APIs to communicate with an encoder.

Summary

Nested types

Encoder.ByteBufferInput

A ByteBufferInput is a BufferProvider implementation and provides InputBuffer to write input data to the encoder.

Encoder.EncoderInput

The encoder's input.

Encoder.SurfaceInput

A SurfaceInput provides a Surface as the interface to receive video raw data.

Encoder.SurfaceInput.OnSurfaceUpdateListener

An interface for receiving the update event of the input Surface of the encoder.

Public methods

abstract @NonNull Encoder.EncoderInput

Returns the encoder's input instance.

abstract @NonNull void

Pauses the encoder.

abstract @NonNull void

Releases the encoder.

abstract @NonNull void

Request a key frame.

abstract @NonNull void
setEncoderCallback(
    @NonNull EncoderCallback encoderCallback,
    @NonNull Executor executor
)

Sets callback to encoder.

abstract @NonNull void

Starts the encoder.

abstract @NonNull void

Stops the encoder.

Public methods

getInput

@NonNull
abstract Encoder.EncoderInput getInput()

Returns the encoder's input instance.

pause

@NonNull
abstract void pause()

Pauses the encoder.

pause only work between start and stop. Once the encoder is paused, it will drop the input data until start is invoked again.

release

@NonNull
abstract void release()

Releases the encoder.

Once the encoder is released, it cannot be used anymore. Any other method call after the encoder is released will get IllegalStateException.

If this encoder takes SurfaceInput, this method will release all the Surfaces updated via setOnSurfaceUpdateListener. So this method should only be called when the frame producer is finished with the surface which may be the current surface or one of the obsolete surfaces.

requestKeyFrame

@NonNull
abstract void requestKeyFrame()

Request a key frame.

Only take effect when the encoder is a video encoder and encoder is started.

setEncoderCallback

@NonNull
abstract void setEncoderCallback(
    @NonNull EncoderCallback encoderCallback,
    @NonNull Executor executor
)

Sets callback to encoder.

Parameters
@NonNull EncoderCallback encoderCallback

the encoder callback

@NonNull Executor executor

the callback executor

start

@NonNull
abstract void start()

Starts the encoder.

If the encoder is not started yet, it will first trigger onEncodeStart. Then continually invoke the onEncodedData callback until the encoder is paused, stopped or released. It can call pause to pause the encoding after started. If the encoder is in paused state, then calling this method will resume the encoding.

stop

@NonNull
abstract void stop()

Stops the encoder.

It will trigger onEncodeStop after the last encoded data. It can call start to start again.