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

Bugfix: Live Playlist Reload Interval #4940

Merged
merged 4 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 8 additions & 40 deletions src/controller/level-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,46 +436,16 @@ export function computeReloadInterval(
newDetails: LevelDetails,
stats: LoaderStats
): number {
const reloadInterval = 1000 * newDetails.levelTargetDuration;
const reloadIntervalAfterMiss = reloadInterval / 2;
const timeSinceLastModified = newDetails.age;
const useLastModified =
timeSinceLastModified > 0 && timeSinceLastModified < reloadInterval * 3;
const reloadInterval = 1000 * newDetails.targetduration;
const roundTrip = stats.loading.end - stats.loading.start;

let estimatedTimeUntilUpdate;
let availabilityDelay = newDetails.availabilityDelay;
// let estimate = 'average';

if (newDetails.updated === false) {
if (useLastModified) {
// estimate = 'miss round trip';
// We should have had a hit so try again in the time it takes to get a response,
// but no less than 1/3 second.
const minRetry = 333 * newDetails.misses;
estimatedTimeUntilUpdate = Math.max(
Math.min(reloadIntervalAfterMiss, roundTrip * 2),
minRetry
);
newDetails.availabilityDelay =
(newDetails.availabilityDelay || 0) + estimatedTimeUntilUpdate;
} else {
// estimate = 'miss half average';
// follow HLS Spec, If the client reloads a Playlist file and finds that it has not
// changed then it MUST wait for a period of one-half the target
// duration before retrying.
estimatedTimeUntilUpdate = reloadIntervalAfterMiss;
}
} else if (useLastModified) {
// estimate = 'next modified date';
// Get the closest we've been to timeSinceLastModified on update
availabilityDelay = Math.min(
availabilityDelay || reloadInterval / 2,
timeSinceLastModified
);
newDetails.availabilityDelay = availabilityDelay;
estimatedTimeUntilUpdate =
availabilityDelay + reloadInterval - timeSinceLastModified;
if (!newDetails.updated) {
// estimate = 'miss half average';
// follow HLS Spec, If the client reloads a Playlist file and finds that it has not
// changed then it MUST wait for a period of one-half the target
// duration before retrying.
estimatedTimeUntilUpdate = reloadInterval / 2;
} else {
estimatedTimeUntilUpdate = reloadInterval - roundTrip;
}
Expand All @@ -484,9 +454,7 @@ export function computeReloadInterval(
// '\n method', estimate,
// '\n estimated time until update =>', estimatedTimeUntilUpdate,
// '\n average target duration', reloadInterval,
// '\n time since modified', timeSinceLastModified,
// '\n time round trip', roundTrip,
// '\n availability delay', availabilityDelay);
// '\n time round trip', roundTrip);

return Math.round(estimatedTimeUntilUpdate);
}
Expand Down
25 changes: 6 additions & 19 deletions tests/unit/controller/level-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,46 +265,33 @@ expect: ${JSON.stringify(merged.fragments[i])}`
});

describe('computeReloadInterval', function () {
it('returns the averagetargetduration of the new level if available', function () {
it('returns the targetduration of the new level if available', function () {
const newPlaylist = generatePlaylist([3, 4]);
newPlaylist.averagetargetduration = 5;
newPlaylist.targetduration = 5;
newPlaylist.updated = true;
const actual = computeReloadInterval(newPlaylist, new LoadStats());
expect(actual).to.equal(5000);
});

it('returns the targetduration of the new level if averagetargetduration is falsy', function () {
const newPlaylist = generatePlaylist([3, 4]);
newPlaylist.averagetargetduration = undefined;
newPlaylist.targetduration = 4;
newPlaylist.updated = true;
let actual = computeReloadInterval(newPlaylist, new LoadStats());
expect(actual).to.equal(4000);

newPlaylist.averagetargetduration = undefined;
actual = computeReloadInterval(newPlaylist, new LoadStats());
expect(actual).to.equal(4000);
});

it('halves the reload interval if the playlist contains the same segments', function () {
const newPlaylist = generatePlaylist([1, 2]);
newPlaylist.updated = false;
newPlaylist.averagetargetduration = 5;
newPlaylist.targetduration = 5;
const actual = computeReloadInterval(newPlaylist, new LoadStats());
expect(actual).to.equal(2500);
});

it('rounds the reload interval', function () {
const newPlaylist = generatePlaylist([3, 4]);
newPlaylist.averagetargetduration = 5.9999;
newPlaylist.targetduration = 5.9999;
newPlaylist.updated = true;
const actual = computeReloadInterval(newPlaylist, new LoadStats());
expect(actual).to.equal(6000);
});

it('subtracts the request time of the last level load from the reload interval', function () {
const newPlaylist = generatePlaylist([3, 4]);
newPlaylist.averagetargetduration = 5;
newPlaylist.targetduration = 5;
newPlaylist.updated = true;
const stats = new LoadStats();
stats.loading.start = 0;
Expand All @@ -315,7 +302,7 @@ expect: ${JSON.stringify(merged.fragments[i])}`

it('returns a minimum of half the target duration', function () {
const newPlaylist = generatePlaylist([3, 4]);
newPlaylist.averagetargetduration = 5;
newPlaylist.targetduration = 5;
newPlaylist.updated = false;
const stats = new LoadStats();
stats.loading.start = 0;
Expand Down