2016-11-22 21:19:08 +01:00
|
|
|
import React from 'react';
|
2017-04-14 00:32:03 +02:00
|
|
|
import {Icon, Thumbnail} from '../component/common.js';
|
2017-02-03 11:25:05 +01:00
|
|
|
import {Link} from '../component/link.js';
|
2016-11-22 21:19:08 +01:00
|
|
|
import lbry from '../lbry.js';
|
2017-04-14 01:43:17 +02:00
|
|
|
import Modal from '../component/modal.js';
|
2017-04-15 00:23:42 +02:00
|
|
|
import lbryio from '../lbryio.js';
|
2017-04-17 14:27:39 +02:00
|
|
|
import rewards from '../rewards.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');
|
|
|
|
|
2017-04-14 01:43:17 +02:00
|
|
|
export let WatchLink = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
uri: React.PropTypes.string,
|
|
|
|
downloadStarted: React.PropTypes.bool,
|
|
|
|
onGet: React.PropTypes.func
|
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
affirmedPurchase: false
|
|
|
|
},
|
|
|
|
onAffirmPurchase: function() {
|
|
|
|
lbry.get({uri: this.props.uri}).then((streamInfo) => {
|
|
|
|
if (streamInfo === null || typeof streamInfo !== 'object') {
|
|
|
|
this.setState({
|
|
|
|
modal: 'timedOut',
|
|
|
|
attemptingDownload: false,
|
|
|
|
});
|
|
|
|
}
|
2017-04-15 00:23:42 +02:00
|
|
|
|
|
|
|
lbryio.call('file', 'view', {
|
|
|
|
uri: this.props.uri,
|
|
|
|
outpoint: streamInfo.outpoint,
|
|
|
|
claimId: streamInfo.claim_id
|
|
|
|
})
|
2017-04-14 01:43:17 +02:00
|
|
|
});
|
|
|
|
if (this.props.onGet) {
|
|
|
|
this.props.onGet()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onWatchClick: function() {
|
|
|
|
this.setState({
|
|
|
|
loading: true
|
|
|
|
});
|
|
|
|
lbry.getCostInfo(this.props.uri).then(({cost}) => {
|
|
|
|
lbry.getBalance((balance) => {
|
|
|
|
if (cost > balance) {
|
|
|
|
this.setState({
|
|
|
|
modal: 'notEnoughCredits',
|
|
|
|
attemptingDownload: false,
|
|
|
|
});
|
|
|
|
} else if (cost <= 0.01) {
|
|
|
|
this.onAffirmPurchase()
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
modal: 'affirmPurchase'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
modal: null,
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
closeModal: function() {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
modal: null,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
return (<div>
|
|
|
|
<Link button={ this.props.button ? this.props.button : null }
|
|
|
|
disabled={this.state.loading}
|
|
|
|
label={this.props.label ? this.props.label : ""}
|
|
|
|
className={this.props.className}
|
|
|
|
icon="icon-play"
|
|
|
|
onClick={this.onWatchClick} />
|
|
|
|
<Modal contentLabel="Not enough credits" isOpen={this.state.modal == 'notEnoughCredits'} onConfirmed={this.closeModal}>
|
|
|
|
You don't have enough LBRY credits to pay for this stream.
|
|
|
|
</Modal>
|
|
|
|
<Modal type="confirm" isOpen={this.state.modal == 'affirmPurchase'}
|
|
|
|
contentLabel="Confirm Purchase" onConfirmed={this.onAffirmPurchase} onAborted={this.closeModal}>
|
|
|
|
Confirm you want to purchase this bro.
|
|
|
|
</Modal>
|
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-04-13 20:52:26 +02:00
|
|
|
export let Video = React.createClass({
|
|
|
|
_isMounted: false,
|
|
|
|
_controlsHideDelay: 3000, // Note: this needs to be shorter than the built-in delay in Electron, or Electron will hide the controls before us
|
|
|
|
_controlsHideTimeout: null,
|
|
|
|
_outpoint: null,
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
uri: React.PropTypes.string,
|
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
downloadStarted: false,
|
|
|
|
readyToPlay: false,
|
2017-04-14 00:32:03 +02:00
|
|
|
isPlaying: false,
|
|
|
|
isPurchased: false,
|
|
|
|
loadStatusMessage: "Requesting stream... it may sit here for like 15-20 seconds in a really awkward way... we're working on it",
|
2017-04-13 20:52:26 +02:00
|
|
|
mimeType: null,
|
|
|
|
controlsShown: false,
|
|
|
|
};
|
|
|
|
},
|
2017-04-14 01:43:17 +02:00
|
|
|
onGet: function() {
|
2017-04-13 20:52:26 +02:00
|
|
|
lbry.get({uri: this.props.uri}).then((fileInfo) => {
|
|
|
|
this._outpoint = fileInfo.outpoint;
|
|
|
|
this.updateLoadStatus();
|
|
|
|
});
|
2017-04-14 00:32:03 +02:00
|
|
|
this.setState({
|
|
|
|
isPlaying: true
|
|
|
|
})
|
2016-05-05 10:12:23 +02:00
|
|
|
},
|
|
|
|
componentDidMount: function() {
|
2017-04-14 00:32:03 +02:00
|
|
|
if (this.props.autoplay) {
|
|
|
|
this.start()
|
|
|
|
}
|
2017-02-03 11:25:05 +01:00
|
|
|
},
|
2017-02-16 08:18:26 +01:00
|
|
|
handleMouseMove: function() {
|
|
|
|
if (this._controlsTimeout) {
|
|
|
|
clearTimeout(this._controlsTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.controlsShown) {
|
|
|
|
this.setState({
|
|
|
|
controlsShown: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this._controlsTimeout = setTimeout(() => {
|
|
|
|
if (!this.isMounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
controlsShown: false,
|
|
|
|
});
|
|
|
|
}, this._controlsHideDelay);
|
|
|
|
},
|
2017-02-23 08:46:19 +01:00
|
|
|
handleMouseLeave: function() {
|
2017-02-16 08:18:26 +01:00
|
|
|
if (this._controlsTimeout) {
|
|
|
|
clearTimeout(this._controlsTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.controlsShown) {
|
|
|
|
this.setState({
|
|
|
|
controlsShown: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2016-05-05 10:12:23 +02:00
|
|
|
updateLoadStatus: function() {
|
2017-03-14 17:30:54 +01:00
|
|
|
lbry.file_list({
|
2017-03-08 08:17:16 +01:00
|
|
|
outpoint: this._outpoint,
|
|
|
|
full_status: true,
|
2017-03-14 17:30:54 +01:00
|
|
|
}).then(([status]) => {
|
|
|
|
if (!status || status.written_bytes == 0) {
|
2016-05-05 12:55:15 +02:00
|
|
|
// Download hasn't started yet, so update status message (if available) then try again
|
2017-03-08 08:17:16 +01:00
|
|
|
// TODO: Would be nice to check if we have the MOOV before starting playing
|
2016-05-05 12:55:15 +02:00
|
|
|
if (status) {
|
|
|
|
this.setState({
|
2017-02-21 05:15:17 +01:00
|
|
|
loadStatusMessage: status.message
|
2016-05-05 12:55:15 +02:00
|
|
|
});
|
|
|
|
}
|
2016-05-05 10:12:23 +02:00
|
|
|
setTimeout(() => { this.updateLoadStatus() }, 250);
|
|
|
|
} else {
|
|
|
|
this.setState({
|
2016-06-03 12:05:14 +02:00
|
|
|
readyToPlay: true,
|
|
|
|
mimeType: status.mime_type,
|
2016-05-13 13:46:39 +02:00
|
|
|
})
|
2017-03-08 08:17:16 +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)
|
|
|
|
}
|
|
|
|
};
|
2017-04-17 14:27:39 +02:00
|
|
|
|
|
|
|
rewards.claimNextPurchaseReward()
|
|
|
|
|
2017-03-08 08:17:16 +01:00
|
|
|
var elem = this.refs.video;
|
|
|
|
var videostream = VideoStream(mediaFile, elem);
|
|
|
|
elem.play();
|
2016-05-05 10:12:23 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-04-21 11:51:27 +02:00
|
|
|
render: function() {
|
2016-05-05 12:55:15 +02:00
|
|
|
return (
|
2017-04-14 00:32:03 +02:00
|
|
|
<div className={"video " + this.props.className + (this.state.isPlaying && this.state.readyToPlay ? " video--active" : " video--hidden")}>{
|
|
|
|
this.state.isPlaying ?
|
|
|
|
!this.state.readyToPlay ?
|
|
|
|
<span>this is the world's world loading screen and we shipped our software with it anyway... <br/><br/>{this.state.loadStatusMessage}</span> :
|
|
|
|
<video controls id="video" ref="video"></video> :
|
2017-04-14 01:43:17 +02:00
|
|
|
<div className="video__cover" style={{backgroundImage: 'url("' + this.props.metadata.thumbnail + '")'}}>
|
|
|
|
<WatchLink className="video__play-button" uri={this.props.uri} onGet={this.onGet} icon="icon-play"></WatchLink>
|
2017-04-14 00:32:03 +02:00
|
|
|
</div>
|
|
|
|
}</div>
|
2016-05-05 12:55:15 +02:00
|
|
|
);
|
2016-04-21 11:51:27 +02:00
|
|
|
}
|
2017-04-14 00:32:03 +02:00
|
|
|
})
|