From c1172410f9cab66b8df263ff22d458ced1048797 Mon Sep 17 00:00:00 2001 From: hensm Date: Tue, 27 Apr 2021 08:48:51 +0100 Subject: [PATCH] Fix cast.media.Media#getEstimatedTime --- ext/src/shim/cast/media/Media.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ext/src/shim/cast/media/Media.ts b/ext/src/shim/cast/media/Media.ts index 1bc682c..a4057f2 100644 --- a/ext/src/shim/cast/media/Media.ts +++ b/ext/src/shim/cast/media/Media.ts @@ -75,7 +75,7 @@ export default class Media { #isActive = true; #updateListeners = new Set(); #sendMediaMessageCallbacks = new Map(); - #lastCurrentTime?: number; + #lastCurrentTime = 0; #listener = onMessage(message => { if ((message as any).data._id !== this.#id) { @@ -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(