Skip to content

Commit

Permalink
Share Networking Engine From Storage
Browse files Browse the repository at this point in the history
Allow users to get access to the networking engine that storage
instances are using.

This is part of allowing storage to be used without a player instance
as it will allow users to configure the networking engine when they
create a storage instance without a player instance.

The code still assumes that Storage should never destroy the networking
engine. This is something that will need to change when storage can
be initialized without player.

Issue #1297

Change-Id: If8d1642259e0cf24cb43f2ca7936227e8d939260
  • Loading branch information
vaage committed Aug 1, 2018
1 parent 0d89b33 commit 36ecbb1
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions lib/offline/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ shaka.offline.Storage = function(player) {
/** @private {?shaka.extern.PlayerConfiguration} */
this.config_ = player.getSharedConfiguration();

/** @private {shaka.net.NetworkingEngine} */
this.networkingEngine_ = player.getNetworkingEngine();
goog.asserts.assert(
this.networkingEngine_,
'Storage should not be initialized with a player that has |destroy| ' +
'called on it.');

/** @private {boolean} */
this.storeInProgress_ = false;

Expand Down Expand Up @@ -118,6 +125,7 @@ shaka.offline.Storage.support = function() {
*/
shaka.offline.Storage.prototype.destroy = function() {
this.config_ = null;
this.networkingEngine_ = null;
this.player_ = null;

// Wait for all the open operations to end. Wrap each operations so that a
Expand Down Expand Up @@ -153,6 +161,22 @@ shaka.offline.Storage.prototype.configure = function(config) {
};


/**
* Return the networking engine that storage is using. If storage was
* initialized with a player instance, then the networking engine returned
* will be the same as |player.getNetworkingEngine()|.
*
* The returned value will only be null if |destroy| was called before
* |getNetworkingEngine|.
*
* @return {shaka.net.NetworkingEngine}
* @export
*/
shaka.offline.Storage.prototype.getNetworkingEngine = function() {
return this.networkingEngine_;
};


/**
* Stores the given manifest. If the content is encrypted, and encrypted
* content cannot be stored on this platform, the Promise will be rejected with
Expand Down Expand Up @@ -312,7 +336,7 @@ shaka.offline.Storage.prototype.store_ = async function(
* @return {!Promise.<shaka.extern.ManifestDB>}
* @private
*/
shaka.offline.Storage.prototype.downloadManifest_ = function(
shaka.offline.Storage.prototype.downloadManifest_ = async function(
storage, drm, manifest, uri, metadata) {
const noSize = 0;
let pendingContent = shaka.offline.StoredContentUtils.fromManifest(
Expand All @@ -325,17 +349,23 @@ shaka.offline.Storage.prototype.downloadManifest_ = function(
this.config_.offline.progressCallback(pendingContent, progress);
});

goog.asserts.assert(
this.networkingEngine_,
'Cannot call |downloadManifest_| after calling |destroy|.');
const networkingEngine = this.networkingEngine_;

/** @type {shaka.extern.ManifestDB} */
let manifestDB;
return shaka.util.Destroyer.with([downloader], () => {

await shaka.util.Destroyer.with([downloader], async () => {
manifestDB = this.createOfflineManifest_(
downloader, storage, drm, manifest, uri, metadata);
return downloader.download(this.getNetEngine_());
}).then(() => {
// Update the size before saving it.
manifestDB.size = pendingContent.size;
return manifestDB;
await downloader.download(networkingEngine);
});

// Update the size before saving it.
manifestDB.size = pendingContent.size;
return manifestDB;
};


Expand Down Expand Up @@ -394,8 +424,6 @@ shaka.offline.Storage.prototype.remove_ = function(contentUri) {
* @private
*/
shaka.offline.Storage.prototype.removeFromDRM_ = function(uri, manifestDB) {
let netEngine = this.getNetEngine_();

let error;
let onError = (e) => {
// Ignore errors if the session was already removed.
Expand All @@ -404,8 +432,11 @@ shaka.offline.Storage.prototype.removeFromDRM_ = function(uri, manifestDB) {
}
};

goog.asserts.assert(
this.networkingEngine_, 'Cannot call |removeFromDRM_| after |destroy|');

let drmEngine = new shaka.media.DrmEngine({
netEngine: netEngine,
netEngine: this.networkingEngine_,
onError: onError,
onKeyStatus: () => {},
onExpirationUpdated: () => {},
Expand Down Expand Up @@ -529,7 +560,11 @@ shaka.offline.Storage.prototype.list_ = function() {
*/
shaka.offline.Storage.prototype.loadInternal = function(
manifestUri, onError, getParser) {
let netEngine = this.getNetEngine_();
goog.asserts.assert(
this.networkingEngine_,
'Cannot call |loadInternal| after calling |destroy|');
const netEngine = this.networkingEngine_;

let config = this.player_.getConfiguration();

/** @type {shaka.extern.Manifest} */
Expand Down Expand Up @@ -955,17 +990,6 @@ shaka.offline.Storage.prototype.requireSupport_ = function() {
};


/**
* @return {!shaka.net.NetworkingEngine}
* @private
*/
shaka.offline.Storage.prototype.getNetEngine_ = function() {
let net = this.player_.getNetworkingEngine();
goog.asserts.assert(net, 'Player must not be destroyed');
return net;
};


/**
* @param {!shaka.media.SegmentReference|
* !shaka.media.InitSegmentReference} segment
Expand Down

0 comments on commit 36ecbb1

Please sign in to comment.