Skip to content

fix(macos): replay an HLS clip from its end without a spurious end - #265

Open
comprido96 wants to merge 1 commit into
kdroidFilter:masterfrom
comprido96:fix/macos-hls-replay-after-end
Open

comprido96 wants to merge 1 commit into
kdroidFilter:masterfrom
comprido96:fix/macos-hls-replay-after-end

Conversation

@comprido96

Copy link
Copy Markdown

Problem

On macOS, replaying an HLS VOD from its end can stop after a few seconds and jump straight to the end. This is the restart cycle you get with loop = true, and with a manual seekTo(0f) + play() after onPlaybackEnded.

It happens when the clip's first segment carries no audio sample:

  • AVPlayer, seeked back from the end, plays that segment;
  • at the first EXT-X-DISCONTINUITY it jumps to the end and posts AVPlayerItemDidPlayToEndTime;
  • with loop = true the library then seeks to 0 again, so it repeats forever.

The same boundary plays fine on first play. Switching the item's audio track off (AVPlayerItemTrack.isEnabled = false) does not help.

Real-world source: Frigate (NVR) fMP4 clips of a camera that records audio only sparsely, one AAC packet per 10 s recording file. A clip that starts inside a recording file then has a first segment with no audio at all.

Fix

When the HLS item has reached its end since the last rebuild, seekTo rebuilds the player on the same, already loaded asset (setupVideoOutputAndPlayer), then seeks. A fresh item plays through the boundary. The rebuild reuses the loaded asset, so a replay costs about as much as the seek did.

Testing

Tested on macOS 26 (Apple Silicon), against Frigate 0.17 HLS VOD clips (fMP4 segments, EXT-X-DISCONTINUITY between segments, keyframes every 2 s) from two cameras: H.264 Baseline 1280×720 25 fps, and H.264 High 2560×1920 ~30 fps VFR with AAC.

  • On a clip with an audio-less first segment, a Swift driver built against this NativeVideoPlayer.swift ran 10 laps of "seek near the end → end → seekTo(0) + play → watch 12 s": 10/10 without a spurious end, about 1.4 s to restart. The same driver on master: 4/4 laps end at ~7.6 s.
  • On clips whose segments all carry audio: no change (10/10 before and after).
  • The same change in a desktop app, over the network through a loopback proxy: 20/20 clean laps on the failing clip (0/20 before).
  • :mediaplayer:jvmJar passes.

Prepared with an AI coding assistant (Claude Code) while integrating the library into a macOS desktop app, and tested as described above. Happy to adjust, split or drop anything that does not fit the project.

🤖 Generated with Claude Code

Seeked back from its end, AVPlayer plays an HLS VOD up to the first
EXT-X-DISCONTINUITY and then jumps to the end and stops, when the first
segment carries no audio sample. Frigate's fMP4 clips of a camera that
records audio only sparsely (one AAC packet per 10 s file) are like that
whenever a clip starts inside a recording file. It happens on every replay,
including loop = true, which restarts with seekTo(0) from the end. On first
play the same boundary goes through, and switching the audio track off does
not help.

seekTo now rebuilds the player on the same, already loaded asset when the HLS
item has reached its end since the last rebuild. The replay costs about as
much as the seek did, and the clip plays through.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

2 participants