]>
Commit | Line | Data |
---|---|---|
1 | import * as Bull from 'bull' | |
2 | import { VideoResolution, VideoState } 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 { CONFIG, sequelizeTypescript } from '../../../initializers' | |
9 | import * as Bluebird from 'bluebird' | |
10 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' | |
11 | import { generateHlsPlaylist, optimizeVideofile, transcodeOriginalVideofile } from '../../video-transcoding' | |
12 | import { Notifier } from '../../notifier' | |
13 | ||
14 | export type VideoTranscodingPayload = { | |
15 | videoUUID: string | |
16 | resolution?: VideoResolution | |
17 | isNewVideo?: boolean | |
18 | isPortraitMode?: boolean | |
19 | generateHlsPlaylist?: boolean | |
20 | } | |
21 | ||
22 | async function processVideoTranscoding (job: Bull.Job) { | |
23 | const payload = job.data as VideoTranscodingPayload | |
24 | logger.info('Processing video file in job %d.', job.id) | |
25 | ||
26 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
27 | // No video, maybe deleted? | |
28 | if (!video) { | |
29 | logger.info('Do not process job %d, video does not exist.', job.id) | |
30 | return undefined | |
31 | } | |
32 | ||
33 | if (payload.generateHlsPlaylist) { | |
34 | await generateHlsPlaylist(video, payload.resolution, payload.isPortraitMode || false) | |
35 | ||
36 | await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video) | |
37 | } else if (payload.resolution) { // Transcoding in other resolution | |
38 | await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false) | |
39 | ||
40 | await retryTransactionWrapper(publishVideoIfNeeded, video, payload) | |
41 | } else { | |
42 | await optimizeVideofile(video) | |
43 | ||
44 | await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload) | |
45 | } | |
46 | ||
47 | return video | |
48 | } | |
49 | ||
50 | async function onHlsPlaylistGenerationSuccess (video: VideoModel) { | |
51 | if (video === undefined) return undefined | |
52 | ||
53 | await sequelizeTypescript.transaction(async t => { | |
54 | // Maybe the video changed in database, refresh it | |
55 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | |
56 | // Video does not exist anymore | |
57 | if (!videoDatabase) return undefined | |
58 | ||
59 | // If the video was not published, we consider it is a new one for other instances | |
60 | await federateVideoIfNeeded(videoDatabase, false, t) | |
61 | }) | |
62 | } | |
63 | ||
64 | async function publishVideoIfNeeded (video: VideoModel, payload?: VideoTranscodingPayload) { | |
65 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { | |
66 | // Maybe the video changed in database, refresh it | |
67 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | |
68 | // Video does not exist anymore | |
69 | if (!videoDatabase) return undefined | |
70 | ||
71 | let videoPublished = false | |
72 | ||
73 | // We transcoded the video file in another format, now we can publish it | |
74 | if (videoDatabase.state !== VideoState.PUBLISHED) { | |
75 | videoPublished = true | |
76 | ||
77 | videoDatabase.state = VideoState.PUBLISHED | |
78 | videoDatabase.publishedAt = new Date() | |
79 | videoDatabase = await videoDatabase.save({ transaction: t }) | |
80 | } | |
81 | ||
82 | // If the video was not published, we consider it is a new one for other instances | |
83 | await federateVideoIfNeeded(videoDatabase, videoPublished, t) | |
84 | ||
85 | return { videoDatabase, videoPublished } | |
86 | }) | |
87 | ||
88 | if (videoPublished) { | |
89 | Notifier.Instance.notifyOnNewVideo(videoDatabase) | |
90 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) | |
91 | } | |
92 | ||
93 | await createHlsJobIfEnabled(payload) | |
94 | } | |
95 | ||
96 | async function onVideoFileOptimizerSuccess (videoArg: VideoModel, payload: VideoTranscodingPayload) { | |
97 | if (videoArg === undefined) return undefined | |
98 | ||
99 | // Outside the transaction (IO on disk) | |
100 | const { videoFileResolution } = await videoArg.getOriginalFileResolution() | |
101 | ||
102 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { | |
103 | // Maybe the video changed in database, refresh it | |
104 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t) | |
105 | // Video does not exist anymore | |
106 | if (!videoDatabase) return undefined | |
107 | ||
108 | // Create transcoding jobs if there are enabled resolutions | |
109 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution) | |
110 | logger.info( | |
111 | 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution, | |
112 | { resolutions: resolutionsEnabled } | |
113 | ) | |
114 | ||
115 | let videoPublished = false | |
116 | ||
117 | if (resolutionsEnabled.length !== 0) { | |
118 | const tasks: (Bluebird<Bull.Job<any>> | Promise<Bull.Job<any>>)[] = [] | |
119 | ||
120 | for (const resolution of resolutionsEnabled) { | |
121 | const dataInput = { | |
122 | videoUUID: videoDatabase.uuid, | |
123 | resolution | |
124 | } | |
125 | ||
126 | const p = JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) | |
127 | tasks.push(p) | |
128 | } | |
129 | ||
130 | await Promise.all(tasks) | |
131 | ||
132 | logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled }) | |
133 | } else { | |
134 | videoPublished = true | |
135 | ||
136 | // No transcoding to do, it's now published | |
137 | videoDatabase.state = VideoState.PUBLISHED | |
138 | videoDatabase = await videoDatabase.save({ transaction: t }) | |
139 | ||
140 | logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy }) | |
141 | } | |
142 | ||
143 | await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t) | |
144 | ||
145 | return { videoDatabase, videoPublished } | |
146 | }) | |
147 | ||
148 | if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideo(videoDatabase) | |
149 | if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) | |
150 | ||
151 | await createHlsJobIfEnabled(Object.assign({}, payload, { resolution: videoDatabase.getOriginalFile().resolution })) | |
152 | } | |
153 | ||
154 | // --------------------------------------------------------------------------- | |
155 | ||
156 | export { | |
157 | processVideoTranscoding, | |
158 | publishVideoIfNeeded | |
159 | } | |
160 | ||
161 | // --------------------------------------------------------------------------- | |
162 | ||
163 | function createHlsJobIfEnabled (payload?: VideoTranscodingPayload) { | |
164 | // Generate HLS playlist? | |
165 | if (payload && CONFIG.TRANSCODING.HLS.ENABLED) { | |
166 | const hlsTranscodingPayload = { | |
167 | videoUUID: payload.videoUUID, | |
168 | resolution: payload.resolution, | |
169 | isPortraitMode: payload.isPortraitMode, | |
170 | ||
171 | generateHlsPlaylist: true | |
172 | } | |
173 | ||
174 | return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload }) | |
175 | } | |
176 | } |