New model update audio player, stream, collections

This commit is contained in:
advplyr 2022-03-12 19:59:35 -06:00
parent 2d19208340
commit 65df377a49
20 changed files with 145 additions and 145 deletions

View File

@ -118,7 +118,7 @@ export default {
return this.book.numTracks return this.book.numTracks
}, },
isStreaming() { isStreaming() {
return this.$store.getters['getAudiobookIdStreaming'] === this.audiobookId return this.$store.getters['getLibraryItemIdStreaming'] === this.audiobookId
}, },
showReadButton() { showReadButton() {
return this.showExperimentalFeatures && this.numEbooks return this.showExperimentalFeatures && this.numEbooks
@ -168,7 +168,7 @@ export default {
}) })
}, },
startStream() { startStream() {
this.$eventBus.$emit('play-audiobook', this.book.id) this.$eventBus.$emit('play-item', this.book.id)
}, },
editClick() { editClick() {
this.$emit('edit', this.book) this.$emit('edit', this.book)

View File

@ -1,17 +1,17 @@
<template> <template>
<div v-if="streamAudiobook" id="streamContainer" class="w-full fixed bottom-0 left-0 right-0 h-48 sm:h-44 md:h-40 z-40 bg-primary px-4 pb-1 md:pb-4 pt-2"> <div v-if="streamLibraryItem" id="streamContainer" class="w-full fixed bottom-0 left-0 right-0 h-48 sm:h-44 md:h-40 z-40 bg-primary px-4 pb-1 md:pb-4 pt-2">
<nuxt-link :to="`/audiobook/${streamAudiobook.id}`" class="absolute left-4 cursor-pointer" :style="{ top: bookCoverPosTop + 'px' }"> <nuxt-link :to="`/item/${streamLibraryItem.id}`" class="absolute left-4 cursor-pointer" :style="{ top: bookCoverPosTop + 'px' }">
<covers-book-cover :audiobook="streamAudiobook" :width="bookCoverWidth" :book-cover-aspect-ratio="bookCoverAspectRatio" /> <covers-book-cover :library-item="streamLibraryItem" :width="bookCoverWidth" :book-cover-aspect-ratio="bookCoverAspectRatio" />
</nuxt-link> </nuxt-link>
<div class="flex items-start pl-24 mb-6 md:mb-0"> <div class="flex items-start pl-24 mb-6 md:mb-0">
<div> <div>
<nuxt-link :to="`/audiobook/${streamAudiobook.id}`" class="hover:underline cursor-pointer text-base sm:text-lg"> <nuxt-link :to="`/item/${streamLibraryItem.id}`" class="hover:underline cursor-pointer text-base sm:text-lg">
{{ title }} {{ title }}
</nuxt-link> </nuxt-link>
<div class="text-gray-400 flex items-center"> <div class="text-gray-400 flex items-center">
<span class="material-icons text-sm">person</span> <span class="material-icons text-sm">person</span>
<p v-if="authorFL" class="pl-1.5 text-sm sm:text-base"> <p v-if="authors.length" class="pl-1.5 text-sm sm:text-base">
<nuxt-link v-for="(author, index) in authorsList" :key="index" :to="`/library/${libraryId}/bookshelf?filter=authors.${$encode(author)}`" class="hover:underline">{{ author }}<span v-if="index < authorsList.length - 1">,&nbsp;</span></nuxt-link> <nuxt-link v-for="(author, index) in authors" :key="index" :to="`/library/${libraryId}/bookshelf?filter=authors.${$encode(author.id)}`" class="hover:underline">{{ author.name }}<span v-if="index < authors.length - 1">,&nbsp;</span></nuxt-link>
</p> </p>
<p v-else class="text-sm sm:text-base cursor-pointer pl-2">Unknown</p> <p v-else class="text-sm sm:text-base cursor-pointer pl-2">Unknown</p>
</div> </div>
@ -89,15 +89,15 @@ export default {
return -64 return -64
}, },
cover() { cover() {
if (this.streamAudiobook && this.streamAudiobook.cover) return this.streamAudiobook.cover if (this.media.coverPath) return this.media.coverPath
return 'Logo.png' return 'Logo.png'
}, },
user() { user() {
return this.$store.state.user.user return this.$store.state.user.user
}, },
userAudiobook() { userAudiobook() {
if (!this.audiobookId) return if (!this.libraryItemId) return
return this.$store.getters['user/getUserAudiobook'](this.audiobookId) return this.$store.getters['user/getUserAudiobook'](this.libraryItemId)
}, },
userAudiobookCurrentTime() { userAudiobookCurrentTime() {
return this.userAudiobook ? this.userAudiobook.currentTime || 0 : 0 return this.userAudiobook ? this.userAudiobook.currentTime || 0 : 0
@ -106,32 +106,29 @@ export default {
if (!this.userAudiobook) return [] if (!this.userAudiobook) return []
return (this.userAudiobook.bookmarks || []).map((bm) => ({ ...bm })).sort((a, b) => a.time - b.time) return (this.userAudiobook.bookmarks || []).map((bm) => ({ ...bm })).sort((a, b) => a.time - b.time)
}, },
streamAudiobook() { streamLibraryItem() {
return this.$store.state.streamAudiobook return this.$store.state.streamLibraryItem
}, },
audiobookId() { libraryItemId() {
return this.streamAudiobook ? this.streamAudiobook.id : null return this.streamLibraryItem ? this.streamLibraryItem.id : null
}, },
book() { media() {
return this.streamAudiobook ? this.streamAudiobook.book || {} : {} return this.streamLibraryItem ? this.streamLibraryItem.media || {} : {}
},
mediaMetadata() {
return this.media.metadata || {}
}, },
chapters() { chapters() {
return this.streamAudiobook ? this.streamAudiobook.chapters || [] : [] return this.media.chapters || []
}, },
title() { title() {
return this.book.title || 'No Title' return this.mediaMetadata.title || 'No Title'
}, },
author() { authors() {
return this.book.author || 'Unknown' return this.mediaMetadata.authors || []
},
authorFL() {
return this.book.authorFL
},
authorsList() {
return this.authorFL ? this.authorFL.split(', ') : []
}, },
libraryId() { libraryId() {
return this.streamAudiobook ? this.streamAudiobook.libraryId : null return this.streamLibraryItem ? this.streamLibraryItem.libraryId : null
}, },
totalDurationPretty() { totalDurationPretty() {
return this.$secondsToTimestamp(this.totalDuration) return this.$secondsToTimestamp(this.totalDuration)
@ -217,7 +214,7 @@ export default {
} }
}, },
showBookmarks() { showBookmarks() {
this.bookmarkAudiobookId = this.audiobookId this.bookmarkAudiobookId = this.libraryItemId
this.bookmarkCurrentTime = this.currentTime this.bookmarkCurrentTime = this.currentTime
this.showBookmarksModal = true this.showBookmarksModal = true
}, },
@ -227,7 +224,7 @@ export default {
}, },
closePlayer() { closePlayer() {
this.playerHandler.closePlayer() this.playerHandler.closePlayer()
this.$store.commit('setStreamAudiobook', null) this.$store.commit('setLibraryItemStream', null)
}, },
streamProgress(data) { streamProgress(data) {
if (!data.numSegments) return if (!data.numSegments) return
@ -240,12 +237,12 @@ export default {
} }
}, },
streamOpen(stream) { streamOpen(stream) {
this.$store.commit('setStreamAudiobook', stream.audiobook) this.$store.commit('setLibraryItemStream', stream.libraryItem)
this.playerHandler.prepareStream(stream) this.playerHandler.prepareStream(stream)
}, },
streamClosed(streamId) { streamClosed(streamId) {
// Stream was closed from the server // Stream was closed from the server
if (this.playerHandler.isPlayingLocalAudiobook && this.playerHandler.currentStreamId === streamId) { if (this.playerHandler.isPlayingLocalItem && this.playerHandler.currentStreamId === streamId) {
console.warn('[StreamContainer] Closing stream due to request from server') console.warn('[StreamContainer] Closing stream due to request from server')
this.playerHandler.closePlayer() this.playerHandler.closePlayer()
} }
@ -260,7 +257,7 @@ export default {
}, },
streamError(streamId) { streamError(streamId) {
// Stream had critical error from the server // Stream had critical error from the server
if (this.playerHandler.isPlayingLocalAudiobook && this.playerHandler.currentStreamId === streamId) { if (this.playerHandler.isPlayingLocalItem && this.playerHandler.currentStreamId === streamId) {
console.warn('[StreamContainer] Closing stream due to stream error from server') console.warn('[StreamContainer] Closing stream due to stream error from server')
this.playerHandler.closePlayer() this.playerHandler.closePlayer()
} }
@ -269,32 +266,32 @@ export default {
this.playerHandler.resetStream(startTime, streamId) this.playerHandler.resetStream(startTime, streamId)
}, },
castSessionActive(isActive) { castSessionActive(isActive) {
if (isActive && this.playerHandler.isPlayingLocalAudiobook) { if (isActive && this.playerHandler.isPlayingLocalItem) {
// Cast session started switch to cast player // Cast session started switch to cast player
this.playerHandler.switchPlayer() this.playerHandler.switchPlayer()
} else if (!isActive && this.playerHandler.isPlayingCastedAudiobook) { } else if (!isActive && this.playerHandler.isPlayingCastedItem) {
// Cast session ended switch to local player // Cast session ended switch to local player
this.playerHandler.switchPlayer() this.playerHandler.switchPlayer()
} }
}, },
async playAudiobook(audiobookId) { async playLibraryItem(libraryItemId) {
var audiobook = await this.$axios.$get(`/api/books/${audiobookId}`).catch((error) => { var libraryItem = await this.$axios.$get(`/api/items/${libraryItemId}?expanded=1`).catch((error) => {
console.error('Failed to fetch full audiobook', error) console.error('Failed to fetch full item', error)
return null return null
}) })
if (!audiobook) return if (!libraryItem) return
this.$store.commit('setStreamAudiobook', audiobook) this.$store.commit('setLibraryItemStream', libraryItem)
this.playerHandler.load(audiobook, true, this.userAudiobookCurrentTime) this.playerHandler.load(libraryItem, true, this.userAudiobookCurrentTime)
} }
}, },
mounted() { mounted() {
this.$eventBus.$on('cast-session-active', this.castSessionActive) this.$eventBus.$on('cast-session-active', this.castSessionActive)
this.$eventBus.$on('play-audiobook', this.playAudiobook) this.$eventBus.$on('play-item', this.playLibraryItem)
}, },
beforeDestroy() { beforeDestroy() {
this.$eventBus.$off('cast-session-active', this.castSessionActive) this.$eventBus.$off('cast-session-active', this.castSessionActive)
this.$eventBus.$off('play-audiobook', this.playAudiobook) this.$eventBus.$off('play-item', this.playLibraryItem)
} }
} }
</script> </script>

