2021-04-27 20:27:36 -06:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2021-2023 The Matrix.org Foundation C.I.C.
|
2021-04-27 20:27:36 -06:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-04-27 20:27:36 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2021-04-27 20:27:36 -06:00
|
|
|
import Clock from "./Clock";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type Playback, PlaybackState } from "../../../audio/Playback";
|
2021-06-21 15:18:34 -06:00
|
|
|
import { UPDATE_EVENT } from "../../../stores/AsyncStore";
|
2021-04-27 20:27:36 -06:00
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
playback: Playback;
|
2021-06-23 19:34:34 -06:00
|
|
|
|
|
|
|
|
// The default number of seconds to show when the playback has completed or
|
|
|
|
|
// has not started. Not used during playback, even when paused. Defaults to
|
|
|
|
|
// clip duration length.
|
|
|
|
|
defaultDisplaySeconds?: number;
|
2021-04-27 20:27:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
|
seconds: number;
|
|
|
|
|
durationSeconds: number;
|
|
|
|
|
playbackPhase: PlaybackState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A clock for a playback of a recording.
|
|
|
|
|
*/
|
|
|
|
|
export default class PlaybackClock extends React.PureComponent<IProps, IState> {
|
2023-02-13 11:39:16 +00:00
|
|
|
public constructor(props: IProps) {
|
2021-04-27 20:27:36 -06:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
seconds: this.props.playback.clockInfo.timeSeconds,
|
|
|
|
|
// we track the duration on state because we won't really know what the clip duration
|
|
|
|
|
// is until the first time update, and as a PureComponent we are trying to dedupe state
|
|
|
|
|
// updates as much as possible. This is just the easiest way to avoid a forceUpdate() or
|
|
|
|
|
// member property to track "did we get a duration".
|
|
|
|
|
durationSeconds: this.props.playback.clockInfo.durationSeconds,
|
|
|
|
|
playbackPhase: PlaybackState.Stopped, // assume not started, so full clock
|
|
|
|
|
};
|
2024-11-01 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public componentDidMount(): void {
|
2021-04-27 20:27:36 -06:00
|
|
|
this.props.playback.on(UPDATE_EVENT, this.onPlaybackUpdate);
|
|
|
|
|
this.props.playback.clockInfo.liveData.onUpdate(this.onTimeUpdate);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onPlaybackUpdate = (ev: PlaybackState): void => {
|
2021-04-27 20:27:36 -06:00
|
|
|
// Convert Decoding -> Stopped because we don't care about the distinction here
|
|
|
|
|
if (ev === PlaybackState.Decoding) ev = PlaybackState.Stopped;
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ playbackPhase: ev });
|
2021-04-27 20:27:36 -06:00
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onTimeUpdate = (time: number[]): void => {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ seconds: time[0], durationSeconds: time[1] });
|
2021-04-27 20:27:36 -06:00
|
|
|
};
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2021-04-27 20:27:36 -06:00
|
|
|
let seconds = this.state.seconds;
|
|
|
|
|
if (this.state.playbackPhase === PlaybackState.Stopped) {
|
2021-06-23 19:34:34 -06:00
|
|
|
if (Number.isFinite(this.props.defaultDisplaySeconds)) {
|
2023-03-21 10:08:44 +01:00
|
|
|
seconds = this.props.defaultDisplaySeconds ?? this.props.playback.durationSeconds;
|
2021-06-23 19:34:34 -06:00
|
|
|
} else {
|
|
|
|
|
seconds = this.state.durationSeconds;
|
|
|
|
|
}
|
2021-04-27 20:27:36 -06:00
|
|
|
}
|
2022-02-08 11:20:03 +00:00
|
|
|
return <Clock seconds={seconds} role="timer" />;
|
2021-04-27 20:27:36 -06:00
|
|
|
}
|
|
|
|
|
}
|