From ed6daffa0c2aaa1b35f18ba314a0df5d305e8a9c Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Thu, 30 Jun 2022 10:06:21 +0200 Subject: [PATCH] [MM-45001] Add support for generating short-lived TURN credentials (#6404) * Generate TURN credentials if needed * Reduce calls to getState --- app/products/calls/client/rest.ts | 7 +++++++ app/products/calls/store/actions/calls.ts | 8 +++++++- app/products/calls/store/types/calls.ts | 4 +++- types/modules/react-native-webrtc.d.ts | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/products/calls/client/rest.ts b/app/products/calls/client/rest.ts index 84742b62e..d4120d2dc 100644 --- a/app/products/calls/client/rest.ts +++ b/app/products/calls/client/rest.ts @@ -59,6 +59,13 @@ const ClientCalls = (superclass: any) => class extends superclass { {method: 'post'}, ); }; + + genTURNCredentials = async () => { + return this.doFetch( + `${this.getCallsRoute()}/turn-credentials`, + {method: 'get'}, + ); + }; }; export default ClientCalls; diff --git a/app/products/calls/store/actions/calls.ts b/app/products/calls/store/actions/calls.ts index 076bff2a2..850133a5a 100644 --- a/app/products/calls/store/actions/calls.ts +++ b/app/products/calls/store/actions/calls.ts @@ -202,7 +202,13 @@ export function joinCall(channelId: string, intl: typeof intlShape): ActionFunc dispatch(setSpeakerphoneOn(false)); try { - ws = await newClient(channelId, getICEServersConfigs(getState()), () => { + const state = getState(); + const iceConfigs = [...getICEServersConfigs(state)]; + if (getConfig(state).NeedsTURNCredentials) { + iceConfigs.push(...await Client4.genTURNCredentials()); + } + + ws = await newClient(channelId, iceConfigs, () => { dispatch(setSpeakerphoneOn(false)); dispatch({type: CallsTypes.RECEIVED_MYSELF_LEFT_CALL}); }, setScreenShareURL); diff --git a/app/products/calls/store/types/calls.ts b/app/products/calls/store/types/calls.ts index e56f9bc93..cabdb9b83 100644 --- a/app/products/calls/store/types/calls.ts +++ b/app/products/calls/store/types/calls.ts @@ -66,6 +66,7 @@ export type ServerConfig = { AllowEnableCalls: boolean; DefaultEnabled: boolean; MaxCallParticipants: number; + NeedsTURNCredentials: boolean; sku_short_name: string; last_retrieved_at: number; } @@ -76,8 +77,9 @@ export const DefaultServerConfig = { AllowEnableCalls: false, DefaultEnabled: false, MaxCallParticipants: 0, + NeedsTURNCredentials: false, sku_short_name: '', last_retrieved_at: 0, } as ServerConfig; -export type ICEServersConfigs = ConfigurationParamWithUrls[] | ConfigurationParamWithUrl[]; +export type ICEServersConfigs = Array; diff --git a/types/modules/react-native-webrtc.d.ts b/types/modules/react-native-webrtc.d.ts index 1e0f68827..0369d34ec 100644 --- a/types/modules/react-native-webrtc.d.ts +++ b/types/modules/react-native-webrtc.d.ts @@ -168,7 +168,7 @@ declare module 'react-native-webrtc' { } export interface RTCPeerConnectionConfiguration { - iceServers: ConfigurationParamWithUrls[] | ConfigurationParamWithUrl[]; + iceServers: Array; iceTransportPolicy?: 'all' | 'relay' | 'nohost' | 'none' | undefined; bundlePolicy?: 'balanced' | 'max-compat' | 'max-bundle' | undefined; rtcpMuxPolicy?: 'negotiate' | 'require' | undefined;