Skip to content

Commit

Permalink
Allow redundant switches from AbrManager
Browse files Browse the repository at this point in the history
Working toward a solution for #435

Change-Id: I662c06c21eab7c5b6f2af85236088f1712ee7a72
joeyparrish committed Jul 22, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9fd3d36 commit 9eadb61
Showing 3 changed files with 41 additions and 48 deletions.
32 changes: 18 additions & 14 deletions lib/abr/simple_abr_manager.js
Original file line number Diff line number Diff line change
@@ -243,23 +243,27 @@ shaka.abr.SimpleAbrManager.prototype.suggestStreams_ = function() {
}
}

var oldAudio = this.streamsByType_['audio'];
var oldVideo = this.streamsByType_['video'];
var chosen = this.chooseStreams_();
if (chosen['audio'] != oldAudio || chosen['video'] != oldVideo) {
var opt_leaveInBuffer = undefined;
if (oldVideo && chosen.video &&
chosen.video.bandwidth > oldVideo.bandwidth) {
opt_leaveInBuffer = shaka.abr.SimpleAbrManager.UPGRADE_LEAVE_IN_BUFFER_;
}
var currentBandwidthKbps =
Math.round(this.bandwidthEstimator_.getBandwidthEstimate() / 1000.0);
shaka.log.debug(
'Calling switch_()...',
'bandwidth=' + currentBandwidthKbps + ' kbps',
'opt_leaveInBuffer=', opt_leaveInBuffer);
this.switch_(chosen, opt_leaveInBuffer);

// Do not clear the buffer.
var opt_leaveInBuffer = undefined;
if (oldVideo && chosen.video &&
chosen.video.bandwidth > oldVideo.bandwidth) {
// We're upgrading video.
// Leave some in buffer, but clear ahead of that.
opt_leaveInBuffer = shaka.abr.SimpleAbrManager.UPGRADE_LEAVE_IN_BUFFER_;
}

var currentBandwidthKbps =
Math.round(this.bandwidthEstimator_.getBandwidthEstimate() / 1000.0);
shaka.log.debug(
'Calling switch_()...',
'bandwidth=' + currentBandwidthKbps + ' kbps',
'opt_leaveInBuffer=', opt_leaveInBuffer);
// If any of these chosen streams are already chosen, Player will filter them
// out before passing the choices on to StreamingEngine.
this.switch_(chosen, opt_leaveInBuffer);
};


30 changes: 23 additions & 7 deletions lib/player.js
Original file line number Diff line number Diff line change
@@ -1483,6 +1483,10 @@ shaka.Player.prototype.canSwitch_ = function() {
*/
shaka.Player.prototype.switch_ = function(streamsByType, opt_leaveInBuffer) {
shaka.log.debug('switch_');
goog.asserts.assert(this.config_.abr.enabled,
'AbrManager should not call switch while disabled!');
goog.asserts.assert(!this.switchingPeriods_,
'AbrManager should not call switch while transitioning between Periods!');

// We have adapted to a new stream, record it in the history. Only add if
// we are actually switching the stream.
@@ -1496,17 +1500,29 @@ shaka.Player.prototype.switch_ = function(streamsByType, opt_leaveInBuffer) {
type: type,
fromAdaptation: true
});
} else {
// If it's the same, remove it from the map.
// This allows us to avoid onAdaptation_() when nothing has changed.
delete streamsByType[type];
}
}

if (this.streamingEngine_) {
for (var type in streamsByType) {
this.streamingEngine_.switch(type, streamsByType[type],
// Only apply opt_leaveInBuffer to video streams.
type == 'video' ? opt_leaveInBuffer : undefined);
}
this.onAdaptation_();
if (shaka.util.MapUtils.empty(streamsByType)) {
// There's nothing to change.
return;
}

if (!this.streamingEngine_) {
// There's no way to change it.
return;
}

for (var type in streamsByType) {
this.streamingEngine_.switch(type, streamsByType[type],
// Only apply opt_leaveInBuffer to video streams.
type == 'video' ? opt_leaveInBuffer : undefined);
}
this.onAdaptation_();
};


27 changes: 0 additions & 27 deletions test/abr/simple_abr_manager_unit.js
Original file line number Diff line number Diff line change
@@ -271,33 +271,6 @@ describe('SimpleAbrManager', function() {
}).catch(fail).then(done);
});

it('does not call switchCallback() if no changes are needed', function(done) {
// Simulate some segments being downloaded just above the needed bandwidth
// for the least stream.
var audioBandwidth = 5e5;
var videoBandwidth = 5e5;
var bytesPerSecond = 1.1 * (audioBandwidth + videoBandwidth) / 8.0;

abrManager.chooseStreams(streamSetsByType);

abrManager.segmentDownloaded(0, 1000, bytesPerSecond);
abrManager.segmentDownloaded(1000, 2000, bytesPerSecond);

abrManager.enable();

// Move outside the startup interval.
loop = shaka.test.Util.fakeEventLoop(
startupInterval + 1, originalSetTimeout);
loop.then(function() {
// Make another call to segmentDownloaded(). switchCallback() will not be
// called because the best streams for the available bandwidth are already
// active.
abrManager.segmentDownloaded(3000, 4000, bytesPerSecond);

expect(switchCallback).not.toHaveBeenCalled();
}).catch(fail).then(done);
});

it('clears ahead on upgrade', function(done) {
// Simulate some segments being downloaded at a high rate, to trigger an
// upgrade.

0 comments on commit 9eadb61

Please sign in to comment.