add chromecast/airplay support

This commit is contained in:
Feross Aboukhadijeh
2016-03-02 04:14:17 -08:00
parent 39b46e126f
commit c1e2b9ec5c
4 changed files with 121 additions and 24 deletions

View File

@@ -70,30 +70,45 @@ body.drag::before {
.torrent .metadata {
float: left;
width: calc(100% - 80px);
width: calc(100% - 240px);
color: #FFF;
text-shadow: rgba(0, 0, 0, 0.5) 0 0 4px;
}
.torrent .play {
float: right;
margin-top: 25px;
background-color: #C62828;
.btn {
width: 50px;
height: 50px;
border-radius: 25px;
color: #FFF;
font-size: 24px;
padding-top: 14px;
padding-left: 3px;
text-align: center;
transition: all 0.1s;
text-align: center;
}
.torrent .play:not(.disabled):hover {
.btn:not(.disabled):hover {
-webkit-filter: brightness(1.5);
}
.torrent .btn {
float: right;
margin-top: 25px;
padding-top: 14px;
margin-left: 20px;
}
.torrent .play {
background-color: #C62828;
padding-left: 3px;
}
.torrent .chromecast {
background-color: #64B5F6;
}
.torrent .airplay {
background-color: #607D8B;
}
.torrent .name {
font-size: 1.6em;
font-weight: bold;

View File

@@ -1,9 +1,12 @@
/* global URL, Blob */
// var prettyBytes = require('pretty-bytes')
var airplay = require('airplay-js')
var chromecasts = require('chromecasts')
var createTorrent = require('create-torrent')
var dragDrop = require('drag-drop')
var electron = require('electron')
var networkAddress = require('network-address')
var path = require('path')
var throttle = require('throttleit')
var thunky = require('thunky')
@@ -26,7 +29,11 @@ global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
})
var state = global.state = {
torrents: []
torrents: [],
server: null,
player: null,
chromcast: null,
airplay: null
}
var currentVDom, rootElement, getClient, updateThrottled
@@ -53,6 +60,15 @@ function init () {
getClient(function () {})
dragDrop('body', onFiles)
chromecasts().on('update', function (player) {
state.chromecasts = player
update()
})
airplay.createBrowser().on('deviceOn', function (player) {
state.airplay = player
}).start()
}
init()
@@ -66,20 +82,23 @@ function update () {
function handler (action, ...args) {
console.log('handler: %s %o', action, args)
if (action === 'addTorrent') {
var torrentId = args[0]
addTorrent(torrentId)
addTorrent(args[0] /* torrentId */)
}
if (action === 'seed') {
var files = args[0]
seed(files)
seed(args[0] /* files */)
}
if (action === 'openPlayer') {
var torrent = args[0]
openPlayer(torrent)
openPlayer(args[0] /* torrent */)
}
if (action === 'closePlayer') {
closePlayer()
}
if (action === 'openChromecast') {
openChromecast(args[0] /* torrent */)
}
if (action === 'openAirplay') {
openAirplay(args[0] /* torrent */)
}
}
electron.ipcRenderer.on('action', function (e, action, ...args) {
@@ -161,7 +180,7 @@ function torrentReady (torrent) {
update()
}
function openPlayer (torrent) {
function startServer (torrent, cb) {
// use largest file
var index = torrent.files.indexOf(torrent.files.reduce(function (a, b) {
return a.length > b.length ? a : b
@@ -170,20 +189,56 @@ function openPlayer (torrent) {
var server = torrent.createServer()
server.listen(0, function () {
var port = server.address().port
state.player = {
var urlSuffix = ':' + port + '/' + index
state.server = {
server: server,
url: 'http://localhost:' + port + '/' + index
localURL: 'http://localhost' + urlSuffix,
networkURL: 'http://' + networkAddress() + urlSuffix
}
cb()
})
}
function closeServer () {
state.server.server.destroy()
state.server = null
}
function openPlayer (torrent) {
startServer(torrent, function () {
state.player = 'local'
update()
})
}
function closePlayer () {
state.player.server.destroy()
closeServer()
state.player = null
update()
}
function openChromecast (torrent) {
startServer(torrent, function () {
console.log(state.server.networkURL)
state.chromecast.play(state.server.networkURL, { title: torrent.name })
state.chromecast.on('error', function (err) {
err.message = 'Chromecast: ' + err.message
onError(err)
})
state.player = 'chromecast'
update()
})
}
function openAirplay (torrent) {
startServer(torrent, function () {
state.airplay.play(state.server.networkURL, 0, function () {})
// TODO: handle airplay errors
state.player = 'airplay'
update()
})
}
// function onTorrent (torrent) {
// function updateSpeed () {
// ipc.send('')

View File

@@ -3,10 +3,10 @@ module.exports = App
var h = require('virtual-dom/h')
function App (state, handler) {
if (state.player) {
if (state.player === 'local') {
return h('.player', [
h('video', {
src: state.player.url,
src: state.server.localURL,
autoplay: true,
controls: true
}),
@@ -34,15 +34,39 @@ function App (state, handler) {
})()
])
]),
h('a.play', {
h('a.btn.play', {
className: !torrent.ready ? 'disabled' : '',
onclick: openPlayer
}, '▶')
}, '▶'),
(function () {
if (state.chromecast) {
return h('a.btn.chromecast', {
className: !torrent.ready ? 'disabled' : '',
onclick: openChromecast
}, 'C')
}
})(),
(function () {
if (state.airplay) {
return h('a.btn.airplay', {
className: !torrent.ready ? 'disabled' : '',
onclick: openAirplay
}, 'A')
}
})()
])
function openPlayer () {
handler('openPlayer', torrent)
}
function openChromecast () {
handler('openChromecast', torrent)
}
function openAirplay () {
handler('openAirplay', torrent)
}
})
return h('.app', [
h('.torrent-list', list),