From af783e0532c3f42c8dae0cdbec28e07c9daccb37 Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 30 Mar 2016 20:44:09 -0700 Subject: [PATCH] Save audio metadata, after extraction Fixes #260 --- renderer/index.js | 22 ++++++++++++++-------- renderer/lib/location-history.js | 5 +++++ renderer/state.js | 3 +-- renderer/views/player.js | 9 +++++---- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/renderer/index.js b/renderer/index.js index 8caa4175..3c7c6500 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -712,16 +712,21 @@ function startServerFromReadyTorrent (torrent, index, cb) { // update state state.playing.infoHash = torrent.infoHash state.playing.fileIndex = index - state.playing.type = TorrentPlayer.isVideo(file) ? 'video' : 'audio' - state.playing.audioInfo = null + state.playing.type = TorrentPlayer.isVideo(file) ? 'video' + : TorrentPlayer.isAudio(file) ? 'audio' + : 'other' // if it's audio, parse out the metadata (artist, title, etc) - musicmetadata(file.createReadStream(), function (err, info) { - if (err) return - console.log('got audio metadata for %s: %v', file.name, info) - state.playing.audioInfo = info - update() - }) + var torrentSummary = getTorrentSummary(torrent.infoHash) + var fileSummary = torrentSummary.files[index] + if (state.playing.type === 'audio' && !fileSummary.audioInfo) { + musicmetadata(file.createReadStream(), function (err, info) { + if (err) return + console.log('got audio metadata for %s: %o', file.name, info) + fileSummary.audioInfo = info + update() + }) + } // either way, start a streaming torrent-to-http server var server = torrent.createServer() @@ -777,6 +782,7 @@ function openPlayer (infoHash, index, cb) { var timeout = setTimeout(function () { torrentSummary.playStatus = 'timeout' /* no seeders available? */ + state.navigation.clearPending() playInterfaceSound('ERROR') update() }, 10000) /* give it a few seconds */ diff --git a/renderer/lib/location-history.js b/renderer/lib/location-history.js index 91f555f7..c7d38b32 100644 --- a/renderer/lib/location-history.js +++ b/renderer/lib/location-history.js @@ -19,6 +19,7 @@ LocationHistory.prototype._go = function (page) { this._pending = page page.onbeforeload((err) => { if (err) return + if (this._pending !== page) return /* navigation was cancelled */ this._pending = null this._history.push(page) }) @@ -67,3 +68,7 @@ LocationHistory.prototype.hasForward = function () { LocationHistory.prototype.pending = function () { return this._pending } + +LocationHistory.prototype.clearPending = function () { + this._pending = null +} diff --git a/renderer/state.js b/renderer/state.js index eaea0314..b3c70d26 100644 --- a/renderer/state.js +++ b/renderer/state.js @@ -24,7 +24,7 @@ module.exports = { infoHash: null, /* the info hash of the torrent we're playing */ fileIndex: null, /* the zero-based index within the torrent */ location: 'local', /* 'local', 'chromecast', 'airplay' */ - type: null, /* 'audio' or 'video' */ + type: null, /* 'audio' or 'video', could be 'other' if ever support eg streaming to VLC */ currentTime: 0, /* seconds */ duration: 1, /* seconds */ isPaused: true, @@ -32,7 +32,6 @@ module.exports = { lastTimeUpdate: 0, /* Unix time in ms */ mouseStationarySince: 0 /* Unix time in ms */ }, - audioInfo: null, /* set whenever an audio file is playing */ pendingTorrents: {}, /* infohash to WebTorrent handle */ devices: { /* playback devices like Chromecast and AppleTV */ airplay: null, /* airplay client. finds and manages AppleTVs */ diff --git a/renderer/views/player.js b/renderer/views/player.js index ab156412..023ebf58 100644 --- a/renderer/views/player.js +++ b/renderer/views/player.js @@ -122,14 +122,15 @@ function renderOverlay (state) { } function renderAudioMetadata (state) { - if (!state.playing.audioInfo) return - var info = state.playing.audioInfo + var torrentSummary = getPlayingTorrentSummary(state) + var fileSummary = torrentSummary.files[state.playing.fileIndex] + if (!fileSummary.audioInfo) return + var info = fileSummary.audioInfo // Get audio track info var title = info.title if (!title) { - var torrentSummary = getPlayingTorrentSummary(state) - title = torrentSummary.files[state.playing.fileIndex].name + title = fileSummary.name } var artist = info.artist && info.artist[0] var album = info.album