vector-im-hydrogen-web/src/matrix/calls/PeerCall.ts

965 lines
37 KiB
TypeScript
Raw Normal View History

2022-02-17 16:58:44 +01:00
/*
Copyright 2022 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.
*/
import {ObservableMap} from "../../observable/map/ObservableMap";
2022-02-25 16:54:00 +01:00
import {recursivelyAssign} from "../../utils/recursivelyAssign";
2022-02-18 16:38:10 +01:00
import {AsyncQueue} from "../../utils/AsyncQueue";
2022-03-09 11:29:39 +01:00
import {Disposables, IDisposable} from "../../utils/Disposables";
2022-02-17 16:58:44 +01:00
import type {Room} from "../room/Room";
import type {StateEvent} from "../storage/types";
import type {ILogItem} from "../../logging/types";
2022-03-01 17:01:47 +01:00
import type {TimeoutCreator, Timeout} from "../../platform/types/types";
2022-03-09 11:29:39 +01:00
import {WebRTC, PeerConnection, PeerConnectionHandler, DataChannel} from "../../platform/types/WebRTC";
2022-02-17 16:58:44 +01:00
import {MediaDevices, Track, AudioTrack, TrackType} from "../../platform/types/MediaDevices";
2022-02-25 16:54:00 +01:00
import type {LocalMedia} from "./LocalMedia";
2022-03-09 11:29:39 +01:00
import {
SDPStreamMetadataKey,
2022-03-09 18:53:51 +01:00
SDPStreamMetadataPurpose,
EventType,
2022-03-09 11:29:39 +01:00
} from "./callEventTypes";
import type {
MCallBase,
MCallInvite,
MCallAnswer,
MCallSDPStreamMetadataChanged,
MCallCandidates,
MCallHangupReject,
SDPStreamMetadata,
2022-03-09 18:53:51 +01:00
SignallingMessage
2022-03-09 11:29:39 +01:00
} from "./callEventTypes";
2022-03-10 14:53:31 +01:00
export type Options = {
webRTC: WebRTC,
createTimeout: TimeoutCreator,
emitUpdate: (peerCall: PeerCall, params: any) => void;
sendSignallingMessage: (message: SignallingMessage<MCallBase>, log: ILogItem) => Promise<void>;
};
2022-02-25 16:54:00 +01:00
// when sending, we need to encrypt message with olm. I think the flow of room => roomEncryption => olmEncryption as we already
// do for sharing keys will be best as that already deals with room tracking.
/**
* Does WebRTC signalling for a single PeerConnection, and deals with WebRTC wrappers from platform
* */
/** Implements a call between two peers with the signalling state keeping, while still delegating the signalling message sending. Used by GroupCall.*/
2022-03-09 11:29:39 +01:00
export class PeerCall implements IDisposable {
2022-02-25 16:54:00 +01:00
private readonly peerConnection: PeerConnection;
2022-03-10 14:53:31 +01:00
private _state = CallState.Fledgling;
2022-02-25 16:54:00 +01:00
private direction: CallDirection;
2022-03-09 11:29:39 +01:00
private localMedia?: LocalMedia;
private seq: number = 0;
2022-02-25 16:54:00 +01:00
// A queue for candidates waiting to go out.
// We try to amalgamate candidates into a single candidate message where
// possible
private candidateSendQueue: Array<RTCIceCandidate> = [];
// 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
2022-03-25 14:43:02 +01:00
private remoteCandidateBuffer? = new Map<PartyId, RTCIceCandidate[]>();
2022-02-25 16:54:00 +01:00
private remoteSDPStreamMetadata?: SDPStreamMetadata;
private responsePromiseChain?: Promise<void>;
private opponentPartyId?: PartyId;
2022-03-01 15:36:31 +01:00
private hangupParty: CallParty;
private disposables = new Disposables();
private statePromiseMap = new Map<CallState, {resolve: () => void, promise: Promise<void>}>();
2022-02-25 16:54:00 +01:00
2022-03-09 11:29:39 +01:00
// perfect negotiation flags
private makingOffer: boolean = false;
private ignoreOffer: boolean = false;
2022-03-16 19:10:51 +01:00
private sentEndOfCandidates: boolean = false;
private iceDisconnectedTimeout?: Timeout;
2022-02-25 16:54:00 +01:00
constructor(
2022-03-16 19:10:51 +01:00
private callId: string,
2022-03-25 14:43:02 +01:00
private readonly options: Options,
private readonly logItem: ILogItem,
2022-02-25 16:54:00 +01:00
) {
const outer = this;
2022-03-10 14:53:31 +01:00
this.peerConnection = options.webRTC.createPeerConnection({
2022-03-16 19:10:51 +01:00
onIceConnectionStateChange(state: RTCIceConnectionState) {
2022-03-25 14:43:02 +01:00
outer.logItem.wrap({l: "onIceConnectionStateChange", status: state}, log => {
outer.onIceConnectionStateChange(state, log);
});
2022-03-16 19:10:51 +01:00
},
onLocalIceCandidate(candidate: RTCIceCandidate) {
2022-03-25 14:43:02 +01:00
outer.logItem.wrap("onLocalIceCandidate", log => {
outer.handleLocalIceCandidate(candidate, log);
});
2022-03-16 19:10:51 +01:00
},
onIceGatheringStateChange(state: RTCIceGatheringState) {
2022-03-25 14:43:02 +01:00
outer.logItem.wrap({l: "onIceGatheringStateChange", status: state}, log => {
outer.handleIceGatheringState(state, log);
});
2022-03-16 19:10:51 +01:00
},
onRemoteTracksChanged(tracks: Track[]) {
2022-03-25 14:43:02 +01:00
outer.logItem.wrap("onRemoteTracksChanged", log => {
outer.options.emitUpdate(outer, undefined);
});
2022-03-16 19:10:51 +01:00
},
2022-02-25 16:54:00 +01:00
onDataChannelChanged(dataChannel: DataChannel | undefined) {},
onNegotiationNeeded() {
2022-03-25 16:53:12 +01:00
const promiseCreator = () => {
return outer.logItem.wrap("onNegotiationNeeded", log => {
return outer.handleNegotiation(log);
});
2022-03-25 14:43:02 +01:00
};
2022-02-25 16:54:00 +01:00
outer.responsePromiseChain = outer.responsePromiseChain?.then(promiseCreator) ?? promiseCreator();
},
getPurposeForStreamId(streamId: string): SDPStreamMetadataPurpose {
return outer.remoteSDPStreamMetadata?.[streamId]?.purpose ?? SDPStreamMetadataPurpose.Usermedia;
}
});
}
2022-03-10 14:53:31 +01:00
get state(): CallState { return this._state; }
2022-03-09 11:29:39 +01:00
get remoteTracks(): Track[] {
return this.peerConnection.remoteTracks;
2022-02-25 16:54:00 +01:00
}
2022-03-25 14:43:02 +01:00
call(localMedia: LocalMedia): Promise<void> {
return this.logItem.wrap("call", async log => {
if (this._state !== CallState.Fledgling) {
return;
}
this.localMedia = localMedia;
this.direction = CallDirection.Outbound;
2022-03-25 16:53:12 +01:00
this.setState(CallState.CreateOffer, log);
2022-03-25 14:43:02 +01:00
for (const t of this.localMedia.tracks) {
this.peerConnection.addTrack(t);
}
// after adding the local tracks, and wait for handleNegotiation to be called,
// or invite glare where we give up our invite and answer instead
await this.waitForState([CallState.InviteSent, CallState.CreateAnswer]);
});
2022-02-25 16:54:00 +01:00
}
2022-03-25 14:43:02 +01:00
answer(localMedia: LocalMedia): Promise<void> {
return this.logItem.wrap("answer", async log => {
if (this._state !== CallState.Ringing) {
return;
}
this.localMedia = localMedia;
2022-03-25 16:53:12 +01:00
this.setState(CallState.CreateAnswer, log);
2022-03-25 14:43:02 +01:00
for (const t of this.localMedia.tracks) {
this.peerConnection.addTrack(t);
}
2022-03-01 15:36:31 +01:00
2022-03-25 14:43:02 +01:00
let myAnswer: RTCSessionDescriptionInit;
try {
myAnswer = await this.peerConnection.createAnswer();
} catch (err) {
await log.wrap(`Failed to create answer`, log => {
log.catch(err);
this.terminate(CallParty.Local, CallErrorCode.CreateAnswer, log);
2022-03-25 14:43:02 +01:00
});
return;
}
2022-03-01 15:36:31 +01:00
2022-03-25 14:43:02 +01:00
try {
await this.peerConnection.setLocalDescription(myAnswer);
2022-03-25 16:53:12 +01:00
this.setState(CallState.Connecting, log);
2022-03-25 14:43:02 +01:00
} catch (err) {
await log.wrap(`Error setting local description!`, log => {
log.catch(err);
this.terminate(CallParty.Local, CallErrorCode.SetLocalDescription, log);
2022-03-25 14:43:02 +01:00
});
return;
}
// Allow a short time for initial candidates to be gathered
try { await this.delay(200); }
catch (err) { return; }
await this.sendAnswer(log);
});
2022-02-25 16:54:00 +01:00
}
2022-03-25 14:43:02 +01:00
setMedia(localMediaPromise: Promise<LocalMedia>): Promise<void> {
return this.logItem.wrap("setMedia", async log => {
const oldMedia = this.localMedia;
this.localMedia = await localMediaPromise;
const applyTrack = (selectTrack: (media: LocalMedia | undefined) => Track | undefined) => {
const oldTrack = selectTrack(oldMedia);
const newTrack = selectTrack(this.localMedia);
if (oldTrack && newTrack) {
this.peerConnection.replaceTrack(oldTrack, newTrack);
} else if (oldTrack) {
this.peerConnection.removeTrack(oldTrack);
} else if (newTrack) {
this.peerConnection.addTrack(newTrack);
}
};
2022-02-25 16:54:00 +01:00
2022-03-25 14:43:02 +01:00
// add the local tracks, and wait for onNegotiationNeeded and handleNegotiation to be called
applyTrack(m => m?.microphoneTrack);
applyTrack(m => m?.cameraTrack);
applyTrack(m => m?.screenShareTrack);
});
2022-02-25 16:54:00 +01:00
}
/** group calls would handle reject at the group call level, not at the peer call level */
2022-03-09 11:29:39 +01:00
async reject() {
2022-02-25 16:54:00 +01:00
2022-03-09 11:29:39 +01:00
}
2022-02-25 16:54:00 +01:00
2022-03-25 14:43:02 +01:00
hangup(errorCode: CallErrorCode): Promise<void> {
return this.logItem.wrap("hangup", log => {
return this._hangup(errorCode, log);
});
}
private async _hangup(errorCode: CallErrorCode, log: ILogItem): Promise<void> {
if (this._state === CallState.Ended) {
return;
2022-03-16 19:10:51 +01:00
}
this.terminate(CallParty.Local, errorCode, log);
await this.sendHangupWithCallId(this.callId, errorCode, log);
2022-03-09 11:29:39 +01:00
}
2022-02-25 16:54:00 +01:00
2022-03-25 14:43:02 +01:00
handleIncomingSignallingMessage<B extends MCallBase>(message: SignallingMessage<B>, partyId: PartyId): Promise<void> {
return this.logItem.wrap({l: "receive", id: message.type, partyId}, async log => {
switch (message.type) {
case EventType.Invite:
if (this.callId !== message.content.call_id) {
await this.handleInviteGlare(message.content, partyId, log);
} else {
await this.handleFirstInvite(message.content, partyId, log);
}
break;
case EventType.Answer:
await this.handleAnswer(message.content, partyId, log);
break;
case EventType.Candidates:
await this.handleRemoteIceCandidates(message.content, partyId, log);
break;
case EventType.Hangup:
default:
2022-04-07 16:54:24 +02:00
log.log(`Unknown event type for call: ${message.type}`);
break;
2022-03-25 14:43:02 +01:00
}
});
2022-03-09 11:29:39 +01:00
}
2022-02-25 16:54:00 +01:00
2022-03-25 14:43:02 +01:00
private sendHangupWithCallId(callId: string, reason: CallErrorCode | undefined, log: ILogItem): Promise<void> {
2022-02-25 16:54:00 +01:00
const content = {
2022-03-09 11:29:39 +01:00
call_id: callId,
2022-02-25 16:54:00 +01:00
version: 1,
seq: this.seq++,
2022-02-25 16:54:00 +01:00
};
// TODO: Don't send UserHangup reason to older clients
2022-03-09 11:29:39 +01:00
if (reason) {
content["reason"] = reason;
2022-02-25 16:54:00 +01:00
}
2022-03-25 14:43:02 +01:00
return this.sendSignallingMessage({
2022-03-09 11:29:39 +01:00
type: EventType.Hangup,
content
2022-03-25 14:43:02 +01:00
}, log);
2022-03-09 11:29:39 +01:00
}
// calls are serialized and deduplicated by responsePromiseChain
2022-03-25 14:43:02 +01:00
private handleNegotiation = async (log: ILogItem): Promise<void> => {
2022-03-09 11:29:39 +01:00
this.makingOffer = true;
try {
try {
await this.peerConnection.setLocalDescription();
} catch (err) {
2022-03-25 14:43:02 +01:00
log.log(`Error setting local description!`).catch(err);
this.terminate(CallParty.Local, CallErrorCode.SetLocalDescription, log);
2022-03-09 11:29:39 +01:00
return;
}
if (this.peerConnection.iceGatheringState === 'gathering') {
// Allow a short time for initial candidates to be gathered
2022-03-25 14:43:02 +01:00
try { await this.delay(200); }
catch (err) { return; }
2022-03-09 11:29:39 +01:00
}
2022-03-10 14:53:31 +01:00
if (this._state === CallState.Ended) {
2022-03-09 11:29:39 +01:00
return;
}
const offer = this.peerConnection.localDescription!;
// Get rid of any candidates waiting to be sent: they'll be included in the local
// description we just got and will send in the offer.
2022-03-25 14:43:02 +01:00
log.log(`Discarding ${
2022-03-09 11:29:39 +01:00
this.candidateSendQueue.length} candidates that will be sent in offer`);
this.candidateSendQueue = [];
// need to queue this
const content = {
call_id: this.callId,
offer,
[SDPStreamMetadataKey]: this.localMedia!.getSDPMetadata(),
version: 1,
seq: this.seq++,
2022-03-09 11:29:39 +01:00
lifetime: CALL_TIMEOUT_MS
};
2022-03-10 14:53:31 +01:00
if (this._state === CallState.CreateOffer) {
2022-03-25 14:43:02 +01:00
await this.sendSignallingMessage({type: EventType.Invite, content}, log);
2022-03-25 16:53:12 +01:00
this.setState(CallState.InviteSent, log);
2022-03-10 14:53:31 +01:00
} else if (this._state === CallState.Connected || this._state === CallState.Connecting) {
2022-03-09 11:29:39 +01:00
// send Negotiate message
2022-03-25 14:43:02 +01:00
//await this.sendSignallingMessage({type: EventType.Invite, content});
2022-03-09 11:29:39 +01:00
//this.setState(CallState.InviteSent);
}
} finally {
this.makingOffer = false;
}
2022-03-25 14:43:02 +01:00
this.sendCandidateQueue(log);
2022-03-01 15:36:31 +01:00
2022-03-25 16:53:12 +01:00
await log.wrap("invite timeout", async log => {
2022-03-10 14:53:31 +01:00
if (this._state === CallState.InviteSent) {
2022-03-25 16:53:12 +01:00
try { await this.delay(CALL_TIMEOUT_MS); }
catch (err) { return; }
// @ts-ignore TS doesn't take the await above into account to know that the state could have changed in between
if (this._state === CallState.InviteSent) {
this._hangup(CallErrorCode.InviteTimeout, log);
}
2022-03-01 17:01:47 +01:00
}
2022-03-25 16:53:12 +01:00
});
2022-02-25 16:54:00 +01:00
};
2022-03-25 14:43:02 +01:00
private async handleInviteGlare(content: MCallInvite<MCallBase>, partyId: PartyId, log: ILogItem): Promise<void> {
2022-03-09 11:29:39 +01:00
// this is only called when the ids are different
const newCallId = content.call_id;
if (this.callId! > newCallId) {
2022-03-25 14:43:02 +01:00
log.log(
2022-03-09 11:29:39 +01:00
"Glare detected: answering incoming call " + newCallId +
2022-03-25 14:43:02 +01:00
" and canceling outgoing call ",
2022-03-09 11:29:39 +01:00
);
// How do we interrupt `call()`? well, perhaps we need to not just await InviteSent but also CreateAnswer?
if (this._state === CallState.Fledgling || this._state === CallState.CreateOffer) {
// TODO: don't send invite!
2022-03-09 11:29:39 +01:00
} else {
2022-03-25 14:43:02 +01:00
await this.sendHangupWithCallId(this.callId, CallErrorCode.Replaced, log);
2022-03-09 11:29:39 +01:00
}
2022-03-25 14:43:02 +01:00
await this.handleInvite(content, partyId, log);
// TODO: need to skip state check
await this.answer(this.localMedia!);
2022-03-09 11:29:39 +01:00
} else {
2022-03-25 14:43:02 +01:00
log.log(
2022-03-09 11:29:39 +01:00
"Glare detected: rejecting incoming call " + newCallId +
2022-03-25 14:43:02 +01:00
" and keeping outgoing call ",
2022-03-09 11:29:39 +01:00
);
2022-03-25 14:43:02 +01:00
await this.sendHangupWithCallId(newCallId, CallErrorCode.Replaced, log);
2022-03-09 11:29:39 +01:00
}
}
2022-03-25 14:43:02 +01:00
private async handleFirstInvite(content: MCallInvite<MCallBase>, partyId: PartyId, log: ILogItem): Promise<void> {
2022-03-10 14:53:31 +01:00
if (this._state !== CallState.Fledgling || this.opponentPartyId !== undefined) {
2022-02-25 16:54:00 +01:00
// TODO: hangup or ignore?
return;
}
2022-03-25 14:43:02 +01:00
await this.handleInvite(content, partyId, log);
2022-03-09 11:29:39 +01:00
}
2022-03-25 14:43:02 +01:00
private async handleInvite(content: MCallInvite<MCallBase>, partyId: PartyId, log: ILogItem): Promise<void> {
2022-02-25 16:54:00 +01:00
// 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.opponentPartyId = partyId;
this.direction = CallDirection.Inbound;
2022-02-17 16:58:44 +01:00
2022-02-25 16:54:00 +01:00
const sdpStreamMetadata = content[SDPStreamMetadataKey];
if (sdpStreamMetadata) {
this.updateRemoteSDPStreamMetadata(sdpStreamMetadata);
} else {
2022-03-25 14:43:02 +01:00
log.log(`Call did not get any SDPStreamMetadata! Can not send/receive multiple streams`);
2022-02-25 16:54:00 +01:00
}
try {
2022-03-01 15:36:31 +01:00
// Q: Why do we set the remote description before accepting the call? To start creating ICE candidates?
2022-02-25 16:54:00 +01:00
await this.peerConnection.setRemoteDescription(content.offer);
2022-03-25 14:43:02 +01:00
await this.addBufferedIceCandidates(log);
2022-02-25 16:54:00 +01:00
} catch (e) {
2022-03-25 14:43:02 +01:00
await log.wrap(`Call failed to set remote description`, async log => {
log.catch(e);
return this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, log);
2022-03-25 14:43:02 +01:00
});
2022-02-25 16:54:00 +01:00
return;
}
// 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 (this.peerConnection.remoteTracks.length === 0) {
2022-03-25 14:43:02 +01:00
await log.wrap(`Call no remote stream or no tracks after setting remote description!`, async log => {
return this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, log);
2022-03-25 14:43:02 +01:00
});
2022-02-25 16:54:00 +01:00
return;
}
2022-03-25 16:53:12 +01:00
this.setState(CallState.Ringing, log);
2022-02-25 16:54:00 +01:00
2022-03-25 14:43:02 +01:00
try { await this.delay(content.lifetime ?? CALL_TIMEOUT_MS); }
catch (err) { return; }
// @ts-ignore TS doesn't take the await above into account to know that the state could have changed in between
2022-03-10 14:53:31 +01:00
if (this._state === CallState.Ringing) {
2022-03-25 14:43:02 +01:00
log.log(`Invite has expired. Hanging up.`);
this.hangupParty = CallParty.Remote; // effectively
2022-03-25 16:53:12 +01:00
this.setState(CallState.Ended, log);
2022-03-30 15:18:46 +02:00
//this.localMedia?.dispose();
//this.localMedia = undefined;
if (this.peerConnection.signalingState != 'closed') {
this.peerConnection.close();
2022-02-25 16:54:00 +01:00
}
}
}
2022-03-25 14:43:02 +01:00
private async handleAnswer(content: MCallAnswer<MCallBase>, partyId: PartyId, log: ILogItem): Promise<void> {
2022-03-10 14:53:31 +01:00
if (this._state === CallState.Ended) {
2022-03-25 14:43:02 +01:00
log.log(`Ignoring answer because call has ended`);
return;
}
if (this.opponentPartyId !== undefined) {
2022-03-25 14:43:02 +01:00
log.log(`Ignoring answer: we already have an answer/reject from ${this.opponentPartyId}`);
return;
}
this.opponentPartyId = partyId;
2022-03-25 14:43:02 +01:00
await this.addBufferedIceCandidates(log);
2022-03-25 16:53:12 +01:00
this.setState(CallState.Connecting, log);
const sdpStreamMetadata = content[SDPStreamMetadataKey];
if (sdpStreamMetadata) {
this.updateRemoteSDPStreamMetadata(sdpStreamMetadata);
} else {
2022-03-25 14:43:02 +01:00
log.log(`Did not get any SDPStreamMetadata! Can not send/receive multiple streams`);
}
try {
await this.peerConnection.setRemoteDescription(content.answer);
} catch (e) {
2022-03-25 14:43:02 +01:00
await log.wrap(`Failed to set remote description`, log => {
log.catch(e);
this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, log);
2022-03-25 14:43:02 +01:00
});
return;
}
2022-02-25 16:54:00 +01:00
}
2022-03-25 14:43:02 +01:00
private handleIceGatheringState(state: RTCIceGatheringState, log: ILogItem) {
2022-03-16 19:10:51 +01:00
if (state === 'complete' && !this.sentEndOfCandidates) {
// If we didn't get an empty-string candidate to signal the end of candidates,
// create one ourselves now gathering has finished.
// We cast because the interface lists all the properties as required but we
// only want to send 'candidate'
// XXX: We probably want to send either sdpMid or sdpMLineIndex, as it's not strictly
// correct to have a candidate that lacks both of these. We'd have to figure out what
// previous candidates had been sent with and copy them.
const c = {
candidate: '',
} as RTCIceCandidate;
2022-03-25 14:43:02 +01:00
this.queueCandidate(c, log);
2022-03-16 19:10:51 +01:00
this.sentEndOfCandidates = true;
}
}
2022-03-25 14:43:02 +01:00
private handleLocalIceCandidate(candidate: RTCIceCandidate, log: ILogItem) {
log.set("sdpMid", candidate.sdpMid);
log.set("candidate", candidate.candidate);
2022-03-16 19:10:51 +01:00
2022-03-25 14:43:02 +01:00
if (this._state === CallState.Ended) {
return;
}
2022-03-16 19:10:51 +01:00
// As with the offer, note we need to make a copy of this object, not
// pass the original: that broke in Chrome ~m43.
if (candidate.candidate !== '' || !this.sentEndOfCandidates) {
2022-03-25 14:43:02 +01:00
this.queueCandidate(candidate, log);
if (candidate.candidate === '') {
this.sentEndOfCandidates = true;
}
2022-03-16 19:10:51 +01:00
}
}
2022-03-25 14:43:02 +01:00
private async handleRemoteIceCandidates(content: MCallCandidates<MCallBase>, partyId: PartyId, log: ILogItem) {
2022-03-11 16:35:32 +01:00
if (this.state === CallState.Ended) {
2022-03-25 14:43:02 +01:00
log.log("Ignoring remote ICE candidate because call has ended");
2022-03-11 16:35:32 +01:00
return;
}
const candidates = content.candidates;
if (!candidates) {
2022-03-25 14:43:02 +01:00
log.log(`Ignoring candidates event with no candidates!`);
2022-03-11 16:35:32 +01:00
return;
}
const fromPartyId = content.version === 0 ? null : partyId || null;
if (this.opponentPartyId === undefined) {
// we haven't picked an opponent yet so save the candidates
2022-03-25 14:43:02 +01:00
log.log(`Buffering ${candidates.length} candidates until we pick an opponent`);
2022-03-11 16:35:32 +01:00
const bufferedCandidates = this.remoteCandidateBuffer!.get(fromPartyId) || [];
bufferedCandidates.push(...candidates);
this.remoteCandidateBuffer!.set(fromPartyId, bufferedCandidates);
return;
}
if (this.opponentPartyId !== partyId) {
2022-03-25 14:43:02 +01:00
log.log(
2022-03-11 16:35:32 +01:00
`Ignoring candidates from party ID ${partyId}: ` +
`we have chosen party ID ${this.opponentPartyId}`,
);
return;
}
2022-03-25 14:43:02 +01:00
await this.addIceCandidates(candidates, log);
2022-03-11 16:35:32 +01:00
}
2022-03-09 11:29:39 +01:00
// private async onNegotiateReceived(event: MatrixEvent): Promise<void> {
// const content = event.getContent<MCallNegotiate>();
// const description = content.description;
// if (!description || !description.sdp || !description.type) {
2022-03-25 14:43:02 +01:00
// this.logger.info(`Ignoring invalid m.call.negotiate event`);
2022-03-09 11:29:39 +01:00
// return;
// }
// // Politeness always follows the direction of the call: in a glare situation,
// // we pick either the inbound or outbound call, so one side will always be
// // inbound and one outbound
// const polite = this.direction === CallDirection.Inbound;
// // Here we follow the perfect negotiation logic from
// // https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Perfect_negotiation
// const offerCollision = (
// (description.type === 'offer') &&
// (this.makingOffer || this.peerConnection.signalingState !== 'stable')
// );
// this.ignoreOffer = !polite && offerCollision;
// if (this.ignoreOffer) {
2022-03-25 14:43:02 +01:00
// this.logger.info(`Ignoring colliding negotiate event because we're impolite`);
2022-03-09 11:29:39 +01:00
// return;
// }
// const sdpStreamMetadata = content[SDPStreamMetadataKey];
// if (sdpStreamMetadata) {
// this.updateRemoteSDPStreamMetadata(sdpStreamMetadata);
// } else {
2022-03-25 14:43:02 +01:00
// this.logger.warn(`Received negotiation event without SDPStreamMetadata!`);
2022-03-09 11:29:39 +01:00
// }
// try {
// await this.peerConnection.setRemoteDescription(description);
// if (description.type === 'offer') {
// await this.peerConnection.setLocalDescription();
2022-03-25 14:43:02 +01:00
// await this.sendSignallingMessage({
2022-03-09 11:29:39 +01:00
// type: EventType.CallNegotiate,
// content: {
// description: this.peerConnection.localDescription!,
// [SDPStreamMetadataKey]: this.localMedia.getSDPMetadata(),
// }
// });
// }
// } catch (err) {
2022-03-25 14:43:02 +01:00
// this.logger.warn(`Failed to complete negotiation`, err);
2022-03-09 11:29:39 +01:00
// }
// }
2022-03-25 14:43:02 +01:00
private async sendAnswer(log: ILogItem): Promise<void> {
2022-03-09 11:29:39 +01:00
const localDescription = this.peerConnection.localDescription!;
2022-03-10 14:53:31 +01:00
const answerContent: MCallAnswer<MCallBase> = {
2022-03-09 11:29:39 +01:00
call_id: this.callId,
version: 1,
seq: this.seq++,
2022-03-09 11:29:39 +01:00
answer: {
sdp: localDescription.sdp,
type: localDescription.type,
},
[SDPStreamMetadataKey]: this.localMedia!.getSDPMetadata(),
2022-03-01 15:36:31 +01:00
};
// We have just taken the local description from the peerConn which will
// contain all the local candidates added so far, so we can discard any candidates
// we had queued up because they'll be in the answer.
2022-03-25 14:43:02 +01:00
log.log(`Discarding ${
2022-03-01 15:36:31 +01:00
this.candidateSendQueue.length} candidates that will be sent in answer`);
this.candidateSendQueue = [];
try {
2022-03-25 14:43:02 +01:00
await this.sendSignallingMessage({type: EventType.Answer, content: answerContent}, log);
2022-03-01 15:36:31 +01:00
} catch (error) {
this.terminate(CallParty.Local, CallErrorCode.SendAnswer, log);
2022-03-01 15:36:31 +01:00
throw error;
}
// error handler re-throws so this won't happen on error, but
// we don't want the same error handling on the candidate queue
2022-03-25 14:43:02 +01:00
this.sendCandidateQueue(log);
2022-03-01 15:36:31 +01:00
}
2022-03-25 14:43:02 +01:00
private queueCandidate(content: RTCIceCandidate, log: ILogItem): void {
2022-03-01 15:36:31 +01:00
// We partially de-trickle candidates by waiting for `delay` before sending them
// amalgamated, in order to avoid sending too many m.call.candidates events and hitting
// rate limits in Matrix.
// In practice, it'd be better to remove rate limits for m.call.*
// N.B. this deliberately lets you queue and send blank candidates, which MSC2746
// currently proposes as the way to indicate that candidate gathering is complete.
// This will hopefully be changed to an explicit rather than implicit notification
// shortly.
this.candidateSendQueue.push(content);
// Don't send the ICE candidates yet if the call is in the ringing state
2022-03-10 14:53:31 +01:00
if (this._state === CallState.Ringing) return;
2022-03-01 15:36:31 +01:00
// MSC2746 recommends these values (can be quite long when calling because the
// callee will need a while to answer the call)
2022-03-25 14:43:02 +01:00
const sendLogItem = this.logItem.child("wait to send candidates");
log.refDetached(sendLogItem);
this.delay(this.direction === CallDirection.Inbound ? 500 : 2000)
.then(() => {
return this.sendCandidateQueue(sendLogItem);
}, err => {}) // swallow delay AbortError
.finally(() => {
sendLogItem.finish();
});
2022-03-01 15:36:31 +01:00
}
2022-03-25 14:43:02 +01:00
private async sendCandidateQueue(log: ILogItem): Promise<void> {
return log.wrap("send candidates queue", async log => {
log.set("queueLength", this.candidateSendQueue.length);
2022-03-01 15:36:31 +01:00
2022-03-25 14:43:02 +01:00
if (this.candidateSendQueue.length === 0 || this._state === CallState.Ended) {
return;
}
const candidates = this.candidateSendQueue;
this.candidateSendQueue = [];
try {
await this.sendSignallingMessage({
type: EventType.Candidates,
content: {
call_id: this.callId,
version: 1,
seq: this.seq++,
2022-03-25 14:43:02 +01:00
candidates
},
}, log);
// Try to send candidates again just in case we received more candidates while sending.
this.sendCandidateQueue(log);
} catch (error) {
log.catch(error);
// don't retry this event: we'll send another one later as we might
// have more candidates by then.
// put all the candidates we failed to send back in the queue
// TODO: terminate doesn't seem to vibe with the comment above?
this.terminate(CallParty.Local, CallErrorCode.SignallingFailed, log);
2022-03-25 14:43:02 +01:00
}
});
2022-03-01 15:36:31 +01:00
}
2022-02-25 16:54:00 +01:00
private updateRemoteSDPStreamMetadata(metadata: SDPStreamMetadata): void {
this.remoteSDPStreamMetadata = recursivelyAssign(this.remoteSDPStreamMetadata || {}, metadata, true);
// will rerequest stream purpose for all tracks and set track.type accordingly
this.peerConnection.notifyStreamPurposeChanged();
for (const track of this.peerConnection.remoteTracks) {
const streamMetaData = this.remoteSDPStreamMetadata?.[track.streamId];
if (streamMetaData) {
if (track.type === TrackType.Microphone) {
track.setMuted(streamMetaData.audio_muted);
} else { // Camera or ScreenShare
track.setMuted(streamMetaData.video_muted);
}
}
}
}
2022-03-25 14:43:02 +01:00
private async addBufferedIceCandidates(log: ILogItem): Promise<void> {
if (this.remoteCandidateBuffer && this.opponentPartyId) {
const bufferedCandidates = this.remoteCandidateBuffer.get(this.opponentPartyId);
if (bufferedCandidates) {
2022-03-25 14:43:02 +01:00
log.log(`Adding ${
bufferedCandidates.length} buffered candidates for opponent ${this.opponentPartyId}`);
2022-03-25 14:43:02 +01:00
await this.addIceCandidates(bufferedCandidates, log);
}
this.remoteCandidateBuffer = undefined;
2022-02-25 16:54:00 +01:00
}
}
2022-03-25 14:43:02 +01:00
private async addIceCandidates(candidates: RTCIceCandidate[], log: ILogItem): Promise<void> {
2022-02-25 16:54:00 +01:00
for (const candidate of candidates) {
if (
(candidate.sdpMid === null || candidate.sdpMid === undefined) &&
(candidate.sdpMLineIndex === null || candidate.sdpMLineIndex === undefined)
) {
2022-03-25 14:43:02 +01:00
log.log(`Ignoring remote ICE candidate with no sdpMid or sdpMLineIndex`);
2022-02-25 16:54:00 +01:00
continue;
}
2022-03-25 14:43:02 +01:00
log.log(`Got remote ICE ${candidate.sdpMid} candidate: ${candidate.candidate}`);
2022-02-25 16:54:00 +01:00
try {
await this.peerConnection.addIceCandidate(candidate);
} catch (err) {
if (!this.ignoreOffer) {
2022-03-25 14:43:02 +01:00
log.log(`Failed to add remote ICE candidate`, err);
2022-02-25 16:54:00 +01:00
}
}
}
}
2022-03-25 14:43:02 +01:00
private onIceConnectionStateChange = (state: RTCIceConnectionState, log: ILogItem): void => {
2022-03-16 19:10:51 +01:00
if (this._state === CallState.Ended) {
return; // because ICE can still complete as we're ending the call
}
// ideally we'd consider the call to be connected when we get media but
// chrome doesn't implement any of the 'onstarted' events yet
if (state == 'connected') {
this.iceDisconnectedTimeout?.abort();
this.iceDisconnectedTimeout = undefined;
2022-03-25 16:53:12 +01:00
this.setState(CallState.Connected, log);
2022-03-16 19:10:51 +01:00
} else if (state == 'failed') {
this.iceDisconnectedTimeout?.abort();
this.iceDisconnectedTimeout = undefined;
2022-03-25 14:43:02 +01:00
this._hangup(CallErrorCode.IceFailed, log);
2022-03-16 19:10:51 +01:00
} else if (state == 'disconnected') {
this.iceDisconnectedTimeout = this.options.createTimeout(30 * 1000);
this.iceDisconnectedTimeout.elapsed().then(() => {
2022-03-25 14:43:02 +01:00
this._hangup(CallErrorCode.IceFailed, log);
2022-03-16 19:10:51 +01:00
}, () => { /* ignore AbortError */ });
}
};
2022-03-25 16:53:12 +01:00
private setState(state: CallState, log: ILogItem): void {
if (state !== this._state) {
log.log({l: "change state", status: state, oldState: this._state});
const oldState = this._state;
this._state = state;
let deferred = this.statePromiseMap.get(state);
if (deferred) {
deferred.resolve();
this.statePromiseMap.delete(state);
}
this.options.emitUpdate(this, undefined);
}
2022-02-25 16:54:00 +01:00
}
2022-03-09 11:29:39 +01:00
private waitForState(states: CallState[]): Promise<void> {
// TODO: rework this, do we need to clean up the promises?
return Promise.race(states.map(state => {
let deferred = this.statePromiseMap.get(state);
if (!deferred) {
let resolve;
const promise = new Promise<void>(r => {
resolve = r;
});
deferred = {resolve, promise};
this.statePromiseMap.set(state, deferred);
}
return deferred.promise;
}));
2022-02-25 16:54:00 +01:00
}
private terminate(hangupParty: CallParty, hangupReason: CallErrorCode, log: ILogItem): void {
if (this._state === CallState.Ended) {
return;
}
this.hangupParty = hangupParty;
// this.hangupReason = hangupReason;
this.setState(CallState.Ended, log);
2022-03-30 15:18:46 +02:00
//this.localMedia?.dispose();
//this.localMedia = undefined;
2022-02-25 16:54:00 +01:00
if (this.peerConnection && this.peerConnection.signalingState !== 'closed') {
this.peerConnection.close();
}
2022-02-25 16:54:00 +01:00
}
2022-03-01 15:36:31 +01:00
private async delay(timeoutMs: number): Promise<void> {
// Allow a short time for initial candidates to be gathered
2022-03-10 14:53:31 +01:00
const timeout = this.disposables.track(this.options.createTimeout(timeoutMs));
await timeout.elapsed();
this.disposables.untrack(timeout);
}
2022-03-25 14:43:02 +01:00
private sendSignallingMessage(message: SignallingMessage<MCallBase>, log: ILogItem) {
return log.wrap({l: "send", id: message.type}, async log => {
return this.options.sendSignallingMessage(message, log);
});
}
public dispose(): void {
this.disposables.dispose();
2022-03-07 10:15:54 +01:00
this.peerConnection.dispose();
}
2022-03-24 13:52:19 +01:00
public close(reason: CallErrorCode | undefined, log: ILogItem): void {
if (reason === undefined) {
reason = CallErrorCode.UserHangup;
}
this.terminate(CallParty.Local, reason, log);
2022-03-24 13:52:19 +01:00
}
2022-02-25 16:54:00 +01:00
}
//import { randomString } from '../randomstring';
2022-02-17 16:58:44 +01:00
// null is used as a special value meaning that the we're in a legacy 1:1 call
// without MSC2746 that doesn't provide an id which device sent the message.
type PartyId = string | null;
2022-02-25 16:54:00 +01:00
export enum CallParty {
Local = 'local',
Remote = 'remote',
2022-02-17 16:58:44 +01:00
}
export enum CallState {
Fledgling = 'fledgling',
CreateOffer = 'create_offer',
2022-03-07 10:15:54 +01:00
InviteSent = 'invite_sent',
2022-02-17 16:58:44 +01:00
CreateAnswer = 'create_answer',
Connecting = 'connecting',
Connected = 'connected',
Ringing = 'ringing',
Ended = 'ended',
}
export enum CallDirection {
Inbound = 'inbound',
Outbound = 'outbound',
}
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',
/**
* 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 = 60000;
2022-03-17 11:31:12 +01:00
//const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
2022-02-17 16:58:44 +01:00
export class CallError extends Error {
code: string;
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;
}
}
2022-03-09 18:53:51 +01:00
export function handlesEventType(eventType: string): boolean {
return eventType === EventType.Invite ||
eventType === EventType.Candidates ||
eventType === EventType.Answer ||
eventType === EventType.Hangup;
}
2022-03-09 11:29:39 +01:00
export function tests() {
2022-03-01 15:36:31 +01:00
2022-02-17 16:58:44 +01:00
}