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. import { CuesInterface } from '../utils/cues';
  11. import { MediaPlaylist } from '../types/media-playlist';
  12.  
  13. type TrackProperties = {
  14. label: string,
  15. languageCode: string,
  16. media?: MediaPlaylist
  17. };
  18.  
  19. type NonNativeCaptionsTrack = {
  20. _id?: string,
  21. label: string,
  22. kind: string,
  23. default: boolean,
  24. closedCaptions?: MediaPlaylist,
  25. subtitleTrack?: MediaPlaylist
  26. };
  27.  
  28. type VTTCCs = {
  29. ccOffset: number,
  30. presentationOffset: number,
  31. [key: number]: {
  32. start: number,
  33. prevCC: number,
  34. new: boolean
  35. }
  36. };
  37.  
  38. class TimelineController extends EventHandler {
  39. private media: HTMLMediaElement | null = null;
  40. private config: HlsConfig;
  41. private enabled: boolean = true;
  42. private Cues: CuesInterface;
  43. private textTracks: Array<TextTrack> = [];
  44. private tracks: Array<MediaPlaylist> = [];
  45. private initPTS: Array<number> = [];
  46. private unparsedVttFrags: Array<{ frag: Fragment, payload: ArrayBuffer }> = [];
  47. private captionsTracks: Record<string, TextTrack> = {};
  48. private nonNativeCaptionsTracks: Record<string, NonNativeCaptionsTrack> = {};
  49. private captionsProperties: {
  50. textTrack1: TrackProperties
  51. textTrack2: TrackProperties
  52. textTrack3: TrackProperties
  53. textTrack4: TrackProperties
  54. };
  55. private readonly cea608Parser1!: Cea608Parser;
  56. private readonly cea608Parser2!: Cea608Parser;
  57. private lastSn: number = -1;
  58. private prevCC: number = -1;
  59. private vttCCs: VTTCCs = newVTTCCs();
  60.  
  61. constructor (hls) {
  62. super(hls,
  63. Event.MEDIA_ATTACHING,
  64. Event.MEDIA_DETACHING,
  65. Event.FRAG_PARSING_USERDATA,
  66. Event.FRAG_DECRYPTED,
  67. Event.MANIFEST_LOADING,
  68. Event.MANIFEST_LOADED,
  69. Event.FRAG_LOADED,
  70. Event.INIT_PTS_FOUND);
  71.  
  72. this.hls = hls;
  73. this.config = hls.config;
  74. this.Cues = hls.config.cueHandler;
  75.  
  76. this.captionsProperties = {
  77. textTrack1: {
  78. label: this.config.captionsTextTrack1Label,
  79. languageCode: this.config.captionsTextTrack1LanguageCode
  80. },
  81. textTrack2: {
  82. label: this.config.captionsTextTrack2Label,
  83. languageCode: this.config.captionsTextTrack2LanguageCode
  84. },
  85. textTrack3: {
  86. label: this.config.captionsTextTrack3Label,
  87. languageCode: this.config.captionsTextTrack3LanguageCode
  88. },
  89. textTrack4: {
  90. label: this.config.captionsTextTrack4Label,
  91. languageCode: this.config.captionsTextTrack4LanguageCode
  92. }
  93. };
  94.  
  95. if (this.config.enableCEA708Captions) {
  96. const channel1 = new OutputFilter(this, 'textTrack1');
  97. const channel2 = new OutputFilter(this, 'textTrack2');
  98. const channel3 = new OutputFilter(this, 'textTrack3');
  99. const channel4 = new OutputFilter(this, 'textTrack4');
  100. this.cea608Parser1 = new Cea608Parser(1, channel1, channel2);
  101. this.cea608Parser2 = new Cea608Parser(3, channel3, channel4);
  102. }
  103. }
  104.  
  105. addCues (trackName: string, startTime: number, endTime: number, screen: CaptionScreen, cueRanges: Array<[number, number]>) {
  106. // skip cues which overlap more than 50% with previously parsed time ranges
  107. let merged = false;
  108. for (let i = cueRanges.length; i--;) {
  109. let cueRange = cueRanges[i];
  110. let overlap = intersection(cueRange[0], cueRange[1], startTime, endTime);
  111. if (overlap >= 0) {
  112. cueRange[0] = Math.min(cueRange[0], startTime);
  113. cueRange[1] = Math.max(cueRange[1], endTime);
  114. merged = true;
  115. if ((overlap / (endTime - startTime)) > 0.5) {
  116. return;
  117. }
  118. }
  119. }
  120. if (!merged) {
  121. cueRanges.push([startTime, endTime]);
  122. }
  123.  
  124. if (this.config.renderTextTracksNatively) {
  125. this.Cues.newCue(this.captionsTracks[trackName], startTime, endTime, screen);
  126. } else {
  127. const cues = this.Cues.newCue(null, startTime, endTime, screen);
  128. this.hls.trigger(Event.CUES_PARSED, { type: 'captions', cues, track: trackName });
  129. }
  130. }
  131.  
  132. // Triggered when an initial PTS is found; used for synchronisation of WebVTT.
  133. onInitPtsFound (data: { id: string, frag: Fragment, initPTS: number}) {
  134. const { frag, id, initPTS } = data;
  135. const { unparsedVttFrags } = this;
  136. if (id === 'main') {
  137. this.initPTS[frag.cc] = initPTS;
  138. }
  139.  
  140. // Due to asynchronous processing, initial PTS may arrive later than the first VTT fragments are loaded.
  141. // Parse any unparsed fragments upon receiving the initial PTS.
  142. if (unparsedVttFrags.length) {
  143. this.unparsedVttFrags = [];
  144. unparsedVttFrags.forEach(frag => {
  145. this.onFragLoaded(frag);
  146. });
  147. }
  148. }
  149.  
  150. getExistingTrack (trackName: string): TextTrack | null {
  151. const { media } = this;
  152. if (media) {
  153. for (let i = 0; i < media.textTracks.length; i++) {
  154. let textTrack = media.textTracks[i];
  155. if (textTrack[trackName]) {
  156. return textTrack;
  157. }
  158. }
  159. }
  160. return null;
  161. }
  162.  
  163. createCaptionsTrack (trackName: string) {
  164. if (this.config.renderTextTracksNatively) {
  165. this.createNativeTrack(trackName);
  166. } else {
  167. this.createNonNativeTrack(trackName);
  168. }
  169. }
  170.  
  171. createNativeTrack (trackName: string) {
  172. if (this.captionsTracks[trackName]) {
  173. return;
  174. }
  175. const { captionsProperties, captionsTracks, media } = this;
  176. const { label, languageCode } = captionsProperties[trackName];
  177. // Enable reuse of existing text track.
  178. const existingTrack = this.getExistingTrack(trackName);
  179. if (!existingTrack) {
  180. const textTrack = this.createTextTrack('captions', label, languageCode);
  181. if (textTrack) {
  182. // Set a special property on the track so we know it's managed by Hls.js
  183. textTrack[trackName] = true;
  184. captionsTracks[trackName] = textTrack;
  185. }
  186. } else {
  187. captionsTracks[trackName] = existingTrack;
  188. clearCurrentCues(captionsTracks[trackName]);
  189. sendAddTrackEvent(captionsTracks[trackName], media as HTMLMediaElement);
  190. }
  191. }
  192.  
  193. createNonNativeTrack (trackName: string) {
  194. if (this.nonNativeCaptionsTracks[trackName]) {
  195. return;
  196. }
  197. // Create a list of a single track for the provider to consume
  198. const trackProperties: TrackProperties = this.captionsProperties[trackName];
  199. if (!trackProperties) {
  200. return;
  201. }
  202. const label = trackProperties.label as string;
  203. const track = {
  204. _id: trackName,
  205. label,
  206. kind: 'captions',
  207. default: trackProperties.media ? !!trackProperties.media.default : false,
  208. closedCaptions: trackProperties.media
  209. };
  210. this.nonNativeCaptionsTracks[trackName] = track;
  211. this.hls.trigger(Event.NON_NATIVE_TEXT_TRACKS_FOUND, { tracks: [track] });
  212. }
  213. createTextTrack (kind: TextTrackKind, label: string, lang?: string): TextTrack | undefined {
  214. const media = this.media;
  215. if (!media) {
  216. return;
  217. }
  218. return media.addTextTrack(kind, label, lang);
  219. }
  220.  
  221. destroy () {
  222. super.destroy();
  223. }
  224.  
  225. onMediaAttaching (data: { media: HTMLMediaElement }) {
  226. this.media = data.media;
  227. this._cleanTracks();
  228. }
  229.  
  230. onMediaDetaching () {
  231. const { captionsTracks } = this;
  232. Object.keys(captionsTracks).forEach(trackName => {
  233. clearCurrentCues(captionsTracks[trackName]);
  234. delete captionsTracks[trackName];
  235. });
  236. this.nonNativeCaptionsTracks = {};
  237. }
  238.  
  239. onManifestLoading () {
  240. this.lastSn = -1; // Detect discontiguity in fragment parsing
  241. this.prevCC = -1;
  242. this.vttCCs = newVTTCCs(); // Detect discontinuity in subtitle manifests
  243. this._cleanTracks();
  244. this.tracks = [];
  245. this.captionsTracks = {};
  246. this.nonNativeCaptionsTracks = {};
  247. }
  248.  
  249. _cleanTracks () {
  250. // clear outdated subtitles
  251. const { media } = this;
  252. if (!media) {
  253. return;
  254. }
  255. const textTracks = media.textTracks;
  256. if (textTracks) {
  257. for (let i = 0; i < textTracks.length; i++) {
  258. clearCurrentCues(textTracks[i]);
  259. }
  260. }
  261. }
  262.  
  263. onManifestLoaded (data: { subtitles: Array<MediaPlaylist>, captions: Array<MediaPlaylist> }) {
  264. this.textTracks = [];
  265. this.unparsedVttFrags = this.unparsedVttFrags || [];
  266. this.initPTS = [];
  267. if (this.cea608Parser1 && this.cea608Parser2) {
  268. this.cea608Parser1.reset();
  269. this.cea608Parser2.reset();
  270. }
  271.  
  272. if (this.config.enableWebVTT) {
  273. const tracks = data.subtitles || [];
  274. const sameTracks = this.tracks && tracks && this.tracks.length === tracks.length;
  275. this.tracks = data.subtitles || [];
  276.  
  277. if (this.config.renderTextTracksNatively) {
  278. const inUseTracks = this.media ? this.media.textTracks : [];
  279.  
  280. this.tracks.forEach((track, index) => {
  281. let textTrack;
  282. if (index < inUseTracks.length) {
  283. let inUseTrack: TextTrack | null = null;
  284.  
  285. for (let i = 0; i < inUseTracks.length; i++) {
  286. if (canReuseVttTextTrack(inUseTracks[i], track)) {
  287. inUseTrack = inUseTracks[i];
  288. break;
  289. }
  290. }
  291.  
  292. // Reuse tracks with the same label, but do not reuse 608/708 tracks
  293. if (inUseTrack) {
  294. textTrack = inUseTrack;
  295. }
  296. }
  297. if (!textTrack) {
  298. textTrack = this.createTextTrack('subtitles', track.name, track.lang);
  299. }
  300.  
  301. if (track.default) {
  302. textTrack.mode = this.hls.subtitleDisplay ? 'showing' : 'hidden';
  303. } else {
  304. textTrack.mode = 'disabled';
  305. }
  306.  
  307. this.textTracks.push(textTrack);
  308. });
  309. } else if (!sameTracks && this.tracks && this.tracks.length) {
  310. // Create a list of tracks for the provider to consume
  311. const tracksList = this.tracks.map((track) => {
  312. return {
  313. label: track.name,
  314. kind: track.type.toLowerCase(),
  315. default: track.default,
  316. subtitleTrack: track
  317. };
  318. });
  319. this.hls.trigger(Event.NON_NATIVE_TEXT_TRACKS_FOUND, { tracks: tracksList });
  320. }
  321. }
  322.  
  323. if (this.config.enableCEA708Captions && data.captions) {
  324. data.captions.forEach(captionsTrack => {
  325. const instreamIdMatch = /(?:CC|SERVICE)([1-4])/.exec(captionsTrack.instreamId as string);
  326. if (!instreamIdMatch) {
  327. return;
  328. }
  329. const trackName = `textTrack${instreamIdMatch[1]}`;
  330. const trackProperties: TrackProperties = this.captionsProperties[trackName];
  331. if (!trackProperties) {
  332. return;
  333. }
  334. trackProperties.label = captionsTrack.name;
  335. if (captionsTrack.lang) { // optional attribute
  336. trackProperties.languageCode = captionsTrack.lang;
  337. }
  338. trackProperties.media = captionsTrack;
  339. });
  340. }
  341. }
  342.  
  343. onFragLoaded (data: { frag: Fragment, payload: ArrayBuffer }) {
  344. const { frag, payload } = data;
  345. const { cea608Parser1, cea608Parser2, initPTS, lastSn, unparsedVttFrags } = this;
  346. if (frag.type === 'main') {
  347. const sn = frag.sn;
  348. // if this frag isn't contiguous, clear the parser so cues with bad start/end times aren't added to the textTrack
  349. if (frag.sn !== lastSn + 1) {
  350. if (cea608Parser1 && cea608Parser2) {
  351. cea608Parser1.reset();
  352. cea608Parser2.reset();
  353. }
  354. }
  355. this.lastSn = sn as number;
  356. } // eslint-disable-line brace-style
  357. // If fragment is subtitle type, parse as WebVTT.
  358. else if (frag.type === 'subtitle') {
  359. if (payload.byteLength) {
  360. // We need an initial synchronisation PTS. Store fragments as long as none has arrived.
  361. if (!Number.isFinite(initPTS[frag.cc])) {
  362. unparsedVttFrags.push(data);
  363. if (initPTS.length) {
  364. // finish unsuccessfully, otherwise the subtitle-stream-controller could be blocked from loading new frags.
  365. this.hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag });
  366. }
  367. return;
  368. }
  369.  
  370. let decryptData = frag.decryptdata;
  371. // If the subtitles are not encrypted, parse VTTs now. Otherwise, we need to wait.
  372. if ((decryptData == null) || (decryptData.key == null) || (decryptData.method !== 'AES-128')) {
  373. this._parseVTTs(frag, payload);
  374. }
  375. } else {
  376. // In case there is no payload, finish unsuccessfully.
  377. this.hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag });
  378. }
  379. }
  380. }
  381.  
  382. _parseVTTs (frag: Fragment, payload: ArrayBuffer) {
  383. const { hls, prevCC, textTracks, vttCCs } = this;
  384. if (!vttCCs[frag.cc]) {
  385. vttCCs[frag.cc] = { start: frag.start, prevCC, new: true };
  386. this.prevCC = frag.cc;
  387. }
  388. // Parse the WebVTT file contents.
  389. WebVTTParser.parse(payload, this.initPTS[frag.cc], vttCCs, frag.cc, (cues) => {
  390. if (this.config.renderTextTracksNatively) {
  391. const currentTrack = textTracks[frag.level];
  392. // WebVTTParser.parse is an async method and if the currently selected text track mode is set to "disabled"
  393. // before parsing is done then don't try to access currentTrack.cues.getCueById as cues will be null
  394. // and trying to access getCueById method of cues will throw an exception
  395. // Because we check if the mode is diabled, we can force check `cues` below. They can't be null.
  396. if (currentTrack.mode === 'disabled') {
  397. hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag: frag });
  398. return;
  399. }
  400.  
  401. // Add cues and trigger event with success true.
  402. cues.forEach(cue => {
  403. // Sometimes there are cue overlaps on segmented vtts so the same
  404. // cue can appear more than once in different vtt files.
  405. // This avoid showing duplicated cues with same timecode and text.
  406. if (!currentTrack.cues!.getCueById(cue.id)) {
  407. try {
  408. currentTrack.addCue(cue);
  409. if (!currentTrack.cues!.getCueById(cue.id)) {
  410. throw new Error(`addCue is failed for: ${cue}`);
  411. }
  412. } catch (err) {
  413. logger.debug(`Failed occurred on adding cues: ${err}`);
  414. const textTrackCue = new (window as any).TextTrackCue(cue.startTime, cue.endTime, cue.text);
  415. textTrackCue.id = cue.id;
  416. currentTrack.addCue(textTrackCue);
  417. }
  418. }
  419. });
  420. } else {
  421. let trackId = this.tracks[frag.level].default ? 'default' : 'subtitles' + frag.level;
  422. hls.trigger(Event.CUES_PARSED, { type: 'subtitles', cues: cues, track: trackId });
  423. }
  424. hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: true, frag: frag });
  425. },
  426. function (e) {
  427. // Something went wrong while parsing. Trigger event with success false.
  428. logger.log(`Failed to parse VTT cue: ${e}`);
  429. hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag: frag });
  430. });
  431. }
  432.  
  433. onFragDecrypted (data: { frag: Fragment, payload: any}) {
  434. const { frag, payload } = data;
  435. if (frag.type === 'subtitle') {
  436. if (!Number.isFinite(this.initPTS[frag.cc])) {
  437. this.unparsedVttFrags.push(data);
  438. return;
  439. }
  440.  
  441. this._parseVTTs(frag, payload);
  442. }
  443. }
  444.  
  445. onFragParsingUserdata (data: { samples: Array<any> }) {
  446. const { cea608Parser1, cea608Parser2 } = this;
  447. if (!this.enabled || !(cea608Parser1 && cea608Parser2)) {
  448. return;
  449. }
  450.  
  451. // If the event contains captions (found in the bytes property), push all bytes into the parser immediately
  452. // It will create the proper timestamps based on the PTS value
  453. for (let i = 0; i < data.samples.length; i++) {
  454. const ccBytes = data.samples[i].bytes;
  455. if (ccBytes) {
  456. const ccdatas = this.extractCea608Data(ccBytes);
  457. cea608Parser1.addData(data.samples[i].pts, ccdatas[0]);
  458. cea608Parser2.addData(data.samples[i].pts, ccdatas[1]);
  459. }
  460. }
  461. }
  462.  
  463. extractCea608Data (byteArray: Uint8Array): number[][] {
  464. const count = byteArray[0] & 31;
  465. let position = 2;
  466. const actualCCBytes: number[][] = [[], []];
  467.  
  468. for (let j = 0; j < count; j++) {
  469. const tmpByte = byteArray[position++];
  470. const ccbyte1 = 0x7F & byteArray[position++];
  471. const ccbyte2 = 0x7F & byteArray[position++];
  472. const ccValid = (4 & tmpByte) !== 0;
  473. const ccType = 3 & tmpByte;
  474.  
  475. if (ccbyte1 === 0 && ccbyte2 === 0) {
  476. continue;
  477. }
  478.  
  479. if (ccValid) {
  480. if (ccType === 0 || ccType === 1) {
  481. actualCCBytes[ccType].push(ccbyte1);
  482. actualCCBytes[ccType].push(ccbyte2);
  483. }
  484. }
  485. }
  486. return actualCCBytes;
  487. }
  488. }
  489.  
  490. function canReuseVttTextTrack (inUseTrack, manifestTrack): boolean {
  491. return inUseTrack && inUseTrack.label === manifestTrack.name && !(inUseTrack.textTrack1 || inUseTrack.textTrack2);
  492. }
  493.  
  494. function intersection (x1: number, x2: number, y1: number, y2: number): number {
  495. return Math.min(x2, y2) - Math.max(x1, y1);
  496. }
  497.  
  498. function newVTTCCs (): VTTCCs {
  499. return {
  500. ccOffset: 0,
  501. presentationOffset: 0,
  502. 0: {
  503. start: 0,
  504. prevCC: -1,
  505. new: false
  506. }
  507. };
  508. }
  509.  
  510. export default TimelineController;