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. };
  44. return content;
  45. }
  46. /**
  47. * @param {!shaka.offline.OfflineUri} offlineUri
  48. * @param {shaka.extern.ManifestDB} manifestDB
  49. * @return {shaka.extern.StoredContent}
  50. */
  51. static fromManifestDB(offlineUri, manifestDB) {
  52. goog.asserts.assert(
  53. manifestDB.streams.length,
  54. 'Cannot create stored content from manifestDB with no streams.');
  55. const converter = new shaka.offline.ManifestConverter(
  56. offlineUri.mechanism(), offlineUri.cell());
  57. /** @type {shaka.extern.Manifest} */
  58. const manifest = converter.fromManifestDB(manifestDB);
  59. /** @type {!Object} */
  60. const metadata = manifestDB.appMetadata || {};
  61. /** @type {!Array.<shaka.extern.Track>} */
  62. const tracks = shaka.offline.StoredContentUtils.getTracks_(manifest);
  63. goog.asserts.assert(
  64. manifestDB.expiration != null,
  65. 'Manifest expiration must be set by now!');
  66. /** @type {shaka.extern.StoredContent} */
  67. const content = {
  68. offlineUri: offlineUri.toString(),
  69. originalManifestUri: manifestDB.originalManifestUri,
  70. duration: manifestDB.duration,
  71. size: manifestDB.size,
  72. expiration: manifestDB.expiration,
  73. tracks: tracks,
  74. appMetadata: metadata,
  75. };
  76. return content;
  77. }
  78. /**
  79. * Gets track representations of all playable variants and all text streams.
  80. *
  81. * @param {shaka.extern.Manifest} manifest
  82. * @return {!Array.<shaka.extern.Track>}
  83. * @private
  84. */
  85. static getTracks_(manifest) {
  86. const StreamUtils = shaka.util.StreamUtils;
  87. const tracks = [];
  88. const variants = StreamUtils.getPlayableVariants(manifest.variants);
  89. for (const variant of variants) {
  90. tracks.push(StreamUtils.variantToTrack(variant));
  91. }
  92. const textStreams = manifest.textStreams;
  93. for (const stream of textStreams) {
  94. tracks.push(StreamUtils.textStreamToTrack(stream));
  95. }
  96. return tracks;
  97. }
  98. };