From ef81d7b1ff6074d170d8984baf92adc07df36300 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Thu, 25 Aug 2022 18:30:43 +0800 Subject: [PATCH] feat: network type detection. (#156) * feat: network type detection. * fix unit test. * chore: Make the network type consistent with the protocol. --- .../Flutter/GeneratedPluginRegistrant.swift | 2 + example/macos/Podfile.lock | 14 +++- example/pubspec.lock | 65 ++++++++++++++++- .../flutter/generated_plugin_registrant.cc | 3 + .../windows/flutter/generated_plugins.cmake | 1 + lib/src/utils.dart | 31 ++++++++ pubspec.lock | 70 +++++++++++++++++++ pubspec.yaml | 1 + 8 files changed, 184 insertions(+), 3 deletions(-) diff --git a/example/macos/Flutter/GeneratedPluginRegistrant.swift b/example/macos/Flutter/GeneratedPluginRegistrant.swift index 8310140db..3d039452f 100644 --- a/example/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/example/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,7 @@ import FlutterMacOS import Foundation +import connectivity_plus_macos import device_info_plus_macos import flutter_webrtc import livekit_client @@ -12,6 +13,7 @@ import path_provider_macos import shared_preferences_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin")) DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin")) FlutterWebRTCPlugin.register(with: registry.registrar(forPlugin: "FlutterWebRTCPlugin")) LiveKitPlugin.register(with: registry.registrar(forPlugin: "LiveKitPlugin")) diff --git a/example/macos/Podfile.lock b/example/macos/Podfile.lock index b1a4d6281..93beb017a 100644 --- a/example/macos/Podfile.lock +++ b/example/macos/Podfile.lock @@ -1,20 +1,25 @@ PODS: + - connectivity_plus_macos (0.0.1): + - FlutterMacOS + - ReachabilitySwift - device_info_plus_macos (0.0.1): - FlutterMacOS - flutter_webrtc (0.9.4): - FlutterMacOS - WebRTC-SDK (= 104.5112.03) - FlutterMacOS (1.0.0) - - livekit_client (1.1.0): + - livekit_client (1.1.1): - FlutterMacOS - WebRTC-SDK (~> 104.5112.03) - path_provider_macos (0.0.1): - FlutterMacOS + - ReachabilitySwift (5.0.0) - shared_preferences_macos (0.0.1): - FlutterMacOS - WebRTC-SDK (104.5112.03) DEPENDENCIES: + - connectivity_plus_macos (from `Flutter/ephemeral/.symlinks/plugins/connectivity_plus_macos/macos`) - device_info_plus_macos (from `Flutter/ephemeral/.symlinks/plugins/device_info_plus_macos/macos`) - flutter_webrtc (from `Flutter/ephemeral/.symlinks/plugins/flutter_webrtc/macos`) - FlutterMacOS (from `Flutter/ephemeral`) @@ -24,9 +29,12 @@ DEPENDENCIES: SPEC REPOS: trunk: + - ReachabilitySwift - WebRTC-SDK EXTERNAL SOURCES: + connectivity_plus_macos: + :path: Flutter/ephemeral/.symlinks/plugins/connectivity_plus_macos/macos device_info_plus_macos: :path: Flutter/ephemeral/.symlinks/plugins/device_info_plus_macos/macos flutter_webrtc: @@ -41,11 +49,13 @@ EXTERNAL SOURCES: :path: Flutter/ephemeral/.symlinks/plugins/shared_preferences_macos/macos SPEC CHECKSUMS: + connectivity_plus_macos: f6e86fd000e971d361e54b5afcadc8c8fa773308 device_info_plus_macos: 1ad388a1ef433505c4038e7dd9605aadd1e2e9c7 flutter_webrtc: 45cd8f5825bb32054e817708bf343ceff2c9cab8 FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424 - livekit_client: d229104e54e72ec7b5ffad62c14117297b5bbb23 + livekit_client: 42da9111e214db06de13279b12a70210294d03dc path_provider_macos: 3c0c3b4b0d4a76d2bf989a913c2de869c5641a19 + ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825 shared_preferences_macos: a64dc611287ed6cbe28fd1297898db1336975727 WebRTC-SDK: 9f50fb5a410edc38e6fbb865fe940e3010bc8e7e diff --git a/example/pubspec.lock b/example/pubspec.lock index 8e2a50e7b..95e1d2917 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,6 +1,13 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.1" async: dependency: transitive description: @@ -43,6 +50,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.16.0" + connectivity_plus: + dependency: transitive + description: + name: connectivity_plus + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.6+1" + connectivity_plus_linux: + dependency: transitive + description: + name: connectivity_plus_linux + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" + connectivity_plus_macos: + dependency: transitive + description: + name: connectivity_plus_macos + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.4" + connectivity_plus_platform_interface: + dependency: transitive + description: + name: connectivity_plus_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.1" + connectivity_plus_web: + dependency: transitive + description: + name: connectivity_plus_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.3" + connectivity_plus_windows: + dependency: transitive + description: + name: connectivity_plus_windows + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.2" crypto: dependency: transitive description: @@ -57,6 +106,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.7" + dbus: + dependency: transitive + description: + name: dbus + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.3" device_info_plus: dependency: transitive description: @@ -225,7 +281,7 @@ packages: path: ".." relative: true source: path - version: "1.1.0-hotifx" + version: "1.1.1" logging: dependency: "direct main" description: @@ -261,6 +317,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.0" + nm: + dependency: transitive + description: + name: nm + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.0" path: dependency: transitive description: diff --git a/example/windows/flutter/generated_plugin_registrant.cc b/example/windows/flutter/generated_plugin_registrant.cc index a36919277..26ad717e4 100644 --- a/example/windows/flutter/generated_plugin_registrant.cc +++ b/example/windows/flutter/generated_plugin_registrant.cc @@ -6,10 +6,13 @@ #include "generated_plugin_registrant.h" +#include #include #include void RegisterPlugins(flutter::PluginRegistry* registry) { + ConnectivityPlusWindowsPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin")); FlutterWebRTCPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("FlutterWebRTCPlugin")); LiveKitPluginRegisterWithRegistrar( diff --git a/example/windows/flutter/generated_plugins.cmake b/example/windows/flutter/generated_plugins.cmake index 96a3b72bd..4ee51f5f9 100644 --- a/example/windows/flutter/generated_plugins.cmake +++ b/example/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + connectivity_plus_windows flutter_webrtc livekit_client ) diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 136d077a9..dcde05189 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:math' as math; import 'package:collection/collection.dart'; +import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc; @@ -160,6 +161,7 @@ class Utils { pathSegments.addAll(lastSegments); final clientInfo = await _clientInfo(); + final networkType = await getNetworkType(); return uri.replace( scheme: validate ? httpScheme : wsScheme, @@ -172,6 +174,7 @@ class Utils { 'protocol': connectOptions.protocolVersion.toStringValue(), 'sdk': 'flutter', 'version': LiveKitClient.version, + 'network': networkType, // client info if (clientInfo != null) ...{ if (clientInfo.hasOs()) 'os': clientInfo.os, @@ -286,6 +289,34 @@ class Utils { return result; } + @internal + static FutureOr getNetworkType() async { + if (lkPlatformIsTest()) { + return 'wifi'; + } + var connectivityResult = await (Connectivity().checkConnectivity()); + // wifi, wired, cellular, vpn, empty if not known + String networkType = 'empty'; + switch (connectivityResult) { + case ConnectivityResult.mobile: + networkType = 'cellular'; + break; + case ConnectivityResult.wifi: + networkType = 'wifi'; + break; + case ConnectivityResult.bluetooth: + networkType = 'bluetooth'; + break; + case ConnectivityResult.ethernet: + networkType = 'wired'; + break; + case ConnectivityResult.none: + networkType = 'empty'; + break; + } + return networkType; + } + @internal static double findEvenScaleDownBy( VideoDimensions sourceDimensions, diff --git a/pubspec.lock b/pubspec.lock index 2d800f671..c74d1a1a5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -92,6 +92,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.16.0" + connectivity_plus: + dependency: "direct main" + description: + name: connectivity_plus + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.6+1" + connectivity_plus_linux: + dependency: transitive + description: + name: connectivity_plus_linux + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" + connectivity_plus_macos: + dependency: transitive + description: + name: connectivity_plus_macos + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.4" + connectivity_plus_platform_interface: + dependency: transitive + description: + name: connectivity_plus_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.1" + connectivity_plus_web: + dependency: transitive + description: + name: connectivity_plus_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.3" + connectivity_plus_windows: + dependency: transitive + description: + name: connectivity_plus_windows + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.2" convert: dependency: transitive description: @@ -120,6 +162,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.7" + dbus: + dependency: transitive + description: + name: dbus + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.4" device_info_plus: dependency: "direct main" description: @@ -296,6 +345,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "5.2.0" + nm: + dependency: transitive + description: + name: nm + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.0" package_config: dependency: transitive description: @@ -359,6 +415,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.6" + petitparser: + dependency: transitive + description: + name: petitparser + url: "https://pub.dartlang.org" + source: hosted + version: "5.0.0" platform: dependency: transitive description: @@ -511,6 +574,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.2.0+1" + xml: + dependency: transitive + description: + name: xml + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.0" yaml: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 861a3cd17..b02a8ab86 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,6 +25,7 @@ dependencies: flutter_webrtc: 0.9.4 dart_webrtc: 1.0.7 device_info_plus: ^3.2.3 + connectivity_plus: ^2.3.6+1 dev_dependencies: flutter_test: