Home Reference Source

src/controller/timeline-controller.ts

  1. import Event from '../events';
  2. import EventHandler from '../event-handler';
  3. import Cea608Parser, { CaptionScreen } from '../utils/cea-608-parser';
  4. import OutputFilter from '../utils/output-filter';
  5. import WebVTTParser from '../utils/webvtt-parser';
  6. import { logger } from '../utils/logger';
  7. import { sendAddTrackEvent, clearCurrentCues } from '../utils/texttrack-utils';
  8. import Fragment from '../loader/fragment';
  9. import { HlsConfig } from '../config';
  10.  
  11. // TS todo: Reduce usage of any
  12. class TimelineController extends EventHandler {
  13. private media: HTMLMediaElement | null = null;
  14. private config: HlsConfig;
  15. private enabled: boolean = true;
  16. private Cues: any;
  17. private textTracks: Array<TextTrack> = [];
  18. private tracks: Array<any> = [];
  19. private initPTS: Array<number> = [];
  20. private unparsedVttFrags: Array<{frag: Fragment, payload: any}> = [];
  21. private cueRanges: Array<any> = [];
  22. private captionsTracks: any = {};
  23. private captionsProperties: any;
  24. private cea608Parser!: Cea608Parser;
  25. private lastSn: number = -1;
  26. private prevCC: number = -1;
  27. private vttCCs: any = null;
  28.  
  29. constructor (hls) {
  30. super(hls, Event.MEDIA_ATTACHING,
  31. Event.MEDIA_DETACHING,
  32. Event.FRAG_PARSING_USERDATA,
  33. Event.FRAG_DECRYPTED,
  34. Event.MANIFEST_LOADING,
  35. Event.MANIFEST_LOADED,
  36. Event.FRAG_LOADED,
  37. Event.INIT_PTS_FOUND);
  38.  
  39. this.hls = hls;
  40. this.config = hls.config;
  41. this.Cues = hls.config.cueHandler;
  42.  
  43. this.captionsProperties = {
  44. textTrack1: {
  45. label: this.config.captionsTextTrack1Label,
  46. languageCode: this.config.captionsTextTrack1LanguageCode
  47. },
  48. textTrack2: {
  49. label: this.config.captionsTextTrack2Label,
  50. languageCode: this.config.captionsTextTrack2LanguageCode
  51. }
  52. };
  53.  
  54. if (this.config.enableCEA708Captions) {
  55. const channel1 = new OutputFilter(this, 'textTrack1');
  56. const channel2 = new OutputFilter(this, 'textTrack2');
  57. this.cea608Parser = new Cea608Parser(0, channel1, channel2);
  58. }
  59. }
  60.  
  61. addCues (trackName: string, startTime: number, endTime: number, screen: CaptionScreen) {
  62. // skip cues which overlap more than 50% with previously parsed time ranges
  63. const ranges = this.cueRanges;
  64. let merged = false;
  65. for (let i = ranges.length; i--;) {
  66. let cueRange = ranges[i];
  67. let overlap = intersection(cueRange[0], cueRange[1], startTime, endTime);
  68. if (overlap >= 0) {
  69. cueRange[0] = Math.min(cueRange[0], startTime);
  70. cueRange[1] = Math.max(cueRange[1], endTime);
  71. merged = true;
  72. if ((overlap / (endTime - startTime)) > 0.5) {
  73. return;
  74. }
  75. }
  76. }
  77. if (!merged) {
  78. ranges.push([startTime, endTime]);
  79. }
  80.  
  81. this.Cues.newCue(this.captionsTracks[trackName], startTime, endTime, screen);
  82. }
  83.  
  84. // Triggered when an initial PTS is found; used for synchronisation of WebVTT.
  85. onInitPtsFound (data: { id: string, frag: Fragment, initPTS: number}) {
  86. const { frag, id, initPTS } = data;
  87. const { unparsedVttFrags } = this;
  88. if (id === 'main') {
  89. this.initPTS[frag.cc] = initPTS;
  90. }
  91.  
  92. // Due to asynchronous processing, initial PTS may arrive later than the first VTT fragments are loaded.
  93. // Parse any unparsed fragments upon receiving the initial PTS.
  94. if (unparsedVttFrags.length) {
  95. this.unparsedVttFrags = [];
  96. unparsedVttFrags.forEach(frag => {
  97. this.onFragLoaded(frag);
  98. });
  99. }
  100. }
  101.  
  102. getExistingTrack (trackName: string): TextTrack | null {
  103. const { media } = this;
  104. if (media) {
  105. for (let i = 0; i < media.textTracks.length; i++) {
  106. let textTrack = media.textTracks[i];
  107. if (textTrack[trackName]) {
  108. return textTrack;
  109. }
  110. }
  111. }
  112. return null;
  113. }
  114.  
  115. createCaptionsTrack (trackName: string) {
  116. const { captionsProperties, captionsTracks, media } = this;
  117. const { label, languageCode } = captionsProperties[trackName];
  118. if (!captionsTracks[trackName]) {
  119. // Enable reuse of existing text track.
  120. const existingTrack = this.getExistingTrack(trackName);
  121. if (!existingTrack) {
  122. const textTrack = this.createTextTrack('captions', label, languageCode);
  123. if (textTrack) {
  124. // Set a special property on the track so we know it's managed by Hls.js
  125. textTrack[trackName] = true;
  126. captionsTracks[trackName] = textTrack;
  127. }
  128. } else {
  129. captionsTracks[trackName] = existingTrack;
  130. clearCurrentCues(captionsTracks[trackName]);
  131. sendAddTrackEvent(captionsTracks[trackName], media as HTMLMediaElement);
  132. }
  133. }
  134. }
  135.  
  136. createTextTrack (kind: TextTrackKind, label: string, lang: string): TextTrack | undefined {
  137. const media = this.media;
  138. if (!media) {
  139. return;
  140. }
  141. return media.addTextTrack(kind, label, lang);
  142. }
  143.  
  144. destroy () {
  145. super.destroy();
  146. }
  147.  
  148. onMediaAttaching (data: { media: HTMLMediaElement }) {
  149. this.media = data.media;
  150. this._cleanTracks();
  151. }
  152.  
  153. onMediaDetaching () {
  154. const { captionsTracks } = this;
  155. Object.keys(captionsTracks).forEach(trackName => {
  156. clearCurrentCues(captionsTracks[trackName]);
  157. delete captionsTracks[trackName];
  158. });
  159. }
  160.  
  161. onManifestLoading () {
  162. this.lastSn = -1; // Detect discontiguity in fragment parsing
  163. this.prevCC = -1;
  164. this.vttCCs = { // Detect discontinuity in subtitle manifests
  165. ccOffset: 0,
  166. presentationOffset: 0,
  167. 0: {
  168. start: 0, prevCC: -1, new: false
  169. }
  170. };
  171. this._cleanTracks();
  172. }
  173.  
  174. _cleanTracks () {
  175. // clear outdated subtitles
  176. const { media } = this;
  177. if (!media) {
  178. return;
  179. }
  180. const textTracks = media.textTracks;
  181. if (textTracks) {
  182. for (let i = 0; i < textTracks.length; i++) {
  183. clearCurrentCues(textTracks[i]);
  184. }
  185. }
  186. }
  187.  
  188. onManifestLoaded (data: { subtitles: Array<any> }) {
  189. this.textTracks = [];
  190. this.unparsedVttFrags = this.unparsedVttFrags || [];
  191. this.initPTS = [];
  192. this.cueRanges = [];
  193.  
  194. if (this.config.enableWebVTT) {
  195. this.tracks = data.subtitles || [];
  196. const inUseTracks = this.media ? this.media.textTracks : [];
  197.  
  198. this.tracks.forEach((track, index) => {
  199. let textTrack;
  200. if (index < inUseTracks.length) {
  201. let inUseTrack: TextTrack | null = null;
  202.  
  203. for (let i = 0; i < inUseTracks.length; i++) {
  204. if (canReuseVttTextTrack(inUseTracks[i], track)) {
  205. inUseTrack = inUseTracks[i];
  206. break;
  207. }
  208. }
  209.  
  210. // Reuse tracks with the same label, but do not reuse 608/708 tracks
  211. if (inUseTrack) {
  212. textTrack = inUseTrack;
  213. }
  214. }
  215. if (!textTrack) {
  216. textTrack = this.createTextTrack('subtitles', track.name, track.lang);
  217. }
  218.  
  219. if (track.default) {
  220. textTrack.mode = this.hls.subtitleDisplay ? 'showing' : 'hidden';
  221. } else {
  222. textTrack.mode = 'disabled';
  223. }
  224.  
  225. this.textTracks.push(textTrack);
  226. });
  227. }
  228. }
  229.  
  230. onFragLoaded (data: { frag: Fragment, payload: any }) {
  231. const { frag, payload } = data;
  232. const { cea608Parser, initPTS, lastSn, unparsedVttFrags } = this;
  233. if (frag.type === 'main') {
  234. const sn = frag.sn;
  235. // if this frag isn't contiguous, clear the parser so cues with bad start/end times aren't added to the textTrack
  236. if (frag.sn !== lastSn + 1) {
  237. if (cea608Parser) {
  238. cea608Parser.reset();
  239. }
  240. }
  241. this.lastSn = sn as number;
  242. } // eslint-disable-line brace-style
  243. // If fragment is subtitle type, parse as WebVTT.
  244. else if (frag.type === 'subtitle') {
  245. if (payload.byteLength) {
  246. // We need an initial synchronisation PTS. Store fragments as long as none has arrived.
  247. if (!Number.isFinite(initPTS[frag.cc])) {
  248. unparsedVttFrags.push(data);
  249. if (initPTS.length) {
  250. // finish unsuccessfully, otherwise the subtitle-stream-controller could be blocked from loading new frags.
  251. this.hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag });
  252. }
  253. return;
  254. }
  255.  
  256. let decryptData = frag.decryptdata;
  257. // If the subtitles are not encrypted, parse VTTs now. Otherwise, we need to wait.
  258. if ((decryptData == null) || (decryptData.key == null) || (decryptData.method !== 'AES-128')) {
  259. this._parseVTTs(frag, payload);
  260. }
  261. } else {
  262. // In case there is no payload, finish unsuccessfully.
  263. this.hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag });
  264. }
  265. }
  266. }
  267.  
  268. _parseVTTs (frag: Fragment, payload) {
  269. const { hls, prevCC, textTracks, vttCCs } = this;
  270. if (!vttCCs[frag.cc]) {
  271. vttCCs[frag.cc] = { start: frag.start, prevCC, new: true };
  272. this.prevCC = frag.cc;
  273. }
  274. // Parse the WebVTT file contents.
  275. WebVTTParser.parse(payload, this.initPTS[frag.cc], vttCCs, frag.cc, function (cues) {
  276. const currentTrack = textTracks[frag.level];
  277. // WebVTTParser.parse is an async method and if the currently selected text track mode is set to "disabled"
  278. // before parsing is done then don't try to access currentTrack.cues.getCueById as cues will be null
  279. // and trying to access getCueById method of cues will throw an exception
  280. if (currentTrack.mode === 'disabled') {
  281. hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag: frag });
  282. return;
  283. }
  284. // Add cues and trigger event with success true.
  285. cues.forEach(cue => {
  286. // Sometimes there are cue overlaps on segmented vtts so the same
  287. // cue can appear more than once in different vtt files.
  288. // This avoid showing duplicated cues with same timecode and text.
  289. if (!currentTrack.cues.getCueById(cue.id)) {
  290. try {
  291. currentTrack.addCue(cue);
  292. if (!currentTrack.cues.getCueById(cue.id)) {
  293. throw new Error(`addCue is failed for: ${cue}`);
  294. }
  295. } catch (err) {
  296. logger.debug(`Failed occurred on adding cues: ${err}`);
  297. const textTrackCue = new (window as any).TextTrackCue(cue.startTime, cue.endTime, cue.text);
  298. textTrackCue.id = cue.id;
  299. currentTrack.addCue(textTrackCue);
  300. }
  301. }
  302. }
  303. );
  304. hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: true, frag: frag });
  305. },
  306. function (e) {
  307. // Something went wrong while parsing. Trigger event with success false.
  308. logger.log(`Failed to parse VTT cue: ${e}`);
  309. hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag: frag });
  310. });
  311. }
  312.  
  313. onFragDecrypted (data: { frag: Fragment, payload: any}) {
  314. const { frag, payload } = data;
  315. if (frag.type === 'subtitle') {
  316. if (!Number.isFinite(this.initPTS[frag.cc])) {
  317. this.unparsedVttFrags.push(data);
  318. return;
  319. }
  320.  
  321. this._parseVTTs(frag, payload);
  322. }
  323. }
  324.  
  325. onFragParsingUserdata (data: { samples: Array<any> }) {
  326. if (!this.enabled || !this.cea608Parser) {
  327. return;
  328. }
  329.  
  330. // If the event contains captions (found in the bytes property), push all bytes into the parser immediately
  331. // It will create the proper timestamps based on the PTS value
  332. for (let i = 0; i < data.samples.length; i++) {
  333. const ccBytes = data.samples[i].bytes;
  334. if (ccBytes) {
  335. const ccdatas = this.extractCea608Data(ccBytes);
  336. this.cea608Parser.addData(data.samples[i].pts, ccdatas);
  337. }
  338. }
  339. }
  340.  
  341. extractCea608Data (byteArray: Uint8Array): Array<number> {
  342. let count = byteArray[0] & 31;
  343. let position = 2;
  344. let tmpByte, ccbyte1, ccbyte2, ccValid, ccType;
  345. let actualCCBytes: number[] = [];
  346.  
  347. for (let j = 0; j < count; j++) {
  348. tmpByte = byteArray[position++];
  349. ccbyte1 = 0x7F & byteArray[position++];
  350. ccbyte2 = 0x7F & byteArray[position++];
  351. ccValid = (4 & tmpByte) !== 0;
  352. ccType = 3 & tmpByte;
  353.  
  354. if (ccbyte1 === 0 && ccbyte2 === 0) {
  355. continue;
  356. }
  357.  
  358. if (ccValid) {
  359. if (ccType === 0) { // || ccType === 1
  360. actualCCBytes.push(ccbyte1);
  361. actualCCBytes.push(ccbyte2);
  362. }
  363. }
  364. }
  365. return actualCCBytes;
  366. }
  367. }
  368.  
  369. function canReuseVttTextTrack (inUseTrack, manifestTrack): boolean {
  370. return inUseTrack && inUseTrack.label === manifestTrack.name && !(inUseTrack.textTrack1 || inUseTrack.textTrack2);
  371. }
  372.  
  373. function intersection (x1: number, x2: number, y1: number, y2: number): number {
  374. return Math.min(x2, y2) - Math.max(x1, y1);
  375. }
  376.  
  377. export default TimelineController;