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