Skip to content

Commit

Permalink
Integrate upstream PR ottomated#149.
Browse files Browse the repository at this point in the history
  • Loading branch information
IVLIVS-III committed Dec 30, 2020
1 parent b9d0509 commit ac1b5b5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
55 changes: 48 additions & 7 deletions src/renderer/Voice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Grid from '@material-ui/core/Grid';
import makeStyles from '@material-ui/core/styles/makeStyles';
import SupportLink from './SupportLink';
import Divider from '@material-ui/core/Divider';
import { validateClientPeerConfig } from './validateClientPeerConfig';

export interface ExtendedAudioElement extends HTMLAudioElement {
setSinkId: (sinkId: string) => Promise<void>;
Expand Down Expand Up @@ -79,6 +80,19 @@ interface SocketError {
message?: string;
}

interface ClientPeerConfig {
forceRelayOnly: boolean;
iceServers: RTCIceServer[];
}

const DEFAULT_ICE_CONFIG: RTCConfiguration = {
iceServers: [
{
urls: 'stun:stun.l.google.com:19302',
},
],
};

function calculateVoiceAudio(
state: AmongUsState,
settings: ISettings,
Expand Down Expand Up @@ -324,6 +338,39 @@ const Voice: React.FC<VoiceProps> = function ({
setConnected(false);
});

let iceConfig: RTCConfiguration = DEFAULT_ICE_CONFIG;
socket.on('clientPeerConfig', (clientPeerConfig: ClientPeerConfig) => {
if (!validateClientPeerConfig(clientPeerConfig)) {
let errorsFormatted = '';
if (validateClientPeerConfig.errors) {
errorsFormatted = validateClientPeerConfig.errors
.map((error) => error.dataPath + ' ' + error.message)
.join('\n');
}
alert(
`Server sent a malformed peer config. Default config will be used. See errors below:\n${errorsFormatted}`
);
return;
}

if (
clientPeerConfig.forceRelayOnly &&
!clientPeerConfig.iceServers.some((server) =>
server.urls.toString().includes('turn:')
)
) {
alert(
'Server has forced relay mode enabled but provides no relay servers. Default config will be used.'
);
return;
}

iceConfig = {
iceTransportPolicy: clientPeerConfig.forceRelayOnly ? 'relay' : 'all',
iceServers: clientPeerConfig.iceServers,
};
});

// Initialize variables
let audioListener: {
connect: () => void;
Expand Down Expand Up @@ -406,13 +453,7 @@ const Voice: React.FC<VoiceProps> = function ({
const connection = new Peer({
stream,
initiator,
config: {
iceServers: [
{
urls: 'stun:stun.l.google.com:19302',
},
],
},
config: iceConfig,
});
setPeerConnections((connections) => {
connections[peer] = connection;
Expand Down
37 changes: 37 additions & 0 deletions src/renderer/validateClientPeerConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Ajv from 'ajv';

export const validateClientPeerConfig = new Ajv({
format: 'full',
allErrors: true,
}).compile({
type: 'object',
properties: {
forceRelayOnly: {
type: 'boolean',
},
iceServers: {
type: 'array',
items: {
type: 'object',
properties: {
urls: {
type: ['string', 'array'],
format: 'uri',
items: {
type: 'string',
format: 'uri',
},
},
username: {
type: 'string',
},
credential: {
type: 'string',
},
},
required: ['urls'],
},
},
},
required: ['forceRelayOnly', 'iceServers'],
});

0 comments on commit ac1b5b5

Please sign in to comment.