Implement missing cast media estimated break time methods

This commit is contained in:
hensm
2022-08-24 22:59:56 +01:00
parent 8db4df0ed8
commit d71d564f91
3 changed files with 75 additions and 40 deletions

View File

@@ -32,6 +32,7 @@ import { ErrorCode } from "../enums";
import { ErrorCallback, SuccessCallback, UpdateListener } from "../../types"; import { ErrorCallback, SuccessCallback, UpdateListener } from "../../types";
import { SenderMediaMessage } from "../types"; import { SenderMediaMessage } from "../types";
import { getEstimatedTime } from "../../utils";
export const NS_MEDIA = "urn:x-cast:com.google.cast.media"; export const NS_MEDIA = "urn:x-cast:com.google.cast.media";
@@ -92,45 +93,67 @@ export default class Media {
.catch(errorCallback); .catch(errorCallback);
} }
/**
* Estimates the current break clip position based on the last
* information reported by the receiver.
*/
getEstimatedBreakClipTime() { getEstimatedBreakClipTime() {
logger.info("STUB :: Media#getEstimatedBreakClipTime"); if (!this.breakStatus?.currentBreakClipTime) return;
const currentBreakClip = this.media?.breakClips?.find(
breakClip => breakClip.id === this.breakStatus?.breakClipId
);
if (!currentBreakClip) return;
return getEstimatedTime({
currentTime: this.breakStatus.currentBreakClipTime,
lastUpdateTime: this._lastUpdateTime,
duration: currentBreakClip.duration
});
} }
/**
* Estimates the current break position based on the last
* information reported by the receiver.
*/
getEstimatedBreakTime() { getEstimatedBreakTime() {
logger.info("STUB :: Media#getEstimatedBreakTime"); if (!this.breakStatus?.currentBreakTime) return;
const currentBreak = this.media?.breaks?.find(
break_ => break_.id === this.breakStatus?.breakId
);
if (!currentBreak) return;
return getEstimatedTime({
currentTime: this.breakStatus.currentBreakTime,
lastUpdateTime: this._lastUpdateTime,
duration: currentBreak.duration
});
} }
getEstimatedLiveSeekableRange() { getEstimatedLiveSeekableRange() {
logger.info("STUB :: Media#getEstimatedLiveSeekableRange"); logger.info("STUB :: Media#getEstimatedLiveSeekableRange");
} }
/** /**
* Estimate the current playback position based on the last * Estimates the current playback position based on the last
* time reported by the receiver and the current playback * information reported by the receiver.
* rate.
*/ */
getEstimatedTime(): number { getEstimatedTime(): number {
if (this.playerState === PlayerState.PLAYING && this._lastUpdateTime) { if (this.playerState === PlayerState.PLAYING && this._lastUpdateTime) {
let estimatedTime = return getEstimatedTime({
this.currentTime + (Date.now() - this._lastUpdateTime) / 1000; currentTime: this.currentTime,
lastUpdateTime: this._lastUpdateTime,
// Enforce valid range duration: this.media?.duration
if (estimatedTime < 0) { });
estimatedTime = 0;
} else if (
this.media?.duration &&
estimatedTime > this.media.duration
) {
estimatedTime = this.media.duration;
}
return estimatedTime;
} }
return this.currentTime; return this.currentTime;
} }
/** /**
* Request media status from the receiver application. This * Request media status from the receiver application. This will
* will also trigger any added media update listeners. * also trigger any added media update listeners.
*/ */
getStatus( getStatus(
getStatusRequest = new GetStatusRequest(), getStatusRequest = new GetStatusRequest(),

View File

@@ -27,3 +27,22 @@ export function hasRequiredCapabilities(
} }
}); });
} }
interface GetEstimatedTimeOpts {
currentTime: number;
lastUpdateTime: number;
duration?: Nullable<number>;
}
export function getEstimatedTime(opts: GetEstimatedTimeOpts) {
let estimatedTime =
opts.currentTime + (Date.now() - opts.lastUpdateTime) / 1000;
// Enforce valid range
if (estimatedTime < 0) {
estimatedTime = 0;
} else if (opts.duration && estimatedTime > opts.duration) {
estimatedTime = opts.duration;
}
return estimatedTime;
}

View File

@@ -14,6 +14,7 @@
const _ = browser.i18n.getMessage; const _ = browser.i18n.getMessage;
import deviceStore from "./deviceStore"; import deviceStore from "./deviceStore";
import { getEstimatedTime } from "../../cast/utils";
const dispatch = createEventDispatcher<{ const dispatch = createEventDispatcher<{
togglePlayback: void; togglePlayback: void;
@@ -99,7 +100,7 @@
// Keep track of update times for currentTime estimations // Keep track of update times for currentTime estimations
let lastUpdateTime = 0; let lastUpdateTime = 0;
let currentTime = getEstimatedTime(); let currentTime = getEstimatedMediaTime();
deviceStore.subscribe(devices => { deviceStore.subscribe(devices => {
const newDevice = devices.find(newDevice => newDevice.id === device.id); const newDevice = devices.find(newDevice => newDevice.id === device.id);
@@ -112,8 +113,8 @@
// Update estimated time every second // Update estimated time every second
onMount(() => { onMount(() => {
const intervalId = window.setInterval(() => { const intervalId = window.setInterval(() => {
if (currentTime !== getEstimatedTime()) { if (currentTime !== getEstimatedMediaTime()) {
currentTime = getEstimatedTime(); currentTime = getEstimatedMediaTime();
} }
}, 1000); }, 1000);
@@ -126,23 +127,15 @@
* Estimates the current playback position based on the last status * Estimates the current playback position based on the last status
* update. * update.
*/ */
function getEstimatedTime() { function getEstimatedMediaTime() {
if (!status.currentTime) return 0; if (!status.currentTime || !lastUpdateTime) return 0;
if (status.playerState === PlayerState.PLAYING && lastUpdateTime) { if (status.playerState === PlayerState.PLAYING) {
let estimatedTime = return getEstimatedTime({
status.currentTime + (Date.now() - lastUpdateTime) / 1000; currentTime: status.currentTime,
lastUpdateTime,
if (estimatedTime < 0) { duration: status.media?.duration
estimatedTime = 0; });
} else if (
status.media?.duration &&
estimatedTime > status.media.duration
) {
estimatedTime = status.media.duration;
}
return estimatedTime;
} }
return status.currentTime; return status.currentTime;