AOSP Expert & Production Engineering
3 min read

Audio Pipeline - Full

The Android Audio Architecture

The Android audio subsystem is a complex, multi-layered architecture designed to handle diverse use cases, from low-latency gaming to background music playback and high-fidelity voice routing.

The Playback Pipeline: AudioTrack to Speaker

When an application plays audio, the standard API used under the hood is AudioTrack.

  1. Application Process: The app writes PCM (Pulse-Code Modulation) audio data into shared memory using AudioTrack.write().
  2. AudioFlinger (System Server): AudioFlinger is the core audio engine in the audioserver process. It constantly reads from the shared memory buffers of all active AudioTrack clients. AudioFlinger uses a MixerThread to combine these disparate audio streams into a single output buffer. It handles sample rate conversion, volume scaling, and software audio effects (like equalization).
  3. Audio HAL: AudioFlinger sends the mixed PCM data to the Audio Hardware Abstraction Layer (HAL).
  4. DSP and Hardware: The HAL routes the data to the audio Digital Signal Processor (DSP) within the SoC. The DSP performs hardware-accelerated processing and sends the signal to the Digital-to-Analog Converter (DAC) and finally to the physical speaker or headphones.
// frameworks/av/services/audioflinger/Threads.cpp
bool AudioFlinger::MixerThread::threadLoop() {
    // ...
    while (!exitPending()) {
        processConfigEvents_l();
        prepareTracks_l(&tracksToRemove);
        mAudioMixer->process();
        threadLoop_write();
        // ...
    }
    return false;
}

The Capture Pipeline: AudioRecord

For recording audio, the pipeline is inverted:

  1. The microphone captures analog sound, the ADC converts it to digital PCM data, and the DSP applies hardware acoustic echo cancellation or noise suppression.
  2. The Audio HAL delivers this data to AudioFlinger.
  3. AudioFlinger's RecordThread reads the data, potentially applies software effects, and writes it to a shared memory buffer.
  4. The application reads from this buffer using the AudioRecord API.

Audio Latency Contributors

Standard audio playback on Android can exhibit 40-100ms of latency. The primary contributors are:

  • Buffer Sizes: Larger buffers prevent audio glitches (underruns) but increase latency.
  • Mixer Thread Overhead: Software mixing adds a processing delay.
  • Hardware/DSP Processing: Hardware effects pipelines introduce mandatory delays.
  • Thread Scheduling: If AudioFlinger or the app thread is preempted by the CPU scheduler, latency increases.

Low Latency Audio: AAudio and Oboe

For latency-sensitive applications like synthesizers or rhythm games, Android introduced AAudio (and the C++ wrapper library Oboe).

AAudio bypasses the standard AudioFlinger MixerThread entirely using the MMAP (Memory-Mapped) path. In exclusive mode, the application writes PCM data directly into a memory buffer that is mapped directly to the Audio DSP. This eliminates the IPC overhead and software mixing stages, reducing latency to single digits (under 10ms on supported hardware).

To dump the current audio state, including active tracks and HAL routing:

adb shell dumpsys media.audio_flinger