Overview
TheAudioDecoder 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.stop()
Stops the decoder, AudioTrack, and releases resources.Configuration
configure()
Configures the MediaCodec decoder with AAC codec-specific data.byte[]
required
Codec-specific data (csd-0) containing AAC configuration
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
AudioTrack Integration
The decoder automatically manages an AudioTrack instance for audio playback.AudioTrack Configuration
Audio Playback Loop
AudioPacket Structure
Audio data is transmitted using theAudioPacket structure.
Packet Format
Header (10 bytes):Flag Types
byte
Value:
0 - Regular audio framebyte
Value:
1 - Key audio framebyte
Value:
2 - Configuration data (codec-specific)byte
Value:
4 - End of audio streamReading 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 profileint
Value:
48000 - 48 kHz sampling rateint
Value:
2 - Stereo outputint
Value:
128000 - 128 kbps encoding bitrateint
Value:
AudioFormat.ENCODING_PCM_16BIT - 16-bit PCM outputPerformance Considerations
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_STREAMfor 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
- Video Decoder - Video stream decoding
- Scrcpy Service - Main service integration
- Event Controller - Input event handling
