]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/live/shared/muxing-session.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / live / shared / muxing-session.ts
index 22a47942a41c5dd0b236e392b7234d2b97a8ed40..98a7b26135859222c0b831e296c35507927f88fd 100644 (file)
@@ -5,14 +5,14 @@ import { FfmpegCommand } from 'fluent-ffmpeg'
 import { appendFile, ensureDir, readFile, stat } from 'fs-extra'
 import { basename, join } from 'path'
 import { EventEmitter } from 'stream'
-import { getLiveMuxingCommand, getLiveTranscodingCommand } from '@server/helpers/ffmpeg-utils'
+import { getLiveMuxingCommand, getLiveTranscodingCommand } from '@server/helpers/ffmpeg'
 import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger'
 import { CONFIG } from '@server/initializers/config'
 import { MEMOIZE_TTL, VIDEO_LIVE } from '@server/initializers/constants'
 import { VideoFileModel } from '@server/models/video/video-file'
 import { MStreamingPlaylistVideo, MUserId, MVideoLiveVideo } from '@server/types/models'
-import { getLiveDirectory } from '../../paths'
-import { VideoTranscodingProfilesManager } from '../../transcoding/video-transcoding-profiles'
+import { getLiveDirectory, getLiveReplayBaseDirectory } from '../../paths'
+import { VideoTranscodingProfilesManager } from '../../transcoding/default-transcoding-profiles'
 import { isAbleToUploadVideo } from '../../user'
 import { LiveQuotaStore } from '../live-quota-store'
 import { LiveSegmentShaStore } from '../live-segment-sha-store'
@@ -28,7 +28,7 @@ interface MuxingSessionEvents {
   'quota-exceeded': ({ videoId: number }) => void
 
   'ffmpeg-end': ({ videoId: number }) => void
-  'ffmpeg-error': ({ sessionId: string }) => void
+  'ffmpeg-error': ({ videoId: string }) => void
 
   'after-cleanup': ({ videoId: number }) => void
 }
@@ -63,6 +63,9 @@ class MuxingSession extends EventEmitter {
   private readonly videoUUID: string
   private readonly saveReplay: boolean
 
+  private readonly outDirectory: string
+  private readonly replayDirectory: string
+
   private readonly lTags: LoggerTagsFn
 
   private segmentsToProcessPerPlaylist: { [playlistId: string]: string[] } = {}
@@ -110,21 +113,26 @@ class MuxingSession extends EventEmitter {
 
     this.saveReplay = this.videoLive.saveReplay
 
+    this.outDirectory = getLiveDirectory(this.videoLive.Video)
+    this.replayDirectory = join(getLiveReplayBaseDirectory(this.videoLive.Video), new Date().toISOString())
+
     this.lTags = loggerTagsFactory('live', this.sessionId, this.videoUUID)
   }
 
   async runMuxing () {
     this.createFiles()
 
-    const outPath = await this.prepareDirectories()
+    await this.prepareDirectories()
 
     this.ffmpegCommand = CONFIG.LIVE.TRANSCODING.ENABLED
       ? await getLiveTranscodingCommand({
         inputUrl: this.inputUrl,
 
-        outPath,
+        outPath: this.outDirectory,
         masterPlaylistName: this.streamingPlaylist.playlistFilename,
 
+        latencyMode: this.videoLive.latencyMode,
+
         resolutions: this.allResolutions,
         fps: this.fps,
         bitrate: this.bitrate,
@@ -133,12 +141,17 @@ class MuxingSession extends EventEmitter {
         availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
         profile: CONFIG.LIVE.TRANSCODING.PROFILE
       })
-      : getLiveMuxingCommand(this.inputUrl, outPath, this.streamingPlaylist.playlistFilename)
+      : getLiveMuxingCommand({
+        inputUrl: this.inputUrl,
+        outPath: this.outDirectory,
+        masterPlaylistName: this.streamingPlaylist.playlistFilename,
+        latencyMode: this.videoLive.latencyMode
+      })
 
     logger.info('Running live muxing/transcoding for %s.', this.videoUUID, this.lTags())
 
-    this.watchTSFiles(outPath)
-    this.watchMasterFile(outPath)
+    this.watchTSFiles()
+    this.watchMasterFile()
 
     let ffmpegShellCommand: string
     this.ffmpegCommand.on('start', cmdline => {
@@ -148,10 +161,14 @@ class MuxingSession extends EventEmitter {
     })
 
     this.ffmpegCommand.on('error', (err, stdout, stderr) => {
-      this.onFFmpegError({ err, stdout, stderr, outPath, ffmpegShellCommand })
+      this.onFFmpegError({ err, stdout, stderr, ffmpegShellCommand })
     })
 
-    this.ffmpegCommand.on('end', () => this.onFFmpegEnded(outPath))
+    this.ffmpegCommand.on('end', () => {
+      this.emit('ffmpeg-end', ({ videoId: this.videoId }))
+
+      this.onFFmpegEnded()
+    })
 
     this.ffmpegCommand.run()
   }
@@ -172,22 +189,21 @@ class MuxingSession extends EventEmitter {
     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
 
     logger.error('Live transcoding error.', { err, stdout, stderr, ffmpegShellCommand, ...this.lTags() })
 
-    this.emit('ffmpeg-error', ({ sessionId: this.sessionId }))
+    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(() => {
@@ -197,12 +213,12 @@ class MuxingSession extends EventEmitter {
         .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() }
           )
         })
@@ -211,21 +227,21 @@ class MuxingSession extends EventEmitter {
     }, 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+])-/
 
@@ -235,7 +251,7 @@ class MuxingSession extends EventEmitter {
       const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
 
       const segmentsToProcess = this.segmentsToProcessPerPlaylist[playlistId] || []
-      this.processSegments(outPath, segmentsToProcess)
+      this.processSegments(segmentsToProcess)
 
       this.segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
 
@@ -256,7 +272,7 @@ class MuxingSession extends EventEmitter {
       }
     }
 
-    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))
@@ -297,16 +313,11 @@ class MuxingSession extends EventEmitter {
   }
 
   private async prepareDirectories () {
-    const outPath = getLiveDirectory(this.videoLive.Video)
-    await ensureDir(outPath)
-
-    const replayDirectory = join(outPath, VIDEO_LIVE.REPLAY_DIRECTORY)
+    await ensureDir(this.outDirectory)
 
     if (this.videoLive.saveReplay === true) {
-      await ensureDir(replayDirectory)
+      await ensureDir(this.replayDirectory)
     }
-
-    return outPath
   }
 
   private isDurationConstraintValid (streamingStartTime: number) {
@@ -320,15 +331,15 @@ class MuxingSession extends EventEmitter {
     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 => logger.error('Cannot process segments', { err, ...this.lTags() }))
   }
 
   private hasClientSocketInBadHealth (sessionId: string) {
@@ -355,9 +366,9 @@ class MuxingSession extends EventEmitter {
     return false
   }
 
-  private async addSegmentToReplay (hlsVideoPath: string, segmentPath: string) {
+  private async addSegmentToReplay (segmentPath: string) {
     const segmentName = basename(segmentPath)
-    const dest = join(hlsVideoPath, VIDEO_LIVE.REPLAY_DIRECTORY, buildConcatenatedName(segmentName))
+    const dest = join(this.replayDirectory, buildConcatenatedName(segmentName))
 
     try {
       const data = await readFile(segmentPath)