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