Skip to content

Commit

Permalink
Add BufferHelper.filterLivingFragments util
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jan 29, 2018
1 parent 204ff18 commit 117a143
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 8 deletions.
48 changes: 41 additions & 7 deletions src/helper/buffer-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@
*/

const BufferHelper = {
isBuffered : function(media,position) {
/**
* filter fragments potentially evicted from buffer.
* @param {{startPTS:number, endPTS:number}[]} bufferedFrags
* @param {HTMLMediaElement} media
* @returns {{startPTS:number, endPTS:number}[]}
*/
filterLivingFragments: function(bufferedFrags, media) {
try {
const mediaBuffered = media.buffered;
return bufferedFrags.filter(frag => {
return this.bufferedIncludesPosition(mediaBuffered, (frag.startPTS + frag.endPTS) / 2);
});
} catch (error) {
// InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer':
// This SourceBuffer has been removed from the parent media source
}
return [];
},
/**
* If `media`'s buffered include `position`, return true.
* @param {HTMLMediaElement} media
* @param {number} position
* @returns {boolean}
*/
isBuffered: function(media,position) {
try {
if (media) {
let buffered = media.buffered;
for (let i = 0; i < buffered.length; i++) {
if (position >= buffered.start(i) && position <= buffered.end(i)) {
return true;
}
}
let mediaBuffered = media.buffered;
return this.bufferedIncludesPosition(mediaBuffered, position);
}
} catch(error) {
// this is to catch
Expand All @@ -20,6 +40,20 @@ const BufferHelper = {
}
return false;
},
/**
* If `mediaBuffered` includes `position`, return true.
* @param {TimeRanges} mediaBuffered
* @param {number} position
* @returns {boolean}
*/
bufferedIncludesPosition(mediaBuffered, position){
for (let i = 0; i < mediaBuffered.length; i++) {
if (position >= mediaBuffered.start(i) && position <= mediaBuffered.end(i)) {
return true;
}
}
return false;
},

bufferInfo : function(media, pos,maxHoleDuration) {
try {
Expand Down
108 changes: 107 additions & 1 deletion tests/unit/helper/buffer-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,112 @@ function createMockBuffer(buffered) {
}

describe('BufferHelper', function() {
describe('filterLivingFragments', function() {
it("should return empty array if the media is invalid", () => {
const invalidMedia = {
get buffered() {
throw new Error("InvalidStateError");
}
};
const fragments = [
{
startPTS: 0,
endPTS: 0.5
},
{
startPTS: 1,
endPTS: 2.0
}
];
const filteredFragments = BufferHelper.filterLivingFragments(fragments, invalidMedia);
assert.equal(filteredFragments.length, 0);
});
it("should return fragments that are not evicted", () => {
// |__________|//////////|//////////|__________|
// 0 1.0 2.0 3.0 4.0
const media = {
get buffered() {
return createMockBuffer([
{
startPTS: 1,
endPTS: 2.0
},
{
startPTS: 2.0,
endPTS: 3.0
}
]);
}
};
// |////|/////|//////////|//////////|//////////|
// 0 1.0 2.0 3.0 4.0
const fragments = [
// ↓ out of buffer ↓
{
startPTS: 0,
endPTS: 0.5
},
{
startPTS: 0.5,
endPTS: 1.0
},
// ↑ out of buffer ↑
{
startPTS: 1.0,
endPTS: 2.0
},
{
startPTS: 2.0,
endPTS: 3.0
},
// ↓ out of buffer ↓
{
startPTS: 3.0,
endPTS: 4.0
}
];
const filteredFragments = BufferHelper.filterLivingFragments(fragments, media);
assert.deepEqual(filteredFragments, [fragments[2], fragments[3]]);
});
});
describe('filterFragByMedia', function() {
const media = {
get buffered() {
return createMockBuffer([
{
startPTS: 0,
endPTS: 0.5
},
{
startPTS: 1,
endPTS: 2.0
},
]);
}
};

it('should return true if media.buffered throw error', function() {
const invalidMedia = {
get buffered() {
throw new Error("InvalidStateError");
}
};
assert.equal(BufferHelper.filterLivingFragments(invalidMedia, 0), false);
});
it('should return true if some media.buffered includes the position', function() {
assert.equal(BufferHelper.isBuffered(media, 0), true);
assert.equal(BufferHelper.isBuffered(media, 0.1), true);
assert.equal(BufferHelper.isBuffered(media, 0.5), true);
assert.equal(BufferHelper.isBuffered(media, 1), true);
assert.equal(BufferHelper.isBuffered(media, 2), true);
});
it('should return false if any media.buffered does not includes the position', function() {
assert.equal(BufferHelper.isBuffered(media, -0.1), false);
assert.equal(BufferHelper.isBuffered(media, 0.51), false);
assert.equal(BufferHelper.isBuffered(media, 0.9), false);
assert.equal(BufferHelper.isBuffered(media, 2.1), false);
});
});
describe('isBuffered', function() {
// |////////|__________|////////////////|
// 0 0.5 1 2.0
Expand Down Expand Up @@ -43,7 +149,7 @@ describe('BufferHelper', function() {
assert.equal(BufferHelper.isBuffered(media, 1), true);
assert.equal(BufferHelper.isBuffered(media, 2), true);
});
it('should return false if some media.buffered includes the position', function() {
it('should return false if any media.buffered does not includes the position', function() {
assert.equal(BufferHelper.isBuffered(media, -0.1), false);
assert.equal(BufferHelper.isBuffered(media, 0.51), false);
assert.equal(BufferHelper.isBuffered(media, 0.9), false);
Expand Down

0 comments on commit 117a143

Please sign in to comment.