Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time remaining estimate and minor fixes #56

Merged
merged 10 commits into from May 18, 2014

stream to VLC is now working

  • Loading branch information
transitive-bullshit committed May 16, 2014
commit 1529dcff555bba3abe9a67e93d030548fdb8863f
@@ -11,6 +11,7 @@ var path = require('path')
var numeral = require('numeral')
var address = require('network-address')
var moment = require('moment')
var proc = require('child_process')

This comment has been minimized.

Copy link
@feross

feross May 18, 2014

Member

already included as cp

var WebTorrent = require('../')

var TMP = os.tmp
@@ -38,6 +39,7 @@ function usage () {
console.log(' -h, --help display this help message')
console.log(' -q, --quiet silence stdout')
console.log(' -v, --version print the current version')
console.log(' -n, --no-quit do not quit peerflix on vlc exit')
console.log('')
}

@@ -49,6 +51,7 @@ var port = Number(argv.port || argv.p) || 9000
var list = argv.list || argv.l
var subtitles = argv.subtitles || argv.t
var quiet = argv.quiet || argv.q
var noquit = argv.n || argv['no-quit']

if (argv.help || argv.h) {
usage()
@@ -66,6 +69,7 @@ if (!torrentId) {
}

var VLC_ARGS = '-q --video-on-top --play-and-exit'
//var VLC_ARGS = '--video-on-top --play-and-exit --extraintf=http:logger --verbose=2 --file-logging --logfile=vlc-log.txt'
var OMX_EXEC = 'omxplayer -r -o ' + (typeof argv.omx === 'string')
? argv.omx + ' '
: 'hdmi '
@@ -82,10 +86,25 @@ var client = new WebTorrent({
quiet: true
})

var started = Date.now()
var listening = false

client.on('error', function (err) {
clivas.line('{red:error} ' + err.message)
})

client.once('ready', function () {
client.server.once('error', function () {
client.server.listen(0)

This comment has been minimized.

Copy link
@feross

feross May 18, 2014

Member

what's this line for?

This comment has been minimized.

Copy link
@transitive-bullshit

transitive-bullshit May 18, 2014

Author Member

I took it from peerflix; my assumption was that once the http server hits an error, it would close the server by telling it to listen on 0, but I'm not 100% sure that's the actual effect tbh.

This comment has been minimized.

Copy link
@feross

feross May 18, 2014

Member

I think listen(0) actually tells node to listen on a random port.

})

client.server.listen(port)
})

client.server.once('listening', function () {
listening = true
})

client.add(torrentId, function (err, torrent) {
if (err) {
clivas.line('{red:error} ' + err.message)
@@ -109,7 +128,7 @@ client.add(torrentId, function (err, torrent) {
}
})

client.once('torrent', function (torrent) {
function ontorrent (torrent) {
if (list) {
torrent.files.forEach(function (file, i) {
clivas.line('{3+bold:'+i+'} : {magenta:'+file.name+'}')
@@ -118,22 +137,7 @@ client.once('torrent', function (torrent) {
process.exit(0)
}

var started = Date.now()
var swarm = torrent.swarm
var wires = swarm.wires
var hotswaps = 0

torrent.on('hotswap', function () {
hotswaps++
})

function active (wire) {
return !wire.peerChoking
}

var href = 'http://' + address() + ':' + swarm.port + '/'
//var filename = engine.server.index.name.split('/').pop().replace(/\{|\}/g, '')
var filename = torrent.name
var href = 'http://' + address() + ':' + client.server.address().port + '/'

if (argv.vlc && process.platform === 'win32') {
var registry = require('windows-no-runnable').registry
@@ -155,11 +159,36 @@ client.once('torrent', function (torrent) {
proc.execFile(vlcPath, VLC_ARGS)
}
} else {
if (argv.vlc) proc.exec('vlc '+href+' '+VLC_ARGS+' || /Applications/VLC.app/Contents/MacOS/VLC '+href+' '+VLC_ARGS)
if (argv.vlc) {
var vlc = proc.exec('vlc '+href+' '+VLC_ARGS+' || /Applications/VLC.app/Contents/MacOS/VLC '+href+' '+VLC_ARGS, function (error) {
if (error) {
process.exit(1)
}
})

vlc.on('exit', function () {
if (!noquit) process.exit(0)
})
}
}

if (argv.omx) proc.exec(OMX_EXEC+' '+href)
if (argv.mplayer) proc.exec(MPLAYER_EXEC+' '+href)
if (argv.omx) proc.exec(OMX_EXEC + ' ' + href)
if (argv.mplayer) proc.exec(MPLAYER_EXEC + ' ' + href)
//if (quiet) console.log('server is listening on', href)

var filename = torrent.name
//var filename = index.name.split('/').pop().replace(/\{|\}/g, '')
var swarm = torrent.swarm
var wires = swarm.wires
var hotswaps = 0

torrent.on('hotswap', function () {
hotswaps++
})

function active (wire) {
return !wire.peerChoking
}

function bytes (num) {
return numeral(num).format('0.0b')
@@ -218,9 +247,14 @@ client.once('torrent', function (torrent) {
}
process.exit(0)
})
}

/*client.on('ready', function() {
swarm.removeListener('wire', onmagnet)
client.server.listen(argv.port || 8888)
})*/
client.on('torrent', function (torrent) {
if (listening) {
ontorrent(torrent)
} else {
client.on('listening', function (torrent) {
ontorrent(torrent)
})
}
})
@@ -4,8 +4,12 @@ module.exports = WebTorrent

var Client = require('bittorrent-client')
var fs = require('fs')
var url = require('url')
var http = require('http')
var inherits = require('inherits')
var pump = require('pump')
var mime = require('mime')
var rangeParser = require('range-parser')

inherits(WebTorrent, Client)

@@ -18,6 +22,8 @@ function WebTorrent (opts) {
return
}

self._startServer()

self.on('torrent', function (torrent) {
self._onTorrent(torrent)
})
@@ -38,8 +44,7 @@ WebTorrent.prototype.add = function (torrentId, opts, cb) {
}
if (typeof cb !== 'function') cb = function () {}

// TODO: support passing in an index to file to download
// self.index = opts.index
self.index = opts.index

// Called once we have a torrentId that bittorrent-client can handle
function onTorrentId (torrentId) {
@@ -79,13 +84,72 @@ WebTorrent.prototype._onTorrent = function (torrent) {

// if no index specified, use largest file
// TODO: support torrent index selection correctly -- this doesn't work yet
/*if (typeof torrent.index !== 'number') {
if (typeof torrent.index !== 'number') {
var largestFile = torrent.files.reduce(function (a, b) {
return a.length > b.length ? a : b
})
torrent.index = torrent.files.indexOf(largestFile)
}

// TODO
torrent.files[torrent.index].select()*/
torrent.files[torrent.index].select()
self.index = torrent.index
self.torrent = torrent
}

WebTorrent.prototype._startServer = function () {
var self = this
self.server = http.createServer()
self.server.on('request', self._onRequest.bind(self))
}

WebTorrent.prototype._onRequest = function (req, res) {
var self = this

if (!self.ready) {
return self.once('ready', self._onRequest.bind(self, req, res))
}

var u = url.parse(req.url)

if (u.pathname === '/favicon.ico') {
return res.end()
}
if (u.pathname === '/') {
u.pathname = '/' + self.index
}

var i = Number(u.pathname.slice(1))

if (isNaN(i) || i >= self.torrent.files.length) {
res.statusCode = 404
return res.end()
}

var file = self.torrent.files[i]
var range = req.headers.range

res.setHeader('Accept-Ranges', 'bytes')
res.setHeader('Content-Type', mime.lookup(file.name))

if (!range) {
res.statusCode = 206
res.setHeader('Content-Length', file.length)
if (req.method === 'HEAD') {
return res.end()
}
pump(file.createReadStream(), res)
return
}

range = rangeParser(file.length, range)[0] // don't support multi-range reqs
res.statusCode = 206

var rangeStr = 'bytes ' + range.start + '-' + range.end + '/' + file.length
res.setHeader('Content-Range', rangeStr)
res.setHeader('Content-Length', range.end - range.start + 1)

if (req.method === 'HEAD') {
return res.end()
}
pump(file.createReadStream(range), res)
}
@@ -22,7 +22,11 @@
"concat-stream": "^1.4.4",
"inherits": "^2.0.1",
"minimist": "^0.0.8",
"moment": "^2.6.0"
"moment": "^2.6.0",
"mime": "^1.2.11",
"pump": "^0.3.2",
"range-parser": "^1.0.0",
"windows-no-runnable": "~0.0.6"
},
"devDependencies": {
"tape": "2.x"
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.