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,
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)
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 })
}) {
const { liveVideo, liveSession, publishedAt, replayDirectory } = options
- await cleanupTMPLiveFiles(getLiveDirectory(liveVideo))
-
const video = new VideoModel({
name: `${liveVideo.name} - ${new Date(publishedAt).toLocaleString()}`,
isLive: false,
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) {
}
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 })
+ }
}