Skip to content

Commit aeba276

Browse files
pierre-lehnen-rcgaolin1
authored andcommitted
chore: improve voip lib configs for better mobile compatibility (RocketChat#37265)
1 parent ec46231 commit aeba276

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

packages/media-signaling/src/definition/call/CallEvents.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export type CallEvents = {
1414
/* Triggered as soon as the call is registered on this client */
1515
initialized: void;
1616

17+
/* Triggered when the call is confirmed to exist in the server */
18+
confirmed: void;
19+
1720
/* Triggered when this client is telling the server that we want to accept the call */
1821
accepting: void;
1922
/* Triggered when the call's state on the server changes to 'accepted' */

packages/media-signaling/src/definition/call/IClientMediaCall.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export interface IClientMediaCall {
9090
audioLevel: number;
9191
localAudioLevel: number;
9292

93+
/** if the call was requested by this session, then this will have the ID used to request the call, otherwise it will be the same as callId */
94+
readonly tempCallId: string;
95+
/** confirmed indicates if the call exists on the server */
96+
readonly confirmed: boolean;
97+
9398
emitter: Emitter<CallEvents>;
9499

95100
getRemoteMediaStream(): MediaStream;

packages/media-signaling/src/lib/Call.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface IClientMediaCallConfig {
3434
sessionId: string;
3535

3636
iceGatheringTimeout: number;
37+
iceServers: RTCIceServer[];
3738
}
3839

3940
const TIMEOUT_TO_ACCEPT = 30000;
@@ -143,6 +144,14 @@ export class ClientMediaCall implements IClientMediaCall {
143144
return !this.isPendingAcceptance() && !this.isOver();
144145
}
145146

147+
public get confirmed(): boolean {
148+
return this.hasRemoteData;
149+
}
150+
151+
public get tempCallId(): string {
152+
return this.localCallId;
153+
}
154+
146155
protected webrtcProcessor: IWebRTCProcessor | null = null;
147156

148157
private acceptedLocally: boolean;
@@ -357,6 +366,7 @@ export class ClientMediaCall implements IClientMediaCall {
357366
if (!wasInitialized) {
358367
this.emitter.emit('initialized');
359368
}
369+
this.emitter.emit('confirmed');
360370

361371
await this.processEarlySignals();
362372
}
@@ -1163,7 +1173,13 @@ export class ClientMediaCall implements IClientMediaCall {
11631173
this.throwError('webrtc-not-implemented');
11641174
}
11651175

1166-
this.webrtcProcessor = webrtcFactory({ logger, iceGatheringTimeout, call: this, inputTrack: this.inputTrack });
1176+
this.webrtcProcessor = webrtcFactory({
1177+
logger,
1178+
iceGatheringTimeout,
1179+
call: this,
1180+
inputTrack: this.inputTrack,
1181+
...(this.config.iceServers.length && { rtc: { iceServers: this.config.iceServers } }),
1182+
});
11671183
this.webrtcProcessor.emitter.on('internalStateChange', (stateName) => this.onWebRTCInternalStateChange(stateName));
11681184

11691185
this.negotiationManager.emitter.on('local-sdp', ({ sdp, negotiationId }) => this.deliverSdp({ sdp, negotiationId }));

packages/media-signaling/src/lib/Session.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type MediaSignalingSessionConfig = {
3030
randomStringFactory: RandomStringFactory;
3131
transport: MediaSignalTransport<ClientMediaSignal>;
3232
iceGatheringTimeout?: number;
33+
iceServers?: RTCIceServer[];
3334
};
3435

3536
const STATE_REPORT_INTERVAL = 60000;
@@ -90,7 +91,7 @@ export class MediaSignalingSession extends Emitter<MediaSignalingEvents> {
9091
}
9192

9293
public isBusy(): boolean {
93-
return this.getMainCall()?.busy ?? false;
94+
return this.getMainCall(false)?.busy ?? false;
9495
}
9596

9697
public enableStateReport(interval: number): void {
@@ -126,14 +127,17 @@ export class MediaSignalingSession extends Emitter<MediaSignalingEvents> {
126127
return this.knownCalls.get(callId) || null;
127128
}
128129

129-
public getMainCall(): IClientMediaCall | null {
130+
public getMainCall(skipLocal = false): IClientMediaCall | null {
130131
let ringingCall: IClientMediaCall | null = null;
131132
let pendingCall: IClientMediaCall | null = null;
132133

133134
for (const call of this.knownCalls.values()) {
134135
if (call.state === 'hangup' || call.ignored) {
135136
continue;
136137
}
138+
if (skipLocal && !call.confirmed) {
139+
continue;
140+
}
137141

138142
if (call.busy) {
139143
return call;
@@ -193,6 +197,10 @@ export class MediaSignalingSession extends Emitter<MediaSignalingEvents> {
193197

194198
public async startCall(calleeType: CallActorType, calleeId: string, params: { contactInfo?: CallContact } = {}): Promise<void> {
195199
this.config.logger?.debug('MediaSignalingSession.startCall', calleeId);
200+
if (this.getMainCall(false)) {
201+
throw new Error(`Already on a call.`);
202+
}
203+
196204
const { contactInfo } = params;
197205

198206
const callId = this.createTemporaryCallId();
@@ -215,6 +223,10 @@ export class MediaSignalingSession extends Emitter<MediaSignalingEvents> {
215223
this.config.iceGatheringTimeout = newTimeout;
216224
}
217225

226+
public setIceServers(iceServers: RTCIceServer[]): void {
227+
this.config.iceServers = iceServers;
228+
}
229+
218230
private createTemporaryCallId(): string {
219231
return `${this._sessionId}-${this.config.randomStringFactory()}`;
220232
}
@@ -450,7 +462,8 @@ export class MediaSignalingSession extends Emitter<MediaSignalingEvents> {
450462
logger: this.config.logger,
451463
transporter: this.transporter,
452464
processorFactories: this.config.processorFactories,
453-
iceGatheringTimeout: this.config.iceGatheringTimeout || 1000,
465+
iceGatheringTimeout: this.config.iceGatheringTimeout || 5000,
466+
iceServers: this.config.iceServers || [],
454467
sessionId: this._sessionId,
455468
};
456469

@@ -462,6 +475,7 @@ export class MediaSignalingSession extends Emitter<MediaSignalingEvents> {
462475
call.emitter.on('clientStateChange', () => this.onCallClientStateChange(call));
463476
call.emitter.on('trackStateChange', () => this.onTrackStateChange(call));
464477
call.emitter.on('initialized', () => this.onNewCall(call));
478+
call.emitter.on('confirmed', () => this.onConfirmedCall(call));
465479
call.emitter.on('accepted', () => this.onAcceptedCall(call));
466480
call.emitter.on('accepting', () => this.onAcceptingCall(call));
467481
call.emitter.on('hidden', () => this.onHiddenCall(call));
@@ -491,6 +505,11 @@ export class MediaSignalingSession extends Emitter<MediaSignalingEvents> {
491505
this.onSessionStateChange();
492506
}
493507

508+
private onConfirmedCall(_call: ClientMediaCall): void {
509+
this.config.logger?.debug('MediaSignalingSession.onConfirmedCall');
510+
this.onSessionStateChange();
511+
}
512+
494513
private onAcceptedCall(_call: ClientMediaCall): void {
495514
this.config.logger?.debug('MediaSignalingSession.onAcceptedCall');
496515
this.onSessionStateChange();
@@ -523,15 +542,16 @@ export class MediaSignalingSession extends Emitter<MediaSignalingEvents> {
523542
}
524543

525544
private onSessionStateChange(): void {
526-
const mainCall = this.getMainCall();
527-
const hasCall = Boolean(mainCall);
528-
const hasVisibleCall = Boolean(mainCall && !mainCall.hidden);
529-
const hasBusyCall = Boolean(hasVisibleCall && mainCall?.busy);
530-
531545
const hadCall = this.lastState.hasCall;
532546
const hadVisibleCall = this.lastState.hasVisibleCall;
533547
const hadBusyCall = this.lastState.hasBusyCall;
534548

549+
// Do not skip local calls if we transitioned from a different active call to it
550+
const mainCall = this.getMainCall(!hadCall);
551+
const hasCall = Boolean(mainCall);
552+
const hasVisibleCall = Boolean(mainCall && !mainCall.hidden);
553+
const hasBusyCall = Boolean(hasVisibleCall && mainCall?.busy);
554+
535555
this.lastState = { hasCall, hasVisibleCall, hasBusyCall };
536556

537557
if (mainCall && !hadCall) {

0 commit comments

Comments
 (0)