]>
Commit | Line | Data |
---|---|---|
1 | import { Job } from 'bullmq' | |
2 | import { readdir, remove } from 'fs-extra' | |
3 | import { join } from 'path' | |
4 | import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo } from '@server/helpers/ffmpeg' | |
5 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' | |
6 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | |
7 | import { cleanupAndDestroyPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live' | |
8 | import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '@server/lib/paths' | |
9 | import { generateVideoMiniature } from '@server/lib/thumbnail' | |
10 | import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/transcoding' | |
11 | import { moveToNextState } from '@server/lib/video-state' | |
12 | import { VideoModel } from '@server/models/video/video' | |
13 | import { VideoBlacklistModel } from '@server/models/video/video-blacklist' | |
14 | import { VideoFileModel } from '@server/models/video/video-file' | |
15 | import { VideoLiveModel } from '@server/models/video/video-live' | |
16 | import { VideoLiveSessionModel } from '@server/models/video/video-live-session' | |
17 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | |
18 | import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models' | |
19 | import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models' | |
20 | import { logger, loggerTagsFactory } from '../../../helpers/logger' | |
21 | import { VideoPathManager } from '@server/lib/video-path-manager' | |
22 | import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting' | |
23 | ||
24 | const lTags = loggerTagsFactory('live', 'job') | |
25 | ||
26 | async 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 | ||
75 | export { | |
76 | processVideoLiveEnding | |
77 | } | |
78 | ||
79 | // --------------------------------------------------------------------------- | |
80 | ||
81 | async 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 | ||
145 | async 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 | ||
211 | async 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 | ||
222 | const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile) | |
223 | ||
224 | const probe = await ffprobePromise(concatenatedTsFilePath) | |
225 | const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe) | |
226 | const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe) | |
227 | ||
228 | try { | |
229 | await generateHlsPlaylistResolutionFromTS({ | |
230 | video, | |
231 | inputFileMutexReleaser, | |
232 | concatenatedTsFilePath, | |
233 | resolution, | |
234 | isAAC: audioStream?.codec_name === 'aac' | |
235 | }) | |
236 | } catch (err) { | |
237 | logger.error('Cannot generate HLS playlist resolution from TS files.', { err }) | |
238 | } | |
239 | ||
240 | inputFileMutexReleaser() | |
241 | } | |
242 | ||
243 | return video | |
244 | } | |
245 | ||
246 | async function cleanupLiveAndFederate (options: { | |
247 | video: MVideo | |
248 | permanentLive: boolean | |
249 | streamingPlaylistId: number | |
250 | }) { | |
251 | const { permanentLive, video, streamingPlaylistId } = options | |
252 | ||
253 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId) | |
254 | ||
255 | if (streamingPlaylist) { | |
256 | if (permanentLive) { | |
257 | await cleanupAndDestroyPermanentLive(video, streamingPlaylist) | |
258 | } else { | |
259 | await cleanupUnsavedNormalLive(video, streamingPlaylist) | |
260 | } | |
261 | } | |
262 | ||
263 | try { | |
264 | const fullVideo = await VideoModel.loadFull(video.id) | |
265 | return federateVideoIfNeeded(fullVideo, false, undefined) | |
266 | } catch (err) { | |
267 | logger.warn('Cannot federate live after cleanup', { videoId: video.id, err }) | |
268 | } | |
269 | } |