]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-live-ending.ts
Fix fast restream in saved permanent live
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
index 55fd09344135bf4a2b5a7e0a7e8af8472c887898..450bda2fdf97be765150bc4ef8ce8c2dd72bd4ae 100644 (file)
@@ -1,10 +1,10 @@
 import { Job } from 'bull'
-import { pathExists, readdir, remove } from 'fs-extra'
+import { readdir, remove } from 'fs-extra'
 import { join } from 'path'
 import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo, getVideoStreamDuration } from '@server/helpers/ffmpeg'
 import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
-import { cleanupLive, LiveSegmentShaStore } from '@server/lib/live'
+import { cleanupUnsavedNormalLive, cleanupPermanentLive, cleanupTMPLiveFiles, LiveSegmentShaStore } from '@server/lib/live'
 import {
   generateHLSMasterPlaylistFilename,
   generateHlsSha256SegmentsFilename,
@@ -22,15 +22,17 @@ import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
 import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models'
 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
-import { logger } from '../../../helpers/logger'
+import { logger, loggerTagsFactory } from '../../../helpers/logger'
+
+const lTags = loggerTagsFactory('live', 'job')
 
 async function processVideoLiveEnding (job: Job) {
   const payload = job.data as VideoLiveEndingPayload
 
-  logger.info('Processing video live ending for %s.', payload.videoId, { payload })
+  logger.info('Processing video live ending for %s.', payload.videoId, { payload, ...lTags() })
 
   function logError () {
-    logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
+    logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId, lTags())
   }
 
   const liveVideo = await VideoModel.load(payload.videoId)
@@ -45,13 +47,13 @@ async function processVideoLiveEnding (job: Job) {
   LiveSegmentShaStore.Instance.cleanupShaSegments(liveVideo.uuid)
 
   if (live.saveReplay !== true) {
-    return cleanupLiveAndFederate({ liveVideo })
+    return cleanupLiveAndFederate({ live, video: liveVideo, streamingPlaylistId: payload.streamingPlaylistId })
   }
 
   if (live.permanentLive) {
     await saveReplayToExternalVideo({ liveVideo, liveSession, publishedAt: payload.publishedAt, replayDirectory: payload.replayDirectory })
 
-    return cleanupLiveAndFederate({ liveVideo })
+    return cleanupLiveAndFederate({ live, video: liveVideo, streamingPlaylistId: payload.streamingPlaylistId })
   }
 
   return replaceLiveByReplay({ liveVideo, live, liveSession, replayDirectory: payload.replayDirectory })
@@ -73,8 +75,6 @@ async function saveReplayToExternalVideo (options: {
 }) {
   const { liveVideo, liveSession, publishedAt, replayDirectory } = options
 
-  await cleanupTMPLiveFiles(getLiveDirectory(liveVideo))
-
   const video = new VideoModel({
     name: `${liveVideo.name} - ${new Date(publishedAt).toLocaleString()}`,
     isLive: false,
@@ -164,7 +164,11 @@ async function replaceLiveByReplay (options: {
 
   await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory })
 
-  await remove(getLiveReplayBaseDirectory(videoWithFiles))
+  if (live.permanentLive) { // Remove session replay
+    await remove(replayDirectory)
+  } else { // We won't stream again in this live, we can delete the base replay directory
+    await remove(getLiveReplayBaseDirectory(videoWithFiles))
+  }
 
   // Regenerate the thumbnail & preview?
   if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
@@ -227,34 +231,26 @@ async function assignReplayFilesToVideo (options: {
 }
 
 async function cleanupLiveAndFederate (options: {
-  liveVideo: MVideo
+  live: MVideoLive
+  video: MVideo
+  streamingPlaylistId: number
 }) {
-  const { liveVideo } = options
+  const { live, video, streamingPlaylistId } = options
 
-  const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(liveVideo.id)
-  await cleanupLive(liveVideo, streamingPlaylist)
+  const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId)
 
-  const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(liveVideo.id)
-  return federateVideoIfNeeded(fullVideo, false, undefined)
-}
-
-async function cleanupTMPLiveFiles (hlsDirectory: string) {
-  if (!await pathExists(hlsDirectory)) return
-
-  const files = await readdir(hlsDirectory)
-
-  for (const filename of files) {
-    if (
-      filename.endsWith('.ts') ||
-      filename.endsWith('.m3u8') ||
-      filename.endsWith('.mpd') ||
-      filename.endsWith('.m4s') ||
-      filename.endsWith('.tmp')
-    ) {
-      const p = join(hlsDirectory, filename)
-
-      remove(p)
-        .catch(err => logger.error('Cannot remove %s.', p, { err }))
+  if (streamingPlaylist) {
+    if (live.permanentLive) {
+      await cleanupPermanentLive(video, streamingPlaylist)
+    } else {
+      await cleanupUnsavedNormalLive(video, streamingPlaylist)
     }
   }
+
+  try {
+    const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
+    return federateVideoIfNeeded(fullVideo, false, undefined)
+  } catch (err) {
+    logger.warn('Cannot federate live after cleanup', { videoId: video.id, err })
+  }
 }