]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-live-ending.ts
Live supports object storage
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
CommitLineData
5a921e7b 1import { Job } from 'bullmq'
5333788c 2import { readdir, remove } from 'fs-extra'
a5cf76af 3import { join } from 'path'
e4fc3697 4import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo } from '@server/helpers/ffmpeg'
4ec52d04
C
5import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
6import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
cfd57d2c 7import { cleanupAndDestroyPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live'
92083e42 8import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '@server/lib/paths'
daf6e480 9import { generateVideoMiniature } from '@server/lib/thumbnail'
c729caf6 10import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/transcoding'
0305db28 11import { moveToNextState } from '@server/lib/video-state'
a5cf76af 12import { VideoModel } from '@server/models/video/video'
26e3e98f 13import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
68e70a74 14import { VideoFileModel } from '@server/models/video/video-file'
b5b68755 15import { VideoLiveModel } from '@server/models/video/video-live'
26e3e98f 16import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
a5cf76af 17import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
26e3e98f 18import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models'
053aed43 19import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
53023be3
C
20import { logger, loggerTagsFactory } from '../../../helpers/logger'
21
22const lTags = loggerTagsFactory('live', 'job')
a5cf76af 23
41fb13c3 24async function processVideoLiveEnding (job: Job) {
a5cf76af
C
25 const payload = job.data as VideoLiveEndingPayload
26
53023be3 27 logger.info('Processing video live ending for %s.', payload.videoId, { payload, ...lTags() })
4ec52d04 28
68e70a74 29 function logError () {
53023be3 30 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId, lTags())
68e70a74
C
31 }
32
c8fa571f 33 const video = await VideoModel.load(payload.videoId)
b5b68755 34 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
26e3e98f 35 const liveSession = await VideoLiveSessionModel.load(payload.liveSessionId)
b5b68755 36
c8fa571f
C
37 const permanentLive = live.permanentLive
38
39 if (!video || !live || !liveSession) {
68e70a74
C
40 logError()
41 return
42 }
43
c8fa571f
C
44 liveSession.endingProcessed = true
45 await liveSession.save()
46
47 if (liveSession.saveReplay !== true) {
48 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
b5b68755
C
49 }
50
c8fa571f
C
51 if (permanentLive) {
52 await saveReplayToExternalVideo({
53 liveVideo: video,
54 liveSession,
55 publishedAt: payload.publishedAt,
56 replayDirectory: payload.replayDirectory
57 })
4ec52d04 58
c8fa571f 59 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
4ec52d04
C
60 }
61
c8fa571f 62 return replaceLiveByReplay({ video, liveSession, live, permanentLive, replayDirectory: payload.replayDirectory })
b5b68755
C
63}
64
65// ---------------------------------------------------------------------------
66
67export {
8ebf2a5d 68 processVideoLiveEnding
b5b68755
C
69}
70
71// ---------------------------------------------------------------------------
72
26e3e98f
C
73async function saveReplayToExternalVideo (options: {
74 liveVideo: MVideo
75 liveSession: MVideoLiveSession
76 publishedAt: string
77 replayDirectory: string
78}) {
79 const { liveVideo, liveSession, publishedAt, replayDirectory } = options
80
c8fa571f 81 const replayVideo = new VideoModel({
4ec52d04
C
82 name: `${liveVideo.name} - ${new Date(publishedAt).toLocaleString()}`,
83 isLive: false,
84 state: VideoState.TO_TRANSCODE,
85 duration: 0,
86
87 remote: liveVideo.remote,
88 category: liveVideo.category,
89 licence: liveVideo.licence,
90 language: liveVideo.language,
91 commentsEnabled: liveVideo.commentsEnabled,
92 downloadEnabled: liveVideo.downloadEnabled,
26e3e98f 93 waitTranscoding: true,
4ec52d04
C
94 nsfw: liveVideo.nsfw,
95 description: liveVideo.description,
96 support: liveVideo.support,
97 privacy: liveVideo.privacy,
98 channelId: liveVideo.channelId
99 }) as MVideoWithAllFiles
100
c8fa571f
C
101 replayVideo.Thumbnails = []
102 replayVideo.VideoFiles = []
103 replayVideo.VideoStreamingPlaylists = []
4ec52d04 104
c8fa571f 105 replayVideo.url = getLocalVideoActivityPubUrl(replayVideo)
4ec52d04 106
c8fa571f 107 await replayVideo.save()
4ec52d04 108
c8fa571f 109 liveSession.replayVideoId = replayVideo.id
26e3e98f
C
110 await liveSession.save()
111
4ec52d04
C
112 // If live is blacklisted, also blacklist the replay
113 const blacklist = await VideoBlacklistModel.loadByVideoId(liveVideo.id)
114 if (blacklist) {
115 await VideoBlacklistModel.create({
c8fa571f 116 videoId: replayVideo.id,
4ec52d04
C
117 unfederated: blacklist.unfederated,
118 reason: blacklist.reason,
119 type: blacklist.type
120 })
121 }
122
c8fa571f 123 await assignReplayFilesToVideo({ video: replayVideo, replayDirectory })
937581b8 124
4ec52d04
C
125 await remove(replayDirectory)
126
127 for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) {
c8fa571f
C
128 const image = await generateVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type })
129 await replayVideo.addAndSaveThumbnail(image)
4ec52d04 130 }
937581b8 131
c8fa571f 132 await moveToNextState({ video: replayVideo, isNewVideo: true })
4ec52d04 133}
937581b8 134
26e3e98f 135async function replaceLiveByReplay (options: {
c8fa571f 136 video: MVideo
26e3e98f
C
137 liveSession: MVideoLiveSession
138 live: MVideoLive
c8fa571f 139 permanentLive: boolean
26e3e98f
C
140 replayDirectory: string
141}) {
c8fa571f 142 const { video, liveSession, live, permanentLive, replayDirectory } = options
26e3e98f 143
cfd57d2c
C
144 const videoWithFiles = await VideoModel.loadFull(video.id)
145 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
146
147 await cleanupTMPLiveFiles(videoWithFiles, hlsPlaylist)
b5b68755 148
31c82cd9
C
149 await live.destroy()
150
cfd57d2c
C
151 videoWithFiles.isLive = false
152 videoWithFiles.waitTranscoding = true
153 videoWithFiles.state = VideoState.TO_TRANSCODE
d846d99c 154
cfd57d2c 155 await videoWithFiles.save()
26e3e98f 156
cfd57d2c 157 liveSession.replayVideoId = videoWithFiles.id
26e3e98f 158 await liveSession.save()
b5b68755 159
97969c4e 160 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
764b1a14
C
161
162 // Reset playlist
97969c4e 163 hlsPlaylist.VideoFiles = []
764b1a14
C
164 hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename()
165 hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename()
166 await hlsPlaylist.save()
97969c4e 167
26e3e98f 168 await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory })
4ec52d04 169
c8fa571f 170 if (permanentLive) { // Remove session replay
5333788c
C
171 await remove(replayDirectory)
172 } else { // We won't stream again in this live, we can delete the base replay directory
173 await remove(getLiveReplayBaseDirectory(videoWithFiles))
174 }
4ec52d04
C
175
176 // Regenerate the thumbnail & preview?
177 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
178 const miniature = await generateVideoMiniature({
179 video: videoWithFiles,
180 videoFile: videoWithFiles.getMaxQualityFile(),
181 type: ThumbnailType.MINIATURE
182 })
26e3e98f 183 await videoWithFiles.addAndSaveThumbnail(miniature)
4ec52d04
C
184 }
185
186 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
187 const preview = await generateVideoMiniature({
188 video: videoWithFiles,
189 videoFile: videoWithFiles.getMaxQualityFile(),
190 type: ThumbnailType.PREVIEW
191 })
26e3e98f 192 await videoWithFiles.addAndSaveThumbnail(preview)
4ec52d04
C
193 }
194
26e3e98f
C
195 // We consider this is a new video
196 await moveToNextState({ video: videoWithFiles, isNewVideo: true })
4ec52d04
C
197}
198
26e3e98f
C
199async function assignReplayFilesToVideo (options: {
200 video: MVideo
201 replayDirectory: string
202}) {
203 const { video, replayDirectory } = options
204
4ec52d04
C
205 const concatenatedTsFiles = await readdir(replayDirectory)
206
207 for (const concatenatedTsFile of concatenatedTsFiles) {
3851e732 208 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
2650d6d4 209
e772bdf1
C
210 const probe = await ffprobePromise(concatenatedTsFilePath)
211 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
84cae54e 212 const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe)
2650d6d4 213
e4fc3697 214 await generateHlsPlaylistResolutionFromTS({
4ec52d04 215 video,
3851e732 216 concatenatedTsFilePath,
679c12e6 217 resolution,
e772bdf1 218 isAAC: audioStream?.codec_name === 'aac'
b5b68755 219 })
97969c4e 220 }
d846d99c 221
4ec52d04
C
222 return video
223}
053aed43 224
26e3e98f 225async function cleanupLiveAndFederate (options: {
5333788c 226 video: MVideo
c8fa571f 227 permanentLive: boolean
cdd83816 228 streamingPlaylistId: number
26e3e98f 229}) {
c8fa571f 230 const { permanentLive, video, streamingPlaylistId } = options
bb4ba6d9 231
cdd83816 232 const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId)
a5cf76af 233
cdd83816 234 if (streamingPlaylist) {
c8fa571f 235 if (permanentLive) {
cfd57d2c 236 await cleanupAndDestroyPermanentLive(video, streamingPlaylist)
cdd83816 237 } else {
53023be3 238 await cleanupUnsavedNormalLive(video, streamingPlaylist)
cdd83816 239 }
a5cf76af 240 }
5333788c 241
c8fdfab0 242 try {
4fae2b1f 243 const fullVideo = await VideoModel.loadFull(video.id)
c8fdfab0
C
244 return federateVideoIfNeeded(fullVideo, false, undefined)
245 } catch (err) {
246 logger.warn('Cannot federate live after cleanup', { videoId: video.id, err })
247 }
a5cf76af 248}