Source: lib/offline/storage.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.Storage');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.Player');
  20. goog.require('shaka.log');
  21. goog.require('shaka.media.DrmEngine');
  22. goog.require('shaka.media.ManifestParser');
  23. goog.require('shaka.offline.DownloadManager');
  24. goog.require('shaka.offline.IStorageEngine');
  25. goog.require('shaka.offline.OfflineManifestParser');
  26. goog.require('shaka.offline.OfflineUtils');
  27. goog.require('shaka.util.ConfigUtils');
  28. goog.require('shaka.util.Error');
  29. goog.require('shaka.util.Functional');
  30. goog.require('shaka.util.IDestroyable');
  31. goog.require('shaka.util.LanguageUtils');
  32. goog.require('shaka.util.ManifestParserUtils');
  33. goog.require('shaka.util.StreamUtils');
  34. /**
  35. * This manages persistent offline data including storage, listing, and deleting
  36. * stored manifests. Playback of offline manifests are done using Player
  37. * using the special URI (e.g. 'offline:12').
  38. *
  39. * First, check support() to see if offline is supported by the platform.
  40. * Second, configure() the storage object with callbacks to your application.
  41. * Third, call store(), remove(), or list() as needed.
  42. * When done, call destroy().
  43. *
  44. * @param {shaka.Player} player
  45. * The player instance to pull configuration data from.
  46. *
  47. * @struct
  48. * @constructor
  49. * @implements {shaka.util.IDestroyable}
  50. * @export
  51. */
  52. shaka.offline.Storage = function(player) {
  53. // It is an easy mistake to make to pass a Player proxy from CastProxy.
  54. // Rather than throw a vague exception later, throw an explicit and clear one
  55. // now.
  56. if (!player || player.constructor != shaka.Player) {
  57. throw new shaka.util.Error(
  58. shaka.util.Error.Severity.CRITICAL,
  59. shaka.util.Error.Category.STORAGE,
  60. shaka.util.Error.Code.LOCAL_PLAYER_INSTANCE_REQUIRED);
  61. }
  62. /** @private {shaka.offline.IStorageEngine} */
  63. this.storageEngine_ = shaka.offline.OfflineUtils.createStorageEngine();
  64. /** @private {shaka.Player} */
  65. this.player_ = player;
  66. /** @private {?shakaExtern.OfflineConfiguration} */
  67. this.config_ = this.defaultConfig_();
  68. /** @private {shaka.media.DrmEngine} */
  69. this.drmEngine_ = null;
  70. /** @private {boolean} */
  71. this.storeInProgress_ = false;
  72. /** @private {Array.<shakaExtern.Track>} */
  73. this.firstPeriodTracks_ = null;
  74. /** @private {number} */
  75. this.manifestId_ = -1;
  76. /** @private {number} */
  77. this.duration_ = 0;
  78. /** @private {?shakaExtern.Manifest} */
  79. this.manifest_ = null;
  80. var netEngine = player.getNetworkingEngine();
  81. goog.asserts.assert(netEngine, 'Player must not be destroyed');
  82. /** @private {shaka.offline.DownloadManager} */
  83. this.downloadManager_ = new shaka.offline.DownloadManager(
  84. this.storageEngine_, netEngine,
  85. player.getConfiguration().streaming.retryParameters, this.config_);
  86. };
  87. /**
  88. * Gets whether offline storage is supported. Returns true if offline storage
  89. * is supported for clear content. Support for offline storage of encrypted
  90. * content will not be determined until storage is attempted.
  91. *
  92. * @return {boolean}
  93. * @export
  94. */
  95. shaka.offline.Storage.support = function() {
  96. return shaka.offline.OfflineUtils.isStorageEngineSupported();
  97. };
  98. /**
  99. * @override
  100. * @export
  101. */
  102. shaka.offline.Storage.prototype.destroy = function() {
  103. var storageEngine = this.storageEngine_;
  104. // Destroy the download manager first since it needs the StorageEngine to
  105. // clean up old segments.
  106. var ret = !this.downloadManager_ ?
  107. Promise.resolve() :
  108. this.downloadManager_.destroy()
  109. .catch(function() {})
  110. .then(function() {
  111. if (storageEngine) return storageEngine.destroy();
  112. });
  113. this.storageEngine_ = null;
  114. this.downloadManager_ = null;
  115. this.player_ = null;
  116. this.config_ = null;
  117. return ret;
  118. };
  119. /**
  120. * Sets configuration values for Storage. This is not associated with
  121. * Player.configure and will not change Player.
  122. *
  123. * There are two important callbacks configured here: one for download progress,
  124. * and one to decide which tracks to store.
  125. *
  126. * The default track selection callback will store the largest SD video track.
  127. * Provide your own callback to choose the tracks you want to store.
  128. *
  129. * @param {!Object} config This should follow the form of
  130. * {@link shakaExtern.OfflineConfiguration}, but you may omit any field you do
  131. * not wish to change.
  132. * @export
  133. */
  134. shaka.offline.Storage.prototype.configure = function(config) {
  135. goog.asserts.assert(this.config_, 'Storage must not be destroyed');
  136. shaka.util.ConfigUtils.mergeConfigObjects(
  137. this.config_, config, this.defaultConfig_(), {}, '');
  138. };
  139. /**
  140. * Stores the given manifest. If the content is encrypted, and encrypted
  141. * content cannot be stored on this platform, the Promise will be rejected with
  142. * error code 6001, REQUESTED_KEY_SYSTEM_CONFIG_UNAVAILABLE.
  143. *
  144. * @param {string} manifestUri The URI of the manifest to store.
  145. * @param {!Object=} opt_appMetadata An arbitrary object from the application
  146. * that will be stored along-side the offline content. Use this for any
  147. * application-specific metadata you need associated with the stored content.
  148. * For details on the data types that can be stored here, please refer to
  149. * https://goo.gl/h62coS
  150. * @param {!shakaExtern.ManifestParser.Factory=} opt_manifestParserFactory
  151. * @return {!Promise.<shakaExtern.StoredContent>} A Promise to a structure
  152. * representing what was stored. The "offlineUri" member is the URI that
  153. * should be given to Player.load() to play this piece of content offline.
  154. * The "appMetadata" member is the appMetadata argument you passed to store().
  155. * @export
  156. */
  157. shaka.offline.Storage.prototype.store = function(
  158. manifestUri, opt_appMetadata, opt_manifestParserFactory) {
  159. if (this.storeInProgress_) {
  160. return Promise.reject(new shaka.util.Error(
  161. shaka.util.Error.Severity.CRITICAL,
  162. shaka.util.Error.Category.STORAGE,
  163. shaka.util.Error.Code.STORE_ALREADY_IN_PROGRESS));
  164. }
  165. this.storeInProgress_ = true;
  166. /** @type {shakaExtern.ManifestDB} */
  167. var manifestDb;
  168. var error = null;
  169. var onError = function(e) { error = e; };
  170. return this.initIfNeeded_()
  171. .then(function() {
  172. this.checkDestroyed_();
  173. return this.loadInternal(
  174. manifestUri, onError, opt_manifestParserFactory);
  175. }.bind(this)).then((
  176. /**
  177. * @param {{manifest: shakaExtern.Manifest,
  178. * drmEngine: !shaka.media.DrmEngine}} data
  179. * @return {!Promise}
  180. */
  181. function(data) {
  182. this.checkDestroyed_();
  183. this.manifest_ = data.manifest;
  184. this.drmEngine_ = data.drmEngine;
  185. if (this.manifest_.presentationTimeline.isLive() ||
  186. this.manifest_.presentationTimeline.isInProgress()) {
  187. throw new shaka.util.Error(
  188. shaka.util.Error.Severity.CRITICAL,
  189. shaka.util.Error.Category.STORAGE,
  190. shaka.util.Error.Code.CANNOT_STORE_LIVE_OFFLINE, manifestUri);
  191. }
  192. // Re-filter now that DrmEngine is initialized.
  193. this.manifest_.periods.forEach(this.filterPeriod_.bind(this));
  194. this.manifestId_ = this.storageEngine_.reserveId('manifest');
  195. this.duration_ = 0;
  196. manifestDb =
  197. this.createOfflineManifest_(manifestUri, opt_appMetadata || {});
  198. return this.downloadManager_.downloadAndStore(manifestDb);
  199. })
  200. .bind(this))
  201. .then(function() {
  202. this.checkDestroyed_();
  203. // Throw any errors from the manifest parser or DrmEngine.
  204. if (error)
  205. throw error;
  206. return this.cleanup_();
  207. }.bind(this))
  208. .then(function() {
  209. return shaka.offline.OfflineUtils.getStoredContent(manifestDb);
  210. }.bind(this))
  211. .catch(function(err) {
  212. var Functional = shaka.util.Functional;
  213. return this.cleanup_().catch(Functional.noop).then(function() {
  214. throw err;
  215. });
  216. }.bind(this));
  217. };
  218. /**
  219. * Removes the given stored content.
  220. *
  221. * @param {shakaExtern.StoredContent} content
  222. * @return {!Promise}
  223. * @export
  224. */
  225. shaka.offline.Storage.prototype.remove = function(content) {
  226. var uri = content.offlineUri;
  227. var parts = /^offline:([0-9]+)$/.exec(uri);
  228. if (!parts) {
  229. return Promise.reject(new shaka.util.Error(
  230. shaka.util.Error.Severity.CRITICAL,
  231. shaka.util.Error.Category.STORAGE,
  232. shaka.util.Error.Code.MALFORMED_OFFLINE_URI, uri));
  233. }
  234. var error = null;
  235. var onError = function(e) {
  236. // Ignore errors if the session was already removed.
  237. if (e.code != shaka.util.Error.Code.OFFLINE_SESSION_REMOVED)
  238. error = e;
  239. };
  240. /** @type {shakaExtern.ManifestDB} */
  241. var manifestDb;
  242. /** @type {!shaka.media.DrmEngine} */
  243. var drmEngine;
  244. var manifestId = Number(parts[1]);
  245. return this.initIfNeeded_().then(function() {
  246. this.checkDestroyed_();
  247. return this.storageEngine_.get('manifest', manifestId);
  248. }.bind(this)).then((
  249. /**
  250. * @param {?shakaExtern.ManifestDB} data
  251. * @return {!Promise}
  252. */
  253. function(data) {
  254. this.checkDestroyed_();
  255. if (!data) {
  256. throw new shaka.util.Error(
  257. shaka.util.Error.Severity.CRITICAL,
  258. shaka.util.Error.Category.STORAGE,
  259. shaka.util.Error.Code.REQUESTED_ITEM_NOT_FOUND, uri);
  260. }
  261. manifestDb = data;
  262. var manifest =
  263. shaka.offline.OfflineManifestParser.reconstructManifest(manifestDb);
  264. var netEngine = this.player_.getNetworkingEngine();
  265. goog.asserts.assert(netEngine, 'Player must not be destroyed');
  266. drmEngine = new shaka.media.DrmEngine(
  267. netEngine, onError, function() {}, function() {});
  268. drmEngine.configure(this.player_.getConfiguration().drm);
  269. var isOffline = this.config_.usePersistentLicense || false;
  270. return drmEngine.init(manifest, isOffline);
  271. })
  272. .bind(this)).then(function() {
  273. return drmEngine.removeSessions(manifestDb.sessionIds);
  274. }.bind(this)).then(function() {
  275. return drmEngine.destroy();
  276. }.bind(this)).then(function() {
  277. this.checkDestroyed_();
  278. if (error) throw error;
  279. var Functional = shaka.util.Functional;
  280. // Get every segment for every stream in the manifest.
  281. /** @type {!Array.<number>} */
  282. var segments = manifestDb.periods.map(function(period) {
  283. return period.streams.map(function(stream) {
  284. var segments = stream.segments.map(function(segment) {
  285. var parts = /^offline:[0-9]+\/[0-9]+\/([0-9]+)$/.exec(segment.uri);
  286. goog.asserts.assert(parts, 'Invalid offline URI');
  287. return Number(parts[1]);
  288. });
  289. if (stream.initSegmentUri) {
  290. var parts = /^offline:[0-9]+\/[0-9]+\/([0-9]+)$/.exec(
  291. stream.initSegmentUri);
  292. goog.asserts.assert(parts, 'Invalid offline URI');
  293. segments.push(Number(parts[1]));
  294. }
  295. return segments;
  296. }).reduce(Functional.collapseArrays, []);
  297. }).reduce(Functional.collapseArrays, []);
  298. // Delete all the segments.
  299. var deleteCount = 0;
  300. var segmentCount = segments.length;
  301. var callback = this.config_.progressCallback;
  302. return this.storageEngine_.removeKeys('segment', segments, function() {
  303. deleteCount++;
  304. callback(content, deleteCount / segmentCount);
  305. });
  306. }.bind(this)).then(function() {
  307. this.checkDestroyed_();
  308. this.config_.progressCallback(content, 1);
  309. return this.storageEngine_.remove('manifest', manifestId);
  310. }.bind(this));
  311. };
  312. /**
  313. * Lists all the stored content available.
  314. *
  315. * @return {!Promise.<!Array.<shakaExtern.StoredContent>>} A Promise to an
  316. * array of structures representing all stored content. The "offlineUri"
  317. * member of the structure is the URI that should be given to Player.load()
  318. * to play this piece of content offline. The "appMetadata" member is the
  319. * appMetadata argument you passed to store().
  320. * @export
  321. */
  322. shaka.offline.Storage.prototype.list = function() {
  323. /** @type {!Array.<shakaExtern.StoredContent>} */
  324. var storedContents = [];
  325. return this.initIfNeeded_()
  326. .then(function() {
  327. this.checkDestroyed_();
  328. return this.storageEngine_.forEach(
  329. 'manifest', function(/** shakaExtern.ManifestDB */ manifest) {
  330. storedContents.push(
  331. shaka.offline.OfflineUtils.getStoredContent(manifest));
  332. });
  333. }.bind(this))
  334. .then(function() { return storedContents; });
  335. };
  336. /**
  337. * Loads the given manifest, parses it, and constructs the DrmEngine. This
  338. * stops the manifest parser. This may be replaced by tests.
  339. *
  340. * @param {string} manifestUri
  341. * @param {function(*)} onError
  342. * @param {!shakaExtern.ManifestParser.Factory=} opt_manifestParserFactory
  343. * @return {!Promise.<{
  344. * manifest: shakaExtern.Manifest,
  345. * drmEngine: !shaka.media.DrmEngine
  346. * }>}
  347. */
  348. shaka.offline.Storage.prototype.loadInternal = function(
  349. manifestUri, onError, opt_manifestParserFactory) {
  350. var netEngine = /** @type {!shaka.net.NetworkingEngine} */ (
  351. this.player_.getNetworkingEngine());
  352. var config = this.player_.getConfiguration();
  353. /** @type {shakaExtern.Manifest} */
  354. var manifest;
  355. /** @type {!shaka.media.DrmEngine} */
  356. var drmEngine;
  357. /** @type {!shakaExtern.ManifestParser} */
  358. var manifestParser;
  359. var onKeyStatusChange = function() {};
  360. return shaka.media.ManifestParser
  361. .getFactory(
  362. manifestUri, netEngine, config.manifest.retryParameters,
  363. opt_manifestParserFactory)
  364. .then(function(factory) {
  365. this.checkDestroyed_();
  366. manifestParser = new factory();
  367. manifestParser.configure(config.manifest);
  368. var playerInterface = {
  369. networkingEngine: netEngine,
  370. filterNewPeriod: this.filterPeriod_.bind(this),
  371. onTimelineRegionAdded: function() {},
  372. onEvent: function() {},
  373. onError: onError
  374. };
  375. return manifestParser.start(manifestUri, playerInterface);
  376. }.bind(this))
  377. .then(function(data) {
  378. this.checkDestroyed_();
  379. manifest = data;
  380. drmEngine = new shaka.media.DrmEngine(
  381. netEngine, onError, onKeyStatusChange, function() {});
  382. drmEngine.configure(config.drm);
  383. var isOffline = this.config_.usePersistentLicense || false;
  384. return drmEngine.init(manifest, isOffline);
  385. }.bind(this))
  386. .then(function() {
  387. this.checkDestroyed_();
  388. return this.createSegmentIndex_(manifest);
  389. }.bind(this))
  390. .then(function() {
  391. this.checkDestroyed_();
  392. return drmEngine.createOrLoad();
  393. }.bind(this))
  394. .then(function() {
  395. this.checkDestroyed_();
  396. return manifestParser.stop();
  397. }.bind(this))
  398. .then(function() {
  399. this.checkDestroyed_();
  400. return {manifest: manifest, drmEngine: drmEngine};
  401. }.bind(this))
  402. .catch(function(error) {
  403. if (manifestParser)
  404. return manifestParser.stop().then(function() { throw error; });
  405. else
  406. throw error;
  407. });
  408. };
  409. /**
  410. * The default track selection function.
  411. *
  412. * @param {!Array.<shakaExtern.Track>} tracks
  413. * @return {!Array.<shakaExtern.Track>}
  414. * @private
  415. */
  416. shaka.offline.Storage.prototype.defaultTrackSelect_ = function(tracks) {
  417. var LanguageUtils = shaka.util.LanguageUtils;
  418. var ContentType = shaka.util.ManifestParserUtils.ContentType;
  419. var selectedTracks = [];
  420. // Select variants with best language match.
  421. var audioLangPref = LanguageUtils.normalize(
  422. this.player_.getConfiguration().preferredAudioLanguage);
  423. var matchTypes = [
  424. LanguageUtils.MatchType.EXACT,
  425. LanguageUtils.MatchType.BASE_LANGUAGE_OKAY,
  426. LanguageUtils.MatchType.OTHER_SUB_LANGUAGE_OKAY
  427. ];
  428. var allVariantTracks =
  429. tracks.filter(function(track) { return track.type == 'variant'; });
  430. // For each match type, get the tracks that match the audio preference for
  431. // that match type.
  432. var tracksByMatchType = matchTypes.map(function(match) {
  433. return allVariantTracks.filter(function(track) {
  434. var lang = LanguageUtils.normalize(track.language);
  435. return LanguageUtils.match(match, audioLangPref, lang);
  436. });
  437. });
  438. // Find the best match type that has any matches.
  439. var variantTracks;
  440. for (var i = 0; i < tracksByMatchType.length; i++) {
  441. if (tracksByMatchType[i].length) {
  442. variantTracks = tracksByMatchType[i];
  443. break;
  444. }
  445. }
  446. // Fall back to "primary" audio tracks, if present.
  447. if (!variantTracks) {
  448. var primaryTracks = allVariantTracks.filter(function(track) {
  449. return track.primary;
  450. });
  451. if (primaryTracks.length)
  452. variantTracks = primaryTracks;
  453. }
  454. // Otherwise, there is no good way to choose the language, so we don't choose
  455. // a language at all.
  456. if (!variantTracks) {
  457. variantTracks = allVariantTracks;
  458. // Issue a warning, but only if the content has multiple languages.
  459. // Otherwise, this warning would just be noise.
  460. var languages = allVariantTracks
  461. .map(function(track) { return track.language; })
  462. .filter(shaka.util.Functional.isNotDuplicate);
  463. if (languages.length > 1) {
  464. shaka.log.warning('Could not choose a good audio track based on ' +
  465. 'language preferences or primary tracks. An ' +
  466. 'arbitrary language will be stored!');
  467. }
  468. }
  469. // From previously selected variants, choose the SD ones (height <= 480).
  470. var tracksByHeight = variantTracks.filter(function(track) {
  471. return track.height && track.height <= 480;
  472. });
  473. // If variants don't have video or no video with height <= 480 was
  474. // found, proceed with the previously selected tracks.
  475. if (tracksByHeight.length) {
  476. // Sort by resolution, then select all variants which match the height
  477. // of the highest SD res. There may be multiple audio bitrates for the
  478. // same video resolution.
  479. tracksByHeight.sort(function(a, b) { return b.height - a.height; });
  480. variantTracks = tracksByHeight.filter(function(track) {
  481. return track.height == tracksByHeight[0].height;
  482. });
  483. }
  484. // Now sort by bandwidth.
  485. variantTracks.sort(function(a, b) { return a.bandwidth - b.bandwidth; });
  486. // In case there are multiple matches at different audio bitrates, select the
  487. // middle bandwidth one.
  488. if (variantTracks.length)
  489. selectedTracks.push(variantTracks[Math.floor(variantTracks.length / 2)]);
  490. // Since this default callback is used primarily by our own demo app and by
  491. // app developers who haven't thought about which tracks they want, we should
  492. // select all text tracks, regardless of language. This makes for a better
  493. // demo for us, and does not rely on user preferences for the unconfigured
  494. // app.
  495. selectedTracks.push.apply(selectedTracks, tracks.filter(function(track) {
  496. return track.type == ContentType.TEXT;
  497. }));
  498. return selectedTracks;
  499. };
  500. /**
  501. * @return {shakaExtern.OfflineConfiguration}
  502. * @private
  503. */
  504. shaka.offline.Storage.prototype.defaultConfig_ = function() {
  505. return {
  506. trackSelectionCallback: this.defaultTrackSelect_.bind(this),
  507. progressCallback: function(storedContent, percent) {
  508. // Reference arguments to keep closure from removing it.
  509. // If the argument is removed, it breaks our function length check
  510. // in mergeConfigObjects_().
  511. // NOTE: Chrome App Content Security Policy prohibits usage of new
  512. // Function().
  513. if (storedContent || percent) return null;
  514. },
  515. usePersistentLicense: true
  516. };
  517. };
  518. /**
  519. * Initializes the IStorageEngine if it is not already.
  520. *
  521. * @return {!Promise}
  522. * @private
  523. */
  524. shaka.offline.Storage.prototype.initIfNeeded_ = function() {
  525. if (!this.storageEngine_) {
  526. return Promise.reject(new shaka.util.Error(
  527. shaka.util.Error.Severity.CRITICAL,
  528. shaka.util.Error.Category.STORAGE,
  529. shaka.util.Error.Code.STORAGE_NOT_SUPPORTED));
  530. } else if (this.storageEngine_.initialized()) {
  531. return Promise.resolve();
  532. } else {
  533. var scheme = shaka.offline.OfflineUtils.DB_SCHEME;
  534. return this.storageEngine_.init(scheme);
  535. }
  536. };
  537. /**
  538. * @param {shakaExtern.Period} period
  539. * @private
  540. */
  541. shaka.offline.Storage.prototype.filterPeriod_ = function(period) {
  542. var StreamUtils = shaka.util.StreamUtils;
  543. var ContentType = shaka.util.ManifestParserUtils.ContentType;
  544. var activeStreams = {};
  545. if (this.firstPeriodTracks_) {
  546. var variantTracks = this.firstPeriodTracks_.filter(function(track) {
  547. return track.type == 'variant';
  548. });
  549. var variant = null;
  550. if (variantTracks.length)
  551. variant = StreamUtils.findVariantForTrack(period, variantTracks[0]);
  552. if (variant) {
  553. // Use the first variant as the container of "active streams". This
  554. // is then used to filter out the streams that are not compatible with it.
  555. // This ensures that in multi-Period content, all Periods have streams
  556. // with compatible MIME types.
  557. if (variant.video) activeStreams[ContentType.VIDEO] = variant.video;
  558. if (variant.audio) activeStreams[ContentType.AUDIO] = variant.audio;
  559. }
  560. }
  561. StreamUtils.filterNewPeriod(this.drmEngine_, activeStreams, period);
  562. StreamUtils.applyRestrictions(
  563. period, this.player_.getConfiguration().restrictions,
  564. /* maxHwRes */ { width: Infinity, height: Infinity });
  565. };
  566. /**
  567. * Cleans up the current store and destroys any objects. This object is still
  568. * usable after this.
  569. *
  570. * @return {!Promise}
  571. * @private
  572. */
  573. shaka.offline.Storage.prototype.cleanup_ = function() {
  574. var ret = this.drmEngine_ ? this.drmEngine_.destroy() : Promise.resolve();
  575. this.drmEngine_ = null;
  576. this.manifest_ = null;
  577. this.storeInProgress_ = false;
  578. this.firstPeriodTracks_ = null;
  579. this.manifestId_ = -1;
  580. return ret;
  581. };
  582. /**
  583. * Calls createSegmentIndex for all streams in the manifest.
  584. *
  585. * @param {shakaExtern.Manifest} manifest
  586. * @return {!Promise}
  587. * @private
  588. */
  589. shaka.offline.Storage.prototype.createSegmentIndex_ = function(manifest) {
  590. var Functional = shaka.util.Functional;
  591. var streams = manifest.periods
  592. .map(function(period) { return period.variants; })
  593. .reduce(Functional.collapseArrays, [])
  594. .map(function(variant) {
  595. var variantStreams = [];
  596. if (variant.audio) variantStreams.push(variant.audio);
  597. if (variant.video) variantStreams.push(variant.video);
  598. return variantStreams;
  599. })
  600. .reduce(Functional.collapseArrays, [])
  601. .filter(Functional.isNotDuplicate);
  602. var textStreams = manifest.periods
  603. .map(function(period) { return period.textStreams; })
  604. .reduce(Functional.collapseArrays, []);
  605. streams.push.apply(streams, textStreams);
  606. return Promise.all(
  607. streams.map(function(stream) { return stream.createSegmentIndex(); }));
  608. };
  609. /**
  610. * Creates an offline 'manifest' for the real manifest. This does not store
  611. * the segments yet, only adds them to the download manager through
  612. * createPeriod_.
  613. *
  614. * @param {string} originalManifestUri
  615. * @param {!Object} appMetadata
  616. * @return {shakaExtern.ManifestDB}
  617. * @private
  618. */
  619. shaka.offline.Storage.prototype.createOfflineManifest_ = function(
  620. originalManifestUri, appMetadata) {
  621. var periods = this.manifest_.periods.map(this.createPeriod_.bind(this));
  622. var drmInfo = this.drmEngine_.getDrmInfo();
  623. var sessions = this.drmEngine_.getSessionIds();
  624. if (drmInfo) {
  625. if (!sessions.length) {
  626. throw new shaka.util.Error(
  627. shaka.util.Error.Severity.CRITICAL, shaka.util.Error.Category.STORAGE,
  628. shaka.util.Error.Code.NO_INIT_DATA_FOR_OFFLINE, originalManifestUri);
  629. }
  630. // Don't store init data since we have stored sessions.
  631. drmInfo.initData = [];
  632. }
  633. return {
  634. key: this.manifestId_,
  635. originalManifestUri: originalManifestUri,
  636. duration: this.duration_,
  637. size: 0,
  638. expiration: this.drmEngine_.getExpiration(),
  639. periods: periods,
  640. sessionIds: this.config_.usePersistentLicense ? sessions : [],
  641. drmInfo: drmInfo,
  642. appMetadata: appMetadata
  643. };
  644. };
  645. /**
  646. * Converts a manifest Period to a database Period. This will use the current
  647. * configuration to get the tracks to use, then it will search each segment
  648. * index and add all the segments to the download manager through createStream_.
  649. *
  650. * @param {shakaExtern.Period} period
  651. * @return {shakaExtern.PeriodDB}
  652. * @private
  653. */
  654. shaka.offline.Storage.prototype.createPeriod_ = function(period) {
  655. var StreamUtils = shaka.util.StreamUtils;
  656. var variantTracks = StreamUtils.getVariantTracks(period, null, null);
  657. var textTracks = StreamUtils.getTextTracks(period, null);
  658. var allTracks = variantTracks.concat(textTracks);
  659. var chosenTracks = this.config_.trackSelectionCallback(allTracks);
  660. if (this.firstPeriodTracks_ == null) {
  661. this.firstPeriodTracks_ = chosenTracks;
  662. // Now that the first tracks are chosen, filter again. This ensures all
  663. // Periods have compatible content types.
  664. this.manifest_.periods.forEach(this.filterPeriod_.bind(this));
  665. }
  666. for (var i = chosenTracks.length - 1; i > 0; --i) {
  667. var foundSimilarTracks = false;
  668. for (var j = i - 1; j >= 0; --j) {
  669. if (chosenTracks[i].type == chosenTracks[j].type &&
  670. chosenTracks[i].kind == chosenTracks[j].kind &&
  671. chosenTracks[i].language == chosenTracks[j].language) {
  672. shaka.log.warning(
  673. 'Multiple tracks of the same type/kind/language given.');
  674. foundSimilarTracks = true;
  675. break;
  676. }
  677. }
  678. if (foundSimilarTracks) break;
  679. }
  680. var streams = [];
  681. for (var i = 0; i < chosenTracks.length; i++) {
  682. var variant = StreamUtils.findVariantForTrack(period, chosenTracks[i]);
  683. if (variant) {
  684. // Make a rough estimation of the streams' bandwidth so download manager
  685. // can track the progress of the download.
  686. var bandwidthEstimation;
  687. if (variant.audio) {
  688. // If the audio stream has already been added to the DB
  689. // as part of another variant, add the ID to the list.
  690. // Otherwise, add it to the DB.
  691. var stream = streams.filter(function(s) {
  692. return s.id == variant.audio.id;
  693. })[0];
  694. if (stream) {
  695. stream.variantIds.push(variant.id);
  696. } else {
  697. // If variant has both audio and video, roughly estimate them
  698. // both to be 1/2 of the variant's bandwidth.
  699. // If variant only has one stream, it's bandwidth equals to
  700. // the bandwidth of the variant.
  701. bandwidthEstimation =
  702. variant.video ? variant.bandwidth / 2 : variant.bandwidth;
  703. streams.push(this.createStream_(period,
  704. variant.audio,
  705. bandwidthEstimation,
  706. variant.id));
  707. }
  708. }
  709. if (variant.video) {
  710. var stream = streams.filter(function(s) {
  711. return s.id == variant.video.id;
  712. })[0];
  713. if (stream) {
  714. stream.variantIds.push(variant.id);
  715. } else {
  716. bandwidthEstimation =
  717. variant.audio ? variant.bandwidth / 2 : variant.bandwidth;
  718. streams.push(this.createStream_(period,
  719. variant.video,
  720. bandwidthEstimation,
  721. variant.id));
  722. }
  723. }
  724. } else {
  725. var textStream =
  726. StreamUtils.findTextStreamForTrack(period, chosenTracks[i]);
  727. goog.asserts.assert(
  728. textStream, 'Could not find track with id ' + chosenTracks[i].id);
  729. streams.push(this.createStream_(
  730. period, textStream, 0 /* estimatedStreamBandwidth */));
  731. }
  732. }
  733. return {
  734. startTime: period.startTime,
  735. streams: streams
  736. };
  737. };
  738. /**
  739. * Converts a manifest stream to a database stream. This will search the
  740. * segment index and add all the segments to the download manager.
  741. *
  742. * @param {shakaExtern.Period} period
  743. * @param {shakaExtern.Stream} stream
  744. * @param {number} estimatedStreamBandwidth
  745. * @param {number=} opt_variantId
  746. * @return {shakaExtern.StreamDB}
  747. * @private
  748. */
  749. shaka.offline.Storage.prototype.createStream_ = function(
  750. period, stream, estimatedStreamBandwidth, opt_variantId) {
  751. /** @type {!Array.<shakaExtern.SegmentDB>} */
  752. var segmentsDb = [];
  753. var startTime =
  754. this.manifest_.presentationTimeline.getSegmentAvailabilityStart();
  755. var endTime = startTime;
  756. var i = stream.findSegmentPosition(startTime);
  757. var ref = (i != null ? stream.getSegmentReference(i) : null);
  758. while (ref) {
  759. var id = this.storageEngine_.reserveId('segment');
  760. var bandwidthSize =
  761. (ref.endTime - ref.startTime) * estimatedStreamBandwidth / 8;
  762. /** @type {shakaExtern.SegmentDataDB} */
  763. var segmentDataDb = {
  764. key: id,
  765. data: null,
  766. manifestKey: this.manifestId_,
  767. streamNumber: stream.id,
  768. segmentNumber: id
  769. };
  770. this.downloadManager_.addSegment(
  771. stream.type, ref, bandwidthSize, segmentDataDb);
  772. segmentsDb.push({
  773. startTime: ref.startTime,
  774. endTime: ref.endTime,
  775. uri: 'offline:' + this.manifestId_ + '/' + stream.id + '/' + id
  776. });
  777. endTime = ref.endTime + period.startTime;
  778. ref = stream.getSegmentReference(++i);
  779. }
  780. this.duration_ = Math.max(this.duration_, (endTime - startTime));
  781. var initUri = null;
  782. if (stream.initSegmentReference) {
  783. var id = this.storageEngine_.reserveId('segment');
  784. initUri = 'offline:' + this.manifestId_ + '/' + stream.id + '/' + id;
  785. /** @type {shakaExtern.SegmentDataDB} */
  786. var initDataDb = {
  787. key: id,
  788. data: null,
  789. manifestKey: this.manifestId_,
  790. streamNumber: stream.id,
  791. segmentNumber: -1
  792. };
  793. this.downloadManager_.addSegment(
  794. stream.contentType, stream.initSegmentReference, 0, initDataDb);
  795. }
  796. var variantIds = [];
  797. if (opt_variantId != null) variantIds.push(opt_variantId);
  798. return {
  799. id: stream.id,
  800. primary: stream.primary,
  801. presentationTimeOffset: stream.presentationTimeOffset || 0,
  802. contentType: stream.type,
  803. mimeType: stream.mimeType,
  804. codecs: stream.codecs,
  805. frameRate: stream.frameRate,
  806. kind: stream.kind,
  807. language: stream.language,
  808. label: stream.label,
  809. width: stream.width || null,
  810. height: stream.height || null,
  811. initSegmentUri: initUri,
  812. encrypted: stream.encrypted,
  813. keyId: stream.keyId,
  814. segments: segmentsDb,
  815. variantIds: variantIds
  816. };
  817. };
  818. /**
  819. * Throws an error if the object is destroyed.
  820. * @private
  821. */
  822. shaka.offline.Storage.prototype.checkDestroyed_ = function() {
  823. if (!this.player_) {
  824. throw new shaka.util.Error(
  825. shaka.util.Error.Severity.CRITICAL,
  826. shaka.util.Error.Category.STORAGE,
  827. shaka.util.Error.Code.OPERATION_ABORTED);
  828. }
  829. };
  830. shaka.Player.registerSupportPlugin('offline', shaka.offline.Storage.support);