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