Source: lib/player.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.Player');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.Deprecate');
  9. goog.require('shaka.config.AutoShowText');
  10. goog.require('shaka.log');
  11. goog.require('shaka.media.AdaptationSetCriteria');
  12. goog.require('shaka.media.BufferingObserver');
  13. goog.require('shaka.media.DrmEngine');
  14. goog.require('shaka.media.ExampleBasedCriteria');
  15. goog.require('shaka.media.ManifestParser');
  16. goog.require('shaka.media.MediaSourceEngine');
  17. goog.require('shaka.media.MediaSourcePlayhead');
  18. goog.require('shaka.media.MetaSegmentIndex');
  19. goog.require('shaka.media.PlayRateController');
  20. goog.require('shaka.media.Playhead');
  21. goog.require('shaka.media.PlayheadObserverManager');
  22. goog.require('shaka.media.PreferenceBasedCriteria');
  23. goog.require('shaka.media.QualityObserver');
  24. goog.require('shaka.media.RegionObserver');
  25. goog.require('shaka.media.RegionTimeline');
  26. goog.require('shaka.media.SegmentIndex');
  27. goog.require('shaka.media.SegmentReference');
  28. goog.require('shaka.media.SrcEqualsPlayhead');
  29. goog.require('shaka.media.StreamingEngine');
  30. goog.require('shaka.media.TimeRangesUtils');
  31. goog.require('shaka.net.NetworkingEngine');
  32. goog.require('shaka.routing.Walker');
  33. goog.require('shaka.text.SimpleTextDisplayer');
  34. goog.require('shaka.text.TextEngine');
  35. goog.require('shaka.text.UITextDisplayer');
  36. goog.require('shaka.text.WebVttGenerator');
  37. goog.require('shaka.util.AbortableOperation');
  38. goog.require('shaka.util.BufferUtils');
  39. goog.require('shaka.util.CmcdManager');
  40. goog.require('shaka.util.ConfigUtils');
  41. goog.require('shaka.util.Error');
  42. goog.require('shaka.util.EventManager');
  43. goog.require('shaka.util.FakeEvent');
  44. goog.require('shaka.util.FakeEventTarget');
  45. goog.require('shaka.util.IDestroyable');
  46. goog.require('shaka.util.LanguageUtils');
  47. goog.require('shaka.util.ManifestParserUtils');
  48. goog.require('shaka.util.MediaReadyState');
  49. goog.require('shaka.util.MimeUtils');
  50. goog.require('shaka.util.ObjectUtils');
  51. goog.require('shaka.util.Platform');
  52. goog.require('shaka.util.PlayerConfiguration');
  53. goog.require('shaka.util.PublicPromise');
  54. goog.require('shaka.util.Stats');
  55. goog.require('shaka.util.StreamUtils');
  56. goog.require('shaka.util.Timer');
  57. goog.require('shaka.lcevc.Dil');
  58. goog.requireType('shaka.media.PresentationTimeline');
  59. goog.requireType('shaka.routing.Node');
  60. goog.requireType('shaka.routing.Payload');
  61. /**
  62. * @event shaka.Player.ErrorEvent
  63. * @description Fired when a playback error occurs.
  64. * @property {string} type
  65. * 'error'
  66. * @property {!shaka.util.Error} detail
  67. * An object which contains details on the error. The error's
  68. * <code>category</code> and <code>code</code> properties will identify the
  69. * specific error that occurred. In an uncompiled build, you can also use the
  70. * <code>message</code> and <code>stack</code> properties to debug.
  71. * @exportDoc
  72. */
  73. /**
  74. * @event shaka.Player.StateChangeEvent
  75. * @description Fired when the player changes load states.
  76. * @property {string} type
  77. * 'onstatechange'
  78. * @property {string} state
  79. * The name of the state that the player just entered.
  80. * @exportDoc
  81. */
  82. /**
  83. * @event shaka.Player.StateIdleEvent
  84. * @description Fired when the player has stopped changing states and will
  85. * remain idle until a new state change request (e.g. <code>load</code>,
  86. * <code>attach</code>, etc.) is made.
  87. * @property {string} type
  88. * 'onstateidle'
  89. * @property {string} state
  90. * The name of the state that the player stopped in.
  91. * @exportDoc
  92. */
  93. /**
  94. * @event shaka.Player.EmsgEvent
  95. * @description Fired when an emsg box is found in a segment.
  96. * If the application calls preventDefault() on this event, further parsing
  97. * will not happen, and no 'metadata' event will be raised for ID3 payloads.
  98. * @property {string} type
  99. * 'emsg'
  100. * @property {shaka.extern.EmsgInfo} detail
  101. * An object which contains the content of the emsg box.
  102. * @exportDoc
  103. */
  104. /**
  105. * @event shaka.Player.DownloadFailed
  106. * @description Fired when a download has failed, for any reason.
  107. * 'downloadfailed'
  108. * @property {!shaka.extern.Request} request
  109. * @property {?shaka.util.Error} error
  110. * @param {number} httpResponseCode
  111. * @param {boolean} aborted
  112. * @exportDoc
  113. */
  114. /**
  115. * @event shaka.Player.DownloadHeadersReceived
  116. * @description Fired when the networking engine has received the headers for
  117. * a download, but before the body has been downloaded.
  118. * If the HTTP plugin being used does not track this information, this event
  119. * will default to being fired when the body is received, instead.
  120. * @property {!Object.<string, string>} headers
  121. * @property {!shaka.extern.Request} request
  122. * @property {!shaka.net.NetworkingEngine.RequestType} type
  123. * 'downloadheadersreceived'
  124. * @exportDoc
  125. */
  126. /**
  127. * @event shaka.Player.DrmSessionUpdateEvent
  128. * @description Fired when the CDM has accepted the license response.
  129. * @property {string} type
  130. * 'drmsessionupdate'
  131. * @exportDoc
  132. */
  133. /**
  134. * @event shaka.Player.TimelineRegionAddedEvent
  135. * @description Fired when a media timeline region is added.
  136. * @property {string} type
  137. * 'timelineregionadded'
  138. * @property {shaka.extern.TimelineRegionInfo} detail
  139. * An object which contains a description of the region.
  140. * @exportDoc
  141. */
  142. /**
  143. * @event shaka.Player.TimelineRegionEnterEvent
  144. * @description Fired when the playhead enters a timeline region.
  145. * @property {string} type
  146. * 'timelineregionenter'
  147. * @property {shaka.extern.TimelineRegionInfo} detail
  148. * An object which contains a description of the region.
  149. * @exportDoc
  150. */
  151. /**
  152. * @event shaka.Player.TimelineRegionExitEvent
  153. * @description Fired when the playhead exits a timeline region.
  154. * @property {string} type
  155. * 'timelineregionexit'
  156. * @property {shaka.extern.TimelineRegionInfo} detail
  157. * An object which contains a description of the region.
  158. * @exportDoc
  159. */
  160. /**
  161. * @event shaka.Player.MediaQualityChangedEvent
  162. * @description Fired when the media quality changes at the playhead.
  163. * That may be caused by an adaptation change or a DASH period transition.
  164. * Separate events are emitted for audio and video contentTypes.
  165. * This is supported for only DASH streams at this time.
  166. * @property {string} type
  167. * 'mediaqualitychanged'
  168. * @property {shaka.extern.MediaQualityInfo} mediaQuality
  169. * Information about media quality at the playhead position.
  170. * @property {number} position
  171. * The playhead position.
  172. * @exportDoc
  173. */
  174. /**
  175. * @event shaka.Player.BufferingEvent
  176. * @description Fired when the player's buffering state changes.
  177. * @property {string} type
  178. * 'buffering'
  179. * @property {boolean} buffering
  180. * True when the Player enters the buffering state.
  181. * False when the Player leaves the buffering state.
  182. * @exportDoc
  183. */
  184. /**
  185. * @event shaka.Player.LoadingEvent
  186. * @description Fired when the player begins loading. The start of loading is
  187. * defined as when the user has communicated intent to load content (i.e.
  188. * <code>Player.load</code> has been called).
  189. * @property {string} type
  190. * 'loading'
  191. * @exportDoc
  192. */
  193. /**
  194. * @event shaka.Player.LoadedEvent
  195. * @description Fired when the player ends the load.
  196. * @property {string} type
  197. * 'loaded'
  198. * @exportDoc
  199. */
  200. /**
  201. * @event shaka.Player.UnloadingEvent
  202. * @description Fired when the player unloads or fails to load.
  203. * Used by the Cast receiver to determine idle state.
  204. * @property {string} type
  205. * 'unloading'
  206. * @exportDoc
  207. */
  208. /**
  209. * @event shaka.Player.TextTrackVisibilityEvent
  210. * @description Fired when text track visibility changes.
  211. * @property {string} type
  212. * 'texttrackvisibility'
  213. * @exportDoc
  214. */
  215. /**
  216. * @event shaka.Player.TracksChangedEvent
  217. * @description Fired when the list of tracks changes. For example, this will
  218. * happen when new tracks are added/removed or when track restrictions change.
  219. * @property {string} type
  220. * 'trackschanged'
  221. * @exportDoc
  222. */
  223. /**
  224. * @event shaka.Player.AdaptationEvent
  225. * @description Fired when an automatic adaptation causes the active tracks
  226. * to change. Does not fire when the application calls
  227. * <code>selectVariantTrack()</code>, <code>selectTextTrack()</code>,
  228. * <code>selectAudioLanguage()</code>, or <code>selectTextLanguage()</code>.
  229. * @property {string} type
  230. * 'adaptation'
  231. * @property {shaka.extern.Track} oldTrack
  232. * @property {shaka.extern.Track} newTrack
  233. * @exportDoc
  234. */
  235. /**
  236. * @event shaka.Player.VariantChangedEvent
  237. * @description Fired when a call from the application caused a variant change.
  238. * Can be triggered by calls to <code>selectVariantTrack()</code> or
  239. * <code>selectAudioLanguage()</code>. Does not fire when an automatic
  240. * adaptation causes a variant change.
  241. * @property {string} type
  242. * 'variantchanged'
  243. * @property {shaka.extern.Track} oldTrack
  244. * @property {shaka.extern.Track} newTrack
  245. * @exportDoc
  246. */
  247. /**
  248. * @event shaka.Player.TextChangedEvent
  249. * @description Fired when a call from the application caused a text stream
  250. * change. Can be triggered by calls to <code>selectTextTrack()</code> or
  251. * <code>selectTextLanguage()</code>.
  252. * @property {string} type
  253. * 'textchanged'
  254. * @exportDoc
  255. */
  256. /**
  257. * @event shaka.Player.ExpirationUpdatedEvent
  258. * @description Fired when there is a change in the expiration times of an
  259. * EME session.
  260. * @property {string} type
  261. * 'expirationupdated'
  262. * @exportDoc
  263. */
  264. /**
  265. * @event shaka.Player.ManifestParsedEvent
  266. * @description Fired after the manifest has been parsed, but before anything
  267. * else happens. The manifest may contain streams that will be filtered out,
  268. * at this stage of the loading process.
  269. * @property {string} type
  270. * 'manifestparsed'
  271. * @exportDoc
  272. */
  273. /**
  274. * @event shaka.Player.MetadataEvent
  275. * @description Triggers after metadata associated with the stream is found.
  276. * Usually they are metadata of type ID3.
  277. * @property {string} type
  278. * 'metadata'
  279. * @property {number} startTime
  280. * The time that describes the beginning of the range of the metadata to
  281. * which the cue applies.
  282. * @property {?number} endTime
  283. * The time that describes the end of the range of the metadata to which
  284. * the cue applies.
  285. * @property {string} metadataType
  286. * Type of metadata. Eg: org.id3 or org.mp4ra
  287. * @property {shaka.extern.MetadataFrame} payload
  288. * The metadata itself
  289. * @exportDoc
  290. */
  291. /**
  292. * @event shaka.Player.StreamingEvent
  293. * @description Fired after the manifest has been parsed and track information
  294. * is available, but before streams have been chosen and before any segments
  295. * have been fetched. You may use this event to configure the player based on
  296. * information found in the manifest.
  297. * @property {string} type
  298. * 'streaming'
  299. * @exportDoc
  300. */
  301. /**
  302. * @event shaka.Player.AbrStatusChangedEvent
  303. * @description Fired when the state of abr has been changed.
  304. * (Enabled or disabled).
  305. * @property {string} type
  306. * 'abrstatuschanged'
  307. * @property {boolean} newStatus
  308. * The new status of the application. True for 'is enabled' and
  309. * false otherwise.
  310. * @exportDoc
  311. */
  312. /**
  313. * @event shaka.Player.RateChangeEvent
  314. * @description Fired when the video's playback rate changes.
  315. * This allows the PlayRateController to update it's internal rate field,
  316. * before the UI updates playback button with the newest playback rate.
  317. * @property {string} type
  318. * 'ratechange'
  319. * @exportDoc
  320. */
  321. /**
  322. * @event shaka.Player.SegmentAppended
  323. * @description Fired when a segment is appended to the media element.
  324. * @property {string} type
  325. * 'segmentappended'
  326. * @property {number} start
  327. * The start time of the segment.
  328. * @property {number} end
  329. * The end time of the segment.
  330. * @property {string} contentType
  331. * The content type of the segment. E.g. 'video', 'audio', or 'text'.
  332. * @exportDoc
  333. */
  334. /**
  335. * @event shaka.Player.SessionDataEvent
  336. * @description Fired when the manifest parser find info about session data.
  337. * Specification: https://tools.ietf.org/html/rfc8216#section-4.3.4.4
  338. * @property {string} type
  339. * 'sessiondata'
  340. * @property {string} id
  341. * The id of the session data.
  342. * @property {string} uri
  343. * The uri with the session data info.
  344. * @property {string} language
  345. * The language of the session data.
  346. * @property {string} value
  347. * The value of the session data.
  348. * @exportDoc
  349. */
  350. /**
  351. * @event shaka.Player.StallDetectedEvent
  352. * @description Fired when a stall in playback is detected by the StallDetector.
  353. * Not all stalls are caused by gaps in the buffered ranges.
  354. * @property {string} type
  355. * 'stalldetected'
  356. * @exportDoc
  357. */
  358. /**
  359. * @event shaka.Player.GapJumpedEvent
  360. * @description Fired when the GapJumpingController jumps over a gap in the
  361. * buffered ranges.
  362. * @property {string} type
  363. * 'gapjumped'
  364. * @exportDoc
  365. */
  366. /**
  367. * @summary The main player object for Shaka Player.
  368. *
  369. * @implements {shaka.util.IDestroyable}
  370. * @export
  371. */
  372. shaka.Player = class extends shaka.util.FakeEventTarget {
  373. /**
  374. * @param {HTMLMediaElement=} mediaElement
  375. * When provided, the player will attach to <code>mediaElement</code>,
  376. * similar to calling <code>attach</code>. When not provided, the player
  377. * will remain detached.
  378. * @param {function(shaka.Player)=} dependencyInjector Optional callback
  379. * which is called to inject mocks into the Player. Used for testing.
  380. */
  381. constructor(mediaElement, dependencyInjector) {
  382. super();
  383. /** @private {shaka.Player.LoadMode} */
  384. this.loadMode_ = shaka.Player.LoadMode.NOT_LOADED;
  385. /** @private {HTMLMediaElement} */
  386. this.video_ = null;
  387. /** @private {HTMLElement} */
  388. this.videoContainer_ = null;
  389. /**
  390. * Since we may not always have a text displayer created (e.g. before |load|
  391. * is called), we need to track what text visibility SHOULD be so that we
  392. * can ensure that when we create the text displayer. When we create our
  393. * text displayer, we will use this to show (or not show) text as per the
  394. * user's requests.
  395. *
  396. * @private {boolean}
  397. */
  398. this.isTextVisible_ = false;
  399. /**
  400. * For listeners scoped to the lifetime of the Player instance.
  401. * @private {shaka.util.EventManager}
  402. */
  403. this.globalEventManager_ = new shaka.util.EventManager();
  404. /**
  405. * For listeners scoped to the lifetime of the media element attachment.
  406. * @private {shaka.util.EventManager}
  407. */
  408. this.attachEventManager_ = new shaka.util.EventManager();
  409. /**
  410. * For listeners scoped to the lifetime of the loaded content.
  411. * @private {shaka.util.EventManager}
  412. */
  413. this.loadEventManager_ = new shaka.util.EventManager();
  414. /** @private {shaka.net.NetworkingEngine} */
  415. this.networkingEngine_ = null;
  416. /** @private {shaka.media.DrmEngine} */
  417. this.drmEngine_ = null;
  418. /** @private {shaka.media.MediaSourceEngine} */
  419. this.mediaSourceEngine_ = null;
  420. /** @private {shaka.media.Playhead} */
  421. this.playhead_ = null;
  422. /**
  423. * The playhead observers are used to monitor the position of the playhead
  424. * and some other source of data (e.g. buffered content), and raise events.
  425. *
  426. * @private {shaka.media.PlayheadObserverManager}
  427. */
  428. this.playheadObservers_ = null;
  429. /**
  430. * This is our control over the playback rate of the media element. This
  431. * provides the missing functionality that we need to provide trick play,
  432. * for example a negative playback rate.
  433. *
  434. * @private {shaka.media.PlayRateController}
  435. */
  436. this.playRateController_ = null;
  437. // We use the buffering observer and timer to track when we move from having
  438. // enough buffered content to not enough. They only exist when content has
  439. // been loaded and are not re-used between loads.
  440. /** @private {shaka.util.Timer} */
  441. this.bufferPoller_ = null;
  442. /** @private {shaka.media.BufferingObserver} */
  443. this.bufferObserver_ = null;
  444. /** @private {shaka.media.RegionTimeline} */
  445. this.regionTimeline_ = null;
  446. /** @private {shaka.util.CmcdManager} */
  447. this.cmcdManager_ = null;
  448. // This is the canvas element that will be used for rendering LCEVC
  449. // enhanced frames.
  450. /** @private {?HTMLCanvasElement} */
  451. this.lcevcCanvas_ = null;
  452. // This is the LCEVC Dil object to decode LCEVC.
  453. /** @private {?shaka.lcevc.Dil} */
  454. this.lcevcDil_ = null;
  455. /** @private {shaka.media.QualityObserver} */
  456. this.qualityObserver_ = null;
  457. /** @private {shaka.media.StreamingEngine} */
  458. this.streamingEngine_ = null;
  459. /** @private {shaka.extern.ManifestParser} */
  460. this.parser_ = null;
  461. /** @private {?shaka.extern.ManifestParser.Factory} */
  462. this.parserFactory_ = null;
  463. /** @private {?shaka.extern.Manifest} */
  464. this.manifest_ = null;
  465. /** @private {?string} */
  466. this.assetUri_ = null;
  467. /** @private {boolean} */
  468. this.fullyLoaded_ = false;
  469. /** @private {shaka.extern.AbrManager} */
  470. this.abrManager_ = null;
  471. /**
  472. * The factory that was used to create the abrManager_ instance.
  473. * @private {?shaka.extern.AbrManager.Factory}
  474. */
  475. this.abrManagerFactory_ = null;
  476. /**
  477. * Contains an ID for use with creating streams. The manifest parser should
  478. * start with small IDs, so this starts with a large one.
  479. * @private {number}
  480. */
  481. this.nextExternalStreamId_ = 1e9;
  482. /** @private {?shaka.extern.PlayerConfiguration} */
  483. this.config_ = this.defaultConfig_();
  484. /**
  485. * The TextDisplayerFactory that was last used to make a text displayer.
  486. * Stored so that we can tell if a new type of text displayer is desired.
  487. * @private {?shaka.extern.TextDisplayer.Factory}
  488. */
  489. this.lastTextFactory_;
  490. /** @private {{width: number, height: number}} */
  491. this.maxHwRes_ = {width: Infinity, height: Infinity};
  492. /** @private {shaka.util.Stats} */
  493. this.stats_ = null;
  494. /** @private {!shaka.media.AdaptationSetCriteria} */
  495. this.currentAdaptationSetCriteria_ =
  496. new shaka.media.PreferenceBasedCriteria(
  497. this.config_.preferredAudioLanguage,
  498. this.config_.preferredVariantRole,
  499. this.config_.preferredAudioChannelCount,
  500. this.config_.preferredVideoHdrLevel);
  501. /** @private {string} */
  502. this.currentTextLanguage_ = this.config_.preferredTextLanguage;
  503. /** @private {string} */
  504. this.currentTextRole_ = this.config_.preferredTextRole;
  505. /** @private {boolean} */
  506. this.currentTextForced_ = this.config_.preferForcedSubs;
  507. /** @private {!Array.<function():(!Promise|undefined)>} */
  508. this.cleanupOnUnload_ = [];
  509. /**
  510. * This playback start position will be used when
  511. * <code>updateStartTime()</code> has been called to provide an updated
  512. * start position during the media loading process.
  513. *
  514. * @private {?number}
  515. */
  516. this.updatedStartTime_ = null;
  517. if (dependencyInjector) {
  518. dependencyInjector(this);
  519. }
  520. // Create the CMCD manager so client data can be attached to all requests
  521. this.cmcdManager_ = this.createCmcd_();
  522. this.networkingEngine_ = this.createNetworkingEngine();
  523. this.networkingEngine_.setForceHTTPS(this.config_.streaming.forceHTTPS);
  524. /** @private {shaka.extern.IAdManager} */
  525. this.adManager_ = null;
  526. if (shaka.Player.adManagerFactory_) {
  527. this.adManager_ = shaka.Player.adManagerFactory_();
  528. }
  529. // If the browser comes back online after being offline, then try to play
  530. // again.
  531. this.globalEventManager_.listen(window, 'online', () => {
  532. this.restoreDisabledVariants_();
  533. this.retryStreaming();
  534. });
  535. /** @private {shaka.routing.Node} */
  536. this.detachNode_ = {name: 'detach'};
  537. /** @private {shaka.routing.Node} */
  538. this.attachNode_ = {name: 'attach'};
  539. /** @private {shaka.routing.Node} */
  540. this.unloadNode_ = {name: 'unload'};
  541. /** @private {shaka.routing.Node} */
  542. this.parserNode_ = {name: 'manifest-parser'};
  543. /** @private {shaka.routing.Node} */
  544. this.manifestNode_ = {name: 'manifest'};
  545. /** @private {shaka.routing.Node} */
  546. this.mediaSourceNode_ = {name: 'media-source'};
  547. /** @private {shaka.routing.Node} */
  548. this.drmNode_ = {name: 'drm-engine'};
  549. /** @private {shaka.routing.Node} */
  550. this.loadNode_ = {name: 'load'};
  551. /** @private {shaka.routing.Node} */
  552. this.srcEqualsDrmNode_ = {name: 'src-equals-drm-engine'};
  553. /** @private {shaka.routing.Node} */
  554. this.srcEqualsNode_ = {name: 'src-equals'};
  555. const AbortableOperation = shaka.util.AbortableOperation;
  556. const actions = new Map();
  557. actions.set(this.attachNode_, (has, wants) => {
  558. return AbortableOperation.notAbortable(this.onAttach_(has, wants));
  559. });
  560. actions.set(this.detachNode_, (has, wants) => {
  561. return AbortableOperation.notAbortable(this.onDetach_(has, wants));
  562. });
  563. actions.set(this.unloadNode_, (has, wants) => {
  564. return AbortableOperation.notAbortable(this.onUnload_(has, wants));
  565. });
  566. actions.set(this.mediaSourceNode_, (has, wants) => {
  567. const p = this.onInitializeMediaSourceEngine_(has, wants);
  568. return AbortableOperation.notAbortable(p);
  569. });
  570. actions.set(this.parserNode_, (has, wants) => {
  571. const p = this.onInitializeParser_(has, wants);
  572. return AbortableOperation.notAbortable(p);
  573. });
  574. actions.set(this.manifestNode_, (has, wants) => {
  575. // This action is actually abortable, so unlike the other callbacks, this
  576. // one will return an abortable operation.
  577. return this.onParseManifest_(has, wants);
  578. });
  579. actions.set(this.drmNode_, (has, wants) => {
  580. const p = this.onInitializeDrm_(has, wants);
  581. return AbortableOperation.notAbortable(p);
  582. });
  583. actions.set(this.loadNode_, (has, wants) => {
  584. return AbortableOperation.notAbortable(this.onLoad_(has, wants));
  585. });
  586. actions.set(this.srcEqualsDrmNode_, (has, wants) => {
  587. const p = this.onInitializeSrcEqualsDrm_(has, wants);
  588. return AbortableOperation.notAbortable(p);
  589. });
  590. actions.set(this.srcEqualsNode_, (has, wants) => {
  591. return this.onSrcEquals_(has, wants);
  592. });
  593. /** @private {shaka.routing.Walker.Implementation} */
  594. const walkerImplementation = {
  595. getNext: (at, has, goingTo, wants) => {
  596. return this.getNextStep_(at, has, goingTo, wants);
  597. },
  598. enterNode: (node, has, wants) => {
  599. this.dispatchEvent(this.makeEvent_(
  600. /* name= */ shaka.util.FakeEvent.EventName.OnStateChange,
  601. /* data= */ (new Map()).set('state', node.name)));
  602. const action = actions.get(node);
  603. return action(has, wants);
  604. },
  605. handleError: async (has, error) => {
  606. shaka.log.warning('The walker saw an error:');
  607. if (error instanceof shaka.util.Error) {
  608. shaka.log.warning('Error Code:', error.code);
  609. } else {
  610. shaka.log.warning('Error Message:', error.message);
  611. shaka.log.warning('Error Stack:', error.stack);
  612. }
  613. // Regardless of what state we were in, if there is an error, we unload.
  614. // This ensures that any initialized system will be torn-down and we
  615. // will go back to a safe foundation. We assume that the media element
  616. // is always safe to use after an error.
  617. await this.onUnload_(has, shaka.Player.createEmptyPayload_());
  618. // There are only two nodes that come before we start loading content,
  619. // attach and detach. If we have a media element, it means we were
  620. // attached to the element, and we can safely return to the attach state
  621. // (we assume that the video element is always re-usable). We favor
  622. // returning to the attach node since it means that the app won't need
  623. // to re-attach if it saw an error.
  624. return has.mediaElement ? this.attachNode_ : this.detachNode_;
  625. },
  626. onIdle: (node) => {
  627. this.dispatchEvent(this.makeEvent_(
  628. /* name= */ shaka.util.FakeEvent.EventName.OnStateIdle,
  629. /* data= */ (new Map()).set('state', node.name)));
  630. },
  631. };
  632. /** @private {shaka.routing.Walker} */
  633. this.walker_ = new shaka.routing.Walker(
  634. this.detachNode_,
  635. shaka.Player.createEmptyPayload_(),
  636. walkerImplementation);
  637. /** @private {shaka.util.Timer} */
  638. this.checkVariantsTimer_ =
  639. new shaka.util.Timer(() => this.checkVariants_());
  640. // Even though |attach| will start in later interpreter cycles, it should be
  641. // the LAST thing we do in the constructor because conceptually it relies on
  642. // player having been initialized.
  643. if (mediaElement) {
  644. this.attach(mediaElement, /* initializeMediaSource= */ true);
  645. }
  646. }
  647. /**
  648. * Create a shaka.lcevc.Dil object
  649. * @param {shaka.extern.LcevcConfiguration} config
  650. * @private
  651. */
  652. createLcevcDil_(config) {
  653. if (this.lcevcDil_ == null) {
  654. this.lcevcDil_ = new shaka.lcevc.Dil(
  655. /** @type {HTMLVideoElement} */ (this.video_),
  656. this.lcevcCanvas_,
  657. config,
  658. );
  659. if (this.mediaSourceEngine_) {
  660. this.mediaSourceEngine_.updateLcevcDil(this.lcevcDil_);
  661. }
  662. }
  663. }
  664. /**
  665. * Close a shaka.lcevc.Dil object if present and hide the canvas.
  666. * @private
  667. */
  668. closeLcevcDil_() {
  669. if (this.lcevcDil_ != null) {
  670. this.lcevcDil_.release();
  671. this.lcevcDil_ = null;
  672. }
  673. }
  674. /**
  675. * Setup shaka.lcevc.Dil object
  676. * @param {?shaka.extern.PlayerConfiguration} config
  677. * @private
  678. */
  679. setupLcevc_(config) {
  680. if (config.lcevc.enabled) {
  681. const tracks = this.getVariantTracks();
  682. if (tracks && tracks[0] &&
  683. tracks[0].videoMimeType ==
  684. shaka.Player.SRC_EQUAL_EXTENSIONS_TO_MIME_TYPES_['ts']) {
  685. const edge = shaka.util.Platform.isEdge() ||
  686. shaka.util.Platform.isLegacyEdge();
  687. if (edge) {
  688. if (!config.mediaSource.forceTransmux) {
  689. // If forceTransmux is disabled for Microsoft Edge, LCEVC data
  690. // is stripped out in case of a MPEG-2 TS container.
  691. // Hence the warning for Microsoft Edge when playing content with
  692. // MPEG-2 TS container.
  693. shaka.log.alwaysWarn('LCEVC Warning: For MPEG-2 TS decoding '+
  694. 'the config.mediaSource.forceTransmux must be enabled.');
  695. }
  696. }
  697. }
  698. this.closeLcevcDil_();
  699. this.createLcevcDil_(config.lcevc);
  700. } else {
  701. this.closeLcevcDil_();
  702. }
  703. }
  704. /**
  705. * @param {!shaka.util.FakeEvent.EventName} name
  706. * @param {Map.<string, Object>=} data
  707. * @return {!shaka.util.FakeEvent}
  708. * @private
  709. */
  710. makeEvent_(name, data) {
  711. return new shaka.util.FakeEvent(name, data);
  712. }
  713. /**
  714. * After destruction, a Player object cannot be used again.
  715. *
  716. * @override
  717. * @export
  718. */
  719. async destroy() {
  720. // Make sure we only execute the destroy logic once.
  721. if (this.loadMode_ == shaka.Player.LoadMode.DESTROYED) {
  722. return;
  723. }
  724. // If LCEVC Dil exists close it.
  725. this.closeLcevcDil_();
  726. // Mark as "dead". This should stop external-facing calls from changing our
  727. // internal state any more. This will stop calls to |attach|, |detach|, etc.
  728. // from interrupting our final move to the detached state.
  729. this.loadMode_ = shaka.Player.LoadMode.DESTROYED;
  730. // Because we have set |loadMode_| to |DESTROYED| we can't call |detach|. We
  731. // must talk to |this.walker_| directly.
  732. const events = this.walker_.startNewRoute((currentPayload) => {
  733. return {
  734. node: this.detachNode_,
  735. payload: shaka.Player.createEmptyPayload_(),
  736. interruptible: false,
  737. };
  738. });
  739. // Wait until the detach has finished so that we don't interrupt it by
  740. // calling |destroy| on |this.walker_|. To avoid failing here, we always
  741. // resolve the promise.
  742. await new Promise((resolve) => {
  743. events.onStart = () => {
  744. shaka.log.info('Preparing to destroy walker...');
  745. };
  746. events.onEnd = () => {
  747. resolve();
  748. };
  749. events.onCancel = () => {
  750. goog.asserts.assert(false,
  751. 'Our final detach call should never be cancelled.');
  752. resolve();
  753. };
  754. events.onError = () => {
  755. goog.asserts.assert(false,
  756. 'Our final detach call should never see an error');
  757. resolve();
  758. };
  759. events.onSkip = () => {
  760. goog.asserts.assert(false,
  761. 'Our final detach call should never be skipped');
  762. resolve();
  763. };
  764. });
  765. await this.walker_.destroy();
  766. // Tear-down the event managers to ensure handlers stop firing.
  767. if (this.globalEventManager_) {
  768. this.globalEventManager_.release();
  769. this.globalEventManager_ = null;
  770. }
  771. if (this.attachEventManager_) {
  772. this.attachEventManager_.release();
  773. this.attachEventManager_ = null;
  774. }
  775. if (this.loadEventManager_) {
  776. this.loadEventManager_.release();
  777. this.loadEventManager_ = null;
  778. }
  779. this.abrManagerFactory_ = null;
  780. this.config_ = null;
  781. this.stats_ = null;
  782. this.videoContainer_ = null;
  783. this.cmcdManager_ = null;
  784. if (this.networkingEngine_) {
  785. await this.networkingEngine_.destroy();
  786. this.networkingEngine_ = null;
  787. }
  788. if (this.abrManager_) {
  789. this.abrManager_.release();
  790. this.abrManager_ = null;
  791. }
  792. // FakeEventTarget implements IReleasable
  793. super.release();
  794. }
  795. /**
  796. * Registers a plugin callback that will be called with
  797. * <code>support()</code>. The callback will return the value that will be
  798. * stored in the return value from <code>support()</code>.
  799. *
  800. * @param {string} name
  801. * @param {function():*} callback
  802. * @export
  803. */
  804. static registerSupportPlugin(name, callback) {
  805. shaka.Player.supportPlugins_[name] = callback;
  806. }
  807. /**
  808. * Set a factory to create an ad manager during player construction time.
  809. * This method needs to be called bafore instantiating the Player class.
  810. *
  811. * @param {!shaka.extern.IAdManager.Factory} factory
  812. * @export
  813. */
  814. static setAdManagerFactory(factory) {
  815. shaka.Player.adManagerFactory_ = factory;
  816. }
  817. /**
  818. * Return whether the browser provides basic support. If this returns false,
  819. * Shaka Player cannot be used at all. In this case, do not construct a
  820. * Player instance and do not use the library.
  821. *
  822. * @return {boolean}
  823. * @export
  824. */
  825. static isBrowserSupported() {
  826. if (!window.Promise) {
  827. shaka.log.alwaysWarn('A Promise implementation or polyfill is required');
  828. }
  829. // Basic features needed for the library to be usable.
  830. const basicSupport = !!window.Promise && !!window.Uint8Array &&
  831. // eslint-disable-next-line no-restricted-syntax
  832. !!Array.prototype.forEach;
  833. if (!basicSupport) {
  834. return false;
  835. }
  836. // We do not support IE
  837. if (shaka.util.Platform.isIE()) {
  838. return false;
  839. }
  840. // We do not support iOS 9, 10, 11 or 12, nor those same versions of
  841. // desktop Safari.
  842. const safariVersion = shaka.util.Platform.safariVersion();
  843. if (safariVersion && safariVersion < 13) {
  844. return false;
  845. }
  846. // DRM support is not strictly necessary, but the APIs at least need to be
  847. // there. Our no-op DRM polyfill should handle that.
  848. // TODO(#1017): Consider making even DrmEngine optional.
  849. const drmSupport = shaka.media.DrmEngine.isBrowserSupported();
  850. if (!drmSupport) {
  851. return false;
  852. }
  853. // If we have MediaSource (MSE) support, we should be able to use Shaka.
  854. if (shaka.util.Platform.supportsMediaSource()) {
  855. return true;
  856. }
  857. // If we don't have MSE, we _may_ be able to use Shaka. Look for native HLS
  858. // support, and call this platform usable if we have it.
  859. return shaka.util.Platform.supportsMediaType('application/x-mpegurl');
  860. }
  861. /**
  862. * Probes the browser to determine what features are supported. This makes a
  863. * number of requests to EME/MSE/etc which may result in user prompts. This
  864. * should only be used for diagnostics.
  865. *
  866. * <p>
  867. * NOTE: This may show a request to the user for permission.
  868. *
  869. * @see https://bit.ly/2ywccmH
  870. * @param {boolean=} promptsOkay
  871. * @return {!Promise.<shaka.extern.SupportType>}
  872. * @export
  873. */
  874. static async probeSupport(promptsOkay=true) {
  875. goog.asserts.assert(shaka.Player.isBrowserSupported(),
  876. 'Must have basic support');
  877. let drm = {};
  878. if (promptsOkay) {
  879. drm = await shaka.media.DrmEngine.probeSupport();
  880. }
  881. const manifest = shaka.media.ManifestParser.probeSupport();
  882. const media = shaka.media.MediaSourceEngine.probeSupport();
  883. const ret = {
  884. manifest: manifest,
  885. media: media,
  886. drm: drm,
  887. };
  888. const plugins = shaka.Player.supportPlugins_;
  889. for (const name in plugins) {
  890. ret[name] = plugins[name]();
  891. }
  892. return ret;
  893. }
  894. /**
  895. * Tell the player to use <code>mediaElement</code> for all <code>load</code>
  896. * requests until <code>detach</code> or <code>destroy</code> are called.
  897. *
  898. * <p>
  899. * Calling <code>attach</code> with <code>initializedMediaSource=true</code>
  900. * will tell the player to take the initial load step and initialize media
  901. * source.
  902. *
  903. * <p>
  904. * Calls to <code>attach</code> will interrupt any in-progress calls to
  905. * <code>load</code> but cannot interrupt calls to <code>attach</code>,
  906. * <code>detach</code>, or <code>unload</code>.
  907. *
  908. * @param {!HTMLMediaElement} mediaElement
  909. * @param {boolean=} initializeMediaSource
  910. * @return {!Promise}
  911. * @export
  912. */
  913. attach(mediaElement, initializeMediaSource = true) {
  914. // Do not allow the player to be used after |destroy| is called.
  915. if (this.loadMode_ == shaka.Player.LoadMode.DESTROYED) {
  916. return Promise.reject(this.createAbortLoadError_());
  917. }
  918. const payload = shaka.Player.createEmptyPayload_();
  919. payload.mediaElement = mediaElement;
  920. // If the platform does not support media source, we will never want to
  921. // initialize media source.
  922. if (!shaka.util.Platform.supportsMediaSource()) {
  923. initializeMediaSource = false;
  924. }
  925. const destination = initializeMediaSource ?
  926. this.mediaSourceNode_ :
  927. this.attachNode_;
  928. // Do not allow this route to be interrupted because calls after this attach
  929. // call will depend on the media element being attached.
  930. const events = this.walker_.startNewRoute((currentPayload) => {
  931. return {
  932. node: destination,
  933. payload: payload,
  934. interruptible: false,
  935. };
  936. });
  937. // List to the events that can occur with our request.
  938. events.onStart = () => shaka.log.info('Starting attach...');
  939. return this.wrapWalkerListenersWithPromise_(events);
  940. }
  941. /**
  942. * Calling <code>attachCanvas</code> will tell the player to set canvas
  943. * element for LCEVC decoding.
  944. *
  945. * @param {HTMLCanvasElement} canvas
  946. * @export
  947. */
  948. attachCanvas(canvas) {
  949. this.lcevcCanvas_ = canvas;
  950. }
  951. /**
  952. * Tell the player to stop using its current media element. If the player is:
  953. * <ul>
  954. * <li>detached, this will do nothing,
  955. * <li>attached, this will release the media element,
  956. * <li>loading, this will abort loading, unload, and release the media
  957. * element,
  958. * <li>playing content, this will stop playback, unload, and release the
  959. * media element.
  960. * </ul>
  961. *
  962. * <p>
  963. * Calls to <code>detach</code> will interrupt any in-progress calls to
  964. * <code>load</code> but cannot interrupt calls to <code>attach</code>,
  965. * <code>detach</code>, or <code>unload</code>.
  966. *
  967. * @return {!Promise}
  968. * @export
  969. */
  970. detach() {
  971. // Do not allow the player to be used after |destroy| is called.
  972. if (this.loadMode_ == shaka.Player.LoadMode.DESTROYED) {
  973. return Promise.reject(this.createAbortLoadError_());
  974. }
  975. // Tell the walker to go "detached", but do not allow it to be interrupted.
  976. // If it could be interrupted it means that our media element could fall out
  977. // of sync.
  978. const events = this.walker_.startNewRoute((currentPayload) => {
  979. return {
  980. node: this.detachNode_,
  981. payload: shaka.Player.createEmptyPayload_(),
  982. interruptible: false,
  983. };
  984. });
  985. events.onStart = () => shaka.log.info('Starting detach...');
  986. return this.wrapWalkerListenersWithPromise_(events);
  987. }
  988. /**
  989. * Tell the player to either return to:
  990. * <ul>
  991. * <li>detached (when it does not have a media element),
  992. * <li>attached (when it has a media element and
  993. * <code>initializedMediaSource=false</code>)
  994. * <li>media source initialized (when it has a media element and
  995. * <code>initializedMediaSource=true</code>)
  996. * </ul>
  997. *
  998. * <p>
  999. * Calls to <code>unload</code> will interrupt any in-progress calls to
  1000. * <code>load</code> but cannot interrupt calls to <code>attach</code>,
  1001. * <code>detach</code>, or <code>unload</code>.
  1002. *
  1003. * @param {boolean=} initializeMediaSource
  1004. * @return {!Promise}
  1005. * @export
  1006. */
  1007. unload(initializeMediaSource = true) {
  1008. // Do not allow the player to be used after |destroy| is called.
  1009. if (this.loadMode_ == shaka.Player.LoadMode.DESTROYED) {
  1010. return Promise.reject(this.createAbortLoadError_());
  1011. }
  1012. this.fullyLoaded_ = false;
  1013. // If the platform does not support media source, we will never want to
  1014. // initialize media source.
  1015. if (!shaka.util.Platform.supportsMediaSource()) {
  1016. initializeMediaSource = false;
  1017. }
  1018. // If LCEVC Dil exists close it.
  1019. this.closeLcevcDil_();
  1020. // Since we are going either to attached or detached (through unloaded), we
  1021. // can't allow it to be interrupted or else we could lose track of what
  1022. // media element we are suppose to use.
  1023. //
  1024. // Using the current payload, we can determine which node we want to go to.
  1025. // If we have a media element, we want to go back to attached. If we have no
  1026. // media element, we want to go back to detached.
  1027. const payload = shaka.Player.createEmptyPayload_();
  1028. const events = this.walker_.startNewRoute((currentPayload) => {
  1029. // When someone calls |unload| we can either be before attached or
  1030. // detached (there is nothing stopping someone from calling |detach| when
  1031. // we are already detached).
  1032. //
  1033. // If we are attached to the correct element, we can tear down the
  1034. // previous playback components and go to the attached media source node
  1035. // depending on whether or not the caller wants to pre-init media source.
  1036. //
  1037. // If we don't have a media element, we assume that we are already at the
  1038. // detached node - but only the walker knows that. To ensure we are
  1039. // actually there, we tell the walker to go to detach. While this is
  1040. // technically unnecessary, it ensures that we are in the state we want
  1041. // to be in and ready for the next request.
  1042. let destination = null;
  1043. if (currentPayload.mediaElement && initializeMediaSource) {
  1044. destination = this.mediaSourceNode_;
  1045. } else if (currentPayload.mediaElement) {
  1046. destination = this.attachNode_;
  1047. } else {
  1048. destination = this.detachNode_;
  1049. }
  1050. goog.asserts.assert(destination, 'We should have picked a destination.');
  1051. // Copy over the media element because we want to keep using the same
  1052. // element - the other values don't matter.
  1053. payload.mediaElement = currentPayload.mediaElement;
  1054. return {
  1055. node: destination,
  1056. payload: payload,
  1057. interruptible: false,
  1058. };
  1059. });
  1060. events.onStart = () => shaka.log.info('Starting unload...');
  1061. return this.wrapWalkerListenersWithPromise_(events);
  1062. }
  1063. /**
  1064. * Provides a way to update the stream start position during the media loading
  1065. * process. Can for example be called from the <code>manifestparsed</code>
  1066. * event handler to update the start position based on information in the
  1067. * manifest.
  1068. *
  1069. * @param {number} startTime
  1070. * @export
  1071. */
  1072. updateStartTime(startTime) {
  1073. this.updatedStartTime_ = startTime;
  1074. }
  1075. /**
  1076. * Tell the player to load the content at <code>assetUri</code> and start
  1077. * playback at <code>startTime</code>. Before calling <code>load</code>,
  1078. * a call to <code>attach</code> must have succeeded.
  1079. *
  1080. * <p>
  1081. * Calls to <code>load</code> will interrupt any in-progress calls to
  1082. * <code>load</code> but cannot interrupt calls to <code>attach</code>,
  1083. * <code>detach</code>, or <code>unload</code>.
  1084. *
  1085. * @param {string} assetUri
  1086. * @param {?number=} startTime
  1087. * When <code>startTime</code> is <code>null</code> or
  1088. * <code>undefined</code>, playback will start at the default start time (0
  1089. * for VOD and liveEdge for LIVE).
  1090. * @param {string=} mimeType
  1091. * @return {!Promise}
  1092. * @export
  1093. */
  1094. load(assetUri, startTime, mimeType) {
  1095. this.updatedStartTime_ = null;
  1096. this.fullyLoaded_ = false;
  1097. // Do not allow the player to be used after |destroy| is called.
  1098. if (this.loadMode_ == shaka.Player.LoadMode.DESTROYED) {
  1099. return Promise.reject(this.createAbortLoadError_());
  1100. }
  1101. // We dispatch the loading event when someone calls |load| because we want
  1102. // to surface the user intent.
  1103. this.dispatchEvent(this.makeEvent_(shaka.util.FakeEvent.EventName.Loading));
  1104. // Right away we know what the asset uri and start-of-load time are. We will
  1105. // fill-in the rest of the information later.
  1106. const payload = shaka.Player.createEmptyPayload_();
  1107. payload.uri = assetUri;
  1108. payload.startTimeOfLoad = Date.now() / 1000;
  1109. if (mimeType) {
  1110. payload.mimeType = mimeType;
  1111. }
  1112. // Because we allow |startTime| to be optional, it means that it will be
  1113. // |undefined| when not provided. This means that we need to re-map
  1114. // |undefined| to |null| while preserving |0| as a meaningful value.
  1115. if (startTime !== undefined) {
  1116. payload.startTime = startTime;
  1117. }
  1118. // TODO: Refactor to determine whether it's a manifest or not, and whether
  1119. // or not we can play it. Then we could return a better error than
  1120. // UNABLE_TO_GUESS_MANIFEST_TYPE for WebM in Safari.
  1121. const useSrcEquals = this.shouldUseSrcEquals_(payload);
  1122. const destination = useSrcEquals ? this.srcEqualsNode_ : this.loadNode_;
  1123. // Allow this request to be interrupted, this will allow other requests to
  1124. // cancel a load and quickly start a new load.
  1125. const events = this.walker_.startNewRoute((currentPayload) => {
  1126. if (currentPayload.mediaElement == null) {
  1127. // Because we return null, this "new route" will not be used.
  1128. return null;
  1129. }
  1130. // Keep using whatever media element we have right now.
  1131. payload.mediaElement = currentPayload.mediaElement;
  1132. return {
  1133. node: destination,
  1134. payload: payload,
  1135. interruptible: true,
  1136. };
  1137. });
  1138. // Stats are for a single playback/load session. Stats must be initialized
  1139. // before we allow calls to |updateStateHistory|.
  1140. this.stats_ = new shaka.util.Stats();
  1141. // Load's request is a little different, so we can't use our normal
  1142. // listeners-to-promise method. It is the only request where we may skip the
  1143. // request, so we need to set the on skip callback to reject with a specific
  1144. // error.
  1145. events.onStart =
  1146. () => shaka.log.info('Starting load of ' + assetUri + '...');
  1147. return new Promise((resolve, reject) => {
  1148. events.onSkip = () => reject(new shaka.util.Error(
  1149. shaka.util.Error.Severity.CRITICAL,
  1150. shaka.util.Error.Category.PLAYER,
  1151. shaka.util.Error.Code.NO_VIDEO_ELEMENT));
  1152. events.onEnd = () => {
  1153. resolve();
  1154. // We dispatch the loaded event when the load promise is resolved
  1155. this.dispatchEvent(
  1156. this.makeEvent_(shaka.util.FakeEvent.EventName.Loaded));
  1157. };
  1158. events.onCancel = () => reject(this.createAbortLoadError_());
  1159. events.onError = (e) => reject(e);
  1160. });
  1161. }
  1162. /**
  1163. * Check if src= should be used to load the asset at |uri|. Assume that media
  1164. * source is the default option, and that src= is for special cases.
  1165. *
  1166. * @param {shaka.routing.Payload} payload
  1167. * @return {boolean}
  1168. * |true| if the content should be loaded with src=, |false| if the content
  1169. * should be loaded with MediaSource.
  1170. * @private
  1171. */
  1172. shouldUseSrcEquals_(payload) {
  1173. const Platform = shaka.util.Platform;
  1174. const MimeUtils = shaka.util.MimeUtils;
  1175. // If we are using a platform that does not support media source, we will
  1176. // fall back to src= to handle all playback.
  1177. if (!Platform.supportsMediaSource()) {
  1178. return true;
  1179. }
  1180. // The most accurate way to tell the player how to load the content is via
  1181. // MIME type. We can fall back to features of the URI if needed.
  1182. let mimeType = payload.mimeType;
  1183. const uri = payload.uri || '';
  1184. // If we don't have a MIME type, try to guess based on the file extension.
  1185. // TODO: Too generic to belong to ManifestParser now. Refactor.
  1186. if (!mimeType) {
  1187. // Try using the uri extension.
  1188. const extension = shaka.media.ManifestParser.getExtension(uri);
  1189. mimeType = shaka.Player.SRC_EQUAL_EXTENSIONS_TO_MIME_TYPES_[extension];
  1190. }
  1191. // TODO: The load graph system has a design limitation that requires routing
  1192. // destination to be chosen synchronously. This means we can only make the
  1193. // right choice about src= consistently if we have a well-known file
  1194. // extension or API-provided MIME type. Detection of MIME type from a HEAD
  1195. // request (as is done for manifest types) can't be done yet.
  1196. if (mimeType) {
  1197. // If we have a MIME type, check if the browser can play it natively.
  1198. // This will cover both single files and native HLS.
  1199. const mediaElement = payload.mediaElement || Platform.anyMediaElement();
  1200. const canPlayNatively = mediaElement.canPlayType(mimeType) != '';
  1201. // If we can't play natively, then src= isn't an option.
  1202. if (!canPlayNatively) {
  1203. return false;
  1204. }
  1205. const canPlayMediaSource =
  1206. shaka.media.ManifestParser.isSupported(uri, mimeType);
  1207. // If MediaSource isn't an option, the native option is our only chance.
  1208. if (!canPlayMediaSource) {
  1209. return true;
  1210. }
  1211. // If we land here, both are feasible.
  1212. goog.asserts.assert(canPlayNatively && canPlayMediaSource,
  1213. 'Both native and MSE playback should be possible!');
  1214. // We would prefer MediaSource in some cases, and src= in others. For
  1215. // example, Android has native HLS, but we'd prefer our own MediaSource
  1216. // version there.
  1217. // Native HLS can be preferred on any platform via this flag:
  1218. if (MimeUtils.isHlsType(mimeType) &&
  1219. this.config_.streaming.preferNativeHls) {
  1220. return true;
  1221. }
  1222. // For Safari, we have an older flag which only applies to this one
  1223. // browser:
  1224. if (Platform.isApple()) {
  1225. return this.config_.streaming.useNativeHlsOnSafari;
  1226. }
  1227. // In all other cases, we prefer MediaSource.
  1228. return false;
  1229. }
  1230. // Unless there are good reasons to use src= (single-file playback or native
  1231. // HLS), we prefer MediaSource. So the final return value for choosing src=
  1232. // is false.
  1233. return false;
  1234. }
  1235. /**
  1236. * This should only be called by the load graph when it is time to attach to
  1237. * a media element. The only times this may be called are when we are being
  1238. * asked to re-attach to the current media element, or attach to a new media
  1239. * element while not attached to a media element.
  1240. *
  1241. * This method assumes that it is safe for it to execute, the load-graph is
  1242. * responsible for ensuring all assumptions are true.
  1243. *
  1244. * Attaching to a media element is defined as:
  1245. * - Registering error listeners to the media element.
  1246. * - Caching the video element for use outside of the load graph.
  1247. *
  1248. * @param {shaka.routing.Payload} has
  1249. * @param {shaka.routing.Payload} wants
  1250. * @return {!Promise}
  1251. * @private
  1252. */
  1253. onAttach_(has, wants) {
  1254. // If we don't have a media element yet, it means we are entering
  1255. // "attach" from another node.
  1256. //
  1257. // If we have a media element, it should match |wants.mediaElement|
  1258. // because it means we are going from "attach" to "attach".
  1259. //
  1260. // These constraints should be maintained and guaranteed by the routing
  1261. // logic in |getNextStep_|.
  1262. goog.asserts.assert(
  1263. has.mediaElement == null || has.mediaElement == wants.mediaElement,
  1264. 'The routing logic failed. MediaElement requirement failed.');
  1265. if (has.mediaElement == null) {
  1266. has.mediaElement = wants.mediaElement;
  1267. const onError = (error) => this.onVideoError_(error);
  1268. this.attachEventManager_.listen(has.mediaElement, 'error', onError);
  1269. }
  1270. this.video_ = has.mediaElement;
  1271. return Promise.resolve();
  1272. }
  1273. /**
  1274. * This should only be called by the load graph when it is time to detach from
  1275. * a media element. The only times this may be called are when we are being
  1276. * asked to detach from the current media element, or detach when we are
  1277. * already detached.
  1278. *
  1279. * This method assumes that it is safe for it to execute, the load-graph is
  1280. * responsible for ensuring all assumptions are true.
  1281. *
  1282. * Detaching from a media element is defined as:
  1283. * - Removing error listeners from the media element.
  1284. * - Dropping the cached reference to the video element.
  1285. *
  1286. * @param {shaka.routing.Payload} has
  1287. * @param {shaka.routing.Payload} wants
  1288. * @return {!Promise}
  1289. * @private
  1290. */
  1291. onDetach_(has, wants) {
  1292. // If we were going from "detached" to "detached" we wouldn't have
  1293. // a media element to detach from.
  1294. if (has.mediaElement) {
  1295. this.attachEventManager_.removeAll();
  1296. has.mediaElement = null;
  1297. }
  1298. if (this.adManager_) {
  1299. // The ad manager is specific to the video, so detach it too.
  1300. this.adManager_.release();
  1301. }
  1302. // Clear our cached copy of the media element.
  1303. this.video_ = null;
  1304. return Promise.resolve();
  1305. }
  1306. /**
  1307. * This should only be called by the load graph when it is time to unload all
  1308. * currently initialized playback components. Unlike the other load actions,
  1309. * this action is built to be more general. We need to do this because we
  1310. * don't know what state the player will be in before unloading (including
  1311. * after an error occurred in the middle of a transition).
  1312. *
  1313. * This method assumes that any component could be |null| and should be safe
  1314. * to call from any point in the load graph.
  1315. *
  1316. * @param {shaka.routing.Payload} has
  1317. * @param {shaka.routing.Payload} wants
  1318. * @return {!Promise}
  1319. * @private
  1320. */
  1321. async onUnload_(has, wants) {
  1322. // Set the load mode to unload right away so that all the public methods
  1323. // will stop using the internal components. We need to make sure that we
  1324. // are not overriding the destroyed state because we will unload when we are
  1325. // destroying the player.
  1326. if (this.loadMode_ != shaka.Player.LoadMode.DESTROYED) {
  1327. this.loadMode_ = shaka.Player.LoadMode.NOT_LOADED;
  1328. }
  1329. // Run any general cleanup tasks now. This should be here at the top, right
  1330. // after setting loadMode_, so that internal components still exist as they
  1331. // did when the cleanup tasks were registered in the array.
  1332. const cleanupTasks = this.cleanupOnUnload_.map((cb) => cb());
  1333. this.cleanupOnUnload_ = [];
  1334. await Promise.all(cleanupTasks);
  1335. // Dispatch the unloading event.
  1336. this.dispatchEvent(
  1337. this.makeEvent_(shaka.util.FakeEvent.EventName.Unloading));
  1338. // Remove everything that has to do with loading content from our payload
  1339. // since we are releasing everything that depended on it.
  1340. has.mimeType = null;
  1341. has.startTime = null;
  1342. has.uri = null;
  1343. // Release the region timeline, which is created when parsing the manifest.
  1344. if (this.regionTimeline_) {
  1345. this.regionTimeline_.release();
  1346. this.regionTimeline_ = null;
  1347. }
  1348. // In most cases we should have a media element. The one exception would
  1349. // be if there was an error and we, by chance, did not have a media element.
  1350. if (has.mediaElement) {
  1351. this.loadEventManager_.removeAll();
  1352. }
  1353. // Stop the variant checker timer
  1354. this.checkVariantsTimer_.stop();
  1355. // Some observers use some playback components, shutting down the observers
  1356. // first ensures that they don't try to use the playback components
  1357. // mid-destroy.
  1358. if (this.playheadObservers_) {
  1359. this.playheadObservers_.release();
  1360. this.playheadObservers_ = null;
  1361. }
  1362. if (this.bufferPoller_) {
  1363. this.bufferPoller_.stop();
  1364. this.bufferPoller_ = null;
  1365. }
  1366. // Stop the parser early. Since it is at the start of the pipeline, it
  1367. // should be start early to avoid is pushing new data downstream.
  1368. if (this.parser_) {
  1369. await this.parser_.stop();
  1370. this.parser_ = null;
  1371. this.parserFactory_ = null;
  1372. }
  1373. // Abr Manager will tell streaming engine what to do, so we need to stop
  1374. // it before we destroy streaming engine. Unlike with the other components,
  1375. // we do not release the instance, we will reuse it in later loads.
  1376. if (this.abrManager_) {
  1377. await this.abrManager_.stop();
  1378. }
  1379. // Streaming engine will push new data to media source engine, so we need
  1380. // to shut it down before destroy media source engine.
  1381. if (this.streamingEngine_) {
  1382. await this.streamingEngine_.destroy();
  1383. this.streamingEngine_ = null;
  1384. }
  1385. if (this.playRateController_) {
  1386. this.playRateController_.release();
  1387. this.playRateController_ = null;
  1388. }
  1389. // Playhead is used by StreamingEngine, so we can't destroy this until after
  1390. // StreamingEngine has stopped.
  1391. if (this.playhead_) {
  1392. this.playhead_.release();
  1393. this.playhead_ = null;
  1394. }
  1395. // Media source engine holds onto the media element, and in order to detach
  1396. // the media keys (with drm engine), we need to break the connection between
  1397. // media source engine and the media element.
  1398. if (this.mediaSourceEngine_) {
  1399. await this.mediaSourceEngine_.destroy();
  1400. this.mediaSourceEngine_ = null;
  1401. }
  1402. if (this.adManager_) {
  1403. this.adManager_.onAssetUnload();
  1404. }
  1405. // In order to unload a media element, we need to remove the src attribute
  1406. // and then load again. When we destroy media source engine, this will be
  1407. // done for us, but for src=, we need to do it here.
  1408. //
  1409. // DrmEngine requires this to be done before we destroy DrmEngine itself.
  1410. if (has.mediaElement && has.mediaElement.src) {
  1411. // TODO: Investigate this more. Only reproduces on Firefox 69.
  1412. // Introduce a delay before detaching the video source. We are seeing
  1413. // spurious Promise rejections involving an AbortError in our tests
  1414. // otherwise.
  1415. await new Promise(
  1416. (resolve) => new shaka.util.Timer(resolve).tickAfter(0.1));
  1417. has.mediaElement.removeAttribute('src');
  1418. has.mediaElement.load();
  1419. // Remove all track nodes
  1420. while (has.mediaElement.lastChild) {
  1421. has.mediaElement.removeChild(has.mediaElement.firstChild);
  1422. }
  1423. }
  1424. if (this.drmEngine_) {
  1425. await this.drmEngine_.destroy();
  1426. this.drmEngine_ = null;
  1427. }
  1428. this.assetUri_ = null;
  1429. this.bufferObserver_ = null;
  1430. if (this.manifest_) {
  1431. for (const variant of this.manifest_.variants) {
  1432. for (const stream of [variant.audio, variant.video]) {
  1433. if (stream && stream.segmentIndex) {
  1434. stream.segmentIndex.release();
  1435. }
  1436. }
  1437. }
  1438. for (const stream of this.manifest_.textStreams) {
  1439. if (stream.segmentIndex) {
  1440. stream.segmentIndex.release();
  1441. }
  1442. }
  1443. }
  1444. this.manifest_ = null;
  1445. this.stats_ = new shaka.util.Stats(); // Replace with a clean stats object.
  1446. this.lastTextFactory_ = null;
  1447. // Make sure that the app knows of the new buffering state.
  1448. this.updateBufferState_();
  1449. }
  1450. /**
  1451. * This should only be called by the load graph when it is time to initialize
  1452. * media source engine. The only time this may be called is when we are
  1453. * attached to the same media element as in the request.
  1454. *
  1455. * This method assumes that it is safe for it to execute. The load-graph is
  1456. * responsible for ensuring all assumptions are true.
  1457. *
  1458. * @param {shaka.routing.Payload} has
  1459. * @param {shaka.routing.Payload} wants
  1460. *
  1461. * @return {!Promise}
  1462. * @private
  1463. */
  1464. async onInitializeMediaSourceEngine_(has, wants) {
  1465. goog.asserts.assert(
  1466. shaka.util.Platform.supportsMediaSource(),
  1467. 'We should not be initializing media source on a platform that does ' +
  1468. 'not support media source.');
  1469. goog.asserts.assert(
  1470. has.mediaElement,
  1471. 'We should have a media element when initializing media source.');
  1472. goog.asserts.assert(
  1473. has.mediaElement == wants.mediaElement,
  1474. '|has| and |wants| should have the same media element when ' +
  1475. 'initializing media source.');
  1476. goog.asserts.assert(
  1477. this.mediaSourceEngine_ == null,
  1478. 'We should not have a media source engine yet.');
  1479. // When changing text visibility we need to update both the text displayer
  1480. // and streaming engine because we don't always stream text. To ensure that
  1481. // text displayer and streaming engine are always in sync, wait until they
  1482. // are both initialized before setting the initial value.
  1483. const textDisplayerFactory = this.config_.textDisplayFactory;
  1484. const textDisplayer = textDisplayerFactory();
  1485. this.lastTextFactory_ = textDisplayerFactory;
  1486. const mediaSourceEngine = this.createMediaSourceEngine(
  1487. has.mediaElement,
  1488. textDisplayer,
  1489. (metadata, offset, endTime) => {
  1490. this.processTimedMetadataMediaSrc_(metadata, offset, endTime);
  1491. },
  1492. this.lcevcDil_);
  1493. mediaSourceEngine.configure(this.config_.mediaSource);
  1494. const {segmentRelativeVttTiming} = this.config_.manifest;
  1495. mediaSourceEngine.setSegmentRelativeVttTiming(segmentRelativeVttTiming);
  1496. // Wait for media source engine to finish opening. This promise should
  1497. // NEVER be rejected as per the media source engine implementation.
  1498. await mediaSourceEngine.open();
  1499. // Wait until it is ready to actually store the reference.
  1500. this.mediaSourceEngine_ = mediaSourceEngine;
  1501. }
  1502. /**
  1503. * Create the parser for the asset located at |wants.uri|. This should only be
  1504. * called as part of the load graph.
  1505. *
  1506. * This method assumes that it is safe for it to execute, the load-graph is
  1507. * responsible for ensuring all assumptions are true.
  1508. *
  1509. * @param {shaka.routing.Payload} has
  1510. * @param {shaka.routing.Payload} wants
  1511. * @return {!Promise}
  1512. * @private
  1513. */
  1514. async onInitializeParser_(has, wants) {
  1515. goog.asserts.assert(
  1516. has.mediaElement,
  1517. 'We should have a media element when initializing the parser.');
  1518. goog.asserts.assert(
  1519. has.mediaElement == wants.mediaElement,
  1520. '|has| and |wants| should have the same media element when ' +
  1521. 'initializing the parser.');
  1522. goog.asserts.assert(
  1523. this.networkingEngine_,
  1524. 'Need networking engine when initializing the parser.');
  1525. goog.asserts.assert(
  1526. this.config_,
  1527. 'Need player config when initializing the parser.');
  1528. // We are going to "lock-in" the mime type and uri since they are
  1529. // what we are going to use to create our parser and parse the manifest.
  1530. has.mimeType = wants.mimeType;
  1531. has.uri = wants.uri;
  1532. goog.asserts.assert(
  1533. has.uri,
  1534. 'We should have an asset uri when initializing the parsing.');
  1535. // Store references to things we asserted so that we don't need to reassert
  1536. // them again later.
  1537. const assetUri = has.uri;
  1538. const networkingEngine = this.networkingEngine_;
  1539. // Save the uri so that it can be used outside of the load-graph.
  1540. this.assetUri_ = assetUri;
  1541. // Create the parser that we will use to parse the manifest.
  1542. this.parserFactory_ = await shaka.media.ManifestParser.getFactory(
  1543. assetUri,
  1544. networkingEngine,
  1545. this.config_.manifest.retryParameters,
  1546. has.mimeType);
  1547. goog.asserts.assert(this.parserFactory_, 'Must have manifest parser');
  1548. this.parser_ = this.parserFactory_();
  1549. const manifestConfig =
  1550. shaka.util.ObjectUtils.cloneObject(this.config_.manifest);
  1551. // Don't read video segments if the player is attached to an audio element
  1552. if (wants.mediaElement && wants.mediaElement.nodeName === 'AUDIO') {
  1553. manifestConfig.disableVideo = true;
  1554. }
  1555. this.parser_.configure(manifestConfig);
  1556. }
  1557. /**
  1558. * Parse the manifest at |has.uri| using the parser that should have already
  1559. * been created. This should only be called as part of the load graph.
  1560. *
  1561. * This method assumes that it is safe for it to execute, the load-graph is
  1562. * responsible for ensuring all assumptions are true.
  1563. *
  1564. * @param {shaka.routing.Payload} has
  1565. * @param {shaka.routing.Payload} wants
  1566. * @return {!shaka.util.AbortableOperation}
  1567. * @private
  1568. */
  1569. onParseManifest_(has, wants) {
  1570. goog.asserts.assert(
  1571. has.mimeType == wants.mimeType,
  1572. '|has| and |wants| should have the same mime type when parsing.');
  1573. goog.asserts.assert(
  1574. has.uri == wants.uri,
  1575. '|has| and |wants| should have the same uri when parsing.');
  1576. goog.asserts.assert(
  1577. has.uri,
  1578. '|has| should have a valid uri when parsing.');
  1579. goog.asserts.assert(
  1580. has.uri == this.assetUri_,
  1581. '|has.uri| should match the cached asset uri.');
  1582. goog.asserts.assert(
  1583. this.networkingEngine_,
  1584. 'Need networking engine to parse manifest.');
  1585. goog.asserts.assert(
  1586. this.cmcdManager_,
  1587. 'Need CMCD manager to populate manifest request data.');
  1588. goog.asserts.assert(
  1589. this.config_,
  1590. 'Need player config to parse manifest.');
  1591. goog.asserts.assert(
  1592. this.parser_,
  1593. '|this.parser_| should have been set in an earlier step.');
  1594. // Store references to things we asserted so that we don't need to reassert
  1595. // them again later.
  1596. const assetUri = has.uri;
  1597. const networkingEngine = this.networkingEngine_;
  1598. // This will be needed by the parser once it starts parsing, so we will
  1599. // initialize it now even through it appears a little out-of-place.
  1600. this.regionTimeline_ =
  1601. new shaka.media.RegionTimeline(() => this.seekRange());
  1602. this.regionTimeline_.addEventListener('regionadd', (event) => {
  1603. /** @type {shaka.extern.TimelineRegionInfo} */
  1604. const region = event['region'];
  1605. this.onRegionEvent_(
  1606. shaka.util.FakeEvent.EventName.TimelineRegionAdded, region);
  1607. if (this.adManager_) {
  1608. this.adManager_.onDashTimedMetadata(region);
  1609. }
  1610. });
  1611. this.qualityObserver_ = null;
  1612. if (this.config_.streaming.observeQualityChanges) {
  1613. this.qualityObserver_ = new shaka.media.QualityObserver(
  1614. () => this.getBufferedInfo());
  1615. this.qualityObserver_.addEventListener('qualitychange', (event) => {
  1616. /** @type {shaka.extern.MediaQualityInfo} */
  1617. const mediaQualityInfo = event['quality'];
  1618. /** @type {number} */
  1619. const position = event['position'];
  1620. this.onMediaQualityChange_(mediaQualityInfo, position);
  1621. });
  1622. }
  1623. const playerInterface = {
  1624. networkingEngine: networkingEngine,
  1625. filter: (manifest) => this.filterManifest_(manifest),
  1626. makeTextStreamsForClosedCaptions: (manifest) => {
  1627. return this.makeTextStreamsForClosedCaptions_(manifest);
  1628. },
  1629. // Called when the parser finds a timeline region. This can be called
  1630. // before we start playback or during playback (live/in-progress
  1631. // manifest).
  1632. onTimelineRegionAdded: (region) => this.regionTimeline_.addRegion(region),
  1633. onEvent: (event) => this.dispatchEvent(event),
  1634. onError: (error) => this.onError_(error),
  1635. isLowLatencyMode: () => this.isLowLatencyMode_(),
  1636. isAutoLowLatencyMode: () => this.isAutoLowLatencyMode_(),
  1637. enableLowLatencyMode: () => {
  1638. this.configure('streaming.lowLatencyMode', true);
  1639. },
  1640. updateDuration: () => {
  1641. if (this.streamingEngine_) {
  1642. this.streamingEngine_.updateDuration();
  1643. }
  1644. },
  1645. newDrmInfo: (stream) => {
  1646. // We may need to create new sessions for any new init data.
  1647. const currentDrmInfo =
  1648. this.drmEngine_ ? this.drmEngine_.getDrmInfo() : null;
  1649. // DrmEngine.newInitData() requires mediaKeys to be available.
  1650. if (currentDrmInfo && this.drmEngine_.getMediaKeys()) {
  1651. this.processDrmInfos_(currentDrmInfo.keySystem, stream);
  1652. }
  1653. },
  1654. };
  1655. const startTime = Date.now() / 1000;
  1656. return new shaka.util.AbortableOperation(/* promise= */ (async () => {
  1657. this.manifest_ = await this.parser_.start(assetUri, playerInterface);
  1658. // This event is fired after the manifest is parsed, but before any
  1659. // filtering takes place.
  1660. const event =
  1661. this.makeEvent_(shaka.util.FakeEvent.EventName.ManifestParsed);
  1662. this.dispatchEvent(event);
  1663. // We require all manifests to have at least one variant.
  1664. if (this.manifest_.variants.length == 0) {
  1665. throw new shaka.util.Error(
  1666. shaka.util.Error.Severity.CRITICAL,
  1667. shaka.util.Error.Category.MANIFEST,
  1668. shaka.util.Error.Code.NO_VARIANTS);
  1669. }
  1670. // Make sure that all variants are either: audio-only, video-only, or
  1671. // audio-video.
  1672. shaka.Player.filterForAVVariants_(this.manifest_);
  1673. const now = Date.now() / 1000;
  1674. const delta = now - startTime;
  1675. this.stats_.setManifestTime(delta);
  1676. })(), /* onAbort= */ () => {
  1677. shaka.log.info('Aborting parser step...');
  1678. return this.parser_.stop();
  1679. });
  1680. }
  1681. /**
  1682. * This should only be called by the load graph when it is time to initialize
  1683. * drmEngine. The only time this may be called is when we are attached a
  1684. * media element and have parsed a manifest.
  1685. *
  1686. * The load-graph is responsible for ensuring all assumptions made by this
  1687. * method are valid before executing it.
  1688. *
  1689. * @param {shaka.routing.Payload} has
  1690. * @param {shaka.routing.Payload} wants
  1691. * @return {!Promise}
  1692. * @private
  1693. */
  1694. async onInitializeDrm_(has, wants) {
  1695. goog.asserts.assert(
  1696. has.mimeType == wants.mimeType,
  1697. 'The load graph should have ensured the mime types matched.');
  1698. goog.asserts.assert(
  1699. has.uri == wants.uri,
  1700. 'The load graph should have ensured the uris matched');
  1701. goog.asserts.assert(
  1702. this.networkingEngine_,
  1703. '|onInitializeDrm_| should never be called after |destroy|');
  1704. goog.asserts.assert(
  1705. this.config_,
  1706. '|onInitializeDrm_| should never be called after |destroy|');
  1707. goog.asserts.assert(
  1708. this.manifest_,
  1709. '|this.manifest_| should have been set in an earlier step.');
  1710. goog.asserts.assert(
  1711. has.mediaElement,
  1712. 'We should have a media element when initializing the DRM Engine.');
  1713. const startTime = Date.now() / 1000;
  1714. let firstEvent = true;
  1715. this.drmEngine_ = this.createDrmEngine({
  1716. netEngine: this.networkingEngine_,
  1717. onError: (e) => {
  1718. this.onError_(e);
  1719. },
  1720. onKeyStatus: (map) => {
  1721. this.onKeyStatus_(map);
  1722. },
  1723. onExpirationUpdated: (id, expiration) => {
  1724. this.onExpirationUpdated_(id, expiration);
  1725. },
  1726. onEvent: (e) => {
  1727. this.dispatchEvent(e);
  1728. if (e.type == shaka.util.FakeEvent.EventName.DrmSessionUpdate &&
  1729. firstEvent) {
  1730. firstEvent = false;
  1731. const now = Date.now() / 1000;
  1732. const delta = now - startTime;
  1733. this.stats_.setDrmTime(delta);
  1734. // LCEVC data by itself is not encrypted in DRM protected streams and
  1735. // can therefore be accessed and decoded as normal. However, the LCEVC
  1736. // decoder needs access to the VideoElement output in order to apply
  1737. // the enhancement. In DRM contexts where the browser CDM restricts
  1738. // access from our decoder, the enhancement cannot be applied and
  1739. // therefore the LCEVC output canvas is hidden accordingly.
  1740. if (this.lcevcDil_) {
  1741. this.lcevcDil_.hideCanvas();
  1742. }
  1743. }
  1744. },
  1745. });
  1746. this.drmEngine_.configure(this.config_.drm);
  1747. await this.drmEngine_.initForPlayback(
  1748. this.manifest_.variants,
  1749. this.manifest_.offlineSessionIds);
  1750. await this.drmEngine_.attach(has.mediaElement);
  1751. // Now that we have drm information, filter the manifest (again) so that we
  1752. // can ensure we only use variants with the selected key system.
  1753. await this.filterManifest_(this.manifest_);
  1754. }
  1755. /**
  1756. * This should only be called by the load graph when it is time to load all
  1757. * playback components needed for playback. The only times this may be called
  1758. * is when we are attached to the same media element as in the request.
  1759. *
  1760. * This method assumes that it is safe for it to execute, the load-graph is
  1761. * responsible for ensuring all assumptions are true.
  1762. *
  1763. * Loading is defined as:
  1764. * - Attaching all playback-related listeners to the media element
  1765. * - Initializing playback and observers
  1766. * - Initializing ABR Manager
  1767. * - Initializing Streaming Engine
  1768. * - Starting playback at |wants.startTime|
  1769. *
  1770. * @param {shaka.routing.Payload} has
  1771. * @param {shaka.routing.Payload} wants
  1772. * @private
  1773. */
  1774. async onLoad_(has, wants) {
  1775. goog.asserts.assert(
  1776. has.mimeType == wants.mimeType,
  1777. '|has| and |wants| should have the same mime type when loading.');
  1778. goog.asserts.assert(
  1779. has.uri == wants.uri,
  1780. '|has| and |wants| should have the same uri when loading.');
  1781. goog.asserts.assert(
  1782. has.mediaElement,
  1783. 'We should have a media element when loading.');
  1784. goog.asserts.assert(
  1785. !isNaN(wants.startTimeOfLoad),
  1786. '|wants| should tell us when the load was originally requested');
  1787. // Since we are about to start playback, we will lock in the start time as
  1788. // something we are now depending on.
  1789. has.startTime = wants.startTime;
  1790. // If updateStartTime() has been called since load() was invoked use the
  1791. // requested startTime
  1792. if (this.updatedStartTime_ != null) {
  1793. has.startTime = this.updatedStartTime_;
  1794. this.updatedStartTime_ = null;
  1795. }
  1796. // Store a reference to values in |has| after asserting so that closure will
  1797. // know that they will still be non-null between calls to await.
  1798. const mediaElement = has.mediaElement;
  1799. const assetUri = has.uri;
  1800. // Save the uri so that it can be used outside of the load-graph.
  1801. this.assetUri_ = assetUri;
  1802. this.playRateController_ = new shaka.media.PlayRateController({
  1803. getRate: () => mediaElement.playbackRate,
  1804. getDefaultRate: () => mediaElement.defaultPlaybackRate,
  1805. setRate: (rate) => { mediaElement.playbackRate = rate; },
  1806. movePlayhead: (delta) => { mediaElement.currentTime += delta; },
  1807. });
  1808. const updateStateHistory = () => this.updateStateHistory_();
  1809. const onRateChange = () => this.onRateChange_();
  1810. this.loadEventManager_.listen(mediaElement, 'playing', updateStateHistory);
  1811. this.loadEventManager_.listen(mediaElement, 'pause', updateStateHistory);
  1812. this.loadEventManager_.listen(mediaElement, 'ended', updateStateHistory);
  1813. this.loadEventManager_.listen(mediaElement, 'ratechange', onRateChange);
  1814. // Check status of the LCEVC DIL Object and reset or
  1815. // create or close based on config
  1816. this.setupLcevc_(this.config_);
  1817. const abrFactory = this.config_.abrFactory;
  1818. if (!this.abrManager_ || this.abrManagerFactory_ != abrFactory) {
  1819. this.abrManagerFactory_ = abrFactory;
  1820. this.abrManager_ = abrFactory();
  1821. if (typeof this.abrManager_.setMediaElement != 'function') {
  1822. shaka.Deprecate.deprecateFeature(5,
  1823. 'AbrManager',
  1824. 'Please use an AbrManager with setMediaElement function.');
  1825. this.abrManager_.setMediaElement = () => {};
  1826. }
  1827. this.abrManager_.configure(this.config_.abr);
  1828. }
  1829. // Copy preferred languages from the config again, in case the config was
  1830. // changed between construction and playback.
  1831. this.currentAdaptationSetCriteria_ =
  1832. new shaka.media.PreferenceBasedCriteria(
  1833. this.config_.preferredAudioLanguage,
  1834. this.config_.preferredVariantRole,
  1835. this.config_.preferredAudioChannelCount,
  1836. this.config_.preferredVideoHdrLevel,
  1837. this.config_.preferredAudioLabel);
  1838. this.currentTextLanguage_ = this.config_.preferredTextLanguage;
  1839. this.currentTextRole_ = this.config_.preferredTextRole;
  1840. this.currentTextForced_ = this.config_.preferForcedSubs;
  1841. shaka.Player.applyPlayRange_(this.manifest_.presentationTimeline,
  1842. this.config_.playRangeStart,
  1843. this.config_.playRangeEnd);
  1844. this.abrManager_.init((variant, clearBuffer, safeMargin) => {
  1845. return this.switch_(variant, clearBuffer, safeMargin);
  1846. });
  1847. this.abrManager_.setMediaElement(mediaElement);
  1848. // If the content is multi-codec and the browser can play more than one of
  1849. // them, choose codecs now before we initialize streaming.
  1850. shaka.util.StreamUtils.chooseCodecsAndFilterManifest(
  1851. this.manifest_,
  1852. this.config_.preferredVideoCodecs,
  1853. this.config_.preferredAudioCodecs,
  1854. this.config_.preferredAudioChannelCount,
  1855. this.config_.preferredDecodingAttributes);
  1856. this.streamingEngine_ = this.createStreamingEngine();
  1857. this.streamingEngine_.configure(this.config_.streaming);
  1858. // Set the load mode to "loaded with media source" as late as possible so
  1859. // that public methods won't try to access internal components until
  1860. // they're all initialized. We MUST switch to loaded before calling
  1861. // "streaming" so that they can access internal information.
  1862. this.loadMode_ = shaka.Player.LoadMode.MEDIA_SOURCE;
  1863. if (mediaElement.textTracks) {
  1864. this.loadEventManager_.listen(
  1865. mediaElement.textTracks, 'addtrack', (e) => {
  1866. const trackEvent = /** @type {!TrackEvent} */(e);
  1867. if (trackEvent.track) {
  1868. const track = trackEvent.track;
  1869. goog.asserts.assert(
  1870. track instanceof TextTrack, 'Wrong track type!');
  1871. switch (track.kind) {
  1872. case 'chapters':
  1873. this.activateChaptersTrack_(track);
  1874. break;
  1875. }
  1876. }
  1877. });
  1878. }
  1879. // The event must be fired after we filter by restrictions but before the
  1880. // active stream is picked to allow those listening for the "streaming"
  1881. // event to make changes before streaming starts.
  1882. this.dispatchEvent(
  1883. this.makeEvent_(shaka.util.FakeEvent.EventName.Streaming));
  1884. // Pick the initial streams to play.
  1885. // Unless the user has already picked a variant, anyway, by calling
  1886. // selectVariantTrack before this loading stage.
  1887. let initialVariant = null;
  1888. const activeVariant = this.streamingEngine_.getCurrentVariant();
  1889. if (!activeVariant) {
  1890. initialVariant = this.chooseVariant_();
  1891. goog.asserts.assert(initialVariant, 'Must choose an initial variant!');
  1892. }
  1893. // Lazy-load the stream, so we will have enough info to make the playhead.
  1894. const createSegmentIndexPromises = [];
  1895. const toLazyLoad = activeVariant || initialVariant;
  1896. for (const stream of [toLazyLoad.video, toLazyLoad.audio]) {
  1897. if (stream && !stream.segmentIndex) {
  1898. createSegmentIndexPromises.push(stream.createSegmentIndex());
  1899. }
  1900. }
  1901. if (createSegmentIndexPromises.length > 0) {
  1902. await Promise.all(createSegmentIndexPromises);
  1903. }
  1904. shaka.Player.applyPlayRange_(this.manifest_.presentationTimeline,
  1905. this.config_.playRangeStart,
  1906. this.config_.playRangeEnd);
  1907. this.playhead_ = this.createPlayhead(has.startTime);
  1908. this.playheadObservers_ =
  1909. this.createPlayheadObserversForMSE_(has.startTimeOfLoad);
  1910. // We need to start the buffer management code near the end because it will
  1911. // set the initial buffering state and that depends on other components
  1912. // being initialized.
  1913. const rebufferThreshold = Math.max(
  1914. this.manifest_.minBufferTime, this.config_.streaming.rebufferingGoal);
  1915. this.startBufferManagement_(rebufferThreshold);
  1916. // Now we can switch to the initial variant.
  1917. if (!activeVariant) {
  1918. goog.asserts.assert(initialVariant,
  1919. 'Must have choosen an initial variant!');
  1920. this.switchVariant_(initialVariant, /* fromAdaptation= */ true,
  1921. /* clearBuffer= */ false, /* safeMargin= */ 0);
  1922. // Now that we have initial streams, we may adjust the start time to align
  1923. // to a segment boundary.
  1924. if (this.config_.streaming.startAtSegmentBoundary) {
  1925. const startTime = this.playhead_.getTime();
  1926. const adjustedTime =
  1927. await this.adjustStartTime_(initialVariant, startTime);
  1928. this.playhead_.setStartTime(adjustedTime);
  1929. }
  1930. }
  1931. this.playhead_.ready();
  1932. // Decide if text should be shown automatically.
  1933. // similar to video/audio track, we would skip switch initial text track
  1934. // if user already pick text track (via selectTextTrack api)
  1935. const activeTextTrack = this.getTextTracks().find((t) => t.active);
  1936. if (!activeTextTrack) {
  1937. const initialTextStream = this.chooseTextStream_();
  1938. if (initialTextStream) {
  1939. this.addTextStreamToSwitchHistory_(
  1940. initialTextStream, /* fromAdaptation= */ true);
  1941. }
  1942. if (initialVariant) {
  1943. this.setInitialTextState_(initialVariant, initialTextStream);
  1944. }
  1945. // Don't initialize with a text stream unless we should be streaming text.
  1946. if (initialTextStream && this.shouldStreamText_()) {
  1947. this.streamingEngine_.switchTextStream(initialTextStream);
  1948. }
  1949. }
  1950. // Start streaming content. This will start the flow of content down to
  1951. // media source.
  1952. await this.streamingEngine_.start();
  1953. if (this.config_.abr.enabled) {
  1954. this.abrManager_.enable();
  1955. this.onAbrStatusChanged_();
  1956. }
  1957. // Re-filter the manifest after streams have been chosen.
  1958. this.filterManifestByCurrentVariant_();
  1959. // Dispatch a 'trackschanged' event now that all initial filtering is done.
  1960. this.onTracksChanged_();
  1961. // Now that we've filtered out variants that aren't compatible with the
  1962. // active one, update abr manager with filtered variants.
  1963. // NOTE: This may be unnecessary. We've already chosen one codec in
  1964. // chooseCodecsAndFilterManifest_ before we started streaming. But it
  1965. // doesn't hurt, and this will all change when we start using
  1966. // MediaCapabilities and codec switching.
  1967. // TODO(#1391): Re-evaluate with MediaCapabilities and codec switching.
  1968. this.updateAbrManagerVariants_();
  1969. const hasPrimary = this.manifest_.variants.some((v) => v.primary);
  1970. if (!this.config_.preferredAudioLanguage && !hasPrimary) {
  1971. shaka.log.warning('No preferred audio language set. We have chosen an ' +
  1972. 'arbitrary language initially');
  1973. }
  1974. if (this.isLive() && (this.config_.streaming.liveSync ||
  1975. this.manifest_.serviceDescription)) {
  1976. const onTimeUpdate = () => this.onTimeUpdate_();
  1977. this.loadEventManager_.listen(mediaElement, 'timeupdate', onTimeUpdate);
  1978. }
  1979. this.fullyLoaded_ = true;
  1980. // Wait for the 'loadedmetadata' event to measure load() latency.
  1981. this.loadEventManager_.listenOnce(mediaElement, 'loadedmetadata', () => {
  1982. const now = Date.now() / 1000;
  1983. const delta = now - wants.startTimeOfLoad;
  1984. this.stats_.setLoadLatency(delta);
  1985. });
  1986. }
  1987. /**
  1988. * This should only be called by the load graph when it is time to initialize
  1989. * drmEngine for src= playbacks.
  1990. *
  1991. * The load-graph is responsible for ensuring all assumptions made by this
  1992. * method are valid before executing it.
  1993. *
  1994. * @param {shaka.routing.Payload} has
  1995. * @param {shaka.routing.Payload} wants
  1996. * @return {!Promise}
  1997. * @private
  1998. */
  1999. async onInitializeSrcEqualsDrm_(has, wants) {
  2000. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  2001. goog.asserts.assert(
  2002. this.networkingEngine_,
  2003. '|onInitializeSrcEqualsDrm_| should never be called after |destroy|');
  2004. goog.asserts.assert(
  2005. this.config_,
  2006. '|onInitializeSrcEqualsDrm_| should never be called after |destroy|');
  2007. const startTime = Date.now() / 1000;
  2008. let firstEvent = true;
  2009. this.drmEngine_ = this.createDrmEngine({
  2010. netEngine: this.networkingEngine_,
  2011. onError: (e) => {
  2012. this.onError_(e);
  2013. },
  2014. onKeyStatus: (map) => {
  2015. this.onKeyStatus_(map);
  2016. },
  2017. onExpirationUpdated: (id, expiration) => {
  2018. this.onExpirationUpdated_(id, expiration);
  2019. },
  2020. onEvent: (e) => {
  2021. this.dispatchEvent(e);
  2022. if (e.type == shaka.util.FakeEvent.EventName.DrmSessionUpdate &&
  2023. firstEvent) {
  2024. firstEvent = false;
  2025. const now = Date.now() / 1000;
  2026. const delta = now - startTime;
  2027. this.stats_.setDrmTime(delta);
  2028. }
  2029. },
  2030. });
  2031. this.drmEngine_.configure(this.config_.drm);
  2032. const uri = wants.uri || '';
  2033. const extension = shaka.media.ManifestParser.getExtension(uri);
  2034. let mimeType = shaka.Player.SRC_EQUAL_EXTENSIONS_TO_MIME_TYPES_[extension];
  2035. if (mimeType == 'application/x-mpegurl' && shaka.util.Platform.isApple()) {
  2036. mimeType = 'application/vnd.apple.mpegurl';
  2037. }
  2038. if (!mimeType) {
  2039. mimeType = 'video/mp4';
  2040. }
  2041. // TODO: Instead of feeding DrmEngine with Variants, we should refactor
  2042. // DrmEngine so that it takes a minimal config derived from Variants. In
  2043. // cases like this one or in removal of stored content, the details are
  2044. // largely unimportant. We should have a saner way to initialize DrmEngine.
  2045. // That would also insulate DrmEngine from manifest changes in the future.
  2046. // For now, that is time-consuming and this synthetic Variant is easy, so
  2047. // I'm putting it off. Since this is only expected to be used for native
  2048. // HLS in Safari, this should be safe. -JCP
  2049. /** @type {shaka.extern.Variant} */
  2050. const variant = {
  2051. id: 0,
  2052. language: 'und',
  2053. disabledUntilTime: 0,
  2054. primary: false,
  2055. audio: null,
  2056. video: {
  2057. id: 0,
  2058. originalId: null,
  2059. createSegmentIndex: () => Promise.resolve(),
  2060. segmentIndex: null,
  2061. mimeType: wants.mimeType ?
  2062. shaka.util.MimeUtils.getBasicType(wants.mimeType) : mimeType,
  2063. codecs: wants.mimeType ?
  2064. shaka.util.MimeUtils.getCodecs(wants.mimeType) : '',
  2065. encrypted: true,
  2066. drmInfos: [], // Filled in by DrmEngine config.
  2067. keyIds: new Set(),
  2068. language: 'und',
  2069. originalLanguage: null,
  2070. label: null,
  2071. type: ContentType.VIDEO,
  2072. primary: false,
  2073. trickModeVideo: null,
  2074. emsgSchemeIdUris: null,
  2075. roles: [],
  2076. forced: false,
  2077. channelsCount: null,
  2078. audioSamplingRate: null,
  2079. spatialAudio: false,
  2080. closedCaptions: null,
  2081. accessibilityPurpose: null,
  2082. external: false,
  2083. },
  2084. bandwidth: 100,
  2085. allowedByApplication: true,
  2086. allowedByKeySystem: true,
  2087. decodingInfos: [],
  2088. };
  2089. this.drmEngine_.setSrcEquals(/* srcEquals= */ true);
  2090. await this.drmEngine_.initForPlayback(
  2091. [variant], /* offlineSessionIds= */ []);
  2092. await this.drmEngine_.attach(has.mediaElement);
  2093. }
  2094. /**
  2095. * This should only be called by the load graph when it is time to set-up the
  2096. * media element to play content using src=. The only times this may be called
  2097. * is when we are attached to the same media element as in the request.
  2098. *
  2099. * This method assumes that it is safe for it to execute, the load-graph is
  2100. * responsible for ensuring all assumptions are true.
  2101. *
  2102. * @param {shaka.routing.Payload} has
  2103. * @param {shaka.routing.Payload} wants
  2104. * @return {!shaka.util.AbortableOperation}
  2105. *
  2106. * @private
  2107. */
  2108. onSrcEquals_(has, wants) {
  2109. goog.asserts.assert(
  2110. has.mediaElement,
  2111. 'We should have a media element when loading.');
  2112. goog.asserts.assert(
  2113. wants.uri,
  2114. '|has| should have a valid uri when loading.');
  2115. goog.asserts.assert(
  2116. !isNaN(wants.startTimeOfLoad),
  2117. '|wants| should tell us when the load was originally requested');
  2118. goog.asserts.assert(
  2119. this.video_ == has.mediaElement,
  2120. 'The video element should match our media element');
  2121. // Lock-in the values that we are using so that the routing logic knows what
  2122. // we have.
  2123. has.uri = wants.uri;
  2124. has.startTime = wants.startTime;
  2125. // Save the uri so that it can be used outside of the load-graph.
  2126. this.assetUri_ = has.uri;
  2127. const mediaElement = has.mediaElement;
  2128. this.playhead_ = new shaka.media.SrcEqualsPlayhead(mediaElement);
  2129. // This flag is used below in the language preference setup to check if
  2130. // this load was canceled before the necessary awaits completed.
  2131. let unloaded = false;
  2132. this.cleanupOnUnload_.push(() => {
  2133. unloaded = true;
  2134. });
  2135. if (has.startTime != null) {
  2136. this.playhead_.setStartTime(has.startTime);
  2137. }
  2138. this.playRateController_ = new shaka.media.PlayRateController({
  2139. getRate: () => mediaElement.playbackRate,
  2140. getDefaultRate: () => mediaElement.defaultPlaybackRate,
  2141. setRate: (rate) => { mediaElement.playbackRate = rate; },
  2142. movePlayhead: (delta) => { mediaElement.currentTime += delta; },
  2143. });
  2144. // We need to start the buffer management code near the end because it will
  2145. // set the initial buffering state and that depends on other components
  2146. // being initialized.
  2147. const rebufferThreshold = this.config_.streaming.rebufferingGoal;
  2148. this.startBufferManagement_(rebufferThreshold);
  2149. // Add all media element listeners.
  2150. const updateStateHistory = () => this.updateStateHistory_();
  2151. const onRateChange = () => this.onRateChange_();
  2152. this.loadEventManager_.listen(mediaElement, 'playing', updateStateHistory);
  2153. this.loadEventManager_.listen(mediaElement, 'pause', updateStateHistory);
  2154. this.loadEventManager_.listen(mediaElement, 'ended', updateStateHistory);
  2155. this.loadEventManager_.listen(mediaElement, 'ratechange', onRateChange);
  2156. // Wait for the 'loadedmetadata' event to measure load() latency, but only
  2157. // if preload is set in a way that would result in this event firing
  2158. // automatically.
  2159. // See https://github.com/shaka-project/shaka-player/issues/2483
  2160. if (mediaElement.preload != 'none') {
  2161. this.loadEventManager_.listenOnce(mediaElement, 'loadedmetadata', () => {
  2162. const now = Date.now() / 1000;
  2163. const delta = now - wants.startTimeOfLoad;
  2164. this.stats_.setLoadLatency(delta);
  2165. });
  2166. }
  2167. // The audio tracks are only available on Safari at the moment, but this
  2168. // drives the tracks API for Safari's native HLS. So when they change,
  2169. // fire the corresponding Shaka Player event.
  2170. if (mediaElement.audioTracks) {
  2171. this.loadEventManager_.listen(
  2172. mediaElement.audioTracks, 'addtrack', () => this.onTracksChanged_());
  2173. this.loadEventManager_.listen(
  2174. mediaElement.audioTracks, 'removetrack',
  2175. () => this.onTracksChanged_());
  2176. this.loadEventManager_.listen(
  2177. mediaElement.audioTracks, 'change', () => this.onTracksChanged_());
  2178. }
  2179. if (mediaElement.textTracks) {
  2180. this.loadEventManager_.listen(
  2181. mediaElement.textTracks, 'addtrack', (e) => {
  2182. const trackEvent = /** @type {!TrackEvent} */(e);
  2183. if (trackEvent.track) {
  2184. const track = trackEvent.track;
  2185. goog.asserts.assert(
  2186. track instanceof TextTrack, 'Wrong track type!');
  2187. switch (track.kind) {
  2188. case 'metadata':
  2189. this.processTimedMetadataSrcEqls_(track);
  2190. break;
  2191. case 'chapters':
  2192. this.activateChaptersTrack_(track);
  2193. break;
  2194. default:
  2195. this.onTracksChanged_();
  2196. break;
  2197. }
  2198. }
  2199. });
  2200. this.loadEventManager_.listen(
  2201. mediaElement.textTracks, 'removetrack',
  2202. () => this.onTracksChanged_());
  2203. this.loadEventManager_.listen(
  2204. mediaElement.textTracks, 'change',
  2205. () => this.onTracksChanged_());
  2206. }
  2207. const extension = shaka.media.ManifestParser.getExtension(has.uri);
  2208. const mimeType =
  2209. shaka.Player.SRC_EQUAL_EXTENSIONS_TO_MIME_TYPES_[extension];
  2210. // By setting |src| we are done "loading" with src=. We don't need to set
  2211. // the current time because |playhead| will do that for us.
  2212. mediaElement.src = this.cmcdManager_.appendSrcData(has.uri, mimeType);
  2213. // Tizen 3 / WebOS won't load anything unless you call load() explicitly,
  2214. // no matter the value of the preload attribute. This is harmful on some
  2215. // other platforms by triggering unbounded loading of media data, but is
  2216. // necessary here.
  2217. if (shaka.util.Platform.isTizen() || shaka.util.Platform.isWebOS()) {
  2218. mediaElement.load();
  2219. }
  2220. // Set the load mode last so that we know that all our components are
  2221. // initialized.
  2222. this.loadMode_ = shaka.Player.LoadMode.SRC_EQUALS;
  2223. // The event doesn't mean as much for src= playback, since we don't control
  2224. // streaming. But we should fire it in this path anyway since some
  2225. // applications may be expecting it as a life-cycle event.
  2226. this.dispatchEvent(
  2227. this.makeEvent_(shaka.util.FakeEvent.EventName.Streaming));
  2228. // The "load" Promise is resolved when we have loaded the metadata. If we
  2229. // wait for the full data, that won't happen on Safari until the play button
  2230. // is hit.
  2231. const fullyLoaded = new shaka.util.PublicPromise();
  2232. shaka.util.MediaReadyState.waitForReadyState(mediaElement,
  2233. HTMLMediaElement.HAVE_METADATA,
  2234. this.loadEventManager_,
  2235. () => {
  2236. this.playhead_.ready();
  2237. fullyLoaded.resolve();
  2238. });
  2239. // We can't switch to preferred languages, though, until the data is loaded.
  2240. shaka.util.MediaReadyState.waitForReadyState(mediaElement,
  2241. HTMLMediaElement.HAVE_CURRENT_DATA,
  2242. this.loadEventManager_,
  2243. async () => {
  2244. this.setupPreferredAudioOnSrc_();
  2245. // Applying the text preference too soon can result in it being
  2246. // reverted. Wait for native HLS to pick something first.
  2247. const textTracks = this.getFilteredTextTracks_();
  2248. if (!textTracks.find((t) => t.mode != 'disabled')) {
  2249. await new Promise((resolve) => {
  2250. this.loadEventManager_.listenOnce(
  2251. mediaElement.textTracks, 'change', resolve);
  2252. // We expect the event to fire because it does on Safari.
  2253. // But in case it doesn't on some other platform or future
  2254. // version, move on in 1 second no matter what. This keeps the
  2255. // language settings from being completely ignored if something
  2256. // goes wrong.
  2257. new shaka.util.Timer(resolve).tickAfter(1);
  2258. });
  2259. } else if (textTracks.length > 0) {
  2260. this.isTextVisible_ = true;
  2261. }
  2262. // If we have moved on to another piece of content while waiting for
  2263. // the above event/timer, we should not change tracks here.
  2264. if (unloaded) {
  2265. return;
  2266. }
  2267. this.setupPreferredTextOnSrc_();
  2268. });
  2269. if (mediaElement.error) {
  2270. // Already failed!
  2271. fullyLoaded.reject(this.videoErrorToShakaError_());
  2272. } else if (mediaElement.preload == 'none') {
  2273. shaka.log.alwaysWarn(
  2274. 'With <video preload="none">, the browser will not load anything ' +
  2275. 'until play() is called. We are unable to measure load latency in ' +
  2276. 'a meaningful way, and we cannot provide track info yet. Please do ' +
  2277. 'not use preload="none" with Shaka Player.');
  2278. // We can't wait for an event load loadedmetadata, since that will be
  2279. // blocked until a user interaction. So resolve the Promise now.
  2280. fullyLoaded.resolve();
  2281. }
  2282. this.loadEventManager_.listenOnce(mediaElement, 'error', () => {
  2283. fullyLoaded.reject(this.videoErrorToShakaError_());
  2284. });
  2285. return new shaka.util.AbortableOperation(fullyLoaded, /* onAbort= */ () => {
  2286. const abortedError = new shaka.util.Error(
  2287. shaka.util.Error.Severity.CRITICAL,
  2288. shaka.util.Error.Category.PLAYER,
  2289. shaka.util.Error.Code.OPERATION_ABORTED);
  2290. fullyLoaded.reject(abortedError);
  2291. return Promise.resolve(); // Abort complete.
  2292. }).chain(() => {
  2293. if (this.isLive() && this.config_.streaming.liveSync) {
  2294. const onTimeUpdate = () => this.onTimeUpdate_();
  2295. this.loadEventManager_.listen(mediaElement, 'timeupdate', onTimeUpdate);
  2296. }
  2297. this.fullyLoaded_ = true;
  2298. });
  2299. }
  2300. /**
  2301. * This method setup the preferred audio using src=..
  2302. *
  2303. * @private
  2304. */
  2305. setupPreferredAudioOnSrc_() {
  2306. const preferredAudioLanguage = this.config_.preferredAudioLanguage;
  2307. // If the user has not selected a preference, the browser preference is
  2308. // left.
  2309. if (preferredAudioLanguage == '') {
  2310. return;
  2311. }
  2312. const preferredVariantRole = this.config_.preferredVariantRole;
  2313. this.selectAudioLanguage(preferredAudioLanguage, preferredVariantRole);
  2314. }
  2315. /**
  2316. * This method setup the preferred text using src=.
  2317. *
  2318. * @private
  2319. */
  2320. setupPreferredTextOnSrc_() {
  2321. const preferredTextLanguage = this.config_.preferredTextLanguage;
  2322. // If the user has not selected a preference, the browser preference is
  2323. // left.
  2324. if (preferredTextLanguage == '') {
  2325. return;
  2326. }
  2327. const preferForcedSubs = this.config_.preferForcedSubs;
  2328. const preferredTextRole = this.config_.preferredTextRole;
  2329. this.selectTextLanguage(preferredTextLanguage, preferredTextRole,
  2330. preferForcedSubs);
  2331. }
  2332. /**
  2333. * We're looking for metadata tracks to process id3 tags. One of the uses is
  2334. * for ad info on LIVE streams
  2335. *
  2336. * @param {!TextTrack} track
  2337. * @private
  2338. */
  2339. processTimedMetadataSrcEqls_(track) {
  2340. if (track.kind != 'metadata') {
  2341. return;
  2342. }
  2343. // Hidden mode is required for the cuechange event to launch correctly
  2344. track.mode = 'hidden';
  2345. this.loadEventManager_.listen(track, 'cuechange', () => {
  2346. if (!track.activeCues) {
  2347. return;
  2348. }
  2349. for (const cue of track.activeCues) {
  2350. this.dispatchMetadataEvent_(cue.startTime, cue.endTime,
  2351. cue.type, cue.value);
  2352. if (this.adManager_) {
  2353. this.adManager_.onCueMetadataChange(cue.value);
  2354. }
  2355. }
  2356. });
  2357. // In Safari the initial assignment does not always work, so we schedule
  2358. // this process to be repeated several times to ensure that it has been put
  2359. // in the correct mode.
  2360. const timer = new shaka.util.Timer(() => {
  2361. const textTracks = this.getMetadataTracks_();
  2362. for (const textTrack of textTracks) {
  2363. textTrack.mode = 'hidden';
  2364. }
  2365. }).tickNow().tickAfter(0.5);
  2366. this.cleanupOnUnload_.push(() => {
  2367. timer.stop();
  2368. });
  2369. }
  2370. /**
  2371. * @param {!Array.<shaka.extern.ID3Metadata>} metadata
  2372. * @param {number} offset
  2373. * @param {?number} segmentEndTime
  2374. * @private
  2375. */
  2376. processTimedMetadataMediaSrc_(metadata, offset, segmentEndTime) {
  2377. for (const sample of metadata) {
  2378. if (sample.data && sample.cueTime && sample.frames) {
  2379. const start = sample.cueTime + offset;
  2380. let end = segmentEndTime;
  2381. // This can happen when the ID3 info arrives in a previous segment.
  2382. if (end && start > end) {
  2383. end = start;
  2384. }
  2385. const metadataType = 'org.id3';
  2386. for (const frame of sample.frames) {
  2387. const payload = frame;
  2388. this.dispatchMetadataEvent_(start, end, metadataType, payload);
  2389. }
  2390. if (this.adManager_) {
  2391. this.adManager_.onHlsTimedMetadata(sample, start);
  2392. }
  2393. }
  2394. }
  2395. }
  2396. /**
  2397. * Construct and fire a Player.Metadata event
  2398. *
  2399. * @param {number} startTime
  2400. * @param {?number} endTime
  2401. * @param {string} metadataType
  2402. * @param {shaka.extern.MetadataFrame} payload
  2403. * @private
  2404. */
  2405. dispatchMetadataEvent_(startTime, endTime, metadataType, payload) {
  2406. goog.asserts.assert(!endTime || startTime <= endTime,
  2407. 'Metadata start time should be less or equal to the end time!');
  2408. const eventName = shaka.util.FakeEvent.EventName.Metadata;
  2409. const data = new Map()
  2410. .set('startTime', startTime)
  2411. .set('endTime', endTime)
  2412. .set('metadataType', metadataType)
  2413. .set('payload', payload);
  2414. this.dispatchEvent(this.makeEvent_(eventName, data));
  2415. }
  2416. /**
  2417. * Set the mode on a chapters track so that it loads.
  2418. *
  2419. * @param {?TextTrack} track
  2420. * @private
  2421. */
  2422. activateChaptersTrack_(track) {
  2423. if (!track || track.kind != 'chapters') {
  2424. return;
  2425. }
  2426. // Hidden mode is required for the cuechange event to launch correctly and
  2427. // get the cues and the activeCues
  2428. track.mode = 'hidden';
  2429. // In Safari the initial assignment does not always work, so we schedule
  2430. // this process to be repeated several times to ensure that it has been put
  2431. // in the correct mode.
  2432. const timer = new shaka.util.Timer(() => {
  2433. track.mode = 'hidden';
  2434. }).tickNow().tickAfter(0.5);
  2435. this.cleanupOnUnload_.push(() => {
  2436. timer.stop();
  2437. });
  2438. }
  2439. /**
  2440. * Take a series of variants and ensure that they only contain one type of
  2441. * variant. The different options are:
  2442. * 1. Audio-Video
  2443. * 2. Audio-Only
  2444. * 3. Video-Only
  2445. *
  2446. * A manifest can only contain a single type because once we initialize media
  2447. * source to expect specific streams, it must always have content for those
  2448. * streams. If we were to start with audio+video and switch to an audio-only
  2449. * variant, media source would block waiting for video content.
  2450. *
  2451. * @param {shaka.extern.Manifest} manifest
  2452. * @private
  2453. */
  2454. static filterForAVVariants_(manifest) {
  2455. const isAVVariant = (variant) => {
  2456. // Audio-video variants may include both streams separately or may be
  2457. // single multiplexed streams with multiple codecs.
  2458. return (variant.video && variant.audio) ||
  2459. (variant.video && variant.video.codecs.includes(','));
  2460. };
  2461. if (manifest.variants.some(isAVVariant)) {
  2462. shaka.log.debug('Found variant with audio and video content, ' +
  2463. 'so filtering out audio-only content.');
  2464. manifest.variants = manifest.variants.filter(isAVVariant);
  2465. }
  2466. }
  2467. /**
  2468. * Create a new DrmEngine instance. This may be replaced by tests to create
  2469. * fake instances. Configuration and initialization will be handled after
  2470. * |createDrmEngine|.
  2471. *
  2472. * @param {shaka.media.DrmEngine.PlayerInterface} playerInterface
  2473. * @return {!shaka.media.DrmEngine}
  2474. */
  2475. createDrmEngine(playerInterface) {
  2476. const updateExpirationTime = this.config_.drm.updateExpirationTime;
  2477. return new shaka.media.DrmEngine(playerInterface, updateExpirationTime);
  2478. }
  2479. /**
  2480. * Creates a new instance of NetworkingEngine. This can be replaced by tests
  2481. * to create fake instances instead.
  2482. *
  2483. * @return {!shaka.net.NetworkingEngine}
  2484. */
  2485. createNetworkingEngine() {
  2486. /** @type {function(number, number)} */
  2487. const onProgressUpdated_ = (deltaTimeMs, bytesDownloaded) => {
  2488. // In some situations, such as during offline storage, the abr manager
  2489. // might not yet exist. Therefore, we need to check if abr manager has
  2490. // been initialized before using it.
  2491. if (this.abrManager_) {
  2492. this.abrManager_.segmentDownloaded(deltaTimeMs, bytesDownloaded);
  2493. }
  2494. };
  2495. /** @type {shaka.net.NetworkingEngine.OnHeadersReceived} */
  2496. const onHeadersReceived_ = (headers, request, requestType) => {
  2497. // Release a 'downloadheadersreceived' event.
  2498. const name = shaka.util.FakeEvent.EventName.DownloadHeadersReceived;
  2499. const data = new Map()
  2500. .set('headers', headers)
  2501. .set('request', request)
  2502. .set('requestType', requestType);
  2503. this.dispatchEvent(this.makeEvent_(name, data));
  2504. };
  2505. /** @type {shaka.net.NetworkingEngine.OnDownloadFailed} */
  2506. const onDownloadFailed_ = (request, error, httpResponseCode, aborted) => {
  2507. // Release a 'downloadfailed' event.
  2508. const name = shaka.util.FakeEvent.EventName.DownloadFailed;
  2509. const data = new Map()
  2510. .set('request', request)
  2511. .set('error', error)
  2512. .set('httpResponseCode', httpResponseCode)
  2513. .set('aborted', aborted);
  2514. this.dispatchEvent(this.makeEvent_(name, data));
  2515. };
  2516. /** @type {shaka.net.NetworkingEngine.OnRequest} */
  2517. const onRequest_ = (type, request, context) => {
  2518. this.cmcdManager_.applyData(type, request, context);
  2519. };
  2520. return new shaka.net.NetworkingEngine(
  2521. onProgressUpdated_, onHeadersReceived_, onDownloadFailed_, onRequest_);
  2522. }
  2523. /**
  2524. * Creates a new instance of Playhead. This can be replaced by tests to
  2525. * create fake instances instead.
  2526. *
  2527. * @param {?number} startTime
  2528. * @return {!shaka.media.Playhead}
  2529. */
  2530. createPlayhead(startTime) {
  2531. goog.asserts.assert(this.manifest_, 'Must have manifest');
  2532. goog.asserts.assert(this.video_, 'Must have video');
  2533. return new shaka.media.MediaSourcePlayhead(
  2534. this.video_,
  2535. this.manifest_,
  2536. this.config_.streaming,
  2537. startTime,
  2538. () => this.onSeek_(),
  2539. (event) => this.dispatchEvent(event));
  2540. }
  2541. /**
  2542. * Create the observers for MSE playback. These observers are responsible for
  2543. * notifying the app and player of specific events during MSE playback.
  2544. *
  2545. * @param {number} startTimeOfLoad
  2546. * @return {!shaka.media.PlayheadObserverManager}
  2547. * @private
  2548. */
  2549. createPlayheadObserversForMSE_(startTimeOfLoad) {
  2550. goog.asserts.assert(this.manifest_, 'Must have manifest');
  2551. goog.asserts.assert(this.regionTimeline_, 'Must have region timeline');
  2552. goog.asserts.assert(this.video_, 'Must have video element');
  2553. const startsPastZero = this.isLive() || startTimeOfLoad > 0;
  2554. // Create the region observer. This will allow us to notify the app when we
  2555. // move in and out of timeline regions.
  2556. const regionObserver = new shaka.media.RegionObserver(
  2557. this.regionTimeline_, startsPastZero);
  2558. regionObserver.addEventListener('enter', (event) => {
  2559. /** @type {shaka.extern.TimelineRegionInfo} */
  2560. const region = event['region'];
  2561. this.onRegionEvent_(
  2562. shaka.util.FakeEvent.EventName.TimelineRegionEnter, region);
  2563. });
  2564. regionObserver.addEventListener('exit', (event) => {
  2565. /** @type {shaka.extern.TimelineRegionInfo} */
  2566. const region = event['region'];
  2567. this.onRegionEvent_(
  2568. shaka.util.FakeEvent.EventName.TimelineRegionExit, region);
  2569. });
  2570. regionObserver.addEventListener('skip', (event) => {
  2571. /** @type {shaka.extern.TimelineRegionInfo} */
  2572. const region = event['region'];
  2573. /** @type {boolean} */
  2574. const seeking = event['seeking'];
  2575. // If we are seeking, we don't want to surface the enter/exit events since
  2576. // they didn't play through them.
  2577. if (!seeking) {
  2578. this.onRegionEvent_(
  2579. shaka.util.FakeEvent.EventName.TimelineRegionEnter, region);
  2580. this.onRegionEvent_(
  2581. shaka.util.FakeEvent.EventName.TimelineRegionExit, region);
  2582. }
  2583. });
  2584. // Now that we have all our observers, create a manager for them.
  2585. const manager = new shaka.media.PlayheadObserverManager(this.video_);
  2586. manager.manage(regionObserver);
  2587. if (this.qualityObserver_) {
  2588. manager.manage(this.qualityObserver_);
  2589. }
  2590. return manager;
  2591. }
  2592. /**
  2593. * Initialize and start the buffering system (observer and timer) so that we
  2594. * can monitor our buffer lead during playback.
  2595. *
  2596. * @param {number} rebufferingGoal
  2597. * @private
  2598. */
  2599. startBufferManagement_(rebufferingGoal) {
  2600. goog.asserts.assert(
  2601. !this.bufferObserver_,
  2602. 'No buffering observer should exist before initialization.');
  2603. goog.asserts.assert(
  2604. !this.bufferPoller_,
  2605. 'No buffer timer should exist before initialization.');
  2606. // Give dummy values, will be updated below.
  2607. this.bufferObserver_ = new shaka.media.BufferingObserver(1, 2);
  2608. // Force us back to a buffering state. This ensure everything is starting in
  2609. // the same state.
  2610. this.bufferObserver_.setState(shaka.media.BufferingObserver.State.STARVING);
  2611. this.updateBufferingSettings_(rebufferingGoal);
  2612. this.updateBufferState_();
  2613. // TODO: We should take some time to look into the effects of our
  2614. // quarter-second refresh practice. We often use a quarter-second
  2615. // but we have no documentation about why.
  2616. this.bufferPoller_ = new shaka.util.Timer(() => {
  2617. this.pollBufferState_();
  2618. }).tickEvery(/* seconds= */ 0.25);
  2619. }
  2620. /**
  2621. * Updates the buffering thresholds based on the new rebuffering goal.
  2622. *
  2623. * @param {number} rebufferingGoal
  2624. * @private
  2625. */
  2626. updateBufferingSettings_(rebufferingGoal) {
  2627. // The threshold to transition back to satisfied when starving.
  2628. const starvingThreshold = rebufferingGoal;
  2629. // The threshold to transition into starving when satisfied.
  2630. // We use a "typical" threshold, unless the rebufferingGoal is unusually
  2631. // low.
  2632. // Then we force the value down to half the rebufferingGoal, since
  2633. // starvingThreshold must be strictly larger than satisfiedThreshold for the
  2634. // logic in BufferingObserver to work correctly.
  2635. const satisfiedThreshold = Math.min(
  2636. shaka.Player.TYPICAL_BUFFERING_THRESHOLD_, rebufferingGoal / 2);
  2637. this.bufferObserver_.setThresholds(starvingThreshold, satisfiedThreshold);
  2638. }
  2639. /**
  2640. * This method is called periodically to check what the buffering observer
  2641. * says so that we can update the rest of the buffering behaviours.
  2642. *
  2643. * @private
  2644. */
  2645. pollBufferState_() {
  2646. goog.asserts.assert(
  2647. this.video_,
  2648. 'Need a media element to update the buffering observer');
  2649. goog.asserts.assert(
  2650. this.bufferObserver_,
  2651. 'Need a buffering observer to update');
  2652. let bufferedToEnd;
  2653. switch (this.loadMode_) {
  2654. case shaka.Player.LoadMode.SRC_EQUALS:
  2655. bufferedToEnd = this.isBufferedToEndSrc_();
  2656. break;
  2657. case shaka.Player.LoadMode.MEDIA_SOURCE:
  2658. bufferedToEnd = this.isBufferedToEndMS_();
  2659. break;
  2660. default:
  2661. bufferedToEnd = false;
  2662. break;
  2663. }
  2664. const bufferLead = shaka.media.TimeRangesUtils.bufferedAheadOf(
  2665. this.video_.buffered,
  2666. this.video_.currentTime);
  2667. const stateChanged = this.bufferObserver_.update(bufferLead, bufferedToEnd);
  2668. // If the state changed, we need to surface the event.
  2669. if (stateChanged) {
  2670. this.updateBufferState_();
  2671. }
  2672. }
  2673. /**
  2674. * Create a new media source engine. This will ONLY be replaced by tests as a
  2675. * way to inject fake media source engine instances.
  2676. *
  2677. * @param {!HTMLMediaElement} mediaElement
  2678. * @param {!shaka.extern.TextDisplayer} textDisplayer
  2679. * @param {!function(!Array.<shaka.extern.ID3Metadata>, number, ?number)}
  2680. * onMetadata
  2681. * @param {shaka.lcevc.Dil} lcevcDil
  2682. *
  2683. * @return {!shaka.media.MediaSourceEngine}
  2684. */
  2685. createMediaSourceEngine(mediaElement, textDisplayer, onMetadata, lcevcDil) {
  2686. return new shaka.media.MediaSourceEngine(
  2687. mediaElement,
  2688. textDisplayer,
  2689. onMetadata,
  2690. lcevcDil);
  2691. }
  2692. /**
  2693. * Create a new CMCD manager.
  2694. *
  2695. * @private
  2696. */
  2697. createCmcd_() {
  2698. /** @type {shaka.util.CmcdManager.PlayerInterface} */
  2699. const playerInterface = {
  2700. getBandwidthEstimate: () => this.abrManager_ ?
  2701. this.abrManager_.getBandwidthEstimate() : NaN,
  2702. getBufferedInfo: () => this.getBufferedInfo(),
  2703. getCurrentTime: () => this.video_ ? this.video_.currentTime : 0,
  2704. getPlaybackRate: () => this.getPlaybackRate(),
  2705. getNetworkingEngine: () => this.getNetworkingEngine(),
  2706. getVariantTracks: () => this.getVariantTracks(),
  2707. isLive: () => this.isLive(),
  2708. };
  2709. return new shaka.util.CmcdManager(playerInterface, this.config_.cmcd);
  2710. }
  2711. /**
  2712. * Creates a new instance of StreamingEngine. This can be replaced by tests
  2713. * to create fake instances instead.
  2714. *
  2715. * @return {!shaka.media.StreamingEngine}
  2716. */
  2717. createStreamingEngine() {
  2718. goog.asserts.assert(
  2719. this.abrManager_ && this.mediaSourceEngine_ && this.manifest_,
  2720. 'Must not be destroyed');
  2721. /** @type {shaka.media.StreamingEngine.PlayerInterface} */
  2722. const playerInterface = {
  2723. getPresentationTime: () => this.playhead_ ? this.playhead_.getTime() : 0,
  2724. getBandwidthEstimate: () => this.abrManager_.getBandwidthEstimate(),
  2725. mediaSourceEngine: this.mediaSourceEngine_,
  2726. netEngine: this.networkingEngine_,
  2727. onError: (error) => this.onError_(error),
  2728. onEvent: (event) => this.dispatchEvent(event),
  2729. onManifestUpdate: () => this.onManifestUpdate_(),
  2730. onSegmentAppended: (start, end, contentType) => {
  2731. this.onSegmentAppended_(start, end, contentType);
  2732. },
  2733. onInitSegmentAppended: (position, initSegment) => {
  2734. const mediaQuality = initSegment.getMediaQuality();
  2735. if (mediaQuality && this.qualityObserver_) {
  2736. this.qualityObserver_.addMediaQualityChange(mediaQuality, position);
  2737. }
  2738. },
  2739. beforeAppendSegment: (contentType, segment) => {
  2740. return this.drmEngine_.parseInbandPssh(contentType, segment);
  2741. },
  2742. onMetadata: (metadata, offset, endTime) => {
  2743. this.processTimedMetadataMediaSrc_(metadata, offset, endTime);
  2744. },
  2745. disableStream: (stream, time) => this.disableStream(stream, time),
  2746. };
  2747. return new shaka.media.StreamingEngine(this.manifest_, playerInterface);
  2748. }
  2749. /**
  2750. * Changes configuration settings on the Player. This checks the names of
  2751. * keys and the types of values to avoid coding errors. If there are errors,
  2752. * this logs them to the console and returns false. Correct fields are still
  2753. * applied even if there are other errors. You can pass an explicit
  2754. * <code>undefined</code> value to restore the default value. This has two
  2755. * modes of operation:
  2756. *
  2757. * <p>
  2758. * First, this can be passed a single "plain" object. This object should
  2759. * follow the {@link shaka.extern.PlayerConfiguration} object. Not all fields
  2760. * need to be set; unset fields retain their old values.
  2761. *
  2762. * <p>
  2763. * Second, this can be passed two arguments. The first is the name of the key
  2764. * to set. This should be a '.' separated path to the key. For example,
  2765. * <code>'streaming.alwaysStreamText'</code>. The second argument is the
  2766. * value to set.
  2767. *
  2768. * @param {string|!Object} config This should either be a field name or an
  2769. * object.
  2770. * @param {*=} value In the second mode, this is the value to set.
  2771. * @return {boolean} True if the passed config object was valid, false if
  2772. * there were invalid entries.
  2773. * @export
  2774. */
  2775. configure(config, value) {
  2776. goog.asserts.assert(this.config_, 'Config must not be null!');
  2777. goog.asserts.assert(typeof(config) == 'object' || arguments.length == 2,
  2778. 'String configs should have values!');
  2779. // ('fieldName', value) format
  2780. if (arguments.length == 2 && typeof(config) == 'string') {
  2781. config = shaka.util.ConfigUtils.convertToConfigObject(config, value);
  2782. }
  2783. goog.asserts.assert(typeof(config) == 'object', 'Should be an object!');
  2784. // Deprecate 'streaming.forceTransmuxTS' configuration.
  2785. if (config['streaming'] && 'forceTransmuxTS' in config['streaming']) {
  2786. shaka.Deprecate.deprecateFeature(5,
  2787. 'streaming.forceTransmuxTS configuration',
  2788. 'Please Use mediaSource.forceTransmux instead.');
  2789. config['mediaSource']['mediaSource'] =
  2790. config['streaming']['forceTransmuxTS'];
  2791. delete config['streaming']['forceTransmuxTS'];
  2792. }
  2793. // Deprecate 'streaming.forceTransmux' configuration.
  2794. if (config['streaming'] && 'forceTransmux' in config['streaming']) {
  2795. shaka.Deprecate.deprecateFeature(5,
  2796. 'streaming.forceTransmux configuration',
  2797. 'Please Use mediaSource.forceTransmux instead.');
  2798. config['mediaSource']['mediaSource'] =
  2799. config['streaming']['forceTransmux'];
  2800. delete config['streaming']['forceTransmux'];
  2801. }
  2802. // If lowLatencyMode is enabled, and inaccurateManifestTolerance and
  2803. // rebufferingGoal and segmentPrefetchLimit are not specified, set
  2804. // inaccurateManifestTolerance to 0 and rebufferingGoal to 0.01 and
  2805. // segmentPrefetchLimit to 2 by default for low latency streaming.
  2806. if (config['streaming'] && config['streaming']['lowLatencyMode']) {
  2807. if (config['streaming']['inaccurateManifestTolerance'] == undefined) {
  2808. config['streaming']['inaccurateManifestTolerance'] = 0;
  2809. }
  2810. if (config['streaming']['rebufferingGoal'] == undefined) {
  2811. config['streaming']['rebufferingGoal'] = 0.01;
  2812. }
  2813. if (config['streaming']['segmentPrefetchLimit'] == undefined) {
  2814. config['streaming']['segmentPrefetchLimit'] = 2;
  2815. }
  2816. }
  2817. const ret = shaka.util.PlayerConfiguration.mergeConfigObjects(
  2818. this.config_, config, this.defaultConfig_());
  2819. this.applyConfig_();
  2820. return ret;
  2821. }
  2822. /**
  2823. * Apply config changes.
  2824. * @private
  2825. */
  2826. applyConfig_() {
  2827. if (this.parser_) {
  2828. const manifestConfig =
  2829. shaka.util.ObjectUtils.cloneObject(this.config_.manifest);
  2830. // Don't read video segments if the player is attached to an audio element
  2831. if (this.video_ && this.video_.nodeName === 'AUDIO') {
  2832. manifestConfig.disableVideo = true;
  2833. }
  2834. this.parser_.configure(manifestConfig);
  2835. }
  2836. if (this.drmEngine_) {
  2837. this.drmEngine_.configure(this.config_.drm);
  2838. }
  2839. if (this.streamingEngine_) {
  2840. this.streamingEngine_.configure(this.config_.streaming);
  2841. // Need to apply the restrictions.
  2842. try {
  2843. // this.filterManifestWithRestrictions_() may throw.
  2844. this.filterManifestWithRestrictions_(this.manifest_);
  2845. } catch (error) {
  2846. this.onError_(error);
  2847. }
  2848. if (this.abrManager_) {
  2849. // Update AbrManager variants to match these new settings.
  2850. this.updateAbrManagerVariants_();
  2851. }
  2852. // If the streams we are playing are restricted, we need to switch.
  2853. const activeVariant = this.streamingEngine_.getCurrentVariant();
  2854. if (activeVariant) {
  2855. if (!activeVariant.allowedByApplication ||
  2856. !activeVariant.allowedByKeySystem) {
  2857. shaka.log.debug('Choosing new variant after changing configuration');
  2858. this.chooseVariantAndSwitch_();
  2859. }
  2860. }
  2861. }
  2862. if (this.networkingEngine_) {
  2863. this.networkingEngine_.setForceHTTPS(this.config_.streaming.forceHTTPS);
  2864. }
  2865. if (this.mediaSourceEngine_) {
  2866. this.mediaSourceEngine_.configure(this.config_.mediaSource);
  2867. const {segmentRelativeVttTiming} = this.config_.manifest;
  2868. this.mediaSourceEngine_.setSegmentRelativeVttTiming(
  2869. segmentRelativeVttTiming);
  2870. const textDisplayerFactory = this.config_.textDisplayFactory;
  2871. if (this.lastTextFactory_ != textDisplayerFactory) {
  2872. const displayer = textDisplayerFactory();
  2873. this.mediaSourceEngine_.setTextDisplayer(displayer);
  2874. this.lastTextFactory_ = textDisplayerFactory;
  2875. if (this.streamingEngine_) {
  2876. // Reload the text stream, so the cues will load again.
  2877. this.streamingEngine_.reloadTextStream();
  2878. }
  2879. }
  2880. }
  2881. if (this.abrManager_) {
  2882. this.abrManager_.configure(this.config_.abr);
  2883. // Simply enable/disable ABR with each call, since multiple calls to these
  2884. // methods have no effect.
  2885. if (this.config_.abr.enabled) {
  2886. this.abrManager_.enable();
  2887. } else {
  2888. this.abrManager_.disable();
  2889. }
  2890. this.onAbrStatusChanged_();
  2891. }
  2892. if (this.bufferObserver_) {
  2893. let rebufferThreshold = this.config_.streaming.rebufferingGoal;
  2894. if (this.manifest_) {
  2895. rebufferThreshold =
  2896. Math.max(rebufferThreshold, this.manifest_.minBufferTime);
  2897. }
  2898. this.updateBufferingSettings_(rebufferThreshold);
  2899. }
  2900. if (this.manifest_) {
  2901. shaka.Player.applyPlayRange_(this.manifest_.presentationTimeline,
  2902. this.config_.playRangeStart,
  2903. this.config_.playRangeEnd);
  2904. }
  2905. if (this.adManager_) {
  2906. this.adManager_.configure(this.config_.ads);
  2907. }
  2908. if (this.cmcdManager_) {
  2909. this.cmcdManager_.configure(this.config_.cmcd);
  2910. }
  2911. }
  2912. /**
  2913. * Return a copy of the current configuration. Modifications of the returned
  2914. * value will not affect the Player's active configuration. You must call
  2915. * <code>player.configure()</code> to make changes.
  2916. *
  2917. * @return {shaka.extern.PlayerConfiguration}
  2918. * @export
  2919. */
  2920. getConfiguration() {
  2921. goog.asserts.assert(this.config_, 'Config must not be null!');
  2922. const ret = this.defaultConfig_();
  2923. shaka.util.PlayerConfiguration.mergeConfigObjects(
  2924. ret, this.config_, this.defaultConfig_());
  2925. return ret;
  2926. }
  2927. /**
  2928. * Return a reference to the current configuration. Modifications to the
  2929. * returned value will affect the Player's active configuration. This method
  2930. * is not exported as sharing configuration with external objects is not
  2931. * supported.
  2932. *
  2933. * @return {shaka.extern.PlayerConfiguration}
  2934. */
  2935. getSharedConfiguration() {
  2936. goog.asserts.assert(
  2937. this.config_, 'Cannot call getSharedConfiguration after call destroy!');
  2938. return this.config_;
  2939. }
  2940. /**
  2941. * Returns the ratio of video length buffered compared to buffering Goal
  2942. * @return {number}
  2943. * @export
  2944. */
  2945. getBufferFullness() {
  2946. if (this.video_) {
  2947. const bufferedLength = this.video_.buffered.length;
  2948. const bufferedEnd =
  2949. bufferedLength ? this.video_.buffered.end(bufferedLength - 1) : 0;
  2950. const bufferingGoal = this.getConfiguration().streaming.bufferingGoal;
  2951. const lengthToBeBuffered = Math.min(this.video_.currentTime +
  2952. bufferingGoal, this.seekRange().end);
  2953. if (bufferedEnd >= lengthToBeBuffered) {
  2954. return 1;
  2955. } else if (bufferedEnd <= this.video_.currentTime) {
  2956. return 0;
  2957. } else if (bufferedEnd < lengthToBeBuffered) {
  2958. return ((bufferedEnd - this.video_.currentTime) /
  2959. (lengthToBeBuffered - this.video_.currentTime));
  2960. }
  2961. }
  2962. return 0;
  2963. }
  2964. /**
  2965. * Reset configuration to default.
  2966. * @export
  2967. */
  2968. resetConfiguration() {
  2969. goog.asserts.assert(this.config_, 'Cannot be destroyed');
  2970. // Remove the old keys so we remove open-ended dictionaries like drm.servers
  2971. // but keeps the same object reference.
  2972. for (const key in this.config_) {
  2973. delete this.config_[key];
  2974. }
  2975. shaka.util.PlayerConfiguration.mergeConfigObjects(
  2976. this.config_, this.defaultConfig_(), this.defaultConfig_());
  2977. this.applyConfig_();
  2978. }
  2979. /**
  2980. * Get the current load mode.
  2981. *
  2982. * @return {shaka.Player.LoadMode}
  2983. * @export
  2984. */
  2985. getLoadMode() {
  2986. return this.loadMode_;
  2987. }
  2988. /**
  2989. * Get the current manifest type.
  2990. *
  2991. * @return {?string}
  2992. * @export
  2993. */
  2994. getManifestType() {
  2995. if (!this.manifest_) {
  2996. return null;
  2997. }
  2998. return this.manifest_.type;
  2999. }
  3000. /**
  3001. * Get the media element that the player is currently using to play loaded
  3002. * content. If the player has not loaded content, this will return
  3003. * <code>null</code>.
  3004. *
  3005. * @return {HTMLMediaElement}
  3006. * @export
  3007. */
  3008. getMediaElement() {
  3009. return this.video_;
  3010. }
  3011. /**
  3012. * @return {shaka.net.NetworkingEngine} A reference to the Player's networking
  3013. * engine. Applications may use this to make requests through Shaka's
  3014. * networking plugins.
  3015. * @export
  3016. */
  3017. getNetworkingEngine() {
  3018. return this.networkingEngine_;
  3019. }
  3020. /**
  3021. * Get the uri to the asset that the player has loaded. If the player has not
  3022. * loaded content, this will return <code>null</code>.
  3023. *
  3024. * @return {?string}
  3025. * @export
  3026. */
  3027. getAssetUri() {
  3028. return this.assetUri_;
  3029. }
  3030. /**
  3031. * Returns a shaka.ads.AdManager instance, responsible for Dynamic
  3032. * Ad Insertion functionality.
  3033. *
  3034. * @return {shaka.extern.IAdManager}
  3035. * @export
  3036. */
  3037. getAdManager() {
  3038. // NOTE: this clause is redundant, but it keeps the compiler from
  3039. // inlining this function. Inlining leads to setting the adManager
  3040. // not taking effect in the compiled build.
  3041. // Closure has a @noinline flag, but apparently not all cases are
  3042. // supported by it, and ours isn't.
  3043. // If they expand support, we might be able to get rid of this
  3044. // clause.
  3045. if (!this.adManager_) {
  3046. return null;
  3047. }
  3048. return this.adManager_;
  3049. }
  3050. /**
  3051. * Get if the player is playing live content. If the player has not loaded
  3052. * content, this will return <code>false</code>.
  3053. *
  3054. * @return {boolean}
  3055. * @export
  3056. */
  3057. isLive() {
  3058. if (this.manifest_) {
  3059. return this.manifest_.presentationTimeline.isLive();
  3060. }
  3061. // For native HLS, the duration for live streams seems to be Infinity.
  3062. if (this.video_ && this.video_.src) {
  3063. return this.video_.duration == Infinity;
  3064. }
  3065. return false;
  3066. }
  3067. /**
  3068. * Get if the player is playing in-progress content. If the player has not
  3069. * loaded content, this will return <code>false</code>.
  3070. *
  3071. * @return {boolean}
  3072. * @export
  3073. */
  3074. isInProgress() {
  3075. return this.manifest_ ?
  3076. this.manifest_.presentationTimeline.isInProgress() :
  3077. false;
  3078. }
  3079. /**
  3080. * Check if the manifest contains only audio-only content. If the player has
  3081. * not loaded content, this will return <code>false</code>.
  3082. *
  3083. * <p>
  3084. * The player does not support content that contain more than one type of
  3085. * variants (i.e. mixing audio-only, video-only, audio-video). Content will be
  3086. * filtered to only contain one type of variant.
  3087. *
  3088. * @return {boolean}
  3089. * @export
  3090. */
  3091. isAudioOnly() {
  3092. if (this.manifest_) {
  3093. const variants = this.manifest_.variants;
  3094. if (!variants.length) {
  3095. return false;
  3096. }
  3097. // Note that if there are some audio-only variants and some audio-video
  3098. // variants, the audio-only variants are removed during filtering.
  3099. // Therefore if the first variant has no video, that's sufficient to say
  3100. // it is audio-only content.
  3101. return !variants[0].video;
  3102. } else if (this.video_ && this.video_.src) {
  3103. // If we have video track info, use that. It will be the least
  3104. // error-prone way with native HLS. In contrast, videoHeight might be
  3105. // unset until the first frame is loaded. Since isAudioOnly is queried
  3106. // by the UI on the 'trackschanged' event, the videoTracks info should be
  3107. // up-to-date.
  3108. if (this.video_.videoTracks) {
  3109. return this.video_.videoTracks.length == 0;
  3110. }
  3111. // We cast to the more specific HTMLVideoElement to access videoHeight.
  3112. // This might be an audio element, though, in which case videoHeight will
  3113. // be undefined at runtime. For audio elements, this will always return
  3114. // true.
  3115. const video = /** @type {HTMLVideoElement} */(this.video_);
  3116. return video.videoHeight == 0;
  3117. } else {
  3118. return false;
  3119. }
  3120. }
  3121. /**
  3122. * Return the value of lowLatencyMode configuration.
  3123. * @return {boolean}
  3124. * @private
  3125. */
  3126. isLowLatencyMode_() {
  3127. return this.config_.streaming.lowLatencyMode;
  3128. }
  3129. /**
  3130. * Return the value of autoLowLatencyMode configuration.
  3131. * @return {boolean}
  3132. * @private
  3133. */
  3134. isAutoLowLatencyMode_() {
  3135. return this.config_.streaming.autoLowLatencyMode;
  3136. }
  3137. /**
  3138. * Get the range of time (in seconds) that seeking is allowed. If the player
  3139. * has not loaded content, this will return a range from 0 to 0.
  3140. *
  3141. * @return {{start: number, end: number}}
  3142. * @export
  3143. */
  3144. seekRange() {
  3145. if (!this.fullyLoaded_) {
  3146. return {'start': 0, 'end': 0};
  3147. }
  3148. if (this.manifest_) {
  3149. const timeline = this.manifest_.presentationTimeline;
  3150. return {
  3151. 'start': timeline.getSeekRangeStart(),
  3152. 'end': timeline.getSeekRangeEnd(),
  3153. };
  3154. }
  3155. // If we have loaded content with src=, we ask the video element for its
  3156. // seekable range. This covers both plain mp4s and native HLS playbacks.
  3157. if (this.video_ && this.video_.src) {
  3158. const seekable = this.video_.seekable;
  3159. if (seekable.length) {
  3160. return {
  3161. 'start': seekable.start(0),
  3162. 'end': seekable.end(seekable.length - 1),
  3163. };
  3164. }
  3165. }
  3166. return {'start': 0, 'end': 0};
  3167. }
  3168. /**
  3169. * Go to live in a live stream.
  3170. *
  3171. * @export
  3172. */
  3173. goToLive() {
  3174. if (this.isLive()) {
  3175. this.video_.currentTime = this.seekRange().end;
  3176. } else {
  3177. shaka.log.warning('goToLive is for live streams!');
  3178. }
  3179. }
  3180. /**
  3181. * Get the key system currently used by EME. If EME is not being used, this
  3182. * will return an empty string. If the player has not loaded content, this
  3183. * will return an empty string.
  3184. *
  3185. * @return {string}
  3186. * @export
  3187. */
  3188. keySystem() {
  3189. return shaka.media.DrmEngine.keySystem(this.drmInfo());
  3190. }
  3191. /**
  3192. * Get the drm info used to initialize EME. If EME is not being used, this
  3193. * will return <code>null</code>. If the player is idle or has not initialized
  3194. * EME yet, this will return <code>null</code>.
  3195. *
  3196. * @return {?shaka.extern.DrmInfo}
  3197. * @export
  3198. */
  3199. drmInfo() {
  3200. return this.drmEngine_ ? this.drmEngine_.getDrmInfo() : null;
  3201. }
  3202. /**
  3203. * Get the drm engine.
  3204. * This method should only be used for testing. Applications SHOULD NOT
  3205. * use this in production.
  3206. *
  3207. * @return {?shaka.media.DrmEngine}
  3208. */
  3209. getDrmEngine() {
  3210. return this.drmEngine_;
  3211. }
  3212. /**
  3213. * Get the next known expiration time for any EME session. If the session
  3214. * never expires, this will return <code>Infinity</code>. If there are no EME
  3215. * sessions, this will return <code>Infinity</code>. If the player has not
  3216. * loaded content, this will return <code>Infinity</code>.
  3217. *
  3218. * @return {number}
  3219. * @export
  3220. */
  3221. getExpiration() {
  3222. return this.drmEngine_ ? this.drmEngine_.getExpiration() : Infinity;
  3223. }
  3224. /**
  3225. * Returns the active sessions metadata
  3226. *
  3227. * @return {!Array.<shaka.extern.DrmSessionMetadata>}
  3228. * @export
  3229. */
  3230. getActiveSessionsMetadata() {
  3231. return this.drmEngine_ ? this.drmEngine_.getActiveSessionsMetadata() : [];
  3232. }
  3233. /**
  3234. * Gets a map of EME key ID to the current key status.
  3235. *
  3236. * @return {!Object<string, string>}
  3237. * @export
  3238. */
  3239. getKeyStatuses() {
  3240. return this.drmEngine_ ? this.drmEngine_.getKeyStatuses() : {};
  3241. }
  3242. /**
  3243. * Check if the player is currently in a buffering state (has too little
  3244. * content to play smoothly). If the player has not loaded content, this will
  3245. * return <code>false</code>.
  3246. *
  3247. * @return {boolean}
  3248. * @export
  3249. */
  3250. isBuffering() {
  3251. const State = shaka.media.BufferingObserver.State;
  3252. return this.bufferObserver_ ?
  3253. this.bufferObserver_.getState() == State.STARVING :
  3254. false;
  3255. }
  3256. /**
  3257. * Get the playback rate of what is playing right now. If we are using trick
  3258. * play, this will return the trick play rate.
  3259. * If no content is playing, this will return 0.
  3260. * If content is buffering, this will return the expected playback rate once
  3261. * the video starts playing.
  3262. *
  3263. * <p>
  3264. * If the player has not loaded content, this will return a playback rate of
  3265. * 0.
  3266. *
  3267. * @return {number}
  3268. * @export
  3269. */
  3270. getPlaybackRate() {
  3271. if (!this.video_) {
  3272. return 0;
  3273. }
  3274. return this.playRateController_ ?
  3275. this.playRateController_.getRealRate() :
  3276. 1;
  3277. }
  3278. /**
  3279. * Enable trick play to skip through content without playing by repeatedly
  3280. * seeking. For example, a rate of 2.5 would result in 2.5 seconds of content
  3281. * being skipped every second. A negative rate will result in moving
  3282. * backwards.
  3283. *
  3284. * <p>
  3285. * If the player has not loaded content or is still loading content this will
  3286. * be a no-op. Wait until <code>load</code> has completed before calling.
  3287. *
  3288. * <p>
  3289. * Trick play will be canceled automatically if the playhead hits the
  3290. * beginning or end of the seekable range for the content.
  3291. *
  3292. * @param {number} rate
  3293. * @export
  3294. */
  3295. trickPlay(rate) {
  3296. // A playbackRate of 0 is used internally when we are in a buffering state,
  3297. // and doesn't make sense for trick play. If you set a rate of 0 for trick
  3298. // play, we will reject it and issue a warning. If it happens during a
  3299. // test, we will fail the test through this assertion.
  3300. goog.asserts.assert(rate != 0, 'Should never set a trick play rate of 0!');
  3301. if (rate == 0) {
  3302. shaka.log.alwaysWarn('A trick play rate of 0 is unsupported!');
  3303. return;
  3304. }
  3305. if (this.video_.paused) {
  3306. // Our fast forward is implemented with playbackRate and needs the video
  3307. // to be playing (to not be paused) to take immediate effect.
  3308. // If the video is paused, "unpause" it.
  3309. this.video_.play();
  3310. }
  3311. this.playRateController_.set(rate);
  3312. if (this.loadMode_ == shaka.Player.LoadMode.MEDIA_SOURCE) {
  3313. this.abrManager_.playbackRateChanged(rate);
  3314. this.streamingEngine_.setTrickPlay(Math.abs(rate) > 1);
  3315. }
  3316. }
  3317. /**
  3318. * Cancel trick-play. If the player has not loaded content or is still loading
  3319. * content this will be a no-op.
  3320. *
  3321. * @export
  3322. */
  3323. cancelTrickPlay() {
  3324. const defaultPlaybackRate = this.playRateController_.getDefaultRate();
  3325. if (this.loadMode_ == shaka.Player.LoadMode.SRC_EQUALS) {
  3326. this.playRateController_.set(defaultPlaybackRate);
  3327. }
  3328. if (this.loadMode_ == shaka.Player.LoadMode.MEDIA_SOURCE) {
  3329. this.playRateController_.set(defaultPlaybackRate);
  3330. this.abrManager_.playbackRateChanged(defaultPlaybackRate);
  3331. this.streamingEngine_.setTrickPlay(false);
  3332. }
  3333. }
  3334. /**
  3335. * Return a list of variant tracks that can be switched to.
  3336. *
  3337. * <p>
  3338. * If the player has not loaded content, this will return an empty list.
  3339. *
  3340. * @return {!Array.<shaka.extern.Track>}
  3341. * @export
  3342. */
  3343. getVariantTracks() {
  3344. if (this.manifest_) {
  3345. const currentVariant = this.streamingEngine_ ?
  3346. this.streamingEngine_.getCurrentVariant() : null;
  3347. const tracks = [];
  3348. let activeTracks = 0;
  3349. // Convert each variant to a track.
  3350. for (const variant of this.manifest_.variants) {
  3351. if (!shaka.util.StreamUtils.isPlayable(variant)) {
  3352. continue;
  3353. }
  3354. const track = shaka.util.StreamUtils.variantToTrack(variant);
  3355. track.active = variant == currentVariant;
  3356. if (!track.active && activeTracks != 1 && currentVariant != null &&
  3357. variant.video == currentVariant.video &&
  3358. variant.audio == currentVariant.audio) {
  3359. track.active = true;
  3360. }
  3361. if (track.active) {
  3362. activeTracks++;
  3363. }
  3364. tracks.push(track);
  3365. }
  3366. goog.asserts.assert(activeTracks <= 1,
  3367. 'It should only have one active track');
  3368. return tracks;
  3369. } else if (this.video_ && this.video_.audioTracks) {
  3370. // Safari's native HLS always shows a single element in videoTracks.
  3371. // You can't use that API to change resolutions. But we can use
  3372. // audioTracks to generate a variant list that is usable for changing
  3373. // languages.
  3374. const audioTracks = Array.from(this.video_.audioTracks);
  3375. return audioTracks.map((audio) =>
  3376. shaka.util.StreamUtils.html5AudioTrackToTrack(audio));
  3377. } else {
  3378. return [];
  3379. }
  3380. }
  3381. /**
  3382. * Return a list of text tracks that can be switched to.
  3383. *
  3384. * <p>
  3385. * If the player has not loaded content, this will return an empty list.
  3386. *
  3387. * @return {!Array.<shaka.extern.Track>}
  3388. * @export
  3389. */
  3390. getTextTracks() {
  3391. if (this.manifest_) {
  3392. const currentTextStream = this.streamingEngine_ ?
  3393. this.streamingEngine_.getCurrentTextStream() : null;
  3394. const tracks = [];
  3395. // Convert all selectable text streams to tracks.
  3396. for (const text of this.manifest_.textStreams) {
  3397. const track = shaka.util.StreamUtils.textStreamToTrack(text);
  3398. track.active = text == currentTextStream;
  3399. tracks.push(track);
  3400. }
  3401. return tracks;
  3402. } else if (this.video_ && this.video_.src && this.video_.textTracks) {
  3403. const textTracks = this.getFilteredTextTracks_();
  3404. const StreamUtils = shaka.util.StreamUtils;
  3405. return textTracks.map((text) => StreamUtils.html5TextTrackToTrack(text));
  3406. } else {
  3407. return [];
  3408. }
  3409. }
  3410. /**
  3411. * Return a list of image tracks that can be switched to.
  3412. *
  3413. * If the player has not loaded content, this will return an empty list.
  3414. *
  3415. * @return {!Array.<shaka.extern.Track>}
  3416. * @export
  3417. */
  3418. getImageTracks() {
  3419. if (this.manifest_) {
  3420. const imageStreams = this.manifest_.imageStreams;
  3421. const StreamUtils = shaka.util.StreamUtils;
  3422. return imageStreams.map((image) => StreamUtils.imageStreamToTrack(image));
  3423. } else {
  3424. return [];
  3425. }
  3426. }
  3427. /**
  3428. * Return a Thumbnail object from a image track Id and time.
  3429. *
  3430. * If the player has not loaded content, this will return a null.
  3431. *
  3432. * @param {number} trackId
  3433. * @param {number} time
  3434. * @return {!Promise.<?shaka.extern.Thumbnail>}
  3435. * @export
  3436. */
  3437. async getThumbnails(trackId, time) {
  3438. if (this.manifest_) {
  3439. const imageStream = this.manifest_.imageStreams.find(
  3440. (stream) => stream.id == trackId);
  3441. if (!imageStream) {
  3442. return null;
  3443. }
  3444. if (!imageStream.segmentIndex) {
  3445. await imageStream.createSegmentIndex();
  3446. }
  3447. const referencePosition = imageStream.segmentIndex.find(time);
  3448. if (referencePosition == null) {
  3449. return null;
  3450. }
  3451. const reference = imageStream.segmentIndex.get(referencePosition);
  3452. const tilesLayout =
  3453. reference.getTilesLayout() || imageStream.tilesLayout;
  3454. // This expression is used to detect one or more numbers (0-9) followed
  3455. // by an x and after one or more numbers (0-9)
  3456. const match = /(\d+)x(\d+)/.exec(tilesLayout);
  3457. if (!match) {
  3458. shaka.log.warning('Tiles layout does not contain a valid format ' +
  3459. ' (columns x rows)');
  3460. return null;
  3461. }
  3462. const fullImageWidth = imageStream.width || 0;
  3463. const fullImageHeight = imageStream.height || 0;
  3464. const columns = parseInt(match[1], 10);
  3465. const rows = parseInt(match[2], 10);
  3466. let width = fullImageWidth / columns;
  3467. let height = fullImageHeight / rows;
  3468. const totalImages = columns * rows;
  3469. const segmentDuration = reference.trueEndTime - reference.startTime;
  3470. const thumbnailDuration =
  3471. reference.getTileDuration() || (segmentDuration / totalImages);
  3472. let thumbnailTime = reference.startTime;
  3473. let positionX = 0;
  3474. let positionY = 0;
  3475. // If the number of images in the segment is greater than 1, we have to
  3476. // find the correct image. For that we will return to the app the
  3477. // coordinates of the position of the correct image.
  3478. // Image search is always from left to right and top to bottom.
  3479. // Note: The time between images within the segment is always
  3480. // equidistant.
  3481. //
  3482. // Eg: Total images 5, tileLayout 5x1, segmentDuration 5, thumbnailTime 2
  3483. // positionX = 0.4 * fullImageWidth
  3484. // positionY = 0
  3485. if (totalImages > 1) {
  3486. const thumbnailPosition =
  3487. Math.floor((time - reference.startTime) / thumbnailDuration);
  3488. thumbnailTime = reference.startTime +
  3489. (thumbnailPosition * thumbnailDuration);
  3490. positionX = (thumbnailPosition % columns) * width;
  3491. positionY = Math.floor(thumbnailPosition / columns) * height;
  3492. }
  3493. let sprite = false;
  3494. const thumbnailSprite = reference.getThumbnailSprite();
  3495. if (thumbnailSprite) {
  3496. sprite = true;
  3497. height = thumbnailSprite.height;
  3498. positionX = thumbnailSprite.positionX;
  3499. positionY = thumbnailSprite.positionY;
  3500. width = thumbnailSprite.width;
  3501. }
  3502. return {
  3503. imageHeight: fullImageHeight,
  3504. imageWidth: fullImageWidth,
  3505. height: height,
  3506. positionX: positionX,
  3507. positionY: positionY,
  3508. startTime: thumbnailTime,
  3509. duration: thumbnailDuration,
  3510. uris: reference.getUris(),
  3511. width: width,
  3512. sprite: sprite,
  3513. };
  3514. }
  3515. return null;
  3516. }
  3517. /**
  3518. * Select a specific text track. <code>track</code> should come from a call to
  3519. * <code>getTextTracks</code>. If the track is not found, this will be a
  3520. * no-op. If the player has not loaded content, this will be a no-op.
  3521. *
  3522. * <p>
  3523. * Note that <code>AdaptationEvents</code> are not fired for manual track
  3524. * selections.
  3525. *
  3526. * @param {shaka.extern.Track} track
  3527. * @export
  3528. */
  3529. selectTextTrack(track) {
  3530. if (this.manifest_ && this.streamingEngine_) {
  3531. const stream = this.manifest_.textStreams.find(
  3532. (stream) => stream.id == track.id);
  3533. if (!stream) {
  3534. shaka.log.error('No stream with id', track.id);
  3535. return;
  3536. }
  3537. if (stream == this.streamingEngine_.getCurrentTextStream()) {
  3538. shaka.log.debug('Text track already selected.');
  3539. return;
  3540. }
  3541. // Add entries to the history.
  3542. this.addTextStreamToSwitchHistory_(stream, /* fromAdaptation= */ false);
  3543. this.streamingEngine_.switchTextStream(stream);
  3544. this.onTextChanged_();
  3545. // Workaround for
  3546. // https://github.com/shaka-project/shaka-player/issues/1299
  3547. // When track is selected, back-propagate the language to
  3548. // currentTextLanguage_.
  3549. this.currentTextLanguage_ = stream.language;
  3550. } else if (this.video_ && this.video_.src && this.video_.textTracks) {
  3551. const textTracks = this.getFilteredTextTracks_();
  3552. for (const textTrack of textTracks) {
  3553. if (shaka.util.StreamUtils.html5TrackId(textTrack) == track.id) {
  3554. // Leave the track in 'hidden' if it's selected but not showing.
  3555. textTrack.mode = this.isTextVisible_ ? 'showing' : 'hidden';
  3556. } else {
  3557. // Safari allows multiple text tracks to have mode == 'showing', so be
  3558. // explicit in resetting the others.
  3559. textTrack.mode = 'disabled';
  3560. }
  3561. }
  3562. this.onTextChanged_();
  3563. }
  3564. }
  3565. /**
  3566. * Select a specific variant track to play. <code>track</code> should come
  3567. * from a call to <code>getVariantTracks</code>. If <code>track</code> cannot
  3568. * be found, this will be a no-op. If the player has not loaded content, this
  3569. * will be a no-op.
  3570. *
  3571. * <p>
  3572. * Changing variants will take effect once the currently buffered content has
  3573. * been played. To force the change to happen sooner, use
  3574. * <code>clearBuffer</code> with <code>safeMargin</code>. Setting
  3575. * <code>clearBuffer</code> to <code>true</code> will clear all buffered
  3576. * content after <code>safeMargin</code>, allowing the new variant to start
  3577. * playing sooner.
  3578. *
  3579. * <p>
  3580. * Note that <code>AdaptationEvents</code> are not fired for manual track
  3581. * selections.
  3582. *
  3583. * @param {shaka.extern.Track} track
  3584. * @param {boolean=} clearBuffer
  3585. * @param {number=} safeMargin Optional amount of buffer (in seconds) to
  3586. * retain when clearing the buffer. Useful for switching variant quickly
  3587. * without causing a buffering event. Defaults to 0 if not provided. Ignored
  3588. * if clearBuffer is false. Can cause hiccups on some browsers if chosen too
  3589. * small, e.g. The amount of two segments is a fair minimum to consider as
  3590. * safeMargin value.
  3591. * @export
  3592. */
  3593. selectVariantTrack(track, clearBuffer = false, safeMargin = 0) {
  3594. if (this.manifest_ && this.streamingEngine_) {
  3595. if (this.config_.abr.enabled) {
  3596. shaka.log.alwaysWarn('Changing tracks while abr manager is enabled ' +
  3597. 'will likely result in the selected track ' +
  3598. 'being overriden. Consider disabling abr before ' +
  3599. 'calling selectVariantTrack().');
  3600. }
  3601. const variant = this.manifest_.variants.find(
  3602. (variant) => variant.id == track.id);
  3603. if (!variant) {
  3604. shaka.log.error('No variant with id', track.id);
  3605. return;
  3606. }
  3607. // Double check that the track is allowed to be played. The track list
  3608. // should only contain playable variants, but if restrictions change and
  3609. // |selectVariantTrack| is called before the track list is updated, we
  3610. // could get a now-restricted variant.
  3611. if (!shaka.util.StreamUtils.isPlayable(variant)) {
  3612. shaka.log.error('Unable to switch to restricted track', track.id);
  3613. return;
  3614. }
  3615. this.switchVariant_(
  3616. variant, /* fromAdaptation= */ false, clearBuffer, safeMargin);
  3617. // Workaround for
  3618. // https://github.com/shaka-project/shaka-player/issues/1299
  3619. // When track is selected, back-propagate the language to
  3620. // currentAudioLanguage_.
  3621. this.currentAdaptationSetCriteria_ = new shaka.media.ExampleBasedCriteria(
  3622. variant);
  3623. // Update AbrManager variants to match these new settings.
  3624. this.updateAbrManagerVariants_();
  3625. } else if (this.video_ && this.video_.audioTracks) {
  3626. // Safari's native HLS won't let you choose an explicit variant, though
  3627. // you can choose audio languages this way.
  3628. const audioTracks = Array.from(this.video_.audioTracks);
  3629. for (const audioTrack of audioTracks) {
  3630. if (shaka.util.StreamUtils.html5TrackId(audioTrack) == track.id) {
  3631. // This will reset the "enabled" of other tracks to false.
  3632. this.switchHtml5Track_(audioTrack);
  3633. return;
  3634. }
  3635. }
  3636. }
  3637. }
  3638. /**
  3639. * Return a list of audio language-role combinations available. If the
  3640. * player has not loaded any content, this will return an empty list.
  3641. *
  3642. * @return {!Array.<shaka.extern.LanguageRole>}
  3643. * @export
  3644. */
  3645. getAudioLanguagesAndRoles() {
  3646. return shaka.Player.getLanguageAndRolesFrom_(this.getVariantTracks());
  3647. }
  3648. /**
  3649. * Return a list of text language-role combinations available. If the player
  3650. * has not loaded any content, this will be return an empty list.
  3651. *
  3652. * @return {!Array.<shaka.extern.LanguageRole>}
  3653. * @export
  3654. */
  3655. getTextLanguagesAndRoles() {
  3656. return shaka.Player.getLanguageAndRolesFrom_(this.getTextTracks());
  3657. }
  3658. /**
  3659. * Return a list of audio languages available. If the player has not loaded
  3660. * any content, this will return an empty list.
  3661. *
  3662. * @return {!Array.<string>}
  3663. * @export
  3664. */
  3665. getAudioLanguages() {
  3666. return Array.from(shaka.Player.getLanguagesFrom_(this.getVariantTracks()));
  3667. }
  3668. /**
  3669. * Return a list of text languages available. If the player has not loaded
  3670. * any content, this will return an empty list.
  3671. *
  3672. * @return {!Array.<string>}
  3673. * @export
  3674. */
  3675. getTextLanguages() {
  3676. return Array.from(shaka.Player.getLanguagesFrom_(this.getTextTracks()));
  3677. }
  3678. /**
  3679. * Sets the current audio language and current variant role to the selected
  3680. * language, role and channel count, and chooses a new variant if need be.
  3681. * If the player has not loaded any content, this will be a no-op.
  3682. *
  3683. * @param {string} language
  3684. * @param {string=} role
  3685. * @param {number=} channelsCount
  3686. * @param {number=} safeMargin
  3687. * @export
  3688. */
  3689. selectAudioLanguage(language, role, channelsCount = 0, safeMargin = 0) {
  3690. if (this.manifest_ && this.playhead_) {
  3691. this.currentAdaptationSetCriteria_ =
  3692. new shaka.media.PreferenceBasedCriteria(language, role || '',
  3693. channelsCount, /* hdrLevel= */ '', /* label= */ '');
  3694. const diff = (a, b) => {
  3695. if (!a.video && !b.video) {
  3696. return 0;
  3697. } else if (!a.video || !b.video) {
  3698. return Infinity;
  3699. } else {
  3700. return Math.abs((a.video.height || 0) - (b.video.height || 0)) +
  3701. Math.abs((a.video.width || 0) - (b.video.width || 0));
  3702. }
  3703. };
  3704. // Find the variant whose size is closest to the active variant. This
  3705. // ensures we stay at about the same resolution when just changing the
  3706. // language/role.
  3707. const active = this.streamingEngine_.getCurrentVariant();
  3708. const set =
  3709. this.currentAdaptationSetCriteria_.create(this.manifest_.variants);
  3710. let bestVariant = null;
  3711. for (const curVariant of set.values()) {
  3712. if (!bestVariant ||
  3713. diff(bestVariant, active) > diff(curVariant, active)) {
  3714. bestVariant = curVariant;
  3715. }
  3716. }
  3717. if (bestVariant) {
  3718. const track = shaka.util.StreamUtils.variantToTrack(bestVariant);
  3719. this.selectVariantTrack(track, /* clearBuffer= */ true, safeMargin);
  3720. return;
  3721. }
  3722. // If we haven't switched yet, just use ABR to find a new track.
  3723. this.chooseVariantAndSwitch_();
  3724. } else if (this.video_ && this.video_.audioTracks) {
  3725. const track = shaka.util.StreamUtils.filterStreamsByLanguageAndRole(
  3726. this.getVariantTracks(), language, role || '', false)[0];
  3727. if (track) {
  3728. this.selectVariantTrack(track);
  3729. }
  3730. }
  3731. }
  3732. /**
  3733. * Sets the current text language and current text role to the selected
  3734. * language and role, and chooses a new variant if need be. If the player has
  3735. * not loaded any content, this will be a no-op.
  3736. *
  3737. * @param {string} language
  3738. * @param {string=} role
  3739. * @param {boolean=} forced
  3740. * @export
  3741. */
  3742. selectTextLanguage(language, role, forced = false) {
  3743. if (this.manifest_ && this.playhead_) {
  3744. this.currentTextLanguage_ = language;
  3745. this.currentTextRole_ = role || '';
  3746. this.currentTextForced_ = forced;
  3747. const chosenText = this.chooseTextStream_();
  3748. if (chosenText) {
  3749. if (chosenText == this.streamingEngine_.getCurrentTextStream()) {
  3750. shaka.log.debug('Text track already selected.');
  3751. return;
  3752. }
  3753. this.addTextStreamToSwitchHistory_(
  3754. chosenText, /* fromAdaptation= */ false);
  3755. if (this.shouldStreamText_()) {
  3756. this.streamingEngine_.switchTextStream(chosenText);
  3757. this.onTextChanged_();
  3758. }
  3759. }
  3760. } else {
  3761. const track = shaka.util.StreamUtils.filterStreamsByLanguageAndRole(
  3762. this.getTextTracks(), language, role || '', forced)[0];
  3763. if (track) {
  3764. this.selectTextTrack(track);
  3765. }
  3766. }
  3767. }
  3768. /**
  3769. * Select variant tracks that have a given label. This assumes the
  3770. * label uniquely identifies an audio stream, so all the variants
  3771. * are expected to have the same variant.audio.
  3772. *
  3773. * @param {string} label
  3774. * @param {boolean=} clearBuffer Optional clear buffer or not when
  3775. * switch to new variant
  3776. * Defaults to true if not provided
  3777. * @param {number=} safeMargin Optional amount of buffer (in seconds) to
  3778. * retain when clearing the buffer.
  3779. * Defaults to 0 if not provided. Ignored if clearBuffer is false.
  3780. * @export
  3781. */
  3782. selectVariantsByLabel(label, clearBuffer = true, safeMargin = 0) {
  3783. if (this.manifest_ && this.playhead_) {
  3784. let firstVariantWithLabel = null;
  3785. for (const variant of this.manifest_.variants) {
  3786. if (variant.audio.label == label) {
  3787. firstVariantWithLabel = variant;
  3788. break;
  3789. }
  3790. }
  3791. if (firstVariantWithLabel == null) {
  3792. shaka.log.warning('No variants were found with label: ' +
  3793. label + '. Ignoring the request to switch.');
  3794. return;
  3795. }
  3796. // Label is a unique identifier of a variant's audio stream.
  3797. // Because of that we assume that all the variants with the same
  3798. // label have the same language.
  3799. this.currentAdaptationSetCriteria_ =
  3800. new shaka.media.PreferenceBasedCriteria(
  3801. firstVariantWithLabel.language, '', 0, '', label);
  3802. this.chooseVariantAndSwitch_(clearBuffer, safeMargin);
  3803. } else if (this.video_ && this.video_.audioTracks) {
  3804. const audioTracks = Array.from(this.video_.audioTracks);
  3805. let trackMatch = null;
  3806. for (const audioTrack of audioTracks) {
  3807. if (audioTrack.label == label) {
  3808. trackMatch = audioTrack;
  3809. }
  3810. }
  3811. if (trackMatch) {
  3812. this.switchHtml5Track_(trackMatch);
  3813. }
  3814. }
  3815. }
  3816. /**
  3817. * Check if the text displayer is enabled.
  3818. *
  3819. * @return {boolean}
  3820. * @export
  3821. */
  3822. isTextTrackVisible() {
  3823. const expected = this.isTextVisible_;
  3824. if (this.mediaSourceEngine_) {
  3825. // Make sure our values are still in-sync.
  3826. const actual = this.mediaSourceEngine_.getTextDisplayer().isTextVisible();
  3827. goog.asserts.assert(
  3828. actual == expected, 'text visibility has fallen out of sync');
  3829. // Always return the actual value so that the app has the most accurate
  3830. // information (in the case that the values come out of sync in prod).
  3831. return actual;
  3832. } else if (this.video_ && this.video_.src && this.video_.textTracks) {
  3833. const textTracks = this.getFilteredTextTracks_();
  3834. return textTracks.some((t) => t.mode == 'showing');
  3835. }
  3836. return expected;
  3837. }
  3838. /**
  3839. * Return a list of chapters tracks.
  3840. *
  3841. * @return {!Array.<shaka.extern.Track>}
  3842. * @export
  3843. */
  3844. getChaptersTracks() {
  3845. if (this.video_ && this.video_.src && this.video_.textTracks) {
  3846. const textTracks = this.getChaptersTracks_();
  3847. const StreamUtils = shaka.util.StreamUtils;
  3848. return textTracks.map((text) => StreamUtils.html5TextTrackToTrack(text));
  3849. } else {
  3850. return [];
  3851. }
  3852. }
  3853. /**
  3854. * This returns the list of chapters.
  3855. *
  3856. * @param {string} language
  3857. * @return {!Array.<shaka.extern.Chapter>}
  3858. * @export
  3859. */
  3860. getChapters(language) {
  3861. const LanguageUtils = shaka.util.LanguageUtils;
  3862. const inputlanguage = LanguageUtils.normalize(language);
  3863. const chaptersTracks = this.getChaptersTracks_();
  3864. const chaptersTracksWithLanguage = chaptersTracks
  3865. .filter((t) => LanguageUtils.normalize(t.language) == inputlanguage);
  3866. if (!chaptersTracksWithLanguage || !chaptersTracksWithLanguage.length) {
  3867. return [];
  3868. }
  3869. const chapters = [];
  3870. const uniqueChapters = new Set();
  3871. for (const chaptersTrack of chaptersTracksWithLanguage) {
  3872. if (chaptersTrack && chaptersTrack.cues) {
  3873. for (const cue of chaptersTrack.cues) {
  3874. let id = cue.id;
  3875. if (!id || id == '') {
  3876. id = cue.startTime + '-' + cue.endTime + '-' + cue.text;
  3877. }
  3878. /** @type {shaka.extern.Chapter} */
  3879. const chapter = {
  3880. id: id,
  3881. title: cue.text,
  3882. startTime: cue.startTime,
  3883. endTime: cue.endTime,
  3884. };
  3885. if (!uniqueChapters.has(id)) {
  3886. chapters.push(chapter);
  3887. uniqueChapters.add(id);
  3888. }
  3889. }
  3890. }
  3891. }
  3892. return chapters;
  3893. }
  3894. /**
  3895. * Ignore the TextTracks with the 'metadata' or 'chapters' kind, or the one
  3896. * generated by the SimpleTextDisplayer.
  3897. *
  3898. * @return {!Array.<TextTrack>}
  3899. * @private
  3900. */
  3901. getFilteredTextTracks_() {
  3902. goog.asserts.assert(this.video_.textTracks,
  3903. 'TextTracks should be valid.');
  3904. return Array.from(this.video_.textTracks)
  3905. .filter((t) => t.kind != 'metadata' && t.kind != 'chapters' &&
  3906. t.label != shaka.Player.TextTrackLabel);
  3907. }
  3908. /**
  3909. * Get the TextTracks with the 'metadata' kind.
  3910. *
  3911. * @return {!Array.<TextTrack>}
  3912. * @private
  3913. */
  3914. getMetadataTracks_() {
  3915. goog.asserts.assert(this.video_.textTracks,
  3916. 'TextTracks should be valid.');
  3917. return Array.from(this.video_.textTracks)
  3918. .filter((t) => t.kind == 'metadata');
  3919. }
  3920. /**
  3921. * Get the TextTracks with the 'chapters' kind.
  3922. *
  3923. * @return {!Array.<TextTrack>}
  3924. * @private
  3925. */
  3926. getChaptersTracks_() {
  3927. goog.asserts.assert(this.video_.textTracks,
  3928. 'TextTracks should be valid.');
  3929. return Array.from(this.video_.textTracks)
  3930. .filter((t) => t.kind == 'chapters');
  3931. }
  3932. /**
  3933. * Enable or disable the text displayer. If the player is in an unloaded
  3934. * state, the request will be applied next time content is loaded.
  3935. *
  3936. * @param {boolean} isVisible
  3937. * @export
  3938. */
  3939. setTextTrackVisibility(isVisible) {
  3940. const oldVisibilty = this.isTextVisible_;
  3941. // Convert to boolean in case apps pass 0/1 instead false/true.
  3942. const newVisibility = !!isVisible;
  3943. if (oldVisibilty == newVisibility) {
  3944. return;
  3945. }
  3946. this.isTextVisible_ = newVisibility;
  3947. // Hold of on setting the text visibility until we have all the components
  3948. // we need. This ensures that they stay in-sync.
  3949. if (this.loadMode_ == shaka.Player.LoadMode.MEDIA_SOURCE) {
  3950. this.mediaSourceEngine_.getTextDisplayer()
  3951. .setTextVisibility(newVisibility);
  3952. // When the user wants to see captions, we stream captions. When the user
  3953. // doesn't want to see captions, we don't stream captions. This is to
  3954. // avoid bandwidth consumption by an unused resource. The app developer
  3955. // can override this and configure us to always stream captions.
  3956. if (!this.config_.streaming.alwaysStreamText) {
  3957. if (newVisibility) {
  3958. if (this.streamingEngine_.getCurrentTextStream()) {
  3959. // We already have a selected text stream.
  3960. } else {
  3961. // Find the text stream that best matches the user's preferences.
  3962. const streams =
  3963. shaka.util.StreamUtils.filterStreamsByLanguageAndRole(
  3964. this.manifest_.textStreams,
  3965. this.currentTextLanguage_,
  3966. this.currentTextRole_,
  3967. this.currentTextForced_);
  3968. // It is possible that there are no streams to play.
  3969. if (streams.length > 0) {
  3970. this.streamingEngine_.switchTextStream(streams[0]);
  3971. this.onTextChanged_();
  3972. }
  3973. }
  3974. } else {
  3975. this.streamingEngine_.unloadTextStream();
  3976. }
  3977. }
  3978. } else if (this.video_ && this.video_.src && this.video_.textTracks) {
  3979. const textTracks = this.getFilteredTextTracks_();
  3980. // Find the active track by looking for one which is not disabled. This
  3981. // is the only way to identify the track which is currently displayed.
  3982. // Set it to 'showing' or 'hidden' based on newVisibility.
  3983. for (const textTrack of textTracks) {
  3984. if (textTrack.mode != 'disabled') {
  3985. textTrack.mode = newVisibility ? 'showing' : 'hidden';
  3986. }
  3987. }
  3988. }
  3989. // We need to fire the event after we have updated everything so that
  3990. // everything will be in a stable state when the app responds to the
  3991. // event.
  3992. this.onTextTrackVisibility_();
  3993. }
  3994. /**
  3995. * Get the current playhead position as a date. This should only be called
  3996. * when the player has loaded a live stream. If the player has not loaded a
  3997. * live stream, this will return <code>null</code>.
  3998. *
  3999. * @return {Date}
  4000. * @export
  4001. */
  4002. getPlayheadTimeAsDate() {
  4003. if (!this.isLive()) {
  4004. shaka.log.warning('getPlayheadTimeAsDate is for live streams!');
  4005. return null;
  4006. }
  4007. const walkerPayload = this.walker_.getCurrentPayload();
  4008. let presentationTime = 0;
  4009. if (this.playhead_) {
  4010. presentationTime = this.playhead_.getTime();
  4011. } else if (walkerPayload) {
  4012. if (walkerPayload.startTime == null) {
  4013. // A live stream with no requested start time and no playhead yet. We
  4014. // would start at the live edge, but we don't have that yet, so return
  4015. // the current date & time.
  4016. return new Date();
  4017. } else {
  4018. // A specific start time has been requested. This is what Playhead will
  4019. // use once it is created.
  4020. presentationTime = walkerPayload.startTime;
  4021. }
  4022. }
  4023. if (this.manifest_) {
  4024. const timeline = this.manifest_.presentationTimeline;
  4025. const startTime = timeline.getPresentationStartTime();
  4026. return new Date(/* ms= */ (startTime + presentationTime) * 1000);
  4027. } else if (this.video_ && this.video_.getStartDate) {
  4028. // Apple's native HLS gives us getStartDate(), which is only available if
  4029. // EXT-X-PROGRAM-DATETIME is in the playlist.
  4030. const startDate = this.video_.getStartDate();
  4031. if (isNaN(startDate.getTime())) {
  4032. shaka.log.warning(
  4033. 'EXT-X-PROGRAM-DATETIME required to get playhead time as Date!');
  4034. return null;
  4035. }
  4036. return new Date(startDate.getTime() + (presentationTime * 1000));
  4037. } else {
  4038. shaka.log.warning('No way to get playhead time as Date!');
  4039. return null;
  4040. }
  4041. }
  4042. /**
  4043. * Get the presentation start time as a date. This should only be called when
  4044. * the player has loaded a live stream. If the player has not loaded a live
  4045. * stream, this will return <code>null</code>.
  4046. *
  4047. * @return {Date}
  4048. * @export
  4049. */
  4050. getPresentationStartTimeAsDate() {
  4051. if (!this.isLive()) {
  4052. shaka.log.warning('getPresentationStartTimeAsDate is for live streams!');
  4053. return null;
  4054. }
  4055. if (this.manifest_) {
  4056. const timeline = this.manifest_.presentationTimeline;
  4057. const startTime = timeline.getPresentationStartTime();
  4058. goog.asserts.assert(startTime != null,
  4059. 'Presentation start time should not be null!');
  4060. return new Date(/* ms= */ startTime * 1000);
  4061. } else if (this.video_ && this.video_.getStartDate) {
  4062. // Apple's native HLS gives us getStartDate(), which is only available if
  4063. // EXT-X-PROGRAM-DATETIME is in the playlist.
  4064. const startDate = this.video_.getStartDate();
  4065. if (isNaN(startDate.getTime())) {
  4066. shaka.log.warning(
  4067. 'EXT-X-PROGRAM-DATETIME required to get presentation start time ' +
  4068. 'as Date!');
  4069. return null;
  4070. }
  4071. return startDate;
  4072. } else {
  4073. shaka.log.warning('No way to get presentation start time as Date!');
  4074. return null;
  4075. }
  4076. }
  4077. /**
  4078. * Get information about what the player has buffered. If the player has not
  4079. * loaded content or is currently loading content, the buffered content will
  4080. * be empty.
  4081. *
  4082. * @return {shaka.extern.BufferedInfo}
  4083. * @export
  4084. */
  4085. getBufferedInfo() {
  4086. if (this.loadMode_ == shaka.Player.LoadMode.MEDIA_SOURCE) {
  4087. return this.mediaSourceEngine_.getBufferedInfo();
  4088. }
  4089. const info = {
  4090. total: [],
  4091. audio: [],
  4092. video: [],
  4093. text: [],
  4094. };
  4095. if (this.loadMode_ == shaka.Player.LoadMode.SRC_EQUALS) {
  4096. const TimeRangesUtils = shaka.media.TimeRangesUtils;
  4097. info.total = TimeRangesUtils.getBufferedInfo(this.video_.buffered);
  4098. }
  4099. return info;
  4100. }
  4101. /**
  4102. * Get statistics for the current playback session. If the player is not
  4103. * playing content, this will return an empty stats object.
  4104. *
  4105. * @return {shaka.extern.Stats}
  4106. * @export
  4107. */
  4108. getStats() {
  4109. // If the Player is not in a fully-loaded state, then return an empty stats
  4110. // blob so that this call will never fail.
  4111. const loaded = this.loadMode_ == shaka.Player.LoadMode.MEDIA_SOURCE ||
  4112. this.loadMode_ == shaka.Player.LoadMode.SRC_EQUALS;
  4113. if (!loaded) {
  4114. return shaka.util.Stats.getEmptyBlob();
  4115. }
  4116. this.updateStateHistory_();
  4117. goog.asserts.assert(this.video_, 'If we have stats, we should have video_');
  4118. const element = /** @type {!HTMLVideoElement} */ (this.video_);
  4119. const completionRatio = element.currentTime / element.duration;
  4120. if (!isNaN(completionRatio)) {
  4121. this.stats_.setCompletionPercent(Math.round(100 * completionRatio));
  4122. }
  4123. if (this.playhead_) {
  4124. this.stats_.setGapsJumped(this.playhead_.getGapsJumped());
  4125. this.stats_.setStallsDetected(this.playhead_.getStallsDetected());
  4126. }
  4127. if (element.getVideoPlaybackQuality) {
  4128. const info = element.getVideoPlaybackQuality();
  4129. this.stats_.setDroppedFrames(
  4130. Number(info.droppedVideoFrames),
  4131. Number(info.totalVideoFrames));
  4132. this.stats_.setCorruptedFrames(Number(info.corruptedVideoFrames));
  4133. }
  4134. const licenseSeconds =
  4135. this.drmEngine_ ? this.drmEngine_.getLicenseTime() : NaN;
  4136. this.stats_.setLicenseTime(licenseSeconds);
  4137. if (this.loadMode_ == shaka.Player.LoadMode.MEDIA_SOURCE) {
  4138. // Event through we are loaded, it is still possible that we don't have a
  4139. // variant yet because we set the load mode before we select the first
  4140. // variant to stream.
  4141. const variant = this.streamingEngine_.getCurrentVariant();
  4142. if (variant) {
  4143. const rate = this.playRateController_ ?
  4144. this.playRateController_.getRealRate() : 1;
  4145. const variantBandwidth = rate * variant.bandwidth;
  4146. // TODO: Should include text bandwidth if it enabled.
  4147. const currentStreamBandwidth = variantBandwidth;
  4148. this.stats_.setCurrentStreamBandwidth(currentStreamBandwidth);
  4149. }
  4150. if (variant && variant.video) {
  4151. this.stats_.setResolution(
  4152. /* width= */ variant.video.width || NaN,
  4153. /* height= */ variant.video.height || NaN);
  4154. }
  4155. if (this.isLive()) {
  4156. const now = this.getPresentationStartTimeAsDate().valueOf() +
  4157. this.seekRange().end * 1000;
  4158. const latency = (Date.now() - now) / 1000;
  4159. this.stats_.setLiveLatency(latency);
  4160. }
  4161. if (this.manifest_ && this.manifest_.presentationTimeline) {
  4162. const maxSegmentDuration =
  4163. this.manifest_.presentationTimeline.getMaxSegmentDuration();
  4164. this.stats_.setMaxSegmentDuration(maxSegmentDuration);
  4165. }
  4166. const estimate = this.abrManager_.getBandwidthEstimate();
  4167. this.stats_.setBandwidthEstimate(estimate);
  4168. }
  4169. if (this.loadMode_ == shaka.Player.LoadMode.SRC_EQUALS) {
  4170. this.stats_.setResolution(
  4171. /* width= */ element.videoWidth || NaN,
  4172. /* height= */ element.videoHeight || NaN);
  4173. }
  4174. return this.stats_.getBlob();
  4175. }
  4176. /**
  4177. * Adds the given text track to the loaded manifest. <code>load()</code> must
  4178. * resolve before calling. The presentation must have a duration.
  4179. *
  4180. * This returns the created track, which can immediately be selected by the
  4181. * application. The track will not be automatically selected.
  4182. *
  4183. * @param {string} uri
  4184. * @param {string} language
  4185. * @param {string} kind
  4186. * @param {string=} mimeType
  4187. * @param {string=} codec
  4188. * @param {string=} label
  4189. * @param {boolean=} forced
  4190. * @return {!Promise.<shaka.extern.Track>}
  4191. * @export
  4192. */
  4193. async addTextTrackAsync(uri, language, kind, mimeType, codec, label,
  4194. forced = false) {
  4195. if (this.loadMode_ != shaka.Player.LoadMode.MEDIA_SOURCE &&
  4196. this.loadMode_ != shaka.Player.LoadMode.SRC_EQUALS) {
  4197. shaka.log.error(
  4198. 'Must call load() and wait for it to resolve before adding text ' +
  4199. 'tracks.');
  4200. throw new shaka.util.Error(
  4201. shaka.util.Error.Severity.RECOVERABLE,
  4202. shaka.util.Error.Category.PLAYER,
  4203. shaka.util.Error.Code.CONTENT_NOT_LOADED);
  4204. }
  4205. if (!mimeType) {
  4206. mimeType = await this.getTextMimetype_(uri);
  4207. }
  4208. let adCuePoints = [];
  4209. if (this.adManager_) {
  4210. try {
  4211. adCuePoints = this.adManager_.getServerSideCuePoints();
  4212. } catch (error) {}
  4213. }
  4214. if (this.loadMode_ == shaka.Player.LoadMode.SRC_EQUALS) {
  4215. if (forced) {
  4216. // See: https://github.com/whatwg/html/issues/4472
  4217. kind = 'forced';
  4218. }
  4219. await this.addSrcTrackElement_(uri, language, kind, mimeType, label || '',
  4220. adCuePoints);
  4221. const textTracks = this.getTextTracks();
  4222. const srcTrack = textTracks.find((t) => {
  4223. return t.language == language &&
  4224. t.label == (label || '') &&
  4225. t.kind == kind;
  4226. });
  4227. if (srcTrack) {
  4228. this.onTracksChanged_();
  4229. return srcTrack;
  4230. }
  4231. // This should not happen, but there are browser implementations that may
  4232. // not support the Track element.
  4233. shaka.log.error('Cannot add this text when loaded with src=');
  4234. throw new shaka.util.Error(
  4235. shaka.util.Error.Severity.RECOVERABLE,
  4236. shaka.util.Error.Category.TEXT,
  4237. shaka.util.Error.Code.CANNOT_ADD_EXTERNAL_TEXT_TO_SRC_EQUALS);
  4238. }
  4239. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  4240. const duration = this.manifest_.presentationTimeline.getDuration();
  4241. if (duration == Infinity) {
  4242. throw new shaka.util.Error(
  4243. shaka.util.Error.Severity.RECOVERABLE,
  4244. shaka.util.Error.Category.MANIFEST,
  4245. shaka.util.Error.Code.CANNOT_ADD_EXTERNAL_TEXT_TO_LIVE_STREAM);
  4246. }
  4247. if (adCuePoints.length) {
  4248. goog.asserts.assert(
  4249. this.networkingEngine_, 'Need networking engine.');
  4250. const data = await this.getTextData_(uri,
  4251. this.networkingEngine_,
  4252. this.config_.streaming.retryParameters);
  4253. const vvtText = this.convertToWebVTT_(data, mimeType, adCuePoints);
  4254. const blob = new Blob([vvtText], {type: 'text/vtt'});
  4255. uri = shaka.media.MediaSourceEngine.createObjectURL(blob);
  4256. mimeType = 'text/vtt';
  4257. }
  4258. /** @type {shaka.extern.Stream} */
  4259. const stream = {
  4260. id: this.nextExternalStreamId_++,
  4261. originalId: null,
  4262. createSegmentIndex: () => Promise.resolve(),
  4263. segmentIndex: shaka.media.SegmentIndex.forSingleSegment(
  4264. /* startTime= */ 0,
  4265. /* duration= */ duration,
  4266. /* uris= */ [uri]),
  4267. mimeType: mimeType || '',
  4268. codecs: codec || '',
  4269. kind: kind,
  4270. encrypted: false,
  4271. drmInfos: [],
  4272. keyIds: new Set(),
  4273. language: language,
  4274. originalLanguage: language,
  4275. label: label || null,
  4276. type: ContentType.TEXT,
  4277. primary: false,
  4278. trickModeVideo: null,
  4279. emsgSchemeIdUris: null,
  4280. roles: [],
  4281. forced: !!forced,
  4282. channelsCount: null,
  4283. audioSamplingRate: null,
  4284. spatialAudio: false,
  4285. closedCaptions: null,
  4286. accessibilityPurpose: null,
  4287. external: true,
  4288. };
  4289. const fullMimeType = shaka.util.MimeUtils.getFullType(
  4290. stream.mimeType, stream.codecs);
  4291. const supported = shaka.text.TextEngine.isTypeSupported(fullMimeType);
  4292. if (!supported) {
  4293. throw new shaka.util.Error(
  4294. shaka.util.Error.Severity.CRITICAL,
  4295. shaka.util.Error.Category.TEXT,
  4296. shaka.util.Error.Code.MISSING_TEXT_PLUGIN,
  4297. mimeType);
  4298. }
  4299. this.manifest_.textStreams.push(stream);
  4300. this.onTracksChanged_();
  4301. return shaka.util.StreamUtils.textStreamToTrack(stream);
  4302. }
  4303. /**
  4304. * Adds the given thumbnails track to the loaded manifest.
  4305. * <code>load()</code> must resolve before calling. The presentation must
  4306. * have a duration.
  4307. *
  4308. * This returns the created track, which can immediately be used by the
  4309. * application.
  4310. *
  4311. * @param {string} uri
  4312. * @param {string=} mimeType
  4313. * @return {!Promise.<shaka.extern.Track>}
  4314. * @export
  4315. */
  4316. async addThumbnailsTrack(uri, mimeType) {
  4317. if (this.loadMode_ != shaka.Player.LoadMode.MEDIA_SOURCE &&
  4318. this.loadMode_ != shaka.Player.LoadMode.SRC_EQUALS) {
  4319. shaka.log.error(
  4320. 'Must call load() and wait for it to resolve before adding image ' +
  4321. 'tracks.');
  4322. throw new shaka.util.Error(
  4323. shaka.util.Error.Severity.RECOVERABLE,
  4324. shaka.util.Error.Category.PLAYER,
  4325. shaka.util.Error.Code.CONTENT_NOT_LOADED);
  4326. }
  4327. if (this.loadMode_ == shaka.Player.LoadMode.SRC_EQUALS) {
  4328. shaka.log.error('Cannot add this thumbnail track when loaded with src=');
  4329. throw new shaka.util.Error(
  4330. shaka.util.Error.Severity.RECOVERABLE,
  4331. shaka.util.Error.Category.TEXT,
  4332. shaka.util.Error.Code.CANNOT_ADD_EXTERNAL_THUMBNAILS_TO_SRC_EQUALS);
  4333. }
  4334. if (!mimeType) {
  4335. mimeType = await this.getTextMimetype_(uri);
  4336. }
  4337. if (mimeType != 'text/vtt') {
  4338. throw new shaka.util.Error(
  4339. shaka.util.Error.Severity.RECOVERABLE,
  4340. shaka.util.Error.Category.TEXT,
  4341. shaka.util.Error.Code.UNSUPPORTED_EXTERNAL_THUMBNAILS_URI,
  4342. uri);
  4343. }
  4344. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  4345. const duration = this.manifest_.presentationTimeline.getDuration();
  4346. if (duration == Infinity) {
  4347. throw new shaka.util.Error(
  4348. shaka.util.Error.Severity.RECOVERABLE,
  4349. shaka.util.Error.Category.MANIFEST,
  4350. shaka.util.Error.Code.CANNOT_ADD_EXTERNAL_THUMBNAILS_TO_LIVE_STREAM);
  4351. }
  4352. goog.asserts.assert(
  4353. this.networkingEngine_, 'Need networking engine.');
  4354. const buffer = await this.getTextData_(uri,
  4355. this.networkingEngine_,
  4356. this.config_.streaming.retryParameters);
  4357. const factory = shaka.text.TextEngine.findParser(mimeType);
  4358. if (!factory) {
  4359. throw new shaka.util.Error(
  4360. shaka.util.Error.Severity.CRITICAL,
  4361. shaka.util.Error.Category.TEXT,
  4362. shaka.util.Error.Code.MISSING_TEXT_PLUGIN,
  4363. mimeType);
  4364. }
  4365. const TextParser = factory();
  4366. const time = {
  4367. periodStart: 0,
  4368. segmentStart: 0,
  4369. segmentEnd: duration,
  4370. vttOffset: 0,
  4371. };
  4372. const data = shaka.util.BufferUtils.toUint8(buffer);
  4373. const cues = TextParser.parseMedia(data, time);
  4374. const references = [];
  4375. for (const cue of cues) {
  4376. const imageUri = shaka.util.ManifestParserUtils.resolveUris(
  4377. [uri], [cue.payload])[0];
  4378. const reference = new shaka.media.SegmentReference(
  4379. cue.startTime,
  4380. cue.endTime,
  4381. () => [imageUri],
  4382. /* startByte= */ 0,
  4383. /* endByte= */ null,
  4384. /* initSegmentReference= */ null,
  4385. /* timestampOffset= */ 0,
  4386. /* appendWindowStart= */ 0,
  4387. /* appendWindowEnd= */ Infinity,
  4388. );
  4389. if (imageUri.includes('#xywh')) {
  4390. const spriteInfo = imageUri.split('#xywh=')[1].split(',');
  4391. if (spriteInfo.length === 4) {
  4392. reference.setThumbnailSprite({
  4393. height: parseInt(spriteInfo[3], 10),
  4394. positionX: parseInt(spriteInfo[0], 10),
  4395. positionY: parseInt(spriteInfo[1], 10),
  4396. width: parseInt(spriteInfo[2], 10),
  4397. });
  4398. }
  4399. }
  4400. references.push(reference);
  4401. }
  4402. /** @type {shaka.extern.Stream} */
  4403. const stream = {
  4404. id: this.nextExternalStreamId_++,
  4405. originalId: null,
  4406. createSegmentIndex: () => Promise.resolve(),
  4407. segmentIndex: new shaka.media.SegmentIndex(references),
  4408. mimeType: mimeType || '',
  4409. codecs: '',
  4410. kind: '',
  4411. encrypted: false,
  4412. drmInfos: [],
  4413. keyIds: new Set(),
  4414. language: 'und',
  4415. originalLanguage: null,
  4416. label: null,
  4417. type: ContentType.IMAGE,
  4418. primary: false,
  4419. trickModeVideo: null,
  4420. emsgSchemeIdUris: null,
  4421. roles: [],
  4422. forced: false,
  4423. channelsCount: null,
  4424. audioSamplingRate: null,
  4425. spatialAudio: false,
  4426. closedCaptions: null,
  4427. tilesLayout: '1x1',
  4428. accessibilityPurpose: null,
  4429. external: true,
  4430. };
  4431. this.manifest_.imageStreams.push(stream);
  4432. this.onTracksChanged_();
  4433. return shaka.util.StreamUtils.imageStreamToTrack(stream);
  4434. }
  4435. /**
  4436. * Adds the given chapters track to the loaded manifest. <code>load()</code>
  4437. * must resolve before calling. The presentation must have a duration.
  4438. *
  4439. * This returns the created track.
  4440. *
  4441. * @param {string} uri
  4442. * @param {string} language
  4443. * @param {string=} mimeType
  4444. * @return {!Promise.<shaka.extern.Track>}
  4445. * @export
  4446. */
  4447. async addChaptersTrack(uri, language, mimeType) {
  4448. if (this.loadMode_ != shaka.Player.LoadMode.MEDIA_SOURCE &&
  4449. this.loadMode_ != shaka.Player.LoadMode.SRC_EQUALS) {
  4450. shaka.log.error(
  4451. 'Must call load() and wait for it to resolve before adding ' +
  4452. 'chapters tracks.');
  4453. throw new shaka.util.Error(
  4454. shaka.util.Error.Severity.RECOVERABLE,
  4455. shaka.util.Error.Category.PLAYER,
  4456. shaka.util.Error.Code.CONTENT_NOT_LOADED);
  4457. }
  4458. if (!mimeType) {
  4459. mimeType = await this.getTextMimetype_(uri);
  4460. }
  4461. let adCuePoints = [];
  4462. if (this.adManager_) {
  4463. try {
  4464. adCuePoints = this.adManager_.getServerSideCuePoints();
  4465. } catch (error) {}
  4466. }
  4467. /** @type {!HTMLTrackElement} */
  4468. const trackElement = await this.addSrcTrackElement_(
  4469. uri, language, /* kind= */ 'chapters', mimeType, /* label= */ '',
  4470. adCuePoints);
  4471. const chaptersTracks = this.getChaptersTracks();
  4472. const chaptersTrack = chaptersTracks.find((t) => {
  4473. return t.language == language;
  4474. });
  4475. if (chaptersTrack) {
  4476. await new Promise((resolve, reject) => {
  4477. // The chapter data isn't available until the 'load' event fires, and
  4478. // that won't happen until the chapters track is activated by the
  4479. // activateChaptersTrack_ method.
  4480. this.loadEventManager_.listenOnce(trackElement, 'load', resolve);
  4481. this.loadEventManager_.listenOnce(trackElement, 'error', (event) => {
  4482. reject(new shaka.util.Error(
  4483. shaka.util.Error.Severity.RECOVERABLE,
  4484. shaka.util.Error.Category.TEXT,
  4485. shaka.util.Error.Code.CHAPTERS_TRACK_FAILED));
  4486. });
  4487. });
  4488. return chaptersTrack;
  4489. }
  4490. // This should not happen, but there are browser implementations that may
  4491. // not support the Track element.
  4492. shaka.log.error('Cannot add this text when loaded with src=');
  4493. throw new shaka.util.Error(
  4494. shaka.util.Error.Severity.RECOVERABLE,
  4495. shaka.util.Error.Category.TEXT,
  4496. shaka.util.Error.Code.CANNOT_ADD_EXTERNAL_TEXT_TO_SRC_EQUALS);
  4497. }
  4498. /**
  4499. * @param {string} uri
  4500. * @return {!Promise.<string>}
  4501. * @private
  4502. */
  4503. async getTextMimetype_(uri) {
  4504. // Try using the uri extension.
  4505. const extension = shaka.media.ManifestParser.getExtension(uri);
  4506. let mimeType = shaka.Player.TEXT_EXTENSIONS_TO_MIME_TYPES_[extension];
  4507. if (mimeType) {
  4508. return mimeType;
  4509. }
  4510. try {
  4511. goog.asserts.assert(
  4512. this.networkingEngine_, 'Need networking engine.');
  4513. // eslint-disable-next-line require-atomic-updates
  4514. mimeType = await shaka.media.ManifestParser.getMimeType(uri,
  4515. this.networkingEngine_,
  4516. this.config_.streaming.retryParameters);
  4517. } catch (error) {}
  4518. if (mimeType) {
  4519. return mimeType;
  4520. }
  4521. shaka.log.error(
  4522. 'The mimeType has not been provided and it could not be deduced ' +
  4523. 'from its extension.');
  4524. throw new shaka.util.Error(
  4525. shaka.util.Error.Severity.RECOVERABLE,
  4526. shaka.util.Error.Category.TEXT,
  4527. shaka.util.Error.Code.TEXT_COULD_NOT_GUESS_MIME_TYPE,
  4528. extension);
  4529. }
  4530. /**
  4531. * @param {string} uri
  4532. * @param {string} language
  4533. * @param {string} kind
  4534. * @param {string} mimeType
  4535. * @param {string} label
  4536. * @param {!Array.<!shaka.extern.AdCuePoint>} adCuePoints
  4537. * @return {!Promise.<!HTMLTrackElement>}
  4538. * @private
  4539. */
  4540. async addSrcTrackElement_(uri, language, kind, mimeType, label,
  4541. adCuePoints) {
  4542. if (mimeType != 'text/vtt' || adCuePoints.length) {
  4543. goog.asserts.assert(
  4544. this.networkingEngine_, 'Need networking engine.');
  4545. const data = await this.getTextData_(uri,
  4546. this.networkingEngine_,
  4547. this.config_.streaming.retryParameters);
  4548. const vvtText = this.convertToWebVTT_(data, mimeType, adCuePoints);
  4549. const blob = new Blob([vvtText], {type: 'text/vtt'});
  4550. uri = shaka.media.MediaSourceEngine.createObjectURL(blob);
  4551. mimeType = 'text/vtt';
  4552. }
  4553. const trackElement =
  4554. /** @type {!HTMLTrackElement} */(document.createElement('track'));
  4555. trackElement.src = this.cmcdManager_.appendTextTrackData(uri);
  4556. trackElement.label = label;
  4557. trackElement.kind = kind;
  4558. trackElement.srclang = language;
  4559. // Because we're pulling in the text track file via Javascript, the
  4560. // same-origin policy applies. If you'd like to have a player served
  4561. // from one domain, but the text track served from another, you'll
  4562. // need to enable CORS in order to do so. In addition to enabling CORS
  4563. // on the server serving the text tracks, you will need to add the
  4564. // crossorigin attribute to the video element itself.
  4565. if (!this.video_.getAttribute('crossorigin')) {
  4566. this.video_.setAttribute('crossorigin', 'anonymous');
  4567. }
  4568. this.video_.appendChild(trackElement);
  4569. return trackElement;
  4570. }
  4571. /**
  4572. * @param {string} uri
  4573. * @param {!shaka.net.NetworkingEngine} netEngine
  4574. * @param {shaka.extern.RetryParameters} retryParams
  4575. * @return {!Promise.<BufferSource>}
  4576. * @private
  4577. */
  4578. async getTextData_(uri, netEngine, retryParams) {
  4579. const type = shaka.net.NetworkingEngine.RequestType.SEGMENT;
  4580. const request = shaka.net.NetworkingEngine.makeRequest([uri], retryParams);
  4581. request.method = 'GET';
  4582. this.cmcdManager_.applyTextData(request);
  4583. const response = await netEngine.request(type, request).promise;
  4584. return response.data;
  4585. }
  4586. /**
  4587. * Converts an input string to a WebVTT format string.
  4588. *
  4589. * @param {BufferSource} buffer
  4590. * @param {string} mimeType
  4591. * @param {!Array.<!shaka.extern.AdCuePoint>} adCuePoints
  4592. * @return {string}
  4593. * @private
  4594. */
  4595. convertToWebVTT_(buffer, mimeType, adCuePoints) {
  4596. const factory = shaka.text.TextEngine.findParser(mimeType);
  4597. if (factory) {
  4598. const obj = factory();
  4599. const time = {
  4600. periodStart: 0,
  4601. segmentStart: 0,
  4602. segmentEnd: this.video_.duration,
  4603. vttOffset: 0,
  4604. };
  4605. const data = shaka.util.BufferUtils.toUint8(buffer);
  4606. const cues = obj.parseMedia(data, time);
  4607. return shaka.text.WebVttGenerator.convert(cues, adCuePoints);
  4608. }
  4609. throw new shaka.util.Error(
  4610. shaka.util.Error.Severity.CRITICAL,
  4611. shaka.util.Error.Category.TEXT,
  4612. shaka.util.Error.Code.MISSING_TEXT_PLUGIN,
  4613. mimeType);
  4614. }
  4615. /**
  4616. * Set the maximum resolution that the platform's hardware can handle.
  4617. * This will be called automatically by <code>shaka.cast.CastReceiver</code>
  4618. * to enforce limitations of the Chromecast hardware.
  4619. *
  4620. * @param {number} width
  4621. * @param {number} height
  4622. * @export
  4623. */
  4624. setMaxHardwareResolution(width, height) {
  4625. this.maxHwRes_.width = width;
  4626. this.maxHwRes_.height = height;
  4627. }
  4628. /**
  4629. * Retry streaming after a streaming failure has occurred. When the player has
  4630. * not loaded content or is loading content, this will be a no-op and will
  4631. * return <code>false</code>.
  4632. *
  4633. * <p>
  4634. * If the player has loaded content, and streaming has not seen an error, this
  4635. * will return <code>false</code>.
  4636. *
  4637. * <p>
  4638. * If the player has loaded content, and streaming seen an error, but the
  4639. * could not resume streaming, this will return <code>false</code>.
  4640. *
  4641. * @param {number=} retryDelaySeconds
  4642. * @return {boolean}
  4643. * @export
  4644. */
  4645. retryStreaming(retryDelaySeconds = 0.1) {
  4646. return this.loadMode_ == shaka.Player.LoadMode.MEDIA_SOURCE ?
  4647. this.streamingEngine_.retry(retryDelaySeconds) :
  4648. false;
  4649. }
  4650. /**
  4651. * Get the manifest that the player has loaded. If the player has not loaded
  4652. * any content, this will return <code>null</code>.
  4653. *
  4654. * NOTE: This structure is NOT covered by semantic versioning compatibility
  4655. * guarantees. It may change at any time!
  4656. *
  4657. * This is marked as deprecated to warn Closure Compiler users at compile-time
  4658. * to avoid using this method.
  4659. *
  4660. * @return {?shaka.extern.Manifest}
  4661. * @export
  4662. * @deprecated
  4663. */
  4664. getManifest() {
  4665. shaka.log.alwaysWarn(
  4666. 'Shaka Player\'s internal Manifest structure is NOT covered by ' +
  4667. 'semantic versioning compatibility guarantees. It may change at any ' +
  4668. 'time! Please consider filing a feature request for whatever you ' +
  4669. 'use getManifest() for.');
  4670. return this.manifest_;
  4671. }
  4672. /**
  4673. * Get the type of manifest parser that the player is using. If the player has
  4674. * not loaded any content, this will return <code>null</code>.
  4675. *
  4676. * @return {?shaka.extern.ManifestParser.Factory}
  4677. * @export
  4678. */
  4679. getManifestParserFactory() {
  4680. return this.parserFactory_;
  4681. }
  4682. /**
  4683. * @param {shaka.extern.Variant} variant
  4684. * @param {boolean} fromAdaptation
  4685. * @private
  4686. */
  4687. addVariantToSwitchHistory_(variant, fromAdaptation) {
  4688. const switchHistory = this.stats_.getSwitchHistory();
  4689. switchHistory.updateCurrentVariant(variant, fromAdaptation);
  4690. }
  4691. /**
  4692. * @param {shaka.extern.Stream} textStream
  4693. * @param {boolean} fromAdaptation
  4694. * @private
  4695. */
  4696. addTextStreamToSwitchHistory_(textStream, fromAdaptation) {
  4697. const switchHistory = this.stats_.getSwitchHistory();
  4698. switchHistory.updateCurrentText(textStream, fromAdaptation);
  4699. }
  4700. /**
  4701. * @return {shaka.extern.PlayerConfiguration}
  4702. * @private
  4703. */
  4704. defaultConfig_() {
  4705. const config = shaka.util.PlayerConfiguration.createDefault();
  4706. config.streaming.failureCallback = (error) => {
  4707. this.defaultStreamingFailureCallback_(error);
  4708. };
  4709. // Because this.video_ may not be set when the config is built, the default
  4710. // TextDisplay factory must capture a reference to "this".
  4711. config.textDisplayFactory = () => {
  4712. if (this.videoContainer_) {
  4713. return new shaka.text.UITextDisplayer(
  4714. this.video_, this.videoContainer_);
  4715. } else {
  4716. return new shaka.text.SimpleTextDisplayer(this.video_);
  4717. }
  4718. };
  4719. return config;
  4720. }
  4721. /**
  4722. * Set the videoContainer to construct UITextDisplayer.
  4723. * @param {HTMLElement} videoContainer
  4724. * @export
  4725. */
  4726. setVideoContainer(videoContainer) {
  4727. this.videoContainer_ = videoContainer;
  4728. }
  4729. /**
  4730. * @param {!shaka.util.Error} error
  4731. * @private
  4732. */
  4733. defaultStreamingFailureCallback_(error) {
  4734. // For live streams, we retry streaming automatically for certain errors.
  4735. // For VOD streams, all streaming failures are fatal.
  4736. if (!this.isLive()) {
  4737. return;
  4738. }
  4739. let retryDelaySeconds = null;
  4740. if (error.code == shaka.util.Error.Code.BAD_HTTP_STATUS ||
  4741. error.code == shaka.util.Error.Code.HTTP_ERROR) {
  4742. // These errors can be near-instant, so delay a bit before retrying.
  4743. retryDelaySeconds = 1;
  4744. } else if (error.code == shaka.util.Error.Code.TIMEOUT) {
  4745. // We already waited for a timeout, so retry quickly.
  4746. retryDelaySeconds = 0.1;
  4747. }
  4748. if (retryDelaySeconds != null) {
  4749. error.severity = shaka.util.Error.Severity.RECOVERABLE;
  4750. shaka.log.warning('Live streaming error. Retrying automatically...');
  4751. this.retryStreaming(retryDelaySeconds);
  4752. }
  4753. }
  4754. /**
  4755. * For CEA closed captions embedded in the video streams, create dummy text
  4756. * stream. This can be safely called again on existing manifests, for
  4757. * manifest updates.
  4758. * @param {!shaka.extern.Manifest} manifest
  4759. * @private
  4760. */
  4761. makeTextStreamsForClosedCaptions_(manifest) {
  4762. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  4763. const TextStreamKind = shaka.util.ManifestParserUtils.TextStreamKind;
  4764. const CEA608_MIME = shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE;
  4765. const CEA708_MIME = shaka.util.MimeUtils.CEA708_CLOSED_CAPTION_MIMETYPE;
  4766. // A set, to make sure we don't create two text streams for the same video.
  4767. const closedCaptionsSet = new Set();
  4768. for (const textStream of manifest.textStreams) {
  4769. if (textStream.mimeType == CEA608_MIME ||
  4770. textStream.mimeType == CEA708_MIME) {
  4771. // This function might be called on a manifest update, so don't make a
  4772. // new text stream for closed caption streams we have seen before.
  4773. closedCaptionsSet.add(textStream.originalId);
  4774. }
  4775. }
  4776. for (const variant of manifest.variants) {
  4777. const video = variant.video;
  4778. if (video && video.closedCaptions) {
  4779. for (const id of video.closedCaptions.keys()) {
  4780. if (!closedCaptionsSet.has(id)) {
  4781. const mimeType = id.startsWith('CC') ? CEA608_MIME : CEA708_MIME;
  4782. // Add an empty segmentIndex, for the benefit of the period combiner
  4783. // in our builtin DASH parser.
  4784. const segmentIndex = new shaka.media.MetaSegmentIndex();
  4785. const language = video.closedCaptions.get(id);
  4786. const textStream = {
  4787. id: this.nextExternalStreamId_++, // A globally unique ID.
  4788. originalId: id, // The CC ID string, like 'CC1', 'CC3', etc.
  4789. createSegmentIndex: () => Promise.resolve(),
  4790. segmentIndex,
  4791. mimeType,
  4792. codecs: '',
  4793. kind: TextStreamKind.CLOSED_CAPTION,
  4794. encrypted: false,
  4795. drmInfos: [],
  4796. keyIds: new Set(),
  4797. language,
  4798. originalLanguage: language,
  4799. label: null,
  4800. type: ContentType.TEXT,
  4801. primary: false,
  4802. trickModeVideo: null,
  4803. emsgSchemeIdUris: null,
  4804. roles: video.roles,
  4805. forced: false,
  4806. channelsCount: null,
  4807. audioSamplingRate: null,
  4808. spatialAudio: false,
  4809. closedCaptions: null,
  4810. accessibilityPurpose: null,
  4811. external: false,
  4812. };
  4813. manifest.textStreams.push(textStream);
  4814. closedCaptionsSet.add(id);
  4815. }
  4816. }
  4817. }
  4818. }
  4819. }
  4820. /**
  4821. * Filters a manifest, removing unplayable streams/variants.
  4822. *
  4823. * @param {?shaka.extern.Manifest} manifest
  4824. * @private
  4825. */
  4826. async filterManifest_(manifest) {
  4827. await this.filterManifestWithStreamUtils_(manifest);
  4828. this.filterManifestWithRestrictions_(manifest);
  4829. }
  4830. /**
  4831. * Filters a manifest, removing unplayable streams/variants.
  4832. *
  4833. * @param {?shaka.extern.Manifest} manifest
  4834. * @private
  4835. */
  4836. async filterManifestWithStreamUtils_(manifest) {
  4837. goog.asserts.assert(manifest, 'Manifest should exist!');
  4838. goog.asserts.assert(this.video_, 'Must not be destroyed');
  4839. /** @type {?shaka.extern.Variant} */
  4840. const currentVariant = this.streamingEngine_ ?
  4841. this.streamingEngine_.getCurrentVariant() : null;
  4842. await shaka.util.StreamUtils.filterManifest(
  4843. this.drmEngine_, currentVariant, manifest);
  4844. this.checkPlayableVariants_(manifest);
  4845. }
  4846. /**
  4847. * Apply the restrictions configuration to the manifest, and check if there's
  4848. * a variant that meets the restrictions.
  4849. *
  4850. * @param {?shaka.extern.Manifest} manifest
  4851. * @private
  4852. */
  4853. filterManifestWithRestrictions_(manifest) {
  4854. // Return if |destroy| is called.
  4855. if (this.loadMode_ == shaka.Player.LoadMode.DESTROYED) {
  4856. return;
  4857. }
  4858. const tracksChanged = shaka.util.StreamUtils.applyRestrictions(
  4859. manifest.variants, this.config_.restrictions, this.maxHwRes_);
  4860. if (tracksChanged && this.streamingEngine_) {
  4861. this.onTracksChanged_();
  4862. }
  4863. // We may need to create new sessions for any new init data.
  4864. const currentDrmInfo =
  4865. this.drmEngine_ ? this.drmEngine_.getDrmInfo() : null;
  4866. // DrmEngine.newInitData() requires mediaKeys to be available.
  4867. if (currentDrmInfo && this.drmEngine_.getMediaKeys()) {
  4868. for (const variant of manifest.variants) {
  4869. this.processDrmInfos_(currentDrmInfo.keySystem, variant.video);
  4870. this.processDrmInfos_(currentDrmInfo.keySystem, variant.audio);
  4871. }
  4872. }
  4873. this.checkRestrictedVariants_(manifest);
  4874. }
  4875. /**
  4876. * @param {string} keySystem
  4877. * @param {?shaka.extern.Stream} stream
  4878. * @private
  4879. */
  4880. processDrmInfos_(keySystem, stream) {
  4881. if (!stream) {
  4882. return;
  4883. }
  4884. for (const drmInfo of stream.drmInfos) {
  4885. // Ignore any data for different key systems.
  4886. if (drmInfo.keySystem == keySystem) {
  4887. for (const initData of (drmInfo.initData || [])) {
  4888. this.drmEngine_.newInitData(
  4889. initData.initDataType, initData.initData);
  4890. }
  4891. }
  4892. }
  4893. }
  4894. /**
  4895. * @private
  4896. */
  4897. filterManifestByCurrentVariant_() {
  4898. goog.asserts.assert(this.manifest_, 'Manifest should be valid');
  4899. goog.asserts.assert(this.streamingEngine_,
  4900. 'StreamingEngine should be valid');
  4901. const currentVariant = this.streamingEngine_ ?
  4902. this.streamingEngine_.getCurrentVariant() : null;
  4903. shaka.util.StreamUtils.filterManifestByCurrentVariant(currentVariant,
  4904. this.manifest_);
  4905. this.checkPlayableVariants_(this.manifest_);
  4906. }
  4907. /**
  4908. * @param {shaka.extern.Variant} initialVariant
  4909. * @param {number} time
  4910. * @return {!Promise.<number>}
  4911. * @private
  4912. */
  4913. async adjustStartTime_(initialVariant, time) {
  4914. /** @type {?shaka.extern.Stream} */
  4915. const activeAudio = initialVariant.audio;
  4916. /** @type {?shaka.extern.Stream} */
  4917. const activeVideo = initialVariant.video;
  4918. /**
  4919. * @param {?shaka.extern.Stream} stream
  4920. * @param {number} time
  4921. * @return {!Promise.<?number>}
  4922. */
  4923. const getAdjustedTime = async (stream, time) => {
  4924. if (!stream) {
  4925. return null;
  4926. }
  4927. await stream.createSegmentIndex();
  4928. const iter = stream.segmentIndex.getIteratorForTime(time);
  4929. const ref = iter ? iter.next().value : null;
  4930. if (!ref) {
  4931. return null;
  4932. }
  4933. const refTime = ref.startTime;
  4934. goog.asserts.assert(refTime <= time,
  4935. 'Segment should start before target time!');
  4936. return refTime;
  4937. };
  4938. const audioStartTime = await getAdjustedTime(activeAudio, time);
  4939. const videoStartTime = await getAdjustedTime(activeVideo, time);
  4940. // If we have both video and audio times, pick the larger one. If we picked
  4941. // the smaller one, that one will download an entire segment to buffer the
  4942. // difference.
  4943. if (videoStartTime != null && audioStartTime != null) {
  4944. return Math.max(videoStartTime, audioStartTime);
  4945. } else if (videoStartTime != null) {
  4946. return videoStartTime;
  4947. } else if (audioStartTime != null) {
  4948. return audioStartTime;
  4949. } else {
  4950. return time;
  4951. }
  4952. }
  4953. /**
  4954. * Update the buffering state to be either "we are buffering" or "we are not
  4955. * buffering", firing events to the app as needed.
  4956. *
  4957. * @private
  4958. */
  4959. updateBufferState_() {
  4960. const isBuffering = this.isBuffering();
  4961. shaka.log.v2('Player changing buffering state to', isBuffering);
  4962. // Make sure we have all the components we need before we consider ourselves
  4963. // as being loaded.
  4964. // TODO: Make the check for "loaded" simpler.
  4965. const loaded = this.stats_ && this.bufferObserver_ && this.playhead_;
  4966. if (loaded) {
  4967. this.playRateController_.setBuffering(isBuffering);
  4968. if (this.cmcdManager_) {
  4969. this.cmcdManager_.setBuffering(isBuffering);
  4970. }
  4971. this.updateStateHistory_();
  4972. }
  4973. // Surface the buffering event so that the app knows if/when we are
  4974. // buffering.
  4975. const eventName = shaka.util.FakeEvent.EventName.Buffering;
  4976. const data = (new Map()).set('buffering', isBuffering);
  4977. this.dispatchEvent(this.makeEvent_(eventName, data));
  4978. }
  4979. /**
  4980. * A callback for when the playback rate changes. We need to watch the
  4981. * playback rate so that if the playback rate on the media element changes
  4982. * (that was not caused by our play rate controller) we can notify the
  4983. * controller so that it can stay in-sync with the change.
  4984. *
  4985. * @private
  4986. */
  4987. onRateChange_() {
  4988. /** @type {number} */
  4989. const newRate = this.video_.playbackRate;
  4990. // On Edge, when someone seeks using the native controls, it will set the
  4991. // playback rate to zero until they finish seeking, after which it will
  4992. // return the playback rate.
  4993. //
  4994. // If the playback rate changes while seeking, Edge will cache the playback
  4995. // rate and use it after seeking.
  4996. //
  4997. // https://github.com/shaka-project/shaka-player/issues/951
  4998. if (newRate == 0) {
  4999. return;
  5000. }
  5001. if (this.playRateController_) {
  5002. // The playback rate has changed. This could be us or someone else.
  5003. // If this was us, setting the rate again will be a no-op.
  5004. this.playRateController_.set(newRate);
  5005. }
  5006. const event = this.makeEvent_(shaka.util.FakeEvent.EventName.RateChange);
  5007. this.dispatchEvent(event);
  5008. }
  5009. /**
  5010. * Try updating the state history. If the player has not finished
  5011. * initializing, this will be a no-op.
  5012. *
  5013. * @private
  5014. */
  5015. updateStateHistory_() {
  5016. // If we have not finish initializing, this will be a no-op.
  5017. if (!this.stats_) {
  5018. return;
  5019. }
  5020. if (!this.bufferObserver_) {
  5021. return;
  5022. }
  5023. const State = shaka.media.BufferingObserver.State;
  5024. const history = this.stats_.getStateHistory();
  5025. if (this.bufferObserver_.getState() == State.STARVING) {
  5026. history.update('buffering');
  5027. } else if (this.video_.paused) {
  5028. history.update('paused');
  5029. } else if (this.video_.ended) {
  5030. history.update('ended');
  5031. } else {
  5032. history.update('playing');
  5033. }
  5034. }
  5035. /**
  5036. * Callback for liveSync
  5037. *
  5038. * @private
  5039. */
  5040. onTimeUpdate_() {
  5041. // If the live stream has reached its end, do not sync.
  5042. if (!this.isLive()) {
  5043. return;
  5044. }
  5045. const seekRange = this.seekRange();
  5046. if (!Number.isFinite(seekRange.end)) {
  5047. return;
  5048. }
  5049. const currentTime = this.video_.currentTime;
  5050. if (currentTime < seekRange.start) {
  5051. // Bad stream?
  5052. return;
  5053. }
  5054. let liveSyncMaxLatency;
  5055. let liveSyncPlaybackRate;
  5056. if (this.config_.streaming.liveSync) {
  5057. liveSyncMaxLatency = this.config_.streaming.liveSyncMaxLatency;
  5058. liveSyncPlaybackRate = this.config_.streaming.liveSyncPlaybackRate;
  5059. } else {
  5060. // serviceDescription must override if it is defined in the MPD and
  5061. // liveSync configuration is not set.
  5062. if (this.manifest_ && this.manifest_.serviceDescription) {
  5063. liveSyncMaxLatency = this.manifest_.serviceDescription.maxLatency ||
  5064. this.config_.streaming.liveSyncMaxLatency;
  5065. liveSyncPlaybackRate =
  5066. this.manifest_.serviceDescription.maxPlaybackRate ||
  5067. this.config_.streaming.liveSyncPlaybackRate;
  5068. }
  5069. }
  5070. const playbackRate = this.video_.playbackRate;
  5071. const latency = seekRange.end - this.video_.currentTime;
  5072. let offset = 0;
  5073. // In src= mode, the seek range isn't updated frequently enough, so we need
  5074. // to fudge the latency number with an offset. The playback rate is used
  5075. // as an offset, since that is the amount we catch up 1 second of
  5076. // accelerated playback.
  5077. if (this.loadMode_ == shaka.Player.LoadMode.SRC_EQUALS) {
  5078. const buffered = this.video_.buffered;
  5079. if (buffered.length > 0) {
  5080. const bufferedEnd = buffered.end(buffered.length - 1);
  5081. offset = Math.max(liveSyncPlaybackRate, bufferedEnd - seekRange.end);
  5082. }
  5083. }
  5084. if (liveSyncMaxLatency && liveSyncPlaybackRate &&
  5085. (latency - offset) > liveSyncMaxLatency) {
  5086. if (playbackRate != liveSyncPlaybackRate) {
  5087. shaka.log.debug('Latency (' + latency + 's) ' +
  5088. 'is greater than liveSyncMaxLatency (' + liveSyncMaxLatency + 's). ' +
  5089. 'Updating playbackRate to ' + liveSyncPlaybackRate);
  5090. this.trickPlay(liveSyncPlaybackRate);
  5091. }
  5092. } else if (playbackRate !== 1 && playbackRate !== 0) {
  5093. this.cancelTrickPlay();
  5094. }
  5095. }
  5096. /**
  5097. * Callback from Playhead.
  5098. *
  5099. * @private
  5100. */
  5101. onSeek_() {
  5102. if (this.playheadObservers_) {
  5103. this.playheadObservers_.notifyOfSeek();
  5104. }
  5105. if (this.streamingEngine_) {
  5106. this.streamingEngine_.seeked();
  5107. }
  5108. if (this.bufferObserver_) {
  5109. // If we seek into an unbuffered range, we should fire a 'buffering' event
  5110. // immediately. If StreamingEngine can buffer fast enough, we may not
  5111. // update our buffering tracking otherwise.
  5112. this.pollBufferState_();
  5113. }
  5114. }
  5115. /**
  5116. * Update AbrManager with variants while taking into account restrictions,
  5117. * preferences, and ABR.
  5118. *
  5119. * On error, this dispatches an error event and returns false.
  5120. *
  5121. * @return {boolean} True if successful.
  5122. * @private
  5123. */
  5124. updateAbrManagerVariants_() {
  5125. try {
  5126. goog.asserts.assert(this.manifest_, 'Manifest should exist by now!');
  5127. this.checkRestrictedVariants_(this.manifest_);
  5128. } catch (e) {
  5129. this.onError_(e);
  5130. return false;
  5131. }
  5132. const playableVariants = shaka.util.StreamUtils.getPlayableVariants(
  5133. this.manifest_.variants);
  5134. // Update the abr manager with newly filtered variants.
  5135. const adaptationSet = this.currentAdaptationSetCriteria_.create(
  5136. playableVariants);
  5137. this.abrManager_.setVariants(Array.from(adaptationSet.values()));
  5138. return true;
  5139. }
  5140. /**
  5141. * Chooses a variant from all possible variants while taking into account
  5142. * restrictions, preferences, and ABR.
  5143. *
  5144. * On error, this dispatches an error event and returns null.
  5145. *
  5146. * @return {?shaka.extern.Variant}
  5147. * @private
  5148. */
  5149. chooseVariant_() {
  5150. if (this.updateAbrManagerVariants_()) {
  5151. return this.abrManager_.chooseVariant();
  5152. } else {
  5153. return null;
  5154. }
  5155. }
  5156. /**
  5157. * Checks to re-enable variants that were temporarily disabled due to network
  5158. * errors. If any variants are enabled this way, a new variant may be chosen
  5159. * for playback.
  5160. * @private
  5161. */
  5162. checkVariants_() {
  5163. goog.asserts.assert(this.manifest_, 'Should have manifest!');
  5164. const now = Date.now() / 1000;
  5165. let hasVariantUpdate = false;
  5166. /** @type {function(shaka.extern.Variant):string} */
  5167. const streamsAsString = (variant) => {
  5168. let str = '';
  5169. if (variant.video) {
  5170. str += 'video:' + variant.video.id;
  5171. }
  5172. if (variant.audio) {
  5173. str += str ? '&' : '';
  5174. str += 'audio:' + variant.audio.id;
  5175. }
  5176. return str;
  5177. };
  5178. for (const variant of this.manifest_.variants) {
  5179. if (variant.disabledUntilTime > 0 && variant.disabledUntilTime <= now) {
  5180. variant.disabledUntilTime = 0;
  5181. hasVariantUpdate = true;
  5182. shaka.log.v2('Re-enabled variant with ' + streamsAsString(variant));
  5183. }
  5184. }
  5185. const shouldStopTimer = this.manifest_.variants.every((variant) => {
  5186. goog.asserts.assert(
  5187. variant.disabledUntilTime >= 0,
  5188. '|variant.disableTimeUntilTime| must always be >= 0');
  5189. return variant.disabledUntilTime === 0;
  5190. });
  5191. if (shouldStopTimer) {
  5192. this.checkVariantsTimer_.stop();
  5193. }
  5194. if (hasVariantUpdate) {
  5195. // Reconsider re-enabled variant for ABR switching.
  5196. this.chooseVariantAndSwitch_(
  5197. /* clearBuffer= */ true, /* safeMargin= */ undefined,
  5198. /* force= */ false, /* fromAdaptation= */ false);
  5199. }
  5200. }
  5201. /**
  5202. * Choose a text stream from all possible text streams while taking into
  5203. * account user preference.
  5204. *
  5205. * @return {?shaka.extern.Stream}
  5206. * @private
  5207. */
  5208. chooseTextStream_() {
  5209. const subset = shaka.util.StreamUtils.filterStreamsByLanguageAndRole(
  5210. this.manifest_.textStreams,
  5211. this.currentTextLanguage_,
  5212. this.currentTextRole_,
  5213. this.currentTextForced_);
  5214. return subset[0] || null;
  5215. }
  5216. /**
  5217. * Chooses a new Variant. If the new variant differs from the old one, it
  5218. * adds the new one to the switch history and switches to it.
  5219. *
  5220. * Called after a config change, a key status event, or an explicit language
  5221. * change.
  5222. *
  5223. * @param {boolean=} clearBuffer Optional clear buffer or not when
  5224. * switch to new variant
  5225. * Defaults to true if not provided
  5226. * @param {number=} safeMargin Optional amount of buffer (in seconds) to
  5227. * retain when clearing the buffer.
  5228. * Defaults to 0 if not provided. Ignored if clearBuffer is false.
  5229. * @private
  5230. */
  5231. chooseVariantAndSwitch_(clearBuffer = true, safeMargin = 0, force = false,
  5232. fromAdaptation = true) {
  5233. goog.asserts.assert(this.config_, 'Must not be destroyed');
  5234. // Because we're running this after a config change (manual language
  5235. // change) or a key status event, it is always okay to clear the buffer
  5236. // here.
  5237. const chosenVariant = this.chooseVariant_();
  5238. if (chosenVariant) {
  5239. this.switchVariant_(chosenVariant, fromAdaptation,
  5240. clearBuffer, safeMargin, force);
  5241. }
  5242. }
  5243. /**
  5244. * @param {shaka.extern.Variant} variant
  5245. * @param {boolean} fromAdaptation
  5246. * @param {boolean} clearBuffer
  5247. * @param {number} safeMargin
  5248. * @param {boolean=} force
  5249. * @private
  5250. */
  5251. switchVariant_(variant, fromAdaptation, clearBuffer, safeMargin,
  5252. force = false) {
  5253. const currentVariant = this.streamingEngine_.getCurrentVariant();
  5254. if (variant == currentVariant) {
  5255. shaka.log.debug('Variant already selected.');
  5256. // If you want to clear the buffer, we force to reselect the same variant.
  5257. // We don't need to reset the timestampOffset since it's the same variant,
  5258. // so 'adaptation' isn't passed here.
  5259. if (clearBuffer) {
  5260. this.streamingEngine_.switchVariant(variant, clearBuffer, safeMargin,
  5261. /* force= */ true);
  5262. }
  5263. return;
  5264. }
  5265. // Add entries to the history.
  5266. this.addVariantToSwitchHistory_(variant, fromAdaptation);
  5267. this.streamingEngine_.switchVariant(
  5268. variant, clearBuffer, safeMargin, force,
  5269. /* adaptation= */ fromAdaptation);
  5270. let oldTrack = null;
  5271. if (currentVariant) {
  5272. oldTrack = shaka.util.StreamUtils.variantToTrack(currentVariant);
  5273. }
  5274. const newTrack = shaka.util.StreamUtils.variantToTrack(variant);
  5275. if (fromAdaptation) {
  5276. // Dispatch an 'adaptation' event
  5277. this.onAdaptation_(oldTrack, newTrack);
  5278. } else {
  5279. // Dispatch a 'variantchanged' event
  5280. this.onVariantChanged_(oldTrack, newTrack);
  5281. }
  5282. }
  5283. /**
  5284. * @param {AudioTrack} track
  5285. * @private
  5286. */
  5287. switchHtml5Track_(track) {
  5288. goog.asserts.assert(this.video_ && this.video_.audioTracks,
  5289. 'Video and video.audioTracks should not be null!');
  5290. const audioTracks = Array.from(this.video_.audioTracks);
  5291. const currentTrack = audioTracks.find((t) => t.enabled);
  5292. // This will reset the "enabled" of other tracks to false.
  5293. track.enabled = true;
  5294. // AirPlay does not reset the "enabled" of other tracks to false, so
  5295. // it must be changed by hand.
  5296. if (track.id !== currentTrack.id) {
  5297. currentTrack.enabled = false;
  5298. }
  5299. const oldTrack =
  5300. shaka.util.StreamUtils.html5AudioTrackToTrack(currentTrack);
  5301. const newTrack =
  5302. shaka.util.StreamUtils.html5AudioTrackToTrack(track);
  5303. this.onVariantChanged_(oldTrack, newTrack);
  5304. }
  5305. /**
  5306. * Decide during startup if text should be streamed/shown.
  5307. * @private
  5308. */
  5309. setInitialTextState_(initialVariant, initialTextStream) {
  5310. // Check if we should show text (based on difference between audio and text
  5311. // languages).
  5312. if (initialTextStream) {
  5313. if (initialVariant.audio && this.shouldInitiallyShowText_(
  5314. initialVariant.audio, initialTextStream)) {
  5315. this.isTextVisible_ = true;
  5316. }
  5317. if (this.isTextVisible_) {
  5318. // If the cached value says to show text, then update the text displayer
  5319. // since it defaults to not shown.
  5320. this.mediaSourceEngine_.getTextDisplayer().setTextVisibility(true);
  5321. goog.asserts.assert(this.shouldStreamText_(),
  5322. 'Should be streaming text');
  5323. }
  5324. this.onTextTrackVisibility_();
  5325. } else {
  5326. this.isTextVisible_ = false;
  5327. }
  5328. }
  5329. /**
  5330. * Check if we should show text on screen automatically.
  5331. *
  5332. * @param {shaka.extern.Stream} audioStream
  5333. * @param {shaka.extern.Stream} textStream
  5334. * @return {boolean}
  5335. * @private
  5336. */
  5337. shouldInitiallyShowText_(audioStream, textStream) {
  5338. const AutoShowText = shaka.config.AutoShowText;
  5339. if (this.config_.autoShowText == AutoShowText.NEVER) {
  5340. return false;
  5341. }
  5342. if (this.config_.autoShowText == AutoShowText.ALWAYS) {
  5343. return true;
  5344. }
  5345. const LanguageUtils = shaka.util.LanguageUtils;
  5346. /** @type {string} */
  5347. const preferredTextLocale =
  5348. LanguageUtils.normalize(this.config_.preferredTextLanguage);
  5349. /** @type {string} */
  5350. const textLocale = LanguageUtils.normalize(textStream.language);
  5351. if (this.config_.autoShowText == AutoShowText.IF_PREFERRED_TEXT_LANGUAGE) {
  5352. // Only the text language match matters.
  5353. return LanguageUtils.areLanguageCompatible(
  5354. textLocale,
  5355. preferredTextLocale);
  5356. }
  5357. if (this.config_.autoShowText == AutoShowText.IF_SUBTITLES_MAY_BE_NEEDED) {
  5358. /* The text should automatically be shown if the text is
  5359. * language-compatible with the user's text language preference, but not
  5360. * compatible with the audio. These are cases where we deduce that
  5361. * subtitles may be needed.
  5362. *
  5363. * For example:
  5364. * preferred | chosen | chosen |
  5365. * text | text | audio | show
  5366. * -----------------------------------
  5367. * en-CA | en | jp | true
  5368. * en | en-US | fr | true
  5369. * fr-CA | en-US | jp | false
  5370. * en-CA | en-US | en-US | false
  5371. *
  5372. */
  5373. /** @type {string} */
  5374. const audioLocale = LanguageUtils.normalize(audioStream.language);
  5375. return (
  5376. LanguageUtils.areLanguageCompatible(textLocale, preferredTextLocale) &&
  5377. !LanguageUtils.areLanguageCompatible(audioLocale, textLocale));
  5378. }
  5379. shaka.log.alwaysWarn('Invalid autoShowText setting!');
  5380. return false;
  5381. }
  5382. /**
  5383. * Callback from StreamingEngine.
  5384. *
  5385. * @private
  5386. */
  5387. onManifestUpdate_() {
  5388. if (this.parser_ && this.parser_.update) {
  5389. this.parser_.update();
  5390. }
  5391. }
  5392. /**
  5393. * Callback from StreamingEngine.
  5394. *
  5395. * @private
  5396. */
  5397. onSegmentAppended_(start, end, contentType) {
  5398. // When we append a segment to media source (via streaming engine) we are
  5399. // changing what data we have buffered, so notify the playhead of the
  5400. // change.
  5401. if (this.playhead_) {
  5402. this.playhead_.notifyOfBufferingChange();
  5403. }
  5404. this.pollBufferState_();
  5405. // Dispatch an event for users to consume, too.
  5406. const data = new Map()
  5407. .set('start', start)
  5408. .set('end', end)
  5409. .set('contentType', contentType);
  5410. this.dispatchEvent(this.makeEvent_(
  5411. shaka.util.FakeEvent.EventName.SegmentAppended, data));
  5412. }
  5413. /**
  5414. * Callback from AbrManager.
  5415. *
  5416. * @param {shaka.extern.Variant} variant
  5417. * @param {boolean=} clearBuffer
  5418. * @param {number=} safeMargin Optional amount of buffer (in seconds) to
  5419. * retain when clearing the buffer.
  5420. * Defaults to 0 if not provided. Ignored if clearBuffer is false.
  5421. * @private
  5422. */
  5423. switch_(variant, clearBuffer = false, safeMargin = 0) {
  5424. shaka.log.debug('switch_');
  5425. goog.asserts.assert(this.config_.abr.enabled,
  5426. 'AbrManager should not call switch while disabled!');
  5427. goog.asserts.assert(this.manifest_, 'We need a manifest to switch ' +
  5428. 'variants.');
  5429. if (!this.streamingEngine_) {
  5430. // There's no way to change it.
  5431. return;
  5432. }
  5433. if (variant == this.streamingEngine_.getCurrentVariant()) {
  5434. // This isn't a change.
  5435. return;
  5436. }
  5437. this.switchVariant_(variant, /* fromAdaptation= */ true,
  5438. clearBuffer, safeMargin);
  5439. }
  5440. /**
  5441. * Dispatches an 'adaptation' event.
  5442. * @param {?shaka.extern.Track} from
  5443. * @param {shaka.extern.Track} to
  5444. * @private
  5445. */
  5446. onAdaptation_(from, to) {
  5447. // Delay the 'adaptation' event so that StreamingEngine has time to absorb
  5448. // the changes before the user tries to query it.
  5449. const data = new Map()
  5450. .set('oldTrack', from)
  5451. .set('newTrack', to);
  5452. if (this.lcevcDil_) {
  5453. this.lcevcDil_.updateVariant(to);
  5454. }
  5455. const event =
  5456. this.makeEvent_(shaka.util.FakeEvent.EventName.Adaptation, data);
  5457. this.delayDispatchEvent_(event);
  5458. }
  5459. /**
  5460. * Dispatches a 'trackschanged' event.
  5461. * @private
  5462. */
  5463. onTracksChanged_() {
  5464. // Delay the 'trackschanged' event so StreamingEngine has time to absorb the
  5465. // changes before the user tries to query it.
  5466. const event = this.makeEvent_(shaka.util.FakeEvent.EventName.TracksChanged);
  5467. this.delayDispatchEvent_(event);
  5468. }
  5469. /**
  5470. * Dispatches a 'variantchanged' event.
  5471. * @param {?shaka.extern.Track} from
  5472. * @param {shaka.extern.Track} to
  5473. * @private
  5474. */
  5475. onVariantChanged_(from, to) {
  5476. // Delay the 'variantchanged' event so StreamingEngine has time to absorb
  5477. // the changes before the user tries to query it.
  5478. const data = new Map()
  5479. .set('oldTrack', from)
  5480. .set('newTrack', to);
  5481. if (this.lcevcDil_) {
  5482. this.lcevcDil_.updateVariant(to);
  5483. }
  5484. const event =
  5485. this.makeEvent_(shaka.util.FakeEvent.EventName.VariantChanged, data);
  5486. this.delayDispatchEvent_(event);
  5487. }
  5488. /**
  5489. * Dispatches a 'textchanged' event.
  5490. * @private
  5491. */
  5492. onTextChanged_() {
  5493. // Delay the 'textchanged' event so StreamingEngine time to absorb the
  5494. // changes before the user tries to query it.
  5495. const event = this.makeEvent_(shaka.util.FakeEvent.EventName.TextChanged);
  5496. this.delayDispatchEvent_(event);
  5497. }
  5498. /** @private */
  5499. onTextTrackVisibility_() {
  5500. const event =
  5501. this.makeEvent_(shaka.util.FakeEvent.EventName.TextTrackVisibility);
  5502. this.delayDispatchEvent_(event);
  5503. }
  5504. /** @private */
  5505. onAbrStatusChanged_() {
  5506. // Restore disabled variants if abr get disabled
  5507. if (!this.config_.abr.enabled) {
  5508. this.restoreDisabledVariants_();
  5509. }
  5510. const data = (new Map()).set('newStatus', this.config_.abr.enabled);
  5511. this.delayDispatchEvent_(this.makeEvent_(
  5512. shaka.util.FakeEvent.EventName.AbrStatusChanged, data));
  5513. }
  5514. /**
  5515. * @param {boolean} updateAbrManager
  5516. * @private
  5517. */
  5518. restoreDisabledVariants_(updateAbrManager=true) {
  5519. if (this.loadMode_ != shaka.Player.LoadMode.MEDIA_SOURCE) {
  5520. return;
  5521. }
  5522. goog.asserts.assert(this.manifest_, 'Should have manifest!');
  5523. shaka.log.v2('Restoring all disabled streams...');
  5524. this.checkVariantsTimer_.stop();
  5525. for (const variant of this.manifest_.variants) {
  5526. variant.disabledUntilTime = 0;
  5527. }
  5528. if (updateAbrManager) {
  5529. this.updateAbrManagerVariants_();
  5530. }
  5531. }
  5532. /**
  5533. * Temporarily disable all variants containing |stream|
  5534. * @param {shaka.extern.Stream} stream
  5535. * @param {number} disableTime
  5536. * @return {boolean}
  5537. */
  5538. disableStream(stream, disableTime) {
  5539. if (!this.config_.abr.enabled ||
  5540. this.loadMode_ === shaka.Player.LoadMode.DESTROYED) {
  5541. return false;
  5542. }
  5543. if (!navigator.onLine) {
  5544. // Don't disable variants if we're completely offline, or else we end up
  5545. // rapidly restricting all of them.
  5546. return false;
  5547. }
  5548. // It only makes sense to disable a stream if we have an alternative else we
  5549. // end up disabling all variants.
  5550. const hasAltStream = this.manifest_.variants.some((variant) => {
  5551. const altStream = variant[stream.type];
  5552. if (altStream && altStream.id !== stream.id) {
  5553. if (shaka.util.StreamUtils.isAudio(stream)) {
  5554. return stream.language === altStream.language;
  5555. }
  5556. return true;
  5557. }
  5558. return false;
  5559. });
  5560. if (hasAltStream) {
  5561. let didDisableStream = false;
  5562. for (const variant of this.manifest_.variants) {
  5563. const candidate = variant[stream.type];
  5564. if (candidate && candidate.id === stream.id) {
  5565. variant.disabledUntilTime = (Date.now() / 1000) + disableTime;
  5566. didDisableStream = true;
  5567. shaka.log.v2(
  5568. 'Disabled stream ' + stream.type + ':' + stream.id +
  5569. ' for ' + disableTime + ' seconds...');
  5570. }
  5571. }
  5572. goog.asserts.assert(didDisableStream, 'Must have disabled stream');
  5573. this.checkVariantsTimer_.tickEvery(1);
  5574. // Get the safeMargin to ensure a seamless playback
  5575. const {video} = this.getBufferedInfo();
  5576. const safeMargin =
  5577. video.reduce((size, {start, end}) => size + end - start, 0);
  5578. // Update abr manager variants and switch to recover playback
  5579. this.chooseVariantAndSwitch_(
  5580. /* clearBuffer= */ true, /* safeMargin= */ safeMargin,
  5581. /* force= */ true, /* fromAdaptation= */ false);
  5582. return true;
  5583. }
  5584. shaka.log.warning(
  5585. 'No alternate stream found for active ' + stream.type + ' stream. ' +
  5586. 'Will ignore request to disable stream...');
  5587. return false;
  5588. }
  5589. /**
  5590. * @param {!shaka.util.Error} error
  5591. * @private
  5592. */
  5593. onError_(error) {
  5594. goog.asserts.assert(error instanceof shaka.util.Error, 'Wrong error type!');
  5595. // Errors dispatched after |destroy| is called are not meaningful and should
  5596. // be safe to ignore.
  5597. if (this.loadMode_ == shaka.Player.LoadMode.DESTROYED) {
  5598. return;
  5599. }
  5600. // Restore disabled variant if the player experienced a critical error.
  5601. if (error.severity === shaka.util.Error.Severity.CRITICAL) {
  5602. this.restoreDisabledVariants_(/* updateAbrManager= */ false);
  5603. }
  5604. const eventName = shaka.util.FakeEvent.EventName.Error;
  5605. const event = this.makeEvent_(eventName, (new Map()).set('detail', error));
  5606. this.dispatchEvent(event);
  5607. if (event.defaultPrevented) {
  5608. error.handled = true;
  5609. }
  5610. }
  5611. /**
  5612. * When we fire region events, we need to copy the information out of the
  5613. * region to break the connection with the player's internal data. We do the
  5614. * copy here because this is the transition point between the player and the
  5615. * app.
  5616. *
  5617. * @param {!shaka.util.FakeEvent.EventName} eventName
  5618. * @param {shaka.extern.TimelineRegionInfo} region
  5619. *
  5620. * @private
  5621. */
  5622. onRegionEvent_(eventName, region) {
  5623. // Always make a copy to avoid exposing our internal data to the app.
  5624. const clone = {
  5625. schemeIdUri: region.schemeIdUri,
  5626. value: region.value,
  5627. startTime: region.startTime,
  5628. endTime: region.endTime,
  5629. id: region.id,
  5630. eventElement: region.eventElement,
  5631. };
  5632. const data = (new Map()).set('detail', clone);
  5633. this.dispatchEvent(this.makeEvent_(eventName, data));
  5634. }
  5635. /**
  5636. * When notified of a media quality change we need to emit a
  5637. * MediaQualityChange event to the app.
  5638. *
  5639. * @param {shaka.extern.MediaQualityInfo} mediaQuality
  5640. * @param {number} position
  5641. *
  5642. * @private
  5643. */
  5644. onMediaQualityChange_(mediaQuality, position) {
  5645. // Always make a copy to avoid exposing our internal data to the app.
  5646. const clone = {
  5647. bandwidth: mediaQuality.bandwidth,
  5648. audioSamplingRate: mediaQuality.audioSamplingRate,
  5649. codecs: mediaQuality.codecs,
  5650. contentType: mediaQuality.contentType,
  5651. frameRate: mediaQuality.frameRate,
  5652. height: mediaQuality.height,
  5653. mimeType: mediaQuality.mimeType,
  5654. channelsCount: mediaQuality.channelsCount,
  5655. pixelAspectRatio: mediaQuality.pixelAspectRatio,
  5656. width: mediaQuality.width,
  5657. };
  5658. const data = new Map()
  5659. .set('mediaQuality', clone)
  5660. .set('position', position);
  5661. this.dispatchEvent(this.makeEvent_(
  5662. shaka.util.FakeEvent.EventName.MediaQualityChanged, data));
  5663. }
  5664. /**
  5665. * Turn the media element's error object into a Shaka Player error object.
  5666. *
  5667. * @return {shaka.util.Error}
  5668. * @private
  5669. */
  5670. videoErrorToShakaError_() {
  5671. goog.asserts.assert(this.video_.error,
  5672. 'Video error expected, but missing!');
  5673. if (!this.video_.error) {
  5674. return null;
  5675. }
  5676. const code = this.video_.error.code;
  5677. if (code == 1 /* MEDIA_ERR_ABORTED */) {
  5678. // Ignore this error code, which should only occur when navigating away or
  5679. // deliberately stopping playback of HTTP content.
  5680. return null;
  5681. }
  5682. // Extra error information from MS Edge:
  5683. let extended = this.video_.error.msExtendedCode;
  5684. if (extended) {
  5685. // Convert to unsigned:
  5686. if (extended < 0) {
  5687. extended += Math.pow(2, 32);
  5688. }
  5689. // Format as hex:
  5690. extended = extended.toString(16);
  5691. }
  5692. // Extra error information from Chrome:
  5693. const message = this.video_.error.message;
  5694. return new shaka.util.Error(
  5695. shaka.util.Error.Severity.CRITICAL,
  5696. shaka.util.Error.Category.MEDIA,
  5697. shaka.util.Error.Code.VIDEO_ERROR,
  5698. code, extended, message);
  5699. }
  5700. /**
  5701. * @param {!Event} event
  5702. * @private
  5703. */
  5704. onVideoError_(event) {
  5705. const error = this.videoErrorToShakaError_();
  5706. if (!error) {
  5707. return;
  5708. }
  5709. this.onError_(error);
  5710. }
  5711. /**
  5712. * @param {!Object.<string, string>} keyStatusMap A map of hex key IDs to
  5713. * statuses.
  5714. * @private
  5715. */
  5716. onKeyStatus_(keyStatusMap) {
  5717. if (!this.streamingEngine_) {
  5718. // We can't use this info to manage restrictions in src= mode, so ignore
  5719. // it.
  5720. return;
  5721. }
  5722. const keyIds = Object.keys(keyStatusMap);
  5723. if (keyIds.length == 0) {
  5724. shaka.log.warning(
  5725. 'Got a key status event without any key statuses, so we don\'t ' +
  5726. 'know the real key statuses. If we don\'t have all the keys, ' +
  5727. 'you\'ll need to set restrictions so we don\'t select those tracks.');
  5728. }
  5729. // If EME is using a synthetic key ID, the only key ID is '00' (a single 0
  5730. // byte). In this case, it is only used to report global success/failure.
  5731. // See note about old platforms in: https://bit.ly/2tpez5Z
  5732. const isGlobalStatus = keyIds.length == 1 && keyIds[0] == '00';
  5733. if (isGlobalStatus) {
  5734. shaka.log.warning(
  5735. 'Got a synthetic key status event, so we don\'t know the real key ' +
  5736. 'statuses. If we don\'t have all the keys, you\'ll need to set ' +
  5737. 'restrictions so we don\'t select those tracks.');
  5738. }
  5739. const restrictedStatuses = shaka.Player.restrictedStatuses_;
  5740. let tracksChanged = false;
  5741. // Only filter tracks for keys if we have some key statuses to look at.
  5742. if (keyIds.length) {
  5743. for (const variant of this.manifest_.variants) {
  5744. const streams = shaka.util.StreamUtils.getVariantStreams(variant);
  5745. for (const stream of streams) {
  5746. const originalAllowed = variant.allowedByKeySystem;
  5747. // Only update if we have key IDs for the stream. If the keys aren't
  5748. // all present, then the track should be restricted.
  5749. if (stream.keyIds.size) {
  5750. variant.allowedByKeySystem = true;
  5751. for (const keyId of stream.keyIds) {
  5752. const keyStatus = keyStatusMap[isGlobalStatus ? '00' : keyId];
  5753. variant.allowedByKeySystem = variant.allowedByKeySystem &&
  5754. !!keyStatus && !restrictedStatuses.includes(keyStatus);
  5755. }
  5756. }
  5757. if (originalAllowed != variant.allowedByKeySystem) {
  5758. tracksChanged = true;
  5759. }
  5760. } // for (const stream of streams)
  5761. } // for (const variant of this.manifest_.variants)
  5762. } // if (keyIds.size)
  5763. if (tracksChanged) {
  5764. const variantsUpdated = this.updateAbrManagerVariants_();
  5765. if (!variantsUpdated) {
  5766. return;
  5767. }
  5768. }
  5769. const currentVariant = this.streamingEngine_.getCurrentVariant();
  5770. if (currentVariant && !currentVariant.allowedByKeySystem) {
  5771. shaka.log.debug('Choosing new streams after key status changed');
  5772. this.chooseVariantAndSwitch_();
  5773. }
  5774. if (tracksChanged) {
  5775. this.onTracksChanged_();
  5776. }
  5777. }
  5778. /**
  5779. * Callback from DrmEngine
  5780. * @param {string} keyId
  5781. * @param {number} expiration
  5782. * @private
  5783. */
  5784. onExpirationUpdated_(keyId, expiration) {
  5785. if (this.parser_ && this.parser_.onExpirationUpdated) {
  5786. this.parser_.onExpirationUpdated(keyId, expiration);
  5787. }
  5788. const event =
  5789. this.makeEvent_(shaka.util.FakeEvent.EventName.ExpirationUpdated);
  5790. this.dispatchEvent(event);
  5791. }
  5792. /**
  5793. * @return {boolean} true if we should stream text right now.
  5794. * @private
  5795. */
  5796. shouldStreamText_() {
  5797. return this.config_.streaming.alwaysStreamText || this.isTextTrackVisible();
  5798. }
  5799. /**
  5800. * Applies playRangeStart and playRangeEnd to the given timeline. This will
  5801. * only affect non-live content.
  5802. *
  5803. * @param {shaka.media.PresentationTimeline} timeline
  5804. * @param {number} playRangeStart
  5805. * @param {number} playRangeEnd
  5806. *
  5807. * @private
  5808. */
  5809. static applyPlayRange_(timeline, playRangeStart, playRangeEnd) {
  5810. if (playRangeStart > 0) {
  5811. if (timeline.isLive()) {
  5812. shaka.log.warning(
  5813. '|playRangeStart| has been configured for live content. ' +
  5814. 'Ignoring the setting.');
  5815. } else {
  5816. timeline.setUserSeekStart(playRangeStart);
  5817. }
  5818. }
  5819. // If the playback has been configured to end before the end of the
  5820. // presentation, update the duration unless it's live content.
  5821. const fullDuration = timeline.getDuration();
  5822. if (playRangeEnd < fullDuration) {
  5823. if (timeline.isLive()) {
  5824. shaka.log.warning(
  5825. '|playRangeEnd| has been configured for live content. ' +
  5826. 'Ignoring the setting.');
  5827. } else {
  5828. timeline.setDuration(playRangeEnd);
  5829. }
  5830. }
  5831. }
  5832. /**
  5833. * Checks if the variants are all restricted, and throw an appropriate
  5834. * exception if so.
  5835. *
  5836. * @param {shaka.extern.Manifest} manifest
  5837. *
  5838. * @private
  5839. */
  5840. checkRestrictedVariants_(manifest) {
  5841. const restrictedStatuses = shaka.Player.restrictedStatuses_;
  5842. const keyStatusMap =
  5843. this.drmEngine_ ? this.drmEngine_.getKeyStatuses() : {};
  5844. const keyIds = Object.keys(keyStatusMap);
  5845. const isGlobalStatus = keyIds.length && keyIds[0] == '00';
  5846. let hasPlayable = false;
  5847. let hasAppRestrictions = false;
  5848. /** @type {!Set.<string>} */
  5849. const missingKeys = new Set();
  5850. /** @type {!Set.<string>} */
  5851. const badKeyStatuses = new Set();
  5852. for (const variant of manifest.variants) {
  5853. // TODO: Combine with onKeyStatus_.
  5854. const streams = [];
  5855. if (variant.audio) {
  5856. streams.push(variant.audio);
  5857. }
  5858. if (variant.video) {
  5859. streams.push(variant.video);
  5860. }
  5861. for (const stream of streams) {
  5862. if (stream.keyIds.size) {
  5863. for (const keyId of stream.keyIds) {
  5864. const keyStatus = keyStatusMap[isGlobalStatus ? '00' : keyId];
  5865. if (!keyStatus) {
  5866. missingKeys.add(keyId);
  5867. } else if (restrictedStatuses.includes(keyStatus)) {
  5868. badKeyStatuses.add(keyStatus);
  5869. }
  5870. }
  5871. } // if (stream.keyIds.size)
  5872. }
  5873. if (!variant.allowedByApplication) {
  5874. hasAppRestrictions = true;
  5875. } else if (variant.allowedByKeySystem) {
  5876. hasPlayable = true;
  5877. }
  5878. }
  5879. if (!hasPlayable) {
  5880. /** @type {shaka.extern.RestrictionInfo} */
  5881. const data = {
  5882. hasAppRestrictions,
  5883. missingKeys: Array.from(missingKeys),
  5884. restrictedKeyStatuses: Array.from(badKeyStatuses),
  5885. };
  5886. throw new shaka.util.Error(
  5887. shaka.util.Error.Severity.CRITICAL,
  5888. shaka.util.Error.Category.MANIFEST,
  5889. shaka.util.Error.Code.RESTRICTIONS_CANNOT_BE_MET,
  5890. data);
  5891. }
  5892. }
  5893. /**
  5894. * Confirm some variants are playable. Otherwise, throw an exception.
  5895. * @param {!shaka.extern.Manifest} manifest
  5896. * @private
  5897. */
  5898. checkPlayableVariants_(manifest) {
  5899. const valid = manifest.variants.some(shaka.util.StreamUtils.isPlayable);
  5900. // If none of the variants are playable, throw
  5901. // CONTENT_UNSUPPORTED_BY_BROWSER.
  5902. if (!valid) {
  5903. throw new shaka.util.Error(
  5904. shaka.util.Error.Severity.CRITICAL,
  5905. shaka.util.Error.Category.MANIFEST,
  5906. shaka.util.Error.Code.CONTENT_UNSUPPORTED_BY_BROWSER);
  5907. }
  5908. }
  5909. /**
  5910. * Fire an event, but wait a little bit so that the immediate execution can
  5911. * complete before the event is handled.
  5912. *
  5913. * @param {!shaka.util.FakeEvent} event
  5914. * @private
  5915. */
  5916. async delayDispatchEvent_(event) {
  5917. // Wait until the next interpreter cycle.
  5918. await Promise.resolve();
  5919. // Only dispatch the event if we are still alive.
  5920. if (this.loadMode_ != shaka.Player.LoadMode.DESTROYED) {
  5921. this.dispatchEvent(event);
  5922. }
  5923. }
  5924. /**
  5925. * Get the normalized languages for a group of tracks.
  5926. *
  5927. * @param {!Array.<?shaka.extern.Track>} tracks
  5928. * @return {!Set.<string>}
  5929. * @private
  5930. */
  5931. static getLanguagesFrom_(tracks) {
  5932. const languages = new Set();
  5933. for (const track of tracks) {
  5934. if (track.language) {
  5935. languages.add(shaka.util.LanguageUtils.normalize(track.language));
  5936. } else {
  5937. languages.add('und');
  5938. }
  5939. }
  5940. return languages;
  5941. }
  5942. /**
  5943. * Get all permutations of normalized languages and role for a group of
  5944. * tracks.
  5945. *
  5946. * @param {!Array.<?shaka.extern.Track>} tracks
  5947. * @return {!Array.<shaka.extern.LanguageRole>}
  5948. * @private
  5949. */
  5950. static getLanguageAndRolesFrom_(tracks) {
  5951. /** @type {!Map.<string, !Set>} */
  5952. const languageToRoles = new Map();
  5953. /** @type {!Map.<string, !Map.<string, string>>} */
  5954. const languageRoleToLabel = new Map();
  5955. for (const track of tracks) {
  5956. let language = 'und';
  5957. let roles = [];
  5958. if (track.language) {
  5959. language = shaka.util.LanguageUtils.normalize(track.language);
  5960. }
  5961. if (track.type == 'variant') {
  5962. roles = track.audioRoles;
  5963. } else {
  5964. roles = track.roles;
  5965. }
  5966. if (!roles || !roles.length) {
  5967. // We must have an empty role so that we will still get a language-role
  5968. // entry from our Map.
  5969. roles = [''];
  5970. }
  5971. if (!languageToRoles.has(language)) {
  5972. languageToRoles.set(language, new Set());
  5973. }
  5974. for (const role of roles) {
  5975. languageToRoles.get(language).add(role);
  5976. if (track.label) {
  5977. if (!languageRoleToLabel.has(language)) {
  5978. languageRoleToLabel.set(language, new Map());
  5979. }
  5980. languageRoleToLabel.get(language).set(role, track.label);
  5981. }
  5982. }
  5983. }
  5984. // Flatten our map to an array of language-role pairs.
  5985. const pairings = [];
  5986. languageToRoles.forEach((roles, language) => {
  5987. for (const role of roles) {
  5988. let label = null;
  5989. if (languageRoleToLabel.has(language) &&
  5990. languageRoleToLabel.get(language).has(role)) {
  5991. label = languageRoleToLabel.get(language).get(role);
  5992. }
  5993. pairings.push({language, role, label});
  5994. }
  5995. });
  5996. return pairings;
  5997. }
  5998. /**
  5999. * Assuming the player is playing content with media source, check if the
  6000. * player has buffered enough content to make it to the end of the
  6001. * presentation.
  6002. *
  6003. * @return {boolean}
  6004. * @private
  6005. */
  6006. isBufferedToEndMS_() {
  6007. goog.asserts.assert(
  6008. this.video_,
  6009. 'We need a video element to get buffering information');
  6010. goog.asserts.assert(
  6011. this.mediaSourceEngine_,
  6012. 'We need a media source engine to get buffering information');
  6013. goog.asserts.assert(
  6014. this.manifest_,
  6015. 'We need a manifest to get buffering information');
  6016. // This is a strong guarantee that we are buffered to the end, because it
  6017. // means the playhead is already at that end.
  6018. if (this.video_.ended) {
  6019. return true;
  6020. }
  6021. // This means that MediaSource has buffered the final segment in all
  6022. // SourceBuffers and is no longer accepting additional segments.
  6023. if (this.mediaSourceEngine_.ended()) {
  6024. return true;
  6025. }
  6026. // Live streams are "buffered to the end" when they have buffered to the
  6027. // live edge or beyond (into the region covered by the presentation delay).
  6028. if (this.manifest_.presentationTimeline.isLive()) {
  6029. const liveEdge =
  6030. this.manifest_.presentationTimeline.getSegmentAvailabilityEnd();
  6031. const bufferEnd =
  6032. shaka.media.TimeRangesUtils.bufferEnd(this.video_.buffered);
  6033. if (bufferEnd != null && bufferEnd >= liveEdge) {
  6034. return true;
  6035. }
  6036. }
  6037. return false;
  6038. }
  6039. /**
  6040. * Assuming the player is playing content with src=, check if the player has
  6041. * buffered enough content to make it to the end of the presentation.
  6042. *
  6043. * @return {boolean}
  6044. * @private
  6045. */
  6046. isBufferedToEndSrc_() {
  6047. goog.asserts.assert(
  6048. this.video_,
  6049. 'We need a video element to get buffering information');
  6050. // This is a strong guarantee that we are buffered to the end, because it
  6051. // means the playhead is already at that end.
  6052. if (this.video_.ended) {
  6053. return true;
  6054. }
  6055. // If we have buffered to the duration of the content, it means we will have
  6056. // enough content to buffer to the end of the presentation.
  6057. const bufferEnd =
  6058. shaka.media.TimeRangesUtils.bufferEnd(this.video_.buffered);
  6059. // Because Safari's native HLS reports slightly inaccurate values for
  6060. // bufferEnd here, we use a fudge factor. Without this, we can end up in a
  6061. // buffering state at the end of the stream. See issue #2117.
  6062. // TODO: Try to remove the fudge here once we no longer manage buffering
  6063. // state above the browser with playbackRate=0.
  6064. const fudge = 1; // 1000 ms
  6065. return bufferEnd != null && bufferEnd >= this.video_.duration - fudge;
  6066. }
  6067. /**
  6068. * Create an error for when we purposely interrupt a load operation.
  6069. *
  6070. * @return {!shaka.util.Error}
  6071. * @private
  6072. */
  6073. createAbortLoadError_() {
  6074. return new shaka.util.Error(
  6075. shaka.util.Error.Severity.CRITICAL,
  6076. shaka.util.Error.Category.PLAYER,
  6077. shaka.util.Error.Code.LOAD_INTERRUPTED);
  6078. }
  6079. /**
  6080. * Key
  6081. * ----------------------
  6082. * D : Detach Node
  6083. * A : Attach Node
  6084. * MS : Media Source Node
  6085. * P : Manifest Parser Node
  6086. * M : Manifest Node
  6087. * DRM : Drm Engine Node
  6088. * L : Load Node
  6089. * U : Unloading Node
  6090. * SRC : Src Equals Node
  6091. *
  6092. * Graph Topology
  6093. * ----------------------
  6094. *
  6095. * [SRC]-----+
  6096. * ^ |
  6097. * | v
  6098. * [D]<-->[A]<-----[U]
  6099. * | ^
  6100. * v |
  6101. * [MS]------+
  6102. * | |
  6103. * v |
  6104. * [P]-------+
  6105. * | |
  6106. * v |
  6107. * [M]-------+
  6108. * | |
  6109. * v |
  6110. * [DRM]-----+
  6111. * | |
  6112. * v |
  6113. * [L]-------+
  6114. *
  6115. * @param {!shaka.routing.Node} currentlyAt
  6116. * @param {shaka.routing.Payload} currentlyWith
  6117. * @param {!shaka.routing.Node} wantsToBeAt
  6118. * @param {shaka.routing.Payload} wantsToHave
  6119. * @return {?shaka.routing.Node}
  6120. * @private
  6121. */
  6122. getNextStep_(currentlyAt, currentlyWith, wantsToBeAt, wantsToHave) {
  6123. let next = null;
  6124. // Detach is very simple, either stay in detach (because |detach| was called
  6125. // while in detached) or go somewhere that requires us to attach to an
  6126. // element.
  6127. if (currentlyAt == this.detachNode_) {
  6128. next = wantsToBeAt == this.detachNode_ ?
  6129. this.detachNode_ :
  6130. this.attachNode_;
  6131. }
  6132. if (currentlyAt == this.attachNode_) {
  6133. next = this.getNextAfterAttach_(wantsToBeAt, currentlyWith, wantsToHave);
  6134. }
  6135. if (currentlyAt == this.mediaSourceNode_) {
  6136. next = this.getNextAfterMediaSource_(
  6137. wantsToBeAt, currentlyWith, wantsToHave);
  6138. }
  6139. if (currentlyAt == this.parserNode_) {
  6140. next = this.getNextMatchingAllDependencies_(
  6141. /* destination= */ this.loadNode_,
  6142. /* next= */ this.manifestNode_,
  6143. /* reset= */ this.unloadNode_,
  6144. /* goingTo= */ wantsToBeAt,
  6145. /* has= */ currentlyWith,
  6146. /* wants= */ wantsToHave);
  6147. }
  6148. if (currentlyAt == this.manifestNode_) {
  6149. next = this.getNextMatchingAllDependencies_(
  6150. /* destination= */ this.loadNode_,
  6151. /* next= */ this.drmNode_,
  6152. /* reset= */ this.unloadNode_,
  6153. /* goingTo= */ wantsToBeAt,
  6154. /* has= */ currentlyWith,
  6155. /* wants= */ wantsToHave);
  6156. }
  6157. // For DRM, we have two options "load" or "unload". If all our constraints
  6158. // are met, we can go to "load". If anything is off, we must go back to
  6159. // "unload" to reset.
  6160. if (currentlyAt == this.drmNode_) {
  6161. next = this.getNextMatchingAllDependencies_(
  6162. /* destination= */ this.loadNode_,
  6163. /* next= */ this.loadNode_,
  6164. /* reset= */ this.unloadNode_,
  6165. /* goingTo= */ wantsToBeAt,
  6166. /* has= */ currentlyWith,
  6167. /* wants= */ wantsToHave);
  6168. }
  6169. // For DRM w/ src= playback, we only care about destination and media
  6170. // element.
  6171. if (currentlyAt == this.srcEqualsDrmNode_) {
  6172. if (wantsToBeAt == this.srcEqualsNode_ &&
  6173. currentlyWith.mediaElement == wantsToHave.mediaElement) {
  6174. next = this.srcEqualsNode_;
  6175. } else {
  6176. next = this.unloadNode_;
  6177. }
  6178. }
  6179. // After we load content, always go through unload because we can't safely
  6180. // use components after we have started playback.
  6181. if (currentlyAt == this.loadNode_ || currentlyAt == this.srcEqualsNode_) {
  6182. next = this.unloadNode_;
  6183. }
  6184. if (currentlyAt == this.unloadNode_) {
  6185. next = this.getNextAfterUnload_(wantsToBeAt, currentlyWith, wantsToHave);
  6186. }
  6187. goog.asserts.assert(next, 'Missing next step!');
  6188. return next;
  6189. }
  6190. /**
  6191. * @param {!shaka.routing.Node} goingTo
  6192. * @param {shaka.routing.Payload} has
  6193. * @param {shaka.routing.Payload} wants
  6194. * @return {?shaka.routing.Node}
  6195. * @private
  6196. */
  6197. getNextAfterAttach_(goingTo, has, wants) {
  6198. // Attach and detach are the only two nodes that we can directly go
  6199. // back-and-forth between.
  6200. if (goingTo == this.detachNode_) {
  6201. return this.detachNode_;
  6202. }
  6203. // If we are going anywhere other than detach, then we need the media
  6204. // element to match, if they don't match, we need to go through detach
  6205. // first.
  6206. if (has.mediaElement != wants.mediaElement) {
  6207. return this.detachNode_;
  6208. }
  6209. // If we are already in attached, and someone calls |attach| again (to the
  6210. // same video element), we can handle the redundant request by re-entering
  6211. // our current state.
  6212. if (goingTo == this.attachNode_) {
  6213. return this.attachNode_;
  6214. }
  6215. // The next step from attached to loaded is through media source.
  6216. if (goingTo == this.mediaSourceNode_ || goingTo == this.loadNode_) {
  6217. return this.mediaSourceNode_;
  6218. }
  6219. // If we are going to src=, then we should set up DRM first. This will
  6220. // support cases like FairPlay HLS on Safari.
  6221. if (goingTo == this.srcEqualsNode_) {
  6222. return this.srcEqualsDrmNode_;
  6223. }
  6224. // We are missing a rule, the null will get caught by a common check in
  6225. // the routing system.
  6226. return null;
  6227. }
  6228. /**
  6229. * @param {!shaka.routing.Node} goingTo
  6230. * @param {shaka.routing.Payload} has
  6231. * @param {shaka.routing.Payload} wants
  6232. * @return {?shaka.routing.Node}
  6233. * @private
  6234. */
  6235. getNextAfterMediaSource_(goingTo, has, wants) {
  6236. // We can only go to parse manifest or unload. If we want to go to load and
  6237. // we have the right media element, we can go to parse manifest. If we
  6238. // don't, no matter where we want to go, we must go through unload.
  6239. if (goingTo == this.loadNode_ && has.mediaElement == wants.mediaElement) {
  6240. return this.parserNode_;
  6241. }
  6242. // Right now the unload node is responsible for tearing down all playback
  6243. // components (including media source). So since we have created media
  6244. // source, we need to unload since our dependencies are not compatible.
  6245. //
  6246. // TODO: We are structured this way to maintain a historic structure. Going
  6247. // forward, there is no reason to restrict ourselves to this. Going
  6248. // forward we should explore breaking apart |onUnload| and develop
  6249. // more meaningful terminology around tearing down playback resources.
  6250. return this.unloadNode_;
  6251. }
  6252. /**
  6253. * After unload there are only two options, attached or detached. This choice
  6254. * is based on whether or not we have a media element. If we have a media
  6255. * element, then we go to attach. If we don't have a media element, we go to
  6256. * detach.
  6257. *
  6258. * @param {!shaka.routing.Node} goingTo
  6259. * @param {shaka.routing.Payload} has
  6260. * @param {shaka.routing.Payload} wants
  6261. * @return {?shaka.routing.Node}
  6262. * @private
  6263. */
  6264. getNextAfterUnload_(goingTo, has, wants) {
  6265. // If we don't want a media element, detach.
  6266. // If we have the wrong media element, detach.
  6267. // Otherwise it means we want to attach to a media element and it is safe to
  6268. // do so.
  6269. return !wants.mediaElement || has.mediaElement != wants.mediaElement ?
  6270. this.detachNode_ :
  6271. this.attachNode_;
  6272. }
  6273. /**
  6274. * A general method used to handle routing when we can either than one step
  6275. * toward our destination (while all our dependencies match) or go to a node
  6276. * that will reset us so we can try again.
  6277. *
  6278. * @param {!shaka.routing.Node} destinationNode
  6279. * What |goingTo| must be for us to step toward |nextNode|. Otherwise we
  6280. * will go to |resetNode|.
  6281. * @param {!shaka.routing.Node} nextNode
  6282. * The node we will go to next if |goingTo == destinationNode| and all
  6283. * dependencies match.
  6284. * @param {!shaka.routing.Node} resetNode
  6285. * The node we will go to next if |goingTo != destinationNode| or any
  6286. * dependency does not match.
  6287. * @param {!shaka.routing.Node} goingTo
  6288. * The node that the walker is trying to go to.
  6289. * @param {shaka.routing.Payload} has
  6290. * The payload that the walker currently has.
  6291. * @param {shaka.routing.Payload} wants
  6292. * The payload that the walker wants to have when iy gets to |goingTo|.
  6293. * @return {shaka.routing.Node}
  6294. * @private
  6295. */
  6296. getNextMatchingAllDependencies_(destinationNode, nextNode, resetNode, goingTo,
  6297. has, wants) {
  6298. if (goingTo == destinationNode &&
  6299. has.mediaElement == wants.mediaElement &&
  6300. has.uri == wants.uri &&
  6301. has.mimeType == wants.mimeType) {
  6302. return nextNode;
  6303. }
  6304. return resetNode;
  6305. }
  6306. /**
  6307. * @return {shaka.routing.Payload}
  6308. * @private
  6309. */
  6310. static createEmptyPayload_() {
  6311. return {
  6312. mediaElement: null,
  6313. mimeType: null,
  6314. startTime: null,
  6315. startTimeOfLoad: NaN,
  6316. uri: null,
  6317. };
  6318. }
  6319. /**
  6320. * Using a promise, wrap the listeners returned by |Walker.startNewRoute|.
  6321. * This will work for most usages in |Player| but should not be used for
  6322. * special cases.
  6323. *
  6324. * This will connect |onCancel|, |onEnd|, |onError|, and |onSkip| with
  6325. * |resolve| and |reject| but will leave |onStart| unset.
  6326. *
  6327. * @param {shaka.routing.Walker.Listeners} listeners
  6328. * @return {!Promise}
  6329. * @private
  6330. */
  6331. wrapWalkerListenersWithPromise_(listeners) {
  6332. return new Promise((resolve, reject) => {
  6333. listeners.onCancel = () => reject(this.createAbortLoadError_());
  6334. listeners.onEnd = () => resolve();
  6335. listeners.onError = (e) => reject(e);
  6336. listeners.onSkip = () => reject(this.createAbortLoadError_());
  6337. });
  6338. }
  6339. };
  6340. /**
  6341. * In order to know what method of loading the player used for some content, we
  6342. * have this enum. It lets us know if content has not been loaded, loaded with
  6343. * media source, or loaded with src equals.
  6344. *
  6345. * This enum has a low resolution, because it is only meant to express the
  6346. * outer limits of the various states that the player is in. For example, when
  6347. * someone calls a public method on player, it should not matter if they have
  6348. * initialized drm engine, it should only matter if they finished loading
  6349. * content.
  6350. *
  6351. * @enum {number}
  6352. * @export
  6353. */
  6354. shaka.Player.LoadMode = {
  6355. 'DESTROYED': 0,
  6356. 'NOT_LOADED': 1,
  6357. 'MEDIA_SOURCE': 2,
  6358. 'SRC_EQUALS': 3,
  6359. };
  6360. /**
  6361. * The typical buffering threshold. When we have less than this buffered (in
  6362. * seconds), we enter a buffering state. This specific value is based on manual
  6363. * testing and evaluation across a variety of platforms.
  6364. *
  6365. * To make the buffering logic work in all cases, this "typical" threshold will
  6366. * be overridden if the rebufferingGoal configuration is too low.
  6367. *
  6368. * @const {number}
  6369. * @private
  6370. */
  6371. shaka.Player.TYPICAL_BUFFERING_THRESHOLD_ = 0.5;
  6372. /**
  6373. * @define {string} A version number taken from git at compile time.
  6374. * @export
  6375. */
  6376. shaka.Player.version = 'v4.4.3-uncompiled';
  6377. // Initialize the deprecation system using the version string we just set
  6378. // on the player.
  6379. shaka.Deprecate.init(shaka.Player.version);
  6380. /**
  6381. * These are the EME key statuses that represent restricted playback.
  6382. * 'usable', 'released', 'output-downscaled', 'status-pending' are statuses
  6383. * of the usable keys. 'expired' status is being handled separately in
  6384. * DrmEngine.
  6385. *
  6386. * @const {!Array.<string>}
  6387. * @private
  6388. */
  6389. shaka.Player.restrictedStatuses_ = ['output-restricted', 'internal-error'];
  6390. /** @private {!Object.<string, function():*>} */
  6391. shaka.Player.supportPlugins_ = {};
  6392. /** @private {?shaka.extern.IAdManager.Factory} */
  6393. shaka.Player.adManagerFactory_ = null;
  6394. /**
  6395. * @const {!Object.<string, string>}
  6396. * @private
  6397. */
  6398. shaka.Player.SRC_EQUAL_EXTENSIONS_TO_MIME_TYPES_ = {
  6399. 'mp4': 'video/mp4',
  6400. 'm4v': 'video/mp4',
  6401. 'm4a': 'audio/mp4',
  6402. 'webm': 'video/webm',
  6403. 'weba': 'audio/webm',
  6404. 'mkv': 'video/webm', // Chromium browsers supports it.
  6405. 'ts': 'video/mp2t',
  6406. 'ogv': 'video/ogg',
  6407. 'ogg': 'audio/ogg',
  6408. 'mpg': 'video/mpeg',
  6409. 'mpeg': 'video/mpeg',
  6410. 'm3u8': 'application/x-mpegurl',
  6411. 'mpd': 'application/dash+xml',
  6412. 'mp3': 'audio/mpeg',
  6413. 'aac': 'audio/aac',
  6414. 'flac': 'audio/flac',
  6415. 'wav': 'audio/wav',
  6416. };
  6417. /**
  6418. * @const {!Object.<string, string>}
  6419. * @private
  6420. */
  6421. shaka.Player.TEXT_EXTENSIONS_TO_MIME_TYPES_ = {
  6422. 'sbv': 'text/x-subviewer',
  6423. 'srt': 'text/srt',
  6424. 'vtt': 'text/vtt',
  6425. 'webvtt': 'text/vtt',
  6426. 'ttml': 'application/ttml+xml',
  6427. 'lrc': 'application/x-subtitle-lrc',
  6428. 'ssa': 'text/x-ssa',
  6429. 'ass': 'text/x-ssa',
  6430. };
  6431. /**
  6432. * @const {string}
  6433. */
  6434. shaka.Player.TextTrackLabel = 'Shaka Player TextTrack';