Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

9.6 KB
Raw Blame
import { Participant } from '../../helpers/Participant';
import { setTestProperties } from '../../helpers/TestProperties';
import { config as testsConfig } from '../../helpers/TestsConfig';
import WebhookProxy from '../../helpers/WebhookProxy';
import { expectations } from '../../helpers/expectations';
import { joinJaasMuc, generateJaasToken as t } from '../../helpers/jaas';

setTestProperties(__filename, {
    requireWebhookProxy: true,
    retry: true,
    useJaas: true
});

/**
 * Tests the recording and live-streaming functionality of JaaS (including relevant webhooks) exercising the iFrame API
 * commands and functions.
 * TODO: also assert "this meeting is being recorded" notifications are show/played?
 */
describe('Recording and live-streaming', () => {
    const tenant = testsConfig.jaas.tenant;
    const customerId = tenant?.replace('vpaas-magic-cookie-', '');
    let recordingEnabled: boolean;
    let liveStreamingEnabled: boolean;
    let p: Participant;
    let webhooksProxy: WebhookProxy;

    it('setup', async () => {
        webhooksProxy = ctx.webhooksProxy;
        p = await joinJaasMuc({ iFrameApi: true, token: t({ moderator: true }) }, { roomName: ctx.roomName });

        recordingEnabled = Boolean(await p.execute(() => config.recordingService?.enabled));
        expect(recordingEnabled).toBe(expectations.jaas.recordingEnabled);

        liveStreamingEnabled = Boolean(await p.execute(() => config.liveStreaming?.enabled));
        expect(liveStreamingEnabled).toBe(expectations.jaas.liveStreamingEnabled);

        if (liveStreamingEnabled && !process.env.YTUBE_TEST_STREAM_KEY) {
            liveStreamingEnabled = false;
            console.log('Skipping live-streaming tests because YTUBE_TEST_STREAM_KEY is not set.');
        }

        await p.switchToMainFrame();
    });

    /**
     * Starts recording and asserts that the expected iFrame and JaaS events are received.
     * @param command whether to use the "command" or the "function" iFrame API.
     */
    async function startRecording(command: boolean) {
        await p.getIframeAPI().addEventListener('recordingStatusChanged');
        await p.getIframeAPI().addEventListener('recordingLinkAvailable');

        if (command) {
            await p.getIframeAPI().executeCommand('startRecording', {
                mode: 'file'
            });
        } else {
            await p.getIframeAPI().startRecording({
                mode: 'file'
            });
        }

        const jaasEvent: {
            customerId: string;
            eventType: string;
        } = await waitForRecordingStarted();

        expect('RECORDING_STARTED').toBe(jaasEvent.eventType);
        expect(jaasEvent.customerId).toBe(customerId);

        webhooksProxy.clearCache();

        const iFrameEvent = await p.driver.waitUntil(() =>
            p.getIframeAPI().getEventResult('recordingStatusChanged'), {
            timeout: 5000,
            timeoutMsg: 'recordingStatusChanged event not received'
        });

        expect(iFrameEvent.mode).toBe('file');
        expect(iFrameEvent.on).toBe(true);

        const linkEvent = await p.driver.waitUntil(() =>
            p.getIframeAPI().getEventResult('recordingLinkAvailable'), {
            timeout: 5000,
            timeoutMsg: 'recordingLinkAvailable event not received'
        });

        expect(linkEvent.link).toStartWith('https://', 'recording link');
        expect(linkEvent.link).toContain(tenant);
        expect(linkEvent.ttl).toBeGreaterThan(0);
    }

    /**
     * Waits for the RECORDING_STARTED webhook, but gives up as soon as the client reports that the start was
     * refused: a refusal (e.g. "all Jibris are busy" on a busy deployment) reaches the client within a second as
     * a recordingStatusChanged event carrying an error, and no webhook ever follows it, so waiting out the full
     * webhook timeout only delays the failure (and the retry of the spec) by two minutes.
     *
     * @returns {Promise<any>} The RECORDING_STARTED event.
     */
    async function waitForRecordingStarted(): Promise<any> {
        let started = false;
        const webhook = webhooksProxy.waitForEvent('RECORDING_STARTED').finally(() => {
            started = true;
        });

        // When the refusal wins the race below, this one keeps waiting for the rest of its timeout with nobody
        // listening; keep the eventual rejection handled so it cannot take down the worker.
        webhook.catch(() => undefined);

        const refused = (async () => {
            while (!started) {
                const event = await p.getIframeAPI().getEventResult('recordingStatusChanged');

                if (event?.error) {
                    throw new Error(`Recording failed to start: ${event.error}`);
                }

                await p.driver.pause(500);
            }
        })();

        const event = await Promise.race([ webhook, refused ]);

        // The poll loop notices the webhook on its next iteration; let it finish so it does not run alongside
        // the checks that follow.
        await refused;

        return event;
    }

    /**
     * Stops recording and asserts that the expected iFrame and JaaS events are received.
     * @param command whether to use the "command" or the "function" iFrame API.
     */
    async function stopRecording(command: boolean) {
        if (command) {
            await p.getIframeAPI().executeCommand('stopRecording', 'file');
        } else {
            await p.getIframeAPI().stopRecording('file');
        }

        const jaasEndedEvent: {
            customerId: string;
            eventType: string;
        } = await webhooksProxy.waitForEvent('RECORDING_ENDED');

        expect('RECORDING_ENDED').toBe(jaasEndedEvent.eventType);
        expect(jaasEndedEvent.customerId).toBe(customerId);

        const jaasUploadedEvent: {
            customerId: string;
            data: {
                initiatorId: string;
                participants: Array<string>;
            };
            eventType: string;
        } = await webhooksProxy.waitForEvent('RECORDING_UPLOADED');

        const jwtPayload = p.getToken()?.payload;

        expect(jaasUploadedEvent.data.initiatorId).toBe(jwtPayload?.context?.user?.id);
        expect(jaasUploadedEvent.data.participants.some(
            // @ts-ignore
            e => e.id === jwtPayload?.context?.user?.id)).toBe(true);

        webhooksProxy.clearCache();

        const iFrameEvent = (await p.getIframeAPI().getEventResult('recordingStatusChanged'));

        expect(iFrameEvent.mode).toBe('file');
        expect(iFrameEvent.on).toBe(false);

        await p.getIframeAPI().clearEventResults('recordingStatusChanged');
    }

    it('start/stop recording using the iFrame command', async function() {
        if (!recordingEnabled) {
            // eslint-disable-next-line @typescript-eslint/no-invalid-this
            this.skip();
        }

        await startRecording(true);
        await stopRecording(true);

        // to avoid rate limits
        await p.driver.pause(30000);
    });

    it('start/stop recording using the iFrame function', async function() {
        if (!recordingEnabled) {
            // eslint-disable-next-line @typescript-eslint/no-invalid-this
            this.skip();
        }

        await startRecording(false);
        await stopRecording(false);

        // to avoid rate limits
        await p.driver.pause(30000);
    });

    it('start/stop live-streaming using the iFrame command', async function() {
        if (!liveStreamingEnabled) {
            // eslint-disable-next-line @typescript-eslint/no-invalid-this
            this.skip();
        }

        await p.getIframeAPI().addEventListener('recordingStatusChanged');

        await p.getIframeAPI().executeCommand('startRecording', {
            youtubeBroadcastID: process.env.YTUBE_TEST_BROADCAST_ID,
            mode: 'stream',
            youtubeStreamKey: process.env.YTUBE_TEST_STREAM_KEY
        });

        const jaasEvent: {
            customerId: string;
            eventType: string;
        } = await webhooksProxy.waitForEvent('LIVE_STREAM_STARTED');

        expect('LIVE_STREAM_STARTED').toBe(jaasEvent.eventType);
        expect(jaasEvent.customerId).toBe(customerId);

        const iFrameEvent = await p.driver.waitUntil(async () => {
            const e = await p.getIframeAPI().getEventResult('recordingStatusChanged');

            return e && e.mode === 'stream' && e.on === true ? e : false;
        }, {
            timeout: 5000,
            timeoutMsg: 'recordingStatusChanged event with mode=stream, on=true not received'
        });

        expect(iFrameEvent.mode).toBe('stream');
        expect(iFrameEvent.on).toBe(true);

        if (process.env.YTUBE_TEST_BROADCAST_ID) {
            const liveStreamUrl = await p.getIframeAPI().getLivestreamUrl();

            expect(liveStreamUrl.livestreamUrl).toBeDefined();
        }

        await p.getIframeAPI().executeCommand('stopRecording', 'stream');

        const jaasEndedEvent: {
            customerId: string;
            eventType: string;
        } = await webhooksProxy.waitForEvent('LIVE_STREAM_ENDED');

        expect(jaasEndedEvent.eventType).toBe('LIVE_STREAM_ENDED');
        expect(jaasEndedEvent.customerId).toBe(customerId);

        const iFrameEndedEvent = await p.driver.waitUntil(async () => {
            const e = await p.getIframeAPI().getEventResult('recordingStatusChanged');

            return e && e.mode === 'stream' && e.on === false ? e : false;
        }, {
            timeout: 5000,
            timeoutMsg: 'recordingStatusChanged event with mode=stream, on=false not received'
        });

        expect(iFrameEndedEvent.mode).toBe('stream');
        expect(iFrameEndedEvent.on).toBe(false);
    });
});