Skip to content

Commit

Permalink
Merge pull request #1510 from matrix-org/dbkr/hang_up_your_hangups
Browse files Browse the repository at this point in the history
Support m.call.reject
  • Loading branch information
dbkr authored Oct 19, 2020
2 parents 08c15e7 + 7c3af91 commit 701dfa0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/@types/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export enum EventType {
CallCandidates = "m.call.candidates",
CallAnswer = "m.call.answer",
CallHangup = "m.call.hangup",
CallReject = "m.call.reject",
KeyVerificationRequest = "m.key.verification.request",
KeyVerificationStart = "m.key.verification.start",
KeyVerificationCancel = "m.key.verification.cancel",
Expand Down
61 changes: 55 additions & 6 deletions src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ export enum CallErrorCode {
Replaced = 'replaced',
}

/**
* The version field that we set in m.call.* events
* Once we are able to speak v1 VoIP sufficiently, this
* bumped to 1. While we partially speak v1 VoIP, it remains
* as 0.
*/
const VOIP_PROTO_VERSION = 0;

/** The fallback ICE server to use for STUN or TURN protocols. */
const FALLBACK_ICE_SERVER = 'stun:turn.matrix.org';

Expand Down Expand Up @@ -222,6 +230,7 @@ export class MatrixCall extends EventEmitter {
// XXX: I don't know why this is called 'config'.
private config: MediaStreamConstraints;
private successor: MatrixCall;
private opponentVersion: number;

constructor(opts: CallOpts) {
super();
Expand Down Expand Up @@ -439,6 +448,7 @@ export class MatrixCall extends EventEmitter {

this.setState(CallState.Ringing);
this.direction = CallDirection.Inbound;
this.opponentVersion = this.msg.version;

if (event.getLocalAge()) {
setTimeout(() => {
Expand All @@ -457,7 +467,7 @@ export class MatrixCall extends EventEmitter {
}

/**
* Configure this call from a hangup event. Used by MatrixClient.
* Configure this call from a hangup or reject event. Used by MatrixClient.
* @param {MatrixEvent} event The m.call.hangup event
*/
initWithHangup(event: MatrixEvent) {
Expand Down Expand Up @@ -536,7 +546,7 @@ export class MatrixCall extends EventEmitter {
logger.debug("Ending call " + this.callId);
this.terminate(CallParty.Local, reason, !suppressEvent);
const content = {
version: 0,
version: VOIP_PROTO_VERSION,
call_id: this.callId,
};
// Continue to send no reason for user hangups temporarily, until
Expand All @@ -545,6 +555,33 @@ export class MatrixCall extends EventEmitter {
this.sendEvent('m.call.hangup', content);
}

/**
* Reject a call
* This used to be done by calling hangup, but is a separate method and protocol
* event as of MSC2746.
*/
reject() {
if (this.state !== CallState.Ringing) {
throw Error("Call must be in 'ringing' state to reject!");
}

if (this.opponentVersion < 1) {
logger.info(
`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);
const content = {
version: VOIP_PROTO_VERSION,
call_id: this.callId,
};
this.sendEvent('m.call.reject', content);
}

/**
* Set whether the local video preview should be muted or not.
* @param {boolean} muted True to mute the local video.
Expand Down Expand Up @@ -713,7 +750,7 @@ export class MatrixCall extends EventEmitter {
await this.peerConn.setLocalDescription(myAnswer);

this.answerContent = {
version: 0,
version: VOIP_PROTO_VERSION,
call_id: this.callId,
answer: {
sdp: this.peerConn.localDescription.sdp,
Expand Down Expand Up @@ -800,6 +837,8 @@ export class MatrixCall extends EventEmitter {
return;
}

this.opponentVersion = msg.version;

try {
this.peerConn.setRemoteDescription(msg.answer);
} catch (e) {
Expand Down Expand Up @@ -829,7 +868,7 @@ export class MatrixCall extends EventEmitter {
}

const content = {
version: 0,
version: VOIP_PROTO_VERSION,
call_id: this.callId,
// OpenWebRTC appears to add extra stuff (like the DTLS fingerprint)
// to the description when setting it on the peerconnection.
Expand Down Expand Up @@ -1004,7 +1043,17 @@ export class MatrixCall extends EventEmitter {

onHangupReceived = (msg) => {
logger.debug("Hangup received");
this.terminate(CallParty.Remote, msg.reason, true);
// default reason is user_hangup
this.terminate(CallParty.Remote, msg.reason || CallErrorCode.UserHangup, true);
};

onRejectReceived = (msg) => {
logger.debug("Reject received");
if (this.state === CallState.InviteSent) {
this.terminate(CallParty.Remote, CallErrorCode.UserHangup, true);
} else {
logger.debug(`Call is in state: ${this.state}: ignoring reject`);
}
};

onAnsweredElsewhere = (msg) => {
Expand Down Expand Up @@ -1127,7 +1176,7 @@ export class MatrixCall extends EventEmitter {
this.candidateSendQueue = [];
++this.candidateSendTries;
const content = {
version: 0,
version: VOIP_PROTO_VERSION,
call_id: this.callId,
candidates: cands,
};
Expand Down
8 changes: 6 additions & 2 deletions src/webrtc/callEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class CallEventHandler {
call.gotRemoteIceCandidate(cand);
}
}
} else if (event.getType() === EventType.CallHangup) {
} else if ([EventType.CallHangup, EventType.CallReject].includes(event.getType())) {
// Note that we also observe our own hangups here so we can see
// if we've already rejected a call that would otherwise be valid
if (!call) {
Expand All @@ -247,7 +247,11 @@ export class CallEventHandler {
}
} else {
if (call.state !== CallState.Ended) {
call.onHangupReceived(content);
if (event.getType() === EventType.CallHangup) {
call.onHangupReceived(content);
} else {
call.onRejectReceived(content);
}
this.calls.delete(content.call_id);
}
}
Expand Down

0 comments on commit 701dfa0

Please sign in to comment.