1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 18:29:27 +02:00

Avoid concurrency issue on transcoding

This commit is contained in:
Chocobozzz 2022-08-09 09:09:31 +02:00
parent bd911b54b5
commit b42c2c7e89
No known key found for this signature in database
GPG key ID: 583A612D890159BE
8 changed files with 120 additions and 54 deletions

View file

@ -1,10 +1,12 @@
import Bluebird from 'bluebird'
import express from 'express'
import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { addTranscodingJob } from '@server/lib/video'
import { JobQueue } from '@server/lib/job-queue'
import { Hooks } from '@server/lib/plugins/hooks'
import { buildTranscodingJob } from '@server/lib/video'
import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models'
import { asyncMiddleware, authenticate, createTranscodingValidator, ensureUserHasRight } from '../../../middlewares'
import { Hooks } from '@server/lib/plugins/hooks'
const lTags = loggerTagsFactory('api', 'video')
const transcodingRouter = express.Router()
@ -44,29 +46,81 @@ async function createTranscoding (req: express.Request, res: express.Response) {
video.state = VideoState.TO_TRANSCODE
await video.save()
for (const resolution of resolutions) {
const hasAudio = !!audioStream
const childrenResolutions = resolutions.filter(r => r !== maxResolution)
const children = await Bluebird.mapSeries(childrenResolutions, resolution => {
if (body.transcodingType === 'hls') {
await addTranscodingJob({
type: 'new-resolution-to-hls',
return buildHLSJobOption({
videoUUID: video.uuid,
hasAudio,
resolution,
hasAudio: !!audioStream,
copyCodecs: false,
isNewVideo: false,
autoDeleteWebTorrentIfNeeded: false,
isMaxQuality: maxResolution === resolution
})
} else if (body.transcodingType === 'webtorrent') {
await addTranscodingJob({
type: 'new-resolution-to-webtorrent',
videoUUID: video.uuid,
isNewVideo: false,
resolution,
hasAudio: !!audioStream,
createHLSIfNeeded: false
isMaxQuality: false
})
}
}
if (body.transcodingType === 'webtorrent') {
return buildWebTorrentJobOption({
videoUUID: video.uuid,
hasAudio,
resolution
})
}
})
const parent = body.transcodingType === 'hls'
? await buildHLSJobOption({
videoUUID: video.uuid,
hasAudio,
resolution: maxResolution,
isMaxQuality: false
})
: await buildWebTorrentJobOption({
videoUUID: video.uuid,
hasAudio,
resolution: maxResolution
})
// Porcess the last resolution after the other ones to prevent concurrency issue
// Because low resolutions use the biggest one as ffmpeg input
await JobQueue.Instance.createJobWithChildren(parent, children)
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
function buildHLSJobOption (options: {
videoUUID: string
hasAudio: boolean
resolution: number
isMaxQuality: boolean
}) {
const { videoUUID, hasAudio, resolution, isMaxQuality } = options
return buildTranscodingJob({
type: 'new-resolution-to-hls',
videoUUID,
resolution,
hasAudio,
copyCodecs: false,
isNewVideo: false,
autoDeleteWebTorrentIfNeeded: false,
isMaxQuality
})
}
function buildWebTorrentJobOption (options: {
videoUUID: string
hasAudio: boolean
resolution: number
}) {
const { videoUUID, hasAudio, resolution } = options
return buildTranscodingJob({
type: 'new-resolution-to-webtorrent',
videoUUID,
isNewVideo: false,
resolution,
hasAudio,
createHLSIfNeeded: false
})
}