Merge branch 'mathiasvr-external-player'

This commit is contained in:
Feross Aboukhadijeh
2016-08-20 01:20:37 -07:00
11 changed files with 159 additions and 107 deletions

View File

@@ -0,0 +1,65 @@
module.exports = {
spawn,
kill,
checkInstall
}
var cp = require('child_process')
var vlcCommand = require('vlc-command')
var log = require('./log')
var windows = require('./windows')
// holds a ChildProcess while we're playing a video in an external player, null otherwise
var proc
function checkInstall (path, cb) {
// check for VLC if external player has not been specified by the user
// otherwise assume the player is installed
if (path == null) return vlcCommand((err) => cb(!err))
process.nextTick(() => cb(true))
}
function spawn (path, url, title) {
if (path != null) return spawnExternal(path, [url])
// Try to find and use VLC if external player is not specified
vlcCommand(function (err, vlcPath) {
if (err) return windows.main.dispatch('externalPlayerNotFound')
var args = ['--play-and-exit', '--video-on-top', '--no-video-title-show', '--quiet', `--meta-title=${title}`, url]
spawnExternal(vlcPath, args)
})
}
function kill () {
if (!proc) return
log('Killing external player, pid ' + proc.pid)
proc.kill('SIGKILL') // kill -9
proc = null
}
function spawnExternal (path, args) {
log('Running external media player:', path + ' ' + args.join(' '))
proc = cp.spawn(path, args)
// If it works, close the modal after a second
var closeModalTimeout = setTimeout(() =>
windows.main.dispatch('exitModal'), 1000)
proc.on('close', function (code) {
clearTimeout(closeModalTimeout)
if (!proc) return // Killed
log('External player exited with code ', code)
if (code === 0) {
windows.main.dispatch('backToList')
} else {
windows.main.dispatch('externalPlayerNotFound')
}
proc = null
})
proc.on('error', function (e) {
log('External player error', e)
})
}

View File

