From 6bff8ce23ac9a2de8c6ddcea9df5f7bd2b653156 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 17 Nov 2020 12:09:25 +0100 Subject: [PATCH] Process remaining segment hashes on live ending --- server/lib/live-manager.ts | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/server/lib/live-manager.ts b/server/lib/live-manager.ts index fe5b33322..feb6c5275 100644 --- a/server/lib/live-manager.ts +++ b/server/lib/live-manager.ts @@ -280,15 +280,21 @@ class LiveManager { const segmentsToProcessPerPlaylist: { [playlistId: string]: string[] } = {} const playlistIdMatcher = /^([\d+])-/ - const addHandler = segmentPath => { - const playlistId = basename(segmentPath).match(playlistIdMatcher)[0] - const segmentsToProcess = segmentsToProcessPerPlaylist[playlistId] || [] - + const processHashSegments = (segmentsToProcess: string[]) => { // Add sha hash of previous segments, because ffmpeg should have finished generating them for (const previousSegment of segmentsToProcess) { this.addSegmentSha(videoUUID, previousSegment) .catch(err => logger.error('Cannot add sha segment of video %s -> %s.', videoUUID, previousSegment, { err })) } + } + + const addHandler = segmentPath => { + logger.debug('Live add handler of %s.', segmentPath) + + const playlistId = basename(segmentPath).match(playlistIdMatcher)[0] + + const segmentsToProcess = segmentsToProcessPerPlaylist[playlistId] || [] + processHashSegments(segmentsToProcess) segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ] @@ -352,11 +358,21 @@ class LiveManager { this.transSessions.delete(sessionId) this.watchersPerVideo.delete(videoLive.videoId) - Promise.all([ tsWatcher.close(), masterWatcher.close() ]) - .catch(err => logger.error('Cannot close watchers of %s.', outPath, { err })) - - this.onEndTransmuxing(videoLive.Video.id) - .catch(err => logger.error('Error in closed transmuxing.', { err })) + setTimeout(() => { + // Wait latest segments generation, and close watchers + + Promise.all([ tsWatcher.close(), masterWatcher.close() ]) + .then(() => { + // Process remaining segments hash + for (const key of Object.keys(segmentsToProcessPerPlaylist)) { + processHashSegments(segmentsToProcessPerPlaylist[key]) + } + }) + .catch(err => logger.error('Cannot close watchers of %s or process remaining hash segments.', outPath, { err })) + + this.onEndTransmuxing(videoLive.Video.id) + .catch(err => logger.error('Error in closed transmuxing.', { err })) + }, 1000) } ffmpegExec.on('error', (err, stdout, stderr) => { -- 2.41.0