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