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/CallHandler.ts

123 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-02-14 21:44:21 +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";
import type {Room} from "../room/Room";
2022-03-09 23:23:51 +05:30
import type {MemberChange} from "../room/members/RoomMember";
2022-02-14 21:44:21 +05:30
import type {StateEvent} from "../storage/types";
import type {ILogItem} from "../../logging/types";
2022-03-09 23:23:51 +05:30
import type {Platform} from "../../platform/web/Platform";
2022-02-14 21:44:21 +05:30
2022-02-16 21:31:02 +05:30
import {WebRTC, PeerConnection, PeerConnectionHandler, StreamPurpose} from "../../platform/types/WebRTC";
import {MediaDevices, Track, AudioTrack, TrackType} from "../../platform/types/MediaDevices";
2022-03-09 23:23:51 +05:30
import {handlesEventType, PeerCall, PeerCallHandler} from "./PeerCall";
import {EventType} from "./callEventTypes";
import type {SignallingMessage, MGroupCallBase} from "./callEventTypes";
import type {GroupCall} from "./group/GroupCall";
2022-02-16 21:31:02 +05:30
2022-02-14 21:44:21 +05:30
const GROUP_CALL_TYPE = "m.call";
const GROUP_CALL_MEMBER_TYPE = "m.call.member";
const CALL_TERMINATED = "m.terminated";
2022-02-17 21:28:44 +05:30
export class GroupCallHandler {
2022-03-09 23:23:51 +05:30
private createPeerCall: (callId: string, handler: PeerCallHandler) => PeerCall;
2022-02-15 21:35:20 +05:30
// group calls by call id
2022-02-17 21:28:44 +05:30
public readonly calls: ObservableMap<string, GroupCall> = new ObservableMap<string, GroupCall>();
2022-03-09 23:23:51 +05:30
// map of userId to set of conf_id's they are in
private memberToCallIds: Map<string, Set<string>> = new Map();
2022-02-14 21:44:21 +05:30
2022-03-09 23:23:51 +05:30
constructor(hsApi: HomeServerApi, platform: Platform, ownUserId: string, ownDeviceId: string) {
this.createPeerCall = (callId: string, handler: PeerCallHandler) => {
return new PeerCall(callId, handler, platform.createTimeout, platform.webRTC);
}
2022-02-14 21:44:21 +05:30
}
2022-02-25 21:24:00 +05:30
// TODO: check and poll turn server credentials here
2022-02-14 21:44:21 +05:30
handleRoomState(room: Room, events: StateEvent[], log: ILogItem) {
// first update call events
for (const event of events) {
2022-03-09 23:23:51 +05:30
if (event.type === EventType.GroupCall) {
this.handleCallEvent(event);
2022-02-14 21:44:21 +05:30
}
}
// then update participants
for (const event of events) {
2022-03-09 23:23:51 +05:30
if (event.type === EventType.GroupCallMember) {
this.handleCallMemberEvent(event);
}
}
}
updateRoomMembers(room: Room, memberChanges: Map<string, MemberChange>) {
}
private handleCallEvent(event: StateEvent) {
const callId = event.state_key;
let call = this.calls.get(callId);
if (call) {
call.updateCallEvent(event);
if (call.isTerminated) {
this.calls.remove(call.id);
}
} else {
call = new GroupCall(event, room, this.createPeerCall);
this.calls.set(call.id, call);
}
}
private handleCallMemberEvent(event: StateEvent) {
const participant = event.state_key;
const calls = event.content["m.calls"] ?? [];
const newCallIdsMemberOf = new Set<string>(calls.map(call => {
const callId = call["m.call_id"];
const groupCall = this.calls.get(callId);
// TODO: also check the participant when receiving the m.call event
groupCall?.addParticipant(participant, call);
return callId;
}));
let previousCallIdsMemberOf = this.memberToCallIds.get(participant);
// remove user as participant of any calls not present anymore
if (previousCallIdsMemberOf) {
for (const previousCallId of previousCallIdsMemberOf) {
if (!newCallIdsMemberOf.has(previousCallId)) {
const groupCall = this.calls.get(previousCallId);
groupCall?.removeParticipant(participant);
2022-02-14 21:44:21 +05:30
}
}
}
2022-03-09 23:23:51 +05:30
if (newCallIdsMemberOf.size === 0) {
this.memberToCallIds.delete(participant);
} else {
this.memberToCallIds.set(participant, newCallIdsMemberOf);
}
2022-02-14 21:44:21 +05:30
}
2022-03-09 23:23:51 +05:30
handlesDeviceMessageEventType(eventType: string): boolean {
return handlesEventType(eventType);
2022-02-14 21:44:21 +05:30
}
2022-03-09 15:59:39 +05:30
handleDeviceMessage(senderUserId: string, senderDeviceId: string, event: SignallingMessage<MGroupCallBase>, log: ILogItem) {
2022-03-09 23:23:51 +05:30
// TODO: buffer messages for calls we haven't received the state event for yet?
2022-03-09 15:59:39 +05:30
const call = this.calls.get(event.content.conf_id);
call?.handleDeviceMessage(senderUserId, senderDeviceId, event, log);
2022-02-14 21:44:21 +05:30
}
}