Skip to content

HLSSegmentProducer preserves a bogus audio start offset on a track with an unset timescale → constant ~1s audio lag (native loopback-HLS) #74

Closed

Description

What happened?

What happened?

A remote progressive MP4 plays with audio a constant ~1 second behind the video on the native
AVPlayer / loopback-HLS path. The same file is in sync in other players (VLC / Infuse), so the
source is playable-correct and the offset is introduced by the engine's remux.

The producer telemetry shows video anchored at 0 while the audio gate keeps the audio's raw first DTS
(~1.003 s) with no compensating shift, and the demuxer warns that the audio track's timescale is
unset:

[mov,mp4,m4a,3gp,3g2,mj2] [warning] stream 0, timescale not set     ← stream 0 = audio
[HLSSegmentProducer] video gate open: actual=0 anchorPts=125 desired=0 shift=0   (125/2997 ≈ 0.042 s)
[HLSSegmentProducer] first video pkt: dts=0 pts=125 duration=125
[HLSSegmentProducer] audio gate open: actual=48128 target=0 desired=0 shift=0 gapMs=0.0  (48128/48000 ≈ 1.003 s)

Video output tfdt = 0, audio output tfdt = 48128 → audio is scheduled ~961 ms after the matching
video. Constant offset, not drift.

Steps to reproduce

  1. Play a remote MP4 whose audio track has an unset/invalid timescale (FFmpeg logs stream N, timescale not set) and whose first audio DTS is well after video start, over the native
    loopback-HLS path.
  2. Observe audio ~1 s behind video for the whole title.
  3. Confirm the same file is in sync in VLC/Infuse.
  4. In [HLSSegmentProducer], note audio gate open: actual=<large> shift=0 — the offset is preserved,
    not reconciled.

AetherEngine version or commit SHA

4.2.1

Host app

Custom / my own integration

Platform

iOS

OS version

27 beta

Device / chip

m2 / a19 pro

Playback path

Native AVPlayer

Source media (for playback bugs)

MP4 (mov,mp4), H.264 1920x1080 + AAC 48 kHz, plus an embedded mjpeg cover + EIA-608 captions.
stream 0 (audio) reports timescale not set; audio first DTS ≈ 48128 @ 1/48000 (≈1.003 s),
video first PTS = 125 @ 1/2997 (≈0.042 s). In sync in VLC/Infuse.

Error codes / log lines

[mov,mp4,m4a,3gp,3g2,mj2] [warning] stream 0, timescale not set
[HLSSegmentProducer] video gate open: actual=0 anchorPts=125 desired=0 shift=0
[HLSSegmentProducer] audio gate open: actual=48128 target=0 desired=0 shift=0 gapMs=0.0
[AQ] AddTrimOrSilence: AudioQueue: request to trim 4114 + 0 = 4114 frames from buffer containing 1024 frames

Anything else

Where it happens.
At head-of-stream the audio gate sets audioShiftPts = rescale(videoShiftPts)
(here 0) and hard-zeroes gapInAudioTb, so it preserves the raw audio-minus-video DTS offset and
the abs(gapMs) > 50 "expect A/V drift audible" warning never fires on the first open (AVgap reads
0). Preserving a real offset is correct (the code cites a deliberate EAC3 +256 ms case). The problem is
this file's ~1 s offset is not intentional — it correlates with the unset audio timescale
(stream 0, timescale not set), so the first DTS is unreliable and other players evidently reconcile it
(edit-list / timescale) while the producer carries it through verbatim.

Suggested direction (deliberately scoped):
when the audio track's timescale is unset/invalid (or an
edit list maps audio presentation to ~0), align audio to the video anchor at head-of-stream instead of
preserving the raw first-DTS offset. Do not blanket-snap audio to video in general — that would
break legitimately-offset tracks (the EAC3 +256 ms case). The change is confined to the single
head-of-stream audio anchor; it must leave untouched: the video shift, restart/seek re-anchor, the live
program-boundary rebase, steady-state playback, and any source with a valid timescale (local or remote),
where today's preservation is already correct. A natural guard is the same timescale not set condition
FFmpeg already detects.

