2021-06-01 10:30:37 +03:00
|
|
|
/*
|
|
|
|
Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com>
|
|
|
|
|
|
|
|
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 { EventType } from "matrix-js-sdk/src/@types/event";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2021-06-01 12:28:45 +03:00
|
|
|
import { CallEvent, CallState, CallType, MatrixCall } from "matrix-js-sdk/src/webrtc/call";
|
2021-06-01 16:31:46 +03:00
|
|
|
import CallHandler, { CallHandlerEvent } from '../../CallHandler';
|
2021-06-01 10:30:37 +03:00
|
|
|
import { EventEmitter } from 'events';
|
2021-06-01 12:28:45 +03:00
|
|
|
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
|
|
|
import defaultDispatcher from "../../dispatcher/dispatcher";
|
2021-06-01 10:30:37 +03:00
|
|
|
|
|
|
|
export enum CallEventGrouperEvent {
|
|
|
|
StateChanged = "state_changed",
|
|
|
|
}
|
|
|
|
|
2021-06-01 11:33:44 +03:00
|
|
|
const SUPPORTED_STATES = [
|
|
|
|
CallState.Connected,
|
|
|
|
CallState.Connecting,
|
|
|
|
CallState.Ringing,
|
|
|
|
];
|
|
|
|
|
2021-06-01 12:28:45 +03:00
|
|
|
export enum CustomCallState {
|
|
|
|
Missed = "missed",
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:30:37 +03:00
|
|
|
export default class CallEventGrouper extends EventEmitter {
|
2021-06-01 17:28:57 +03:00
|
|
|
events: Set<MatrixEvent> = new Set<MatrixEvent>();
|
2021-06-01 10:30:37 +03:00
|
|
|
call: MatrixCall;
|
2021-06-01 12:28:45 +03:00
|
|
|
state: CallState | CustomCallState;
|
2021-06-01 10:30:37 +03:00
|
|
|
|
2021-06-01 16:31:46 +03:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
CallHandler.sharedInstance().addListener(CallHandlerEvent.CallsChanged, this.setCall)
|
|
|
|
}
|
|
|
|
|
2021-06-01 11:42:21 +03:00
|
|
|
private get invite(): MatrixEvent {
|
2021-06-01 17:28:57 +03:00
|
|
|
return [...this.events].find((event) => event.getType() === EventType.CallInvite);
|
2021-06-01 11:42:21 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 11:03:17 +03:00
|
|
|
public answerCall = () => {
|
2021-06-01 10:30:37 +03:00
|
|
|
this.call?.answer();
|
|
|
|
}
|
|
|
|
|
2021-06-01 11:03:17 +03:00
|
|
|
public rejectCall = () => {
|
2021-06-01 10:30:37 +03:00
|
|
|
this.call?.reject();
|
|
|
|
}
|
|
|
|
|
2021-06-01 11:03:17 +03:00
|
|
|
public callBack = () => {
|
2021-06-01 12:28:45 +03:00
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: 'place_call',
|
|
|
|
type: this.isVoice ? CallType.Voice : CallType.Video,
|
2021-06-01 17:28:57 +03:00
|
|
|
room_id: [...this.events][0]?.getRoomId(),
|
2021-06-01 12:28:45 +03:00
|
|
|
});
|
2021-06-01 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public isVoice(): boolean {
|
|
|
|
const invite = this.invite;
|
|
|
|
|
|
|
|
// FIXME: Find a better way to determine this from the event?
|
|
|
|
let isVoice = true;
|
|
|
|
if (
|
|
|
|
invite.getContent().offer && invite.getContent().offer.sdp &&
|
|
|
|
invite.getContent().offer.sdp.indexOf('m=video') !== -1
|
|
|
|
) {
|
|
|
|
isVoice = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isVoice;
|
|
|
|
}
|
|
|
|
|
2021-06-01 12:28:45 +03:00
|
|
|
public getState(): CallState | CustomCallState {
|
2021-06-01 10:30:37 +03:00
|
|
|
return this.state;
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:46:41 +03:00
|
|
|
public getHangupReason(): string | null {
|
2021-06-01 17:28:57 +03:00
|
|
|
return [...this.events].find((event) => event.getType() === EventType.CallHangup)?.getContent()?.reason;
|
2021-06-01 15:46:41 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 14:40:25 +03:00
|
|
|
/**
|
|
|
|
* Returns true if there are only events from the other side - we missed the call
|
|
|
|
*/
|
|
|
|
private wasThisCallMissed(): boolean {
|
2021-06-01 17:28:57 +03:00
|
|
|
return ![...this.events].some((event) => event.sender?.userId === MatrixClientPeg.get().getUserId());
|
2021-06-01 14:40:25 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:30:37 +03:00
|
|
|
private setCallListeners() {
|
2021-06-01 11:55:03 +03:00
|
|
|
if (!this.call) return;
|
2021-06-01 16:31:46 +03:00
|
|
|
this.call.addListener(CallEvent.State, this.setState);
|
2021-06-01 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 16:31:46 +03:00
|
|
|
private setState = () => {
|
2021-06-01 11:55:03 +03:00
|
|
|
if (SUPPORTED_STATES.includes(this.call?.state)) {
|
2021-06-01 11:33:44 +03:00
|
|
|
this.state = this.call.state;
|
2021-06-01 11:55:03 +03:00
|
|
|
} else {
|
2021-06-01 17:28:57 +03:00
|
|
|
const lastEvent = [...this.events][this.events.size - 1];
|
2021-06-01 11:55:03 +03:00
|
|
|
const lastEventType = lastEvent.getType();
|
|
|
|
|
2021-06-01 14:40:25 +03:00
|
|
|
if (this.wasThisCallMissed()) this.state = CustomCallState.Missed;
|
|
|
|
else if (lastEventType === EventType.CallHangup) this.state = CallState.Ended;
|
2021-06-01 11:55:03 +03:00
|
|
|
else if (lastEventType === EventType.CallReject) this.state = CallState.Ended;
|
2021-06-01 12:28:45 +03:00
|
|
|
else if (lastEventType === EventType.CallInvite && this.call) this.state = CallState.Connecting;
|
2021-06-01 11:33:44 +03:00
|
|
|
}
|
2021-06-01 11:55:03 +03:00
|
|
|
this.emit(CallEventGrouperEvent.StateChanged, this.state);
|
2021-06-01 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 16:31:46 +03:00
|
|
|
private setCall = () => {
|
2021-06-01 17:28:57 +03:00
|
|
|
const callId = [...this.events][0].getContent().call_id;
|
2021-06-01 11:55:03 +03:00
|
|
|
if (!this.call) {
|
|
|
|
this.call = CallHandler.sharedInstance().getCallById(callId);
|
|
|
|
this.setCallListeners();
|
|
|
|
}
|
2021-06-01 16:31:46 +03:00
|
|
|
this.setState();
|
|
|
|
}
|
|
|
|
|
|
|
|
public add(event: MatrixEvent) {
|
2021-06-01 17:28:57 +03:00
|
|
|
this.events.add(event);
|
2021-06-01 16:31:46 +03:00
|
|
|
this.setState();
|
2021-06-01 10:30:37 +03:00
|
|
|
}
|
|
|
|
}
|