Перейти к содержанию

main.js

1. Локальные переменные

Объявление локальных переменных для работы с константами, SFU SDK, для отображения локального видео и работы с конфигурацией клиента

code

const constants = SFU.constants;
const sfu = SFU;
const quality = window.ConnectionQualityBadge;
const CONNECTION_STATUS = quality.CONNECTION_STATUS;
let localDisplay;
let cControls;

2. Конфигурация по умолчанию

Объявление конфигурации комнаты и публикации потоков по умолчанию, на случай, если нет файла конфигурации config.json

code

Клиент настраивается на соединение с сервером по WSS по адресу localhost для входа в комнату "ROOM1" с пин-кодом "1234" под именем "Alice". Секция media задает публикацию аудио и видео дорожек. Видео публикуется двумя дорожками с качествами high (h) и medium (m)

const defaultConfig = {
    room: {
        url: "wss://127.0.0.1:8888",
        name: "ROOM1",
        pin: "1234",
        nickName: "Alice",
        turnServer: "",
        forceRelay: false
    },
    media: {
        audio: {
            tracks: [
                {
                    source: "mic",
                    channels: 1
                }
            ]
        },
        video: {
            tracks: [
                {
                    source: "camera",
                    width: 1280,
                    height: 720,
                    codec: "H264",
                    encodings: [
                        {rid: "m", active: true, maxBitrate: 300000, scaleResolutionDownBy: 2},
                        {rid: "h", active: true, maxBitrate: 900000}
                    ]
                }
            ]
        }
    },
    initPoolParticipantsCount: 10,
    idleTransceiverTimeoutMs: 60000
};

3. Инициализация

init() code

Функция init() вызывается после того, как страница загрузится. Функция загружает config.json или конфигурацию по умолчанию и открывает модальное окно входа

const init = function () {
    $.getJSON("config.json", function (config) {
        cControls = createControls(config);
    }).fail(function () {
        //use default config
        cControls = createControls(defaultConfig);
    });

    // insert transport values in entrance modal
    const transportSelect = document.getElementById('transport');
    Object.values(constants.SFU_TRANSPORT_TYPE).forEach(function(value) {
        const option = document.createElement('option');
        option.value = value;
        option.textContent = value;
        transportSelect.appendChild(option);
    });
    // insert participant view types in entrance modal
    const participantViewTypeSelect = document.getElementById("participantViewType");
    Object.values(PARTICIPANT_VIEW_TYPE).forEach(function(value) {
        const option = document.createElement('option');
        option.value = value;
        option.textContent = value;
        participantViewTypeSelect.appendChild(option);
    })
    //open entrance modal
    $('#entranceModal').modal('show');
}

4. Соединение с сервером и создание либо вход в комнату

connect() code

Функция вызывается по щелчку пользователя по кнопке Enter в модальном окне входа

async function connect() {
    // hide modal
    $('#entranceModal').modal('hide');
    // disable controls
    cControls.muteInput();
    //get config object for room creation
    const roomConfig = cControls.roomConfig();
    //kick off connect to server and local room creation
    try {
        const rtcConfiguration = buildRtcConfiguration(roomConfig);
        const pc = rtcConfiguration ? new RTCPeerConnection(rtcConfiguration) : new RTCPeerConnection();
        const session = await sfu.createRoom(roomConfig);
        // Now we connected to the server (if no exception was thrown)
        session.on(constants.SFU_EVENT.FAILED, function (e) {
            if (e.status && e.statusText) {
                displayError("CONNECTION FAILED: " + e.status + " " + e.statusText);
            } else if (e.type && e.info) {
                displayError("CONNECTION FAILED: " + e.info);
            } else {
                displayError("CONNECTION FAILED: " + e);
            }
        }).on(constants.SFU_EVENT.DISCONNECTED, function (e) {
            displayError("DISCONNECTED. Refresh the page to enter the room again");
        });
        const room = session.room();
        room.on(constants.SFU_ROOM_EVENT.FAILED, function (e) {
            displayError(e);
        }).on(constants.SFU_ROOM_EVENT.OPERATION_FAILED, function (e) {
            displayError(e.operation + " failed: " + e.error);
        })

        // create local display to show local streams
        localDisplay = initLocalDisplay(document.getElementById("localDisplay"));
        // display audio and video control tables
        await cControls.displayTables();
        cControls.onTrack(async function (s) {
            await publishNewTrack(room, pc, s);
        });
        //create and bind chat to the new room
        const chatDiv = document.getElementById('messages');
        const chatInput = document.getElementById('localMessage');
        const chatButton = document.getElementById('sendMessage');
        createChat(room, chatDiv, chatInput, chatButton);

        //setup remote display for showing remote audio/video tracks
        const remoteDisplay = document.getElementById("display");
        const displayOptions = {
            quality: true,
            autoAbr: false
        };
        const abrOptions = {
            thresholds: [
                {parameter: "nackCount", maxLeap: 10},
                {parameter: "freezeCount", maxLeap: 10},
                {parameter: "packetsLost", maxLeap: 10}
            ],
            abrKeepOnGoodQuality: ABR_KEEP_ON_QUALITY,
            abrTryForUpperQuality: ABR_TRY_UPPER_QUALITY,
            interval: ABR_QUALITY_CHECK_PERIOD
        };
        initDefaultRemoteDisplay(room, remoteDisplay, displayOptions, abrOptions, roomConfig.participantViewType);
        bindTrafficWidget(room);

        //get configured local video streams
        let streams = cControls.getVideoStreams();
        //combine local video streams with audio streams
        streams.push.apply(streams, cControls.getAudioStreams());

        // Publish preconfigured streams
        publishPreconfiguredStreams(room, pc, streams);
    } catch (e) {
        console.error(e);
        displayError(formatError(e));
    }
}

