AudioProcessingPipeline


@UnstableApi
public final class AudioProcessingPipeline


Handles passing buffers through multiple AudioProcessor instances.

Two instances of AudioProcessingPipeline are considered equal if they have the same underlying AudioProcessor references, in the same order.

To make use of this class, the caller must:

  • Initialize an instance, passing in all audio processors that may be used for processing.
  • Call configure with the AudioFormat of the input data. This method will give back the AudioFormat that will be output from the pipeline when this configuration is in use.
  • Call flush to apply the pending configuration.
  • Check if the pipeline isOperational. If not, then the pipeline can not be used to process buffers in the current configuration. This is because none of the underlying AudioProcessor instances are active.
  • If the pipeline isOperational, queueInput then getOutput to process buffers.
  • queueEndOfStream to inform the pipeline the current input stream is at an end.
  • Repeatedly call getOutput and handle those buffers until isEnded returns true.
  • When finished with the pipeline, call reset to release underlying resources.

If underlying AudioProcessor instances have pending configuration changes, or the AudioFormat of the input is changing:

Summary

Public fields

AudioProcessor.AudioFormat

The AudioFormat currently being output by the pipeline.

Public constructors

Creates an instance.

Public methods

AudioProcessor.AudioFormat

Configures the pipeline to process input audio with the specified format.

boolean

Indicates whether some other object is "equal to" this one.

void

Clears any buffered data and pending output.

ByteBuffer

Returns a ByteBuffer containing processed output data between its position and limit.

AudioProcessor.AudioFormat

Returns the AudioFormat of data being output through getOutput.

int
boolean

Returns whether the pipeline has ended.

boolean

Whether the pipeline can be used for processing buffers.

void

Queues an end of stream signal.

void
queueInput(ByteBuffer inputBuffer)

Queues audio data between the position and limit of the inputBuffer for processing.

void

Resets the pipeline and its underlying AudioProcessor instances to their unconfigured state, releasing any resources.

Public fields

outputAudioFormat

public AudioProcessor.AudioFormat outputAudioFormat

The AudioFormat currently being output by the pipeline.

Public constructors

AudioProcessingPipeline

public AudioProcessingPipeline(ImmutableList<AudioProcessor> audioProcessors)

Creates an instance.

Parameters
ImmutableList<AudioProcessor> audioProcessors

The AudioProcessor instances to be used for processing buffers.

Public methods

configure

@CanIgnoreReturnValue
public AudioProcessor.AudioFormat configure(AudioProcessor.AudioFormat inputAudioFormat)

Configures the pipeline to process input audio with the specified format. Returns the configured output audio format.

To apply the new configuration for use, the pipeline must be flushed. Before applying the new configuration, it is safe to queue input and get output in the old input/output formats/configuration. Call queueEndOfStream when no more input will be supplied for processing in the old configuration.

Parameters
AudioProcessor.AudioFormat inputAudioFormat

The format of audio that will be queued after the next call to flush.

Returns
AudioProcessor.AudioFormat

The configured output audio format.

Throws
androidx.media3.common.audio.AudioProcessor.UnhandledAudioFormatException

If the specified format is not supported by the pipeline.

equals

public boolean equals(@Nullable Object o)

Indicates whether some other object is "equal to" this one.

Two instances of AudioProcessingPipeline are considered equal if they have the same underlying AudioProcessor references in the same order.

flush

public void flush()

Clears any buffered data and pending output. If any underlying audio processors are active, this also prepares them to receive a new stream of input in the last configured (pending) format.

configure must have been called at least once since the last call to reset before calling this.

getOutput

public ByteBuffer getOutput()

Returns a ByteBuffer containing processed output data between its position and limit. The buffer will be empty if no output is available.

Buffers returned from this method are retained by pipeline, and it is necessary to consume the data (or copy it into another buffer) to allow the pipeline to progress.

Returns
ByteBuffer

A buffer containing processed output data between its position and limit.

getOutputAudioFormat

public AudioProcessor.AudioFormat getOutputAudioFormat()

Returns the AudioFormat of data being output through getOutput.

Returns
AudioProcessor.AudioFormat

The AudioFormat currently being output, or NOT_SET if no configuration has been applied.

hashCode

public int hashCode()

isEnded

public boolean isEnded()

Returns whether the pipeline has ended.

The pipeline is considered ended when:

isOperational

public boolean isOperational()

Whether the pipeline can be used for processing buffers.

For this to happen the pipeline must be configured, flushed and have activeunderlying audio processors that are ready to process buffers with the current configuration.

queueEndOfStream

public void queueEndOfStream()

Queues an end of stream signal. After this method has been called, queueInput should not be called until after the next call to flush. Calling getOutput will return any remaining output data. Multiple calls may be required to read all of the remaining output data. isEnded will return true once all remaining output data has been read.

queueInput

public void queueInput(ByteBuffer inputBuffer)

Queues audio data between the position and limit of the inputBuffer for processing. After calling this method, processed output may be available via getOutput.

Parameters
ByteBuffer inputBuffer

The input buffer to process. It must be a direct ByteBuffer with native byte order. Its contents are treated as read-only. Its position will be advanced by the number of bytes consumed (which may be zero). The caller retains ownership of the provided buffer.

reset

public void reset()

Resets the pipeline and its underlying AudioProcessor instances to their unconfigured state, releasing any resources.