2022-04-10 13:02:53 +02:00
|
|
|
function getMediaInfoFromTrack(libraryItem, castImage, track) {
|
2024-06-06 00:02:03 +02:00
|
|
|
let metadata = null
|
|
|
|
if (libraryItem.mediaType === 'podcast') {
|
|
|
|
metadata = new chrome.cast.media.MusicTrackMediaMetadata()
|
|
|
|
metadata.albumArtist = libraryItem.media.metadata.author
|
|
|
|
metadata.artist = libraryItem.media.metadata.author
|
|
|
|
metadata.title = track.title
|
|
|
|
metadata.albumName = libraryItem.media.metadata.title
|
|
|
|
metadata.images = [castImage]
|
|
|
|
} else {
|
|
|
|
// https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.AudiobookChapterMediaMetadata
|
|
|
|
metadata = new chrome.cast.media.AudiobookChapterMediaMetadata()
|
|
|
|
metadata.bookTitle = libraryItem.media.metadata.title
|
|
|
|
metadata.chapterNumber = track.index
|
|
|
|
metadata.chapterTitle = track.title
|
|
|
|
metadata.images = [castImage]
|
|
|
|
metadata.title = track.title
|
|
|
|
metadata.subtitle = libraryItem.media.metadata.title
|
|
|
|
}
|
2022-02-23 00:33:55 +01:00
|
|
|
|
|
|
|
var trackurl = track.fullContentUrl
|
|
|
|
var mimeType = track.mimeType
|
|
|
|
var mediainfo = new chrome.cast.media.MediaInfo(trackurl, mimeType)
|
|
|
|
mediainfo.metadata = metadata
|
|
|
|
mediainfo.itemId = track.index
|
|
|
|
mediainfo.duration = track.duration
|
|
|
|
return mediainfo
|
|
|
|
}
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
function buildCastMediaInfo(libraryItem, coverUrl, tracks) {
|
2022-02-23 00:33:55 +01:00
|
|
|
const castImage = new chrome.cast.Image(coverUrl)
|
2024-06-06 00:02:03 +02:00
|
|
|
return tracks.map((t) => getMediaInfoFromTrack(libraryItem, castImage, t))
|
2022-02-23 00:33:55 +01:00
|
|
|
}
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
function buildCastQueueRequest(libraryItem, coverUrl, tracks, startTime) {
|
|
|
|
var mediaInfoItems = buildCastMediaInfo(libraryItem, coverUrl, tracks)
|
2022-02-23 00:33:55 +01:00
|
|
|
|
2024-06-06 00:02:03 +02:00
|
|
|
let containerMetadata = null
|
|
|
|
let queueType = chrome.cast.media.QueueType.AUDIOBOOK
|
|
|
|
if (libraryItem.mediaType === 'podcast') {
|
|
|
|
queueType = chrome.cast.media.QueueType.PODCAST_SERIES
|
|
|
|
containerMetadata = new chrome.cast.media.ContainerMetadata(chrome.cast.media.ContainerType.GENERIC_CONTAINER)
|
|
|
|
containerMetadata.title = libraryItem.media.metadata.title
|
|
|
|
} else {
|
|
|
|
containerMetadata = new chrome.cast.media.AudiobookContainerMetadata()
|
|
|
|
containerMetadata.authors = libraryItem.media.metadata.authors?.map((a) => a.name)
|
|
|
|
containerMetadata.narrators = libraryItem.media.metadata.narrators || []
|
|
|
|
containerMetadata.publisher = libraryItem.media.metadata.publisher || undefined
|
|
|
|
containerMetadata.title = libraryItem.media.metadata.title
|
|
|
|
}
|
2022-02-23 00:33:55 +01:00
|
|
|
|
|
|
|
var mediaQueueItems = mediaInfoItems.map((mi) => {
|
|
|
|
var queueItem = new chrome.cast.media.QueueItem(mi)
|
|
|
|
return queueItem
|
|
|
|
})
|
|
|
|
|
|
|
|
// Find track to start playback and calculate track start offset
|
2024-06-06 00:02:03 +02:00
|
|
|
var track = tracks.find((at) => at.startOffset <= startTime && at.startOffset + at.duration > startTime)
|
2022-02-23 00:33:55 +01:00
|
|
|
var trackStartIndex = track ? track.index - 1 : 0
|
|
|
|
var trackStartTime = Math.floor(track ? startTime - track.startOffset : 0)
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
var queueData = new chrome.cast.media.QueueData(libraryItem.id, libraryItem.media.metadata.title, '', false, mediaQueueItems, trackStartIndex, trackStartTime)
|
2022-02-23 00:33:55 +01:00
|
|
|
queueData.containerMetadata = containerMetadata
|
2024-06-06 00:02:03 +02:00
|
|
|
queueData.queueType = queueType
|
2022-02-23 00:33:55 +01:00
|
|
|
return queueData
|
|
|
|
}
|
|
|
|
|
|
|
|
function castLoadMedia(castSession, request) {
|
|
|
|
return new Promise((resolve) => {
|
2024-06-06 00:02:03 +02:00
|
|
|
castSession.loadMedia(request).then(
|
|
|
|
() => resolve(true),
|
|
|
|
(reason) => {
|
2022-02-23 00:33:55 +01:00
|
|
|
console.error('Load media failed', reason)
|
|
|
|
resolve(false)
|
2024-06-06 00:02:03 +02:00
|
|
|
}
|
|
|
|
)
|
2022-02-23 00:33:55 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
function buildCastLoadRequest(libraryItem, coverUrl, tracks, startTime, autoplay, playbackRate) {
|
2022-02-23 00:33:55 +01:00
|
|
|
var request = new chrome.cast.media.LoadRequest()
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
request.queueData = buildCastQueueRequest(libraryItem, coverUrl, tracks, startTime)
|
2022-02-23 00:33:55 +01:00
|
|
|
request.currentTime = request.queueData.startTime
|
|
|
|
|
|
|
|
request.autoplay = autoplay
|
|
|
|
request.playbackRate = playbackRate
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
2024-06-06 00:02:03 +02:00
|
|
|
export { buildCastLoadRequest, castLoadMedia }
|