"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findExternalSavedVideo = exports.waitUntilLiveWaitingOnAllServers = exports.waitUntilLiveReplacedByReplayOnAllServers = exports.waitUntilLivePublishedOnAllServers = exports.stopFfmpeg = exports.testFfmpegStreamError = exports.waitFfmpegUntilError = exports.sendRTMPStream = void 0;
const tslib_1 = require("tslib");
const fluent_ffmpeg_1 = tslib_1.__importDefault(require("fluent-ffmpeg"));
const core_utils_1 = require("../../core-utils");
function sendRTMPStream(options) {
    const { rtmpBaseUrl, streamKey, fixtureName = 'video_short.mp4', copyCodecs = false } = options;
    const fixture = (0, core_utils_1.buildAbsoluteFixturePath)(fixtureName);
    const command = (0, fluent_ffmpeg_1.default)(fixture);
    command.inputOption('-stream_loop -1');
    command.inputOption('-re');
    if (copyCodecs) {
        command.outputOption('-c copy');
    }
    else {
        command.outputOption('-c:v libx264');
        command.outputOption('-g 50');
        command.outputOption('-keyint_min 2');
        command.outputOption('-r 60');
    }
    command.outputOption('-f flv');
    const rtmpUrl = rtmpBaseUrl + '/' + streamKey;
    command.output(rtmpUrl);
    command.on('error', err => {
        var _a;
        if ((_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.includes('Exiting normally'))
            return;
        if (process.env.DEBUG)
            console.error(err);
    });
    if (process.env.DEBUG) {
        command.on('stderr', data => console.log(data));
        command.on('stdout', data => console.log(data));
    }
    command.run();
    return command;
}
exports.sendRTMPStream = sendRTMPStream;
function waitFfmpegUntilError(command, successAfterMS = 10000) {
    return new Promise((res, rej) => {
        command.on('error', err => {
            return rej(err);
        });
        setTimeout(() => {
            res();
        }, successAfterMS);
    });
}
exports.waitFfmpegUntilError = waitFfmpegUntilError;
function testFfmpegStreamError(command, shouldHaveError) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        let error;
        try {
            yield waitFfmpegUntilError(command, 35000);
        }
        catch (err) {
            error = err;
        }
        yield stopFfmpeg(command);
        if (shouldHaveError && !error)
            throw new Error('Ffmpeg did not have an error');
        if (!shouldHaveError && error)
            throw error;
    });
}
exports.testFfmpegStreamError = testFfmpegStreamError;
function stopFfmpeg(command) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        command.kill('SIGINT');
        yield (0, core_utils_1.wait)(500);
    });
}
exports.stopFfmpeg = stopFfmpeg;
function waitUntilLivePublishedOnAllServers(servers, videoId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        for (const server of servers) {
            yield server.live.waitUntilPublished({ videoId });
        }
    });
}
exports.waitUntilLivePublishedOnAllServers = waitUntilLivePublishedOnAllServers;
function waitUntilLiveWaitingOnAllServers(servers, videoId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        for (const server of servers) {
            yield server.live.waitUntilWaiting({ videoId });
        }
    });
}
exports.waitUntilLiveWaitingOnAllServers = waitUntilLiveWaitingOnAllServers;
function waitUntilLiveReplacedByReplayOnAllServers(servers, videoId) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        for (const server of servers) {
            yield server.live.waitUntilReplacedByReplay({ videoId });
        }
    });
}
exports.waitUntilLiveReplacedByReplayOnAllServers = waitUntilLiveReplacedByReplayOnAllServers;
function findExternalSavedVideo(server, liveDetails) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const include = 2;
        const privacyOneOf = [4, 3, 1, 2];
        const { data } = yield server.videos.list({ token: server.accessToken, sort: '-publishedAt', include, privacyOneOf });
        return data.find(v => v.name === liveDetails.name + ' - ' + new Date(liveDetails.publishedAt).toLocaleString());
    });
}
exports.findExternalSavedVideo = findExternalSavedVideo;
//# sourceMappingURL=live.js.map