Skip to content

Commit

Permalink
Process toDevice events in order
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlong committed Feb 17, 2022
1 parent fa5eae7 commit 7f21f56
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export class MatrixCall extends EventEmitter {
public direction: CallDirection;
public ourPartyId: string;
public peerConn?: RTCPeerConnection;
public toDeviceSeq = 0;

private client: MatrixClient;
private forceTURN: boolean;
Expand Down Expand Up @@ -1984,6 +1985,8 @@ export class MatrixCall extends EventEmitter {
});

if (this.opponentDeviceId) {
const toDeviceSeq = this.toDeviceSeq++;

this.emit(CallEvent.SendVoipEvent, {
type: "toDevice",
eventType,
Expand All @@ -1994,6 +1997,7 @@ export class MatrixCall extends EventEmitter {
device_id: this.client.deviceId,
sender_session_id: this.client.getSessionId(),
dest_session_id: this.opponentSessionId,
seq: toDeviceSeq,
},
});

Expand All @@ -2004,6 +2008,7 @@ export class MatrixCall extends EventEmitter {
device_id: this.client.deviceId,
sender_session_id: this.client.getSessionId(),
dest_session_id: this.opponentSessionId,
seq: toDeviceSeq,
},
},
});
Expand Down
44 changes: 43 additions & 1 deletion src/webrtc/callEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class CallEventHandler {
calls: Map<string, MatrixCall>;
callEventBuffer: MatrixEvent[];
candidateEventsByCall: Map<string, Array<MatrixEvent>>;
nextSeqByCall: Map<string, number> = new Map();
toDeviceEventBuffers: Map<string, Array<MatrixEvent>> = new Map();

private eventBufferPromiseChain?: Promise<void>;

Expand Down Expand Up @@ -80,7 +82,45 @@ export class CallEventHandler {
};

private onToDeviceEvent = (event: MatrixEvent): void => {
this.callEventBuffer.push(event);
const content = event.getContent();

if (!content.call_id) {
this.callEventBuffer.push(event);
return;
}

if (!this.nextSeqByCall.has(content.call_id)) {
this.nextSeqByCall.set(content.call_id, 0);
}

if (content.seq === undefined) {
this.callEventBuffer.push(event);
return;
}

const nextSeq = this.nextSeqByCall.get(content.call_id) || 0;

if (content.seq !== nextSeq) {
if (!this.toDeviceEventBuffers.has(content.call_id)) {
this.toDeviceEventBuffers.set(content.call_id, []);
}

const buffer = this.toDeviceEventBuffers.get(content.call_id);
const index = buffer.findIndex((e) => e.getContent().seq > content.seq);
buffer.splice(index, 0, event);
} else {
const callId = content.call_id;
this.callEventBuffer.push(event);
this.nextSeqByCall.set(callId, content.seq + 1);

const buffer = this.toDeviceEventBuffers.get(callId);

while (buffer.length > 0 && buffer[0].getContent().seq === content.seq + 1) {
const nextEvent = buffer.pop();
this.callEventBuffer.push(nextEvent);
this.nextSeqByCall.set(callId, nextEvent.getContent().seq + 1);
}
}
};

private async evaluateEventBuffer(eventBuffer: MatrixEvent[]) {
Expand Down Expand Up @@ -122,6 +162,8 @@ export class CallEventHandler {
}

private async handleCallEvent(event: MatrixEvent) {
this.client.emit("received_voip_event", event);

const content = event.getContent();
const callRoomId = (
event.getRoomId() ||
Expand Down

1 comment on commit 7f21f56

@daniel-abramov
Copy link
Contributor

@daniel-abramov daniel-abramov commented on 7f21f56 Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we get a To-Device event with a seq of 1, then 2, then 1000000, then 999999, 999998, ... and so on? - This would eventually deplete all available memory. But I believe there is some protection somewhere to not allow such things to happen, right? (maybe it's in a different file/commit that I just did not notice yet)

Please sign in to comment.