2020-02-27 03:48:40 +08:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
const MEDIA = Meteor.settings.public.media;
|
|
|
|
const STUN_TURN_FETCH_URL = MEDIA.stunTurnServersFetchAddress;
|
2020-05-21 12:20:46 +08:00
|
|
|
const CACHE_STUN_TURN = MEDIA.cacheStunTurnServers;
|
|
|
|
const FALLBACK_STUN_SERVER = MEDIA.fallbackStunServer;
|
|
|
|
|
|
|
|
let STUN_TURN_DICT;
|
|
|
|
let MAPPED_STUN_TURN_DICT;
|
2018-07-10 05:29:27 +08:00
|
|
|
|
|
|
|
const fetchStunTurnServers = function (sessionToken) {
|
2020-05-21 12:20:46 +08:00
|
|
|
if (STUN_TURN_DICT && CACHE_STUN_TURN) return Promise.resolve(STUN_TURN_DICT);
|
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
const handleStunTurnResponse = ({ stunServers, turnServers }) => {
|
|
|
|
if (!stunServers && !turnServers) {
|
2020-05-21 12:20:46 +08:00
|
|
|
return Promise.reject(new Error('Could not fetch STUN/TURN servers'));
|
2018-07-10 05:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const turnReply = [];
|
|
|
|
_.each(turnServers, (turnEntry) => {
|
|
|
|
const { password, url, username } = turnEntry;
|
|
|
|
turnReply.push({
|
|
|
|
urls: url,
|
|
|
|
password,
|
|
|
|
username,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-21 12:20:46 +08:00
|
|
|
const stDictionary = {
|
2018-07-10 05:29:27 +08:00
|
|
|
stun: stunServers.map(server => server.url),
|
|
|
|
turn: turnReply,
|
|
|
|
};
|
2020-05-21 12:20:46 +08:00
|
|
|
|
|
|
|
STUN_TURN_DICT = stDictionary;
|
|
|
|
|
|
|
|
return Promise.resolve(stDictionary);
|
2018-07-10 05:29:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const url = `${STUN_TURN_FETCH_URL}?sessionToken=${sessionToken}`;
|
|
|
|
return fetch(url, { credentials: 'same-origin' })
|
|
|
|
.then(res => res.json())
|
2020-05-26 01:32:24 +08:00
|
|
|
.then(handleStunTurnResponse);
|
2020-02-27 03:48:40 +08:00
|
|
|
};
|
2018-07-10 05:29:27 +08:00
|
|
|
|
2020-05-21 12:20:46 +08:00
|
|
|
const mapStunTurn = ({ stun, turn }) => {
|
|
|
|
const rtcStuns = stun.map(url => ({ urls: url }));
|
|
|
|
const rtcTurns = turn.map(t => ({ urls: t.urls, credential: t.password, username: t.username }));
|
|
|
|
return rtcStuns.concat(rtcTurns);
|
2020-02-27 03:48:40 +08:00
|
|
|
};
|
2018-07-10 05:29:27 +08:00
|
|
|
|
2020-05-21 12:20:46 +08:00
|
|
|
const getFallbackStun = () => {
|
2020-05-26 01:32:24 +08:00
|
|
|
const stun = FALLBACK_STUN_SERVER ? [FALLBACK_STUN_SERVER] : [];
|
2020-05-21 12:20:46 +08:00
|
|
|
return { stun, turn: [] };
|
2020-05-26 01:32:24 +08:00
|
|
|
};
|
2020-05-21 12:20:46 +08:00
|
|
|
|
2020-05-26 01:32:24 +08:00
|
|
|
const getMappedFallbackStun = () => (FALLBACK_STUN_SERVER ? [{ urls: FALLBACK_STUN_SERVER }] : []);
|
2020-05-21 12:20:46 +08:00
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
const fetchWebRTCMappedStunTurnServers = function (sessionToken) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
2020-05-21 12:20:46 +08:00
|
|
|
if (MAPPED_STUN_TURN_DICT && CACHE_STUN_TURN) {
|
|
|
|
return resolve(MAPPED_STUN_TURN_DICT);
|
|
|
|
}
|
|
|
|
|
2020-05-26 01:32:24 +08:00
|
|
|
const stDictionary = await fetchStunTurnServers(sessionToken);
|
2020-05-21 12:20:46 +08:00
|
|
|
|
|
|
|
MAPPED_STUN_TURN_DICT = mapStunTurn(stDictionary);
|
|
|
|
return resolve(MAPPED_STUN_TURN_DICT);
|
2018-07-10 05:29:27 +08:00
|
|
|
} catch (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-05-21 12:20:46 +08:00
|
|
|
export {
|
|
|
|
fetchStunTurnServers,
|
|
|
|
fetchWebRTCMappedStunTurnServers,
|
|
|
|
getFallbackStun,
|
|
|
|
getMappedFallbackStun,
|
|
|
|
};
|