Skip to content

Commit

Permalink
Move common code in offline to offline_utils.
Browse files Browse the repository at this point in the history
Added new offline_utils.js to hold utility methods for offline
support.  This also removes a circular dependency between
Storage and DownloadManager.

Closes #431

Change-Id: I98842c63c7ba57d4d02d7fe6bace57227982317c
  • Loading branch information
TheModMaker committed Jun 30, 2016
1 parent ed15d46 commit beb902e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 44 deletions.
1 change: 1 addition & 0 deletions build/types/offline
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
+../../lib/offline/download_manager.js
+../../lib/offline/offline_manifest_parser.js
+../../lib/offline/offline_scheme.js
+../../lib/offline/offline_utils.js
+../../lib/offline/storage.js
3 changes: 2 additions & 1 deletion lib/offline/download_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ goog.provide('shaka.offline.DownloadManager');

goog.require('goog.asserts');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.offline.OfflineUtils');
goog.require('shaka.util.Error');
goog.require('shaka.util.IDestroyable');

Expand Down Expand Up @@ -237,6 +238,6 @@ shaka.offline.DownloadManager.prototype.updateProgress_ = function() {
(this.givenBytesTotal_ + this.bandwidthBytesTotal_);

goog.asserts.assert(this.manifest_, 'Must not be destroyed');
var manifest = shaka.offline.Storage.getStoredContent(this.manifest_);
var manifest = shaka.offline.OfflineUtils.getStoredContent(this.manifest_);
this.config_.progressCallback(manifest, progress);
};
3 changes: 2 additions & 1 deletion lib/offline/offline_manifest_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ goog.require('shaka.media.PresentationTimeline');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.offline.DBEngine');
goog.require('shaka.offline.OfflineUtils');
goog.require('shaka.util.Error');


Expand Down Expand Up @@ -55,7 +56,7 @@ shaka.offline.OfflineManifestParser.prototype.start =
var manifestId = Number(parts[1]);
var dbEngine = new shaka.offline.DBEngine();

