Skip to content

Commit

Permalink
Allow injection of a custom IAbrManager implementation at runtime.
Browse files Browse the repository at this point in the history
Closes #48.

Change-Id: I8f15adb9143076626bdc616e3f3ba4f3be90324e
  • Loading branch information
natalieharris authored and Gerrit Code Review committed May 22, 2015
1 parent 202799a commit f3e6003
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
16 changes: 12 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ app.deleteStream = function() {
console.assert(app.estimator_);
var estimator = /** @type {!shaka.util.IBandwidthEstimator} */(
app.estimator_);
var offlineSource = new shaka.player.OfflineVideoSource(groupId, estimator);
var abrManager = new shaka.media.SimpleAbrManager();
var offlineSource = new shaka.player.OfflineVideoSource(
groupId, estimator, abrManager);
offlineSource.deleteGroup().then(
function() {
var deleted = app.offlineStreams_.indexOf(text);
Expand Down Expand Up @@ -430,7 +432,9 @@ app.storeStream = function() {
console.assert(app.estimator_);
var estimator = /** @type {!shaka.util.IBandwidthEstimator} */(
app.estimator_);
var offlineSource = new shaka.player.OfflineVideoSource(null, estimator);
var abrManager = new shaka.media.SimpleAbrManager();
var offlineSource = new shaka.player.OfflineVideoSource(
null, estimator, abrManager);
offlineSource.addEventListener('progress', app.progressEventHandler_);
offlineSource.store(
mediaUrl, preferredLanguage, app.interpretContentProtection_,
Expand Down Expand Up @@ -607,11 +611,13 @@ app.loadDashStream = function() {

var estimator = /** @type {!shaka.util.IBandwidthEstimator} */(
app.estimator_);
var abrManager = new shaka.media.SimpleAbrManager();
app.load_(
new shaka.player.DashVideoSource(
mediaUrl,
app.interpretContentProtection_,
estimator));
estimator,
abrManager));
};


Expand All @@ -628,7 +634,9 @@ app.loadOfflineStream = function() {
console.assert(app.estimator_);
var estimator = /** @type {!shaka.util.IBandwidthEstimator} */(
app.estimator_);
app.load_(new shaka.player.OfflineVideoSource(groupId, estimator));
var abrManager = new shaka.media.SimpleAbrManager();
app.load_(new shaka.player.OfflineVideoSource(
groupId, estimator, abrManager));
};


Expand Down
4 changes: 3 additions & 1 deletion lib/media/i_abr_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ shaka.media.IAbrManager.prototype.destroy = function() {};
/**
* Starts the AbrManager.
*
* @param {!shaka.util.IBandwidthEstimator} estimator
* @param {!shaka.player.IVideoSource} videoSource
* @expose
*/
shaka.media.IAbrManager.prototype.start = function() {};
shaka.media.IAbrManager.prototype.start = function(estimator, videoSource) {};


/**
Expand Down
27 changes: 18 additions & 9 deletions lib/media/simple_abr_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

goog.provide('shaka.media.SimpleAbrManager');

goog.require('shaka.asserts');
goog.require('shaka.log');
goog.require('shaka.media.IAbrManager');
goog.require('shaka.player.AudioTrack');
Expand All @@ -32,19 +33,17 @@ goog.require('shaka.util.IBandwidthEstimator');
* Creates a SimpleAbrManager, which selects video tracks using a basic set of
* heuristics.
*
* @param {!shaka.util.IBandwidthEstimator} estimator
* @param {!shaka.player.IVideoSource} videoSource
*
* @struct
* @constructor
* @implements {shaka.media.IAbrManager}
* @export
*/
shaka.media.SimpleAbrManager = function(estimator, videoSource) {
/** @private {!shaka.util.IBandwidthEstimator} */
this.estimator_ = estimator;
shaka.media.SimpleAbrManager = function() {
/** @private {shaka.util.IBandwidthEstimator} */
this.estimator_ = null;

/** @private {!shaka.player.IVideoSource} */
this.videoSource_ = videoSource;
/** @private {shaka.player.IVideoSource} */
this.videoSource_ = null;

/** @private {!shaka.util.EventManager} */
this.eventManager_ = new shaka.util.EventManager();
Expand Down Expand Up @@ -126,7 +125,13 @@ shaka.media.SimpleAbrManager.prototype.destroy = function() {


/** @override */
shaka.media.SimpleAbrManager.prototype.start = function() {
shaka.media.SimpleAbrManager.prototype.start = function(estimator,
videoSource) {
if (this.estimator_ && this.videoSource_) return;

this.estimator_ = estimator;
this.videoSource_ = videoSource;

this.nextAdaptationTime_ =
Date.now() + shaka.media.SimpleAbrManager.FIRST_SWITCH_INTERVAL_;
this.eventManager_.listen(this.estimator_, 'bandwidth',
Expand All @@ -144,6 +149,10 @@ shaka.media.SimpleAbrManager.prototype.enable = function(enabled) {

/** @override */
shaka.media.SimpleAbrManager.prototype.getInitialVideoTrackId = function() {
shaka.asserts.assert(this.videoSource_);
shaka.asserts.assert(this.estimator_);
if (!this.videoSource_ || !this.estimator_) return null;

var chosen = this.chooseVideoTrack_();
return chosen ? chosen.id : null;
};
Expand Down
11 changes: 8 additions & 3 deletions lib/player/dash_video_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ goog.provide('shaka.player.DashVideoSource');
goog.require('shaka.dash.MpdProcessor');
goog.require('shaka.dash.MpdRequest');
goog.require('shaka.dash.mpd');
goog.require('shaka.media.IAbrManager');
goog.require('shaka.media.SimpleAbrManager');
goog.require('shaka.player.DrmSchemeInfo');
goog.require('shaka.player.StreamVideoSource');
goog.require('shaka.util.EWMA');
Expand Down Expand Up @@ -48,6 +50,7 @@ goog.require('shaka.util.TypedBind');
* interpretContentProtection A callback to interpret the ContentProtection
* elements in the MPD.
* @param {shaka.util.IBandwidthEstimator} estimator
* @param {shaka.media.IAbrManager} abrManager
*
* @fires shaka.player.DashVideoSource.SeekRangeChanged
*
Expand All @@ -57,14 +60,16 @@ goog.require('shaka.util.TypedBind');
* @export
*/
shaka.player.DashVideoSource =
function(mpdUrl, interpretContentProtection, estimator) {
function(mpdUrl, interpretContentProtection, estimator, abrManager) {
if (!estimator) {
// For backward compatibility, provide an instance of the default
// implementation if none is provided.
estimator = new shaka.util.EWMABandwidthEstimator();
}

shaka.player.StreamVideoSource.call(this, null, estimator);
if (!abrManager) {
abrManager = new shaka.media.SimpleAbrManager();
}
shaka.player.StreamVideoSource.call(this, null, estimator, abrManager);

/** @private {string} */
this.mpdUrl_ = mpdUrl;
Expand Down
11 changes: 9 additions & 2 deletions lib/player/offline_video_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ goog.require('shaka.dash.MpdRequest');
goog.require('shaka.dash.mpd');
goog.require('shaka.log');
goog.require('shaka.media.EmeManager');
goog.require('shaka.media.IAbrManager');
goog.require('shaka.media.SimpleAbrManager');
goog.require('shaka.media.StreamInfo');
goog.require('shaka.player.StreamVideoSource');
goog.require('shaka.util.ContentDatabase');
Expand All @@ -39,19 +41,24 @@ goog.require('shaka.util.TypedBind');
* @param {?number} groupId The unique ID of the group of streams
* in this source.
* @param {shaka.util.IBandwidthEstimator} estimator
* @param {shaka.media.IAbrManager} abrManager
*
* @struct
* @constructor
* @extends {shaka.player.StreamVideoSource}
* @export
*/
shaka.player.OfflineVideoSource = function(groupId, estimator) {
shaka.player.OfflineVideoSource = function(groupId, estimator, abrManager) {
if (!estimator) {
// For backward compatibility, provide an instance of the default
// implementation if none is provided.
estimator = new shaka.util.EWMABandwidthEstimator();
}
if (!abrManager) {
abrManager = new shaka.media.SimpleAbrManager();
}

shaka.player.StreamVideoSource.call(this, null, estimator);
shaka.player.StreamVideoSource.call(this, null, estimator, abrManager);

/** @private {?number} */
this.groupId_ = groupId;
Expand Down
7 changes: 4 additions & 3 deletions lib/player/stream_video_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ goog.require('shaka.util.Task');
* @param {shaka.media.ManifestInfo} manifestInfo The ManifestInfo, which may
* be modified by this VideoSource.
* @param {!shaka.util.IBandwidthEstimator} estimator
* @param {!shaka.media.IAbrManager} abrManager
*
* @listens shaka.media.Stream.EndedEvent
* @listens shaka.media.Stream.PleaseBufferEvent
Expand All @@ -61,7 +62,7 @@ goog.require('shaka.util.Task');
* @extends {shaka.util.FakeEventTarget}
* @export
*/
shaka.player.StreamVideoSource = function(manifestInfo, estimator) {
shaka.player.StreamVideoSource = function(manifestInfo, estimator, abrManager) {
shaka.util.FakeEventTarget.call(this, null);

/** @protected {shaka.media.ManifestInfo} */
Expand Down Expand Up @@ -130,7 +131,7 @@ shaka.player.StreamVideoSource = function(manifestInfo, estimator) {
this.stats_ = null;

/** @private {!shaka.media.IAbrManager} */
this.abrManager_ = new shaka.media.SimpleAbrManager(this.estimator, this);
this.abrManager_ = abrManager;
};
goog.inherits(shaka.player.StreamVideoSource, shaka.util.FakeEventTarget);

Expand Down Expand Up @@ -1010,6 +1011,7 @@ shaka.player.StreamVideoSource.prototype.createAndStartStreams_ = function() {
// would cause a seek.
this.originalPlaybackRate_ = this.video.playbackRate;
this.video.playbackRate = 0;
this.abrManager_.start(this.estimator, this);

/** @type {!Object.<string, !shaka.media.StreamInfo>} */
var selectedStreamInfosByType =
Expand Down Expand Up @@ -1038,7 +1040,6 @@ shaka.player.StreamVideoSource.prototype.createAndStartStreams_ = function() {
this.startTask_.append(
function() {
this.startStreams_(selectedStreamInfosByType);
this.abrManager_.start();
return [Promise.resolve()];
}.bind(this));

Expand Down

0 comments on commit f3e6003

Please sign in to comment.