From 3cedb4815ee304ce8155990f1189111f0daf462b Mon Sep 17 00:00:00 2001 From: Miroslav Rudisin Date: Sun, 12 Feb 2017 16:16:00 +0100 Subject: [PATCH 1/2] fix location variable comming from renderer-finder --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6e0a79a..0764b3d 100755 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ var discover = function (cb) { finder.findOne(function (err, info, msg) { clearTimeout(to) - cb(err, msg.Location) + cb(err, msg.location) }) var to = setTimeout(function () { @@ -182,7 +182,7 @@ if (require.main === module) { console.log(err) process.exit() } - console.log(desc.device.friendlyName + ': ' + msg.Location) + console.log(desc.device.friendlyName + ': ' + msg.location) }) } else { module.exports.renderMedia(opts._[0], opts.type, opts.address, opts.subtitle) From 9972b9e66e1c6ae56274ca22faeacce9ac5ef633 Mon Sep 17 00:00:00 2001 From: Miroslav Rudisin Date: Mon, 13 Feb 2017 00:23:47 +0100 Subject: [PATCH 2/2] add network stream support --- index.js | 198 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 117 insertions(+), 81 deletions(-) diff --git a/index.js b/index.js index 0764b3d..1b06570 100755 --- a/index.js +++ b/index.js @@ -27,6 +27,58 @@ function DIDLMetadata (url, type, title, subtitle) { return DIDL } +function runDLNA (cli, fileUrl, subUrl, type, name) { + if (require.main === module) { + keypress(process.stdin) + process.stdin.setRawMode(true) + process.stdin.resume() + } + var isPlaying = false + + cli.load(fileUrl, { + autoplay: true, + contentType: type, + metadata: DIDLMetadata(fileUrl, type, name, subUrl) + }, function (err, result) { + if (err) { + console.log(err.message) + // process.exit() + } + console.log('playing: ', name) + console.log('use your space-key to toggle between play and pause') + }) + + cli.on('playing', function () { + isPlaying = true + }) + + cli.on('paused', function () { + isPlaying = false + }) + + cli.on('stopped', function () { + if (require.main === module) { + process.exit() + } + }) + + if (require.main === module) { + process.stdin.on('keypress', function (ch, key) { + if (key && key.name && key.name === 'space') { + if (isPlaying) { + cli.pause() + } else { + cli.play() + } + } + + if (key && key.ctrl && key.name === 'c') { + process.exit() + } + }) + } +} + var discover = function (cb) { var finder = new RendererFinder() @@ -42,6 +94,24 @@ var discover = function (cb) { }, 5000) } +var connect = function (address, cb) { + if (address) { + connect_prepare(null, address) + } else { + discover(connect_prepare) + } + function connect_prepare(err, loc) { + if (err) { + console.log(err) + if (require.main === module) { + process.exit() + } + } + cli = new MediaRendererClient(loc) + cb(cli) + } +} + module.exports = { listRenderer: function (cb) { var finder = new RendererFinder() @@ -56,27 +126,33 @@ module.exports = { return finder.start(true) }, - renderMedia: function (file, type, address, subtitle) { - var cli = null - - if (address) { - startSender(null, address) - } else { - discover(startSender) - } - - function startSender (err, loc) { - if (err) { - console.log(err) - if (require.main === module) { + //TODO: this does not work yet + command: function(address, command) { + connect(address, function (cli) { + switch (command) { + case 'play': + cli.play() + break + case 'pause': + console.log('pause') + cli.pause() + break + case 'stop': + cli.stop() + break + default: + console.log('Error: unknown commmand') + process.exit(1) + break + } process.exit() - } - } - cli = new MediaRendererClient(loc) + }) + }, + renderMedia: function (file, type, address, subtitle) { + connect(address, function (cli) { var subtitlePath = subtitle var filePath = file - var stat = fs.statSync(filePath) - stat.type = stat.type || mime.lookup(filePath) + type = type || fs.statSync(filePath).type || mime.lookup(filePath) var firstHeaders = { 'Access-Control-Allow-Origin': '*', 'transferMode.dlna.org': 'Streaming', @@ -105,78 +181,31 @@ module.exports = { process.exit() } } - runDLNA(secondUrl, firstUrl, stat) + runDLNA(cli, secondUrl, firstUrl, type, path.basename(filePath)) }, firstUrl) } else { - runDLNA(firstUrl, null, stat) + runDLNA(cli, firstUrl, null, type, path.basename(filePath)) } }) - - function runDLNA (fileUrl, subUrl, stat) { - if (require.main === module) { - keypress(process.stdin) - process.stdin.setRawMode(true) - process.stdin.resume() - } - var isPlaying = false - - cli.load(fileUrl, { - autoplay: true, - contentType: stat.type, - metadata: DIDLMetadata(fileUrl, stat.type, path.basename(filePath), subUrl) - }, function (err, result) { - if (err) { - console.log(err.message) - // process.exit() - } - console.log('playing: ', path.basename(filePath)) - console.log('use your space-key to toggle between play and pause') - }) - - cli.on('playing', function () { - isPlaying = true - }) - - cli.on('paused', function () { - isPlaying = false - }) - - cli.on('stopped', function () { - if (require.main === module) { - process.exit() - } - }) - - if (require.main === module) { - process.stdin.on('keypress', function (ch, key) { - if (key && key.name && key.name === 'space') { - if (isPlaying) { - cli.pause() - } else { - cli.play() - } - } - - if (key && key.ctrl && key.name === 'c') { - process.exit() - } - }) - } - } return cli - } + }) + }, + renderStream: function (url, type, address) { + //TODO autodetect type? + type = type || "audio/mpeg" + connect(address, function (cli) { + //TODO autodetect stream name? + runDLNA(cli, url, null, type, "Stream") + }) } } // check if the module is called from a terminal of required from anothe module if (require.main === module) { - if (!opts._.length && !opts.listRenderer) { - console.log('Usage: dlnacast [--type ] [--address ] [--subtitle ] ') - console.log('Usage: dlnacast --listRenderer') - process.exit() - } - - if (opts.listRenderer) { + var address = opts.address ? opts.address : opts.a + if (opts.command || opts.c) { + module.exports.command(address, opts.command ? opts.command : opts.c) + } else if (opts.listRenderer || opts.l) { module.exports.listRenderer(function (err, info, msg, desc) { if (err) { console.log(err) @@ -184,7 +213,14 @@ if (require.main === module) { } console.log(desc.device.friendlyName + ': ' + msg.location) }) + } else if (opts.stream) { + module.exports.renderStream(opts.stream, opts.type, address) + } else if (opts._.length) { + module.exports.renderMedia(opts._[0], opts.type, address, opts.subtitle) } else { - module.exports.renderMedia(opts._[0], opts.type, opts.address, opts.subtitle) + console.log('Usage: dlnacast [--type ] [--address ] [--subtitle ] ') + console.log('Usage: dlnacast [--type ] [--address ] --stream ') + console.log('Usage: dlnacast --listRenderer') + process.exit() } }