Skip to content

Commit

Permalink
Merge branch 'main' into improve-reconnect-logic-for-websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc authored Nov 30, 2023
2 parents 64cbccc + 0d015a2 commit c78308c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 55 deletions.
6 changes: 6 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_window_close/flutter_window_close.dart';
import 'package:livekit_example/theme.dart';
import 'package:logging/logging.dart';
import 'package:intl/intl.dart';
import 'pages/connect.dart';
import 'utils.dart';

void main() async {
final format = DateFormat('HH:mm:ss');
Expand All @@ -14,6 +16,10 @@ void main() async {

WidgetsFlutterBinding.ensureInitialized();

FlutterWindowClose.setWindowShouldCloseHandler(() async {
await onWindowShouldClose?.call();
return true;
});
runApp(const LiveKitExampleApp());
}

Expand Down
13 changes: 11 additions & 2 deletions example/lib/pages/room.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math' as math;

Expand All @@ -7,6 +8,7 @@ import 'package:livekit_client/livekit_client.dart';
import 'package:visibility_detector/visibility_detector.dart';

import '../exts.dart';
import '../utils.dart';
import '../widgets/controls.dart';
import '../widgets/participant.dart';
import '../widgets/participant_info.dart';
Expand All @@ -28,7 +30,6 @@ class RoomPage extends StatefulWidget {
}

class _RoomPageState extends State<RoomPage> with WidgetsBindingObserver {
//
List<Participant> participantTracks = [];
EventsListener<RoomEvent> get _listener => widget.listener;
bool get fastConnection => widget.room.engine.fastConnectOptions != null;
Expand All @@ -52,6 +53,14 @@ class _RoomPageState extends State<RoomPage> with WidgetsBindingObserver {
if (lkPlatformIsMobile()) {
Hardware.instance.setSpeakerphoneOn(true);
}

if (!lkPlatformIs(PlatformType.web) && !lkPlatformIsTest()) {
onWindowShouldClose = () async {
unawaited(widget.room.disconnect());
await _listener.waitFor<RoomDisconnectedEvent>(
duration: const Duration(seconds: 5));
};
}
}

@override
Expand All @@ -62,8 +71,8 @@ class _RoomPageState extends State<RoomPage> with WidgetsBindingObserver {
await _listener.dispose();
await widget.room.dispose();
})();

WidgetsBinding.instance.removeObserver(this);
onWindowShouldClose = null;
super.dispose();
}

Expand Down
3 changes: 3 additions & 0 deletions example/lib/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'dart:async';

FutureOr<void> Function()? onWindowShouldClose;
2 changes: 0 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ dependencies:
shared_preferences: ^2.0.7
google_fonts: ^4.0.4
flutter_svg: ^2.0.5

fluttertoast: ^8.2.4

livekit_client:
path: ../
visibility_detector: ^0.4.0+2
Expand Down
12 changes: 0 additions & 12 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import '../participant/participant.dart';
import '../participant/remote.dart';
import '../proto/livekit_models.pb.dart' as lk_models;
import '../proto/livekit_rtc.pb.dart' as lk_rtc;
import '../support/app_state.dart';
import '../support/disposable.dart';
import '../support/platform.dart';
import '../track/local/audio.dart';
Expand Down Expand Up @@ -109,8 +108,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
//
late EventsListener<SignalEvent> _signalListener;

StreamSubscription<String>? _appCloseSubscription;

Room({
ConnectOptions connectOptions = const ConnectOptions(),
RoomOptions roomOptions = const RoomOptions(),
Expand All @@ -124,13 +121,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
_engineListener = this.engine.createListener();
_setUpEngineListeners();

if (!kIsWeb && !lkPlatformIsTest()) {
_appCloseSubscription =
AppStateListener.instance.onWindowShouldClose.stream.listen((event) {
disconnect();
});
}

_signalListener = this.engine.signalClient.createListener();
_setUpSignalListeners();

Expand All @@ -153,8 +143,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
await _engineListener.dispose();
// dispose the engine
await this.engine.dispose();
// dispose the app state listener
await _appCloseSubscription?.cancel();
});
}

Expand Down
5 changes: 2 additions & 3 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
try {
// try to use getSettings for more accurate resolution
final settings = track.mediaStreamTrack.getSettings();
if (settings['width'] is int) {
if ((settings['width'] is int && settings['width'] as int > 0) &&
(settings['height'] is int && settings['height'] as int > 0)) {
dimensions = dimensions.copyWith(width: settings['width'] as int);
}
if (settings['height'] is int) {
dimensions = dimensions.copyWith(height: settings['height'] as int);
}
} catch (_) {
Expand Down
29 changes: 0 additions & 29 deletions lib/src/support/app_state.dart

This file was deleted.

18 changes: 13 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ dependencies:
uuid: '>=3.0.6'
synchronized: ^3.0.0+3
protobuf: ^3.0.0
flutter_webrtc:
git:
url: https://github.com/flutter-webrtc/flutter-webrtc.git
ref: fix/throw-exception-for-set-src-object
flutter_window_close: ^0.2.2
flutter_webrtc: 0.9.47
device_info_plus: '>=8.0.0'
js: ^0.6.4
platform_detect: ^2.0.7
Expand Down Expand Up @@ -78,3 +74,15 @@ flutter:
web:
pluginClass: LiveKitWebPlugin
fileName: livekit_client_web.dart


topics:
- webrtc
- voip
- audio
- video
- livestream
- conference
- meeting
- chat
- real-time
2 changes: 1 addition & 1 deletion web/e2ee.cryptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'dart:typed_data';
import 'dart:collection';
import 'dart:async';

import 'package:flutter_webrtc/src/web/rtc_transform_stream.dart';
import 'package:dart_webrtc/src/rtc_transform_stream.dart';

import 'crypto.dart' as crypto;
import 'e2ee.utils.dart';
Expand Down
2 changes: 1 addition & 1 deletion web/e2ee.worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'package:js/js.dart';

import 'e2ee.cryptor.dart';

import 'package:flutter_webrtc/src/web/rtc_transform_stream.dart';
import 'package:dart_webrtc/src/rtc_transform_stream.dart';
import 'package:collection/collection.dart';

import 'crypto.dart' as crypto;
Expand Down

0 comments on commit c78308c

Please sign in to comment.