2016-11-22 14:19:08 -06:00
|
|
|
import React from 'react';
|
|
|
|
import lbry from '../lbry.js';
|
2017-01-02 20:21:23 -06:00
|
|
|
import LoadScreen from '../component/load_screen.js'
|
|
|
|
|
2017-01-19 03:50:08 -06:00
|
|
|
const fs = require('fs');
|
|
|
|
const VideoStream = require('videostream');
|
|
|
|
|
|
|
|
|
2016-04-21 05:51:27 -04:00
|
|
|
var WatchPage = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
name: React.PropTypes.string,
|
|
|
|
},
|
2016-05-05 04:12:23 -04:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-05-05 06:55:15 -04:00
|
|
|
downloadStarted: false,
|
2016-05-05 04:12:23 -04:00
|
|
|
readyToPlay: false,
|
|
|
|
loadStatusMessage: "Requesting stream",
|
2016-06-03 06:05:14 -04:00
|
|
|
mimeType: null,
|
2016-05-05 04:12:23 -04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
componentDidMount: function() {
|
|
|
|
lbry.getStream(this.props.name);
|
|
|
|
this.updateLoadStatus();
|
2016-05-05 08:10:19 -04:00
|
|
|
},
|
2016-05-05 04:12:23 -04:00
|
|
|
updateLoadStatus: function() {
|
|
|
|
lbry.getFileStatus(this.props.name, (status) => {
|
2017-02-16 00:19:10 -05:00
|
|
|
if (!status || !['running', 'stopped'].includes(status.code) || status.written_bytes == 0) {
|
2016-05-05 06:55:15 -04:00
|
|
|
// Download hasn't started yet, so update status message (if available) then try again
|
2017-01-19 03:50:08 -06:00
|
|
|
// TODO: Would be nice to check if we have the MOOV before starting playing
|
2016-05-05 06:55:15 -04:00
|
|
|
if (status) {
|
|
|
|
this.setState({
|
2017-02-20 23:15:17 -05:00
|
|
|
loadStatusMessage: status.message
|
2016-05-05 06:55:15 -04:00
|
|
|
});
|
|
|
|
}
|
2016-05-05 04:12:23 -04:00
|
|
|
setTimeout(() => { this.updateLoadStatus() }, 250);
|
|
|
|
} else {
|
|
|
|
this.setState({
|
2016-06-03 06:05:14 -04:00
|
|
|
readyToPlay: true,
|
|
|
|
mimeType: status.mime_type,
|
2016-05-13 07:46:39 -04:00
|
|
|
})
|
2017-01-19 03:50:08 -06:00
|
|
|
const mediaFile = {
|
|
|
|
createReadStream: function (opts) {
|
|
|
|
// Return a readable stream that provides the bytes
|
|
|
|
// between offsets "start" and "end" inclusive
|
|
|
|
console.log('Stream between ' + opts.start + ' and ' + opts.end + '.');
|
|
|
|
return fs.createReadStream(status.download_path, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var elem = this.refs.video;
|
|
|
|
var videostream = VideoStream(mediaFile, elem);
|
|
|
|
elem.play();
|
2016-05-05 04:12:23 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-04-21 05:51:27 -04:00
|
|
|
render: function() {
|
2016-05-05 06:55:15 -04:00
|
|
|
return (
|
2016-09-21 01:37:43 -04:00
|
|
|
!this.state.readyToPlay
|
2016-09-24 15:27:07 -04:00
|
|
|
? <LoadScreen message={'Loading video...'} details={this.state.loadStatusMessage} />
|
2017-02-20 23:15:17 -05:00
|
|
|
: <main className="full-screen">
|
2017-01-19 03:50:08 -06:00
|
|
|
<video controls width="100%" height="100%" id="video" ref="video">
|
2017-02-20 23:15:17 -05:00
|
|
|
</video>
|
2016-09-21 01:37:43 -04:00
|
|
|
</main>
|
2016-05-05 06:55:15 -04:00
|
|
|
);
|
2016-04-21 05:51:27 -04:00
|
|
|
}
|
|
|
|
});
|
2016-11-22 14:19:08 -06:00
|
|
|
|
|
|
|
export default WatchPage;
|