Skip to content

Commit

Permalink
Fix dart analyze result of flutter_webrtc_demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
wanchao-xu committed Nov 3, 2023
1 parent 617092e commit e7bd9b6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class _CallSampleState extends State<CallSample> {
});
break;
case CallState.CallStateRinging:
bool? accept = await _showAcceptDialog();
var accept = await _showAcceptDialog();
if (accept!) {
_accept();
setState(() {
Expand Down Expand Up @@ -213,7 +213,7 @@ class _CallSampleState extends State<CallSample> {
);
if (source != null) {
try {
MediaStream stream =
var stream =
await navigator.mediaDevices.getDisplayMedia(<String, dynamic>{
'video': {
'deviceId': {'exact': source.id},
Expand Down Expand Up @@ -244,7 +244,7 @@ class _CallSampleState extends State<CallSample> {
}

ListBody _buildRow(context, peer) {
bool self = peer['id'] == _selfId;
var self = peer['id'] == _selfId;
return ListBody(children: <Widget>[
ListTile(
title: Text(self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class _DataChannelSampleState extends State<DataChannelSample> {
});
break;
case CallState.CallStateRinging:
bool? accept = await _showAcceptDialog();
var accept = await _showAcceptDialog();
if (accept!) {
_accept();
setState(() {
Expand Down Expand Up @@ -188,7 +188,7 @@ class _DataChannelSampleState extends State<DataChannelSample> {
}

Future<void> _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));
Expand All @@ -205,7 +205,7 @@ class _DataChannelSampleState extends State<DataChannelSample> {
}

Widget _buildRow(context, peer) {
bool self = peer['id'] == _selfId;
var self = peer['id'] == _selfId;
return ListBody(children: <Widget>[
ListTile(
title: Text(self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -159,22 +159,22 @@ 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;
}
_createAnswer(session, media);
}

void reject(String sessionId) {
Session? session = _sessions[sessionId];
var session = _sessions[sessionId];
if (session == null) {
return;
}
Expand All @@ -187,7 +187,7 @@ class Signaling {
{
List<dynamic> peers = message['data'] ?? [];
if (onPeersUpdate != null) {
Map<String, dynamic> event = {};
var event = {};
event['self'] = _selfId;
event['peers'] = peers;
onPeersUpdate?.call(event);
Expand All @@ -201,8 +201,8 @@ class Signaling {
Map<String, dynamic> 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,
Expand All @@ -227,7 +227,7 @@ class Signaling {
Map<String, dynamic> data = message['data'] ?? {};
Map<String, dynamic> 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);
Expand All @@ -239,8 +239,8 @@ class Signaling {
String peerId = data['from'] ?? '';
Map<String, dynamic> 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) {
Expand Down Expand Up @@ -268,7 +268,7 @@ class Signaling {
Map<String, dynamic> 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);
Expand All @@ -286,7 +286,7 @@ class Signaling {
}

Future<void> connect() async {
String url = 'https://$_host:$_port/ws';
var url = 'https://$_host:$_port/ws';
_socket = SimpleWebSocket(url);

print('connect to $url');
Expand Down Expand Up @@ -341,7 +341,7 @@ class Signaling {

Future<MediaStream> createStream(String media, bool userScreen,
{BuildContext? context}) async {
final Map<String, dynamic> mediaConstraints = {
final mediaConstraints = <String, dynamic>{
'audio': userScreen ? false : true,
'video': userScreen
? true
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -511,16 +511,16 @@ class Signaling {

Future<void> _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<void> _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', {
Expand All @@ -536,15 +536,15 @@ 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;
}

Future<void> _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', {
Expand All @@ -559,7 +559,7 @@ class Signaling {
}

void _send(event, data) {
Map<String, dynamic> request = {};
var request = {};
request['type'] = event;
request['data'] = data;
_socket?.send(_encoder.convert(request));
Expand All @@ -582,8 +582,8 @@ class Signaling {

void _closeSessionByPeerId(String peerId) {
_sessions.removeWhere((String key, Session session) {
List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import 'dart:convert';
import 'dart:io';

Future<Map> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'dart:convert';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;

Future<Map> 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.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@ class SimpleWebSocket {

Future<WebSocket> _connectForSelfSignedCert(url) async {
try {
Random r = Random();
String key = base64.encode(List<int>.generate(8, (_) => r.nextInt(255)));
HttpClient client = HttpClient(context: SecurityContext());
var r = Random();
var key = base64.encode(List<int>.generate(8, (_) => r.nextInt(255)));
var client = HttpClient(context: SecurityContext());
client.badCertificateCallback =
(X509Certificate cert, String host, int port) {
print(
'SimpleWebSocket: Allow self-signed certificate => $host:$port. ');
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');
request.headers.add(
'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,
Expand Down

0 comments on commit e7bd9b6

Please sign in to comment.