Skip to content

Commit

Permalink
Fix cast.media.Media#getEstimatedTime
Browse files Browse the repository at this point in the history
  • Loading branch information
hensm committed Apr 27, 2021
1 parent c627028 commit c117241
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions ext/src/shim/cast/media/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class Media {
#isActive = true;
#updateListeners = new Set<UpdateListener>();
#sendMediaMessageCallbacks = new Map<string, Callbacks>();
#lastCurrentTime?: number;
#lastCurrentTime = 0;

#listener = onMessage(message => {
if ((message as any).data._id !== this.#id) {
Expand Down Expand Up @@ -194,12 +194,21 @@ export default class Media {
}

public getEstimatedTime(): number {
if (this.currentTime === undefined
|| this.#lastCurrentTime === undefined) {
return 0;
if (this.playerState === PlayerState.PLAYING) {
let estimatedTime = this.currentTime + (this.playbackRate * (
Date.now() - this.#lastCurrentTime) / 1000);

// Enforce valid range
if (this.media?.duration && estimatedTime > this.media.duration) {
estimatedTime = this.media.duration;
} else if (estimatedTime < 0) {
estimatedTime = 0;
}

return estimatedTime;
}

return this.currentTime + ((Date.now() / 1000) - this.#lastCurrentTime);
return this.currentTime;
}

public getStatus(
Expand Down

0 comments on commit c117241

Please sign in to comment.