Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Defer closeSegmentIndex() for old streams during ABR switches when segment fetches are ongoing #7157

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion demo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ shakaDemo.Config = class {
.addBoolInput_('Infinite Live Stream Duration',
'streaming.infiniteLiveStreamDuration')
.addBoolInput_('Clear decodingInfo cache on unload',
'streaming.clearDecodingCache');
'streaming.clearDecodingCache')
.addBoolInput_('Defer old stream cleanup during ABR switches',
'streaming.deferOldStreamCleanupDuringSwitch');
if (!shakaDemoMain.getNativeControlsEnabled()) {
this.addBoolInput_('Always Stream Text', 'streaming.alwaysStreamText');
} else {
Expand Down
11 changes: 10 additions & 1 deletion externs/shaka/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,8 @@ shaka.extern.LiveSyncConfiguration;
* loadTimeout: number,
* clearDecodingCache: boolean,
* dontChooseCodecs: boolean,
* shouldFixTimestampOffset: boolean
* shouldFixTimestampOffset: boolean,
* deferOldStreamCleanupDuringSwitch: boolean
* }}
*
* @description
Expand Down Expand Up @@ -1824,6 +1825,14 @@ shaka.extern.LiveSyncConfiguration;
* <br>
* Defaults to <code>false</code> except on Tizen, WebOS whose default value
* is <code>true</code>.
* @property {boolean} deferOldStreamCleanupDuringSwitch
* If true, defers cleanup of the old stream during an ABR stream switch
* until it is safe to cleanup.
* For example, if <code>StreamingEngine</code> is in the middle of a segment
* fetch using the old stream information when a switch happens, segment
* availability issues may arise if the cleanup is not deferred.
* <br>
* Defaults to <code>false</code>.
* @exportDoc
*/
shaka.extern.StreamingConfiguration;
Expand Down
43 changes: 39 additions & 4 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ shaka.media.StreamingEngine = class {
/** @private {?shaka.extern.StreamingConfiguration} */
this.config_ = null;

/**
* Retains deferred closeSegmentIndex() operations for streams which were
* switched away from during an ongoing fetchAndAppend_().
* These close operations are resumed and cleared from this map when the
* next onUpdate_() for a stream's content type occurs.
* This is because at that point, it is guaranteed no logic is actively
* using information for these old streams.
* @private {!Map.<string, !function()>}
*/
this.deferredCloseSegmentIndex_ = new Map();

/** @private {number} */
this.bufferingGoalScale_ = 1;

Expand Down Expand Up @@ -174,6 +185,7 @@ shaka.media.StreamingEngine = class {
this.playerInterface_ = null;
this.manifest_ = null;
this.config_ = null;
this.deferredCloseSegmentIndex_.clear();
}

/**
Expand Down Expand Up @@ -572,20 +584,34 @@ shaka.media.StreamingEngine = class {
fullMimeType, this.manifest_.sequenceMode, stream.external);
}

// Releases the segmentIndex of the old stream.
// Releases the segmentIndex of the old stream if safe to do so.
// Do not close segment indexes we are prefetching.
if (!this.audioPrefetchMap_.has(mediaState.stream)) {
if (mediaState.stream.closeSegmentIndex) {
mediaState.stream.closeSegmentIndex();
if (this.config_.deferOldStreamCleanupDuringSwitch &&
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
mediaState.performingUpdate) {
const oldStreamTag =
shaka.media.StreamingEngine.logPrefix_(mediaState);
// The ongoing update is still using the old stream's segment
// reference information.
// If we close the old stream now, the update will not complete
// correctly.
// onUpdate_() will resume the closeSegmentIndex() operation for the
// old stream when the update has finished.
this.deferredCloseSegmentIndex_.set(
oldStreamTag, mediaState.stream.closeSegmentIndex);
} else {
mediaState.stream.closeSegmentIndex();
}
}
}

mediaState.stream = stream;
mediaState.segmentIterator = null;
mediaState.adaptation = !!adaptation;

const streamTag = shaka.media.StreamingEngine.logPrefix_(mediaState);
shaka.log.debug('switch: switching to Stream ' + streamTag);
const newStreamTag = shaka.media.StreamingEngine.logPrefix_(mediaState);
shaka.log.debug('switch: switching to Stream ' + newStreamTag);

if (clearBuffer) {
if (mediaState.clearingBuffer) {
Expand Down Expand Up @@ -1168,6 +1194,15 @@ shaka.media.StreamingEngine = class {
mediaState.clearBufferSafeMargin);
return;
}
// Handle deferred closeSegmentIndex() operations.
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
for (const [key, value] of this.deferredCloseSegmentIndex_.entries()) {
const streamId = /** @type {string} */ (key);
const closeSegmentIndex = /** @type {!function()} */ (value);
if (streamId.includes(mediaState.type)) {
closeSegmentIndex();
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
}
this.deferredCloseSegmentIndex_.delete(streamId);
}

// Make sure the segment index exists. If not, create the segment index.
if (!mediaState.stream.segmentIndex) {
Expand Down
1 change: 1 addition & 0 deletions lib/util/player_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ shaka.util.PlayerConfiguration = class {
dontChooseCodecs: false,
shouldFixTimestampOffset: shaka.util.Platform.isWebOS() ||
shaka.util.Platform.isTizen(),
deferOldStreamCleanupDuringSwitch: false,
};

// WebOS, Tizen, Chromecast and Hisense have long hardware pipelines
Expand Down
Loading