Source: lib/offline/offline_manifest_parser.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.OfflineManifestParser');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.log');
  20. goog.require('shaka.media.ManifestParser');
  21. goog.require('shaka.offline.ManifestConverter');
  22. goog.require('shaka.offline.OfflineUri');
  23. goog.require('shaka.offline.StorageMuxer');
  24. goog.require('shaka.util.Error');
  25. goog.require('shaka.util.IDestroyable');
  26. /**
  27. * Creates a new offline manifest parser.
  28. * @struct
  29. * @constructor
  30. * @implements {shakaExtern.ManifestParser}
  31. */
  32. shaka.offline.OfflineManifestParser = function() {
  33. /** @private {shaka.offline.OfflineUri} */
  34. this.uri_ = null;
  35. };
  36. /** @override */
  37. shaka.offline.OfflineManifestParser.prototype.configure = function(config) {
  38. // No-op
  39. };
  40. /** @override */
  41. shaka.offline.OfflineManifestParser.prototype.start =
  42. function(uriString, playerInterface) {
  43. let uri = shaka.offline.OfflineUri.parse(uriString);
  44. this.uri_ = uri;
  45. if (uri == null || !uri.isManifest()) {
  46. return Promise.reject(new shaka.util.Error(
  47. shaka.util.Error.Severity.CRITICAL,
  48. shaka.util.Error.Category.NETWORK,
  49. shaka.util.Error.Code.MALFORMED_OFFLINE_URI,
  50. uri));
  51. }
  52. let muxer = new shaka.offline.StorageMuxer();
  53. return shaka.util.IDestroyable.with([muxer], async () => {
  54. await muxer.init();
  55. let cell = await muxer.getCell(uri.mechanism(), uri.cell());
  56. let manifests = await cell.getManifests([uri.key()]);
  57. let manifest = manifests[0];
  58. let converter = new shaka.offline.ManifestConverter(
  59. uri.mechanism(), uri.cell());
  60. return converter.fromManifestDB(manifest);
  61. });
  62. };
  63. /** @override */
  64. shaka.offline.OfflineManifestParser.prototype.stop = function() {
  65. return Promise.resolve();
  66. };
  67. /** @override */
  68. shaka.offline.OfflineManifestParser.prototype.update = function() {
  69. // No-op
  70. };
  71. /** @override */
  72. shaka.offline.OfflineManifestParser.prototype.onExpirationUpdated = function(
  73. sessionId, expiration) {
  74. let uri = this.uri_;
  75. goog.asserts.assert(
  76. uri, 'Should not get update event before start has been called');
  77. let muxer = new shaka.offline.StorageMuxer();
  78. return shaka.util.IDestroyable.with([muxer], async () => {
  79. await muxer.init();
  80. let cell = await muxer.getCell(uri.mechanism(), uri.cell());
  81. let manifests = await cell.getManifests([uri.key()]);
  82. let manifest = manifests[0];
  83. let foundSession = manifest.sessionIds.indexOf(sessionId) >= 0;
  84. let newExpiration = manifest.expiration == undefined ||
  85. manifest.expiration > expiration;
  86. if (foundSession && newExpiration) {
  87. shaka.log.debug('Updating expiration for stored content');
  88. await cell.updateManifestExpiration(uri.key(), expiration);
  89. }
  90. }).catch((e) => {
  91. // Ignore errors with update.
  92. shaka.log.error('There was an error updating', uri, e);
  93. });
  94. };
  95. shaka.media.ManifestParser.registerParserByMime(
  96. 'application/x-offline-manifest', shaka.offline.OfflineManifestParser);