2021-08-18 00:01:11 +02:00
|
|
|
const express = require('express')
|
|
|
|
const Path = require('path')
|
2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
2022-03-18 01:10:47 +01:00
|
|
|
const Logger = require('../Logger')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
class HlsRouter {
|
2022-03-16 01:28:54 +01:00
|
|
|
constructor(db, auth, playbackSessionManager, emitter) {
|
2021-08-18 00:01:11 +02:00
|
|
|
this.db = db
|
|
|
|
this.auth = auth
|
2022-03-18 01:10:47 +01:00
|
|
|
this.playbackSessionManager = playbackSessionManager
|
2021-08-18 00:01:11 +02:00
|
|
|
this.emitter = emitter
|
|
|
|
|
|
|
|
this.router = express()
|
|
|
|
this.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.router.get('/:stream/:file', this.streamFileRequest.bind(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
parseSegmentFilename(filename) {
|
2021-10-01 01:52:32 +02:00
|
|
|
var basename = Path.basename(filename, Path.extname(filename))
|
2021-08-18 00:01:11 +02:00
|
|
|
var num_part = basename.split('-')[1]
|
|
|
|
return Number(num_part)
|
|
|
|
}
|
|
|
|
|
|
|
|
async streamFileRequest(req, res) {
|
|
|
|
var streamId = req.params.stream
|
2022-03-18 01:10:47 +01:00
|
|
|
var fullFilePath = Path.join(this.playbackSessionManager.StreamsPath, streamId, req.params.file)
|
2021-09-01 20:47:18 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
var exists = await fs.pathExists(fullFilePath)
|
|
|
|
if (!exists) {
|
2021-08-21 01:29:10 +02:00
|
|
|
Logger.warn('File path does not exist', fullFilePath)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
var fileExt = Path.extname(req.params.file)
|
2021-10-01 01:52:32 +02:00
|
|
|
if (fileExt === '.ts' || fileExt === '.m4s') {
|
2021-08-18 00:01:11 +02:00
|
|
|
var segNum = this.parseSegmentFilename(req.params.file)
|
2022-03-18 01:10:47 +01:00
|
|
|
var stream = this.playbackSessionManager.getStream(streamId)
|
2021-08-18 00:01:11 +02:00
|
|
|
if (!stream) {
|
2022-03-18 01:10:47 +01:00
|
|
|
Logger.error(`[HlsRouter] Stream ${streamId} does not exist`)
|
2021-08-18 00:01:11 +02:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream.isResetting) {
|
2022-03-18 01:10:47 +01:00
|
|
|
Logger.info(`[HlsRouter] Stream ${streamId} is currently resetting`)
|
2021-08-18 00:01:11 +02:00
|
|
|
return res.sendStatus(404)
|
|
|
|
} else {
|
|
|
|
var startTimeForReset = await stream.checkSegmentNumberRequest(segNum)
|
|
|
|
if (startTimeForReset) {
|
2021-08-21 01:29:10 +02:00
|
|
|
// HLS.js will restart the stream at the new time
|
2022-03-18 01:10:47 +01:00
|
|
|
Logger.info(`[HlsRouter] Resetting Stream - notify client @${startTimeForReset}s`)
|
2021-08-18 00:01:11 +02:00
|
|
|
this.emitter('stream_reset', {
|
|
|
|
startTime: startTimeForReset,
|
|
|
|
streamId: stream.id
|
|
|
|
})
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 01:52:32 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
// Logger.info('Sending file', fullFilePath)
|
|
|
|
res.sendFile(fullFilePath)
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 01:10:47 +01:00
|
|
|
module.exports = HlsRouter
|