video-dev / hls.js

HLS.js is a JavaScript library that plays HLS in browsers with support for MSE.
https://hlsjs.video-dev.org/demo
Other
14.88k stars 2.58k forks source link

How can I make sure my users are always as close as possible to the live edge of my stream? #2371

Closed s3thi closed 3 years ago

s3thi commented 5 years ago

What do you want to do with Hls.js?

I'm building an app where I overlay a bunch of data on top of a live video stream. The data is synced to the video using timing metadata embedded into the stream.

My app is extremely time-sensitive, so I want my stream to be as low-latency as possible. I'm currently able to achieve a latency of around 8-10 seconds by tweaking my stream parameters as well as the hls.js configuration. I'm pretty happy with this. However there are 2 issues that I've been seeing in production:

  1. Users on networks that have variable speed fall behind the live edge over time because the video occasionally stalls while hls.js loads new fragments. As these stalls add up over time, I end up with latencies close to 15-20 seconds. It seems like hls.js automatically recovers from this situation, but I'm not sure what the rules around this are.

  2. On browsers that block auto-playing videos, if a user waits around on my page for a few seconds before hitting the play button, the video permanently lags behind the live edge by latency + wait time seconds. There's no recovery in this case.

What are the rules around recovering from high-latency situations?

Is there a way to ensure that my users are always as close as possible to the live edge of a stream?

Could I tell hls.js to automatically fast-forward in a live stream if the user falls too far behind the live edge?

What have you tried so far?

robwalch commented 4 years ago

if a user waits around on my page for a few seconds before hitting the play button, the video permanently lags behind the live edge by latency + wait time seconds.

That could happen when preloading, but it's more likely that there is always going to be additional latency up to TARGETDURATION based on how old the live playlist is when it is first loaded.

A few suggestions:

The playbackRate adjustment might get added to hls.js in the future, following version 1.0 and low-latency playback. But I recommend you try it yourself. You can find an example of code that achieves this here https://github.com/jwplayer/hls.js/blob/demo/nab-lhls/src/controller/playback-rate-controller.ts

Playlist synchronization will be also improved in v1.o leading to more stable latency estimates. Whether latency will be accurate on join depends on your playlist and whether it and the CDN it's behind exposes the "age" header.

Set liveSyncDurationCount to 1, and liveMaxLatencyDurationCount... ... It seems like hls.js automatically recovers from this situation, but I'm not sure what the rules around this are.

The rules for resyncing are based on liveSyncDurationCount and liveMaxLatencyDurationCount which are subject to your playlist's TARGETDURATION (or liveSyncDuration and liveMaxLatencyDuration if you want to specify values in seconds). See _ensureFragmentAtLivePoint: https://github.com/video-dev/hls.js/blob/f29283260253a569d37ce37b8f9fe15dc220d453/src/controller/stream-controller.js#L259

teenoh commented 4 years ago

Thanks @robwalch. I will try this out

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

chloekim02 commented 1 year ago

@teenoh I know it has been a while. but did you manage to do it? @robwalch with playbackRate:2 , can I get latest segment? what I want to do is when there are latest buffer, I always want to show lates segments