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