Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Feb 10, 2021
1 parent 150464e commit 18a5476
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/segment-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,30 @@ export default class SegmentLoader extends videojs.EventTarget {
// equal to the last appended mediaIndex
if (this.mediaIndex !== null) {
this.mediaIndex -= mediaSequenceDiff;

/*
// mediaIndex is invalid, set it to null
if (this.mediaIndex < 0) {
this.resyncLoader();
} else {
const segment = this.playlist_.segments[this.mediaIndex];
// partIndex should remain the same for the same segment
// unless parts fell off of the playlist for this segment.
// In that case we need to reset partIndex and resync
if (this.partIndex && (!segment.parts || !segment.parts.length || !segment.parts[this.partIndex])) {
console.log(`part fell off on part ${this.partIndex}`);
this.partIndex = null;
this.remove(0, Infinity);
// clears fmp4 captions
if (this.transmuxer_) {
this.transmuxer_.postMessage({
action: 'clearAllMp4Captions'
});
}
}
}*/
}

// update the mediaIndex on the SegmentInfo object
Expand Down
142 changes: 142 additions & 0 deletions test/loader-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,148 @@ export const LoaderCommonFactory = ({
});
});

QUnit.test('mediaIndex and partIndex are used', function(assert) {
const appendPart = (segmentIndex, partIndex) => {
this.clock.tick(1);

assert.equal(
this.requests[0].url,
`segment${segmentIndex}.part${partIndex}.ts`,
`requested mediaIndex #${segmentIndex} partIndex #${partIndex}`
);
standardXHRResponse(this.requests.shift(), testData());

if (usesAsyncAppends) {
return new Promise((resolve, reject) => {
loader.one('appended', resolve);
loader.one('error', reject);
});
}

return Promise.resolve();
};

return setupMediaSource(loader.mediaSource_, loader.sourceUpdater_).then(() => {
loader.playlist(playlistWithDuration(50, {
mediaSequence: 0,
endList: false,
llhls: true
}));

loader.load();
loader.mediaIndex = 2;
return Promise.resolve();
}).then(() => appendPart(2, 0))
.then(() => appendPart(2, 1))
.then(() => appendPart(2, 2))
.then(() => appendPart(2, 3))
.then(() => appendPart(2, 4))
.then(() => appendPart(3, 0));
});

QUnit.test('mediaIndex and partIndex survive playlist change', function(assert) {
const appendPart = (segmentIndex, partIndex) => {
this.clock.tick(1);

assert.equal(
this.requests[0].url,
`segment${segmentIndex}.part${partIndex}.ts`,
`requested mediaIndex #${segmentIndex} partIndex #${partIndex}`
);
standardXHRResponse(this.requests.shift(), testData());

if (usesAsyncAppends) {
return new Promise((resolve, reject) => {
loader.one('appended', resolve);
loader.one('error', reject);
});
}

return Promise.resolve();
};

return setupMediaSource(loader.mediaSource_, loader.sourceUpdater_).then(() => {
loader.playlist(playlistWithDuration(50, {
mediaSequence: 0,
endList: false,
llhls: true
}));

loader.load();
loader.mediaIndex = 4;
return Promise.resolve();
}).then(() => appendPart(4, 0))
.then(() => appendPart(4, 1))
.then(() => appendPart(4, 2))
.then(() => {

// Update the playlist shifting the mediaSequence by 2 which will result
// in a decrement of the mediaIndex by 2 to 1
loader.playlist(playlistWithDuration(50, {
mediaSequence: 2,
endList: false,
llhls: true
}));
// verify that we still try to append the next part for that segment.
return appendPart(2, 3);
}).then(() => appendPart(2, 4));
});

QUnit.test('drops partIndex if playlist update drops parts', function(assert) {
const appendPart = (segmentIndex, partIndex) => {
this.clock.tick(1);

assert.equal(
this.requests[0].url,
`segment${segmentIndex}.part${partIndex}.ts`,
`requested mediaIndex #${segmentIndex} partIndex #${partIndex}`
);
standardXHRResponse(this.requests.shift(), testData());

if (usesAsyncAppends) {
return new Promise((resolve, reject) => {
loader.one('appended', resolve);
loader.one('error', reject);
});
}

return Promise.resolve();
};

return setupMediaSource(loader.mediaSource_, loader.sourceUpdater_).then(() => {
loader.playlist(playlistWithDuration(50, {
mediaSequence: 0,
endList: false,
llhls: true
}));

loader.load();
loader.mediaIndex = 4;
return Promise.resolve();
}).then(() => appendPart(4, 0))
.then(() => appendPart(4, 1))
.then(() => appendPart(4, 2))
.then(() => {

// Update the playlist shifting the mediaSequence by 2 which will result
// in a decrement of the mediaIndex by 2 to 1
loader.playlist(playlistWithDuration(50, {
mediaSequence: 4,
endList: false,
llhls: true
}));

assert.equal(loader.partIndex, null, 'partIndex was dropped');
this.clock.tick(1);

assert.equal(
this.requests[0].url,
'1.ts',
'requested mediaIndex 1 only'
);
});
});

QUnit.test('segment 404s should trigger an error', function(assert) {
const errors = [];

Expand Down
1 change: 1 addition & 0 deletions test/segment-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ QUnit.module('SegmentLoader', function(hooks) {
this.clock.tick(1);

assert.equal(loader.mediaIndex, null, 'mediaIndex reset by seek to seekable');
assert.equal(loader.partIndex, null, 'partIndex reset by seek to seekable');
assert.equal(syncInfoUpdates, 1, 'syncinfoupdate was triggered');
});
});
Expand Down
30 changes: 24 additions & 6 deletions test/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ export const playlistWithDuration = function(time, conf) {

result.id = result.uri;

const count = Math.floor(time / 10);
const count = Math.ceil(time / 10);
const remainder = time % 10;
let i;
const isEncrypted = conf && conf.isEncrypted;
Expand All @@ -447,19 +447,37 @@ export const playlistWithDuration = function(time, conf) {
timeline++;
discontinuityStartsIndex++;
}

result.segments.push({
const segment = {
uri: i + extension,
resolvedUri: i + extension,
duration: 10,
// last segment will be less then 10 if duration is uneven
duration: i + 1 === count && remainder ? remainder : 10,
timeline
});
};

if (isEncrypted) {
result.segments[i].key = {
segment.key = {
uri: i + '-key.php',
resolvedUri: i + '-key.php'
};
}
if (conf && conf.llhls) {
// only add parts for the last 3 segments
if ((count - i) <= 3) {
segment.parts = [];
for (let z = 0; z < 5; z++) {
const uri = `segment${i}.part${z}${extension}`;

segment.parts.push({
uri,
resolvedUri: uri,
duration: 2
});
}
}

}
result.segments.push(segment);
}
if (remainder) {
result.segments.push({
Expand Down

0 comments on commit 18a5476

Please sign in to comment.