2021-09-04 21:17:26 +02:00
|
|
|
const Stream = require('./objects/Stream')
|
2021-09-18 23:42:20 +02:00
|
|
|
// const StreamTest = require('./test/StreamTest')
|
2021-08-18 00:01:11 +02:00
|
|
|
const Logger = require('./Logger')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
const Path = require('path')
|
|
|
|
|
|
|
|
class StreamManager {
|
2021-09-22 03:57:33 +02:00
|
|
|
constructor(db, MetadataPath) {
|
2021-08-18 00:01:11 +02:00
|
|
|
this.db = db
|
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
this.MetadataPath = MetadataPath
|
2021-08-18 00:01:11 +02:00
|
|
|
this.streams = []
|
2021-09-22 03:57:33 +02:00
|
|
|
this.StreamsPath = Path.join(this.MetadataPath, 'streams')
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get audiobooks() {
|
|
|
|
return this.db.audiobooks
|
|
|
|
}
|
|
|
|
|
|
|
|
getStream(streamId) {
|
|
|
|
return this.streams.find(s => s.id === streamId)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeStream(stream) {
|
|
|
|
this.streams = this.streams.filter(s => s.id !== stream.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
async openStream(client, audiobook) {
|
2021-09-22 03:57:33 +02:00
|
|
|
var stream = new Stream(this.StreamsPath, client, audiobook)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
stream.on('closed', () => {
|
|
|
|
this.removeStream(stream)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.streams.push(stream)
|
|
|
|
|
|
|
|
await stream.generatePlaylist()
|
|
|
|
stream.start()
|
|
|
|
|
|
|
|
Logger.info('Stream Opened for client', client.user.username, 'for audiobook', audiobook.title, 'with streamId', stream.id)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
async tempCheckStrayStreams() {
|
|
|
|
try {
|
|
|
|
var dirs = await fs.readdir(this.MetadataPath)
|
|
|
|
if (!dirs || !dirs.length) return true
|
|
|
|
|
|
|
|
await Promise.all(dirs.map(async (dirname) => {
|
2021-09-25 17:35:33 +02:00
|
|
|
if (dirname !== 'streams' && dirname !== 'books' && dirname !== 'downloads') {
|
2021-09-22 03:57:33 +02:00
|
|
|
var fullPath = Path.join(this.MetadataPath, dirname)
|
|
|
|
Logger.warn(`Removing OLD Orphan Stream ${dirname}`)
|
|
|
|
return fs.remove(fullPath)
|
|
|
|
}
|
|
|
|
}))
|
2021-09-25 17:35:33 +02:00
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
return true
|
|
|
|
} catch (error) {
|
|
|
|
Logger.debug('No old orphan streams', error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
async removeOrphanStreams() {
|
2021-09-22 03:57:33 +02:00
|
|
|
await this.tempCheckStrayStreams()
|
2021-08-18 00:01:11 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async openStreamSocketRequest(socket, audiobookId) {
|
|
|
|
Logger.info('Open Stream Request', socket.id, audiobookId)
|
|
|
|
var audiobook = this.audiobooks.find(ab => ab.id === audiobookId)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var stream = await this.openStream(client, audiobook)
|
|
|
|
this.db.updateUserStream(client.user.id, stream.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-22 03:57:33 +02:00
|
|
|
async openTestStream(StreamsPath, audiobookId) {
|
2021-09-01 20:47:18 +02:00
|
|
|
Logger.info('Open Stream Test Request', audiobookId)
|
2021-09-18 23:42:20 +02:00
|
|
|
// var audiobook = this.audiobooks.find(ab => ab.id === audiobookId)
|
2021-09-22 03:57:33 +02:00
|
|
|
// var stream = new StreamTest(StreamsPath, audiobook)
|
2021-09-01 20:47:18 +02:00
|
|
|
|
2021-09-18 23:42:20 +02:00
|
|
|
// stream.on('closed', () => {
|
|
|
|
// console.log('Stream closed')
|
|
|
|
// })
|
2021-09-01 20:47:18 +02:00
|
|
|
|
2021-09-18 23:42:20 +02:00
|
|
|
// var playlistUri = await stream.generatePlaylist()
|
|
|
|
// stream.start()
|
2021-09-01 20:47:18 +02:00
|
|
|
|
2021-09-18 23:42:20 +02:00
|
|
|
// Logger.info('Stream Playlist', playlistUri)
|
|
|
|
// Logger.info('Test Stream Opened for audiobook', audiobook.title, 'with streamId', stream.id)
|
|
|
|
// return playlistUri
|
2021-09-01 20:47:18 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
streamUpdate(socket, { currentTime, streamId }) {
|
|
|
|
var client = socket.sheepClient
|
|
|
|
if (!client || !client.stream) {
|
2021-09-05 21:30:33 +02:00
|
|
|
Logger.error('No stream for client', (client && client.user) ? client.user.id : 'No Client')
|
2021-08-18 00:01:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (client.stream.id !== streamId) {
|
|
|
|
Logger.error('Stream id mismatch on stream update', streamId, client.stream.id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client.stream.updateClientCurrentTime(currentTime)
|
2021-08-18 00:43:29 +02:00
|
|
|
if (!client.user) {
|
|
|
|
Logger.error('No User for client', client)
|
|
|
|
return
|
|
|
|
}
|
2021-09-06 21:13:01 +02:00
|
|
|
if (!client.user.updateAudiobookProgressFromStream) {
|
2021-08-18 13:50:24 +02:00
|
|
|
Logger.error('Invalid User for client', client)
|
|
|
|
return
|
|
|
|
}
|
2021-09-06 21:13:01 +02:00
|
|
|
client.user.updateAudiobookProgressFromStream(client.stream)
|
2021-08-18 13:50:24 +02:00
|
|
|
this.db.updateEntity('user', client.user)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = StreamManager
|