cabana/src/components/RouteVideoSync.js

211 lines
5.6 KiB
JavaScript
Raw Normal View History

2017-12-12 19:27:20 -07:00
import React, { Component } from "react";
import PropTypes from "prop-types";
import { StyleSheet, css } from "aphrodite/no-important";
2019-06-14 15:39:49 -06:00
import { derived as RouteApi, video as VideoApi } from "@commaai/comma-api";
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
import HLS from "./HLS";
import RouteSeeker from "./RouteSeeker/RouteSeeker";
const Styles = StyleSheet.create({
2017-12-12 19:27:20 -07:00
loadingOverlay: {
position: "absolute",
top: 0,
left: 0,
width: "100%",
height: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center",
zIndex: 3
},
loadingSpinner: {
width: "25%",
height: "25%",
display: "block"
},
img: {
height: 480,
display: "block",
position: "absolute",
zIndex: 2
},
hls: {
zIndex: 1,
height: 480,
backgroundColor: "rgba(0,0,0,0.9)"
},
seekBar: {
position: "absolute",
bottom: 0,
left: 0,
width: "100%",
zIndex: 4
}
});
export default class RouteVideoSync extends Component {
2017-12-12 19:27:20 -07:00
static propTypes = {
userSeekIndex: PropTypes.number.isRequired,
secondsLoaded: PropTypes.number.isRequired,
startOffset: PropTypes.number.isRequired,
message: PropTypes.object,
canFrameOffset: PropTypes.number.isRequired,
url: PropTypes.string.isRequired,
playing: PropTypes.bool.isRequired,
onPlaySeek: PropTypes.func.isRequired,
onUserSeek: PropTypes.func.isRequired,
2017-12-12 19:27:20 -07:00
onPlay: PropTypes.func.isRequired,
onPause: PropTypes.func.isRequired,
userSeekTime: PropTypes.number.isRequired
};
constructor(props) {
super(props);
this.state = {
shouldShowJpeg: true,
isLoading: true,
2017-12-12 19:27:20 -07:00
videoElement: null,
shouldRestartHls: false
2017-06-13 18:40:05 -06:00
};
this.onLoadStart = this.onLoadStart.bind(this);
this.onLoadEnd = this.onLoadEnd.bind(this);
2017-12-12 19:27:20 -07:00
this.segmentProgress = this.segmentProgress.bind(this);
this.onVideoElementAvailable = this.onVideoElementAvailable.bind(this);
this.onUserSeek = this.onUserSeek.bind(this);
this.onHlsRestart = this.onHlsRestart.bind(this);
this.ratioTime = this.ratioTime.bind(this);
}
componentWillReceiveProps(nextProps) {
if (
this.props.userSeekIndex !== nextProps.userSeekIndex ||
this.props.canFrameOffset !== nextProps.canFrameOffset ||
(this.props.message &&
nextProps.message &&
this.props.message.entries.length !== nextProps.message.entries.length)
) {
this.setState({ shouldRestartHls: true });
2017-06-17 17:42:04 -06:00
}
2017-12-12 19:27:20 -07:00
}
nearestFrameUrl() {
const { url } = this.props;
const sec = Math.round(this.props.userSeekTime);
2019-06-14 15:39:49 -06:00
return RouteApi(url).getJpegUrl(sec);
2017-12-12 19:27:20 -07:00
}
loadingOverlay() {
return (
<div className={css(Styles.loadingOverlay)}>
<img
className={css(Styles.loadingSpinner)}
src={process.env.PUBLIC_URL + "/img/loading.svg"}
alt={"Loading video"}
/>
</div>
);
}
onLoadStart() {
this.setState({
shouldShowJpeg: true,
isLoading: true
});
}
onLoadEnd() {
this.setState({
shouldShowJpeg: false,
isLoading: false
});
}
2017-12-12 19:27:20 -07:00
segmentProgress(currentTime) {
// returns progress as number in [0,1]
2017-12-12 19:27:20 -07:00
if (currentTime < this.props.startOffset) {
currentTime = this.props.startOffset;
2017-06-13 18:40:05 -06:00
}
const ratio =
(currentTime - this.props.startOffset) / this.props.secondsLoaded;
2017-12-12 19:27:20 -07:00
return Math.max(0, Math.min(1, ratio));
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
ratioTime(ratio) {
return ratio * this.props.secondsLoaded + this.props.startOffset;
2017-12-12 19:27:20 -07:00
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
onVideoElementAvailable(videoElement) {
this.setState({ videoElement });
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
onUserSeek(ratio) {
/* ratio in [0,1] */
2017-06-14 18:08:25 -06:00
let { videoElement } = this.state;
let seekTime = videoElement.duration * ratio;
videoElement.currentTime = seekTime;
const funcSeekToRatio = () => this.props.onUserSeek(seekTime);
2017-12-12 19:27:20 -07:00
if (ratio === 0) {
this.setState({ shouldRestartHls: true }, funcSeekToRatio);
} else {
funcSeekToRatio();
}
2017-12-12 19:27:20 -07:00
}
onHlsRestart() {
this.setState({ shouldRestartHls: false });
}
render() {
return (
<div className="cabana-explorer-visuals-camera">
{this.state.isLoading ? this.loadingOverlay() : null}
{this.state.shouldShowJpeg ? (
2017-12-12 19:27:20 -07:00
<img
src={this.nearestFrameUrl()}
className={css(Styles.img)}
alt={"Camera preview at t = " + Math.round(this.props.userSeekTime)}
/>
) : null}
<HLS
className={css(Styles.hls)}
2019-08-09 16:48:54 -06:00
source={VideoApi(
this.props.url,
process.env.REACT_APP_VIDEO_CDN
).getRearCameraStreamIndexUrl()}
2017-12-12 19:27:20 -07:00
startTime={this.props.userSeekTime}
2018-07-26 12:33:01 -06:00
playbackSpeed={this.props.playSpeed}
2017-12-12 19:27:20 -07:00
onVideoElementAvailable={this.onVideoElementAvailable}
playing={this.props.playing}
onClick={this.props.onVideoClick}
onLoadStart={this.onLoadStart}
onLoadEnd={this.onLoadEnd}
onUserSeek={this.onUserSeek}
onPlaySeek={this.props.onPlaySeek}
2017-12-12 19:27:20 -07:00
segmentProgress={this.segmentProgress}
shouldRestart={this.state.shouldRestartHls}
onRestart={this.onHlsRestart}
/>
<RouteSeeker
className={css(Styles.seekBar)}
nearestFrameTime={this.props.userSeekTime}
segmentProgress={this.segmentProgress}
secondsLoaded={this.props.secondsLoaded}
segmentIndices={this.props.segmentIndices}
onUserSeek={this.onUserSeek}
onPlaySeek={this.props.onPlaySeek}
2017-12-12 19:27:20 -07:00
videoElement={this.state.videoElement}
onPlay={this.props.onPlay}
onPause={this.props.onPause}
playing={this.props.playing}
ratioTime={this.ratioTime}
/>
</div>
);
}
}