element-call/src/ConferenceCallManager.js

453 lines
11 KiB
JavaScript
Raw Normal View History

2021-07-27 12:27:59 -07:00
/*
Copyright 2021 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-07-21 23:28:01 -07:00
import EventEmitter from "events";
import { ConferenceCallDebugger } from "./ConferenceCallDebugger";
2021-07-21 23:28:01 -07:00
const CONF_ROOM = "me.robertlong.conf";
const CONF_PARTICIPANT = "me.robertlong.conf.participant";
const PARTICIPANT_TIMEOUT = 1000 * 5;
2021-07-21 23:28:01 -07:00
function waitForSync(client) {
return new Promise((resolve, reject) => {
const onSync = (state) => {
if (state === "PREPARED") {
resolve();
client.removeListener("sync", onSync);
}
};
client.on("sync", onSync);
});
}
export class ConferenceCallManager extends EventEmitter {
static async restore(homeserverUrl) {
try {
const authStore = localStorage.getItem("matrix-auth-store");
if (authStore) {
const { user_id, device_id, access_token } = JSON.parse(authStore);
const client = matrixcs.createClient({
baseUrl: homeserverUrl,
accessToken: access_token,
userId: user_id,
deviceId: device_id,
});
const manager = new ConferenceCallManager(client);
await client.startClient();
await waitForSync(client);
return manager;
}
} catch (err) {
localStorage.removeItem("matrix-auth-store");
throw err;
}
}
static async login(homeserverUrl, username, password) {
try {
const registrationClient = matrixcs.createClient(homeserverUrl);
const { user_id, device_id, access_token } =
await registrationClient.loginWithPassword(username, password);
const client = matrixcs.createClient({
baseUrl: homeserverUrl,
accessToken: access_token,
userId: user_id,
deviceId: device_id,
});
localStorage.setItem(
"matrix-auth-store",
JSON.stringify({ user_id, device_id, access_token })
);
const manager = new ConferenceCallManager(client);
await client.startClient();
await waitForSync(client);
return manager;
} catch (err) {
localStorage.removeItem("matrix-auth-store");
throw err;
}
}
static async register(homeserverUrl, username, password) {
try {
const registrationClient = matrixcs.createClient(homeserverUrl);
const { user_id, device_id, access_token } =
await registrationClient.register(username, password, null, {
type: "m.login.dummy",
});
const client = matrixcs.createClient({
baseUrl: homeserverUrl,
accessToken: access_token,
userId: user_id,
deviceId: device_id,
});
localStorage.setItem(
"matrix-auth-store",
JSON.stringify({ user_id, device_id, access_token })
);
const manager = new ConferenceCallManager(client);
await client.startClient();
await waitForSync(client);
return manager;
} catch (err) {
localStorage.removeItem("matrix-auth-store");
throw err;
}
}
constructor(client) {
2021-07-21 23:28:01 -07:00
super();
this.client = client;
this.joined = false;
2021-07-26 16:01:13 -07:00
this.room = null;
2021-07-30 16:55:25 -07:00
const localUserId = client.getUserId();
2021-07-21 23:28:01 -07:00
this.localParticipant = {
local: true,
2021-07-30 16:55:25 -07:00
userId: localUserId,
stream: null,
2021-07-21 23:28:01 -07:00
call: null,
muted: true,
};
this.participants = [this.localParticipant];
2021-07-27 16:27:04 -07:00
this.pendingCalls = [];
2021-07-22 16:41:57 -07:00
this.client.on("RoomState.members", this._onMemberChanged);
this.client.on("Call.incoming", this._onIncomingCall);
this.callDebugger = new ConferenceCallDebugger(this);
}
2021-07-22 16:41:57 -07:00
2021-07-30 16:55:25 -07:00
setRoom(roomId) {
this.roomId = roomId;
2021-07-26 16:01:13 -07:00
this.room = this.client.getRoom(this.roomId);
2021-07-30 16:55:25 -07:00
}
async join() {
const mediaStream = await this.client.getLocalVideoStream();
this.localParticipant.stream = mediaStream;
2021-07-30 16:55:25 -07:00
this.joined = true;
this.emit("debugstate", this.client.getUserId(), null, "you");
2021-07-26 16:01:13 -07:00
2021-07-21 23:28:01 -07:00
const activeConf = this.room.currentState
.getStateEvents(CONF_ROOM, "")
?.getContent()?.active;
if (!activeConf) {
this.client.sendStateEvent(this.roomId, CONF_ROOM, { active: true }, "");
}
const roomMemberIds = this.room.getMembers().map(({ userId }) => userId);
roomMemberIds.forEach((userId) => {
this._processMember(userId);
});
2021-07-23 14:52:42 -07:00
2021-07-27 16:27:04 -07:00
for (const { call, onHangup, onReplaced } of this.pendingCalls) {
2021-07-30 16:55:25 -07:00
if (call.roomId !== this.roomId) {
2021-07-30 13:58:15 -07:00
continue;
}
2021-07-27 16:27:04 -07:00
call.removeListener("hangup", onHangup);
call.removeListener("replaced", onReplaced);
const userId = call.opponentMember.userId;
this._addCall(call, userId);
call.answer();
this.emit("call", call);
2021-07-27 16:27:04 -07:00
}
this.pendingCalls = [];
2021-07-21 23:28:01 -07:00
this._updateParticipantState();
}
_updateParticipantState = () => {
const userId = this.client.getUserId();
const currentMemberState = this.room.currentState.getStateEvents(
"m.room.member",
userId
);
this.client.sendStateEvent(
this.roomId,
"m.room.member",
{
...currentMemberState.getContent(),
[CONF_PARTICIPANT]: new Date().getTime(),
},
userId
);
this._participantStateTimeout = setTimeout(
this._updateParticipantState,
PARTICIPANT_TIMEOUT
);
};
_onMemberChanged = (_event, _state, member) => {
2021-07-30 13:58:15 -07:00
if (member.roomId !== this.roomId) {
return;
}
2021-07-21 23:28:01 -07:00
this._processMember(member.userId);
};
_processMember(userId) {
const localUserId = this.client.getUserId();
if (userId === localUserId) {
return;
}
// Don't process members until we've joined
if (!this.joined) {
2021-07-21 23:28:01 -07:00
return;
}
const participant = this.participants.find((p) => p.userId === userId);
if (participant) {
// Member has already been processed
return;
}
2021-07-21 23:28:01 -07:00
const memberStateEvent = this.room.currentState.getStateEvents(
"m.room.member",
userId
);
const participantTimeout = memberStateEvent.getContent()[CONF_PARTICIPANT];
if (
2021-07-30 13:49:22 -07:00
typeof participantTimeout !== "number" ||
new Date().getTime() - participantTimeout > PARTICIPANT_TIMEOUT
2021-07-21 23:28:01 -07:00
) {
// Member is inactive so don't call them.
this.emit("debugstate", userId, null, "inactive");
2021-07-28 16:14:38 -07:00
return;
}
// Only initiate a call with a user who has a userId that is lexicographically
// less than your own. Otherwise, that user will call you.
if (userId < localUserId) {
this.emit("debugstate", userId, null, "waiting for invite");
2021-07-21 23:28:01 -07:00
return;
}
const call = this.client.createCall(this.roomId, userId);
this._addCall(call, userId);
2021-08-03 15:05:29 -07:00
call.placeVideoCall().then(() => {
this.emit("call", call);
2021-08-03 15:05:29 -07:00
});
2021-07-21 23:28:01 -07:00
}
_onIncomingCall = (call) => {
if (!this.joined) {
2021-07-27 16:27:04 -07:00
const onHangup = (call) => {
const index = this.pendingCalls.findIndex((p) => p.call === call);
if (index !== -1) {
this.pendingCalls.splice(index, 1);
}
};
const onReplaced = (call, newCall) => {
const index = this.pendingCalls.findIndex((p) => p.call === call);
if (index !== -1) {
this.pendingCalls.splice(index, 1, {
call: newCall,
onHangup: () => onHangup(newCall),
onReplaced: (nextCall) => onReplaced(newCall, nextCall),
});
}
};
this.pendingCalls.push({
call,
onHangup: () => onHangup(call),
onReplaced: (newCall) => onReplaced(call, newCall),
});
call.on("hangup", onHangup);
call.on("replaced", onReplaced);
return;
}
2021-07-30 13:58:15 -07:00
if (call.roomId !== this.roomId) {
return;
}
2021-07-28 18:09:04 -07:00
const userId = call.opponentMember.userId;
this._addCall(call, userId);
call.answer();
this.emit("call", call);
2021-07-21 23:28:01 -07:00
};
_addCall(call, userId) {
2021-07-27 16:27:04 -07:00
if (call.state === "ended") {
return;
}
2021-07-22 16:41:57 -07:00
const existingCall = this.participants.find(
2021-07-27 13:26:18 -07:00
(p) => !p.local && p.call && p.call.callId === call.callId
2021-07-22 16:41:57 -07:00
);
if (existingCall) {
return;
}
2021-07-21 23:28:01 -07:00
this.participants.push({
2021-07-22 16:41:57 -07:00
userId,
stream: null,
2021-07-21 23:28:01 -07:00
call,
});
2021-07-30 16:55:25 -07:00
call.on("state", (state) =>
this.emit("debugstate", userId, call.callId, state)
2021-07-30 16:55:25 -07:00
);
2021-07-21 23:28:01 -07:00
call.on("feeds_changed", () => this._onCallFeedsChanged(call));
call.on("hangup", () => this._onCallHangup(call));
2021-07-22 16:41:57 -07:00
const onReplaced = (newCall) => {
this._onCallReplaced(call, newCall);
call.removeListener("replaced", onReplaced);
};
call.on("replaced", onReplaced);
2021-07-21 23:28:01 -07:00
this._onCallFeedsChanged(call);
this.emit("participants_changed");
}
_onCallFeedsChanged = (call) => {
for (const participant of this.participants) {
if (participant.local || participant.call !== call) {
continue;
}
2021-07-21 23:28:01 -07:00
const remoteFeeds = call.getRemoteFeeds();
2021-07-21 23:28:01 -07:00
if (
remoteFeeds.length > 0 &&
participant.stream !== remoteFeeds[0].stream
) {
participant.stream = remoteFeeds[0].stream;
this.emit("participants_changed");
}
2021-07-21 23:28:01 -07:00
}
};
_onCallHangup = (call) => {
const participantIndex = this.participants.findIndex(
2021-07-27 13:26:18 -07:00
(p) => !p.local && p.call === call
2021-07-21 23:28:01 -07:00
);
2021-07-30 16:55:25 -07:00
if (call.hangupReason === "replaced") {
return;
}
2021-07-21 23:28:01 -07:00
if (participantIndex === -1) {
return;
}
this.participants.splice(participantIndex, 1);
this.emit("participants_changed");
};
_onCallReplaced = (call, newCall) => {
2021-07-27 13:26:18 -07:00
const remoteParticipant = this.participants.find(
(p) => !p.local && p.call === call
);
2021-07-21 23:28:01 -07:00
remoteParticipant.call = newCall;
this.emit("call", newCall);
2021-07-21 23:28:01 -07:00
newCall.on("feeds_changed", () => this._onCallFeedsChanged(newCall));
newCall.on("hangup", () => this._onCallHangup(newCall));
newCall.on("replaced", (nextCall) =>
this._onCallReplaced(newCall, nextCall)
);
this._onCallFeedsChanged(newCall);
this.emit("participants_changed");
};
2021-07-27 13:26:18 -07:00
leaveCall() {
2021-07-30 13:49:22 -07:00
if (!this.joined) {
return;
}
2021-07-27 15:49:00 -07:00
const userId = this.client.getUserId();
const currentMemberState = this.room.currentState.getStateEvents(
"m.room.member",
userId
);
this.client.sendStateEvent(
this.roomId,
"m.room.member",
{
...currentMemberState.getContent(),
[CONF_PARTICIPANT]: null,
},
userId
);
2021-07-27 13:26:18 -07:00
for (const participant of this.participants) {
if (!participant.local && participant.call) {
participant.call.hangup("user_hangup", false);
}
}
2021-08-05 12:52:54 -07:00
this.client.stopLocalMediaStream();
2021-07-27 13:26:18 -07:00
this.joined = false;
this.participants = [this.localParticipant];
this.localParticipant.stream = null;
2021-07-27 13:26:18 -07:00
this.localParticipant.call = null;
2021-07-27 15:49:00 -07:00
2021-07-27 13:26:18 -07:00
this.emit("participants_changed");
}
2021-07-28 16:14:38 -07:00
2021-07-30 14:32:52 -07:00
logout() {
localStorage.removeItem("matrix-auth-store");
}
2021-07-21 23:28:01 -07:00
}