Source: lib/util/cmcd_manager.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.CmcdManager');
  7. goog.require('goog.Uri');
  8. goog.require('shaka.log');
  9. goog.require('shaka.net.NetworkingEngine');
  10. goog.require('shaka.util.ArrayUtils');
  11. goog.require('shaka.util.EventManager');
  12. goog.requireType('shaka.media.SegmentReference');
  13. /**
  14. * @summary
  15. * A CmcdManager maintains CMCD state as well as a collection of utility
  16. * functions.
  17. */
  18. shaka.util.CmcdManager = class {
  19. /**
  20. * @param {shaka.util.CmcdManager.PlayerInterface} playerInterface
  21. * @param {shaka.extern.CmcdConfiguration} config
  22. */
  23. constructor(playerInterface, config) {
  24. /** @private {shaka.util.CmcdManager.PlayerInterface} */
  25. this.playerInterface_ = playerInterface;
  26. /** @private {?shaka.extern.CmcdConfiguration} */
  27. this.config_ = config;
  28. /**
  29. * Streaming format
  30. *
  31. * @private {(shaka.util.CmcdManager.StreamingFormat|undefined)}
  32. */
  33. this.sf_ = undefined;
  34. /**
  35. * @private {boolean}
  36. */
  37. this.playbackStarted_ = false;
  38. /**
  39. * @private {boolean}
  40. */
  41. this.buffering_ = true;
  42. /**
  43. * @private {boolean}
  44. */
  45. this.starved_ = false;
  46. /**
  47. * @private {boolean}
  48. */
  49. this.lowLatency_ = false;
  50. /**
  51. * @private {number|undefined}
  52. */
  53. this.playbackPlayTime_ = undefined;
  54. /**
  55. * @private {number|undefined}
  56. */
  57. this.playbackPlayingTime_ = undefined;
  58. /**
  59. * @private {number}
  60. */
  61. this.startTimeOfLoad_ = 0;
  62. /**
  63. * @private {boolean}
  64. */
  65. this.msdSent_ = false;
  66. /**
  67. * @private {shaka.util.EventManager}
  68. */
  69. this.eventManager_ = new shaka.util.EventManager();
  70. /** @private {HTMLMediaElement} */
  71. this.video_ = null;
  72. }
  73. /**
  74. * Set media element and setup event listeners
  75. * @param {HTMLMediaElement} mediaElement The video element
  76. */
  77. setMediaElement(mediaElement) {
  78. this.video_ = mediaElement;
  79. this.setupMSDEventListeners_();
  80. }
  81. /**
  82. * Called by the Player to provide an updated configuration any time it
  83. * changes.
  84. *
  85. * @param {shaka.extern.CmcdConfiguration} config
  86. */
  87. configure(config) {
  88. this.config_ = config;
  89. }
  90. /**
  91. * Resets the CmcdManager.
  92. */
  93. reset() {
  94. this.playbackStarted_ = false;
  95. this.buffering_ = true;
  96. this.starved_ = false;
  97. this.lowLatency_ = false;
  98. this.playbackPlayTime_ = 0;
  99. this.playbackPlayingTime_ = 0;
  100. this.startTimeOfLoad_ = 0;
  101. this.msdSent_ = false;
  102. this.video_ = null;
  103. this.eventManager_.removeAll();
  104. }
  105. /**
  106. * Set the buffering state
  107. *
  108. * @param {boolean} buffering
  109. */
  110. setBuffering(buffering) {
  111. if (!buffering && !this.playbackStarted_) {
  112. this.playbackStarted_ = true;
  113. }
  114. if (this.playbackStarted_ && buffering) {
  115. this.starved_ = true;
  116. }
  117. this.buffering_ = buffering;
  118. }
  119. /**
  120. * Set the low latency
  121. *
  122. * @param {boolean} lowLatency
  123. */
  124. setLowLatency(lowLatency) {
  125. this.lowLatency_ = lowLatency;
  126. const StreamingFormat = shaka.util.CmcdManager.StreamingFormat;
  127. if (this.lowLatency_) {
  128. if (this.sf_ == StreamingFormat.DASH) {
  129. this.sf_ = StreamingFormat.LOW_LATENCY_DASH;
  130. } else if (this.sf_ == StreamingFormat.HLS) {
  131. this.sf_ = StreamingFormat.LOW_LATENCY_HLS;
  132. }
  133. } else {
  134. if (this.sf_ == StreamingFormat.LOW_LATENCY_DASH) {
  135. this.sf_ = StreamingFormat.DASH;
  136. } else if (this.sf_ == StreamingFormat.LOW_LATENCY_HLS) {
  137. this.sf_ = StreamingFormat.HLS;
  138. }
  139. }
  140. }
  141. /**
  142. * Set start time of load if autoplay is enabled
  143. *
  144. * @param {number} startTimeOfLoad
  145. */
  146. setStartTimeOfLoad(startTimeOfLoad) {
  147. if (this.video_) {
  148. const playResult = this.video_.play();
  149. if (playResult) {
  150. playResult.then(() => {
  151. this.startTimeOfLoad_ = startTimeOfLoad;
  152. }).catch((e) => {
  153. this.startTimeOfLoad_ = 0;
  154. });
  155. }
  156. }
  157. }
  158. /**
  159. * Apply CMCD data to a request.
  160. *
  161. * @param {!shaka.net.NetworkingEngine.RequestType} type
  162. * The request type
  163. * @param {!shaka.extern.Request} request
  164. * The request to apply CMCD data to
  165. * @param {shaka.extern.RequestContext=} context
  166. * The request context
  167. */
  168. applyData(type, request, context = {}) {
  169. if (!this.config_.enabled) {
  170. return;
  171. }
  172. if (request.method === 'HEAD') {
  173. this.apply_(request);
  174. return;
  175. }
  176. const RequestType = shaka.net.NetworkingEngine.RequestType;
  177. const ObjectType = shaka.util.CmcdManager.ObjectType;
  178. switch (type) {
  179. case RequestType.MANIFEST:
  180. this.applyManifestData(request, context);
  181. break;
  182. case RequestType.SEGMENT:
  183. this.applySegmentData(request, context);
  184. break;
  185. case RequestType.LICENSE:
  186. case RequestType.SERVER_CERTIFICATE:
  187. case RequestType.KEY:
  188. this.apply_(request, {ot: ObjectType.KEY});
  189. break;
  190. case RequestType.TIMING:
  191. this.apply_(request, {ot: ObjectType.OTHER});
  192. break;
  193. }
  194. }
  195. /**
  196. * Apply CMCD data to a manifest request.
  197. *
  198. * @param {!shaka.extern.Request} request
  199. * The request to apply CMCD data to
  200. * @param {shaka.extern.RequestContext} context
  201. * The request context
  202. */
  203. applyManifestData(request, context) {
  204. try {
  205. if (!this.config_.enabled) {
  206. return;
  207. }
  208. if (context.type) {
  209. this.sf_ = this.getStreamFormat_(context.type);
  210. }
  211. this.apply_(request, {
  212. ot: shaka.util.CmcdManager.ObjectType.MANIFEST,
  213. su: !this.playbackStarted_,
  214. });
  215. } catch (error) {
  216. shaka.log.warnOnce('CMCD_MANIFEST_ERROR',
  217. 'Could not generate manifest CMCD data.', error);
  218. }
  219. }
  220. /**
  221. * Apply CMCD data to a segment request
  222. *
  223. * @param {!shaka.extern.Request} request
  224. * @param {shaka.extern.RequestContext} context
  225. * The request context
  226. */
  227. applySegmentData(request, context) {
  228. try {
  229. if (!this.config_.enabled) {
  230. return;
  231. }
  232. const segment = context.segment;
  233. let duration = 0;
  234. if (segment) {
  235. duration = segment.endTime - segment.startTime;
  236. }
  237. const data = {
  238. d: duration * 1000,
  239. st: this.getStreamType_(),
  240. };
  241. data.ot = this.getObjectType_(context);
  242. const ObjectType = shaka.util.CmcdManager.ObjectType;
  243. const isMedia = data.ot === ObjectType.VIDEO ||
  244. data.ot === ObjectType.AUDIO ||
  245. data.ot === ObjectType.MUXED ||
  246. data.ot === ObjectType.TIMED_TEXT;
  247. const stream = context.stream;
  248. if (stream) {
  249. const playbackRate = this.playerInterface_.getPlaybackRate();
  250. if (isMedia) {
  251. data.bl = this.getBufferLength_(stream.type);
  252. if (data.ot !== ObjectType.TIMED_TEXT) {
  253. const remainingBufferLength =
  254. this.getRemainingBufferLength_(stream.type);
  255. if (playbackRate) {
  256. data.dl = remainingBufferLength / Math.abs(playbackRate);
  257. } else {
  258. data.dl = remainingBufferLength;
  259. }
  260. }
  261. }
  262. if (stream.bandwidth) {
  263. data.br = stream.bandwidth / 1000;
  264. }
  265. if (stream.segmentIndex && segment) {
  266. const reverse = playbackRate < 0;
  267. const iterator = stream.segmentIndex.getIteratorForTime(
  268. segment.endTime, /* allowNonIndependent= */ true, reverse);
  269. if (iterator) {
  270. const nextSegment = iterator.next().value;
  271. if (nextSegment && nextSegment != segment) {
  272. if (!shaka.util.ArrayUtils.equal(
  273. segment.getUris(), nextSegment.getUris())) {
  274. data.nor = this.urlToRelativePath_(
  275. nextSegment.getUris()[0], request.uris[0]);
  276. }
  277. if ((nextSegment.startByte || nextSegment.endByte) &&
  278. (segment.startByte != nextSegment.startByte ||
  279. segment.endByte != nextSegment.endByte)) {
  280. let range = nextSegment.startByte + '-';
  281. if (nextSegment.endByte) {
  282. range += nextSegment.endByte;
  283. }
  284. data.nrr = range;
  285. }
  286. }
  287. }
  288. const rtp = this.calculateRtp_(stream, segment);
  289. if (!isNaN(rtp)) {
  290. data.rtp = rtp;
  291. }
  292. }
  293. }
  294. if (isMedia && data.ot !== ObjectType.TIMED_TEXT) {
  295. data.tb = this.getTopBandwidth_(data.ot) / 1000;
  296. }
  297. this.apply_(request, data);
  298. } catch (error) {
  299. shaka.log.warnOnce('CMCD_SEGMENT_ERROR',
  300. 'Could not generate segment CMCD data.', error);
  301. }
  302. }
  303. /**
  304. * Apply CMCD data to a text request
  305. *
  306. * @param {!shaka.extern.Request} request
  307. */
  308. applyTextData(request) {
  309. try {
  310. if (!this.config_.enabled) {
  311. return;
  312. }
  313. this.apply_(request, {
  314. ot: shaka.util.CmcdManager.ObjectType.CAPTION,
  315. su: true,
  316. });
  317. } catch (error) {
  318. shaka.log.warnOnce('CMCD_TEXT_ERROR',
  319. 'Could not generate text CMCD data.', error);
  320. }
  321. }
  322. /**
  323. * Apply CMCD data to streams loaded via src=.
  324. *
  325. * @param {string} uri
  326. * @param {string} mimeType
  327. * @return {string}
  328. */
  329. appendSrcData(uri, mimeType) {
  330. try {
  331. if (!this.config_.enabled) {
  332. return uri;
  333. }
  334. const data = this.createData_();
  335. data.ot = this.getObjectTypeFromMimeType_(mimeType);
  336. data.su = true;
  337. const query = shaka.util.CmcdManager.toQuery(data);
  338. return shaka.util.CmcdManager.appendQueryToUri(uri, query);
  339. } catch (error) {
  340. shaka.log.warnOnce('CMCD_SRC_ERROR',
  341. 'Could not generate src CMCD data.', error);
  342. return uri;
  343. }
  344. }
  345. /**
  346. * Apply CMCD data to side car text track uri.
  347. *
  348. * @param {string} uri
  349. * @return {string}
  350. */
  351. appendTextTrackData(uri) {
  352. try {
  353. if (!this.config_.enabled) {
  354. return uri;
  355. }
  356. const data = this.createData_();
  357. data.ot = shaka.util.CmcdManager.ObjectType.CAPTION;
  358. data.su = true;
  359. const query = shaka.util.CmcdManager.toQuery(data);
  360. return shaka.util.CmcdManager.appendQueryToUri(uri, query);
  361. } catch (error) {
  362. shaka.log.warnOnce('CMCD_TEXT_TRACK_ERROR',
  363. 'Could not generate text track CMCD data.', error);
  364. return uri;
  365. }
  366. }
  367. /**
  368. * Set playbackPlayTime_ when the play event is triggered
  369. * @private
  370. */
  371. onPlaybackPlay_() {
  372. if (!this.playbackPlayTime_) {
  373. this.playbackPlayTime_ = Date.now();
  374. }
  375. }
  376. /**
  377. * Set playbackPlayingTime_
  378. * @private
  379. */
  380. onPlaybackPlaying_() {
  381. if (!this.playbackPlayingTime_) {
  382. this.playbackPlayingTime_ = Date.now();
  383. }
  384. }
  385. /**
  386. * Setup event listeners for msd calculation
  387. * @private
  388. */
  389. setupMSDEventListeners_() {
  390. const onPlaybackPlay = () => this.onPlaybackPlay_();
  391. this.eventManager_.listenOnce(
  392. this.video_, 'play', onPlaybackPlay);
  393. const onPlaybackPlaying = () => this.onPlaybackPlaying_();
  394. this.eventManager_.listenOnce(
  395. this.video_, 'playing', onPlaybackPlaying);
  396. }
  397. /**
  398. * Create baseline CMCD data
  399. *
  400. * @return {CmcdData}
  401. * @private
  402. */
  403. createData_() {
  404. if (!this.config_.sessionId) {
  405. this.config_.sessionId = window.crypto.randomUUID();
  406. }
  407. return {
  408. v: this.config_.version,
  409. sf: this.sf_,
  410. sid: this.config_.sessionId,
  411. cid: this.config_.contentId,
  412. mtp: this.playerInterface_.getBandwidthEstimate() / 1000,
  413. };
  414. }
  415. /**
  416. * Apply CMCD data to a request.
  417. *
  418. * @param {!shaka.extern.Request} request The request to apply CMCD data to
  419. * @param {!CmcdData} data The data object
  420. * @param {boolean} useHeaders Send data via request headers
  421. * @private
  422. */
  423. apply_(request, data = {}, useHeaders = this.config_.useHeaders) {
  424. if (!this.config_.enabled) {
  425. return;
  426. }
  427. // apply baseline data
  428. Object.assign(data, this.createData_());
  429. data.pr = this.playerInterface_.getPlaybackRate();
  430. const isVideo = data.ot === shaka.util.CmcdManager.ObjectType.VIDEO ||
  431. data.ot === shaka.util.CmcdManager.ObjectType.MUXED;
  432. if (this.starved_ && isVideo) {
  433. data.bs = true;
  434. data.su = true;
  435. this.starved_ = false;
  436. }
  437. if (data.su == null) {
  438. data.su = this.buffering_;
  439. }
  440. if (data.v === shaka.util.CmcdManager.Version.VERSION_2) {
  441. if (this.playerInterface_.isLive()) {
  442. data.ltc = this.playerInterface_.getLiveLatency();
  443. }
  444. const msd = this.calculateMSD_();
  445. if (msd != undefined) {
  446. data.msd = msd;
  447. this.msdSent_ = true;
  448. }
  449. }
  450. const output = this.filterKeys_(data);
  451. if (useHeaders) {
  452. const headers = shaka.util.CmcdManager.toHeaders(output);
  453. if (!Object.keys(headers).length) {
  454. return;
  455. }
  456. Object.assign(request.headers, headers);
  457. } else {
  458. const query = shaka.util.CmcdManager.toQuery(output);
  459. if (!query) {
  460. return;
  461. }
  462. request.uris = request.uris.map((uri) => {
  463. return shaka.util.CmcdManager.appendQueryToUri(uri, query);
  464. });
  465. }
  466. }
  467. /**
  468. * Filter the CMCD data object to include only the keys specified in the
  469. * configuration.
  470. *
  471. * @param {CmcdData} data
  472. * @return {CmcdData}
  473. * @private
  474. */
  475. filterKeys_(data) {
  476. const includeKeys = this.config_.includeKeys;
  477. if (!includeKeys.length) {
  478. return data;
  479. }
  480. return Object.keys(data).reduce((acc, key) => {
  481. if (includeKeys.includes(key)) {
  482. acc[key] = data[key];
  483. }
  484. return acc;
  485. }, {});
  486. }
  487. /**
  488. * The CMCD object type.
  489. *
  490. * @param {shaka.extern.RequestContext} context
  491. * The request context
  492. * @return {shaka.util.CmcdManager.ObjectType|undefined}
  493. * @private
  494. */
  495. getObjectType_(context) {
  496. if (context.type ===
  497. shaka.net.NetworkingEngine.AdvancedRequestType.INIT_SEGMENT) {
  498. return shaka.util.CmcdManager.ObjectType.INIT;
  499. }
  500. const stream = context.stream;
  501. if (!stream) {
  502. return undefined;
  503. }
  504. const type = stream.type;
  505. if (type == 'video') {
  506. if (stream.codecs && stream.codecs.includes(',')) {
  507. return shaka.util.CmcdManager.ObjectType.MUXED;
  508. }
  509. return shaka.util.CmcdManager.ObjectType.VIDEO;
  510. }
  511. if (type == 'audio') {
  512. return shaka.util.CmcdManager.ObjectType.AUDIO;
  513. }
  514. if (type == 'text') {
  515. if (stream.mimeType === 'application/mp4') {
  516. return shaka.util.CmcdManager.ObjectType.TIMED_TEXT;
  517. }
  518. return shaka.util.CmcdManager.ObjectType.CAPTION;
  519. }
  520. return undefined;
  521. }
  522. /**
  523. * The CMCD object type from mimeType.
  524. *
  525. * @param {!string} mimeType
  526. * @return {(shaka.util.CmcdManager.ObjectType|undefined)}
  527. * @private
  528. */
  529. getObjectTypeFromMimeType_(mimeType) {
  530. switch (mimeType.toLowerCase()) {
  531. case 'audio/mp4':
  532. case 'audio/webm':
  533. case 'audio/ogg':
  534. case 'audio/mpeg':
  535. case 'audio/aac':
  536. case 'audio/flac':
  537. case 'audio/wav':
  538. return shaka.util.CmcdManager.ObjectType.AUDIO;
  539. case 'video/webm':
  540. case 'video/mp4':
  541. case 'video/mpeg':
  542. case 'video/mp2t':
  543. return shaka.util.CmcdManager.ObjectType.MUXED;
  544. case 'application/x-mpegurl':
  545. case 'application/vnd.apple.mpegurl':
  546. case 'application/dash+xml':
  547. case 'video/vnd.mpeg.dash.mpd':
  548. case 'application/vnd.ms-sstr+xml':
  549. return shaka.util.CmcdManager.ObjectType.MANIFEST;
  550. default:
  551. return undefined;
  552. }
  553. }
  554. /**
  555. * Get the buffer length for a media type in milliseconds
  556. *
  557. * @param {string} type
  558. * @return {number}
  559. * @private
  560. */
  561. getBufferLength_(type) {
  562. const ranges = this.playerInterface_.getBufferedInfo()[type];
  563. if (!ranges.length) {
  564. return NaN;
  565. }
  566. const start = this.playerInterface_.getCurrentTime();
  567. const range = ranges.find((r) => r.start <= start && r.end >= start);
  568. if (!range) {
  569. return NaN;
  570. }
  571. return (range.end - start) * 1000;
  572. }
  573. /**
  574. * Get the remaining buffer length for a media type in milliseconds
  575. *
  576. * @param {string} type
  577. * @return {number}
  578. * @private
  579. */
  580. getRemainingBufferLength_(type) {
  581. const ranges = this.playerInterface_.getBufferedInfo()[type];
  582. if (!ranges.length) {
  583. return 0;
  584. }
  585. const start = this.playerInterface_.getCurrentTime();
  586. const range = ranges.find((r) => r.start <= start && r.end >= start);
  587. if (!range) {
  588. return 0;
  589. }
  590. return (range.end - start) * 1000;
  591. }
  592. /**
  593. * Constructs a relative path from a URL
  594. *
  595. * @param {string} url
  596. * @param {string} base
  597. * @return {string}
  598. * @private
  599. */
  600. urlToRelativePath_(url, base) {
  601. const to = new URL(url);
  602. const from = new URL(base);
  603. if (to.origin !== from.origin) {
  604. return url;
  605. }
  606. const toPath = to.pathname.split('/').slice(1);
  607. const fromPath = from.pathname.split('/').slice(1, -1);
  608. // remove common parents
  609. while (toPath[0] === fromPath[0]) {
  610. toPath.shift();
  611. fromPath.shift();
  612. }
  613. // add back paths
  614. while (fromPath.length) {
  615. fromPath.shift();
  616. toPath.unshift('..');
  617. }
  618. return toPath.join('/');
  619. }
  620. /**
  621. * Calculate measured start delay
  622. *
  623. * @return {number|undefined}
  624. * @private
  625. */
  626. calculateMSD_() {
  627. if (!this.msdSent_ &&
  628. this.playbackPlayingTime_ &&
  629. this.playbackPlayTime_) {
  630. const startTime = this.startTimeOfLoad_ || this.playbackPlayTime_;
  631. return this.playbackPlayingTime_ - startTime;
  632. }
  633. return undefined;
  634. }
  635. /**
  636. * Calculate requested maximum throughput
  637. *
  638. * @param {shaka.extern.Stream} stream
  639. * @param {shaka.media.SegmentReference} segment
  640. * @return {number}
  641. * @private
  642. */
  643. calculateRtp_(stream, segment) {
  644. const playbackRate = this.playerInterface_.getPlaybackRate() || 1;
  645. const currentBufferLevel =
  646. this.getRemainingBufferLength_(stream.type) || 500;
  647. const bandwidth = stream.bandwidth;
  648. if (!bandwidth) {
  649. return NaN;
  650. }
  651. const segmentDuration = segment.endTime - segment.startTime;
  652. // Calculate file size in kilobits
  653. const segmentSize = bandwidth * segmentDuration / 1000;
  654. // Calculate time available to load file in seconds
  655. const timeToLoad = (currentBufferLevel / playbackRate) / 1000;
  656. // Calculate the exact bandwidth required
  657. const minBandwidth = segmentSize / timeToLoad;
  658. // Include a safety buffer
  659. return minBandwidth * this.config_.rtpSafetyFactor;
  660. }
  661. /**
  662. * Get the stream format
  663. *
  664. * @param {shaka.net.NetworkingEngine.AdvancedRequestType} type
  665. * The request's advanced type
  666. * @return {(shaka.util.CmcdManager.StreamingFormat|undefined)}
  667. * @private
  668. */
  669. getStreamFormat_(type) {
  670. const AdvancedRequestType = shaka.net.NetworkingEngine.AdvancedRequestType;
  671. switch (type) {
  672. case AdvancedRequestType.MPD:
  673. if (this.lowLatency_) {
  674. return shaka.util.CmcdManager.StreamingFormat.LOW_LATENCY_DASH;
  675. }
  676. return shaka.util.CmcdManager.StreamingFormat.DASH;
  677. case AdvancedRequestType.MASTER_PLAYLIST:
  678. case AdvancedRequestType.MEDIA_PLAYLIST:
  679. if (this.lowLatency_) {
  680. return shaka.util.CmcdManager.StreamingFormat.LOW_LATENCY_HLS;
  681. }
  682. return shaka.util.CmcdManager.StreamingFormat.HLS;
  683. case AdvancedRequestType.MSS:
  684. return shaka.util.CmcdManager.StreamingFormat.SMOOTH;
  685. }
  686. return undefined;
  687. }
  688. /**
  689. * Get the stream type
  690. *
  691. * @return {shaka.util.CmcdManager.StreamType}
  692. * @private
  693. */
  694. getStreamType_() {
  695. const isLive = this.playerInterface_.isLive();
  696. if (isLive) {
  697. return shaka.util.CmcdManager.StreamType.LIVE;
  698. } else {
  699. return shaka.util.CmcdManager.StreamType.VOD;
  700. }
  701. }
  702. /**
  703. * Get the highest bandwidth for a given type.
  704. *
  705. * @param {shaka.util.CmcdManager.ObjectType|undefined} type
  706. * @return {number}
  707. * @private
  708. */
  709. getTopBandwidth_(type) {
  710. const variants = this.playerInterface_.getVariantTracks();
  711. if (!variants.length) {
  712. return NaN;
  713. }
  714. let top = variants[0];
  715. for (const variant of variants) {
  716. if (variant.type === 'variant' && variant.bandwidth > top.bandwidth) {
  717. top = variant;
  718. }
  719. }
  720. const ObjectType = shaka.util.CmcdManager.ObjectType;
  721. switch (type) {
  722. case ObjectType.VIDEO:
  723. return top.videoBandwidth || NaN;
  724. case ObjectType.AUDIO:
  725. return top.audioBandwidth || NaN;
  726. default:
  727. return top.bandwidth;
  728. }
  729. }
  730. /**
  731. * Serialize a CMCD data object according to the rules defined in the
  732. * section 3.2 of
  733. * [CTA-5004](https://cdn.cta.tech/cta/media/media/resources/standards/pdfs/cta-5004-final.pdf).
  734. *
  735. * @param {CmcdData} data The CMCD data object
  736. * @return {string}
  737. */
  738. static serialize(data) {
  739. const results = [];
  740. const isValid = (value) =>
  741. !Number.isNaN(value) && value != null && value !== '' && value !== false;
  742. const toRounded = (value) => Math.round(value);
  743. const toHundred = (value) => toRounded(value / 100) * 100;
  744. const toUrlSafe = (value) => encodeURIComponent(value);
  745. const formatters = {
  746. br: toRounded,
  747. d: toRounded,
  748. bl: toHundred,
  749. dl: toHundred,
  750. mtp: toHundred,
  751. nor: toUrlSafe,
  752. rtp: toHundred,
  753. tb: toRounded,
  754. };
  755. const keys = Object.keys(data || {}).sort();
  756. for (const key of keys) {
  757. let value = data[key];
  758. // ignore invalid values
  759. if (!isValid(value)) {
  760. continue;
  761. }
  762. // Version should only be reported if not equal to 1.
  763. if (key === 'v' && value === 1) {
  764. continue;
  765. }
  766. // Playback rate should only be sent if not equal to 1.
  767. if (key == 'pr' && value === 1) {
  768. continue;
  769. }
  770. // Certain values require special formatting
  771. const formatter = formatters[key];
  772. if (formatter) {
  773. value = formatter(value);
  774. }
  775. // Serialize the key/value pair
  776. const type = typeof value;
  777. let result;
  778. if (type === 'string' && key !== 'ot' && key !== 'sf' && key !== 'st') {
  779. result = `${key}=${JSON.stringify(value)}`;
  780. } else if (type === 'boolean') {
  781. result = key;
  782. } else if (type === 'symbol') {
  783. result = `${key}=${value.description}`;
  784. } else {
  785. result = `${key}=${value}`;
  786. }
  787. results.push(result);
  788. }
  789. return results.join(',');
  790. }
  791. /**
  792. * Convert a CMCD data object to request headers according to the rules
  793. * defined in the section 2.1 and 3.2 of
  794. * [CTA-5004](https://cdn.cta.tech/cta/media/media/resources/standards/pdfs/cta-5004-final.pdf).
  795. *
  796. * @param {CmcdData} data The CMCD data object
  797. * @return {!Object}
  798. */
  799. static toHeaders(data) {
  800. const keys = Object.keys(data);
  801. const headers = {};
  802. const headerNames = ['Object', 'Request', 'Session', 'Status'];
  803. const headerGroups = [{}, {}, {}, {}];
  804. const headerMap = {
  805. br: 0, d: 0, ot: 0, tb: 0,
  806. bl: 1, dl: 1, mtp: 1, nor: 1, nrr: 1, su: 1, ltc: 1,
  807. cid: 2, pr: 2, sf: 2, sid: 2, st: 2, v: 2, msd: 2,
  808. bs: 3, rtp: 3,
  809. };
  810. for (const key of keys) {
  811. // Unmapped fields are mapped to the Request header
  812. const index = (headerMap[key] != null) ? headerMap[key] : 1;
  813. headerGroups[index][key] = data[key];
  814. }
  815. for (let i = 0; i < headerGroups.length; i++) {
  816. const value = shaka.util.CmcdManager.serialize(headerGroups[i]);
  817. if (value) {
  818. headers[`CMCD-${headerNames[i]}`] = value;
  819. }
  820. }
  821. return headers;
  822. }
  823. /**
  824. * Convert a CMCD data object to query args according to the rules
  825. * defined in the section 2.2 and 3.2 of
  826. * [CTA-5004](https://cdn.cta.tech/cta/media/media/resources/standards/pdfs/cta-5004-final.pdf).
  827. *
  828. * @param {CmcdData} data The CMCD data object
  829. * @return {string}
  830. */
  831. static toQuery(data) {
  832. return shaka.util.CmcdManager.serialize(data);
  833. }
  834. /**
  835. * Append query args to a uri.
  836. *
  837. * @param {string} uri
  838. * @param {string} query
  839. * @return {string}
  840. */
  841. static appendQueryToUri(uri, query) {
  842. if (!query) {
  843. return uri;
  844. }
  845. if (uri.includes('offline:')) {
  846. return uri;
  847. }
  848. const url = new goog.Uri(uri);
  849. url.getQueryData().set('CMCD', query);
  850. return url.toString();
  851. }
  852. };
  853. /**
  854. * @typedef {{
  855. * getBandwidthEstimate: function():number,
  856. * getBufferedInfo: function():shaka.extern.BufferedInfo,
  857. * getCurrentTime: function():number,
  858. * getPlaybackRate: function():number,
  859. * getVariantTracks: function():Array<shaka.extern.Track>,
  860. * isLive: function():boolean,
  861. * getLiveLatency: function():number
  862. * }}
  863. *
  864. * @property {function():number} getBandwidthEstimate
  865. * Get the estimated bandwidth in bits per second.
  866. * @property {function():shaka.extern.BufferedInfo} getBufferedInfo
  867. * Get information about what the player has buffered.
  868. * @property {function():number} getCurrentTime
  869. * Get the current time
  870. * @property {function():number} getPlaybackRate
  871. * Get the playback rate
  872. * @property {function():Array<shaka.extern.Track>} getVariantTracks
  873. * Get the variant tracks
  874. * @property {function():boolean} isLive
  875. * Get if the player is playing live content.
  876. * @property {function():number} getLiveLatency
  877. * Get latency in milliseconds between the live edge and what's currently
  878. * playing.
  879. */
  880. shaka.util.CmcdManager.PlayerInterface;
  881. /**
  882. * @enum {string}
  883. */
  884. shaka.util.CmcdManager.ObjectType = {
  885. MANIFEST: 'm',
  886. AUDIO: 'a',
  887. VIDEO: 'v',
  888. MUXED: 'av',
  889. INIT: 'i',
  890. CAPTION: 'c',
  891. TIMED_TEXT: 'tt',
  892. KEY: 'k',
  893. OTHER: 'o',
  894. };
  895. /**
  896. * @enum {number}
  897. */
  898. shaka.util.CmcdManager.Version = {
  899. VERSION_1: 1,
  900. VERSION_2: 2,
  901. };
  902. /**
  903. * @enum {string}
  904. */
  905. shaka.util.CmcdManager.StreamType = {
  906. VOD: 'v',
  907. LIVE: 'l',
  908. };
  909. /**
  910. * @enum {string}
  911. * @export
  912. */
  913. shaka.util.CmcdManager.StreamingFormat = {
  914. DASH: 'd',
  915. LOW_LATENCY_DASH: 'ld',
  916. HLS: 'h',
  917. LOW_LATENCY_HLS: 'lh',
  918. SMOOTH: 's',
  919. OTHER: 'o',
  920. };