forked from matrix-org/matrix-js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupCall.ts
More file actions
1267 lines (1014 loc) · 43.8 KB
/
Copy pathgroupCall.ts
File metadata and controls
1267 lines (1014 loc) · 43.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { TypedEventEmitter } from "../models/typed-event-emitter";
import { CallFeed, SPEAKING_THRESHOLD } from "./callFeed";
import { MatrixClient } from "../client";
import { CallErrorCode, CallEvent, CallState, genCallID, MatrixCall, setTracksEnabled } from "./call";
import { RoomMember } from "../models/room-member";
import { Room } from "../models/room";
import { logger } from "../logger";
import { ReEmitter } from "../ReEmitter";
import { SDPStreamMetadataPurpose } from "./callEventTypes";
import { createNewMatrixCall } from "./call";
import { ISendEventResponse } from "../@types/requests";
import { MatrixEvent } from "../models/event";
import { EventType } from "../@types/event";
import { CallEventHandlerEvent } from "./callEventHandler";
import { GroupCallEventHandlerEvent } from "./groupCallEventHandler";
export enum GroupCallIntent {
Ring = "m.ring",
Prompt = "m.prompt",
Room = "m.room",
}
export enum GroupCallType {
Video = "m.video",
Voice = "m.voice",
}
export enum GroupCallTerminationReason {
CallEnded = "call_ended",
}
export enum GroupCallEvent {
GroupCallStateChanged = "group_call_state_changed",
ActiveSpeakerChanged = "active_speaker_changed",
CallsChanged = "calls_changed",
UserMediaFeedsChanged = "user_media_feeds_changed",
ScreenshareFeedsChanged = "screenshare_feeds_changed",
LocalScreenshareStateChanged = "local_screenshare_state_changed",
LocalMuteStateChanged = "local_mute_state_changed",
ParticipantsChanged = "participants_changed",
Error = "error",
}
export type GroupCallEventHandlerMap = {
[GroupCallEvent.GroupCallStateChanged]: (newState: GroupCallState, oldState: GroupCallState) => void;
[GroupCallEvent.ActiveSpeakerChanged]: (activeSpeaker: string) => void;
[GroupCallEvent.CallsChanged]: (calls: MatrixCall[]) => void;
[GroupCallEvent.UserMediaFeedsChanged]: (feeds: CallFeed[]) => void;
[GroupCallEvent.ScreenshareFeedsChanged]: (feeds: CallFeed[]) => void;
[GroupCallEvent.LocalScreenshareStateChanged]: (
isScreensharing: boolean, feed: CallFeed, sourceId: string,
) => void;
[GroupCallEvent.LocalMuteStateChanged]: (audioMuted: boolean, videoMuted: boolean) => void;
[GroupCallEvent.ParticipantsChanged]: (participants: RoomMember[]) => void;
[GroupCallEvent.Error]: (error: GroupCallError) => void;
};
export enum GroupCallErrorCode {
NoUserMedia = "no_user_media",
UnknownDevice = "unknown_device",
PlaceCallFailed = "place_call_failed"
}
export class GroupCallError extends Error {
code: string;
constructor(code: GroupCallErrorCode, msg: string, err?: Error) {
// Still don't think there's any way to have proper nested errors
if (err) {
super(msg + ": " + err);
} else {
super(msg);
}
this.code = code;
}
}
export class GroupCallUnknownDeviceError extends GroupCallError {
constructor(public userId: string) {
super(GroupCallErrorCode.UnknownDevice, "No device found for " + userId);
}
}
export class OtherUserSpeakingError extends Error {
constructor() {
super("Cannot unmute: another user is speaking");
}
}
export interface IGroupCallDataChannelOptions {
ordered: boolean;
maxPacketLifeTime: number;
maxRetransmits: number;
protocol: string;
}
export interface IGroupCallRoomMemberFeed {
purpose: SDPStreamMetadataPurpose;
// TODO: Sources for adaptive bitrate
}
export interface IGroupCallRoomMemberDevice {
"device_id": string;
"session_id": string;
"feeds": IGroupCallRoomMemberFeed[];
}
export interface IGroupCallRoomMemberCallState {
"m.call_id": string;
"m.foci"?: string[];
"m.devices": IGroupCallRoomMemberDevice[];
}
export interface IGroupCallRoomMemberState {
"m.calls": IGroupCallRoomMemberCallState[];
"m.expires_ts": number;
}
export enum GroupCallState {
LocalCallFeedUninitialized = "local_call_feed_uninitialized",
InitializingLocalCallFeed = "initializing_local_call_feed",
LocalCallFeedInitialized = "local_call_feed_initialized",
Entering = "entering",
Entered = "entered",
Ended = "ended",
}
interface ICallHandlers {
onCallFeedsChanged: (feeds: CallFeed[]) => void;
onCallStateChanged: (state: CallState, oldState: CallState) => void;
onCallHangup: (call: MatrixCall) => void;
onCallReplaced: (newCall: MatrixCall) => void;
}
const CALL_MEMBER_STATE_TIMEOUT = 1000 * 60 * 60; // 1 hour
const callMemberStateIsExpired = (event: MatrixEvent): boolean => {
const now = Date.now();
const content = event?.getContent<IGroupCallRoomMemberState>() ?? {};
const expiresAt = typeof content["m.expires_ts"] === "number" ? content["m.expires_ts"] : -Infinity;
return expiresAt <= now;
};
function getCallUserId(call: MatrixCall): string | null {
return call.getOpponentMember()?.userId || call.invitee || null;
}
/**
* Returns a call feed for passing to a new call in the group call. The media
* This could be either return the passed feed as-is or a clone, depending on the
* platform.
* @returns CallFeed
*/
function feedForNewCallFromFeed(feed: CallFeed): CallFeed {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
// Safari can't send a MediaStream to multiple sources, so we clone it,
// however cloning mediastreams on Chrome appears to cause the audio renderer
// to become unstable and hang: https://github.com/vector-im/element-call/issues/267
// It's a bit arbitrary what we do for other browsers: I've made Safari the special
// case on a somewhat arbitrary basis.
// To retest later to see if this hack is still necessary:
// * In Safari, you should be able to have a group call with 2 other people and both
// of them see your video stream (either desktop or mobile Safari)
// * In Chrome, you should be able to enter a call and then go to youtube and play
// a video (both desktop & Android Chrome, although in Android you may have to
// open YouTube in incognito mode to avoid being redirected to the app.)
if (isSafari) {
return feed.clone();
}
return feed;
}
export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventHandlerMap> {
// Config
public activeSpeakerInterval = 1000;
public retryCallInterval = 5000;
public participantTimeout = 1000 * 15;
public pttMaxTransmitTime = 1000 * 20;
public state = GroupCallState.LocalCallFeedUninitialized;
public activeSpeaker?: string; // userId
public localCallFeed?: CallFeed;
public localScreenshareFeed?: CallFeed;
public localDesktopCapturerSourceId?: string;
public calls: MatrixCall[] = [];
public participants: RoomMember[] = [];
public userMediaFeeds: CallFeed[] = [];
public screenshareFeeds: CallFeed[] = [];
public groupCallId: string;
private callHandlers: Map<string, ICallHandlers> = new Map();
private activeSpeakerLoopTimeout?: ReturnType<typeof setTimeout>;
private retryCallLoopTimeout?: ReturnType<typeof setTimeout>;
private retryCallCounts: Map<string, number> = new Map();
private reEmitter: ReEmitter;
private transmitTimer: ReturnType<typeof setTimeout> | null = null;
private memberStateExpirationTimers: Map<string, ReturnType<typeof setTimeout>> = new Map();
private resendMemberStateTimer: ReturnType<typeof setTimeout> | null = null;
constructor(
private client: MatrixClient,
public room: Room,
public type: GroupCallType,
public isPtt: boolean,
public intent: GroupCallIntent,
groupCallId?: string,
private dataChannelsEnabled?: boolean,
private dataChannelOptions?: IGroupCallDataChannelOptions,
) {
super();
this.reEmitter = new ReEmitter(this);
this.groupCallId = groupCallId || genCallID();
for (const stateEvent of this.getMemberStateEvents()) {
this.onMemberStateChanged(stateEvent);
}
}
public async create() {
this.client.groupCallEventHandler.groupCalls.set(this.room.roomId, this);
await this.client.sendStateEvent(
this.room.roomId,
EventType.GroupCallPrefix,
{
"m.intent": this.intent,
"m.type": this.type,
"io.element.ptt": this.isPtt,
// TODO: Specify datachannels
"dataChannelsEnabled": this.dataChannelsEnabled,
"dataChannelOptions": this.dataChannelOptions,
},
this.groupCallId,
);
return this;
}
private setState(newState: GroupCallState): void {
const oldState = this.state;
this.state = newState;
this.emit(GroupCallEvent.GroupCallStateChanged, newState, oldState);
}
public getLocalFeeds(): CallFeed[] {
const feeds = [];
if (this.localCallFeed) feeds.push(this.localCallFeed);
if (this.localScreenshareFeed) feeds.push(this.localScreenshareFeed);
return feeds;
}
public hasLocalParticipant(): boolean {
const userId = this.client.getUserId();
return this.participants.some((member) => member.userId === userId);
}
public async initLocalCallFeed(): Promise<CallFeed> {
logger.log(`groupCall ${this.groupCallId} initLocalCallFeed`);
if (this.state !== GroupCallState.LocalCallFeedUninitialized) {
throw new Error(`Cannot initialize local call feed in the "${this.state}" state.`);
}
this.setState(GroupCallState.InitializingLocalCallFeed);
let stream: MediaStream;
try {
stream = await this.client.getMediaHandler().getUserMediaStream(true, this.type === GroupCallType.Video);
} catch (error) {
this.setState(GroupCallState.LocalCallFeedUninitialized);
throw error;
}
// start muted on ptt calls
if (this.isPtt) {
setTracksEnabled(stream.getAudioTracks(), false);
}
const userId = this.client.getUserId();
const callFeed = new CallFeed({
client: this.client,
roomId: this.room.roomId,
userId,
stream,
purpose: SDPStreamMetadataPurpose.Usermedia,
audioMuted: stream.getAudioTracks().length === 0 || this.isPtt,
videoMuted: stream.getVideoTracks().length === 0,
});
this.localCallFeed = callFeed;
this.addUserMediaFeed(callFeed);
this.setState(GroupCallState.LocalCallFeedInitialized);
return callFeed;
}
public async updateLocalUsermediaStream(stream: MediaStream) {
if (this.localCallFeed) {
const oldStream = this.localCallFeed.stream;
this.localCallFeed.setNewStream(stream);
const micShouldBeMuted = this.localCallFeed.isAudioMuted();
const vidShouldBeMuted = this.localCallFeed.isVideoMuted();
logger.log(`groupCall ${this.groupCallId} updateLocalUsermediaStream oldStream ${
oldStream.id} newStream ${stream.id} micShouldBeMuted ${
micShouldBeMuted} vidShouldBeMuted ${vidShouldBeMuted}`);
setTracksEnabled(stream.getAudioTracks(), !micShouldBeMuted);
setTracksEnabled(stream.getVideoTracks(), !vidShouldBeMuted);
this.client.getMediaHandler().stopUserMediaStream(oldStream);
}
}
public async enter() {
if (!(this.state === GroupCallState.LocalCallFeedUninitialized ||
this.state === GroupCallState.LocalCallFeedInitialized)) {
throw new Error(`Cannot enter call in the "${this.state}" state`);
}
if (this.state === GroupCallState.LocalCallFeedUninitialized) {
await this.initLocalCallFeed();
}
this.addParticipant(this.room.getMember(this.client.getUserId()));
await this.sendMemberStateEvent();
this.activeSpeaker = null;
this.setState(GroupCallState.Entered);
logger.log(`Entered group call ${this.groupCallId}`);
this.client.on(CallEventHandlerEvent.Incoming, this.onIncomingCall);
const calls = this.client.callEventHandler.calls.values();
for (const call of calls) {
this.onIncomingCall(call);
}
// Set up participants for the members currently in the room.
// Other members will be picked up by the RoomState.members event.
for (const stateEvent of this.getMemberStateEvents()) {
this.onMemberStateChanged(stateEvent);
}
this.retryCallLoopTimeout = setTimeout(this.onRetryCallLoop, this.retryCallInterval);
this.onActiveSpeakerLoop();
}
private dispose() {
if (this.localCallFeed) {
this.removeUserMediaFeed(this.localCallFeed);
this.localCallFeed = null;
}
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.state !== GroupCallState.Entered) {
return;
}
this.removeParticipant(this.room.getMember(this.client.getUserId()));
this.removeMemberStateEvent();
while (this.calls.length > 0) {
this.removeCall(this.calls[this.calls.length - 1], CallErrorCode.UserHangup);
}
this.activeSpeaker = null;
clearTimeout(this.activeSpeakerLoopTimeout);
this.retryCallCounts.clear();
clearTimeout(this.retryCallLoopTimeout);
if (this.transmitTimer !== null) {
clearTimeout(this.transmitTimer);
this.transmitTimer = null;
}
this.client.removeListener(CallEventHandlerEvent.Incoming, this.onIncomingCall);
}
public leave() {
if (this.transmitTimer !== null) {
clearTimeout(this.transmitTimer);
this.transmitTimer = null;
}
this.dispose();
this.setState(GroupCallState.LocalCallFeedUninitialized);
}
public async terminate(emitStateEvent = true) {
this.dispose();
if (this.transmitTimer !== null) {
clearTimeout(this.transmitTimer);
this.transmitTimer = null;
}
this.participants = [];
this.client.groupCallEventHandler.groupCalls.delete(this.room.roomId);
if (emitStateEvent) {
const existingStateEvent = this.room.currentState.getStateEvents(
EventType.GroupCallPrefix, this.groupCallId,
);
await this.client.sendStateEvent(
this.room.roomId,
EventType.GroupCallPrefix,
{
...existingStateEvent.getContent(),
["m.terminated"]: GroupCallTerminationReason.CallEnded,
},
this.groupCallId,
);
}
this.client.emit(GroupCallEventHandlerEvent.Ended, this);
this.setState(GroupCallState.Ended);
}
/**
* Local Usermedia
*/
public isLocalVideoMuted() {
if (this.localCallFeed) {
return this.localCallFeed.isVideoMuted();
}
return true;
}
public isMicrophoneMuted() {
if (this.localCallFeed) {
return this.localCallFeed.isAudioMuted();
}
return true;
}
/**
* Sets the mute state of the local participants's microphone.
* @param {boolean} muted Whether to mute the microphone
* @returns {Promise<boolean>} Whether muting/unmuting was successful
*/
public async setMicrophoneMuted(muted: boolean): Promise<boolean> {
// 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()) {
clearTimeout(this.transmitTimer);
this.transmitTimer = null;
}
}
for (const call of this.calls) {
call.localUsermediaFeed.setAudioMuted(muted);
}
if (sendUpdatesBefore) {
try {
await Promise.all(this.calls.map(c => c.sendMetadataUpdate()));
} catch (e) {
logger.info("Failed to send one or more metadata updates", e);
}
}
if (this.localCallFeed) {
logger.log(`groupCall ${this.groupCallId} setMicrophoneMuted stream ${
this.localCallFeed.stream.id} muted ${muted}`);
this.localCallFeed.setAudioMuted(muted);
// 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.
setTracksEnabled(this.localCallFeed.stream.getAudioTracks(), !muted);
}
for (const call of this.calls) {
setTracksEnabled(call.localUsermediaFeed.stream.getAudioTracks(), !muted);
}
if (!sendUpdatesBefore) {
try {
await Promise.all(this.calls.map(c => c.sendMetadataUpdate()));
} catch (e) {
logger.info("Failed to send one or more metadata updates", e);
}
}
this.emit(GroupCallEvent.LocalMuteStateChanged, muted, this.isLocalVideoMuted());
return true;
}
/**
* Sets the mute state of the local participants's video.
* @param {boolean} muted Whether to mute the video
* @returns {Promise<boolean>} Whether muting/unmuting was successful
*/
public async setLocalVideoMuted(muted: boolean): Promise<boolean> {
// 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) {
logger.log(`groupCall ${this.groupCallId} setLocalVideoMuted stream ${
this.localCallFeed.stream.id} muted ${muted}`);
this.localCallFeed.setVideoMuted(muted);
setTracksEnabled(this.localCallFeed.stream.getVideoTracks(), !muted);
}
for (const call of this.calls) {
call.setLocalVideoMuted(muted);
}
this.emit(GroupCallEvent.LocalMuteStateChanged, this.isMicrophoneMuted(), muted);
return true;
}
public async setScreensharingEnabled(
enabled: boolean, desktopCapturerSourceId?: string,
): Promise<boolean> {
if (enabled === this.isScreensharing()) {
return enabled;
}
if (enabled) {
try {
logger.log("Asking for screensharing permissions...");
const stream = await this.client.getMediaHandler().getScreensharingStream(desktopCapturerSourceId);
for (const track of stream.getTracks()) {
const onTrackEnded = () => {
this.setScreensharingEnabled(false);
track.removeEventListener("ended", onTrackEnded);
};
track.addEventListener("ended", onTrackEnded);
}
logger.log("Screensharing permissions granted. Setting screensharing enabled on all calls");
this.localDesktopCapturerSourceId = desktopCapturerSourceId;
this.localScreenshareFeed = new CallFeed({
client: this.client,
roomId: this.room.roomId,
userId: this.client.getUserId(),
stream,
purpose: SDPStreamMetadataPurpose.Screenshare,
audioMuted: false,
videoMuted: false,
});
this.addScreenshareFeed(this.localScreenshareFeed);
this.emit(
GroupCallEvent.LocalScreenshareStateChanged,
true,
this.localScreenshareFeed,
this.localDesktopCapturerSourceId,
);
// TODO: handle errors
await Promise.all(this.calls.map(call => call.pushLocalFeed(
feedForNewCallFromFeed(this.localScreenshareFeed),
)));
await this.sendMemberStateEvent();
return true;
} catch (error) {
logger.error("Enabling screensharing error", error);
this.emit(GroupCallEvent.Error,
new GroupCallError(GroupCallErrorCode.NoUserMedia, "Failed to get screen-sharing stream: ", error),
);
return false;
}
} else {
await Promise.all(this.calls.map(call => call.removeLocalFeed(call.localScreensharingFeed)));
this.client.getMediaHandler().stopScreensharingStream(this.localScreenshareFeed.stream);
this.removeScreenshareFeed(this.localScreenshareFeed);
this.localScreenshareFeed = undefined;
this.localDesktopCapturerSourceId = undefined;
await this.sendMemberStateEvent();
this.emit(GroupCallEvent.LocalScreenshareStateChanged, false, undefined, undefined);
return false;
}
}
public isScreensharing(): boolean {
return !!this.localScreenshareFeed;
}
/**
* 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.
*/
private onIncomingCall = (newCall: MatrixCall) => {
// The incoming calls may be for another room, which we will ignore.
if (newCall.roomId !== this.room.roomId) {
return;
}
if (newCall.state !== CallState.Ringing) {
logger.warn("Incoming call no longer in ringing state. Ignoring.");
return;
}
if (!newCall.groupCallId || newCall.groupCallId !== this.groupCallId) {
logger.log(`Incoming call with groupCallId ${
newCall.groupCallId} ignored because it doesn't match the current group call`);
newCall.reject();
return;
}
const opponentMemberId = newCall.getOpponentMember().userId;
const existingCall = this.getCallByUserId(opponentMemberId);
if (existingCall && existingCall.callId === newCall.callId) {
return;
}
logger.log(`GroupCall: incoming call from: ${opponentMemberId}`);
// we are handlng this call as a PTT call, so enable PTT semantics
newCall.isPtt = this.isPtt;
// Check if the user calling has an existing call and use this call instead.
if (existingCall) {
this.replaceCall(existingCall, newCall);
} else {
this.addCall(newCall);
}
newCall.answerWithCallFeeds(this.getLocalFeeds().map((feed) => feedForNewCallFromFeed(feed)));
};
/**
* Room Member State
*/
private getMemberStateEvents(): MatrixEvent[];
private getMemberStateEvents(userId: string): MatrixEvent | null;
private getMemberStateEvents(userId?: string): MatrixEvent[] | MatrixEvent | null {
if (userId != null) {
const event = this.room.currentState.getStateEvents(EventType.GroupCallMemberPrefix, userId);
return callMemberStateIsExpired(event) ? null : event;
} else {
return this.room.currentState.getStateEvents(EventType.GroupCallMemberPrefix)
.filter(event => !callMemberStateIsExpired(event));
}
}
private async sendMemberStateEvent(): Promise<ISendEventResponse> {
const send = () => this.updateMemberCallState({
"m.call_id": this.groupCallId,
"m.devices": [
{
"device_id": this.client.getDeviceId(),
"session_id": this.client.getSessionId(),
"feeds": this.getLocalFeeds().map((feed) => ({
purpose: feed.purpose,
})),
// TODO: Add data channels
},
],
// TODO "m.foci"
});
const res = await send();
// Resend the state event every so often so it doesn't become stale
this.resendMemberStateTimer = setInterval(async () => {
logger.log("Resending call member state");
await send();
}, CALL_MEMBER_STATE_TIMEOUT * 3 / 4);
return res;
}
private async removeMemberStateEvent(): Promise<ISendEventResponse> {
clearInterval(this.resendMemberStateTimer);
this.resendMemberStateTimer = null;
return await this.updateMemberCallState(undefined);
}
private async updateMemberCallState(memberCallState?: IGroupCallRoomMemberCallState): Promise<ISendEventResponse> {
const localUserId = this.client.getUserId();
const memberState = this.getMemberStateEvents(localUserId)?.getContent<IGroupCallRoomMemberState>();
let calls: IGroupCallRoomMemberCallState[] = [];
// Sanitize existing member state event
if (memberState && Array.isArray(memberState["m.calls"])) {
calls = memberState["m.calls"].filter((call) => !!call);
}
const existingCallIndex = calls.findIndex((call) => call && call["m.call_id"] === this.groupCallId);
if (existingCallIndex !== -1) {
if (memberCallState) {
calls.splice(existingCallIndex, 1, memberCallState);
} else {
calls.splice(existingCallIndex, 1);
}
} else if (memberCallState) {
calls.push(memberCallState);
}
const content = {
"m.calls": calls,
"m.expires_ts": Date.now() + CALL_MEMBER_STATE_TIMEOUT,
};
return this.client.sendStateEvent(this.room.roomId, EventType.GroupCallMemberPrefix, content, localUserId);
}
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;
const member = this.room.getMember(event.getStateKey());
if (!member) return;
const ignore = () => {
this.removeParticipant(member);
clearTimeout(this.memberStateExpirationTimers.get(member.userId));
this.memberStateExpirationTimers.delete(member.userId);
};
const content = event.getContent<IGroupCallRoomMemberState>();
const callsState = !callMemberStateIsExpired(event) && Array.isArray(content["m.calls"])
? content["m.calls"].filter((call) => call)
: []; // Ignore expired device data
if (callsState.length === 0) {
logger.log(`Ignoring member state from ${member.userId} member not in any calls.`);
ignore();
return;
}
// Currently we only support a single call per room. So grab the first call.
const callState = callsState[0];
const callId = callState["m.call_id"];
if (!callId) {
logger.warn(`Room member ${member.userId} does not have a valid m.call_id set. Ignoring.`);
ignore();
return;
}
if (callId !== this.groupCallId) {
logger.warn(`Call id ${callId} does not match group call id ${this.groupCallId}, ignoring.`);
ignore();
return;
}
this.addParticipant(member);
clearTimeout(this.memberStateExpirationTimers.get(member.userId));
this.memberStateExpirationTimers.set(member.userId, setTimeout(() => {
logger.warn(`Call member state for ${member.userId} has expired`);
this.removeParticipant(member);
}, content["m.expires_ts"] - Date.now()));
// Don't process your own member.
const localUserId = this.client.getUserId();
if (member.userId === localUserId) {
return;
}
if (this.state !== GroupCallState.Entered) {
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 (member.userId < localUserId) {
logger.log(`Waiting for ${member.userId} to send call invite.`);
return;
}
const opponentDevice = this.getDeviceForMember(member.userId);
if (!opponentDevice) {
logger.warn(`No opponent device found for ${member.userId}, ignoring.`);
this.emit(
GroupCallEvent.Error,
new GroupCallUnknownDeviceError(member.userId),
);
return;
}
const existingCall = this.getCallByUserId(member.userId);
if (
existingCall &&
existingCall.getOpponentSessionId() === opponentDevice.session_id
) {
return;
}
const newCall = createNewMatrixCall(
this.client,
this.room.roomId,
{
invitee: member.userId,
opponentDeviceId: opponentDevice.device_id,
opponentSessionId: opponentDevice.session_id,
groupCallId: this.groupCallId,
},
);
newCall.isPtt = this.isPtt;
const requestScreenshareFeed = opponentDevice.feeds.some(
(feed) => feed.purpose === SDPStreamMetadataPurpose.Screenshare);
try {
await newCall.placeCallWithCallFeeds(
this.getLocalFeeds().map(feed => feedForNewCallFromFeed(feed)),
requestScreenshareFeed,
);
} catch (e) {
logger.warn(`Failed to place call to ${member.userId}!`, e);
if (e.code === GroupCallErrorCode.UnknownDevice) {
this.emit(GroupCallEvent.Error, e);
} else {
this.emit(
GroupCallEvent.Error,
new GroupCallError(
GroupCallErrorCode.PlaceCallFailed,
`Failed to place call to ${member.userId}.`,
),
);
}
return;
}
if (this.dataChannelsEnabled) {
newCall.createDataChannel("datachannel", this.dataChannelOptions);
}
if (existingCall) {
this.replaceCall(existingCall, newCall, CallErrorCode.NewSession);
} else {
this.addCall(newCall);
}
};
public getDeviceForMember(userId: string): IGroupCallRoomMemberDevice {
const memberStateEvent = this.getMemberStateEvents(userId);
if (!memberStateEvent) {
return undefined;
}
const memberState = memberStateEvent.getContent<IGroupCallRoomMemberState>();
const memberGroupCallState = memberState["m.calls"]?.find(
(call) => call && call["m.call_id"] === this.groupCallId);
if (!memberGroupCallState) {
return undefined;
}
const memberDevices = memberGroupCallState["m.devices"];
if (!memberDevices || memberDevices.length === 0) {
return undefined;
}
// NOTE: For now we only support one device so we use the device id in the first source.
return memberDevices[0];
}
private onRetryCallLoop = () => {
for (const event of this.getMemberStateEvents()) {
const memberId = event.getStateKey();
const existingCall = this.calls.find((call) => getCallUserId(call) === memberId);
const retryCallCount = this.retryCallCounts.get(memberId) || 0;
if (!existingCall && retryCallCount < 3) {
this.retryCallCounts.set(memberId, retryCallCount + 1);
this.onMemberStateChanged(event);
}
}
this.retryCallLoopTimeout = setTimeout(this.onRetryCallLoop, this.retryCallInterval);
};
/**
* Call Event Handlers
*/
public getCallByUserId(userId: string): MatrixCall {
return this.calls.find((call) => getCallUserId(call) === userId);
}
private addCall(call: MatrixCall) {
this.calls.push(call);
this.initCall(call);
this.emit(GroupCallEvent.CallsChanged, this.calls);
}
private replaceCall(existingCall: MatrixCall, replacementCall: MatrixCall, hangupReason = CallErrorCode.Replaced) {
const existingCallIndex = this.calls.indexOf(existingCall);
if (existingCallIndex === -1) {
throw new Error("Couldn't find call to replace");
}
this.calls.splice(existingCallIndex, 1, replacementCall);
this.disposeCall(existingCall, hangupReason);
this.initCall(replacementCall);
this.emit(GroupCallEvent.CallsChanged, this.calls);
}
private removeCall(call: MatrixCall, hangupReason: CallErrorCode) {
this.disposeCall(call, hangupReason);
const callIndex = this.calls.indexOf(call);
if (callIndex === -1) {
throw new Error("Couldn't find call to remove");
}
this.calls.splice(callIndex, 1);
this.emit(GroupCallEvent.CallsChanged, this.calls);
}
private initCall(call: MatrixCall) {
const opponentMemberId = getCallUserId(call);
if (!opponentMemberId) {
throw new Error("Cannot init call without user id");
}
const onCallFeedsChanged = () => this.onCallFeedsChanged(call);
const onCallStateChanged =
(state: CallState, oldState: CallState) => this.onCallStateChanged(call, state, oldState);
const onCallHangup = this.onCallHangup;
const onCallReplaced = (newCall: MatrixCall) => this.replaceCall(call, newCall);
this.callHandlers.set(opponentMemberId, {
onCallFeedsChanged,
onCallStateChanged,
onCallHangup,
onCallReplaced,
});
call.on(CallEvent.FeedsChanged, onCallFeedsChanged);
call.on(CallEvent.State, onCallStateChanged);
call.on(CallEvent.Hangup, onCallHangup);
call.on(CallEvent.Replaced, onCallReplaced);