Skip to content

Commit

Permalink
Fix ContentDatabase/AjaxRequest circular dep.
Browse files Browse the repository at this point in the history
The circular dependency,
ContentDatabase -> RangeRequest -> AjaxRequest -> ContentDatabase,
causes issues for the upstream Closure compiler. This patch breaks that
cycle by factoring out reading and writing operations from ContentDatabase
into ContentDatabaseReader and ContentDatabaseWriter respectively.

The following minor changes have been made to code that has moved:
* Removed retrieveInitSegment() since it's not used.
* Changed .stream_ids to ['stream_ids'] in retriveGroup().
* Reworked deleteGroup() so that it doesn't depend on retrieveGroup().
* Made deleteStream() private.
* Made minor formatting changes to meet the 80 character limit.

Change-Id: Idfc5d04ad32225a915b1531e0f4205137de5cc73
  • Loading branch information
Timothy Drews committed Aug 10, 2015
1 parent 94937d7 commit 7623b58
Show file tree
Hide file tree
Showing 7 changed files with 766 additions and 709 deletions.
71 changes: 34 additions & 37 deletions lib/player/offline_video_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SimpleAbrManager');
goog.require('shaka.media.StreamInfo');
goog.require('shaka.player.StreamVideoSource');
goog.require('shaka.util.ContentDatabase');
goog.require('shaka.util.ContentDatabaseReader');
goog.require('shaka.util.ContentDatabaseWriter');
goog.require('shaka.util.IBandwidthEstimator');
goog.require('shaka.util.LanguageUtils');
goog.require('shaka.util.TypedBind');
Expand Down Expand Up @@ -135,7 +136,7 @@ shaka.player.OfflineVideoSource.prototype.configure = function(config) {
* @export
*/
shaka.player.OfflineVideoSource.retrieveGroupIds = function() {
var contentDatabase = new shaka.util.ContentDatabase(null, null);
var contentDatabase = new shaka.util.ContentDatabaseReader();

var p = contentDatabase.setUpDatabase().then(
function() {
Expand Down Expand Up @@ -347,48 +348,45 @@ shaka.player.OfflineVideoSource.prototype.onSessionReady_ = function(event) {
*/
shaka.player.OfflineVideoSource.prototype.insertGroup_ =
function(selectedStreams, drmScheme, duration) {
var contentDatabase = new shaka.util.ContentDatabase(this.estimator, this);
var contentDatabase =
new shaka.util.ContentDatabaseWriter(this.estimator, this);
if (this.config_['rangeRequestTimeout'] != null) {
contentDatabase.setRangeRequestTimeout(
Number(this.config_['rangeRequestTimeout']));
}
var p = contentDatabase.setUpDatabase();

// Insert the group of streams into the database and close the connection.
p = p.then(shaka.util.TypedBind(this, function() {
return contentDatabase.setUpDatabase().then(shaka.util.TypedBind(this,
function() {
return contentDatabase.insertGroup(
selectedStreams, this.sessionIds_, duration, drmScheme);
})).then(
/** @param {number} groupId */
function(groupId) {
contentDatabase.closeDatabaseConnection();
return Promise.resolve(groupId);
}
).catch(
/** @param {*} e */
function(e) {
contentDatabase.closeDatabaseConnection();
return Promise.reject(e);
});
return p;
})
).then(
/** @param {number} groupId */
function(groupId) {
contentDatabase.closeDatabaseConnection();
return Promise.resolve(groupId);
}
).catch(
/** @param {*} e */
function(e) {
contentDatabase.closeDatabaseConnection();
return Promise.reject(e);
}
);
};


/** @override */
shaka.player.OfflineVideoSource.prototype.load = function(preferredLanguage) {
shaka.asserts.assert(this.groupId_ >= 0);
var contentDatabase = new shaka.util.ContentDatabase(null, null);
if (this.config_['rangeRequestTimeout'] != null) {
contentDatabase.setRangeRequestTimeout(
Number(this.config_['rangeRequestTimeout']));
}
var p = contentDatabase.setUpDatabase();
var contentDatabase = new shaka.util.ContentDatabaseReader();
var duration, keySystem;

return p.then(shaka.util.TypedBind(this,
return contentDatabase.setUpDatabase().then(shaka.util.TypedBind(this,
function() {
return contentDatabase.retrieveGroup(/** @type {number} */(
this.groupId_));
return contentDatabase.retrieveGroup(
/** @type {number} */(this.groupId_));
})
).then(shaka.util.TypedBind(this,
/** @param {shaka.util.ContentDatabase.GroupInformation} group */
Expand Down Expand Up @@ -422,7 +420,8 @@ shaka.player.OfflineVideoSource.prototype.load = function(preferredLanguage) {
function(e) {
contentDatabase.closeDatabaseConnection();
return Promise.reject(e);
});
}
);
};


Expand Down Expand Up @@ -485,15 +484,13 @@ shaka.player.OfflineVideoSource.prototype.reconstructManifestInfo_ =
*/
shaka.player.OfflineVideoSource.prototype.deleteGroup = function() {
shaka.asserts.assert(this.groupId_ >= 0);
var contentDatabase = new shaka.util.ContentDatabase(null, null);
if (this.config_['rangeRequestTimeout'] != null) {
contentDatabase.setRangeRequestTimeout(
Number(this.config_['rangeRequestTimeout']));
}
var p = contentDatabase.setUpDatabase();
return p.then(
contentDatabase.deleteGroup.bind(
contentDatabase, /** @type {number} */(this.groupId_))
var contentDatabase = new shaka.util.ContentDatabaseWriter(null, null);

return contentDatabase.setUpDatabase().then(shaka.util.TypedBind(this,
function() {
return contentDatabase.deleteGroup(
/** @type {number} */(this.groupId_));
})
).then(
function() {
contentDatabase.closeDatabaseConnection();
Expand Down
13 changes: 8 additions & 5 deletions lib/util/ajax_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ goog.provide('shaka.util.AjaxRequest');
goog.require('goog.Uri');
goog.require('shaka.asserts');
goog.require('shaka.util.Clock');
goog.require('shaka.util.ContentDatabaseReader');
goog.require('shaka.util.IBandwidthEstimator');
goog.require('shaka.util.PublicPromise');
goog.require('shaka.util.StringUtils');
Expand Down Expand Up @@ -328,10 +329,12 @@ shaka.util.AjaxRequest.prototype.handleOfflineUri_ = function() {
shaka.asserts.assert(!isNaN(streamId));
shaka.asserts.assert(!isNaN(segmentId));

var db = new shaka.util.ContentDatabase(null, null);
return db.setUpDatabase().then(
// TODO: It may be inefficient to keep re-opening the database connection.
// Find a way to keep the database connection open between requests.
var contentDatabase = new shaka.util.ContentDatabaseReader();
return contentDatabase.setUpDatabase().then(
function() {
return db.retrieveSegment(streamId, segmentId);
return contentDatabase.retrieveSegment(streamId, segmentId);
}
).then(shaka.util.TypedBind(this,
/** @param {ArrayBuffer} data */
Expand All @@ -344,14 +347,14 @@ shaka.util.AjaxRequest.prototype.handleOfflineUri_ = function() {

var promise = this.promise_;
promise.resolve(xhr);
db.closeDatabaseConnection();
contentDatabase.closeDatabaseConnection();
this.destroy_();
return promise;
})
).catch(shaka.util.TypedBind(this,
/** @param {*} e */
function(e) {
db.closeDatabaseConnection();
contentDatabase.closeDatabaseConnection();
this.destroy_();
return Promise.reject(e);
})
Expand Down
Loading

0 comments on commit 7623b58

Please sign in to comment.