2026-09-13 14:14:38 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
|
value: true
|
|
|
|
|
});
|
|
|
|
|
exports.OtherUserSpeakingError = exports.GroupCallUnknownDeviceError = exports.GroupCallType = exports.GroupCallTerminationReason = exports.GroupCallStatsReportEvent = exports.GroupCallState = exports.GroupCallIntent = exports.GroupCallEvent = exports.GroupCallErrorCode = exports.GroupCallError = exports.GroupCall = void 0;
|
|
|
|
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
|
|
|
var _typedEventEmitter = require("../models/typed-event-emitter");
|
|
|
|
|
var _callFeed = require("./callFeed");
|
|
|
|
|
var _call = require("./call");
|
|
|
|
|
var _roomState = require("../models/room-state");
|
|
|
|
|
var _logger = require("../logger");
|
|
|
|
|
var _ReEmitter = require("../ReEmitter");
|
|
|
|
|
var _callEventTypes = require("./callEventTypes");
|
|
|
|
|
var _event = require("../@types/event");
|
|
|
|
|
var _callEventHandler = require("./callEventHandler");
|
|
|
|
|
var _groupCallEventHandler = require("./groupCallEventHandler");
|
|
|
|
|
var _utils = require("../utils");
|
|
|
|
|
var _groupCallStats = require("./stats/groupCallStats");
|
|
|
|
|
var _statsReport = require("./stats/statsReport");
|
|
|
|
|
var _summaryStatsReportGatherer = require("./stats/summaryStatsReportGatherer");
|
|
|
|
|
var _callFeedStatsReporter = require("./stats/callFeedStatsReporter");
|
|
|
|
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
|
|
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
|
|
|
let GroupCallIntent = /*#__PURE__*/function (GroupCallIntent) {
|
2026-09-12 23:57:45 -04:00
|
|
|
GroupCallIntent["Ring"] = "m.ring";
|
|
|
|
|
GroupCallIntent["Prompt"] = "m.prompt";
|
|
|
|
|
GroupCallIntent["Room"] = "m.room";
|
|
|
|
|
return GroupCallIntent;
|
|
|
|
|
}({});
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallIntent = GroupCallIntent;
|
|
|
|
|
let GroupCallType = /*#__PURE__*/function (GroupCallType) {
|
2026-09-12 23:57:45 -04:00
|
|
|
GroupCallType["Video"] = "m.video";
|
|
|
|
|
GroupCallType["Voice"] = "m.voice";
|
|
|
|
|
return GroupCallType;
|
|
|
|
|
}({});
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallType = GroupCallType;
|
|
|
|
|
let GroupCallTerminationReason = /*#__PURE__*/function (GroupCallTerminationReason) {
|
2026-09-12 23:57:45 -04:00
|
|
|
GroupCallTerminationReason["CallEnded"] = "call_ended";
|
|
|
|
|
return GroupCallTerminationReason;
|
|
|
|
|
}({});
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallTerminationReason = GroupCallTerminationReason;
|
2026-09-12 23:57:45 -04:00
|
|
|
/**
|
|
|
|
|
* Because event names are just strings, they do need
|
|
|
|
|
* to be unique over all event types of event emitter.
|
|
|
|
|
* Some objects could emit more then one set of events.
|
|
|
|
|
*/
|
2026-09-13 14:14:38 -04:00
|
|
|
let GroupCallEvent = /*#__PURE__*/function (GroupCallEvent) {
|
2026-09-12 23:57:45 -04:00
|
|
|
GroupCallEvent["GroupCallStateChanged"] = "group_call_state_changed";
|
|
|
|
|
GroupCallEvent["ActiveSpeakerChanged"] = "active_speaker_changed";
|
|
|
|
|
GroupCallEvent["CallsChanged"] = "calls_changed";
|
|
|
|
|
GroupCallEvent["UserMediaFeedsChanged"] = "user_media_feeds_changed";
|
|
|
|
|
GroupCallEvent["ScreenshareFeedsChanged"] = "screenshare_feeds_changed";
|
|
|
|
|
GroupCallEvent["LocalScreenshareStateChanged"] = "local_screenshare_state_changed";
|
|
|
|
|
GroupCallEvent["LocalMuteStateChanged"] = "local_mute_state_changed";
|
|
|
|
|
GroupCallEvent["ParticipantsChanged"] = "participants_changed";
|
|
|
|
|
GroupCallEvent["Error"] = "group_call_error";
|
|
|
|
|
return GroupCallEvent;
|
|
|
|
|
}({});
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallEvent = GroupCallEvent;
|
|
|
|
|
let GroupCallStatsReportEvent = /*#__PURE__*/function (GroupCallStatsReportEvent) {
|
2026-09-12 23:57:45 -04:00
|
|
|
GroupCallStatsReportEvent["ConnectionStats"] = "GroupCall.connection_stats";
|
|
|
|
|
GroupCallStatsReportEvent["ByteSentStats"] = "GroupCall.byte_sent_stats";
|
|
|
|
|
GroupCallStatsReportEvent["SummaryStats"] = "GroupCall.summary_stats";
|
|
|
|
|
GroupCallStatsReportEvent["CallFeedStats"] = "GroupCall.call_feed_stats";
|
|
|
|
|
return GroupCallStatsReportEvent;
|
|
|
|
|
}({});
|
|
|
|
|
/**
|
|
|
|
|
* The final report-events that get consumed by client.
|
|
|
|
|
*/
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallStatsReportEvent = GroupCallStatsReportEvent;
|
|
|
|
|
let GroupCallErrorCode = /*#__PURE__*/function (GroupCallErrorCode) {
|
2026-09-12 23:57:45 -04:00
|
|
|
GroupCallErrorCode["NoUserMedia"] = "no_user_media";
|
|
|
|
|
GroupCallErrorCode["UnknownDevice"] = "unknown_device";
|
|
|
|
|
GroupCallErrorCode["PlaceCallFailed"] = "place_call_failed";
|
|
|
|
|
return GroupCallErrorCode;
|
|
|
|
|
}({});
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallErrorCode = GroupCallErrorCode;
|
|
|
|
|
class GroupCallError extends Error {
|
2026-09-12 23:57:45 -04:00
|
|
|
constructor(code, msg, err) {
|
|
|
|
|
// Still don't think there's any way to have proper nested errors
|
|
|
|
|
if (err) {
|
|
|
|
|
super(msg + ": " + err);
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "code", void 0);
|
2026-09-12 23:57:45 -04:00
|
|
|
} else {
|
|
|
|
|
super(msg);
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "code", void 0);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
this.code = code;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallError = GroupCallError;
|
|
|
|
|
class GroupCallUnknownDeviceError extends GroupCallError {
|
2026-09-12 23:57:45 -04:00
|
|
|
constructor(userId) {
|
|
|
|
|
super(GroupCallErrorCode.UnknownDevice, "No device found for " + userId);
|
|
|
|
|
this.userId = userId;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallUnknownDeviceError = GroupCallUnknownDeviceError;
|
|
|
|
|
class OtherUserSpeakingError extends Error {
|
2026-09-12 23:57:45 -04:00
|
|
|
constructor() {
|
|
|
|
|
super("Cannot unmute: another user is speaking");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.OtherUserSpeakingError = OtherUserSpeakingError;
|
|
|
|
|
let GroupCallState = /*#__PURE__*/function (GroupCallState) {
|
2026-09-12 23:57:45 -04:00
|
|
|
GroupCallState["LocalCallFeedUninitialized"] = "local_call_feed_uninitialized";
|
|
|
|
|
GroupCallState["InitializingLocalCallFeed"] = "initializing_local_call_feed";
|
|
|
|
|
GroupCallState["LocalCallFeedInitialized"] = "local_call_feed_initialized";
|
|
|
|
|
GroupCallState["Entered"] = "entered";
|
|
|
|
|
GroupCallState["Ended"] = "ended";
|
|
|
|
|
return GroupCallState;
|
|
|
|
|
}({});
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCallState = GroupCallState;
|
2026-09-12 23:57:45 -04:00
|
|
|
const DEVICE_TIMEOUT = 1000 * 60 * 60; // 1 hour
|
|
|
|
|
|
|
|
|
|
function getCallUserId(call) {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _call$getOpponentMemb;
|
|
|
|
|
return ((_call$getOpponentMemb = call.getOpponentMember()) === null || _call$getOpponentMemb === void 0 ? void 0 : _call$getOpponentMemb.userId) || call.invitee || null;
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
class GroupCall extends _typedEventEmitter.TypedEventEmitter {
|
|
|
|
|
constructor(client, room, type, isPtt, intent, groupCallId, dataChannelsEnabled, dataChannelOptions, isCallWithoutVideoAndAudio) {
|
|
|
|
|
var _room$currentState$ge, _room$currentState$ge2;
|
2026-09-12 23:57:45 -04:00
|
|
|
super();
|
2026-09-13 14:14:38 -04:00
|
|
|
this.client = client;
|
|
|
|
|
this.room = room;
|
|
|
|
|
this.type = type;
|
|
|
|
|
this.isPtt = isPtt;
|
|
|
|
|
this.intent = intent;
|
|
|
|
|
this.dataChannelsEnabled = dataChannelsEnabled;
|
|
|
|
|
this.dataChannelOptions = dataChannelOptions;
|
2026-09-12 23:57:45 -04:00
|
|
|
// Config
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "activeSpeakerInterval", 1000);
|
|
|
|
|
(0, _defineProperty2.default)(this, "retryCallInterval", 5000);
|
|
|
|
|
(0, _defineProperty2.default)(this, "participantTimeout", 1000 * 15);
|
|
|
|
|
(0, _defineProperty2.default)(this, "pttMaxTransmitTime", 1000 * 20);
|
|
|
|
|
(0, _defineProperty2.default)(this, "activeSpeaker", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "localCallFeed", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "localScreenshareFeed", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "localDesktopCapturerSourceId", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "userMediaFeeds", []);
|
|
|
|
|
(0, _defineProperty2.default)(this, "screenshareFeeds", []);
|
|
|
|
|
(0, _defineProperty2.default)(this, "groupCallId", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "allowCallWithoutVideoAndAudio", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "calls", new Map());
|
2026-09-12 23:57:45 -04:00
|
|
|
// user_id -> device_id -> MatrixCall
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "callHandlers", new Map());
|
2026-09-12 23:57:45 -04:00
|
|
|
// user_id -> device_id -> ICallHandlers
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "activeSpeakerLoopInterval", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "retryCallLoopInterval", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "retryCallCounts", new Map());
|
2026-09-12 23:57:45 -04:00
|
|
|
// user_id -> device_id -> count
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "reEmitter", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "transmitTimer", null);
|
|
|
|
|
(0, _defineProperty2.default)(this, "participantsExpirationTimer", null);
|
|
|
|
|
(0, _defineProperty2.default)(this, "resendMemberStateTimer", null);
|
|
|
|
|
(0, _defineProperty2.default)(this, "initWithAudioMuted", false);
|
|
|
|
|
(0, _defineProperty2.default)(this, "initWithVideoMuted", false);
|
|
|
|
|
(0, _defineProperty2.default)(this, "initCallFeedPromise", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "stats", void 0);
|
2026-09-12 23:57:45 -04:00
|
|
|
/**
|
|
|
|
|
* Configure default webrtc stats collection interval in ms
|
|
|
|
|
* Disable collecting webrtc stats by setting interval to 0
|
|
|
|
|
*/
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "statsCollectIntervalTime", 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "onConnectionStats", report => {
|
2026-09-12 23:57:45 -04:00
|
|
|
// Final emit of the summary event, to be consumed by the client
|
|
|
|
|
this.emit(GroupCallStatsReportEvent.ConnectionStats, {
|
|
|
|
|
report
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onByteSentStats", report => {
|
2026-09-12 23:57:45 -04:00
|
|
|
// Final emit of the summary event, to be consumed by the client
|
|
|
|
|
this.emit(GroupCallStatsReportEvent.ByteSentStats, {
|
|
|
|
|
report
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onSummaryStats", report => {
|
|
|
|
|
_summaryStatsReportGatherer.SummaryStatsReportGatherer.extendSummaryReport(report, this.participants);
|
2026-09-12 23:57:45 -04:00
|
|
|
// Final emit of the summary event, to be consumed by the client
|
|
|
|
|
this.emit(GroupCallStatsReportEvent.SummaryStats, {
|
|
|
|
|
report
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onCallFeedReport", report => {
|
2026-09-12 23:57:45 -04:00
|
|
|
if (this.localCallFeed) {
|
2026-09-13 14:14:38 -04:00
|
|
|
report = _callFeedStatsReporter.CallFeedStatsReporter.expandCallFeedReport(report, [this.localCallFeed], "from-local-feed");
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
const callFeeds = [];
|
|
|
|
|
this.forEachCall(call => {
|
|
|
|
|
if (call.callId === report.callId) {
|
|
|
|
|
call.getFeeds().forEach(f => callFeeds.push(f));
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
report = _callFeedStatsReporter.CallFeedStatsReporter.expandCallFeedReport(report, callFeeds, "from-call-feed");
|
2026-09-12 23:57:45 -04:00
|
|
|
this.emit(GroupCallStatsReportEvent.CallFeedStats, {
|
|
|
|
|
report
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "_state", GroupCallState.LocalCallFeedUninitialized);
|
|
|
|
|
(0, _defineProperty2.default)(this, "_participants", new Map());
|
|
|
|
|
(0, _defineProperty2.default)(this, "_creationTs", null);
|
|
|
|
|
(0, _defineProperty2.default)(this, "_enteredViaAnotherSession", false);
|
2026-09-12 23:57:45 -04:00
|
|
|
/*
|
|
|
|
|
* Call Setup
|
|
|
|
|
*
|
|
|
|
|
* There are two different paths for calls to be created:
|
|
|
|
|
* 1. Incoming calls triggered by the Call.incoming event.
|
|
|
|
|
* 2. Outgoing calls to the initial members of a room or new members
|
|
|
|
|
* as they are observed by the RoomState.members event.
|
|
|
|
|
*/
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onIncomingCall", newCall => {
|
|
|
|
|
var _newCall$getOpponentM, _this$calls$get;
|
2026-09-12 23:57:45 -04:00
|
|
|
// The incoming calls may be for another room, which we will ignore.
|
|
|
|
|
if (newCall.roomId !== this.room.roomId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
if (newCall.state !== _call.CallState.Ringing) {
|
|
|
|
|
_logger.logger.warn(`GroupCall ${this.groupCallId} onIncomingCall() incoming call no longer in ringing state - ignoring`);
|
2026-09-12 23:57:45 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!newCall.groupCallId || newCall.groupCallId !== this.groupCallId) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} onIncomingCall() ignored because it doesn't match the current group call`);
|
2026-09-12 23:57:45 -04:00
|
|
|
newCall.reject();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
const opponentUserId = (_newCall$getOpponentM = newCall.getOpponentMember()) === null || _newCall$getOpponentM === void 0 ? void 0 : _newCall$getOpponentM.userId;
|
2026-09-12 23:57:45 -04:00
|
|
|
if (opponentUserId === undefined) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.warn(`GroupCall ${this.groupCallId} onIncomingCall() incoming call with no member - ignoring`);
|
2026-09-12 23:57:45 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
const deviceMap = (_this$calls$get = this.calls.get(opponentUserId)) !== null && _this$calls$get !== void 0 ? _this$calls$get : new Map();
|
2026-09-12 23:57:45 -04:00
|
|
|
const prevCall = deviceMap.get(newCall.getOpponentDeviceId());
|
2026-09-13 14:14:38 -04:00
|
|
|
if ((prevCall === null || prevCall === void 0 ? void 0 : prevCall.callId) === newCall.callId) return;
|
|
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} onIncomingCall() incoming call (userId=${opponentUserId}, callId=${newCall.callId})`);
|
|
|
|
|
if (prevCall) prevCall.hangup(_call.CallErrorCode.Replaced, false);
|
2026-09-12 23:57:45 -04:00
|
|
|
// We must do this before we start initialising / answering the call as we
|
|
|
|
|
// need to know it is the active call for this user+deviceId and to not ignore
|
|
|
|
|
// events from it.
|
|
|
|
|
deviceMap.set(newCall.getOpponentDeviceId(), newCall);
|
|
|
|
|
this.calls.set(opponentUserId, deviceMap);
|
|
|
|
|
this.initCall(newCall);
|
|
|
|
|
const feeds = this.getLocalFeeds().map(feed => feed.clone());
|
|
|
|
|
if (!this.callExpected(newCall)) {
|
|
|
|
|
// Disable our tracks for users not explicitly participating in the
|
|
|
|
|
// call but trying to receive the feeds
|
|
|
|
|
for (const feed of feeds) {
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _call.setTracksEnabled)(feed.stream.getAudioTracks(), false);
|
|
|
|
|
(0, _call.setTracksEnabled)(feed.stream.getVideoTracks(), false);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
newCall.answerWithCallFeeds(feeds);
|
|
|
|
|
this.emit(GroupCallEvent.CallsChanged, this.calls);
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onRetryCallLoop", () => {
|
2026-09-12 23:57:45 -04:00
|
|
|
let needsRetry = false;
|
|
|
|
|
for (const [{
|
|
|
|
|
userId
|
|
|
|
|
}, participantMap] of this.participants) {
|
|
|
|
|
const callMap = this.calls.get(userId);
|
|
|
|
|
let retriesMap = this.retryCallCounts.get(userId);
|
|
|
|
|
for (const [deviceId, participant] of participantMap) {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _retriesMap$get, _retriesMap;
|
|
|
|
|
const call = callMap === null || callMap === void 0 ? void 0 : callMap.get(deviceId);
|
|
|
|
|
const retries = (_retriesMap$get = (_retriesMap = retriesMap) === null || _retriesMap === void 0 ? void 0 : _retriesMap.get(deviceId)) !== null && _retriesMap$get !== void 0 ? _retriesMap$get : 0;
|
|
|
|
|
if ((call === null || call === void 0 ? void 0 : call.getOpponentSessionId()) !== participant.sessionId && this.wantsOutgoingCall(userId, deviceId) && retries < 3) {
|
2026-09-12 23:57:45 -04:00
|
|
|
if (retriesMap === undefined) {
|
|
|
|
|
retriesMap = new Map();
|
|
|
|
|
this.retryCallCounts.set(userId, retriesMap);
|
|
|
|
|
}
|
|
|
|
|
retriesMap.set(deviceId, retries + 1);
|
|
|
|
|
needsRetry = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (needsRetry) this.placeOutgoingCalls();
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onCallFeedsChanged", call => {
|
2026-09-12 23:57:45 -04:00
|
|
|
const opponentMemberId = getCallUserId(call);
|
|
|
|
|
const opponentDeviceId = call.getOpponentDeviceId();
|
|
|
|
|
if (!opponentMemberId) {
|
|
|
|
|
throw new Error("Cannot change call feeds without user id");
|
|
|
|
|
}
|
|
|
|
|
const currentUserMediaFeed = this.getUserMediaFeed(opponentMemberId, opponentDeviceId);
|
|
|
|
|
const remoteUsermediaFeed = call.remoteUsermediaFeed;
|
|
|
|
|
const remoteFeedChanged = remoteUsermediaFeed !== currentUserMediaFeed;
|
|
|
|
|
const deviceMap = this.calls.get(opponentMemberId);
|
2026-09-13 14:14:38 -04:00
|
|
|
const currentCallForUserDevice = deviceMap === null || deviceMap === void 0 ? void 0 : deviceMap.get(opponentDeviceId);
|
|
|
|
|
if ((currentCallForUserDevice === null || currentCallForUserDevice === void 0 ? void 0 : currentCallForUserDevice.callId) !== call.callId) {
|
2026-09-12 23:57:45 -04:00
|
|
|
// the call in question is not the current call for this user/deviceId
|
|
|
|
|
// so ignore feed events from it otherwise we'll remove our real feeds
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (remoteFeedChanged) {
|
|
|
|
|
if (!currentUserMediaFeed && remoteUsermediaFeed) {
|
|
|
|
|
this.addUserMediaFeed(remoteUsermediaFeed);
|
|
|
|
|
} else if (currentUserMediaFeed && remoteUsermediaFeed) {
|
|
|
|
|
this.replaceUserMediaFeed(currentUserMediaFeed, remoteUsermediaFeed);
|
|
|
|
|
} else if (currentUserMediaFeed && !remoteUsermediaFeed) {
|
|
|
|
|
this.removeUserMediaFeed(currentUserMediaFeed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const currentScreenshareFeed = this.getScreenshareFeed(opponentMemberId, opponentDeviceId);
|
|
|
|
|
const remoteScreensharingFeed = call.remoteScreensharingFeed;
|
|
|
|
|
const remoteScreenshareFeedChanged = remoteScreensharingFeed !== currentScreenshareFeed;
|
|
|
|
|
if (remoteScreenshareFeedChanged) {
|
|
|
|
|
if (!currentScreenshareFeed && remoteScreensharingFeed) {
|
|
|
|
|
this.addScreenshareFeed(remoteScreensharingFeed);
|
|
|
|
|
} else if (currentScreenshareFeed && remoteScreensharingFeed) {
|
|
|
|
|
this.replaceScreenshareFeed(currentScreenshareFeed, remoteScreensharingFeed);
|
|
|
|
|
} else if (currentScreenshareFeed && !remoteScreensharingFeed) {
|
|
|
|
|
this.removeScreenshareFeed(currentScreenshareFeed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onCallStateChanged", (call, state, _oldState) => {
|
|
|
|
|
var _call$getOpponentMemb2;
|
|
|
|
|
if (state === _call.CallState.Ended) return;
|
2026-09-12 23:57:45 -04:00
|
|
|
const audioMuted = this.localCallFeed.isAudioMuted();
|
|
|
|
|
if (call.localUsermediaStream && call.isMicrophoneMuted() !== audioMuted) {
|
|
|
|
|
call.setMicrophoneMuted(audioMuted);
|
|
|
|
|
}
|
|
|
|
|
const videoMuted = this.localCallFeed.isVideoMuted();
|
|
|
|
|
if (call.localUsermediaStream && call.isLocalVideoMuted() !== videoMuted) {
|
|
|
|
|
call.setLocalVideoMuted(videoMuted);
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
const opponentUserId = (_call$getOpponentMemb2 = call.getOpponentMember()) === null || _call$getOpponentMemb2 === void 0 ? void 0 : _call$getOpponentMemb2.userId;
|
|
|
|
|
if (state === _call.CallState.Connected && opponentUserId) {
|
2026-09-12 23:57:45 -04:00
|
|
|
const retriesMap = this.retryCallCounts.get(opponentUserId);
|
2026-09-13 14:14:38 -04:00
|
|
|
retriesMap === null || retriesMap === void 0 ? void 0 : retriesMap.delete(call.getOpponentDeviceId());
|
|
|
|
|
if ((retriesMap === null || retriesMap === void 0 ? void 0 : retriesMap.size) === 0) this.retryCallCounts.delete(opponentUserId);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onCallHangup", call => {
|
|
|
|
|
var _call$getOpponentMemb3, _call$getOpponentMemb4;
|
|
|
|
|
if (call.hangupReason === _call.CallErrorCode.Replaced) return;
|
|
|
|
|
const opponentUserId = (_call$getOpponentMemb3 = (_call$getOpponentMemb4 = call.getOpponentMember()) === null || _call$getOpponentMemb4 === void 0 ? void 0 : _call$getOpponentMemb4.userId) !== null && _call$getOpponentMemb3 !== void 0 ? _call$getOpponentMemb3 : this.room.getMember(call.invitee).userId;
|
2026-09-12 23:57:45 -04:00
|
|
|
const deviceMap = this.calls.get(opponentUserId);
|
|
|
|
|
|
|
|
|
|
// Sanity check that this call is in fact in the map
|
2026-09-13 14:14:38 -04:00
|
|
|
if ((deviceMap === null || deviceMap === void 0 ? void 0 : deviceMap.get(call.getOpponentDeviceId())) === call) {
|
2026-09-12 23:57:45 -04:00
|
|
|
this.disposeCall(call, call.hangupReason);
|
|
|
|
|
deviceMap.delete(call.getOpponentDeviceId());
|
|
|
|
|
if (deviceMap.size === 0) this.calls.delete(opponentUserId);
|
|
|
|
|
this.emit(GroupCallEvent.CallsChanged, this.calls);
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onCallReplaced", (prevCall, newCall) => {
|
2026-09-12 23:57:45 -04:00
|
|
|
const opponentUserId = prevCall.getOpponentMember().userId;
|
|
|
|
|
let deviceMap = this.calls.get(opponentUserId);
|
|
|
|
|
if (deviceMap === undefined) {
|
|
|
|
|
deviceMap = new Map();
|
|
|
|
|
this.calls.set(opponentUserId, deviceMap);
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
prevCall.hangup(_call.CallErrorCode.Replaced, false);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.initCall(newCall);
|
|
|
|
|
deviceMap.set(prevCall.getOpponentDeviceId(), newCall);
|
|
|
|
|
this.emit(GroupCallEvent.CallsChanged, this.calls);
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onActiveSpeakerLoop", () => {
|
2026-09-12 23:57:45 -04:00
|
|
|
let topAvg = undefined;
|
|
|
|
|
let nextActiveSpeaker = undefined;
|
|
|
|
|
for (const callFeed of this.userMediaFeeds) {
|
|
|
|
|
if (callFeed.isLocal() && this.userMediaFeeds.length > 1) continue;
|
2026-09-13 14:14:38 -04:00
|
|
|
const total = callFeed.speakingVolumeSamples.reduce((acc, volume) => acc + Math.max(volume, _callFeed.SPEAKING_THRESHOLD));
|
2026-09-12 23:57:45 -04:00
|
|
|
const avg = total / callFeed.speakingVolumeSamples.length;
|
|
|
|
|
if (!topAvg || avg > topAvg) {
|
|
|
|
|
topAvg = avg;
|
|
|
|
|
nextActiveSpeaker = callFeed;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
if (nextActiveSpeaker && this.activeSpeaker !== nextActiveSpeaker && topAvg && topAvg > _callFeed.SPEAKING_THRESHOLD) {
|
2026-09-12 23:57:45 -04:00
|
|
|
this.activeSpeaker = nextActiveSpeaker;
|
|
|
|
|
this.emit(GroupCallEvent.ActiveSpeakerChanged, this.activeSpeaker);
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onRoomState", () => this.updateParticipants());
|
|
|
|
|
(0, _defineProperty2.default)(this, "onParticipantsChanged", () => {
|
2026-09-12 23:57:45 -04:00
|
|
|
// Re-run setTracksEnabled on all calls, so that participants that just
|
|
|
|
|
// left get denied access to our media, and participants that just
|
|
|
|
|
// joined get granted access
|
|
|
|
|
this.forEachCall(call => {
|
|
|
|
|
const expected = this.callExpected(call);
|
|
|
|
|
for (const feed of call.getLocalFeeds()) {
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _call.setTracksEnabled)(feed.stream.getAudioTracks(), !feed.isAudioMuted() && expected);
|
|
|
|
|
(0, _call.setTracksEnabled)(feed.stream.getVideoTracks(), !feed.isVideoMuted() && expected);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
if (this.state === GroupCallState.Entered) this.placeOutgoingCalls();
|
2026-09-12 23:57:45 -04:00
|
|
|
|
|
|
|
|
// Update the participants stored in the stats object
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onStateChanged", (newState, oldState) => {
|
2026-09-12 23:57:45 -04:00
|
|
|
if (newState === GroupCallState.Entered || oldState === GroupCallState.Entered || newState === GroupCallState.Ended) {
|
|
|
|
|
// We either entered, left, or ended the call
|
|
|
|
|
this.updateParticipants();
|
2026-09-13 14:14:38 -04:00
|
|
|
this.updateMemberState().catch(e => _logger.logger.error(`GroupCall ${this.groupCallId} onStateChanged() failed to update member state devices"`, e));
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onLocalFeedsChanged", () => {
|
2026-09-12 23:57:45 -04:00
|
|
|
if (this.state === GroupCallState.Entered) {
|
2026-09-13 14:14:38 -04:00
|
|
|
this.updateMemberState().catch(e => _logger.logger.error(`GroupCall ${this.groupCallId} onLocalFeedsChanged() failed to update member state feeds`, e));
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
this.reEmitter = new _ReEmitter.ReEmitter(this);
|
|
|
|
|
this.groupCallId = groupCallId !== null && groupCallId !== void 0 ? groupCallId : (0, _call.genCallID)();
|
|
|
|
|
this.creationTs = (_room$currentState$ge = (_room$currentState$ge2 = room.currentState.getStateEvents(_event.EventType.GroupCallPrefix, this.groupCallId)) === null || _room$currentState$ge2 === void 0 ? void 0 : _room$currentState$ge2.getTs()) !== null && _room$currentState$ge !== void 0 ? _room$currentState$ge : null;
|
2026-09-12 23:57:45 -04:00
|
|
|
this.updateParticipants();
|
2026-09-13 14:14:38 -04:00
|
|
|
room.on(_roomState.RoomStateEvent.Update, this.onRoomState);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.on(GroupCallEvent.ParticipantsChanged, this.onParticipantsChanged);
|
|
|
|
|
this.on(GroupCallEvent.GroupCallStateChanged, this.onStateChanged);
|
|
|
|
|
this.on(GroupCallEvent.LocalScreenshareStateChanged, this.onLocalFeedsChanged);
|
|
|
|
|
this.allowCallWithoutVideoAndAudio = !!isCallWithoutVideoAndAudio;
|
|
|
|
|
}
|
|
|
|
|
async create() {
|
|
|
|
|
this.creationTs = Date.now();
|
|
|
|
|
this.client.groupCallEventHandler.groupCalls.set(this.room.roomId, this);
|
2026-09-13 14:14:38 -04:00
|
|
|
this.client.emit(_groupCallEventHandler.GroupCallEventHandlerEvent.Outgoing, this);
|
2026-09-12 23:57:45 -04:00
|
|
|
const groupCallState = {
|
|
|
|
|
"m.intent": this.intent,
|
|
|
|
|
"m.type": this.type,
|
|
|
|
|
"io.element.ptt": this.isPtt,
|
|
|
|
|
// TODO: Specify data-channels better
|
|
|
|
|
"dataChannelsEnabled": this.dataChannelsEnabled,
|
|
|
|
|
"dataChannelOptions": this.dataChannelsEnabled ? this.dataChannelOptions : undefined
|
|
|
|
|
};
|
2026-09-13 14:14:38 -04:00
|
|
|
await this.client.sendStateEvent(this.room.roomId, _event.EventType.GroupCallPrefix, groupCallState, this.groupCallId);
|
|
|
|
|
return this;
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* The group call's state.
|
|
|
|
|
*/
|
|
|
|
|
get state() {
|
|
|
|
|
return this._state;
|
|
|
|
|
}
|
|
|
|
|
set state(value) {
|
|
|
|
|
const prevValue = this._state;
|
|
|
|
|
if (value !== prevValue) {
|
|
|
|
|
this._state = value;
|
|
|
|
|
this.emit(GroupCallEvent.GroupCallStateChanged, value, prevValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* The current participants in the call, as a map from members to device IDs
|
|
|
|
|
* to participant info.
|
|
|
|
|
*/
|
|
|
|
|
get participants() {
|
|
|
|
|
return this._participants;
|
|
|
|
|
}
|
|
|
|
|
set participants(value) {
|
|
|
|
|
const prevValue = this._participants;
|
|
|
|
|
const participantStateEqual = (x, y) => x.sessionId === y.sessionId && x.screensharing === y.screensharing;
|
2026-09-13 14:14:38 -04:00
|
|
|
const deviceMapsEqual = (x, y) => (0, _utils.mapsEqual)(x, y, participantStateEqual);
|
2026-09-12 23:57:45 -04:00
|
|
|
|
|
|
|
|
// Only update if the map actually changed
|
2026-09-13 14:14:38 -04:00
|
|
|
if (!(0, _utils.mapsEqual)(value, prevValue, deviceMapsEqual)) {
|
2026-09-12 23:57:45 -04:00
|
|
|
this._participants = value;
|
|
|
|
|
this.emit(GroupCallEvent.ParticipantsChanged, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* The timestamp at which the call was created, or null if it has not yet
|
|
|
|
|
* been created.
|
|
|
|
|
*/
|
|
|
|
|
get creationTs() {
|
|
|
|
|
return this._creationTs;
|
|
|
|
|
}
|
|
|
|
|
set creationTs(value) {
|
|
|
|
|
this._creationTs = value;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Whether the local device has entered this call via another session, such
|
|
|
|
|
* as a widget.
|
|
|
|
|
*/
|
|
|
|
|
get enteredViaAnotherSession() {
|
|
|
|
|
return this._enteredViaAnotherSession;
|
|
|
|
|
}
|
|
|
|
|
set enteredViaAnotherSession(value) {
|
|
|
|
|
this._enteredViaAnotherSession = value;
|
|
|
|
|
this.updateParticipants();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Executes the given callback on all calls in this group call.
|
|
|
|
|
* @param f - The callback.
|
|
|
|
|
*/
|
|
|
|
|
forEachCall(f) {
|
|
|
|
|
for (const deviceMap of this.calls.values()) {
|
|
|
|
|
for (const call of deviceMap.values()) f(call);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
getLocalFeeds() {
|
|
|
|
|
const feeds = [];
|
|
|
|
|
if (this.localCallFeed) feeds.push(this.localCallFeed);
|
|
|
|
|
if (this.localScreenshareFeed) feeds.push(this.localScreenshareFeed);
|
|
|
|
|
return feeds;
|
|
|
|
|
}
|
|
|
|
|
hasLocalParticipant() {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _this$participants$ge, _this$participants$ge2;
|
|
|
|
|
return (_this$participants$ge = (_this$participants$ge2 = this.participants.get(this.room.getMember(this.client.getUserId()))) === null || _this$participants$ge2 === void 0 ? void 0 : _this$participants$ge2.has(this.client.getDeviceId())) !== null && _this$participants$ge !== void 0 ? _this$participants$ge : false;
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines whether the given call is one that we were expecting to exist
|
|
|
|
|
* given our knowledge of who is participating in the group call.
|
|
|
|
|
*/
|
|
|
|
|
callExpected(call) {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _this$participants$ge3;
|
2026-09-12 23:57:45 -04:00
|
|
|
const userId = getCallUserId(call);
|
|
|
|
|
const member = userId === null ? null : this.room.getMember(userId);
|
|
|
|
|
const deviceId = call.getOpponentDeviceId();
|
2026-09-13 14:14:38 -04:00
|
|
|
return member !== null && deviceId !== undefined && ((_this$participants$ge3 = this.participants.get(member)) === null || _this$participants$ge3 === void 0 ? void 0 : _this$participants$ge3.get(deviceId)) !== undefined;
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
async initLocalCallFeed() {
|
|
|
|
|
if (this.state !== GroupCallState.LocalCallFeedUninitialized) {
|
|
|
|
|
throw new Error(`Cannot initialize local call feed in the "${this.state}" state.`);
|
|
|
|
|
}
|
|
|
|
|
this.state = GroupCallState.InitializingLocalCallFeed;
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
try {
|
|
|
|
|
this.initCallFeedPromise = this.initLocalCallFeedInternal();
|
|
|
|
|
await this.initCallFeedPromise;
|
|
|
|
|
} finally {
|
|
|
|
|
this.initCallFeedPromise = undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async initLocalCallFeedInternal() {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} initLocalCallFeedInternal() running`);
|
2026-09-12 23:57:45 -04:00
|
|
|
let stream;
|
|
|
|
|
try {
|
|
|
|
|
stream = await this.client.getMediaHandler().getUserMediaStream(true, this.type === GroupCallType.Video);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// If is allowed to join a call without a media stream, then we
|
|
|
|
|
// don't throw an error here. But we need an empty Local Feed to establish
|
|
|
|
|
// a connection later.
|
|
|
|
|
if (this.allowCallWithoutVideoAndAudio) {
|
|
|
|
|
stream = new MediaStream();
|
|
|
|
|
} else {
|
|
|
|
|
this.state = GroupCallState.LocalCallFeedUninitialized;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
throw new Error("Group call disposed while gathering media stream");
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
const callFeed = new _callFeed.CallFeed({
|
2026-09-12 23:57:45 -04:00
|
|
|
client: this.client,
|
|
|
|
|
roomId: this.room.roomId,
|
|
|
|
|
userId: this.client.getUserId(),
|
|
|
|
|
deviceId: this.client.getDeviceId(),
|
|
|
|
|
stream,
|
2026-09-13 14:14:38 -04:00
|
|
|
purpose: _callEventTypes.SDPStreamMetadataPurpose.Usermedia,
|
2026-09-12 23:57:45 -04:00
|
|
|
audioMuted: this.initWithAudioMuted || stream.getAudioTracks().length === 0 || this.isPtt,
|
|
|
|
|
videoMuted: this.initWithVideoMuted || stream.getVideoTracks().length === 0
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _call.setTracksEnabled)(stream.getAudioTracks(), !callFeed.isAudioMuted());
|
|
|
|
|
(0, _call.setTracksEnabled)(stream.getVideoTracks(), !callFeed.isVideoMuted());
|
2026-09-12 23:57:45 -04:00
|
|
|
this.localCallFeed = callFeed;
|
|
|
|
|
this.addUserMediaFeed(callFeed);
|
|
|
|
|
this.state = GroupCallState.LocalCallFeedInitialized;
|
|
|
|
|
}
|
|
|
|
|
async updateLocalUsermediaStream(stream) {
|
|
|
|
|
if (this.localCallFeed) {
|
|
|
|
|
const oldStream = this.localCallFeed.stream;
|
|
|
|
|
this.localCallFeed.setNewStream(stream);
|
|
|
|
|
const micShouldBeMuted = this.localCallFeed.isAudioMuted();
|
|
|
|
|
const vidShouldBeMuted = this.localCallFeed.isVideoMuted();
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} updateLocalUsermediaStream() (oldStreamId=${oldStream.id}, newStreamId=${stream.id}, micShouldBeMuted=${micShouldBeMuted}, vidShouldBeMuted=${vidShouldBeMuted})`);
|
|
|
|
|
(0, _call.setTracksEnabled)(stream.getAudioTracks(), !micShouldBeMuted);
|
|
|
|
|
(0, _call.setTracksEnabled)(stream.getVideoTracks(), !vidShouldBeMuted);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.client.getMediaHandler().stopUserMediaStream(oldStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async enter() {
|
|
|
|
|
if (this.state === GroupCallState.LocalCallFeedUninitialized) {
|
|
|
|
|
await this.initLocalCallFeed();
|
|
|
|
|
} else if (this.state !== GroupCallState.LocalCallFeedInitialized) {
|
|
|
|
|
throw new Error(`Cannot enter call in the "${this.state}" state`);
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} enter() running`);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.state = GroupCallState.Entered;
|
2026-09-13 14:14:38 -04:00
|
|
|
this.client.on(_callEventHandler.CallEventHandlerEvent.Incoming, this.onIncomingCall);
|
2026-09-12 23:57:45 -04:00
|
|
|
for (const call of this.client.callEventHandler.calls.values()) {
|
|
|
|
|
this.onIncomingCall(call);
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
this.retryCallLoopInterval = setInterval(this.onRetryCallLoop, this.retryCallInterval);
|
|
|
|
|
this.activeSpeaker = undefined;
|
|
|
|
|
this.onActiveSpeakerLoop();
|
|
|
|
|
this.activeSpeakerLoopInterval = setInterval(this.onActiveSpeakerLoop, this.activeSpeakerInterval);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
dispose() {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _this$stats;
|
2026-09-12 23:57:45 -04:00
|
|
|
if (this.localCallFeed) {
|
|
|
|
|
this.removeUserMediaFeed(this.localCallFeed);
|
|
|
|
|
this.localCallFeed = undefined;
|
|
|
|
|
}
|
|
|
|
|
if (this.localScreenshareFeed) {
|
|
|
|
|
this.client.getMediaHandler().stopScreensharingStream(this.localScreenshareFeed.stream);
|
|
|
|
|
this.removeScreenshareFeed(this.localScreenshareFeed);
|
|
|
|
|
this.localScreenshareFeed = undefined;
|
|
|
|
|
this.localDesktopCapturerSourceId = undefined;
|
|
|
|
|
}
|
|
|
|
|
this.client.getMediaHandler().stopAllStreams();
|
|
|
|
|
if (this.transmitTimer !== null) {
|
|
|
|
|
clearTimeout(this.transmitTimer);
|
|
|
|
|
this.transmitTimer = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.retryCallLoopInterval !== undefined) {
|
|
|
|
|
clearInterval(this.retryCallLoopInterval);
|
|
|
|
|
this.retryCallLoopInterval = undefined;
|
|
|
|
|
}
|
|
|
|
|
if (this.participantsExpirationTimer !== null) {
|
|
|
|
|
clearTimeout(this.participantsExpirationTimer);
|
|
|
|
|
this.participantsExpirationTimer = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.state !== GroupCallState.Entered) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
this.forEachCall(call => call.hangup(_call.CallErrorCode.UserHangup, false));
|
2026-09-12 23:57:45 -04:00
|
|
|
this.activeSpeaker = undefined;
|
|
|
|
|
clearInterval(this.activeSpeakerLoopInterval);
|
|
|
|
|
this.retryCallCounts.clear();
|
|
|
|
|
clearInterval(this.retryCallLoopInterval);
|
2026-09-13 14:14:38 -04:00
|
|
|
this.client.removeListener(_callEventHandler.CallEventHandlerEvent.Incoming, this.onIncomingCall);
|
|
|
|
|
(_this$stats = this.stats) === null || _this$stats === void 0 ? void 0 : _this$stats.stop();
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
leave() {
|
|
|
|
|
this.dispose();
|
|
|
|
|
this.state = GroupCallState.LocalCallFeedUninitialized;
|
|
|
|
|
}
|
|
|
|
|
async terminate(emitStateEvent = true) {
|
|
|
|
|
this.dispose();
|
2026-09-13 14:14:38 -04:00
|
|
|
this.room.off(_roomState.RoomStateEvent.Update, this.onRoomState);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.client.groupCallEventHandler.groupCalls.delete(this.room.roomId);
|
2026-09-13 14:14:38 -04:00
|
|
|
this.client.emit(_groupCallEventHandler.GroupCallEventHandlerEvent.Ended, this);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.state = GroupCallState.Ended;
|
|
|
|
|
if (emitStateEvent) {
|
2026-09-13 14:14:38 -04:00
|
|
|
const existingStateEvent = this.room.currentState.getStateEvents(_event.EventType.GroupCallPrefix, this.groupCallId);
|
|
|
|
|
await this.client.sendStateEvent(this.room.roomId, _event.EventType.GroupCallPrefix, _objectSpread(_objectSpread({}, existingStateEvent.getContent()), {}, {
|
2026-09-12 23:57:45 -04:00
|
|
|
"m.terminated": GroupCallTerminationReason.CallEnded
|
|
|
|
|
}), this.groupCallId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local Usermedia
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
isLocalVideoMuted() {
|
|
|
|
|
if (this.localCallFeed) {
|
|
|
|
|
return this.localCallFeed.isVideoMuted();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
isMicrophoneMuted() {
|
|
|
|
|
if (this.localCallFeed) {
|
|
|
|
|
return this.localCallFeed.isAudioMuted();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the mute state of the local participants's microphone.
|
|
|
|
|
* @param muted - Whether to mute the microphone
|
|
|
|
|
* @returns Whether muting/unmuting was successful
|
|
|
|
|
*/
|
|
|
|
|
async setMicrophoneMuted(muted) {
|
|
|
|
|
// hasAudioDevice can block indefinitely if the window has lost focus,
|
|
|
|
|
// and it doesn't make much sense to keep a device from being muted, so
|
|
|
|
|
// we always allow muted = true changes to go through
|
|
|
|
|
if (!muted && !(await this.client.getMediaHandler().hasAudioDevice())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const sendUpdatesBefore = !muted && this.isPtt;
|
|
|
|
|
|
|
|
|
|
// set a timer for the maximum transmit time on PTT calls
|
|
|
|
|
if (this.isPtt) {
|
|
|
|
|
// Set or clear the max transmit timer
|
|
|
|
|
if (!muted && this.isMicrophoneMuted()) {
|
|
|
|
|
this.transmitTimer = setTimeout(() => {
|
|
|
|
|
this.setMicrophoneMuted(true);
|
|
|
|
|
}, this.pttMaxTransmitTime);
|
|
|
|
|
} else if (muted && !this.isMicrophoneMuted()) {
|
|
|
|
|
if (this.transmitTimer !== null) clearTimeout(this.transmitTimer);
|
|
|
|
|
this.transmitTimer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
this.forEachCall(call => {
|
|
|
|
|
var _call$localUsermediaF;
|
|
|
|
|
return (_call$localUsermediaF = call.localUsermediaFeed) === null || _call$localUsermediaF === void 0 ? void 0 : _call$localUsermediaF.setAudioVideoMuted(muted, null);
|
|
|
|
|
});
|
2026-09-12 23:57:45 -04:00
|
|
|
const sendUpdates = async () => {
|
|
|
|
|
const updates = [];
|
|
|
|
|
this.forEachCall(call => updates.push(call.sendMetadataUpdate()));
|
2026-09-13 14:14:38 -04:00
|
|
|
await Promise.all(updates).catch(e => _logger.logger.info(`GroupCall ${this.groupCallId} setMicrophoneMuted() failed to send some metadata updates`, e));
|
2026-09-12 23:57:45 -04:00
|
|
|
};
|
|
|
|
|
if (sendUpdatesBefore) await sendUpdates();
|
|
|
|
|
if (this.localCallFeed) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setMicrophoneMuted() (streamId=${this.localCallFeed.stream.id}, muted=${muted})`);
|
2026-09-12 23:57:45 -04:00
|
|
|
const hasPermission = await this.checkAudioPermissionIfNecessary(muted);
|
|
|
|
|
if (!hasPermission) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
this.localCallFeed.setAudioVideoMuted(muted, null);
|
|
|
|
|
// I don't believe its actually necessary to enable these tracks: they
|
|
|
|
|
// are the one on the GroupCall's own CallFeed and are cloned before being
|
|
|
|
|
// given to any of the actual calls, so these tracks don't actually go
|
|
|
|
|
// anywhere. Let's do it anyway to avoid confusion.
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _call.setTracksEnabled)(this.localCallFeed.stream.getAudioTracks(), !muted);
|
2026-09-12 23:57:45 -04:00
|
|
|
} else {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setMicrophoneMuted() no stream muted (muted=${muted})`);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.initWithAudioMuted = muted;
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
this.forEachCall(call => (0, _call.setTracksEnabled)(call.localUsermediaFeed.stream.getAudioTracks(), !muted && this.callExpected(call)));
|
2026-09-12 23:57:45 -04:00
|
|
|
this.emit(GroupCallEvent.LocalMuteStateChanged, muted, this.isLocalVideoMuted());
|
|
|
|
|
if (!sendUpdatesBefore) await sendUpdates();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If we allow entering a call without a camera and without video, it can happen that the access rights to the
|
|
|
|
|
* devices have not yet been queried. If a stream does not yet have an audio track, we assume that the rights have
|
|
|
|
|
* not yet been checked.
|
|
|
|
|
*
|
|
|
|
|
* `this.client.getMediaHandler().getUserMediaStream` clones the current stream, so it only wanted to be called when
|
|
|
|
|
* not Audio Track exists.
|
|
|
|
|
* As such, this is a compromise, because, the access rights should always be queried before the call.
|
|
|
|
|
*/
|
|
|
|
|
async checkAudioPermissionIfNecessary(muted) {
|
|
|
|
|
// We needed this here to avoid an error in case user join a call without a device.
|
|
|
|
|
try {
|
|
|
|
|
if (!muted && this.localCallFeed && !this.localCallFeed.hasAudioTrack) {
|
|
|
|
|
const stream = await this.client.getMediaHandler().getUserMediaStream(true, !this.localCallFeed.isVideoMuted());
|
2026-09-13 14:14:38 -04:00
|
|
|
if ((stream === null || stream === void 0 ? void 0 : stream.getTracks().length) === 0) {
|
2026-09-12 23:57:45 -04:00
|
|
|
// if case permission denied to get a stream stop this here
|
|
|
|
|
/* istanbul ignore next */
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setMicrophoneMuted() no device to receive local stream, muted=${muted}`);
|
2026-09-12 23:57:45 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
} catch (e) {
|
2026-09-12 23:57:45 -04:00
|
|
|
/* istanbul ignore next */
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setMicrophoneMuted() no device or permission to receive local stream, muted=${muted}`);
|
2026-09-12 23:57:45 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the mute state of the local participants's video.
|
|
|
|
|
* @param muted - Whether to mute the video
|
|
|
|
|
* @returns Whether muting/unmuting was successful
|
|
|
|
|
*/
|
|
|
|
|
async setLocalVideoMuted(muted) {
|
|
|
|
|
// hasAudioDevice can block indefinitely if the window has lost focus,
|
|
|
|
|
// and it doesn't make much sense to keep a device from being muted, so
|
|
|
|
|
// we always allow muted = true changes to go through
|
|
|
|
|
if (!muted && !(await this.client.getMediaHandler().hasVideoDevice())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (this.localCallFeed) {
|
|
|
|
|
/* istanbul ignore next */
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setLocalVideoMuted() (stream=${this.localCallFeed.stream.id}, muted=${muted})`);
|
2026-09-12 23:57:45 -04:00
|
|
|
try {
|
|
|
|
|
const stream = await this.client.getMediaHandler().getUserMediaStream(true, !muted);
|
|
|
|
|
await this.updateLocalUsermediaStream(stream);
|
|
|
|
|
this.localCallFeed.setAudioVideoMuted(null, muted);
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _call.setTracksEnabled)(this.localCallFeed.stream.getVideoTracks(), !muted);
|
|
|
|
|
} catch (_) {
|
2026-09-12 23:57:45 -04:00
|
|
|
// No permission to video device
|
|
|
|
|
/* istanbul ignore next */
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setLocalVideoMuted() no device or permission to receive local stream, muted=${muted}`);
|
2026-09-12 23:57:45 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setLocalVideoMuted() no stream muted (muted=${muted})`);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.initWithVideoMuted = muted;
|
|
|
|
|
}
|
|
|
|
|
const updates = [];
|
|
|
|
|
this.forEachCall(call => updates.push(call.setLocalVideoMuted(muted)));
|
|
|
|
|
await Promise.all(updates);
|
|
|
|
|
|
|
|
|
|
// We setTracksEnabled again, independently from the call doing it
|
|
|
|
|
// internally, since we might not be expecting the call
|
2026-09-13 14:14:38 -04:00
|
|
|
this.forEachCall(call => (0, _call.setTracksEnabled)(call.localUsermediaFeed.stream.getVideoTracks(), !muted && this.callExpected(call)));
|
2026-09-12 23:57:45 -04:00
|
|
|
this.emit(GroupCallEvent.LocalMuteStateChanged, this.isMicrophoneMuted(), muted);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
async setScreensharingEnabled(enabled, opts = {}) {
|
|
|
|
|
if (enabled === this.isScreensharing()) {
|
|
|
|
|
return enabled;
|
|
|
|
|
}
|
|
|
|
|
if (enabled) {
|
|
|
|
|
try {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setScreensharingEnabled() is asking for screensharing permissions`);
|
2026-09-12 23:57:45 -04:00
|
|
|
const stream = await this.client.getMediaHandler().getScreensharingStream(opts);
|
|
|
|
|
for (const track of stream.getTracks()) {
|
|
|
|
|
const onTrackEnded = () => {
|
|
|
|
|
this.setScreensharingEnabled(false);
|
|
|
|
|
track.removeEventListener("ended", onTrackEnded);
|
|
|
|
|
};
|
|
|
|
|
track.addEventListener("ended", onTrackEnded);
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} setScreensharingEnabled() granted screensharing permissions. Setting screensharing enabled on all calls`);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.localDesktopCapturerSourceId = opts.desktopCapturerSourceId;
|
2026-09-13 14:14:38 -04:00
|
|
|
this.localScreenshareFeed = new _callFeed.CallFeed({
|
2026-09-12 23:57:45 -04:00
|
|
|
client: this.client,
|
|
|
|
|
roomId: this.room.roomId,
|
|
|
|
|
userId: this.client.getUserId(),
|
|
|
|
|
deviceId: this.client.getDeviceId(),
|
|
|
|
|
stream,
|
2026-09-13 14:14:38 -04:00
|
|
|
purpose: _callEventTypes.SDPStreamMetadataPurpose.Screenshare,
|
2026-09-12 23:57:45 -04:00
|
|
|
audioMuted: false,
|
|
|
|
|
videoMuted: false
|
|
|
|
|
});
|
|
|
|
|
this.addScreenshareFeed(this.localScreenshareFeed);
|
|
|
|
|
this.emit(GroupCallEvent.LocalScreenshareStateChanged, true, this.localScreenshareFeed, this.localDesktopCapturerSourceId);
|
|
|
|
|
|
|
|
|
|
// TODO: handle errors
|
|
|
|
|
this.forEachCall(call => call.pushLocalFeed(this.localScreenshareFeed.clone()));
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (opts.throwOnFail) throw error;
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.error(`GroupCall ${this.groupCallId} setScreensharingEnabled() enabling screensharing error`, error);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.emit(GroupCallEvent.Error, new GroupCallError(GroupCallErrorCode.NoUserMedia, "Failed to get screen-sharing stream: ", error));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.forEachCall(call => {
|
|
|
|
|
if (call.localScreensharingFeed) call.removeLocalFeed(call.localScreensharingFeed);
|
|
|
|
|
});
|
|
|
|
|
this.client.getMediaHandler().stopScreensharingStream(this.localScreenshareFeed.stream);
|
|
|
|
|
this.removeScreenshareFeed(this.localScreenshareFeed);
|
|
|
|
|
this.localScreenshareFeed = undefined;
|
|
|
|
|
this.localDesktopCapturerSourceId = undefined;
|
|
|
|
|
this.emit(GroupCallEvent.LocalScreenshareStateChanged, false, undefined, undefined);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
isScreensharing() {
|
|
|
|
|
return !!this.localScreenshareFeed;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Determines whether a given participant expects us to call them (versus
|
|
|
|
|
* them calling us).
|
|
|
|
|
* @param userId - The participant's user ID.
|
|
|
|
|
* @param deviceId - The participant's device ID.
|
|
|
|
|
* @returns Whether we need to place an outgoing call to the participant.
|
|
|
|
|
*/
|
|
|
|
|
wantsOutgoingCall(userId, deviceId) {
|
|
|
|
|
const localUserId = this.client.getUserId();
|
|
|
|
|
const localDeviceId = this.client.getDeviceId();
|
|
|
|
|
return (
|
|
|
|
|
// If a user's ID is less than our own, they'll call us
|
|
|
|
|
userId >= localUserId && (
|
|
|
|
|
// If this is another one of our devices, compare device IDs to tell whether it'll call us
|
|
|
|
|
userId !== localUserId || deviceId > localDeviceId)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Places calls to all participants that we're responsible for calling.
|
|
|
|
|
*/
|
|
|
|
|
placeOutgoingCalls() {
|
|
|
|
|
let callsChanged = false;
|
|
|
|
|
for (const [{
|
|
|
|
|
userId
|
|
|
|
|
}, participantMap] of this.participants) {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _this$calls$get2;
|
|
|
|
|
const callMap = (_this$calls$get2 = this.calls.get(userId)) !== null && _this$calls$get2 !== void 0 ? _this$calls$get2 : new Map();
|
2026-09-12 23:57:45 -04:00
|
|
|
for (const [deviceId, participant] of participantMap) {
|
|
|
|
|
const prevCall = callMap.get(deviceId);
|
2026-09-13 14:14:38 -04:00
|
|
|
if ((prevCall === null || prevCall === void 0 ? void 0 : prevCall.getOpponentSessionId()) !== participant.sessionId && this.wantsOutgoingCall(userId, deviceId)) {
|
2026-09-12 23:57:45 -04:00
|
|
|
callsChanged = true;
|
|
|
|
|
if (prevCall !== undefined) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.debug(`GroupCall ${this.groupCallId} placeOutgoingCalls() replacing call (userId=${userId}, deviceId=${deviceId}, callId=${prevCall.callId})`);
|
|
|
|
|
prevCall.hangup(_call.CallErrorCode.NewSession, false);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
const newCall = (0, _call.createNewMatrixCall)(this.client, this.room.roomId, {
|
2026-09-12 23:57:45 -04:00
|
|
|
invitee: userId,
|
|
|
|
|
opponentDeviceId: deviceId,
|
|
|
|
|
opponentSessionId: participant.sessionId,
|
|
|
|
|
groupCallId: this.groupCallId
|
|
|
|
|
});
|
|
|
|
|
if (newCall === null) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.error(`GroupCall ${this.groupCallId} placeOutgoingCalls() failed to create call (userId=${userId}, device=${deviceId})`);
|
2026-09-12 23:57:45 -04:00
|
|
|
callMap.delete(deviceId);
|
|
|
|
|
} else {
|
|
|
|
|
this.initCall(newCall);
|
|
|
|
|
callMap.set(deviceId, newCall);
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.debug(`GroupCall ${this.groupCallId} placeOutgoingCalls() placing call (userId=${userId}, deviceId=${deviceId}, sessionId=${participant.sessionId})`);
|
2026-09-12 23:57:45 -04:00
|
|
|
newCall.placeCallWithCallFeeds(this.getLocalFeeds().map(feed => feed.clone()), participant.screensharing).then(() => {
|
|
|
|
|
if (this.dataChannelsEnabled) {
|
|
|
|
|
newCall.createDataChannel("datachannel", this.dataChannelOptions);
|
|
|
|
|
}
|
|
|
|
|
}).catch(e => {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.warn(`GroupCall ${this.groupCallId} placeOutgoingCalls() failed to place call (userId=${userId})`, e);
|
|
|
|
|
if (e instanceof _call.CallError && e.code === GroupCallErrorCode.UnknownDevice) {
|
2026-09-12 23:57:45 -04:00
|
|
|
this.emit(GroupCallEvent.Error, e);
|
|
|
|
|
} else {
|
|
|
|
|
this.emit(GroupCallEvent.Error, new GroupCallError(GroupCallErrorCode.PlaceCallFailed, `Failed to place call to ${userId}`));
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
newCall.hangup(_call.CallErrorCode.SignallingFailed, false);
|
2026-09-12 23:57:45 -04:00
|
|
|
if (callMap.get(deviceId) === newCall) callMap.delete(deviceId);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (callMap.size > 0) {
|
|
|
|
|
this.calls.set(userId, callMap);
|
|
|
|
|
} else {
|
|
|
|
|
this.calls.delete(userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (callsChanged) this.emit(GroupCallEvent.CallsChanged, this.calls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Room Member State
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
getMemberStateEvents(userId) {
|
2026-09-13 14:14:38 -04:00
|
|
|
return userId === undefined ? this.room.currentState.getStateEvents(_event.EventType.GroupCallMemberPrefix) : this.room.currentState.getStateEvents(_event.EventType.GroupCallMemberPrefix, userId);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
initCall(call) {
|
|
|
|
|
const opponentMemberId = getCallUserId(call);
|
|
|
|
|
if (!opponentMemberId) {
|
|
|
|
|
throw new Error("Cannot init call without user id");
|
|
|
|
|
}
|
|
|
|
|
const onCallFeedsChanged = () => this.onCallFeedsChanged(call);
|
|
|
|
|
const onCallStateChanged = (state, oldState) => this.onCallStateChanged(call, state, oldState);
|
|
|
|
|
const onCallHangup = this.onCallHangup;
|
|
|
|
|
const onCallReplaced = newCall => this.onCallReplaced(call, newCall);
|
|
|
|
|
let deviceMap = this.callHandlers.get(opponentMemberId);
|
|
|
|
|
if (deviceMap === undefined) {
|
|
|
|
|
deviceMap = new Map();
|
|
|
|
|
this.callHandlers.set(opponentMemberId, deviceMap);
|
|
|
|
|
}
|
|
|
|
|
deviceMap.set(call.getOpponentDeviceId(), {
|
|
|
|
|
onCallFeedsChanged,
|
|
|
|
|
onCallStateChanged,
|
|
|
|
|
onCallHangup,
|
|
|
|
|
onCallReplaced
|
|
|
|
|
});
|
2026-09-13 14:14:38 -04:00
|
|
|
call.on(_call.CallEvent.FeedsChanged, onCallFeedsChanged);
|
|
|
|
|
call.on(_call.CallEvent.State, onCallStateChanged);
|
|
|
|
|
call.on(_call.CallEvent.Hangup, onCallHangup);
|
|
|
|
|
call.on(_call.CallEvent.Replaced, onCallReplaced);
|
2026-09-12 23:57:45 -04:00
|
|
|
call.isPtt = this.isPtt;
|
2026-09-13 14:14:38 -04:00
|
|
|
this.reEmitter.reEmit(call, Object.values(_call.CallEvent));
|
2026-09-12 23:57:45 -04:00
|
|
|
call.initStats(this.getGroupCallStats());
|
|
|
|
|
onCallFeedsChanged();
|
|
|
|
|
}
|
|
|
|
|
disposeCall(call, hangupReason) {
|
|
|
|
|
const opponentMemberId = getCallUserId(call);
|
|
|
|
|
const opponentDeviceId = call.getOpponentDeviceId();
|
|
|
|
|
if (!opponentMemberId) {
|
|
|
|
|
throw new Error("Cannot dispose call without user id");
|
|
|
|
|
}
|
|
|
|
|
const deviceMap = this.callHandlers.get(opponentMemberId);
|
|
|
|
|
const {
|
|
|
|
|
onCallFeedsChanged,
|
|
|
|
|
onCallStateChanged,
|
|
|
|
|
onCallHangup,
|
|
|
|
|
onCallReplaced
|
|
|
|
|
} = deviceMap.get(opponentDeviceId);
|
2026-09-13 14:14:38 -04:00
|
|
|
call.removeListener(_call.CallEvent.FeedsChanged, onCallFeedsChanged);
|
|
|
|
|
call.removeListener(_call.CallEvent.State, onCallStateChanged);
|
|
|
|
|
call.removeListener(_call.CallEvent.Hangup, onCallHangup);
|
|
|
|
|
call.removeListener(_call.CallEvent.Replaced, onCallReplaced);
|
2026-09-12 23:57:45 -04:00
|
|
|
deviceMap.delete(opponentMemberId);
|
|
|
|
|
if (deviceMap.size === 0) this.callHandlers.delete(opponentMemberId);
|
2026-09-13 14:14:38 -04:00
|
|
|
if (call.hangupReason === _call.CallErrorCode.Replaced) {
|
2026-09-12 23:57:45 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const usermediaFeed = this.getUserMediaFeed(opponentMemberId, opponentDeviceId);
|
|
|
|
|
if (usermediaFeed) {
|
|
|
|
|
this.removeUserMediaFeed(usermediaFeed);
|
|
|
|
|
}
|
|
|
|
|
const screenshareFeed = this.getScreenshareFeed(opponentMemberId, opponentDeviceId);
|
|
|
|
|
if (screenshareFeed) {
|
|
|
|
|
this.removeScreenshareFeed(screenshareFeed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* UserMedia CallFeed Event Handlers
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
getUserMediaFeed(userId, deviceId) {
|
|
|
|
|
return this.userMediaFeeds.find(f => f.userId === userId && f.deviceId === deviceId);
|
|
|
|
|
}
|
|
|
|
|
addUserMediaFeed(callFeed) {
|
|
|
|
|
this.userMediaFeeds.push(callFeed);
|
|
|
|
|
callFeed.measureVolumeActivity(true);
|
|
|
|
|
this.emit(GroupCallEvent.UserMediaFeedsChanged, this.userMediaFeeds);
|
|
|
|
|
}
|
|
|
|
|
replaceUserMediaFeed(existingFeed, replacementFeed) {
|
|
|
|
|
const feedIndex = this.userMediaFeeds.findIndex(f => f.userId === existingFeed.userId && f.deviceId === existingFeed.deviceId);
|
|
|
|
|
if (feedIndex === -1) {
|
|
|
|
|
throw new Error("Couldn't find user media feed to replace");
|
|
|
|
|
}
|
|
|
|
|
this.userMediaFeeds.splice(feedIndex, 1, replacementFeed);
|
|
|
|
|
existingFeed.dispose();
|
|
|
|
|
replacementFeed.measureVolumeActivity(true);
|
|
|
|
|
this.emit(GroupCallEvent.UserMediaFeedsChanged, this.userMediaFeeds);
|
|
|
|
|
}
|
|
|
|
|
removeUserMediaFeed(callFeed) {
|
|
|
|
|
const feedIndex = this.userMediaFeeds.findIndex(f => f.userId === callFeed.userId && f.deviceId === callFeed.deviceId);
|
|
|
|
|
if (feedIndex === -1) {
|
|
|
|
|
throw new Error("Couldn't find user media feed to remove");
|
|
|
|
|
}
|
|
|
|
|
this.userMediaFeeds.splice(feedIndex, 1);
|
|
|
|
|
callFeed.dispose();
|
|
|
|
|
this.emit(GroupCallEvent.UserMediaFeedsChanged, this.userMediaFeeds);
|
|
|
|
|
if (this.activeSpeaker === callFeed) {
|
|
|
|
|
this.activeSpeaker = this.userMediaFeeds[0];
|
|
|
|
|
this.emit(GroupCallEvent.ActiveSpeakerChanged, this.activeSpeaker);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Screenshare Call Feed Event Handlers
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
getScreenshareFeed(userId, deviceId) {
|
|
|
|
|
return this.screenshareFeeds.find(f => f.userId === userId && f.deviceId === deviceId);
|
|
|
|
|
}
|
|
|
|
|
addScreenshareFeed(callFeed) {
|
|
|
|
|
this.screenshareFeeds.push(callFeed);
|
|
|
|
|
this.emit(GroupCallEvent.ScreenshareFeedsChanged, this.screenshareFeeds);
|
|
|
|
|
}
|
|
|
|
|
replaceScreenshareFeed(existingFeed, replacementFeed) {
|
|
|
|
|
const feedIndex = this.screenshareFeeds.findIndex(f => f.userId === existingFeed.userId && f.deviceId === existingFeed.deviceId);
|
|
|
|
|
if (feedIndex === -1) {
|
|
|
|
|
throw new Error("Couldn't find screenshare feed to replace");
|
|
|
|
|
}
|
|
|
|
|
this.screenshareFeeds.splice(feedIndex, 1, replacementFeed);
|
|
|
|
|
existingFeed.dispose();
|
|
|
|
|
this.emit(GroupCallEvent.ScreenshareFeedsChanged, this.screenshareFeeds);
|
|
|
|
|
}
|
|
|
|
|
removeScreenshareFeed(callFeed) {
|
|
|
|
|
const feedIndex = this.screenshareFeeds.findIndex(f => f.userId === callFeed.userId && f.deviceId === callFeed.deviceId);
|
|
|
|
|
if (feedIndex === -1) {
|
|
|
|
|
throw new Error("Couldn't find screenshare feed to remove");
|
|
|
|
|
}
|
|
|
|
|
this.screenshareFeeds.splice(feedIndex, 1);
|
|
|
|
|
callFeed.dispose();
|
|
|
|
|
this.emit(GroupCallEvent.ScreenshareFeedsChanged, this.screenshareFeeds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recalculates and updates the participant map to match the room state.
|
|
|
|
|
*/
|
|
|
|
|
updateParticipants() {
|
2026-09-13 14:14:38 -04:00
|
|
|
const localMember = this.room.getMember(this.client.getUserId());
|
2026-09-12 23:57:45 -04:00
|
|
|
if (!localMember) {
|
|
|
|
|
// The client hasn't fetched enough of the room state to get our own member
|
|
|
|
|
// event. This probably shouldn't happen, but sanity check & exit for now.
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.warn(`GroupCall ${this.groupCallId} updateParticipants() tried to update participants before local room member is available`);
|
2026-09-12 23:57:45 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (this.participantsExpirationTimer !== null) {
|
|
|
|
|
clearTimeout(this.participantsExpirationTimer);
|
|
|
|
|
this.participantsExpirationTimer = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.state === GroupCallState.Ended) {
|
|
|
|
|
this.participants = new Map();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const participants = new Map();
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
const entered = this.state === GroupCallState.Entered || this.enteredViaAnotherSession;
|
|
|
|
|
let nextExpiration = Infinity;
|
|
|
|
|
for (const e of this.getMemberStateEvents()) {
|
|
|
|
|
const member = this.room.getMember(e.getStateKey());
|
|
|
|
|
const content = e.getContent();
|
|
|
|
|
const calls = Array.isArray(content["m.calls"]) ? content["m.calls"] : [];
|
|
|
|
|
const call = calls.find(call => call["m.call_id"] === this.groupCallId);
|
2026-09-13 14:14:38 -04:00
|
|
|
const devices = Array.isArray(call === null || call === void 0 ? void 0 : call["m.devices"]) ? call["m.devices"] : [];
|
2026-09-12 23:57:45 -04:00
|
|
|
|
|
|
|
|
// Filter out invalid and expired devices
|
|
|
|
|
let validDevices = devices.filter(d => typeof d.device_id === "string" && typeof d.session_id === "string" && typeof d.expires_ts === "number" && d.expires_ts > now && Array.isArray(d.feeds));
|
|
|
|
|
|
|
|
|
|
// Apply local echo for the unentered case
|
2026-09-13 14:14:38 -04:00
|
|
|
if (!entered && (member === null || member === void 0 ? void 0 : member.userId) === this.client.getUserId()) {
|
2026-09-12 23:57:45 -04:00
|
|
|
validDevices = validDevices.filter(d => d.device_id !== this.client.getDeviceId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Must have a connected device and be joined to the room
|
2026-09-13 14:14:38 -04:00
|
|
|
if (validDevices.length > 0 && (member === null || member === void 0 ? void 0 : member.membership) === "join") {
|
2026-09-12 23:57:45 -04:00
|
|
|
const deviceMap = new Map();
|
|
|
|
|
participants.set(member, deviceMap);
|
|
|
|
|
for (const d of validDevices) {
|
|
|
|
|
deviceMap.set(d.device_id, {
|
|
|
|
|
sessionId: d.session_id,
|
2026-09-13 14:14:38 -04:00
|
|
|
screensharing: d.feeds.some(f => f.purpose === _callEventTypes.SDPStreamMetadataPurpose.Screenshare)
|
2026-09-12 23:57:45 -04:00
|
|
|
});
|
|
|
|
|
if (d.expires_ts < nextExpiration) nextExpiration = d.expires_ts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply local echo for the entered case
|
|
|
|
|
if (entered) {
|
|
|
|
|
let deviceMap = participants.get(localMember);
|
|
|
|
|
if (deviceMap === undefined) {
|
|
|
|
|
deviceMap = new Map();
|
|
|
|
|
participants.set(localMember, deviceMap);
|
|
|
|
|
}
|
|
|
|
|
if (!deviceMap.has(this.client.getDeviceId())) {
|
|
|
|
|
deviceMap.set(this.client.getDeviceId(), {
|
|
|
|
|
sessionId: this.client.getSessionId(),
|
2026-09-13 14:14:38 -04:00
|
|
|
screensharing: this.getLocalFeeds().some(f => f.purpose === _callEventTypes.SDPStreamMetadataPurpose.Screenshare)
|
2026-09-12 23:57:45 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.participants = participants;
|
|
|
|
|
if (nextExpiration < Infinity) {
|
|
|
|
|
this.participantsExpirationTimer = setTimeout(() => this.updateParticipants(), nextExpiration - now);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the local user's member state with the devices returned by the given function.
|
|
|
|
|
* @param fn - A function from the current devices to the new devices. If it
|
|
|
|
|
* returns null, the update will be skipped.
|
|
|
|
|
* @param keepAlive - Whether the request should outlive the window.
|
|
|
|
|
*/
|
|
|
|
|
async updateDevices(fn, keepAlive = false) {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _event$getContent;
|
2026-09-12 23:57:45 -04:00
|
|
|
const now = Date.now();
|
|
|
|
|
const localUserId = this.client.getUserId();
|
|
|
|
|
const event = this.getMemberStateEvents(localUserId);
|
2026-09-13 14:14:38 -04:00
|
|
|
const content = (_event$getContent = event === null || event === void 0 ? void 0 : event.getContent()) !== null && _event$getContent !== void 0 ? _event$getContent : {};
|
2026-09-12 23:57:45 -04:00
|
|
|
const calls = Array.isArray(content["m.calls"]) ? content["m.calls"] : [];
|
|
|
|
|
let call = null;
|
|
|
|
|
const otherCalls = [];
|
|
|
|
|
for (const c of calls) {
|
|
|
|
|
if (c["m.call_id"] === this.groupCallId) {
|
|
|
|
|
call = c;
|
|
|
|
|
} else {
|
|
|
|
|
otherCalls.push(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (call === null) call = {};
|
|
|
|
|
const devices = Array.isArray(call["m.devices"]) ? call["m.devices"] : [];
|
|
|
|
|
|
|
|
|
|
// Filter out invalid and expired devices
|
|
|
|
|
const validDevices = devices.filter(d => typeof d.device_id === "string" && typeof d.session_id === "string" && typeof d.expires_ts === "number" && d.expires_ts > now && Array.isArray(d.feeds));
|
|
|
|
|
const newDevices = fn(validDevices);
|
|
|
|
|
if (newDevices === null) return;
|
|
|
|
|
const newCalls = [...otherCalls];
|
|
|
|
|
if (newDevices.length > 0) {
|
|
|
|
|
newCalls.push(_objectSpread(_objectSpread({}, call), {}, {
|
|
|
|
|
"m.call_id": this.groupCallId,
|
|
|
|
|
"m.devices": newDevices
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
const newContent = {
|
|
|
|
|
"m.calls": newCalls
|
|
|
|
|
};
|
2026-09-13 14:14:38 -04:00
|
|
|
await this.client.sendStateEvent(this.room.roomId, _event.EventType.GroupCallMemberPrefix, newContent, localUserId, {
|
2026-09-12 23:57:45 -04:00
|
|
|
keepAlive
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
async addDeviceToMemberState() {
|
|
|
|
|
await this.updateDevices(devices => [...devices.filter(d => d.device_id !== this.client.getDeviceId()), {
|
|
|
|
|
device_id: this.client.getDeviceId(),
|
|
|
|
|
session_id: this.client.getSessionId(),
|
|
|
|
|
expires_ts: Date.now() + DEVICE_TIMEOUT,
|
|
|
|
|
feeds: this.getLocalFeeds().map(feed => ({
|
|
|
|
|
purpose: feed.purpose
|
|
|
|
|
}))
|
|
|
|
|
// TODO: Add data channels
|
|
|
|
|
}]);
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
|
2026-09-12 23:57:45 -04:00
|
|
|
async updateMemberState() {
|
|
|
|
|
// Clear the old update interval before proceeding
|
|
|
|
|
if (this.resendMemberStateTimer !== null) {
|
|
|
|
|
clearInterval(this.resendMemberStateTimer);
|
|
|
|
|
this.resendMemberStateTimer = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.state === GroupCallState.Entered) {
|
|
|
|
|
// Add the local device
|
|
|
|
|
await this.addDeviceToMemberState();
|
|
|
|
|
|
|
|
|
|
// Resend the state event every so often so it doesn't become stale
|
|
|
|
|
this.resendMemberStateTimer = setInterval(async () => {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.log(`GroupCall ${this.groupCallId} updateMemberState() resending call member state"`);
|
2026-09-12 23:57:45 -04:00
|
|
|
try {
|
|
|
|
|
await this.addDeviceToMemberState();
|
|
|
|
|
} catch (e) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.error(`GroupCall ${this.groupCallId} updateMemberState() failed to resend call member state`, e);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
}, DEVICE_TIMEOUT * 3 / 4);
|
|
|
|
|
} else {
|
|
|
|
|
// Remove the local device
|
|
|
|
|
await this.updateDevices(devices => devices.filter(d => d.device_id !== this.client.getDeviceId()), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cleans up our member state by filtering out logged out devices, inactive
|
|
|
|
|
* devices, and our own device (if we know we haven't entered).
|
|
|
|
|
*/
|
|
|
|
|
async cleanMemberState() {
|
|
|
|
|
const {
|
|
|
|
|
devices: myDevices
|
|
|
|
|
} = await this.client.getDevices();
|
|
|
|
|
const deviceMap = new Map(myDevices.map(d => [d.device_id, d]));
|
|
|
|
|
|
|
|
|
|
// updateDevices takes care of filtering out inactive devices for us
|
|
|
|
|
await this.updateDevices(devices => {
|
|
|
|
|
const newDevices = devices.filter(d => {
|
|
|
|
|
const device = deviceMap.get(d.device_id);
|
2026-09-13 14:14:38 -04:00
|
|
|
return (device === null || device === void 0 ? void 0 : device.last_seen_ts) !== undefined && !(d.device_id === this.client.getDeviceId() && this.state !== GroupCallState.Entered && !this.enteredViaAnotherSession);
|
2026-09-12 23:57:45 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Skip the update if the devices are unchanged
|
|
|
|
|
return newDevices.length === devices.length ? null : newDevices;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
getGroupCallStats() {
|
|
|
|
|
if (this.stats === undefined) {
|
|
|
|
|
const userID = this.client.getUserId() || "unknown";
|
2026-09-13 14:14:38 -04:00
|
|
|
this.stats = new _groupCallStats.GroupCallStats(this.groupCallId, userID, this.statsCollectIntervalTime);
|
|
|
|
|
this.stats.reports.on(_statsReport.StatsReport.CONNECTION_STATS, this.onConnectionStats);
|
|
|
|
|
this.stats.reports.on(_statsReport.StatsReport.BYTE_SENT_STATS, this.onByteSentStats);
|
|
|
|
|
this.stats.reports.on(_statsReport.StatsReport.SUMMARY_STATS, this.onSummaryStats);
|
|
|
|
|
this.stats.reports.on(_statsReport.StatsReport.CALL_FEED_REPORT, this.onCallFeedReport);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
return this.stats;
|
|
|
|
|
}
|
|
|
|
|
setGroupCallStatsInterval(interval) {
|
|
|
|
|
this.statsCollectIntervalTime = interval;
|
|
|
|
|
if (this.stats !== undefined) {
|
|
|
|
|
this.stats.stop();
|
|
|
|
|
this.stats.setInterval(interval);
|
|
|
|
|
if (interval > 0) {
|
|
|
|
|
this.stats.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.GroupCall = GroupCall;
|
2026-09-12 23:57:45 -04:00
|
|
|
//# sourceMappingURL=groupCall.js.map
|