5. Подробнее о функции connect()

Скрытие модального окна входа и отключение полей ввода до установки соединения с сервером

code

async function connect() {
    // hide modal
    $('#entranceModal').modal('hide');
    // disable controls
    cControls.muteInput();
    ...
}

Создание объекта PeerConnection и подготовка объекта конфигурации комнаты

code

async function connect() {
    ...
    //get config object for room creation
    const roomConfig = cControls.roomConfig();
    //kick off connect to server and local room creation
    try {
        const rtcConfiguration = buildRtcConfiguration(roomConfig);
        const pc = rtcConfiguration ? new RTCPeerConnection(rtcConfiguration) : new RTCPeerConnection();
        ...
    } catch (e) {
        console.error(e);
        displayError(e);
    }
    ...
}

Создание сессии и установка соединения с сервером

code

async function connect() {
    ...
    //kick off connect to server and local room creation
    try {
        ...
        const session = await sfu.createRoom(roomConfig);
        ...
    } catch (e) {
        console.error(e);
        displayError(e);
    }
}

Подписка на события сессии

code

async function connect() {
    ...
    //kick off connect to server and local room creation
    try {
        ...
        // Now we connected to the server (if no exception was thrown)
        session.on(constants.SFU_EVENT.FAILED, function (e) {
            if (e.status && e.statusText) {
                displayError("CONNECTION FAILED: " + e.status + " " + e.statusText);
            } else if (e.type && e.info) {
                displayError("CONNECTION FAILED: " + e.info);
            } else {
                displayError("CONNECTION FAILED: " + e);
            }
        }).on(constants.SFU_EVENT.DISCONNECTED, function (e) {
            displayError("DISCONNECTED. Refresh the page to enter the room again");
        });
        ...
    } catch (e) {
        console.error(e);
        displayError(e);
    }
}

Создание объекта комнаты и подписка на сообщения об ошибках

code

async function connect() {
    ...
    //kick off connect to server and local room creation
    try {
        ...
        const room = session.room();
        room.on(constants.SFU_ROOM_EVENT.FAILED, function (e) {
            displayError(e);
        }).on(constants.SFU_ROOM_EVENT.OPERATION_FAILED, function (e) {
            displayError(e.operation + " failed: " + e.error);
        })
        ...
    } catch (e) {
        console.error(e);
        displayError(e);
    }
}

Создание объекта для отображения локального видео

code

async function connect() {
    ...
    //kick off connect to server and local room creation
    try {
        ...
        // create local display to show local streams
        localDisplay = initLocalDisplay(document.getElementById("localDisplay"));
        // display audio and video control tables
        await cControls.displayTables();
        cControls.onTrack(async function (s) {
            await publishNewTrack(room, pc, s);
        });
        ...
    } catch (e) {
        console.error(e);
        displayError(e);
    }
}

Инициализация окна чата

code

async function connect() {
    ...
    //kick off connect to server and local room creation
    try {
        ...
        //create and bind chat to the new room
        const chatDiv = document.getElementById('messages');
        const chatInput = document.getElementById('localMessage');
        const chatButton = document.getElementById('sendMessage');
        createChat(room, chatDiv, chatInput, chatButton);
        ...
    } catch (e) {
        console.error(e);
        displayError(e);
    }
}

