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