Skip to content

Commit

Permalink
Merge pull request video-dev#1379 from Waidd/subtitleTracks-api
Browse files Browse the repository at this point in the history
Rewrite setSubtitleTrackInternal method.
  • Loading branch information
mangui authored Oct 11, 2017
2 parents 2e3e5b2 + 971e617 commit 9763ba6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
20 changes: 20 additions & 0 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
- [Audio Tracks Control API](#audio-tracks-control-api)
- [`hls.audioTracks`](#hlsaudiotracks)
- [`hls.audioTrack`](#hlsaudiotrack)
- [Subtitle Tracks Control API](#subtitle-tracks-control-api)
- [`hls.subtitleTracks`](#hlssubtitletracks)
- [`hls.subtitleTrack`](#hlssubtitletrack)
- [`hls.subtitleDisplay`](#hlssubtitledisplay)
- [Live stream API](#live-stream-api)
- [`hls.liveSyncPosition`](#hlslivesyncposition)
- [Runtime Events](#runtime-events)
Expand Down Expand Up @@ -1075,6 +1079,22 @@ get : array of audio tracks exposed in manifest

get/set : audio track id (returned by)

## Subtitle Tracks Control API

### `hls.subtitleTracks`

get : array of subtitle tracks exposed in manifest

### `hls.subtitleTrack`

get/set : subtitle track id (returned by)

### `hls.subtitleDisplay`

(default: `false`)

get/set : boolean to enable/disable subtitle display by hls.js

## Live stream API

### `hls.liveSyncPosition`
Expand Down
54 changes: 37 additions & 17 deletions src/controller/subtitle-track-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SubtitleTrackController extends EventHandler {
this.tracks = [];
this.trackId = -1;
this.media = undefined;
this.subtitleDisplay = false;
}

_onTextTracksChanged() {
Expand Down Expand Up @@ -164,23 +165,42 @@ class SubtitleTrackController extends EventHandler {

setSubtitleTrackInternal(newId) {
// check if level idx is valid
if (newId >= 0 && newId < this.tracks.length) {
// stopping live reloading timer if any
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
this.trackId = newId;
logger.log(`switching to subtitle track ${newId}`);
let subtitleTrack = this.tracks[newId];
this.hls.trigger(Event.SUBTITLE_TRACK_SWITCH, {id: newId});
// check if we need to load playlist for this subtitle Track
let details = subtitleTrack.details;
if (details === undefined || details.live === true) {
// track not retrieved yet, or live playlist we need to (re)load it
logger.log(`(re)loading playlist for subtitle track ${newId}`);
this.hls.trigger(Event.SUBTITLE_TRACK_LOADING, {url: subtitleTrack.url, id: newId});
}
if (newId < -1 || newId >= this.tracks.length) {
return;
}

// stopping live reloading timer if any
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}

let textTracks = filterSubtitleTracks(this.media.textTracks);

// hide currently enabled subtitle track
if (this.trackId !== -1 && this.subtitleDisplay) {
textTracks[this.trackId].mode = 'hidden';
}

this.trackId = newId;
logger.log(`switching to subtitle track ${newId}`);
this.hls.trigger(Event.SUBTITLE_TRACK_SWITCH, {id: newId});

if (newId === -1) {
return;
}

let subtitleTrack = this.tracks[newId];
if (this.subtitleDisplay) {
textTracks[newId].mode = 'showing';
}

// check if we need to load playlist for this subtitle Track
let details = subtitleTrack.details;
if (details === undefined || details.live === true) {
// track not retrieved yet, or live playlist we need to (re)load it
logger.log(`(re)loading playlist for subtitle track ${newId}`);
this.hls.trigger(Event.SUBTITLE_TRACK_LOADING, {url: subtitleTrack.url, id: newId});
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/hls.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,16 @@ export default class Hls {
subtitleTrackController.subtitleTrack = subtitleTrackId;
}
}

get subtitleDisplay() {
const subtitleTrackController = this.subtitleTrackController;
return subtitleTrackController ? subtitleTrackController.subtitleDisplay : false;
}

set subtitleDisplay(value) {
const subtitleTrackController = this.subtitleTrackController;
if (subtitleTrackController) {
subtitleTrackController.subtitleDisplay = value;
}
}
}

0 comments on commit 9763ba6

Please sign in to comment.