Инициализация объекта для отображения потоков от других участников

code

async function connect() {
    ...
    //kick off connect to server and local room creation
    try {
        ...
        //setup remote display for showing remote audio/video tracks
        const remoteDisplay = document.getElementById("display");
        const displayOptions = {
            quality: true,
            autoAbr: false
        };
        const abrOptions = {
            thresholds: [
                {parameter: "nackCount", maxLeap: 10},
                {parameter: "freezeCount", maxLeap: 10},
                {parameter: "packetsLost", maxLeap: 10}
            ],
            abrKeepOnGoodQuality: ABR_KEEP_ON_QUALITY,
            abrTryForUpperQuality: ABR_TRY_UPPER_QUALITY,
            interval: ABR_QUALITY_CHECK_PERIOD
        };
        initDefaultRemoteDisplay(room, remoteDisplay, displayOptions, abrOptions, roomConfig.participantViewType);
        ...
    } catch (e) {
        console.error(e);
        displayError(e);
    }
}

Привязка виджета отображения трафика к данным комнаты и запуск отображения состояния канала

code

async function connect() {
    ...
    //kick off connect to server and local room creation
    try {
        ...
        bindTrafficWidget(room);
        ...
    } catch (e) {
        console.error(e);
        displayError(e);
    }
}

Получение настроек и публикация локальных медиа потоков

code

async function connect() {
    ...
    //kick off connect to server and local room creation
    try {
        ...
        //get configured local video streams
        let streams = cControls.getVideoStreams();
        //combine local video streams with audio streams
        streams.push.apply(streams, cControls.getAudioStreams());

        // Publish preconfigured streams
        publishPreconfiguredStreams(room, pc, streams);
    } catch (e) {
        console.error(e);
        displayError(e);
    }
}

6. Вход в комнату и публикация локальных потоков из файла конфигурации

publishPreconfiguredStreams(), Room.join() code

const publishPreconfiguredStreams = async function (room, pc, streams) {
    try {
        const config = {};
        //add our local streams to the room (to PeerConnection)
        streams.forEach(function (s) {
            let contentType = s.type || s.source;
            //add each track to PeerConnection
            s.stream.getTracks().forEach((track) => {
                config[track.id] = contentType;
                addTrackToPeerConnection(pc, s.stream, track, s.encodings);
                subscribeTrackToEndedEvent(room, track, pc);
            });
            localDisplay.add(s.stream.id, "local", s.stream, contentType);
        });
        //join room
        const transportType = cControls.roomConfig().transport;
        await room.join(pc, null, config, cControls.initPoolParticipantsCount(), cControls.idleTransceiverTimeoutMs(), transportType);
        // Enable Delete button for each preconfigured stream #WCS-3689
        streams.forEach(function (s) {
            $('#' + s.stream.id + "-button").prop('disabled', false);
        });
        ...
    } catch (e) {
        onOperationFailed("Failed to publish a preconfigured streams", e);
        // Enable Delete button for each preconfigured stream #WCS-3689
        streams.forEach(function (s) {
            $('#' + s.stream.id + "-button").prop('disabled', false);
        });
    }
}

7. Публикация дополнительных локальных потоков

publishNewTrack(), Room.updateState() code

const publishNewTrack = async function (room, pc, media) {
    try {
        let config = {};
        //add local stream to local display
        let contentType = media.type || media.source;

        localDisplay.add(media.stream.id, "local", media.stream, contentType);
        //add each track to PeerConnection
        media.stream.getTracks().forEach((track) => {
            config[track.id] = contentType;
            addTrackToPeerConnection(pc, media.stream, track, media.encodings);
            subscribeTrackToEndedEvent(room, track, pc);
        });
        // Clean error message
        displayError("");
        //kickoff renegotiation
        await room.updateState(config);
        // Enable Delete button for a new stream #WCS-3689
        $('#' + media.stream.id + "-button").prop('disabled', false);
    } catch (e) {
        onOperationFailed("Failed to publish a new track", e);
        // Enable Delete button for a new stream #WCS-3689
        $('#' + media.stream.id + "-button").prop('disabled', false);
    }
}

8. Завершение публикации потока

subscribeTrackToEndedEvent() code

Вспомогательная функция, которая подписывается на событие "ended" для локального потока. При получении события поток удаляется из PeerConnection, и состояние комнаты обновляется

