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