]>
Commit | Line | Data |
---|---|---|
5a921e7b | 1 | import { Job } from 'bullmq' |
5333788c | 2 | import { readdir, remove } from 'fs-extra' |
a5cf76af | 3 | import { join } from 'path' |
4ec52d04 C |
4 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' |
5 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | |
cfd57d2c | 6 | import { cleanupAndDestroyPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live' |
92083e42 | 7 | import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '@server/lib/paths' |
daf6e480 | 8 | import { generateVideoMiniature } from '@server/lib/thumbnail' |
0c9668f7 C |
9 | import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/hls-transcoding' |
10 | import { VideoPathManager } from '@server/lib/video-path-manager' | |
0305db28 | 11 | import { moveToNextState } from '@server/lib/video-state' |
a5cf76af | 12 | import { VideoModel } from '@server/models/video/video' |
26e3e98f | 13 | import { VideoBlacklistModel } from '@server/models/video/video-blacklist' |
68e70a74 | 14 | import { VideoFileModel } from '@server/models/video/video-file' |
b5b68755 | 15 | import { VideoLiveModel } from '@server/models/video/video-live' |
0c9668f7 | 16 | import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting' |
26e3e98f | 17 | import { VideoLiveSessionModel } from '@server/models/video/video-live-session' |
a5cf76af | 18 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
26e3e98f | 19 | import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models' |
0c9668f7 | 20 | import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '@shared/ffmpeg' |
053aed43 | 21 | import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models' |
53023be3 C |
22 | import { logger, loggerTagsFactory } from '../../../helpers/logger' |
23 | ||
24 | const lTags = loggerTagsFactory('live', 'job') | |
a5cf76af | 25 | |
41fb13c3 | 26 | async function processVideoLiveEnding (job: Job) { |
a5cf76af C |
27 | const payload = job.data as VideoLiveEndingPayload |
28 | ||
53023be3 | 29 | logger.info('Processing video live ending for %s.', payload.videoId, { payload, ...lTags() }) |
4ec52d04 | 30 | |
68e70a74 | 31 | function logError () { |
53023be3 | 32 | logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId, lTags()) |
68e70a74 C |
33 | } |
34 | ||
c8fa571f | 35 | const video = await VideoModel.load(payload.videoId) |
b5b68755 | 36 | const live = await VideoLiveModel.loadByVideoId(payload.videoId) |
26e3e98f | 37 | const liveSession = await VideoLiveSessionModel.load(payload.liveSessionId) |
b5b68755 | 38 | |
c8fa571f | 39 | if (!video || !live || !liveSession) { |
68e70a74 C |
40 | logError() |
41 | return | |
42 | } | |
43 | ||
e37ca6cb C |
44 | const permanentLive = live.permanentLive |
45 | ||
c8fa571f C |
46 | liveSession.endingProcessed = true |
47 | await liveSession.save() | |
48 | ||
49 | if (liveSession.saveReplay !== true) { | |
50 | return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId }) | |
b5b68755 C |
51 | } |
52 | ||
c8fa571f C |
53 | if (permanentLive) { |
54 | await saveReplayToExternalVideo({ | |
55 | liveVideo: video, | |
56 | liveSession, | |
57 | publishedAt: payload.publishedAt, | |
58 | replayDirectory: payload.replayDirectory | |
59 | }) | |
4ec52d04 | 60 | |
c8fa571f | 61 | return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId }) |
4ec52d04 C |
62 | } |
63 | ||
05a60d85 W |
64 | return replaceLiveByReplay({ |
65 | video, | |
66 | liveSession, | |
67 | live, | |
68 | permanentLive, | |
69 | replayDirectory: payload.replayDirectory | |
70 | }) | |
b5b68755 C |
71 | } |
72 | ||
73 | // --------------------------------------------------------------------------- | |
74 | ||
75 | export { | |
8ebf2a5d | 76 | processVideoLiveEnding |
b5b68755 C |
77 | } |
78 | ||
79 | // --------------------------------------------------------------------------- | |
80 | ||
26e3e98f C |
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 | ||
05a60d85 W |
89 | const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId) |
90 | ||
c8fa571f | 91 | const replayVideo = new VideoModel({ |
4ec52d04 C |
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, | |
26e3e98f | 103 | waitTranscoding: true, |
4ec52d04 C |
104 | nsfw: liveVideo.nsfw, |
105 | description: liveVideo.description, | |
106 | support: liveVideo.support, | |
05a60d85 | 107 | privacy: replaySettings.privacy, |
4ec52d04 C |
108 | channelId: liveVideo.channelId |
109 | }) as MVideoWithAllFiles | |
110 | ||
c8fa571f C |
111 | replayVideo.Thumbnails = [] |
112 | replayVideo.VideoFiles = [] | |
113 | replayVideo.VideoStreamingPlaylists = [] | |
4ec52d04 | 114 | |
c8fa571f | 115 | replayVideo.url = getLocalVideoActivityPubUrl(replayVideo) |
4ec52d04 | 116 | |
c8fa571f | 117 | await replayVideo.save() |
4ec52d04 | 118 | |
c8fa571f | 119 | liveSession.replayVideoId = replayVideo.id |
26e3e98f C |
120 | await liveSession.save() |
121 | ||
4ec52d04 C |
122 | // If live is blacklisted, also blacklist the replay |
123 | const blacklist = await VideoBlacklistModel.loadByVideoId(liveVideo.id) | |
124 | if (blacklist) { | |
125 | await VideoBlacklistModel.create({ | |
c8fa571f | 126 | videoId: replayVideo.id, |
4ec52d04 C |
127 | unfederated: blacklist.unfederated, |
128 | reason: blacklist.reason, | |
129 | type: blacklist.type | |
130 | }) | |
131 | } | |
132 | ||
c8fa571f | 133 | await assignReplayFilesToVideo({ video: replayVideo, replayDirectory }) |
937581b8 | 134 | |
4ec52d04 C |
135 | await remove(replayDirectory) |
136 | ||
137 | for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) { | |
c8fa571f C |
138 | const image = await generateVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type }) |
139 | await replayVideo.addAndSaveThumbnail(image) | |
4ec52d04 | 140 | } |
937581b8 | 141 | |
c8fa571f | 142 | await moveToNextState({ video: replayVideo, isNewVideo: true }) |
4ec52d04 | 143 | } |
937581b8 | 144 | |
26e3e98f | 145 | async function replaceLiveByReplay (options: { |
c8fa571f | 146 | video: MVideo |
26e3e98f C |
147 | liveSession: MVideoLiveSession |
148 | live: MVideoLive | |
c8fa571f | 149 | permanentLive: boolean |
26e3e98f C |
150 | replayDirectory: string |
151 | }) { | |
c8fa571f | 152 | const { video, liveSession, live, permanentLive, replayDirectory } = options |
26e3e98f | 153 | |
05a60d85 | 154 | const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId) |
cfd57d2c C |
155 | const videoWithFiles = await VideoModel.loadFull(video.id) |
156 | const hlsPlaylist = videoWithFiles.getHLSPlaylist() | |
157 | ||
158 | await cleanupTMPLiveFiles(videoWithFiles, hlsPlaylist) | |
b5b68755 | 159 | |
31c82cd9 C |
160 | await live.destroy() |
161 | ||
cfd57d2c | 162 | videoWithFiles.isLive = false |
05a60d85 | 163 | videoWithFiles.privacy = replaySettings.privacy |
cfd57d2c C |
164 | videoWithFiles.waitTranscoding = true |
165 | videoWithFiles.state = VideoState.TO_TRANSCODE | |
d846d99c | 166 | |
cfd57d2c | 167 | await videoWithFiles.save() |
26e3e98f | 168 | |
cfd57d2c | 169 | liveSession.replayVideoId = videoWithFiles.id |
26e3e98f | 170 | await liveSession.save() |
b5b68755 | 171 | |
97969c4e | 172 | await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) |
764b1a14 C |
173 | |
174 | // Reset playlist | |
97969c4e | 175 | hlsPlaylist.VideoFiles = [] |
764b1a14 C |
176 | hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename() |
177 | hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename() | |
178 | await hlsPlaylist.save() | |
97969c4e | 179 | |
26e3e98f | 180 | await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory }) |
4ec52d04 | 181 | |
c8fa571f | 182 | if (permanentLive) { // Remove session replay |
5333788c C |
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 | } | |
4ec52d04 C |
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 | }) | |
26e3e98f | 195 | await videoWithFiles.addAndSaveThumbnail(miniature) |
4ec52d04 C |
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 | }) | |
26e3e98f | 204 | await videoWithFiles.addAndSaveThumbnail(preview) |
4ec52d04 C |
205 | } |
206 | ||
26e3e98f C |
207 | // We consider this is a new video |
208 | await moveToNextState({ video: videoWithFiles, isNewVideo: true }) | |
4ec52d04 C |
209 | } |
210 | ||
26e3e98f C |
211 | async function assignReplayFilesToVideo (options: { |
212 | video: MVideo | |
213 | replayDirectory: string | |
214 | }) { | |
215 | const { video, replayDirectory } = options | |
216 | ||
4ec52d04 C |
217 | const concatenatedTsFiles = await readdir(replayDirectory) |
218 | ||
219 | for (const concatenatedTsFile of concatenatedTsFiles) { | |
3545e72c | 220 | const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid) |
a687879e | 221 | await video.reload() |
3545e72c | 222 | |
3851e732 | 223 | const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile) |
2650d6d4 | 224 | |
e772bdf1 C |
225 | const probe = await ffprobePromise(concatenatedTsFilePath) |
226 | const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe) | |
84cae54e | 227 | const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe) |
0c9668f7 | 228 | const fps = await getVideoStreamFPS(concatenatedTsFilePath, probe) |
2650d6d4 | 229 | |
3545e72c C |
230 | try { |
231 | await generateHlsPlaylistResolutionFromTS({ | |
232 | video, | |
233 | inputFileMutexReleaser, | |
234 | concatenatedTsFilePath, | |
235 | resolution, | |
0c9668f7 | 236 | fps, |
3545e72c C |
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() | |
97969c4e | 244 | } |
d846d99c | 245 | |
4ec52d04 C |
246 | return video |
247 | } | |
053aed43 | 248 | |
26e3e98f | 249 | async function cleanupLiveAndFederate (options: { |
5333788c | 250 | video: MVideo |
c8fa571f | 251 | permanentLive: boolean |
cdd83816 | 252 | streamingPlaylistId: number |
26e3e98f | 253 | }) { |
c8fa571f | 254 | const { permanentLive, video, streamingPlaylistId } = options |
bb4ba6d9 | 255 | |
cdd83816 | 256 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId) |
a5cf76af | 257 | |
cdd83816 | 258 | if (streamingPlaylist) { |
c8fa571f | 259 | if (permanentLive) { |
cfd57d2c | 260 | await cleanupAndDestroyPermanentLive(video, streamingPlaylist) |
cdd83816 | 261 | } else { |
53023be3 | 262 | await cleanupUnsavedNormalLive(video, streamingPlaylist) |
cdd83816 | 263 | } |
a5cf76af | 264 | } |
5333788c | 265 | |
c8fdfab0 | 266 | try { |
4fae2b1f | 267 | const fullVideo = await VideoModel.loadFull(video.id) |
c8fdfab0 C |
268 | return federateVideoIfNeeded(fullVideo, false, undefined) |
269 | } catch (err) { | |
270 | logger.warn('Cannot federate live after cleanup', { videoId: video.id, err }) | |
271 | } | |
a5cf76af | 272 | } |