Skip to content

Commit

Permalink
refactor: make voip.calls and voip.groupCalls simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
td-famedly committed May 15, 2024
1 parent 9217231 commit 3330956
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 108 deletions.
8 changes: 5 additions & 3 deletions lib/src/voip/call_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CallSession {
String? get groupCallId => opts.groupCallId;
String get callId => opts.callId;
String get localPartyId => opts.localPartyId;
VoipId get voipId => VoipId(roomId: room.id, callId: callId);

CallDirection get direction => opts.dir;

Expand Down Expand Up @@ -216,8 +217,9 @@ class CallSession {
final prevCallId = voip.incomingCallRoomId[room.id];
if (prevCallId != null) {
// This is probably an outbound call, but we already have a incoming invite, so let's terminate it.
final prevCall =
voip.calls[VoipId(roomId: room.id, callId: prevCallId)];
final prevCall = voip.calls.singleWhereOrNull((element) =>
element.room.id == room.id && element.callId == prevCallId);

if (prevCall != null) {
if (prevCall._inviteOrAnswerSent) {
Logs().d('[glare] invite or answer sent, lex compare now');
Expand Down Expand Up @@ -975,7 +977,7 @@ class CallSession {
voip.incomingCallRoomId.removeWhere((key, value) => value == callId);
}

voip.calls.removeWhere((key, value) => key.callId == callId);
voip.calls.remove(this);

await cleanUp();
if (shouldEmit) {
Expand Down
17 changes: 15 additions & 2 deletions lib/src/voip/group_call_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class GroupCallSession {
final Client client;
final VoIP voip;
final Room room;
VoipId get voipId => VoipId(
roomId: room.id,
callId: groupCallId,
callBackendType: backend.type,
application: application,
scope: scope,
);

/// is a list of backend to allow passing multiple backend in the future
/// we use the first backend everywhere as of now
Expand Down Expand Up @@ -131,7 +138,13 @@ class GroupCallSession {

await backend.setupP2PCallsWithExistingMembers(this);

voip.currentGroupCID = VoipId(roomId: room.id, callId: groupCallId);
voip.currentGroupCID = VoipId(
roomId: room.id,
callId: groupCallId,
callBackendType: backend.type,
application: application,
scope: scope,
);

await voip.delegate.handleNewGroupCall(this);
}
Expand All @@ -142,7 +155,7 @@ class GroupCallSession {
setState(GroupCallState.localCallFeedUninitialized);
voip.currentGroupCID = null;
_participants.clear();
voip.groupCalls.remove(VoipId(roomId: room.id, callId: groupCallId));
voip.groupCalls.remove(this);
await voip.delegate.handleGroupCallEnded(this);
_resendMemberStateEventTimer?.cancel();
setState(GroupCallState.ended);
Expand Down
35 changes: 22 additions & 13 deletions lib/src/voip/models/voip_id.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
class VoipId {
final String roomId;
final String callId;
final String? callBackendType;
final String? application;
final String? scope;

String get id => '$roomId:$callId';

factory VoipId.fromId(String id) {
final int lastIndex = id.lastIndexOf(':');
return VoipId(
roomId: id.substring(0, lastIndex),
callId: id.substring(lastIndex + 1),
);
}

VoipId({required this.roomId, required this.callId});
VoipId({
required this.roomId,
required this.callId,
this.callBackendType,
this.application,
this.scope,
});

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is VoipId && roomId == other.roomId && callId == other.callId;
other is VoipId &&
roomId == other.roomId &&
callId == other.callId &&
callBackendType == other.callBackendType &&
application == other.application &&
scope == other.scope;

@override
int get hashCode => roomId.hashCode ^ callId.hashCode;
int get hashCode =>
roomId.hashCode ^
callId.hashCode ^
callBackendType.hashCode ^
application.hashCode ^
scope.hashCode;
}
11 changes: 11 additions & 0 deletions lib/src/voip/utils/famedly_call_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ extension FamedlyCallMemberEventsExtension on Room {
return mem ?? [];
}

/// returns a list of memberships in the room for `callParticipant`
List<CallMembership> getCallMembershipsForUserWithDeviceId(
CallParticipant callParticipant,
) {
final userMems = getCallMembershipsForUser(callParticipant.userId);
final mem = userMems
.where((element) => element.deviceId == callParticipant.deviceId)
.toList();
return mem;
}

/// returns the user count (not sessions, yet) for the group call with id: `groupCallId`.
/// returns 0 if group call not found
int groupCallParticipantCount(String groupCallId) {
Expand Down
Loading

0 comments on commit 3330956

Please sign in to comment.