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