matrix-js-sdk
Version:
Matrix Client-Server SDK for Javascript
1,326 lines (1,135 loc) • 110 kB
text/typescript
/*
Copyright 2015, 2016 OpenMarket Ltd
Copyright 2017 New Vector Ltd
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Copyright 2021 - 2022 Š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.
*/
/**
* This is an internal module. See {@link createNewMatrixCall} for the public API.
*/
import { v4 as uuidv4 } from "uuid";
import { parse as parseSdp, write as writeSdp } from "sdp-transform";
import { logger } from "../logger";
import * as utils from "../utils";
import { IContent, MatrixEvent } from "../models/event";
import { EventType, ToDeviceMessageId } from "../@types/event";
import { RoomMember } from "../models/room-member";
import { randomString } from "../randomstring";
import {
MCallReplacesEvent,
MCallAnswer,
MCallInviteNegotiate,
CallCapabilities,
SDPStreamMetadataPurpose,
SDPStreamMetadata,
SDPStreamMetadataKey,
MCallSDPStreamMetadataChanged,
MCallSelectAnswer,
MCAllAssertedIdentity,
MCallCandidates,
MCallBase,
MCallHangupReject,
} from "./callEventTypes";
import { CallFeed } from "./callFeed";
import { MatrixClient } from "../client";
import { EventEmitterEvents, TypedEventEmitter } from "../models/typed-event-emitter";
import { DeviceInfo } from "../crypto/deviceinfo";
import { GroupCallUnknownDeviceError } from "./groupCall";
import { IScreensharingOpts } from "./mediaHandler";
import { MatrixError } from "../http-api";
interface CallOpts {
// The room ID for this call.
roomId?: string;
invitee?: string;
// The Matrix Client instance to send events to.
client: MatrixClient;
/**
* Whether relay through TURN should be forced.
* @deprecated use opts.forceTURN when creating the matrix client
* since it's only possible to set this option on outbound calls.
*/
forceTURN?: boolean;
// A list of TURN servers.
turnServers?: Array<TurnServer>;
opponentDeviceId?: string;
opponentSessionId?: string;
groupCallId?: string;
}
interface TurnServer {
urls: Array<string>;
username?: string;
password?: string;
ttl?: number;
}
interface AssertedIdentity {
id: string;
displayName: string;
}
enum MediaType {
AUDIO = "audio",
VIDEO = "video",
}
enum CodecName {
OPUS = "opus",
// add more as needed
}
// Used internally to specify modifications to codec parameters in SDP
interface CodecParamsMod {
mediaType: MediaType;
codec: CodecName;
enableDtx?: boolean; // true to enable discontinuous transmission, false to disable, undefined to leave as-is
maxAverageBitrate?: number; // sets the max average bitrate, or undefined to leave as-is
}
export enum CallState {
Fledgling = "fledgling",
InviteSent = "invite_sent",
WaitLocalMedia = "wait_local_media",
CreateOffer = "create_offer",
CreateAnswer = "create_answer",
Connecting = "connecting",
Connected = "connected",
Ringing = "ringing",
Ended = "ended",
}
export enum CallType {
Voice = "voice",
Video = "video",
}
export enum CallDirection {
Inbound = "inbound",
Outbound = "outbound",
}
export enum CallParty {
Local = "local",
Remote = "remote",
}
export enum CallEvent {
Hangup = "hangup",
State = "state",
Error = "error",
Replaced = "replaced",
// The value of isLocalOnHold() has changed
LocalHoldUnhold = "local_hold_unhold",
// The value of isRemoteOnHold() has changed
RemoteHoldUnhold = "remote_hold_unhold",
// backwards compat alias for LocalHoldUnhold: remove in a major version bump
HoldUnhold = "hold_unhold",
// Feeds have changed
FeedsChanged = "feeds_changed",
AssertedIdentityChanged = "asserted_identity_changed",
LengthChanged = "length_changed",
DataChannel = "datachannel",
SendVoipEvent = "send_voip_event",
}
export enum CallErrorCode {
/** The user chose to end the call */
UserHangup = "user_hangup",
/** An error code when the local client failed to create an offer. */
LocalOfferFailed = "local_offer_failed",
/**
* An error code when there is no local mic/camera to use. This may be because
* the hardware isn't plugged in, or the user has explicitly denied access.
*/
NoUserMedia = "no_user_media",
/**
* Error code used when a call event failed to send
* because unknown devices were present in the room
*/
UnknownDevices = "unknown_devices",
/**
* Error code used when we fail to send the invite
* for some reason other than there being unknown devices
*/
SendInvite = "send_invite",
/**
* An answer could not be created
*/
CreateAnswer = "create_answer",
/**
* An offer could not be created
*/
CreateOffer = "create_offer",
/**
* Error code used when we fail to send the answer
* for some reason other than there being unknown devices
*/
SendAnswer = "send_answer",
/**
* The session description from the other side could not be set
*/
SetRemoteDescription = "set_remote_description",
/**
* The session description from this side could not be set
*/
SetLocalDescription = "set_local_description",
/**
* A different device answered the call
*/
AnsweredElsewhere = "answered_elsewhere",
/**
* No media connection could be established to the other party
*/
IceFailed = "ice_failed",
/**
* The invite timed out whilst waiting for an answer
*/
InviteTimeout = "invite_timeout",
/**
* The call was replaced by another call
*/
Replaced = "replaced",
/**
* Signalling for the call could not be sent (other than the initial invite)
*/
SignallingFailed = "signalling_timeout",
/**
* The remote party is busy
*/
UserBusy = "user_busy",
/**
* We transferred the call off to somewhere else
*/
Transfered = "transferred",
/**
* A call from the same user was found with a new session id
*/
NewSession = "new_session",
}
/**
* The version field that we set in m.call.* events
*/
const VOIP_PROTO_VERSION = "1";
/** The fallback ICE server to use for STUN or TURN protocols. */
const FALLBACK_ICE_SERVER = "stun:turn.matrix.org";
/** The length of time a call can be ringing for. */
const CALL_TIMEOUT_MS = 60 * 1000; // ms
/** The time after which we increment callLength */
const CALL_LENGTH_INTERVAL = 1000; // ms
/** The time after which we end the call, if ICE got disconnected */
const ICE_DISCONNECTED_TIMEOUT = 30 * 1000; // ms
export class CallError extends Error {
public readonly code: string;
public constructor(code: CallErrorCode, msg: string, err: Error) {
// Still don't think there's any way to have proper nested errors
super(msg + ": " + err);
this.code = code;
}
}
export function genCallID(): string {
return Date.now().toString() + randomString(16);
}
function getCodecParamMods(isPtt: boolean): CodecParamsMod[] {
const mods = [
{
mediaType: "audio",
codec: "opus",
enableDtx: true,
maxAverageBitrate: isPtt ? 12000 : undefined,
},
] as CodecParamsMod[];
return mods;
}
export type CallEventHandlerMap = {
[CallEvent.DataChannel]: (channel: RTCDataChannel) => void;
[CallEvent.FeedsChanged]: (feeds: CallFeed[]) => void;
[CallEvent.Replaced]: (newCall: MatrixCall) => void;
[CallEvent.Error]: (error: CallError) => void;
[CallEvent.RemoteHoldUnhold]: (onHold: boolean) => void;
[CallEvent.LocalHoldUnhold]: (onHold: boolean) => void;
[CallEvent.LengthChanged]: (length: number) => void;
[CallEvent.State]: (state: CallState, oldState?: CallState) => void;
[CallEvent.Hangup]: (call: MatrixCall) => void;
[CallEvent.AssertedIdentityChanged]: () => void;
/* @deprecated */
[CallEvent.HoldUnhold]: (onHold: boolean) => void;
[CallEvent.SendVoipEvent]: (event: Record<string, any>) => void;
};
// The key of the transceiver map (purpose + media type, separated by ':')
type TransceiverKey = string;
// generates keys for the map of transceivers
// kind is unfortunately a string rather than MediaType as this is the type of
// track.kind
function getTransceiverKey(purpose: SDPStreamMetadataPurpose, kind: TransceiverKey): string {
return purpose + ":" + kind;
}
export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap> {
public roomId?: string;
public callId: string;
public invitee?: string;
public hangupParty?: CallParty;
public hangupReason?: string;
public direction?: CallDirection;
public ourPartyId: string;
public peerConn?: RTCPeerConnection;
public toDeviceSeq = 0;
// whether this call should have push-to-talk semantics
// This should be set by the consumer on incoming & outgoing calls.
public isPtt = false;
private _state = CallState.Fledgling;
private readonly client: MatrixClient;
private readonly forceTURN?: boolean;
private readonly turnServers: Array<TurnServer>;
// A queue for candidates waiting to go out.
// We try to amalgamate candidates into a single candidate message where
// possible
private candidateSendQueue: Array<RTCIceCandidate> = [];
private candidateSendTries = 0;
private candidatesEnded = false;
private feeds: Array<CallFeed> = [];
// our transceivers for each purpose and type of media
private transceivers = new Map<TransceiverKey, RTCRtpTransceiver>();
private inviteOrAnswerSent = false;
private waitForLocalAVStream = false;
private successor?: MatrixCall;
private opponentMember?: RoomMember;
private opponentVersion?: number | string;
// The party ID of the other side: undefined if we haven't chosen a partner
// yet, null if we have but they didn't send a party ID.
private opponentPartyId: string | null | undefined;
private opponentCaps?: CallCapabilities;
private iceDisconnectedTimeout?: ReturnType<typeof setTimeout>;
private inviteTimeout?: ReturnType<typeof setTimeout>;
private readonly removeTrackListeners = new Map<MediaStream, () => void>();
// The logic of when & if a call is on hold is nontrivial and explained in is*OnHold
// This flag represents whether we want the other party to be on hold
private remoteOnHold = false;
// the stats for the call at the point it ended. We can't get these after we
// tear the call down, so we just grab a snapshot before we stop the call.
// The typescript definitions have this type as 'any' :(
private callStatsAtEnd?: any[];
// Perfect negotiation state: https://www.w3.org/TR/webrtc/#perfect-negotiation-example
private makingOffer = false;
private ignoreOffer = false;
private responsePromiseChain?: Promise<void>;
// If candidates arrive before we've picked an opponent (which, in particular,
// will happen if the opponent sends candidates eagerly before the user answers
// the call) we buffer them up here so we can then add the ones from the party we pick
private remoteCandidateBuffer = new Map<string, RTCIceCandidate[]>();
private remoteAssertedIdentity?: AssertedIdentity;
private remoteSDPStreamMetadata?: SDPStreamMetadata;
private callLengthInterval?: ReturnType<typeof setInterval>;
private callStartTime?: number;
private opponentDeviceId?: string;
private opponentDeviceInfo?: DeviceInfo;
private opponentSessionId?: string;
public groupCallId?: string;
/**
* Construct a new Matrix Call.
* @param opts - Config options.
*/
public constructor(opts: CallOpts) {
super();
this.roomId = opts.roomId;
this.invitee = opts.invitee;
this.client = opts.client;
if (!this.client.deviceId) throw new Error("Client must have a device ID to start calls");
this.forceTURN = opts.forceTURN ?? false;
this.ourPartyId = this.client.deviceId;
this.opponentDeviceId = opts.opponentDeviceId;
this.opponentSessionId = opts.opponentSessionId;
this.groupCallId = opts.groupCallId;
// Array of Objects with urls, username, credential keys
this.turnServers = opts.turnServers || [];
if (this.turnServers.length === 0 && this.client.isFallbackICEServerAllowed()) {
this.turnServers.push({
urls: [FALLBACK_ICE_SERVER],
});
}
for (const server of this.turnServers) {
utils.checkObjectHasKeys(server, ["urls"]);
}
this.callId = genCallID();
}
/**
* Place a voice call to this room.
* @throws If you have not specified a listener for 'error' events.
*/
public async placeVoiceCall(): Promise<void> {
await this.placeCall(true, false);
}
/**
* Place a video call to this room.
* @throws If you have not specified a listener for 'error' events.
*/
public async placeVideoCall(): Promise<void> {
await this.placeCall(true, true);
}
/**
* Create a datachannel using this call's peer connection.
* @param label - A human readable label for this datachannel
* @param options - An object providing configuration options for the data channel.
*/
public createDataChannel(label: string, options: RTCDataChannelInit | undefined): RTCDataChannel {
const dataChannel = this.peerConn!.createDataChannel(label, options);
this.emit(CallEvent.DataChannel, dataChannel);
return dataChannel;
}
public getOpponentMember(): RoomMember | undefined {
return this.opponentMember;
}
public getOpponentDeviceId(): string | undefined {
return this.opponentDeviceId;
}
public getOpponentSessionId(): string | undefined {
return this.opponentSessionId;
}
public opponentCanBeTransferred(): boolean {
return Boolean(this.opponentCaps && this.opponentCaps["m.call.transferee"]);
}
public opponentSupportsDTMF(): boolean {
return Boolean(this.opponentCaps && this.opponentCaps["m.call.dtmf"]);
}
public getRemoteAssertedIdentity(): AssertedIdentity | undefined {
return this.remoteAssertedIdentity;
}
public get state(): CallState {
return this._state;
}
private set state(state: CallState) {
const oldState = this._state;
this._state = state;
this.emit(CallEvent.State, state, oldState);
}
public get type(): CallType {
return this.hasLocalUserMediaVideoTrack || this.hasRemoteUserMediaVideoTrack ? CallType.Video : CallType.Voice;
}
public get hasLocalUserMediaVideoTrack(): boolean {
return !!this.localUsermediaStream?.getVideoTracks().length;
}
public get hasRemoteUserMediaVideoTrack(): boolean {
return this.getRemoteFeeds().some((feed) => {
return feed.purpose === SDPStreamMetadataPurpose.Usermedia && feed.stream?.getVideoTracks().length;
});
}
public get hasLocalUserMediaAudioTrack(): boolean {
return !!this.localUsermediaStream?.getAudioTracks().length;
}
public get hasRemoteUserMediaAudioTrack(): boolean {
return this.getRemoteFeeds().some((feed) => {
return feed.purpose === SDPStreamMetadataPurpose.Usermedia && !!feed.stream?.getAudioTracks().length;
});
}
public get localUsermediaFeed(): CallFeed | undefined {
return this.getLocalFeeds().find((feed) => feed.purpose === SDPStreamMetadataPurpose.Usermedia);
}
public get localScreensharingFeed(): CallFeed | undefined {
return this.getLocalFeeds().find((feed) => feed.purpose === SDPStreamMetadataPurpose.Screenshare);
}
public get localUsermediaStream(): MediaStream | undefined {
return this.localUsermediaFeed?.stream;
}
public get localScreensharingStream(): MediaStream | undefined {
return this.localScreensharingFeed?.stream;
}
public get remoteUsermediaFeed(): CallFeed | undefined {
return this.getRemoteFeeds().find((feed) => feed.purpose === SDPStreamMetadataPurpose.Usermedia);
}
public get remoteScreensharingFeed(): CallFeed | undefined {
return this.getRemoteFeeds().find((feed) => feed.purpose === SDPStreamMetadataPurpose.Screenshare);
}
public get remoteUsermediaStream(): MediaStream | undefined {
return this.remoteUsermediaFeed?.stream;
}
public get remoteScreensharingStream(): MediaStream | undefined {
return this.remoteScreensharingFeed?.stream;
}
private getFeedByStreamId(streamId: string): CallFeed | undefined {
return this.getFeeds().find((feed) => feed.stream.id === streamId);
}
/**
* Returns an array of all CallFeeds
* @returns CallFeeds
*/
public getFeeds(): Array<CallFeed> {
return this.feeds;
}
/**
* Returns an array of all local CallFeeds
* @returns local CallFeeds
*/
public getLocalFeeds(): Array<CallFeed> {
return this.feeds.filter((feed) => feed.isLocal());
}
/**
* Returns an array of all remote CallFeeds
* @returns remote CallFeeds
*/
public getRemoteFeeds(): Array<CallFeed> {
return this.feeds.filter((feed) => !feed.isLocal());
}
private async initOpponentCrypto(): Promise<void> {
if (!this.opponentDeviceId) return;
if (!this.client.getUseE2eForGroupCall()) return;
// It's possible to want E2EE and yet not have the means to manage E2EE
// ourselves (for example if the client is a RoomWidgetClient)
if (!this.client.isCryptoEnabled()) {
// All we know is the device ID
this.opponentDeviceInfo = new DeviceInfo(this.opponentDeviceId);
return;
}
// if we've got to this point, we do want to init crypto, so throw if we can't
if (!this.client.crypto) throw new Error("Crypto is not initialised.");
const userId = this.invitee || this.getOpponentMember()?.userId;
if (!userId) throw new Error("Couldn't find opponent user ID to init crypto");
const deviceInfoMap = await this.client.crypto.deviceList.downloadKeys([userId], false);
this.opponentDeviceInfo = deviceInfoMap[userId][this.opponentDeviceId];
if (this.opponentDeviceInfo === undefined) {
throw new GroupCallUnknownDeviceError(userId);
}
}
/**
* Generates and returns localSDPStreamMetadata
* @returns localSDPStreamMetadata
*/
private getLocalSDPStreamMetadata(updateStreamIds = false): SDPStreamMetadata {
const metadata: SDPStreamMetadata = {};
for (const localFeed of this.getLocalFeeds()) {
if (updateStreamIds) {
localFeed.sdpMetadataStreamId = localFeed.stream.id;
}
metadata[localFeed.sdpMetadataStreamId] = {
purpose: localFeed.purpose,
audio_muted: localFeed.isAudioMuted(),
video_muted: localFeed.isVideoMuted(),
};
}
return metadata;
}
/**
* Returns true if there are no incoming feeds,
* otherwise returns false
* @returns no incoming feeds
*/
public noIncomingFeeds(): boolean {
return !this.feeds.some((feed) => !feed.isLocal());
}
private pushRemoteFeed(stream: MediaStream): void {
// Fallback to old behavior if the other side doesn't support SDPStreamMetadata
if (!this.opponentSupportsSDPStreamMetadata()) {
this.pushRemoteFeedWithoutMetadata(stream);
return;
}
const userId = this.getOpponentMember()!.userId;
const purpose = this.remoteSDPStreamMetadata![stream.id].purpose;
const audioMuted = this.remoteSDPStreamMetadata![stream.id].audio_muted;
const videoMuted = this.remoteSDPStreamMetadata![stream.id].video_muted;
if (!purpose) {
logger.warn(
`Call ${this.callId} Ignoring stream with id ${stream.id} because we didn't get any metadata about it`,
);
return;
}
if (this.getFeedByStreamId(stream.id)) {
logger.warn(`Ignoring stream with id ${stream.id} because we already have a feed for it`);
return;
}
this.feeds.push(
new CallFeed({
client: this.client,
call: this,
roomId: this.roomId,
userId,
deviceId: this.getOpponentDeviceId(),
stream,
purpose,
audioMuted,
videoMuted,
}),
);
this.emit(CallEvent.FeedsChanged, this.feeds);
logger.info(
`Call ${this.callId} pushed remote stream (id="${stream.id}", ` +
`active="${stream.active}", purpose=${purpose})`,
);
}
/**
* This method is used ONLY if the other client doesn't support sending SDPStreamMetadata
*/
private pushRemoteFeedWithoutMetadata(stream: MediaStream): void {
const userId = this.getOpponentMember()!.userId;
// We can guess the purpose here since the other client can only send one stream
const purpose = SDPStreamMetadataPurpose.Usermedia;
const oldRemoteStream = this.feeds.find((feed) => !feed.isLocal())?.stream;
// Note that we check by ID and always set the remote stream: Chrome appears
// to make new stream objects when transceiver directionality is changed and the 'active'
// status of streams change - Dave
// If we already have a stream, check this stream has the same id
if (oldRemoteStream && stream.id !== oldRemoteStream.id) {
logger.warn(
`Call ${this.callId} Ignoring new stream ID ${stream.id}: we already have stream ID ${oldRemoteStream.id}`,
);
return;
}
if (this.getFeedByStreamId(stream.id)) {
logger.warn(`Ignoring stream with id ${stream.id} because we already have a feed for it`);
return;
}
this.feeds.push(
new CallFeed({
client: this.client,
call: this,
roomId: this.roomId,
audioMuted: false,
videoMuted: false,
userId,
deviceId: this.getOpponentDeviceId(),
stream,
purpose,
}),
);
this.emit(CallEvent.FeedsChanged, this.feeds);
logger.info(`Call ${this.callId} pushed remote stream (id="${stream.id}", active="${stream.active}")`);
}
private pushNewLocalFeed(stream: MediaStream, purpose: SDPStreamMetadataPurpose, addToPeerConnection = true): void {
const userId = this.client.getUserId()!;
// Tracks don't always start off enabled, eg. chrome will give a disabled
// audio track if you ask for user media audio and already had one that
// you'd set to disabled (presumably because it clones them internally).
setTracksEnabled(stream.getAudioTracks(), true);
setTracksEnabled(stream.getVideoTracks(), true);
if (this.getFeedByStreamId(stream.id)) {
logger.warn(`Ignoring stream with id ${stream.id} because we already have a feed for it`);
return;
}
this.pushLocalFeed(
new CallFeed({
client: this.client,
roomId: this.roomId,
audioMuted: false,
videoMuted: false,
userId,
deviceId: this.getOpponentDeviceId(),
stream,
purpose,
}),
addToPeerConnection,
);
}
/**
* Pushes supplied feed to the call
* @param callFeed - to push
* @param addToPeerConnection - whether to add the tracks to the peer connection
*/
public pushLocalFeed(callFeed: CallFeed, addToPeerConnection = true): void {
if (this.feeds.some((feed) => callFeed.stream.id === feed.stream.id)) {
logger.info(`Ignoring duplicate local stream ${callFeed.stream.id} in call ${this.callId}`);
return;
}
this.feeds.push(callFeed);
if (addToPeerConnection) {
for (const track of callFeed.stream.getTracks()) {
logger.info(
`Call ${this.callId} ` +
`Adding track (` +
`id="${track.id}", ` +
`kind="${track.kind}", ` +
`streamId="${callFeed.stream.id}", ` +
`streamPurpose="${callFeed.purpose}", ` +
`enabled=${track.enabled}` +
`) to peer connection`,
);
const tKey = getTransceiverKey(callFeed.purpose, track.kind);
if (this.transceivers.has(tKey)) {
// we already have a sender, so we re-use it. We try to re-use transceivers as much
// as possible because they can't be removed once added, so otherwise they just
// accumulate which makes the SDP very large very quickly: in fact it only takes
// about 6 video tracks to exceed the maximum size of an Olm-encrypted
// Matrix event.
const transceiver = this.transceivers.get(tKey)!;
// this is what would allow us to use addTransceiver(), but it's not available
// on Firefox yet. We call it anyway if we have it.
if (transceiver.sender.setStreams) transceiver.sender.setStreams(callFeed.stream);
transceiver.sender.replaceTrack(track);
// set the direction to indicate we're going to start sending again
// (this will trigger the re-negotiation)
transceiver.direction = transceiver.direction === "inactive" ? "sendonly" : "sendrecv";
} else {
// create a new one. We need to use addTrack rather addTransceiver for this because firefox
// doesn't yet implement RTCRTPSender.setStreams()
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1510802) so we'd have no way to group the
// two tracks together into a stream.
const newSender = this.peerConn!.addTrack(track, callFeed.stream);
// now go & fish for the new transceiver
const newTransciever = this.peerConn!.getTransceivers().find((t) => t.sender === newSender);
if (newTransciever) {
this.transceivers.set(tKey, newTransciever);
} else {
logger.warn("Didn't find a matching transceiver after adding track!");
}
}
}
}
logger.info(
`Call ${this.callId} ` +
`Pushed local stream ` +
`(id="${callFeed.stream.id}", ` +
`active="${callFeed.stream.active}", ` +
`purpose="${callFeed.purpose}")`,
);
this.emit(CallEvent.FeedsChanged, this.feeds);
}
/**
* Removes local call feed from the call and its tracks from the peer
* connection
* @param callFeed - to remove
*/
public removeLocalFeed(callFeed: CallFeed): void {
const audioTransceiverKey = getTransceiverKey(callFeed.purpose, "audio");
const videoTransceiverKey = getTransceiverKey(callFeed.purpose, "video");
for (const transceiverKey of [audioTransceiverKey, videoTransceiverKey]) {
// this is slightly mixing the track and transceiver API but is basically just shorthand.
// There is no way to actually remove a transceiver, so this just sets it to inactive
// (or recvonly) and replaces the source with nothing.
if (this.transceivers.has(transceiverKey)) {
const transceiver = this.transceivers.get(transceiverKey)!;
if (transceiver.sender) this.peerConn!.removeTrack(transceiver.sender);
}
}
if (callFeed.purpose === SDPStreamMetadataPurpose.Screenshare) {
this.client.getMediaHandler().stopScreensharingStream(callFeed.stream);
}
this.deleteFeed(callFeed);
}
private deleteAllFeeds(): void {
for (const feed of this.feeds) {
if (!feed.isLocal() || !this.groupCallId) {
feed.dispose();
}
}
this.feeds = [];
this.emit(CallEvent.FeedsChanged, this.feeds);
}
private deleteFeedByStream(stream: MediaStream): void {
const feed = this.getFeedByStreamId(stream.id);
if (!feed) {
logger.warn(`Call ${this.callId} Didn't find the feed with stream id ${stream.id} to delete`);
return;
}
this.deleteFeed(feed);
}
private deleteFeed(feed: CallFeed): void {
feed.dispose();
this.feeds.splice(this.feeds.indexOf(feed), 1);
this.emit(CallEvent.FeedsChanged, this.feeds);
}
// The typescript definitions have this type as 'any' :(
public async getCurrentCallStats(): Promise<any[] | undefined> {
if (this.callHasEnded()) {
return this.callStatsAtEnd;
}
return this.collectCallStats();
}
private async collectCallStats(): Promise<any[] | undefined> {
// This happens when the call fails before it starts.
// For example when we fail to get capture sources
if (!this.peerConn) return;
const statsReport = await this.peerConn.getStats();
const stats: any[] = [];
statsReport.forEach((item) => {
stats.push(item);
});
return stats;
}
/**
* Configure this call from an invite event. Used by MatrixClient.
* @param event - The m.call.invite event
*/
public async initWithInvite(event: MatrixEvent): Promise<void> {
const invite = event.getContent<MCallInviteNegotiate>();
this.direction = CallDirection.Inbound;
// make sure we have valid turn creds. Unless something's gone wrong, it should
// poll and keep the credentials valid so this should be instant.
const haveTurnCreds = await this.client.checkTurnServers();
if (!haveTurnCreds) {
logger.warn(`Call ${this.callId} Failed to get TURN credentials! Proceeding with call anyway...`);
}
const sdpStreamMetadata = invite[SDPStreamMetadataKey];
if (sdpStreamMetadata) {
this.updateRemoteSDPStreamMetadata(sdpStreamMetadata);
} else {
logger.debug(
`Call ${this.callId} did not get any SDPStreamMetadata! Can not send/receive multiple streams`,
);
}
this.peerConn = this.createPeerConnection();
// we must set the party ID before await-ing on anything: the call event
// handler will start giving us more call events (eg. candidates) so if
// we haven't set the party ID, we'll ignore them.
this.chooseOpponent(event);
await this.initOpponentCrypto();
try {
await this.peerConn.setRemoteDescription(invite.offer);
await this.addBufferedIceCandidates();
} catch (e) {
logger.debug(`Call ${this.callId} failed to set remote description`, e);
this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, false);
return;
}
const remoteStream = this.feeds.find((feed) => !feed.isLocal())?.stream;
// According to previous comments in this file, firefox at some point did not
// add streams until media started arriving on them. Testing latest firefox
// (81 at time of writing), this is no longer a problem, so let's do it the correct way.
if (!remoteStream || remoteStream.getTracks().length === 0) {
logger.error(`Call ${this.callId} no remote stream or no tracks after setting remote description!`);
this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, false);
return;
}
this.state = CallState.Ringing;
if (event.getLocalAge()) {
// Time out the call if it's ringing for too long
const ringingTimer = setTimeout(() => {
if (this.state == CallState.Ringing) {
logger.debug(`Call ${this.callId} invite has expired. Hanging up.`);
this.hangupParty = CallParty.Remote; // effectively
this.state = CallState.Ended;
this.stopAllMedia();
if (this.peerConn!.signalingState != "closed") {
this.peerConn!.close();
}
this.emit(CallEvent.Hangup, this);
}
}, invite.lifetime - event.getLocalAge());
const onState = (state: CallState): void => {
if (state !== CallState.Ringing) {
clearTimeout(ringingTimer);
this.off(CallEvent.State, onState);
}
};
this.on(CallEvent.State, onState);
}
}
/**
* Configure this call from a hangup or reject event. Used by MatrixClient.
* @param event - The m.call.hangup event
*/
public initWithHangup(event: MatrixEvent): void {
// perverse as it may seem, sometimes we want to instantiate a call with a
// hangup message (because when getting the state of the room on load, events
// come in reverse order and we want to remember that a call has been hung up)
this.state = CallState.Ended;
}
private shouldAnswerWithMediaType(
wantedValue: boolean | undefined,
valueOfTheOtherSide: boolean,
type: "audio" | "video",
): boolean {
if (wantedValue && !valueOfTheOtherSide) {
// TODO: Figure out how to do this
logger.warn(
`Call ${this.callId} Unable to answer with ${type} because the other side isn't sending it either.`,
);
return false;
} else if (
!utils.isNullOrUndefined(wantedValue) &&
wantedValue !== valueOfTheOtherSide &&
!this.opponentSupportsSDPStreamMetadata()
) {
logger.warn(
`Call ${this.callId} Unable to answer with ${type}=${wantedValue} because the other side doesn't support it. Answering with ${type}=${valueOfTheOtherSide}.`,
);
return valueOfTheOtherSide!;
}
return wantedValue ?? valueOfTheOtherSide!;
}
/**
* Answer a call.
*/
public async answer(audio?: boolean, video?: boolean): Promise<void> {
if (this.inviteOrAnswerSent) return;
// TODO: Figure out how to do this
if (audio === false && video === false) throw new Error("You CANNOT answer a call without media");
if (!this.localUsermediaStream && !this.waitForLocalAVStream) {
const prevState = this.state;
const answerWithAudio = this.shouldAnswerWithMediaType(audio, this.hasRemoteUserMediaAudioTrack, "audio");
const answerWithVideo = this.shouldAnswerWithMediaType(video, this.hasRemoteUserMediaVideoTrack, "video");
this.state = CallState.WaitLocalMedia;
this.waitForLocalAVStream = true;
try {
const stream = await this.client.getMediaHandler().getUserMediaStream(answerWithAudio, answerWithVideo);
this.waitForLocalAVStream = false;
const usermediaFeed = new CallFeed({
client: this.client,
roomId: this.roomId,
userId: this.client.getUserId()!,
deviceId: this.client.getDeviceId() ?? undefined,
stream,
purpose: SDPStreamMetadataPurpose.Usermedia,
audioMuted: false,
videoMuted: false,
});
const feeds = [usermediaFeed];
if (this.localScreensharingFeed) {
feeds.push(this.localScreensharingFeed);
}
this.answerWithCallFeeds(feeds);
} catch (e) {
if (answerWithVideo) {
// Try to answer without video
logger.warn(`Call ${this.callId} Failed to getUserMedia(), trying to getUserMedia() without video`);
this.state = prevState;
this.waitForLocalAVStream = false;
await this.answer(answerWithAudio, false);
} else {
this.getUserMediaFailed(<Error>e);
return;
}
}
} else if (this.waitForLocalAVStream) {
this.state = CallState.WaitLocalMedia;
}
}
public answerWithCallFeeds(callFeeds: CallFeed[]): void {
if (this.inviteOrAnswerSent) return;
this.queueGotCallFeedsForAnswer(callFeeds);
}
/**
* Replace this call with a new call, e.g. for glare resolution. Used by
* MatrixClient.
* @param newCall - The new call.
*/
public replacedBy(newCall: MatrixCall): void {
logger.debug(`Call ${this.callId} replaced by ${newCall.callId}`);
if (this.state === CallState.WaitLocalMedia) {
logger.debug(`Call ${this.callId} telling new call ${newCall.callId} to wait for local media`);
newCall.waitForLocalAVStream = true;
} else if ([CallState.CreateOffer, CallState.InviteSent].includes(this.state)) {
if (newCall.direction === CallDirection.Outbound) {
newCall.queueGotCallFeedsForAnswer([]);
} else {
logger.debug(`Call ${this.callId} handing local stream to new call ${newCall.callId}`);
newCall.queueGotCallFeedsForAnswer(this.getLocalFeeds().map((feed) => feed.clone()));
}
}
this.successor = newCall;
this.emit(CallEvent.Replaced, newCall);
this.hangup(CallErrorCode.Replaced, true);
}
/**
* Hangup a call.
* @param reason - The reason why the call is being hung up.
* @param suppressEvent - True to suppress emitting an event.
*/
public hangup(reason: CallErrorCode, suppressEvent: boolean): void {
if (this.callHasEnded()) return;
logger.debug(`Ending call ${this.callId} with reason ${reason}`);
this.terminate(CallParty.Local, reason, !suppressEvent);
// We don't want to send hangup here if we didn't even get to sending an invite
if ([CallState.Fledgling, CallState.WaitLocalMedia].includes(this.state)) return;
const content: IContent = {};
// Don't send UserHangup reason to older clients
if ((this.opponentVersion && this.opponentVersion !== 0) || reason !== CallErrorCode.UserHangup) {
content["reason"] = reason;
}
this.sendVoipEvent(EventType.CallHangup, content);
}
/**
* Reject a call
* This used to be done by calling hangup, but is a separate method and protocol
* event as of MSC2746.
*/
public reject(): void {
if (this.state !== CallState.Ringing) {
throw Error("Call must be in 'ringing' state to reject!");
}
if (this.opponentVersion === 0) {
logger.info(
`Call ${this.callId} Opponent version is less than 1 (${this.opponentVersion}): sending hangup instead of reject`,
);
this.hangup(CallErrorCode.UserHangup, true);
return;
}
logger.debug("Rejecting call: " + this.callId);
this.terminate(CallParty.Local, CallErrorCode.UserHangup, true);
this.sendVoipEvent(EventType.CallReject, {});
}
/**
* Adds an audio and/or video track - upgrades the call
* @param audio - should add an audio track
* @param video - should add an video track
*/
private async upgradeCall(audio: boolean, video: boolean): Promise<void> {
// We don't do call downgrades
if (!audio && !video) return;
if (!this.opponentSupportsSDPStreamMetadata()) return;
try {
logger.debug(`Upgrading call ${this.callId}: audio?=${audio} video?=${video}`);
const getAudio = audio || this.hasLocalUserMediaAudioTrack;
const getVideo = video || this.hasLocalUserMediaVideoTrack;
// updateLocalUsermediaStream() will take the tracks, use them as
// replacement and throw the stream away, so it isn't reusable
const stream = await this.client.getMediaHandler().getUserMediaStream(getAudio, getVideo, false);
await this.updateLocalUsermediaStream(stream, audio, video);
} catch (error) {
logger.error(`Call ${this.callId} Failed to upgrade the call`, error);
this.emit(
CallEvent.Error,
new CallError(CallErrorCode.NoUserMedia, "Failed to get camera access: ", <Error>error),
);
}
}
/**
* Returns true if this.remoteSDPStreamMetadata is defined, otherwise returns false
* @returns can screenshare
*/
public opponentSupportsSDPStreamMetadata(): boolean {
return Boolean(this.remoteSDPStreamMetadata);
}
/**
* If there is a screensharing stream returns true, otherwise returns false
* @returns is screensharing
*/
public isScreensharing(): boolean {
return Boolean(this.localScreensharingStream);
}
/**
* Starts/stops screensharing
* @param enabled - the desired screensharing state
* @param desktopCapturerSourceId - optional id of the desktop capturer source to use
* @returns new screensharing state
*/
public async setScreensharingEnabled(enabled: boolean, opts?: IScreensharingOpts): Promise<boolean> {
// Skip if there is nothing to do
if (enabled && this.isScreensharing()) {
logger.warn(`Call ${this.callId} There is already a screensharing stream - there is nothing to do!`);
return true;
} else if (!enabled && !this.isScreensharing()) {
logger.warn(`Call ${this.callId} There already isn't a screensharing stream - there is nothing to do!`);
return false;
}
// Fallback to replaceTrack()
if (!this.opponentSupportsSDPStreamMetadata()) {
return this.setScreensharingEnabledWithoutMetadataSupport(enabled, opts);
}
logger.debug(`Call ${this.callId} set screensharing enabled? ${enabled}`);
if (enabled) {
try {
const stream = await this.client.getMediaHandler().getScreensharingStream(opts);
if (!stream) return false;
this.pushNewLocalFeed(stream, SDPStreamMetadataPurpose.Screenshare);
return true;
} catch (err) {
logger.error(`Call ${this.callId} Failed to get screen-sharing stream:`, err);
return false;
}
} else {
const audioTransceiver = this.transceivers.get(
getTransceiverKey(SDPStreamMetadataPurpose.Screenshare, "audio"),
);
const videoTransceiver = this.transceivers.get(
getTransceiverKey(SDPStreamMetadataPurpose.Screenshare, "video"),
);
for (const transceiver of [audioTransceiver, videoTransceiver]) {
// this is slightly mixing the track and transceiver API but is basically just shorthand
// for removing the sender.
if (transceiver && transceiver.sender) this.peerConn!.removeTrack(transceiver.sender);
}
this.client.getMediaHandler().stopScreensharingStream(this.localScreensharingStream!);
this.deleteFeedByStream(this.localScreensharingStream!);
return false;
}
}
/**
* Starts/stops screensharing
* Should be used ONLY if the opponent doesn't support SDPStreamMetadata
* @param enabled - the desired screensharing state
* @param desktopCapturerSourceId - optional id of the desktop capturer source to use
* @returns new screensharing state
*/
private async setScreensharingEnabledWithoutMetadataSupport(
enabled: boolean,
opts?: IScreensharingOpts,
): Promise<boolean> {
logger.debug(`Call ${this.callId} Set screensharing enabled? ${enabled} using replaceTrack()`);
if (enabled) {
try {
const stream = await this.client.getMediaHandler().getScreensharingStream(opts);
if (!stream) return false;
const track = stream.getTracks().find((track) => track.kind === "video");
const sender = this.transceivers.get(
getTransceiverKey(SDPStreamMetadataPurpose.Usermedia, "video"),
)?.sender;
sender?.replaceTrack(track ?? null);
this.pushNewLocalFeed(stream, SDPStreamMetadataPurpose.Screenshare, false);
return true;
} catch (err) {
logger.error(`Call ${this.callId} Failed to get screen-sharing stream:`, err);
return false;
}
} else {
const track = this.localUsermediaStream?.getTracks().find((track) => track.kind === "video");
const sender = this.transceivers.get(
getTransceiverKey(SDPStreamMetadataPurpose.Usermedia, "video"),
)?.sender;
sender?.replaceTrack(track ?? null);
this.client.getMediaHandler().stopScreensharingStream(this.localScreensharingStream!);
this.deleteFeedByStream(this.localScreensharingStream!);
return false;
}
}
/**
* Replaces/adds the tracks from the passed stream to the localUsermediaStream
* @param stream - to use a replacement for the local usermedia stream
*/
public async updateLocalUsermediaStream(
stream: MediaStream,
forceAudio = false,
forceVideo = false,
): Promise<void> {
const callFeed = this.localUsermediaFeed!;
const audioEnabled = forceAudio || (!callFeed.isAudioMuted() && !this.remoteOnHold);
const videoEnabled = forceVideo || (!callFeed.isVideoMuted() && !this.remoteOnHold);
logger.log(
`call ${this.callId} updateLocalUsermediaStream stream ${stream.id} audioEnabled ${audioEnabled} videoEnabled ${videoEnabled}`,
);
setTracksEnabled(stream.getAudioTracks(), audioEnabled);
setTracksEnabled(stream.getVideoTracks(), videoEnabled);
// We want to keep the same stream id, so we replace the tracks rather
// than the whole stream.
// Firstly, we replace the tracks in our localUsermediaStream.
for (const track of this.localUsermediaStream!.getTracks()) {
this.localUsermediaStream!.removeTrack(track);
track.stop();
}
for (const track of stream.getTracks()) {
this.localUsermediaStream!.addTrack(track);
}
// Secondly, we remove tracks that we no longer need from the peer
// connection, if any. This only happens when we mute the video atm.
// This will change the transceiver direction to "inactive" and
// therefore cause re-negotiation.
for (const kind of ["audio", "video"]) {
const sender = this.transceivers.get(getTransceiverKey(SDPStreamMetadataPurpose.Usermedia, kind))?.sender;
// Only remove the track if we aren't going to immediately replace it
if (sender && !stream.getTracks().find((t) => t.kind === kind)) {
this.peerConn?.removeTrack(sender);
}
}
// Thirdly, we replace the old tracks, if possible.
for (const track of stream.getTracks()) {
const tKey = getTransceiverKey(SDPStreamMetadataPurpose.Usermedia, track.kind);
const transceiver = this.transceivers.get(tKey);
const oldSender = transceiver?.sender;
let added = false;
if (oldSender) {
try {
logger.info(
`Call ${this.callId} ` +
`Replacing track (` +
`id="${track.id}", ` +
`kind="${track.kind}", ` +
`streamId="${stream.id}", ` +
`streamPurpose="${callFeed.purpose}"` +
`) to peer connection`,
);
await oldSender.replaceTrack(track);
// Set the direction to indicate we're going to be sending.
// This is only necessary in the cases where we'