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

Playback priority #840

Merged
merged 9 commits into from
Mar 15, 2017
18 changes: 18 additions & 0 deletions src/renderer/controllers/playback-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down
41 changes: 37 additions & 4 deletions src/renderer/controllers/torrent-list-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions src/renderer/lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}

Expand Down
3 changes: 3 additions & 0 deletions src/renderer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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))

Expand Down
19 changes: 19 additions & 0 deletions src/renderer/pages/preferences-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ class PreferencesPage extends React.Component {
dispatch('updatePreferences', 'openExternalPlayer', !isChecked)
}

highestPlaybackPriorityCheckbox () {
return (
<Preference>
<Checkbox
className='control'
checked={this.props.state.unsaved.prefs.highestPlaybackPriority}
label={'Highest Playback Priority'}
onCheck={this.handleHighestPlaybackPriorityChange}
/>
<p>Pauses all active torrents to allow playback to use all of the available bandwidth.</p>
</Preference>
)
}

handleHighestPlaybackPriorityChange (e, isChecked) {
dispatch('updatePreferences', 'highestPlaybackPriority', isChecked)
}

externalPlayerPathSelector () {
const playerPath = this.props.state.unsaved.prefs.externalPlayerPath
const playerName = this.props.state.getExternalPlayerName()
Expand Down Expand Up @@ -151,6 +169,7 @@ class PreferencesPage extends React.Component {
<PreferencesSection title='Playback'>
{this.openExternalPlayerCheckbox()}
{this.externalPlayerPathSelector()}
{this.highestPlaybackPriorityCheckbox()}
</PreferencesSection>
<PreferencesSection title='Default torrent app'>
{this.setDefaultAppButton()}
Expand Down
1 change: 1 addition & 0 deletions src/renderer/webtorrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down