2022-02-17 21:28:44 +05:30
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {ObservableMap} from "../../../observable/map/ObservableMap";
|
2022-03-10 19:23:31 +05:30
|
|
|
import {Member} from "./Member";
|
2022-03-09 15:59:39 +05:30
|
|
|
import {LocalMedia} from "../LocalMedia";
|
2022-04-22 19:18:14 +05:30
|
|
|
import {MuteSettings} from "../common";
|
2022-03-10 19:23:31 +05:30
|
|
|
import {RoomMember} from "../../room/members/RoomMember";
|
2022-03-24 18:22:19 +05:30
|
|
|
import {EventEmitter} from "../../../utils/EventEmitter";
|
2022-04-07 14:02:23 +05:30
|
|
|
import {EventType, CallIntent} from "../callEventTypes";
|
2022-03-11 19:10:37 +05:30
|
|
|
|
2022-03-10 19:23:31 +05:30
|
|
|
import type {Options as MemberOptions} from "./Member";
|
|
|
|
import type {BaseObservableMap} from "../../../observable/map/BaseObservableMap";
|
2022-03-09 15:59:39 +05:30
|
|
|
import type {Track} from "../../../platform/types/MediaDevices";
|
2022-03-29 20:43:33 +05:30
|
|
|
import type {SignallingMessage, MGroupCallBase, CallMembership} from "../callEventTypes";
|
2022-03-09 23:23:51 +05:30
|
|
|
import type {Room} from "../../room/Room";
|
|
|
|
import type {StateEvent} from "../../storage/types";
|
|
|
|
import type {Platform} from "../../../platform/web/Platform";
|
2022-03-10 19:23:31 +05:30
|
|
|
import type {EncryptedMessage} from "../../e2ee/olm/Encryption";
|
|
|
|
import type {ILogItem} from "../../../logging/types";
|
2022-03-10 22:09:29 +05:30
|
|
|
import type {Storage} from "../../storage/idb/Storage";
|
|
|
|
|
|
|
|
export enum GroupCallState {
|
2022-03-11 19:10:37 +05:30
|
|
|
Fledgling = "fledgling",
|
|
|
|
Creating = "creating",
|
|
|
|
Created = "created",
|
|
|
|
Joining = "joining",
|
|
|
|
Joined = "joined",
|
2022-03-10 22:09:29 +05:30
|
|
|
}
|
2022-03-10 19:23:31 +05:30
|
|
|
|
2022-03-29 20:43:33 +05:30
|
|
|
function getMemberKey(userId: string, deviceId: string) {
|
|
|
|
return JSON.stringify(userId)+`,`+JSON.stringify(deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function memberKeyIsForUser(key: string, userId: string) {
|
|
|
|
return key.startsWith(JSON.stringify(userId)+`,`);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDeviceFromMemberKey(key: string): string {
|
|
|
|
return JSON.parse(`[${key}]`)[1];
|
|
|
|
}
|
|
|
|
|
2022-03-10 19:23:31 +05:30
|
|
|
export type Options = Omit<MemberOptions, "emitUpdate" | "confId" | "encryptDeviceMessage"> & {
|
|
|
|
emitUpdate: (call: GroupCall, params?: any) => void;
|
2022-03-23 16:53:10 +05:30
|
|
|
encryptDeviceMessage: (roomId: string, userId: string, message: SignallingMessage<MGroupCallBase>, log: ILogItem) => Promise<EncryptedMessage>,
|
2022-03-10 22:09:29 +05:30
|
|
|
storage: Storage,
|
2022-03-10 19:23:31 +05:30
|
|
|
};
|
2022-02-17 21:28:44 +05:30
|
|
|
|
2022-03-24 18:22:19 +05:30
|
|
|
export class GroupCall extends EventEmitter<{change: never}> {
|
2022-03-10 19:23:31 +05:30
|
|
|
private readonly _members: ObservableMap<string, Member> = new ObservableMap();
|
2022-03-23 00:59:31 +05:30
|
|
|
private _localMedia?: LocalMedia = undefined;
|
2022-03-10 19:23:31 +05:30
|
|
|
private _memberOptions: MemberOptions;
|
2022-03-11 19:10:37 +05:30
|
|
|
private _state: GroupCallState;
|
2022-04-22 19:18:14 +05:30
|
|
|
private localMuteSettings: MuteSettings = new MuteSettings(false, false);
|
2022-04-27 00:41:41 +05:30
|
|
|
private bufferedDeviceMessages = new Map<string, Set<SignallingMessage<MGroupCallBase>>>();
|
2022-03-11 19:10:37 +05:30
|
|
|
|
2022-03-09 23:23:51 +05:30
|
|
|
constructor(
|
2022-04-07 14:02:23 +05:30
|
|
|
public readonly id: string,
|
|
|
|
newCall: boolean,
|
|
|
|
private callContent: Record<string, any>,
|
2022-03-17 17:34:14 +05:30
|
|
|
public readonly roomId: string,
|
2022-03-25 19:13:02 +05:30
|
|
|
private readonly options: Options,
|
|
|
|
private readonly logItem: ILogItem,
|
2022-03-09 23:23:51 +05:30
|
|
|
) {
|
2022-03-24 18:22:19 +05:30
|
|
|
super();
|
2022-03-25 19:13:02 +05:30
|
|
|
logItem.set("id", this.id);
|
2022-04-07 14:02:23 +05:30
|
|
|
this._state = newCall ? GroupCallState.Fledgling : GroupCallState.Created;
|
2022-03-23 16:53:10 +05:30
|
|
|
this._memberOptions = Object.assign({}, options, {
|
2022-03-10 19:23:31 +05:30
|
|
|
confId: this.id,
|
2022-03-29 20:43:33 +05:30
|
|
|
emitUpdate: member => this._members.update(getMemberKey(member.userId, member.deviceId), member),
|
2022-03-23 16:53:10 +05:30
|
|
|
encryptDeviceMessage: (userId: string, message: SignallingMessage<MGroupCallBase>, log) => {
|
|
|
|
return this.options.encryptDeviceMessage(this.roomId, userId, message, log);
|
2022-03-10 19:23:31 +05:30
|
|
|
}
|
2022-03-23 16:53:10 +05:30
|
|
|
});
|
2022-02-17 21:28:44 +05:30
|
|
|
}
|
|
|
|
|
2022-03-11 19:10:37 +05:30
|
|
|
get localMedia(): LocalMedia | undefined { return this._localMedia; }
|
2022-03-10 19:23:31 +05:30
|
|
|
get members(): BaseObservableMap<string, Member> { return this._members; }
|
|
|
|
|
|
|
|
get isTerminated(): boolean {
|
2022-03-11 19:10:37 +05:30
|
|
|
return this.callContent?.["m.terminated"] === true;
|
2022-03-10 19:23:31 +05:30
|
|
|
}
|
|
|
|
|
2022-03-30 18:48:46 +05:30
|
|
|
get isRinging(): boolean {
|
|
|
|
return this._state === GroupCallState.Created && this.intent === "m.ring" && !this.isMember(this.options.ownUserId);
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:34:14 +05:30
|
|
|
get name(): string {
|
|
|
|
return this.callContent?.["m.name"];
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:02:23 +05:30
|
|
|
get intent(): CallIntent {
|
2022-03-30 18:48:46 +05:30
|
|
|
return this.callContent?.["m.intent"];
|
|
|
|
}
|
|
|
|
|
2022-03-25 19:13:02 +05:30
|
|
|
join(localMedia: LocalMedia): Promise<void> {
|
|
|
|
return this.logItem.wrap("join", async log => {
|
|
|
|
if (this._state !== GroupCallState.Created) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._state = GroupCallState.Joining;
|
|
|
|
this._localMedia = localMedia;
|
|
|
|
this.emitChange();
|
|
|
|
const memberContent = await this._createJoinPayload();
|
|
|
|
// send m.call.member state event
|
2022-04-07 14:02:23 +05:30
|
|
|
const request = this.options.hsApi.sendState(this.roomId, EventType.GroupCallMember, this.options.ownUserId, memberContent, {log});
|
2022-03-25 19:13:02 +05:30
|
|
|
await request.response();
|
|
|
|
this.emitChange();
|
|
|
|
// send invite to all members that are < my userId
|
|
|
|
for (const [,member] of this._members) {
|
2022-04-22 19:18:14 +05:30
|
|
|
member.connect(this._localMedia!.clone(), this.localMuteSettings);
|
2022-03-25 19:13:02 +05:30
|
|
|
}
|
|
|
|
});
|
2022-03-11 19:10:37 +05:30
|
|
|
}
|
|
|
|
|
2022-04-15 02:49:44 +05:30
|
|
|
async setMedia(localMedia: LocalMedia): Promise<void> {
|
2022-04-21 13:41:24 +05:30
|
|
|
if (this._state === GroupCallState.Joining || this._state === GroupCallState.Joined && this._localMedia) {
|
|
|
|
const oldMedia = this._localMedia!;
|
2022-04-15 02:49:44 +05:30
|
|
|
this._localMedia = localMedia;
|
|
|
|
await Promise.all(Array.from(this._members.values()).map(m => {
|
2022-04-21 13:41:24 +05:30
|
|
|
return m.setMedia(localMedia, oldMedia);
|
2022-04-15 02:49:44 +05:30
|
|
|
}));
|
2022-04-21 13:41:24 +05:30
|
|
|
oldMedia?.stopExcept(localMedia);
|
2022-04-15 02:49:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:18:14 +05:30
|
|
|
setMuted(muteSettings: MuteSettings) {
|
|
|
|
this.localMuteSettings = muteSettings;
|
|
|
|
if (this._state === GroupCallState.Joining || this._state === GroupCallState.Joined) {
|
|
|
|
for (const [,member] of this._members) {
|
|
|
|
member.setMuted(this.localMuteSettings);
|
|
|
|
}
|
|
|
|
}
|
2022-04-26 19:48:49 +05:30
|
|
|
this.emitChange();
|
2022-04-22 19:18:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get muteSettings(): MuteSettings {
|
|
|
|
return this.localMuteSettings;
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:53:10 +05:30
|
|
|
get hasJoined() {
|
|
|
|
return this._state === GroupCallState.Joining || this._state === GroupCallState.Joined;
|
|
|
|
}
|
|
|
|
|
2022-03-25 19:13:02 +05:30
|
|
|
leave(): Promise<void> {
|
|
|
|
return this.logItem.wrap("leave", async log => {
|
|
|
|
const memberContent = await this._leaveCallMemberContent();
|
|
|
|
// send m.call.member state event
|
|
|
|
if (memberContent) {
|
2022-04-07 14:02:23 +05:30
|
|
|
const request = this.options.hsApi.sendState(this.roomId, EventType.GroupCallMember, this.options.ownUserId, memberContent, {log});
|
2022-03-25 19:13:02 +05:30
|
|
|
await request.response();
|
|
|
|
// our own user isn't included in members, so not in the count
|
2022-04-07 14:02:23 +05:30
|
|
|
if (this.intent === CallIntent.Ring && this._members.size === 0) {
|
2022-03-25 19:13:02 +05:30
|
|
|
await this.terminate();
|
|
|
|
}
|
2022-03-30 18:48:46 +05:30
|
|
|
} else {
|
|
|
|
log.set("already_left", true);
|
2022-03-24 18:22:19 +05:30
|
|
|
}
|
2022-03-25 19:13:02 +05:30
|
|
|
});
|
2022-03-11 19:10:37 +05:30
|
|
|
}
|
|
|
|
|
2022-03-25 19:13:02 +05:30
|
|
|
terminate(): Promise<void> {
|
|
|
|
return this.logItem.wrap("terminate", async log => {
|
|
|
|
if (this._state === GroupCallState.Fledgling) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-07 14:02:23 +05:30
|
|
|
const request = this.options.hsApi.sendState(this.roomId, EventType.GroupCall, this.id, Object.assign({}, this.callContent, {
|
2022-03-25 19:13:02 +05:30
|
|
|
"m.terminated": true
|
|
|
|
}), {log});
|
|
|
|
await request.response();
|
|
|
|
});
|
2022-03-24 18:22:19 +05:30
|
|
|
}
|
|
|
|
|
2022-03-11 19:10:37 +05:30
|
|
|
/** @internal */
|
2022-04-21 21:09:11 +05:30
|
|
|
create(type: "m.video" | "m.voice"): Promise<void> {
|
2022-03-25 19:13:02 +05:30
|
|
|
return this.logItem.wrap("create", async log => {
|
|
|
|
if (this._state !== GroupCallState.Fledgling) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._state = GroupCallState.Creating;
|
|
|
|
this.emitChange();
|
2022-04-07 14:02:23 +05:30
|
|
|
this.callContent = Object.assign({
|
2022-04-21 21:09:11 +05:30
|
|
|
"m.type": type,
|
2022-04-07 14:02:23 +05:30
|
|
|
}, this.callContent);
|
|
|
|
const request = this.options.hsApi.sendState(this.roomId, EventType.GroupCall, this.id, this.callContent!, {log});
|
2022-03-25 19:13:02 +05:30
|
|
|
await request.response();
|
|
|
|
this._state = GroupCallState.Created;
|
|
|
|
this.emitChange();
|
|
|
|
});
|
2022-02-17 21:28:44 +05:30
|
|
|
}
|
|
|
|
|
2022-03-10 19:23:31 +05:30
|
|
|
/** @internal */
|
2022-03-25 19:13:02 +05:30
|
|
|
updateCallEvent(callContent: Record<string, any>, syncLog: ILogItem) {
|
|
|
|
this.logItem.wrap("updateCallEvent", log => {
|
|
|
|
syncLog.refDetached(log);
|
|
|
|
this.callContent = callContent;
|
|
|
|
if (this._state === GroupCallState.Creating) {
|
|
|
|
this._state = GroupCallState.Created;
|
|
|
|
}
|
|
|
|
log.set("status", this._state);
|
|
|
|
this.emitChange();
|
|
|
|
});
|
2022-02-17 21:28:44 +05:30
|
|
|
}
|
|
|
|
|
2022-03-10 19:23:31 +05:30
|
|
|
/** @internal */
|
2022-03-30 18:48:46 +05:30
|
|
|
updateMembership(userId: string, callMembership: CallMembership, syncLog: ILogItem) {
|
2022-03-29 20:43:33 +05:30
|
|
|
this.logItem.wrap({l: "updateMember", id: userId}, log => {
|
2022-03-25 19:13:02 +05:30
|
|
|
syncLog.refDetached(log);
|
2022-03-29 20:43:33 +05:30
|
|
|
const devices = callMembership["m.devices"];
|
|
|
|
const previousDeviceIds = this.getDeviceIdsForUserId(userId);
|
|
|
|
for (const device of devices) {
|
|
|
|
const deviceId = device.device_id;
|
|
|
|
const memberKey = getMemberKey(userId, deviceId);
|
2022-04-27 23:10:49 +05:30
|
|
|
log.wrap({l: "update device member", id: memberKey, sessionId: device.session_id}, log => {
|
2022-03-30 18:48:46 +05:30
|
|
|
if (userId === this.options.ownUserId && deviceId === this.options.ownDeviceId) {
|
|
|
|
if (this._state === GroupCallState.Joining) {
|
|
|
|
log.set("update_own", true);
|
|
|
|
this._state = GroupCallState.Joined;
|
|
|
|
this.emitChange();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let member = this._members.get(memberKey);
|
2022-04-28 21:22:42 +05:30
|
|
|
const sessionIdChanged = member && member.sessionId !== device.session_id;
|
|
|
|
if (member && !sessionIdChanged) {
|
2022-03-30 18:48:46 +05:30
|
|
|
log.set("update", true);
|
2022-04-28 21:22:42 +05:30
|
|
|
member.updateCallInfo(device, log);
|
2022-03-30 18:48:46 +05:30
|
|
|
} else {
|
2022-04-28 21:22:42 +05:30
|
|
|
if (member && sessionIdChanged) {
|
|
|
|
log.set("removedSessionId", member.sessionId);
|
|
|
|
member.disconnect(false);
|
|
|
|
this._members.remove(memberKey);
|
|
|
|
member = undefined;
|
|
|
|
}
|
2022-03-30 18:48:46 +05:30
|
|
|
const logItem = this.logItem.child({l: "member", id: memberKey});
|
|
|
|
log.set("add", true);
|
|
|
|
log.refDetached(logItem);
|
|
|
|
member = new Member(
|
|
|
|
RoomMember.fromUserId(this.roomId, userId, "join"),
|
|
|
|
device, this._memberOptions, logItem
|
|
|
|
);
|
|
|
|
this._members.add(memberKey, member);
|
|
|
|
if (this._state === GroupCallState.Joining || this._state === GroupCallState.Joined) {
|
2022-04-14 17:14:11 +05:30
|
|
|
// Safari can't send a MediaStream to multiple sources, so clone it
|
2022-04-22 19:18:14 +05:30
|
|
|
member.connect(this._localMedia!.clone(), this.localMuteSettings);
|
2022-03-30 18:48:46 +05:30
|
|
|
}
|
|
|
|
}
|
2022-04-27 00:41:41 +05:30
|
|
|
// flush pending messages, either after having created the member,
|
|
|
|
// or updated the session id with updateCallInfo
|
|
|
|
this.flushPendingDeviceMessages(member, log);
|
2022-03-29 20:43:33 +05:30
|
|
|
}
|
2022-03-30 18:48:46 +05:30
|
|
|
});
|
2022-03-11 19:10:37 +05:30
|
|
|
}
|
2022-03-29 20:43:33 +05:30
|
|
|
|
|
|
|
const newDeviceIds = new Set<string>(devices.map(call => call.device_id));
|
|
|
|
// remove user as member of any calls not present anymore
|
|
|
|
for (const previousDeviceId of previousDeviceIds) {
|
|
|
|
if (!newDeviceIds.has(previousDeviceId)) {
|
2022-03-30 18:48:46 +05:30
|
|
|
log.wrap({l: "remove device member", id: getMemberKey(userId, previousDeviceId)}, log => {
|
|
|
|
this.removeMemberDevice(userId, previousDeviceId, log);
|
|
|
|
});
|
2022-03-25 19:13:02 +05:30
|
|
|
}
|
2022-03-11 19:10:37 +05:30
|
|
|
}
|
2022-03-30 18:48:46 +05:30
|
|
|
if (userId === this.options.ownUserId && !newDeviceIds.has(this.options.ownDeviceId)) {
|
|
|
|
this.removeOwnDevice(log);
|
|
|
|
}
|
2022-03-25 19:13:02 +05:30
|
|
|
});
|
2022-02-17 21:28:44 +05:30
|
|
|
}
|
|
|
|
|
2022-03-10 19:23:31 +05:30
|
|
|
/** @internal */
|
2022-03-30 18:48:46 +05:30
|
|
|
removeMembership(userId: string, syncLog: ILogItem) {
|
2022-03-29 20:43:33 +05:30
|
|
|
const deviceIds = this.getDeviceIdsForUserId(userId);
|
2022-03-30 18:48:46 +05:30
|
|
|
this.logItem.wrap("removeMember", log => {
|
|
|
|
syncLog.refDetached(log);
|
|
|
|
for (const deviceId of deviceIds) {
|
|
|
|
this.removeMemberDevice(userId, deviceId, log);
|
|
|
|
}
|
|
|
|
if (userId === this.options.ownUserId) {
|
|
|
|
this.removeOwnDevice(log);
|
|
|
|
}
|
|
|
|
});
|
2022-03-29 20:43:33 +05:30
|
|
|
}
|
|
|
|
|
2022-04-27 00:41:41 +05:30
|
|
|
private flushPendingDeviceMessages(member: Member, log: ILogItem) {
|
|
|
|
const memberKey = getMemberKey(member.userId, member.deviceId);
|
|
|
|
const bufferedMessages = this.bufferedDeviceMessages.get(memberKey);
|
|
|
|
// check if we have any pending message for the member with (userid, deviceid, sessionid)
|
|
|
|
if (bufferedMessages) {
|
|
|
|
for (const message of bufferedMessages) {
|
|
|
|
if (message.content.sender_session_id === member.sessionId) {
|
|
|
|
member.handleDeviceMessage(message, log);
|
|
|
|
bufferedMessages.delete(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bufferedMessages.size === 0) {
|
|
|
|
this.bufferedDeviceMessages.delete(memberKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:43:33 +05:30
|
|
|
private getDeviceIdsForUserId(userId: string): string[] {
|
|
|
|
return Array.from(this._members.keys())
|
|
|
|
.filter(key => memberKeyIsForUser(key, userId))
|
|
|
|
.map(key => getDeviceFromMemberKey(key));
|
|
|
|
}
|
|
|
|
|
2022-03-30 18:48:46 +05:30
|
|
|
private isMember(userId: string): boolean {
|
|
|
|
return Array.from(this._members.keys()).some(key => memberKeyIsForUser(key, userId));
|
|
|
|
}
|
|
|
|
|
|
|
|
private removeOwnDevice(log: ILogItem) {
|
|
|
|
if (this._state === GroupCallState.Joined) {
|
|
|
|
log.set("leave_own", true);
|
|
|
|
for (const [,member] of this._members) {
|
2022-04-14 17:15:21 +05:30
|
|
|
member.disconnect(true);
|
2022-03-30 18:48:46 +05:30
|
|
|
}
|
2022-04-14 17:14:11 +05:30
|
|
|
this._localMedia?.dispose();
|
|
|
|
this._localMedia = undefined;
|
2022-03-30 18:48:46 +05:30
|
|
|
this._state = GroupCallState.Created;
|
|
|
|
this.emitChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:43:33 +05:30
|
|
|
/** @internal */
|
2022-03-30 18:48:46 +05:30
|
|
|
private removeMemberDevice(userId: string, deviceId: string, log: ILogItem) {
|
2022-03-29 20:43:33 +05:30
|
|
|
const memberKey = getMemberKey(userId, deviceId);
|
2022-04-28 21:22:00 +05:30
|
|
|
log.set("id", memberKey);
|
|
|
|
const member = this._members.get(memberKey);
|
|
|
|
if (member) {
|
|
|
|
log.set("leave", true);
|
|
|
|
this._members.remove(memberKey);
|
|
|
|
member.disconnect(false);
|
|
|
|
}
|
|
|
|
this.emitChange();
|
2022-03-09 23:23:51 +05:30
|
|
|
}
|
|
|
|
|
2022-03-10 19:23:31 +05:30
|
|
|
/** @internal */
|
2022-03-25 19:13:02 +05:30
|
|
|
handleDeviceMessage(message: SignallingMessage<MGroupCallBase>, userId: string, deviceId: string, syncLog: ILogItem) {
|
2022-03-10 19:23:31 +05:30
|
|
|
// TODO: return if we are not membering to the call
|
2022-04-27 00:41:41 +05:30
|
|
|
const key = getMemberKey(userId, deviceId);
|
|
|
|
let member = this._members.get(key);
|
|
|
|
if (member && message.content.sender_session_id === member.sessionId) {
|
|
|
|
member.handleDeviceMessage(message, syncLog);
|
2022-03-10 19:23:31 +05:30
|
|
|
} else {
|
2022-04-27 23:10:49 +05:30
|
|
|
const item = this.logItem.log({
|
|
|
|
l: "buffering to_device message, member not found",
|
|
|
|
userId,
|
|
|
|
deviceId,
|
|
|
|
sessionId: message.content.sender_session_id,
|
|
|
|
type: message.type
|
|
|
|
});
|
2022-03-25 19:13:02 +05:30
|
|
|
syncLog.refDetached(item);
|
2022-04-27 00:41:41 +05:30
|
|
|
// we haven't received the m.call.member yet for this caller (or with this session id).
|
|
|
|
// buffer the device messages or create the member/call as it should arrive in a moment
|
|
|
|
let messages = this.bufferedDeviceMessages.get(key);
|
|
|
|
if (!messages) {
|
|
|
|
messages = new Set();
|
|
|
|
this.bufferedDeviceMessages.set(key, messages);
|
|
|
|
}
|
|
|
|
messages.add(message);
|
2022-02-17 21:28:44 +05:30
|
|
|
}
|
|
|
|
}
|
2022-03-10 22:09:29 +05:30
|
|
|
|
2022-03-25 19:13:02 +05:30
|
|
|
/** @internal */
|
|
|
|
dispose() {
|
|
|
|
this.logItem.finish();
|
|
|
|
}
|
|
|
|
|
2022-03-24 18:22:19 +05:30
|
|
|
private async _createJoinPayload() {
|
2022-03-10 22:09:29 +05:30
|
|
|
const {storage} = this.options;
|
|
|
|
const txn = await storage.readTxn([storage.storeNames.roomState]);
|
2022-04-07 14:02:23 +05:30
|
|
|
const stateEvent = await txn.roomState.get(this.roomId, EventType.GroupCallMember, this.options.ownUserId);
|
2022-03-10 22:09:29 +05:30
|
|
|
const stateContent = stateEvent?.event?.content ?? {
|
|
|
|
["m.calls"]: []
|
|
|
|
};
|
|
|
|
const callsInfo = stateContent["m.calls"];
|
|
|
|
let callInfo = callsInfo.find(c => c["m.call_id"] === this.id);
|
|
|
|
if (!callInfo) {
|
|
|
|
callInfo = {
|
|
|
|
["m.call_id"]: this.id,
|
|
|
|
["m.devices"]: []
|
|
|
|
};
|
|
|
|
callsInfo.push(callInfo);
|
|
|
|
}
|
2022-04-20 20:12:20 +05:30
|
|
|
callInfo["m.devices"] = callInfo["m.devices"].filter(d => d["device_id"] !== this.options.ownDeviceId);
|
|
|
|
callInfo["m.devices"].push({
|
|
|
|
["device_id"]: this.options.ownDeviceId,
|
|
|
|
["session_id"]: this.options.sessionId,
|
|
|
|
feeds: [{purpose: "m.usermedia"}]
|
|
|
|
});
|
2022-03-10 22:09:29 +05:30
|
|
|
return stateContent;
|
|
|
|
}
|
2022-03-11 19:10:37 +05:30
|
|
|
|
|
|
|
private async _leaveCallMemberContent(): Promise<Record<string, any> | undefined> {
|
|
|
|
const {storage} = this.options;
|
|
|
|
const txn = await storage.readTxn([storage.storeNames.roomState]);
|
2022-04-07 14:02:23 +05:30
|
|
|
const stateEvent = await txn.roomState.get(this.roomId, EventType.GroupCallMember, this.options.ownUserId);
|
2022-03-24 18:22:19 +05:30
|
|
|
if (stateEvent) {
|
|
|
|
const content = stateEvent.event.content;
|
2022-03-30 18:48:46 +05:30
|
|
|
const callInfo = content["m.calls"]?.find(c => c["m.call_id"] === this.id);
|
|
|
|
if (callInfo) {
|
|
|
|
const devicesInfo = callInfo["m.devices"];
|
|
|
|
const deviceIndex = devicesInfo.findIndex(d => d["device_id"] === this.options.ownDeviceId);
|
|
|
|
if (deviceIndex !== -1) {
|
|
|
|
devicesInfo.splice(deviceIndex, 1);
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|
2022-03-24 18:22:19 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected emitChange() {
|
|
|
|
this.emit("change");
|
|
|
|
this.options.emitUpdate(this);
|
2022-03-11 19:10:37 +05:30
|
|
|
}
|
2022-02-17 21:28:44 +05:30
|
|
|
}
|