Navigation Menu

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

Improve end-of-stream handling #3704

Merged
merged 3 commits into from Mar 31, 2021
Merged

Improve end-of-stream handling #3704

merged 3 commits into from Mar 31, 2021

Conversation

robwalch
Copy link
Collaborator

@robwalch robwalch commented Mar 30, 2021

This PR will...

Improve end-of-stream handling in Firefox and Safari with multi-track streams

Why is this Pull Request needed?

Playback can stall when seeking to the end of streams. This is often why our functional tests fail in Safari and Firefox. These changes help the case where these tests would timeout completely because both 'seeked' and 'ended' events never fire.

HLS.js now calls mediaSource.endOfStream() only once after both audio and video tracks have appended their final segment, and both SourceBuffers are marked ended prevents playback from stalling while seeking to the end. The 'ended' event still may not fire in cases where audio and video are not aligned near the end.

Before, each call to mediaSource.endOfStream() triggered a "durationchange" on the video element. This was happening twice, once for each track, with some streams. Now, since the duration already set should match the end of the stream after both SourceBuffers are marked ended, we shouldn't see any additional "durationchange" events unless a level or track switch produces a new track end time.

Steps to repro:

  1. Goto https://hls-js-dev.netlify.app/demo/?src=https%3A%2F%2Fplayertest.longtailvideo.com%2Fadaptive%2Felephants_dream_v4%2Findex.m3u8&demoConfig=eyJlbmFibGVTdHJlYW1pbmciOnRydWUsImF1dG9SZWNvdmVyRXJyb3IiOnRydWUsInN0b3BPblN0YWxsIjpmYWxzZSwiZHVtcGZNUDQiOmZhbHNlLCJsZXZlbENhcHBpbmciOi0xLCJsaW1pdE1ldHJpY3MiOi0xfQ==
  2. Play
  3. Seek to duration-3

Expected behavior:
Seeks and plays last 3 seconds then ends

Actual behavior:
Stalls in the "seeking" state at (duration-3)

Checklist

  • changes have been done against master branch, and PR does not conflict
  • new unit / functional tests have been added (whenever applicable)
  • API or design changes are documented in API.md

@robwalch robwalch added this to the 1.0.0 milestone Mar 30, 2021
@robwalch robwalch requested a review from itsjamie March 31, 2021 00:00
Comment on lines 496 to 506
let ended = true;
this.getSourceBufferTypes().forEach((type) => {
const sb = this.sourceBuffer[type];
if (!data.type || data.type === type) {
const sb = this.sourceBuffer[type as SourceBufferName];
if (sb && !sb.ended) {
sb.ended = true;
logger.log(`[buffer-controller]: ${type} sourceBuffer now EOS`);
}
}
}
ended = ended && !!(!sb || sb.ended);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to use Array.prototype.reduce instead of Array.prototype.forEach.

const ended = this.getSourceBufferTypes().reduce((acc: boolean, type: string) => {
  const sb = this.sourceBuffer[type];
  if (!data.type || data.type === type) {
    if (sb && !sb.ended) {
      sb.ended = true;
      logger.log(`[buffer-controller]: ${type} sourceBuffer now EOS`);
    }
  }
  return acc && !!(!sb || sb.ended);
}, true);

@robwalch robwalch merged commit 324cac1 into master Mar 31, 2021
@robwalch robwalch deleted the bugfix/seek-to-end-stall branch March 31, 2021 04:49
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.

None yet

2 participants