Skip to content

Commit

Permalink
Handle group call getting initialised twice in quick succession
Browse files Browse the repository at this point in the history
This happens in dev mode with React 18 and caused extra user media
stream to end up floating around.

Fixes element-hq/element-call#847
  • Loading branch information
dbkr committed Jan 19, 2023
1 parent a34d06c commit 495642d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class GroupCall extends TypedEventEmitter<
);
}

public async initLocalCallFeed(): Promise<CallFeed> {
public async initLocalCallFeed(): Promise<void> {
logger.log(`groupCall ${this.groupCallId} initLocalCallFeed`);

if (this.state !== GroupCallState.LocalCallFeedUninitialized) {
Expand Down Expand Up @@ -376,7 +376,11 @@ export class GroupCall extends TypedEventEmitter<
}

// The call could've been disposed while we were waiting
if (disposed) throw new Error("Group call disposed");
if (disposed) {
logger.info("Group call disposed while gathering media stream");
this.client.getMediaHandler().stopUserMediaStream(stream);
return;
}

const callFeed = new CallFeed({
client: this.client,
Expand All @@ -396,8 +400,6 @@ export class GroupCall extends TypedEventEmitter<
this.addUserMediaFeed(callFeed);

this.state = GroupCallState.LocalCallFeedInitialized;

return callFeed;
}

public async updateLocalUsermediaStream(stream: MediaStream): Promise<void> {
Expand Down
16 changes: 16 additions & 0 deletions src/webrtc/mediaHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export class MediaHandler extends TypedEventEmitter<
public userMediaStreams: MediaStream[] = [];
public screensharingStreams: MediaStream[] = [];

// Promise chain to serialise calls to getMediaStream
private getMediaStreamPromise?: Promise<MediaStream>;

public constructor(private client: MatrixClient) {
super();
}
Expand Down Expand Up @@ -196,6 +199,19 @@ export class MediaHandler extends TypedEventEmitter<
* @returns based on passed parameters
*/
public async getUserMediaStream(audio: boolean, video: boolean, reusable = true): Promise<MediaStream> {
// Serialise calls, othertwise we can't sensibly re-use the stream
if (this.getMediaStreamPromise) {
this.getMediaStreamPromise = this.getMediaStreamPromise.then(() => {
return this.getUserMediaStreamInternal(audio, video, reusable);
});
} else {
this.getMediaStreamPromise = this.getUserMediaStreamInternal(audio, video, reusable);
}

return this.getMediaStreamPromise;
}

private async getUserMediaStreamInternal(audio: boolean, video: boolean, reusable: boolean): Promise<MediaStream> {
const shouldRequestAudio = audio && (await this.hasAudioDevice());
const shouldRequestVideo = video && (await this.hasVideoDevice());

Expand Down

0 comments on commit 495642d

Please sign in to comment.