Skip to content

Commit

Permalink
fix: Allow change the native playbackRate with the same functionality…
Browse files Browse the repository at this point in the history
… as trickPlay (#7993)
  • Loading branch information
avelad committed Feb 5, 2025
1 parent d1e64c2 commit be36d90
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -4687,7 +4687,6 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
shaka.log.alwaysWarn('A trick play rate of 0 is unsupported!');
return;
}
this.trickPlayEventManager_.removeAll();

this.playRateController_.set(rate);

Expand All @@ -4696,26 +4695,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.streamingEngine_.setTrickPlay(
useTrickPlayTrack && Math.abs(rate) > 1);
}
if (this.isLive()) {
this.trickPlayEventManager_.listen(this.video_, 'timeupdate', () => {
const currentTime = this.video_.currentTime;
const seekRange = this.seekRange();
const safeSeekOffset = this.config_.streaming.safeSeekOffset;

// Cancel trick play if we hit the beginning or end of the seekable
// (Sub-second accuracy not required here)
if (rate > 0) {
if (Math.floor(currentTime) >= Math.floor(seekRange.end)) {
this.cancelTrickPlay();
}
} else {
if (Math.floor(currentTime) <=
Math.floor(seekRange.start + safeSeekOffset)) {
this.cancelTrickPlay();
}
}
});
}
this.setupTrickPlayEventListeners_(rate);
}

/**
Expand Down Expand Up @@ -6801,13 +6781,48 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
// The playback rate has changed. This could be us or someone else.
// If this was us, setting the rate again will be a no-op.
this.playRateController_.set(newRate);

if (this.loadMode_ == shaka.Player.LoadMode.MEDIA_SOURCE) {
this.abrManager_.playbackRateChanged(newRate);
}
this.setupTrickPlayEventListeners_(newRate);
}

const event = shaka.Player.makeEvent_(
shaka.util.FakeEvent.EventName.RateChange);
this.dispatchEvent(event);
}

/**
* Configures all the necessary listeners when trick play is being performed.
*
* @param {number} rate
* @private
*/
setupTrickPlayEventListeners_(rate) {
this.trickPlayEventManager_.removeAll();
if (this.isLive()) {
this.trickPlayEventManager_.listen(this.video_, 'timeupdate', () => {
const currentTime = this.video_.currentTime;
const seekRange = this.seekRange();
const safeSeekOffset = this.config_.streaming.safeSeekOffset;

// Cancel trick play if we hit the beginning or end of the seekable
// (Sub-second accuracy not required here)
if (rate > 0) {
if (Math.floor(currentTime) >= Math.floor(seekRange.end)) {
this.cancelTrickPlay();
}
} else {
if (Math.floor(currentTime) <=
Math.floor(seekRange.start + safeSeekOffset)) {
this.cancelTrickPlay();
}
}
});
}
}

/**
* Try updating the state history. If the player has not finished
* initializing, this will be a no-op.
Expand Down

0 comments on commit be36d90

Please sign in to comment.