const subscribeTrackToEndedEvent = function (room, track, pc) {
    track.addEventListener("ended", async function () {
        try {
            //track ended, see if we need to cleanup
            let negotiate = false;
            for (const sender of pc.getSenders()) {
                if (sender.track === track) {
                    pc.removeTrack(sender);
                    //track found, set renegotiation flag
                    negotiate = true;
                    if (sender.track) {
                        sender.track.stop();
                        sender.track.active = false;
                    }
                    break;
                }
            }
            // Clean error message
            displayError("");
            if (negotiate) {
                //kickoff renegotiation
                await room.updateState();
                pc.restartIce();
            }
        } catch (e) {
            onOperationFailed("Failed to update room state", e);
        }
    });
}

9. Добавление новой дорожки в PeerConnection

addTrackToPeerConnection() code

Вспомогательная функция, которая добавляет новую дорожку в PeerConnection для публикации

const addTrackToPeerConnection = function (pc, stream, track, encodings) {
    if (encodings) {
        for (const encoding of encodings) {
            if (encoding.scalabilityMode === "") {
                delete encoding.scalabilityMode;
            }
        }
    }
    pc.addTransceiver(track, {
        direction: "sendonly",
        streams: [stream],
        sendEncodings: encodings ? encodings : [] //passing encoding types for video simulcast tracks
    });
}

10. Отображение состояния канала

bindTrafficWidget code

const bindTrafficWidget = function (room) {
    const SERVER_TRAFFIC_TTL_MS = 3000;
    let lastClientTraffic = null;
    let lastServerTraffic = null;
    let lastServerTrafficAt = 0;
    let lastKnownPing = 0;

    const isServerTrafficFresh = function () {
        return lastServerTraffic && Date.now() - lastServerTrafficAt <= SERVER_TRAFFIC_TTL_MS;
    };

    const resolvePing = function (clientTraffic, serverTraffic) {
        if (serverTraffic && typeof serverTraffic.ping === "number" && !Number.isNaN(serverTraffic.ping) && serverTraffic.ping > 0) {
            return serverTraffic.ping;
        }
        if (clientTraffic && typeof clientTraffic.ping === "number" && !Number.isNaN(clientTraffic.ping) && clientTraffic.ping > 0) {
            return clientTraffic.ping;
        }
        return lastKnownPing;
    };

    const hasActiveRoute = function (traffic) {
        const links = Array.isArray(traffic && traffic.badges && traffic.badges.links) ? traffic.badges.links : [];
        if (links.length === 0) {
            return false;
        }
        return links.some(function (link) {
            return link.active === true || link.connected === true || link.status === CONNECTION_STATUS.GREEN || link.status === CONNECTION_STATUS.YELLOW;
        });
    };

    const updatePingMetric = function (clientTraffic, serverTraffic) {
        const ping = resolvePing(clientTraffic, serverTraffic);
        if (ping > 0) {
            lastKnownPing = ping;
        }
        const routeActive = hasActiveRoute(serverTraffic) || hasActiveRoute(clientTraffic);
        const pingQuality = quality.evaluateConnectionBadgeQuality({
            connected: routeActive || lastKnownPing > 0,
            ping: ping
        });
        document.getElementById("stat-ping").innerText = ping > 0 ? Math.round(ping) + " ms" : (routeActive ? "Connected" : "—");
        updateStatusCircle("ping-status", ping > 0 ? pingQuality.status : routeActive);
    };

    const updateClientTraffic = function (traffic) {
        if (!traffic) {
            return;
        }

        lastClientTraffic = traffic;
        const serverTraffic = isServerTrafficFresh() ? lastServerTraffic : null;
        const pingTraffic = serverTraffic || lastServerTraffic;
        const topologyBadges = quality.resolveTopologyBadges(traffic, serverTraffic);

        document.getElementById("stat-outbound").innerText = quality.formatSpeed(traffic.outboundBitrate);
        document.getElementById("stat-inbound").innerText = quality.formatSpeed(traffic.inboundBitrate);
        updatePingMetric(traffic, pingTraffic);
        updateStatusCircle("outbound-status", traffic.outboundBitrate > 0);
        updateStatusCircle("inbound-status", traffic.inboundBitrate > 0);

        document.getElementById("stat-participants").innerText = topologyBadges.participants.length;
        updateStatusCircle("participants-status", topologyBadges.participants.length > 0);
        updateRouteIndicator(topologyBadges);
        renderConnectionBadges(quality.createParticipantBadgeModel(topologyBadges, traffic, serverTraffic));
    };

    const updateServerTraffic = function (serverTraffic) {
        if (!serverTraffic) {
            return;
        }
        lastServerTraffic = serverTraffic;
        lastServerTrafficAt = Date.now();

        const traffic = lastClientTraffic;
        if (traffic) {
            document.getElementById("stat-outbound").innerText = quality.formatSpeed(traffic.outboundBitrate);
            document.getElementById("stat-inbound").innerText = quality.formatSpeed(traffic.inboundBitrate);

            updateStatusCircle("outbound-status", traffic.outboundBitrate > 0);
            updateStatusCircle("inbound-status", traffic.inboundBitrate > 0);
        }
        updatePingMetric(traffic, serverTraffic);

        const topologyBadges = quality.resolveTopologyBadges(lastClientTraffic, serverTraffic);
        document.getElementById("stat-participants").innerText = topologyBadges.participants.length;
        updateStatusCircle("participants-status", topologyBadges.participants.length > 0);
        updateRouteIndicator(topologyBadges);

        const badgeModel = quality.createParticipantBadgeModel(topologyBadges, lastClientTraffic, serverTraffic);
        renderConnectionBadges(badgeModel);
    };

    const clientListener = function (traffic) {
        updateClientTraffic(traffic);
    };

    room.addTrafficListener(clientListener);

    room.getTraffic().then(function (traffic) {
        updateClientTraffic(traffic);
    });

    room.addServerTrafficListener(updateServerTraffic);

    const cleanup = function () {
        room.removeTrafficListener(clientListener);
        room.removeServerTrafficListener(updateServerTraffic);
    };
    room.on(constants.SFU_ROOM_EVENT.LEFT, function (participant) {
        if (participant && participant.userId === room.userId()) {
            cleanup();
        }
    });
    room.on(constants.SFU_ROOM_EVENT.ENDED, cleanup);
};

