]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
e8d246d5 | 2 | import { VideoResolution, VideoState } from '../../../../shared' |
da854ddd | 3 | import { logger } from '../../../helpers/logger' |
3fd3ab2d | 4 | import { VideoModel } from '../../../models/video/video' |
94a5ff8a | 5 | import { JobQueue } from '../job-queue' |
2186386c C |
6 | import { federateVideoIfNeeded } from '../../activitypub' |
7 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
6dd9de95 | 8 | import { sequelizeTypescript } from '../../../initializers' |
94831479 | 9 | import * as Bluebird from 'bluebird' |
06215f15 | 10 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' |
536598cf | 11 | import { generateHlsPlaylist, optimizeVideofile, transcodeOriginalVideofile, mergeAudioVideofile } from '../../video-transcoding' |
cef534ed | 12 | import { Notifier } from '../../notifier' |
6dd9de95 | 13 | import { CONFIG } from '../../../initializers/config' |
453e83ea | 14 | import { MVideoUUID, MVideoWithFile } from '@server/typings/models' |
40298b02 | 15 | |
536598cf | 16 | interface BaseTranscodingPayload { |
94a5ff8a | 17 | videoUUID: string |
09209296 | 18 | isNewVideo?: boolean |
536598cf C |
19 | } |
20 | ||
21 | interface HLSTranscodingPayload extends BaseTranscodingPayload { | |
22 | type: 'hls' | |
23 | isPortraitMode?: boolean | |
24 | resolution: VideoResolution | |
25 | } | |
26 | ||
27 | interface NewResolutionTranscodingPayload extends BaseTranscodingPayload { | |
28 | type: 'new-resolution' | |
056aa7f2 | 29 | isPortraitMode?: boolean |
536598cf C |
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' | |
94a5ff8a C |
40 | } |
41 | ||
536598cf C |
42 | export type VideoTranscodingPayload = HLSTranscodingPayload | NewResolutionTranscodingPayload |
43 | | OptimizeTranscodingPayload | MergeAudioTranscodingPayload | |
44 | ||
a0327eed C |
45 | async function processVideoTranscoding (job: Bull.Job) { |
46 | const payload = job.data as VideoTranscodingPayload | |
94a5ff8a C |
47 | logger.info('Processing video file in job %d.', job.id) |
48 | ||
627621c1 | 49 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) |
f5028693 C |
50 | // No video, maybe deleted? |
51 | if (!video) { | |
c1e791ba | 52 | logger.info('Do not process job %d, video does not exist.', job.id) |
f5028693 C |
53 | return undefined |
54 | } | |
55 | ||
536598cf | 56 | if (payload.type === 'hls') { |
09209296 C |
57 | await generateHlsPlaylist(video, payload.resolution, payload.isPortraitMode || false) |
58 | ||
59 | await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video) | |
536598cf | 60 | } else if (payload.type === 'new-resolution') { |
098eb377 | 61 | await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false) |
2186386c | 62 | |
536598cf C |
63 | await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload) |
64 | } else if (payload.type === 'merge-audio') { | |
65 | await mergeAudioVideofile(video, payload.resolution) | |
66 | ||
67 | await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload) | |
94a5ff8a | 68 | } else { |
edb4ffc7 | 69 | await optimizeVideofile(video) |
2186386c | 70 | |
09209296 | 71 | await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload) |
94a5ff8a | 72 | } |
031094f7 | 73 | |
f5028693 | 74 | return video |
40298b02 C |
75 | } |
76 | ||
453e83ea | 77 | async function onHlsPlaylistGenerationSuccess (video: MVideoUUID) { |
09209296 C |
78 | if (video === undefined) return undefined |
79 | ||
80 | await sequelizeTypescript.transaction(async t => { | |
81 | // Maybe the video changed in database, refresh it | |
82 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | |
83 | // Video does not exist anymore | |
84 | if (!videoDatabase) return undefined | |
85 | ||
86 | // If the video was not published, we consider it is a new one for other instances | |
87 | await federateVideoIfNeeded(videoDatabase, false, t) | |
88 | }) | |
89 | } | |
90 | ||
453e83ea | 91 | async function publishNewResolutionIfNeeded (video: MVideoUUID, payload?: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload) { |
dc133480 | 92 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { |
2186386c | 93 | // Maybe the video changed in database, refresh it |
627621c1 | 94 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) |
2186386c C |
95 | // Video does not exist anymore |
96 | if (!videoDatabase) return undefined | |
94a5ff8a | 97 | |
dc133480 | 98 | let videoPublished = false |
1b952dd4 | 99 | |
2186386c | 100 | // We transcoded the video file in another format, now we can publish it |
1b952dd4 | 101 | if (videoDatabase.state !== VideoState.PUBLISHED) { |
dc133480 | 102 | videoPublished = true |
1b952dd4 C |
103 | |
104 | videoDatabase.state = VideoState.PUBLISHED | |
105 | videoDatabase.publishedAt = new Date() | |
106 | videoDatabase = await videoDatabase.save({ transaction: t }) | |
107 | } | |
2186386c C |
108 | |
109 | // If the video was not published, we consider it is a new one for other instances | |
dc133480 | 110 | await federateVideoIfNeeded(videoDatabase, videoPublished, t) |
94a5ff8a | 111 | |
dc133480 | 112 | return { videoDatabase, videoPublished } |
2186386c | 113 | }) |
e8d246d5 | 114 | |
7ccddd7b | 115 | if (videoPublished) { |
5b77537c | 116 | Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) |
7ccddd7b | 117 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) |
dc133480 | 118 | } |
09209296 C |
119 | |
120 | await createHlsJobIfEnabled(payload) | |
40298b02 C |
121 | } |
122 | ||
453e83ea | 123 | async function onVideoFileOptimizerSuccess (videoArg: MVideoWithFile, payload: OptimizeTranscodingPayload) { |
56b13bd1 | 124 | if (videoArg === undefined) return undefined |
031094f7 | 125 | |
2186386c | 126 | // Outside the transaction (IO on disk) |
56b13bd1 | 127 | const { videoFileResolution } = await videoArg.getOriginalFileResolution() |
2186386c | 128 | |
dc133480 | 129 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { |
2186386c | 130 | // Maybe the video changed in database, refresh it |
56b13bd1 | 131 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t) |
2186386c C |
132 | // Video does not exist anymore |
133 | if (!videoDatabase) return undefined | |
134 | ||
135 | // Create transcoding jobs if there are enabled resolutions | |
136 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution) | |
137 | logger.info( | |
138 | 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution, | |
139 | { resolutions: resolutionsEnabled } | |
140 | ) | |
141 | ||
dc133480 C |
142 | let videoPublished = false |
143 | ||
2186386c | 144 | if (resolutionsEnabled.length !== 0) { |
374c1db9 | 145 | const tasks: (Bluebird<Bull.Job<any>> | Promise<Bull.Job<any>>)[] = [] |
2186386c C |
146 | |
147 | for (const resolution of resolutionsEnabled) { | |
148 | const dataInput = { | |
536598cf | 149 | type: 'new-resolution' as 'new-resolution', |
2186386c C |
150 | videoUUID: videoDatabase.uuid, |
151 | resolution | |
152 | } | |
153 | ||
a0327eed | 154 | const p = JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) |
2186386c C |
155 | tasks.push(p) |
156 | } | |
f5028693 | 157 | |
2186386c | 158 | await Promise.all(tasks) |
a7977280 | 159 | |
2186386c C |
160 | logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled }) |
161 | } else { | |
dc133480 C |
162 | videoPublished = true |
163 | ||
2186386c | 164 | // No transcoding to do, it's now published |
56b13bd1 C |
165 | videoDatabase.state = VideoState.PUBLISHED |
166 | videoDatabase = await videoDatabase.save({ transaction: t }) | |
a7977280 | 167 | |
56b13bd1 | 168 | logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy }) |
f5028693 | 169 | } |
a7977280 | 170 | |
09209296 | 171 | await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t) |
e8d246d5 | 172 | |
dc133480 | 173 | return { videoDatabase, videoPublished } |
2186386c | 174 | }) |
e8d246d5 | 175 | |
5b77537c | 176 | if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) |
7ccddd7b | 177 | if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) |
09209296 | 178 | |
536598cf C |
179 | const hlsPayload = Object.assign({}, payload, { resolution: videoDatabase.getOriginalFile().resolution }) |
180 | await createHlsJobIfEnabled(hlsPayload) | |
40298b02 C |
181 | } |
182 | ||
183 | // --------------------------------------------------------------------------- | |
184 | ||
185 | export { | |
a0327eed | 186 | processVideoTranscoding, |
536598cf | 187 | publishNewResolutionIfNeeded |
40298b02 | 188 | } |
09209296 C |
189 | |
190 | // --------------------------------------------------------------------------- | |
191 | ||
536598cf | 192 | function createHlsJobIfEnabled (payload?: { videoUUID: string, resolution: number, isPortraitMode?: boolean }) { |
09209296 C |
193 | // Generate HLS playlist? |
194 | if (payload && CONFIG.TRANSCODING.HLS.ENABLED) { | |
195 | const hlsTranscodingPayload = { | |
536598cf | 196 | type: 'hls' as 'hls', |
09209296 C |
197 | videoUUID: payload.videoUUID, |
198 | resolution: payload.resolution, | |
536598cf | 199 | isPortraitMode: payload.isPortraitMode |
09209296 C |
200 | } |
201 | ||
a0327eed | 202 | return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload }) |
09209296 C |
203 | } |
204 | } |