Skip to content

Commit

Permalink
Merge pull request #3785 from advplyr/playback-session-use-new-librar…
Browse files Browse the repository at this point in the history
…y-item

Update PlaybackSession to use new library item model
  • Loading branch information
advplyr authored Jan 3, 2025
2 parents d205c6f + c251f18 commit de7296e
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 188 deletions.
2 changes: 1 addition & 1 deletion server/controllers/SessionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class SessionController {
* @param {Response} res
*/
async getOpenSession(req, res) {
const libraryItem = await Database.libraryItemModel.getOldById(req.playbackSession.libraryItemId)
const libraryItem = await Database.libraryItemModel.getExpandedById(req.playbackSession.libraryItemId)
const sessionForClient = req.playbackSession.toJSONForClient(libraryItem)
res.json(sessionForClient)
}
Expand Down
13 changes: 6 additions & 7 deletions server/controllers/ShareController.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ class ShareController {
}

try {
const oldLibraryItem = await Database.mediaItemShareModel.getMediaItemsOldLibraryItem(mediaItemShare.mediaItemId, mediaItemShare.mediaItemType)

if (!oldLibraryItem) {
const libraryItem = await Database.mediaItemShareModel.getMediaItemsLibraryItem(mediaItemShare.mediaItemId, mediaItemShare.mediaItemType)
if (!libraryItem) {
return res.status(404).send('Media item not found')
}

let startOffset = 0
const publicTracks = oldLibraryItem.media.includedAudioFiles.map((audioFile) => {
const publicTracks = libraryItem.media.includedAudioFiles.map((audioFile) => {
const audioTrack = {
index: audioFile.index,
startOffset,
Expand All @@ -86,7 +85,7 @@ class ShareController {
contentUrl: `${global.RouterBasePath}/public/share/${slug}/track/${audioFile.index}`,
mimeType: audioFile.mimeType,
codec: audioFile.codec || null,
metadata: audioFile.metadata.clone()
metadata: structuredClone(audioFile.metadata)
}
startOffset += audioTrack.duration
return audioTrack
Expand All @@ -105,12 +104,12 @@ class ShareController {
const deviceInfo = await this.playbackSessionManager.getDeviceInfo(req, clientDeviceInfo)

const newPlaybackSession = new PlaybackSession()
newPlaybackSession.setData(oldLibraryItem, null, 'web-share', deviceInfo, startTime)
newPlaybackSession.setData(libraryItem, null, 'web-share', deviceInfo, startTime)
newPlaybackSession.audioTracks = publicTracks
newPlaybackSession.playMethod = PlayMethod.DIRECTPLAY
newPlaybackSession.shareSessionId = shareSessionId
newPlaybackSession.mediaItemShareId = mediaItemShare.id
newPlaybackSession.coverAspectRatio = oldLibraryItem.librarySettings.coverAspectRatio
newPlaybackSession.coverAspectRatio = libraryItem.library.settings.coverAspectRatio

mediaItemShare.playbackSession = newPlaybackSession.toJSONForClient()
ShareManager.addOpenSharePlaybackSession(newPlaybackSession)
Expand Down
10 changes: 5 additions & 5 deletions server/managers/PlaybackSessionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class PlaybackSessionManager {
async startSessionRequest(req, res, episodeId) {
const deviceInfo = await this.getDeviceInfo(req, req.body?.deviceInfo)
Logger.debug(`[PlaybackSessionManager] startSessionRequest for device ${deviceInfo.deviceDescription}`)
const { oldLibraryItem: libraryItem, body: options } = req
const { libraryItem, body: options } = req
const session = await this.startSession(req.user, deviceInfo, libraryItem, episodeId, options)
res.json(session.toJSONForClient(libraryItem))
}
Expand Down Expand Up @@ -279,7 +279,7 @@ class PlaybackSessionManager {
*
* @param {import('../models/User')} user
* @param {DeviceInfo} deviceInfo
* @param {import('../objects/LibraryItem')} libraryItem
* @param {import('../models/LibraryItem')} libraryItem
* @param {string|null} episodeId
* @param {{forceDirectPlay?:boolean, forceTranscode?:boolean, mediaPlayer:string, supportedMimeTypes?:string[]}} options
* @returns {Promise<PlaybackSession>}
Expand All @@ -292,15 +292,15 @@ class PlaybackSessionManager {
await this.closeSession(user, session, null)
}

const shouldDirectPlay = options.forceDirectPlay || (!options.forceTranscode && libraryItem.media.checkCanDirectPlay(options, episodeId))
const shouldDirectPlay = options.forceDirectPlay || (!options.forceTranscode && libraryItem.media.checkCanDirectPlay(options.supportedMimeTypes, episodeId))
const mediaPlayer = options.mediaPlayer || 'unknown'

const mediaItemId = episodeId || libraryItem.media.id
const userProgress = user.getMediaProgress(mediaItemId)
let userStartTime = 0
if (userProgress) {
if (userProgress.isFinished) {
Logger.info(`[PlaybackSessionManager] Starting session for user "${user.username}" and resetting progress for finished item "${libraryItem.media.metadata.title}"`)
Logger.info(`[PlaybackSessionManager] Starting session for user "${user.username}" and resetting progress for finished item "${libraryItem.media.title}"`)
// Keep userStartTime as 0 so the client restarts the media
} else {
userStartTime = Number.parseFloat(userProgress.currentTime) || 0
Expand All @@ -312,7 +312,7 @@ class PlaybackSessionManager {
let audioTracks = []
if (shouldDirectPlay) {
Logger.debug(`[PlaybackSessionManager] "${user.username}" starting direct play session for item "${libraryItem.id}" with id ${newPlaybackSession.id} (Device: ${newPlaybackSession.deviceDescription})`)
audioTracks = libraryItem.getDirectPlayTracklist(episodeId)
audioTracks = libraryItem.getTrackList(episodeId)
newPlaybackSession.playMethod = PlayMethod.DIRECTPLAY
} else {
Logger.debug(`[PlaybackSessionManager] "${user.username}" starting stream session for item "${libraryItem.id}" (Device: ${newPlaybackSession.deviceDescription})`)
Expand Down
64 changes: 57 additions & 7 deletions server/models/Book.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ const parseNameString = require('../utils/parsers/parseNameString')
* @property {ChapterObject[]} chapters
* @property {Object} metaTags
* @property {string} mimeType
*
* @typedef AudioTrackProperties
* @property {string} title
* @property {string} contentUrl
* @property {number} startOffset
*
* @typedef {AudioFileObject & AudioTrackProperties} AudioTrack
*/

class Book extends Model {
Expand Down Expand Up @@ -367,22 +374,65 @@ class Book extends Model {
return this.audioFiles.filter((af) => !af.exclude)
}

get trackList() {
get hasMediaFiles() {
return !!this.hasAudioTracks || !!this.ebookFile
}

get hasAudioTracks() {
return !!this.includedAudioFiles.length
}

/**
* Supported mime types are sent from the web client and are retrieved using the browser Audio player "canPlayType" function.
*
* @param {string[]} supportedMimeTypes
* @returns {boolean}
*/
checkCanDirectPlay(supportedMimeTypes) {
if (!Array.isArray(supportedMimeTypes)) {
Logger.error(`[Book] checkCanDirectPlay: supportedMimeTypes is not an array`, supportedMimeTypes)
return false
}
return this.includedAudioFiles.every((af) => supportedMimeTypes.includes(af.mimeType))
}

/**
* Get the track list to be used in client audio players
* AudioTrack is the AudioFile with startOffset, contentUrl and title
*
* @param {string} libraryItemId
* @returns {AudioTrack[]}
*/
getTracklist(libraryItemId) {
let startOffset = 0
return this.includedAudioFiles.map((af) => {
const track = structuredClone(af)
track.title = af.metadata.filename
track.startOffset = startOffset
track.contentUrl = `${global.RouterBasePath}/api/items/${libraryItemId}/file/${track.ino}`
startOffset += track.duration
return track
})
}

get hasMediaFiles() {
return !!this.hasAudioTracks || !!this.ebookFile
/**
*
* @returns {ChapterObject[]}
*/
getChapters() {
return structuredClone(this.chapters) || []
}

get hasAudioTracks() {
return !!this.includedAudioFiles.length
getPlaybackTitle() {
return this.title
}

getPlaybackAuthor() {
return this.authorName
}

getPlaybackDuration() {
return this.duration
}

/**
Expand Down Expand Up @@ -635,7 +685,7 @@ class Book extends Model {
metadata: this.oldMetadataToJSONMinified(),
coverPath: this.coverPath,
tags: [...(this.tags || [])],
numTracks: this.trackList.length,
numTracks: this.includedAudioFiles.length,
numAudioFiles: this.audioFiles?.length || 0,
numChapters: this.chapters?.length || 0,
duration: this.duration,
Expand Down Expand Up @@ -666,7 +716,7 @@ class Book extends Model {
ebookFile: structuredClone(this.ebookFile),
duration: this.duration,
size: this.size,
tracks: structuredClone(this.trackList)
tracks: this.getTracklist(libraryItemId)
}
}
}
Expand Down
24 changes: 13 additions & 11 deletions server/models/FeedEpisode.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ class FeedEpisode extends Model {
/**
* If chapters for an audiobook match the audio tracks then use chapter titles instead of audio file names
*
* @param {import('./Book').AudioTrack[]} trackList
* @param {import('./Book')} book
* @returns {boolean}
*/
static checkUseChapterTitlesForEpisodes(book) {
const tracks = book.trackList || []
static checkUseChapterTitlesForEpisodes(trackList, book) {
const chapters = book.chapters || []
if (tracks.length !== chapters.length) return false
for (let i = 0; i < tracks.length; i++) {
if (Math.abs(chapters[i].start - tracks[i].startOffset) >= 1) {
if (trackList.length !== chapters.length) return false
for (let i = 0; i < trackList.length; i++) {
if (Math.abs(chapters[i].start - trackList[i].startOffset) >= 1) {
return false
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ class FeedEpisode extends Model {
const contentUrl = `/feed/${slug}/item/${episodeId}/media${Path.extname(audioTrack.metadata.filename)}`

let title = Path.basename(audioTrack.metadata.filename, Path.extname(audioTrack.metadata.filename))
if (book.trackList.length == 1) {
if (book.includedAudioFiles.length == 1) {
// If audiobook is a single file, use book title instead of chapter/file title
title = book.title
} else {
Expand Down Expand Up @@ -185,11 +185,12 @@ class FeedEpisode extends Model {
* @returns {Promise<FeedEpisode[]>}
*/
static async createFromAudiobookTracks(libraryItemExpanded, feed, slug, transaction) {
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(libraryItemExpanded.media)
const trackList = libraryItemExpanded.getTrackList()
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(trackList, libraryItemExpanded.media)

const feedEpisodeObjs = []
let numExisting = 0
for (const track of libraryItemExpanded.media.trackList) {
for (const track of trackList) {
// Check for existing episode by filepath
const existingEpisode = feed.feedEpisodes?.find((episode) => {
return episode.filePath === track.metadata.path
Expand All @@ -204,7 +205,7 @@ class FeedEpisode extends Model {

/**
*
* @param {import('./Book')[]} books
* @param {import('./Book').BookExpandedWithLibraryItem[]} books
* @param {import('./Feed')} feed
* @param {string} slug
* @param {import('sequelize').Transaction} transaction
Expand All @@ -218,8 +219,9 @@ class FeedEpisode extends Model {
const feedEpisodeObjs = []
let numExisting = 0
for (const book of books) {
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(book)
for (const track of book.trackList) {
const trackList = book.libraryItem.getTrackList()
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(trackList, book)
for (const track of trackList) {
// Check for existing episode by filepath
const existingEpisode = feed.feedEpisodes?.find((episode) => {
return episode.filePath === track.metadata.path
Expand Down
67 changes: 67 additions & 0 deletions server/models/LibraryItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,57 @@ class LibraryItem extends Model {
return libraryItem
}

/**
*
* @param {import('sequelize').WhereOptions} where
* @param {import('sequelize').IncludeOptions} [include]
* @returns {Promise<LibraryItemExpanded>}
*/
static async findOneExpanded(where, include = null) {
const libraryItem = await this.findOne({
where,
include
})
if (!libraryItem) {
Logger.error(`[LibraryItem] Library item not found`)
return null
}

if (libraryItem.mediaType === 'podcast') {
libraryItem.media = await libraryItem.getMedia({
include: [
{
model: this.sequelize.models.podcastEpisode
}
]
})
} else {
libraryItem.media = await libraryItem.getMedia({
include: [
{
model: this.sequelize.models.author,
through: {
attributes: []
}
},
{
model: this.sequelize.models.series,
through: {
attributes: ['id', 'sequence']
}
}
],
order: [
[this.sequelize.models.author, this.sequelize.models.bookAuthor, 'createdAt', 'ASC'],
[this.sequelize.models.series, 'bookSeries', 'createdAt', 'ASC']
]
})
}

if (!libraryItem.media) return null
return libraryItem
}

/**
* Get old library item by id
* @param {string} libraryItemId
Expand Down Expand Up @@ -1176,6 +1227,22 @@ class LibraryItem extends Model {
}
}

/**
* Get the track list to be used in client audio players
* AudioTrack is the AudioFile with startOffset and contentUrl
* Podcasts must have an episodeId to get the track list
*
* @param {string} [episodeId]
* @returns {import('./Book').AudioTrack[]}
*/
getTrackList(episodeId) {
if (!this.media) {
Logger.error(`[LibraryItem] getTrackList: Library item "${this.id}" does not have media`)
return []
}
return this.media.getTracklist(this.id, episodeId)
}

/**
*
* @param {string} ino
Expand Down
Loading

0 comments on commit de7296e

Please sign in to comment.