add error boundary to GroupCall

This commit is contained in:
Bruno Windels 2023-01-09 14:03:52 +01:00
parent 7f9edbb742
commit 1e4180a71f

View File

@ -21,6 +21,7 @@ import {MuteSettings, CALL_LOG_TYPE, CALL_MEMBER_VALIDITY_PERIOD_MS, mute} from
import {MemberChange, RoomMember} from "../../room/members/RoomMember"; import {MemberChange, RoomMember} from "../../room/members/RoomMember";
import {EventEmitter} from "../../../utils/EventEmitter"; import {EventEmitter} from "../../../utils/EventEmitter";
import {EventType, CallIntent} from "../callEventTypes"; import {EventType, CallIntent} from "../callEventTypes";
import { ErrorBoundary } from "../../../utils/ErrorBoundary";
import type {Options as MemberOptions} from "./Member"; import type {Options as MemberOptions} from "./Member";
import type {TurnServerSource} from "../TurnServerSource"; import type {TurnServerSource} from "../TurnServerSource";
@ -92,6 +93,9 @@ export class GroupCall extends EventEmitter<{change: never}> {
private bufferedDeviceMessages = new Map<string, Set<SignallingMessage<MGroupCallBase>>>(); private bufferedDeviceMessages = new Map<string, Set<SignallingMessage<MGroupCallBase>>>();
/** Set between calling join and leave. */ /** Set between calling join and leave. */
private joinedData?: JoinedData; private joinedData?: JoinedData;
private errorBoundary = new ErrorBoundary(err => {
this.emitChange();
});
constructor( constructor(
public readonly id: string, public readonly id: string,
@ -154,6 +158,10 @@ export class GroupCall extends EventEmitter<{change: never}> {
return this.joinedData?.logItem; return this.joinedData?.logItem;
} }
get error(): Error | undefined {
return this.errorBoundary.error;
}
async join(localMedia: LocalMedia): Promise<void> { async join(localMedia: LocalMedia): Promise<void> {
if (this._state !== GroupCallState.Created || this.joinedData) { if (this._state !== GroupCallState.Created || this.joinedData) {
return; return;
@ -206,6 +214,9 @@ export class GroupCall extends EventEmitter<{change: never}> {
// and update the track info so PeerCall can use it to send up to date metadata, // and update the track info so PeerCall can use it to send up to date metadata,
this.joinedData.localMuteSettings.updateTrackInfo(localMedia.userMedia); this.joinedData.localMuteSettings.updateTrackInfo(localMedia.userMedia);
this.emitChange(); //allow listeners to see new media/mute settings this.emitChange(); //allow listeners to see new media/mute settings
// TODO: if setMedia fails on one of the members, we should revert to the old media
// on the members processed so far, and show an error that we could not set the new media
// for this, we will need to remove the usage of the errorBoundary in member.setMedia.
await Promise.all(Array.from(this._members.values()).map(m => { await Promise.all(Array.from(this._members.values()).map(m => {
return m.setMedia(localMedia, oldMedia); return m.setMedia(localMedia, oldMedia);
})); }));
@ -234,6 +245,9 @@ export class GroupCall extends EventEmitter<{change: never}> {
if (this.localMedia) { if (this.localMedia) {
mute(this.localMedia, muteSettings, this.joinedData!.logItem); mute(this.localMedia, muteSettings, this.joinedData!.logItem);
} }
// TODO: if setMuted fails on one of the members, we should revert to the old media
// on the members processed so far, and show an error that we could not set the new media
// for this, we will need to remove the usage of the errorBoundary in member.setMuted.
await Promise.all(Array.from(this._members.values()).map(m => { await Promise.all(Array.from(this._members.values()).map(m => {
return m.setMuted(joinedData.localMuteSettings); return m.setMuted(joinedData.localMuteSettings);
})); }));
@ -271,7 +285,14 @@ export class GroupCall extends EventEmitter<{change: never}> {
log.set("already_left", true); log.set("already_left", true);
} }
} finally { } finally {
this.disconnect(log); // disconnect is called both from the sync loop and from methods like this one that
// are called from the view model. We want errors during the sync loop being caught
// by the errorboundary, but since leave is called from the view model, we want
// the error to be thrown. So here we check if disconnect succeeded, and if not
// we rethrow the error put into the errorBoundary.
if(!this.disconnect(log)) {
throw this.errorBoundary.error;
}
} }
}); });
} }
@ -308,126 +329,134 @@ export class GroupCall extends EventEmitter<{change: never}> {
/** @internal */ /** @internal */
updateCallEvent(callContent: Record<string, any>, syncLog: ILogItem) { updateCallEvent(callContent: Record<string, any>, syncLog: ILogItem) {
syncLog.wrap({l: "update call", t: CALL_LOG_TYPE, id: this.id}, log => { this.errorBoundary.try(() => {
this.callContent = callContent; syncLog.wrap({l: "update call", t: CALL_LOG_TYPE, id: this.id}, log => {
if (this._state === GroupCallState.Creating) { this.callContent = callContent;
this._state = GroupCallState.Created; if (this._state === GroupCallState.Creating) {
} this._state = GroupCallState.Created;
log.set("status", this._state); }
this.emitChange(); log.set("status", this._state);
this.emitChange();
});
}); });
} }
/** @internal */ /** @internal */
updateRoomMembers(memberChanges: Map<string, MemberChange>) { updateRoomMembers(memberChanges: Map<string, MemberChange>) {
for (const change of memberChanges.values()) { this.errorBoundary.try(() => {
const {member} = change; for (const change of memberChanges.values()) {
for (const callMember of this._members.values()) { const {member} = change;
// find all call members for a room member (can be multiple, for every device) for (const callMember of this._members.values()) {
if (callMember.userId === member.userId) { // find all call members for a room member (can be multiple, for every device)
callMember.updateRoomMember(member); if (callMember.userId === member.userId) {
callMember.updateRoomMember(member);
}
} }
} }
} });
} }
/** @internal */ /** @internal */
updateMembership(userId: string, roomMember: RoomMember, callMembership: CallMembership, syncLog: ILogItem) { updateMembership(userId: string, roomMember: RoomMember, callMembership: CallMembership, syncLog: ILogItem) {
syncLog.wrap({l: "update call membership", t: CALL_LOG_TYPE, id: this.id, userId}, log => { this.errorBoundary.try(() => {
const now = this.options.clock.now(); syncLog.wrap({l: "update call membership", t: CALL_LOG_TYPE, id: this.id, userId}, log => {
const devices = callMembership["m.devices"]; const now = this.options.clock.now();
const previousDeviceIds = this.getDeviceIdsForUserId(userId); const devices = callMembership["m.devices"];
for (const device of devices) { const previousDeviceIds = this.getDeviceIdsForUserId(userId);
const deviceId = device.device_id; for (const device of devices) {
const memberKey = getMemberKey(userId, deviceId); const deviceId = device.device_id;
if (userId === this.options.ownUserId && deviceId === this.options.ownDeviceId) { const memberKey = getMemberKey(userId, deviceId);
log.wrap("update own membership", log => { if (userId === this.options.ownUserId && deviceId === this.options.ownDeviceId) {
if (this.hasJoined) { log.wrap("update own membership", log => {
if (this.joinedData) { if (this.hasJoined) {
this.joinedData.logItem.refDetached(log); if (this.joinedData) {
} this.joinedData.logItem.refDetached(log);
this._setupRenewMembershipTimeout(device, log); }
} this._setupRenewMembershipTimeout(device, log);
if (this._state === GroupCallState.Joining) { }
log.set("joined", true); if (this._state === GroupCallState.Joining) {
this._state = GroupCallState.Joined; log.set("joined", true);
this.emitChange(); this._state = GroupCallState.Joined;
} this.emitChange();
}); }
} else { });
log.wrap({l: "update device membership", id: memberKey, sessionId: device.session_id}, log => { } else {
if (isMemberExpired(device, now)) { log.wrap({l: "update device membership", id: memberKey, sessionId: device.session_id}, log => {
log.set("expired", true); if (isMemberExpired(device, now)) {
const member = this._members.get(memberKey); log.set("expired", true);
if (member) { const member = this._members.get(memberKey);
member.dispose(); if (member) {
this._members.remove(memberKey); member.dispose();
log.set("removed", true); this._members.remove(memberKey);
} log.set("removed", true);
return; }
} return;
let member = this._members.get(memberKey); }
const sessionIdChanged = member && member.sessionId !== device.session_id; let member = this._members.get(memberKey);
if (member && !sessionIdChanged) { const sessionIdChanged = member && member.sessionId !== device.session_id;
log.set("update", true); if (member && !sessionIdChanged) {
member.updateCallInfo(device, log); log.set("update", true);
} else { member.updateCallInfo(device, log);
if (member && sessionIdChanged) { } else {
log.set("removedSessionId", member.sessionId); if (member && sessionIdChanged) {
const disconnectLogItem = member.disconnect(false); log.set("removedSessionId", member.sessionId);
if (disconnectLogItem) { const disconnectLogItem = member.disconnect(false);
log.refDetached(disconnectLogItem); if (disconnectLogItem) {
} log.refDetached(disconnectLogItem);
member.dispose(); }
this._members.remove(memberKey); member.dispose();
member = undefined; this._members.remove(memberKey);
} member = undefined;
log.set("add", true); }
member = new Member( log.set("add", true);
roomMember, member = new Member(
device, this._memberOptions, roomMember,
log device, this._memberOptions,
); log
this._members.add(memberKey, member); );
if (this.joinedData) { this._members.add(memberKey, member);
this.connectToMember(member, this.joinedData, log); if (this.joinedData) {
} this.connectToMember(member, this.joinedData, log);
} }
// flush pending messages, either after having created the member, }
// or updated the session id with updateCallInfo // flush pending messages, either after having created the member,
this.flushPendingIncomingDeviceMessages(member, log); // or updated the session id with updateCallInfo
}); this.flushPendingIncomingDeviceMessages(member, log);
} });
} }
}
const newDeviceIds = new Set<string>(devices.map(call => call.device_id));
// remove user as member of any calls not present anymore const newDeviceIds = new Set<string>(devices.map(call => call.device_id));
for (const previousDeviceId of previousDeviceIds) { // remove user as member of any calls not present anymore
if (!newDeviceIds.has(previousDeviceId)) { for (const previousDeviceId of previousDeviceIds) {
this.removeMemberDevice(userId, previousDeviceId, log); if (!newDeviceIds.has(previousDeviceId)) {
} this.removeMemberDevice(userId, previousDeviceId, log);
} }
if (userId === this.options.ownUserId && !newDeviceIds.has(this.options.ownDeviceId)) { }
this.removeOwnDevice(log); if (userId === this.options.ownUserId && !newDeviceIds.has(this.options.ownDeviceId)) {
} this.removeOwnDevice(log);
}
});
}); });
} }
/** @internal */ /** @internal */
removeMembership(userId: string, syncLog: ILogItem) { removeMembership(userId: string, syncLog: ILogItem) {
const deviceIds = this.getDeviceIdsForUserId(userId); this.errorBoundary.try(() => {
syncLog.wrap({ const deviceIds = this.getDeviceIdsForUserId(userId);
l: "remove call member", syncLog.wrap({
t: CALL_LOG_TYPE, l: "remove call member",
id: this.id, t: CALL_LOG_TYPE,
userId id: this.id,
}, log => { userId
for (const deviceId of deviceIds) { }, log => {
this.removeMemberDevice(userId, deviceId, log); for (const deviceId of deviceIds) {
} this.removeMemberDevice(userId, deviceId, log);
if (userId === this.options.ownUserId) { }
this.removeOwnDevice(log); if (userId === this.options.ownUserId) {
} this.removeOwnDevice(log);
}
});
}); });
} }
@ -465,19 +494,21 @@ export class GroupCall extends EventEmitter<{change: never}> {
} }
/** @internal */ /** @internal */
disconnect(log: ILogItem) { disconnect(log: ILogItem): boolean {
if (this.hasJoined) { return this.errorBoundary.try(() => {
for (const [,member] of this._members) { if (this.hasJoined) {
const disconnectLogItem = member.disconnect(true); for (const [,member] of this._members) {
if (disconnectLogItem) { const disconnectLogItem = member.disconnect(true);
log.refDetached(disconnectLogItem); if (disconnectLogItem) {
log.refDetached(disconnectLogItem);
}
} }
this._state = GroupCallState.Created;
} }
this._state = GroupCallState.Created; this.joinedData?.dispose();
} this.joinedData = undefined;
this.joinedData?.dispose(); this.emitChange();
this.joinedData = undefined; }, false) || true;
this.emitChange();
} }
/** @internal */ /** @internal */
@ -500,31 +531,33 @@ export class GroupCall extends EventEmitter<{change: never}> {
/** @internal */ /** @internal */
handleDeviceMessage(message: SignallingMessage<MGroupCallBase>, userId: string, deviceId: string, syncLog: ILogItem) { handleDeviceMessage(message: SignallingMessage<MGroupCallBase>, userId: string, deviceId: string, syncLog: ILogItem) {
// TODO: return if we are not membering to the call this.errorBoundary.try(() => {
const key = getMemberKey(userId, deviceId); // TODO: return if we are not membering to the call
let member = this._members.get(key); const key = getMemberKey(userId, deviceId);
if (member && message.content.sender_session_id === member.sessionId) { let member = this._members.get(key);
member.handleDeviceMessage(message, syncLog); if (member && message.content.sender_session_id === member.sessionId) {
} else { member.handleDeviceMessage(message, syncLog);
const item = syncLog.log({ } else {
l: "call: buffering to_device message, member not found", const item = syncLog.log({
t: CALL_LOG_TYPE, l: "call: buffering to_device message, member not found",
id: this.id, t: CALL_LOG_TYPE,
userId, id: this.id,
deviceId, userId,
sessionId: message.content.sender_session_id, deviceId,
type: message.type sessionId: message.content.sender_session_id,
}); type: message.type
syncLog.refDetached(item); });
// we haven't received the m.call.member yet for this caller (or with this session id). syncLog.refDetached(item);
// buffer the device messages or create the member/call as it should arrive in a moment // we haven't received the m.call.member yet for this caller (or with this session id).
let messages = this.bufferedDeviceMessages.get(key); // buffer the device messages or create the member/call as it should arrive in a moment
if (!messages) { let messages = this.bufferedDeviceMessages.get(key);
messages = new Set(); if (!messages) {
this.bufferedDeviceMessages.set(key, messages); messages = new Set();
this.bufferedDeviceMessages.set(key, messages);
}
messages.add(message);
} }
messages.add(message); });
}
} }
private async _createMemberPayload(includeOwn: boolean): Promise<CallMemberContent> { private async _createMemberPayload(includeOwn: boolean): Promise<CallMemberContent> {