"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transcodingRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = (0, tslib_1.__importDefault)(require("express"));
const ffprobe_utils_1 = require("../../../helpers/ffprobe-utils");
const logger_1 = require("../../../helpers/logger");
const video_1 = require("../../../lib/video");
const middlewares_1 = require("../../../middlewares");
const lTags = (0, logger_1.loggerTagsFactory)('api', 'video');
const transcodingRouter = express_1.default.Router();
exports.transcodingRouter = transcodingRouter;
transcodingRouter.post('/:videoId/transcoding', middlewares_1.authenticate, (0, middlewares_1.ensureUserHasRight)(26), (0, middlewares_1.asyncMiddleware)(middlewares_1.createTranscodingValidator), (0, middlewares_1.asyncMiddleware)(createTranscoding));
function createTranscoding(req, res) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const video = res.locals.videoAll;
        logger_1.logger.info('Creating %s transcoding job for %s.', req.body.transcodingType, video.url, lTags());
        const body = req.body;
        const { resolution: maxResolution, isPortraitMode, audioStream } = yield video.getMaxQualityFileInfo();
        const resolutions = (0, ffprobe_utils_1.computeLowerResolutionsToTranscode)(maxResolution, 'vod').concat([maxResolution]);
        video.state = 2;
        yield video.save();
        for (const resolution of resolutions) {
            if (body.transcodingType === 'hls') {
                yield (0, video_1.addTranscodingJob)({
                    type: 'new-resolution-to-hls',
                    videoUUID: video.uuid,
                    resolution,
                    isPortraitMode,
                    hasAudio: !!audioStream,
                    copyCodecs: false,
                    isNewVideo: false,
                    autoDeleteWebTorrentIfNeeded: false,
                    isMaxQuality: maxResolution === resolution
                });
            }
            else if (body.transcodingType === 'webtorrent') {
                yield (0, video_1.addTranscodingJob)({
                    type: 'new-resolution-to-webtorrent',
                    videoUUID: video.uuid,
                    isNewVideo: false,
                    resolution,
                    hasAudio: !!audioStream,
                    createHLSIfNeeded: false,
                    isPortraitMode
                });
            }
        }
        return res.sendStatus(204);
    });
}
//# sourceMappingURL=transcoding.js.map