return dbEngine.init(shaka.offline.Storage.DB_SCHEME)
return dbEngine.init(shaka.offline.OfflineUtils.DB_SCHEME)
.then(function() { return dbEngine.get('manifest', manifestId); })
.then(function(manifest) {
if (!manifest) {
Expand Down
3 changes: 2 additions & 1 deletion lib/offline/offline_scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ goog.provide('shaka.offline.OfflineScheme');

goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.offline.DBEngine');
goog.require('shaka.offline.OfflineUtils');
goog.require('shaka.util.Error');


Expand All @@ -44,7 +45,7 @@ shaka.offline.OfflineScheme = function(uri, request) {
var segmentParts = /^offline:[0-9]+\/[0-9]+\/([0-9]+)$/.exec(uri);
if (segmentParts) {
var segmentId = Number(segmentParts[1]);
var scheme = shaka.offline.Storage.DB_SCHEME;
var scheme = shaka.offline.OfflineUtils.DB_SCHEME;
var dbEngine = new shaka.offline.DBEngine();
return dbEngine.init(scheme)
.then(function() { return dbEngine.get('segment', segmentId); })
Expand Down
56 changes: 56 additions & 0 deletions lib/offline/offline_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

goog.provide('shaka.offline.OfflineUtils');

goog.require('goog.asserts');


/** @const {!Object.<string, string>} */
shaka.offline.OfflineUtils.DB_SCHEME = {'manifest': 'key', 'segment': 'key'};


/**
* Converts the given database manifest to a storedContent structure.
*
* @param {shakaExtern.ManifestDB} manifest
* @return {shakaExtern.StoredContent}
*/
shaka.offline.OfflineUtils.getStoredContent = function(manifest) {
goog.asserts.assert(manifest.periods.length > 0,
'Must be at least one Period.');
return {
offlineUri: 'offline:' + manifest.key,
originalManifestUri: manifest.originalManifestUri,
duration: manifest.duration,
size: manifest.size,
tracks: manifest.periods[0].streams.map(function(stream) {
return {
id: stream.id,
active: false,
type: stream.contentType,
bandwidth: 0,
language: stream.language,
kind: stream.kind || null,
width: stream.width,
height: stream.height,
hasOutputRestrictions: false
};
}),
appMetadata: manifest.appMetadata
};
};
43 changes: 4 additions & 39 deletions lib/offline/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ goog.require('shaka.media.ManifestParser');
goog.require('shaka.offline.DBEngine');
goog.require('shaka.offline.DownloadManager');
goog.require('shaka.offline.OfflineManifestParser');
goog.require('shaka.offline.OfflineUtils');
goog.require('shaka.util.ConfigUtils');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
Expand Down Expand Up @@ -91,10 +92,6 @@ shaka.offline.Storage = function(player) {
};


/** @const {!Object.<string, string>} */
shaka.offline.Storage.DB_SCHEME = {'manifest': 'key', 'segment': 'key'};


/**
* Gets whether offline storage is supported.
*
Expand All @@ -106,38 +103,6 @@ shaka.offline.Storage.support = function() {
};


/**
* Converts the given database manifest to a storedContent structure.
*
* @param {shakaExtern.ManifestDB} manifest
* @return {shakaExtern.StoredContent}
*/
shaka.offline.Storage.getStoredContent = function(manifest) {
goog.asserts.assert(manifest.periods.length > 0,
'Must be at least one Period.');
return {
offlineUri: 'offline:' + manifest.key,
originalManifestUri: manifest.originalManifestUri,
duration: manifest.duration,
size: manifest.size,
tracks: manifest.periods[0].streams.map(function(stream) {
return {
id: stream.id,
active: false,
type: stream.contentType,
bandwidth: 0,
language: stream.language,
kind: stream.kind || null,
width: stream.width,
height: stream.height,
hasOutputRestrictions: false
};
}),
appMetadata: manifest.appMetadata
};
};


/**
* Sets the DBEngine instance to use. This is used for testing.
*
Expand Down Expand Up @@ -252,7 +217,7 @@ shaka.offline.Storage.prototype.store = function(
return this.cleanup_();
}.bind(this))
.then(function() {
return shaka.offline.Storage.getStoredContent(manifestDb);
return shaka.offline.OfflineUtils.getStoredContent(manifestDb);
}.bind(this))
.catch(function(err) {
var Functional = shaka.util.Functional;
Expand Down Expand Up @@ -372,7 +337,7 @@ shaka.offline.Storage.prototype.list = function() {
return this.dbEngine_.forEach(
'manifest', function(/** shakaExtern.ManifestDB */ manifest) {
storedContents.push(
shaka.offline.Storage.getStoredContent(manifest));
shaka.offline.OfflineUtils.getStoredContent(manifest));
});
}.bind(this))
.then(function() { return storedContents; });
Expand Down Expand Up @@ -532,7 +497,7 @@ shaka.offline.Storage.prototype.defaultConfig_ = function() {
* @private
*/
shaka.offline.Storage.prototype.initIfNeeded_ = function() {
var scheme = shaka.offline.Storage.DB_SCHEME;
var scheme = shaka.offline.OfflineUtils.DB_SCHEME;
return this.dbEngine_.initialized() ? Promise.resolve() :
this.dbEngine_.init(scheme);
};
Expand Down
2 changes: 1 addition & 1 deletion test/offline_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Offline', function() {
player = new shaka.Player(video);
storage = new shaka.offline.Storage(player);
dbEngine = new shaka.offline.DBEngine();
dbEngine.init(shaka.offline.Storage.DB_SCHEME).catch(fail).then(done);
dbEngine.init(shaka.offline.OfflineUtils.DB_SCHEME).catch(fail).then(done);
});

afterEach(function(done) {
Expand Down
2 changes: 1 addition & 1 deletion test/storage_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Storage', function() {
storage = new shaka.offline.Storage(player);
storage.setDbEngine(fakeDbEngine);

fakeDbEngine.init(shaka.offline.Storage.DB_SCHEME)
fakeDbEngine.init(shaka.offline.OfflineUtils.DB_SCHEME)
.catch(fail)
.then(done);
});
Expand Down

0 comments on commit beb902e

Please sign in to comment.