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