Skip to content

Add experimental synced playback, and a synced mode to the sample app - #50

Merged
514sid merged 1 commit into
mainfrom
synced-playback
Nov 23, 2026
Merged

514sid merged 1 commit into
mainfrom
synced-playback

Conversation

@514sid

@514sid 514sid commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Lets several devices show the same content in step, for video walls and screens side by side. The library part is experimental and needs @OptIn(ExperimentalGaplessApi::class); without it nothing changes.

Library

  • play(asset, startAtMs) puts a video on a timeline: its position should be zero at startAtMs on the controller's clock. A new constructor takes that clock as a GaplessClock; otherwise it is the wall clock. play(asset) and the existing constructor are unchanged.
  • TimelineKeeper keeps the playing clip on its timeline, checked every 250 ms:
    • a few milliseconds off: speed changed in proportion, up to 5%, closing the gap over about two seconds;
    • half a second or more off: a seek, at most once every five seconds;
    • a speed-up never asks the decoder for more than 60 frames a second, so a 60 fps clip is only slowed down; a clip that stalls while sped up goes back to normal speed for the rest of it.
  • The keeper is attached only to an anchored clip on screen. Any slot that is cleared or loaded goes back to normal speed, so a reused player never inherits a corrected speed. It keeps working on a player rebuilt by a retry.
  • setprop log.tag.GaplessTimeline DEBUG logs each anchored clip's distance from its timeline once a second.
  • ExperimentalGaplessApi (opt-in, error level) marks GaplessClock, the clock constructor and play(asset, startAtMs). The library opts itself in.

Sample app

  • --es sync leader on one device, --es sync <leader address> on the others. Followers ask the leader for its time over UDP port 47800 about once a second and keep the exchange with the shortest round trip. Replies carry the playlist and when its first slot starts, so each device works out the same schedule with no messages per cut. Monotonic clocks, so a wall clock correction can't move the schedule.
  • --ez anchor false plays without start times, and --ei lateMs N cuts late on purpose, for testing.
  • --ez clock true shows the shared clock and the sync status on screen.
  • samples/sync/compare.py lines up two devices' logs, at the cuts and once a second during clips.

Measured on two identical TVs (MediaTek, Android 14), muted, 5 minutes per run

Run Cuts apart (median) Pictures apart during clips (median) Seconds within one frame
Start times, both on time 6 ms 4 ms 221 of 224
Start times, one TV cutting 150 ms late 142 ms 54 ms 76 of 191
No start times, one TV cutting 150 ms late 148 ms (no keeper)

With a late cut, 30 fps clips caught up from 150 ms to under 20 ms within four seconds. Images, web pages and 60 fps clips stay as late as their cut. An earlier version sped 60 fps clips up too; the TV's decoder can't go past 60 frames a second, so they stalled for about two seconds and seeked. The frame rate cap and the stall fallback came from that, and the reruns had no stalls.

Audio was not tested: its start-up delay differs between devices and isn't compensated.

Devices that share a clock can now keep a video on a common timeline.
play(asset, startAtMs) puts a clip on the timeline of the controller's
GaplessClock, and TimelineKeeper keeps it there while it plays: a late
or drifting clip is sped up or slowed down by up to 5%, one that is half
a second off seeks, a speed-up never asks the decoder for more than 60
frames a second, and a clip that stalls while sped up isn't sped up
again. The API is marked @ExperimentalGaplessApi and needs an opt-in;
without a start time playback is unchanged.

The sample app gets a sync mode: the leader answers time requests over
UDP on the LAN and hands out the playlist and its start, each device
works out the same schedule from the shared clock and plays it with
start times. samples/sync/compare.py compares two devices' logs at the
cuts and during clips.
@514sid
514sid merged commit bab3881 into main Nov 23, 2026
1 check passed
@514sid
514sid deleted the synced-playback branch November 23, 2026 17:10
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