clipper/frontend/src/App.tsx

284 lines
7.6 KiB
TypeScript
Raw Normal View History

2021-10-22 19:30:09 +00:00
import {
2021-11-02 16:20:47 +00:00
MediaSet,
GrpcWebImpl,
MediaSetServiceClientImpl,
GetVideoProgress,
2021-11-29 17:44:31 +00:00
GetAudioProgress,
2021-11-02 16:20:47 +00:00
} from './generated/media_set';
2021-10-29 12:52:31 +00:00
import { useState, useEffect, useRef, useCallback } from 'react';
2021-10-08 14:38:35 +00:00
import { VideoPreview } from './VideoPreview';
2021-11-29 17:44:31 +00:00
import { Overview, CanvasLogicalWidth } from './Overview';
2021-10-08 14:38:35 +00:00
import { Waveform } from './Waveform';
import { ControlBar } from './ControlBar';
import { SeekBar } from './SeekBar';
2021-09-06 10:17:50 +00:00
import './App.css';
2021-11-02 16:20:47 +00:00
import { Duration } from './generated/google/protobuf/duration';
2021-11-30 19:41:34 +00:00
import { firstValueFrom, from, Observable } from 'rxjs';
import { first, map } from 'rxjs/operators';
2021-09-06 14:25:23 +00:00
2021-11-02 16:20:47 +00:00
// ported from backend, where should they live?
const thumbnailWidth = 177;
const thumbnailHeight = 100;
2021-10-08 14:38:35 +00:00
2021-12-06 22:52:24 +00:00
const initialViewportCanvasPixels = 100;
const apiURL = process.env.REACT_APP_API_URL || 'http://localhost:8888';
2021-10-08 14:38:35 +00:00
// Frames represents a selection of audio frames.
export interface Frames {
start: number;
end: number;
}
2021-09-06 10:17:50 +00:00
2021-11-25 18:02:37 +00:00
export interface VideoPosition {
currentTime: number;
percent: number;
}
2021-11-30 19:41:34 +00:00
const video = document.createElement('video');
const audio = document.createElement('audio');
2021-09-30 19:08:48 +00:00
function App(): JSX.Element {
2021-10-08 14:38:35 +00:00
const [mediaSet, setMediaSet] = useState<MediaSet | null>(null);
const [viewport, setViewport] = useState({ start: 0, end: 0 });
2021-11-29 17:44:31 +00:00
const [overviewPeaks, setOverviewPeaks] = useState<Observable<number[]>>(
from([])
);
2021-10-08 14:38:35 +00:00
2021-11-30 19:41:34 +00:00
// position stores the current playback position. positionRef makes it
// available inside a setInterval callback.
const [position, setPosition] = useState({ currentTime: 0, percent: 0 });
const positionRef = useRef(position);
positionRef.current = position;
2021-10-08 14:38:35 +00:00
// effects
// TODO: error handling
const videoID = new URLSearchParams(window.location.search).get('video_id');
2021-10-22 19:30:09 +00:00
if (videoID == null) {
return <></>;
}
2021-10-08 14:38:35 +00:00
// fetch mediaset on page load:
useEffect(() => {
(async function () {
const rpc = newRPC();
2021-11-02 16:20:47 +00:00
const service = new MediaSetServiceClientImpl(rpc);
const mediaSet = await service.Get({ youtubeId: videoID });
2021-10-22 19:30:09 +00:00
2021-10-29 12:52:31 +00:00
console.log('got media set:', mediaSet);
2021-11-02 16:20:47 +00:00
setMediaSet(mediaSet);
2021-10-08 14:38:35 +00:00
})();
}, []);
2021-11-30 19:41:34 +00:00
const updatePlayerPositionIntevalMillis = 30;
2021-10-08 14:38:35 +00:00
// setup player on first page load only:
useEffect(() => {
2021-11-25 18:02:37 +00:00
if (mediaSet == null) {
return;
}
2021-11-30 19:41:34 +00:00
const intervalID = setInterval(() => {
if (video.currentTime == positionRef.current.currentTime) {
2021-11-25 18:02:37 +00:00
return;
}
const duration = mediaSet.audioFrames / mediaSet.audioSampleRate;
const percent = (video.currentTime / duration) * 100;
setPosition({ currentTime: video.currentTime, percent: percent });
2021-11-30 19:41:34 +00:00
}, updatePlayerPositionIntevalMillis);
return () => clearInterval(intervalID);
2021-11-25 18:02:37 +00:00
}, [mediaSet]);
2021-10-08 14:38:35 +00:00
2021-11-29 17:44:31 +00:00
// load audio when MediaSet is loaded:
useEffect(() => {
(async function () {
if (mediaSet == null) {
return;
}
console.log('fetching audio...');
const service = new MediaSetServiceClientImpl(newRPC());
const audioProgressStream = service.GetAudio({
id: mediaSet.id,
numBins: CanvasLogicalWidth,
});
const peaks = audioProgressStream.pipe(map((progress) => progress.peaks));
setOverviewPeaks(peaks);
2021-11-30 19:41:34 +00:00
const pipe = audioProgressStream.pipe(
first((progress: GetAudioProgress) => progress.url != '')
);
const progressWithURL = await firstValueFrom(pipe);
2021-11-29 17:44:31 +00:00
2021-11-30 19:41:34 +00:00
audio.src = progressWithURL.url;
2021-11-29 17:44:31 +00:00
audio.muted = false;
audio.volume = 1;
2021-11-30 19:41:34 +00:00
console.log('set audio src', progressWithURL.url);
2021-11-29 17:44:31 +00:00
})();
}, [mediaSet]);
2021-10-08 14:38:35 +00:00
// load video when MediaSet is loaded:
useEffect(() => {
(async function () {
if (mediaSet == null) {
return;
}
2021-11-02 16:20:47 +00:00
2021-11-30 19:41:34 +00:00
console.log('fetching video...');
const service = new MediaSetServiceClientImpl(newRPC());
const videoProgressStream = service.GetVideo({ id: mediaSet.id });
2021-11-30 19:41:34 +00:00
const pipe = videoProgressStream.pipe(
first((progress: GetVideoProgress) => progress.url != '')
);
const progressWithURL = await firstValueFrom(pipe);
2021-11-30 19:41:34 +00:00
video.src = progressWithURL.url;
console.log('set video src', progressWithURL.url);
})();
2021-10-08 14:38:35 +00:00
}, [mediaSet]);
// set viewport when MediaSet is loaded:
useEffect(() => {
if (mediaSet == null) {
return;
}
const numFrames = Math.min(
2021-12-06 22:52:24 +00:00
Math.round(mediaSet.audioFrames / CanvasLogicalWidth) *
initialViewportCanvasPixels,
mediaSet.audioFrames
);
setViewport({ start: 0, end: numFrames });
2021-10-08 14:38:35 +00:00
}, [mediaSet]);
useEffect(() => {
console.debug('viewport updated', viewport);
}, [viewport]);
// handlers
2021-11-30 19:41:34 +00:00
const handleOverviewSelectionChange = (newViewport: Frames) => {
2021-10-08 14:38:35 +00:00
if (mediaSet == null) {
return;
}
2021-11-30 19:41:34 +00:00
console.log('set new viewport', newViewport);
setViewport({ ...newViewport });
if (!audio.paused) {
return;
}
2021-10-08 14:38:35 +00:00
2021-11-30 19:41:34 +00:00
const ratio = newViewport.start / mediaSet.audioFrames;
const currentTime =
(mediaSet.audioFrames / mediaSet.audioSampleRate) * ratio;
audio.currentTime = currentTime;
video.currentTime = currentTime;
2021-10-08 14:38:35 +00:00
};
const handlePlay = useCallback(() => {
audio.play();
video.play();
}, [audio, video]);
const handlePause = useCallback(() => {
video.pause();
audio.pause();
}, [audio, video]);
2021-10-08 14:38:35 +00:00
// render component
const containerStyles = {
border: '1px solid black',
width: '90%',
margin: '1em auto',
minHeight: '500px',
height: '700px',
display: 'flex',
flexDirection: 'column',
} as React.CSSProperties;
2021-11-02 16:20:47 +00:00
const offsetPixels = Math.floor(thumbnailWidth / 2);
2021-10-08 14:38:35 +00:00
if (mediaSet == null) {
// TODO: improve
return <></>;
}
2021-09-06 10:17:50 +00:00
return (
2021-10-08 14:38:35 +00:00
<>
<div className="App">
<div style={containerStyles}>
<ControlBar onPlay={handlePlay} onPause={handlePause} />
2021-10-08 14:38:35 +00:00
<Overview
2021-11-29 17:44:31 +00:00
peaks={overviewPeaks}
2021-10-08 14:38:35 +00:00
mediaSet={mediaSet}
offsetPixels={offsetPixels}
height={80}
2021-11-30 19:41:34 +00:00
viewport={viewport}
2021-10-08 14:38:35 +00:00
position={position}
onSelectionChange={handleOverviewSelectionChange}
/>
<Waveform
mediaSet={mediaSet}
position={position}
viewport={viewport}
offsetPixels={offsetPixels}
/>
<SeekBar
position={video.currentTime}
2021-11-02 16:20:47 +00:00
duration={mediaSet.audioFrames / mediaSet.audioSampleRate}
2021-10-08 14:38:35 +00:00
offsetPixels={offsetPixels}
onPositionChanged={(position: number) => {
video.currentTime = position;
2021-11-29 17:44:31 +00:00
audio.currentTime = position;
2021-10-08 14:38:35 +00:00
}}
/>
<VideoPreview
2021-11-21 19:43:40 +00:00
mediaSet={mediaSet}
2021-10-08 14:38:35 +00:00
video={video}
position={position}
2021-11-02 16:20:47 +00:00
duration={millisFromDuration(mediaSet.videoDuration)}
height={thumbnailHeight}
2021-10-08 14:38:35 +00:00
/>
</div>
2021-12-11 16:25:30 +00:00
<ul style={{ listStyleType: 'none' } as React.CSSProperties}>
<li>Frames: {mediaSet.audioFrames}</li>
<li>
Viewport (frames): {viewport.start} to {viewport.end}
</li>
<li>
Selection (frames): {selection.start} to {selection.end}
</li>
<li>
Position (frames):{' '}
{Math.round(mediaSet.audioFrames * (position.percent / 100))}
</li>
<li>Position (seconds): {position.currentTime}</li>
<li></li>
</ul>
2021-10-08 14:38:35 +00:00
</div>
</>
2021-09-06 10:17:50 +00:00
);
}
export default App;
2021-11-02 16:20:47 +00:00
function millisFromDuration(dur?: Duration): number {
if (dur == undefined) {
return 0;
}
return Math.floor(dur.seconds * 1000.0 + dur.nanos / 1000.0 / 1000.0);
}
export function newRPC(): GrpcWebImpl {
return new GrpcWebImpl(apiURL, {});
}