Skip to content

Commit

Permalink
Avoid ewma sampling from failing on 0 duration segments. (#583)
Browse files Browse the repository at this point in the history
This fixes a divide-by-zero that caused the estimate to become NaN.

Fixes #582
  • Loading branch information
Sanborn Hilland authored and joeyparrish committed Nov 16, 2016
1 parent b147e1a commit 465206b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/abr/ewma.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ shaka.abr.Ewma = function(halfLife) {
*/
shaka.abr.Ewma.prototype.sample = function(weight, value) {
var adjAlpha = Math.pow(this.alpha_, weight);
this.estimate_ = value * (1 - adjAlpha) + adjAlpha * this.estimate_;
this.totalWeight_ += weight;
var newEstimate = value * (1 - adjAlpha) + adjAlpha * this.estimate_;

if (!isNaN(newEstimate)) {
this.estimate_ = newEstimate;
this.totalWeight_ += weight;
}
};


Expand Down
23 changes: 23 additions & 0 deletions test/abr/simple_abr_manager_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ describe('SimpleAbrManager', function() {
});
});

it('can handle 0 duration segments', function() {
var audioBandwidth = 5e5;
var videoBandwidth = 2e6;
var bytesPerSecond =
sufficientBWMultiplier * (audioBandwidth + videoBandwidth) / 8.0;

abrManager.chooseStreams(streamSetsByType);

// 0 duration segment shouldn't cause us to get stuck on the lowest variant
abrManager.segmentDownloaded(1000, 1000, bytesPerSecond);
abrManager.segmentDownloaded(2000, 3000, bytesPerSecond);

abrManager.enable();

abrManager.segmentDownloaded(4000, 5000, bytesPerSecond);

expect(switchCallback).toHaveBeenCalled();
expect(switchCallback.calls.argsFor(0)[0]).toEqual({
'audio': jasmine.objectContaining({bandwidth: audioBandwidth}),
'video': jasmine.objectContaining({bandwidth: videoBandwidth})
});
});

it('picks lowest audio Stream when there is insufficient bandwidth',
function() {
// The lowest audio track will only be chosen if needed to fit the
Expand Down

0 comments on commit 465206b

Please sign in to comment.