This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/matrix/calls/group/GroupCall.ts

110 lines
4.1 KiB
TypeScript
Raw Normal View History

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-03-10 19:23:31 +05:30
import {RoomMember} from "../../room/members/RoomMember";
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-09 23:23:51 +05:30
import type {SignallingMessage, MGroupCallBase} from "../callEventTypes";
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";
export type Options = Omit<MemberOptions, "emitUpdate" | "confId" | "encryptDeviceMessage"> & {
emitUpdate: (call: GroupCall, params?: any) => void;
encryptDeviceMessage: (roomId: string, message: SignallingMessage<MGroupCallBase>, log: ILogItem) => Promise<EncryptedMessage>,
};
2022-02-17 21:28:44 +05:30
2022-03-09 15:59:39 +05:30
export class GroupCall {
2022-03-10 19:23:31 +05:30
private readonly _members: ObservableMap<string, Member> = new ObservableMap();
2022-03-09 15:59:39 +05:30
private localMedia?: Promise<LocalMedia>;
2022-03-10 19:23:31 +05:30
private _memberOptions: MemberOptions;
2022-02-17 21:28:44 +05:30
2022-03-09 23:23:51 +05:30
constructor(
private callEvent: StateEvent,
private readonly room: Room,
2022-03-10 19:23:31 +05:30
private readonly options: Options
2022-03-09 23:23:51 +05:30
) {
2022-03-10 19:23:31 +05:30
this._memberOptions = Object.assign({
confId: this.id,
emitUpdate: member => this._members.update(member.member.userId, member),
encryptDeviceMessage: (message: SignallingMessage<MGroupCallBase>, log) => {
return this.options.encryptDeviceMessage(this.room.id, message, log);
}
}, options);
2022-02-17 21:28:44 +05:30
}
2022-03-10 19:23:31 +05:30
get members(): BaseObservableMap<string, Member> { return this._members; }
2022-02-17 21:28:44 +05:30
get id(): string { return this.callEvent.state_key; }
2022-03-10 19:23:31 +05:30
get isTerminated(): boolean {
return this.callEvent.content["m.terminated"] === true;
}
async join(tracks: Promise<Track[]>) {
2022-03-09 15:59:39 +05:30
this.localMedia = tracks.then(tracks => LocalMedia.fromTracks(tracks));
2022-02-17 21:28:44 +05:30
// send m.call.member state event
2022-03-10 19:23:31 +05:30
const request = this.options.hsApi.sendState(this.room.id, "m.call.member", this.options.ownUserId, {
2022-02-17 21:28:44 +05:30
2022-03-10 19:23:31 +05:30
});
await request.response();
// send invite to all members that are < my userId
for (const [,member] of this._members) {
member.connect(this.localMedia);
2022-02-17 21:28:44 +05:30
}
}
2022-03-10 19:23:31 +05:30
/** @internal */
2022-02-17 21:28:44 +05:30
updateCallEvent(callEvent: StateEvent) {
this.callEvent = callEvent;
2022-03-10 19:23:31 +05:30
// TODO: emit update
2022-02-17 21:28:44 +05:30
}
2022-03-10 19:23:31 +05:30
/** @internal */
addMember(userId, memberCallInfo) {
let member = this._members.get(userId);
if (member) {
member.updateCallInfo(memberCallInfo);
2022-02-17 21:28:44 +05:30
} else {
2022-03-10 19:23:31 +05:30
member = new Member(RoomMember.fromUserId(this.room.id, userId, "join"), this._memberOptions);
member.updateCallInfo(memberCallInfo);
this._members.add(userId, member);
2022-02-17 21:28:44 +05:30
}
}
2022-03-10 19:23:31 +05:30
/** @internal */
removeMember(userId) {
this._members.remove(userId);
2022-03-09 23:23:51 +05:30
}
2022-03-10 19:23:31 +05:30
/** @internal */
handleDeviceMessage(message: SignallingMessage<MGroupCallBase>, userId: string, deviceId: string, log: ILogItem) {
// TODO: return if we are not membering to the call
let member = this._members.get(userId);
if (member) {
member.handleDeviceMessage(message, deviceId, log);
} else {
// we haven't received the m.call.member yet for this caller. buffer the device messages or create the member/call anyway?
2022-02-17 21:28:44 +05:30
}
}
}