Skip to content

Commit

Permalink
Fix: Discard only invalid periods
Browse files Browse the repository at this point in the history
Discard only invalid periods instead of all obsolete periods to support modification of previous periods
  • Loading branch information
echoy-harmonicinc committed Aug 26, 2021
1 parent 1c64e20 commit 94d696f
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions lib/dash/dash_parser.js
Expand Up @@ -102,10 +102,10 @@ shaka.dash.DashParser = class {
this.largestPeriodStartTime_ = null;

/**
* Period IDs that have seen before.
* Period start times seen in previous manifest.
* @private {!Array.<number>}
*/
this.seenPeriodIds_ = [];
this.seenPeriodStartTime_ = [];

/**
* The minimum of the availabilityTimeOffset values among the adaptation
Expand Down Expand Up @@ -542,6 +542,7 @@ shaka.dash.DashParser = class {
mpd, 'mediaPresentationDuration', XmlUtils.parseDuration);

const periods = [];
const periodStartTime = [];
let prevEnd = 0;
const periodNodes = XmlUtils.findChildren(mpd, 'Period');
// This uses a for-loop rather than a for-of loop because this needs to look
Expand Down Expand Up @@ -583,21 +584,34 @@ shaka.dash.DashParser = class {
periodDuration = givenDuration;
}

// Skip all periods with start time < maximum period start time, excepts
// the last period in manifest
if (this.largestPeriodStartTime_ !== null && start !== null &&
start < this.largestPeriodStartTime_ &&
!this.seenPeriodIds_.includes(start) &&
/**
* This is to improve the robustness of the player
* when it received bad content/manifest, while still supporting
* modification of previous periods.
*
* Skip periods that match all of the following criteria:
* - Start time is earlier than latest period start time ever seen
* - Start time is never seen in the previous manifest
* - Not the last period in the manifest
*
* Periods that meet the aforementioned criteria are considered invalid
* and should be safe to discard.
*/
if (this.largestPeriodStartTime_ !== null && start !== null) {
if (start < this.largestPeriodStartTime_ &&
!this.seenPeriodStartTime_.includes(start) &&
i + 1 != periodNodes.length) {
shaka.log.debug(
`Skipping Period ${i+1} as its start time is smaller than ` +
'the largest period start time that has been seen, and start ' +
'time is unseen before');
continue;
} else if (!this.seenPeriodIds_.includes(start)) {
this.seenPeriodIds_.push(start);
shaka.log.debug(
`Skipping Period ${i+1} as its start time is smaller than ` +
'the largest period start time that has been seen, and start ' +
'time is unseen before');
continue;
} else {
periodStartTime.push(start);
}
}


// Save maximum period start time if it is the last period
if (start !== null &&
(this.largestPeriodStartTime_ === null ||
Expand Down Expand Up @@ -636,6 +650,9 @@ shaka.dash.DashParser = class {
prevEnd = start + periodDuration;
} // end of period parsing loop

// Replace previous seen start time with the current one.
this.seenPeriodStartTime_ = periodStartTime;

if (presentationDuration != null) {
if (prevEnd != presentationDuration) {
shaka.log.warning(
Expand Down

0 comments on commit 94d696f

Please sign in to comment.