11. Подробнее о функции bindTrafficWidget()

Проверка, обновились ли значения трафика, полученные от сервера

code

    const isServerTrafficFresh = function () {
        return lastServerTraffic && Date.now() - lastServerTrafficAt <= SERVER_TRAFFIC_TTL_MS;
    };

Получение актуального значения метрики ping

code

    const resolvePing = function (clientTraffic, serverTraffic) {
        if (serverTraffic && typeof serverTraffic.ping === "number" && !Number.isNaN(serverTraffic.ping) && serverTraffic.ping > 0) {
            return serverTraffic.ping;
        }
        if (clientTraffic && typeof clientTraffic.ping === "number" && !Number.isNaN(clientTraffic.ping) && clientTraffic.ping > 0) {
            return clientTraffic.ping;
        }
        return lastKnownPing;
    };

Проверка, есть ли активный маршрут для пропуска трафика между клиентом и сервером

code

    const hasActiveRoute = function (traffic) {
        const links = Array.isArray(traffic && traffic.badges && traffic.badges.links) ? traffic.badges.links : [];
        if (links.length === 0) {
            return false;
        }
        return links.some(function (link) {
            return link.active === true || link.connected === true || link.status === CONNECTION_STATUS.GREEN || link.status === CONNECTION_STATUS.YELLOW;
        });
    };

Обновление отображения метрики ping

code

    const updatePingMetric = function (clientTraffic, serverTraffic) {
        const ping = resolvePing(clientTraffic, serverTraffic);
        if (ping > 0) {
            lastKnownPing = ping;
        }
        const routeActive = hasActiveRoute(serverTraffic) || hasActiveRoute(clientTraffic);
        const pingQuality = quality.evaluateConnectionBadgeQuality({
            connected: routeActive || lastKnownPing > 0,
            ping: ping
        });
        document.getElementById("stat-ping").innerText = ping > 0 ? Math.round(ping) + " ms" : (routeActive ? "Connected" : "—");
        updateStatusCircle("ping-status", ping > 0 ? pingQuality.status : routeActive);
    };

Обновление измеренного значения локального медиатрафика

code

    const updateClientTraffic = function (traffic) {
        if (!traffic) {
            return;
        }

        lastClientTraffic = traffic;
        const serverTraffic = isServerTrafficFresh() ? lastServerTraffic : null;
        const pingTraffic = serverTraffic || lastServerTraffic;
        const topologyBadges = quality.resolveTopologyBadges(traffic, serverTraffic);

        document.getElementById("stat-outbound").innerText = quality.formatSpeed(traffic.outboundBitrate);
        document.getElementById("stat-inbound").innerText = quality.formatSpeed(traffic.inboundBitrate);
        updatePingMetric(traffic, pingTraffic);
        updateStatusCircle("outbound-status", traffic.outboundBitrate > 0);
        updateStatusCircle("inbound-status", traffic.inboundBitrate > 0);

        document.getElementById("stat-participants").innerText = topologyBadges.participants.length;
        updateStatusCircle("participants-status", topologyBadges.participants.length > 0);
        updateRouteIndicator(topologyBadges);
        renderConnectionBadges(quality.createParticipantBadgeModel(topologyBadges, traffic, serverTraffic));
    };

