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

Group call improvements #1985

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
4 changes: 4 additions & 0 deletions src/@types/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export enum EventType {
RoomKeyRequest = "m.room_key_request",
ForwardedRoomKey = "m.forwarded_room_key",
Dummy = "m.dummy",

// Group call events
GroupCallPrefix = "org.matrix.msc3401.call",
GroupCallMemberPrefix = "org.matrix.msc3401.call.member",
}

export enum RelationType {
Expand Down
38 changes: 22 additions & 16 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { SDPStreamMetadataPurpose } from "./callEventTypes";
import { createNewMatrixCall } from "./call";
import { ISendEventResponse } from "../@types/requests";
import { MatrixEvent } from "../models/event";

export const GROUP_CALL_ROOM_EVENT = "org.matrix.msc3401.call";
export const GROUP_CALL_MEMBER_EVENT = "org.matrix.msc3401.call.member";
import { EventType } from "../@types/event";

export enum GroupCallIntent {
Ring = "m.ring",
Expand Down Expand Up @@ -129,7 +127,7 @@ export class GroupCall extends EventEmitter {
this.sessionId = genCallID();

const roomState = this.room.currentState;
const memberStateEvents = roomState.getStateEvents(GROUP_CALL_MEMBER_EVENT);
const memberStateEvents = roomState.getStateEvents(EventType.GroupCallMemberPrefix);

logger.log("Processing initial members", memberStateEvents);

Expand All @@ -143,7 +141,7 @@ export class GroupCall extends EventEmitter {

await this.client.sendStateEvent(
this.room.roomId,
GROUP_CALL_ROOM_EVENT,
EventType.GroupCallPrefix,
{
"m.intent": this.intent,
"m.type": this.type,
Expand Down Expand Up @@ -226,8 +224,6 @@ export class GroupCall extends EventEmitter {
await this.initLocalCallFeed();
}

logger.log(`Sending member state event with current call.`);

this.sendEnteredMemberStateEvent();

this.activeSpeaker = null;
Expand All @@ -247,7 +243,7 @@ export class GroupCall extends EventEmitter {
// Set up participants for the members currently in the room.
// Other members will be picked up by the RoomState.members event.
const roomState = this.room.currentState;
const memberStateEvents = roomState.getStateEvents(GROUP_CALL_MEMBER_EVENT);
const memberStateEvents = roomState.getStateEvents(EventType.GroupCallMemberPrefix);

logger.log("Processing initial members");

Expand Down Expand Up @@ -307,11 +303,13 @@ export class GroupCall extends EventEmitter {
this.client.groupCallEventHandler.groupCalls.delete(this.room.roomId);

if (emitStateEvent) {
const existingStateEvent = this.room.currentState.getStateEvents(GROUP_CALL_ROOM_EVENT, this.groupCallId);
const existingStateEvent = this.room.currentState.getStateEvents(
EventType.GroupCallPrefix, this.groupCallId,
);

await this.client.sendStateEvent(
this.room.roomId,
GROUP_CALL_ROOM_EVENT,
EventType.GroupCallPrefix,
{
...existingStateEvent.getContent(),
["m.terminated"]: GroupCallTerminationReason.CallEnded,
Expand Down Expand Up @@ -502,11 +500,17 @@ export class GroupCall extends EventEmitter {
private async updateMemberCallState(memberCallState?: IGroupCallRoomMemberCallState): Promise<ISendEventResponse> {
const localUserId = this.client.getUserId();

const currentStateEvent = this.room.currentState.getStateEvents(GROUP_CALL_MEMBER_EVENT, localUserId);
const currentStateEvent = this.room.currentState.getStateEvents(EventType.GroupCallMemberPrefix, localUserId);
const memberStateEvent = currentStateEvent?.getContent<IGroupCallRoomMemberState>();

const calls = currentStateEvent?.getContent<IGroupCallRoomMemberState>()["m.calls"] || [];

const existingCallIndex = calls.findIndex((call) => call["m.call_id"] === this.groupCallId);
let calls: IGroupCallRoomMemberCallState[] = [];
let existingCallIndex: number;
if (memberCallState) {
calls = memberStateEvent["m.calls"] || [];
existingCallIndex = calls.findIndex((call) => call && call["m.call_id"] === this.groupCallId);
} else {
existingCallIndex = 0;
}

if (existingCallIndex === -1) {
calls.push(memberCallState);
Expand All @@ -516,9 +520,11 @@ export class GroupCall extends EventEmitter {
calls.splice(existingCallIndex, 1);
}

return this.client.sendStateEvent(this.room.roomId, GROUP_CALL_MEMBER_EVENT, {
const content = {
"m.calls": calls,
}, localUserId);
};
logger.log("Sending group call member state event", content);
return this.client.sendStateEvent(this.room.roomId, EventType.GroupCallMemberPrefix, content, localUserId);
}

public onMemberStateChanged = (event: MatrixEvent) => {
Expand Down
9 changes: 4 additions & 5 deletions src/webrtc/groupCallEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
import { MatrixEvent } from '../models/event';
import { MatrixClient } from '../client';
import {
GROUP_CALL_ROOM_EVENT,
GROUP_CALL_MEMBER_EVENT,
GroupCall,
GroupCallIntent,
GroupCallType,
Expand All @@ -27,6 +25,7 @@ import {
import { Room } from "../models/room";
import { RoomState } from "../models/room-state";
import { logger } from '../logger';
import { EventType } from "../@types/event";

export class GroupCallEventHandler {
public groupCalls = new Map<string, GroupCall>(); // roomId -> GroupCall
Expand All @@ -53,7 +52,7 @@ export class GroupCallEventHandler {
}

private createGroupCallForRoom(room: Room): GroupCall | undefined {
const callEvents = room.currentState.getStateEvents(GROUP_CALL_ROOM_EVENT);
const callEvents = room.currentState.getStateEvents(EventType.GroupCallPrefix);
const sortedCallEvents = callEvents.sort((a, b) => b.getTs() - a.getTs());

for (const callEvent of sortedCallEvents) {
Expand Down Expand Up @@ -125,7 +124,7 @@ export class GroupCallEventHandler {
private onRoomStateChanged = (event: MatrixEvent, state: RoomState): void => {
const eventType = event.getType();

if (eventType === GROUP_CALL_ROOM_EVENT) {
if (eventType === EventType.GroupCallPrefix) {
const groupCallId = event.getStateKey();
const content = event.getContent();

Expand All @@ -146,7 +145,7 @@ export class GroupCallEventHandler {
logger.warn(`Multiple group calls detected for room: ${
state.roomId}. Multiple group calls are currently unsupported.`);
}
} else if (eventType === GROUP_CALL_MEMBER_EVENT) {
} else if (eventType === EventType.GroupCallMemberPrefix) {
const groupCall = this.groupCalls.get(state.roomId);

if (!groupCall) {
Expand Down