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

Load text tracks on demand #6043

Merged
merged 1 commit into from
Nov 4, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/guides/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* [nativeAudioTracks](#nativeaudiotracks)
* [nativeTextTracks](#nativetexttracks)
* [nativeVideoTracks](#nativevideotracks)
* [preloadTextTracks](#preloadtexttracks)

## Standard `<video>` Element Options

Expand Down Expand Up @@ -607,6 +608,14 @@ Can be set to `false` to force emulation of text tracks instead of native suppor

Can be set to `false` to disable native video track support. Most commonly used with [videojs-contrib-hls][videojs-contrib-hls].

#### `preloadTextTracks`

> Type: `boolean`

Can be set to `false` to delay loading of non-active text tracks until use. This can cause a short delay when switching captions during which there may be missing captions.

The default behavior is to preload all text tracks.

[plugins]: /docs/guides/plugins.md

[languages]: /docs/guides/languages.md
Expand Down
2 changes: 2 additions & 0 deletions src/js/tech/tech.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class Tech extends Component {
this.emulateTextTracks();
}

this.preloadTextTracks = options.preloadTextTracks !== false;

this.autoRemoteTextTracks_ = new TRACK_TYPES.ALL.text.ListClass();

this.initTrackListeners();
Expand Down
15 changes: 14 additions & 1 deletion src/js/tracks/text-track.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ class TextTrack extends Track {
this.cues_ = [];
this.activeCues_ = [];

this.preload_ = this.tech_.preloadTextTracks !== false;

const cues = new TextTrackCueList(this.cues_);
const activeCues = new TextTrackCueList(this.activeCues_);
let changed = false;
Expand Down Expand Up @@ -229,6 +231,10 @@ class TextTrack extends Track {
return;
}
mode = newMode;
if (!this.preload_ && mode !== 'disabled' && this.cues.length === 0) {
// On-demand load.
loadTrack(this.src, this);
}
if (mode !== 'disabled') {
this.tech_.ready(() => {
this.tech_.on('timeupdate', timeupdateHandler);
Expand Down Expand Up @@ -324,7 +330,14 @@ class TextTrack extends Track {

if (settings.src) {
this.src = settings.src;
loadTrack(settings.src, this);
if (!this.preload_) {
// Tracks will load on-demand.
// Act like we're loaded for other purposes.
this.loaded_ = true;
}
if (this.preload_ || default_ || (settings.kind !== 'subtitles' && settings.kind !== 'captions')) {
loadTrack(this.src, this);
}
} else {
this.loaded_ = true;
}
Expand Down