Skip to content

Clink: anchor audio PTS to the transport clock, not the video offset - #30

Merged
riko2chen merged 2 commits into
OpenXPlay:mainfrom
hellowalker:audio-pts-anchor-transport-clock
Nov 5, 2026
Merged

riko2chen merged 2 commits into
OpenXPlay:mainfrom
hellowalker:audio-pts-anchor-transport-clock

Conversation

@hellowalker

Copy link
Copy Markdown
Contributor

PR 1(Stream):CarPlay 音频 PTS 与传输时钟解耦

Branch: audio-pts-anchor-transport-clock
Title: Clink: anchor audio PTS to the transport clock, not the video offset
Base: main
Files: kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/ClinkOutput.kt


What was broken

CarPlay audio relayed through clink reaches the head unit at 31 packets/s while the stream
itself carries 46.9/s (AAC-LC 48 kHz, 1024 samples per packet). The ear hears constant stutter.

Measured with three counters along the path:

Stage Counter Value
Arrival at relay rtp_in 54/s (phone pre-buffers)
Handed to clink worker_out 54/s, queued=0
Emitted to head unit audioFrames 31/s ← loss is inside clink

Timestamps, sampled every 2 s:

v=65986061  a=66032833  t=66032833     audio behind transport:   0 ms
v=68083273  a=67954948  t=68083273                            -128 ms
v=70066606  a=69900951  t=70066606                            -165 ms
v=72066606  a=71788527  t=72066606                            -278 ms

a (last emitted audio PTS) falls monotonically behind t (transport clock).

Why

Three defects in media(), all in the audio path.

1. audioOffset is never initialised when video goes live first

if (!gate.live && candidate != null && audioOffset == null) audioOffset = maxOf(0L, outputPts) - candidate.ptsUs
val offset = if (gate.live) liveOffset else audioOffset ?: 0L

The offset is only computed while !gate.live. In practice video goes live first, so
audioOffset stays null and audio borrows the video's liveOffset.

The two clocks have different origins — audio PTS is stamped from System.nanoTime() in
AudioClock, video PTS comes from the encoder — so mapped ends up 58~238 ms behind
transportPts, outside the 50 ms admission gate, and the packet is dropped silently:

if (mapped >= 0 && mapped >= transportPts - 50_000) { ...emit... }

2. Audio PTS is flattened onto the transport clock

val pts = maxOf(mapped, transportPts)

transportPts is advanced by whoever emits, and video frames push it forward. When mapped
lags, consecutive audio packets all collapse onto the same transportPts value — the PTS stops
advancing
, which the receiver decodes as "time is not moving".

Measured: 288 of 468 packets per 10 s window had step <= 0.

3. The admission gate still compares against transportPts

After fixing 1 and 2, audio still sat ~103 ms behind (video keyframes push the clock forward),
against a 200 ms gate — half the packets were still rejected:

n=236~267 (should be 468), step avg=41491 (≈2× the 21333 ideal)

What this PR changes

// 1. anchor to the transport clock, unconditionally (anchor > 0 means a clock exists)
val anchor = maxOf(transportPts, outputPts)
if (candidate != null && audioOffset == null && anchor > 0) {
    audioOffset = anchor - candidate.ptsUs
}

// 2. audio PTS advances on its own cadence; monotonic per stream, not per session
val pts = if (lastAudioPts > 0) maxOf(mapped, lastAudioPts + 1) else mapped

// 3. audio no longer pushes the shared transport clock
- transportPts = pts

// 4. the gate only rejects a negative PTS
- if (mapped >= 0 && mapped >= transportPts - 50_000) {
+ if (mapped >= 0) {

Plus: audioOffset and lastAudioPts move from media() locals to ClinkOutput fields, and are
cleared in onAudioFormat (stream changed) and onSession (session reset). Without this, a
rebuilt audio stream gets a fresh AudioClock anchor while the old offset survives, leaving
mapped 53 s away from transportPts.

Measurements after

Metric Before After Reference
audioFrames 31/s 46.8/s theoretical 46.9
step (PTS delta, µs) 0 / 21404 / 352952 21333 / 21333 / 21334 constant = no jitter
back (PTS backwards) 288 / 468 0 0 = no audible glitch
jump (gap > 64 ms) 26 / 10 s 0 0 = no dropout
queued 71 (backlog) 0 no queue pressure

Raw window after the change:

02:00:45  n=468  step=21333/21333/21334  back=0  jump=0  skew=-266077
02:00:55  n=468  step=21333/21333/21334  back=0  jump=0  skew=-258466
02:01:05  n=468  step=21333/21333/21334  back=0  jump=0  skew=-259045

step being constant at 21333 µs is the decisive evidence: every packet is exactly
1024/48000 s apart.

Scope and safety

  • Video path untouched: transportPts is still advanced by video frames (2 call sites), and
    video PTS still uses maxOf(outputPts + 1, transportPts + 1, f.ptsUs + liveOffset).
  • Audio remains monotonic per stream (maxOf(mapped, lastAudioPts + 1)), which is what a muxer
    requires; it is no longer forced to stay above the video-driven clock.
  • Reset points are the same places that already clear run.audio, so no new lifecycle is
    introduced.

Open question for the maintainer

Is the shared transportPts intentional?

If audio and video are meant to advance one common clock (to keep A/V in sync), then change 3
(dropping transportPts = pts) is the wrong fix — the right one would be keeping the shared
clock and relaxing only the audio admission rule. My reading is that transportPts exists to keep
mux calls ordered, not to couple the two streams' rates.

Residual: skew (audio PTS minus transport clock) now sits at -400 ms ~ +800 ms instead of
growing without bound. No audible sync problem was reported, but it is not perfectly zero.

Environment

MI 8 SE (sirius), MIUI 12.5 / Android 10 / API 29 → XPeng G9 (CMCC_XP_CAR).
Wi-Fi 866.7 Mbps (VHT-MCS9 / 80 MHz / 2x2), so bandwidth is ruled out.
Relay side needs the companion change in OpenXPlay-Relay (audio stream rebuild); see the
relay PR for the stream.lock bump.

CarPlay audio reached the head unit at 31 packets/s against a 46.9/s
stream. Three counters along the path put the loss inside clink: arrival
54/s, worker 54/s with queued=0, emitted 31/s.

audioOffset was only computed while !gate.live. Video goes live first in
practice, so it stayed null and audio borrowed the video's liveOffset.
The two clocks have different origins (audio is stamped from nanoTime,
video comes from the encoder), leaving mapped 58~238 ms behind
transportPts - outside the 50 ms gate, dropped silently.

maxOf(mapped, transportPts) then flattened audio PTS onto the video
clock: 288 of 468 packets per window had step <= 0, which the receiver
decodes as time not advancing. And the gate still compared against
transportPts, so even after both fixes half the packets were rejected
(236~267/s, step avg 41491 = 2x the 21333 ideal).

Anchor to the transport clock unconditionally, let audio PTS advance on
its own cadence, stop pushing the shared clock from audio, and reject
only a negative PTS. The anchors move to ClinkOutput fields and are
cleared when the media stream changes or the session resets - otherwise a
rebuilt stream keeps the old offset (measured: mapped 53 s off).

After: 46.8 packets/s, step constant at 21333/21333/21334, back=0,
jump=0, queued=0.

Measured on MI 8 SE (Android 10) with an XPeng G9, Wi-Fi 866 Mbps.
@riko2chen
riko2chen enabled auto-merge November 5, 2026 06:01
@riko2chen
riko2chen merged commit 2cd30b6 into OpenXPlay:main Nov 5, 2026
6 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants