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-19 21:02:51 +03:00
|
|
|
SilencedChanged = "silenced_changed",
|
2021-08-27 17:23:06 +03:00
|
|
|
LengthChanged = "length_changed",
|
2021-06-01 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 13:42:38 +03:00
|
|
|
const CONNECTING_STATES = [
|
|
|
|
CallState.Connecting,
|
|
|
|
CallState.WaitLocalMedia,
|
|
|
|
CallState.CreateOffer,
|
|
|
|
CallState.CreateAnswer,
|
|
|
|
];
|
|
|
|
|
2021-06-01 11:33:44 +03:00
|
|
|
const SUPPORTED_STATES = [
|
|
|
|
CallState.Connected,
|
|
|
|
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-02 11:18:32 +03:00
|
|
|
private events: Set<MatrixEvent> = new Set<MatrixEvent>();
|
|
|
|
private call: MatrixCall;
|
|
|
|
public state: CallState | CustomCallState;
|
2021-06-01 10:30:37 +03:00
|
|
|
|
2021-06-01 16:31:46 +03:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2021-06-19 21:02:51 +03:00
|
|
|
CallHandler.sharedInstance().addListener(CallHandlerEvent.CallsChanged, this.setCall);
|
|
|
|
CallHandler.sharedInstance().addListener(CallHandlerEvent.SilencedCallsChanged, this.onSilencedCallsChanged);
|
2021-06-01 16:31:46 +03:00
|
|
|
}
|
|
|
|
|
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-02 11:18:32 +03:00
|
|
|
private get hangup(): MatrixEvent {
|
|
|
|
return [...this.events].find((event) => event.getType() === EventType.CallHangup);
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:30:17 +03:00
|
|
|
private get reject(): MatrixEvent {
|
|
|
|
return [...this.events].find((event) => event.getType() === EventType.CallReject);
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:59:26 +03:00
|
|
|
private get selectAnswer(): MatrixEvent {
|
|
|
|
return [...this.events].find((event) => event.getType() === EventType.CallSelectAnswer);
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:18:32 +03:00
|
|
|
public get isVoice(): boolean {
|
|
|
|
const invite = this.invite;
|
|
|
|
if (!invite) return;
|
|
|
|
|
|
|
|
// FIXME: Find a better way to determine this from the event?
|
|
|
|
if (invite.getContent()?.offer?.sdp?.indexOf('m=video') !== -1) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get hangupReason(): string | null {
|
|
|
|
return this.hangup?.getContent()?.reason;
|
|
|
|
}
|
|
|
|
|
2021-07-25 11:28:27 +03:00
|
|
|
public get rejectParty(): string {
|
|
|
|
return this.reject?.getSender();
|
|
|
|
}
|
|
|
|
|
|
|
|
public get gotRejected(): boolean {
|
|
|
|
return Boolean(this.reject);
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:38:53 +03:00
|
|
|
public get duration(): Date {
|
2021-08-06 14:59:26 +03:00
|
|
|
if (!this.hangup || !this.selectAnswer) return;
|
|
|
|
return new Date(this.hangup.getDate().getTime() - this.selectAnswer.getDate().getTime());
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:18:32 +03:00
|
|
|
/**
|
|
|
|
* Returns true if there are only events from the other side - we missed the call
|
|
|
|
*/
|
|
|
|
private get callWasMissed(): boolean {
|
|
|
|
return ![...this.events].some((event) => event.sender?.userId === MatrixClientPeg.get().getUserId());
|
|
|
|
}
|
|
|
|
|
2021-09-02 18:52:00 +03:00
|
|
|
private get callId(): string | undefined {
|
|
|
|
return [...this.events][0]?.getContent()?.call_id;
|
2021-06-19 21:02:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-02 18:52:00 +03:00
|
|
|
private get roomId(): string | undefined {
|
2021-08-28 16:06:48 +03:00
|
|
|
return [...this.events][0]?.getRoomId();
|
2021-06-19 21:02:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private onSilencedCallsChanged = () => {
|
|
|
|
const newState = CallHandler.sharedInstance().isCallSilenced(this.callId);
|
2021-07-02 14:20:02 +03:00
|
|
|
this.emit(CallEventGrouperEvent.SilencedChanged, newState);
|
|
|
|
};
|
2021-06-19 21:02:51 +03:00
|
|
|
|
2021-08-27 17:23:06 +03:00
|
|
|
private onLengthChanged = (length: number): void => {
|
|
|
|
this.emit(CallEventGrouperEvent.LengthChanged, length);
|
|
|
|
};
|
|
|
|
|
2021-06-01 11:03:17 +03:00
|
|
|
public answerCall = () => {
|
2021-08-28 16:06:48 +03:00
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: 'answer',
|
|
|
|
room_id: this.roomId,
|
|
|
|
});
|
2021-07-02 14:20:02 +03:00
|
|
|
};
|
2021-06-01 10:30:37 +03:00
|
|
|
|
2021-06-01 11:03:17 +03:00
|
|
|
public rejectCall = () => {
|
2021-08-28 16:06:48 +03:00
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: 'reject',
|
|
|
|
room_id: this.roomId,
|
|
|
|
});
|
2021-07-02 14:20:02 +03:00
|
|
|
};
|
2021-06-01 10:30:37 +03:00
|
|
|
|
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-08-28 16:06:48 +03:00
|
|
|
room_id: this.roomId,
|
2021-06-01 12:28:45 +03:00
|
|
|
});
|
2021-07-02 14:20:02 +03:00
|
|
|
};
|
2021-06-01 10:30:37 +03:00
|
|
|
|
2021-06-19 21:02:51 +03:00
|
|
|
public toggleSilenced = () => {
|
|
|
|
const silenced = CallHandler.sharedInstance().isCallSilenced(this.callId);
|
|
|
|
silenced ?
|
|
|
|
CallHandler.sharedInstance().unSilenceCall(this.callId) :
|
|
|
|
CallHandler.sharedInstance().silenceCall(this.callId);
|
2021-07-02 14:20:02 +03:00
|
|
|
};
|
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-08-27 17:23:06 +03:00
|
|
|
this.call.addListener(CallEvent.LengthChanged, this.onLengthChanged);
|
2021-06-01 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 16:31:46 +03:00
|
|
|
private setState = () => {
|
2021-08-06 13:42:38 +03:00
|
|
|
if (CONNECTING_STATES.includes(this.call?.state)) {
|
|
|
|
this.state = CallState.Connecting;
|
|
|
|
} else 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-02 11:18:32 +03:00
|
|
|
if (this.callWasMissed) this.state = CustomCallState.Missed;
|
2021-06-02 11:30:17 +03:00
|
|
|
else if (this.reject) this.state = CallState.Ended;
|
|
|
|
else if (this.hangup) this.state = CallState.Ended;
|
|
|
|
else if (this.invite && 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-07-02 14:20:02 +03:00
|
|
|
};
|
2021-06-01 10:30:37 +03:00
|
|
|
|
2021-06-01 16:31:46 +03:00
|
|
|
private setCall = () => {
|
2021-06-02 11:18:32 +03:00
|
|
|
if (this.call) return;
|
|
|
|
|
2021-06-19 21:02:51 +03:00
|
|
|
this.call = CallHandler.sharedInstance().getCallById(this.callId);
|
2021-06-02 11:18:32 +03:00
|
|
|
this.setCallListeners();
|
2021-06-01 16:31:46 +03:00
|
|
|
this.setState();
|
2021-07-02 14:20:02 +03:00
|
|
|
};
|
2021-06-01 16:31:46 +03:00
|
|
|
|
|
|
|
public add(event: MatrixEvent) {
|
2021-06-01 17:28:57 +03:00
|
|
|
this.events.add(event);
|
2021-06-02 11:18:32 +03:00
|
|
|
this.setCall();
|
2021-06-01 10:30:37 +03:00
|
|
|
}
|
|
|
|
}
|