"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transcodingRouter = void 0;
const tslib_1 = require("tslib");
const bluebird_1 = tslib_1.__importDefault(require("bluebird"));
const express_1 = tslib_1.__importDefault(require("express"));
const ffmpeg_1 = require("../../../helpers/ffmpeg");
const logger_1 = require("../../../helpers/logger");
const job_queue_1 = require("../../../lib/job-queue");
const hooks_1 = require("../../../lib/plugins/hooks");
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 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, hasAudio } = yield video.probeMaxQualityFile();
        const resolutions = yield hooks_1.Hooks.wrapObject((0, ffmpeg_1.computeResolutionsToTranscode)({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false, hasAudio }), 'filter:transcoding.manual.resolutions-to-transcode.result', body);
        if (resolutions.length === 0) {
            return res.sendStatus(204);
        }
        video.state = 2;
        yield video.save();
        const childrenResolutions = resolutions.filter(r => r !== maxResolution);
        logger_1.logger.info('Manually creating transcoding jobs for %s.', body.transcodingType, { childrenResolutions, maxResolution });
        const children = yield bluebird_1.default.mapSeries(childrenResolutions, resolution => {
            if (body.transcodingType === 'hls') {
                return buildHLSJobOption({
                    videoUUID: video.uuid,
                    hasAudio,
                    resolution,
                    isMaxQuality: false
                });
            }
            if (body.transcodingType === 'webtorrent') {
                return buildWebTorrentJobOption({
                    videoUUID: video.uuid,
                    hasAudio,
                    resolution
                });
            }
        });
        const parent = body.transcodingType === 'hls'
            ? yield buildHLSJobOption({
                videoUUID: video.uuid,
                hasAudio,
                resolution: maxResolution,
                isMaxQuality: false
            })
            : yield buildWebTorrentJobOption({
                videoUUID: video.uuid,
                hasAudio,
                resolution: maxResolution
            });
        yield job_queue_1.JobQueue.Instance.createJobWithChildren(parent, children);
        return res.sendStatus(204);
    });
}
function buildHLSJobOption(options) {
    const { videoUUID, hasAudio, resolution, isMaxQuality } = options;
    return (0, video_1.buildTranscodingJob)({
        type: 'new-resolution-to-hls',
        videoUUID,
        resolution,
        hasAudio,
        copyCodecs: false,
        isNewVideo: false,
        autoDeleteWebTorrentIfNeeded: false,
        isMaxQuality
    });
}
function buildWebTorrentJobOption(options) {
    const { videoUUID, hasAudio, resolution } = options;
    return (0, video_1.buildTranscodingJob)({
        type: 'new-resolution-to-webtorrent',
        videoUUID,
        isNewVideo: false,
        resolution,
        hasAudio,
        createHLSIfNeeded: false
    });
}
//# sourceMappingURL=transcoding.js.map