Source: lib/offline/download_info.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.offline.DownloadInfo');
  7. goog.require('shaka.util.Networking');
  8. goog.require('shaka.util.Uint8ArrayUtils');
  9. goog.requireType('shaka.media.InitSegmentReference');
  10. goog.requireType('shaka.media.SegmentReference');
  11. /**
  12. * An object that represents a single segment, that the storage system will soon
  13. * download, but has not yet started downloading.
  14. */
  15. shaka.offline.DownloadInfo = class {
  16. /**
  17. * @param {shaka.media.SegmentReference|shaka.media.InitSegmentReference} ref
  18. * @param {number} estimateId
  19. * @param {number} groupId
  20. * @param {boolean} isInitSegment
  21. * @param {number} refPosition
  22. */
  23. constructor(ref, estimateId, groupId, isInitSegment, refPosition) {
  24. /** @type {shaka.media.SegmentReference|shaka.media.InitSegmentReference} */
  25. this.ref = ref;
  26. /** @type {number} */
  27. this.estimateId = estimateId;
  28. /** @type {number} */
  29. this.groupId = groupId;
  30. /** @type {boolean} */
  31. this.isInitSegment = isInitSegment;
  32. /** @type {number} */
  33. this.refPosition = refPosition;
  34. }
  35. /**
  36. * Creates an ID that encapsulates all important information in the ref, which
  37. * can then be used to check for equality.
  38. * @param {shaka.media.SegmentReference|shaka.media.InitSegmentReference} ref
  39. * @return {string}
  40. */
  41. static idForSegmentRef(ref) {
  42. const segmentData = ref.getSegmentData();
  43. if (segmentData) {
  44. return shaka.util.Uint8ArrayUtils.toBase64(segmentData);
  45. }
  46. // Escape the URIs using encodeURI, to make sure that a weirdly formed URI
  47. // cannot cause two unrelated refs to be considered equivalent.
  48. const removeSprites = (uri) => {
  49. return uri.split('#xywh=')[0];
  50. };
  51. return ref.getUris().map(
  52. (uri) => '{' + encodeURI(removeSprites(uri)) + '}').join('') +
  53. ':' + ref.startByte + ':' + ref.endByte;
  54. }
  55. /** @return {string} */
  56. getRefId() {
  57. return shaka.offline.DownloadInfo.idForSegmentRef(this.ref);
  58. }
  59. /**
  60. * @param {shaka.extern.PlayerConfiguration} config
  61. * @return {!shaka.extern.Request}
  62. */
  63. makeSegmentRequest(config) {
  64. return shaka.util.Networking.createSegmentRequest(
  65. this.ref.getUris(),
  66. this.ref.startByte,
  67. this.ref.endByte,
  68. config.streaming.retryParameters);
  69. }
  70. };