Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve races between initLocalCallFeed and leave #2826

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion spec/unit/webrtc/groupCall.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
Room,
RoomMember,
} from '../../../src';
import { GroupCall, GroupCallEvent } from "../../../src/webrtc/groupCall";
import { GroupCall, GroupCallEvent, GroupCallState } from "../../../src/webrtc/groupCall";
import { MatrixClient } from "../../../src/client";
import {
installWebRTCMocks,
Expand Down Expand Up @@ -174,6 +174,13 @@ describe('Group Call', function() {
groupCall.leave();
});

it("stops initializing local call feed when leaving", async () => {
const initPromise = groupCall.initLocalCallFeed();
groupCall.leave();
await expect(initPromise).rejects.toBeDefined();
expect(groupCall.state).toBe(GroupCallState.LocalCallFeedUninitialized);
});

it("sends state event to room when creating", async () => {
await groupCall.create();

Expand Down
13 changes: 13 additions & 0 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,26 @@ export class GroupCall extends TypedEventEmitter<

let stream: MediaStream;

let disposed = false;
const onState = (state: GroupCallState) => {
if (state === GroupCallState.LocalCallFeedUninitialized) {
disposed = true;
}
};
this.on(GroupCallEvent.GroupCallStateChanged, onState);

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

// The call could've been disposed while we were waiting
if (disposed) throw new Error("Group call disposed");

const userId = this.client.getUserId()!;

const callFeed = new CallFeed({
Expand Down