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