Skip to content

Commit d1d9aba

Browse files
pkuzcoBahaa Naamneh
andauthored
feat(client): allow disabling VoIP support (#5021)
* feat(client): allow disabling VoIP support * feat(client): allow disabling VoIP support Signed-off-by: Bahaa Naamneh <[email protected]> * add a unit-test for disableVoip option * fix lint issue --------- Signed-off-by: Bahaa Naamneh <[email protected]> Co-authored-by: Bahaa Naamneh <[email protected]>
1 parent 52bcc2c commit d1d9aba

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

spec/unit/matrix-client.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,61 @@ describe("MatrixClient", function () {
23662366
});
23672367
});
23682368

2369+
describe("disableVoip option", () => {
2370+
const baseUrl = "https://alice-server.com";
2371+
const userId = "@alice:bar";
2372+
const accessToken = "sometoken";
2373+
2374+
beforeEach(() => {
2375+
mocked(supportsMatrixCall).mockReturnValue(true);
2376+
});
2377+
2378+
afterAll(() => {
2379+
mocked(supportsMatrixCall).mockReset();
2380+
});
2381+
2382+
it("should not call /voip/turnServer when disableVoip = true", () => {
2383+
fetchMock.getOnce(`${baseUrl}/_matrix/client/unstable/voip/turnServer`, 200);
2384+
2385+
const client = createClient({
2386+
baseUrl,
2387+
accessToken,
2388+
userId,
2389+
disableVoip: true,
2390+
});
2391+
2392+
// Only check createCall / supportsVoip, avoid startClient
2393+
expect(client.createCall("!roomId:example.com")).toBeNull();
2394+
expect(client.supportsVoip?.()).toBe(false);
2395+
});
2396+
2397+
it("should call /voip/turnServer when disableVoip is not set", () => {
2398+
fetchMock.getOnce(`${baseUrl}/_matrix/client/unstable/voip/turnServer`, {
2399+
uris: ["turn:turn.example.org"],
2400+
});
2401+
2402+
createClient({
2403+
baseUrl,
2404+
accessToken,
2405+
userId,
2406+
});
2407+
2408+
// The call will trigger the request if VoIP is supported
2409+
expect(fetchMock.called(`${baseUrl}/_matrix/client/unstable/voip/turnServer`)).toBe(false);
2410+
});
2411+
2412+
it("should return null from createCall when disableVoip = true", () => {
2413+
const client = createClient({
2414+
baseUrl,
2415+
accessToken,
2416+
userId,
2417+
disableVoip: true,
2418+
});
2419+
2420+
expect(client.createCall("!roomId:example.com")).toBeNull();
2421+
});
2422+
});
2423+
23692424
describe("support for ignoring invites", () => {
23702425
beforeEach(() => {
23712426
// Mockup `getAccountData`/`setAccountData`.

src/client.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,12 @@ export interface ICreateClientOpts {
443443
*/
444444
isVoipWithNoMediaAllowed?: boolean;
445445

446+
/**
447+
* Disable VoIP support (prevents fetching TURN servers, etc.)
448+
* Default: false (VoIP enabled)
449+
*/
450+
disableVoip?: boolean;
451+
446452
/**
447453
* If true, group calls will not establish media connectivity and only create the signaling events,
448454
* so that livekit media can be used in the application layer (js-sdk contains no livekit code).
@@ -1220,6 +1226,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
12201226
public idBaseUrl?: string;
12211227
public baseUrl: string;
12221228
public readonly isVoipWithNoMediaAllowed;
1229+
public disableVoip: boolean;
12231230

12241231
public useLivekitForGroupCalls: boolean;
12251232

@@ -1346,7 +1353,9 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
13461353
});
13471354
}
13481355

1349-
if (supportsMatrixCall()) {
1356+
this.disableVoip = opts.disableVoip ?? false;
1357+
1358+
if (!this.disableVoip && supportsMatrixCall()) {
13501359
this.callEventHandler = new CallEventHandler(this);
13511360
this.groupCallEventHandler = new GroupCallEventHandler(this);
13521361
this.canSupportVoip = true;
@@ -1433,7 +1442,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
14331442
}
14341443

14351444
// periodically poll for turn servers if we support voip
1436-
if (this.canSupportVoip) {
1445+
if (this.supportsVoip()) {
14371446
this.checkTurnServersIntervalID = setInterval(() => {
14381447
this.checkTurnServers();
14391448
}, TURN_CHECK_INTERVAL);
@@ -1670,7 +1679,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
16701679
* @returns True if VoIP is supported.
16711680
*/
16721681
public supportsVoip(): boolean {
1673-
return this.canSupportVoip;
1682+
return !this.disableVoip && this.canSupportVoip;
16741683
}
16751684

16761685
/**
@@ -5622,7 +5631,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
56225631

56235632
// XXX: Intended private, used in code.
56245633
public async checkTurnServers(): Promise<boolean | undefined> {
5625-
if (!this.canSupportVoip) {
5634+
if (!this.supportsVoip()) {
56265635
return;
56275636
}
56285637

0 commit comments

Comments
 (0)