cabana/src/components/HLS.js

111 lines
2.8 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 Hls from "hls.js/lib";
2017-06-13 18:40:05 -06:00
export default class HLS extends Component {
static propTypes = {
source: PropTypes.string.isRequired,
startTime: PropTypes.number.isRequired,
playbackSpeed: PropTypes.number.isRequired,
playing: PropTypes.bool.isRequired,
onVideoElementAvailable: PropTypes.func,
onClick: PropTypes.func,
onLoadStart: PropTypes.func,
2017-06-14 18:08:25 -06:00
onLoadEnd: PropTypes.func,
onPlaySeek: PropTypes.func,
2017-06-17 17:42:04 -06:00
segmentProgress: PropTypes.func,
shouldRestart: PropTypes.bool,
onRestart: PropTypes.func
2017-06-13 18:40:05 -06:00
};
componentWillReceiveProps(nextProps) {
2017-12-12 19:27:20 -07:00
if (
(nextProps.shouldRestart ||
nextProps.startTime !== this.props.startTime) &&
isFinite(nextProps.startTime)
) {
2017-06-13 18:40:05 -06:00
this.videoElement.currentTime = nextProps.startTime;
2017-06-17 17:42:04 -06:00
this.props.onRestart();
2017-06-13 18:40:05 -06:00
}
if (!this.videoElement.currentTime) {
this.videoElement.currentTime = nextProps.startTime;
}
if (nextProps.source !== this.props.source) {
this.loadSource(nextProps.source);
}
2017-12-12 19:27:20 -07:00
if (nextProps.playing) {
if (
this.videoElement &&
(this.videoElement.paused || this.videoElement.currentTime < 0.01)
) {
this.videoElement.play();
}
2017-06-13 18:40:05 -06:00
} else {
this.videoElement.pause();
}
}
onSeeking = () => {
if (!this.props.playing) {
this.props.onLoadStart();
this.props.onPlaySeek(this.videoElement.currentTime);
}
};
// legacy outer scope variable. Revisit this to see if putting in state
// makes more sense
shouldInitVideoTime = true;
onSeeked = () => {
if (!this.props.playing) {
if (this.shouldInitVideoTime) {
this.videoElement.currentTime = this.props.startTime;
this.shouldInitVideoTime = false;
}
this.props.onLoadEnd();
}
};
2017-06-13 18:40:05 -06:00
componentDidMount() {
this.player = new Hls();
this.loadSource();
}
loadSource(source = this.props.source) {
if (this.videoElement) {
this.player.loadSource(source);
this.player.attachMedia(this.videoElement);
this.props.onVideoElementAvailable(this.videoElement);
}
}
componentWillUnmount() {
// destroy hls video source
if (this.player) {
this.player.destroy();
2017-07-05 16:57:03 -06:00
}
2017-06-13 18:40:05 -06:00
}
render() {
return (
<div
2017-12-12 19:27:20 -07:00
className="cabana-explorer-visuals-camera-wrapper"
onClick={this.props.onClick}
>
<video
ref={video => {
this.videoElement = video;
}}
autoPlay={this.props.playing}
muted
onWaiting={this.props.onLoadStart}
onPlaying={this.props.onLoadEnd}
onSeeking={this.onSeeking}
onSeeked={this.onSeeked}
2017-12-12 19:27:20 -07:00
/>
</div>
);
2017-06-13 18:40:05 -06:00
}
}