X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Flive-manager.ts;h=d63e79dfc6edb8055aa98ba8bfb9ee32e4127840;hb=992f498e312efcde46f818ba719b50f1f958272f;hp=fe5b333220bc73d278f5900adf33808ea467d781;hpb=5c0904fc664e3eb04ac75a9430c1297c2a14f853;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/live-manager.ts b/server/lib/live-manager.ts index fe5b33322..d63e79dfc 100644 --- a/server/lib/live-manager.ts +++ b/server/lib/live-manager.ts @@ -4,13 +4,8 @@ import { FfmpegCommand } from 'fluent-ffmpeg' import { ensureDir, stat } from 'fs-extra' import { basename } from 'path' import { isTestInstance } from '@server/helpers/core-utils' -import { - computeResolutionsToTranscode, - getVideoFileFPS, - getVideoFileResolution, - runLiveMuxing, - runLiveTranscoding -} from '@server/helpers/ffmpeg-utils' +import { getLiveMuxingCommand, getLiveTranscodingCommand } from '@server/helpers/ffmpeg-utils' +import { computeResolutionsToTranscode, getVideoFileFPS, getVideoFileResolution } from '@server/helpers/ffprobe-utils' import { logger } from '@server/helpers/logger' import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config' import { MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE, VIEW_LIFETIME, WEBSERVER } from '@server/initializers/constants' @@ -27,6 +22,7 @@ import { JobQueue } from './job-queue' import { PeerTubeSocket } from './peertube-socket' import { isAbleToUploadVideo } from './user' import { getHLSDirectory } from './video-paths' +import { availableEncoders } from './video-transcoding-profiles' import memoizee = require('memoizee') const NodeRtmpServer = require('node-media-server/node_rtmp_server') @@ -269,8 +265,16 @@ class LiveManager { const deleteSegments = videoLive.saveReplay === false const ffmpegExec = CONFIG.LIVE.TRANSCODING.ENABLED - ? runLiveTranscoding(rtmpUrl, outPath, allResolutions, fps, deleteSegments) - : runLiveMuxing(rtmpUrl, outPath, deleteSegments) + ? await getLiveTranscodingCommand({ + rtmpUrl, + outPath, + resolutions: allResolutions, + fps, + deleteSegments, + availableEncoders, + profile: 'default' + }) + : getLiveMuxingCommand(rtmpUrl, outPath, deleteSegments) logger.info('Running live muxing/transcoding for %s.', videoUUID) this.transSessions.set(sessionId, ffmpegExec) @@ -280,15 +284,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 +362,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) => { @@ -371,6 +391,8 @@ class LiveManager { }) ffmpegExec.on('end', () => onFFmpegEnded()) + + ffmpegExec.run() } private async onEndTransmuxing (videoId: number, cleanupNow = false) {