Skip to content

android: deliver AirPods mic frames in arrival order - #957

Open
oenderg wants to merge 1 commit into
librepods-org:android/rewritefrom
oenderg:fix/android-mic-frame-order
Open

oenderg wants to merge 1 commit into
librepods-org:android/rewritefrom
oenderg:fix/android-mic-frame-order

Conversation

@oenderg

@oenderg oenderg commented Dec 2, 2026 •

Copy link
Copy Markdown

The AACP 0x58 handler launches one coroutine per microphone frame:

MicrophoneFrame.parsePacket(packet).forEach { frame ->
    CoroutineScope(Dispatchers.IO).launch {
        device.emitMicrophoneFrame(frame)
    }
}

Dispatchers.IO runs these in parallel, so frames from the same packet (and from back-to-back packets, which often arrive within 1 ms of each other) can overtake each other before they reach microphoneFrames. AAC-ELD overlaps neighbouring frames, so every swap garbles the decoded audio around it. It sounds robotic or crackly.

Measured on AirPods Pro 3 by checking that each frame's timestamp is larger than the previous one (timestamps step by 180 per frame): 21 and 37 of every 400 frames arrived out of order. With this change: 0 of 400 over several streams, and the audio sounds clean.

The fix emits synchronously on the AACP reader thread with tryEmit, so frames keep their arrival order. The flow already has extraBufferCapacity = 64 and collectors only need to hand frames off, so tryEmit shouldn't drop anything in practice; a failed tryEmit is logged.

Tested on Android 17 with MediaCodec decoding (AAC-ELD, 480 samples, 64 kHz).

Each 0x58 frame was emitted from its own Dispatchers.IO coroutine, so frames overtook each other (21-37 of every 400 out of order on a Pro 3). AAC-ELD overlaps neighbouring frames, so every swap garbled the audio. Emit synchronously with tryEmit on the AACP reader thread instead (0 of 400).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant