Home Reference Source

src/controller/base-stream-controller.ts

  1. import TaskLoop from '../task-loop';
  2. import { FragmentState } from './fragment-tracker';
  3. import { Bufferable, BufferHelper } from '../utils/buffer-helper';
  4. import { logger } from '../utils/logger';
  5. import { Events } from '../events';
  6. import { ErrorDetails } from '../errors';
  7. import * as LevelHelper from './level-helper';
  8. import { ChunkMetadata } from '../types/transmuxer';
  9. import { appendUint8Array } from '../utils/mp4-tools';
  10. import { alignStream } from '../utils/discontinuities';
  11. import {
  12. findFragmentByPDT,
  13. findFragmentByPTS,
  14. findFragWithCC,
  15. } from './fragment-finders';
  16. import TransmuxerInterface from '../demux/transmuxer-interface';
  17. import { Fragment, Part } from '../loader/fragment';
  18. import FragmentLoader, {
  19. FragmentLoadProgressCallback,
  20. LoadError,
  21. } from '../loader/fragment-loader';
  22. import { LevelDetails } from '../loader/level-details';
  23. import {
  24. BufferAppendingData,
  25. ErrorData,
  26. FragLoadedData,
  27. PartsLoadedData,
  28. KeyLoadedData,
  29. MediaAttachingData,
  30. BufferFlushingData,
  31. } from '../types/events';
  32. import Decrypter from '../crypt/decrypter';
  33. import TimeRanges from '../utils/time-ranges';
  34. import { PlaylistLevelType } from '../types/loader';
  35. import type { FragmentTracker } from './fragment-tracker';
  36. import type { Level } from '../types/level';
  37. import type { RemuxedTrack } from '../types/remuxer';
  38. import type Hls from '../hls';
  39. import type { HlsConfig } from '../config';
  40. import type { HlsEventEmitter } from '../events';
  41. import type { NetworkComponentAPI } from '../types/component-api';
  42. import type { SourceBufferName } from '../types/buffer';
  43.  
  44. export const State = {
  45. STOPPED: 'STOPPED',
  46. IDLE: 'IDLE',
  47. KEY_LOADING: 'KEY_LOADING',
  48. FRAG_LOADING: 'FRAG_LOADING',
  49. FRAG_LOADING_WAITING_RETRY: 'FRAG_LOADING_WAITING_RETRY',
  50. WAITING_TRACK: 'WAITING_TRACK',
  51. PARSING: 'PARSING',
  52. PARSED: 'PARSED',
  53. BACKTRACKING: 'BACKTRACKING',
  54. ENDED: 'ENDED',
  55. ERROR: 'ERROR',
  56. WAITING_INIT_PTS: 'WAITING_INIT_PTS',
  57. WAITING_LEVEL: 'WAITING_LEVEL',
  58. };
  59.  
  60. export default class BaseStreamController
  61. extends TaskLoop
  62. implements NetworkComponentAPI {
  63. protected hls: Hls;
  64.  
  65. protected fragPrevious: Fragment | null = null;
  66. protected fragCurrent: Fragment | null = null;
  67. protected fragmentTracker: FragmentTracker;
  68. protected transmuxer: TransmuxerInterface | null = null;
  69. protected _state: string = State.STOPPED;
  70. protected media?: any;
  71. protected mediaBuffer?: any;
  72. protected config: HlsConfig;
  73. protected lastCurrentTime: number = 0;
  74. protected nextLoadPosition: number = 0;
  75. protected startPosition: number = 0;
  76. protected loadedmetadata: boolean = false;
  77. protected fragLoadError: number = 0;
  78. protected retryDate: number = 0;
  79. protected levels: Array<Level> | null = null;
  80. protected fragmentLoader!: FragmentLoader;
  81. protected levelLastLoaded: number | null = null;
  82. protected startFragRequested: boolean = false;
  83. protected decrypter: Decrypter;
  84. protected initPTS: Array<number> = [];
  85. protected onvseeking: EventListener | null = null;
  86. protected onvended: EventListener | null = null;
  87.  
  88. private readonly logPrefix: string = '';
  89. protected readonly log: (msg: any) => void;
  90. protected readonly warn: (msg: any) => void;
  91.  
  92. constructor(hls: Hls, fragmentTracker: FragmentTracker, logPrefix: string) {
  93. super();
  94. this.logPrefix = logPrefix;
  95. this.log = logger.log.bind(logger, `${logPrefix}:`);
  96. this.warn = logger.warn.bind(logger, `${logPrefix}:`);
  97. this.hls = hls;
  98. this.fragmentTracker = fragmentTracker;
  99. this.config = hls.config;
  100. this.decrypter = new Decrypter(hls as HlsEventEmitter, hls.config);
  101. hls.on(Events.KEY_LOADED, this.onKeyLoaded, this);
  102. }
  103.  
  104. protected doTick() {
  105. this.onTickEnd();
  106. }
  107.  
  108. protected onTickEnd() {}
  109.  
  110. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  111. public startLoad(startPosition: number): void {}
  112.  
  113. public stopLoad() {
  114. const frag = this.fragCurrent;
  115. if (frag) {
  116. if (frag.loader) {
  117. frag.loader.abort();
  118. }
  119. this.fragmentTracker.removeFragment(frag);
  120. }
  121. if (this.transmuxer) {
  122. this.transmuxer.destroy();
  123. this.transmuxer = null;
  124. }
  125. this.fragCurrent = null;
  126. this.fragPrevious = null;
  127. this.clearInterval();
  128. this.clearNextTick();
  129. this.state = State.STOPPED;
  130. }
  131.  
  132. protected _streamEnded(bufferInfo, levelDetails) {
  133. const { fragCurrent, fragmentTracker } = this;
  134. // we just got done loading the final fragment and there is no other buffered range after ...
  135. // rationale is that in case there are any buffered ranges after, it means that there are unbuffered portion in between
  136. // so we should not switch to ENDED in that case, to be able to buffer them
  137. if (
  138. !levelDetails.live &&
  139. fragCurrent &&
  140. fragCurrent.sn === levelDetails.endSN &&
  141. !bufferInfo.nextStart
  142. ) {
  143. const fragState = fragmentTracker.getState(fragCurrent);
  144. return (
  145. fragState === FragmentState.PARTIAL || fragState === FragmentState.OK
  146. );
  147. }
  148. return false;
  149. }
  150.  
  151. protected onMediaAttached(
  152. event: Events.MEDIA_ATTACHED,
  153. data: MediaAttachingData
  154. ) {
  155. const media = (this.media = this.mediaBuffer = data.media);
  156. this.onvseeking = this.onMediaSeeking.bind(this);
  157. this.onvended = this.onMediaEnded.bind(this);
  158. media.addEventListener('seeking', this.onvseeking as EventListener);
  159. media.addEventListener('ended', this.onvended as EventListener);
  160. const config = this.config;
  161. if (this.levels && config.autoStartLoad && this.state === State.STOPPED) {
  162. this.startLoad(config.startPosition);
  163. }
  164. }
  165.  
  166. protected onMediaDetaching() {
  167. const media = this.media;
  168. if (media?.ended) {
  169. this.log('MSE detaching and video ended, reset startPosition');
  170. this.startPosition = this.lastCurrentTime = 0;
  171. }
  172.  
  173. // remove video listeners
  174. if (media) {
  175. media.removeEventListener('seeking', this.onvseeking);
  176. media.removeEventListener('ended', this.onvended);
  177. this.onvseeking = this.onvended = null;
  178. }
  179. this.media = this.mediaBuffer = null;
  180. this.loadedmetadata = false;
  181. this.fragmentTracker.removeAllFragments();
  182. this.stopLoad();
  183. }
  184.  
  185. protected onMediaSeeking() {
  186. const { config, fragCurrent, media, mediaBuffer, state } = this;
  187. const currentTime = media ? media.currentTime : null;
  188. const bufferInfo = BufferHelper.bufferInfo(
  189. mediaBuffer || media,
  190. currentTime,
  191. config.maxBufferHole
  192. );
  193.  
  194. this.log(
  195. `media seeking to ${
  196. Number.isFinite(currentTime) ? currentTime.toFixed(3) : currentTime
  197. }, state: ${state}`
  198. );
  199.  
  200. if (state === State.ENDED) {
  201. // if seeking to unbuffered area, clean up fragPrevious
  202. if (!bufferInfo.len) {
  203. this.fragPrevious = null;
  204. this.fragCurrent = null;
  205. }
  206. // switch to IDLE state to check for potential new fragment
  207. this.state = State.IDLE;
  208. } else if (fragCurrent && !bufferInfo.len) {
  209. // check if we are seeking to a unbuffered area AND if frag loading is in progress
  210. const tolerance = config.maxFragLookUpTolerance;
  211. const fragStartOffset = fragCurrent.start - tolerance;
  212. const fragEndOffset =
  213. fragCurrent.start + fragCurrent.duration + tolerance;
  214. // check if the seek position will be out of currently loaded frag range : if out cancel frag load, if in, don't do anything
  215. if (currentTime < fragStartOffset || currentTime > fragEndOffset) {
  216. if (fragCurrent.loader) {
  217. this.log(
  218. 'seeking outside of buffer while fragment load in progress, cancel fragment load'
  219. );
  220. fragCurrent.loader.abort();
  221. }
  222. this.fragCurrent = null;
  223. this.fragPrevious = null;
  224. // switch to IDLE state to load new fragment
  225. this.state = State.IDLE;
  226. }
  227. }
  228.  
  229. if (media) {
  230. this.lastCurrentTime = currentTime;
  231. }
  232.  
  233. // in case seeking occurs although no media buffered, adjust startPosition and nextLoadPosition to seek target
  234. if (!this.loadedmetadata) {
  235. this.nextLoadPosition = this.startPosition = currentTime;
  236. }
  237.  
  238. // tick to speed up processing
  239. this.tick();
  240. }
  241.  
  242. protected onMediaEnded() {
  243. // reset startPosition and lastCurrentTime to restart playback @ stream beginning
  244. this.startPosition = this.lastCurrentTime = 0;
  245. }
  246.  
  247. onKeyLoaded(event: Events.KEY_LOADED, data: KeyLoadedData) {
  248. if (this.state === State.KEY_LOADING && this.levels) {
  249. this.state = State.IDLE;
  250. const levelDetails = this.levels[data.frag.level].details;
  251. if (levelDetails) {
  252. this.loadFragment(data.frag, levelDetails, data.frag.start);
  253. }
  254. }
  255. }
  256.  
  257. protected onHandlerDestroying() {
  258. this.stopLoad();
  259. super.onHandlerDestroying();
  260. }
  261.  
  262. protected onHandlerDestroyed() {
  263. this.state = State.STOPPED;
  264. this.hls.off(Events.KEY_LOADED, this.onKeyLoaded, this);
  265. super.onHandlerDestroyed();
  266. }
  267.  
  268. protected loadFragment(
  269. frag: Fragment,
  270. levelDetails: LevelDetails,
  271. targetBufferTime: number
  272. ) {
  273. this._loadFragForPlayback(frag, levelDetails, targetBufferTime);
  274. }
  275.  
  276. private _loadFragForPlayback(
  277. frag: Fragment,
  278. levelDetails: LevelDetails,
  279. targetBufferTime: number
  280. ) {
  281. const progressCallback: FragmentLoadProgressCallback = (
  282. data: FragLoadedData
  283. ) => {
  284. if (this.fragContextChanged(frag)) {
  285. this.warn(
  286. `Fragment ${frag.sn}${
  287. data.part ? ' p: ' + data.part.index : ''
  288. } of level ${frag.level} was dropped during download.`
  289. );
  290. this.fragmentTracker.removeFragment(frag);
  291. return;
  292. }
  293. frag.stats.chunkCount++;
  294. this._handleFragmentLoadProgress(data);
  295. };
  296.  
  297. this._doFragLoad(frag, levelDetails, targetBufferTime, progressCallback)
  298. .then((data) => {
  299. if (!data) {
  300. // if we're here we probably needed to backtrack or are waiting for more parts
  301. return;
  302. }
  303. this.fragLoadError = 0;
  304. if (this.fragContextChanged(frag)) {
  305. if (
  306. this.state === State.FRAG_LOADING ||
  307. this.state === State.BACKTRACKING
  308. ) {
  309. this.fragmentTracker.removeFragment(frag);
  310. this.state = State.IDLE;
  311. }
  312. return;
  313. }
  314.  
  315. if ('payload' in data) {
  316. this.log(`Loaded fragment ${frag.sn} of level ${frag.level}`);
  317. this.hls.trigger(Events.FRAG_LOADED, data);
  318.  
  319. // Tracker backtrack must be called after onFragLoaded to update the fragment entity state to BACKTRACKED
  320. // This happens after handleTransmuxComplete when the worker or progressive is disabled
  321. if (this.state === State.BACKTRACKING) {
  322. this.fragmentTracker.backtrack(frag, data);
  323. this.resetFragmentLoading(frag);
  324. return;
  325. }
  326. }
  327.  
  328. // Pass through the whole payload; controllers not implementing progressive loading receive data from this callback
  329. this._handleFragmentLoadComplete(data);
  330. })
  331. .catch((reason) => {
  332. this.warn(reason);
  333. this.resetFragmentLoading(frag);
  334. });
  335. }
  336.  
  337. protected flushMainBuffer(
  338. startOffset: number,
  339. endOffset: number,
  340. type: SourceBufferName | null = null
  341. ) {
  342. // When alternate audio is playing, the audio-stream-controller is responsible for the audio buffer. Otherwise,
  343. // passing a null type flushes both buffers
  344. const flushScope: BufferFlushingData = { startOffset, endOffset, type };
  345. // Reset load errors on flush
  346. this.fragLoadError = 0;
  347. this.hls.trigger(Events.BUFFER_FLUSHING, flushScope);
  348. }
  349.  
  350. protected _loadInitSegment(frag: Fragment) {
  351. this._doFragLoad(frag)
  352. .then((data) => {
  353. if (!data || this.fragContextChanged(frag) || !this.levels) {
  354. throw new Error('init load aborted');
  355. }
  356.  
  357. return data;
  358. })
  359. .then((data: FragLoadedData) => {
  360. const { hls } = this;
  361. const { payload } = data;
  362. const decryptData = frag.decryptdata;
  363.  
  364. // check to see if the payload needs to be decrypted
  365. if (
  366. payload &&
  367. payload.byteLength > 0 &&
  368. decryptData &&
  369. decryptData.key &&
  370. decryptData.iv &&
  371. decryptData.method === 'AES-128'
  372. ) {
  373. const startTime = self.performance.now();
  374. // decrypt the subtitles
  375. return this.decrypter
  376. .webCryptoDecrypt(
  377. new Uint8Array(payload),
  378. decryptData.key.buffer,
  379. decryptData.iv.buffer
  380. )
  381. .then((decryptedData) => {
  382. const endTime = self.performance.now();
  383. hls.trigger(Events.FRAG_DECRYPTED, {
  384. frag,
  385. payload: decryptedData,
  386. stats: {
  387. tstart: startTime,
  388. tdecrypt: endTime,
  389. },
  390. });
  391. data.payload = decryptedData;
  392.  
  393. return data;
  394. });
  395. }
  396.  
  397. return data;
  398. })
  399. .then((data: FragLoadedData) => {
  400. const { fragCurrent, hls, levels } = this;
  401. if (!levels) {
  402. throw new Error('init load aborted, missing levels');
  403. }
  404.  
  405. const details = levels[frag.level].details as LevelDetails;
  406. console.assert(
  407. details,
  408. 'Level details are defined when init segment is loaded'
  409. );
  410. const initSegment = details.initSegment as Fragment;
  411. console.assert(
  412. initSegment,
  413. 'Fragment initSegment is defined when init segment is loaded'
  414. );
  415.  
  416. const stats = frag.stats;
  417. this.state = State.IDLE;
  418. this.fragLoadError = 0;
  419. initSegment.data = new Uint8Array(data.payload);
  420. stats.parsing.start = stats.buffering.start = self.performance.now();
  421. stats.parsing.end = stats.buffering.end = self.performance.now();
  422.  
  423. // Silence FRAG_BUFFERED event if fragCurrent is null
  424. if (data.frag === fragCurrent) {
  425. hls.trigger(Events.FRAG_BUFFERED, {
  426. stats,
  427. frag: fragCurrent,
  428. part: null,
  429. id: frag.type,
  430. });
  431. }
  432. this.tick();
  433. })
  434. .catch((reason) => {
  435. this.warn(reason);
  436. this.resetFragmentLoading(frag);
  437. });
  438. }
  439.  
  440. protected fragContextChanged(frag: Fragment | null) {
  441. const { fragCurrent } = this;
  442. return (
  443. !frag ||
  444. !fragCurrent ||
  445. frag.level !== fragCurrent.level ||
  446. frag.sn !== fragCurrent.sn ||
  447. frag.urlId !== fragCurrent.urlId
  448. );
  449. }
  450.  
  451. protected fragBufferedComplete(frag: Fragment, part: Part | null) {
  452. const media = this.mediaBuffer ? this.mediaBuffer : this.media;
  453. this.log(
  454. `Buffered ${frag.type} sn: ${frag.sn}${
  455. part ? ' part: ' + part.index : ''
  456. } of ${this.logPrefix === '[stream-controller]' ? 'level' : 'track'} ${
  457. frag.level
  458. } ${TimeRanges.toString(BufferHelper.getBuffered(media))}`
  459. );
  460. this.state = State.IDLE;
  461. this.tick();
  462. }
  463.  
  464. protected _handleFragmentLoadComplete(fragLoadedEndData: PartsLoadedData) {
  465. const { transmuxer } = this;
  466. if (!transmuxer) {
  467. return;
  468. }
  469. const { frag, part, partsLoaded } = fragLoadedEndData;
  470. // If we did not load parts, or loaded all parts, we have complete (not partial) fragment data
  471. const complete =
  472. !partsLoaded ||
  473. (partsLoaded &&
  474. (partsLoaded.length === 0 ||
  475. partsLoaded.some((fragLoaded) => !fragLoaded)));
  476. const chunkMeta = new ChunkMetadata(
  477. frag.level,
  478. frag.sn as number,
  479. frag.stats.chunkCount + 1,
  480. 0,
  481. part ? part.index : -1,
  482. !complete
  483. );
  484. transmuxer.flush(chunkMeta);
  485. }
  486.  
  487. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  488. protected _handleFragmentLoadProgress(frag: FragLoadedData) {}
  489.  
  490. protected _doFragLoad(
  491. frag: Fragment,
  492. details?: LevelDetails,
  493. targetBufferTime: number | null = null,
  494. progressCallback?: FragmentLoadProgressCallback
  495. ): Promise<PartsLoadedData | FragLoadedData | null> {
  496. if (!this.levels) {
  497. throw new Error('frag load aborted, missing levels');
  498. }
  499. targetBufferTime = Math.max(frag.start, targetBufferTime || 0);
  500. if (this.config.lowLatencyMode && details) {
  501. const partList = details.partList;
  502. if (partList && progressCallback) {
  503. const partIndex = this.getNextPart(partList, frag, targetBufferTime);
  504. if (partIndex > -1) {
  505. const part = partList[partIndex];
  506. this.log(
  507. `Loading part sn: ${frag.sn} p: ${part.index} cc: ${
  508. frag.cc
  509. } of playlist [${details.startSN}-${
  510. details.endSN
  511. }] parts [0-${partIndex}-${partList.length - 1}] ${
  512. this.logPrefix === '[stream-controller]' ? 'level' : 'track'
  513. }: ${frag.level}, target: ${parseFloat(
  514. targetBufferTime.toFixed(3)
  515. )}`
  516. );
  517. this.state = State.FRAG_LOADING;
  518. this.hls.trigger(Events.FRAG_LOADING, {
  519. frag,
  520. part: partList[partIndex],
  521. targetBufferTime,
  522. });
  523. return this.doFragPartsLoad(
  524. frag,
  525. partList,
  526. partIndex,
  527. progressCallback
  528. ).catch((error: LoadError) => this.handleFragLoadError(error));
  529. } else if (
  530. !frag.url ||
  531. this.loadedEndOfParts(partList, targetBufferTime)
  532. ) {
  533. // Fragment hint has no parts
  534. return Promise.resolve(null);
  535. }
  536. }
  537. }
  538.  
  539. this.log(
  540. `Loading fragment ${frag.sn} cc: ${frag.cc} ${
  541. details ? 'of [' + details.startSN + '-' + details.endSN + '] ' : ''
  542. }${this.logPrefix === '[stream-controller]' ? 'level' : 'track'}: ${
  543. frag.level
  544. }, target: ${parseFloat(targetBufferTime.toFixed(3))}`
  545. );
  546.  
  547. this.state = State.FRAG_LOADING;
  548. this.hls.trigger(Events.FRAG_LOADING, { frag, targetBufferTime });
  549.  
  550. return this.fragmentLoader
  551. .load(frag, progressCallback)
  552. .catch((error: LoadError) => this.handleFragLoadError(error));
  553. }
  554.  
  555. private doFragPartsLoad(
  556. frag: Fragment,
  557. partList: Part[],
  558. partIndex: number,
  559. progressCallback: FragmentLoadProgressCallback
  560. ): Promise<PartsLoadedData | null> {
  561. return new Promise(
  562. (resolve: (FragLoadedEndData) => void, reject: (LoadError) => void) => {
  563. const partsLoaded: FragLoadedData[] = [];
  564. const loadPartIndex = (index: number) => {
  565. const part = partList[index];
  566. this.fragmentLoader
  567. .loadPart(frag, part, progressCallback)
  568. .then((partLoadedData: FragLoadedData) => {
  569. partsLoaded[part.index] = partLoadedData;
  570. const loadedPart = partLoadedData.part as Part;
  571. this.hls.trigger(Events.FRAG_LOADED, partLoadedData);
  572. const nextPart = partList[index + 1];
  573. if (nextPart && nextPart.fragment === frag) {
  574. loadPartIndex(index + 1);
  575. } else {
  576. return resolve({
  577. frag,
  578. part: loadedPart,
  579. partsLoaded,
  580. });
  581. }
  582. })
  583. .catch(reject);
  584. };
  585. loadPartIndex(partIndex);
  586. }
  587. );
  588. }
  589.  
  590. private handleFragLoadError({ data }: LoadError) {
  591. if (data && data.details === ErrorDetails.INTERNAL_ABORTED) {
  592. this.handleFragLoadAborted(data.frag, data.part);
  593. } else {
  594. this.hls.trigger(Events.ERROR, data as ErrorData);
  595. }
  596. return null;
  597. }
  598.  
  599. protected _handleTransmuxerFlush(chunkMeta: ChunkMetadata) {
  600. const context = this.getCurrentContext(chunkMeta);
  601. if (!context || this.state !== State.PARSING) {
  602. if (!this.fragCurrent) {
  603. this.state = State.IDLE;
  604. }
  605. return;
  606. }
  607. const { frag, part, level } = context;
  608. const now = self.performance.now();
  609. frag.stats.parsing.end = now;
  610. if (part) {
  611. part.stats.parsing.end = now;
  612. }
  613. this.updateLevelTiming(frag, part, level, chunkMeta.partial);
  614. }
  615.  
  616. protected getCurrentContext(
  617. chunkMeta: ChunkMetadata
  618. ): { frag: Fragment; part: Part | null; level: Level } | null {
  619. const { levels } = this;
  620. const { level: levelIndex, sn, part: partIndex } = chunkMeta;
  621. if (!levels || !levels[levelIndex]) {
  622. this.warn(
  623. `Levels object was unset while buffering fragment ${sn} of level ${levelIndex}. The current chunk will not be buffered.`
  624. );
  625. return null;
  626. }
  627. const level = levels[levelIndex];
  628. const part =
  629. partIndex > -1 ? LevelHelper.getPartWith(level, sn, partIndex) : null;
  630. const frag = part
  631. ? part.fragment
  632. : LevelHelper.getFragmentWithSN(level, sn);
  633. if (!frag) {
  634. return null;
  635. }
  636. return { frag, part, level };
  637. }
  638.  
  639. protected bufferFragmentData(
  640. data: RemuxedTrack,
  641. frag: Fragment,
  642. part: Part | null,
  643. chunkMeta: ChunkMetadata
  644. ) {
  645. if (!data || this.state !== State.PARSING) {
  646. return;
  647. }
  648.  
  649. const { data1, data2 } = data;
  650. let buffer = data1;
  651. if (data1 && data2) {
  652. // Combine the moof + mdat so that we buffer with a single append
  653. buffer = appendUint8Array(data1, data2);
  654. }
  655.  
  656. if (!buffer || !buffer.length) {
  657. return;
  658. }
  659.  
  660. const segment: BufferAppendingData = {
  661. type: data.type,
  662. frag,
  663. part,
  664. chunkMeta,
  665. parent: frag.type,
  666. data: buffer,
  667. };
  668. this.hls.trigger(Events.BUFFER_APPENDING, segment);
  669.  
  670. if (data.dropped && data.independent && !part) {
  671. // Clear buffer so that we reload previous segments sequentially if required
  672. this.flushBufferGap(frag);
  673. }
  674. }
  675.  
  676. protected flushBufferGap(frag: Fragment) {
  677. const media = this.media;
  678. if (!media) {
  679. return;
  680. }
  681. // If currentTime is not buffered, clear the back buffer so that we can backtrack as much as needed
  682. if (!BufferHelper.isBuffered(media, media.currentTime)) {
  683. this.flushMainBuffer(0, frag.start);
  684. return;
  685. }
  686. // Remove back-buffer without interrupting playback to allow back tracking
  687. const currentTime = media.currentTime;
  688. const bufferInfo = BufferHelper.bufferInfo(media, currentTime, 0);
  689. const fragDuration = frag.duration;
  690. const segmentFraction = Math.min(
  691. this.config.maxFragLookUpTolerance * 2,
  692. fragDuration * 0.25
  693. );
  694. const start = Math.max(
  695. Math.min(frag.start - segmentFraction, bufferInfo.end - segmentFraction),
  696. currentTime + segmentFraction
  697. );
  698. if (frag.start - start > segmentFraction) {
  699. this.flushMainBuffer(start, frag.start);
  700. }
  701. }
  702.  
  703. protected reduceMaxBufferLength(threshold?: number) {
  704. const config = this.config;
  705. const minLength = threshold || config.maxBufferLength;
  706. if (config.maxMaxBufferLength >= minLength) {
  707. // reduce max buffer length as it might be too high. we do this to avoid loop flushing ...
  708. config.maxMaxBufferLength /= 2;
  709. this.warn(`Reduce max buffer length to ${config.maxMaxBufferLength}s`);
  710. return true;
  711. }
  712. return false;
  713. }
  714.  
  715. protected getNextFragment(
  716. pos: number,
  717. levelDetails: LevelDetails
  718. ): Fragment | null {
  719. const fragments = levelDetails.fragments;
  720. const fragLen = fragments.length;
  721.  
  722. if (!fragLen) {
  723. return null;
  724. }
  725.  
  726. // find fragment index, contiguous with end of buffer position
  727. const { config } = this;
  728. const start = fragments[0].start;
  729. let frag;
  730.  
  731. // If an initSegment is present, it must be buffered first
  732. if (levelDetails.initSegment && !levelDetails.initSegment.data) {
  733. frag = levelDetails.initSegment;
  734. } else if (levelDetails.live) {
  735. const initialLiveManifestSize = config.initialLiveManifestSize;
  736. if (fragLen < initialLiveManifestSize) {
  737. this.warn(
  738. `Not enough fragments to start playback (have: ${fragLen}, need: ${initialLiveManifestSize})`
  739. );
  740. return null;
  741. }
  742. // The real fragment start times for a live stream are only known after the PTS range for that level is known.
  743. // In order to discover the range, we load the best matching fragment for that level and demux it.
  744. // Do not load using live logic if the starting frag is requested - we want to use getFragmentAtPosition() so that
  745. // we get the fragment matching that start time
  746. if (!levelDetails.PTSKnown && !this.startFragRequested) {
  747. frag = this.getInitialLiveFragment(levelDetails, fragments);
  748. }
  749. } else if (pos <= start) {
  750. // VoD playlist: if loadPosition before start of playlist, load first fragment
  751. frag = fragments[0];
  752. }
  753.  
  754. // If we haven't run into any special cases already, just load the fragment most closely matching the requested position
  755. if (!frag) {
  756. const end = config.lowLatencyMode
  757. ? levelDetails.partEnd
  758. : levelDetails.fragmentEnd;
  759. frag = this.getFragmentAtPosition(pos, end, levelDetails);
  760. }
  761.  
  762. return frag;
  763. }
  764.  
  765. getNextPart(
  766. partList: Part[],
  767. frag: Fragment,
  768. targetBufferTime: number
  769. ): number {
  770. let nextPart = -1;
  771. let contiguous = false;
  772. for (let i = 0, len = partList.length; i < len; i++) {
  773. const part = partList[i];
  774. if (nextPart > -1 && targetBufferTime < part.start) {
  775. break;
  776. }
  777. const loaded = part.loaded;
  778. if (
  779. !loaded &&
  780. (contiguous || part.independent) &&
  781. part.fragment === frag
  782. ) {
  783. nextPart = i;
  784. }
  785. contiguous = loaded;
  786. }
  787. return nextPart;
  788. }
  789.  
  790. private loadedEndOfParts(
  791. partList: Part[],
  792. targetBufferTime: number
  793. ): boolean {
  794. const lastPart = partList[partList.length - 1];
  795. return lastPart && targetBufferTime > lastPart.start && lastPart.loaded;
  796. }
  797.  
  798. /*
  799. This method is used find the best matching first fragment for a live playlist. This fragment is used to calculate the
  800. "sliding" of the playlist, which is its offset from the start of playback. After sliding we can compute the real
  801. start and end times for each fragment in the playlist (after which this method will not need to be called).
  802. */
  803. protected getInitialLiveFragment(
  804. levelDetails: LevelDetails,
  805. fragments: Array<Fragment>
  806. ): Fragment | null {
  807. const fragPrevious = this.fragPrevious;
  808. let frag: Fragment | null = null;
  809. if (fragPrevious) {
  810. if (levelDetails.hasProgramDateTime) {
  811. // Prefer using PDT, because it can be accurate enough to choose the correct fragment without knowing the level sliding
  812. this.log(
  813. `Live playlist, switching playlist, load frag with same PDT: ${fragPrevious.programDateTime}`
  814. );
  815. frag = findFragmentByPDT(
  816. fragments,
  817. fragPrevious.endProgramDateTime,
  818. this.config.maxFragLookUpTolerance
  819. );
  820. }
  821. if (!frag) {
  822. // SN does not need to be accurate between renditions, but depending on the packaging it may be so.
  823. const targetSN = (fragPrevious.sn as number) + 1;
  824. if (
  825. targetSN >= levelDetails.startSN &&
  826. targetSN <= levelDetails.endSN
  827. ) {
  828. const fragNext = fragments[targetSN - levelDetails.startSN];
  829. // Ensure that we're staying within the continuity range, since PTS resets upon a new range
  830. if (fragPrevious.cc === fragNext.cc) {
  831. frag = fragNext;
  832. this.log(
  833. `Live playlist, switching playlist, load frag with next SN: ${
  834. frag!.sn
  835. }`
  836. );
  837. }
  838. }
  839. // It's important to stay within the continuity range if available; otherwise the fragments in the playlist
  840. // will have the wrong start times
  841. if (!frag) {
  842. frag = findFragWithCC(fragments, fragPrevious.cc);
  843. if (frag) {
  844. this.log(
  845. `Live playlist, switching playlist, load frag with same CC: ${frag.sn}`
  846. );
  847. }
  848. }
  849. }
  850. } else {
  851. // Find a new start fragment when fragPrevious is null
  852. const liveStart = this.hls.liveSyncPosition;
  853. if (liveStart !== null) {
  854. frag = this.getFragmentAtPosition(
  855. liveStart,
  856. levelDetails.edge,
  857. levelDetails
  858. );
  859. }
  860. }
  861.  
  862. return frag;
  863. }
  864.  
  865. /*
  866. This method finds the best matching fragment given the provided position.
  867. */
  868. protected getFragmentAtPosition(
  869. bufferEnd: number,
  870. end: number,
  871. levelDetails: LevelDetails
  872. ): Fragment | null {
  873. const { config, fragPrevious } = this;
  874. let { fragments, endSN } = levelDetails;
  875. const { fragmentHint } = levelDetails;
  876. const tolerance = config.maxFragLookUpTolerance;
  877.  
  878. const loadingParts = !!(
  879. config.lowLatencyMode &&
  880. levelDetails.partList &&
  881. fragmentHint
  882. );
  883. if (loadingParts && fragmentHint) {
  884. // Include incomplete fragment with parts at end
  885. fragments = fragments.concat(fragmentHint);
  886. endSN = fragmentHint.sn as number;
  887. }
  888.  
  889. let frag;
  890. if (bufferEnd < end) {
  891. const lookupTolerance = bufferEnd > end - tolerance ? 0 : tolerance;
  892. // Remove the tolerance if it would put the bufferEnd past the actual end of stream
  893. // Uses buffer and sequence number to calculate switch segment (required if using EXT-X-DISCONTINUITY-SEQUENCE)
  894. frag = findFragmentByPTS(
  895. fragPrevious,
  896. fragments,
  897. bufferEnd,
  898. lookupTolerance
  899. );
  900. } else {
  901. // reach end of playlist
  902. frag = fragments[fragments.length - 1];
  903. }
  904.  
  905. if (frag) {
  906. const curSNIdx = frag.sn - levelDetails.startSN;
  907. const sameLevel = fragPrevious && frag.level === fragPrevious.level;
  908. const nextFrag = fragments[curSNIdx + 1];
  909. const fragState = this.fragmentTracker.getState(frag);
  910. if (fragState === FragmentState.BACKTRACKED) {
  911. frag = null;
  912. let i = curSNIdx;
  913. while (
  914. fragments[i] &&
  915. this.fragmentTracker.getState(fragments[i]) ===
  916. FragmentState.BACKTRACKED
  917. ) {
  918. // When fragPrevious is null, backtrack to first the first fragment is not BACKTRACKED for loading
  919. // When fragPrevious is set, we want the first BACKTRACKED fragment for parsing and buffering
  920. if (!fragPrevious) {
  921. frag = fragments[--i];
  922. } else {
  923. frag = fragments[i--];
  924. }
  925. }
  926. if (!frag) {
  927. frag = nextFrag;
  928. }
  929. } else if (fragPrevious && frag.sn === fragPrevious.sn && !loadingParts) {
  930. // Force the next fragment to load if the previous one was already selected. This can occasionally happen with
  931. // non-uniform fragment durations
  932. if (sameLevel) {
  933. if (
  934. frag.sn < endSN &&
  935. this.fragmentTracker.getState(nextFrag) !== FragmentState.OK
  936. ) {
  937. this.log(
  938. `SN ${frag.sn} just loaded, load next one: ${nextFrag.sn}`
  939. );
  940. frag = nextFrag;
  941. } else {
  942. frag = null;
  943. }
  944. }
  945. }
  946. }
  947. return frag;
  948. }
  949.  
  950. protected synchronizeToLiveEdge(levelDetails: LevelDetails) {
  951. const { config, media } = this;
  952. if (!media) {
  953. return;
  954. }
  955. const liveSyncPosition = this.hls.liveSyncPosition;
  956. const currentTime = media.currentTime;
  957. if (
  958. liveSyncPosition !== null &&
  959. media.duration > liveSyncPosition &&
  960. liveSyncPosition > currentTime
  961. ) {
  962. const maxLatency =
  963. config.liveMaxLatencyDuration !== undefined
  964. ? config.liveMaxLatencyDuration
  965. : config.liveMaxLatencyDurationCount * levelDetails.targetduration;
  966. const start = levelDetails.fragments[0].start;
  967. const end = levelDetails.edge;
  968. if (
  969. currentTime <
  970. Math.max(start - config.maxFragLookUpTolerance, end - maxLatency)
  971. ) {
  972. if (!this.loadedmetadata) {
  973. this.nextLoadPosition = liveSyncPosition;
  974. }
  975. if (media.readyState) {
  976. this.warn(
  977. `Playback: ${currentTime.toFixed(
  978. 3
  979. )} is located too far from the end of live sliding playlist: ${end}, reset currentTime to : ${liveSyncPosition.toFixed(
  980. 3
  981. )}`
  982. );
  983. media.currentTime = liveSyncPosition;
  984. }
  985. }
  986. }
  987. }
  988.  
  989. protected alignPlaylists(
  990. details: LevelDetails,
  991. previousDetails?: LevelDetails
  992. ): number {
  993. const { levels, levelLastLoaded } = this;
  994. const lastLevel: Level | null =
  995. levelLastLoaded !== null ? levels![levelLastLoaded] : null;
  996.  
  997. // FIXME: If not for `shouldAlignOnDiscontinuities` requiring fragPrevious.cc,
  998. // this could all go in LevelHelper.mergeDetails
  999. let sliding = 0;
  1000. if (previousDetails && details.fragments.length > 0) {
  1001. sliding = details.fragments[0].start;
  1002. if (details.alignedSliding && Number.isFinite(sliding)) {
  1003. this.log(`Live playlist sliding:${sliding.toFixed(3)}`);
  1004. } else if (!sliding) {
  1005. this.warn(
  1006. `[${this.constructor.name}] Live playlist - outdated PTS, unknown sliding`
  1007. );
  1008. alignStream(this.fragPrevious, lastLevel, details);
  1009. }
  1010. } else {
  1011. this.log('Live playlist - first load, unknown sliding');
  1012. alignStream(this.fragPrevious, lastLevel, details);
  1013. }
  1014.  
  1015. return sliding;
  1016. }
  1017.  
  1018. protected waitForCdnTuneIn(details: LevelDetails) {
  1019. // Wait for Low-Latency CDN Tune-in to get an updated playlist
  1020. const advancePartLimit = 3;
  1021. return (
  1022. details.live &&
  1023. details.canBlockReload &&
  1024. details.tuneInGoal >
  1025. Math.max(details.partHoldBack, details.partTarget * advancePartLimit)
  1026. );
  1027. }
  1028.  
  1029. protected setStartPosition(details: LevelDetails, sliding: number) {
  1030. // compute start position if set to -1. use it straight away if value is defined
  1031. if (this.startPosition === -1 || this.lastCurrentTime === -1) {
  1032. // first, check if start time offset has been set in playlist, if yes, use this value
  1033. let startTimeOffset = details.startTimeOffset!;
  1034. if (Number.isFinite(startTimeOffset)) {
  1035. if (startTimeOffset < 0) {
  1036. this.log(
  1037. `Negative start time offset ${startTimeOffset}, count from end of last fragment`
  1038. );
  1039. startTimeOffset = sliding + details.totalduration + startTimeOffset;
  1040. }
  1041. this.log(
  1042. `Start time offset found in playlist, adjust startPosition to ${startTimeOffset}`
  1043. );
  1044. this.startPosition = startTimeOffset;
  1045. } else {
  1046. if (details.live) {
  1047. this.startPosition = this.hls.liveSyncPosition || sliding;
  1048. this.log(`Configure startPosition to ${this.startPosition}`);
  1049. } else {
  1050. this.startPosition = 0;
  1051. }
  1052. }
  1053. this.lastCurrentTime = this.startPosition;
  1054. }
  1055. this.nextLoadPosition = this.startPosition;
  1056. }
  1057.  
  1058. protected getLoadPosition(): number {
  1059. const { media } = this;
  1060. // if we have not yet loaded any fragment, start loading from start position
  1061. let pos = 0;
  1062. if (this.loadedmetadata) {
  1063. pos = media.currentTime;
  1064. } else if (this.nextLoadPosition) {
  1065. pos = this.nextLoadPosition;
  1066. }
  1067.  
  1068. return pos;
  1069. }
  1070.  
  1071. private handleFragLoadAborted(frag: Fragment, part: Part | undefined) {
  1072. if (this.transmuxer && frag.sn !== 'initSegment') {
  1073. this.warn(
  1074. `Fragment ${frag.sn}${part ? ' part' + part.index : ''} of level ${
  1075. frag.level
  1076. } was aborted`
  1077. );
  1078. this.resetFragmentLoading(frag);
  1079. }
  1080. }
  1081.  
  1082. protected resetFragmentLoading(frag: Fragment) {
  1083. if (!this.fragCurrent || !this.fragContextChanged(frag)) {
  1084. this.state = State.IDLE;
  1085. }
  1086. }
  1087.  
  1088. protected onFragmentOrKeyLoadError(
  1089. filterType: PlaylistLevelType,
  1090. data: ErrorData
  1091. ) {
  1092. if (data.fatal) {
  1093. return;
  1094. }
  1095. const frag = data.frag;
  1096. // Handle frag error related to caller's filterType
  1097. if (!frag || frag.type !== filterType) {
  1098. return;
  1099. }
  1100. const fragCurrent = this.fragCurrent;
  1101. console.assert(
  1102. fragCurrent &&
  1103. frag.sn === fragCurrent.sn &&
  1104. frag.level === fragCurrent.level &&
  1105. frag.urlId === fragCurrent.urlId,
  1106. 'Frag load error must match current frag to retry'
  1107. );
  1108. const config = this.config;
  1109. // keep retrying until the limit will be reached
  1110. if (this.fragLoadError + 1 <= config.fragLoadingMaxRetry) {
  1111. if (this.resetLiveStartWhenNotLoaded(frag.level)) {
  1112. return;
  1113. }
  1114. // exponential backoff capped to config.fragLoadingMaxRetryTimeout
  1115. const delay = Math.min(
  1116. Math.pow(2, this.fragLoadError) * config.fragLoadingRetryDelay,
  1117. config.fragLoadingMaxRetryTimeout
  1118. );
  1119. this.warn(
  1120. `Fragment ${frag.sn} of ${filterType} ${frag.level} failed to load, retrying in ${delay}ms`
  1121. );
  1122. this.retryDate = self.performance.now() + delay;
  1123. this.fragLoadError++;
  1124. this.state = State.FRAG_LOADING_WAITING_RETRY;
  1125. } else if (data.levelRetry) {
  1126. if (filterType === PlaylistLevelType.AUDIO) {
  1127. // Reset current fragment since audio track audio is essential and may not have a fail-over track
  1128. this.fragCurrent = null;
  1129. }
  1130. // Fragment errors that result in a level switch or redundant fail-over
  1131. // should reset the stream controller state to idle
  1132. this.fragLoadError = 0;
  1133. this.state = State.IDLE;
  1134. } else {
  1135. logger.error(
  1136. `${data.details} reaches max retry, redispatch as fatal ...`
  1137. );
  1138. // switch error to fatal
  1139. data.fatal = true;
  1140. this.hls.stopLoad();
  1141. this.state = State.ERROR;
  1142. }
  1143. }
  1144.  
  1145. protected afterBufferFlushed(media: Bufferable, type: SourceBufferName) {
  1146. if (!media) {
  1147. return;
  1148. }
  1149. // After successful buffer flushing, filter flushed fragments from bufferedFrags use mediaBuffered instead of media
  1150. // (so that we will check against video.buffered ranges in case of alt audio track)
  1151. const bufferedTimeRanges = BufferHelper.getBuffered(media);
  1152. this.fragmentTracker.detectEvictedFragments(type, bufferedTimeRanges);
  1153. }
  1154.  
  1155. protected resetLiveStartWhenNotLoaded(level: number): boolean {
  1156. // if loadedmetadata is not set, it means that we are emergency switch down on first frag
  1157. // in that case, reset startFragRequested flag
  1158. if (!this.loadedmetadata) {
  1159. this.startFragRequested = false;
  1160. const details = this.levels ? this.levels[level].details : null;
  1161. if (details?.live) {
  1162. // We can't afford to retry after a delay in a live scenario. Update the start position and return to IDLE.
  1163. this.startPosition = -1;
  1164. this.setStartPosition(details, 0);
  1165. this.state = State.IDLE;
  1166. return true;
  1167. }
  1168. this.nextLoadPosition = this.startPosition;
  1169. }
  1170. return false;
  1171. }
  1172.  
  1173. private updateLevelTiming(
  1174. frag: Fragment,
  1175. part: Part | null,
  1176. level: Level,
  1177. partial: boolean
  1178. ) {
  1179. const details = level.details as LevelDetails;
  1180. console.assert(!!details, 'level.details must be defined');
  1181. const parsed = Object.keys(frag.elementaryStreams).reduce(
  1182. (result, type) => {
  1183. const info = frag.elementaryStreams[type];
  1184. if (info) {
  1185. const parsedDuration = info.endPTS - info.startPTS;
  1186. if (parsedDuration <= 0) {
  1187. // Destroy the transmuxer after it's next time offset failed to advance because duration was <= 0.
  1188. // The new transmuxer will be configured with a time offset matching the next fragment start,
  1189. // preventing the timeline from shifting.
  1190. this.warn(
  1191. `Could not parse fragment ${frag.sn} ${type} duration reliably (${parsedDuration}) resetting transmuxer to fallback to playlist timing`
  1192. );
  1193. if (this.transmuxer) {
  1194. this.transmuxer.destroy();
  1195. this.transmuxer = null;
  1196. }
  1197. return result || false;
  1198. }
  1199. const drift = partial
  1200. ? 0
  1201. : LevelHelper.updateFragPTSDTS(
  1202. details,
  1203. frag,
  1204. info.startPTS,
  1205. info.endPTS,
  1206. info.startDTS,
  1207. info.endDTS
  1208. );
  1209. this.hls.trigger(Events.LEVEL_PTS_UPDATED, {
  1210. details,
  1211. level,
  1212. drift,
  1213. type,
  1214. frag,
  1215. start: info.startPTS,
  1216. end: info.endPTS,
  1217. });
  1218. return true;
  1219. }
  1220. return result;
  1221. },
  1222. false
  1223. );
  1224. if (parsed) {
  1225. this.state = State.PARSED;
  1226. this.hls.trigger(Events.FRAG_PARSED, { frag, part });
  1227. } else {
  1228. this.fragCurrent = null;
  1229. this.fragPrevious = null;
  1230. this.state = State.IDLE;
  1231. }
  1232. }
  1233.  
  1234. set state(nextState) {
  1235. const previousState = this._state;
  1236. if (previousState !== nextState) {
  1237. this._state = nextState;
  1238. this.log(`${previousState}->${nextState}`);
  1239. }
  1240. }
  1241.  
  1242. get state() {
  1243. return this._state;
  1244. }
  1245. }