From af2098327bc023062da0b6ac6607f1051eea9549 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 26 Sep 2022 00:47:12 +0100 Subject: [PATCH] first cut at implementing TURN --- src/matrix/Session.js | 13 +++++++++++++ src/matrix/calls/CallHandler.ts | 6 ++++++ src/matrix/net/HomeServerApi.ts | 6 +++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/matrix/Session.js b/src/matrix/Session.js index f82ad555..de05f040 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -513,6 +513,7 @@ export class Session { // TODO: what can we do if this throws? await txn.complete(); } + await this._updateTurnServers(); // enable session backup, this requests the latest backup version if (!this._keyBackup.get()) { if (dehydratedDevice) { @@ -559,6 +560,18 @@ export class Session { } } + async _updateTurnServers() { + const turnServersData = await this._hsApi.getTurnServers().response(); + this._callHandler.setTurnServers({ + urls: turnServerData.uris, + username: turnServerData.username, + credential: turnServerData.password, + }); + if (turnServersData.ttl > 0) { + setTimeout(this._updateTurnServers, turnServersData.ttl * 1000); + } + } + async _getPendingEventsByRoom(txn) { const pendingEvents = await txn.pendingEvents.getAll(); return pendingEvents.reduce((groups, pe) => { diff --git a/src/matrix/calls/CallHandler.ts b/src/matrix/calls/CallHandler.ts index ca58c4a8..93491e07 100644 --- a/src/matrix/calls/CallHandler.ts +++ b/src/matrix/calls/CallHandler.ts @@ -75,6 +75,12 @@ export class CallHandler implements RoomStateHandler { this._loadCallEntries(callEntries, txn); } + async setTurnServers(turnServers: RTCIceServer) { + this.options.turnServers = turnServers; + this.groupCallOptions.turnServers = turnServers; + // TODO: we should update any ongoing peerconnections if the TURN server details have changed + } + private async _getLoadTxn(): Promise { const names = this.options.storage.storeNames; const txn = await this.options.storage.readTxn([ diff --git a/src/matrix/net/HomeServerApi.ts b/src/matrix/net/HomeServerApi.ts index 30406c34..828a1c82 100644 --- a/src/matrix/net/HomeServerApi.ts +++ b/src/matrix/net/HomeServerApi.ts @@ -305,10 +305,14 @@ export class HomeServerApi { createRoom(payload: Record, options?: BaseRequestOptions): IHomeServerRequest { return this._post(`/createRoom`, {}, payload, options); } - + setAccountData(ownUserId: string, type: string, content: Record, options?: BaseRequestOptions): IHomeServerRequest { return this._put(`/user/${encodeURIComponent(ownUserId)}/account_data/${encodeURIComponent(type)}`, {}, content, options); } + + getTurnServer(options?: BaseRequestOptions): IHomeServerRequest { + return this._get(`/voip/turnServer`, undefined, undefined, options); + } } import {Request as MockRequest} from "../../mocks/Request.js";