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.requireType('shaka.media.InitSegmentReference');
  9. goog.requireType('shaka.media.SegmentReference');
  10. /**
  11. * An object that represents a single segment, that the storage system will soon
  12. * download, but has not yet started downloading.
  13. */
  14. shaka.offline.DownloadInfo = class {
  15. /**
  16. * @param {shaka.media.SegmentReference|shaka.media.InitSegmentReference} ref
  17. * @param {number} estimateId
  18. * @param {number} groupId
  19. * @param {boolean} isInitSegment
  20. */
  21. constructor(ref, estimateId, groupId, isInitSegment) {
  22. /** @type {shaka.media.SegmentReference|shaka.media.InitSegmentReference} */
  23. this.ref = ref;
  24. /** @type {number} */
  25. this.estimateId = estimateId;
  26. /** @type {number} */
  27. this.groupId = groupId;
  28. /** @type {boolean} */
  29. this.isInitSegment = isInitSegment;
  30. }
  31. /**
  32. * Creates an ID that encapsulates all important information in the ref, which
  33. * can then be used to check for equality.
  34. * @param {shaka.media.SegmentReference|shaka.media.InitSegmentReference} ref
  35. * @return {string}
  36. */
  37. static idForSegmentRef(ref) {
  38. // Escape the URIs using encodeURI, to make sure that a weirdly formed URI
  39. // cannot cause two unrelated refs to be considered equivalent.
  40. return ref.getUris().map((uri) => '{' + encodeURI(uri) + '}').join('') +
  41. ':' + ref.startByte + ':' + ref.endByte;
  42. }
  43. /** @return {string} */
  44. getRefId() {
  45. return shaka.offline.DownloadInfo.idForSegmentRef(this.ref);
  46. }
  47. /**
  48. * @param {shaka.extern.PlayerConfiguration} config
  49. * @return {!shaka.extern.Request}
  50. */
  51. makeSegmentRequest(config) {
  52. return shaka.util.Networking.createSegmentRequest(
  53. this.ref.getUris(),
  54. this.ref.startByte,
  55. this.ref.endByte,
  56. config.streaming.retryParameters);
  57. }
  58. };