View File

@ -512,7 +512,7 @@ export default {
}, },
play() { play() {
var eventBus = this.$eventBus || this.$nuxt.$eventBus var eventBus = this.$eventBus || this.$nuxt.$eventBus
eventBus.$emit('play-audiobook', this.libraryItemId) eventBus.$emit('play-item', this.libraryItemId)
}, },
mouseover() { mouseover() {
this.isHovering = true this.isHovering = true

View File

@ -26,8 +26,8 @@
</td> </td>
<td class="text-sm">{{ user.type }}</td> <td class="text-sm">{{ user.type }}</td>
<td class="hidden lg:table-cell"> <td class="hidden lg:table-cell">
<div v-if="usersOnline[user.id] && usersOnline[user.id].stream && usersOnline[user.id].stream.audiobook && usersOnline[user.id].stream.audiobook.book"> <div v-if="usersOnline[user.id] && usersOnline[user.id].stream && usersOnline[user.id].stream.libraryItem && usersOnline[user.id].stream.libraryItem.media">
<p class="truncate text-xs">Reading: {{ usersOnline[user.id].stream.audiobook.book.title || '' }}</p> <p class="truncate text-xs">Reading: {{ usersOnline[user.id].stream.libraryItem.media.metadata.title || '' }}</p>
</div> </div>
<div v-else-if="user.audiobooks && getLastRead(user.audiobooks)"> <div v-else-if="user.audiobooks && getLastRead(user.audiobooks)">
<p class="truncate text-xs">Last: {{ getLastRead(user.audiobooks) }}</p> <p class="truncate text-xs">Last: {{ getLastRead(user.audiobooks) }}</p>

View File

@ -108,7 +108,7 @@ export default {
return this.media.tracks.length return this.media.tracks.length
}, },
isStreaming() { isStreaming() {
return this.$store.getters['getAudiobookIdStreaming'] === this.book.id return this.$store.getters['getLibraryItemIdStreaming'] === this.book.id
}, },
showPlayBtn() { showPlayBtn() {
return !this.isMissing && !this.isInvalid && !this.isStreaming && this.numTracks return !this.isMissing && !this.isInvalid && !this.isStreaming && this.numTracks
@ -136,7 +136,7 @@ export default {
this.isHovering = false this.isHovering = false
}, },
playClick() { playClick() {
this.$eventBus.$emit('play-audiobook', this.book.id) this.$eventBus.$emit('play-item', this.book.id)
}, },
clickEdit() { clickEdit() {
this.$emit('edit', this.book) this.$emit('edit', this.book)

View File

@ -167,19 +167,6 @@ export default {
this.$toast.error(`Stream Failed: ${errorMessage}`) this.$toast.error(`Stream Failed: ${errorMessage}`)
if (this.$refs.streamContainer) this.$refs.streamContainer.streamError(id) if (this.$refs.streamContainer) this.$refs.streamContainer.streamError(id)
}, },
audiobookAdded(audiobook) {
this.$store.commit('libraries/updateFilterDataWithAudiobook', audiobook)
},
audiobooksAdded(audiobooks) {
audiobooks.forEach((ab) => {
this.audiobookAdded(ab)
})
},
audiobooksUpdated(audiobooks) {
audiobooks.forEach((ab) => {
this.audiobookUpdated(ab)
})
},
libraryAdded(library) { libraryAdded(library) {
this.$store.commit('libraries/addUpdate', library) this.$store.commit('libraries/addUpdate', library)
}, },
@ -189,6 +176,9 @@ export default {
libraryRemoved(library) { libraryRemoved(library) {
this.$store.commit('libraries/remove', library) this.$store.commit('libraries/remove', library)
}, },
libraryItemAdded(libraryItem) {
// this.$store.commit('libraries/updateFilterDataWithAudiobook', libraryItem)
},
libraryItemUpdated(libraryItem) { libraryItemUpdated(libraryItem) {
if (this.$store.state.selectedLibraryItem && this.$store.state.selectedLibraryItem.id === libraryItem.id) { if (this.$store.state.selectedLibraryItem && this.$store.state.selectedLibraryItem.id === libraryItem.id) {
this.$store.commit('setSelectedLibraryItem', libraryItem) this.$store.commit('setSelectedLibraryItem', libraryItem)
@ -202,6 +192,16 @@ export default {
} }
} }
}, },
libraryItemsUpdated(libraryItems) {
libraryItems.forEach((li) => {
this.libraryItemUpdated(li)
})
},
libraryItemsAdded(libraryItems) {
libraryItems.forEach((ab) => {
this.libraryItemAdded(ab)
})
},
scanComplete(data) { scanComplete(data) {
console.log('Scan complete received', data) console.log('Scan complete received', data)
@ -376,21 +376,17 @@ export default {
this.socket.on('stream_reset', this.streamReset) this.socket.on('stream_reset', this.streamReset)
this.socket.on('stream_error', this.streamError) this.socket.on('stream_error', this.streamError)
// Audiobook Listeners
this.socket.on('audiobook_updated', this.audiobookUpdated)
this.socket.on('audiobook_added', this.audiobookAdded)
this.socket.on('audiobook_removed', this.audiobookRemoved)
this.socket.on('audiobooks_updated', this.audiobooksUpdated)
this.socket.on('audiobooks_added', this.audiobooksAdded)
// Library Listeners // Library Listeners
this.socket.on('library_updated', this.libraryUpdated) this.socket.on('library_updated', this.libraryUpdated)
this.socket.on('library_added', this.libraryAdded) this.socket.on('library_added', this.libraryAdded)
this.socket.on('library_removed', this.libraryRemoved) this.socket.on('library_removed', this.libraryRemoved)
// Library Item Listeners // Library Item Listeners
this.socket.on('item_added', this.libraryItemAdded)
this.socket.on('item_updated', this.libraryItemUpdated) this.socket.on('item_updated', this.libraryItemUpdated)
this.socket.on('item_removed', this.libraryItemRemoved) this.socket.on('item_removed', this.libraryItemRemoved)
this.socket.on('items_updated', this.libraryItemsUpdated)
this.socket.on('items_added', this.libraryItemsAdded)
// User Listeners // User Listeners
this.socket.on('user_updated', this.userUpdated) this.socket.on('user_updated', this.userUpdated)

View File

@ -437,7 +437,7 @@ export default {
}) })
}, },
startStream() { startStream() {
this.$eventBus.$emit('play-audiobook', this.audiobook.id) this.$eventBus.$emit('play-item', this.audiobook.id)
}, },
editClick() { editClick() {
this.$store.commit('setBookshelfBookIds', []) this.$store.commit('setBookshelfBookIds', [])

View File

@ -87,7 +87,7 @@ export default {
}) })
}, },
streaming() { streaming() {
return !!this.playableBooks.find((b) => b.id === this.$store.getters['getAudiobookIdStreaming']) return !!this.playableBooks.find((b) => b.id === this.$store.getters['getLibraryItemIdStreaming'])
}, },
showPlayButton() { showPlayButton() {
return this.playableBooks.length return this.playableBooks.length
@ -120,7 +120,7 @@ export default {
clickPlay() { clickPlay() {
var nextBookNotRead = this.playableBooks.find((pb) => !this.userAudiobooks[pb.id] || !this.userAudiobooks[pb.id].isRead) var nextBookNotRead = this.playableBooks.find((pb) => !this.userAudiobooks[pb.id] || !this.userAudiobooks[pb.id].isRead)
if (nextBookNotRead) { if (nextBookNotRead) {
this.$eventBus.$emit('play-audiobook', nextBookNotRead.id) this.$eventBus.$emit('play-item', nextBookNotRead.id)
} }
} }
}, },

View File

@ -418,7 +418,7 @@ export default {
}) })
}, },
startStream() { startStream() {
this.$eventBus.$emit('play-audiobook', this.libraryItem.id) this.$eventBus.$emit('play-item', this.libraryItem.id)
}, },
editClick() { editClick() {
this.$store.commit('setBookshelfBookIds', []) this.$store.commit('setBookshelfBookIds', [])

View File

@ -3,7 +3,7 @@ export default class AudioTrack {
this.index = track.index || 0 this.index = track.index || 0
this.startOffset = track.startOffset || 0 // Total time of all previous tracks this.startOffset = track.startOffset || 0 // Total time of all previous tracks
this.duration = track.duration || 0 this.duration = track.duration || 0
this.title = track.filename || '' this.title = track.metadata ? track.metadata.filename || '' : ''
this.contentUrl = track.contentUrl || null this.contentUrl = track.contentUrl || null
this.mimeType = track.mimeType this.mimeType = track.mimeType
} }

View File

@ -8,7 +8,7 @@ export default class LocalPlayer extends EventEmitter {
this.ctx = ctx this.ctx = ctx
this.player = null this.player = null
this.audiobook = null this.libraryItem = null
this.audioTracks = [] this.audioTracks = []
this.currentTrackIndex = 0 this.currentTrackIndex = 0
this.hlsStreamId = null this.hlsStreamId = null
@ -110,8 +110,8 @@ export default class LocalPlayer extends EventEmitter {
} }
} }
set(audiobook, tracks, hlsStreamId, startTime, playWhenReady = false) { set(libraryItem, tracks, hlsStreamId, startTime, playWhenReady = false) {
this.audiobook = audiobook this.libraryItem = libraryItem
this.audioTracks = tracks this.audioTracks = tracks
this.hlsStreamId = hlsStreamId this.hlsStreamId = hlsStreamId
this.playWhenReady = playWhenReady this.playWhenReady = playWhenReady
@ -198,7 +198,7 @@ export default class LocalPlayer extends EventEmitter {
async resetStream(startTime) { async resetStream(startTime) {
this.destroyHlsInstance() this.destroyHlsInstance()
await new Promise((resolve) => setTimeout(resolve, 1000)) await new Promise((resolve) => setTimeout(resolve, 1000))
this.set(this.audiobook, this.audioTracks, this.hlsStreamId, startTime, true) this.set(this.libraryItem, this.audioTracks, this.hlsStreamId, startTime, true)
} }
playPause() { playPause() {

View File

@ -5,7 +5,7 @@ import AudioTrack from './AudioTrack'
export default class PlayerHandler { export default class PlayerHandler {
constructor(ctx) { constructor(ctx) {
this.ctx = ctx this.ctx = ctx
this.audiobook = null this.libraryItem = null
this.playWhenReady = false this.playWhenReady = false
this.player = null this.player = null
this.playerState = 'IDLE' this.playerState = 'IDLE'
@ -22,11 +22,11 @@ export default class PlayerHandler {
get isCasting() { get isCasting() {
return this.ctx.$store.state.globals.isCasting return this.ctx.$store.state.globals.isCasting
} }
get isPlayingCastedAudiobook() { get isPlayingCastedItem() {
return this.audiobook && (this.player instanceof CastPlayer) return this.libraryItem && (this.player instanceof CastPlayer)
} }
get isPlayingLocalAudiobook() { get isPlayingLocalItem() {
return this.audiobook && (this.player instanceof LocalPlayer) return this.libraryItem && (this.player instanceof LocalPlayer)
} }
get userToken() { get userToken() {
return this.ctx.$store.getters['user/getToken'] return this.ctx.$store.getters['user/getToken']
@ -35,10 +35,10 @@ export default class PlayerHandler {
return this.playerState === 'PLAYING' return this.playerState === 'PLAYING'
} }
load(audiobook, playWhenReady, startTime = 0) { load(libraryItem, playWhenReady, startTime = 0) {
if (!this.player) this.switchPlayer() if (!this.player) this.switchPlayer()
this.audiobook = audiobook this.libraryItem = libraryItem
this.startTime = startTime this.startTime = startTime
this.playWhenReady = playWhenReady this.playWhenReady = playWhenReady
this.prepare() this.prepare()
@ -58,8 +58,8 @@ export default class PlayerHandler {
this.player = new CastPlayer(this.ctx) this.player = new CastPlayer(this.ctx)
this.setPlayerListeners() this.setPlayerListeners()
if (this.audiobook) { if (this.libraryItem) {
// Audiobook was already loaded - prepare for cast // libraryItem was already loaded - prepare for cast
this.playWhenReady = false this.playWhenReady = false
this.prepare() this.prepare()
} }
@ -75,8 +75,8 @@ export default class PlayerHandler {
this.player = new LocalPlayer(this.ctx) this.player = new LocalPlayer(this.ctx)
this.setPlayerListeners() this.setPlayerListeners()
if (this.audiobook) { if (this.libraryItem) {
// Audiobook was already loaded - prepare for local play // libraryItem was already loaded - prepare for local play
this.playWhenReady = false this.playWhenReady = false
this.prepare() this.prepare()
} }
@ -129,10 +129,15 @@ export default class PlayerHandler {
var useHls = false var useHls = false
var runningTotal = 0 var runningTotal = 0
var audioTracks = (this.audiobook.tracks || []).map((track) => {
var audioTracks = (this.libraryItem.media.tracks || []).map((track) => {
if (!track.metadata) {
console.error('INVALID TRACK', track)
return null
}
var audioTrack = new AudioTrack(track) var audioTrack = new AudioTrack(track)
audioTrack.startOffset = runningTotal audioTrack.startOffset = runningTotal
audioTrack.contentUrl = `/lib/${this.audiobook.libraryId}/${this.audiobook.folderId}/${this.ctx.$encodeUriPath(track.path)}?token=${this.userToken}` audioTrack.contentUrl = `/s/item/${this.libraryItem.id}/${this.ctx.$encodeUriPath(track.metadata.relPath.replace(/^\//, ''))}?token=${this.userToken}`
audioTrack.mimeType = this.getMimeTypeForTrack(track) audioTrack.mimeType = this.getMimeTypeForTrack(track)
audioTrack.canDirectPlay = !!this.player.playableMimetypes[audioTrack.mimeType] audioTrack.canDirectPlay = !!this.player.playableMimetypes[audioTrack.mimeType]
@ -156,7 +161,7 @@ export default class PlayerHandler {
if (useHls) { if (useHls) {
var stream = await this.ctx.$axios.$get(`/api/books/${this.audiobook.id}/stream`).catch((error) => { var stream = await this.ctx.$axios.$get(`/api/items/${this.libraryItem.id}/stream`).catch((error) => {
console.error('Failed to start stream', error) console.error('Failed to start stream', error)
}) })
if (stream) { if (stream) {
@ -171,7 +176,7 @@ export default class PlayerHandler {
} }
getMimeTypeForTrack(track) { getMimeTypeForTrack(track) {
var ext = track.ext var ext = track.metadata.ext
if (ext === '.mp3' || ext === '.m4b' || ext === '.m4a') { if (ext === '.mp3' || ext === '.m4b' || ext === '.m4a') {
return 'audio/mpeg' return 'audio/mpeg'
} else if (ext === '.mp4') { } else if (ext === '.mp4') {
@ -187,13 +192,13 @@ export default class PlayerHandler {
} }
closePlayer() { closePlayer() {
console.log('[PlayerHandler] CLose Player') console.log('[PlayerHandler] Close Player')
if (this.player) { if (this.player) {
this.player.destroy() this.player.destroy()
} }
this.player = null this.player = null
this.playerState = 'IDLE' this.playerState = 'IDLE'
this.audiobook = null this.libraryItem = null
this.currentStreamId = null this.currentStreamId = null
this.startTime = 0 this.startTime = 0
this.stopPlayInterval() this.stopPlayInterval()
@ -201,7 +206,7 @@ export default class PlayerHandler {
prepareStream(stream) { prepareStream(stream) {
if (!this.player) this.switchPlayer() if (!this.player) this.switchPlayer()
this.audiobook = stream.audiobook this.libraryItem = stream.libraryItem
this.setHlsStream({ this.setHlsStream({
streamId: stream.id, streamId: stream.id,
streamUrl: stream.clientPlaylistUri, streamUrl: stream.clientPlaylistUri,
@ -212,19 +217,19 @@ export default class PlayerHandler {
setHlsStream(stream) { setHlsStream(stream) {
this.currentStreamId = stream.streamId this.currentStreamId = stream.streamId
var audioTrack = new AudioTrack({ var audioTrack = new AudioTrack({
duration: this.audiobook.duration, duration: this.libraryItem.media.duration,
contentUrl: stream.streamUrl + '?token=' + this.userToken, contentUrl: stream.streamUrl + '?token=' + this.userToken,
mimeType: 'application/vnd.apple.mpegurl' mimeType: 'application/vnd.apple.mpegurl'
}) })
this.startTime = stream.startTime this.startTime = stream.startTime
this.ctx.playerLoading = true this.ctx.playerLoading = true
this.player.set(this.audiobook, [audioTrack], this.currentStreamId, stream.startTime, this.playWhenReady) this.player.set(this.libraryItem, [audioTrack], this.currentStreamId, stream.startTime, this.playWhenReady)
} }
setDirectPlay(audioTracks) { setDirectPlay(audioTracks) {
this.currentStreamId = null this.currentStreamId = null
this.ctx.playerLoading = true this.ctx.playerLoading = true
this.player.set(this.audiobook, audioTracks, null, this.startTime, this.playWhenReady) this.player.set(this.libraryItem, audioTracks, null, this.startTime, this.playWhenReady)
} }
resetStream(startTime, streamId) { resetStream(startTime, streamId) {
@ -265,21 +270,21 @@ export default class PlayerHandler {
timeListened: listeningTimeToAdd, timeListened: listeningTimeToAdd,
currentTime, currentTime,
streamId: this.currentStreamId, streamId: this.currentStreamId,
audiobookId: this.audiobook.id audiobookId: this.libraryItem.id
} }
this.ctx.$axios.$post('/api/syncStream', syncData, { timeout: 1000 }).catch((error) => { this.ctx.$axios.$post('/api/syncStream', syncData, { timeout: 1000 }).catch((error) => {
console.error('Failed to update stream progress', error) console.error('Failed to update stream progress', error)
}) })
} else { } else {
// Direct play via chromecast does not yet have backend stream session model // Direct play via chromecast does not yet have backend stream session model
// so the progress update for the audiobook is updated this way (instead of through the stream) // so the progress update for the libraryItem is updated this way (instead of through the stream)
var duration = this.getDuration() var duration = this.getDuration()
var syncData = { var syncData = {
totalDuration: duration, totalDuration: duration,
currentTime, currentTime,
progress: duration > 0 ? currentTime / duration : 0, progress: duration > 0 ? currentTime / duration : 0,
isRead: false, isRead: false,
audiobookId: this.audiobook.id, audiobookId: this.libraryItem.id,
lastUpdate: Date.now() lastUpdate: Date.now()
} }
this.ctx.$axios.$post('/api/syncLocal', syncData, { timeout: 1000 }).catch((error) => { this.ctx.$axios.$post('/api/syncLocal', syncData, { timeout: 1000 }).catch((error) => {

View File

@ -4,7 +4,7 @@ import Vue from 'vue'
export const state = () => ({ export const state = () => ({
versionData: null, versionData: null,
serverSettings: null, serverSettings: null,
streamAudiobook: null, streamLibraryItem: null,
editModalTab: 'details', editModalTab: 'details',
showEditModal: false, showEditModal: false,
showEReader: false, showEReader: false,
@ -37,8 +37,8 @@ export const getters = {
return state.serverSettings.coverAspectRatio === 0 ? 1.6 : 1 return state.serverSettings.coverAspectRatio === 0 ? 1.6 : 1
}, },
getNumAudiobooksSelected: state => state.selectedAudiobooks.length, getNumAudiobooksSelected: state => state.selectedAudiobooks.length,
getAudiobookIdStreaming: state => { getLibraryItemIdStreaming: state => {
return state.streamAudiobook ? state.streamAudiobook.id : null return state.streamLibraryItem ? state.streamLibraryItem.id : null
} }
} }
@ -106,8 +106,8 @@ export const mutations = {
if (!settings) return if (!settings) return
state.serverSettings = settings state.serverSettings = settings
}, },
setStreamAudiobook(state, audiobook) { setLibraryItemStream(state, libraryItem) {
state.streamAudiobook = audiobook state.streamLibraryItem = libraryItem
}, },
showEditModal(state, libraryItem) { showEditModal(state, libraryItem) {
state.editModalTab = 'details' state.editModalTab = 'details'

View File

@ -81,6 +81,7 @@ class ApiController {
this.router.post('/items/:id/cover', LibraryItemController.middleware.bind(this), LibraryItemController.uploadCover.bind(this)) this.router.post('/items/:id/cover', LibraryItemController.middleware.bind(this), LibraryItemController.uploadCover.bind(this))
this.router.patch('/items/:id/cover', LibraryItemController.middleware.bind(this), LibraryItemController.updateCover.bind(this)) this.router.patch('/items/:id/cover', LibraryItemController.middleware.bind(this), LibraryItemController.updateCover.bind(this))
this.router.delete('/items/:id/cover', LibraryItemController.middleware.bind(this), LibraryItemController.removeCover.bind(this)) this.router.delete('/items/:id/cover', LibraryItemController.middleware.bind(this), LibraryItemController.removeCover.bind(this))
this.router.get('/items/:id/stream', LibraryItemController.middleware.bind(this), LibraryItemController.openStream.bind(this))
// //
// Book Routes // Book Routes

View File

@ -15,10 +15,6 @@ class StreamManager {
this.StreamsPath = Path.join(global.MetadataPath, 'streams') this.StreamsPath = Path.join(global.MetadataPath, 'streams')
} }
get audiobooks() {
return this.db.audiobooks
}
getStream(streamId) { getStream(streamId) {
return this.streams.find(s => s.id === streamId) return this.streams.find(s => s.id === streamId)
} }
@ -27,12 +23,12 @@ class StreamManager {
this.streams = this.streams.filter(s => s.id !== stream.id) this.streams = this.streams.filter(s => s.id !== stream.id)
} }
async openStream(client, audiobook, transcodeOptions = {}) { async openStream(client, libraryItem, transcodeOptions = {}) {
if (!client || !client.user) { if (!client || !client.user) {
Logger.error('[StreamManager] Cannot open stream invalid client', client) Logger.error('[StreamManager] Cannot open stream invalid client', client)
return return
} }
var stream = new Stream(this.StreamsPath, client, audiobook, transcodeOptions) var stream = new Stream(this.StreamsPath, client, libraryItem, transcodeOptions)
stream.on('closed', () => { stream.on('closed', () => {
this.removeStream(stream) this.removeStream(stream)
@ -43,7 +39,7 @@ class StreamManager {
await stream.generatePlaylist() await stream.generatePlaylist()
stream.start() stream.start()
Logger.info('Stream Opened for client', client.user.username, 'for audiobook', audiobook.title, 'with streamId', stream.id) Logger.info('Stream Opened for client', client.user.username, 'for item', stream.itemTitle, 'with streamId', stream.id)
client.stream = stream client.stream = stream
client.user.stream = stream.id client.user.stream = stream.id
@ -103,25 +99,25 @@ class StreamManager {
} }
} }
async openStreamApiRequest(res, user, audiobook) { async openStreamApiRequest(res, user, libraryItem) {
Logger.info(`[StreamManager] User "${user.username}" open stream request for "${audiobook.title}"`) Logger.info(`[StreamManager] User "${user.username}" open stream request for "${libraryItem.media.metadata.title}"`)
var client = { var client = {
user user
} }
var stream = await this.openStream(client, audiobook) var stream = await this.openStream(client, libraryItem)
this.db.updateUserStream(client.user.id, stream.id) this.db.updateUserStream(client.user.id, stream.id)
res.json({ res.json({
audiobookId: audiobook.id, libraryItemId: libraryItem.id,
startTime: stream.startTime, startTime: stream.startTime,
streamId: stream.id, streamId: stream.id,
streamUrl: stream.clientPlaylistUri streamUrl: stream.clientPlaylistUri
}) })
} }
async openStreamSocketRequest(socket, audiobookId) { async openStreamSocketRequest(socket, libraryItemId) {
Logger.info('[StreamManager] Open Stream Request', socket.id, audiobookId) Logger.info('[StreamManager] Open Stream Request', socket.id, libraryItemId)
var audiobook = this.audiobooks.find(ab => ab.id === audiobookId) var libraryItem = this.db.libraryItems.find(li => li.id === libraryItemId)
var client = socket.sheepClient var client = socket.sheepClient
if (client.stream) { if (client.stream) {
@ -131,7 +127,7 @@ class StreamManager {
client.stream = null client.stream = null
} }
var stream = await this.openStream(client, audiobook) var stream = await this.openStream(client, libraryItem)
this.db.updateUserStream(client.user.id, stream.id) this.db.updateUserStream(client.user.id, stream.id)
this.emitter('user_stream_update', client.user.toJSONForPublic(this.streams)) this.emitter('user_stream_update', client.user.toJSONForPublic(this.streams))

View File

@ -177,6 +177,11 @@ class LibraryItemController {
return this.cacheManager.handleCoverCache(res, libraryItem, options) return this.cacheManager.handleCoverCache(res, libraryItem, options)
} }
// GET: api/items/:id/stream
openStream(req, res) {
this.streamManager.openStreamApiRequest(res, req.user, req.libraryItem)
}
middleware(req, res, next) { middleware(req, res, next) {
var item = this.db.libraryItems.find(li => li.id === req.params.id) var item = this.db.libraryItems.find(li => li.id === req.params.id)
if (!item || !item.media) return res.sendStatus(404) if (!item || !item.media) return res.sendStatus(404)

View File

@ -10,12 +10,12 @@ const hlsPlaylistGenerator = require('../utils/hlsPlaylistGenerator')
const UserListeningSession = require('./UserListeningSession') const UserListeningSession = require('./UserListeningSession')
class Stream extends EventEmitter { class Stream extends EventEmitter {
constructor(streamPath, client, audiobook, transcodeOptions = {}) { constructor(streamPath, client, libraryItem, transcodeOptions = {}) {
super() super()
this.id = getId('str') this.id = getId('str')
this.client = client this.client = client
this.audiobook = audiobook this.libraryItem = libraryItem
this.transcodeOptions = transcodeOptions this.transcodeOptions = transcodeOptions
@ -37,7 +37,7 @@ class Stream extends EventEmitter {
this.clientCurrentTime = 0 this.clientCurrentTime = 0
this.listeningSession = new UserListeningSession() this.listeningSession = new UserListeningSession()
this.listeningSession.setData(audiobook, client.user) this.listeningSession.setData(libraryItem, client.user)
this.init() this.init()
} }
@ -46,21 +46,21 @@ class Stream extends EventEmitter {
return this.client ? this.client.socket || null : null return this.client ? this.client.socket || null : null
} }
get audiobookId() { get libraryItemId() {
return this.audiobook.id return this.libraryItem.id
} }
get audiobookTitle() { get itemTitle() {
return this.audiobook ? this.audiobook.title : null return this.libraryItem ? this.libraryItem.media.metadata.title : null
} }
get totalDuration() { get totalDuration() {
return this.audiobook.duration return this.libraryItem.media.duration
} }
get tracksAudioFileType() { get tracksAudioFileType() {
if (!this.tracks.length) return null if (!this.tracks.length) return null
return this.tracks[0].ext.toLowerCase().slice(1) return this.tracks[0].metadata.ext.toLowerCase().slice(1)
} }
// Fmp4 does not work on iOS devices: https://github.com/advplyr/audiobookshelf-app/issues/85 // Fmp4 does not work on iOS devices: https://github.com/advplyr/audiobookshelf-app/issues/85
@ -90,7 +90,7 @@ class Stream extends EventEmitter {
} }
get tracks() { get tracks() {
return this.audiobook.tracks return this.libraryItem.media.tracks
} }
get clientUser() { get clientUser() {
@ -106,7 +106,7 @@ class Stream extends EventEmitter {
} }
get clientUserAudiobookData() { get clientUserAudiobookData() {
return this.client ? this.clientUserAudiobooks[this.audiobookId] : null return this.client ? this.clientUserAudiobooks[this.libraryItemId] : null
} }
get clientPlaylistUri() { get clientPlaylistUri() {
@ -132,7 +132,7 @@ class Stream extends EventEmitter {
id: this.id, id: this.id,
clientId: this.client.id, clientId: this.client.id,
userId: this.client.user.id, userId: this.client.user.id,
audiobook: this.audiobook.toJSONExpanded(), libraryItem: this.libraryItem.toJSONExpanded(),
segmentLength: this.segmentLength, segmentLength: this.segmentLength,
playlistPath: this.playlistPath, playlistPath: this.playlistPath,
clientPlaylistUri: this.clientPlaylistUri, clientPlaylistUri: this.clientPlaylistUri,
@ -147,7 +147,7 @@ class Stream extends EventEmitter {
init() { init() {
if (this.clientUserAudiobookData) { if (this.clientUserAudiobookData) {
var timeRemaining = this.totalDuration - this.clientUserAudiobookData.currentTime var timeRemaining = this.totalDuration - this.clientUserAudiobookData.currentTime
Logger.info('[STREAM] User has progress for audiobook', this.clientUserAudiobookData.progress, `Time Remaining: ${timeRemaining}s`) Logger.info('[STREAM] User has progress for item', this.clientUserAudiobookData.progress, `Time Remaining: ${timeRemaining}s`)
if (timeRemaining > 15) { if (timeRemaining > 15) {
this.startTime = this.clientUserAudiobookData.currentTime this.startTime = this.clientUserAudiobookData.currentTime
this.clientCurrentTime = this.startTime this.clientCurrentTime = this.startTime
@ -194,7 +194,7 @@ class Stream extends EventEmitter {
return null return null
} }
this.listeningSession = new UserListeningSession() this.listeningSession = new UserListeningSession()
this.listeningSession.setData(this.audiobook, this.clientUser) this.listeningSession.setData(this.libraryItem, this.clientUser)
Logger.debug(`[Stream] Listening session rolled to next day`) Logger.debug(`[Stream] Listening session rolled to next day`)
} }
@ -284,7 +284,6 @@ class Stream extends EventEmitter {
} }
startLoop() { startLoop() {
// Logger.info(`[Stream] ${this.audiobookTitle} (${this.id}) Start Loop`)
if (this.socket) { if (this.socket) {
this.socket.emit('stream_progress', { stream: this.id, chunks: [], numSegments: 0, percent: '0%' }) this.socket.emit('stream_progress', { stream: this.id, chunks: [], numSegments: 0, percent: '0%' })
} }
@ -295,7 +294,7 @@ class Stream extends EventEmitter {
this.checkFiles() this.checkFiles()
} else { } else {
if (this.socket) { if (this.socket) {
Logger.info(`[Stream] ${this.audiobookTitle} sending stream_ready`) Logger.info(`[Stream] ${this.itemTitle} sending stream_ready`)
this.socket.emit('stream_ready') this.socket.emit('stream_ready')
} }
clearInterval(intervalId) clearInterval(intervalId)

View File

@ -61,7 +61,7 @@ class UserAudiobookData {
} }
updateProgressFromStream(stream) { updateProgressFromStream(stream) {
this.audiobookId = stream.audiobookId this.audiobookId = stream.libraryItemId
this.totalDuration = stream.totalDuration this.totalDuration = stream.totalDuration
this.progress = stream.clientProgress this.progress = stream.clientProgress
this.currentTime = stream.clientCurrentTime this.currentTime = stream.clientCurrentTime

View File

@ -61,14 +61,15 @@ class UserListeningSession {
this.startedAt = session.startedAt this.startedAt = session.startedAt
} }
setData(audiobook, user) { setData(libraryItem, user) {
this.id = getId('ls') this.id = getId('ls')
this.userId = user.id this.userId = user.id
this.audiobookId = audiobook.id this.audiobookId = libraryItem.id
this.audiobookTitle = audiobook.title || '' // TODO: For podcasts this needs to be generic
this.audiobookAuthor = audiobook.authorFL || '' this.audiobookTitle = libraryItem.media.metadata.title || ''
this.audiobookDuration = audiobook.duration || 0 this.audiobookAuthor = libraryItem.media.metadata.authorName || ''
this.audiobookGenres = [...audiobook.genres] this.audiobookDuration = libraryItem.media.duration || 0
this.audiobookGenres = [...libraryItem.media.metadata.genres]
this.timeListening = 0 this.timeListening = 0
this.lastUpdate = Date.now() this.lastUpdate = Date.now()

View File

@ -30,7 +30,7 @@ async function writeConcatFile(tracks, outputPath, startTime = 0) {
var tracksToInclude = tracks.filter(t => t.index >= trackToStartWithIndex) var tracksToInclude = tracks.filter(t => t.index >= trackToStartWithIndex)
var trackPaths = tracksToInclude.map(t => { var trackPaths = tracksToInclude.map(t => {
var line = 'file ' + escapeSingleQuotes(t.fullPath) + '\n' + `duration ${t.duration}` var line = 'file ' + escapeSingleQuotes(t.metadata.path) + '\n' + `duration ${t.duration}`
return line return line
}) })
var inputstr = trackPaths.join('\n\n') var inputstr = trackPaths.join('\n\n')