Skip to content

Commit

Permalink
Mute disconnected peers in PTT mode
Browse files Browse the repository at this point in the history
When we lose ICE connection to peers, set the status of their feeds
to muted so to end their talking session.

For element-hq/element-call#331
  • Loading branch information
dbkr committed May 30, 2022
1 parent 18e2052 commit 746d6ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
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 client: MatrixClient;
private forceTURN: boolean;
private turnServers: Array<TurnServer>;
Expand Down Expand Up @@ -1876,9 +1880,10 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
logger.debug(
"Call ID " + this.callId + ": ICE connection state changed to: " + this.peerConn.iceConnectionState,
);

// 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 (this.peerConn.iceConnectionState == 'connected') {
if (["connected", "completed"].includes(this.peerConn.iceConnectionState)) {
clearTimeout(this.iceDisconnectedTimeout);
this.setState(CallState.Connected);

Expand All @@ -1900,6 +1905,17 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
this.hangup(CallErrorCode.IceFailed, false);
}, 30 * 1000);
}

// In PTT mode, override feed status to muted when we lose connection to
// the peer, since we don't want to block the line if they're not saying anything.
// Experimenting in Chrome, this happens after 5 or 6 seconds, which is probably
// fast enough.
if (this.isPtt && !["connected", "completed"].includes(this.peerConn.iceConnectionState)) {
for (const feed of this.getRemoteFeeds()) {
feed.setAudioMuted(true);
feed.setVideoMuted(true);
}
}
};

private onSignallingStateChanged = (): void => {
Expand Down
5 changes: 5 additions & 0 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,9 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH

logger.log(`GroupCall: incoming call from: ${opponentMemberId}`);

// we are handlng this call as a PTT call, so enable PTT semantics
newCall.isPtt = true;

// Check if the user calling has an existing call and use this call instead.
if (existingCall) {
this.replaceCall(existingCall, newCall);
Expand Down Expand Up @@ -775,6 +778,8 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
},
);

newCall.isPtt = true;

const requestScreenshareFeed = opponentDevice.feeds.some(
(feed) => feed.purpose === SDPStreamMetadataPurpose.Screenshare);

Expand Down

0 comments on commit 746d6ac

Please sign in to comment.