Source: lib/offline/stored_content_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.offline.StoredContentUtils');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.offline.ManifestConverter');
  9. goog.require('shaka.offline.OfflineUri');
  10. goog.require('shaka.util.StreamUtils');
  11. /**
  12. * A utility class used to create |shaka.extern.StoredContent| from different
  13. * types of input.
  14. */
  15. shaka.offline.StoredContentUtils = class {
  16. /**
  17. * @param {string} originalUri
  18. * @param {shaka.extern.Manifest} manifest
  19. * @param {number} size
  20. * @param {!Object} metadata
  21. * @return {shaka.extern.StoredContent}
  22. */
  23. static fromManifest(originalUri, manifest, size, metadata) {
  24. goog.asserts.assert(
  25. manifest.variants.length,
  26. 'Cannot create stored content from manifest with no variants.');
  27. /** @type {number} */
  28. const duration = manifest.presentationTimeline.getDuration();
  29. /** @type {!Array.<shaka.extern.Track>} */
  30. const tracks = shaka.offline.StoredContentUtils.getTracks_(manifest);
  31. /** @type {shaka.extern.StoredContent} */
  32. const content = {
  33. offlineUri: null,
  34. originalManifestUri: originalUri,
  35. duration: duration,
  36. size: size,
  37. // This expiration value is temporary and will be used in progress reports
  38. // during the storage process. The real value would have to come from
  39. // DrmEngine.
  40. expiration: Infinity,
  41. tracks: tracks,
  42. appMetadata: metadata,
  43. isIncomplete: false,
  44. };
  45. return content;
  46. }
  47. /**
  48. * @param {!shaka.offline.OfflineUri} offlineUri
  49. * @param {shaka.extern.ManifestDB} manifestDB
  50. * @return {shaka.extern.StoredContent}
  51. */
  52. static fromManifestDB(offlineUri, manifestDB) {
  53. goog.asserts.assert(
  54. manifestDB.streams.length,
  55. 'Cannot create stored content from manifestDB with no streams.');
  56. const converter = new shaka.offline.ManifestConverter(
  57. offlineUri.mechanism(), offlineUri.cell());
  58. /** @type {shaka.extern.Manifest} */
  59. const manifest = converter.fromManifestDB(manifestDB);
  60. /** @type {!Object} */
  61. const metadata = manifestDB.appMetadata || {};
  62. /** @type {!Array.<shaka.extern.Track>} */
  63. const tracks = shaka.offline.StoredContentUtils.getTracks_(manifest);
  64. goog.asserts.assert(
  65. manifestDB.expiration != null,
  66. 'Manifest expiration must be set by now!');
  67. /** @type {shaka.extern.StoredContent} */
  68. const content = {
  69. offlineUri: offlineUri.toString(),
  70. originalManifestUri: manifestDB.originalManifestUri,
  71. duration: manifestDB.duration,
  72. size: manifestDB.size,
  73. expiration: manifestDB.expiration,
  74. tracks: tracks,
  75. appMetadata: metadata,
  76. isIncomplete: (manifestDB.isIncomplete || false),
  77. };
  78. return content;
  79. }
  80. /**
  81. * Gets track representations of all playable variants and all text streams.
  82. *
  83. * @param {shaka.extern.Manifest} manifest
  84. * @return {!Array.<shaka.extern.Track>}
  85. * @private
  86. */
  87. static getTracks_(manifest) {
  88. const StreamUtils = shaka.util.StreamUtils;
  89. const tracks = [];
  90. const variants = StreamUtils.getPlayableVariants(manifest.variants);
  91. for (const variant of variants) {
  92. tracks.push(StreamUtils.variantToTrack(variant));
  93. }
  94. const textStreams = manifest.textStreams;
  95. for (const stream of textStreams) {
  96. tracks.push(StreamUtils.textStreamToTrack(stream));
  97. }
  98. return tracks;
  99. }
  100. };