🐛 Bug fixes of webrtc

This commit is contained in:
2025-10-19 18:22:03 +08:00
parent e96b1fd9d4
commit 0910be88ef
3 changed files with 136 additions and 22 deletions

View File

@@ -120,14 +120,16 @@ class CallNotifier extends _$CallNotifier {
// Add local participant immediately when WebRTC is initialized
final userinfo = ref.watch(userInfoProvider);
_addLocalParticipant(userinfo.value!);
if (userinfo.value != null) {
_addLocalParticipant(userinfo.value!);
}
}
void _addLocalParticipant(SnAccount userinfo) {
if (_webrtcManager == null) return;
// Remove any existing local participant first
_participants.removeWhere((p) => p.participant.name == 'You');
_participants.removeWhere((p) => p.participant.identity == userinfo.id);
// Add local participant (current user)
final localParticipant = CallParticipantLive(
@@ -154,12 +156,16 @@ class CallNotifier extends _$CallNotifier {
final webrtcParticipants = _webrtcManager!.participants;
// Get the local participant (should be the first one)
final localParticipant =
_participants.isNotEmpty && _participants[0].participant.name == 'You'
// Always ensure local participant exists
final existingLocalParticipant =
_participants.isNotEmpty &&
_participants[0].remoteParticipant.id == _webrtcManager!.roomId
? _participants[0]
: null;
final localParticipant =
existingLocalParticipant ?? _createLocalParticipant();
// Add remote participants
final remoteParticipants =
webrtcParticipants.map((p) {
@@ -179,14 +185,63 @@ class CallNotifier extends _$CallNotifier {
}).toList();
// Combine local participant with remote participants
_participants =
localParticipant != null
? [localParticipant, ...remoteParticipants]
: remoteParticipants;
_participants = [localParticipant, ...remoteParticipants];
state = state.copyWith();
}
CallParticipantLive _createLocalParticipant() {
return CallParticipantLive(
participant: CallParticipant(
identity: _webrtcManager!.roomId, // Use roomId as local identity
name: 'You',
accountId: '',
account: null,
joinedAt: DateTime.now(),
),
remoteParticipant: WebRTCParticipant(
id: _webrtcManager!.roomId,
name: 'You',
userinfo: SnAccount(
id: '',
name: '',
nick: '',
language: '',
isSuperuser: false,
automatedId: null,
profile: SnAccountProfile(
id: '',
firstName: '',
middleName: '',
lastName: '',
bio: '',
gender: '',
pronouns: '',
location: '',
timeZone: '',
links: [],
experience: 0,
level: 0,
socialCredits: 0,
socialCreditsLevel: 0,
levelingProgress: 0,
picture: null,
background: null,
verification: null,
usernameColor: null,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
deletedAt: null,
),
perkSubscription: null,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
deletedAt: null,
),
)..remoteStream = _webrtcManager!.localStream, // Access local stream
);
}
Future<void> joinRoom(String roomId) async {
if (_roomId == roomId && _webrtcManager != null) {
talker.info('[Call] Call skipped. Already connected to this room');
@@ -258,12 +313,24 @@ class CallNotifier extends _$CallNotifier {
final target = !state.isMicrophoneEnabled;
state = state.copyWith(isMicrophoneEnabled: target);
await _webrtcManager?.toggleMicrophone(target);
// Update local participant's audio state
if (_participants.isNotEmpty) {
_participants[0].remoteParticipant.isAudioEnabled = target;
state = state.copyWith(); // Trigger UI update
}
}
Future<void> toggleCamera() async {
final target = !state.isCameraEnabled;
state = state.copyWith(isCameraEnabled: target);
await _webrtcManager?.toggleCamera(target);
// Update local participant's video state
if (_participants.isNotEmpty) {
_participants[0].remoteParticipant.isVideoEnabled = target;
state = state.copyWith(); // Trigger UI update
}
}
Future<void> toggleScreenShare(BuildContext context) async {