Обновление значения медиатрафика, которое получено от сервера

code

    const updateServerTraffic = function (serverTraffic) {
        if (!serverTraffic) {
            return;
        }
        lastServerTraffic = serverTraffic;
        lastServerTrafficAt = Date.now();

        const traffic = lastClientTraffic;
        if (traffic) {
            document.getElementById("stat-outbound").innerText = quality.formatSpeed(traffic.outboundBitrate);
            document.getElementById("stat-inbound").innerText = quality.formatSpeed(traffic.inboundBitrate);

            updateStatusCircle("outbound-status", traffic.outboundBitrate > 0);
            updateStatusCircle("inbound-status", traffic.inboundBitrate > 0);
        }
        updatePingMetric(traffic, serverTraffic);

        const topologyBadges = quality.resolveTopologyBadges(lastClientTraffic, serverTraffic);
        document.getElementById("stat-participants").innerText = topologyBadges.participants.length;
        updateStatusCircle("participants-status", topologyBadges.participants.length > 0);
        updateRouteIndicator(topologyBadges);

        const badgeModel = quality.createParticipantBadgeModel(topologyBadges, lastClientTraffic, serverTraffic);
        renderConnectionBadges(badgeModel);
    };

Функция прослушивания локального трафика в комнате

code

    const clientListener = function (traffic) {
        updateClientTraffic(traffic);
    };

Добавления функций прослушивания и обновления данных в объект комнаты

code

    room.addTrafficListener(clientListener);

    room.getTraffic().then(function (traffic) {
        updateClientTraffic(traffic);
    });

    room.addServerTrafficListener(updateServerTraffic);

    const cleanup = function () {
        room.removeTrafficListener(clientListener);
        room.removeServerTrafficListener(updateServerTraffic);
    };
    room.on(constants.SFU_ROOM_EVENT.LEFT, function (participant) {
        if (participant && participant.userId === room.userId()) {
            cleanup();
        }
    });
    room.on(constants.SFU_ROOM_EVENT.ENDED, cleanup);

12. Построение объекта конфигурации RTC для RTCPeerConnection

buildRtcConfiguration code

const buildRtcConfiguration = function (roomConfig) {
    const iceServers = (roomConfig.turnServer || "")
        .split(/[\n,]+/)
        .map(parseIceServerEntry)
        .filter(Boolean);
    const hasTurnServer = iceServers.some(function (server) {
        const urls = Array.isArray(server.urls) ? server.urls : [server.urls];
        return urls.some(function (url) {
            return /^turns?:/i.test(url);
        });
    });

    if (iceServers.length === 0 && !roomConfig.forceRelay) {
        return undefined;
    }
    if (roomConfig.forceRelay && !hasTurnServer) {
        throw new Error("Force relay requires a TURN server. Fill TURN Server, for example turn:user:password@turn.example.com:3478?transport=udp.");
    }

    const rtcConfiguration = {
        iceServers: iceServers
    };

    if (roomConfig.forceRelay) {
        rtcConfiguration.iceTransportPolicy = "relay";
    }

    return rtcConfiguration;
};

12. Вспомогательная функция для разбора JSON объекта iceServers

parseIceServerEntry code

const parseIceServerEntry = function (value) {
    const trimmedValue = value.trim();
    if (!trimmedValue) {
        return null;
    }

    let urls = trimmedValue;
    if (!/^(stun|turn|turns):/i.test(urls)) {
        urls = "turn:" + urls;
    }

    const credentialMatch = urls.match(/^(stun|turn|turns):([^@]+)@(.+)$/i);
    if (!credentialMatch) {
        return {urls: urls};
    }

    const server = {
        urls: credentialMatch[1] + ":" + credentialMatch[3]
    };
    const authParts = credentialMatch[2].split(":");
    const username = authParts.shift() || "";
    const credential = authParts.join(":");

    if (username) {
        server.username = username;
    }
    if (credential) {
        server.credential = credential;
        server.credentialType = "password";
    }

    return server;
};