lbry-desktop/js/page/watch.js

83 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-11-22 21:19:08 +01:00
import React from 'react';
2017-02-03 11:25:05 +01:00
import {Icon} from '../component/common.js';
import {Link} from '../component/link.js';
2016-11-22 21:19:08 +01:00
import lbry from '../lbry.js';
2017-01-03 03:21:23 +01:00
import LoadScreen from '../component/load_screen.js'
2017-01-19 10:50:08 +01:00
const fs = require('fs');
const VideoStream = require('videostream');
2016-04-21 11:51:27 +02:00
var WatchPage = React.createClass({
propTypes: {
name: React.PropTypes.string,
},
getInitialState: function() {
return {
downloadStarted: false,
readyToPlay: false,
loadStatusMessage: "Requesting stream",
mimeType: null,
};
},
componentDidMount: function() {
lbry.getStream(this.props.name);
this.updateLoadStatus();
},
2017-02-03 11:25:05 +01:00
handleBackClicked: function() {
history.back();
},
updateLoadStatus: function() {
lbry.getFileStatus(this.props.name, (status) => {
2017-02-16 06:19:10 +01:00
if (!status || !['running', 'stopped'].includes(status.code) || status.written_bytes == 0) {
// Download hasn't started yet, so update status message (if available) then try again
2017-01-19 10:50:08 +01:00
// TODO: Would be nice to check if we have the MOOV before starting playing
if (status) {
this.setState({
loadStatusMessage: status.message
});
}
setTimeout(() => { this.updateLoadStatus() }, 250);
} else {
this.setState({
readyToPlay: true,
mimeType: status.mime_type,
})
2017-01-19 10:50:08 +01: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-04-21 11:51:27 +02:00
render: function() {
return (
!this.state.readyToPlay
? <LoadScreen message={'Loading video...'} details={this.state.loadStatusMessage} />
2017-02-03 11:25:05 +01:00
: <main className="video full-screen">
<video controls width="100%" height="100%" id="video" ref="video"></video>
<div className="video__overlay">
<div className="video__back">
<Link icon="icon-arrow-circle-o-left" className="video__back-link" onClick={this.handleBackClicked}/>
<div className="video__back-label">
<Icon icon="icon-caret-left" className="video__back-label-arrow" />
<div className="video__back-label-content">
Back to LBRY
</div>
</div>
</div>
</div>
</main>
);
2016-04-21 11:51:27 +02:00
}
});
2016-11-22 21:19:08 +01:00
export default WatchPage;