Skip to content

Commit

Permalink
perf(DASH): Delete old matchedStreams (#7301)
Browse files Browse the repository at this point in the history
Issue: #6239
  • Loading branch information
avelad authored and joeyparrish committed Sep 13, 2024
1 parent 71a8414 commit 729afb1
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,9 @@ shaka.dash.DashParser = class {
});
for (const pId of diffPeriodsIDs) {
for (const contextId of this.indexStreamMap_[pId]) {
if (this.periodCombiner_) {
this.periodCombiner_.deleteStream(this.streamMap_[contextId]);
}
delete this.streamMap_[contextId];
}
delete this.indexStreamMap_[pId];
Expand Down
42 changes: 42 additions & 0 deletions lib/util/periods.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,48 @@ shaka.util.PeriodCombiner = class {
return this.imageStreams_;
}

/**
* Deletes a stream from matchedStreams because it is no longer needed
*
* @param {?shaka.extern.Stream} stream
*
* @export
*/
deleteStream(stream) {
if (!stream) {
return;
}
const ContentType = shaka.util.ManifestParserUtils.ContentType;
if (stream.type == ContentType.AUDIO) {
for (const audioStream of this.audioStreams_) {
audioStream.matchedStreams = audioStream.matchedStreams.filter((s) => {
return s !== stream;
});
}
} else if (stream.type == ContentType.VIDEO) {
for (const videoStream of this.videoStreams_) {
videoStream.matchedStreams = videoStream.matchedStreams.filter((s) => {
return s !== stream;
});
}
} else if (stream.type == ContentType.TEXT) {
for (const textStream of this.textStreams_) {
textStream.matchedStreams = textStream.matchedStreams.filter((s) => {
return s !== stream;
});
}
} else if (stream.type == ContentType.IMAGE) {
for (const imageStream of this.imageStreams_) {
imageStream.matchedStreams = imageStream.matchedStreams.filter((s) => {
return s !== stream;
});
}
}
if (stream.segmentIndex) {
stream.closeSegmentIndex();
}
}

/**
* Returns an object that contains arrays of streams by type
* @param {!Array<shaka.extern.Period>} periods
Expand Down
70 changes: 70 additions & 0 deletions test/util/periods_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,76 @@ describe('PeriodCombiner', () => {
expect(video4.originalId).toBe('7,8');
});

it('Delete old matchedStreams', async () => {
const stream1 = makeVideoStream(1080);
stream1.originalId = '1';
stream1.bandwidth = 120000;
stream1.codecs = 'hvc1.1.4.L126.B0';

const stream2 = makeVideoStream(1080);
stream2.originalId = '2';
stream2.bandwidth = 120000;
stream2.codecs = 'hev1.2.4.L123.B0';

const stream3 = makeVideoStream(1080);
stream3.originalId = '3';
stream3.bandwidth = 120000;
stream3.codecs = 'dvhe.05.01';

const stream4 = makeVideoStream(1080);
stream4.originalId = '4';
stream4.bandwidth = 120000;
stream4.codecs = 'dvh1.05.01';

/** @type {!Array.<shaka.extern.Period>} */
const periods = [
{
id: '0',
videoStreams: [
stream1, stream3,
],
audioStreams: [],
textStreams: [],
imageStreams: [],
},
{
id: '1',
videoStreams: [
stream2, stream4,
],
audioStreams: [],
textStreams: [],
imageStreams: [],
},
];

await combiner.combinePeriods(periods, /* isDynamic= */ true);
let variants = combiner.getVariants();
expect(variants.length).toBe(2);

let video1 = variants[0].video;
expect(video1.originalId).toBe('1,2');
expect(video1.matchedStreams.length).toBe(2);

let video2 = variants[1].video;
expect(video2.originalId).toBe('3,4');
expect(video2.matchedStreams.length).toBe(2);

combiner.deleteStream(stream1);
combiner.deleteStream(stream3);

variants = combiner.getVariants();
expect(variants.length).toBe(2);

video1 = variants[0].video;
expect(video1.originalId).toBe('1,2');
expect(video1.matchedStreams.length).toBe(1);

video2 = variants[1].video;
expect(video2.originalId).toBe('3,4');
expect(video2.matchedStreams.length).toBe(1);
});

it('Variant has highest bandwidth from matched streams', async () => {
const stream1 = makeVideoStream(1080);
stream1.originalId = '1';
Expand Down

0 comments on commit 729afb1

Please sign in to comment.