Skip to content

Commit

Permalink
Make buffering runtime-configurable.
Browse files Browse the repository at this point in the history
Closes shaka-project#49.

Change-Id: I954e247e83a27c5f9f2e56bb7b57c714c1d8db71
  • Loading branch information
natalieharris authored and jonoblinkbox committed May 28, 2015
1 parent 68a0a63 commit 1753bb3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/media/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ shaka.media.Stream =
this.streamInfo_ = null;

/** @private {number} */
this.bufferingGoal_ = shaka.media.Stream.BUFFER_SIZE_SECONDS_;
this.initialBufferSize_ = 0;

/** @private {?function()} */
this.nextSwitch_ = null;
Expand Down Expand Up @@ -161,10 +161,9 @@ shaka.media.Stream.State_ = {
/**
* The amount of content we will try to buffer by default, after startup.
*
* @private {number}
* @const
* @type {number}
*/
shaka.media.Stream.BUFFER_SIZE_SECONDS_ = 15.0;
shaka.media.Stream.bufferSizeSeconds = 15.0;


/**
Expand Down Expand Up @@ -239,7 +238,7 @@ shaka.media.Stream.prototype.start = function(streamInfo, initialBufferSize) {
this.streamInfo_ = streamInfo;
this.type_ = streamInfo.mimeType.split('/')[0];
this.state_ = Stream.State_.INITIALIZING;
this.bufferingGoal_ = Math.max(this.bufferingGoal_, initialBufferSize);
this.initialBufferSize_ = initialBufferSize;

var expectedFirstTimestamp;

Expand Down Expand Up @@ -371,7 +370,8 @@ shaka.media.Stream.prototype.switch = function(streamInfo, immediate) {
} else {
var availableBitsPerSecond = this.estimator_.getBandwidth();
var requiredBitsPerSecond = streamInfo.bandwidth;
var estimatedFetchBits = this.bufferingGoal_ * requiredBitsPerSecond;
var estimatedFetchBits =
this.getBufferingGoal_() * requiredBitsPerSecond;
var estimatedFetchLatency =
estimatedFetchBits / availableBitsPerSecond;
var bufferedAhead =
Expand All @@ -389,7 +389,7 @@ shaka.media.Stream.prototype.switch = function(streamInfo, immediate) {

// Fetch new segments to meet the buffering requirement.
var segmentRange = streamInfo.segmentIndex.getRangeForInterval(
switchTime, this.bufferingGoal_);
switchTime, this.getBufferingGoal_());
if (!segmentRange) {
return Promise.reject(new Error('No segments available.'));
}
Expand Down Expand Up @@ -569,7 +569,7 @@ shaka.media.Stream.prototype.onUpdate_ = function() {

var currentTime = this.video_.currentTime;
var bufferedAhead = this.sbm_.bufferedAheadOf(currentTime);
if (bufferedAhead >= this.bufferingGoal_) {
if (bufferedAhead >= this.getBufferingGoal_()) {
// We don't need to make a request right now, so check again in a second.
this.updateTimerId_ = window.setTimeout(this.onUpdate_.bind(this), 1000);
return;
Expand Down Expand Up @@ -628,6 +628,19 @@ shaka.media.Stream.prototype.onUpdate_ = function() {
};


/**
* Returns the max of shaka.media.Stream.bufferSizeSeconds and
* the initalBufferSize of an instance.
*
* @return {number}
* @private
*/
shaka.media.Stream.prototype.getBufferingGoal_ = function() {
return Math.max(
shaka.media.Stream.bufferSizeSeconds, this.initialBufferSize_);
};


/**
* Returns the index of the SegmentReference corresponding to the first
* unbuffered segment starting at |time|. -1 is returned if there are no
Expand Down
22 changes: 22 additions & 0 deletions lib/player/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,28 @@ shaka.player.Player.prototype.seek = function(seconds) {
};


/**
* @param {number} bufferSize The amount of content streams will try to buffer
* in seconds.
* @export
*/
shaka.player.Player.prototype.setStreamBufferSize = function(bufferSize) {
shaka.asserts.assert(bufferSize >= 0);
var buffer = (bufferSize < 0) ? 0 : bufferSize;
shaka.media.Stream.bufferSizeSeconds = buffer;
};


/**
* Returns the seconds of content streams will try to buffer.
* @return {number}
* @export
*/
shaka.player.Player.prototype.getStreamBufferSize = function() {
return shaka.media.Stream.bufferSizeSeconds;
};


/**
* @param {number} timeout Timeout for LicenseRequests in ms.
* @export
Expand Down
24 changes: 24 additions & 0 deletions spec/player_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

goog.require('shaka.dash.MpdRequest');
goog.require('shaka.media.Stream');
goog.require('shaka.player.DashVideoSource');
goog.require('shaka.player.Player');
goog.require('shaka.polyfill.installAll');
Expand Down Expand Up @@ -669,6 +670,29 @@ describe('Player', function() {
});
});

it('sets buffer size on streams', function(done) {
player.setStreamBufferSize(5);
var mediaSource = new MediaSource();
video.src = window.URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', function() {
var buffer = mediaSource.addSourceBuffer(
'video/mp4; codecs="avc1.4d4015"');
var stream = new shaka.media.Stream(
player, video, mediaSource, buffer, estimator);
expect(stream.getBufferingGoal_()).toEqual(5);
video.src = '';
player.setStreamBufferSize(15);
done();
});
});

it('gets streams buffer size', function() {
player.setStreamBufferSize(5);
expect(player.getStreamBufferSize()).toEqual(5);
player.setStreamBufferSize(15);
expect(player.getStreamBufferSize()).toEqual(15);
});

it('sets timeout on LicenseRequests', function() {
player.setLicenseRequestTimeout(5);
var request = new shaka.util.LicenseRequest(
Expand Down

0 comments on commit 1753bb3

Please sign in to comment.