Skip to content

Commit

Permalink
Also wrap the initLocalFeed method of groupCall
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkr committed Jan 19, 2023
1 parent 4185491 commit ea5ce8d
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export class GroupCall extends TypedEventEmitter<
private resendMemberStateTimer: ReturnType<typeof setInterval> | null = null;
private initWithAudioMuted = false;
private initWithVideoMuted = false;
private initCallFeedPromise?: Promise<void>;

public constructor(
private client: MatrixClient,
Expand Down Expand Up @@ -348,38 +349,41 @@ export class GroupCall extends TypedEventEmitter<
}

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

if (this.state !== GroupCallState.LocalCallFeedUninitialized) {
throw new Error(`Cannot initialize local call feed in the "${this.state}" state.`);
}

this.state = GroupCallState.InitializingLocalCallFeed;

let stream: MediaStream;
// wraps the real method to serialise calls, because we don't want to try starting
// multiple call feeds at once
if (this.initCallFeedPromise) return this.initCallFeedPromise;

let disposed = false;
const onState = (state: GroupCallState): void => {
if (state === GroupCallState.LocalCallFeedUninitialized) {
disposed = true;
}
};
this.on(GroupCallEvent.GroupCallStateChanged, onState);
try {
this.initCallFeedPromise = this.initLocalCallFeedInternal();
await this.initCallFeedPromise;
} finally {
this.initCallFeedPromise = undefined;
}
}

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

let stream: MediaStream;

try {
stream = await this.client.getMediaHandler().getUserMediaStream(true, this.type === GroupCallType.Video);
} catch (error) {
this.state = GroupCallState.LocalCallFeedUninitialized;
throw error;
} finally {
this.off(GroupCallEvent.GroupCallStateChanged, onState);
}

// The call could've been disposed while we were waiting
if (disposed) {
logger.info("Group call disposed while gathering media stream");
// The call could've been disposed while we were waiting, and could
// also have been started back up again (hello, React 18) so if we're
// still in this 'initializing' state, carry on, otherwise bail.
if (this._state !== GroupCallState.InitializingLocalCallFeed) {
this.client.getMediaHandler().stopUserMediaStream(stream);
return;
throw new Error("Group call disposed while gathering media stream");
}

const callFeed = new CallFeed({
Expand Down

0 comments on commit ea5ce8d

Please sign in to comment.