- LiveKit/MatrixRTC voice+video calls with full call screen UI - Incoming call overlay (accept/decline) - Media upload/download — file picker, image rendering, file download - Spaces navigation — space list + expandable child rooms - Drift persistence — rooms + messages written on every sync - Sync persistence auto-starts on login and session restore - Chat: typing indicators, long-press menu, reply, emoji reactions - User search dialog + start DM from rooms screen - Android: INTERNET + CAMERA + RECORD_AUDIO permissions in main manifest - Emoji picker for reactions Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
120 lines
3.7 KiB
Dart
120 lines
3.7 KiB
Dart
// Version: 1.1.0 | Created: 2026-04-01
|
|
// MatrixRTC repository — handles outgoing call invites and detects incoming
|
|
// calls via m.call.invite events per MSC4143 (MatrixRTC spec).
|
|
//
|
|
// Incoming calls are surfaced via the incomingCallStream so the
|
|
// IncomingCallOverlay can react to them.
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../../../core/auth/auth_notifier.dart';
|
|
import '../../../core/auth/auth_state.dart';
|
|
import '../../../core/network/matrix_client.dart';
|
|
import '../domain/incoming_call.dart';
|
|
|
|
part 'matrixrtc_repository.g.dart';
|
|
|
|
/// Repository for sending and receiving MatrixRTC call signalling events.
|
|
class MatrixRtcRepository {
|
|
MatrixRtcRepository({required Client client, required String? myUserId})
|
|
: _client = client,
|
|
_myUserId = myUserId;
|
|
|
|
final Client _client;
|
|
final String? _myUserId;
|
|
|
|
final _incomingCallController = StreamController<IncomingCall>.broadcast();
|
|
|
|
/// Emits whenever an incoming call invite arrives for the local user.
|
|
Stream<IncomingCall> get incomingCallStream => _incomingCallController.stream;
|
|
|
|
StreamSubscription<EventUpdate>? _eventSubscription;
|
|
|
|
/// Begin listening for incoming m.call.invite events.
|
|
void startListening() {
|
|
_eventSubscription?.cancel();
|
|
_eventSubscription = _client.onEvent.stream.listen(_onEvent);
|
|
}
|
|
|
|
void stopListening() {
|
|
_eventSubscription?.cancel();
|
|
_eventSubscription = null;
|
|
_incomingCallController.close();
|
|
}
|
|
|
|
/// Send a call invite to [roomId] to start a voice or video call.
|
|
///
|
|
/// Uses the standard m.call.invite event type. The room's other participants
|
|
/// will receive this via their sync stream.
|
|
Future<void> sendCallInvite({
|
|
required String roomId,
|
|
required bool isVideo,
|
|
}) async {
|
|
final room = _client.getRoomById(roomId);
|
|
if (room == null) return;
|
|
|
|
final callId = 'call_${DateTime.now().millisecondsSinceEpoch}';
|
|
|
|
await room.sendEvent({
|
|
'msgtype': 'm.call.invite',
|
|
'call_id': callId,
|
|
'lifetime': 60000, // 60 seconds before invite expires
|
|
'offer': {
|
|
'type': 'offer',
|
|
'sdp': '', // SDP is populated by LiveKit once connected
|
|
},
|
|
'version': '1',
|
|
'invitee': null, // null = invite entire room
|
|
'm.intentional_mentions': {'user_ids': [], 'room': false},
|
|
}, type: 'm.call.invite');
|
|
}
|
|
|
|
void _onEvent(EventUpdate update) {
|
|
if (update.type != EventUpdateType.timeline) return;
|
|
if (update.content['type'] != 'm.call.invite') return;
|
|
|
|
final senderId = update.content['sender'] as String?;
|
|
// Ignore our own invites.
|
|
if (senderId == _myUserId) return;
|
|
|
|
final roomId = update.roomID;
|
|
final content = update.content['content'] as Map<String, dynamic>?;
|
|
if (content == null) return;
|
|
|
|
final callId = content['call_id'] as String?;
|
|
if (callId == null) return;
|
|
|
|
final room = _client.getRoomById(roomId);
|
|
final senderProfile = room?.unsafeGetUserFromMemoryOrFallback(
|
|
senderId ?? '',
|
|
);
|
|
|
|
_incomingCallController.add(
|
|
IncomingCall(
|
|
callId: callId,
|
|
roomId: roomId,
|
|
callerId: senderId ?? '',
|
|
callerDisplayName:
|
|
senderProfile?.displayName ?? senderId?.split(':').first ?? '',
|
|
callerAvatarUrl: senderProfile?.avatarUrl?.toString(),
|
|
isVideo: (content['offer'] != null),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@Riverpod(keepAlive: true)
|
|
MatrixRtcRepository matrixRtcRepository(Ref ref) {
|
|
final client = ref.watch(matrixClientProvider);
|
|
final authState = ref.watch(authProvider);
|
|
final myUserId = authState is AuthAuthenticated ? authState.userId : null;
|
|
|
|
final repo = MatrixRtcRepository(client: client, myUserId: myUserId);
|
|
repo.startListening();
|
|
ref.onDispose(repo.stopListening);
|
|
return repo;
|
|
}
|