2022-03-16 01:28:54 +01:00
|
|
|
const Stream = require('../Stream')
|
2021-09-18 23:42:20 +02:00
|
|
|
// const StreamTest = require('./test/StreamTest')
|
2022-03-16 01:28:54 +01:00
|
|
|
const Logger = require('../../Logger')
|
2021-08-18 00:01:11 +02:00
|
|
|
const fs = require('fs-extra')
|
|
|
|
const Path = require('path')
|
|
|
|
|
|
|
|
class StreamManager {
|
2022-02-27 20:47:52 +01:00
|
|
|
constructor(db, emitter, clientEmitter) {
|
2021-08-18 00:01:11 +02:00
|
|
|
this.db = db
|
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
this.emitter = emitter
|
2021-10-26 04:14:54 +02:00
|
|
|
this.clientEmitter = clientEmitter
|
2021-10-23 03:08:02 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.streams = []
|
2022-02-27 20:47:52 +01:00
|
|
|
this.StreamsPath = Path.join(global.MetadataPath, 'streams')
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getStream(streamId) {
|
|
|
|
return this.streams.find(s => s.id === streamId)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeStream(stream) {
|
|
|
|
this.streams = this.streams.filter(s => s.id !== stream.id)
|
|
|
|
}
|
|
|
|
|
2022-03-13 02:59:35 +01:00
|
|
|
async openStream(client, libraryItem, transcodeOptions = {}) {
|
2021-11-13 22:24:56 +01:00
|
|
|
if (!client || !client.user) {
|
|
|
|
Logger.error('[StreamManager] Cannot open stream invalid client', client)
|
|
|
|
return
|
|
|
|
}
|
2022-03-13 02:59:35 +01:00
|
|
|
var stream = new Stream(this.StreamsPath, client, libraryItem, transcodeOptions)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
stream.on('closed', () => {
|
|
|
|
this.removeStream(stream)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.streams.push(stream)
|
|
|
|
|
|
|
|
await stream.generatePlaylist()
|
|
|
|
stream.start()
|
|
|
|
|
2022-03-13 02:59:35 +01:00
|
|
|
Logger.info('Stream Opened for client', client.user.username, 'for item', stream.itemTitle, 'with streamId', stream.id)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
client.stream = stream
|
|
|
|
client.user.stream = stream.id
|
|
|
|
|
|
|
|
return stream
|
|
|
|
}
|
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
ensureStreamsDir() {
|
|
|
|
return fs.ensureDir(this.StreamsPath)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
removeOrphanStreamFiles(streamId) {
|
|
|
|
try {
|
2021-09-22 03:57:33 +02:00
|
|
|
var StreamsPath = Path.join(this.StreamsPath, streamId)
|
|
|
|
return fs.remove(StreamsPath)
|
2021-08-18 00:01:11 +02:00
|
|
|
} catch (error) {
|
|
|
|
Logger.debug('No orphan stream', streamId)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeOrphanStreams() {
|
|
|
|
try {
|
2021-09-22 03:57:33 +02:00
|
|
|
var dirs = await fs.readdir(this.StreamsPath)
|
2021-08-18 00:01:11 +02:00
|
|
|
if (!dirs || !dirs.length) return true
|
|
|
|
|
|
|
|
await Promise.all(dirs.map(async (dirname) => {
|
2021-09-22 03:57:33 +02:00
|
|
|
var fullPath = Path.join(this.StreamsPath, dirname)
|
2021-08-18 00:01:11 +02:00
|
|
|
Logger.info(`Removing Orphan Stream ${dirname}`)
|
|
|
|
return fs.remove(fullPath)
|
|
|
|
}))
|
|
|
|
return true
|
|
|
|
} catch (error) {
|
2021-09-22 03:57:33 +02:00
|
|
|
Logger.debug('No orphan stream', error)
|
2021-08-18 00:01:11 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 02:59:35 +01:00
|
|
|
async openStreamApiRequest(res, user, libraryItem) {
|
|
|
|
Logger.info(`[StreamManager] User "${user.username}" open stream request for "${libraryItem.media.metadata.title}"`)
|
2021-11-11 15:39:21 +01:00
|
|
|
var client = {
|
|
|
|
user
|
|
|
|
}
|
2022-03-13 02:59:35 +01:00
|
|
|
var stream = await this.openStream(client, libraryItem)
|
2021-11-11 15:39:21 +01:00
|
|
|
this.db.updateUserStream(client.user.id, stream.id)
|
|
|
|
|
|
|
|
res.json({
|
2022-03-13 02:59:35 +01:00
|
|
|
libraryItemId: libraryItem.id,
|
2021-11-11 15:39:21 +01:00
|
|
|
startTime: stream.startTime,
|
|
|
|
streamId: stream.id,
|
|
|
|
streamUrl: stream.clientPlaylistUri
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-13 02:59:35 +01:00
|
|
|
async openStreamSocketRequest(socket, libraryItemId) {
|
|
|
|
Logger.info('[StreamManager] Open Stream Request', socket.id, libraryItemId)
|
|
|
|
var libraryItem = this.db.libraryItems.find(li => li.id === libraryItemId)
|
2021-08-18 00:01:11 +02:00
|
|
|
var client = socket.sheepClient
|
|
|
|
|
|
|
|
if (client.stream) {
|
|
|
|
Logger.info('Closing client stream first', client.stream.id)
|
|
|
|
await client.stream.close()
|
|
|
|
client.user.stream = null
|
|
|
|
client.stream = null
|
|
|
|
}
|
|
|
|
|
2022-03-13 02:59:35 +01:00
|
|
|
var stream = await this.openStream(client, libraryItem)
|
2021-08-18 00:01:11 +02:00
|
|
|
this.db.updateUserStream(client.user.id, stream.id)
|
2021-10-13 03:07:42 +02:00
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
this.emitter('user_stream_update', client.user.toJSONForPublic(this.streams))
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async closeStreamRequest(socket) {
|
|
|
|
Logger.info('Close Stream Request', socket.id)
|
|
|
|
var client = socket.sheepClient
|
|
|
|
if (!client || !client.stream) {
|
2021-09-03 13:40:59 +02:00
|
|
|
Logger.error('No stream for client', (client && client.user) ? client.user.username : 'No Client')
|
2021-09-02 02:39:38 +02:00
|
|
|
client.socket.emit('stream_closed', 'n/a')
|
2021-08-18 00:01:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// var streamId = client.stream.id
|
|
|
|
await client.stream.close()
|
|
|
|
client.user.stream = null
|
|
|
|
client.stream = null
|
|
|
|
this.db.updateUserStream(client.user.id, null)
|
2021-10-13 03:07:42 +02:00
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
this.emitter('user_stream_update', client.user.toJSONForPublic(this.streams))
|
2021-09-01 20:47:18 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
async closeStreamApiRequest(userId, streamId) {
|
|
|
|
Logger.info('[StreamManager] Close Stream Api Request', streamId)
|
|
|
|
|
|
|
|
var stream = this.streams.find(s => s.id === streamId)
|
|
|
|
if (!stream) {
|
|
|
|
Logger.warn('[StreamManager] Stream not found', streamId)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream.client || !stream.client.user || stream.client.user.id !== userId) {
|
|
|
|
Logger.warn(`[StreamManager] Stream close request from invalid user ${userId}`, stream.client)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.client.user.stream = null
|
|
|
|
stream.client.stream = null
|
|
|
|
this.db.updateUserStream(stream.client.user.id, null)
|
|
|
|
|
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
this.streams = this.streams.filter(s => s.id !== streamId)
|
|
|
|
Logger.info(`[StreamManager] Stream ${streamId} closed via API request by ${userId}`)
|
|
|
|
}
|
|
|
|
|
2021-11-13 02:43:16 +01:00
|
|
|
streamSync(socket, syncData) {
|
|
|
|
const client = socket.sheepClient
|
|
|
|
if (!client || !client.stream) {
|
|
|
|
Logger.error('[StreamManager] streamSync: No stream for client', (client && client.user) ? client.user.id : 'No Client')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (client.stream.id !== syncData.streamId) {
|
|
|
|
Logger.error('[StreamManager] streamSync: Stream id mismatch on stream update', syncData.streamId, client.stream.id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!client.user) {
|
|
|
|
Logger.error('[StreamManager] streamSync: No User for client', client)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// const { timeListened, currentTime, streamId } = syncData
|
|
|
|
var listeningSession = client.stream.syncStream(syncData)
|
|
|
|
|
|
|
|
if (listeningSession && listeningSession.timeListening > 0) {
|
|
|
|
// Save listening session
|
|
|
|
var existingListeningSession = this.db.sessions.find(s => s.id === listeningSession.id)
|
|
|
|
if (existingListeningSession) {
|
|
|
|
this.db.updateEntity('session', listeningSession)
|
|
|
|
} else {
|
|
|
|
this.db.sessions.push(listeningSession.toJSON()) // Insert right away to prevent duplicate session
|
|
|
|
this.db.insertEntity('session', listeningSession)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var userAudiobook = client.user.updateAudiobookProgressFromStream(client.stream)
|
|
|
|
this.db.updateEntity('user', client.user)
|
|
|
|
|
|
|
|
if (userAudiobook) {
|
|
|
|
this.clientEmitter(client.user.id, 'current_user_audiobook_update', {
|
|
|
|
id: userAudiobook.audiobookId,
|
|
|
|
data: userAudiobook.toJSON()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 01:45:56 +01:00
|
|
|
streamSyncFromApi(req, res) {
|
|
|
|
var user = req.user
|
|
|
|
var syncData = req.body
|
|
|
|
|
|
|
|
var stream = this.streams.find(s => s.id === syncData.streamId)
|
|
|
|
if (!stream) {
|
|
|
|
Logger.error(`[StreamManager] streamSyncFromApi stream not found ${syncData.streamId}`)
|
|
|
|
return res.status(404).send('Stream not found')
|
|
|
|
}
|
|
|
|
if (stream.userToken !== user.token) {
|
|
|
|
Logger.error(`[StreamManager] streamSyncFromApi Invalid stream not owned by user`)
|
|
|
|
return res.status(500).send('Invalid stream auth')
|
|
|
|
}
|
|
|
|
|
|
|
|
var listeningSession = stream.syncStream(syncData)
|
|
|
|
|
|
|
|
if (listeningSession && listeningSession.timeListening > 0) {
|
|
|
|
// Save listening session
|
|
|
|
var existingListeningSession = this.db.sessions.find(s => s.id === listeningSession.id)
|
|
|
|
if (existingListeningSession) {
|
|
|
|
this.db.updateEntity('session', listeningSession)
|
|
|
|
} else {
|
|
|
|
this.db.sessions.push(listeningSession.toJSON()) // Insert right away to prevent duplicate session
|
|
|
|
this.db.insertEntity('session', listeningSession)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var userAudiobook = user.updateAudiobookProgressFromStream(stream)
|
|
|
|
this.db.updateEntity('user', user)
|
|
|
|
|
|
|
|
if (userAudiobook) {
|
|
|
|
this.clientEmitter(user.id, 'current_user_audiobook_update', {
|
|
|
|
id: userAudiobook.audiobookId,
|
|
|
|
data: userAudiobook.toJSON()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = StreamManager
|