]>
Commit | Line | Data |
---|---|---|
1 | import * as Bull from 'bull' | |
2 | import { VideoResolution } from '../../../../shared' | |
3 | import { logger } from '../../../helpers/logger' | |
4 | import { VideoModel } from '../../../models/video/video' | |
5 | import { JobQueue } from '../job-queue' | |
6 | import { federateVideoIfNeeded } from '../../activitypub' | |
7 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
8 | import { sequelizeTypescript } from '../../../initializers' | |
9 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' | |
10 | import { generateHlsPlaylist, mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewResolution } from '../../video-transcoding' | |
11 | import { Notifier } from '../../notifier' | |
12 | import { CONFIG } from '../../../initializers/config' | |
13 | import { MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/typings/models' | |
14 | ||
15 | interface BaseTranscodingPayload { | |
16 | videoUUID: string | |
17 | isNewVideo?: boolean | |
18 | } | |
19 | ||
20 | interface HLSTranscodingPayload extends BaseTranscodingPayload { | |
21 | type: 'hls' | |
22 | isPortraitMode?: boolean | |
23 | resolution: VideoResolution | |
24 | copyCodecs: boolean | |
25 | } | |
26 | ||
27 | interface NewResolutionTranscodingPayload extends BaseTranscodingPayload { | |
28 | type: 'new-resolution' | |
29 | isPortraitMode?: boolean | |
30 | resolution: VideoResolution | |
31 | } | |
32 | ||
33 | interface MergeAudioTranscodingPayload extends BaseTranscodingPayload { | |
34 | type: 'merge-audio' | |
35 | resolution: VideoResolution | |
36 | } | |
37 | ||
38 | interface OptimizeTranscodingPayload extends BaseTranscodingPayload { | |
39 | type: 'optimize' | |
40 | } | |
41 | ||
42 | export type VideoTranscodingPayload = | |
43 | HLSTranscodingPayload | |
44 | | NewResolutionTranscodingPayload | |
45 | | OptimizeTranscodingPayload | |
46 | | MergeAudioTranscodingPayload | |
47 | ||
48 | async function processVideoTranscoding (job: Bull.Job) { | |
49 | const payload = job.data as VideoTranscodingPayload | |
50 | logger.info('Processing video file in job %d.', job.id) | |
51 | ||
52 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
53 | // No video, maybe deleted? | |
54 | if (!video) { | |
55 | logger.info('Do not process job %d, video does not exist.', job.id) | |
56 | return undefined | |
57 | } | |
58 | ||
59 | if (payload.type === 'hls') { | |
60 | await generateHlsPlaylist(video, payload.resolution, payload.copyCodecs, payload.isPortraitMode || false) | |
61 | ||
62 | await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video) | |
63 | } else if (payload.type === 'new-resolution') { | |
64 | await transcodeNewResolution(video, payload.resolution, payload.isPortraitMode || false) | |
65 | ||
66 | await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload) | |
67 | } else if (payload.type === 'merge-audio') { | |
68 | await mergeAudioVideofile(video, payload.resolution) | |
69 | ||
70 | await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload) | |
71 | } else { | |
72 | await optimizeOriginalVideofile(video) | |
73 | ||
74 | await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload) | |
75 | } | |
76 | ||
77 | return video | |
78 | } | |
79 | ||
80 | async function onHlsPlaylistGenerationSuccess (video: MVideoFullLight) { | |
81 | if (video === undefined) return undefined | |
82 | ||
83 | // We generated the HLS playlist, we don't need the webtorrent files anymore if the admin disabled it | |
84 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) { | |
85 | for (const file of video.VideoFiles) { | |
86 | await video.removeFile(file) | |
87 | await file.destroy() | |
88 | } | |
89 | ||
90 | video.VideoFiles = [] | |
91 | } | |
92 | ||
93 | return publishAndFederateIfNeeded(video) | |
94 | } | |
95 | ||
96 | async function publishNewResolutionIfNeeded (video: MVideoUUID, payload?: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload) { | |
97 | await publishAndFederateIfNeeded(video) | |
98 | ||
99 | await createHlsJobIfEnabled(payload) | |
100 | } | |
101 | ||
102 | async function onVideoFileOptimizerSuccess (videoArg: MVideoWithFile, payload: OptimizeTranscodingPayload) { | |
103 | if (videoArg === undefined) return undefined | |
104 | ||
105 | // Outside the transaction (IO on disk) | |
106 | const { videoFileResolution } = await videoArg.getMaxQualityResolution() | |
107 | ||
108 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { | |
109 | // Maybe the video changed in database, refresh it | |
110 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t) | |
111 | // Video does not exist anymore | |
112 | if (!videoDatabase) return undefined | |
113 | ||
114 | // Create transcoding jobs if there are enabled resolutions | |
115 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution) | |
116 | logger.info( | |
117 | 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution, | |
118 | { resolutions: resolutionsEnabled } | |
119 | ) | |
120 | ||
121 | let videoPublished = false | |
122 | ||
123 | const hlsPayload = Object.assign({}, payload, { resolution: videoDatabase.getMaxQualityFile().resolution }) | |
124 | await createHlsJobIfEnabled(hlsPayload) | |
125 | ||
126 | if (resolutionsEnabled.length !== 0) { | |
127 | for (const resolution of resolutionsEnabled) { | |
128 | let dataInput: VideoTranscodingPayload | |
129 | ||
130 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED) { | |
131 | dataInput = { | |
132 | type: 'new-resolution' as 'new-resolution', | |
133 | videoUUID: videoDatabase.uuid, | |
134 | resolution | |
135 | } | |
136 | } else if (CONFIG.TRANSCODING.HLS.ENABLED) { | |
137 | dataInput = { | |
138 | type: 'hls', | |
139 | videoUUID: videoDatabase.uuid, | |
140 | resolution, | |
141 | isPortraitMode: false, | |
142 | copyCodecs: false | |
143 | } | |
144 | } | |
145 | ||
146 | JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) | |
147 | } | |
148 | ||
149 | logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled }) | |
150 | } else { | |
151 | // No transcoding to do, it's now published | |
152 | videoPublished = await videoDatabase.publishIfNeededAndSave(t) | |
153 | ||
154 | logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy }) | |
155 | } | |
156 | ||
157 | await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t) | |
158 | ||
159 | return { videoDatabase, videoPublished } | |
160 | }) | |
161 | ||
162 | if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) | |
163 | if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) | |
164 | } | |
165 | ||
166 | // --------------------------------------------------------------------------- | |
167 | ||
168 | export { | |
169 | processVideoTranscoding, | |
170 | publishNewResolutionIfNeeded | |
171 | } | |
172 | ||
173 | // --------------------------------------------------------------------------- | |
174 | ||
175 | function createHlsJobIfEnabled (payload?: { videoUUID: string, resolution: number, isPortraitMode?: boolean }) { | |
176 | // Generate HLS playlist? | |
177 | if (payload && CONFIG.TRANSCODING.HLS.ENABLED) { | |
178 | const hlsTranscodingPayload = { | |
179 | type: 'hls' as 'hls', | |
180 | videoUUID: payload.videoUUID, | |
181 | resolution: payload.resolution, | |
182 | isPortraitMode: payload.isPortraitMode, | |
183 | copyCodecs: true | |
184 | } | |
185 | ||
186 | return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload }) | |
187 | } | |
188 | } | |
189 | ||
190 | async function publishAndFederateIfNeeded (video: MVideoUUID) { | |
191 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { | |
192 | // Maybe the video changed in database, refresh it | |
193 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | |
194 | // Video does not exist anymore | |
195 | if (!videoDatabase) return undefined | |
196 | ||
197 | // We transcoded the video file in another format, now we can publish it | |
198 | const videoPublished = await videoDatabase.publishIfNeededAndSave(t) | |
199 | ||
200 | // If the video was not published, we consider it is a new one for other instances | |
201 | await federateVideoIfNeeded(videoDatabase, videoPublished, t) | |
202 | ||
203 | return { videoDatabase, videoPublished } | |
204 | }) | |
205 | ||
206 | if (videoPublished) { | |
207 | Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) | |
208 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) | |
209 | } | |
210 | } |