]>
Commit | Line | Data |
---|---|---|
1 | import * as Bull from 'bull' | |
2 | import { TranscodeOptionsType } from '@server/helpers/ffmpeg-utils' | |
3 | import { JOB_PRIORITY } from '@server/initializers/constants' | |
4 | import { getJobTranscodingPriorityMalus, publishAndFederateIfNeeded } from '@server/lib/video' | |
5 | import { getVideoFilePath } from '@server/lib/video-paths' | |
6 | import { UserModel } from '@server/models/account/user' | |
7 | import { MUser, MUserId, MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/types/models' | |
8 | import { | |
9 | HLSTranscodingPayload, | |
10 | MergeAudioTranscodingPayload, | |
11 | NewResolutionTranscodingPayload, | |
12 | OptimizeTranscodingPayload, | |
13 | VideoTranscodingPayload | |
14 | } from '../../../../shared' | |
15 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
16 | import { computeResolutionsToTranscode } from '../../../helpers/ffprobe-utils' | |
17 | import { logger } from '../../../helpers/logger' | |
18 | import { CONFIG } from '../../../initializers/config' | |
19 | import { sequelizeTypescript } from '../../../initializers/database' | |
20 | import { VideoModel } from '../../../models/video/video' | |
21 | import { federateVideoIfNeeded } from '../../activitypub/videos' | |
22 | import { Notifier } from '../../notifier' | |
23 | import { | |
24 | generateHlsPlaylistResolution, | |
25 | mergeAudioVideofile, | |
26 | optimizeOriginalVideofile, | |
27 | transcodeNewWebTorrentResolution | |
28 | } from '../../video-transcoding' | |
29 | import { JobQueue } from '../job-queue' | |
30 | ||
31 | type HandlerFunction = (job: Bull.Job, payload: VideoTranscodingPayload, video: MVideoFullLight, user: MUser) => Promise<any> | |
32 | ||
33 | const handlers: { [ id: string ]: HandlerFunction } = { | |
34 | // Deprecated, introduced in 3.1 | |
35 | 'hls': handleHLSJob, | |
36 | 'new-resolution-to-hls': handleHLSJob, | |
37 | ||
38 | // Deprecated, introduced in 3.1 | |
39 | 'new-resolution': handleNewWebTorrentResolutionJob, | |
40 | 'new-resolution-to-webtorrent': handleNewWebTorrentResolutionJob, | |
41 | ||
42 | // Deprecated, introduced in 3.1 | |
43 | 'merge-audio': handleWebTorrentMergeAudioJob, | |
44 | 'merge-audio-to-webtorrent': handleWebTorrentMergeAudioJob, | |
45 | ||
46 | // Deprecated, introduced in 3.1 | |
47 | 'optimize': handleWebTorrentOptimizeJob, | |
48 | 'optimize-to-webtorrent': handleWebTorrentOptimizeJob | |
49 | } | |
50 | ||
51 | async function processVideoTranscoding (job: Bull.Job) { | |
52 | const payload = job.data as VideoTranscodingPayload | |
53 | logger.info('Processing video file in job %d.', job.id) | |
54 | ||
55 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
56 | // No video, maybe deleted? | |
57 | if (!video) { | |
58 | logger.info('Do not process job %d, video does not exist.', job.id) | |
59 | return undefined | |
60 | } | |
61 | ||
62 | const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) | |
63 | ||
64 | const handler = handlers[payload.type] | |
65 | ||
66 | if (!handler) { | |
67 | throw new Error('Cannot find transcoding handler for ' + payload.type) | |
68 | } | |
69 | ||
70 | await handler(job, payload, video, user) | |
71 | ||
72 | return video | |
73 | } | |
74 | ||
75 | // --------------------------------------------------------------------------- | |
76 | // Job handlers | |
77 | // --------------------------------------------------------------------------- | |
78 | ||
79 | async function handleHLSJob (job: Bull.Job, payload: HLSTranscodingPayload, video: MVideoFullLight, user: MUser) { | |
80 | const videoFileInput = payload.copyCodecs | |
81 | ? video.getWebTorrentFile(payload.resolution) | |
82 | : video.getMaxQualityFile() | |
83 | ||
84 | const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist() | |
85 | const videoInputPath = getVideoFilePath(videoOrStreamingPlaylist, videoFileInput) | |
86 | ||
87 | await generateHlsPlaylistResolution({ | |
88 | video, | |
89 | videoInputPath, | |
90 | resolution: payload.resolution, | |
91 | copyCodecs: payload.copyCodecs, | |
92 | isPortraitMode: payload.isPortraitMode || false, | |
93 | job | |
94 | }) | |
95 | ||
96 | await retryTransactionWrapper(onHlsPlaylistGeneration, video, user, payload) | |
97 | } | |
98 | ||
99 | async function handleNewWebTorrentResolutionJob ( | |
100 | job: Bull.Job, | |
101 | payload: NewResolutionTranscodingPayload, | |
102 | video: MVideoFullLight, | |
103 | user: MUserId | |
104 | ) { | |
105 | await transcodeNewWebTorrentResolution(video, payload.resolution, payload.isPortraitMode || false, job) | |
106 | ||
107 | await retryTransactionWrapper(onNewWebTorrentFileResolution, video, user, payload) | |
108 | } | |
109 | ||
110 | async function handleWebTorrentMergeAudioJob (job: Bull.Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) { | |
111 | await mergeAudioVideofile(video, payload.resolution, job) | |
112 | ||
113 | await retryTransactionWrapper(onVideoFileOptimizer, video, payload, 'video', user) | |
114 | } | |
115 | ||
116 | async function handleWebTorrentOptimizeJob (job: Bull.Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) { | |
117 | const transcodeType = await optimizeOriginalVideofile(video, video.getMaxQualityFile(), job) | |
118 | ||
119 | await retryTransactionWrapper(onVideoFileOptimizer, video, payload, transcodeType, user) | |
120 | } | |
121 | ||
122 | // --------------------------------------------------------------------------- | |
123 | ||
124 | async function onHlsPlaylistGeneration (video: MVideoFullLight, user: MUser, payload: HLSTranscodingPayload) { | |
125 | if (video === undefined) return undefined | |
126 | ||
127 | if (payload.isMaxQuality && CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) { | |
128 | // Remove webtorrent files if not enabled | |
129 | for (const file of video.VideoFiles) { | |
130 | await video.removeFile(file) | |
131 | await file.removeTorrent() | |
132 | await file.destroy() | |
133 | } | |
134 | ||
135 | video.VideoFiles = [] | |
136 | ||
137 | // Create HLS new resolution jobs | |
138 | await createLowerResolutionsJobs(video, user, payload.resolution, payload.isPortraitMode, 'hls') | |
139 | } | |
140 | ||
141 | return publishAndFederateIfNeeded(video) | |
142 | } | |
143 | ||
144 | async function onVideoFileOptimizer ( | |
145 | videoArg: MVideoWithFile, | |
146 | payload: OptimizeTranscodingPayload | MergeAudioTranscodingPayload, | |
147 | transcodeType: TranscodeOptionsType, | |
148 | user: MUserId | |
149 | ) { | |
150 | if (videoArg === undefined) return undefined | |
151 | ||
152 | // Outside the transaction (IO on disk) | |
153 | const { videoFileResolution, isPortraitMode } = await videoArg.getMaxQualityResolution() | |
154 | ||
155 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { | |
156 | // Maybe the video changed in database, refresh it | |
157 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t) | |
158 | // Video does not exist anymore | |
159 | if (!videoDatabase) return undefined | |
160 | ||
161 | let videoPublished = false | |
162 | ||
163 | // Generate HLS version of the original file | |
164 | const originalFileHLSPayload = Object.assign({}, payload, { | |
165 | isPortraitMode, | |
166 | resolution: videoDatabase.getMaxQualityFile().resolution, | |
167 | // If we quick transcoded original file, force transcoding for HLS to avoid some weird playback issues | |
168 | copyCodecs: transcodeType !== 'quick-transcode', | |
169 | isMaxQuality: true | |
170 | }) | |
171 | const hasHls = await createHlsJobIfEnabled(user, originalFileHLSPayload) | |
172 | ||
173 | const hasNewResolutions = await createLowerResolutionsJobs(videoDatabase, user, videoFileResolution, isPortraitMode, 'webtorrent') | |
174 | ||
175 | if (!hasHls && !hasNewResolutions) { | |
176 | // No transcoding to do, it's now published | |
177 | videoPublished = await videoDatabase.publishIfNeededAndSave(t) | |
178 | } | |
179 | ||
180 | await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t) | |
181 | ||
182 | return { videoDatabase, videoPublished } | |
183 | }) | |
184 | ||
185 | if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) | |
186 | if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) | |
187 | } | |
188 | ||
189 | async function onNewWebTorrentFileResolution ( | |
190 | video: MVideoUUID, | |
191 | user: MUserId, | |
192 | payload: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload | |
193 | ) { | |
194 | await publishAndFederateIfNeeded(video) | |
195 | ||
196 | await createHlsJobIfEnabled(user, Object.assign({}, payload, { copyCodecs: true, isMaxQuality: false })) | |
197 | } | |
198 | ||
199 | // --------------------------------------------------------------------------- | |
200 | ||
201 | export { | |
202 | processVideoTranscoding, | |
203 | onNewWebTorrentFileResolution | |
204 | } | |
205 | ||
206 | // --------------------------------------------------------------------------- | |
207 | ||
208 | async function createHlsJobIfEnabled (user: MUserId, payload: { | |
209 | videoUUID: string | |
210 | resolution: number | |
211 | isPortraitMode?: boolean | |
212 | copyCodecs: boolean | |
213 | isMaxQuality: boolean | |
214 | }) { | |
215 | if (!payload || CONFIG.TRANSCODING.HLS.ENABLED !== true) return false | |
216 | ||
217 | const jobOptions = { | |
218 | priority: JOB_PRIORITY.TRANSCODING.NEW_RESOLUTION + await getJobTranscodingPriorityMalus(user) | |
219 | } | |
220 | ||
221 | const hlsTranscodingPayload: HLSTranscodingPayload = { | |
222 | type: 'new-resolution-to-hls', | |
223 | videoUUID: payload.videoUUID, | |
224 | resolution: payload.resolution, | |
225 | isPortraitMode: payload.isPortraitMode, | |
226 | copyCodecs: payload.copyCodecs, | |
227 | isMaxQuality: payload.isMaxQuality | |
228 | } | |
229 | ||
230 | JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload }, jobOptions) | |
231 | ||
232 | return true | |
233 | } | |
234 | ||
235 | async function createLowerResolutionsJobs ( | |
236 | video: MVideoFullLight, | |
237 | user: MUserId, | |
238 | videoFileResolution: number, | |
239 | isPortraitMode: boolean, | |
240 | type: 'hls' | 'webtorrent' | |
241 | ) { | |
242 | // Create transcoding jobs if there are enabled resolutions | |
243 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution, 'vod') | |
244 | const resolutionCreated: number[] = [] | |
245 | ||
246 | for (const resolution of resolutionsEnabled) { | |
247 | let dataInput: VideoTranscodingPayload | |
248 | ||
249 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED && type === 'webtorrent') { | |
250 | // WebTorrent will create subsequent HLS job | |
251 | dataInput = { | |
252 | type: 'new-resolution-to-webtorrent', | |
253 | videoUUID: video.uuid, | |
254 | resolution, | |
255 | isPortraitMode | |
256 | } | |
257 | } | |
258 | ||
259 | if (CONFIG.TRANSCODING.HLS.ENABLED && type === 'hls') { | |
260 | dataInput = { | |
261 | type: 'new-resolution-to-hls', | |
262 | videoUUID: video.uuid, | |
263 | resolution, | |
264 | isPortraitMode, | |
265 | copyCodecs: false, | |
266 | isMaxQuality: false | |
267 | } | |
268 | } | |
269 | ||
270 | if (!dataInput) continue | |
271 | ||
272 | resolutionCreated.push(resolution) | |
273 | ||
274 | const jobOptions = { | |
275 | priority: JOB_PRIORITY.TRANSCODING.NEW_RESOLUTION + await getJobTranscodingPriorityMalus(user) | |
276 | } | |
277 | ||
278 | JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }, jobOptions) | |
279 | } | |
280 | ||
281 | if (resolutionCreated.length === 0) { | |
282 | logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid) | |
283 | ||
284 | return false | |
285 | } | |
286 | ||
287 | logger.info( | |
288 | 'New resolutions %s transcoding jobs created for video %s and origin file resolution of %d.', type, video.uuid, videoFileResolution, | |
289 | { resolutionCreated } | |
290 | ) | |
291 | ||
292 | return true | |
293 | } |