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