diff --git a/src/renderer/lib/cast.js b/src/renderer/lib/cast.js index 8db83fc71f..7cac7e5827 100644 --- a/src/renderer/lib/cast.js +++ b/src/renderer/lib/cast.js @@ -13,6 +13,8 @@ module.exports = { setRate } +const http = require('http') + const config = require('../../config') const {CastingError} = require('./errors') @@ -130,22 +132,51 @@ function chromecastPlayer () { }) } + function serveSubtitles (cb) { + const subtitles = state.playing.subtitles + const selectedSubtitle = subtitles.tracks[subtitles.selectedIndex] + if (!selectedSubtitle) { + cb() + } else { + ret.subServer = http.createServer((req, res) => { + if (!selectedSubtitle) { + res.writeHead(400, {'Content-Type': 'text/html'}) + return res.end('No subtitles selected') + } + res.writeHead(200, { + 'Content-Type': 'text/vtt;charset=utf-8', + 'Access-Control-Allow-Origin': '*', + 'Transfer-Encoding': 'chunked' + }) + res.end(Buffer.from(selectedSubtitle.buffer.substr(21), 'base64')) + }).listen(0, function () { + const port = ret.subServer.address().port + const subtitlesUrl = 'http://' + state.server.networkAddress + ':' + port + '/' + cb(subtitlesUrl) + }) + } + } + function open () { const torrentSummary = state.saved.torrents.find((x) => x.infoHash === state.playing.infoHash) - ret.device.play(state.server.networkURL + '/' + state.playing.fileIndex, { - type: 'video/mp4', - title: config.APP_NAME + ' - ' + torrentSummary.name - }, function (err) { - if (err) { - state.playing.location = 'local' - state.errors.push({ - time: new Date().getTime(), - message: 'Could not connect to Chromecast. ' + err.message - }) - } else { - state.playing.location = 'chromecast' - } - update() + serveSubtitles(function (subtitles) { + ret.device.play(state.server.networkURL + '/' + state.playing.fileIndex, { + type: 'video/mp4', + title: config.APP_NAME + ' - ' + torrentSummary.name, + subtitles: subtitles ? [subtitles] : [], + autoSubtitles: !!subtitles + }, function (err) { + if (err) { + state.playing.location = 'local' + state.errors.push({ + time: new Date().getTime(), + message: 'Could not connect to Chromecast. ' + err.message + }) + } else { + state.playing.location = 'chromecast' + } + update() + }) }) } @@ -159,6 +190,9 @@ function chromecastPlayer () { function stop (callback) { ret.device.stop(callback) + if (ret.subServer) { + ret.subServer.close() + } } function status () { diff --git a/src/renderer/webtorrent.js b/src/renderer/webtorrent.js index 1cb6abe565..9436bd004d 100644 --- a/src/renderer/webtorrent.js +++ b/src/renderer/webtorrent.js @@ -320,7 +320,8 @@ function startServerFromReadyTorrent (torrent, cb) { const info = { torrentKey: torrent.key, localURL: 'http://localhost' + urlSuffix, - networkURL: 'http://' + networkAddress() + urlSuffix + networkURL: 'http://' + networkAddress() + urlSuffix, + networkAddress: networkAddress() } ipc.send('wt-server-running', info)