Source: lib/dash/dash_parser.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.dash.DashParser');
  7. goog.require('goog.asserts');
  8. goog.require('goog.Uri');
  9. goog.require('shaka.Deprecate');
  10. goog.require('shaka.abr.Ewma');
  11. goog.require('shaka.dash.ContentProtection');
  12. goog.require('shaka.dash.MpdUtils');
  13. goog.require('shaka.dash.SegmentBase');
  14. goog.require('shaka.dash.SegmentList');
  15. goog.require('shaka.dash.SegmentTemplate');
  16. goog.require('shaka.log');
  17. goog.require('shaka.media.Capabilities');
  18. goog.require('shaka.media.ManifestParser');
  19. goog.require('shaka.media.PresentationTimeline');
  20. goog.require('shaka.media.SegmentIndex');
  21. goog.require('shaka.media.SegmentUtils');
  22. goog.require('shaka.net.NetworkingEngine');
  23. goog.require('shaka.text.TextEngine');
  24. goog.require('shaka.util.ContentSteeringManager');
  25. goog.require('shaka.util.Error');
  26. goog.require('shaka.util.EventManager');
  27. goog.require('shaka.util.Functional');
  28. goog.require('shaka.util.LanguageUtils');
  29. goog.require('shaka.util.ManifestParserUtils');
  30. goog.require('shaka.util.MimeUtils');
  31. goog.require('shaka.util.Networking');
  32. goog.require('shaka.util.ObjectUtils');
  33. goog.require('shaka.util.OperationManager');
  34. goog.require('shaka.util.PeriodCombiner');
  35. goog.require('shaka.util.PlayerConfiguration');
  36. goog.require('shaka.util.StreamUtils');
  37. goog.require('shaka.util.StringUtils');
  38. goog.require('shaka.util.Timer');
  39. goog.require('shaka.util.TXml');
  40. goog.require('shaka.util.XmlUtils');
  41. /**
  42. * Creates a new DASH parser.
  43. *
  44. * @implements {shaka.extern.ManifestParser}
  45. * @export
  46. */
  47. shaka.dash.DashParser = class {
  48. /** Creates a new DASH parser. */
  49. constructor() {
  50. /** @private {?shaka.extern.ManifestConfiguration} */
  51. this.config_ = null;
  52. /** @private {?shaka.extern.ManifestParser.PlayerInterface} */
  53. this.playerInterface_ = null;
  54. /** @private {!Array<string>} */
  55. this.manifestUris_ = [];
  56. /** @private {?shaka.extern.Manifest} */
  57. this.manifest_ = null;
  58. /** @private {number} */
  59. this.globalId_ = 1;
  60. /** @private {!Array<shaka.extern.xml.Node>} */
  61. this.patchLocationNodes_ = [];
  62. /**
  63. * A context of the living manifest used for processing
  64. * Patch MPD's
  65. * @private {!shaka.dash.DashParser.PatchContext}
  66. */
  67. this.manifestPatchContext_ = {
  68. mpdId: '',
  69. type: '',
  70. profiles: [],
  71. mediaPresentationDuration: null,
  72. availabilityTimeOffset: 0,
  73. getBaseUris: null,
  74. publishTime: 0,
  75. };
  76. /**
  77. * This is a cache is used the store a snapshot of the context
  78. * object which is built up throughout node traversal to maintain
  79. * a current state. This data needs to be preserved for parsing
  80. * patches.
  81. * The key is a combination period and representation id's.
  82. * @private {!Map<string, !shaka.dash.DashParser.Context>}
  83. */
  84. this.contextCache_ = new Map();
  85. /**
  86. * A map of IDs to Stream objects.
  87. * ID: Period@id,Representation@id
  88. * e.g.: '1,23'
  89. * @private {!Map<string, !shaka.extern.Stream>}
  90. */
  91. this.streamMap_ = new Map();
  92. /**
  93. * A map of Period IDs to Stream Map IDs.
  94. * Use to have direct access to streamMap key.
  95. * @private {!Map<string, !Array<string>>}
  96. */
  97. this.indexStreamMap_ = new Map();
  98. /**
  99. * A map of period ids to their durations
  100. * @private {!Map<string, number>}
  101. */
  102. this.periodDurations_ = new Map();
  103. /** @private {shaka.util.PeriodCombiner} */
  104. this.periodCombiner_ = new shaka.util.PeriodCombiner();
  105. /**
  106. * The update period in seconds, or 0 for no updates.
  107. * @private {number}
  108. */
  109. this.updatePeriod_ = 0;
  110. /**
  111. * An ewma that tracks how long updates take.
  112. * This is to mitigate issues caused by slow parsing on embedded devices.
  113. * @private {!shaka.abr.Ewma}
  114. */
  115. this.averageUpdateDuration_ = new shaka.abr.Ewma(5);
  116. /** @private {shaka.util.Timer} */
  117. this.updateTimer_ = new shaka.util.Timer(() => {
  118. if (this.mediaElement_ && !this.config_.continueLoadingWhenPaused) {
  119. this.eventManager_.unlisten(this.mediaElement_, 'timeupdate');
  120. if (this.mediaElement_.paused) {
  121. this.eventManager_.listenOnce(
  122. this.mediaElement_, 'timeupdate', () => this.onUpdate_());
  123. return;
  124. }
  125. }
  126. this.onUpdate_();
  127. });
  128. /** @private {!shaka.util.OperationManager} */
  129. this.operationManager_ = new shaka.util.OperationManager();
  130. /**
  131. * Largest period start time seen.
  132. * @private {?number}
  133. */
  134. this.largestPeriodStartTime_ = null;
  135. /**
  136. * Period IDs seen in previous manifest.
  137. * @private {!Array<string>}
  138. */
  139. this.lastManifestUpdatePeriodIds_ = [];
  140. /**
  141. * The minimum of the availabilityTimeOffset values among the adaptation
  142. * sets.
  143. * @private {number}
  144. */
  145. this.minTotalAvailabilityTimeOffset_ = Infinity;
  146. /** @private {boolean} */
  147. this.lowLatencyMode_ = false;
  148. /** @private {?shaka.util.ContentSteeringManager} */
  149. this.contentSteeringManager_ = null;
  150. /** @private {number} */
  151. this.gapCount_ = 0;
  152. /** @private {boolean} */
  153. this.isLowLatency_ = false;
  154. /** @private {shaka.util.EventManager} */
  155. this.eventManager_ = new shaka.util.EventManager();
  156. /** @private {HTMLMediaElement} */
  157. this.mediaElement_ = null;
  158. /** @private {boolean} */
  159. this.isTransitionFromDynamicToStatic_ = false;
  160. /** @private {string} */
  161. this.lastManifestQueryParams_ = '';
  162. /** @private {function():boolean} */
  163. this.isPreloadFn_ = () => false;
  164. /** @private {?Array<string>} */
  165. this.lastCalculatedBaseUris_ = [];
  166. }
  167. /**
  168. * @param {shaka.extern.ManifestConfiguration} config
  169. * @param {(function():boolean)=} isPreloadFn
  170. * @override
  171. * @exportInterface
  172. */
  173. configure(config, isPreloadFn) {
  174. goog.asserts.assert(config.dash != null,
  175. 'DashManifestConfiguration should not be null!');
  176. const needFireUpdate = this.playerInterface_ &&
  177. config.updatePeriod != this.config_.updatePeriod &&
  178. config.updatePeriod >= 0;
  179. this.config_ = config;
  180. if (isPreloadFn) {
  181. this.isPreloadFn_ = isPreloadFn;
  182. }
  183. if (needFireUpdate && this.manifest_ &&
  184. this.manifest_.presentationTimeline.isLive()) {
  185. this.updateNow_();
  186. }
  187. if (this.contentSteeringManager_) {
  188. this.contentSteeringManager_.configure(this.config_);
  189. }
  190. if (this.periodCombiner_) {
  191. this.periodCombiner_.setAllowMultiTypeVariants(
  192. this.config_.dash.multiTypeVariantsAllowed &&
  193. shaka.media.Capabilities.isChangeTypeSupported());
  194. this.periodCombiner_.setUseStreamOnce(
  195. this.config_.dash.useStreamOnceInPeriodFlattening);
  196. }
  197. }
  198. /**
  199. * @override
  200. * @exportInterface
  201. */
  202. async start(uri, playerInterface) {
  203. goog.asserts.assert(this.config_, 'Must call configure() before start()!');
  204. this.lowLatencyMode_ = playerInterface.isLowLatencyMode();
  205. this.manifestUris_ = [uri];
  206. this.playerInterface_ = playerInterface;
  207. const updateDelay = await this.requestManifest_();
  208. if (this.playerInterface_) {
  209. this.setUpdateTimer_(updateDelay);
  210. }
  211. // Make sure that the parser has not been destroyed.
  212. if (!this.playerInterface_) {
  213. throw new shaka.util.Error(
  214. shaka.util.Error.Severity.CRITICAL,
  215. shaka.util.Error.Category.PLAYER,
  216. shaka.util.Error.Code.OPERATION_ABORTED);
  217. }
  218. goog.asserts.assert(this.manifest_, 'Manifest should be non-null!');
  219. return this.manifest_;
  220. }
  221. /**
  222. * @override
  223. * @exportInterface
  224. */
  225. stop() {
  226. // When the parser stops, release all segment indexes, which stops their
  227. // timers, as well.
  228. for (const stream of this.streamMap_.values()) {
  229. if (stream.segmentIndex) {
  230. stream.segmentIndex.release();
  231. }
  232. }
  233. if (this.periodCombiner_) {
  234. this.periodCombiner_.release();
  235. }
  236. this.playerInterface_ = null;
  237. this.config_ = null;
  238. this.manifestUris_ = [];
  239. this.manifest_ = null;
  240. this.streamMap_.clear();
  241. this.indexStreamMap_.clear();
  242. this.contextCache_.clear();
  243. this.manifestPatchContext_ = {
  244. mpdId: '',
  245. type: '',
  246. profiles: [],
  247. mediaPresentationDuration: null,
  248. availabilityTimeOffset: 0,
  249. getBaseUris: null,
  250. publishTime: 0,
  251. };
  252. this.periodCombiner_ = null;
  253. if (this.updateTimer_ != null) {
  254. this.updateTimer_.stop();
  255. this.updateTimer_ = null;
  256. }
  257. if (this.contentSteeringManager_) {
  258. this.contentSteeringManager_.destroy();
  259. }
  260. if (this.eventManager_) {
  261. this.eventManager_.release();
  262. this.eventManager_ = null;
  263. }
  264. return this.operationManager_.destroy();
  265. }
  266. /**
  267. * @override
  268. * @exportInterface
  269. */
  270. async update() {
  271. try {
  272. await this.requestManifest_();
  273. } catch (error) {
  274. if (!this.playerInterface_ || !error) {
  275. return;
  276. }
  277. goog.asserts.assert(error instanceof shaka.util.Error, 'Bad error type');
  278. this.playerInterface_.onError(error);
  279. }
  280. }
  281. /**
  282. * @override
  283. * @exportInterface
  284. */
  285. onExpirationUpdated(sessionId, expiration) {
  286. // No-op
  287. }
  288. /**
  289. * @override
  290. * @exportInterface
  291. */
  292. onInitialVariantChosen(variant) {
  293. // For live it is necessary that the first time we update the manifest with
  294. // a shorter time than indicated to take into account that the last segment
  295. // added could be halfway, for example
  296. if (this.manifest_ && this.manifest_.presentationTimeline.isLive()) {
  297. const stream = variant.video || variant.audio;
  298. if (stream && stream.segmentIndex) {
  299. const availabilityEnd =
  300. this.manifest_.presentationTimeline.getSegmentAvailabilityEnd();
  301. const position = stream.segmentIndex.find(availabilityEnd);
  302. if (position == null) {
  303. return;
  304. }
  305. const reference = stream.segmentIndex.get(position);
  306. if (!reference) {
  307. return;
  308. }
  309. this.updatePeriod_ = reference.endTime - availabilityEnd;
  310. this.setUpdateTimer_(/* offset= */ 0);
  311. }
  312. }
  313. }
  314. /**
  315. * @override
  316. * @exportInterface
  317. */
  318. banLocation(uri) {
  319. if (this.contentSteeringManager_) {
  320. this.contentSteeringManager_.banLocation(uri);
  321. }
  322. }
  323. /**
  324. * @override
  325. * @exportInterface
  326. */
  327. setMediaElement(mediaElement) {
  328. this.mediaElement_ = mediaElement;
  329. }
  330. /**
  331. * Makes a network request for the manifest and parses the resulting data.
  332. *
  333. * @return {!Promise<number>} Resolves with the time it took, in seconds, to
  334. * fulfill the request and parse the data.
  335. * @private
  336. */
  337. async requestManifest_() {
  338. const requestType = shaka.net.NetworkingEngine.RequestType.MANIFEST;
  339. let type = shaka.net.NetworkingEngine.AdvancedRequestType.MPD;
  340. let rootElement = 'MPD';
  341. const patchLocationUris = this.getPatchLocationUris_();
  342. let manifestUris = this.manifestUris_;
  343. if (patchLocationUris.length) {
  344. manifestUris = patchLocationUris;
  345. rootElement = 'Patch';
  346. type = shaka.net.NetworkingEngine.AdvancedRequestType.MPD_PATCH;
  347. } else if (this.manifestUris_.length > 1 && this.contentSteeringManager_) {
  348. const locations = this.contentSteeringManager_.getLocations(
  349. 'Location', /* ignoreBaseUrls= */ true);
  350. if (locations.length) {
  351. manifestUris = locations;
  352. }
  353. }
  354. const request = shaka.net.NetworkingEngine.makeRequest(
  355. manifestUris, this.config_.retryParameters);
  356. const startTime = Date.now();
  357. const response = await this.makeNetworkRequest_(
  358. request, requestType, {type});
  359. // Detect calls to stop().
  360. if (!this.playerInterface_) {
  361. return 0;
  362. }
  363. // For redirections add the response uri to the first entry in the
  364. // Manifest Uris array.
  365. if (response.uri && response.uri != response.originalUri &&
  366. !this.manifestUris_.includes(response.uri)) {
  367. this.manifestUris_.unshift(response.uri);
  368. }
  369. const uriObj = new goog.Uri(response.uri);
  370. this.lastManifestQueryParams_ = uriObj.getQueryData().toString();
  371. // This may throw, but it will result in a failed promise.
  372. await this.parseManifest_(response.data, response.uri, rootElement);
  373. // Keep track of how long the longest manifest update took.
  374. const endTime = Date.now();
  375. const updateDuration = (endTime - startTime) / 1000.0;
  376. this.averageUpdateDuration_.sample(1, updateDuration);
  377. // Let the caller know how long this update took.
  378. return updateDuration;
  379. }
  380. /**
  381. * Parses the manifest XML. This also handles updates and will update the
  382. * stored manifest.
  383. *
  384. * @param {BufferSource} data
  385. * @param {string} finalManifestUri The final manifest URI, which may
  386. * differ from this.manifestUri_ if there has been a redirect.
  387. * @param {string} rootElement MPD or Patch, depending on context
  388. * @return {!Promise}
  389. * @private
  390. */
  391. async parseManifest_(data, finalManifestUri, rootElement) {
  392. let manifestData = data;
  393. const manifestPreprocessor = this.config_.dash.manifestPreprocessor;
  394. const defaultManifestPreprocessor =
  395. shaka.util.PlayerConfiguration.defaultManifestPreprocessor;
  396. if (manifestPreprocessor != defaultManifestPreprocessor) {
  397. shaka.Deprecate.deprecateFeature(5,
  398. 'manifest.dash.manifestPreprocessor configuration',
  399. 'Please Use manifest.dash.manifestPreprocessorTXml instead.');
  400. const mpdElement =
  401. shaka.util.XmlUtils.parseXml(manifestData, rootElement);
  402. if (!mpdElement) {
  403. throw new shaka.util.Error(
  404. shaka.util.Error.Severity.CRITICAL,
  405. shaka.util.Error.Category.MANIFEST,
  406. shaka.util.Error.Code.DASH_INVALID_XML,
  407. finalManifestUri);
  408. }
  409. manifestPreprocessor(mpdElement);
  410. manifestData = shaka.util.XmlUtils.toArrayBuffer(mpdElement);
  411. }
  412. const mpd = shaka.util.TXml.parseXml(manifestData, rootElement);
  413. if (!mpd) {
  414. throw new shaka.util.Error(
  415. shaka.util.Error.Severity.CRITICAL,
  416. shaka.util.Error.Category.MANIFEST,
  417. shaka.util.Error.Code.DASH_INVALID_XML,
  418. finalManifestUri);
  419. }
  420. const manifestPreprocessorTXml =
  421. this.config_.dash.manifestPreprocessorTXml;
  422. const defaultManifestPreprocessorTXml =
  423. shaka.util.PlayerConfiguration.defaultManifestPreprocessorTXml;
  424. if (manifestPreprocessorTXml != defaultManifestPreprocessorTXml) {
  425. manifestPreprocessorTXml(mpd);
  426. }
  427. if (rootElement === 'Patch') {
  428. return this.processPatchManifest_(mpd);
  429. }
  430. const disableXlinkProcessing = this.config_.dash.disableXlinkProcessing;
  431. if (disableXlinkProcessing) {
  432. return this.processManifest_(mpd, finalManifestUri);
  433. }
  434. // Process the mpd to account for xlink connections.
  435. const failGracefully = this.config_.dash.xlinkFailGracefully;
  436. const xlinkOperation = shaka.dash.MpdUtils.processXlinks(
  437. mpd, this.config_.retryParameters, failGracefully, finalManifestUri,
  438. this.playerInterface_.networkingEngine);
  439. this.operationManager_.manage(xlinkOperation);
  440. const finalMpd = await xlinkOperation.promise;
  441. return this.processManifest_(finalMpd, finalManifestUri);
  442. }
  443. /**
  444. * Takes a formatted MPD and converts it into a manifest.
  445. *
  446. * @param {!shaka.extern.xml.Node} mpd
  447. * @param {string} finalManifestUri The final manifest URI, which may
  448. * differ from this.manifestUri_ if there has been a redirect.
  449. * @return {!Promise}
  450. * @private
  451. */
  452. async processManifest_(mpd, finalManifestUri) {
  453. const TXml = shaka.util.TXml;
  454. goog.asserts.assert(this.config_,
  455. 'Must call configure() before processManifest_()!');
  456. if (this.contentSteeringManager_) {
  457. this.contentSteeringManager_.clearPreviousLocations();
  458. }
  459. // Get any Location elements. This will update the manifest location and
  460. // the base URI.
  461. /** @type {!Array<string>} */
  462. let manifestBaseUris = [finalManifestUri];
  463. /** @type {!Array<string>} */
  464. const locations = [];
  465. /** @type {!Map<string, string>} */
  466. const locationsMapping = new Map();
  467. const locationsObjs = TXml.findChildren(mpd, 'Location');
  468. for (const locationsObj of locationsObjs) {
  469. const serviceLocation = locationsObj.attributes['serviceLocation'];
  470. const uri = TXml.getContents(locationsObj);
  471. if (!uri) {
  472. continue;
  473. }
  474. const finalUri = shaka.util.ManifestParserUtils.resolveUris(
  475. manifestBaseUris, [uri])[0];
  476. if (serviceLocation) {
  477. if (this.contentSteeringManager_) {
  478. this.contentSteeringManager_.addLocation(
  479. 'Location', serviceLocation, finalUri);
  480. } else {
  481. locationsMapping.set(serviceLocation, finalUri);
  482. }
  483. }
  484. locations.push(finalUri);
  485. }
  486. if (this.contentSteeringManager_) {
  487. const steeringLocations = this.contentSteeringManager_.getLocations(
  488. 'Location', /* ignoreBaseUrls= */ true);
  489. if (steeringLocations.length > 0) {
  490. this.manifestUris_ = steeringLocations;
  491. manifestBaseUris = steeringLocations;
  492. }
  493. } else if (locations.length) {
  494. this.manifestUris_ = locations;
  495. manifestBaseUris = locations;
  496. }
  497. this.manifestPatchContext_.mpdId = mpd.attributes['id'] || '';
  498. this.manifestPatchContext_.publishTime =
  499. TXml.parseAttr(mpd, 'publishTime', TXml.parseDate) || 0;
  500. this.patchLocationNodes_ = TXml.findChildren(mpd, 'PatchLocation');
  501. let contentSteeringPromise = Promise.resolve();
  502. const contentSteering = TXml.findChild(mpd, 'ContentSteering');
  503. if (contentSteering && this.playerInterface_) {
  504. const defaultPathwayId =
  505. contentSteering.attributes['defaultServiceLocation'];
  506. if (!this.contentSteeringManager_) {
  507. this.contentSteeringManager_ =
  508. new shaka.util.ContentSteeringManager(this.playerInterface_);
  509. this.contentSteeringManager_.configure(this.config_);
  510. this.contentSteeringManager_.setManifestType(
  511. shaka.media.ManifestParser.DASH);
  512. this.contentSteeringManager_.setBaseUris(manifestBaseUris);
  513. this.contentSteeringManager_.setDefaultPathwayId(defaultPathwayId);
  514. const uri = TXml.getContents(contentSteering);
  515. if (uri) {
  516. const queryBeforeStart =
  517. TXml.parseAttr(contentSteering, 'queryBeforeStart',
  518. TXml.parseBoolean, /* defaultValue= */ false);
  519. if (queryBeforeStart) {
  520. contentSteeringPromise =
  521. this.contentSteeringManager_.requestInfo(uri);
  522. } else {
  523. this.contentSteeringManager_.requestInfo(uri);
  524. }
  525. }
  526. } else {
  527. this.contentSteeringManager_.setBaseUris(manifestBaseUris);
  528. this.contentSteeringManager_.setDefaultPathwayId(defaultPathwayId);
  529. }
  530. for (const serviceLocation of locationsMapping.keys()) {
  531. const uri = locationsMapping.get(serviceLocation);
  532. this.contentSteeringManager_.addLocation(
  533. 'Location', serviceLocation, uri);
  534. }
  535. }
  536. const uriObjs = TXml.findChildren(mpd, 'BaseURL');
  537. let someLocationValid = false;
  538. if (this.contentSteeringManager_) {
  539. for (const uriObj of uriObjs) {
  540. const serviceLocation = uriObj.attributes['serviceLocation'];
  541. const uri = TXml.getContents(uriObj);
  542. if (serviceLocation && uri) {
  543. this.contentSteeringManager_.addLocation(
  544. 'BaseURL', serviceLocation, uri);
  545. someLocationValid = true;
  546. }
  547. }
  548. }
  549. this.lastCalculatedBaseUris_ = null;
  550. if (!someLocationValid || !this.contentSteeringManager_) {
  551. const uris = uriObjs.map(TXml.getContents);
  552. this.lastCalculatedBaseUris_ = shaka.util.ManifestParserUtils.resolveUris(
  553. manifestBaseUris, uris);
  554. }
  555. const getBaseUris = () => {
  556. if (this.contentSteeringManager_ && someLocationValid) {
  557. return this.contentSteeringManager_.getLocations('BaseURL');
  558. }
  559. if (this.lastCalculatedBaseUris_) {
  560. return this.lastCalculatedBaseUris_;
  561. }
  562. return [];
  563. };
  564. this.manifestPatchContext_.getBaseUris = getBaseUris;
  565. let availabilityTimeOffset = 0;
  566. if (uriObjs && uriObjs.length) {
  567. availabilityTimeOffset = TXml.parseAttr(uriObjs[0],
  568. 'availabilityTimeOffset', TXml.parseFloat) || 0;
  569. }
  570. this.manifestPatchContext_.availabilityTimeOffset = availabilityTimeOffset;
  571. this.updatePeriod_ = /** @type {number} */ (TXml.parseAttr(
  572. mpd, 'minimumUpdatePeriod', TXml.parseDuration, -1));
  573. const presentationStartTime = TXml.parseAttr(
  574. mpd, 'availabilityStartTime', TXml.parseDate);
  575. let segmentAvailabilityDuration = TXml.parseAttr(
  576. mpd, 'timeShiftBufferDepth', TXml.parseDuration);
  577. const ignoreSuggestedPresentationDelay =
  578. this.config_.dash.ignoreSuggestedPresentationDelay;
  579. let suggestedPresentationDelay = null;
  580. if (!ignoreSuggestedPresentationDelay) {
  581. suggestedPresentationDelay = TXml.parseAttr(
  582. mpd, 'suggestedPresentationDelay', TXml.parseDuration);
  583. }
  584. const ignoreMaxSegmentDuration =
  585. this.config_.dash.ignoreMaxSegmentDuration;
  586. let maxSegmentDuration = null;
  587. if (!ignoreMaxSegmentDuration) {
  588. maxSegmentDuration = TXml.parseAttr(
  589. mpd, 'maxSegmentDuration', TXml.parseDuration);
  590. }
  591. const mpdType = mpd.attributes['type'] || 'static';
  592. if (this.manifest_ && this.manifest_.presentationTimeline) {
  593. this.isTransitionFromDynamicToStatic_ =
  594. this.manifest_.presentationTimeline.isLive() && mpdType == 'static';
  595. }
  596. this.manifestPatchContext_.type = mpdType;
  597. /** @type {!shaka.media.PresentationTimeline} */
  598. let presentationTimeline;
  599. if (this.manifest_) {
  600. presentationTimeline = this.manifest_.presentationTimeline;
  601. // Before processing an update, evict from all segment indexes. Some of
  602. // them may not get updated otherwise if their corresponding Period
  603. // element has been dropped from the manifest since the last update.
  604. // Without this, playback will still work, but this is necessary to
  605. // maintain conditions that we assert on for multi-Period content.
  606. // This gives us confidence that our state is maintained correctly, and
  607. // that the complex logic of multi-Period eviction and period-flattening
  608. // is correct. See also:
  609. // https://github.com/shaka-project/shaka-player/issues/3169#issuecomment-823580634
  610. const availabilityStart =
  611. presentationTimeline.getSegmentAvailabilityStart();
  612. for (const stream of this.streamMap_.values()) {
  613. if (stream.segmentIndex) {
  614. stream.segmentIndex.evict(availabilityStart);
  615. }
  616. }
  617. } else {
  618. const ignoreMinBufferTime = this.config_.dash.ignoreMinBufferTime;
  619. let minBufferTime = 0;
  620. if (!ignoreMinBufferTime) {
  621. minBufferTime =
  622. TXml.parseAttr(mpd, 'minBufferTime', TXml.parseDuration) || 0;
  623. }
  624. // DASH IOP v3.0 suggests using a default delay between minBufferTime
  625. // and timeShiftBufferDepth. This is literally the range of all
  626. // feasible choices for the value. Nothing older than
  627. // timeShiftBufferDepth is still available, and anything less than
  628. // minBufferTime will cause buffering issues.
  629. let delay = 0;
  630. if (suggestedPresentationDelay != null) {
  631. // 1. If a suggestedPresentationDelay is provided by the manifest, that
  632. // will be used preferentially.
  633. // This is given a minimum bound of segmentAvailabilityDuration.
  634. // Content providers should provide a suggestedPresentationDelay
  635. // whenever possible to optimize the live streaming experience.
  636. delay = Math.min(
  637. suggestedPresentationDelay,
  638. segmentAvailabilityDuration || Infinity);
  639. } else if (this.config_.defaultPresentationDelay > 0) {
  640. // 2. If the developer provides a value for
  641. // "manifest.defaultPresentationDelay", that is used as a fallback.
  642. delay = this.config_.defaultPresentationDelay;
  643. } else {
  644. // 3. Otherwise, we default to the lower of segmentAvailabilityDuration
  645. // and 1.5 * minBufferTime. This is fairly conservative.
  646. delay = Math.min(
  647. minBufferTime * 1.5, segmentAvailabilityDuration || Infinity);
  648. }
  649. presentationTimeline = new shaka.media.PresentationTimeline(
  650. presentationStartTime, delay, this.config_.dash.autoCorrectDrift);
  651. }
  652. presentationTimeline.setStatic(mpdType == 'static');
  653. const isLive = presentationTimeline.isLive();
  654. // If it's live, we check for an override.
  655. if (isLive && !isNaN(this.config_.availabilityWindowOverride)) {
  656. segmentAvailabilityDuration = this.config_.availabilityWindowOverride;
  657. }
  658. // If it's null, that means segments are always available. This is always
  659. // the case for VOD, and sometimes the case for live.
  660. if (segmentAvailabilityDuration == null) {
  661. segmentAvailabilityDuration = Infinity;
  662. }
  663. presentationTimeline.setSegmentAvailabilityDuration(
  664. segmentAvailabilityDuration);
  665. const profiles = mpd.attributes['profiles'] || '';
  666. this.manifestPatchContext_.profiles = profiles.split(',');
  667. /** @type {shaka.dash.DashParser.Context} */
  668. const context = {
  669. // Don't base on updatePeriod_ since emsg boxes can cause manifest
  670. // updates.
  671. dynamic: mpdType != 'static',
  672. presentationTimeline: presentationTimeline,
  673. period: null,
  674. periodInfo: null,
  675. adaptationSet: null,
  676. representation: null,
  677. bandwidth: 0,
  678. indexRangeWarningGiven: false,
  679. availabilityTimeOffset: availabilityTimeOffset,
  680. mediaPresentationDuration: null,
  681. profiles: profiles.split(','),
  682. roles: null,
  683. urlParams: () => '',
  684. };
  685. await contentSteeringPromise;
  686. this.gapCount_ = 0;
  687. const periodsAndDuration = this.parsePeriods_(
  688. context, getBaseUris, mpd, /* newPeriod= */ false);
  689. const duration = periodsAndDuration.duration;
  690. const periods = periodsAndDuration.periods;
  691. if ((mpdType == 'static' && !this.isTransitionFromDynamicToStatic_) ||
  692. !periodsAndDuration.durationDerivedFromPeriods) {
  693. // Ignore duration calculated from Period lengths if this is dynamic.
  694. presentationTimeline.setDuration(duration || Infinity);
  695. }
  696. if (this.isLowLatency_ && this.lowLatencyMode_) {
  697. presentationTimeline.setAvailabilityTimeOffset(
  698. this.minTotalAvailabilityTimeOffset_);
  699. }
  700. // Use @maxSegmentDuration to override smaller, derived values.
  701. presentationTimeline.notifyMaxSegmentDuration(maxSegmentDuration || 1);
  702. if (goog.DEBUG && !this.isTransitionFromDynamicToStatic_) {
  703. presentationTimeline.assertIsValid();
  704. }
  705. if (this.isLowLatency_ && this.lowLatencyMode_) {
  706. const presentationDelay = suggestedPresentationDelay != null ?
  707. suggestedPresentationDelay : this.config_.defaultPresentationDelay;
  708. presentationTimeline.setDelay(presentationDelay);
  709. }
  710. // These steps are not done on manifest update.
  711. if (!this.manifest_) {
  712. await this.periodCombiner_.combinePeriods(periods, context.dynamic);
  713. this.manifest_ = {
  714. presentationTimeline: presentationTimeline,
  715. variants: this.periodCombiner_.getVariants(),
  716. textStreams: this.periodCombiner_.getTextStreams(),
  717. imageStreams: this.periodCombiner_.getImageStreams(),
  718. offlineSessionIds: [],
  719. sequenceMode: this.config_.dash.sequenceMode,
  720. ignoreManifestTimestampsInSegmentsMode: false,
  721. type: shaka.media.ManifestParser.DASH,
  722. serviceDescription: this.parseServiceDescription_(mpd),
  723. nextUrl: this.parseMpdChaining_(mpd),
  724. periodCount: periods.length,
  725. gapCount: this.gapCount_,
  726. isLowLatency: this.isLowLatency_,
  727. startTime: null,
  728. };
  729. // We only need to do clock sync when we're using presentation start
  730. // time. This condition also excludes VOD streams.
  731. if (presentationTimeline.usingPresentationStartTime()) {
  732. const TXml = shaka.util.TXml;
  733. const timingElements = TXml.findChildren(mpd, 'UTCTiming');
  734. const offset = await this.parseUtcTiming_(getBaseUris, timingElements);
  735. // Detect calls to stop().
  736. if (!this.playerInterface_) {
  737. return;
  738. }
  739. presentationTimeline.setClockOffset(offset);
  740. }
  741. // This is the first point where we have a meaningful presentation start
  742. // time, and we need to tell PresentationTimeline that so that it can
  743. // maintain consistency from here on.
  744. presentationTimeline.lockStartTime();
  745. if (this.periodCombiner_ &&
  746. !this.manifest_.presentationTimeline.isLive()) {
  747. this.periodCombiner_.release();
  748. }
  749. } else {
  750. this.manifest_.periodCount = periods.length;
  751. this.manifest_.gapCount = this.gapCount_;
  752. await this.postPeriodProcessing_(periods, /* isPatchUpdate= */ false);
  753. }
  754. // Add text streams to correspond to closed captions. This happens right
  755. // after period combining, while we still have a direct reference, so that
  756. // any new streams will appear in the period combiner.
  757. this.playerInterface_.makeTextStreamsForClosedCaptions(this.manifest_);
  758. this.cleanStreamMap_();
  759. }
  760. /**
  761. * Handles common procedures after processing new periods.
  762. *
  763. * @param {!Array<shaka.extern.Period>} periods to be appended
  764. * @param {boolean} isPatchUpdate does call comes from mpd patch update
  765. * @private
  766. */
  767. async postPeriodProcessing_(periods, isPatchUpdate) {
  768. await this.periodCombiner_.combinePeriods(periods, true, isPatchUpdate);
  769. // Just update the variants and text streams, which may change as periods
  770. // are added or removed.
  771. this.manifest_.variants = this.periodCombiner_.getVariants();
  772. const textStreams = this.periodCombiner_.getTextStreams();
  773. if (textStreams.length > 0) {
  774. this.manifest_.textStreams = textStreams;
  775. }
  776. this.manifest_.imageStreams = this.periodCombiner_.getImageStreams();
  777. // Re-filter the manifest. This will check any configured restrictions on
  778. // new variants, and will pass any new init data to DrmEngine to ensure
  779. // that key rotation works correctly.
  780. this.playerInterface_.filter(this.manifest_);
  781. }
  782. /**
  783. * Takes a formatted Patch MPD and converts it into a manifest.
  784. *
  785. * @param {!shaka.extern.xml.Node} mpd
  786. * @return {!Promise}
  787. * @private
  788. */
  789. async processPatchManifest_(mpd) {
  790. const TXml = shaka.util.TXml;
  791. const mpdId = mpd.attributes['mpdId'];
  792. const originalPublishTime = TXml.parseAttr(mpd, 'originalPublishTime',
  793. TXml.parseDate);
  794. if (!mpdId || mpdId !== this.manifestPatchContext_.mpdId ||
  795. originalPublishTime !== this.manifestPatchContext_.publishTime) {
  796. // Clean patch location nodes, so it will force full MPD update.
  797. this.patchLocationNodes_ = [];
  798. throw new shaka.util.Error(
  799. shaka.util.Error.Severity.RECOVERABLE,
  800. shaka.util.Error.Category.MANIFEST,
  801. shaka.util.Error.Code.DASH_INVALID_PATCH);
  802. }
  803. /** @type {!Array<shaka.extern.Period>} */
  804. const newPeriods = [];
  805. /** @type {!Array<shaka.extern.xml.Node>} */
  806. const periodAdditions = [];
  807. /** @type {!Set<string>} */
  808. const modifiedTimelines = new Set();
  809. for (const patchNode of TXml.getChildNodes(mpd)) {
  810. let handled = true;
  811. const paths = TXml.parseXpath(patchNode.attributes['sel'] || '');
  812. const node = paths[paths.length - 1];
  813. const content = TXml.getContents(patchNode) || '';
  814. if (node.name === 'MPD') {
  815. if (node.attribute === 'mediaPresentationDuration') {
  816. const content = TXml.getContents(patchNode) || '';
  817. this.parsePatchMediaPresentationDurationChange_(content);
  818. } else if (node.attribute === 'type') {
  819. this.parsePatchMpdTypeChange_(content);
  820. } else if (node.attribute === 'publishTime') {
  821. this.manifestPatchContext_.publishTime = TXml.parseDate(content) || 0;
  822. } else if (node.attribute === null && patchNode.tagName === 'add') {
  823. periodAdditions.push(patchNode);
  824. } else {
  825. handled = false;
  826. }
  827. } else if (node.name === 'PatchLocation') {
  828. this.updatePatchLocationNodes_(patchNode);
  829. } else if (node.name === 'Period') {
  830. if (patchNode.tagName === 'add') {
  831. periodAdditions.push(patchNode);
  832. } else if (patchNode.tagName === 'remove' && node.id) {
  833. this.removePatchPeriod_(node.id);
  834. }
  835. } else if (node.name === 'SegmentTemplate') {
  836. const timelines = this.modifySegmentTemplate_(patchNode);
  837. for (const timeline of timelines) {
  838. modifiedTimelines.add(timeline);
  839. }
  840. } else if (node.name === 'SegmentTimeline' || node.name === 'S') {
  841. const timelines = this.modifyTimepoints_(patchNode);
  842. for (const timeline of timelines) {
  843. modifiedTimelines.add(timeline);
  844. }
  845. } else {
  846. handled = false;
  847. }
  848. if (!handled) {
  849. shaka.log.warning('Unhandled ' + patchNode.tagName + ' operation',
  850. patchNode.attributes['sel']);
  851. }
  852. }
  853. for (const timeline of modifiedTimelines) {
  854. this.parsePatchSegment_(timeline);
  855. }
  856. // Add new periods after extending timelines, as new periods
  857. // remove context cache of previous periods.
  858. for (const periodAddition of periodAdditions) {
  859. newPeriods.push(...this.parsePatchPeriod_(periodAddition));
  860. }
  861. if (newPeriods.length) {
  862. this.manifest_.periodCount += newPeriods.length;
  863. this.manifest_.gapCount = this.gapCount_;
  864. await this.postPeriodProcessing_(newPeriods, /* isPatchUpdate= */ true);
  865. }
  866. if (this.manifestPatchContext_.type == 'static') {
  867. const duration = this.manifestPatchContext_.mediaPresentationDuration;
  868. this.manifest_.presentationTimeline.setDuration(duration || Infinity);
  869. }
  870. }
  871. /**
  872. * Handles manifest type changes, this transition is expected to be
  873. * "dynamic" to "static".
  874. *
  875. * @param {!string} mpdType
  876. * @private
  877. */
  878. parsePatchMpdTypeChange_(mpdType) {
  879. this.manifest_.presentationTimeline.setStatic(mpdType == 'static');
  880. this.manifestPatchContext_.type = mpdType;
  881. for (const context of this.contextCache_.values()) {
  882. context.dynamic = mpdType == 'dynamic';
  883. }
  884. if (mpdType == 'static') {
  885. // Manifest is no longer dynamic, so stop live updates.
  886. this.updatePeriod_ = -1;
  887. }
  888. }
  889. /**
  890. * @param {string} durationString
  891. * @private
  892. */
  893. parsePatchMediaPresentationDurationChange_(durationString) {
  894. const duration = shaka.util.TXml.parseDuration(durationString);
  895. if (duration == null) {
  896. return;
  897. }
  898. this.manifestPatchContext_.mediaPresentationDuration = duration;
  899. for (const context of this.contextCache_.values()) {
  900. context.mediaPresentationDuration = duration;
  901. }
  902. }
  903. /**
  904. * Ingests a full MPD period element from a patch update
  905. *
  906. * @param {!shaka.extern.xml.Node} periods
  907. * @private
  908. */
  909. parsePatchPeriod_(periods) {
  910. goog.asserts.assert(this.manifestPatchContext_.getBaseUris,
  911. 'Must provide getBaseUris on manifestPatchContext_');
  912. /** @type {shaka.dash.DashParser.Context} */
  913. const context = {
  914. dynamic: this.manifestPatchContext_.type == 'dynamic',
  915. presentationTimeline: this.manifest_.presentationTimeline,
  916. period: null,
  917. periodInfo: null,
  918. adaptationSet: null,
  919. representation: null,
  920. bandwidth: 0,
  921. indexRangeWarningGiven: false,
  922. availabilityTimeOffset: this.manifestPatchContext_.availabilityTimeOffset,
  923. profiles: this.manifestPatchContext_.profiles,
  924. mediaPresentationDuration:
  925. this.manifestPatchContext_.mediaPresentationDuration,
  926. roles: null,
  927. urlParams: () => '',
  928. };
  929. const periodsAndDuration = this.parsePeriods_(context,
  930. this.manifestPatchContext_.getBaseUris, periods, /* newPeriod= */ true);
  931. return periodsAndDuration.periods;
  932. }
  933. /**
  934. * @param {string} periodId
  935. * @private
  936. */
  937. removePatchPeriod_(periodId) {
  938. const SegmentTemplate = shaka.dash.SegmentTemplate;
  939. this.manifest_.periodCount--;
  940. for (const contextId of this.contextCache_.keys()) {
  941. if (contextId.startsWith(periodId)) {
  942. const context = this.contextCache_.get(contextId);
  943. SegmentTemplate.removeTimepoints(context);
  944. this.parsePatchSegment_(contextId);
  945. this.contextCache_.delete(contextId);
  946. }
  947. }
  948. const newPeriods = this.lastManifestUpdatePeriodIds_.filter((pID) => {
  949. return pID !== periodId;
  950. });
  951. this.lastManifestUpdatePeriodIds_ = newPeriods;
  952. }
  953. /**
  954. * @param {!Array<shaka.util.TXml.PathNode>} paths
  955. * @return {!Array<string>}
  956. * @private
  957. */
  958. getContextIdsFromPath_(paths) {
  959. let periodId = '';
  960. let adaptationSetId = '';
  961. let adaptationSetPosition = -1;
  962. let representationId = '';
  963. for (const node of paths) {
  964. if (node.name === 'Period') {
  965. periodId = node.id;
  966. } else if (node.name === 'AdaptationSet') {
  967. adaptationSetId = node.id;
  968. if (node.position !== null) {
  969. adaptationSetPosition = node.position;
  970. }
  971. } else if (node.name === 'Representation') {
  972. representationId = node.id;
  973. }
  974. }
  975. /** @type {!Array<string>} */
  976. const contextIds = [];
  977. if (representationId) {
  978. contextIds.push(periodId + ',' + representationId);
  979. } else {
  980. if (adaptationSetId) {
  981. for (const context of this.contextCache_.values()) {
  982. if (context.period.id === periodId &&
  983. context.adaptationSet.id === adaptationSetId &&
  984. context.representation.id) {
  985. contextIds.push(periodId + ',' + context.representation.id);
  986. }
  987. }
  988. } else {
  989. if (adaptationSetPosition > -1) {
  990. for (const context of this.contextCache_.values()) {
  991. if (context.period.id === periodId &&
  992. context.adaptationSet.position === adaptationSetPosition &&
  993. context.representation.id) {
  994. contextIds.push(periodId + ',' + context.representation.id);
  995. }
  996. }
  997. }
  998. }
  999. }
  1000. return contextIds;
  1001. }
  1002. /**
  1003. * Modifies SegmentTemplate based on MPD patch.
  1004. *
  1005. * @param {!shaka.extern.xml.Node} patchNode
  1006. * @return {!Array<string>} context ids with updated timeline
  1007. * @private
  1008. */
  1009. modifySegmentTemplate_(patchNode) {
  1010. const TXml = shaka.util.TXml;
  1011. const paths = TXml.parseXpath(patchNode.attributes['sel'] || '');
  1012. const lastPath = paths[paths.length - 1];
  1013. if (!lastPath.attribute) {
  1014. return [];
  1015. }
  1016. const contextIds = this.getContextIdsFromPath_(paths);
  1017. const content = TXml.getContents(patchNode) || '';
  1018. for (const contextId of contextIds) {
  1019. /** @type {shaka.dash.DashParser.Context} */
  1020. const context = this.contextCache_.get(contextId);
  1021. goog.asserts.assert(context && context.representation.segmentTemplate,
  1022. 'cannot modify segment template');
  1023. TXml.modifyNodeAttribute(context.representation.segmentTemplate,
  1024. patchNode.tagName, lastPath.attribute, content);
  1025. }
  1026. return contextIds;
  1027. }
  1028. /**
  1029. * Ingests Patch MPD segments into timeline.
  1030. *
  1031. * @param {!shaka.extern.xml.Node} patchNode
  1032. * @return {!Array<string>} context ids with updated timeline
  1033. * @private
  1034. */
  1035. modifyTimepoints_(patchNode) {
  1036. const TXml = shaka.util.TXml;
  1037. const SegmentTemplate = shaka.dash.SegmentTemplate;
  1038. const paths = TXml.parseXpath(patchNode.attributes['sel'] || '');
  1039. const contextIds = this.getContextIdsFromPath_(paths);
  1040. for (const contextId of contextIds) {
  1041. /** @type {shaka.dash.DashParser.Context} */
  1042. const context = this.contextCache_.get(contextId);
  1043. SegmentTemplate.modifyTimepoints(context, patchNode);
  1044. }
  1045. return contextIds;
  1046. }
  1047. /**
  1048. * Parses modified segments.
  1049. *
  1050. * @param {string} contextId
  1051. * @private
  1052. */
  1053. parsePatchSegment_(contextId) {
  1054. /** @type {shaka.dash.DashParser.Context} */
  1055. const context = this.contextCache_.get(contextId);
  1056. const currentStream = this.streamMap_.get(contextId);
  1057. goog.asserts.assert(currentStream, 'stream should exist');
  1058. if (currentStream.segmentIndex) {
  1059. currentStream.segmentIndex.evict(
  1060. this.manifest_.presentationTimeline.getSegmentAvailabilityStart());
  1061. }
  1062. try {
  1063. const requestSegment = (uris, startByte, endByte, isInit) => {
  1064. return this.requestSegment_(uris, startByte, endByte, isInit);
  1065. };
  1066. // TODO we should obtain lastSegmentNumber if possible
  1067. const streamInfo = shaka.dash.SegmentTemplate.createStreamInfo(
  1068. context, requestSegment, this.streamMap_, /* isUpdate= */ true,
  1069. this.config_.dash.initialSegmentLimit, this.periodDurations_,
  1070. context.representation.aesKey, /* lastSegmentNumber= */ null,
  1071. /* isPatchUpdate= */ true);
  1072. currentStream.createSegmentIndex = async () => {
  1073. if (!currentStream.segmentIndex) {
  1074. currentStream.segmentIndex =
  1075. await streamInfo.generateSegmentIndex();
  1076. }
  1077. };
  1078. } catch (error) {
  1079. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  1080. const contentType = context.representation.contentType;
  1081. const isText = contentType == ContentType.TEXT ||
  1082. contentType == ContentType.APPLICATION;
  1083. const isImage = contentType == ContentType.IMAGE;
  1084. if (!(isText || isImage) ||
  1085. error.code != shaka.util.Error.Code.DASH_NO_SEGMENT_INFO) {
  1086. // We will ignore any DASH_NO_SEGMENT_INFO errors for text/image
  1087. throw error;
  1088. }
  1089. }
  1090. }
  1091. /**
  1092. * Reads maxLatency and maxPlaybackRate properties from service
  1093. * description element.
  1094. *
  1095. * @param {!shaka.extern.xml.Node} mpd
  1096. * @return {?shaka.extern.ServiceDescription}
  1097. * @private
  1098. */
  1099. parseServiceDescription_(mpd) {
  1100. const TXml = shaka.util.TXml;
  1101. const elem = TXml.findChild(mpd, 'ServiceDescription');
  1102. if (!elem ) {
  1103. return null;
  1104. }
  1105. const latencyNode = TXml.findChild(elem, 'Latency');
  1106. const playbackRateNode = TXml.findChild(elem, 'PlaybackRate');
  1107. if (!latencyNode && !playbackRateNode) {
  1108. return null;
  1109. }
  1110. const description = {};
  1111. if (latencyNode) {
  1112. if ('target' in latencyNode.attributes) {
  1113. description.targetLatency =
  1114. parseInt(latencyNode.attributes['target'], 10) / 1000;
  1115. }
  1116. if ('max' in latencyNode.attributes) {
  1117. description.maxLatency =
  1118. parseInt(latencyNode.attributes['max'], 10) / 1000;
  1119. }
  1120. if ('min' in latencyNode.attributes) {
  1121. description.minLatency =
  1122. parseInt(latencyNode.attributes['min'], 10) / 1000;
  1123. }
  1124. }
  1125. if (playbackRateNode) {
  1126. if ('max' in playbackRateNode.attributes) {
  1127. description.maxPlaybackRate =
  1128. parseFloat(playbackRateNode.attributes['max']);
  1129. }
  1130. if ('min' in playbackRateNode.attributes) {
  1131. description.minPlaybackRate =
  1132. parseFloat(playbackRateNode.attributes['min']);
  1133. }
  1134. }
  1135. return description;
  1136. }
  1137. /**
  1138. * Reads chaining url.
  1139. *
  1140. * @param {!shaka.extern.xml.Node} mpd
  1141. * @return {?string}
  1142. * @private
  1143. */
  1144. parseMpdChaining_(mpd) {
  1145. const TXml = shaka.util.TXml;
  1146. const supplementalProperties =
  1147. TXml.findChildren(mpd, 'SupplementalProperty');
  1148. if (!supplementalProperties.length) {
  1149. return null;
  1150. }
  1151. for (const prop of supplementalProperties) {
  1152. const schemeId = prop.attributes['schemeIdUri'];
  1153. if (schemeId == 'urn:mpeg:dash:chaining:2016') {
  1154. return prop.attributes['value'];
  1155. }
  1156. }
  1157. return null;
  1158. }
  1159. /**
  1160. * Reads and parses the periods from the manifest. This first does some
  1161. * partial parsing so the start and duration is available when parsing
  1162. * children.
  1163. *
  1164. * @param {shaka.dash.DashParser.Context} context
  1165. * @param {function(): !Array<string>} getBaseUris
  1166. * @param {!shaka.extern.xml.Node} mpd
  1167. * @param {!boolean} newPeriod
  1168. * @return {{
  1169. * periods: !Array<shaka.extern.Period>,
  1170. * duration: ?number,
  1171. * durationDerivedFromPeriods: boolean
  1172. * }}
  1173. * @private
  1174. */
  1175. parsePeriods_(context, getBaseUris, mpd, newPeriod) {
  1176. const TXml = shaka.util.TXml;
  1177. let presentationDuration = context.mediaPresentationDuration;
  1178. if (!presentationDuration) {
  1179. presentationDuration = TXml.parseAttr(
  1180. mpd, 'mediaPresentationDuration', TXml.parseDuration);
  1181. this.manifestPatchContext_.mediaPresentationDuration =
  1182. presentationDuration;
  1183. }
  1184. let seekRangeStart = 0;
  1185. if (this.manifest_ && this.manifest_.presentationTimeline &&
  1186. this.isTransitionFromDynamicToStatic_) {
  1187. seekRangeStart = this.manifest_.presentationTimeline.getSeekRangeStart();
  1188. }
  1189. const periods = [];
  1190. let prevEnd = seekRangeStart;
  1191. const periodNodes = TXml.findChildren(mpd, 'Period');
  1192. for (let i = 0; i < periodNodes.length; i++) {
  1193. const elem = periodNodes[i];
  1194. const next = periodNodes[i + 1];
  1195. let start = /** @type {number} */ (
  1196. TXml.parseAttr(elem, 'start', TXml.parseDuration, prevEnd));
  1197. const periodId = elem.attributes['id'];
  1198. const givenDuration =
  1199. TXml.parseAttr(elem, 'duration', TXml.parseDuration);
  1200. start = (i == 0 && start == 0 && this.isTransitionFromDynamicToStatic_) ?
  1201. seekRangeStart : start;
  1202. let periodDuration = null;
  1203. if (next) {
  1204. // "The difference between the start time of a Period and the start time
  1205. // of the following Period is the duration of the media content
  1206. // represented by this Period."
  1207. const nextStart =
  1208. TXml.parseAttr(next, 'start', TXml.parseDuration);
  1209. if (nextStart != null) {
  1210. periodDuration = nextStart - start + seekRangeStart;
  1211. }
  1212. } else if (presentationDuration != null) {
  1213. // "The Period extends until the Period.start of the next Period, or
  1214. // until the end of the Media Presentation in the case of the last
  1215. // Period."
  1216. periodDuration = presentationDuration - start + seekRangeStart;
  1217. }
  1218. const threshold =
  1219. shaka.util.ManifestParserUtils.GAP_OVERLAP_TOLERANCE_SECONDS;
  1220. if (periodDuration && givenDuration &&
  1221. Math.abs(periodDuration - givenDuration) > threshold) {
  1222. shaka.log.warning('There is a gap/overlap between Periods', elem);
  1223. // This means it's a gap, the distance between period starts is
  1224. // larger than the period's duration
  1225. if (periodDuration > givenDuration) {
  1226. this.gapCount_++;
  1227. }
  1228. }
  1229. // Only use the @duration in the MPD if we can't calculate it. We should
  1230. // favor the @start of the following Period. This ensures that there
  1231. // aren't gaps between Periods.
  1232. if (periodDuration == null) {
  1233. periodDuration = givenDuration;
  1234. }
  1235. /**
  1236. * This is to improve robustness when the player observes manifest with
  1237. * past periods that are inconsistent to previous ones.
  1238. *
  1239. * This may happen when a CDN or proxy server switches its upstream from
  1240. * one encoder to another redundant encoder.
  1241. *
  1242. * Skip periods that match all of the following criteria:
  1243. * - Start time is earlier than latest period start time ever seen
  1244. * - Period ID is never seen in the previous manifest
  1245. * - Not the last period in the manifest
  1246. *
  1247. * Periods that meet the aforementioned criteria are considered invalid
  1248. * and should be safe to discard.
  1249. */
  1250. if (this.largestPeriodStartTime_ !== null &&
  1251. periodId !== null && start !== null &&
  1252. start < this.largestPeriodStartTime_ &&
  1253. !this.lastManifestUpdatePeriodIds_.includes(periodId) &&
  1254. i + 1 != periodNodes.length) {
  1255. shaka.log.debug(
  1256. `Skipping Period with ID ${periodId} as its start time is smaller` +
  1257. ' than the largest period start time that has been seen, and ID ' +
  1258. 'is unseen before');
  1259. continue;
  1260. }
  1261. // Save maximum period start time if it is the last period
  1262. if (start !== null &&
  1263. (this.largestPeriodStartTime_ === null ||
  1264. start > this.largestPeriodStartTime_)) {
  1265. this.largestPeriodStartTime_ = start;
  1266. }
  1267. // Parse child nodes.
  1268. const info = {
  1269. start: start,
  1270. duration: periodDuration,
  1271. node: elem,
  1272. isLastPeriod: periodDuration == null || !next,
  1273. };
  1274. const period = this.parsePeriod_(context, getBaseUris, info);
  1275. periods.push(period);
  1276. if (context.period.id && periodDuration) {
  1277. this.periodDurations_.set(context.period.id, periodDuration);
  1278. }
  1279. if (periodDuration == null) {
  1280. if (next) {
  1281. // If the duration is still null and we aren't at the end, then we
  1282. // will skip any remaining periods.
  1283. shaka.log.warning(
  1284. 'Skipping Period', i + 1, 'and any subsequent Periods:', 'Period',
  1285. i + 1, 'does not have a valid start time.', next);
  1286. }
  1287. // The duration is unknown, so the end is unknown.
  1288. prevEnd = null;
  1289. break;
  1290. }
  1291. prevEnd = start + periodDuration;
  1292. } // end of period parsing loop
  1293. if (newPeriod) {
  1294. // append new period from the patch manifest
  1295. for (const el of periods) {
  1296. const periodID = el.id;
  1297. if (!this.lastManifestUpdatePeriodIds_.includes(periodID)) {
  1298. this.lastManifestUpdatePeriodIds_.push(periodID);
  1299. }
  1300. }
  1301. } else {
  1302. // Replace previous seen periods with the current one.
  1303. this.lastManifestUpdatePeriodIds_ = periods.map((el) => el.id);
  1304. }
  1305. if (presentationDuration != null) {
  1306. if (prevEnd != null) {
  1307. const threshold =
  1308. shaka.util.ManifestParserUtils.GAP_OVERLAP_TOLERANCE_SECONDS;
  1309. const difference = prevEnd - seekRangeStart - presentationDuration;
  1310. if (Math.abs(difference) > threshold) {
  1311. shaka.log.warning(
  1312. '@mediaPresentationDuration does not match the total duration ',
  1313. 'of all Periods.');
  1314. // Assume @mediaPresentationDuration is correct.
  1315. }
  1316. }
  1317. return {
  1318. periods: periods,
  1319. duration: presentationDuration + seekRangeStart,
  1320. durationDerivedFromPeriods: false,
  1321. };
  1322. } else {
  1323. return {
  1324. periods: periods,
  1325. duration: prevEnd,
  1326. durationDerivedFromPeriods: true,
  1327. };
  1328. }
  1329. }
  1330. /**
  1331. * Clean StreamMap Object to remove reference of deleted Stream Object
  1332. * @private
  1333. */
  1334. cleanStreamMap_() {
  1335. const oldPeriodIds = Array.from(this.indexStreamMap_.keys());
  1336. const diffPeriodsIDs = oldPeriodIds.filter((pId) => {
  1337. return !this.lastManifestUpdatePeriodIds_.includes(pId);
  1338. });
  1339. for (const pId of diffPeriodsIDs) {
  1340. let shouldDeleteIndex = true;
  1341. for (const contextId of this.indexStreamMap_.get(pId)) {
  1342. const stream = this.streamMap_.get(contextId);
  1343. if (!stream) {
  1344. continue;
  1345. }
  1346. if (stream.segmentIndex && !stream.segmentIndex.isEmpty()) {
  1347. shouldDeleteIndex = false;
  1348. continue;
  1349. }
  1350. if (this.periodCombiner_) {
  1351. this.periodCombiner_.deleteStream(stream, pId);
  1352. }
  1353. this.streamMap_.delete(contextId);
  1354. }
  1355. if (shouldDeleteIndex) {
  1356. this.indexStreamMap_.delete(pId);
  1357. }
  1358. }
  1359. }
  1360. /**
  1361. * Parses a Period XML element. Unlike the other parse methods, this is not
  1362. * given the Node; it is given a PeriodInfo structure. Also, partial parsing
  1363. * was done before this was called so start and duration are valid.
  1364. *
  1365. * @param {shaka.dash.DashParser.Context} context
  1366. * @param {function(): !Array<string>} getBaseUris
  1367. * @param {shaka.dash.DashParser.PeriodInfo} periodInfo
  1368. * @return {shaka.extern.Period}
  1369. * @private
  1370. */
  1371. parsePeriod_(context, getBaseUris, periodInfo) {
  1372. const Functional = shaka.util.Functional;
  1373. const TXml = shaka.util.TXml;
  1374. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  1375. goog.asserts.assert(periodInfo.node, 'periodInfo.node should exist');
  1376. context.period = this.createFrame_(periodInfo.node, null, getBaseUris);
  1377. context.periodInfo = periodInfo;
  1378. context.period.availabilityTimeOffset = context.availabilityTimeOffset;
  1379. // If the period doesn't have an ID, give it one based on its start time.
  1380. if (!context.period.id) {
  1381. shaka.log.info(
  1382. 'No Period ID given for Period with start time ' + periodInfo.start +
  1383. ', Assigning a default');
  1384. context.period.id = '__shaka_period_' + periodInfo.start;
  1385. }
  1386. const eventStreamNodes =
  1387. TXml.findChildren(periodInfo.node, 'EventStream');
  1388. const availabilityStart =
  1389. context.presentationTimeline.getSegmentAvailabilityStart();
  1390. for (const node of eventStreamNodes) {
  1391. this.parseEventStream_(
  1392. periodInfo.start, periodInfo.duration, node, availabilityStart);
  1393. }
  1394. const supplementalProperties =
  1395. TXml.findChildren(periodInfo.node, 'SupplementalProperty');
  1396. for (const prop of supplementalProperties) {
  1397. const schemeId = prop.attributes['schemeIdUri'];
  1398. if (schemeId == 'urn:mpeg:dash:urlparam:2014') {
  1399. const urlParams = this.getURLParametersFunction_(prop);
  1400. if (urlParams) {
  1401. context.urlParams = urlParams;
  1402. }
  1403. }
  1404. }
  1405. const adaptationSets =
  1406. TXml.findChildren(periodInfo.node, 'AdaptationSet')
  1407. .map((node, position) =>
  1408. this.parseAdaptationSet_(context, position, node))
  1409. .filter(Functional.isNotNull);
  1410. // For dynamic manifests, we use rep IDs internally, and they must be
  1411. // unique.
  1412. if (context.dynamic) {
  1413. const ids = [];
  1414. for (const set of adaptationSets) {
  1415. for (const id of set.representationIds) {
  1416. ids.push(id);
  1417. }
  1418. }
  1419. const uniqueIds = new Set(ids);
  1420. if (ids.length != uniqueIds.size) {
  1421. throw new shaka.util.Error(
  1422. shaka.util.Error.Severity.CRITICAL,
  1423. shaka.util.Error.Category.MANIFEST,
  1424. shaka.util.Error.Code.DASH_DUPLICATE_REPRESENTATION_ID);
  1425. }
  1426. }
  1427. /** @type {!Map<string, shaka.extern.Stream>} */
  1428. const dependencyStreamMap = new Map();
  1429. for (const adaptationSet of adaptationSets) {
  1430. for (const [dependencyId, stream] of adaptationSet.dependencyStreamMap) {
  1431. dependencyStreamMap.set(dependencyId, stream);
  1432. }
  1433. }
  1434. if (dependencyStreamMap.size) {
  1435. let duplicateAdaptationSets = null;
  1436. for (const adaptationSet of adaptationSets) {
  1437. const streamsWithDependencyStream = [];
  1438. for (const stream of adaptationSet.streams) {
  1439. if (dependencyStreamMap.has(stream.originalId)) {
  1440. if (!duplicateAdaptationSets) {
  1441. duplicateAdaptationSets =
  1442. TXml.findChildren(periodInfo.node, 'AdaptationSet')
  1443. .map((node, position) =>
  1444. this.parseAdaptationSet_(context, position, node))
  1445. .filter(Functional.isNotNull);
  1446. }
  1447. for (const duplicateAdaptationSet of duplicateAdaptationSets) {
  1448. const newStream = duplicateAdaptationSet.streams.find(
  1449. (s) => s.originalId == stream.originalId);
  1450. if (newStream) {
  1451. newStream.dependencyStream =
  1452. dependencyStreamMap.get(newStream.originalId);
  1453. streamsWithDependencyStream.push(newStream);
  1454. }
  1455. }
  1456. }
  1457. }
  1458. if (streamsWithDependencyStream.length) {
  1459. adaptationSet.streams.push(...streamsWithDependencyStream);
  1460. }
  1461. }
  1462. }
  1463. const normalAdaptationSets = adaptationSets
  1464. .filter((as) => { return !as.trickModeFor; });
  1465. const trickModeAdaptationSets = adaptationSets
  1466. .filter((as) => { return as.trickModeFor; });
  1467. // Attach trick mode tracks to normal tracks.
  1468. if (!this.config_.disableIFrames) {
  1469. for (const trickModeSet of trickModeAdaptationSets) {
  1470. const targetIds = trickModeSet.trickModeFor.split(' ');
  1471. for (const normalSet of normalAdaptationSets) {
  1472. if (targetIds.includes(normalSet.id)) {
  1473. for (const stream of normalSet.streams) {
  1474. shaka.util.StreamUtils.setBetterIFrameStream(
  1475. stream, trickModeSet.streams);
  1476. }
  1477. }
  1478. }
  1479. }
  1480. }
  1481. const audioStreams = this.getStreamsFromSets_(
  1482. this.config_.disableAudio,
  1483. normalAdaptationSets,
  1484. ContentType.AUDIO);
  1485. const videoStreams = this.getStreamsFromSets_(
  1486. this.config_.disableVideo,
  1487. normalAdaptationSets,
  1488. ContentType.VIDEO);
  1489. const textStreams = this.getStreamsFromSets_(
  1490. this.config_.disableText,
  1491. normalAdaptationSets,
  1492. ContentType.TEXT);
  1493. const imageStreams = this.getStreamsFromSets_(
  1494. this.config_.disableThumbnails,
  1495. normalAdaptationSets,
  1496. ContentType.IMAGE);
  1497. if (videoStreams.length === 0 && audioStreams.length === 0) {
  1498. throw new shaka.util.Error(
  1499. shaka.util.Error.Severity.CRITICAL,
  1500. shaka.util.Error.Category.MANIFEST,
  1501. shaka.util.Error.Code.DASH_EMPTY_PERIOD,
  1502. );
  1503. }
  1504. return {
  1505. id: context.period.id,
  1506. audioStreams,
  1507. videoStreams,
  1508. textStreams,
  1509. imageStreams,
  1510. };
  1511. }
  1512. /**
  1513. * Gets the streams from the given sets or returns an empty array if disabled
  1514. * or no streams are found.
  1515. * @param {boolean} disabled
  1516. * @param {!Array<!shaka.dash.DashParser.AdaptationInfo>} adaptationSets
  1517. * @param {string} contentType
  1518. * @private
  1519. */
  1520. getStreamsFromSets_(disabled, adaptationSets, contentType) {
  1521. if (disabled || !adaptationSets.length) {
  1522. return [];
  1523. }
  1524. return adaptationSets.reduce((all, part) => {
  1525. if (part.contentType != contentType) {
  1526. return all;
  1527. }
  1528. all.push(...part.streams);
  1529. return all;
  1530. }, []);
  1531. }
  1532. /**
  1533. * Parses an AdaptationSet XML element.
  1534. *
  1535. * @param {shaka.dash.DashParser.Context} context
  1536. * @param {number} position
  1537. * @param {!shaka.extern.xml.Node} elem The AdaptationSet element.
  1538. * @return {?shaka.dash.DashParser.AdaptationInfo}
  1539. * @private
  1540. */
  1541. parseAdaptationSet_(context, position, elem) {
  1542. const TXml = shaka.util.TXml;
  1543. const Functional = shaka.util.Functional;
  1544. const ManifestParserUtils = shaka.util.ManifestParserUtils;
  1545. const ContentType = ManifestParserUtils.ContentType;
  1546. const ContentProtection = shaka.dash.ContentProtection;
  1547. context.adaptationSet = this.createFrame_(elem, context.period, null);
  1548. context.adaptationSet.position = position;
  1549. let main = false;
  1550. const roleElements = TXml.findChildren(elem, 'Role');
  1551. const roleValues = roleElements.map((role) => {
  1552. return role.attributes['value'];
  1553. }).filter(Functional.isNotNull);
  1554. // Default kind for text streams is 'subtitle' if unspecified in the
  1555. // manifest.
  1556. let kind = undefined;
  1557. const isText = context.adaptationSet.contentType == ContentType.TEXT;
  1558. if (isText) {
  1559. kind = ManifestParserUtils.TextStreamKind.SUBTITLE;
  1560. }
  1561. for (const roleElement of roleElements) {
  1562. const scheme = roleElement.attributes['schemeIdUri'];
  1563. if (scheme == null || scheme == 'urn:mpeg:dash:role:2011') {
  1564. // These only apply for the given scheme, but allow them to be specified
  1565. // if there is no scheme specified.
  1566. // See: DASH section 5.8.5.5
  1567. const value = roleElement.attributes['value'];
  1568. switch (value) {
  1569. case 'main':
  1570. main = true;
  1571. break;
  1572. case 'caption':
  1573. case 'subtitle':
  1574. kind = value;
  1575. break;
  1576. }
  1577. }
  1578. }
  1579. // Parallel for HLS VIDEO-RANGE as defined in DASH-IF IOP v4.3 6.2.5.1.
  1580. let videoRange;
  1581. let colorGamut;
  1582. // Ref. https://dashif.org/docs/DASH-IF-IOP-v4.3.pdf
  1583. // If signaled, a Supplemental or Essential Property descriptor
  1584. // shall be used, with the schemeIdUri set to
  1585. // urn:mpeg:mpegB:cicp:<Parameter> as defined in
  1586. // ISO/IEC 23001-8 [49] and <Parameter> one of the
  1587. // following: ColourPrimaries, TransferCharacteristics,
  1588. // or MatrixCoefficients.
  1589. const scheme = 'urn:mpeg:mpegB:cicp';
  1590. const transferCharacteristicsScheme = `${scheme}:TransferCharacteristics`;
  1591. const colourPrimariesScheme = `${scheme}:ColourPrimaries`;
  1592. const matrixCoefficientsScheme = `${scheme}:MatrixCoefficients`;
  1593. const getVideoRangeFromTransferCharacteristicCICP = (cicp) => {
  1594. switch (cicp) {
  1595. case 1:
  1596. case 6:
  1597. case 13:
  1598. case 14:
  1599. case 15:
  1600. return 'SDR';
  1601. case 16:
  1602. return 'PQ';
  1603. case 18:
  1604. return 'HLG';
  1605. }
  1606. return undefined;
  1607. };
  1608. const getColorGamutFromColourPrimariesCICP = (cicp) => {
  1609. switch (cicp) {
  1610. case 1:
  1611. case 5:
  1612. case 6:
  1613. case 7:
  1614. return 'srgb';
  1615. case 9:
  1616. return 'rec2020';
  1617. case 11:
  1618. case 12:
  1619. return 'p3';
  1620. }
  1621. return undefined;
  1622. };
  1623. const parseFont = (prop) => {
  1624. const fontFamily = prop.attributes['dvb:fontFamily'];
  1625. const fontUrl = prop.attributes['dvb:url'];
  1626. if (fontFamily && fontUrl) {
  1627. const uris = shaka.util.ManifestParserUtils.resolveUris(
  1628. context.adaptationSet.getBaseUris(), [fontUrl],
  1629. context.urlParams());
  1630. this.playerInterface_.addFont(fontFamily, uris[0]);
  1631. }
  1632. };
  1633. const essentialProperties =
  1634. TXml.findChildren(elem, 'EssentialProperty');
  1635. // ID of real AdaptationSet if this is a trick mode set:
  1636. let trickModeFor = null;
  1637. let isFastSwitching = false;
  1638. let adaptationSetUrlParams = null;
  1639. let unrecognizedEssentialProperty = false;
  1640. for (const prop of essentialProperties) {
  1641. const schemeId = prop.attributes['schemeIdUri'];
  1642. if (schemeId == 'http://dashif.org/guidelines/trickmode') {
  1643. trickModeFor = prop.attributes['value'];
  1644. } else if (schemeId == transferCharacteristicsScheme) {
  1645. videoRange = getVideoRangeFromTransferCharacteristicCICP(
  1646. parseInt(prop.attributes['value'], 10),
  1647. );
  1648. } else if (schemeId == colourPrimariesScheme) {
  1649. colorGamut = getColorGamutFromColourPrimariesCICP(
  1650. parseInt(prop.attributes['value'], 10),
  1651. );
  1652. } else if (schemeId == matrixCoefficientsScheme) {
  1653. continue;
  1654. } else if (schemeId == 'urn:mpeg:dash:ssr:2023' &&
  1655. this.config_.dash.enableFastSwitching) {
  1656. isFastSwitching = true;
  1657. } else if (schemeId == 'urn:dvb:dash:fontdownload:2014') {
  1658. parseFont(prop);
  1659. } else if (schemeId == 'urn:mpeg:dash:urlparam:2014') {
  1660. adaptationSetUrlParams = this.getURLParametersFunction_(prop);
  1661. if (!adaptationSetUrlParams) {
  1662. unrecognizedEssentialProperty = true;
  1663. }
  1664. } else {
  1665. unrecognizedEssentialProperty = true;
  1666. }
  1667. }
  1668. // According to DASH spec (2014) section 5.8.4.8, "the successful processing
  1669. // of the descriptor is essential to properly use the information in the
  1670. // parent element". According to DASH IOP v3.3, section 3.3.4, "if the
  1671. // scheme or the value" for EssentialProperty is not recognized, "the DASH
  1672. // client shall ignore the parent element."
  1673. if (unrecognizedEssentialProperty) {
  1674. // Stop parsing this AdaptationSet and let the caller filter out the
  1675. // nulls.
  1676. return null;
  1677. }
  1678. let lastSegmentNumber = null;
  1679. const supplementalProperties =
  1680. TXml.findChildren(elem, 'SupplementalProperty');
  1681. for (const prop of supplementalProperties) {
  1682. const schemeId = prop.attributes['schemeIdUri'];
  1683. if (schemeId == 'http://dashif.org/guidelines/last-segment-number') {
  1684. lastSegmentNumber = parseInt(prop.attributes['value'], 10) - 1;
  1685. } else if (schemeId == transferCharacteristicsScheme) {
  1686. videoRange = getVideoRangeFromTransferCharacteristicCICP(
  1687. parseInt(prop.attributes['value'], 10),
  1688. );
  1689. } else if (schemeId == colourPrimariesScheme) {
  1690. colorGamut = getColorGamutFromColourPrimariesCICP(
  1691. parseInt(prop.attributes['value'], 10),
  1692. );
  1693. } else if (schemeId == 'urn:dvb:dash:fontdownload:2014') {
  1694. parseFont(prop);
  1695. } else if (schemeId == 'urn:mpeg:dash:urlparam:2014') {
  1696. adaptationSetUrlParams = this.getURLParametersFunction_(prop);
  1697. }
  1698. }
  1699. if (adaptationSetUrlParams) {
  1700. context.urlParams = adaptationSetUrlParams;
  1701. }
  1702. const accessibilities = TXml.findChildren(elem, 'Accessibility');
  1703. const LanguageUtils = shaka.util.LanguageUtils;
  1704. const closedCaptions = new Map();
  1705. /** @type {?shaka.media.ManifestParser.AccessibilityPurpose} */
  1706. let accessibilityPurpose;
  1707. for (const prop of accessibilities) {
  1708. const schemeId = prop.attributes['schemeIdUri'];
  1709. const value = prop.attributes['value'];
  1710. if (schemeId == 'urn:scte:dash:cc:cea-608:2015' &&
  1711. !this.config_.disableText) {
  1712. let channelId = 1;
  1713. if (value != null) {
  1714. const channelAssignments = value.split(';');
  1715. for (const captionStr of channelAssignments) {
  1716. let channel;
  1717. let language;
  1718. // Some closed caption descriptions have channel number and
  1719. // language ("CC1=eng") others may only have language ("eng,spa").
  1720. if (!captionStr.includes('=')) {
  1721. // When the channel assignments are not explicitly provided and
  1722. // there are only 2 values provided, it is highly likely that the
  1723. // assignments are CC1 and CC3 (most commonly used CC streams).
  1724. // Otherwise, cycle through all channels arbitrarily (CC1 - CC4)
  1725. // in order of provided langs.
  1726. channel = `CC${channelId}`;
  1727. if (channelAssignments.length == 2) {
  1728. channelId += 2;
  1729. } else {
  1730. channelId ++;
  1731. }
  1732. language = captionStr;
  1733. } else {
  1734. const channelAndLanguage = captionStr.split('=');
  1735. // The channel info can be '1' or 'CC1'.
  1736. // If the channel info only has channel number(like '1'), add 'CC'
  1737. // as prefix so that it can be a full channel id (like 'CC1').
  1738. channel = channelAndLanguage[0].startsWith('CC') ?
  1739. channelAndLanguage[0] : `CC${channelAndLanguage[0]}`;
  1740. // 3 letters (ISO 639-2). In b/187442669, we saw a blank string
  1741. // (CC2=;CC3=), so default to "und" (the code for "undetermined").
  1742. language = channelAndLanguage[1] || 'und';
  1743. }
  1744. closedCaptions.set(channel, LanguageUtils.normalize(language));
  1745. }
  1746. } else {
  1747. // If channel and language information has not been provided, assign
  1748. // 'CC1' as channel id and 'und' as language info.
  1749. closedCaptions.set('CC1', 'und');
  1750. }
  1751. } else if (schemeId == 'urn:scte:dash:cc:cea-708:2015' &&
  1752. !this.config_.disableText) {
  1753. let serviceNumber = 1;
  1754. if (value != null) {
  1755. for (const captionStr of value.split(';')) {
  1756. let service;
  1757. let language;
  1758. // Similar to CEA-608, it is possible that service # assignments
  1759. // are not explicitly provided e.g. "eng;deu;swe" In this case,
  1760. // we just cycle through the services for each language one by one.
  1761. if (!captionStr.includes('=')) {
  1762. service = `svc${serviceNumber}`;
  1763. serviceNumber ++;
  1764. language = captionStr;
  1765. } else {
  1766. // Otherwise, CEA-708 caption values take the form "
  1767. // 1=lang:eng;2=lang:deu" i.e. serviceNumber=lang:threeLetterCode.
  1768. const serviceAndLanguage = captionStr.split('=');
  1769. service = `svc${serviceAndLanguage[0]}`;
  1770. // The language info can be different formats, lang:eng',
  1771. // or 'lang:eng,war:1,er:1'. Extract the language info.
  1772. language = serviceAndLanguage[1].split(',')[0].split(':').pop();
  1773. }
  1774. closedCaptions.set(service, LanguageUtils.normalize(language));
  1775. }
  1776. } else {
  1777. // If service and language information has not been provided, assign
  1778. // 'svc1' as service number and 'und' as language info.
  1779. closedCaptions.set('svc1', 'und');
  1780. }
  1781. } else if (schemeId == 'urn:mpeg:dash:role:2011') {
  1782. // See DASH IOP 3.9.2 Table 4.
  1783. if (value != null) {
  1784. roleValues.push(value);
  1785. if (value == 'captions') {
  1786. kind = ManifestParserUtils.TextStreamKind.CLOSED_CAPTION;
  1787. }
  1788. }
  1789. } else if (schemeId == 'urn:tva:metadata:cs:AudioPurposeCS:2007') {
  1790. // See DASH DVB Document A168 Rev.6 Table 5.
  1791. if (value == '1') {
  1792. accessibilityPurpose =
  1793. shaka.media.ManifestParser.AccessibilityPurpose.VISUALLY_IMPAIRED;
  1794. } else if (value == '2') {
  1795. accessibilityPurpose =
  1796. shaka.media.ManifestParser.AccessibilityPurpose.HARD_OF_HEARING;
  1797. }
  1798. }
  1799. }
  1800. const contentProtectionElements =
  1801. TXml.findChildren(elem, 'ContentProtection');
  1802. const contentProtection = ContentProtection.parseFromAdaptationSet(
  1803. contentProtectionElements,
  1804. this.config_.ignoreDrmInfo,
  1805. this.config_.dash.keySystemsByURI);
  1806. // We us contentProtectionElements instead of drmInfos as the latter is
  1807. // not populated yet, and we need the encrypted flag for the upcoming
  1808. // parseRepresentation that will set the encrypted flag to the init seg.
  1809. context.adaptationSet.encrypted = contentProtectionElements.length > 0;
  1810. const language = shaka.util.LanguageUtils.normalize(
  1811. context.adaptationSet.language || 'und');
  1812. const label = context.adaptationSet.label;
  1813. /** @type {!Map<string, shaka.extern.Stream>} */
  1814. const dependencyStreamMap = new Map();
  1815. // Parse Representations into Streams.
  1816. const representations = TXml.findChildren(elem, 'Representation');
  1817. if (!this.config_.ignoreSupplementalCodecs) {
  1818. const supplementalRepresentations = [];
  1819. for (const rep of representations) {
  1820. const supplementalCodecs = TXml.getAttributeNS(
  1821. rep, shaka.dash.DashParser.SCTE214_, 'supplementalCodecs');
  1822. if (supplementalCodecs) {
  1823. // Duplicate representations with their supplementalCodecs
  1824. const obj = shaka.util.ObjectUtils.cloneObject(rep);
  1825. obj.attributes['codecs'] = supplementalCodecs.split(' ').join(',');
  1826. if (obj.attributes['id']) {
  1827. obj.attributes['supplementalId'] =
  1828. obj.attributes['id'] + '_supplementalCodecs';
  1829. }
  1830. supplementalRepresentations.push(obj);
  1831. }
  1832. }
  1833. representations.push(...supplementalRepresentations);
  1834. }
  1835. const streams = representations.map((representation) => {
  1836. const parsedRepresentation = this.parseRepresentation_(context,
  1837. contentProtection, kind, language, label, main, roleValues,
  1838. closedCaptions, representation, accessibilityPurpose,
  1839. lastSegmentNumber);
  1840. if (parsedRepresentation) {
  1841. parsedRepresentation.hdr = parsedRepresentation.hdr || videoRange;
  1842. parsedRepresentation.colorGamut =
  1843. parsedRepresentation.colorGamut || colorGamut;
  1844. parsedRepresentation.fastSwitching = isFastSwitching;
  1845. const dependencyId = representation.attributes['dependencyId'];
  1846. if (dependencyId) {
  1847. parsedRepresentation.baseOriginalId = dependencyId;
  1848. dependencyStreamMap.set(dependencyId, parsedRepresentation);
  1849. return null;
  1850. }
  1851. }
  1852. return parsedRepresentation;
  1853. }).filter((s) => !!s);
  1854. if (streams.length == 0 && dependencyStreamMap.size == 0) {
  1855. const isImage = context.adaptationSet.contentType == ContentType.IMAGE;
  1856. // Ignore empty AdaptationSets if ignoreEmptyAdaptationSet is true
  1857. // or they are for text/image content.
  1858. if (this.config_.dash.ignoreEmptyAdaptationSet || isText || isImage) {
  1859. return null;
  1860. }
  1861. throw new shaka.util.Error(
  1862. shaka.util.Error.Severity.CRITICAL,
  1863. shaka.util.Error.Category.MANIFEST,
  1864. shaka.util.Error.Code.DASH_EMPTY_ADAPTATION_SET);
  1865. }
  1866. // If AdaptationSet's type is unknown or is ambiguously "application",
  1867. // guess based on the information in the first stream. If the attributes
  1868. // mimeType and codecs are split across levels, they will both be inherited
  1869. // down to the stream level by this point, so the stream will have all the
  1870. // necessary information.
  1871. if (!context.adaptationSet.contentType ||
  1872. context.adaptationSet.contentType == ContentType.APPLICATION) {
  1873. const mimeType = streams[0].mimeType;
  1874. const codecs = streams[0].codecs;
  1875. context.adaptationSet.contentType =
  1876. shaka.dash.DashParser.guessContentType_(mimeType, codecs);
  1877. for (const stream of streams) {
  1878. stream.type = context.adaptationSet.contentType;
  1879. }
  1880. }
  1881. const adaptationId = context.adaptationSet.id ||
  1882. ('__fake__' + this.globalId_++);
  1883. for (const stream of streams) {
  1884. // Some DRM license providers require that we have a default
  1885. // key ID from the manifest in the wrapped license request.
  1886. // Thus, it should be put in drmInfo to be accessible to request filters.
  1887. for (const drmInfo of contentProtection.drmInfos) {
  1888. drmInfo.keyIds = drmInfo.keyIds && stream.keyIds ?
  1889. new Set([...drmInfo.keyIds, ...stream.keyIds]) :
  1890. drmInfo.keyIds || stream.keyIds;
  1891. }
  1892. stream.groupId = adaptationId;
  1893. }
  1894. const repIds = representations
  1895. .map((node) => {
  1896. return node.attributes['supplementalId'] || node.attributes['id'];
  1897. }).filter(shaka.util.Functional.isNotNull);
  1898. return {
  1899. id: adaptationId,
  1900. contentType: context.adaptationSet.contentType,
  1901. language: language,
  1902. main: main,
  1903. streams: streams,
  1904. drmInfos: contentProtection.drmInfos,
  1905. trickModeFor: trickModeFor,
  1906. representationIds: repIds,
  1907. dependencyStreamMap,
  1908. };
  1909. }
  1910. /**
  1911. * @param {!shaka.extern.xml.Node} elem
  1912. * @return {?function():string}
  1913. * @private
  1914. */
  1915. getURLParametersFunction_(elem) {
  1916. const TXml = shaka.util.TXml;
  1917. const urlQueryInfo = TXml.findChildNS(
  1918. elem, shaka.dash.DashParser.UP_NAMESPACE_, 'UrlQueryInfo');
  1919. if (urlQueryInfo && TXml.parseAttr(urlQueryInfo, 'useMPDUrlQuery',
  1920. TXml.parseBoolean, /* defaultValue= */ false)) {
  1921. const queryTemplate = urlQueryInfo.attributes['queryTemplate'];
  1922. if (queryTemplate) {
  1923. return () => {
  1924. if (queryTemplate == '$querypart$') {
  1925. return this.lastManifestQueryParams_;
  1926. }
  1927. const parameters = queryTemplate.split('&').map((param) => {
  1928. if (param == '$querypart$') {
  1929. return this.lastManifestQueryParams_;
  1930. } else {
  1931. const regex = /\$query:(.*?)\$/g;
  1932. const parts = regex.exec(param);
  1933. if (parts && parts.length == 2) {
  1934. const paramName = parts[1];
  1935. const queryData =
  1936. new goog.Uri.QueryData(this.lastManifestQueryParams_);
  1937. const value = queryData.get(paramName);
  1938. if (value.length) {
  1939. return paramName + '=' + value[0];
  1940. }
  1941. }
  1942. return param;
  1943. }
  1944. });
  1945. return parameters.join('&');
  1946. };
  1947. }
  1948. }
  1949. return null;
  1950. }
  1951. /**
  1952. * Parses a Representation XML element.
  1953. *
  1954. * @param {shaka.dash.DashParser.Context} context
  1955. * @param {shaka.dash.ContentProtection.Context} contentProtection
  1956. * @param {(string|undefined)} kind
  1957. * @param {string} language
  1958. * @param {?string} label
  1959. * @param {boolean} isPrimary
  1960. * @param {!Array<string>} roles
  1961. * @param {Map<string, string>} closedCaptions
  1962. * @param {!shaka.extern.xml.Node} node
  1963. * @param {?shaka.media.ManifestParser.AccessibilityPurpose
  1964. * } accessibilityPurpose
  1965. * @param {?number} lastSegmentNumber
  1966. *
  1967. * @return {?shaka.extern.Stream} The Stream, or null when there is a
  1968. * non-critical parsing error.
  1969. * @private
  1970. */
  1971. parseRepresentation_(context, contentProtection, kind, language, label,
  1972. isPrimary, roles, closedCaptions, node, accessibilityPurpose,
  1973. lastSegmentNumber) {
  1974. const TXml = shaka.util.TXml;
  1975. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  1976. context.representation =
  1977. this.createFrame_(node, context.adaptationSet, null);
  1978. const representationId = context.representation.id;
  1979. this.minTotalAvailabilityTimeOffset_ =
  1980. Math.min(this.minTotalAvailabilityTimeOffset_,
  1981. context.representation.availabilityTimeOffset);
  1982. this.isLowLatency_ = this.minTotalAvailabilityTimeOffset_ > 0;
  1983. if (!this.verifyRepresentation_(context.representation)) {
  1984. shaka.log.warning('Skipping Representation', context.representation);
  1985. return null;
  1986. }
  1987. const periodStart = context.periodInfo.start;
  1988. // NOTE: bandwidth is a mandatory attribute according to the spec, and zero
  1989. // does not make sense in the DASH spec's bandwidth formulas.
  1990. // In some content, however, the attribute is missing or zero.
  1991. // To avoid NaN at the variant level on broken content, fall back to zero.
  1992. // https://github.com/shaka-project/shaka-player/issues/938#issuecomment-317278180
  1993. context.bandwidth =
  1994. TXml.parseAttr(node, 'bandwidth', TXml.parsePositiveInt) || 0;
  1995. context.roles = roles;
  1996. const supplementalPropertyElements =
  1997. TXml.findChildren(node, 'SupplementalProperty');
  1998. const essentialPropertyElements =
  1999. TXml.findChildren(node, 'EssentialProperty');
  2000. const contentProtectionElements =
  2001. TXml.findChildren(node, 'ContentProtection');
  2002. let representationUrlParams = null;
  2003. let urlParamsElement = essentialPropertyElements.find((element) => {
  2004. const schemeId = element.attributes['schemeIdUri'];
  2005. return schemeId == 'urn:mpeg:dash:urlparam:2014';
  2006. });
  2007. if (urlParamsElement) {
  2008. representationUrlParams =
  2009. this.getURLParametersFunction_(urlParamsElement);
  2010. } else {
  2011. urlParamsElement = supplementalPropertyElements.find((element) => {
  2012. const schemeId = element.attributes['schemeIdUri'];
  2013. return schemeId == 'urn:mpeg:dash:urlparam:2014';
  2014. });
  2015. if (urlParamsElement) {
  2016. representationUrlParams =
  2017. this.getURLParametersFunction_(urlParamsElement);
  2018. }
  2019. }
  2020. if (representationUrlParams) {
  2021. context.urlParams = representationUrlParams;
  2022. }
  2023. /** @type {?shaka.dash.DashParser.StreamInfo} */
  2024. let streamInfo;
  2025. const contentType = context.representation.contentType;
  2026. const isText = contentType == ContentType.TEXT ||
  2027. contentType == ContentType.APPLICATION;
  2028. const isImage = contentType == ContentType.IMAGE;
  2029. if (contentProtectionElements.length) {
  2030. context.adaptationSet.encrypted = true;
  2031. }
  2032. try {
  2033. /** @type {shaka.extern.aesKey|undefined} */
  2034. let aesKey = undefined;
  2035. if (contentProtection.aes128Info) {
  2036. const getBaseUris = context.representation.getBaseUris;
  2037. const urlParams = context.urlParams;
  2038. const uris = shaka.util.ManifestParserUtils.resolveUris(
  2039. getBaseUris(), [contentProtection.aes128Info.keyUri], urlParams());
  2040. const requestType = shaka.net.NetworkingEngine.RequestType.KEY;
  2041. const request = shaka.net.NetworkingEngine.makeRequest(
  2042. uris, this.config_.retryParameters);
  2043. aesKey = {
  2044. bitsKey: 128,
  2045. blockCipherMode: 'CBC',
  2046. iv: contentProtection.aes128Info.iv,
  2047. firstMediaSequenceNumber: 0,
  2048. };
  2049. // Don't download the key object until the segment is parsed, to
  2050. // avoid a startup delay for long manifests with lots of keys.
  2051. aesKey.fetchKey = async () => {
  2052. const keyResponse =
  2053. await this.makeNetworkRequest_(request, requestType);
  2054. // keyResponse.status is undefined when URI is
  2055. // "data:text/plain;base64,"
  2056. if (!keyResponse.data || keyResponse.data.byteLength != 16) {
  2057. throw new shaka.util.Error(
  2058. shaka.util.Error.Severity.CRITICAL,
  2059. shaka.util.Error.Category.MANIFEST,
  2060. shaka.util.Error.Code.AES_128_INVALID_KEY_LENGTH);
  2061. }
  2062. const algorithm = {
  2063. name: 'AES-CBC',
  2064. };
  2065. aesKey.cryptoKey = await window.crypto.subtle.importKey(
  2066. 'raw', keyResponse.data, algorithm, true, ['decrypt']);
  2067. aesKey.fetchKey = undefined; // No longer needed.
  2068. };
  2069. }
  2070. context.representation.aesKey = aesKey;
  2071. const requestSegment = (uris, startByte, endByte, isInit) => {
  2072. return this.requestSegment_(uris, startByte, endByte, isInit);
  2073. };
  2074. if (context.representation.segmentBase) {
  2075. streamInfo = shaka.dash.SegmentBase.createStreamInfo(
  2076. context, requestSegment, aesKey);
  2077. } else if (context.representation.segmentList) {
  2078. streamInfo = shaka.dash.SegmentList.createStreamInfo(
  2079. context, this.streamMap_, aesKey);
  2080. } else if (context.representation.segmentTemplate) {
  2081. const hasManifest = !!this.manifest_;
  2082. streamInfo = shaka.dash.SegmentTemplate.createStreamInfo(
  2083. context, requestSegment, this.streamMap_, hasManifest,
  2084. this.config_.dash.initialSegmentLimit, this.periodDurations_,
  2085. aesKey, lastSegmentNumber, /* isPatchUpdate= */ false);
  2086. } else {
  2087. goog.asserts.assert(isText,
  2088. 'Must have Segment* with non-text streams.');
  2089. const duration = context.periodInfo.duration || 0;
  2090. const getBaseUris = context.representation.getBaseUris;
  2091. const mimeType = context.representation.mimeType;
  2092. const codecs = context.representation.codecs;
  2093. streamInfo = {
  2094. generateSegmentIndex: () => {
  2095. const segmentIndex = shaka.media.SegmentIndex.forSingleSegment(
  2096. periodStart, duration, getBaseUris());
  2097. segmentIndex.forEachTopLevelReference((ref) => {
  2098. ref.mimeType = mimeType;
  2099. ref.codecs = codecs;
  2100. });
  2101. return Promise.resolve(segmentIndex);
  2102. },
  2103. };
  2104. }
  2105. } catch (error) {
  2106. if ((isText || isImage) &&
  2107. error.code == shaka.util.Error.Code.DASH_NO_SEGMENT_INFO) {
  2108. // We will ignore any DASH_NO_SEGMENT_INFO errors for text/image
  2109. // streams.
  2110. return null;
  2111. }
  2112. // For anything else, re-throw.
  2113. throw error;
  2114. }
  2115. const keyId = shaka.dash.ContentProtection.parseFromRepresentation(
  2116. contentProtectionElements, contentProtection,
  2117. this.config_.ignoreDrmInfo,
  2118. this.config_.dash.keySystemsByURI);
  2119. const keyIds = new Set(keyId ? [keyId] : []);
  2120. // Detect the presence of E-AC3 JOC audio content, using DD+JOC signaling.
  2121. // See: ETSI TS 103 420 V1.2.1 (2018-10)
  2122. const hasJoc = supplementalPropertyElements.some((element) => {
  2123. const expectedUri = 'tag:dolby.com,2018:dash:EC3_ExtensionType:2018';
  2124. const expectedValue = 'JOC';
  2125. return element.attributes['schemeIdUri'] == expectedUri &&
  2126. element.attributes['value'] == expectedValue;
  2127. });
  2128. let spatialAudio = false;
  2129. if (hasJoc) {
  2130. spatialAudio = true;
  2131. }
  2132. let forced = false;
  2133. if (isText) {
  2134. // See: https://github.com/shaka-project/shaka-player/issues/2122 and
  2135. // https://github.com/Dash-Industry-Forum/DASH-IF-IOP/issues/165
  2136. forced = roles.includes('forced_subtitle') ||
  2137. roles.includes('forced-subtitle');
  2138. }
  2139. let tilesLayout;
  2140. if (isImage) {
  2141. const thumbnailTileElem = essentialPropertyElements.find((element) => {
  2142. const expectedUris = [
  2143. 'http://dashif.org/thumbnail_tile',
  2144. 'http://dashif.org/guidelines/thumbnail_tile',
  2145. ];
  2146. return expectedUris.includes(element.attributes['schemeIdUri']);
  2147. });
  2148. if (thumbnailTileElem) {
  2149. tilesLayout = thumbnailTileElem.attributes['value'];
  2150. }
  2151. // Filter image adaptation sets that has no tilesLayout.
  2152. if (!tilesLayout) {
  2153. return null;
  2154. }
  2155. }
  2156. let hdr;
  2157. const profiles = context.profiles;
  2158. const codecs = context.representation.codecs;
  2159. const hevcHDR = 'http://dashif.org/guidelines/dash-if-uhd#hevc-hdr-pq10';
  2160. if (profiles.includes(hevcHDR) && (codecs.includes('hvc1.2.4.L153.B0') ||
  2161. codecs.includes('hev1.2.4.L153.B0'))) {
  2162. hdr = 'PQ';
  2163. }
  2164. const contextId = context.representation.id ?
  2165. context.period.id + ',' + context.representation.id : '';
  2166. if (this.patchLocationNodes_.length && representationId) {
  2167. this.contextCache_.set(`${context.period.id},${representationId}`,
  2168. this.cloneContext_(context));
  2169. }
  2170. /** @type {shaka.extern.Stream} */
  2171. let stream;
  2172. if (contextId && this.streamMap_.has(contextId)) {
  2173. stream = this.streamMap_.get(contextId);
  2174. } else {
  2175. stream = {
  2176. id: this.globalId_++,
  2177. originalId: context.representation.id,
  2178. groupId: null,
  2179. createSegmentIndex: () => Promise.resolve(),
  2180. closeSegmentIndex: () => {
  2181. if (stream.segmentIndex) {
  2182. stream.segmentIndex.release();
  2183. stream.segmentIndex = null;
  2184. }
  2185. },
  2186. segmentIndex: null,
  2187. mimeType: context.representation.mimeType,
  2188. codecs,
  2189. frameRate: context.representation.frameRate,
  2190. pixelAspectRatio: context.representation.pixelAspectRatio,
  2191. bandwidth: context.bandwidth,
  2192. width: context.representation.width,
  2193. height: context.representation.height,
  2194. kind,
  2195. encrypted: contentProtection.drmInfos.length > 0,
  2196. drmInfos: contentProtection.drmInfos,
  2197. keyIds,
  2198. language,
  2199. originalLanguage: context.adaptationSet.language,
  2200. label,
  2201. type: context.adaptationSet.contentType,
  2202. primary: isPrimary,
  2203. trickModeVideo: null,
  2204. dependencyStream: null,
  2205. emsgSchemeIdUris:
  2206. context.representation.emsgSchemeIdUris,
  2207. roles,
  2208. forced,
  2209. channelsCount: context.representation.numChannels,
  2210. audioSamplingRate: context.representation.audioSamplingRate,
  2211. spatialAudio,
  2212. closedCaptions,
  2213. hdr,
  2214. colorGamut: undefined,
  2215. videoLayout: undefined,
  2216. tilesLayout,
  2217. accessibilityPurpose,
  2218. external: false,
  2219. fastSwitching: false,
  2220. fullMimeTypes: new Set([shaka.util.MimeUtils.getFullType(
  2221. context.representation.mimeType, context.representation.codecs)]),
  2222. isAudioMuxedInVideo: false,
  2223. baseOriginalId: null,
  2224. };
  2225. }
  2226. stream.createSegmentIndex = async () => {
  2227. if (!stream.segmentIndex) {
  2228. stream.segmentIndex = await streamInfo.generateSegmentIndex();
  2229. }
  2230. };
  2231. if (contextId && context.dynamic && !this.streamMap_.has(contextId)) {
  2232. const periodId = context.period.id || '';
  2233. if (!this.indexStreamMap_.has(periodId)) {
  2234. this.indexStreamMap_.set(periodId, []);
  2235. }
  2236. this.streamMap_.set(contextId, stream);
  2237. this.indexStreamMap_.get(periodId).push(contextId);
  2238. }
  2239. return stream;
  2240. }
  2241. /**
  2242. * Clone context and remove xml document references.
  2243. *
  2244. * @param {!shaka.dash.DashParser.Context} context
  2245. * @return {!shaka.dash.DashParser.Context}
  2246. * @private
  2247. */
  2248. cloneContext_(context) {
  2249. /**
  2250. * @param {?shaka.dash.DashParser.InheritanceFrame} frame
  2251. * @return {?shaka.dash.DashParser.InheritanceFrame}
  2252. */
  2253. const cloneFrame = (frame) => {
  2254. if (!frame) {
  2255. return null;
  2256. }
  2257. const clone = shaka.util.ObjectUtils.shallowCloneObject(frame);
  2258. clone.segmentBase = null;
  2259. clone.segmentList = null;
  2260. clone.segmentTemplate = shaka.util.TXml.cloneNode(clone.segmentTemplate);
  2261. return clone;
  2262. };
  2263. const contextClone = shaka.util.ObjectUtils.shallowCloneObject(context);
  2264. contextClone.period = cloneFrame(contextClone.period);
  2265. contextClone.adaptationSet = cloneFrame(contextClone.adaptationSet);
  2266. contextClone.representation = cloneFrame(contextClone.representation);
  2267. if (contextClone.periodInfo) {
  2268. contextClone.periodInfo =
  2269. shaka.util.ObjectUtils.shallowCloneObject(contextClone.periodInfo);
  2270. contextClone.periodInfo.node = null;
  2271. }
  2272. return contextClone;
  2273. }
  2274. /**
  2275. * Called when the update timer ticks.
  2276. *
  2277. * @return {!Promise}
  2278. * @private
  2279. */
  2280. async onUpdate_() {
  2281. goog.asserts.assert(this.updatePeriod_ >= 0,
  2282. 'There should be an update period');
  2283. shaka.log.info('Updating manifest...');
  2284. // Default the update delay to 0 seconds so that if there is an error we can
  2285. // try again right away.
  2286. let updateDelay = 0;
  2287. try {
  2288. updateDelay = await this.requestManifest_();
  2289. } catch (error) {
  2290. goog.asserts.assert(error instanceof shaka.util.Error,
  2291. 'Should only receive a Shaka error');
  2292. // Try updating again, but ensure we haven't been destroyed.
  2293. if (this.playerInterface_) {
  2294. if (this.config_.raiseFatalErrorOnManifestUpdateRequestFailure) {
  2295. this.playerInterface_.onError(error);
  2296. return;
  2297. }
  2298. // We will retry updating, so override the severity of the error.
  2299. error.severity = shaka.util.Error.Severity.RECOVERABLE;
  2300. this.playerInterface_.onError(error);
  2301. }
  2302. }
  2303. // Detect a call to stop()
  2304. if (!this.playerInterface_) {
  2305. return;
  2306. }
  2307. this.playerInterface_.onManifestUpdated();
  2308. this.setUpdateTimer_(updateDelay);
  2309. }
  2310. /**
  2311. * Update now the manifest
  2312. *
  2313. * @private
  2314. */
  2315. updateNow_() {
  2316. this.updateTimer_.tickNow();
  2317. }
  2318. /**
  2319. * Sets the update timer. Does nothing if the manifest does not specify an
  2320. * update period.
  2321. *
  2322. * @param {number} offset An offset, in seconds, to apply to the manifest's
  2323. * update period.
  2324. * @private
  2325. */
  2326. setUpdateTimer_(offset) {
  2327. // NOTE: An updatePeriod_ of -1 means the attribute was missing.
  2328. // An attribute which is present and set to 0 should still result in
  2329. // periodic updates. For more, see:
  2330. // https://github.com/Dash-Industry-Forum/Guidelines-TimingModel/issues/48
  2331. if (this.updatePeriod_ < 0) {
  2332. return;
  2333. }
  2334. let updateTime = this.updatePeriod_;
  2335. if (this.config_.updatePeriod >= 0) {
  2336. updateTime = this.config_.updatePeriod;
  2337. }
  2338. const finalDelay = Math.max(
  2339. updateTime - offset,
  2340. this.averageUpdateDuration_.getEstimate());
  2341. // We do not run the timer as repeating because part of update is async and
  2342. // we need schedule the update after it finished.
  2343. this.updateTimer_.tickAfter(/* seconds= */ finalDelay);
  2344. }
  2345. /**
  2346. * Creates a new inheritance frame for the given element.
  2347. *
  2348. * @param {!shaka.extern.xml.Node} elem
  2349. * @param {?shaka.dash.DashParser.InheritanceFrame} parent
  2350. * @param {?function(): !Array<string>} getBaseUris
  2351. * @return {shaka.dash.DashParser.InheritanceFrame}
  2352. * @private
  2353. */
  2354. createFrame_(elem, parent, getBaseUris) {
  2355. goog.asserts.assert(parent || getBaseUris,
  2356. 'Must provide either parent or getBaseUris');
  2357. const SegmentUtils = shaka.media.SegmentUtils;
  2358. const ManifestParserUtils = shaka.util.ManifestParserUtils;
  2359. const TXml = shaka.util.TXml;
  2360. parent = parent || /** @type {shaka.dash.DashParser.InheritanceFrame} */ ({
  2361. contentType: '',
  2362. mimeType: '',
  2363. codecs: '',
  2364. emsgSchemeIdUris: [],
  2365. frameRate: undefined,
  2366. pixelAspectRatio: undefined,
  2367. numChannels: null,
  2368. audioSamplingRate: null,
  2369. availabilityTimeOffset: 0,
  2370. segmentSequenceCadence: 0,
  2371. encrypted: false,
  2372. });
  2373. getBaseUris = getBaseUris || parent.getBaseUris;
  2374. const parseNumber = TXml.parseNonNegativeInt;
  2375. const evalDivision = TXml.evalDivision;
  2376. const id = elem.attributes['id'];
  2377. const supplementalId = elem.attributes['supplementalId'];
  2378. const uriObjs = TXml.findChildren(elem, 'BaseURL');
  2379. let calculatedBaseUris;
  2380. let someLocationValid = false;
  2381. if (this.contentSteeringManager_) {
  2382. for (const uriObj of uriObjs) {
  2383. const serviceLocation = uriObj.attributes['serviceLocation'];
  2384. const uri = TXml.getContents(uriObj);
  2385. if (serviceLocation && uri) {
  2386. this.contentSteeringManager_.addLocation(
  2387. id, serviceLocation, uri);
  2388. someLocationValid = true;
  2389. }
  2390. }
  2391. }
  2392. if (!someLocationValid || !this.contentSteeringManager_) {
  2393. calculatedBaseUris = uriObjs.map(TXml.getContents);
  2394. }
  2395. const getFrameUris = () => {
  2396. if (!uriObjs.length) {
  2397. return [];
  2398. }
  2399. if (this.contentSteeringManager_ && someLocationValid) {
  2400. return this.contentSteeringManager_.getLocations(id);
  2401. }
  2402. if (calculatedBaseUris) {
  2403. return calculatedBaseUris;
  2404. }
  2405. return [];
  2406. };
  2407. let contentType = elem.attributes['contentType'] || parent.contentType;
  2408. const mimeType = elem.attributes['mimeType'] || parent.mimeType;
  2409. const allCodecs = [
  2410. elem.attributes['codecs'] || parent.codecs,
  2411. ];
  2412. const codecs = SegmentUtils.codecsFiltering(allCodecs).join(',');
  2413. const frameRate =
  2414. TXml.parseAttr(elem, 'frameRate', evalDivision) || parent.frameRate;
  2415. const pixelAspectRatio =
  2416. elem.attributes['sar'] || parent.pixelAspectRatio;
  2417. const emsgSchemeIdUris = this.emsgSchemeIdUris_(
  2418. TXml.findChildren(elem, 'InbandEventStream'),
  2419. parent.emsgSchemeIdUris);
  2420. const audioChannelConfigs =
  2421. TXml.findChildren(elem, 'AudioChannelConfiguration');
  2422. const numChannels =
  2423. this.parseAudioChannels_(audioChannelConfigs) || parent.numChannels;
  2424. const audioSamplingRate =
  2425. TXml.parseAttr(elem, 'audioSamplingRate', parseNumber) ||
  2426. parent.audioSamplingRate;
  2427. if (!contentType) {
  2428. contentType = shaka.dash.DashParser.guessContentType_(mimeType, codecs);
  2429. }
  2430. const segmentBase = TXml.findChild(elem, 'SegmentBase');
  2431. const segmentTemplate = TXml.findChild(elem, 'SegmentTemplate');
  2432. // The availabilityTimeOffset is the sum of all @availabilityTimeOffset
  2433. // values that apply to the adaptation set, via BaseURL, SegmentBase,
  2434. // or SegmentTemplate elements.
  2435. const segmentBaseAto = segmentBase ?
  2436. (TXml.parseAttr(segmentBase, 'availabilityTimeOffset',
  2437. TXml.parseFloat) || 0) : 0;
  2438. const segmentTemplateAto = segmentTemplate ?
  2439. (TXml.parseAttr(segmentTemplate, 'availabilityTimeOffset',
  2440. TXml.parseFloat) || 0) : 0;
  2441. const baseUriAto = uriObjs && uriObjs.length ?
  2442. (TXml.parseAttr(uriObjs[0], 'availabilityTimeOffset',
  2443. TXml.parseFloat) || 0) : 0;
  2444. const availabilityTimeOffset = parent.availabilityTimeOffset + baseUriAto +
  2445. segmentBaseAto + segmentTemplateAto;
  2446. let segmentSequenceCadence = null;
  2447. const segmentSequenceProperties =
  2448. TXml.findChild(elem, 'SegmentSequenceProperties');
  2449. if (segmentSequenceProperties) {
  2450. const sap = TXml.findChild(segmentSequenceProperties, 'SAP');
  2451. if (sap) {
  2452. segmentSequenceCadence = TXml.parseAttr(sap, 'cadence',
  2453. TXml.parseInt);
  2454. }
  2455. }
  2456. // This attribute is currently non-standard, but it is supported by Kaltura.
  2457. let label = elem.attributes['label'];
  2458. // See DASH IOP 4.3 here https://dashif.org/docs/DASH-IF-IOP-v4.3.pdf (page 35)
  2459. const labelElements = TXml.findChildren(elem, 'Label');
  2460. if (labelElements && labelElements.length) {
  2461. // NOTE: Right now only one label field is supported.
  2462. const firstLabelElement = labelElements[0];
  2463. if (TXml.getTextContents(firstLabelElement)) {
  2464. label = TXml.getTextContents(firstLabelElement);
  2465. }
  2466. }
  2467. return {
  2468. getBaseUris:
  2469. () => ManifestParserUtils.resolveUris(getBaseUris(), getFrameUris()),
  2470. segmentBase: segmentBase || parent.segmentBase,
  2471. segmentList:
  2472. TXml.findChild(elem, 'SegmentList') || parent.segmentList,
  2473. segmentTemplate: segmentTemplate || parent.segmentTemplate,
  2474. width: TXml.parseAttr(elem, 'width', parseNumber) || parent.width,
  2475. height: TXml.parseAttr(elem, 'height', parseNumber) || parent.height,
  2476. contentType: contentType,
  2477. mimeType: mimeType,
  2478. codecs: codecs,
  2479. frameRate: frameRate,
  2480. pixelAspectRatio: pixelAspectRatio,
  2481. emsgSchemeIdUris: emsgSchemeIdUris,
  2482. id: supplementalId || id,
  2483. originalId: id,
  2484. language: elem.attributes['lang'],
  2485. numChannels: numChannels,
  2486. audioSamplingRate: audioSamplingRate,
  2487. availabilityTimeOffset: availabilityTimeOffset,
  2488. initialization: null,
  2489. segmentSequenceCadence:
  2490. segmentSequenceCadence || parent.segmentSequenceCadence,
  2491. label: label || null,
  2492. encrypted: false,
  2493. };
  2494. }
  2495. /**
  2496. * Returns a new array of InbandEventStream schemeIdUri containing the union
  2497. * of the ones parsed from inBandEventStreams and the ones provided in
  2498. * emsgSchemeIdUris.
  2499. *
  2500. * @param {!Array<!shaka.extern.xml.Node>} inBandEventStreams
  2501. * Array of InbandEventStream
  2502. * elements to parse and add to the returned array.
  2503. * @param {!Array<string>} emsgSchemeIdUris Array of parsed
  2504. * InbandEventStream schemeIdUri attributes to add to the returned array.
  2505. * @return {!Array<string>} schemeIdUris Array of parsed
  2506. * InbandEventStream schemeIdUri attributes.
  2507. * @private
  2508. */
  2509. emsgSchemeIdUris_(inBandEventStreams, emsgSchemeIdUris) {
  2510. const schemeIdUris = emsgSchemeIdUris.slice();
  2511. for (const event of inBandEventStreams) {
  2512. const schemeIdUri = event.attributes['schemeIdUri'];
  2513. if (!schemeIdUris.includes(schemeIdUri)) {
  2514. schemeIdUris.push(schemeIdUri);
  2515. }
  2516. }
  2517. return schemeIdUris;
  2518. }
  2519. /**
  2520. * @param {!Array<!shaka.extern.xml.Node>} audioChannelConfigs An array of
  2521. * AudioChannelConfiguration elements.
  2522. * @return {?number} The number of audio channels, or null if unknown.
  2523. * @private
  2524. */
  2525. parseAudioChannels_(audioChannelConfigs) {
  2526. for (const elem of audioChannelConfigs) {
  2527. const scheme = elem.attributes['schemeIdUri'];
  2528. if (!scheme) {
  2529. continue;
  2530. }
  2531. const value = elem.attributes['value'];
  2532. if (!value) {
  2533. continue;
  2534. }
  2535. switch (scheme) {
  2536. case 'urn:mpeg:dash:outputChannelPositionList:2012':
  2537. // A space-separated list of speaker positions, so the number of
  2538. // channels is the length of this list.
  2539. return value.trim().split(/ +/).length;
  2540. case 'urn:mpeg:dash:23003:3:audio_channel_configuration:2011':
  2541. case 'urn:dts:dash:audio_channel_configuration:2012': {
  2542. // As far as we can tell, this is a number of channels.
  2543. const intValue = parseInt(value, 10);
  2544. if (!intValue) { // 0 or NaN
  2545. shaka.log.warning('Channel parsing failure! ' +
  2546. 'Ignoring scheme and value', scheme, value);
  2547. continue;
  2548. }
  2549. return intValue;
  2550. }
  2551. case 'tag:dolby.com,2015:dash:audio_channel_configuration:2015': {
  2552. // ETSI TS 103 190-2 v1.2.1, Annex G.3
  2553. // LSB-to-MSB order
  2554. const channelCountMapping =
  2555. [2, 1, 2, 2, 2, 2, 1, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2];
  2556. const hexValue = parseInt(value, 16);
  2557. if (!hexValue) { // 0 or NaN
  2558. shaka.log.warning('Channel parsing failure! ' +
  2559. 'Ignoring scheme and value', scheme, value);
  2560. continue;
  2561. }
  2562. let numBits = 0;
  2563. for (let i = 0; i < channelCountMapping.length; i++) {
  2564. if (hexValue & (1<<i)) {
  2565. numBits += channelCountMapping[i];
  2566. }
  2567. }
  2568. if (numBits) {
  2569. return numBits;
  2570. }
  2571. continue;
  2572. }
  2573. case 'tag:dolby.com,2014:dash:audio_channel_configuration:2011':
  2574. case 'urn:dolby:dash:audio_channel_configuration:2011': {
  2575. // Defined by https://ott.dolby.com/OnDelKits/DDP/Dolby_Digital_Plus_Online_Delivery_Kit_v1.5/Documentation/Content_Creation/SDM/help_files/topics/ddp_mpeg_dash_c_mpd_auchlconfig.html
  2576. // keep list in order of the spec; reverse for LSB-to-MSB order
  2577. const channelCountMapping =
  2578. [1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 1, 1].reverse();
  2579. const hexValue = parseInt(value, 16);
  2580. if (!hexValue) { // 0 or NaN
  2581. shaka.log.warning('Channel parsing failure! ' +
  2582. 'Ignoring scheme and value', scheme, value);
  2583. continue;
  2584. }
  2585. let numBits = 0;
  2586. for (let i = 0; i < channelCountMapping.length; i++) {
  2587. if (hexValue & (1<<i)) {
  2588. numBits += channelCountMapping[i];
  2589. }
  2590. }
  2591. if (numBits) {
  2592. return numBits;
  2593. }
  2594. continue;
  2595. }
  2596. // Defined by https://dashif.org/identifiers/audio_source_metadata/ and clause 8.2, in ISO/IEC 23001-8.
  2597. case 'urn:mpeg:mpegB:cicp:ChannelConfiguration': {
  2598. const noValue = 0;
  2599. const channelCountMapping = [
  2600. noValue, 1, 2, 3, 4, 5, 6, 8, 2, 3, /* 0--9 */
  2601. 4, 7, 8, 24, 8, 12, 10, 12, 14, 12, /* 10--19 */
  2602. 14, /* 20 */
  2603. ];
  2604. const intValue = parseInt(value, 10);
  2605. if (!intValue) { // 0 or NaN
  2606. shaka.log.warning('Channel parsing failure! ' +
  2607. 'Ignoring scheme and value', scheme, value);
  2608. continue;
  2609. }
  2610. if (intValue > noValue && intValue < channelCountMapping.length) {
  2611. return channelCountMapping[intValue];
  2612. }
  2613. continue;
  2614. }
  2615. default:
  2616. shaka.log.warning(
  2617. 'Unrecognized audio channel scheme:', scheme, value);
  2618. continue;
  2619. }
  2620. }
  2621. return null;
  2622. }
  2623. /**
  2624. * Verifies that a Representation has exactly one Segment* element. Prints
  2625. * warnings if there is a problem.
  2626. *
  2627. * @param {shaka.dash.DashParser.InheritanceFrame} frame
  2628. * @return {boolean} True if the Representation is usable; otherwise return
  2629. * false.
  2630. * @private
  2631. */
  2632. verifyRepresentation_(frame) {
  2633. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  2634. let n = 0;
  2635. n += frame.segmentBase ? 1 : 0;
  2636. n += frame.segmentList ? 1 : 0;
  2637. n += frame.segmentTemplate ? 1 : 0;
  2638. if (n == 0) {
  2639. // TODO: Extend with the list of MIME types registered to TextEngine.
  2640. if (frame.contentType == ContentType.TEXT ||
  2641. frame.contentType == ContentType.APPLICATION) {
  2642. return true;
  2643. } else {
  2644. shaka.log.warning(
  2645. 'Representation does not contain a segment information source:',
  2646. 'the Representation must contain one of SegmentBase, SegmentList,',
  2647. 'SegmentTemplate, or explicitly indicate that it is "text".',
  2648. frame);
  2649. return false;
  2650. }
  2651. }
  2652. if (n != 1) {
  2653. shaka.log.warning(
  2654. 'Representation contains multiple segment information sources:',
  2655. 'the Representation should only contain one of SegmentBase,',
  2656. 'SegmentList, or SegmentTemplate.',
  2657. frame);
  2658. if (frame.segmentBase) {
  2659. shaka.log.info('Using SegmentBase by default.');
  2660. frame.segmentList = null;
  2661. frame.segmentTemplate = null;
  2662. } else {
  2663. goog.asserts.assert(frame.segmentList, 'There should be a SegmentList');
  2664. shaka.log.info('Using SegmentList by default.');
  2665. frame.segmentTemplate = null;
  2666. }
  2667. }
  2668. return true;
  2669. }
  2670. /**
  2671. * Makes a request to the given URI and calculates the clock offset.
  2672. *
  2673. * @param {function(): !Array<string>} getBaseUris
  2674. * @param {string} uri
  2675. * @param {string} method
  2676. * @return {!Promise<number>}
  2677. * @private
  2678. */
  2679. async requestForTiming_(getBaseUris, uri, method) {
  2680. const uris = [shaka.util.StringUtils.htmlUnescape(uri)];
  2681. const requestUris =
  2682. shaka.util.ManifestParserUtils.resolveUris(getBaseUris(), uris);
  2683. const request = shaka.net.NetworkingEngine.makeRequest(
  2684. requestUris, this.config_.retryParameters);
  2685. request.method = method;
  2686. const type = shaka.net.NetworkingEngine.RequestType.TIMING;
  2687. const operation =
  2688. this.playerInterface_.networkingEngine.request(
  2689. type, request, {isPreload: this.isPreloadFn_()});
  2690. this.operationManager_.manage(operation);
  2691. const response = await operation.promise;
  2692. let text;
  2693. if (method == 'HEAD') {
  2694. if (!response.headers || !response.headers['date']) {
  2695. shaka.log.warning('UTC timing response is missing',
  2696. 'expected date header');
  2697. return 0;
  2698. }
  2699. text = response.headers['date'];
  2700. } else {
  2701. text = shaka.util.StringUtils.fromUTF8(response.data);
  2702. }
  2703. const date = Date.parse(text);
  2704. if (isNaN(date)) {
  2705. shaka.log.warning('Unable to parse date from UTC timing response');
  2706. return 0;
  2707. }
  2708. return (date - Date.now());
  2709. }
  2710. /**
  2711. * Parses an array of UTCTiming elements.
  2712. *
  2713. * @param {function(): !Array<string>} getBaseUris
  2714. * @param {!Array<!shaka.extern.xml.Node>} elements
  2715. * @return {!Promise<number>}
  2716. * @private
  2717. */
  2718. async parseUtcTiming_(getBaseUris, elements) {
  2719. const schemesAndValues = elements.map((elem) => {
  2720. return {
  2721. scheme: elem.attributes['schemeIdUri'],
  2722. value: elem.attributes['value'],
  2723. };
  2724. });
  2725. // If there's nothing specified in the manifest, but we have a default from
  2726. // the config, use that.
  2727. const clockSyncUri = this.config_.dash.clockSyncUri;
  2728. if (!schemesAndValues.length && clockSyncUri) {
  2729. schemesAndValues.push({
  2730. scheme: 'urn:mpeg:dash:utc:http-head:2014',
  2731. value: clockSyncUri,
  2732. });
  2733. }
  2734. for (const sv of schemesAndValues) {
  2735. try {
  2736. const scheme = sv.scheme;
  2737. const value = sv.value;
  2738. switch (scheme) {
  2739. // See DASH IOP Guidelines Section 4.7
  2740. // https://bit.ly/DashIop3-2
  2741. // Some old ISO23009-1 drafts used 2012.
  2742. case 'urn:mpeg:dash:utc:http-head:2014':
  2743. case 'urn:mpeg:dash:utc:http-head:2012':
  2744. // eslint-disable-next-line no-await-in-loop
  2745. return await this.requestForTiming_(getBaseUris, value, 'HEAD');
  2746. case 'urn:mpeg:dash:utc:http-xsdate:2014':
  2747. case 'urn:mpeg:dash:utc:http-iso:2014':
  2748. case 'urn:mpeg:dash:utc:http-xsdate:2012':
  2749. case 'urn:mpeg:dash:utc:http-iso:2012':
  2750. // eslint-disable-next-line no-await-in-loop
  2751. return await this.requestForTiming_(getBaseUris, value, 'GET');
  2752. case 'urn:mpeg:dash:utc:direct:2014':
  2753. case 'urn:mpeg:dash:utc:direct:2012': {
  2754. const date = Date.parse(value);
  2755. return isNaN(date) ? 0 : (date - Date.now());
  2756. }
  2757. case 'urn:mpeg:dash:utc:http-ntp:2014':
  2758. case 'urn:mpeg:dash:utc:ntp:2014':
  2759. case 'urn:mpeg:dash:utc:sntp:2014':
  2760. shaka.log.alwaysWarn('NTP UTCTiming scheme is not supported');
  2761. break;
  2762. default:
  2763. shaka.log.alwaysWarn(
  2764. 'Unrecognized scheme in UTCTiming element', scheme);
  2765. break;
  2766. }
  2767. } catch (e) {
  2768. shaka.log.warning('Error fetching time from UTCTiming elem', e.message);
  2769. }
  2770. }
  2771. shaka.log.alwaysWarn(
  2772. 'A UTCTiming element should always be given in live manifests! ' +
  2773. 'This content may not play on clients with bad clocks!');
  2774. return 0;
  2775. }
  2776. /**
  2777. * Parses an EventStream element.
  2778. *
  2779. * @param {number} periodStart
  2780. * @param {?number} periodDuration
  2781. * @param {!shaka.extern.xml.Node} elem
  2782. * @param {number} availabilityStart
  2783. * @private
  2784. */
  2785. parseEventStream_(periodStart, periodDuration, elem, availabilityStart) {
  2786. const TXml = shaka.util.TXml;
  2787. const parseNumber = shaka.util.TXml.parseNonNegativeInt;
  2788. const schemeIdUri = elem.attributes['schemeIdUri'] || '';
  2789. const value = elem.attributes['value'] || '';
  2790. const timescale = TXml.parseAttr(elem, 'timescale', parseNumber) || 1;
  2791. const presentationTimeOffset =
  2792. TXml.parseAttr(elem, 'presentationTimeOffset', parseNumber) || 0;
  2793. for (const eventNode of TXml.findChildren(elem, 'Event')) {
  2794. const presentationTime =
  2795. TXml.parseAttr(eventNode, 'presentationTime', parseNumber) || 0;
  2796. const duration =
  2797. TXml.parseAttr(eventNode, 'duration', parseNumber) || 0;
  2798. // Ensure start time won't be lower than period start.
  2799. let startTime = Math.max(
  2800. (presentationTime - presentationTimeOffset) / timescale + periodStart,
  2801. periodStart);
  2802. let endTime = startTime + (duration / timescale);
  2803. if (periodDuration != null) {
  2804. // An event should not go past the Period, even if the manifest says so.
  2805. // See: Dash sec. 5.10.2.1
  2806. startTime = Math.min(startTime, periodStart + periodDuration);
  2807. endTime = Math.min(endTime, periodStart + periodDuration);
  2808. }
  2809. // Don't add unavailable regions to the timeline.
  2810. if (endTime < availabilityStart) {
  2811. continue;
  2812. }
  2813. /** @type {shaka.extern.TimelineRegionInfo} */
  2814. const region = {
  2815. schemeIdUri: schemeIdUri,
  2816. value: value,
  2817. startTime: startTime,
  2818. endTime: endTime,
  2819. id: eventNode.attributes['id'] || '',
  2820. timescale: timescale,
  2821. eventElement: TXml.txmlNodeToDomElement(eventNode),
  2822. eventNode: TXml.cloneNode(eventNode),
  2823. };
  2824. this.playerInterface_.onTimelineRegionAdded(region);
  2825. }
  2826. }
  2827. /**
  2828. * Makes a network request on behalf of SegmentBase.createStreamInfo.
  2829. *
  2830. * @param {!Array<string>} uris
  2831. * @param {?number} startByte
  2832. * @param {?number} endByte
  2833. * @param {boolean} isInit
  2834. * @return {!Promise<BufferSource>}
  2835. * @private
  2836. */
  2837. async requestSegment_(uris, startByte, endByte, isInit) {
  2838. const requestType = shaka.net.NetworkingEngine.RequestType.SEGMENT;
  2839. const type = isInit ?
  2840. shaka.net.NetworkingEngine.AdvancedRequestType.INIT_SEGMENT :
  2841. shaka.net.NetworkingEngine.AdvancedRequestType.MEDIA_SEGMENT;
  2842. const request = shaka.util.Networking.createSegmentRequest(
  2843. uris,
  2844. startByte,
  2845. endByte,
  2846. this.config_.retryParameters);
  2847. const response = await this.makeNetworkRequest_(
  2848. request, requestType, {type});
  2849. return response.data;
  2850. }
  2851. /**
  2852. * Guess the content type based on MIME type and codecs.
  2853. *
  2854. * @param {string} mimeType
  2855. * @param {string} codecs
  2856. * @return {string}
  2857. * @private
  2858. */
  2859. static guessContentType_(mimeType, codecs) {
  2860. const fullMimeType = shaka.util.MimeUtils.getFullType(mimeType, codecs);
  2861. if (shaka.text.TextEngine.isTypeSupported(fullMimeType)) {
  2862. // If it's supported by TextEngine, it's definitely text.
  2863. // We don't check MediaSourceEngine, because that would report support
  2864. // for platform-supported video and audio types as well.
  2865. return shaka.util.ManifestParserUtils.ContentType.TEXT;
  2866. }
  2867. // Otherwise, just split the MIME type. This handles video and audio
  2868. // types well.
  2869. return mimeType.split('/')[0];
  2870. }
  2871. /**
  2872. * Create a networking request. This will manage the request using the
  2873. * parser's operation manager.
  2874. *
  2875. * @param {shaka.extern.Request} request
  2876. * @param {shaka.net.NetworkingEngine.RequestType} type
  2877. * @param {shaka.extern.RequestContext=} context
  2878. * @return {!Promise<shaka.extern.Response>}
  2879. * @private
  2880. */
  2881. makeNetworkRequest_(request, type, context) {
  2882. if (!context) {
  2883. context = {};
  2884. }
  2885. context.isPreload = this.isPreloadFn_();
  2886. const op = this.playerInterface_.networkingEngine.request(
  2887. type, request, context);
  2888. this.operationManager_.manage(op);
  2889. return op.promise;
  2890. }
  2891. /**
  2892. * @param {!shaka.extern.xml.Node} patchNode
  2893. * @private
  2894. */
  2895. updatePatchLocationNodes_(patchNode) {
  2896. const TXml = shaka.util.TXml;
  2897. TXml.modifyNodes(this.patchLocationNodes_, patchNode);
  2898. }
  2899. /**
  2900. * @return {!Array<string>}
  2901. * @private
  2902. */
  2903. getPatchLocationUris_() {
  2904. const TXml = shaka.util.TXml;
  2905. const mpdId = this.manifestPatchContext_.mpdId;
  2906. const publishTime = this.manifestPatchContext_.publishTime;
  2907. if (!mpdId || !publishTime || !this.patchLocationNodes_.length) {
  2908. return [];
  2909. }
  2910. const now = Date.now() / 1000;
  2911. const patchLocations = this.patchLocationNodes_.filter((patchLocation) => {
  2912. const ttl = TXml.parseNonNegativeInt(patchLocation.attributes['ttl']);
  2913. return !ttl || publishTime + ttl > now;
  2914. })
  2915. .map(TXml.getContents)
  2916. .filter(shaka.util.Functional.isNotNull);
  2917. if (!patchLocations.length) {
  2918. return [];
  2919. }
  2920. return shaka.util.ManifestParserUtils.resolveUris(
  2921. this.manifestUris_, patchLocations);
  2922. }
  2923. };
  2924. /**
  2925. * @typedef {{
  2926. * mpdId: string,
  2927. * type: string,
  2928. * mediaPresentationDuration: ?number,
  2929. * profiles: !Array<string>,
  2930. * availabilityTimeOffset: number,
  2931. * getBaseUris: ?function():!Array<string>,
  2932. * publishTime: number
  2933. * }}
  2934. *
  2935. * @property {string} mpdId
  2936. * ID of the original MPD file.
  2937. * @property {string} type
  2938. * Specifies the type of the dash manifest i.e. "static"
  2939. * @property {?number} mediaPresentationDuration
  2940. * Media presentation duration, or null if unknown.
  2941. * @property {!Array<string>} profiles
  2942. * Profiles of DASH are defined to enable interoperability and the
  2943. * signaling of the use of features.
  2944. * @property {number} availabilityTimeOffset
  2945. * Specifies the total availabilityTimeOffset of the segment.
  2946. * @property {?function():!Array<string>} getBaseUris
  2947. * An array of absolute base URIs.
  2948. * @property {number} publishTime
  2949. * Time when manifest has been published, in seconds.
  2950. */
  2951. shaka.dash.DashParser.PatchContext;
  2952. /**
  2953. * @const {string}
  2954. * @private
  2955. */
  2956. shaka.dash.DashParser.SCTE214_ = 'urn:scte:dash:scte214-extensions';
  2957. /**
  2958. * @const {string}
  2959. * @private
  2960. */
  2961. shaka.dash.DashParser.UP_NAMESPACE_ = 'urn:mpeg:dash:schema:urlparam:2014';
  2962. /**
  2963. * @typedef {
  2964. * function(!Array<string>, ?number, ?number, boolean):
  2965. * !Promise<BufferSource>
  2966. * }
  2967. */
  2968. shaka.dash.DashParser.RequestSegmentCallback;
  2969. /**
  2970. * @typedef {{
  2971. * segmentBase: ?shaka.extern.xml.Node,
  2972. * segmentList: ?shaka.extern.xml.Node,
  2973. * segmentTemplate: ?shaka.extern.xml.Node,
  2974. * getBaseUris: function():!Array<string>,
  2975. * width: (number|undefined),
  2976. * height: (number|undefined),
  2977. * contentType: string,
  2978. * mimeType: string,
  2979. * codecs: string,
  2980. * frameRate: (number|undefined),
  2981. * pixelAspectRatio: (string|undefined),
  2982. * emsgSchemeIdUris: !Array<string>,
  2983. * id: ?string,
  2984. * originalId: ?string,
  2985. * position: (number|undefined),
  2986. * language: ?string,
  2987. * numChannels: ?number,
  2988. * audioSamplingRate: ?number,
  2989. * availabilityTimeOffset: number,
  2990. * initialization: ?string,
  2991. * aesKey: (shaka.extern.aesKey|undefined),
  2992. * segmentSequenceCadence: number,
  2993. * label: ?string,
  2994. * encrypted: boolean
  2995. * }}
  2996. *
  2997. * @description
  2998. * A collection of elements and properties which are inherited across levels
  2999. * of a DASH manifest.
  3000. *
  3001. * @property {?shaka.extern.xml.Node} segmentBase
  3002. * The XML node for SegmentBase.
  3003. * @property {?shaka.extern.xml.Node} segmentList
  3004. * The XML node for SegmentList.
  3005. * @property {?shaka.extern.xml.Node} segmentTemplate
  3006. * The XML node for SegmentTemplate.
  3007. * @property {function():!Array<string>} getBaseUris
  3008. * Function than returns an array of absolute base URIs for the frame.
  3009. * @property {(number|undefined)} width
  3010. * The inherited width value.
  3011. * @property {(number|undefined)} height
  3012. * The inherited height value.
  3013. * @property {string} contentType
  3014. * The inherited media type.
  3015. * @property {string} mimeType
  3016. * The inherited MIME type value.
  3017. * @property {string} codecs
  3018. * The inherited codecs value.
  3019. * @property {(number|undefined)} frameRate
  3020. * The inherited framerate value.
  3021. * @property {(string|undefined)} pixelAspectRatio
  3022. * The inherited pixel aspect ratio value.
  3023. * @property {!Array<string>} emsgSchemeIdUris
  3024. * emsg registered schemeIdUris.
  3025. * @property {?string} id
  3026. * The ID of the element.
  3027. * @property {?string} originalId
  3028. * The original ID of the element.
  3029. * @property {number|undefined} position
  3030. * Position of the element used for indexing in case of no id
  3031. * @property {?string} language
  3032. * The original language of the element.
  3033. * @property {?number} numChannels
  3034. * The number of audio channels, or null if unknown.
  3035. * @property {?number} audioSamplingRate
  3036. * Specifies the maximum sampling rate of the content, or null if unknown.
  3037. * @property {number} availabilityTimeOffset
  3038. * Specifies the total availabilityTimeOffset of the segment, or 0 if unknown.
  3039. * @property {?string} initialization
  3040. * Specifies the file where the init segment is located, or null.
  3041. * @property {(shaka.extern.aesKey|undefined)} aesKey
  3042. * AES-128 Content protection key
  3043. * @property {number} segmentSequenceCadence
  3044. * Specifies the cadence of independent segments in Segment Sequence
  3045. * Representation.
  3046. * @property {?string} label
  3047. * Label or null if unknown.
  3048. * @property {boolean} encrypted
  3049. * Specifies is encrypted or not.
  3050. */
  3051. shaka.dash.DashParser.InheritanceFrame;
  3052. /**
  3053. * @typedef {{
  3054. * dynamic: boolean,
  3055. * presentationTimeline: !shaka.media.PresentationTimeline,
  3056. * period: ?shaka.dash.DashParser.InheritanceFrame,
  3057. * periodInfo: ?shaka.dash.DashParser.PeriodInfo,
  3058. * adaptationSet: ?shaka.dash.DashParser.InheritanceFrame,
  3059. * representation: ?shaka.dash.DashParser.InheritanceFrame,
  3060. * bandwidth: number,
  3061. * indexRangeWarningGiven: boolean,
  3062. * availabilityTimeOffset: number,
  3063. * mediaPresentationDuration: ?number,
  3064. * profiles: !Array<string>,
  3065. * roles: ?Array<string>,
  3066. * urlParams: function():string
  3067. * }}
  3068. *
  3069. * @description
  3070. * Contains context data for the streams. This is designed to be
  3071. * shallow-copyable, so the parser must overwrite (not modify) each key as the
  3072. * parser moves through the manifest and the parsing context changes.
  3073. *
  3074. * @property {boolean} dynamic
  3075. * True if the MPD is dynamic (not all segments available at once)
  3076. * @property {!shaka.media.PresentationTimeline} presentationTimeline
  3077. * The PresentationTimeline.
  3078. * @property {?shaka.dash.DashParser.InheritanceFrame} period
  3079. * The inheritance from the Period element.
  3080. * @property {?shaka.dash.DashParser.PeriodInfo} periodInfo
  3081. * The Period info for the current Period.
  3082. * @property {?shaka.dash.DashParser.InheritanceFrame} adaptationSet
  3083. * The inheritance from the AdaptationSet element.
  3084. * @property {?shaka.dash.DashParser.InheritanceFrame} representation
  3085. * The inheritance from the Representation element.
  3086. * @property {number} bandwidth
  3087. * The bandwidth of the Representation, or zero if missing.
  3088. * @property {boolean} indexRangeWarningGiven
  3089. * True if the warning about SegmentURL@indexRange has been printed.
  3090. * @property {number} availabilityTimeOffset
  3091. * The sum of the availabilityTimeOffset values that apply to the element.
  3092. * @property {!Array<string>} profiles
  3093. * Profiles of DASH are defined to enable interoperability and the signaling
  3094. * of the use of features.
  3095. * @property {?number} mediaPresentationDuration
  3096. * Media presentation duration, or null if unknown.
  3097. * @property {function():string} urlParams
  3098. * The query params for the segments.
  3099. */
  3100. shaka.dash.DashParser.Context;
  3101. /**
  3102. * @typedef {{
  3103. * start: number,
  3104. * duration: ?number,
  3105. * node: ?shaka.extern.xml.Node,
  3106. * isLastPeriod: boolean
  3107. * }}
  3108. *
  3109. * @description
  3110. * Contains information about a Period element.
  3111. *
  3112. * @property {number} start
  3113. * The start time of the period.
  3114. * @property {?number} duration
  3115. * The duration of the period; or null if the duration is not given. This
  3116. * will be non-null for all periods except the last.
  3117. * @property {?shaka.extern.xml.Node} node
  3118. * The XML Node for the Period.
  3119. * @property {boolean} isLastPeriod
  3120. * Whether this Period is the last one in the manifest.
  3121. */
  3122. shaka.dash.DashParser.PeriodInfo;
  3123. /**
  3124. * @typedef {{
  3125. * id: string,
  3126. * contentType: ?string,
  3127. * language: string,
  3128. * main: boolean,
  3129. * streams: !Array<shaka.extern.Stream>,
  3130. * drmInfos: !Array<shaka.extern.DrmInfo>,
  3131. * trickModeFor: ?string,
  3132. * representationIds: !Array<string>,
  3133. * dependencyStreamMap: !Map<string, shaka.extern.Stream>
  3134. * }}
  3135. *
  3136. * @description
  3137. * Contains information about an AdaptationSet element.
  3138. *
  3139. * @property {string} id
  3140. * The unique ID of the adaptation set.
  3141. * @property {?string} contentType
  3142. * The content type of the AdaptationSet.
  3143. * @property {string} language
  3144. * The language of the AdaptationSet.
  3145. * @property {boolean} main
  3146. * Whether the AdaptationSet has the 'main' type.
  3147. * @property {!Array<shaka.extern.Stream>} streams
  3148. * The streams this AdaptationSet contains.
  3149. * @property {!Array<shaka.extern.DrmInfo>} drmInfos
  3150. * The DRM info for the AdaptationSet.
  3151. * @property {?string} trickModeFor
  3152. * If non-null, this AdaptationInfo represents trick mode tracks. This
  3153. * property is the ID of the normal AdaptationSet these tracks should be
  3154. * associated with.
  3155. * @property {!Array<string>} representationIds
  3156. * An array of the IDs of the Representations this AdaptationSet contains.
  3157. * @property {!Map<string, string>} dependencyStreamMap
  3158. * A map of dependencyStream
  3159. */
  3160. shaka.dash.DashParser.AdaptationInfo;
  3161. /**
  3162. * @typedef {function(): !Promise<shaka.media.SegmentIndex>}
  3163. * @description
  3164. * An async function which generates and returns a SegmentIndex.
  3165. */
  3166. shaka.dash.DashParser.GenerateSegmentIndexFunction;
  3167. /**
  3168. * @typedef {{
  3169. * generateSegmentIndex: shaka.dash.DashParser.GenerateSegmentIndexFunction
  3170. * }}
  3171. *
  3172. * @description
  3173. * Contains information about a Stream. This is passed from the createStreamInfo
  3174. * methods.
  3175. *
  3176. * @property {shaka.dash.DashParser.GenerateSegmentIndexFunction
  3177. * } generateSegmentIndex
  3178. * An async function to create the SegmentIndex for the stream.
  3179. */
  3180. shaka.dash.DashParser.StreamInfo;
  3181. shaka.media.ManifestParser.registerParserByMime(
  3182. 'application/dash+xml', () => new shaka.dash.DashParser());
  3183. shaka.media.ManifestParser.registerParserByMime(
  3184. 'video/vnd.mpeg.dash.mpd', () => new shaka.dash.DashParser());