Add: api endpoint for starting streams for android auto support

This commit is contained in:
advplyr 2021-11-11 08:39:21 -06:00
parent 2451861e0e
commit 663d02e9fe
5 changed files with 58 additions and 22 deletions

View File

@ -1,6 +1,6 @@
{
"name": "audiobookshelf-client",
"version": "1.6.14",
"version": "1.6.15",
"description": "Audiobook manager and player",
"main": "index.js",
"scripts": {

View File

@ -1,6 +1,6 @@
{
"name": "audiobookshelf",
"version": "1.6.14",
"version": "1.6.15",
"description": "Self-hosted audiobook server for managing and playing audiobooks",
"main": "index.js",
"scripts": {

View File

@ -57,6 +57,7 @@ class ApiController {
this.router.post('/audiobook/:id/cover', this.uploadAudiobookCover.bind(this))
this.router.patch('/audiobook/:id/coverfile', this.updateAudiobookCoverFromFile.bind(this))
this.router.get('/audiobook/:id/match', this.matchAudiobookBook.bind(this))
this.router.get('/audiobook/:id/stream', this.openAudiobookStream.bind(this))
this.router.patch('/audiobook/:id', this.updateAudiobook.bind(this))
this.router.patch('/match/:id', this.match.bind(this))
@ -541,6 +542,13 @@ class ApiController {
res.json(results)
}
async openAudiobookStream(req, res) {
var audiobook = this.db.audiobooks.find(a => a.id === req.params.id)
if (!audiobook) return res.sendStatus(404)
this.streamManager.openStreamApiRequest(res, req.user, audiobook)
}
async updateAudiobook(req, res) {
if (!req.user.canUpdate) {
Logger.warn('User attempted to update without permission', req.user)

View File

@ -100,8 +100,24 @@ class StreamManager {
}
}
async openStreamApiRequest(res, user, audiobook) {
Logger.info(`[StreamManager] User "${user.username}" open stream request for "${audiobook.title}"`)
var client = {
user
}
var stream = await this.openStream(client, audiobook)
this.db.updateUserStream(client.user.id, stream.id)
res.json({
audiobookId: audiobook.id,
startTime: stream.startTime,
streamId: stream.id,
streamUrl: stream.clientPlaylistUri
})
}
async openStreamSocketRequest(socket, audiobookId) {
Logger.info('Open Stream Request', socket.id, audiobookId)
Logger.info('[StreamManager] Open Stream Request', socket.id, audiobookId)
var audiobook = this.audiobooks.find(ab => ab.id === audiobookId)
var client = socket.sheepClient

View File

@ -41,7 +41,7 @@ class Stream extends EventEmitter {
}
get socket() {
return this.client.socket
return this.client ? this.client.socket || null : null
}
get audiobookId() {
@ -89,15 +89,15 @@ class Stream extends EventEmitter {
}
get clientUser() {
return this.client.user || {}
return this.client ? this.client.user || {} : null
}
get clientUserAudiobooks() {
return this.clientUser.audiobooks || {}
return this.client ? this.clientUser.audiobooks || {} : null
}
get clientUserAudiobookData() {
return this.clientUserAudiobooks[this.audiobookId]
return this.client ? this.clientUserAudiobooks[this.audiobookId] : null
}
get clientPlaylistUri() {
@ -189,8 +189,10 @@ class Stream extends EventEmitter {
if (this.segmentsCreated.size > 6 && !this.isClientInitialized) {
this.isClientInitialized = true
Logger.info(`[STREAM] ${this.id} notifying client that stream is ready`)
this.socket.emit('stream_open', this.toJSON())
if (this.socket) {
Logger.info(`[STREAM] ${this.id} notifying client that stream is ready`)
this.socket.emit('stream_open', this.toJSON())
}
}
var chunks = []
@ -221,14 +223,16 @@ class Stream extends EventEmitter {
var perc = (this.segmentsCreated.size * 100 / this.numSegments).toFixed(2) + '%'
Logger.info('[STREAM-CHECK] Check Files', this.segmentsCreated.size, 'of', this.numSegments, perc, `Furthest Segment: ${this.furthestSegmentCreated}`)
Logger.debug('[STREAM-CHECK] Chunks', chunks.join(', '))
// Logger.debug('[STREAM-CHECK] Chunks', chunks.join(', '))
this.socket.emit('stream_progress', {
stream: this.id,
percent: perc,
chunks,
numSegments: this.numSegments
})
if (this.socket) {
this.socket.emit('stream_progress', {
stream: this.id,
percent: perc,
chunks,
numSegments: this.numSegments
})
}
} catch (error) {
Logger.error('Failed checking files', error)
}
@ -236,15 +240,19 @@ class Stream extends EventEmitter {
startLoop() {
// Logger.info(`[Stream] ${this.audiobookTitle} (${this.id}) Start Loop`)
this.socket.emit('stream_progress', { stream: this.id, chunks: [], numSegments: 0, percent: '0%' })
if (this.socket) {
this.socket.emit('stream_progress', { stream: this.id, chunks: [], numSegments: 0, percent: '0%' })
}
clearInterval(this.loop)
var intervalId = setInterval(() => {
if (!this.isTranscodeComplete) {
this.checkFiles()
} else {
Logger.info(`[Stream] ${this.audiobookTitle} sending stream_ready`)
this.socket.emit('stream_ready')
if (this.socket) {
Logger.info(`[Stream] ${this.audiobookTitle} sending stream_ready`)
this.socket.emit('stream_ready')
}
clearInterval(intervalId)
}
}, 2000)
@ -342,8 +350,10 @@ class Stream extends EventEmitter {
// For very small fast load
if (!this.isClientInitialized) {
this.isClientInitialized = true
Logger.info(`[STREAM] ${this.id} notifying client that stream is ready`)
this.socket.emit('stream_open', this.toJSON())
if (this.socket) {
Logger.info(`[STREAM] ${this.id} notifying client that stream is ready`)
this.socket.emit('stream_open', this.toJSON())
}
}
this.isTranscodeComplete = true
this.ffmpeg = null
@ -367,7 +377,9 @@ class Stream extends EventEmitter {
Logger.error('Failed to delete session data', err)
})
this.client.socket.emit('stream_closed', this.id)
if (this.socket) {
this.socket.emit('stream_closed', this.id)
}
this.emit('closed')
}