in progress: move webtorrent into background BrowserWindow
This commit is contained in:
23
background/capture-video-frame.js
Normal file
23
background/capture-video-frame.js
Normal file
@@ -0,0 +1,23 @@
|
||||
module.exports = captureVideoFrame
|
||||
|
||||
function captureVideoFrame (video, format) {
|
||||
if (typeof video === 'string') video = document.querySelector(video)
|
||||
if (!video || video.nodeName !== 'VIDEO') {
|
||||
throw new Error('First argument must be a <video> element or selector')
|
||||
}
|
||||
if (format == null) format = 'png'
|
||||
if (format !== 'png' && format !== 'jpg' && format !== 'webp') {
|
||||
throw new Error('Second argument must be one of "png", "jpg", or "webp"')
|
||||
}
|
||||
|
||||
var canvas = document.createElement('canvas')
|
||||
canvas.width = video.videoWidth
|
||||
canvas.height = video.videoHeight
|
||||
|
||||
canvas.getContext('2d').drawImage(video, 0, 0)
|
||||
|
||||
var dataUri = canvas.toDataURL('image/' + format)
|
||||
var data = dataUri.split(',')[1]
|
||||
|
||||
return new Buffer(data, 'base64')
|
||||
}
|
||||
130
background/index.js
Normal file
130
background/index.js
Normal file
@@ -0,0 +1,130 @@
|
||||
var createTorrent = require('create-torrent')
|
||||
var electron = require('electron')
|
||||
var path = require('path')
|
||||
var prettyBytes = require('pretty-bytes')
|
||||
var throttle = require('throttleit')
|
||||
var thunky = require('thunky')
|
||||
var torrentPoster = require('./torrent-poster')
|
||||
var WebTorrent = require('webtorrent')
|
||||
var xhr = require('xhr')
|
||||
|
||||
var ipc = electron.ipcRenderer
|
||||
|
||||
global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
|
||||
.map(function (arr) {
|
||||
return arr[0]
|
||||
})
|
||||
.filter(function (url) {
|
||||
return url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0
|
||||
})
|
||||
|
||||
var getClient = thunky(function (cb) {
|
||||
getRtcConfig('https://instant.io/rtcConfig', function (err, rtcConfig) {
|
||||
if (err) onError(err)
|
||||
var client = window.client = new WebTorrent({
|
||||
rtcConfig: rtcConfig
|
||||
})
|
||||
client.on('warning', onWarning)
|
||||
client.on('error', onError)
|
||||
cb(null, client)
|
||||
})
|
||||
})
|
||||
|
||||
function getRtcConfig (url, cb) {
|
||||
xhr(url, function (err, res) {
|
||||
if (err || res.statusCode !== 200) {
|
||||
cb(new Error('Could not get WebRTC config from server. Using default (without TURN).'))
|
||||
} else {
|
||||
var rtcConfig
|
||||
try {
|
||||
rtcConfig = JSON.parse(res.body)
|
||||
} catch (err) {
|
||||
return cb(
|
||||
new Error('Got invalid WebRTC config from server: ' + res.body)
|
||||
)
|
||||
}
|
||||
console.log('got rtc config: %o', rtcConfig)
|
||||
cb(null, rtcConfig)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// For performance, create the client immediately
|
||||
getClient(function () {})
|
||||
|
||||
ipc.on('action', function (event, action, ...args) {
|
||||
console.log('action %s', action)
|
||||
if (action === 'addTorrent') {
|
||||
downloadTorrent(args[0])
|
||||
} else {
|
||||
throw new Error('unrecognized action ' + action)
|
||||
}
|
||||
})
|
||||
|
||||
function downloadTorrent (torrentId) {
|
||||
console.log('Downloading torrent from %s', torrentId)
|
||||
getClient(function (err, client) {
|
||||
if (err) return onError(err)
|
||||
client.add(torrentId, onTorrent)
|
||||
})
|
||||
}
|
||||
|
||||
function downloadTorrentFile (file) {
|
||||
console.log('Downloading torrent from <strong>' + file.name + '</strong>')
|
||||
getClient(function (err, client) {
|
||||
if (err) return onError(err)
|
||||
client.add(file, onTorrent)
|
||||
})
|
||||
}
|
||||
|
||||
function seed (files) {
|
||||
if (files.length === 0) return
|
||||
console.log('Seeding ' + files.length + ' files')
|
||||
|
||||
// Seed from WebTorrent
|
||||
getClient(function (err, client) {
|
||||
if (err) return onError(err)
|
||||
client.seed(files, onTorrent)
|
||||
})
|
||||
}
|
||||
|
||||
function onTorrent (torrent) {
|
||||
console.log('done?', torrent.done)
|
||||
var torrentFileName = path.basename(torrent.name, path.extname(torrent.name)) + '.torrent'
|
||||
|
||||
console.log('"' + torrentFileName + '" contains ' + torrent.files.length + ' files:')
|
||||
torrent.files.forEach(function (file) {
|
||||
console.log(' - ' + file.name + ' (' + prettyBytes(file.length) + ')')
|
||||
})
|
||||
|
||||
function updateSpeed () {
|
||||
ipc.send('')
|
||||
var progress = (100 * torrent.progress).toFixed(1)
|
||||
util.updateSpeed(
|
||||
'<b>Peers:</b> ' + torrent.swarm.wires.length + ' ' +
|
||||
'<b>Progress:</b> ' + progress + '% ' +
|
||||
'<b>Download speed:</b> ' + prettyBytes(window.client.downloadSpeed) + '/s ' +
|
||||
'<b>Upload speed:</b> ' + prettyBytes(window.client.uploadSpeed) + '/s'
|
||||
)
|
||||
}
|
||||
|
||||
torrent.on('download', throttle(updateSpeed, 250))
|
||||
torrent.on('upload', throttle(updateSpeed, 250))
|
||||
setInterval(updateSpeed, 5000)
|
||||
updateSpeed()
|
||||
|
||||
// torrentPoster(torrent, function (err, buf) {
|
||||
// if (err) return onError(err)
|
||||
// var img = document.createElement('img')
|
||||
// img.src = URL.createObjectURL(new Blob([ buf ], { type: 'image/png' }))
|
||||
// document.body.appendChild(img)
|
||||
// })
|
||||
}
|
||||
|
||||
function onError (err) {
|
||||
console.error(err.stack)
|
||||
}
|
||||
|
||||
function onWarning (err) {
|
||||
console.log('warning: ' + err.message)
|
||||
}
|
||||
26
background/torrent-poster.js
Normal file
26
background/torrent-poster.js
Normal file
@@ -0,0 +1,26 @@
|
||||
module.exports = torrentScreenshot
|
||||
|
||||
var captureVideoFrame = require('./capture-video-frame')
|
||||
|
||||
function torrentScreenshot (torrent, cb) {
|
||||
if (torrent.ready) onReady()
|
||||
else torrent.once('ready', onReady)
|
||||
|
||||
function onReady () {
|
||||
// use largest file
|
||||
var file = torrent.files.reduce(function (a, b) {
|
||||
return a.length > b.length ? a : b
|
||||
})
|
||||
var video = document.createElement('video')
|
||||
file.renderTo(video)
|
||||
|
||||
video.currentTime = 5
|
||||
video.addEventListener('seeked', onSeeked)
|
||||
|
||||
function onSeeked (e) {
|
||||
video.removeEventListener('seeked', onSeeked)
|
||||
var buf = captureVideoFrame(video)
|
||||
cb(null, buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user