2020-09-24 17:16:20 +02: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';
|
2020-10-29 18:56:24 +01:00
|
|
|
import Matrix from 'matrix-js-sdk/src/browser-index';
|
2020-09-24 17:16:20 +02:00
|
|
|
import dis from './dispatcher/dispatcher';
|
|
|
|
import WidgetUtils from './utils/WidgetUtils';
|
|
|
|
import WidgetEchoStore from './stores/WidgetEchoStore';
|
|
|
|
import SettingsStore from './settings/SettingsStore';
|
|
|
|
import {generateHumanReadableId} from "./utils/NamingUtils";
|
|
|
|
import {Jitsi} from "./widgets/Jitsi";
|
|
|
|
import {WidgetType} from "./widgets/WidgetType";
|
|
|
|
import {SettingLevel} from "./settings/SettingLevel";
|
2020-09-24 19:30:30 +02:00
|
|
|
import { ActionPayload } from "./dispatcher/payloads";
|
2020-09-24 17:16:20 +02:00
|
|
|
import {base32} from "rfc4648";
|
|
|
|
|
|
|
|
import QuestionDialog from "./components/views/dialogs/QuestionDialog";
|
|
|
|
import ErrorDialog from "./components/views/dialogs/ErrorDialog";
|
2020-09-28 21:53:44 +02:00
|
|
|
import WidgetStore from "./stores/WidgetStore";
|
2020-10-01 04:09:23 +02:00
|
|
|
import { WidgetMessagingStore } from "./stores/widgets/WidgetMessagingStore";
|
|
|
|
import { ElementWidgetActions } from "./stores/widgets/ElementWidgetActions";
|
2020-10-30 17:49:42 +01:00
|
|
|
import { MatrixCall, CallErrorCode, CallState, CallEvent, CallParty, CallType } from "matrix-js-sdk/src/webrtc/call";
|
2020-10-19 15:56:15 +02:00
|
|
|
import Analytics from './Analytics';
|
2020-10-29 16:53:14 +01:00
|
|
|
import CountlyAnalytics from "./CountlyAnalytics";
|
2020-09-24 17:16:20 +02:00
|
|
|
|
2020-10-12 10:55:21 +02:00
|
|
|
enum AudioID {
|
2020-10-09 19:56:07 +02:00
|
|
|
Ring = 'ringAudio',
|
|
|
|
Ringback = 'ringbackAudio',
|
|
|
|
CallEnd = 'callendAudio',
|
|
|
|
Busy = 'busyAudio',
|
|
|
|
}
|
2020-09-24 19:18:26 +02:00
|
|
|
|
2020-10-12 12:38:32 +02: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',
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:56:24 +01:00
|
|
|
function getRemoteAudioElement(): HTMLAudioElement {
|
|
|
|
// this needs to be somewhere at the top of the DOM which
|
|
|
|
// always exists to avoid audio interruptions.
|
|
|
|
// Might as well just use DOM.
|
|
|
|
const remoteAudioElement = document.getElementById("remoteAudio") as HTMLAudioElement;
|
|
|
|
if (!remoteAudioElement) {
|
|
|
|
console.error("Failed to find remoteAudio element - cannot play audio!"
|
|
|
|
+ "You need to add an <audio/> to the DOM.");
|
|
|
|
}
|
|
|
|
return remoteAudioElement;
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:16:20 +02:00
|
|
|
export default class CallHandler {
|
2020-10-09 19:56:07 +02:00
|
|
|
private calls = new Map<string, MatrixCall>();
|
2020-10-12 10:55:21 +02:00
|
|
|
private audioPromises = new Map<AudioID, Promise<void>>();
|
2020-09-24 17:16:20 +02:00
|
|
|
|
|
|
|
static sharedInstance() {
|
|
|
|
if (!window.mxCallHandler) {
|
|
|
|
window.mxCallHandler = new CallHandler()
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.mxCallHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
dis.register(this.onAction);
|
|
|
|
// 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-10-09 19:56:07 +02:00
|
|
|
getCallForRoom(roomId: string): MatrixCall {
|
2020-09-24 19:18:26 +02:00
|
|
|
return this.calls.get(roomId) || null;
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getAnyActiveCall() {
|
2020-10-01 12:28:42 +02:00
|
|
|
for (const call of this.calls.values()) {
|
2020-10-12 11:25:23 +02:00
|
|
|
if (call.state !== CallState.Ended) {
|
2020-10-01 12:28:42 +02:00
|
|
|
return call;
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-12 10:55:21 +02:00
|
|
|
play(audioId: AudioID) {
|
2020-09-24 17:16:20 +02: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 19:28:46 +02:00
|
|
|
if (this.audioPromises.has(audioId)) {
|
|
|
|
this.audioPromises.set(audioId, this.audioPromises.get(audioId).then(() => {
|
2020-09-24 17:16:20 +02:00
|
|
|
audio.load();
|
|
|
|
return playAudio();
|
2020-09-24 19:28:46 +02:00
|
|
|
}));
|
2020-09-24 17:16:20 +02:00
|
|
|
} else {
|
2020-09-24 19:28:46 +02:00
|
|
|
this.audioPromises.set(audioId, playAudio());
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 10:55:21 +02:00
|
|
|
pause(audioId: AudioID) {
|
2020-09-24 17:16:20 +02:00
|
|
|
// TODO: Attach an invisible element for this instead
|
|
|
|
// which listens?
|
|
|
|
const audio = document.getElementById(audioId) as HTMLMediaElement;
|
|
|
|
if (audio) {
|
2020-09-24 19:28:46 +02:00
|
|
|
if (this.audioPromises.has(audioId)) {
|
|
|
|
this.audioPromises.set(audioId, this.audioPromises.get(audioId).then(() => audio.pause()));
|
2020-09-24 17:16:20 +02:00
|
|
|
} else {
|
2020-09-24 19:28:46 +02:00
|
|
|
// pause doesn't return a promise, so just do it
|
|
|
|
audio.pause();
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 16:08:23 +02: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.
|
|
|
|
const callForThisRoom = this.getCallForRoom(call.roomId);
|
|
|
|
return callForThisRoom && call.callId === callForThisRoom.callId;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:56:07 +02:00
|
|
|
private setCallListeners(call: MatrixCall) {
|
2020-10-12 11:25:23 +02:00
|
|
|
call.on(CallEvent.Error, (err) => {
|
2020-10-13 16:08:23 +02:00
|
|
|
if (!this.matchesCallForThisRoom(call)) return;
|
|
|
|
|
2020-10-19 15:56:15 +02:00
|
|
|
Analytics.trackEvent('voip', 'callError', 'error', err);
|
2020-09-24 17:16:20 +02:00
|
|
|
console.error("Call error:", err);
|
|
|
|
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 11:25:23 +02:00
|
|
|
call.on(CallEvent.Hangup, () => {
|
2020-10-13 16:08:23 +02:00
|
|
|
if (!this.matchesCallForThisRoom(call)) return;
|
|
|
|
|
2020-10-19 15:56:15 +02:00
|
|
|
Analytics.trackEvent('voip', 'callHangup');
|
|
|
|
|
2020-10-01 12:28:42 +02:00
|
|
|
this.removeCallForRoom(call.roomId);
|
2020-09-24 17:16:20 +02:00
|
|
|
});
|
2020-10-12 11:25:23 +02:00
|
|
|
call.on(CallEvent.State, (newState: CallState, oldState: CallState) => {
|
2020-10-13 16:08:23 +02:00
|
|
|
if (!this.matchesCallForThisRoom(call)) return;
|
|
|
|
|
2020-10-09 19:56:07 +02:00
|
|
|
this.setCallState(call, newState);
|
|
|
|
|
|
|
|
switch (oldState) {
|
|
|
|
case CallState.Ringing:
|
2020-10-12 10:55:21 +02:00
|
|
|
this.pause(AudioID.Ring);
|
2020-10-09 19:56:07 +02:00
|
|
|
break;
|
|
|
|
case CallState.InviteSent:
|
2020-10-12 10:55:21 +02:00
|
|
|
this.pause(AudioID.Ringback);
|
2020-10-09 19:56:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (newState) {
|
|
|
|
case CallState.Ringing:
|
2020-10-12 10:55:21 +02:00
|
|
|
this.play(AudioID.Ring);
|
2020-10-09 19:56:07 +02:00
|
|
|
break;
|
|
|
|
case CallState.InviteSent:
|
2020-10-12 10:55:21 +02:00
|
|
|
this.play(AudioID.Ringback);
|
2020-10-09 19:56:07 +02:00
|
|
|
break;
|
|
|
|
case CallState.Ended:
|
2020-10-19 16:04:57 +02:00
|
|
|
Analytics.trackEvent('voip', 'callEnded', 'hangupReason', call.hangupReason);
|
2020-10-09 19:56:07 +02:00
|
|
|
this.removeCallForRoom(call.roomId);
|
|
|
|
if (oldState === CallState.InviteSent && (
|
2020-10-12 11:25:23 +02:00
|
|
|
call.hangupParty === CallParty.Remote ||
|
|
|
|
(call.hangupParty === CallParty.Local && call.hangupReason === CallErrorCode.InviteTimeout)
|
2020-09-24 17:16:20 +02:00
|
|
|
)) {
|
2020-10-12 10:55:21 +02:00
|
|
|
this.play(AudioID.Busy);
|
2020-10-15 15:54:03 +02:00
|
|
|
let title;
|
|
|
|
let description;
|
2020-10-16 21:28:20 +02:00
|
|
|
if (call.hangupReason === CallErrorCode.UserHangup) {
|
2020-10-15 15:54:03 +02:00
|
|
|
title = _t("Call Declined");
|
|
|
|
description = _t("The other party declined the call.");
|
|
|
|
} 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 19:56:07 +02:00
|
|
|
});
|
2020-10-21 12:54:48 +02:00
|
|
|
} else if (call.hangupReason === CallErrorCode.AnsweredElsewhere) {
|
|
|
|
this.play(AudioID.Busy);
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'Call Failed', ErrorDialog, {
|
|
|
|
title: _t("Answered Elsewhere"),
|
|
|
|
description: _t("The call was answered on another device."),
|
|
|
|
});
|
2020-10-09 19:56:07 +02:00
|
|
|
} else {
|
2020-10-12 10:55:21 +02:00
|
|
|
this.play(AudioID.CallEnd);
|
2020-10-09 19:56:07 +02:00
|
|
|
}
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
|
|
|
});
|
2020-10-13 16:08:23 +02: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.calls.set(newCall.roomId, newCall);
|
|
|
|
this.setCallListeners(newCall);
|
|
|
|
this.setCallState(newCall, newCall.state);
|
|
|
|
});
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:56:24 +01:00
|
|
|
private setCallAudioElement(call: MatrixCall) {
|
|
|
|
const audioElement = getRemoteAudioElement();
|
|
|
|
if (audioElement) call.setRemoteAudioElement(audioElement);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:56:07 +02:00
|
|
|
private setCallState(call: MatrixCall, status: CallState) {
|
2020-09-24 17:16:20 +02:00
|
|
|
console.log(
|
2020-10-09 19:56:07 +02:00
|
|
|
`Call state in ${call.roomId} changed to ${status}`,
|
2020-09-24 17:16:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'call_state',
|
2020-10-09 19:56:07 +02:00
|
|
|
room_id: call.roomId,
|
2020-09-24 17:16:20 +02:00
|
|
|
state: status,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-01 12:28:42 +02:00
|
|
|
private removeCallForRoom(roomId: string) {
|
2020-10-09 19:56:07 +02:00
|
|
|
this.calls.delete(roomId);
|
2020-10-01 12:28:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:16:20 +02: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-10-09 19:56:07 +02:00
|
|
|
|
2020-10-12 12:38:32 +02:00
|
|
|
private placeCall(
|
|
|
|
roomId: string, type: PlaceCallType,
|
|
|
|
localElement: HTMLVideoElement, remoteElement: HTMLVideoElement,
|
|
|
|
) {
|
2020-10-19 15:56:15 +02:00
|
|
|
Analytics.trackEvent('voip', 'placeCall', 'type', type);
|
2020-10-29 16:53:14 +01:00
|
|
|
CountlyAnalytics.instance.trackStartCall(roomId, type === PlaceCallType.Video, false);
|
2020-10-09 19:56:07 +02:00
|
|
|
const call = Matrix.createNewMatrixCall(MatrixClientPeg.get(), roomId);
|
|
|
|
this.calls.set(roomId, call);
|
|
|
|
this.setCallListeners(call);
|
2020-10-29 18:56:24 +01:00
|
|
|
this.setCallAudioElement(call);
|
|
|
|
|
2020-10-12 12:38:32 +02:00
|
|
|
if (type === PlaceCallType.Voice) {
|
2020-10-09 19:56:07 +02:00
|
|
|
call.placeVoiceCall();
|
|
|
|
} else if (type === 'video') {
|
|
|
|
call.placeVideoCall(
|
|
|
|
remoteElement,
|
|
|
|
localElement,
|
|
|
|
);
|
2020-10-12 12:38:32 +02:00
|
|
|
} else if (type === PlaceCallType.ScreenSharing) {
|
2020-10-09 19:56:07 +02: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 17:16:20 +02:00
|
|
|
}
|
2020-10-09 19:56:07 +02:00
|
|
|
call.placeScreenSharingCall(remoteElement, localElement);
|
|
|
|
} else {
|
|
|
|
console.error("Unknown conf call type: %s", type);
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
2020-10-09 19:56:07 +02:00
|
|
|
}
|
2020-09-24 17:16:20 +02:00
|
|
|
|
2020-10-09 19:56:07 +02:00
|
|
|
private onAction = (payload: ActionPayload) => {
|
2020-09-24 17:16:20 +02:00
|
|
|
switch (payload.action) {
|
|
|
|
case 'place_call':
|
|
|
|
{
|
|
|
|
if (this.getAnyActiveCall()) {
|
|
|
|
Modal.createTrackedDialog('Call Handler', 'Existing Call', ErrorDialog, {
|
|
|
|
title: _t('Existing Call'),
|
|
|
|
description: _t('You are already in a call.'),
|
|
|
|
});
|
|
|
|
return; // don't allow >1 call to be placed.
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const room = MatrixClientPeg.get().getRoom(payload.room_id);
|
|
|
|
if (!room) {
|
|
|
|
console.error("Room %s does not exist.", payload.room_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
console.info("Place %s call in %s", payload.type, payload.room_id);
|
2020-10-09 19:56:07 +02:00
|
|
|
|
|
|
|
this.placeCall(payload.room_id, payload.type, payload.local_element, payload.remote_element);
|
2020-09-24 17:16:20 +02:00
|
|
|
} else { // > 2
|
|
|
|
dis.dispatch({
|
|
|
|
action: "place_conference_call",
|
|
|
|
room_id: payload.room_id,
|
|
|
|
type: payload.type,
|
|
|
|
remote_element: payload.remote_element,
|
|
|
|
local_element: payload.local_element,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'place_conference_call':
|
|
|
|
console.info("Place conference call in %s", payload.room_id);
|
2020-10-19 15:56:15 +02:00
|
|
|
Analytics.trackEvent('voip', 'placeConferenceCall');
|
2020-10-29 16:53:14 +01:00
|
|
|
CountlyAnalytics.instance.trackStartCall(payload.room_id, payload.type === PlaceCallType.Video, true);
|
2020-09-24 17:16:20 +02:00
|
|
|
this.startCallApp(payload.room_id, payload.type);
|
|
|
|
break;
|
2020-09-28 21:53:44 +02:00
|
|
|
case 'end_conference':
|
|
|
|
console.info("Terminating conference call in %s", payload.room_id);
|
|
|
|
this.terminateCallApp(payload.room_id);
|
|
|
|
break;
|
|
|
|
case 'hangup_conference':
|
|
|
|
console.info("Leaving conference call in %s", payload.room_id);
|
|
|
|
this.hangupCallApp(payload.room_id);
|
|
|
|
break;
|
2020-09-24 17:16:20 +02:00
|
|
|
case 'incoming_call':
|
|
|
|
{
|
|
|
|
if (this.getAnyActiveCall()) {
|
|
|
|
// ignore multiple incoming calls. in future, we may want a line-1/line-2 setup.
|
|
|
|
// we avoid rejecting with "busy" in case the user wants to answer it on a different device.
|
|
|
|
// in future we could signal a "local busy" as a warning to the caller.
|
|
|
|
// see https://github.com/vector-im/vector-web/issues/1964
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the runtime env doesn't do VoIP, stop here.
|
|
|
|
if (!MatrixClientPeg.get().supportsVoip()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:56:07 +02:00
|
|
|
const call = payload.call as MatrixCall;
|
2020-10-19 15:56:15 +02:00
|
|
|
Analytics.trackEvent('voip', 'receiveCall', 'type', call.type);
|
2020-10-09 19:56:07 +02:00
|
|
|
this.calls.set(call.roomId, call)
|
2020-09-24 17:16:20 +02:00
|
|
|
this.setCallListeners(call);
|
2020-10-29 18:56:24 +01:00
|
|
|
this.setCallAudioElement(call);
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'hangup':
|
2020-10-15 15:54:03 +02:00
|
|
|
case 'reject':
|
2020-09-24 19:18:26 +02:00
|
|
|
if (!this.calls.get(payload.room_id)) {
|
2020-09-24 17:16:20 +02:00
|
|
|
return; // no call to hangup
|
|
|
|
}
|
2020-10-15 15:54:03 +02:00
|
|
|
if (payload.action === 'reject') {
|
|
|
|
this.calls.get(payload.room_id).reject();
|
|
|
|
} else {
|
|
|
|
this.calls.get(payload.room_id).hangup(CallErrorCode.UserHangup, false);
|
|
|
|
}
|
2020-10-01 12:28:42 +02:00
|
|
|
this.removeCallForRoom(payload.room_id);
|
2020-09-24 17:16:20 +02:00
|
|
|
break;
|
2020-10-29 16:53:14 +01:00
|
|
|
case 'answer': {
|
2020-10-09 19:56:07 +02:00
|
|
|
if (!this.calls.has(payload.room_id)) {
|
2020-09-24 17:16:20 +02:00
|
|
|
return; // no call to answer
|
|
|
|
}
|
2020-10-29 16:53:14 +01:00
|
|
|
const call = this.calls.get(payload.room_id);
|
|
|
|
call.answer();
|
|
|
|
CountlyAnalytics.instance.trackJoinCall(payload.room_id, call.type === CallType.Video, false);
|
2020-09-24 17:16:20 +02:00
|
|
|
dis.dispatch({
|
|
|
|
action: "view_room",
|
|
|
|
room_id: payload.room_id,
|
|
|
|
});
|
|
|
|
break;
|
2020-10-29 16:53:14 +01:00
|
|
|
}
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 19:28:46 +02:00
|
|
|
private async startCallApp(roomId: string, type: string) {
|
2020-09-24 17:16:20 +02:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'appsDrawer',
|
|
|
|
show: true,
|
|
|
|
});
|
|
|
|
|
2020-09-28 21:53:44 +02:00
|
|
|
// prevent double clicking the call button
|
2020-09-24 17:16:20 +02:00
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
|
|
|
const currentJitsiWidgets = WidgetUtils.getRoomWidgetsOfType(room, WidgetType.JITSI);
|
2020-09-28 21:53:44 +02:00
|
|
|
const hasJitsi = currentJitsiWidgets.length > 0
|
|
|
|
|| WidgetEchoStore.roomHasPendingWidgetsOfType(roomId, currentJitsiWidgets, WidgetType.JITSI);
|
|
|
|
if (hasJitsi) {
|
2020-09-24 17:16:20 +02: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 {
|
|
|
|
// Create a random human readable conference ID
|
|
|
|
confId = `JitsiConference${generateHumanReadableId()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
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 21:53:44 +02:00
|
|
|
|
|
|
|
private terminateCallApp(roomId: string) {
|
|
|
|
Modal.createTrackedDialog('Confirm Jitsi Terminate', '', QuestionDialog, {
|
|
|
|
hasCancelButton: true,
|
|
|
|
title: _t("End conference"),
|
2020-09-29 18:20:54 +02:00
|
|
|
description: _t("This will end the conference for everyone. Continue?"),
|
2020-09-28 21:53:44 +02: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 04:09:23 +02:00
|
|
|
const messaging = WidgetMessagingStore.instance.getMessagingForId(w.id);
|
2020-09-28 21:53:44 +02:00
|
|
|
if (!messaging) return; // more "should never happen" words
|
|
|
|
|
2020-10-01 04:09:23 +02:00
|
|
|
messaging.transport.send(ElementWidgetActions.HangupCall, {});
|
2020-09-28 21:53:44 +02:00
|
|
|
});
|
|
|
|
}
|
2020-09-24 17:16:20 +02:00
|
|
|
}
|