Source: lib/offline/stored_content_utils.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.offline.StoredContentUtils');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.media.PresentationTimeline');
  20. goog.require('shaka.offline.ManifestConverter');
  21. goog.require('shaka.offline.OfflineUri');
  22. goog.require('shaka.util.StreamUtils');
  23. /**
  24. * A utility class used to create |shakaExtern.StoredContent| from different
  25. * types of input.
  26. */
  27. shaka.offline.StoredContentUtils = class {
  28. /**
  29. * @param {string} originalUri
  30. * @param {shakaExtern.Manifest} manifest
  31. * @param {number} size
  32. * @param {!Object} metadata
  33. * @return {shakaExtern.StoredContent}
  34. */
  35. static fromManifest(originalUri, manifest, size, metadata) {
  36. goog.asserts.assert(
  37. manifest.periods.length,
  38. 'Cannot create stored content from manifest with no periods.');
  39. /** @type {number} */
  40. let expiration = manifest.expiration == undefined ?
  41. Infinity :
  42. manifest.expiration;
  43. /** @type {number} */
  44. let duration = manifest.presentationTimeline.getDuration();
  45. /** @type {shakaExtern.Period} */
  46. let firstPeriod = manifest.periods[0];
  47. /** @type {!Array.<shakaExtern.Track>} */
  48. let tracks = shaka.util.StreamUtils.getTracks(firstPeriod);
  49. /** @type {shakaExtern.StoredContent} */
  50. let content = {
  51. offlineUri: null,
  52. originalManifestUri: originalUri,
  53. duration: duration,
  54. size: size,
  55. expiration: expiration,
  56. tracks: tracks,
  57. appMetadata: metadata
  58. };
  59. return content;
  60. }
  61. /**
  62. * @param {!shaka.offline.OfflineUri} offlineUri
  63. * @param {shakaExtern.ManifestDB} manifestDB
  64. * @return {shakaExtern.StoredContent}
  65. */
  66. static fromManifestDB(offlineUri, manifestDB) {
  67. goog.asserts.assert(
  68. manifestDB.periods.length,
  69. 'Cannot create stored content from manifestDB with no periods.');
  70. let converter = new shaka.offline.ManifestConverter(
  71. offlineUri.mechanism(), offlineUri.cell());
  72. /** @type {shakaExtern.PeriodDB} */
  73. let firstPeriodDB = manifestDB.periods[0];
  74. /** @type {!shaka.media.PresentationTimeline} */
  75. let timeline = new shaka.media.PresentationTimeline(null, 0);
  76. /** @type {shakaExtern.Period} */
  77. let firstPeriod = converter.fromPeriodDB(firstPeriodDB, timeline);
  78. /** @type {!Object} */
  79. let metadata = manifestDB.appMetadata || {};
  80. /** @type {!Array.<shakaExtern.Track>} */
  81. let tracks = shaka.util.StreamUtils.getTracks(firstPeriod);
  82. /** @type {shakaExtern.StoredContent} */
  83. let content = {
  84. offlineUri: offlineUri.toString(),
  85. originalManifestUri: manifestDB.originalManifestUri,
  86. duration: manifestDB.duration,
  87. size: manifestDB.size,
  88. expiration: manifestDB.expiration,
  89. tracks: tracks,
  90. appMetadata: metadata
  91. };
  92. return content;
  93. }
  94. };