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

Handle other members having no e2e keys #2383

Merged
merged 1 commit into from
May 19, 2022
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
23 changes: 20 additions & 3 deletions src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { CallFeed } from './callFeed';
import { MatrixClient } from "../client";
import { ISendEventResponse } from "../@types/requests";
import { EventEmitterEvents, TypedEventEmitter } from "../models/typed-event-emitter";
import { DeviceInfo } from '../crypto/deviceinfo';

// events: hangup, error(err), replaced(call), state(state, oldState)

Expand Down Expand Up @@ -343,6 +344,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
private callLength = 0;

private opponentDeviceId: string;
private opponentDeviceInfo: DeviceInfo;
private opponentSessionId: string;
public groupCallId: string;

Expand Down Expand Up @@ -508,6 +510,17 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
return this.feeds.filter((feed) => !feed.isLocal());
}

private async initOpponentCrypto() {
if (!this.opponentDeviceId) return;

const userId = this.invitee || this.getOpponentMember().userId;
const deviceInfoMap = await this.client.crypto.deviceList.downloadKeys([userId], false);
this.opponentDeviceInfo = deviceInfoMap[userId][this.opponentDeviceId];
if (this.opponentDeviceInfo === undefined) {
throw new Error(`No keys found for opponent device ${this.opponentDeviceId}!`);
}
}

/**
* Generates and returns localSDPStreamMetadata
* @returns {SDPStreamMetadata} localSDPStreamMetadata
Expand Down Expand Up @@ -792,6 +805,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
// handler will start giving us more call events (eg. candidates) so if
// we haven't set the party ID, we'll ignore them.
this.chooseOpponent(event);
await this.initOpponentCrypto();
try {
await this.peerConn.setRemoteDescription(invite.offer);
await this.addBufferedIceCandidates();
Expand Down Expand Up @@ -2052,9 +2066,10 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
},
};
const userId = this.invitee || this.getOpponentMember().userId;
const deviceInfoMap = await this.client.crypto.deviceList.downloadKeys([userId], false);
const deviceInfo = deviceInfoMap[userId][this.opponentDeviceId];
return this.client.crypto.encryptAndSendToDevices([{ userId, deviceInfo }], payload);
return this.client.crypto.encryptAndSendToDevices([{
userId,
deviceInfo: this.opponentDeviceInfo,
}], payload);
} else {
this.emit(CallEvent.SendVoipEvent, {
type: "sendEvent",
Expand Down Expand Up @@ -2351,6 +2366,8 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
this.checkForErrorListener();
this.direction = CallDirection.Outbound;

await this.initOpponentCrypto();

// XXX Find a better way to do this
this.client.callEventHandler.calls.set(this.callId, this);

Expand Down
24 changes: 20 additions & 4 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export type GroupCallEventHandlerMap = {

export enum GroupCallErrorCode {
NoUserMedia = "no_user_media",
UnknownDevice = "unknown_device"
UnknownDevice = "unknown_device",
PlaceCallFailed = "place_call_failed"
}

export class GroupCallError extends Error {
Expand Down Expand Up @@ -653,7 +654,7 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
return this.client.sendStateEvent(this.room.roomId, EventType.GroupCallMemberPrefix, content, localUserId);
}

public onMemberStateChanged = (event: MatrixEvent) => {
public onMemberStateChanged = async (event: MatrixEvent) => {
// The member events may be received for another room, which we will ignore.
if (event.getRoomId() !== this.room.roomId) {
return;
Expand Down Expand Up @@ -751,8 +752,23 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
const requestScreenshareFeed = opponentDevice.feeds.some(
(feed) => feed.purpose === SDPStreamMetadataPurpose.Screenshare);

// Safari can't send a MediaStream to multiple sources, so clone it
newCall.placeCallWithCallFeeds(this.getLocalFeeds().map(feed => feed.clone()), requestScreenshareFeed);
try {
// Safari can't send a MediaStream to multiple sources, so clone it
await newCall.placeCallWithCallFeeds(
this.getLocalFeeds().map(feed => feed.clone()),
requestScreenshareFeed,
);
} catch (e) {
logger.warn(`Failed to place call to ${member.userId}!`, e);
this.emit(
GroupCallEvent.Error,
new GroupCallError(
GroupCallErrorCode.PlaceCallFailed,
`Failed to place call to ${member.userId}.`,
),
);
return;
}

if (this.dataChannelsEnabled) {
newCall.createDataChannel("datachannel", this.dataChannelOptions);
Expand Down