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