Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows exact live sync for HLS #1752

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

stevemayhew
Copy link
Contributor

This pull request satisfies the enhancement request #702. For playback longer than a few hours the fix for SntpClient is also required, see pull request #697

This commit addresses the issue of inaccurate live position measurement in HLS
by introducing NTP time synchronization. Unlike DASH, HLS lacks origin time
synchronization logic, resulting in potential discrepancies in wall-clock time
when using `System.currentTimeMillis()`.

The commit utilizes the `SntpClient` in ExoPlayer to determine the clock offset
with the default NTP server (`time.android.com`) and applies this information to the `HlsMediaSource`.

With the implementation of this change, multiple devices can now synchronize playback to
a common time source, as long as the origin server also synchronizes to an NTP time source.
Enables testing the Live Offset settings with the main demo PlayerActivity.  Using an
intent like:

```
adb shell am start -n androidx.media3.demo.main/.PlayerActivity -a androidx.media3.demo.main.action.VIEW --ei live_offset_target 30 --ef live_offset_adjust_speed 35.0
```

Will start playback synced to a 30 second offset with agressive speed adjustment to achive it, note
this requires audio codec that supports speed changes (AAC for example)
@tonihei tonihei self-assigned this Sep 27, 2024
@tonihei
Copy link
Collaborator

tonihei commented Oct 10, 2024

When we initially implemented support for low-latency HLS, this clock sync wasn't technically needed because all available segments and parts are explicitly published in the HLS playlist without the need for an explicit clock source (unlike DASH where this part of the core logic). The current live offset is always simply measured against the current device time and the "program data time" tags in the playlist. And the target offset is adjusted according to the HLS playlist settings, making it independent of the current system time (=no clock sync needed).

Having said that, it also means the streams don't use the exact same target offset across devices. And if you manually set a target offset, it will be fully dependent on the system time again (this is what I think you are doing in your use case?)

More concretely:

  • I think we can definitely add the offset from the SntpClient in HlsMediaSource as suggested
  • The IntentUtil changes should probably be removed from the PR as they don't add a lot of value (we have lots of settings that can't be changed from a command line, that's fine for a demo app)
  • The changes in DefaultHlsPlaylistTracker are more interesting.
    • You could argue that we don't need them at all. If an app cares about this exact sync, they can trigger the SntpClient sync manually before playback.
    • Why is the update triggered before every playlist load? I assume it's sufficient to only sync in the start method?
    • One problem of this change is that it postpones the initial playlist load for all HLS streams until the SntpClient reports back. What do you think about starting the time sync in the start method as already done. But instead of only triggering the playlist load after the time sync, do it immediately (in parallel). Then we'd need an additional check in processLoadedPlaylist to only process it if the time sync is already available.

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