import memoizee = require('memoizee')
interface MuxingSessionEvents {
- 'master-playlist-created': ({ videoId: number }) => void
+ 'master-playlist-created': (options: { videoId: number }) => void
- 'bad-socket-health': ({ videoId: number }) => void
- 'duration-exceeded': ({ videoId: number }) => void
- 'quota-exceeded': ({ videoId: number }) => void
+ 'bad-socket-health': (options: { videoId: number }) => void
+ 'duration-exceeded': (options: { videoId: number }) => void
+ 'quota-exceeded': (options: { videoId: number }) => void
- 'ffmpeg-end': ({ videoId: number }) => void
- 'ffmpeg-error': ({ videoId: string }) => void
+ 'ffmpeg-end': (options: { videoId: number }) => void
+ 'ffmpeg-error': (options: { videoId: number }) => void
- 'after-cleanup': ({ videoId: number }) => void
+ 'after-cleanup': (options: { videoId: number }) => void
}
declare interface MuxingSession {
private readonly bitrate: number
private readonly ratio: number
+ private readonly hasAudio: boolean
+
private readonly videoId: number
private readonly videoUUID: string
private readonly saveReplay: boolean
private tsWatcher: FSWatcher
private masterWatcher: FSWatcher
+ private aborted = false
+
private readonly isAbleToUploadVideoWithCache = memoizee((userId: number) => {
return isAbleToUploadVideo(userId, 1000)
}, { maxAge: MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD })
bitrate: number
ratio: number
allResolutions: number[]
+ hasAudio: boolean
}) {
super()
this.bitrate = options.bitrate
this.ratio = options.ratio
+ this.hasAudio = options.hasAudio
+
this.allResolutions = options.allResolutions
this.videoId = this.videoLive.Video.id
bitrate: this.bitrate,
ratio: this.ratio,
+ hasAudio: this.hasAudio,
+
availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
profile: CONFIG.LIVE.TRANSCODING.PROFILE
})
logger.info('Running live muxing/transcoding for %s.', this.videoUUID, this.lTags())
- this.watchTSFiles(this.outDirectory)
- this.watchMasterFile(this.outDirectory)
+ this.watchTSFiles()
+ this.watchMasterFile()
let ffmpegShellCommand: string
this.ffmpegCommand.on('start', cmdline => {
})
this.ffmpegCommand.on('error', (err, stdout, stderr) => {
- this.onFFmpegError({ err, stdout, stderr, outPath: this.outDirectory, ffmpegShellCommand })
+ this.onFFmpegError({ err, stdout, stderr, ffmpegShellCommand })
})
this.ffmpegCommand.on('end', () => {
this.emit('ffmpeg-end', ({ videoId: this.videoId }))
- this.onFFmpegEnded(this.outDirectory)
+ this.onFFmpegEnded()
})
this.ffmpegCommand.run()
abort () {
if (!this.ffmpegCommand) return
+ this.aborted = true
this.ffmpegCommand.kill('SIGINT')
}
err: any
stdout: string
stderr: string
- outPath: string
ffmpegShellCommand: string
}) {
- const { err, stdout, stderr, outPath, ffmpegShellCommand } = options
+ const { err, stdout, stderr, ffmpegShellCommand } = options
- this.onFFmpegEnded(outPath)
+ this.onFFmpegEnded()
// Don't care that we killed the ffmpeg process
if (err?.message?.includes('Exiting normally')) return
this.emit('ffmpeg-error', ({ videoId: this.videoId }))
}
- private onFFmpegEnded (outPath: string) {
+ private onFFmpegEnded () {
logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', this.inputUrl, this.lTags())
setTimeout(() => {
.then(() => {
// Process remaining segments hash
for (const key of Object.keys(this.segmentsToProcessPerPlaylist)) {
- this.processSegments(outPath, this.segmentsToProcessPerPlaylist[key])
+ this.processSegments(this.segmentsToProcessPerPlaylist[key])
}
})
.catch(err => {
logger.error(
- 'Cannot close watchers of %s or process remaining hash segments.', outPath,
+ 'Cannot close watchers of %s or process remaining hash segments.', this.outDirectory,
{ err, ...this.lTags() }
)
})
}, 1000)
}
- private watchMasterFile (outPath: string) {
- this.masterWatcher = watch(outPath + '/' + this.streamingPlaylist.playlistFilename)
+ private watchMasterFile () {
+ this.masterWatcher = watch(this.outDirectory + '/' + this.streamingPlaylist.playlistFilename)
this.masterWatcher.on('add', () => {
this.emit('master-playlist-created', { videoId: this.videoId })
this.masterWatcher.close()
- .catch(err => logger.error('Cannot close master watcher of %s.', outPath, { err, ...this.lTags() }))
+ .catch(err => logger.error('Cannot close master watcher of %s.', this.outDirectory, { err, ...this.lTags() }))
})
}
- private watchTSFiles (outPath: string) {
+ private watchTSFiles () {
const startStreamDateTime = new Date().getTime()
- this.tsWatcher = watch(outPath + '/*.ts')
+ this.tsWatcher = watch(this.outDirectory + '/*.ts')
const playlistIdMatcher = /^([\d+])-/
const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
const segmentsToProcess = this.segmentsToProcessPerPlaylist[playlistId] || []
- this.processSegments(outPath, segmentsToProcess)
+ this.processSegments(segmentsToProcess)
this.segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
}
}
- const deleteHandler = segmentPath => LiveSegmentShaStore.Instance.removeSegmentSha(this.videoUUID, segmentPath)
+ const deleteHandler = (segmentPath: string) => LiveSegmentShaStore.Instance.removeSegmentSha(this.videoUUID, segmentPath)
this.tsWatcher.on('add', p => addHandler(p))
this.tsWatcher.on('unlink', p => deleteHandler(p))
private async isQuotaExceeded (segmentPath: string) {
if (this.saveReplay !== true) return false
+ if (this.aborted) return false
try {
const segmentStat = await stat(segmentPath)
return now <= max
}
- private processSegments (hlsVideoPath: string, segmentPaths: string[]) {
+ private processSegments (segmentPaths: string[]) {
mapSeries(segmentPaths, async previousSegment => {
// Add sha hash of previous segments, because ffmpeg should have finished generating them
await LiveSegmentShaStore.Instance.addSegmentSha(this.videoUUID, previousSegment)
if (this.saveReplay) {
- await this.addSegmentToReplay(hlsVideoPath, previousSegment)
+ await this.addSegmentToReplay(previousSegment)
}
- }).catch(err => logger.error('Cannot process segments in %s', hlsVideoPath, { err, ...this.lTags() }))
+ }).catch(err => {
+ if (this.aborted) return
+
+ logger.error('Cannot process segments', { err, ...this.lTags() })
+ })
}
private hasClientSocketInBadHealth (sessionId: string) {
return false
}
- private async addSegmentToReplay (hlsVideoPath: string, segmentPath: string) {
+ private async addSegmentToReplay (segmentPath: string) {
const segmentName = basename(segmentPath)
const dest = join(this.replayDirectory, buildConcatenatedName(segmentName))