Skip to content

Commit

Permalink
Don't expose calls on GroupCall
Browse files Browse the repository at this point in the history
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
  • Loading branch information
SimonBrandner committed Dec 4, 2022
1 parent 0af2f79 commit f812ea9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap

this.feeds.push(new CallFeed({
client: this.client,
call: this,
roomId: this.roomId,
userId,
deviceId: this.getOpponentDeviceId(),
Expand Down Expand Up @@ -699,6 +700,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap

this.feeds.push(new CallFeed({
client: this.client,
call: this,
roomId: this.roomId,
audioMuted: false,
videoMuted: false,
Expand Down
34 changes: 34 additions & 0 deletions src/webrtc/callFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { MatrixClient } from "../client";
import { RoomMember } from "../models/room-member";
import { logger } from "../logger";
import { TypedEventEmitter } from "../models/typed-event-emitter";
import { CallEvent, CallState, MatrixCall } from "./call";

const POLLING_INTERVAL = 200; // ms
export const SPEAKING_THRESHOLD = -60; // dB
Expand All @@ -40,13 +41,18 @@ export interface ICallFeedOpts {
* Whether or not the remote SDPStreamMetadata says video is muted
*/
videoMuted: boolean;
/**
* The MatrixCall which is the source of this CallFeed
*/
call?: MatrixCall;
}

export enum CallFeedEvent {
NewStream = "new_stream",
MuteStateChanged = "mute_state_changed",
LocalVolumeChanged = "local_volume_changed",
VolumeChanged = "volume_changed",
ConnectedChanged = "connected_changed",
Speaking = "speaking",
Disposed = "disposed",
}
Expand All @@ -56,6 +62,7 @@ type EventHandlerMap = {
[CallFeedEvent.MuteStateChanged]: (audioMuted: boolean, videoMuted: boolean) => void;
[CallFeedEvent.LocalVolumeChanged]: (localVolume: number) => void;
[CallFeedEvent.VolumeChanged]: (volume: number) => void;
[CallFeedEvent.ConnectedChanged]: (connected: boolean) => void;
[CallFeedEvent.Speaking]: (speaking: boolean) => void;
[CallFeedEvent.Disposed]: () => void;
};
Expand All @@ -69,6 +76,7 @@ export class CallFeed extends TypedEventEmitter<CallFeedEvent, EventHandlerMap>
public speakingVolumeSamples: number[];

private client: MatrixClient;
private call?: MatrixCall;
private roomId?: string;
private audioMuted: boolean;
private videoMuted: boolean;
Expand All @@ -81,11 +89,13 @@ export class CallFeed extends TypedEventEmitter<CallFeedEvent, EventHandlerMap>
private speaking = false;
private volumeLooperTimeout?: ReturnType<typeof setTimeout>;
private _disposed = false;
private _connected = false;

public constructor(opts: ICallFeedOpts) {
super();

this.client = opts.client;
this.call = opts.call;
this.roomId = opts.roomId;
this.userId = opts.userId;
this.deviceId = opts.deviceId;
Expand All @@ -101,6 +111,21 @@ export class CallFeed extends TypedEventEmitter<CallFeedEvent, EventHandlerMap>
if (this.hasAudioTrack) {
this.initVolumeMeasuring();
}

if (opts.call) {
opts.call.addListener(CallEvent.State, this.onCallState);
this.onCallState(opts.call.state);
}
}

public get connected(): boolean {
// Local feeds are always considered connected
return this.isLocal() || this._connected;
}

private set connected(connected: boolean) {
this._connected = connected;
this.emit(CallFeedEvent.ConnectedChanged, connected);
}

private get hasAudioTrack(): boolean {
Expand Down Expand Up @@ -145,6 +170,14 @@ export class CallFeed extends TypedEventEmitter<CallFeedEvent, EventHandlerMap>
this.emit(CallFeedEvent.NewStream, this.stream);
};

private onCallState = (state: CallState): void => {
if (state === CallState.Connected) {
this.connected = true;
} else if (state === CallState.Connecting) {
this.connected = false;
}
};

/**
* Returns callRoom member
* @returns member of the callRoom
Expand Down Expand Up @@ -297,6 +330,7 @@ export class CallFeed extends TypedEventEmitter<CallFeedEvent, EventHandlerMap>
public dispose(): void {
clearTimeout(this.volumeLooperTimeout);
this.stream?.removeEventListener("addtrack", this.onAddTrack);
this.call?.removeListener(CallEvent.State, this.onCallState);
if (this.audioContext) {
this.audioContext = undefined;
this.analyser = undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TypedEventEmitter } from "../models/typed-event-emitter";
import { CallFeed, SPEAKING_THRESHOLD } from "./callFeed";
import { CallFeed, CallFeedEvent, SPEAKING_THRESHOLD } from "./callFeed";
import { MatrixClient, IMyDevice } from "../client";
import {
CallErrorCode,
Expand Down Expand Up @@ -168,11 +168,11 @@ export class GroupCall extends TypedEventEmitter<
public localCallFeed?: CallFeed;
public localScreenshareFeed?: CallFeed;
public localDesktopCapturerSourceId?: string;
public readonly calls = new Map<RoomMember, Map<string, MatrixCall>>();
public readonly userMediaFeeds: CallFeed[] = [];
public readonly screenshareFeeds: CallFeed[] = [];
public groupCallId: string;

private readonly calls = new Map<RoomMember, Map<string, MatrixCall>>(); // RoomMember -> device ID -> MatrixCall
private callHandlers = new Map<string, Map<string, ICallHandlers>>(); // User ID -> device ID -> handlers
private activeSpeakerLoopInterval?: ReturnType<typeof setTimeout>;
private retryCallLoopInterval?: ReturnType<typeof setTimeout>;
Expand Down

0 comments on commit f812ea9

Please sign in to comment.