From aab5d1bf1c3c5a3a5ca1704d9c5ac7843200a0c1 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Wed, 6 Nov 2024 15:15:41 +0800 Subject: [PATCH] improvements. --- lib/src/context/media_device_context.dart | 11 +- lib/src/context/room_context.dart | 29 +++-- .../ui/builder/room/room_participants.dart | 3 +- .../participant/participant_status_bar.dart | 111 +++++++++--------- 4 files changed, 82 insertions(+), 72 deletions(-) diff --git a/lib/src/context/media_device_context.dart b/lib/src/context/media_device_context.dart index 5d90475..790bb3d 100644 --- a/lib/src/context/media_device_context.dart +++ b/lib/src/context/media_device_context.dart @@ -62,9 +62,12 @@ class MediaDeviceContext extends ChangeNotifier { LocalAudioTrack? get localAudioTrack => _roomCtx.localAudioTrack; + StreamSubscription? _deviceChangeSub; + Future loadDevices() async { _loadDevices(await Hardware.instance.enumerateDevices()); - Hardware.instance.onDeviceChange.stream.listen(_loadDevices); + _deviceChangeSub = + Hardware.instance.onDeviceChange.stream.listen(_loadDevices); } _loadDevices(List devices) { @@ -311,4 +314,10 @@ class MediaDeviceContext extends ChangeNotifier { } notifyListeners(); } + + @override + void dispose() { + super.dispose(); + _deviceChangeSub?.cancel(); + } } diff --git a/lib/src/context/room_context.dart b/lib/src/context/room_context.dart index a0fd67a..9320183 100644 --- a/lib/src/context/room_context.dart +++ b/lib/src/context/room_context.dart @@ -59,7 +59,7 @@ class RoomContext extends ChangeNotifier with ChatContextMixin { _listener = _room.createListener(); _listener ..on((event) { - Debug.event('RoomContext: RoomConnectedEvent'); + Debug.event('RoomContext: RoomConnectedEvent $roomName'); chatContextSetup(_listener, _room.localParticipant!); _connectionState = _room.connectionState; _connected = true; @@ -72,7 +72,7 @@ class RoomContext extends ChangeNotifier with ChatContextMixin { notifyListeners(); }) ..on((event) { - Debug.event('RoomContext: RoomDisconnectedEvent'); + Debug.event('RoomContext: RoomDisconnectedEvent $roomName'); _connectionState = _room.connectionState; chatContextSetup(null, null); _connected = false; @@ -81,18 +81,18 @@ class RoomContext extends ChangeNotifier with ChatContextMixin { notifyListeners(); }) ..on((event) { - Debug.event('RoomContext: RoomReconnectedEvent'); + Debug.event('RoomContext: RoomReconnectedEvent $roomName'); _connectionState = _room.connectionState; notifyListeners(); }) ..on((event) { - Debug.event('RoomContext: RoomReconnectingEvent'); + Debug.event('RoomContext: RoomReconnectingEvent $roomName'); _connectionState = _room.connectionState; notifyListeners(); }) ..on((event) { Debug.event( - 'RoomContext: RoomMetadataChangedEvent metadata = ${event.metadata}'); + 'RoomContext: RoomMetadataChangedEvent $roomName metadata = ${event.metadata}'); _roomMetadata = event.metadata; notifyListeners(); }) @@ -104,22 +104,22 @@ class RoomContext extends ChangeNotifier with ChatContextMixin { }) ..on((event) { Debug.event( - 'RoomContext: ParticipantConnectedEvent participant = ${event.participant.identity}'); + 'RoomContext: ParticipantConnectedEvent $roomName participant = ${event.participant.identity}'); _buildParticipants(); }) ..on((event) { Debug.event( - 'RoomContext: ParticipantDisconnectedEvent participant = ${event.participant.identity}'); + 'RoomContext: ParticipantDisconnectedEvent $roomName participant = ${event.participant.identity}'); _participants .removeWhere((p) => p.identity == event.participant.identity); notifyListeners(); }) ..on((event) { - Debug.event('ParticipantContext: TrackPublishedEvent'); + Debug.event('ParticipantContext: TrackPublishedEvent $roomName'); _buildParticipants(); }) ..on((event) { - Debug.event('ParticipantContext: TrackUnpublishedEvent'); + Debug.event('ParticipantContext: TrackUnpublishedEvent $roomName'); _buildParticipants(); }) ..on((event) { @@ -228,6 +228,8 @@ class RoomContext extends ChangeNotifier with ChatContextMixin { /// Get the connected status. bool get connected => _connected; + LocalParticipant? get localParticipant => _room.localParticipant; + int get participantCount => _participants.length; final List _participants = []; @@ -305,8 +307,13 @@ class RoomContext extends ChangeNotifier with ChatContextMixin { } @override - void dispose() { - _listener.dispose(); + void dispose() async { + await _listener.dispose(); + Debug.event('RoomContext disposed ${roomName ?? ''}'); + if (_room.connectionState == ConnectionState.connected) { + await _room.disconnect(); + } + await _room.dispose(); super.dispose(); } } diff --git a/lib/src/ui/builder/room/room_participants.dart b/lib/src/ui/builder/room/room_participants.dart index e6164ad..64989fc 100644 --- a/lib/src/ui/builder/room/room_participants.dart +++ b/lib/src/ui/builder/room/room_participants.dart @@ -17,7 +17,6 @@ import 'package:flutter/material.dart'; import 'package:livekit_client/livekit_client.dart'; import 'package:provider/provider.dart'; -import '../../../context/participant_context.dart'; import '../../../context/room_context.dart'; import '../../../debug/logger.dart'; @@ -35,7 +34,7 @@ class RoomParticipants extends StatelessWidget { return Consumer( builder: (context, roomCtx, child) { Debug.log('====> RoomParticipants for ${roomCtx.roomName}'); - return Selector>( + return Selector>( selector: (context, participants) => roomCtx.participants, builder: (context, participants, child) { return builder(context, participants); diff --git a/lib/src/ui/widgets/participant/participant_status_bar.dart b/lib/src/ui/widgets/participant/participant_status_bar.dart index 7d19d08..98bfda8 100644 --- a/lib/src/ui/widgets/participant/participant_status_bar.dart +++ b/lib/src/ui/widgets/participant/participant_status_bar.dart @@ -47,68 +47,63 @@ class ParticipantStatusBar extends StatelessWidget { '===> ParticipantStatusBar for ${participantContext.name}'); var trackCtx = Provider.of(context); var isScreenShare = trackCtx?.isScreenShare ?? false; - return Positioned( - bottom: 0, - left: 0, - right: 0, - child: Container( - padding: const EdgeInsets.symmetric(horizontal: 6), - color: Colors.black.withOpacity(0.6), - child: Row( - mainAxisAlignment: MainAxisAlignment.end, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - if (showMuteStatus && !isScreenShare) - ParticipantMutedIndicator( - builder: (context, isMuted) => isMuted - ? const Icon( - Icons.mic_off, - color: Colors.white54, - size: 20, - ) - : const SizedBox(), - ), - if (isScreenShare) - const Icon( - Icons.screen_share, - color: Colors.white54, - size: 20, - ), - if (showName) - ParticipantName( - builder: (context, name) => name != null - ? Flexible( - child: Text( - isScreenShare ? '$name\'s screen' : name, - style: const TextStyle( - color: Colors.white54, - fontSize: 16, - ), - overflow: TextOverflow.ellipsis, + return Container( + padding: const EdgeInsets.symmetric(horizontal: 6), + color: Colors.black.withOpacity(0.6), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + if (showMuteStatus && !isScreenShare) + ParticipantMutedIndicator( + builder: (context, isMuted) => isMuted + ? const Icon( + Icons.mic_off, + color: Colors.white54, + size: 20, + ) + : const SizedBox(), + ), + if (isScreenShare) + const Icon( + Icons.screen_share, + color: Colors.white54, + size: 20, + ), + if (showName) + ParticipantName( + builder: (context, name) => name != null + ? Flexible( + child: Text( + isScreenShare ? '$name\'s screen' : name, + style: const TextStyle( + color: Colors.white54, + fontSize: 16, ), - ) - : Container(), - ), - if (showConnectionQuality) - ConnectionQualityIndicator( - builder: (context, connectionQuality) => - ConnectionQualityIndicatorWidget( - connectionQuality: connectionQuality, - ), + overflow: TextOverflow.ellipsis, + ), + ) + : Container(), + ), + if (showConnectionQuality) + ConnectionQualityIndicator( + builder: (context, connectionQuality) => + ConnectionQualityIndicatorWidget( + connectionQuality: connectionQuality, ), - if (showE2EEStatus) - E2EEncryptionIndicator( - builder: (context, isEncrypted) => Padding( - padding: const EdgeInsets.only(left: 5), - child: Icon( - isEncrypted ? Icons.lock : Icons.lock_open, - color: Colors.white54, - size: 20, - ), + ), + if (showE2EEStatus) + E2EEncryptionIndicator( + builder: (context, isEncrypted) => Padding( + padding: const EdgeInsets.only(left: 5), + child: Icon( + isEncrypted ? Icons.lock : Icons.lock_open, + color: Colors.white54, + size: 20, ), ), - ]), - ), + ), + ]), ); }, );