Skip to content

Commit

Permalink
Fix Camera Stream Freezing (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 authored Oct 21, 2024
1 parent 2e8537f commit ce6d05b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
19 changes: 10 additions & 9 deletions lib/services/nt_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class NTConnection {
List<VoidCallback> onConnectedListeners = [];
List<VoidCallback> onDisconnectedListeners = [];

bool _ntConnected = false;
final ValueNotifier<bool> _ntConnected = ValueNotifier(false);
ValueNotifier<bool> get ntConnected => _ntConnected;
bool _dsConnected = false;

bool get isNT4Connected => _ntConnected;
bool get isNT4Connected => _ntConnected.value;

bool get isDSConnected => _dsConnected;
DSInteropClient get dsClient => _dsClient;
Expand All @@ -42,14 +43,14 @@ class NTConnection {
_ntClient = NT4Client(
serverBaseAddress: ipAddress,
onConnect: () {
_ntConnected = true;
_ntConnected.value = true;

for (VoidCallback callback in onConnectedListeners) {
callback.call();
}
},
onDisconnect: () {
_ntConnected = false;
_ntConnected.value = false;

for (VoidCallback callback in onDisconnectedListeners) {
callback.call();
Expand Down Expand Up @@ -119,13 +120,13 @@ class NTConnection {
}

Stream<bool> connectionStatus() async* {
yield _ntConnected;
bool lastYielded = _ntConnected;
yield _ntConnected.value;
bool lastYielded = _ntConnected.value;

while (true) {
if (_ntConnected != lastYielded) {
yield _ntConnected;
lastYielded = _ntConnected;
if (_ntConnected.value != lastYielded) {
yield _ntConnected.value;
lastYielded = _ntConnected.value;
}
await Future.delayed(const Duration(seconds: 1));
}
Expand Down
7 changes: 5 additions & 2 deletions lib/widgets/nt_widgets/multi-topic/camera_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ class CameraStreamWidget extends NTWidget {
CameraStreamModel model = cast(context.watch<NTWidgetModel>());

return ListenableBuilder(
listenable: model.streamsSubscription,
listenable: Listenable.merge([
model.streamsSubscription,
model.ntConnection.ntConnected,
]),
builder: (context, child) {
List<Object?> rawStreams =
tryCast(model.streamsSubscription.value) ?? [];
Expand All @@ -274,7 +277,7 @@ class CameraStreamWidget extends NTWidget {
streams.add(stream.substring('mjpg:'.length));
}

if (streams.isEmpty || !model.ntConnection.isNT4Connected) {
if (streams.isEmpty || !model.ntConnection.ntConnected.value) {
return Stack(
fit: StackFit.expand,
children: [
Expand Down
2 changes: 2 additions & 0 deletions test/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ MockNTConnection createMockOfflineNT4() {

when(mockSubscription.listen(any)).thenAnswer((invocation) {});

when(mockNT4Connection.ntConnected).thenReturn(ValueNotifier(false));
when(mockNT4Connection.isNT4Connected).thenReturn(false);

when(mockNT4Connection.serverTime).thenReturn(0);
Expand Down Expand Up @@ -97,6 +98,7 @@ MockNTConnection createMockOnlineNT4({

when(mockSubscription.listen(any)).thenAnswer((_) {});

when(mockNT4Connection.ntConnected).thenReturn(ValueNotifier(true));
when(mockNT4Connection.isNT4Connected).thenReturn(true);

when(mockNT4Connection.serverTime).thenReturn(serverTime);
Expand Down

0 comments on commit ce6d05b

Please sign in to comment.