Skip to content

Commit

Permalink
fix: Follow the configured playRange for src= (#7825)
Browse files Browse the repository at this point in the history
When 'playRangeStart' and/or 'playRangeEnd' is specified in the config,
limit the actual play range to this range.

In particular, this means stop the video after playRangeEnd. This
implies that 'video.ended' is no longer a good marker of the end of the
video.
  • Loading branch information
avelad authored and joeyparrish committed Jan 6, 2025
1 parent f77850c commit 3f413d9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/cast/cast_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ shaka.cast.CastUtils.PlayerGetterMethods = {
'getLoadMode': 10,
'getManifestType': 10,
'isFullyLoaded': 1,
'isEnded': 1,
};


Expand Down
59 changes: 48 additions & 11 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.preloadDueAdManagerTimer_.stop();
if (!this.preloadDueAdManager_) {
this.preloadDueAdManagerVideo_ = this.video_;
this.preloadDueAdManagerVideoEnded_ = this.video_.ended;
this.preloadDueAdManagerVideoEnded_ = this.isEnded();
const saveLivePosition = /** @type {boolean} */(
e['saveLivePosition']) || false;
this.preloadDueAdManager_ = await this.detachAndSavePreload(
Expand Down Expand Up @@ -2767,7 +2767,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
if (this.manifest_.nextUrl) {
if (this.config_.streaming.preloadNextUrlWindow > 0) {
const onTimeUpdate = async () => {
const timeToEnd = this.video_.duration - this.video_.currentTime;
const timeToEnd = this.seekRange().end - this.video_.currentTime;
if (!isNaN(timeToEnd)) {
if (timeToEnd <= this.config_.streaming.preloadNextUrlWindow) {
this.loadEventManager_.unlisten(
Expand Down Expand Up @@ -4491,10 +4491,16 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
// seekable range. This covers both plain mp4s and native HLS playbacks.
if (this.video_ && this.video_.src) {
const seekable = this.video_.seekable;
if (seekable.length) {
if (seekable && seekable.length) {
const playRangeStart =
this.config_ ? this.config_.playRangeStart : 0;
const start = Math.max(seekable.start(0), playRangeStart);
const playRangeEnd =
this.config_ ? this.config_.playRangeEnd : Infinity;
const end = Math.min(seekable.end(seekable.length - 1), playRangeEnd);
return {
'start': seekable.start(0),
'end': seekable.end(seekable.length - 1),
'start': start,
'end': end,
};
}
}
Expand Down Expand Up @@ -5920,7 +5926,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {

const ContentType = shaka.util.ManifestParserUtils.ContentType;

let duration = this.video_.duration;
const seekRange = this.seekRange();
let duration = seekRange.end - seekRange.start;
if (this.manifest_) {
duration = this.manifest_.presentationTimeline.getDuration();
}
Expand Down Expand Up @@ -6034,7 +6041,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}

const ContentType = shaka.util.ManifestParserUtils.ContentType;
let duration = this.video_.duration;
const seekRange = this.seekRange();
let duration = seekRange.end - seekRange.start;
if (this.manifest_) {
duration = this.manifest_.presentationTimeline.getDuration();
}
Expand Down Expand Up @@ -6801,7 +6809,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
let updateState = 'playing';
if (this.bufferObserver_.getState() == State.STARVING) {
updateState = 'buffering';
} else if (this.video_.ended) {
} else if (this.isEnded()) {
updateState = 'ended';
} else if (this.video_.paused) {
updateState = 'paused';
Expand Down Expand Up @@ -7016,7 +7024,20 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
return false;
};

const completionRatio = this.video_.currentTime / this.video_.duration;
const checkEnded = () => {
if (this.config_ && this.config_.playRangeEnd != Infinity) {
// Make sure the video stops when we reach the end.
// This is required when there is a custom playRangeEnd specified.
if (this.isEnded()) {
this.video_.pause();
}
}
};

const seekRange = this.seekRange();
const duration = seekRange.end - seekRange.start;
const completionRatio =
duration > 0 ? this.video_.currentTime / duration : 0;

if (isNaN(completionRatio)) {
return;
Expand All @@ -7039,6 +7060,9 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
} else if (isQuartile(100, percent)) {
event = shaka.Player.makeEvent_(
shaka.util.FakeEvent.EventName.Complete);
checkEnded();
} else {
checkEnded();
}

if (event) {
Expand Down Expand Up @@ -8139,7 +8163,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {

// This is a strong guarantee that we are buffered to the end, because it
// means the playhead is already at that end.
if (this.video_.ended) {
if (this.isEnded()) {
return true;
}

Expand Down Expand Up @@ -8179,7 +8203,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {

// This is a strong guarantee that we are buffered to the end, because it
// means the playhead is already at that end.
if (this.video_.ended) {
if (this.isEnded()) {
return true;
}

Expand Down Expand Up @@ -8220,6 +8244,19 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}
return this.video_.remote.state != 'disconnected';
}

/**
* Indicate if the video has ended.
*
* @return {boolean}
* @export
*/
isEnded() {
if (!this.video_ || this.video_.ended) {
return true;
}
return this.video_.currentTime >= this.seekRange().end;
}
};

/**
Expand Down
4 changes: 4 additions & 0 deletions ui/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,10 @@ shaka.ui.Controls = class extends shaka.util.FakeEventTarget {
}

if (this.presentationIsPaused()) {
// If we are at the end, go back to the beginning.
if (this.player_.isEnded()) {
this.video_.currentTime = this.player_.seekRange().start;
}
this.video_.play();
} else {
this.video_.pause();
Expand Down
2 changes: 1 addition & 1 deletion ui/play_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ shaka.ui.PlayButton = class extends shaka.ui.Element {
return false;
}

return this.video.ended;
return this.player ? this.player.isEnded() : true;
}

/**
Expand Down

0 comments on commit 3f413d9

Please sign in to comment.