From ff856d843ce7dedd2677e4def1c8347ac1c36568 Mon Sep 17 00:00:00 2001 From: Bruno Windels <274386+bwindels@users.noreply.github.com> Date: Thu, 14 Apr 2022 13:44:11 +0200 Subject: [PATCH] ensure all member streams are cloned so we can stop them without affecting the main one also, only stop them when disconnecting from the member, rather then when the peer call ends, as we might want to retry connecting to the peer with the same stream. --- src/matrix/calls/LocalMedia.ts | 4 ++-- src/matrix/calls/PeerCall.ts | 1 - src/matrix/calls/group/GroupCall.ts | 7 ++++--- src/matrix/calls/group/Member.ts | 1 + 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/matrix/calls/LocalMedia.ts b/src/matrix/calls/LocalMedia.ts index 6622f641..6564adac 100644 --- a/src/matrix/calls/LocalMedia.ts +++ b/src/matrix/calls/LocalMedia.ts @@ -37,10 +37,10 @@ export class LocalMedia { return new LocalMedia(this.userMedia, this.screenShare, options); } - clone() { + clone(): LocalMedia { return new LocalMedia(this.userMedia?.clone(), this.screenShare?.clone(), this.dataChannelOptions); } - + dispose() { this.userMedia?.audioTrack?.stop(); this.userMedia?.videoTrack?.stop(); diff --git a/src/matrix/calls/PeerCall.ts b/src/matrix/calls/PeerCall.ts index 78ffc818..5b6e2ca8 100644 --- a/src/matrix/calls/PeerCall.ts +++ b/src/matrix/calls/PeerCall.ts @@ -814,7 +814,6 @@ export class PeerCall implements IDisposable { this.hangupParty = hangupParty; this._hangupReason = hangupReason; this.setState(CallState.Ended, log); - this.localMedia?.dispose(); this.localMedia = undefined; if (this.peerConnection && this.peerConnection.signalingState !== 'closed') { diff --git a/src/matrix/calls/group/GroupCall.ts b/src/matrix/calls/group/GroupCall.ts index 47d87e5e..d077f27c 100644 --- a/src/matrix/calls/group/GroupCall.ts +++ b/src/matrix/calls/group/GroupCall.ts @@ -118,7 +118,7 @@ export class GroupCall extends EventEmitter<{change: never}> { this.emitChange(); // send invite to all members that are < my userId for (const [,member] of this._members) { - member.connect(this._localMedia); + member.connect(this._localMedia!.clone()); } }); } @@ -218,6 +218,7 @@ export class GroupCall extends EventEmitter<{change: never}> { ); this._members.add(memberKey, member); if (this._state === GroupCallState.Joining || this._state === GroupCallState.Joined) { + // Safari can't send a MediaStream to multiple sources, so clone it member.connect(this._localMedia!.clone()); } } @@ -267,11 +268,11 @@ export class GroupCall extends EventEmitter<{change: never}> { private removeOwnDevice(log: ILogItem) { if (this._state === GroupCallState.Joined) { log.set("leave_own", true); - this._localMedia?.dispose(); - this._localMedia = undefined; for (const [,member] of this._members) { member.disconnect(); } + this._localMedia?.dispose(); + this._localMedia = undefined; this._state = GroupCallState.Created; this.emitChange(); } diff --git a/src/matrix/calls/group/Member.ts b/src/matrix/calls/group/Member.ts index 110aba6d..1305fcf1 100644 --- a/src/matrix/calls/group/Member.ts +++ b/src/matrix/calls/group/Member.ts @@ -104,6 +104,7 @@ export class Member { this.peerCall?.close(undefined, log); this.peerCall?.dispose(); this.peerCall = undefined; + this.localMedia?.dispose(); this.localMedia = undefined; }); }