Skip to content

Commit

Permalink
handle switch stream properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrelltle committed Dec 3, 2022
1 parent bb601d8 commit ca32f5a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
37 changes: 27 additions & 10 deletions lib/media/segment_prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ shaka.media.SegmentPrefetch = class {
const logPrefix = shaka.media.SegmentPrefetch.logPrefix_(
this.stream_,
);
if (!this.stream_.segmentIndex) {
shaka.log.info(
logPrefix, 'missing segmentIndex',
);
return;
}
const currTime = segmentIterator.current().startTime;
const maxTime = Math.max(currTime, this.prefetchPosTime_);
const it = this.stream_.segmentIndex.getIteratorForTime(maxTime);
Expand Down Expand Up @@ -122,6 +128,14 @@ shaka.media.SegmentPrefetch = class {
return op;
}

/**
* Clear all segment data.
* @public
*/
clearAll() {
this.clearWithinRange(0, 0);
}

/**
* Clear prefetched segment data within given time range.
* @param {number} startTime
Expand All @@ -148,24 +162,27 @@ shaka.media.SegmentPrefetch = class {
}

/**
* Clear all segment data.
* Reset the prefetchLimit and clear all internal states.
* Called by StreamingEngine when configure() was called.
* @param {number} prefetchLimit
* @public
*/
clearAll() {
this.clearWithinRange(0, 0);
resetLimit(prefetchLimit) {
if (prefetchLimit !== this.prefetchLimit_) {
this.clearAll();
this.prefetchLimit_ = prefetchLimit;
}
}

/**
* Reset the prefetchLimit and clear all internal states.
* Called by StreamingEngine when configure() was called.
* @param {number} prefetchLimit.
* Called by Streaming Engine when switching variant.
* @param {shaka.extern.Stream} stream
* @public
*/
reset(prefetchLimit) {
if (prefetchLimit !== this.prefetchLimit_) {
switchStream(stream) {
if (stream && stream !== this.stream_) {
this.clearAll();
this.prefetchLimit_ = prefetchLimit;
this.prefetchPosTime_ = 0;
this.stream_ = stream;
}
}

Expand Down
17 changes: 14 additions & 3 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ shaka.media.StreamingEngine = class {
const autoReset = true;
this.failureCallbackBackoff_ =
new shaka.net.Backoff(failureRetryParams, autoReset);

// Allow configuring the segment prefetch in middle of the playback.
for (const type of this.mediaStates_.keys()) {
const state = this.mediaStates_.get(type);
if (state.segmentPrefetch) {
state.segmentPrefetch.reset(config.segmentPrefetchLimit);
state.segmentPrefetch.resetLimit(config.segmentPrefetchLimit);
} else {
state.segmentPrefetch = this.createSegmentPrefetch_(state.stream);
}
Expand Down Expand Up @@ -450,6 +450,10 @@ shaka.media.StreamingEngine = class {
return;
}

if (mediaState.segmentPrefetch) {
mediaState.segmentPrefetch.switchStream(stream);
}

if (stream.type == ContentType.TEXT) {
// Mime types are allowed to change for text streams.
// Reinitialize the text parser, but only if we are going to fetch the
Expand Down Expand Up @@ -2123,7 +2127,8 @@ shaka.media.StreamingEngine = class {
const duration = this.playerInterface_.mediaSourceEngine.getDuration();
if (mediaState.segmentPrefetch) {
mediaState.segmentPrefetch.clearWithinRange(
presentationTime + safeMargin, duration,
presentationTime + safeMargin,
duration,
);
}
await this.playerInterface_.mediaSourceEngine.remove(
Expand All @@ -2140,11 +2145,17 @@ shaka.media.StreamingEngine = class {
mediaState.type);
}
}

if (mediaState.segmentPrefetch) {
mediaState.segmentPrefetch.clearAll();
}

this.destroyer_.ensureNotDestroyed();

shaka.log.debug(logPrefix, 'cleared buffer');
mediaState.clearingBuffer = false;
mediaState.endOfStream = false;

this.scheduleUpdate_(mediaState, 0);
}

Expand Down

0 comments on commit ca32f5a

Please sign in to comment.