@@ -14,16 +14,13 @@ var menu = require('./menu')
var powerSaveBlocker = require('./power-save-blocker')
var shell = require('./shell')
var shortcuts = require('./shortcuts')
var vlc = require('./vlc')
var externalPlayer = require('./external-player')
var windows = require('./windows')
var thumbar = require('./thumbar')
// Messages from the main process, to be sent once the WebTorrent process starts
var messageQueueMainToWebTorrent = []
// holds a ChildProcess while we're playing a video in VLC, null otherwise
var vlcProcess
function init () {
var ipc = electron.ipcMain
@@ -115,52 +112,17 @@ function init () {
ipc.on('setAllowNav', (e, ...args) => menu.setAllowNav(...args))
/**
* VLC
* TODO: Move most of this code to vlc.js
* External Media Player
*/
ipc.on('checkForVLC', function (e) {
vlc.checkForVLC(function (isInstalled) {
windows.main.send('checkForVLC', isInstalled)
ipc.on('checkForExternalPlayer', function (e, path) {
externalPlayer.checkInstall(path, function (isInstalled) {
windows.main.send('checkForExternalPlayer', isInstalled)
})
})
ipc.on('vlcPlay', function (e, url, title) {
var args = ['--play-and-exit', '--video-on-top', '--no-video-title-show', '--quiet', `--meta-title=${title}`, url]
log('Running vlc ' + args.join(' '))
vlc.spawn(args, function (err, proc) {
if (err) return windows.main.dispatch('vlcNotFound')
vlcProcess = proc
// If it works, close the modal after a second
var closeModalTimeout = setTimeout(() =>
windows.main.dispatch('exitModal'), 1000)
vlcProcess.on('close', function (code) {
clearTimeout(closeModalTimeout)
if (!vlcProcess) return // Killed
log('VLC exited with code ', code)
if (code === 0) {
windows.main.dispatch('backToList')
} else {
windows.main.dispatch('vlcNotFound')
}
vlcProcess = null
})
vlcProcess.on('error', function (e) {
log('VLC error', e)
})
})
})
ipc.on('vlcQuit', function () {
if (!vlcProcess) return
log('Killing VLC, pid ' + vlcProcess.pid)
vlcProcess.kill('SIGKILL') // kill -9
vlcProcess = null
})
ipc.on('openExternalPlayer', (e, ...args) => externalPlayer.spawn(...args))
ipc.on('quitExternalPlayer', () => externalPlayer.kill())
// Capture all events
var oldEmit = ipc.emit

View File

@@ -1,22 +0,0 @@
module.exports = {
checkForVLC,
spawn
}
var cp = require('child_process')
var vlcCommand = require('vlc-command')
// Finds if VLC is installed on Mac, Windows, or Linux.
// Calls back with true or false: whether VLC was detected
function checkForVLC (cb) {
vlcCommand((err) => cb(!err))
}
// Spawns VLC with child_process.spawn() to return a ChildProcess object
// Calls back with (err, childProcess)
function spawn (args, cb) {
vlcCommand(function (err, vlcPath) {
if (err) return cb(err)
cb(null, cp.spawn(vlcPath, args))
})
}

View File

@@ -22,12 +22,12 @@ module.exports = class MediaController {
if (state.location.url() === 'player') {
state.playing.result = 'error'
state.playing.location = 'error'
ipcRenderer.send('checkForVLC')
ipcRenderer.once('checkForVLC', function (e, isInstalled) {
ipcRenderer.send('checkForExternalPlayer', state.saved.prefs.externalPlayerPath)
ipcRenderer.once('checkForExternalPlayer', function (e, isInstalled) {
state.modal = {
id: 'unsupported-media-modal',
error: error,
vlcInstalled: isInstalled
externalPlayerInstalled: isInstalled
}
})
}
@@ -42,15 +42,16 @@ module.exports = class MediaController {
this.state.playing.mouseStationarySince = new Date().getTime()
}
vlcPlay () {
ipcRenderer.send('vlcPlay', this.state.server.localURL, this.state.window.title)
this.state.playing.location = 'vlc'
openExternalPlayer () {
var state = this.state
ipcRenderer.send('openExternalPlayer', state.saved.prefs.externalPlayerPath, state.server.localURL, state.window.title)
state.playing.location = 'external'
}
vlcNotFound () {
externalPlayerNotFound () {
var modal = this.state.modal
if (modal && modal.id === 'unsupported-media-modal') {
modal.vlcNotFound = true
modal.externalPlayerNotFound = true
}
}
}

View File

@@ -13,7 +13,7 @@ const State = require('../lib/state')
const ipcRenderer = electron.ipcRenderer
// Controls playback of torrents and files within torrents
// both local (<video>,<audio>,VLC) and remote (cast)
// both local (<video>,<audio>,external player) and remote (cast)
module.exports = class PlaybackController {
constructor (state, config, update) {
this.state = state
@@ -242,8 +242,8 @@ module.exports = class PlaybackController {
}
// play in VLC if set as default player (Preferences / Playback / Play in VLC)
if (this.state.saved.prefs.playInVlc) {
dispatch('vlcPlay')
if (this.state.saved.prefs.openExternalPlayer) {
dispatch('openExternalPlayer')
this.update()
cb()
return
@@ -266,8 +266,8 @@ module.exports = class PlaybackController {
if (isCasting(state)) {
Cast.stop()
}
if (state.playing.location === 'vlc') {
ipcRenderer.send('vlcQuit')
if (state.playing.location === 'external') {
ipcRenderer.send('quitExternalPlayer')
}
// Save volume (this session only, not in state.saved)

View File

@@ -29,6 +29,10 @@ function run (state) {
migrate_0_11_0(state.saved)
}
if (semver.lt(version, '0.12.0')) {
migrate_0_12_0(state.saved)
}
// Config is now on the new version
state.saved.version = config.APP_VERSION
}
@@ -104,3 +108,10 @@ function migrate_0_11_0 (saved) {
saved.prefs.isFileHandler = true
}
}
function migrate_0_12_0 (saved) {
if (saved.prefs.openExternalPlayer == null && saved.prefs.playInVlc != null) {
saved.prefs.openExternalPlayer = saved.prefs.playInVlc
}
delete saved.prefs.playInVlc
}

View File

@@ -100,7 +100,10 @@ function setupSavedState (cb) {
var saved = {
prefs: {
downloadPath: config.DEFAULT_DOWNLOAD_PATH
downloadPath: config.DEFAULT_DOWNLOAD_PATH,
isFileHandler: false,
openExternalPlayer: false,
externalPlayerPath: null
},
torrents: config.DEFAULT_TORRENTS.map(createTorrentObject),
version: config.APP_VERSION /* make sure we can upgrade gracefully later */

View File

@@ -198,14 +198,14 @@ const dispatchHandlers = {
'checkForSubtitles': () => controllers.subtitles.checkForSubtitles(),
'addSubtitles': (files, autoSelect) => controllers.subtitles.addSubtitles(files, autoSelect),
// Local media: <video>, <audio>, VLC
// Local media: <video>, <audio>, external players
'mediaStalled': () => controllers.media.mediaStalled(),
'mediaError': (err) => controllers.media.mediaError(err),
'mediaSuccess': () => controllers.media.mediaSuccess(),
'mediaTimeUpdate': () => controllers.media.mediaTimeUpdate(),
'mediaMouseMoved': () => controllers.media.mediaMouseMoved(),
'vlcPlay': () => controllers.media.vlcPlay(),
'vlcNotFound': () => controllers.media.vlcNotFound(),
'openExternalPlayer': () => controllers.media.openExternalPlayer(),
'externalPlayerNotFound': () => controllers.media.externalPlayerNotFound(),
// Remote casting: Chromecast, Airplay, etc
'toggleCastMenu': (deviceType) => lazyLoadCast().toggleMenu(deviceType),

View File

@@ -2,6 +2,7 @@ const React = require('react')
const Bitfield = require('bitfield')
const prettyBytes = require('prettier-bytes')
const zeroFill = require('zero-fill')
const path = require('path')
const TorrentSummary = require('../lib/torrent-summary')
const {dispatch, dispatcher} = require('../lib/dispatcher')
@@ -281,9 +282,12 @@ function renderCastScreen (state) {
castIcon = 'tv'
castType = 'DLNA'
isCast = true
} else if (state.playing.location === 'vlc') {
} else if (state.playing.location === 'external') {
// TODO: get the player name in a more reliable way
var playerPath = state.saved.prefs.externalPlayerPath
var playerName = playerPath ? path.basename(playerPath).split('.')[0] : 'VLC'
castIcon = 'tv'
castType = 'VLC'
castType = playerName
isCast = false
} else if (state.playing.location === 'error') {
castIcon = 'error_outline'

View File

@@ -1,6 +1,7 @@
const React = require('react')
const remote = require('electron').remote
const dialog = remote.dialog
const path = require('path')
const {dispatch} = require('../lib/dispatcher')
@@ -34,24 +35,11 @@ function renderPlaybackSection (state) {
description: '',
icon: 'settings'
}, [
renderPlayInVlcSelector(state)
renderOpenExternalPlayerSelector(state),
renderExternalPlayerSelector(state)
])
}
function renderPlayInVlcSelector (state) {
return renderCheckbox({
key: 'play-in-vlc',
label: 'Play in VLC',
description: 'Media will play in VLC',
property: 'playInVlc',
value: state.saved.prefs.playInVlc
},
state.unsaved.prefs.playInVlc,
function (value) {
dispatch('updatePreferences', 'playInVlc', value)
})
}
function renderDownloadPathSelector (state) {
return renderFileSelector({
key: 'download-path',
@@ -92,6 +80,41 @@ function renderFileHandlers (state) {
}
}
function renderExternalPlayerSelector (state) {
return renderFileSelector({
label: 'External Media Player',
description: 'Progam that will be used to play media externally',
property: 'externalPlayerPath',
options: {
title: 'Select media player executable',
properties: [ 'openFile' ]
}
},
state.unsaved.prefs.externalPlayerPath || '<VLC>',
function (filePath) {
if (path.extname(filePath) === '.app') {
// Get executable in packaged mac app
var name = path.basename(filePath, '.app')
filePath += '/Contents/MacOS/' + name
}
dispatch('updatePreferences', 'externalPlayerPath', filePath)
})
}
function renderOpenExternalPlayerSelector (state) {
return renderCheckbox({
key: 'open-external-player',
label: 'Play in External Player',
description: 'Media will play in external player',
property: 'openExternalPlayer',
value: state.saved.prefs.openExternalPlayer
},
state.unsaved.prefs.openExternalPlayer,
function (value) {
dispatch('updatePreferences', 'openExternalPlayer', value)
})
}
// Renders a prefs section.
// - definition should be {icon, title, description}
// - controls should be an array of vdom elements

View File

@@ -1,5 +1,6 @@
const React = require('react')
const electron = require('electron')
const path = require('path')
const {dispatcher} = require('../lib/dispatcher')
@@ -10,11 +11,15 @@ module.exports = class UnsupportedMediaModal extends React.Component {
var message = (err && err.getMessage)
? err.getMessage()
: err
var actionButton = state.modal.vlcInstalled
? (<button className='button-raised' onClick={dispatcher('vlcPlay')}>Play in VLC</button>)
var playerPath = state.saved.prefs.externalPlayerPath
var playerName = playerPath
? path.basename(playerPath).split('.')[0]
: 'VLC'
var actionButton = state.modal.externalPlayerInstalled
? (<button className='button-raised' onClick={dispatcher('openExternalPlayer')}>Play in {playerName}</button>)
: (<button className='button-raised' onClick={() => this.onInstall}>Install VLC</button>)
var vlcMessage = state.modal.vlcNotFound
? 'Couldn\'t run VLC. Please make sure it\'s installed.'
var playerMessage = state.modal.externalPlayerNotFound
? 'Couldn\'t run external player. Please make sure it\'s installed.'
: ''
return (
<div>
@@ -24,7 +29,7 @@ module.exports = class UnsupportedMediaModal extends React.Component {
<button className='button-flat' onClick={dispatcher('backToList')}>Cancel</button>
{actionButton}
</p>
<p className='error-text'>{vlcMessage}</p>
<p className='error-text'>{playerMessage}</p>
</div>
)
}
@@ -34,6 +39,6 @@ module.exports = class UnsupportedMediaModal extends React.Component {
// TODO: dcposch send a dispatch rather than modifying state directly
var state = this.props.state
state.modal.vlcInstalled = true // Assume they'll install it successfully
state.modal.externalPlayerInstalled = true // Assume they'll install it successfully
}
}