Skip to content

Commit

Permalink
Fix/segment abandon (#4297)
Browse files Browse the repository at this point in the history
* Fix handling of the AbandonRequest rule.
  • Loading branch information
dsilhavy committed Oct 11, 2023
1 parent 536371a commit 425fa72
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Expand Up @@ -705,6 +705,8 @@ declare namespace dashjs {

getNextSegmentRequest(mediaInfo: MediaInfo, representation: Representation): FragmentRequest | null;

repeatSegmentRequest(mediaInfo: MediaInfo, representation: Representation): FragmentRequest | null;

getValidTimeCloseToTargetTime(time: number, mediaInfo: MediaInfo, representation: Representation, targetThreshold: number): number;

getValidTimeAheadOfTargetTime(time: number, mediaInfo: MediaInfo, representation: Representation): number;
Expand Down
34 changes: 24 additions & 10 deletions src/dash/DashHandler.js
Expand Up @@ -276,14 +276,27 @@ function DashHandler(config) {
* @return {FragmentRequest|null}
*/
function getNextSegmentRequest(mediaInfo, representation) {
let request = null;

if (!representation || !representation.segmentInfoType) {
return null;
}

let indexToRequest = lastSegment ? lastSegment.index + 1 : 0;

return _getRequest(mediaInfo, representation, indexToRequest);
}

function repeatSegmentRequest(mediaInfo, representation) {
if (!representation || !representation.segmentInfoType) {
return null;
}

let indexToRequest = lastSegment ? lastSegment.index : 0;

return _getRequest(mediaInfo, representation, indexToRequest);
}

function _getRequest(mediaInfo, representation, indexToRequest) {
let request = null;
const segment = segmentsController.getSegmentByIndex(representation, indexToRequest, lastSegment ? lastSegment.mediaStartTime : -1);

// No segment found
Expand Down Expand Up @@ -391,18 +404,19 @@ function DashHandler(config) {
}

instance = {
initialize,
getStreamId,
getType,
getStreamInfo,
getInitRequest,
getSegmentRequestForTime,
getCurrentIndex,
getInitRequest,
getNextSegmentRequest,
getNextSegmentRequestIdempotent,
getSegmentRequestForTime,
getStreamId,
getStreamInfo,
getType,
getValidTimeAheadOfTargetTime,
initialize,
isLastSegmentRequested,
repeatSegmentRequest,
reset,
getNextSegmentRequestIdempotent,
getValidTimeAheadOfTargetTime
};

setup();
Expand Down
29 changes: 29 additions & 0 deletions src/streaming/StreamProcessor.js
Expand Up @@ -86,6 +86,7 @@ function StreamProcessor(config) {
scheduleController,
representationController,
shouldUseExplicitTimeForRequest,
shouldRepeatRequest,
qualityChangeInProgress,
dashHandler,
segmentsController,
Expand Down Expand Up @@ -194,6 +195,7 @@ function StreamProcessor(config) {

bufferingTime = 0;
shouldUseExplicitTimeForRequest = false;
shouldRepeatRequest = false;
}

function getStreamId() {
Expand All @@ -213,6 +215,7 @@ function StreamProcessor(config) {
mediaInfo = null;
bufferingTime = 0;
shouldUseExplicitTimeForRequest = false;
shouldRepeatRequest = false;
qualityChangeInProgress = false;
pendingSwitchToRepresentationInfo = null;
}
Expand Down Expand Up @@ -432,6 +435,7 @@ function StreamProcessor(config) {
let request = _getFragmentRequest();
if (request) {
shouldUseExplicitTimeForRequest = false;
shouldRepeatRequest = false;
_mediaRequestGenerated(request);
} else {
_noMediaRequestGenerated(rescheduleIfNoRequest);
Expand Down Expand Up @@ -546,6 +550,8 @@ function StreamProcessor(config) {

if (shouldUseExplicitTimeForRequest) {
request = dashHandler.getSegmentRequestForTime(mediaInfo, representation, bufferingTime);
} else if (shouldRepeatRequest) {
request = dashHandler.repeatSegmentRequest(mediaInfo, representation);
} else {
request = dashHandler.getNextSegmentRequest(mediaInfo, representation);
}
Expand Down Expand Up @@ -657,6 +663,11 @@ function StreamProcessor(config) {
_prepareForForceReplacementQualitySwitch(representationInfo);
}

// We abandoned a current request
else if (e && e.reason && e.reason.forceAbandon) {
_prepareForAbandonQualitySwitch(representationInfo)
}

// If fast switch is enabled we check if we are supposed to replace existing stuff in the buffer
else if (settings.get().streaming.buffer.fastSwitchEnabled) {
_prepareForFastQualitySwitch(representationInfo);
Expand Down Expand Up @@ -690,14 +701,32 @@ function StreamProcessor(config) {
bufferController.prepareForForceReplacementQualitySwitch(representationInfo)
.then(() => {
_bufferClearedForReplacement();
pendingSwitchToRepresentationInfo = null;
qualityChangeInProgress = false;
})
.catch(() => {
_bufferClearedForReplacement();
pendingSwitchToRepresentationInfo = null;
qualityChangeInProgress = false;
});
}

function _prepareForAbandonQualitySwitch(representationInfo) {
bufferController.updateBufferTimestampOffset(representationInfo)
.then(() => {
fragmentModel.abortRequests();
shouldRepeatRequest = true;
scheduleController.setCheckPlaybackQuality(false);
scheduleController.startScheduleTimer();
qualityChangeInProgress = false;
pendingSwitchToRepresentationInfo = null;
})
.catch(() => {
pendingSwitchToRepresentationInfo = null;
qualityChangeInProgress = false;
})
}

function _prepareForFastQualitySwitch(representationInfo) {
// if we switch up in quality and need to replace existing parts in the buffer we need to adjust the buffer target
const time = playbackController.getTime();
Expand Down
4 changes: 4 additions & 0 deletions src/streaming/rules/abr/ABRRulesCollection.js
Expand Up @@ -213,6 +213,10 @@ function ABRRulesCollection(config) {
const activeRules = _getRulesWithChange(abandonRequestArray);
const shouldAbandon = getMinSwitchRequest(activeRules);

if (shouldAbandon) {
shouldAbandon.reason.forceAbandon = true
}

return shouldAbandon || SwitchRequest(context).create();
}

Expand Down

0 comments on commit 425fa72

Please sign in to comment.