cabana/src/components/HLS.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Hls from 'hls.js';
2017-06-13 18:40:05 -06:00
export default class HLS extends Component {
2017-06-13 18:40:05 -06:00
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,
onLoadEnd: PropTypes.func,
2017-06-14 18:08:25 -06:00
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) {
2018-07-26 12:33:01 -06:00
this.videoElement.playbackRate = nextProps.playbackSpeed;
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({ disablePtsDtsCorrectionInMp4Remux: true });
this.loadSource();
}
loadSource(source = this.props.source) {
if (this.videoElement) {
this.player.loadSource(source);
this.player.attachMedia(this.videoElement);
this.props.onVideoElementAvailable(this.videoElement);
this.videoElement.currentTime = this.props.startTime;
}
}
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) => {
2017-12-12 19:27:20 -07:00
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
}
}