Skip to content

Commit

Permalink
Cue removal and Error fixes
Browse files Browse the repository at this point in the history
adding code to remvoe the cues after 5 mins for live and setting the state to idle for any error not just subtitle related.
  • Loading branch information
desidiver committed Oct 17, 2019
1 parent 0ee8b02 commit 0d23c19
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/controller/subtitle-stream-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ export class SubtitleStreamController extends TaskLoop {

// If something goes wrong, procede to next frag, if we were processing one.
onError (data) {
let frag = data.frag;
// don't handle error not related to subtitle fragment
if (!frag || frag.type !== 'subtitle') {
return;
}
//Commenting out this code since we need to reset the state for any error
// let frag = data.frag;
// // don't handle error not related to subtitle fragment
// if (!frag || frag.type !== 'subtitle') {
// return;
// }
this.state = State.IDLE;
}

Expand Down Expand Up @@ -221,7 +222,7 @@ export class SubtitleStreamController extends TaskLoop {
this.state = State.KEY_LOADING;
this.hls.trigger(Event.KEY_LOADING, { frag: foundFrag });
} else if (foundFrag && this.fragmentTracker.getState(foundFrag) === FragmentState.NOT_LOADED) {
// only load if fragment is not loaded
// only load if fragment is not loaded
foundFrag.trackId = trackId; // Frags don't know their subtitle track ID, so let's just add that...
this.fragCurrent = foundFrag;
this.state = State.FRAG_LOADING;
Expand Down
20 changes: 18 additions & 2 deletions src/controller/timeline-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class TimelineController extends EventHandler {
hls = this.hls;

// Parse the WebVTT file contents.
WebVTTParser.parse(payload, this.initPTS[frag.cc], vttCCs, frag.cc, function (cues) {
WebVTTParser.parse(payload, this.initPTS[frag.cc], vttCCs, frag.cc,(cues) => {
const currentTrack = textTracks[frag.trackId];
// WebVTTParser.parse is an async method and if the currently selected text track mode is set to "disabled"
// before parsing is done then don't try to access currentTrack.cues.getCueById as cues will be null
Expand All @@ -280,8 +280,12 @@ class TimelineController extends EventHandler {
hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag: frag });
return;
}
let minStartTime = Number.MAX_SAFE_INTEGER;
// Add cues and trigger event with success true.

cues.forEach(cue => {
//find the first cue in the entire cue set since its not always sorted
minStartTime = Math.min(cue.startTime,minStartTime);
// Sometimes there are cue overlaps on segmented vtts so the same
// cue can appear more than once in different vtt files.
// This avoid showing duplicated cues with same timecode and text.
Expand All @@ -294,8 +298,20 @@ class TimelineController extends EventHandler {
currentTrack.addCue(textTrackCue);
}
}
}
}
);

let liveSyncPosition = hls.streamController.liveSyncPosition;
//need to remove cues only for live so if there is no sync position its a VOD
// if there is a better way to detect live vs vod we can use it.
while (cues.length > 0 && liveSyncPosition !== undefined ){
//We remove cues which are more than 5 minutes old than the current cue
if (currentTrack.cues[0].startTime < minStartTime - 300){
currentTrack.removeCue(currentTrack.cues[0]);
} else {
break;
}
}
hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: true, frag: frag });
},
function (e) {
Expand Down
5 changes: 1 addition & 4 deletions src/utils/webvtt-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,12 @@ const WebVTTParser = {
}

if (presentationTime) {
// If we have MPEGTS, offset = presentation time + discontinuity offset

// If we have MPEGTS, offset = presentation time + discontinuity offset
cueOffset = presentationTime - vttCCs.presentationOffset;
console.log('cueOFFSET IS ' + presentationTime + ' vttCCs.presentationOffset ' + vttCCs.presentationOffset);
}
// console.log(cue.startTime + ' before');
cue.startTime += cueOffset - localTime;
cue.endTime += cueOffset - localTime;
console.log(cue.startTime);
// Create a unique hash id for a cue based on start/end times and text.
// This helps timeline-controller to avoid showing repeated captions.
cue.id = hash(cue.startTime.toString()) + hash(cue.endTime.toString()) + hash(cue.text);
Expand Down

0 comments on commit 0d23c19

Please sign in to comment.