]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/transcoding.ts
Avoid concurrency issue on transcoding
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / transcoding.ts
CommitLineData
b42c2c7e 1import Bluebird from 'bluebird'
ad5db104 2import express from 'express'
84cae54e 3import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg'
ad5db104 4import { logger, loggerTagsFactory } from '@server/helpers/logger'
b42c2c7e
C
5import { JobQueue } from '@server/lib/job-queue'
6import { Hooks } from '@server/lib/plugins/hooks'
7import { buildTranscodingJob } from '@server/lib/video'
ad5db104
C
8import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models'
9import { asyncMiddleware, authenticate, createTranscodingValidator, ensureUserHasRight } from '../../../middlewares'
10
11const lTags = loggerTagsFactory('api', 'video')
12const transcodingRouter = express.Router()
13
14transcodingRouter.post('/:videoId/transcoding',
15 authenticate,
16 ensureUserHasRight(UserRight.RUN_VIDEO_TRANSCODING),
17 asyncMiddleware(createTranscodingValidator),
18 asyncMiddleware(createTranscoding)
19)
20
21// ---------------------------------------------------------------------------
22
23export {
24 transcodingRouter
25}
26
27// ---------------------------------------------------------------------------
28
29async function createTranscoding (req: express.Request, res: express.Response) {
30 const video = res.locals.videoAll
0f11ec8d 31 logger.info('Creating %s transcoding job for %s.', req.body.transcodingType, video.url, lTags())
ad5db104
C
32
33 const body: VideoTranscodingCreate = req.body
34
84cae54e 35 const { resolution: maxResolution, audioStream } = await video.probeMaxQualityFile()
ebb9e53a 36 const resolutions = await Hooks.wrapObject(
5e2afe42 37 computeResolutionsToTranscode({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false }),
64fd6158 38 'filter:transcoding.manual.resolutions-to-transcode.result',
ebb9e53a
C
39 body
40 )
41
42 if (resolutions.length === 0) {
43 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
44 }
ad5db104
C
45
46 video.state = VideoState.TO_TRANSCODE
47 await video.save()
48
b42c2c7e
C
49 const hasAudio = !!audioStream
50 const childrenResolutions = resolutions.filter(r => r !== maxResolution)
51
52 const children = await Bluebird.mapSeries(childrenResolutions, resolution => {
ad5db104 53 if (body.transcodingType === 'hls') {
b42c2c7e 54 return buildHLSJobOption({
ad5db104 55 videoUUID: video.uuid,
b42c2c7e 56 hasAudio,
ad5db104 57 resolution,
b42c2c7e 58 isMaxQuality: false
ad5db104 59 })
b42c2c7e
C
60 }
61
62 if (body.transcodingType === 'webtorrent') {
63 return buildWebTorrentJobOption({
ad5db104 64 videoUUID: video.uuid,
b42c2c7e
C
65 hasAudio,
66 resolution
ad5db104
C
67 })
68 }
b42c2c7e
C
69 })
70
71 const parent = body.transcodingType === 'hls'
72 ? await buildHLSJobOption({
73 videoUUID: video.uuid,
74 hasAudio,
75 resolution: maxResolution,
76 isMaxQuality: false
77 })
78 : await buildWebTorrentJobOption({
79 videoUUID: video.uuid,
80 hasAudio,
81 resolution: maxResolution
82 })
83
84 // Porcess the last resolution after the other ones to prevent concurrency issue
85 // Because low resolutions use the biggest one as ffmpeg input
86 await JobQueue.Instance.createJobWithChildren(parent, children)
ad5db104
C
87
88 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
89}
b42c2c7e
C
90
91function buildHLSJobOption (options: {
92 videoUUID: string
93 hasAudio: boolean
94 resolution: number
95 isMaxQuality: boolean
96}) {
97 const { videoUUID, hasAudio, resolution, isMaxQuality } = options
98
99 return buildTranscodingJob({
100 type: 'new-resolution-to-hls',
101 videoUUID,
102 resolution,
103 hasAudio,
104 copyCodecs: false,
105 isNewVideo: false,
106 autoDeleteWebTorrentIfNeeded: false,
107 isMaxQuality
108 })
109}
110
111function buildWebTorrentJobOption (options: {
112 videoUUID: string
113 hasAudio: boolean
114 resolution: number
115}) {
116 const { videoUUID, hasAudio, resolution } = options
117
118 return buildTranscodingJob({
119 type: 'new-resolution-to-webtorrent',
120 videoUUID,
121 isNewVideo: false,
122 resolution,
123 hasAudio,
124 createHLSIfNeeded: false
125 })
126}