X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fhandlers%2Fvideo-live-ending.ts;h=8a3ee09a2290210354bedfb8b2895fdadd3ab020;hb=e15bc9dcd59ebb6102d1455450d2e8bbf9f49c27;hp=1a58a9f7ee2c34d38ca91528ade36d252dd632e8;hpb=a5cf76afa378aae81af2a9b0ce548e5d2582f832;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/job-queue/handlers/video-live-ending.ts b/server/lib/job-queue/handlers/video-live-ending.ts index 1a58a9f7e..8a3ee09a2 100644 --- a/server/lib/job-queue/handlers/video-live-ending.ts +++ b/server/lib/job-queue/handlers/video-live-ending.ts @@ -1,43 +1,65 @@ -import * as Bull from 'bull' +import { Job } from 'bullmq' import { readdir, remove } from 'fs-extra' import { join } from 'path' -import { getHLSDirectory } from '@server/lib/video-paths' +import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo } from '@server/helpers/ffmpeg' +import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' +import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' +import { cleanupPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live' +import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '@server/lib/paths' +import { generateVideoMiniature } from '@server/lib/thumbnail' +import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/transcoding' +import { moveToNextState } from '@server/lib/video-state' import { VideoModel } from '@server/models/video/video' +import { VideoBlacklistModel } from '@server/models/video/video-blacklist' +import { VideoFileModel } from '@server/models/video/video-file' +import { VideoLiveModel } from '@server/models/video/video-live' +import { VideoLiveSessionModel } from '@server/models/video/video-live-session' import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' -import { VideoLiveEndingPayload } from '@shared/models' -import { logger } from '../../../helpers/logger' +import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models' +import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models' +import { logger, loggerTagsFactory } from '../../../helpers/logger' -async function processVideoLiveEnding (job: Bull.Job) { +const lTags = loggerTagsFactory('live', 'job') + +async function processVideoLiveEnding (job: Job) { const payload = job.data as VideoLiveEndingPayload - const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId) - if (!video) { - logger.warn('Video live %d does not exist anymore. Cannot cleanup.', payload.videoId) + 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, lTags()) + } + + const video = await VideoModel.load(payload.videoId) + const live = await VideoLiveModel.loadByVideoId(payload.videoId) + const liveSession = await VideoLiveSessionModel.load(payload.liveSessionId) + + const permanentLive = live.permanentLive + + if (!video || !live || !liveSession) { + logError() return } - const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) - const hlsDirectory = getHLSDirectory(video, false) + liveSession.endingProcessed = true + await liveSession.save() - const files = await readdir(hlsDirectory) + if (liveSession.saveReplay !== true) { + return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId }) + } - 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) + if (permanentLive) { + await saveReplayToExternalVideo({ + liveVideo: video, + liveSession, + publishedAt: payload.publishedAt, + replayDirectory: payload.replayDirectory + }) - remove(p) - .catch(err => logger.error('Cannot remove %s.', p, { err })) - } + return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId }) } - streamingPlaylist.destroy() - .catch(err => logger.error('Cannot remove live streaming playlist.', { err })) + return replaceLiveByReplay({ video, liveSession, live, permanentLive, replayDirectory: payload.replayDirectory }) } // --------------------------------------------------------------------------- @@ -45,3 +67,183 @@ async function processVideoLiveEnding (job: Bull.Job) { export { processVideoLiveEnding } + +// --------------------------------------------------------------------------- + +async function saveReplayToExternalVideo (options: { + liveVideo: MVideo + liveSession: MVideoLiveSession + publishedAt: string + replayDirectory: string +}) { + const { liveVideo, liveSession, publishedAt, replayDirectory } = options + + const replayVideo = new VideoModel({ + name: `${liveVideo.name} - ${new Date(publishedAt).toLocaleString()}`, + isLive: false, + state: VideoState.TO_TRANSCODE, + duration: 0, + + remote: liveVideo.remote, + category: liveVideo.category, + licence: liveVideo.licence, + language: liveVideo.language, + commentsEnabled: liveVideo.commentsEnabled, + downloadEnabled: liveVideo.downloadEnabled, + waitTranscoding: true, + nsfw: liveVideo.nsfw, + description: liveVideo.description, + support: liveVideo.support, + privacy: liveVideo.privacy, + channelId: liveVideo.channelId + }) as MVideoWithAllFiles + + replayVideo.Thumbnails = [] + replayVideo.VideoFiles = [] + replayVideo.VideoStreamingPlaylists = [] + + replayVideo.url = getLocalVideoActivityPubUrl(replayVideo) + + await replayVideo.save() + + liveSession.replayVideoId = replayVideo.id + await liveSession.save() + + // If live is blacklisted, also blacklist the replay + const blacklist = await VideoBlacklistModel.loadByVideoId(liveVideo.id) + if (blacklist) { + await VideoBlacklistModel.create({ + videoId: replayVideo.id, + unfederated: blacklist.unfederated, + reason: blacklist.reason, + type: blacklist.type + }) + } + + await assignReplayFilesToVideo({ video: replayVideo, replayDirectory }) + + await remove(replayDirectory) + + for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) { + const image = await generateVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type }) + await replayVideo.addAndSaveThumbnail(image) + } + + await moveToNextState({ video: replayVideo, isNewVideo: true }) +} + +async function replaceLiveByReplay (options: { + video: MVideo + liveSession: MVideoLiveSession + live: MVideoLive + permanentLive: boolean + replayDirectory: string +}) { + const { video, liveSession, live, permanentLive, replayDirectory } = options + + await cleanupTMPLiveFiles(video) + + await live.destroy() + + video.isLive = false + video.waitTranscoding = true + video.state = VideoState.TO_TRANSCODE + + await video.save() + + liveSession.replayVideoId = video.id + await liveSession.save() + + // Remove old HLS playlist video files + const videoWithFiles = await VideoModel.loadFull(video.id) + + const hlsPlaylist = videoWithFiles.getHLSPlaylist() + await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) + + // Reset playlist + hlsPlaylist.VideoFiles = [] + hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename() + hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename() + await hlsPlaylist.save() + + await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory }) + + if (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) { + const miniature = await generateVideoMiniature({ + video: videoWithFiles, + videoFile: videoWithFiles.getMaxQualityFile(), + type: ThumbnailType.MINIATURE + }) + await videoWithFiles.addAndSaveThumbnail(miniature) + } + + if (videoWithFiles.getPreview().automaticallyGenerated === true) { + const preview = await generateVideoMiniature({ + video: videoWithFiles, + videoFile: videoWithFiles.getMaxQualityFile(), + type: ThumbnailType.PREVIEW + }) + await videoWithFiles.addAndSaveThumbnail(preview) + } + + // We consider this is a new video + await moveToNextState({ video: videoWithFiles, isNewVideo: true }) +} + +async function assignReplayFilesToVideo (options: { + video: MVideo + replayDirectory: string +}) { + const { video, replayDirectory } = options + + const concatenatedTsFiles = await readdir(replayDirectory) + + for (const concatenatedTsFile of concatenatedTsFiles) { + const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile) + + const probe = await ffprobePromise(concatenatedTsFilePath) + const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe) + const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe) + + await generateHlsPlaylistResolutionFromTS({ + video, + concatenatedTsFilePath, + resolution, + isAAC: audioStream?.codec_name === 'aac' + }) + } + + return video +} + +async function cleanupLiveAndFederate (options: { + video: MVideo + permanentLive: boolean + streamingPlaylistId: number +}) { + const { permanentLive, video, streamingPlaylistId } = options + + const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId) + + if (streamingPlaylist) { + if (permanentLive) { + await cleanupPermanentLive(video, streamingPlaylist) + } else { + await cleanupUnsavedNormalLive(video, streamingPlaylist) + } + } + + try { + const fullVideo = await VideoModel.loadFull(video.id) + return federateVideoIfNeeded(fullVideo, false, undefined) + } catch (err) { + logger.warn('Cannot federate live after cleanup', { videoId: video.id, err }) + } +}