2020-09-24 18:16:20 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
|
|
|
Copyright 2019, 2020 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Manages a list of all the currently active calls.
|
|
|
|
*
|
|
|
|
* This handler dispatches when voip calls are added/updated/removed from this list:
|
|
|
|
* {
|
|
|
|
* action: 'call_state'
|
|
|
|
* room_id: <room ID of the call>
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* To know the state of the call, this handler exposes a getter to
|
|
|
|
* obtain the call for a room:
|
|
|
|
* var call = CallHandler.getCall(roomId)
|
|
|
|
* var state = call.call_state; // ringing|ringback|connected|ended|busy|stop_ringback|stop_ringing
|
|
|
|
*
|
|
|
|
* This handler listens for and handles the following actions:
|
|
|
|
* {
|
|
|
|
* action: 'place_call',
|
|
|
|
* type: 'voice|video',
|
|
|
|
* room_id: <room that the place call button was pressed in>
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* action: 'incoming_call'
|
|
|
|
* call: MatrixCall
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* action: 'hangup'
|
|
|
|
* room_id: <room that the hangup button was pressed in>
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* action: 'answer'
|
|
|
|
* room_id: <room that the answer button was pressed in>
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import {MatrixClientPeg} from './MatrixClientPeg';
|
|
|
|
import PlatformPeg from './PlatformPeg';
|
|
|
|
import Modal from './Modal';
|
|
|
|
import { _t } from './languageHandler';
|
|
|
|
import dis from './dispatcher/dispatcher';
|
|
|
|
import WidgetUtils from './utils/WidgetUtils';
|
|
|
|
import WidgetEchoStore from './stores/WidgetEchoStore';
|
|
|
|
import SettingsStore from './settings/SettingsStore';
|
|
|
|
import {Jitsi} from "./widgets/Jitsi";
|
|
|
|
import {WidgetType} from "./widgets/WidgetType";
|
|
|
|
import {SettingLevel} from "./settings/SettingLevel";
|
2020-09-24 20:30:30 +03:00
|
|
|
import { ActionPayload } from "./dispatcher/payloads";
|
2020-09-24 18:16:20 +03:00
|
|
|
import {base32} from "rfc4648";
|
|
|
|
|
|
|
|
import QuestionDialog from "./components/views/dialogs/QuestionDialog";
|
|
|
|
import ErrorDialog from "./components/views/dialogs/ErrorDialog";
|
2020-09-28 22:53:44 +03:00
|
|
|
import WidgetStore from "./stores/WidgetStore";
|
2020-10-01 05:09:23 +03:00
|
|
|
import { WidgetMessagingStore } from "./stores/widgets/WidgetMessagingStore";
|
|
|
|
import { ElementWidgetActions } from "./stores/widgets/ElementWidgetActions";
|
2020-10-30 19:49:42 +03:00
|
|
|
import { MatrixCall, CallErrorCode, CallState, CallEvent, CallParty, CallType } from "matrix-js-sdk/src/webrtc/call";
|
2020-10-19 16:56:15 +03:00
|
|
|
import Analytics from './Analytics';
|
2020-10-29 18:53:14 +03:00
|
|
|
import CountlyAnalytics from "./CountlyAnalytics";
|
2020-11-23 19:20:15 +03:00
|
|
|
import {UIFeature} from "./settings/UIFeature";
|
2020-11-27 15:53:09 +03:00
|
|
|
import { CallError } from "matrix-js-sdk/src/webrtc/call";
|
2020-12-03 20:45:49 +03:00
|
|
|
import { logger } from 'matrix-js-sdk/src/logger';
|
2020-12-26 10:32:51 +03:00
|
|
|
import DesktopCapturerSourcePicker from "./components/views/elements/DesktopCapturerSourcePicker"
|
2020-12-23 22:02:01 +03:00
|
|
|
import { Action } from './dispatcher/actions';
|
2021-02-12 23:55:54 +03:00
|
|
|
import VoipUserMapper from './VoipUserMapper';
|
2021-01-29 17:26:33 +03:00
|
|
|
import { addManagedHybridWidget, isManagedHybridWidgetEnabled } from './widgets/ManagedHybrid';
|
2021-02-22 19:48:12 +03:00
|
|
|
import { randomUppercaseString, randomLowercaseString } from "matrix-js-sdk/src/randomstring";
|
2021-04-27 12:01:36 +03:00
|
|
|
import EventEmitter from 'events';
|
2021-04-19 22:30:51 +03:00
|
|
|
import SdkConfig from './SdkConfig';
|
|
|
|
import { ensureDMExists, findDMForUser } from './createRoom';
|
2020-12-23 22:02:01 +03:00
|
|
|
|
2021-02-12 23:55:54 +03:00
|
|
|
export const PROTOCOL_PSTN = 'm.protocol.pstn';
|
|
|
|
export const PROTOCOL_PSTN_PREFIXED = 'im.vector.protocol.pstn';
|
|
|
|
export const PROTOCOL_SIP_NATIVE = 'im.vector.protocol.sip_native';
|
|
|
|
export const PROTOCOL_SIP_VIRTUAL = 'im.vector.protocol.sip_virtual';
|
|
|
|
|
|
|
|
const CHECK_PROTOCOLS_ATTEMPTS = 3;
|
2021-02-17 21:51:21 +03:00
|
|
|
// Event type for room account data and room creation content used to mark rooms as virtual rooms
|
|
|
|
// (and store the ID of their native room)
|
2021-02-12 23:55:54 +03:00
|
|
|
export const VIRTUAL_ROOM_EVENT_TYPE = 'im.vector.is_virtual_room';
|
2020-09-24 18:16:20 +03:00
|
|
|
|
2020-10-12 11:55:21 +03:00
|
|
|
enum AudioID {
|
2020-10-09 20:56:07 +03:00
|
|
|
Ring = 'ringAudio',
|
|
|
|
Ringback = 'ringbackAudio',
|
|
|
|
CallEnd = 'callendAudio',
|
|
|
|
Busy = 'busyAudio',
|
|
|
|
}
|
2020-09-24 20:18:26 +03:00
|
|
|
|
2021-02-15 18:25:07 +03:00
|
|
|
interface ThirdpartyLookupResponseFields {
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
|
|
|
|
// im.vector.sip_native
|
2021-02-16 21:52:49 +03:00
|
|
|
virtual_mxid?: string;
|
|
|
|
is_virtual?: boolean;
|
2021-02-15 18:25:07 +03:00
|
|
|
|
|
|
|
// im.vector.sip_virtual
|
2021-02-16 21:52:49 +03:00
|
|
|
native_mxid?: string;
|
|
|
|
is_native?: boolean;
|
2021-02-15 18:25:07 +03:00
|
|
|
|
|
|
|
// common
|
2021-02-16 21:52:49 +03:00
|
|
|
lookup_success?: boolean;
|
2021-02-15 18:25:07 +03:00
|
|
|
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:04:01 +03:00
|
|
|
interface ThirdpartyLookupResponse {
|
2021-02-12 23:55:54 +03:00
|
|
|
userid: string,
|
|
|
|
protocol: string,
|
2021-02-15 18:25:07 +03:00
|
|
|
fields: ThirdpartyLookupResponseFields,
|
2021-02-12 23:55:54 +03:00
|
|
|
}
|
|
|
|
|
2020-10-12 13:38:32 +03:00
|
|
|
// Unlike 'CallType' in js-sdk, this one includes screen sharing
|
|
|
|
// (because a screen sharing call is only a screen sharing call to the caller,
|
|
|
|
// to the callee it's just a video call, at least as far as the current impl
|
|
|
|
// is concerned).
|
|
|
|
export enum PlaceCallType {
|
|
|
|
Voice = 'voice',
|
|
|
|
Video = 'video',
|
|
|
|
ScreenSharing = 'screensharing',
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:01:36 +03:00
|
|
|
export enum CallHandlerEvent {
|
|
|
|
CallsChanged = "calls_changed",
|
2021-04-28 12:49:07 +03:00
|
|
|
CallChangeRoom = "call_change_room",
|
2021-04-27 12:01:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class CallHandler extends EventEmitter {
|
2020-12-03 20:45:49 +03:00
|
|
|
private calls = new Map<string, MatrixCall>(); // roomId -> call
|
2021-03-25 22:56:21 +03:00
|
|
|
// Calls started as an attended transfer, ie. with the intention of transferring another
|
|
|
|
// call with a different party to this one.
|
|
|
|
private transferees = new Map<string, MatrixCall>(); // callId (target) -> call (transferee)
|
2020-10-12 11:55:21 +03:00
|
|
|
private audioPromises = new Map<AudioID, Promise<void>>();
|
2020-12-15 21:01:42 +03:00
|
|
|
private dispatcherRef: string = null;
|
2020-12-23 22:02:01 +03:00
|
|
|
private supportsPstnProtocol = null;
|
2021-02-12 23:55:54 +03:00
|
|
|
private pstnSupportPrefixed = null; // True if the server only support the prefixed pstn protocol
|
|
|
|
private supportsSipNativeVirtual = null; // im.vector.protocol.sip_virtual and im.vector.protocol.sip_native
|
2020-12-23 22:02:01 +03:00
|
|
|
private pstnSupportCheckTimer: NodeJS.Timeout; // number actually because we're in the browser
|
2021-02-12 23:55:54 +03:00
|
|
|
// For rooms we've been invited to, true if they're from virtual user, false if we've checked and they aren't.
|
|
|
|
private invitedRoomsAreVirtual = new Map<string, boolean>();
|
|
|
|
private invitedRoomCheckInProgress = false;
|
2020-09-24 18:16:20 +03:00
|
|
|
|
2021-04-27 20:55:53 +03:00
|
|
|
// Map of the asserted identity users after we've looked them up using the API.
|
2021-04-19 22:30:51 +03:00
|
|
|
// We need to be be able to determine the mapped room synchronously, so we
|
|
|
|
// do the async lookup when we get new information and then store these mappings here
|
|
|
|
private assertedIdentityNativeUsers = new Map<string, string>();
|
|
|
|
|
2020-09-24 18:16:20 +03:00
|
|
|
static sharedInstance() {
|
|
|
|
if (!window.mxCallHandler) {
|
|
|
|
window.mxCallHandler = new CallHandler()
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.mxCallHandler;
|
|
|
|
}
|
|
|
|
|
2021-01-21 22:20:35 +03:00
|
|
|
/*
|
|
|
|
* Gets the user-facing room associated with a call (call.roomId may be the call "virtual room"
|
|
|
|
* if a voip_mxid_translate_pattern is set in the config)
|
|
|
|
*/
|
2021-04-19 22:30:51 +03:00
|
|
|
public roomIdForCall(call: MatrixCall): string {
|
2021-01-21 22:20:35 +03:00
|
|
|
if (!call) return null;
|
2021-04-19 22:30:51 +03:00
|
|
|
|
2021-04-27 21:33:53 +03:00
|
|
|
const voipConfig = SdkConfig.get()['voip'];
|
|
|
|
|
|
|
|
if (voipConfig && voipConfig.obeyAssertedIdentity) {
|
2021-04-19 22:30:51 +03:00
|
|
|
const nativeUser = this.assertedIdentityNativeUsers[call.callId];
|
|
|
|
if (nativeUser) {
|
|
|
|
const room = findDMForUser(MatrixClientPeg.get(), nativeUser);
|
|
|
|
if (room) return room.roomId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 23:55:54 +03:00
|
|
|
return VoipUserMapper.sharedInstance().nativeRoomForVirtualRoom(call.roomId) || call.roomId;
|
2021-01-21 22:20:35 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:20:15 +03:00
|
|
|
start() {
|
2020-12-15 19:53:11 +03:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2020-09-24 18:16:20 +03:00
|
|
|
// add empty handlers for media actions, otherwise the media keys
|
|
|
|
// end up causing the audio elements with our ring/ringback etc
|
|
|
|
// audio clips in to play.
|
|
|
|
if (navigator.mediaSession) {
|
|
|
|
navigator.mediaSession.setActionHandler('play', function() {});
|
|
|
|
navigator.mediaSession.setActionHandler('pause', function() {});
|
|
|
|
navigator.mediaSession.setActionHandler('seekbackward', function() {});
|
|
|
|
navigator.mediaSession.setActionHandler('seekforward', function() {});
|
|
|
|
navigator.mediaSession.setActionHandler('previoustrack', function() {});
|
|
|
|
navigator.mediaSession.setActionHandler('nexttrack', function() {});
|
|
|
|
}
|
2020-11-23 19:20:15 +03:00
|
|
|
|
|
|
|
if (SettingsStore.getValue(UIFeature.Voip)) {
|
|
|
|
MatrixClientPeg.get().on('Call.incoming', this.onCallIncoming);
|
|
|
|
}
|
2020-12-23 22:02:01 +03:00
|
|
|
|
2021-02-12 23:55:54 +03:00
|
|
|
this.checkProtocols(CHECK_PROTOCOLS_ATTEMPTS);
|
2020-11-23 19:20:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
if (cli) {
|
|
|
|
cli.removeListener('Call.incoming', this.onCallIncoming);
|
|
|
|
}
|
2020-12-16 13:53:59 +03:00
|
|
|
if (this.dispatcherRef !== null) {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
this.dispatcherRef = null;
|
|
|
|
}
|
2020-11-23 19:20:15 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 23:55:54 +03:00
|
|
|
private async checkProtocols(maxTries) {
|
2020-12-23 22:02:01 +03:00
|
|
|
try {
|
|
|
|
const protocols = await MatrixClientPeg.get().getThirdpartyProtocols();
|
2021-02-12 23:55:54 +03:00
|
|
|
|
|
|
|
if (protocols[PROTOCOL_PSTN] !== undefined) {
|
|
|
|
this.supportsPstnProtocol = Boolean(protocols[PROTOCOL_PSTN]);
|
|
|
|
if (this.supportsPstnProtocol) this.pstnSupportPrefixed = false;
|
|
|
|
} else if (protocols[PROTOCOL_PSTN_PREFIXED] !== undefined) {
|
|
|
|
this.supportsPstnProtocol = Boolean(protocols[PROTOCOL_PSTN_PREFIXED]);
|
|
|
|
if (this.supportsPstnProtocol) this.pstnSupportPrefixed = true;
|
2020-12-23 22:02:01 +03:00
|
|
|
} else {
|
|
|
|
this.supportsPstnProtocol = null;
|
|
|
|
}
|
2021-02-12 23:55:54 +03:00
|
|
|
|
2020-12-23 22:02:01 +03:00
|
|
|
dis.dispatch({action: Action.PstnSupportUpdated});
|
2021-02-12 23:55:54 +03:00
|
|
|
|
|
|
|
if (protocols[PROTOCOL_SIP_NATIVE] !== undefined && protocols[PROTOCOL_SIP_VIRTUAL] !== undefined) {
|
|
|
|
this.supportsSipNativeVirtual = Boolean(
|
|
|
|
protocols[PROTOCOL_SIP_NATIVE] && protocols[PROTOCOL_SIP_VIRTUAL],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
dis.dispatch({action: Action.VirtualRoomSupportUpdated});
|
2020-12-23 22:02:01 +03:00
|
|
|
} catch (e) {
|
|
|
|
if (maxTries === 1) {
|
2021-02-12 23:55:54 +03:00
|
|
|
console.log("Failed to check for protocol support and no retries remain: assuming no support", e);
|
2020-12-23 22:02:01 +03:00
|
|
|
} else {
|
2021-02-12 23:55:54 +03:00
|
|
|
console.log("Failed to check for protocol support: will retry", e);
|
2020-12-23 22:02:01 +03:00
|
|
|
this.pstnSupportCheckTimer = setTimeout(() => {
|
2021-02-12 23:55:54 +03:00
|
|
|
this.checkProtocols(maxTries - 1);
|
2020-12-23 22:02:01 +03:00
|
|
|
}, 10000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 23:55:54 +03:00
|
|
|
public getSupportsPstnProtocol() {
|
|
|
|
return this.supportsPstnProtocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getSupportsVirtualRooms() {
|
2020-12-23 22:02:01 +03:00
|
|
|
return this.supportsPstnProtocol;
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:04:01 +03:00
|
|
|
public pstnLookup(phoneNumber: string): Promise<ThirdpartyLookupResponse[]> {
|
2021-02-12 23:55:54 +03:00
|
|
|
return MatrixClientPeg.get().getThirdpartyUser(
|
|
|
|
this.pstnSupportPrefixed ? PROTOCOL_PSTN_PREFIXED : PROTOCOL_PSTN, {
|
|
|
|
'm.id.phone': phoneNumber,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:04:01 +03:00
|
|
|
public sipVirtualLookup(nativeMxid: string): Promise<ThirdpartyLookupResponse[]> {
|
2021-02-12 23:55:54 +03:00
|
|
|
return MatrixClientPeg.get().getThirdpartyUser(
|
|
|
|
PROTOCOL_SIP_VIRTUAL, {
|
|
|
|
'native_mxid': nativeMxid,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:04:01 +03:00
|
|
|
public sipNativeLookup(virtualMxid: string): Promise<ThirdpartyLookupResponse[]> {
|
2021-02-12 23:55:54 +03:00
|
|
|
return MatrixClientPeg.get().getThirdpartyUser(
|
|
|
|
PROTOCOL_SIP_NATIVE, {
|
|
|
|
'virtual_mxid': virtualMxid,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:20:15 +03:00
|
|
|
private onCallIncoming = (call) => {
|
|
|
|
// we dispatch this synchronously to make sure that the event
|
|
|
|
// handlers on the call are set up immediately (so that if
|
|
|
|
// we get an immediate hangup, we don't get a stuck call)
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'incoming_call',
|
|
|
|
call: call,
|
|
|
|
}, true);
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:56:07 +03:00
|
|
|
getCallForRoom(roomId: string): MatrixCall {
|
2020-09-24 20:18:26 +03:00
|
|
|
return this.calls.get(roomId) || null;
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getAnyActiveCall() {
|
2020-10-01 13:28:42 +03:00
|
|
|
for (const call of this.calls.values()) {
|
2020-10-12 12:25:23 +03:00
|
|
|
if (call.state !== CallState.Ended) {
|
2020-10-01 13:28:42 +03:00
|
|
|
return call;
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-03 20:45:49 +03:00
|
|
|
getAllActiveCalls() {
|
|
|
|
const activeCalls = [];
|
|
|
|
|
|
|
|
for (const call of this.calls.values()) {
|
|
|
|
if (call.state !== CallState.Ended && call.state !== CallState.Ringing) {
|
|
|
|
activeCalls.push(call);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return activeCalls;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllActiveCallsNotInRoom(notInThisRoomId) {
|
|
|
|
const callsNotInThatRoom = [];
|
|
|
|
|
|
|
|
for (const [roomId, call] of this.calls.entries()) {
|
|
|
|
if (roomId !== notInThisRoomId && call.state !== CallState.Ended) {
|
|
|
|
callsNotInThatRoom.push(call);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return callsNotInThatRoom;
|
|
|
|
}
|
|
|
|
|
2021-03-25 22:56:21 +03:00
|
|
|
getTransfereeForCallId(callId: string): MatrixCall {
|
|
|
|
return this.transferees[callId];
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:55:21 +03:00
|
|
|
play(audioId: AudioID) {
|
2020-09-24 18:16:20 +03:00
|
|
|
// TODO: Attach an invisible element for this instead
|
|
|
|
// which listens?
|
|
|
|
const audio = document.getElementById(audioId) as HTMLMediaElement;
|
|
|
|
if (audio) {
|
|
|
|
const playAudio = async () => {
|
|
|
|
try {
|
|
|
|
// This still causes the chrome debugger to break on promise rejection if
|
|
|
|
// the promise is rejected, even though we're catching the exception.
|
|
|
|
await audio.play();
|
|
|
|
} catch (e) {
|
|
|
|
// This is usually because the user hasn't interacted with the document,
|
|
|
|
// or chrome doesn't think so and is denying the request. Not sure what
|
|
|
|
// we can really do here...
|
|
|
|
// https://github.com/vector-im/element-web/issues/7657
|
|
|
|
console.log("Unable to play audio clip", e);
|
|
|
|
}
|
|
|
|
};
|
2020-09-24 20:28:46 +03:00
|
|
|
if (this.audioPromises.has(audioId)) {
|
|
|
|
this.audioPromises.set(audioId, this.audioPromises.get(audioId).then(() => {
|
2020-09-24 18:16:20 +03:00
|
|
|
audio.load();
|
|
|
|
return playAudio();
|
2020-09-24 20:28:46 +03:00
|
|
|
}));
|
2020-09-24 18:16:20 +03:00
|
|
|
} else {
|
2020-09-24 20:28:46 +03:00
|
|
|
this.audioPromises.set(audioId, playAudio());
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:55:21 +03:00
|
|
|
pause(audioId: AudioID) {
|
2020-09-24 18:16:20 +03:00
|
|
|
// TODO: Attach an invisible element for this instead
|
|
|
|
// which listens?
|
|
|
|
const audio = document.getElementById(audioId) as HTMLMediaElement;
|
|
|
|
if (audio) {
|
2020-09-24 20:28:46 +03:00
|
|
|
if (this.audioPromises.has(audioId)) {
|
|
|
|
this.audioPromises.set(audioId, this.audioPromises.get(audioId).then(() => audio.pause()));
|
2020-09-24 18:16:20 +03:00
|
|
|
} else {
|
2020-09-24 20:28:46 +03:00
|
|
|
// pause doesn't return a promise, so just do it
|
|
|
|
audio.pause();
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 17:08:23 +03:00
|
|
|
private matchesCallForThisRoom(call: MatrixCall) {
|
|
|
|
// We don't allow placing more than one call per room, but that doesn't mean there
|
|
|
|
// can't be more than one, eg. in a glare situation. This checks that the given call
|
|
|
|
// is the call we consider 'the' call for its room.
|
2021-04-23 16:39:39 +03:00
|
|
|
const mappedRoomId = this.roomIdForCall(call);
|
2021-01-21 22:20:35 +03:00
|
|
|
|
|
|
|
const callForThisRoom = this.getCallForRoom(mappedRoomId);
|
2020-10-13 17:08:23 +03:00
|
|
|
return callForThisRoom && call.callId === callForThisRoom.callId;
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:56:07 +03:00
|
|
|
private setCallListeners(call: MatrixCall) {
|
2021-04-19 22:30:51 +03:00
|
|
|
let mappedRoomId = CallHandler.sharedInstance().roomIdForCall(call);
|
2021-01-21 22:20:35 +03:00
|
|
|
|
2020-11-27 15:53:09 +03:00
|
|
|
call.on(CallEvent.Error, (err: CallError) => {
|
2020-10-13 17:08:23 +03:00
|
|
|
if (!this.matchesCallForThisRoom(call)) return;
|
|
|
|
|
2020-11-27 15:53:09 +03:00
|
|
|
Analytics.trackEvent('voip', 'callError', 'error', err.toString());
|
2020-09-24 18:16:20 +03:00
|
|
|
console.error("Call error:", err);
|
2020-11-27 15:53:09 +03:00
|
|
|
|
|
|
|
if (err.code === CallErrorCode.NoUserMedia) {
|
|
|
|
this.showMediaCaptureError(call);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:16:20 +03:00
|
|
|
if (
|
|
|
|
MatrixClientPeg.get().getTurnServers().length === 0 &&
|
|
|
|
SettingsStore.getValue("fallbackICEServerAllowed") === null
|
|
|
|
) {
|
|
|
|
this.showICEFallbackPrompt();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Modal.createTrackedDialog('Call Failed', '', ErrorDialog, {
|
|
|
|
title: _t('Call Failed'),
|
|
|
|
description: err.message,
|
|
|
|
});
|
|
|
|
});
|
2020-10-12 12:25:23 +03:00
|
|
|
call.on(CallEvent.Hangup, () => {
|
2020-10-13 17:08:23 +03:00
|
|
|
if (!this.matchesCallForThisRoom(call)) return;
|
|
|
|
|
2020-10-19 16:56:15 +03:00
|
|
|
Analytics.trackEvent('voip', 'callHangup');
|
|
|
|
|
2021-01-21 22:20:35 +03:00
|
|
|
this.removeCallForRoom(mappedRoomId);
|
2020-09-24 18:16:20 +03:00
|
|
|
});
|
2020-10-12 12:25:23 +03:00
|
|
|
call.on(CallEvent.State, (newState: CallState, oldState: CallState) => {
|
2020-10-13 17:08:23 +03:00
|
|
|
if (!this.matchesCallForThisRoom(call)) return;
|
|
|
|
|
2020-10-09 20:56:07 +03:00
|
|
|
this.setCallState(call, newState);
|
|
|
|
|
|
|
|
switch (oldState) {
|
|
|
|
case CallState.Ringing:
|
2020-10-12 11:55:21 +03:00
|
|
|
this.pause(AudioID.Ring);
|
2020-10-09 20:56:07 +03:00
|
|
|
break;
|
|
|
|
case CallState.InviteSent:
|
2020-10-12 11:55:21 +03:00
|
|
|
this.pause(AudioID.Ringback);
|
2020-10-09 20:56:07 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (newState) {
|
|
|
|
case CallState.Ringing:
|
2020-10-12 11:55:21 +03:00
|
|
|
this.play(AudioID.Ring);
|
2020-10-09 20:56:07 +03:00
|
|
|
break;
|
|
|
|
case CallState.InviteSent:
|
2020-10-12 11:55:21 +03:00
|
|
|
this.play(AudioID.Ringback);
|
2020-10-09 20:56:07 +03:00
|
|
|
break;
|
|
|
|
case CallState.Ended:
|
2021-01-26 12:41:57 +03:00
|
|
|
{
|
2020-10-19 17:04:57 +03:00
|
|
|
Analytics.trackEvent('voip', 'callEnded', 'hangupReason', call.hangupReason);
|
2021-01-21 22:20:35 +03:00
|
|
|
this.removeCallForRoom(mappedRoomId);
|
2020-10-09 20:56:07 +03:00
|
|
|
if (oldState === CallState.InviteSent && (
|
2020-10-12 12:25:23 +03:00
|
|
|
call.hangupParty === CallParty.Remote ||
|
|
|
|
(call.hangupParty === CallParty.Local && call.hangupReason === CallErrorCode.InviteTimeout)
|
2020-09-24 18:16:20 +03:00
|
|
|
)) {
|
2020-10-12 11:55:21 +03:00
|
|
|
this.play(AudioID.Busy);
|
2020-10-15 16:54:03 +03:00
|
|
|
let title;
|
|
|
|
let description;
|
2020-10-16 22:28:20 +03:00
|
|
|
if (call.hangupReason === CallErrorCode.UserHangup) {
|
2020-10-15 16:54:03 +03:00
|
|
|
title = _t("Call Declined");
|
|
|
|
description = _t("The other party declined the call.");
|
2021-05-27 20:57:22 +03:00
|
|
|
} else if (call.hangupReason === CallErrorCode.UserBusy) {
|
|
|
|
title = _t("User Busy");
|
|
|
|
description = _t("The user you called is busy.");
|
2020-10-15 16:54:03 +03:00
|
|
|
} else if (call.hangupReason === CallErrorCode.InviteTimeout) {
|
|
|
|
title = _t("Call Failed");
|
|
|
|
// XXX: full stop appended as some relic here, but these
|
|
|
|
// strings need proper input from design anyway, so let's
|
|
|
|
// not change this string until we have a proper one.
|
|
|
|
description = _t('The remote side failed to pick up') + '.';
|
|
|
|
} else {
|
|
|
|
title = _t("Call Failed");
|
|
|
|
description = _t("The call could not be established");
|
|
|
|
}
|
|
|
|
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'Call Failed', ErrorDialog, {
|
|
|
|
title, description,
|
2020-10-09 20:56:07 +03:00
|
|
|
});
|
2020-11-30 18:17:20 +03:00
|
|
|
} else if (
|
|
|
|
call.hangupReason === CallErrorCode.AnsweredElsewhere && oldState === CallState.Connecting
|
|
|
|
) {
|
2020-10-21 13:54:48 +03:00
|
|
|
Modal.createTrackedDialog('Call Handler', 'Call Failed', ErrorDialog, {
|
|
|
|
title: _t("Answered Elsewhere"),
|
|
|
|
description: _t("The call was answered on another device."),
|
|
|
|
});
|
2021-01-25 19:18:14 +03:00
|
|
|
} else if (oldState !== CallState.Fledgling && oldState !== CallState.Ringing) {
|
2020-12-18 16:46:58 +03:00
|
|
|
// don't play the end-call sound for calls that never got off the ground
|
2020-10-12 11:55:21 +03:00
|
|
|
this.play(AudioID.CallEnd);
|
2020-10-09 20:56:07 +03:00
|
|
|
}
|
2021-01-26 12:41:57 +03:00
|
|
|
|
|
|
|
this.logCallStats(call, mappedRoomId);
|
2021-01-27 13:36:40 +03:00
|
|
|
break;
|
2021-01-26 12:41:57 +03:00
|
|
|
}
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
});
|
2020-10-13 17:08:23 +03:00
|
|
|
call.on(CallEvent.Replaced, (newCall: MatrixCall) => {
|
|
|
|
if (!this.matchesCallForThisRoom(call)) return;
|
|
|
|
|
|
|
|
console.log(`Call ID ${call.callId} is being replaced by call ID ${newCall.callId}`);
|
|
|
|
|
|
|
|
if (call.state === CallState.Ringing) {
|
|
|
|
this.pause(AudioID.Ring);
|
|
|
|
} else if (call.state === CallState.InviteSent) {
|
|
|
|
this.pause(AudioID.Ringback);
|
|
|
|
}
|
|
|
|
|
2021-01-21 22:20:35 +03:00
|
|
|
this.calls.set(mappedRoomId, newCall);
|
2021-04-27 12:01:36 +03:00
|
|
|
this.emit(CallHandlerEvent.CallsChanged, this.calls);
|
2020-10-13 17:08:23 +03:00
|
|
|
this.setCallListeners(newCall);
|
|
|
|
this.setCallState(newCall, newCall.state);
|
|
|
|
});
|
2021-04-19 22:30:51 +03:00
|
|
|
call.on(CallEvent.AssertedIdentityChanged, async () => {
|
|
|
|
if (!this.matchesCallForThisRoom(call)) return;
|
|
|
|
|
|
|
|
console.log(`Call ID ${call.callId} got new asserted identity:`, call.getRemoteAssertedIdentity());
|
|
|
|
|
|
|
|
const newAssertedIdentity = call.getRemoteAssertedIdentity().id;
|
|
|
|
let newNativeAssertedIdentity = newAssertedIdentity;
|
|
|
|
if (newAssertedIdentity) {
|
|
|
|
const response = await this.sipNativeLookup(newAssertedIdentity);
|
2021-06-02 19:39:13 +03:00
|
|
|
if (response.length && response[0].fields.lookup_success) {
|
|
|
|
newNativeAssertedIdentity = response[0].userid;
|
|
|
|
}
|
2021-04-19 22:30:51 +03:00
|
|
|
}
|
|
|
|
console.log(`Asserted identity ${newAssertedIdentity} mapped to ${newNativeAssertedIdentity}`);
|
|
|
|
|
|
|
|
if (newNativeAssertedIdentity) {
|
|
|
|
this.assertedIdentityNativeUsers[call.callId] = newNativeAssertedIdentity;
|
|
|
|
|
2021-04-19 23:05:05 +03:00
|
|
|
// If we don't already have a room with this user, make one. This will be slightly odd
|
|
|
|
// if they called us because we'll be inviting them, but there's not much we can do about
|
|
|
|
// this if we want the actual, native room to exist (which we do). This is why it's
|
|
|
|
// important to only obey asserted identity in trusted environments, since anyone you're
|
|
|
|
// on a call with can cause you to send a room invite to someone.
|
2021-04-19 22:30:51 +03:00
|
|
|
await ensureDMExists(MatrixClientPeg.get(), newNativeAssertedIdentity);
|
|
|
|
|
2021-04-23 16:39:39 +03:00
|
|
|
const newMappedRoomId = this.roomIdForCall(call);
|
2021-04-19 22:30:51 +03:00
|
|
|
console.log(`Old room ID: ${mappedRoomId}, new room ID: ${newMappedRoomId}`);
|
|
|
|
if (newMappedRoomId !== mappedRoomId) {
|
|
|
|
this.removeCallForRoom(mappedRoomId);
|
|
|
|
mappedRoomId = newMappedRoomId;
|
|
|
|
this.calls.set(mappedRoomId, call);
|
2021-04-28 12:49:07 +03:00
|
|
|
this.emit(CallHandlerEvent.CallChangeRoom, call);
|
2021-04-19 22:30:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
|
2021-01-26 12:41:57 +03:00
|
|
|
private async logCallStats(call: MatrixCall, mappedRoomId: string) {
|
|
|
|
const stats = await call.getCurrentCallStats();
|
|
|
|
logger.debug(
|
|
|
|
`Call completed. Call ID: ${call.callId}, virtual room ID: ${call.roomId}, ` +
|
|
|
|
`user-facing room ID: ${mappedRoomId}, direction: ${call.direction}, ` +
|
|
|
|
`our Party ID: ${call.ourPartyId}, hangup party: ${call.hangupParty}, ` +
|
|
|
|
`hangup reason: ${call.hangupReason}`,
|
|
|
|
);
|
2021-02-09 16:52:48 +03:00
|
|
|
if (!stats) {
|
|
|
|
logger.debug(
|
|
|
|
"Call statistics are undefined. The call has " +
|
|
|
|
"probably failed before a peerConn was established",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 12:41:57 +03:00
|
|
|
logger.debug("Local candidates:");
|
|
|
|
for (const cand of stats.filter(item => item.type === 'local-candidate')) {
|
2021-01-26 13:52:35 +03:00
|
|
|
const address = cand.address || cand.ip; // firefox uses 'address', chrome uses 'ip'
|
2021-01-26 12:41:57 +03:00
|
|
|
logger.debug(
|
2021-01-26 13:52:35 +03:00
|
|
|
`${cand.id} - type: ${cand.candidateType}, address: ${address}, port: ${cand.port}, ` +
|
2021-01-26 12:41:57 +03:00
|
|
|
`protocol: ${cand.protocol}, relay protocol: ${cand.relayProtocol}, network type: ${cand.networkType}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
logger.debug("Remote candidates:");
|
|
|
|
for (const cand of stats.filter(item => item.type === 'remote-candidate')) {
|
2021-01-26 13:52:35 +03:00
|
|
|
const address = cand.address || cand.ip; // firefox uses 'address', chrome uses 'ip'
|
2021-01-26 12:41:57 +03:00
|
|
|
logger.debug(
|
2021-01-26 13:52:35 +03:00
|
|
|
`${cand.id} - type: ${cand.candidateType}, address: ${address}, port: ${cand.port}, ` +
|
2021-01-26 12:41:57 +03:00
|
|
|
`protocol: ${cand.protocol}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
logger.debug("Candidate pairs:");
|
|
|
|
for (const pair of stats.filter(item => item.type === 'candidate-pair')) {
|
|
|
|
logger.debug(
|
|
|
|
`${pair.localCandidateId} / ${pair.remoteCandidateId} - state: ${pair.state}, ` +
|
|
|
|
`nominated: ${pair.nominated}, ` +
|
|
|
|
`requests sent ${pair.requestsSent}, requests received ${pair.requestsReceived}, ` +
|
|
|
|
`responses received: ${pair.responsesReceived}, responses sent: ${pair.responsesSent}, ` +
|
|
|
|
`bytes received: ${pair.bytesReceived}, bytes sent: ${pair.bytesSent}, `,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:56:07 +03:00
|
|
|
private setCallState(call: MatrixCall, status: CallState) {
|
2021-04-19 22:30:51 +03:00
|
|
|
const mappedRoomId = CallHandler.sharedInstance().roomIdForCall(call);
|
2021-01-21 22:20:35 +03:00
|
|
|
|
2020-09-24 18:16:20 +03:00
|
|
|
console.log(
|
2021-01-21 22:20:35 +03:00
|
|
|
`Call state in ${mappedRoomId} changed to ${status}`,
|
2020-09-24 18:16:20 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'call_state',
|
2021-01-21 22:20:35 +03:00
|
|
|
room_id: mappedRoomId,
|
2020-09-24 18:16:20 +03:00
|
|
|
state: status,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:28:42 +03:00
|
|
|
private removeCallForRoom(roomId: string) {
|
2020-10-09 20:56:07 +03:00
|
|
|
this.calls.delete(roomId);
|
2021-04-28 11:31:49 +03:00
|
|
|
this.emit(CallHandlerEvent.CallsChanged, this.calls);
|
2020-10-01 13:28:42 +03:00
|
|
|
}
|
|
|
|
|
2020-09-24 18:16:20 +03:00
|
|
|
private showICEFallbackPrompt() {
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const code = sub => <code>{sub}</code>;
|
|
|
|
Modal.createTrackedDialog('No TURN servers', '', QuestionDialog, {
|
|
|
|
title: _t("Call failed due to misconfigured server"),
|
|
|
|
description: <div>
|
|
|
|
<p>{_t(
|
|
|
|
"Please ask the administrator of your homeserver " +
|
|
|
|
"(<code>%(homeserverDomain)s</code>) to configure a TURN server in " +
|
|
|
|
"order for calls to work reliably.",
|
|
|
|
{ homeserverDomain: cli.getDomain() }, { code },
|
|
|
|
)}</p>
|
|
|
|
<p>{_t(
|
|
|
|
"Alternatively, you can try to use the public server at " +
|
|
|
|
"<code>turn.matrix.org</code>, but this will not be as reliable, and " +
|
|
|
|
"it will share your IP address with that server. You can also manage " +
|
|
|
|
"this in Settings.",
|
|
|
|
null, { code },
|
|
|
|
)}</p>
|
|
|
|
</div>,
|
|
|
|
button: _t('Try using turn.matrix.org'),
|
|
|
|
cancelButton: _t('OK'),
|
|
|
|
onFinished: (allow) => {
|
|
|
|
SettingsStore.setValue("fallbackICEServerAllowed", null, SettingLevel.DEVICE, allow);
|
|
|
|
cli.setFallbackICEServerAllowed(allow);
|
|
|
|
},
|
|
|
|
}, null, true);
|
|
|
|
}
|
|
|
|
|
2020-11-27 15:53:09 +03:00
|
|
|
private showMediaCaptureError(call: MatrixCall) {
|
|
|
|
let title;
|
|
|
|
let description;
|
|
|
|
|
|
|
|
if (call.type === CallType.Voice) {
|
|
|
|
title = _t("Unable to access microphone");
|
|
|
|
description = <div>
|
|
|
|
{_t(
|
2020-12-06 12:32:52 +03:00
|
|
|
"Call failed because microphone could not be accessed. " +
|
2020-11-27 15:53:09 +03:00
|
|
|
"Check that a microphone is plugged in and set up correctly.",
|
|
|
|
)}
|
|
|
|
</div>;
|
|
|
|
} else if (call.type === CallType.Video) {
|
|
|
|
title = _t("Unable to access webcam / microphone");
|
|
|
|
description = <div>
|
2020-12-06 12:32:52 +03:00
|
|
|
{_t("Call failed because webcam or microphone could not be accessed. Check that:")}
|
2020-11-27 15:53:09 +03:00
|
|
|
<ul>
|
|
|
|
<li>{_t("A microphone and webcam are plugged in and set up correctly")}</li>
|
2020-11-27 17:03:52 +03:00
|
|
|
<li>{_t("Permission is granted to use the webcam")}</li>
|
2020-11-27 15:53:09 +03:00
|
|
|
<li>{_t("No other application is using the webcam")}</li>
|
|
|
|
</ul>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
Modal.createTrackedDialog('Media capture failed', '', ErrorDialog, {
|
|
|
|
title, description,
|
|
|
|
}, null, true);
|
|
|
|
}
|
2020-10-09 20:56:07 +03:00
|
|
|
|
2021-04-03 10:15:55 +03:00
|
|
|
private async placeCall(roomId: string, type: PlaceCallType, transferee: MatrixCall) {
|
2020-10-19 16:56:15 +03:00
|
|
|
Analytics.trackEvent('voip', 'placeCall', 'type', type);
|
2020-10-29 18:53:14 +03:00
|
|
|
CountlyAnalytics.instance.trackStartCall(roomId, type === PlaceCallType.Video, false);
|
2021-01-21 22:20:35 +03:00
|
|
|
|
2021-02-12 23:55:54 +03:00
|
|
|
const mappedRoomId = (await VoipUserMapper.sharedInstance().getOrCreateVirtualRoomForRoom(roomId)) || roomId;
|
2021-01-21 22:20:35 +03:00
|
|
|
logger.debug("Mapped real room " + roomId + " to room ID " + mappedRoomId);
|
|
|
|
|
2021-02-26 17:48:18 +03:00
|
|
|
const timeUntilTurnCresExpire = MatrixClientPeg.get().getTurnServersExpiry() - Date.now();
|
2021-03-08 21:55:33 +03:00
|
|
|
console.log("Current turn creds expire in " + timeUntilTurnCresExpire + " ms");
|
2021-04-23 16:39:39 +03:00
|
|
|
const call = MatrixClientPeg.get().createCall(mappedRoomId);
|
2021-01-21 22:20:35 +03:00
|
|
|
|
2020-10-09 20:56:07 +03:00
|
|
|
this.calls.set(roomId, call);
|
2021-04-27 12:01:36 +03:00
|
|
|
this.emit(CallHandlerEvent.CallsChanged, this.calls);
|
2021-03-25 22:56:21 +03:00
|
|
|
if (transferee) {
|
2021-03-26 17:21:58 +03:00
|
|
|
this.transferees[call.callId] = transferee;
|
2021-03-25 22:56:21 +03:00
|
|
|
}
|
2021-01-21 22:20:35 +03:00
|
|
|
|
2020-10-09 20:56:07 +03:00
|
|
|
this.setCallListeners(call);
|
2020-10-29 20:56:24 +03:00
|
|
|
|
2020-12-03 20:45:49 +03:00
|
|
|
this.setActiveCallRoomId(roomId);
|
|
|
|
|
2020-10-12 13:38:32 +03:00
|
|
|
if (type === PlaceCallType.Voice) {
|
2020-10-09 20:56:07 +03:00
|
|
|
call.placeVoiceCall();
|
|
|
|
} else if (type === 'video') {
|
2021-03-07 10:13:35 +03:00
|
|
|
call.placeVideoCall();
|
2020-10-12 13:38:32 +03:00
|
|
|
} else if (type === PlaceCallType.ScreenSharing) {
|
2020-10-09 20:56:07 +03:00
|
|
|
const screenCapErrorString = PlatformPeg.get().screenCaptureErrorString();
|
|
|
|
if (screenCapErrorString) {
|
|
|
|
this.removeCallForRoom(roomId);
|
|
|
|
console.log("Can't capture screen: " + screenCapErrorString);
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'Unable to capture screen', ErrorDialog, {
|
|
|
|
title: _t('Unable to capture screen'),
|
|
|
|
description: screenCapErrorString,
|
|
|
|
});
|
|
|
|
return;
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
2020-12-26 10:32:51 +03:00
|
|
|
|
2020-12-26 10:40:58 +03:00
|
|
|
call.placeScreenSharingCall(
|
2021-04-01 16:15:21 +03:00
|
|
|
async (): Promise<DesktopCapturerSource> => {
|
2021-01-14 14:44:48 +03:00
|
|
|
const {finished} = Modal.createDialog(DesktopCapturerSourcePicker);
|
2020-12-26 10:40:58 +03:00
|
|
|
const [source] = await finished;
|
|
|
|
return source;
|
2021-04-28 11:52:23 +03:00
|
|
|
},
|
|
|
|
);
|
2020-10-09 20:56:07 +03:00
|
|
|
} else {
|
2021-02-08 17:47:03 +03:00
|
|
|
console.error("Unknown conf call type: " + type);
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
2020-10-09 20:56:07 +03:00
|
|
|
}
|
2020-09-24 18:16:20 +03:00
|
|
|
|
2020-10-09 20:56:07 +03:00
|
|
|
private onAction = (payload: ActionPayload) => {
|
2020-09-24 18:16:20 +03:00
|
|
|
switch (payload.action) {
|
|
|
|
case 'place_call':
|
|
|
|
{
|
2021-01-29 17:26:33 +03:00
|
|
|
// We might be using managed hybrid widgets
|
|
|
|
if (isManagedHybridWidgetEnabled()) {
|
|
|
|
addManagedHybridWidget(payload.room_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:16:20 +03:00
|
|
|
// if the runtime env doesn't do VoIP, whine.
|
|
|
|
if (!MatrixClientPeg.get().supportsVoip()) {
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'VoIP is unsupported', ErrorDialog, {
|
|
|
|
title: _t('VoIP is unsupported'),
|
|
|
|
description: _t('You cannot place VoIP calls in this browser.'),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-03 20:45:49 +03:00
|
|
|
// don't allow > 2 calls to be placed.
|
|
|
|
if (this.getAllActiveCalls().length > 1) {
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'Existing Call', ErrorDialog, {
|
|
|
|
title: _t('Too Many Calls'),
|
|
|
|
description: _t("You've reached the maximum number of simultaneous calls."),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:16:20 +03:00
|
|
|
const room = MatrixClientPeg.get().getRoom(payload.room_id);
|
|
|
|
if (!room) {
|
2021-02-08 17:47:03 +03:00
|
|
|
console.error(`Room ${payload.room_id} does not exist.`);
|
2020-09-24 18:16:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-03 23:23:21 +03:00
|
|
|
if (this.getCallForRoom(room.roomId)) {
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'Existing Call with user', ErrorDialog, {
|
|
|
|
title: _t('Already in call'),
|
|
|
|
description: _t("You're already in a call with this person."),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:16:20 +03:00
|
|
|
const members = room.getJoinedMembers();
|
|
|
|
if (members.length <= 1) {
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'Cannot place call with self', ErrorDialog, {
|
|
|
|
description: _t('You cannot place a call with yourself.'),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (members.length === 2) {
|
2021-02-08 17:47:03 +03:00
|
|
|
console.info(`Place ${payload.type} call in ${payload.room_id}`);
|
2020-10-09 20:56:07 +03:00
|
|
|
|
2021-04-03 10:15:55 +03:00
|
|
|
this.placeCall(payload.room_id, payload.type, payload.transferee);
|
2020-09-24 18:16:20 +03:00
|
|
|
} else { // > 2
|
|
|
|
dis.dispatch({
|
|
|
|
action: "place_conference_call",
|
|
|
|
room_id: payload.room_id,
|
|
|
|
type: payload.type,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'place_conference_call':
|
2021-02-08 17:47:03 +03:00
|
|
|
console.info("Place conference call in " + payload.room_id);
|
2020-10-19 16:56:15 +03:00
|
|
|
Analytics.trackEvent('voip', 'placeConferenceCall');
|
2020-10-29 18:53:14 +03:00
|
|
|
CountlyAnalytics.instance.trackStartCall(payload.room_id, payload.type === PlaceCallType.Video, true);
|
2020-09-24 18:16:20 +03:00
|
|
|
this.startCallApp(payload.room_id, payload.type);
|
|
|
|
break;
|
2020-09-28 22:53:44 +03:00
|
|
|
case 'end_conference':
|
2021-02-08 17:47:03 +03:00
|
|
|
console.info("Terminating conference call in " + payload.room_id);
|
2020-09-28 22:53:44 +03:00
|
|
|
this.terminateCallApp(payload.room_id);
|
|
|
|
break;
|
|
|
|
case 'hangup_conference':
|
2021-02-08 17:47:03 +03:00
|
|
|
console.info("Leaving conference call in "+ payload.room_id);
|
2020-09-28 22:53:44 +03:00
|
|
|
this.hangupCallApp(payload.room_id);
|
|
|
|
break;
|
2020-09-24 18:16:20 +03:00
|
|
|
case 'incoming_call':
|
|
|
|
{
|
|
|
|
// if the runtime env doesn't do VoIP, stop here.
|
|
|
|
if (!MatrixClientPeg.get().supportsVoip()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:56:07 +03:00
|
|
|
const call = payload.call as MatrixCall;
|
2020-12-03 20:45:49 +03:00
|
|
|
|
2021-04-19 22:30:51 +03:00
|
|
|
const mappedRoomId = CallHandler.sharedInstance().roomIdForCall(call);
|
2021-01-21 22:20:35 +03:00
|
|
|
if (this.getCallForRoom(mappedRoomId)) {
|
2021-06-01 21:04:52 +03:00
|
|
|
console.log(
|
|
|
|
"Got incoming call for room " + mappedRoomId +
|
|
|
|
" but there's already a call for this room: ignoring",
|
|
|
|
);
|
2020-12-03 20:45:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:56:15 +03:00
|
|
|
Analytics.trackEvent('voip', 'receiveCall', 'type', call.type);
|
2021-01-21 22:20:35 +03:00
|
|
|
this.calls.set(mappedRoomId, call)
|
2021-04-27 12:01:36 +03:00
|
|
|
this.emit(CallHandlerEvent.CallsChanged, this.calls);
|
2020-09-24 18:16:20 +03:00
|
|
|
this.setCallListeners(call);
|
2021-02-16 17:52:11 +03:00
|
|
|
|
|
|
|
// get ready to send encrypted events in the room, so if the user does answer
|
|
|
|
// the call, we'll be ready to send. NB. This is the protocol-level room ID not
|
|
|
|
// the mapped one: that's where we'll send the events.
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
cli.prepareToEncrypt(cli.getRoom(call.roomId));
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'hangup':
|
2020-10-15 16:54:03 +03:00
|
|
|
case 'reject':
|
2020-09-24 20:18:26 +03:00
|
|
|
if (!this.calls.get(payload.room_id)) {
|
2020-09-24 18:16:20 +03:00
|
|
|
return; // no call to hangup
|
|
|
|
}
|
2020-10-15 16:54:03 +03:00
|
|
|
if (payload.action === 'reject') {
|
|
|
|
this.calls.get(payload.room_id).reject();
|
|
|
|
} else {
|
|
|
|
this.calls.get(payload.room_id).hangup(CallErrorCode.UserHangup, false);
|
|
|
|
}
|
2020-12-04 23:22:01 +03:00
|
|
|
// don't remove the call yet: let the hangup event handler do it (otherwise it will throw
|
|
|
|
// the hangup event away)
|
2020-09-24 18:16:20 +03:00
|
|
|
break;
|
2021-03-12 15:55:14 +03:00
|
|
|
case 'hangup_all':
|
|
|
|
for (const call of this.calls.values()) {
|
|
|
|
call.hangup(CallErrorCode.UserHangup, false);
|
|
|
|
}
|
|
|
|
break;
|
2020-10-29 18:53:14 +03:00
|
|
|
case 'answer': {
|
2020-10-09 20:56:07 +03:00
|
|
|
if (!this.calls.has(payload.room_id)) {
|
2020-09-24 18:16:20 +03:00
|
|
|
return; // no call to answer
|
|
|
|
}
|
2020-12-03 20:45:49 +03:00
|
|
|
|
|
|
|
if (this.getAllActiveCalls().length > 1) {
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'Existing Call', ErrorDialog, {
|
|
|
|
title: _t('Too Many Calls'),
|
|
|
|
description: _t("You've reached the maximum number of simultaneous calls."),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:53:14 +03:00
|
|
|
const call = this.calls.get(payload.room_id);
|
|
|
|
call.answer();
|
2020-12-03 20:45:49 +03:00
|
|
|
this.setActiveCallRoomId(payload.room_id);
|
2020-10-29 18:53:14 +03:00
|
|
|
CountlyAnalytics.instance.trackJoinCall(payload.room_id, call.type === CallType.Video, false);
|
2020-09-24 18:16:20 +03:00
|
|
|
dis.dispatch({
|
|
|
|
action: "view_room",
|
|
|
|
room_id: payload.room_id,
|
|
|
|
});
|
|
|
|
break;
|
2020-10-29 18:53:14 +03:00
|
|
|
}
|
2021-06-02 19:39:13 +03:00
|
|
|
case Action.DialNumber:
|
|
|
|
this.dialNumber(payload.number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async dialNumber(number: string) {
|
|
|
|
const results = await this.pstnLookup(number);
|
|
|
|
if (!results || results.length === 0 || !results[0].userid) {
|
|
|
|
Modal.createTrackedDialog('', '', ErrorDialog, {
|
|
|
|
title: _t("Unable to look up phone number"),
|
|
|
|
description: _t("There was an error looking up the phone number"),
|
|
|
|
});
|
|
|
|
return;
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
2021-06-02 19:39:13 +03:00
|
|
|
const userId = results[0].userid;
|
|
|
|
|
|
|
|
// Now check to see if this is a virtual user, in which case we should find the
|
|
|
|
// native user
|
|
|
|
const nativeLookupResults = await this.sipNativeLookup(userId);
|
|
|
|
const lookupSuccess = nativeLookupResults.length > 0 && nativeLookupResults[0].fields.lookup_success;
|
|
|
|
const nativeUserId = lookupSuccess ? nativeLookupResults[0].userid : userId;
|
|
|
|
console.log("Looked up " + number + " to " + userId + " and mapped to native user " + nativeUserId);
|
|
|
|
|
|
|
|
const roomId = await ensureDMExists(MatrixClientPeg.get(), nativeUserId);
|
|
|
|
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: roomId,
|
|
|
|
});
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
|
2020-12-03 20:45:49 +03:00
|
|
|
setActiveCallRoomId(activeCallRoomId: string) {
|
|
|
|
logger.info("Setting call in room " + activeCallRoomId + " active");
|
|
|
|
|
|
|
|
for (const [roomId, call] of this.calls.entries()) {
|
|
|
|
if (call.state === CallState.Ended) continue;
|
|
|
|
|
|
|
|
if (roomId === activeCallRoomId) {
|
|
|
|
call.setRemoteOnHold(false);
|
|
|
|
} else {
|
|
|
|
logger.info("Holding call in room " + roomId + " because another call is being set active");
|
|
|
|
call.setRemoteOnHold(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 22:35:41 +03:00
|
|
|
/**
|
2020-12-21 14:21:41 +03:00
|
|
|
* @returns true if we are currently in any call where we haven't put the remote party on hold
|
2020-12-18 22:35:41 +03:00
|
|
|
*/
|
|
|
|
hasAnyUnheldCall() {
|
|
|
|
for (const call of this.calls.values()) {
|
|
|
|
if (call.state === CallState.Ended) continue;
|
|
|
|
if (!call.isRemoteOnHold()) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:28:46 +03:00
|
|
|
private async startCallApp(roomId: string, type: string) {
|
2020-09-24 18:16:20 +03:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'appsDrawer',
|
|
|
|
show: true,
|
|
|
|
});
|
|
|
|
|
2020-09-28 22:53:44 +03:00
|
|
|
// prevent double clicking the call button
|
2020-09-24 18:16:20 +03:00
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
|
|
|
const currentJitsiWidgets = WidgetUtils.getRoomWidgetsOfType(room, WidgetType.JITSI);
|
2020-09-28 22:53:44 +03:00
|
|
|
const hasJitsi = currentJitsiWidgets.length > 0
|
|
|
|
|| WidgetEchoStore.roomHasPendingWidgetsOfType(roomId, currentJitsiWidgets, WidgetType.JITSI);
|
|
|
|
if (hasJitsi) {
|
2020-09-24 18:16:20 +03:00
|
|
|
Modal.createTrackedDialog('Call already in progress', '', ErrorDialog, {
|
|
|
|
title: _t('Call in Progress'),
|
|
|
|
description: _t('A call is currently being placed!'),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const jitsiDomain = Jitsi.getInstance().preferredDomain;
|
|
|
|
const jitsiAuth = await Jitsi.getInstance().getJitsiAuth();
|
|
|
|
let confId;
|
|
|
|
if (jitsiAuth === 'openidtoken-jwt') {
|
|
|
|
// Create conference ID from room ID
|
|
|
|
// For compatibility with Jitsi, use base32 without padding.
|
|
|
|
// More details here:
|
|
|
|
// https://github.com/matrix-org/prosody-mod-auth-matrix-user-verification
|
|
|
|
confId = base32.stringify(Buffer.from(roomId), { pad: false });
|
|
|
|
} else {
|
2021-02-22 19:48:12 +03:00
|
|
|
// Create a random conference ID
|
|
|
|
const random = randomUppercaseString(1) + randomLowercaseString(23);
|
|
|
|
confId = 'Jitsi' + random;
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let widgetUrl = WidgetUtils.getLocalJitsiWrapperUrl({auth: jitsiAuth});
|
|
|
|
|
|
|
|
// TODO: Remove URL hacks when the mobile clients eventually support v2 widgets
|
|
|
|
const parsedUrl = new URL(widgetUrl);
|
|
|
|
parsedUrl.search = ''; // set to empty string to make the URL class use searchParams instead
|
|
|
|
parsedUrl.searchParams.set('confId', confId);
|
|
|
|
widgetUrl = parsedUrl.toString();
|
|
|
|
|
|
|
|
const widgetData = {
|
|
|
|
conferenceId: confId,
|
|
|
|
isAudioOnly: type === 'voice',
|
|
|
|
domain: jitsiDomain,
|
|
|
|
auth: jitsiAuth,
|
2021-02-15 18:53:37 +03:00
|
|
|
roomName: room.name,
|
2020-09-24 18:16:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const widgetId = (
|
|
|
|
'jitsi_' +
|
|
|
|
MatrixClientPeg.get().credentials.userId +
|
|
|
|
'_' +
|
|
|
|
Date.now()
|
|
|
|
);
|
|
|
|
|
|
|
|
WidgetUtils.setRoomWidget(roomId, widgetId, WidgetType.JITSI, widgetUrl, 'Jitsi', widgetData).then(() => {
|
|
|
|
console.log('Jitsi widget added');
|
|
|
|
}).catch((e) => {
|
|
|
|
if (e.errcode === 'M_FORBIDDEN') {
|
|
|
|
Modal.createTrackedDialog('Call Failed', '', ErrorDialog, {
|
|
|
|
title: _t('Permission Required'),
|
|
|
|
description: _t("You do not have permission to start a conference call in this room"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
}
|
2020-09-28 22:53:44 +03:00
|
|
|
|
|
|
|
private terminateCallApp(roomId: string) {
|
|
|
|
Modal.createTrackedDialog('Confirm Jitsi Terminate', '', QuestionDialog, {
|
|
|
|
hasCancelButton: true,
|
|
|
|
title: _t("End conference"),
|
2020-09-29 19:20:54 +03:00
|
|
|
description: _t("This will end the conference for everyone. Continue?"),
|
2020-09-28 22:53:44 +03:00
|
|
|
button: _t("End conference"),
|
|
|
|
onFinished: (proceed) => {
|
|
|
|
if (!proceed) return;
|
|
|
|
|
|
|
|
// We'll just obliterate them all. There should only ever be one, but might as well
|
|
|
|
// be safe.
|
|
|
|
const roomInfo = WidgetStore.instance.getRoom(roomId);
|
|
|
|
const jitsiWidgets = roomInfo.widgets.filter(w => WidgetType.JITSI.matches(w.type));
|
|
|
|
jitsiWidgets.forEach(w => {
|
|
|
|
// setting invalid content removes it
|
|
|
|
WidgetUtils.setRoomWidget(roomId, w.id);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private hangupCallApp(roomId: string) {
|
|
|
|
const roomInfo = WidgetStore.instance.getRoom(roomId);
|
|
|
|
if (!roomInfo) return; // "should never happen" clauses go here
|
|
|
|
|
|
|
|
const jitsiWidgets = roomInfo.widgets.filter(w => WidgetType.JITSI.matches(w.type));
|
|
|
|
jitsiWidgets.forEach(w => {
|
2020-10-01 05:09:23 +03:00
|
|
|
const messaging = WidgetMessagingStore.instance.getMessagingForId(w.id);
|
2020-09-28 22:53:44 +03:00
|
|
|
if (!messaging) return; // more "should never happen" words
|
|
|
|
|
2020-10-01 05:09:23 +03:00
|
|
|
messaging.transport.send(ElementWidgetActions.HangupCall, {});
|
2020-09-28 22:53:44 +03:00
|
|
|
});
|
|
|
|
}
|
2020-09-24 18:16:20 +03:00
|
|
|
}
|