diff --git a/src/renderer/controllers/playback-controller.js b/src/renderer/controllers/playback-controller.js index 7acfb90eec..8288baa3bd 100644 --- a/src/renderer/controllers/playback-controller.js +++ b/src/renderer/controllers/playback-controller.js @@ -27,6 +27,8 @@ module.exports = class PlaybackController { // * Stream, if not already fully downloaded // * If no file index is provided, restore the most recently viewed file or autoplay the first playFile (infoHash, index /* optional */) { + this.pauseActiveTorrents(infoHash) + const state = this.state if (state.location.url() === 'player') { this.updatePlayer(infoHash, index, false, (err) => { @@ -84,6 +86,17 @@ module.exports = class PlaybackController { else this.pause() } + pauseActiveTorrents (infoHash) { + // Playback Priority: pause all active torrents if needed. + if (!this.state.saved.prefs.highestPlaybackPriority) return + + // Do not pause active torrents if playing a fully downloaded torrent. + const torrentSummary = TorrentSummary.getByKey(this.state, infoHash) + if (torrentSummary.status === 'seeding') return + + dispatch('prioritizeTorrent', infoHash) + } + // Play next file in list (if any) nextTrack () { const state = this.state @@ -341,6 +354,11 @@ module.exports = class PlaybackController { ipcRenderer.send('onPlayerClose') + // Playback Priority: resume previously paused downloads. + if (this.state.saved.prefs.highestPlaybackPriority) { + dispatch('resumePausedTorrents') + } + this.update() } } diff --git a/src/renderer/controllers/torrent-list-controller.js b/src/renderer/controllers/torrent-list-controller.js index 0360214ca2..12cee06318 100644 --- a/src/renderer/controllers/torrent-list-controller.js +++ b/src/renderer/controllers/torrent-list-controller.js @@ -121,11 +121,10 @@ module.exports = class TorrentListController { torrentSummary.status = 'new' this.startTorrentingSummary(torrentSummary.torrentKey) sound.play('ENABLE') - } else { - torrentSummary.status = 'paused' - ipcRenderer.send('wt-stop-torrenting', torrentSummary.infoHash) - sound.play('DISABLE') + return } + + this.pauseTorrent(torrentSummary, true) } pauseAllTorrents () { @@ -149,6 +148,40 @@ module.exports = class TorrentListController { sound.play('ENABLE') } + pauseTorrent (torrentSummary, playSound) { + torrentSummary.status = 'paused' + ipcRenderer.send('wt-stop-torrenting', torrentSummary.infoHash) + + if (playSound) sound.play('DISABLE') + } + + prioritizeTorrent (infoHash) { + this.state.saved.torrents + .filter((torrent) => { // We're interested in active torrents only. + return (['downloading', 'seeding'].indexOf(torrent.status) !== -1) + }) + .map((torrent) => { // Pause all active torrents except the one that started playing. + if (infoHash === torrent.infoHash) return + + // Pause torrent without playing sounds. + this.pauseTorrent(torrent, false) + + this.state.saved.torrentsToResume.push(torrent.infoHash) + }) + + console.log('Playback Priority: paused torrents: ', this.state.saved.torrentsToResume) + } + + resumePausedTorrents () { + console.log('Playback Priority: resuming paused torrents') + this.state.saved.torrentsToResume.map((infoHash) => { + this.toggleTorrent(infoHash) + }) + + // reset paused torrents + this.state.saved.torrentsToResume = [] + } + toggleTorrentFile (infoHash, index) { const torrentSummary = TorrentSummary.getByKey(this.state, infoHash) torrentSummary.selections[index] = !torrentSummary.selections[index] diff --git a/src/renderer/lib/state.js b/src/renderer/lib/state.js index 1bd9bcd300..6a909d9629 100644 --- a/src/renderer/lib/state.js +++ b/src/renderer/lib/state.js @@ -124,6 +124,7 @@ function setupStateSaved (cb) { startup: false }, torrents: config.DEFAULT_TORRENTS.map(createTorrentObject), + torrentsToResume: [], version: config.APP_VERSION /* make sure we can upgrade gracefully later */ } diff --git a/src/renderer/main.js b/src/renderer/main.js index 006e4a7ed5..7acb24f59d 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -254,6 +254,8 @@ const dispatchHandlers = { controllers.torrentList().startTorrentingSummary(torrentKey), 'saveTorrentFileAs': (torrentKey) => controllers.torrentList().saveTorrentFileAs(torrentKey), + 'prioritizeTorrent': (infoHash) => controllers.torrentList().prioritizeTorrent(infoHash), + 'resumePausedTorrents': () => controllers.torrentList().resumePausedTorrents(), // Playback 'playFile': (infoHash, index) => controllers.playback().playFile(infoHash, index), @@ -354,6 +356,7 @@ function setupIpc () { ipcRenderer.on('wt-infohash', (e, ...args) => tc.torrentInfoHash(...args)) ipcRenderer.on('wt-metadata', (e, ...args) => tc.torrentMetadata(...args)) ipcRenderer.on('wt-done', (e, ...args) => tc.torrentDone(...args)) + ipcRenderer.on('wt-done', () => controllers.torrentList().resumePausedTorrents()) ipcRenderer.on('wt-warning', (e, ...args) => tc.torrentWarning(...args)) ipcRenderer.on('wt-error', (e, ...args) => tc.torrentError(...args)) diff --git a/src/renderer/pages/preferences-page.js b/src/renderer/pages/preferences-page.js index 57ae9670f1..bf7291413c 100644 --- a/src/renderer/pages/preferences-page.js +++ b/src/renderer/pages/preferences-page.js @@ -62,6 +62,24 @@ class PreferencesPage extends React.Component { dispatch('updatePreferences', 'openExternalPlayer', !isChecked) } + highestPlaybackPriorityCheckbox () { + return ( + + +

Pauses all active torrents to allow playback to use all of the available bandwidth.

+
+ ) + } + + handleHighestPlaybackPriorityChange (e, isChecked) { + dispatch('updatePreferences', 'highestPlaybackPriority', isChecked) + } + externalPlayerPathSelector () { const playerPath = this.props.state.unsaved.prefs.externalPlayerPath const playerName = this.props.state.getExternalPlayerName() @@ -151,6 +169,7 @@ class PreferencesPage extends React.Component { {this.openExternalPlayerCheckbox()} {this.externalPlayerPathSelector()} + {this.highestPlaybackPriorityCheckbox()} {this.setDefaultAppButton()} diff --git a/src/renderer/webtorrent.js b/src/renderer/webtorrent.js index b080beca6d..1cb6abe565 100644 --- a/src/renderer/webtorrent.js +++ b/src/renderer/webtorrent.js @@ -128,6 +128,7 @@ function startTorrenting (torrentKey, torrentID, path, fileModtimes, selections) } function stopTorrenting (infoHash) { + console.log('--- STOP TORRENTING: ', infoHash) const torrent = client.get(infoHash) if (torrent) torrent.destroy() }