Skip to main content

Overview

The AudioDecoder class provides hardware-accelerated AAC audio decoding using Android’s MediaCodec API and plays the decoded PCM audio through AudioTrack. It runs in a separate thread for continuous audio playback. Package: org.client.scrcpy.decoder Audio Codec: audio/mp4a-latm (AAC-LC) Sample Rate: 48000 Hz Channels: 2 (Stereo) Encoding: PCM 16-bit

Initialization

start()

Starts the decoder worker thread.
Example:

stop()

Stops the decoder, AudioTrack, and releases resources.
Example:

Configuration

configure()

Configures the MediaCodec decoder with AAC codec-specific data.
byte[]
required
Codec-specific data (csd-0) containing AAC configuration
Example:
Internal Implementation:
Calling configure() while already configured will stop the current decoder and AudioTrack, then create new instances. This allows dynamic reconfiguration.

Decoding

decodeSample()

Queues an encoded audio sample for decoding and playback.
byte[]
required
Byte array containing the encoded audio frame
int
required
Starting position in the data array
int
required
Number of bytes to decode
long
required
Presentation timestamp in microseconds
int
required
MediaCodec flags
Example:
Frame Processing:

AudioTrack Integration

The decoder automatically manages an AudioTrack instance for audio playback.

AudioTrack Configuration

Audio Playback Loop

AudioPacket Structure

Audio data is transmitted using the AudioPacket structure.

Packet Format

Header (10 bytes):
Total packet structure:

Flag Types

byte
Value: 0 - Regular audio frame
byte
Value: 1 - Key audio frame
byte
Value: 2 - Configuration data (codec-specific)
byte
Value: 4 - End of audio stream

Reading Packets

Usage Example

Complete example of setting up and using the audio decoder:

Threading Model

The AudioDecoder uses an internal Worker thread:

Audio Specifications

String
Value: "audio/mp4a-latm" - AAC Low Complexity profile
int
Value: 48000 - 48 kHz sampling rate
int
Value: 2 - Stereo output
int
Value: 128000 - 128 kbps encoding bitrate
int
Value: AudioFormat.ENCODING_PCM_16BIT - 16-bit PCM output

Performance Considerations

The decoder uses hardware acceleration and real-time audio playback. Ensure that:
  • The device supports AAC hardware decoding
  • Audio focus is properly managed
  • The AudioTrack stream type is appropriate for your use case
  • Frame processing keeps up with the sample rate to avoid buffer underruns
Key Points:
  • Decoded PCM data is written directly to AudioTrack
  • Input buffer dequeue timeout is set to -1 (wait indefinitely)
  • Output buffer dequeue timeout is 0 (non-blocking)
  • AudioTrack uses MODE_STREAM for continuous playback
  • Minimum buffer size is calculated automatically

Common Issues

Buffer Underruns

If you experience audio stuttering:

Audio Focus

Manage audio focus properly:

See Also