From e7bd9b62bba873239d557b2fcf20cf2345584338 Mon Sep 17 00:00:00 2001 From: wanchao-xu Date: Fri, 3 Nov 2023 16:32:56 +0800 Subject: [PATCH] Fix dart analyze result of flutter_webrtc_demo. --- .../lib/src/call_sample/call_sample.dart | 6 +-- .../src/call_sample/data_channel_sample.dart | 6 +-- .../lib/src/call_sample/random_string.dart | 2 +- .../lib/src/call_sample/signaling.dart | 50 +++++++++---------- .../lib/src/route_item.dart | 2 +- .../lib/src/utils/turn.dart | 11 ++-- .../lib/src/utils/turn_web.dart | 7 ++- .../lib/src/utils/websocket.dart | 14 +++--- 8 files changed, 48 insertions(+), 50 deletions(-) diff --git a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/call_sample.dart b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/call_sample.dart index 7fec192d2..b73bf2c97 100644 --- a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/call_sample.dart +++ b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/call_sample.dart @@ -66,7 +66,7 @@ class _CallSampleState extends State { }); break; case CallState.CallStateRinging: - bool? accept = await _showAcceptDialog(); + var accept = await _showAcceptDialog(); if (accept!) { _accept(); setState(() { @@ -213,7 +213,7 @@ class _CallSampleState extends State { ); if (source != null) { try { - MediaStream stream = + var stream = await navigator.mediaDevices.getDisplayMedia({ 'video': { 'deviceId': {'exact': source.id}, @@ -244,7 +244,7 @@ class _CallSampleState extends State { } ListBody _buildRow(context, peer) { - bool self = peer['id'] == _selfId; + var self = peer['id'] == _selfId; return ListBody(children: [ ListTile( title: Text(self diff --git a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/data_channel_sample.dart b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/data_channel_sample.dart index f7ee77249..c26a874d2 100644 --- a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/data_channel_sample.dart +++ b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/data_channel_sample.dart @@ -153,7 +153,7 @@ class _DataChannelSampleState extends State { }); break; case CallState.CallStateRinging: - bool? accept = await _showAcceptDialog(); + var accept = await _showAcceptDialog(); if (accept!) { _accept(); setState(() { @@ -188,7 +188,7 @@ class _DataChannelSampleState extends State { } Future _handleDataChannelTest(Timer timer) async { - String text = 'Say hello ${timer.tick} times, from [$_selfId]'; + var text = 'Say hello ${timer.tick} times, from [$_selfId]'; await _dataChannel ?.send(RTCDataChannelMessage.fromBinary(Uint8List(timer.tick + 1))); await _dataChannel?.send(RTCDataChannelMessage(text)); @@ -205,7 +205,7 @@ class _DataChannelSampleState extends State { } Widget _buildRow(context, peer) { - bool self = peer['id'] == _selfId; + var self = peer['id'] == _selfId; return ListBody(children: [ ListTile( title: Text(self diff --git a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/random_string.dart b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/random_string.dart index 5278af018..e12e71f90 100644 --- a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/random_string.dart +++ b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/random_string.dart @@ -33,7 +33,7 @@ const UPPER_ALPHA_END = 90; /// Generates a random integer where [from] <= [to]. int randomBetween(int from, int to) { if (from > to) throw Exception('$from cannot be > $to'); - Random rand = Random(); + var rand = Random(); return ((to - from) * rand.nextDouble()).toInt() + from; } diff --git a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/signaling.dart b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/signaling.dart index 0bbdf8a37..bf068a71d 100644 --- a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/signaling.dart +++ b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/call_sample/signaling.dart @@ -133,14 +133,14 @@ class Signaling { void muteMic() { if (_localStream != null) { - bool enabled = _localStream!.getAudioTracks().first.enabled; + var enabled = _localStream!.getAudioTracks().first.enabled; _localStream!.getAudioTracks().first.enabled = !enabled; } } void invite(String peerId, String media, bool useScreen) async { - String sessionId = '$_selfId-$peerId'; - Session session = await _createSession(null, + var sessionId = '$_selfId-$peerId'; + var session = await _createSession(null, peerId: peerId, sessionId: sessionId, media: media, @@ -159,14 +159,14 @@ class Signaling { 'session_id': sessionId, 'from': _selfId, }); - Session? session = _sessions[sessionId]; + var session = _sessions[sessionId]; if (session != null) { _closeSession(session); } } void accept(String sessionId, String media) { - Session? session = _sessions[sessionId]; + var session = _sessions[sessionId]; if (session == null) { return; } @@ -174,7 +174,7 @@ class Signaling { } void reject(String sessionId) { - Session? session = _sessions[sessionId]; + var session = _sessions[sessionId]; if (session == null) { return; } @@ -187,7 +187,7 @@ class Signaling { { List peers = message['data'] ?? []; if (onPeersUpdate != null) { - Map event = {}; + var event = {}; event['self'] = _selfId; event['peers'] = peers; onPeersUpdate?.call(event); @@ -201,8 +201,8 @@ class Signaling { Map description = data['description'] ?? {}; String media = data['media'] ?? ''; String sessionId = data['session_id'] ?? ''; - Session? session = _sessions[sessionId]; - Session newSession = await _createSession(session, + var session = _sessions[sessionId]; + var newSession = await _createSession(session, peerId: peerId, sessionId: sessionId, media: media, @@ -227,7 +227,7 @@ class Signaling { Map data = message['data'] ?? {}; Map description = data['description'] ?? {}; String sessionId = data['session_id'] ?? ''; - Session? session = _sessions[sessionId]; + var session = _sessions[sessionId]; await session?.pc?.setRemoteDescription( RTCSessionDescription(description['sdp'], description['type'])); onCallStateChange?.call(session!, CallState.CallStateConnected); @@ -239,8 +239,8 @@ class Signaling { String peerId = data['from'] ?? ''; Map candidateMap = data['candidate'] ?? {}; String sessionId = data['session_id'] ?? ''; - Session? session = _sessions[sessionId]; - RTCIceCandidate candidate = RTCIceCandidate(candidateMap['candidate'], + var session = _sessions[sessionId]; + var candidate = RTCIceCandidate(candidateMap['candidate'], candidateMap['sdpMid'], candidateMap['sdpMLineIndex']); if (session != null) { @@ -268,7 +268,7 @@ class Signaling { Map data = message['data'] ?? {}; String sessionId = data['session_id'] ?? ''; print('bye: $sessionId'); - Session? session = _sessions.remove(sessionId); + var session = _sessions.remove(sessionId); if (session != null) { onCallStateChange?.call(session, CallState.CallStateBye); await _closeSession(session); @@ -286,7 +286,7 @@ class Signaling { } Future connect() async { - String url = 'https://$_host:$_port/ws'; + var url = 'https://$_host:$_port/ws'; _socket = SimpleWebSocket(url); print('connect to $url'); @@ -341,7 +341,7 @@ class Signaling { Future createStream(String media, bool userScreen, {BuildContext? context}) async { - final Map mediaConstraints = { + final mediaConstraints = { 'audio': userScreen ? false : true, 'video': userScreen ? true @@ -389,13 +389,13 @@ class Signaling { required String media, required bool screenSharing, }) async { - Session newSession = session ?? Session(sid: sessionId, pid: peerId); + var newSession = session ?? Session(sid: sessionId, pid: peerId); if (media != 'data') { _localStream = await createStream(media, screenSharing, context: _context); } print(_iceServers); - RTCPeerConnection pc = await createPeerConnection({ + var pc = await createPeerConnection({ ..._iceServers, ...{'sdpSemantics': sdpSemantics} }, _config); @@ -511,16 +511,16 @@ class Signaling { Future _createDataChannel(Session session, {label = 'fileTransfer'}) async { - RTCDataChannelInit dataChannelDict = RTCDataChannelInit() + var dataChannelDict = RTCDataChannelInit() ..maxRetransmits = 30; - RTCDataChannel channel = + var channel = await session.pc!.createDataChannel(label, dataChannelDict); _addDataChannel(session, channel); } Future _createOffer(Session session, String media) async { try { - RTCSessionDescription s = + var s = await session.pc!.createOffer(media == 'data' ? _dcConstraints : {}); await session.pc!.setLocalDescription(_fixSdp(s)); _send('offer', { @@ -536,7 +536,7 @@ class Signaling { } RTCSessionDescription _fixSdp(RTCSessionDescription s) { - String? sdp = s.sdp; + var sdp = s.sdp; s.sdp = sdp?.replaceAll('profile-level-id=640c1f', 'profile-level-id=42e032'); return s; @@ -544,7 +544,7 @@ class Signaling { Future _createAnswer(Session session, String media) async { try { - RTCSessionDescription s = + var s = await session.pc!.createAnswer(media == 'data' ? _dcConstraints : {}); await session.pc!.setLocalDescription(_fixSdp(s)); _send('answer', { @@ -559,7 +559,7 @@ class Signaling { } void _send(event, data) { - Map request = {}; + var request = {}; request['type'] = event; request['data'] = data; _socket?.send(_encoder.convert(request)); @@ -582,8 +582,8 @@ class Signaling { void _closeSessionByPeerId(String peerId) { _sessions.removeWhere((String key, Session session) { - List ids = key.split('-'); - bool found = peerId == ids[0] || peerId == ids[1]; + var ids = key.split('-'); + var found = peerId == ids[0] || peerId == ids[1]; if (found) { _closeSession(session); onCallStateChange?.call(session, CallState.CallStateBye); diff --git a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/route_item.dart b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/route_item.dart index c82e00d78..0d5fa2866 100644 --- a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/route_item.dart +++ b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/route_item.dart @@ -2,7 +2,7 @@ import 'dart:core'; import 'package:flutter/material.dart'; -typedef void RouteCallback(BuildContext context); +typedef RouteCallback = void Function(BuildContext context); class RouteItem { RouteItem({ diff --git a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/turn.dart b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/turn.dart index 34139e5c3..38abac39f 100644 --- a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/turn.dart +++ b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/turn.dart @@ -3,17 +3,16 @@ import 'dart:convert'; import 'dart:io'; Future getTurnCredential(String host, int port) async { - HttpClient client = HttpClient(context: SecurityContext()); + var client = HttpClient(context: SecurityContext()); client.badCertificateCallback = (X509Certificate cert, String host, int port) { print('getTurnCredential: Allow self-signed certificate => $host:$port. '); return true; }; - final String url = - 'https://$host:$port/api/turn?service=turn&username=flutter-webrtc'; - HttpClientRequest request = await client.getUrl(Uri.parse(url)); - HttpClientResponse response = await request.close(); - final String responseBody = await response.transform(Utf8Decoder()).join(); + var url = 'https://$host:$port/api/turn?service=turn&username=flutter-webrtc'; + var request = await client.getUrl(Uri.parse(url)); + var response = await request.close(); + var responseBody = await response.transform(Utf8Decoder()).join(); print('getTurnCredential:response => $responseBody.'); Map data = JsonDecoder().convert(responseBody); return data; diff --git a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/turn_web.dart b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/turn_web.dart index 889192cab..bf2ea7adc 100644 --- a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/turn_web.dart +++ b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/turn_web.dart @@ -1,10 +1,9 @@ import 'dart:convert'; -import 'package:http/http.dart'; +import 'package:http/http.dart' as http; Future getTurnCredential(String host, int port) async { - final String url = - 'https://$host:$port/api/turn?service=turn&username=flutter-webrtc'; - final Response res = await get(Uri.parse(url)); + var url = 'https://$host:$port/api/turn?service=turn&username=flutter-webrtc'; + final res = await http.get(Uri.parse(url)); if (res.statusCode == 200) { var data = json.decode(res.body); print('getTurnCredential:response => $data.'); diff --git a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/websocket.dart b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/websocket.dart index 22e240c03..1048edf48 100644 --- a/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/websocket.dart +++ b/packages/flutter_webrtc/example/flutter_webrtc_demo/lib/src/utils/websocket.dart @@ -39,9 +39,9 @@ class SimpleWebSocket { Future _connectForSelfSignedCert(url) async { try { - Random r = Random(); - String key = base64.encode(List.generate(8, (_) => r.nextInt(255))); - HttpClient client = HttpClient(context: SecurityContext()); + var r = Random(); + var key = base64.encode(List.generate(8, (_) => r.nextInt(255))); + var client = HttpClient(context: SecurityContext()); client.badCertificateCallback = (X509Certificate cert, String host, int port) { print( @@ -49,7 +49,7 @@ class SimpleWebSocket { return true; }; - HttpClientRequest request = + var request = await client.getUrl(Uri.parse(url)); // form the correct url here request.headers.add('Connection', 'Upgrade'); request.headers.add('Upgrade', 'websocket'); @@ -57,10 +57,10 @@ class SimpleWebSocket { 'Sec-WebSocket-Version', '13'); // insert the correct version here request.headers.add('Sec-WebSocket-Key', key.toLowerCase()); - HttpClientResponse response = await request.close(); + var response = await request.close(); // ignore: close_sinks - Socket socket = await response.detachSocket(); - WebSocket webSocket = WebSocket.fromUpgradedSocket( + var socket = await response.detachSocket(); + var webSocket = WebSocket.fromUpgradedSocket( socket, protocol: 'signaling', serverSide: false,