first cut at implementing TURN

This commit is contained in:
Matthew Hodgson 2022-09-26 00:47:12 +01:00
parent 7ce5cdfc4a
commit af2098327b
3 changed files with 24 additions and 1 deletions

View File

@ -513,6 +513,7 @@ export class Session {
// TODO: what can we do if this throws? // TODO: what can we do if this throws?
await txn.complete(); await txn.complete();
} }
await this._updateTurnServers();
// enable session backup, this requests the latest backup version // enable session backup, this requests the latest backup version
if (!this._keyBackup.get()) { if (!this._keyBackup.get()) {
if (dehydratedDevice) { 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) { async _getPendingEventsByRoom(txn) {
const pendingEvents = await txn.pendingEvents.getAll(); const pendingEvents = await txn.pendingEvents.getAll();
return pendingEvents.reduce((groups, pe) => { return pendingEvents.reduce((groups, pe) => {

View File

@ -75,6 +75,12 @@ export class CallHandler implements RoomStateHandler {
this._loadCallEntries(callEntries, txn); 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<Transaction> { private async _getLoadTxn(): Promise<Transaction> {
const names = this.options.storage.storeNames; const names = this.options.storage.storeNames;
const txn = await this.options.storage.readTxn([ const txn = await this.options.storage.readTxn([

View File

@ -309,6 +309,10 @@ export class HomeServerApi {
setAccountData(ownUserId: string, type: string, content: Record<string, any>, options?: BaseRequestOptions): IHomeServerRequest { setAccountData(ownUserId: string, type: string, content: Record<string, any>, options?: BaseRequestOptions): IHomeServerRequest {
return this._put(`/user/${encodeURIComponent(ownUserId)}/account_data/${encodeURIComponent(type)}`, {}, content, options); 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"; import {Request as MockRequest} from "../../mocks/Request.js";