Update chromecast with new data model

This commit is contained in:
advplyr 2022-04-10 06:02:53 -05:00
parent 8df05896b5
commit 99e2ea228d
3 changed files with 22 additions and 21 deletions

View File

@ -9,7 +9,7 @@ export default class CastPlayer extends EventEmitter {
this.player = null this.player = null
this.playerController = null this.playerController = null
this.audiobook = null this.libraryItem = null
this.audioTracks = [] this.audioTracks = []
this.currentTrackIndex = 0 this.currentTrackIndex = 0
this.isHlsTranscode = null this.isHlsTranscode = null
@ -70,22 +70,22 @@ export default class CastPlayer extends EventEmitter {
} }
} }
async set(audiobook, tracks, isHlsTranscode, startTime, playWhenReady = false) { async set(libraryItem, tracks, isHlsTranscode, startTime, playWhenReady = false) {
this.audiobook = audiobook this.libraryItem = libraryItem
this.audioTracks = tracks this.audioTracks = tracks
this.isHlsTranscode = isHlsTranscode this.isHlsTranscode = isHlsTranscode
this.playWhenReady = playWhenReady this.playWhenReady = playWhenReady
this.currentTime = startTime this.currentTime = startTime
var coverImg = this.ctx.$store.getters['globals/getLibraryItemCoverSrc'](audiobook) var coverImg = this.ctx.$store.getters['globals/getLibraryItemCoverSrc'](libraryItem)
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
this.coverUrl = coverImg this.coverUrl = coverImg
} else { } else {
this.coverUrl = `${window.location.origin}${coverImg}` this.coverUrl = `${window.location.origin}${coverImg}`
} }
var request = buildCastLoadRequest(this.audiobook, this.coverUrl, this.audioTracks, this.currentTime, playWhenReady, this.defaultPlaybackRate) var request = buildCastLoadRequest(this.libraryItem, this.coverUrl, this.audioTracks, this.currentTime, playWhenReady, this.defaultPlaybackRate)
var castSession = cast.framework.CastContext.getInstance().getCurrentSession() var castSession = cast.framework.CastContext.getInstance().getCurrentSession()
await castLoadMedia(castSession, request) await castLoadMedia(castSession, request)
@ -126,7 +126,7 @@ export default class CastPlayer extends EventEmitter {
if (!this.player) return if (!this.player) return
if (time < this.currentTrack.startOffset || time > this.currentTrack.startOffset + this.currentTrack.duration) { if (time < this.currentTrack.startOffset || time > this.currentTrack.startOffset + this.currentTrack.duration) {
// Change Track // Change Track
var request = buildCastLoadRequest(this.audiobook, this.coverUrl, this.audioTracks, time, playWhenReady, this.defaultPlaybackRate) var request = buildCastLoadRequest(this.libraryItem, this.coverUrl, this.audioTracks, time, playWhenReady, this.defaultPlaybackRate)
var castSession = cast.framework.CastContext.getInstance().getCurrentSession() var castSession = cast.framework.CastContext.getInstance().getCurrentSession()
await castLoadMedia(castSession, request) await castLoadMedia(castSession, request)
} else { } else {

View File

@ -1,13 +1,13 @@
function getMediaInfoFromTrack(audiobook, castImage, track) { function getMediaInfoFromTrack(libraryItem, castImage, track) {
// https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.AudiobookChapterMediaMetadata // https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.AudiobookChapterMediaMetadata
var metadata = new chrome.cast.media.AudiobookChapterMediaMetadata() var metadata = new chrome.cast.media.AudiobookChapterMediaMetadata()
metadata.bookTitle = audiobook.book.title metadata.bookTitle = libraryItem.media.metadata.title
metadata.chapterNumber = track.index metadata.chapterNumber = track.index
metadata.chapterTitle = track.title metadata.chapterTitle = track.title
metadata.images = [castImage] metadata.images = [castImage]
metadata.title = track.title metadata.title = track.title
metadata.subtitle = audiobook.book.title metadata.subtitle = libraryItem.media.metadata.title
var trackurl = track.fullContentUrl var trackurl = track.fullContentUrl
var mimeType = track.mimeType var mimeType = track.mimeType
@ -18,18 +18,19 @@ function getMediaInfoFromTrack(audiobook, castImage, track) {
return mediainfo return mediainfo
} }
function buildCastMediaInfo(audiobook, coverUrl, tracks) { function buildCastMediaInfo(libraryItem, coverUrl, tracks) {
const castImage = new chrome.cast.Image(coverUrl) const castImage = new chrome.cast.Image(coverUrl)
return tracks.map(t => getMediaInfoFromTrack(audiobook, castImage, t)) return tracks.map(t => getMediaInfoFromTrack(libraryItem, castImage, t))
} }
function buildCastQueueRequest(audiobook, coverUrl, tracks, startTime) { function buildCastQueueRequest(libraryItem, coverUrl, tracks, startTime) {
var mediaInfoItems = buildCastMediaInfo(audiobook, coverUrl, tracks) var mediaInfoItems = buildCastMediaInfo(libraryItem, coverUrl, tracks)
var containerMetadata = new chrome.cast.media.AudiobookContainerMetadata() var containerMetadata = new chrome.cast.media.AudiobookContainerMetadata()
containerMetadata.authors = [audiobook.book.authorFL] containerMetadata.authors = libraryItem.media.metadata.authors.map(a => a.name)
containerMetadata.narrators = [audiobook.book.narratorFL] containerMetadata.narrators = libraryItem.media.metadata.narrators || []
containerMetadata.publisher = audiobook.book.publisher || undefined containerMetadata.publisher = libraryItem.media.metadata.publisher || undefined
containerMetadata.title = libraryItem.media.metadata.title
var mediaQueueItems = mediaInfoItems.map((mi) => { var mediaQueueItems = mediaInfoItems.map((mi) => {
var queueItem = new chrome.cast.media.QueueItem(mi) var queueItem = new chrome.cast.media.QueueItem(mi)
@ -41,7 +42,7 @@ function buildCastQueueRequest(audiobook, coverUrl, tracks, startTime) {
var trackStartIndex = track ? track.index - 1 : 0 var trackStartIndex = track ? track.index - 1 : 0
var trackStartTime = Math.floor(track ? startTime - track.startOffset : 0) var trackStartTime = Math.floor(track ? startTime - track.startOffset : 0)
var queueData = new chrome.cast.media.QueueData(audiobook.id, audiobook.book.title, '', false, mediaQueueItems, trackStartIndex, trackStartTime) var queueData = new chrome.cast.media.QueueData(libraryItem.id, libraryItem.media.metadata.title, '', false, mediaQueueItems, trackStartIndex, trackStartTime)
queueData.containerMetadata = containerMetadata queueData.containerMetadata = containerMetadata
queueData.queueType = chrome.cast.media.QueueType.AUDIOBOOK queueData.queueType = chrome.cast.media.QueueType.AUDIOBOOK
return queueData return queueData
@ -57,10 +58,10 @@ function castLoadMedia(castSession, request) {
}) })
} }
function buildCastLoadRequest(audiobook, coverUrl, tracks, startTime, autoplay, playbackRate) { function buildCastLoadRequest(libraryItem, coverUrl, tracks, startTime, autoplay, playbackRate) {
var request = new chrome.cast.media.LoadRequest() var request = new chrome.cast.media.LoadRequest()
request.queueData = buildCastQueueRequest(audiobook, coverUrl, tracks, startTime) request.queueData = buildCastQueueRequest(libraryItem, coverUrl, tracks, startTime)
request.currentTime = request.queueData.startTime request.currentTime = request.queueData.startTime
request.autoplay = autoplay request.autoplay = autoplay

View File

@ -2,8 +2,8 @@ export default (ctx) => {
var sendInit = async (castContext) => { var sendInit = async (castContext) => {
// Fetch background covers for chromecast (temp) // Fetch background covers for chromecast (temp)
var covers = await ctx.$axios.$get(`/api/libraries/${ctx.$store.state.libraries.currentLibraryId}/items?limit=40&minified=1`).then((data) => { var covers = await ctx.$axios.$get(`/api/libraries/${ctx.$store.state.libraries.currentLibraryId}/items?limit=40&minified=1`).then((data) => {
return data.results.filter((b) => b.book.cover).map((ab) => { return data.results.filter((b) => b.media.coverPath).map((libraryItem) => {
var coverUrl = ctx.$store.getters['globals/getLibraryItemCoverSrc'](ab) var coverUrl = ctx.$store.getters['globals/getLibraryItemCoverSrc'](libraryItem)
if (process.env.NODE_ENV === 'development') return coverUrl if (process.env.NODE_ENV === 'development') return coverUrl
return `${window.location.origin}${coverUrl}` return `${window.location.origin}${coverUrl}`
}) })