Repository navigation
Clink: anchor audio PTS to the transport clock, not the video offset - #30
Merged
riko2chen merged 2 commits intoNov 5, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR 1(Stream):CarPlay 音频 PTS 与传输时钟解耦
Branch:
audio-pts-anchor-transport-clockTitle:
Clink: anchor audio PTS to the transport clock, not the video offsetBase:
mainFiles:
kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/ClinkOutput.ktWhat 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:
rtp_inworker_outqueued=0audioFramesTimestamps, sampled every 2 s:
a(last emitted audio PTS) falls monotonically behindt(transport clock).Why
Three defects in
media(), all in the audio path.1.
audioOffsetis never initialised when video goes live firstThe offset is only computed while
!gate.live. In practice video goes live first, soaudioOffsetstaysnulland audio borrows the video'sliveOffset.The two clocks have different origins — audio PTS is stamped from
System.nanoTime()inAudioClock, video PTS comes from the encoder — somappedends up 58~238 ms behindtransportPts, outside the 50 ms admission gate, and the packet is dropped silently:2. Audio PTS is flattened onto the transport clock
transportPtsis advanced by whoever emits, and video frames push it forward. Whenmappedlags, consecutive audio packets all collapse onto the same
transportPtsvalue — the PTS stopsadvancing, 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
transportPtsAfter 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:
What this PR changes
Plus:
audioOffsetandlastAudioPtsmove frommedia()locals toClinkOutputfields, and arecleared in
onAudioFormat(stream changed) andonSession(session reset). Without this, arebuilt audio stream gets a fresh
AudioClockanchor while the old offset survives, leavingmapped53 s away fromtransportPts.Measurements after
audioFramesstep(PTS delta, µs)back(PTS backwards)jump(gap > 64 ms)queuedRaw window after the change:
stepbeing constant at 21333 µs is the decisive evidence: every packet is exactly1024/48000 s apart.
Scope and safety
transportPtsis still advanced by video frames (2 call sites), andvideo PTS still uses
maxOf(outputPts + 1, transportPts + 1, f.ptsUs + liveOffset).maxOf(mapped, lastAudioPts + 1)), which is what a muxerrequires; it is no longer forced to stay above the video-driven clock.
run.audio, so no new lifecycle isintroduced.
Open question for the maintainer
Is the shared
transportPtsintentional?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 sharedclock and relaxing only the audio admission rule. My reading is that
transportPtsexists to keepmux calls ordered, not to couple the two streams' rates.
Residual:
skew(audio PTS minus transport clock) now sits at -400 ms ~ +800 ms instead ofgrowing 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 therelay PR for the
stream.lockbump.