Verification:
ffprobe of the audio stream (start_time, time_base, any elst) will confirm
whether the offset is an edit-list/timescale artifact (this bug) vs an intended gap (must stay
preserved).

Activity

  1. ghost added
    bug
    on Jun 26, 2026
  2. superuser404notfound commented on Jun 26, 2026

    @superuser404notfound
    Owner

    Fixed in 4.5.1.

    Your corrected analysis was exactly right (the comment that landed in #75): this is the HLSSegmentProducer audio gate, not a timescale/tfdt artifact. At head-of-stream the gate dropped every audio packet that arrived before the first video packet. Because this source muxes the leading ~1s of audio (dts 0..47104) ahead of the first video packet (dts 0) in file order, the whole leading second was discarded, and AVPlayer pulled the survivors forward into the constant ~1s lag.

    The fix is the one you proposed: the producer now buffers that pre-gate audio (bounded by an 8 MB cap) and replays it in DTS order once the video gate opens, so it flows through the existing target-filter / anchor / write path. It is scoped to head-of-stream (restartTargetVideoDts == Int64.min); restart and seek producers keep the original drop, where the post-gate shift already snaps their surviving audio onto the video tfdt. Over the cap, the original drop resumes. Normal interleaved media buffers ~0 packets, so it is unaffected.

    If you can retest your file on 4.5.1 and confirm the ~1s offset is gone, that would be great. Thanks again for the precise root-cause work.

  3. DrHurt commented on Jun 26, 2026

    @DrHurt
    Contributor

    This is interesting and a quick search shows that this fix is exactly what VLC does. It buffers instead of drops!

    I’m guessing the fix will also handle starting media playback at non-zero positions like resuming a movie?

    I have a lazy question though: what happens in cases where audio actually starts after video? Are there any special measures taken by segment producer in this case? Would that be needed? I don’t have such a file to test.

    I only ask so we can keep this in mind with any future sync issues reported.

  4. superuser404notfound commented on Jun 26, 2026

    @superuser404notfound
    Owner

    Fixed in 4.5.4.

    Your follow-up was exactly right, and it's the same root cause as the head-of-stream case, just reached through the seek/restart path. On a VOD restart the demuxer lands before the video keyframe and scans forward to it (isKey && dts >= restartTargetVideoDts). The audio that matches that keyframe is muxed ~1 s earlier in file order, so it gets read during the scan-forward while the audio gate is still closed — and the restart path dropped it. The post-gate restart-target filter then accepted the next available audio (~1 s later) and snapped it onto the keyframe tfdt, which is the actual=44112896 target=44064064 gapMs=1017.3 in your trace: audio ahead of picture.

    The fix is the one you proposed: the producer now buffers pre-gate audio on a VOD restart too, not just head-of-stream. The buffered packets replay in DTS order once the video gate opens and flow through the existing restart-target filter, which keeps only the [target, …] window — so it now selects the matching packet and the gate opens at gapMs ≈ 0. Live restart keeps the original drop (its program-boundary re-anchor handles audio separately), the buffer stays under the existing 8 MB cap, and normal-interleave seeks are unaffected (their pre-gate audio is all below target and dropped by the filter exactly as before).

    If you can retest the seek/resume case on 4.5.4 and confirm the mid-file offset is gone, that would close it out. Thanks again for the precise trace and the scan-forward-drop diagnosis.

  5. DrHurt commented on Jun 26, 2026

    @DrHurt
    Contributor

    Really looking forward to this landing in Sodalite. I have some series with wonky audio sync which this will most likely fix. Thank you both for identifying and fixing this. I'm very happy to see the talent this project is attracting and the progress it's making!

  6. ghost closed this as completedon Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions