Skip to content

Commit

Permalink
feat: let back buffer be configurable
Browse files Browse the repository at this point in the history
Introduces a new config option BACK_BUFFER_LENGTH that defaults to 30,
the previous value.
When set, we trim the back buffer according to the provided value.
  • Loading branch information
Lars Erik G-K authored and gkatsev committed Jun 19, 2020
1 parent e50f4c9 commit 8c96e6c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
GOAL_BUFFER_LENGTH: 30,
MAX_GOAL_BUFFER_LENGTH: 60,
BACK_BUFFER_LENGTH: 30,
GOAL_BUFFER_LENGTH_RATE: 1,
// 0.5 MB/s
INITIAL_BANDWIDTH: 4194304,
Expand Down
6 changes: 3 additions & 3 deletions src/segment-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const illegalMediaSwitch = (loaderType, startingMedia, trackInfo) => {
};

/**
* Calculates a time value that is safe to remove from the back buffer without interupting
* Calculates a time value that is safe to remove from the back buffer without interrupting
* playback.
*
* @param {TimeRange} seekable
Expand All @@ -63,15 +63,15 @@ export const illegalMediaSwitch = (loaderType, startingMedia, trackInfo) => {
* @param {number} targetDuration
* The target duration of the current playlist
* @return {number}
* Time that is safe to remove from the back buffer without interupting playback
* Time that is safe to remove from the back buffer without interrupting playback
*/
export const safeBackBufferTrimTime = (seekable, currentTime, targetDuration) => {
// 30 seconds before the playhead provides a safe default for trimming.
//
// Choosing a reasonable default is particularly important for high bitrate content and
// VOD videos/live streams with large windows, as the buffer may end up overfilled and
// throw an APPEND_BUFFER_ERR.
let trimTime = currentTime - 30;
let trimTime = currentTime - Config.BACK_BUFFER_LENGTH;

if (seekable.length) {
// Some live playlists may have a shorter window of content than the full allowed back
Expand Down
3 changes: 2 additions & 1 deletion src/videojs-http-streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ const Vhs = {
xhr: xhrFactory()
};

// Define getter/setters for config properites
// Define getter/setters for config properties
[
'GOAL_BUFFER_LENGTH',
'MAX_GOAL_BUFFER_LENGTH',
'BACK_BUFFER_LENGTH',
'GOAL_BUFFER_LENGTH_RATE',
'BUFFER_LOW_WATER_LINE',
'MAX_BUFFER_LOW_WATER_LINE',
Expand Down
32 changes: 30 additions & 2 deletions test/configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,34 @@ QUnit.test('MAX_GOAL_BUFFER_LENGTH set warning and invalid', function(assert) {
assert.equal(Config.MAX_GOAL_BUFFER_LENGTH, 60, 'default');
});

QUnit.test('BACK_BUFFER_LENGTH get warning', function(assert) {
assert.equal(
Vhs.BACK_BUFFER_LENGTH,
Config.BACK_BUFFER_LENGTH,
'Vhs.BACK_BUFFER_LENGTH returns the default'
);
assert.equal(this.env.log.warn.calls, 1, 'logged a warning');
});

QUnit.test('BACK_BUFFER_LENGTH set warning', function(assert) {
Vhs.BACK_BUFFER_LENGTH = 10;
assert.equal(this.env.log.warn.calls, 1, 'logged a warning');

assert.equal(Config.BACK_BUFFER_LENGTH, 10, 'returns what we set it to');
});

QUnit.test('BACK_BUFFER_LENGTH set warning and invalid', function(assert) {
Vhs.BACK_BUFFER_LENGTH = 'nope';
assert.equal(this.env.log.warn.calls, 2, 'logged two warnings');

assert.equal(Config.BACK_BUFFER_LENGTH, 30, 'default');

Vhs.BACK_BUFFER_LENGTH = -1;
assert.equal(this.env.log.warn.calls, 2, 'logged two warnings');

assert.equal(Config.BACK_BUFFER_LENGTH, 30, 'default');
});

QUnit.test('GOAL_BUFFER_LENGTH_RATE get warning', function(assert) {
assert.equal(
Vhs.GOAL_BUFFER_LENGTH_RATE,
Expand Down Expand Up @@ -390,7 +418,7 @@ options.forEach((opt) => {
assert.deepEqual(
vhs.options_[opt.name],
opt.test,
`${opt.name} should be equal to sourchHandler option`
`${opt.name} should be equal to sourceHandler option`
);
});

Expand All @@ -412,7 +440,7 @@ options.forEach((opt) => {
assert.deepEqual(
vhs.options_[opt.name],
opt.test,
`${opt.name} should be equal to sourchHandler option`
`${opt.name} should be equal to sourceHandler option`
);
});
});

0 comments on commit 8c96e6c

Please sign in to comment.