Skip to content

Commit

Permalink
refactor: remove duplicate bufferIntersection code
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Jun 25, 2020
1 parent f879563 commit 90a856c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ranges.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const findSoleUncommonTimeRangesEnd = function(original, update) {
* @param {TimeRanges} bufferB
* @return {TimeRanges} The interesection of `bufferA` with `bufferB`
*/
const bufferIntersection = function(bufferA, bufferB) {
export const bufferIntersection = function(bufferA, bufferB) {
let start = null;
let end = null;
let arity = 0;
Expand Down
12 changes: 10 additions & 2 deletions src/source-updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import videojs from 'video.js';
import logger from './util/logger';
import noop from './util/noop';
import { buffered } from './util/buffer';
import { bufferIntersection } from './ranges.js';
import {getMimeForCodec} from '@videojs/vhs-utils/dist/codecs.js';

const updating = (type, sourceUpdater) => {
Expand Down Expand Up @@ -322,7 +322,15 @@ export default class SourceUpdater extends videojs.EventTarget {
}

buffered() {
return buffered(this.videoBuffer, this.audioBuffer);
if (this.audioBuffer && !this.videoBuffer) {
return this.audioBuffered();
}

if (this.videoBuffer && !this.audioBuffer) {
return this.videoBuffered();
}

return bufferIntersection(this.audioBuffered(), this.videoBuffered());
}

setDuration(duration, doneFn = noop) {
Expand Down
50 changes: 49 additions & 1 deletion test/ranges.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Ranges from '../src/ranges';
import videojs from 'video.js';
import QUnit from 'qunit';

const createTimeRanges = videojs.createTimeRanges;

const rangesEqual = (rangeOne, rangeTwo) => {
Expand Down Expand Up @@ -496,3 +495,52 @@ QUnit.test('converts time ranges to an array', function(assert) {
'formats ranges correctly'
);
});

QUnit.module('bufferIntersection');

QUnit.test('returns intersection of two buffers', function(assert) {
const a = createTimeRanges([[0, 5], [12, 100]]);
const b = createTimeRanges([[4, 5], [10, 101]]);

assert.ok(
rangesEqual(Ranges.bufferIntersection(a, b), createTimeRanges([[4, 5], [12, 100]])),
'returns intersection'
);
});

QUnit.test('returns empty when no buffers', function(assert) {
assert.ok(
rangesEqual(Ranges.bufferIntersection(null, null), createTimeRanges()),
'returns empty'
);
});

QUnit.test('returns empty when buffers are empty', function(assert) {
const a = createTimeRanges();
const b = createTimeRanges();

assert.ok(
rangesEqual(Ranges.bufferIntersection(a, b), createTimeRanges()),
'returns empty'
);
});

QUnit.test('returns empty when one buffer is empty', function(assert) {
const a = createTimeRanges([[0, 1], [2, 3]]);
const b = createTimeRanges();

assert.ok(
rangesEqual(Ranges.bufferIntersection(a, b), createTimeRanges()),
'returns empty'
);
});

QUnit.test('returns empty when other buffer empty', function(assert) {
const a = createTimeRanges();
const b = createTimeRanges([[0, 1], [2, 3]]);

assert.ok(
rangesEqual(Ranges.bufferIntersection(a, b), createTimeRanges()),
'returns empty'
);
});

0 comments on commit 90a856c

Please sign in to comment.