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