]>
Commit | Line | Data |
---|---|---|
1 | import { Job } from 'bull' | |
2 | import { TranscodeVODOptionsType } from '@server/helpers/ffmpeg' | |
3 | import { addTranscodingJob, getTranscodingJobPriority } from '@server/lib/video' | |
4 | import { VideoPathManager } from '@server/lib/video-path-manager' | |
5 | import { moveToFailedTranscodingState, moveToNextState } from '@server/lib/video-state' | |
6 | import { UserModel } from '@server/models/user/user' | |
7 | import { VideoJobInfoModel } from '@server/models/video/video-job-info' | |
8 | import { MUser, MUserId, MVideo, MVideoFullLight, MVideoWithFile } from '@server/types/models' | |
9 | import { pick } from '@shared/core-utils' | |
10 | import { | |
11 | HLSTranscodingPayload, | |
12 | MergeAudioTranscodingPayload, | |
13 | NewWebTorrentResolutionTranscodingPayload, | |
14 | OptimizeTranscodingPayload, | |
15 | VideoResolution, | |
16 | VideoTranscodingPayload | |
17 | } from '@shared/models' | |
18 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
19 | import { computeLowerResolutionsToTranscode } from '../../../helpers/ffmpeg' | |
20 | import { logger, loggerTagsFactory } from '../../../helpers/logger' | |
21 | import { CONFIG } from '../../../initializers/config' | |
22 | import { VideoModel } from '../../../models/video/video' | |
23 | import { | |
24 | generateHlsPlaylistResolution, | |
25 | mergeAudioVideofile, | |
26 | optimizeOriginalVideofile, | |
27 | transcodeNewWebTorrentResolution | |
28 | } from '../../transcoding/transcoding' | |
29 | ||
30 | type HandlerFunction = (job: Job, payload: VideoTranscodingPayload, video: MVideoFullLight, user: MUser) => Promise<void> | |
31 | ||
32 | const handlers: { [ id in VideoTranscodingPayload['type'] ]: HandlerFunction } = { | |
33 | 'new-resolution-to-hls': handleHLSJob, | |
34 | 'new-resolution-to-webtorrent': handleNewWebTorrentResolutionJob, | |
35 | 'merge-audio-to-webtorrent': handleWebTorrentMergeAudioJob, | |
36 | 'optimize-to-webtorrent': handleWebTorrentOptimizeJob | |
37 | } | |
38 | ||
39 | const lTags = loggerTagsFactory('transcoding') | |
40 | ||
41 | async function processVideoTranscoding (job: Job) { | |
42 | const payload = job.data as VideoTranscodingPayload | |
43 | logger.info('Processing transcoding job %d.', job.id, lTags(payload.videoUUID)) | |
44 | ||
45 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
46 | // No video, maybe deleted? | |
47 | if (!video) { | |
48 | logger.info('Do not process job %d, video does not exist.', job.id, lTags(payload.videoUUID)) | |
49 | return undefined | |
50 | } | |
51 | ||
52 | const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) | |
53 | ||
54 | const handler = handlers[payload.type] | |
55 | ||
56 | if (!handler) { | |
57 | await moveToFailedTranscodingState(video) | |
58 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') | |
59 | ||
60 | throw new Error('Cannot find transcoding handler for ' + payload.type) | |
61 | } | |
62 | ||
63 | try { | |
64 | await handler(job, payload, video, user) | |
65 | } catch (error) { | |
66 | await moveToFailedTranscodingState(video) | |
67 | ||
68 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') | |
69 | ||
70 | throw error | |
71 | } | |
72 | ||
73 | return video | |
74 | } | |
75 | ||
76 | // --------------------------------------------------------------------------- | |
77 | ||
78 | export { | |
79 | processVideoTranscoding | |
80 | } | |
81 | ||
82 | // --------------------------------------------------------------------------- | |
83 | // Job handlers | |
84 | // --------------------------------------------------------------------------- | |
85 | ||
86 | async function handleHLSJob (job: Job, payload: HLSTranscodingPayload, video: MVideoFullLight, user: MUser) { | |
87 | logger.info('Handling HLS transcoding job for %s.', video.uuid, lTags(video.uuid)) | |
88 | ||
89 | const videoFileInput = payload.copyCodecs | |
90 | ? video.getWebTorrentFile(payload.resolution) | |
91 | : video.getMaxQualityFile() | |
92 | ||
93 | const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist() | |
94 | ||
95 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFileInput.withVideoOrPlaylist(videoOrStreamingPlaylist), videoInputPath => { | |
96 | return generateHlsPlaylistResolution({ | |
97 | video, | |
98 | videoInputPath, | |
99 | resolution: payload.resolution, | |
100 | copyCodecs: payload.copyCodecs, | |
101 | isPortraitMode: payload.isPortraitMode || false, | |
102 | job | |
103 | }) | |
104 | }) | |
105 | ||
106 | logger.info('HLS transcoding job for %s ended.', video.uuid, lTags(video.uuid)) | |
107 | ||
108 | await onHlsPlaylistGeneration(video, user, payload) | |
109 | } | |
110 | ||
111 | async function handleNewWebTorrentResolutionJob ( | |
112 | job: Job, | |
113 | payload: NewWebTorrentResolutionTranscodingPayload, | |
114 | video: MVideoFullLight, | |
115 | user: MUserId | |
116 | ) { | |
117 | logger.info('Handling WebTorrent transcoding job for %s.', video.uuid, lTags(video.uuid)) | |
118 | ||
119 | await transcodeNewWebTorrentResolution(video, payload.resolution, payload.isPortraitMode || false, job) | |
120 | ||
121 | logger.info('WebTorrent transcoding job for %s ended.', video.uuid, lTags(video.uuid)) | |
122 | ||
123 | await onNewWebTorrentFileResolution(video, user, payload) | |
124 | } | |
125 | ||
126 | async function handleWebTorrentMergeAudioJob (job: Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) { | |
127 | logger.info('Handling merge audio transcoding job for %s.', video.uuid, lTags(video.uuid)) | |
128 | ||
129 | await mergeAudioVideofile(video, payload.resolution, job) | |
130 | ||
131 | logger.info('Merge audio transcoding job for %s ended.', video.uuid, lTags(video.uuid)) | |
132 | ||
133 | await onVideoFirstWebTorrentTranscoding(video, payload, 'video', user) | |
134 | } | |
135 | ||
136 | async function handleWebTorrentOptimizeJob (job: Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) { | |
137 | logger.info('Handling optimize transcoding job for %s.', video.uuid, lTags(video.uuid)) | |
138 | ||
139 | const { transcodeType } = await optimizeOriginalVideofile(video, video.getMaxQualityFile(), job) | |
140 | ||
141 | logger.info('Optimize transcoding job for %s ended.', video.uuid, lTags(video.uuid)) | |
142 | ||
143 | await onVideoFirstWebTorrentTranscoding(video, payload, transcodeType, user) | |
144 | } | |
145 | ||
146 | // --------------------------------------------------------------------------- | |
147 | ||
148 | async function onHlsPlaylistGeneration (video: MVideoFullLight, user: MUser, payload: HLSTranscodingPayload) { | |
149 | if (payload.isMaxQuality && payload.autoDeleteWebTorrentIfNeeded && CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) { | |
150 | // Remove webtorrent files if not enabled | |
151 | for (const file of video.VideoFiles) { | |
152 | await video.removeWebTorrentFileAndTorrent(file) | |
153 | await file.destroy() | |
154 | } | |
155 | ||
156 | video.VideoFiles = [] | |
157 | ||
158 | // Create HLS new resolution jobs | |
159 | await createLowerResolutionsJobs({ | |
160 | video, | |
161 | user, | |
162 | videoFileResolution: payload.resolution, | |
163 | isPortraitMode: payload.isPortraitMode, | |
164 | hasAudio: payload.hasAudio, | |
165 | isNewVideo: payload.isNewVideo ?? true, | |
166 | type: 'hls' | |
167 | }) | |
168 | } | |
169 | ||
170 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') | |
171 | await retryTransactionWrapper(moveToNextState, { video, isNewVideo: payload.isNewVideo }) | |
172 | } | |
173 | ||
174 | async function onVideoFirstWebTorrentTranscoding ( | |
175 | videoArg: MVideoWithFile, | |
176 | payload: OptimizeTranscodingPayload | MergeAudioTranscodingPayload, | |
177 | transcodeType: TranscodeVODOptionsType, | |
178 | user: MUserId | |
179 | ) { | |
180 | const { resolution, isPortraitMode, audioStream } = await videoArg.probeMaxQualityFile() | |
181 | ||
182 | // Maybe the video changed in database, refresh it | |
183 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid) | |
184 | // Video does not exist anymore | |
185 | if (!videoDatabase) return undefined | |
186 | ||
187 | // Generate HLS version of the original file | |
188 | const originalFileHLSPayload = { | |
189 | ...payload, | |
190 | ||
191 | isPortraitMode, | |
192 | hasAudio: !!audioStream, | |
193 | resolution: videoDatabase.getMaxQualityFile().resolution, | |
194 | // If we quick transcoded original file, force transcoding for HLS to avoid some weird playback issues | |
195 | copyCodecs: transcodeType !== 'quick-transcode', | |
196 | isMaxQuality: true | |
197 | } | |
198 | const hasHls = await createHlsJobIfEnabled(user, originalFileHLSPayload) | |
199 | const hasNewResolutions = await createLowerResolutionsJobs({ | |
200 | video: videoDatabase, | |
201 | user, | |
202 | videoFileResolution: resolution, | |
203 | hasAudio: !!audioStream, | |
204 | isPortraitMode, | |
205 | type: 'webtorrent', | |
206 | isNewVideo: payload.isNewVideo ?? true | |
207 | }) | |
208 | ||
209 | await VideoJobInfoModel.decrease(videoDatabase.uuid, 'pendingTranscode') | |
210 | ||
211 | // Move to next state if there are no other resolutions to generate | |
212 | if (!hasHls && !hasNewResolutions) { | |
213 | await retryTransactionWrapper(moveToNextState, { video: videoDatabase, isNewVideo: payload.isNewVideo }) | |
214 | } | |
215 | } | |
216 | ||
217 | async function onNewWebTorrentFileResolution ( | |
218 | video: MVideo, | |
219 | user: MUserId, | |
220 | payload: NewWebTorrentResolutionTranscodingPayload | MergeAudioTranscodingPayload | |
221 | ) { | |
222 | if (payload.createHLSIfNeeded) { | |
223 | await createHlsJobIfEnabled(user, { hasAudio: true, copyCodecs: true, isMaxQuality: false, ...payload }) | |
224 | } | |
225 | ||
226 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') | |
227 | ||
228 | await retryTransactionWrapper(moveToNextState, { video, isNewVideo: payload.isNewVideo }) | |
229 | } | |
230 | ||
231 | // --------------------------------------------------------------------------- | |
232 | ||
233 | async function createHlsJobIfEnabled (user: MUserId, payload: { | |
234 | videoUUID: string | |
235 | resolution: number | |
236 | hasAudio: boolean | |
237 | isPortraitMode?: boolean | |
238 | copyCodecs: boolean | |
239 | isMaxQuality: boolean | |
240 | isNewVideo?: boolean | |
241 | }) { | |
242 | if (!payload || CONFIG.TRANSCODING.ENABLED !== true || CONFIG.TRANSCODING.HLS.ENABLED !== true) return false | |
243 | ||
244 | const jobOptions = { | |
245 | priority: await getTranscodingJobPriority(user) | |
246 | } | |
247 | ||
248 | const hlsTranscodingPayload: HLSTranscodingPayload = { | |
249 | type: 'new-resolution-to-hls', | |
250 | autoDeleteWebTorrentIfNeeded: true, | |
251 | ||
252 | ...pick(payload, [ 'videoUUID', 'resolution', 'isPortraitMode', 'copyCodecs', 'isMaxQuality', 'isNewVideo', 'hasAudio' ]) | |
253 | } | |
254 | ||
255 | await addTranscodingJob(hlsTranscodingPayload, jobOptions) | |
256 | ||
257 | return true | |
258 | } | |
259 | ||
260 | async function createLowerResolutionsJobs (options: { | |
261 | video: MVideoFullLight | |
262 | user: MUserId | |
263 | videoFileResolution: number | |
264 | isPortraitMode: boolean | |
265 | hasAudio: boolean | |
266 | isNewVideo: boolean | |
267 | type: 'hls' | 'webtorrent' | |
268 | }) { | |
269 | const { video, user, videoFileResolution, isPortraitMode, isNewVideo, hasAudio, type } = options | |
270 | ||
271 | // Create transcoding jobs if there are enabled resolutions | |
272 | const resolutionsEnabled = computeLowerResolutionsToTranscode(videoFileResolution, 'vod') | |
273 | const resolutionCreated: string[] = [] | |
274 | ||
275 | for (const resolution of resolutionsEnabled) { | |
276 | if (resolution === VideoResolution.H_NOVIDEO && hasAudio === false) continue | |
277 | ||
278 | let dataInput: VideoTranscodingPayload | |
279 | ||
280 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED && type === 'webtorrent') { | |
281 | // WebTorrent will create subsequent HLS job | |
282 | dataInput = { | |
283 | type: 'new-resolution-to-webtorrent', | |
284 | videoUUID: video.uuid, | |
285 | resolution, | |
286 | isPortraitMode, | |
287 | hasAudio, | |
288 | createHLSIfNeeded: true, | |
289 | isNewVideo | |
290 | } | |
291 | ||
292 | resolutionCreated.push('webtorrent-' + resolution) | |
293 | } | |
294 | ||
295 | if (CONFIG.TRANSCODING.HLS.ENABLED && type === 'hls') { | |
296 | dataInput = { | |
297 | type: 'new-resolution-to-hls', | |
298 | videoUUID: video.uuid, | |
299 | resolution, | |
300 | isPortraitMode, | |
301 | hasAudio, | |
302 | copyCodecs: false, | |
303 | isMaxQuality: false, | |
304 | autoDeleteWebTorrentIfNeeded: true, | |
305 | isNewVideo | |
306 | } | |
307 | ||
308 | resolutionCreated.push('hls-' + resolution) | |
309 | } | |
310 | ||
311 | if (!dataInput) continue | |
312 | ||
313 | const jobOptions = { | |
314 | priority: await getTranscodingJobPriority(user) | |
315 | } | |
316 | ||
317 | await addTranscodingJob(dataInput, jobOptions) | |
318 | } | |
319 | ||
320 | if (resolutionCreated.length === 0) { | |
321 | logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid, lTags(video.uuid)) | |
322 | ||
323 | return false | |
324 | } | |
325 | ||
326 | logger.info( | |
327 | 'New resolutions %s transcoding jobs created for video %s and origin file resolution of %d.', type, video.uuid, videoFileResolution, | |
328 | { resolutionCreated, ...lTags(video.uuid) } | |
329 | ) | |
330 | ||
331 | return true | |
332 | } |