2022-02-14 17:14:21 +01:00
|
|
|
/*
|
|
|
|
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";
|
|
|
|
import type {StateEvent} from "../storage/types";
|
|
|
|
import type {ILogItem} from "../../logging/types";
|
|
|
|
|
2022-02-16 17:01:02 +01:00
|
|
|
import {WebRTC, PeerConnection, PeerConnectionHandler, StreamPurpose} from "../../platform/types/WebRTC";
|
|
|
|
import {MediaDevices, Track, AudioTrack, TrackType} from "../../platform/types/MediaDevices";
|
2022-03-09 11:29:39 +01:00
|
|
|
import type {SignallingMessage} from "./PeerCall";
|
|
|
|
import type {MGroupCallBase} from "./callEventTypes";
|
2022-02-16 17:01:02 +01:00
|
|
|
|
2022-02-14 17:14:21 +01:00
|
|
|
const GROUP_CALL_TYPE = "m.call";
|
|
|
|
const GROUP_CALL_MEMBER_TYPE = "m.call.member";
|
|
|
|
|
|
|
|
enum CallSetupMessageType {
|
|
|
|
Invite = "m.call.invite",
|
|
|
|
Answer = "m.call.answer",
|
|
|
|
Candidates = "m.call.candidates",
|
|
|
|
Hangup = "m.call.hangup",
|
|
|
|
}
|
|
|
|
|
2022-03-09 11:29:39 +01:00
|
|
|
const CONF_ID = "conf_id";
|
2022-02-14 17:14:21 +01:00
|
|
|
const CALL_TERMINATED = "m.terminated";
|
|
|
|
|
2022-02-17 16:58:44 +01:00
|
|
|
export class GroupCallHandler {
|
2022-02-15 17:05:20 +01:00
|
|
|
// group calls by call id
|
2022-02-17 16:58:44 +01:00
|
|
|
public readonly calls: ObservableMap<string, GroupCall> = new ObservableMap<string, GroupCall>();
|
2022-02-14 17:14:21 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-25 16:54:00 +01:00
|
|
|
// TODO: check and poll turn server credentials here
|
|
|
|
|
2022-02-14 17:14:21 +01:00
|
|
|
handleRoomState(room: Room, events: StateEvent[], log: ILogItem) {
|
|
|
|
// first update call events
|
|
|
|
for (const event of events) {
|
|
|
|
if (event.type === GROUP_CALL_TYPE) {
|
|
|
|
const callId = event.state_key;
|
2022-02-17 16:58:44 +01:00
|
|
|
let call = this.calls.get(callId);
|
2022-02-14 17:14:21 +01:00
|
|
|
if (call) {
|
|
|
|
call.updateCallEvent(event);
|
|
|
|
if (call.isTerminated) {
|
2022-02-17 16:58:44 +01:00
|
|
|
this.calls.remove(call.id);
|
2022-02-14 17:14:21 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
call = new GroupCall(event, room);
|
2022-02-17 16:58:44 +01:00
|
|
|
this.calls.set(call.id, call);
|
2022-02-14 17:14:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// then update participants
|
|
|
|
for (const event of events) {
|
|
|
|
if (event.type === GROUP_CALL_MEMBER_TYPE) {
|
|
|
|
const participant = event.state_key;
|
|
|
|
const sources = event.content["m.sources"];
|
|
|
|
for (const source of sources) {
|
2022-03-09 11:29:39 +01:00
|
|
|
const call = this.calls.get(source[CONF_ID]);
|
2022-02-14 17:14:21 +01:00
|
|
|
if (call && !call.isTerminated) {
|
|
|
|
call.addParticipant(participant, source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handlesDeviceMessageEventType(eventType: string | undefined): boolean {
|
|
|
|
return eventType === CallSetupMessageType.Invite ||
|
|
|
|
eventType === CallSetupMessageType.Candidates ||
|
|
|
|
eventType === CallSetupMessageType.Answer ||
|
|
|
|
eventType === CallSetupMessageType.Hangup;
|
|
|
|
}
|
|
|
|
|
2022-03-09 11:29:39 +01:00
|
|
|
handleDeviceMessage(senderUserId: string, senderDeviceId: string, event: SignallingMessage<MGroupCallBase>, log: ILogItem) {
|
|
|
|
const call = this.calls.get(event.content.conf_id);
|
|
|
|
call?.handleDeviceMessage(senderUserId, senderDeviceId, event, log);
|
2022-02-14 17:14:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|