Source: lib/mss/mss_parser.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.mss.MssParser');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.Deprecate');
  9. goog.require('shaka.abr.Ewma');
  10. goog.require('shaka.log');
  11. goog.require('shaka.media.InitSegmentReference');
  12. goog.require('shaka.media.ManifestParser');
  13. goog.require('shaka.media.PresentationTimeline');
  14. goog.require('shaka.media.QualityObserver');
  15. goog.require('shaka.media.SegmentIndex');
  16. goog.require('shaka.media.SegmentReference');
  17. goog.require('shaka.mss.ContentProtection');
  18. goog.require('shaka.net.NetworkingEngine');
  19. goog.require('shaka.util.Error');
  20. goog.require('shaka.util.LanguageUtils');
  21. goog.require('shaka.util.ManifestParserUtils');
  22. goog.require('shaka.util.MimeUtils');
  23. goog.require('shaka.util.Mp4Generator');
  24. goog.require('shaka.util.OperationManager');
  25. goog.require('shaka.util.PlayerConfiguration');
  26. goog.require('shaka.util.Timer');
  27. goog.require('shaka.util.TXml');
  28. goog.require('shaka.util.XmlUtils');
  29. /**
  30. * Creates a new MSS parser.
  31. *
  32. * @implements {shaka.extern.ManifestParser}
  33. * @export
  34. */
  35. shaka.mss.MssParser = class {
  36. /** Creates a new MSS parser. */
  37. constructor() {
  38. /** @private {?shaka.extern.ManifestConfiguration} */
  39. this.config_ = null;
  40. /** @private {?shaka.extern.ManifestParser.PlayerInterface} */
  41. this.playerInterface_ = null;
  42. /** @private {!Array.<string>} */
  43. this.manifestUris_ = [];
  44. /** @private {?shaka.extern.Manifest} */
  45. this.manifest_ = null;
  46. /** @private {number} */
  47. this.globalId_ = 1;
  48. /**
  49. * The update period in seconds, or 0 for no updates.
  50. * @private {number}
  51. */
  52. this.updatePeriod_ = 0;
  53. /** @private {?shaka.media.PresentationTimeline} */
  54. this.presentationTimeline_ = null;
  55. /**
  56. * An ewma that tracks how long updates take.
  57. * This is to mitigate issues caused by slow parsing on embedded devices.
  58. * @private {!shaka.abr.Ewma}
  59. */
  60. this.averageUpdateDuration_ = new shaka.abr.Ewma(5);
  61. /** @private {shaka.util.Timer} */
  62. this.updateTimer_ = new shaka.util.Timer(() => {
  63. this.onUpdate_();
  64. });
  65. /** @private {!shaka.util.OperationManager} */
  66. this.operationManager_ = new shaka.util.OperationManager();
  67. /**
  68. * @private {!Map.<number, !BufferSource>}
  69. */
  70. this.initSegmentDataByStreamId_ = new Map();
  71. /** @private {function():boolean} */
  72. this.isPreloadFn_ = () => false;
  73. }
  74. /**
  75. * @param {shaka.extern.ManifestConfiguration} config
  76. * @param {(function():boolean)=} isPreloadFn
  77. * @override
  78. * @exportInterface
  79. */
  80. configure(config, isPreloadFn) {
  81. goog.asserts.assert(config.mss != null,
  82. 'MssManifestConfiguration should not be null!');
  83. this.config_ = config;
  84. if (isPreloadFn) {
  85. this.isPreloadFn_ = isPreloadFn;
  86. }
  87. }
  88. /**
  89. * @override
  90. * @exportInterface
  91. */
  92. async start(uri, playerInterface) {
  93. goog.asserts.assert(this.config_, 'Must call configure() before start()!');
  94. this.manifestUris_ = [uri];
  95. this.playerInterface_ = playerInterface;
  96. await this.requestManifest_();
  97. // Make sure that the parser has not been destroyed.
  98. if (!this.playerInterface_) {
  99. throw new shaka.util.Error(
  100. shaka.util.Error.Severity.CRITICAL,
  101. shaka.util.Error.Category.PLAYER,
  102. shaka.util.Error.Code.OPERATION_ABORTED);
  103. }
  104. this.setUpdateTimer_();
  105. goog.asserts.assert(this.manifest_, 'Manifest should be non-null!');
  106. return this.manifest_;
  107. }
  108. /**
  109. * Called when the update timer ticks.
  110. *
  111. * @return {!Promise}
  112. * @private
  113. */
  114. async onUpdate_() {
  115. goog.asserts.assert(this.updatePeriod_ >= 0,
  116. 'There should be an update period');
  117. shaka.log.info('Updating manifest...');
  118. try {
  119. await this.requestManifest_();
  120. } catch (error) {
  121. goog.asserts.assert(error instanceof shaka.util.Error,
  122. 'Should only receive a Shaka error');
  123. // Try updating again, but ensure we haven't been destroyed.
  124. if (this.playerInterface_) {
  125. // We will retry updating, so override the severity of the error.
  126. error.severity = shaka.util.Error.Severity.RECOVERABLE;
  127. this.playerInterface_.onError(error);
  128. }
  129. }
  130. // Detect a call to stop()
  131. if (!this.playerInterface_) {
  132. return;
  133. }
  134. this.setUpdateTimer_();
  135. }
  136. /**
  137. * Sets the update timer. Does nothing if the manifest is not live.
  138. *
  139. * @private
  140. */
  141. setUpdateTimer_() {
  142. if (this.updatePeriod_ <= 0) {
  143. return;
  144. }
  145. const finalDelay = Math.max(
  146. shaka.mss.MssParser.MIN_UPDATE_PERIOD_,
  147. this.updatePeriod_,
  148. this.averageUpdateDuration_.getEstimate());
  149. // We do not run the timer as repeating because part of update is async and
  150. // we need schedule the update after it finished.
  151. this.updateTimer_.tickAfter(/* seconds= */ finalDelay);
  152. }
  153. /**
  154. * @override
  155. * @exportInterface
  156. */
  157. stop() {
  158. this.playerInterface_ = null;
  159. this.config_ = null;
  160. this.manifestUris_ = [];
  161. this.manifest_ = null;
  162. if (this.updateTimer_ != null) {
  163. this.updateTimer_.stop();
  164. this.updateTimer_ = null;
  165. }
  166. this.initSegmentDataByStreamId_.clear();
  167. return this.operationManager_.destroy();
  168. }
  169. /**
  170. * @override
  171. * @exportInterface
  172. */
  173. async update() {
  174. try {
  175. await this.requestManifest_();
  176. } catch (error) {
  177. if (!this.playerInterface_ || !error) {
  178. return;
  179. }
  180. goog.asserts.assert(error instanceof shaka.util.Error, 'Bad error type');
  181. this.playerInterface_.onError(error);
  182. }
  183. }
  184. /**
  185. * @override
  186. * @exportInterface
  187. */
  188. onExpirationUpdated(sessionId, expiration) {
  189. // No-op
  190. }
  191. /**
  192. * @override
  193. * @exportInterface
  194. */
  195. onInitialVariantChosen(variant) {
  196. // No-op
  197. }
  198. /**
  199. * @override
  200. * @exportInterface
  201. */
  202. banLocation(uri) {
  203. // No-op
  204. }
  205. /** @override */
  206. setMediaElement(mediaElement) {
  207. // No-op
  208. }
  209. /**
  210. * Makes a network request for the manifest and parses the resulting data.
  211. *
  212. * @private
  213. */
  214. async requestManifest_() {
  215. const requestType = shaka.net.NetworkingEngine.RequestType.MANIFEST;
  216. const type = shaka.net.NetworkingEngine.AdvancedRequestType.MSS;
  217. const request = shaka.net.NetworkingEngine.makeRequest(
  218. this.manifestUris_, this.config_.retryParameters);
  219. const networkingEngine = this.playerInterface_.networkingEngine;
  220. const startTime = Date.now();
  221. const operation = networkingEngine.request(requestType, request, {
  222. type,
  223. isPreload: this.isPreloadFn_(),
  224. });
  225. this.operationManager_.manage(operation);
  226. const response = await operation.promise;
  227. // Detect calls to stop().
  228. if (!this.playerInterface_) {
  229. return;
  230. }
  231. // For redirections add the response uri to the first entry in the
  232. // Manifest Uris array.
  233. if (response.uri && !this.manifestUris_.includes(response.uri)) {
  234. this.manifestUris_.unshift(response.uri);
  235. }
  236. // This may throw, but it will result in a failed promise.
  237. this.parseManifest_(response.data, response.uri);
  238. // Keep track of how long the longest manifest update took.
  239. const endTime = Date.now();
  240. const updateDuration = (endTime - startTime) / 1000.0;
  241. this.averageUpdateDuration_.sample(1, updateDuration);
  242. }
  243. /**
  244. * Parses the manifest XML. This also handles updates and will update the
  245. * stored manifest.
  246. *
  247. * @param {BufferSource} data
  248. * @param {string} finalManifestUri The final manifest URI, which may
  249. * differ from this.manifestUri_ if there has been a redirect.
  250. * @return {!Promise}
  251. * @private
  252. */
  253. parseManifest_(data, finalManifestUri) {
  254. let manifestData = data;
  255. const manifestPreprocessor = this.config_.mss.manifestPreprocessor;
  256. const defaultManifestPreprocessor =
  257. shaka.util.PlayerConfiguration.defaultManifestPreprocessor;
  258. if (manifestPreprocessor != defaultManifestPreprocessor) {
  259. shaka.Deprecate.deprecateFeature(5,
  260. 'manifest.mss.manifestPreprocessor configuration',
  261. 'Please Use manifest.mss.manifestPreprocessorTXml instead.');
  262. const mssElement =
  263. shaka.util.XmlUtils.parseXml(manifestData, 'SmoothStreamingMedia');
  264. if (!mssElement) {
  265. throw new shaka.util.Error(
  266. shaka.util.Error.Severity.CRITICAL,
  267. shaka.util.Error.Category.MANIFEST,
  268. shaka.util.Error.Code.MSS_INVALID_XML,
  269. finalManifestUri);
  270. }
  271. manifestPreprocessor(mssElement);
  272. manifestData = shaka.util.XmlUtils.toArrayBuffer(mssElement);
  273. }
  274. const mss = shaka.util.TXml.parseXml(manifestData, 'SmoothStreamingMedia');
  275. if (!mss) {
  276. throw new shaka.util.Error(
  277. shaka.util.Error.Severity.CRITICAL,
  278. shaka.util.Error.Category.MANIFEST,
  279. shaka.util.Error.Code.MSS_INVALID_XML,
  280. finalManifestUri);
  281. }
  282. const manifestPreprocessorTXml = this.config_.mss.manifestPreprocessorTXml;
  283. const defaultManifestPreprocessorTXml =
  284. shaka.util.PlayerConfiguration.defaultManifestPreprocessorTXml;
  285. if (manifestPreprocessorTXml != defaultManifestPreprocessorTXml) {
  286. manifestPreprocessorTXml(mss);
  287. }
  288. this.processManifest_(mss, finalManifestUri);
  289. return Promise.resolve();
  290. }
  291. /**
  292. * Takes a formatted MSS and converts it into a manifest.
  293. *
  294. * @param {!shaka.extern.xml.Node} mss
  295. * @param {string} finalManifestUri The final manifest URI, which may
  296. * differ from this.manifestUri_ if there has been a redirect.
  297. * @private
  298. */
  299. processManifest_(mss, finalManifestUri) {
  300. const TXml = shaka.util.TXml;
  301. if (!this.presentationTimeline_) {
  302. this.presentationTimeline_ = new shaka.media.PresentationTimeline(
  303. /* presentationStartTime= */ null, /* delay= */ 0);
  304. }
  305. const isLive = TXml.parseAttr(mss, 'IsLive',
  306. TXml.parseBoolean, /* defaultValue= */ false);
  307. if (isLive) {
  308. throw new shaka.util.Error(
  309. shaka.util.Error.Severity.CRITICAL,
  310. shaka.util.Error.Category.MANIFEST,
  311. shaka.util.Error.Code.MSS_LIVE_CONTENT_NOT_SUPPORTED);
  312. }
  313. this.presentationTimeline_.setStatic(!isLive);
  314. const timescale = TXml.parseAttr(mss, 'TimeScale',
  315. TXml.parseNonNegativeInt, shaka.mss.MssParser.DEFAULT_TIME_SCALE_);
  316. goog.asserts.assert(timescale && timescale >= 0,
  317. 'Timescale must be defined!');
  318. let dvrWindowLength = TXml.parseAttr(mss, 'DVRWindowLength',
  319. TXml.parseNonNegativeInt);
  320. // If the DVRWindowLength field is omitted for a live presentation or set
  321. // to 0, the DVR window is effectively infinite
  322. if (isLive && (dvrWindowLength === 0 || isNaN(dvrWindowLength))) {
  323. dvrWindowLength = Infinity;
  324. }
  325. // Start-over
  326. const canSeek = TXml.parseAttr(mss, 'CanSeek',
  327. TXml.parseBoolean, /* defaultValue= */ false);
  328. if (dvrWindowLength === 0 && canSeek) {
  329. dvrWindowLength = Infinity;
  330. }
  331. let segmentAvailabilityDuration = null;
  332. if (dvrWindowLength && dvrWindowLength > 0) {
  333. segmentAvailabilityDuration = dvrWindowLength / timescale;
  334. }
  335. // If it's live, we check for an override.
  336. if (isLive && !isNaN(this.config_.availabilityWindowOverride)) {
  337. segmentAvailabilityDuration = this.config_.availabilityWindowOverride;
  338. }
  339. // If it's null, that means segments are always available. This is always
  340. // the case for VOD, and sometimes the case for live.
  341. if (segmentAvailabilityDuration == null) {
  342. segmentAvailabilityDuration = Infinity;
  343. }
  344. this.presentationTimeline_.setSegmentAvailabilityDuration(
  345. segmentAvailabilityDuration);
  346. // Duration in timescale units.
  347. const duration = TXml.parseAttr(mss, 'Duration',
  348. TXml.parseNonNegativeInt, Infinity);
  349. goog.asserts.assert(duration && duration >= 0,
  350. 'Duration must be defined!');
  351. if (!isLive) {
  352. this.presentationTimeline_.setDuration(duration / timescale);
  353. }
  354. /** @type {!shaka.mss.MssParser.Context} */
  355. const context = {
  356. variants: [],
  357. textStreams: [],
  358. timescale: timescale,
  359. duration: duration / timescale,
  360. };
  361. this.parseStreamIndexes_(mss, context);
  362. // These steps are not done on manifest update.
  363. if (!this.manifest_) {
  364. this.manifest_ = {
  365. presentationTimeline: this.presentationTimeline_,
  366. variants: context.variants,
  367. textStreams: context.textStreams,
  368. imageStreams: [],
  369. offlineSessionIds: [],
  370. minBufferTime: 0,
  371. sequenceMode: this.config_.mss.sequenceMode,
  372. ignoreManifestTimestampsInSegmentsMode: false,
  373. type: shaka.media.ManifestParser.MSS,
  374. serviceDescription: null,
  375. nextUrl: null,
  376. periodCount: 1,
  377. gapCount: 0,
  378. isLowLatency: false,
  379. startTime: null,
  380. };
  381. // This is the first point where we have a meaningful presentation start
  382. // time, and we need to tell PresentationTimeline that so that it can
  383. // maintain consistency from here on.
  384. this.presentationTimeline_.lockStartTime();
  385. } else {
  386. // Just update the variants and text streams.
  387. this.manifest_.variants = context.variants;
  388. this.manifest_.textStreams = context.textStreams;
  389. // Re-filter the manifest. This will check any configured restrictions on
  390. // new variants, and will pass any new init data to DrmEngine to ensure
  391. // that key rotation works correctly.
  392. this.playerInterface_.filter(this.manifest_);
  393. }
  394. }
  395. /**
  396. * @param {!shaka.extern.xml.Node} mss
  397. * @param {!shaka.mss.MssParser.Context} context
  398. * @private
  399. */
  400. parseStreamIndexes_(mss, context) {
  401. const ContentProtection = shaka.mss.ContentProtection;
  402. const TXml = shaka.util.TXml;
  403. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  404. const protectionElems = TXml.findChildren(mss, 'Protection');
  405. const drmInfos = ContentProtection.parseFromProtection(
  406. protectionElems, this.config_.mss.keySystemsBySystemId);
  407. const audioStreams = [];
  408. const videoStreams = [];
  409. const textStreams = [];
  410. const streamIndexes = TXml.findChildren(mss, 'StreamIndex');
  411. for (const streamIndex of streamIndexes) {
  412. const qualityLevels = TXml.findChildren(streamIndex, 'QualityLevel');
  413. const timeline = this.createTimeline_(
  414. streamIndex, context.timescale, context.duration);
  415. // For each QualityLevel node, create a stream element
  416. for (const qualityLevel of qualityLevels) {
  417. const stream = this.createStream_(
  418. streamIndex, qualityLevel, timeline, drmInfos, context);
  419. if (!stream) {
  420. // Skip unsupported stream
  421. continue;
  422. }
  423. if (stream.type == ContentType.AUDIO &&
  424. !this.config_.disableAudio) {
  425. audioStreams.push(stream);
  426. } else if (stream.type == ContentType.VIDEO &&
  427. !this.config_.disableVideo) {
  428. videoStreams.push(stream);
  429. } else if (stream.type == ContentType.TEXT &&
  430. !this.config_.disableText) {
  431. textStreams.push(stream);
  432. }
  433. }
  434. }
  435. const variants = [];
  436. for (const audio of (audioStreams.length > 0 ? audioStreams : [null])) {
  437. for (const video of (videoStreams.length > 0 ? videoStreams : [null])) {
  438. variants.push(this.createVariant_(audio, video));
  439. }
  440. }
  441. context.variants = variants;
  442. context.textStreams = textStreams;
  443. }
  444. /**
  445. * @param {!shaka.extern.xml.Node} streamIndex
  446. * @param {!shaka.extern.xml.Node} qualityLevel
  447. * @param {!Array.<shaka.mss.MssParser.TimeRange>} timeline
  448. * @param {!Array.<shaka.extern.DrmInfo>} drmInfos
  449. * @param {!shaka.mss.MssParser.Context} context
  450. * @return {?shaka.extern.Stream}
  451. * @private
  452. */
  453. createStream_(streamIndex, qualityLevel, timeline, drmInfos, context) {
  454. const TXml = shaka.util.TXml;
  455. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  456. const MssParser = shaka.mss.MssParser;
  457. const type = streamIndex.attributes['Type'];
  458. const isValidType = type === 'audio' || type === 'video' ||
  459. type === 'text';
  460. if (!isValidType) {
  461. shaka.log.alwaysWarn('Ignoring unrecognized type:', type);
  462. return null;
  463. }
  464. const lang = streamIndex.attributes['Language'];
  465. const id = this.globalId_++;
  466. const bandwidth = TXml.parseAttr(
  467. qualityLevel, 'Bitrate', TXml.parsePositiveInt);
  468. const width = TXml.parseAttr(
  469. qualityLevel, 'MaxWidth', TXml.parsePositiveInt);
  470. const height = TXml.parseAttr(
  471. qualityLevel, 'MaxHeight', TXml.parsePositiveInt);
  472. const channelsCount = TXml.parseAttr(
  473. qualityLevel, 'Channels', TXml.parsePositiveInt);
  474. const audioSamplingRate = TXml.parseAttr(
  475. qualityLevel, 'SamplingRate', TXml.parsePositiveInt);
  476. let duration = context.duration;
  477. if (timeline.length) {
  478. const start = timeline[0].start;
  479. const end = timeline[timeline.length - 1].end;
  480. duration = end - start;
  481. }
  482. const presentationDuration = this.presentationTimeline_.getDuration();
  483. this.presentationTimeline_.setDuration(
  484. Math.min(duration, presentationDuration));
  485. /** @type {!shaka.extern.Stream} */
  486. const stream = {
  487. id: id,
  488. originalId: streamIndex.attributes['Name'] || String(id),
  489. groupId: null,
  490. createSegmentIndex: () => Promise.resolve(),
  491. closeSegmentIndex: () => Promise.resolve(),
  492. segmentIndex: null,
  493. mimeType: '',
  494. codecs: '',
  495. frameRate: undefined,
  496. pixelAspectRatio: undefined,
  497. bandwidth: bandwidth || 0,
  498. width: width || undefined,
  499. height: height || undefined,
  500. kind: '',
  501. encrypted: drmInfos.length > 0,
  502. drmInfos: drmInfos,
  503. keyIds: new Set(),
  504. language: shaka.util.LanguageUtils.normalize(lang || 'und'),
  505. originalLanguage: lang,
  506. label: '',
  507. type: '',
  508. primary: false,
  509. trickModeVideo: null,
  510. emsgSchemeIdUris: [],
  511. roles: [],
  512. forced: false,
  513. channelsCount: channelsCount,
  514. audioSamplingRate: audioSamplingRate,
  515. spatialAudio: false,
  516. closedCaptions: null,
  517. hdr: undefined,
  518. colorGamut: undefined,
  519. videoLayout: undefined,
  520. tilesLayout: undefined,
  521. matchedStreams: [],
  522. mssPrivateData: {
  523. duration: duration,
  524. timescale: context.timescale,
  525. codecPrivateData: null,
  526. },
  527. accessibilityPurpose: null,
  528. external: false,
  529. fastSwitching: false,
  530. fullMimeTypes: new Set(),
  531. isAudioMuxedInVideo: false,
  532. };
  533. // This is specifically for text tracks.
  534. const subType = streamIndex.attributes['Subtype'];
  535. if (subType) {
  536. const role = MssParser.ROLE_MAPPING_[subType];
  537. if (role) {
  538. stream.roles.push(role);
  539. }
  540. if (role === 'main') {
  541. stream.primary = true;
  542. }
  543. }
  544. let fourCCValue = qualityLevel.attributes['FourCC'];
  545. // If FourCC not defined at QualityLevel level,
  546. // then get it from StreamIndex level
  547. if (fourCCValue === null || fourCCValue === '') {
  548. fourCCValue = streamIndex.attributes['FourCC'];
  549. }
  550. // If still not defined (optional for audio stream,
  551. // see https://msdn.microsoft.com/en-us/library/ff728116%28v=vs.95%29.aspx),
  552. // then we consider the stream is an audio AAC stream
  553. if (!fourCCValue) {
  554. if (type === 'audio') {
  555. fourCCValue = 'AAC';
  556. } else if (type === 'video') {
  557. shaka.log.alwaysWarn('FourCC is not defined whereas it is required ' +
  558. 'for a QualityLevel element for a StreamIndex of type "video"');
  559. return null;
  560. }
  561. }
  562. // Check if codec is supported
  563. if (!MssParser.SUPPORTED_CODECS_.includes(fourCCValue.toUpperCase())) {
  564. shaka.log.alwaysWarn('Codec not supported:', fourCCValue);
  565. return null;
  566. }
  567. const codecPrivateData = this.getCodecPrivateData_(
  568. qualityLevel, type, fourCCValue, stream);
  569. stream.mssPrivateData.codecPrivateData = codecPrivateData;
  570. switch (type) {
  571. case 'audio':
  572. if (!codecPrivateData) {
  573. shaka.log.alwaysWarn('Quality unsupported without CodecPrivateData',
  574. type);
  575. return null;
  576. }
  577. stream.type = ContentType.AUDIO;
  578. // This mimetype is fake to allow the transmuxing.
  579. stream.mimeType = 'mss/audio/mp4';
  580. stream.codecs = this.getAACCodec_(
  581. qualityLevel, fourCCValue, codecPrivateData);
  582. break;
  583. case 'video':
  584. if (!codecPrivateData) {
  585. shaka.log.alwaysWarn('Quality unsupported without CodecPrivateData',
  586. type);
  587. return null;
  588. }
  589. stream.type = ContentType.VIDEO;
  590. // This mimetype is fake to allow the transmuxing.
  591. stream.mimeType = 'mss/video/mp4';
  592. stream.codecs = this.getH264Codec_(
  593. qualityLevel, codecPrivateData);
  594. break;
  595. case 'text':
  596. stream.type = ContentType.TEXT;
  597. stream.mimeType = 'application/mp4';
  598. if (fourCCValue === 'TTML' || fourCCValue === 'DFXP') {
  599. stream.codecs = 'stpp';
  600. }
  601. break;
  602. }
  603. stream.fullMimeTypes.add(shaka.util.MimeUtils.getFullType(
  604. stream.mimeType, stream.codecs));
  605. // Lazy-Load the segment index to avoid create all init segment at the
  606. // same time
  607. stream.createSegmentIndex = () => {
  608. if (stream.segmentIndex) {
  609. return Promise.resolve();
  610. }
  611. let initSegmentData;
  612. if (this.initSegmentDataByStreamId_.has(stream.id)) {
  613. initSegmentData = this.initSegmentDataByStreamId_.get(stream.id);
  614. } else {
  615. let videoNalus = [];
  616. if (stream.type == ContentType.VIDEO) {
  617. const codecPrivateData = stream.mssPrivateData.codecPrivateData;
  618. videoNalus = codecPrivateData.split('00000001').slice(1);
  619. }
  620. /** @type {shaka.util.Mp4Generator.StreamInfo} */
  621. const streamInfo = {
  622. id: stream.id,
  623. type: stream.type,
  624. codecs: stream.codecs,
  625. encrypted: stream.encrypted,
  626. timescale: stream.mssPrivateData.timescale,
  627. duration: stream.mssPrivateData.duration,
  628. videoNalus: videoNalus,
  629. audioConfig: new Uint8Array([]),
  630. videoConfig: new Uint8Array([]),
  631. hSpacing: 0,
  632. vSpacing: 0,
  633. data: null, // Data is not necessary for init segement.
  634. stream: stream,
  635. };
  636. const mp4Generator = new shaka.util.Mp4Generator([streamInfo]);
  637. initSegmentData = mp4Generator.initSegment();
  638. this.initSegmentDataByStreamId_.set(stream.id, initSegmentData);
  639. }
  640. const qualityInfo =
  641. shaka.media.QualityObserver.createQualityInfo(stream);
  642. const initSegmentRef = new shaka.media.InitSegmentReference(
  643. () => [],
  644. /* startByte= */ 0,
  645. /* endByte= */ null,
  646. qualityInfo,
  647. stream.mssPrivateData.timescale,
  648. initSegmentData);
  649. const segments = this.createSegments_(initSegmentRef,
  650. stream, streamIndex, timeline);
  651. stream.segmentIndex = new shaka.media.SegmentIndex(segments);
  652. return Promise.resolve();
  653. };
  654. stream.closeSegmentIndex = () => {
  655. // If we have a segment index, release it.
  656. if (stream.segmentIndex) {
  657. stream.segmentIndex.release();
  658. stream.segmentIndex = null;
  659. }
  660. };
  661. return stream;
  662. }
  663. /**
  664. * @param {!shaka.extern.xml.Node} qualityLevel
  665. * @param {string} type
  666. * @param {string} fourCCValue
  667. * @param {!shaka.extern.Stream} stream
  668. * @return {?string}
  669. * @private
  670. */
  671. getCodecPrivateData_(qualityLevel, type, fourCCValue, stream) {
  672. const codecPrivateData = qualityLevel.attributes['CodecPrivateData'];
  673. if (codecPrivateData) {
  674. return codecPrivateData;
  675. }
  676. if (type !== 'audio') {
  677. return null;
  678. }
  679. // For the audio we can reconstruct the CodecPrivateData
  680. // By default stereo
  681. const channels = stream.channelsCount || 2;
  682. // By default 44,1kHz.
  683. const samplingRate = stream.audioSamplingRate || 44100;
  684. const samplingFrequencyIndex = {
  685. 96000: 0x0,
  686. 88200: 0x1,
  687. 64000: 0x2,
  688. 48000: 0x3,
  689. 44100: 0x4,
  690. 32000: 0x5,
  691. 24000: 0x6,
  692. 22050: 0x7,
  693. 16000: 0x8,
  694. 12000: 0x9,
  695. 11025: 0xA,
  696. 8000: 0xB,
  697. 7350: 0xC,
  698. };
  699. const indexFreq = samplingFrequencyIndex[samplingRate];
  700. if (fourCCValue === 'AACH') {
  701. // High Efficiency AAC Profile
  702. const objectType = 0x05;
  703. // 4 bytes :
  704. // XXXXX XXXX XXXX XXXX
  705. // 'ObjectType' 'Freq Index' 'Channels value' 'Extens Sampl Freq'
  706. // XXXXX XXX XXXXXXX
  707. // 'ObjectType' 'GAS' 'alignment = 0'
  708. const data = new Uint8Array(4);
  709. // In HE AAC Extension Sampling frequence
  710. // equals to SamplingRate * 2
  711. const extensionSamplingFrequencyIndex =
  712. samplingFrequencyIndex[samplingRate * 2];
  713. // Freq Index is present for 3 bits in the first byte, last bit is in
  714. // the second
  715. data[0] = (objectType << 3) | (indexFreq >> 1);
  716. data[1] = (indexFreq << 7) | (channels << 3) |
  717. (extensionSamplingFrequencyIndex >> 1);
  718. // Origin object type equals to 2 => AAC Main Low Complexity
  719. data[2] = (extensionSamplingFrequencyIndex << 7) | (0x02 << 2);
  720. // Slignment bits
  721. data[3] = 0x0;
  722. // Put the 4 bytes in an 16 bits array
  723. const arr16 = new Uint16Array(2);
  724. arr16[0] = (data[0] << 8) + data[1];
  725. arr16[1] = (data[2] << 8) + data[3];
  726. // Convert decimal to hex value
  727. return arr16[0].toString(16) + arr16[1].toString(16);
  728. } else {
  729. // AAC Main Low Complexity
  730. const objectType = 0x02;
  731. // 2 bytes:
  732. // XXXXX XXXX XXXX XXX
  733. // 'ObjectType' 'Freq Index' 'Channels value' 'GAS = 000'
  734. const data = new Uint8Array(2);
  735. // Freq Index is present for 3 bits in the first byte, last bit is in
  736. // the second
  737. data[0] = (objectType << 3) | (indexFreq >> 1);
  738. data[1] = (indexFreq << 7) | (channels << 3);
  739. // Put the 2 bytes in an 16 bits array
  740. const arr16 = new Uint16Array(1);
  741. arr16[0] = (data[0] << 8) + data[1];
  742. // Convert decimal to hex value
  743. return arr16[0].toString(16);
  744. }
  745. }
  746. /**
  747. * @param {!shaka.extern.xml.Node} qualityLevel
  748. * @param {string} fourCCValue
  749. * @param {?string} codecPrivateData
  750. * @return {string}
  751. * @private
  752. */
  753. getAACCodec_(qualityLevel, fourCCValue, codecPrivateData) {
  754. let objectType = 0;
  755. // Chrome problem, in implicit AAC HE definition, so when AACH is detected
  756. // in FourCC set objectType to 5 => strange, it should be 2
  757. if (fourCCValue === 'AACH') {
  758. objectType = 0x05;
  759. }
  760. if (!codecPrivateData) {
  761. // AAC Main Low Complexity => object Type = 2
  762. objectType = 0x02;
  763. if (fourCCValue === 'AACH') {
  764. // High Efficiency AAC Profile = object Type = 5 SBR
  765. objectType = 0x05;
  766. }
  767. } else if (objectType === 0) {
  768. objectType = (parseInt(codecPrivateData.substr(0, 2), 16) & 0xF8) >> 3;
  769. }
  770. return 'mp4a.40.' + objectType;
  771. }
  772. /**
  773. * @param {!shaka.extern.xml.Node} qualityLevel
  774. * @param {?string} codecPrivateData
  775. * @return {string}
  776. * @private
  777. */
  778. getH264Codec_(qualityLevel, codecPrivateData) {
  779. // Extract from the CodecPrivateData field the hexadecimal representation
  780. // of the following three bytes in the sequence parameter set NAL unit.
  781. // => Find the SPS nal header
  782. const nalHeader = /00000001[0-9]7/.exec(codecPrivateData);
  783. if (!nalHeader.length) {
  784. return '';
  785. }
  786. if (!codecPrivateData) {
  787. return '';
  788. }
  789. // => Find the 6 characters after the SPS nalHeader (if it exists)
  790. const avcoti = codecPrivateData.substr(
  791. codecPrivateData.indexOf(nalHeader[0]) + 10, 6);
  792. return 'avc1.' + avcoti;
  793. }
  794. /**
  795. * @param {!shaka.media.InitSegmentReference} initSegmentRef
  796. * @param {!shaka.extern.Stream} stream
  797. * @param {!shaka.extern.xml.Node} streamIndex
  798. * @param {!Array.<shaka.mss.MssParser.TimeRange>} timeline
  799. * @return {!Array.<!shaka.media.SegmentReference>}
  800. * @private
  801. */
  802. createSegments_(initSegmentRef, stream, streamIndex, timeline) {
  803. const ManifestParserUtils = shaka.util.ManifestParserUtils;
  804. const url = streamIndex.attributes['Url'];
  805. goog.asserts.assert(url, 'Missing URL for segments');
  806. const mediaUrl = url.replace('{bitrate}', String(stream.bandwidth));
  807. const segments = [];
  808. for (const time of timeline) {
  809. const getUris = () => {
  810. return ManifestParserUtils.resolveUris(this.manifestUris_,
  811. [mediaUrl.replace('{start time}', String(time.unscaledStart))]);
  812. };
  813. segments.push(new shaka.media.SegmentReference(
  814. time.start,
  815. time.end,
  816. getUris,
  817. /* startByte= */ 0,
  818. /* endByte= */ null,
  819. initSegmentRef,
  820. /* timestampOffset= */ 0,
  821. /* appendWindowStart= */ 0,
  822. /* appendWindowEnd= */ stream.mssPrivateData.duration));
  823. }
  824. return segments;
  825. }
  826. /**
  827. * Expands a streamIndex into an array-based timeline. The results are in
  828. * seconds.
  829. *
  830. * @param {!shaka.extern.xml.Node} streamIndex
  831. * @param {number} timescale
  832. * @param {number} duration The duration in seconds.
  833. * @return {!Array.<shaka.mss.MssParser.TimeRange>}
  834. * @private
  835. */
  836. createTimeline_(streamIndex, timescale, duration) {
  837. goog.asserts.assert(
  838. timescale > 0 && timescale < Infinity,
  839. 'timescale must be a positive, finite integer');
  840. goog.asserts.assert(
  841. duration > 0, 'duration must be a positive integer');
  842. const TXml = shaka.util.TXml;
  843. const timePoints = TXml.findChildren(streamIndex, 'c');
  844. /** @type {!Array.<shaka.mss.MssParser.TimeRange>} */
  845. const timeline = [];
  846. let lastEndTime = 0;
  847. for (let i = 0; i < timePoints.length; ++i) {
  848. const timePoint = timePoints[i];
  849. const next = timePoints[i + 1];
  850. const t =
  851. TXml.parseAttr(timePoint, 't', TXml.parseNonNegativeInt);
  852. const d =
  853. TXml.parseAttr(timePoint, 'd', TXml.parseNonNegativeInt);
  854. const r = TXml.parseAttr(timePoint, 'r', TXml.parseInt);
  855. if (!d) {
  856. shaka.log.warning(
  857. '"c" element must have a duration:',
  858. 'ignoring the remaining "c" elements.', timePoint);
  859. return timeline;
  860. }
  861. let startTime = t != null ? t : lastEndTime;
  862. let repeat = r || 0;
  863. // Unlike in DASH, in MSS r does not start counting repetitions at 0 but
  864. // at 1, to maintain the code equivalent to DASH if r exists we
  865. // subtract 1.
  866. if (repeat) {
  867. repeat--;
  868. }
  869. if (repeat < 0) {
  870. if (next) {
  871. const nextStartTime =
  872. TXml.parseAttr(next, 't', TXml.parseNonNegativeInt);
  873. if (nextStartTime == null) {
  874. shaka.log.warning(
  875. 'An "c" element cannot have a negative repeat',
  876. 'if the next "c" element does not have a valid start time:',
  877. 'ignoring the remaining "c" elements.', timePoint);
  878. return timeline;
  879. } else if (startTime >= nextStartTime) {
  880. shaka.log.warning(
  881. 'An "c" element cannot have a negative repeatif its start ',
  882. 'time exceeds the next "c" element\'s start time:',
  883. 'ignoring the remaining "c" elements.', timePoint);
  884. return timeline;
  885. }
  886. repeat = Math.ceil((nextStartTime - startTime) / d) - 1;
  887. } else {
  888. if (duration == Infinity) {
  889. // The MSS spec. actually allows the last "c" element to have a
  890. // negative repeat value even when it has an infinite
  891. // duration. No one uses this feature and no one ever should,
  892. // ever.
  893. shaka.log.warning(
  894. 'The last "c" element cannot have a negative repeat',
  895. 'if the Period has an infinite duration:',
  896. 'ignoring the last "c" element.', timePoint);
  897. return timeline;
  898. } else if (startTime / timescale >= duration) {
  899. shaka.log.warning(
  900. 'The last "c" element cannot have a negative repeat',
  901. 'if its start time exceeds the duration:',
  902. 'igoring the last "c" element.', timePoint);
  903. return timeline;
  904. }
  905. repeat = Math.ceil((duration * timescale - startTime) / d) - 1;
  906. }
  907. }
  908. for (let j = 0; j <= repeat; ++j) {
  909. const endTime = startTime + d;
  910. const item = {
  911. start: startTime / timescale,
  912. end: endTime / timescale,
  913. unscaledStart: startTime,
  914. };
  915. timeline.push(item);
  916. startTime = endTime;
  917. lastEndTime = endTime;
  918. }
  919. }
  920. return timeline;
  921. }
  922. /**
  923. * @param {?shaka.extern.Stream} audioStream
  924. * @param {?shaka.extern.Stream} videoStream
  925. * @return {!shaka.extern.Variant}
  926. * @private
  927. */
  928. createVariant_(audioStream, videoStream) {
  929. const ContentType = shaka.util.ManifestParserUtils.ContentType;
  930. goog.asserts.assert(!audioStream ||
  931. audioStream.type == ContentType.AUDIO, 'Audio parameter mismatch!');
  932. goog.asserts.assert(!videoStream ||
  933. videoStream.type == ContentType.VIDEO, 'Video parameter mismatch!');
  934. let bandwidth = 0;
  935. if (audioStream && audioStream.bandwidth && audioStream.bandwidth > 0) {
  936. bandwidth += audioStream.bandwidth;
  937. }
  938. if (videoStream && videoStream.bandwidth && videoStream.bandwidth > 0) {
  939. bandwidth += videoStream.bandwidth;
  940. }
  941. return {
  942. id: this.globalId_++,
  943. language: audioStream ? audioStream.language : 'und',
  944. disabledUntilTime: 0,
  945. primary: (!!audioStream && audioStream.primary) ||
  946. (!!videoStream && videoStream.primary),
  947. audio: audioStream,
  948. video: videoStream,
  949. bandwidth: bandwidth,
  950. allowedByApplication: true,
  951. allowedByKeySystem: true,
  952. decodingInfos: [],
  953. };
  954. }
  955. };
  956. /**
  957. * Contains the minimum amount of time, in seconds, between manifest update
  958. * requests.
  959. *
  960. * @private
  961. * @const {number}
  962. */
  963. shaka.mss.MssParser.MIN_UPDATE_PERIOD_ = 3;
  964. /**
  965. * @private
  966. * @const {number}
  967. */
  968. shaka.mss.MssParser.DEFAULT_TIME_SCALE_ = 1e7;
  969. /**
  970. * MSS supported codecs.
  971. *
  972. * @private
  973. * @const {!Array.<string>}
  974. */
  975. shaka.mss.MssParser.SUPPORTED_CODECS_ = [
  976. 'AAC',
  977. 'AACL',
  978. 'AACH',
  979. 'AACP',
  980. 'AVC1',
  981. 'H264',
  982. 'TTML',
  983. 'DFXP',
  984. ];
  985. /**
  986. * MPEG-DASH Role and accessibility mapping for text tracks according to
  987. * ETSI TS 103 285 v1.1.1 (section 7.1.2)
  988. *
  989. * @const {!Object.<string, string>}
  990. * @private
  991. */
  992. shaka.mss.MssParser.ROLE_MAPPING_ = {
  993. 'CAPT': 'main',
  994. 'SUBT': 'alternate',
  995. 'DESC': 'main',
  996. };
  997. /**
  998. * @typedef {{
  999. * variants: !Array.<shaka.extern.Variant>,
  1000. * textStreams: !Array.<shaka.extern.Stream>,
  1001. * timescale: number,
  1002. * duration: number
  1003. * }}
  1004. *
  1005. * @property {!Array.<shaka.extern.Variant>} variants
  1006. * The presentation's Variants.
  1007. * @property {!Array.<shaka.extern.Stream>} textStreams
  1008. * The presentation's text streams.
  1009. * @property {number} timescale
  1010. * The presentation's timescale.
  1011. * @property {number} duration
  1012. * The presentation's duration.
  1013. */
  1014. shaka.mss.MssParser.Context;
  1015. /**
  1016. * @typedef {{
  1017. * start: number,
  1018. * unscaledStart: number,
  1019. * end: number
  1020. * }}
  1021. *
  1022. * @description
  1023. * Defines a time range of a media segment. Times are in seconds.
  1024. *
  1025. * @property {number} start
  1026. * The start time of the range.
  1027. * @property {number} unscaledStart
  1028. * The start time of the range in representation timescale units.
  1029. * @property {number} end
  1030. * The end time (exclusive) of the range.
  1031. */
  1032. shaka.mss.MssParser.TimeRange;
  1033. shaka.media.ManifestParser.registerParserByMime(
  1034. 'application/vnd.ms-sstr+xml', () => new shaka.mss.MssParser());