Skip to content

Commit

Permalink
Prevent double mute status changed events (#2502)
Browse files Browse the repository at this point in the history
Audio & video mute status were set in separate calls but share a
mute status changed event, so you'd always get two mute status
changed events emitted. We could suppress events where the mute
status didn't change, but this would still get two events saying
the same thing when they both changed. Instead, merge setAudioMuted
& setVideoMuted into a single call that sets either or both.
  • Loading branch information
dbkr authored Jul 8, 2022
1 parent bdb91b3 commit 984dd26
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
10 changes: 4 additions & 6 deletions src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
await this.upgradeCall(false, true);
return this.isLocalVideoMuted();
}
this.localUsermediaFeed?.setVideoMuted(muted);
this.localUsermediaFeed?.setAudioVideoMuted(null, muted);
this.updateMuteStatus();
await this.sendMetadataUpdate();
return this.isLocalVideoMuted();
Expand Down Expand Up @@ -1296,7 +1296,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
await this.upgradeCall(true, false);
return this.isMicrophoneMuted();
}
this.localUsermediaFeed?.setAudioMuted(muted);
this.localUsermediaFeed?.setAudioVideoMuted(muted, null);
this.updateMuteStatus();
await this.sendMetadataUpdate();
return this.isMicrophoneMuted();
Expand Down Expand Up @@ -1801,8 +1801,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
const streamId = feed.stream.id;
const metadata = this.remoteSDPStreamMetadata[streamId];

feed.setAudioMuted(metadata?.audio_muted);
feed.setVideoMuted(metadata?.video_muted);
feed.setAudioVideoMuted(metadata?.audio_muted, metadata?.video_muted);
feed.purpose = this.remoteSDPStreamMetadata[streamId]?.purpose;
}
}
Expand Down Expand Up @@ -2027,8 +2026,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
// fast enough.
if (this.isPtt && ["failed", "disconnected"].includes(this.peerConn.iceConnectionState)) {
for (const feed of this.getRemoteFeeds()) {
feed.setAudioMuted(true);
feed.setVideoMuted(true);
feed.setAudioVideoMuted(true, true);
}
}
};
Expand Down
23 changes: 10 additions & 13 deletions src/webrtc/callFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,18 @@ export class CallFeed extends TypedEventEmitter<CallFeedEvent, EventHandlerMap>
}

/**
* Set feed's internal audio mute state
* @param muted is the feed's audio muted?
*/
public setAudioMuted(muted: boolean): void {
this.audioMuted = muted;
this.speakingVolumeSamples.fill(-Infinity);
this.emit(CallFeedEvent.MuteStateChanged, this.audioMuted, this.videoMuted);
}

/**
* Set feed's internal video mute state
* Set one or both of feed's internal audio and video video mute state
* Either value may be null to leave it as-is
* @param muted is the feed's video muted?
*/
public setVideoMuted(muted: boolean): void {
this.videoMuted = muted;
public setAudioVideoMuted(audioMuted: boolean, videoMuted: boolean): void {
if (audioMuted !== null) {
if (this.audioMuted !== audioMuted) {
this.speakingVolumeSamples.fill(-Infinity);
}
this.audioMuted = audioMuted;
}
if (videoMuted !== null) this.videoMuted = videoMuted;
this.emit(CallFeedEvent.MuteStateChanged, this.audioMuted, this.videoMuted);
}

Expand Down
6 changes: 3 additions & 3 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
}

for (const call of this.calls) {
call.localUsermediaFeed.setAudioMuted(muted);
call.localUsermediaFeed.setAudioVideoMuted(muted, null);
}

if (sendUpdatesBefore) {
Expand All @@ -474,7 +474,7 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
if (this.localCallFeed) {
logger.log(`groupCall ${this.groupCallId} setMicrophoneMuted stream ${
this.localCallFeed.stream.id} muted ${muted}`);
this.localCallFeed.setAudioMuted(muted);
this.localCallFeed.setAudioVideoMuted(muted, null);
// I don't believe its actually necessary to enable these tracks: they
// are the one on the groupcall's own CallFeed and are cloned before being
// given to any of the actual calls, so these tracks don't actually go
Expand Down Expand Up @@ -514,7 +514,7 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
if (this.localCallFeed) {
logger.log(`groupCall ${this.groupCallId} setLocalVideoMuted stream ${
this.localCallFeed.stream.id} muted ${muted}`);
this.localCallFeed.setVideoMuted(muted);
this.localCallFeed.setAudioVideoMuted(null, muted);
setTracksEnabled(this.localCallFeed.stream.getVideoTracks(), !muted);
}

Expand Down

0 comments on commit 984dd26

Please sign in to comment.