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