Skip to content

Commit

Permalink
feat: Evict instead delete on prefetch (#6404)
Browse files Browse the repository at this point in the history
This change gives better performance when playing low latency streams
  • Loading branch information
avelad committed Apr 5, 2024
1 parent 83d3f51 commit 0fc5814
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 38 deletions.
28 changes: 6 additions & 22 deletions lib/media/segment_prefetch.js
Expand Up @@ -25,9 +25,8 @@ shaka.media.SegmentPrefetch = class {
* @param {number} prefetchLimit
* @param {shaka.extern.Stream} stream
* @param {shaka.media.SegmentPrefetch.FetchDispatcher} fetchDispatcher
* @param {boolean} deleteOnGet
*/
constructor(prefetchLimit, stream, fetchDispatcher, deleteOnGet = true) {
constructor(prefetchLimit, stream, fetchDispatcher) {
/** @private {number} */
this.prefetchLimit_ = prefetchLimit;

Expand All @@ -40,9 +39,6 @@ shaka.media.SegmentPrefetch = class {
/** @private {shaka.media.SegmentPrefetch.FetchDispatcher} */
this.fetchDispatcher_ = fetchDispatcher;

/** @private {boolean} */
this.deleteOnGet_ = deleteOnGet;

/**
* @private {!Map.<
* !(shaka.media.SegmentReference),
Expand Down Expand Up @@ -185,9 +181,6 @@ shaka.media.SegmentPrefetch = class {
if (streamDataCallback) {
segmentPrefetchOperation.setStreamDataCallback(streamDataCallback);
}
if (this.deleteOnGet_) {
prefetchMap.delete(reference);
}
if (reference instanceof shaka.media.SegmentReference) {
shaka.log.debug(
logPrefix,
Expand Down Expand Up @@ -242,14 +235,17 @@ shaka.media.SegmentPrefetch = class {

/**
* @param {number} time
* @param {boolean=} clearInitSegments
*/
evict(time) {
evict(time, clearInitSegments = false) {
for (const ref of this.segmentPrefetchMap_.keys()) {
if (time > ref.endTime) {
this.abortPrefetchedSegment_(ref);
}
}
this.clearInitSegments_();
if (clearInitSegments) {
this.clearInitSegments_();
}
}

/**
Expand Down Expand Up @@ -296,24 +292,12 @@ shaka.media.SegmentPrefetch = class {
this.clearInitSegments_();
}

/**
* Update deleteOnGet.
* @param {boolean} newDeleteOnGet
* @public
*/
deleteOnGet(newDeleteOnGet) {
this.deleteOnGet_ = newDeleteOnGet;
}

/**
* Called by Streaming Engine when switching variant.
* @param {shaka.extern.Stream} stream
* @public
*/
switchStream(stream) {
goog.asserts.assert(this.deleteOnGet_,
'switchStream should only be used if deleteOnGet is true');

if (stream && stream !== this.stream_) {
this.clearAll();
this.stream_ = stream;
Expand Down
8 changes: 2 additions & 6 deletions lib/media/streaming_engine.js
Expand Up @@ -1030,10 +1030,6 @@ shaka.media.StreamingEngine = class {
(lang) => LanguageUtils.areLanguageCompatible(
variant.audio.language, lang))
) {
// if we're here, we are switching back to single stream prefetch,
// so we want to switch back to delete on get, even if the prefetch
// may end up getting deleted below.
prefetch.deleteOnGet(true);
const type = /** @type {!shaka.util.ManifestParserUtils.ContentType}*/
(variant.audio.type);
const mediaState = this.mediaStates_.get(type);
Expand Down Expand Up @@ -1064,7 +1060,6 @@ shaka.media.StreamingEngine = class {
// use the helper to create a segment prefetch to ensure that existing
// objects are reused.
const segmentPrefetch = this.createSegmentPrefetch_(variant.audio);
segmentPrefetch.deleteOnGet(false);

// if a segment prefetch wasn't created, skip the rest
if (!segmentPrefetch) {
Expand Down Expand Up @@ -1245,7 +1240,7 @@ shaka.media.StreamingEngine = class {
// evict all prefetched segments that are before the presentationTime
for (const stream of this.audioPrefetchMap_.keys()) {
const prefetch = this.audioPrefetchMap_.get(stream);
prefetch.evict(presentationTime);
prefetch.evict(presentationTime, /* clearInitSegments= */ true);
prefetch.prefetchSegmentsByTime(presentationTime);
}
}
Expand Down Expand Up @@ -1355,6 +1350,7 @@ shaka.media.StreamingEngine = class {

if (mediaState.segmentPrefetch && mediaState.segmentIterator &&
!this.audioPrefetchMap_.has(mediaState.stream)) {
mediaState.segmentPrefetch.evict(presentationTime);
mediaState.segmentPrefetch.prefetchSegmentsByTime(reference.startTime);
}

Expand Down
2 changes: 0 additions & 2 deletions test/media/segment_prefetch_unit.js
Expand Up @@ -160,7 +160,6 @@ describe('SegmentPrefetch', () => {

describe('evict', () => {
it('does not evict a segment that straddles the given time', async () => {
segmentPrefetch.deleteOnGet(false);
segmentPrefetch.prefetchSegmentsByTime(references[0].startTime);
segmentPrefetch.evict(5);
await expectSegmentsPrefetched(0);
Expand All @@ -177,7 +176,6 @@ describe('SegmentPrefetch', () => {
});

it('segments that end before the provided time', async () => {
segmentPrefetch.deleteOnGet(false);
segmentPrefetch.prefetchSegmentsByTime(references[0].startTime);
segmentPrefetch.evict(21);
for (let i = 0; i < 2; i++) {
Expand Down
8 changes: 0 additions & 8 deletions test/test/util/fake_segment_prefetch.js
Expand Up @@ -39,9 +39,6 @@ shaka.test.FakeSegmentPrefetch = class {
/** @private {number} */
this.prefetchPosTime_ = 0;

/** @private {boolean} */
this.deleteOnGet_ = true;

/** @private {number} */
this.segmentNum_ = 0;
}
Expand Down Expand Up @@ -97,11 +94,6 @@ shaka.test.FakeSegmentPrefetch = class {
this.evictions_.push(time);
}

/** @override */
deleteOnGet(newDeleteOnGet) {
this.deleteOnGet_ = newDeleteOnGet;
}

/**
* @override
* @param {shaka.media.InitSegmentReference|
Expand Down

0 comments on commit 0fc5814

Please sign in to comment.