chore: Improve the code.

This commit is contained in:
cloudwebrtc 2021-11-29 22:26:18 +08:00
parent 01276bbf60
commit edeea47dec
1 changed files with 19 additions and 40 deletions

View File

@ -6,15 +6,20 @@ import 'package:sdp_transform/sdp_transform.dart' as sdp_transform;
import '../matrix.dart'; import '../matrix.dart';
/// Delegate WebRTC basic functionality.
abstract class WebRTCDelegate { abstract class WebRTCDelegate {
RTCFactory get rtcFactory; MediaDevices get mediaDevices;
bool get isBackgroud; Future<RTCPeerConnection> createPeerConnection(
bool get isWeb; Map<String, dynamic> configuration,
[Map<String, dynamic> constraints]);
VideoRenderer createRenderer(); VideoRenderer createRenderer();
void playRingtone(); void playRingtone();
void stopRingtone(); void stopRingtone();
Function(CallSession session)? onNewCall; void handleNewCall(CallSession session);
Function(CallSession session)? onCallEnded; void handleCallEnded(CallSession session);
bool get isBackgroud;
bool get isWeb;
} }
/// The default life time for call events, in millisecond. /// The default life time for call events, in millisecond.
@ -58,7 +63,7 @@ class WrappedMediaStream {
renderer.srcObject = stream; renderer.srcObject = stream;
renderer.onResize = () { renderer.onResize = () {
Logs().i( Logs().i(
'onResize [${stream!.id.substring(0, 8)}] ${renderer?.videoWidth} x ${renderer?.videoHeight}'); 'onResize [${stream!.id.substring(0, 8)}] ${renderer.videoWidth} x ${renderer.videoHeight}');
}; };
} }
@ -467,7 +472,7 @@ class CallSession {
if (pc != null && if (pc != null &&
pc!.iceConnectionState == pc!.iceConnectionState ==
RTCIceConnectionState.RTCIceConnectionStateDisconnected) { RTCIceConnectionState.RTCIceConnectionStateDisconnected) {
_restartIce(); restartIce();
} }
} }
@ -924,7 +929,7 @@ class CallSession {
return metadata; return metadata;
} }
void _restartIce() async { void restartIce() async {
Logs().v('[VOIP] iceRestart.'); Logs().v('[VOIP] iceRestart.');
// Needs restart ice on session.pc and renegotiation. // Needs restart ice on session.pc and renegotiation.
iceGatheringFinished = false; iceGatheringFinished = false;
@ -950,8 +955,7 @@ class CallSession {
: false, : false,
}; };
try { try {
return await voip.factory.navigator.mediaDevices return await voip.delegate.mediaDevices.getUserMedia(mediaConstraints);
.getUserMedia(mediaConstraints);
} catch (e) { } catch (e) {
_getUserMediaFailed(e); _getUserMediaFailed(e);
} }
@ -964,8 +968,7 @@ class CallSession {
'video': true, 'video': true,
}; };
try { try {
return await voip.factory.navigator.mediaDevices return await voip.delegate.mediaDevices.getDisplayMedia(mediaConstraints);
.getDisplayMedia(mediaConstraints);
} catch (e) { } catch (e) {
_getUserMediaFailed(e); _getUserMediaFailed(e);
} }
@ -977,7 +980,7 @@ class CallSession {
'iceServers': opts.iceServers, 'iceServers': opts.iceServers,
'sdpSemantics': 'unified-plan' 'sdpSemantics': 'unified-plan'
}; };
final pc = await voip.factory.createPeerConnection(configuration); final pc = await voip.delegate.createPeerConnection(configuration);
pc.onTrack = (RTCTrackEvent event) { pc.onTrack = (RTCTrackEvent event) {
if (event.streams.isNotEmpty) { if (event.streams.isNotEmpty) {
final stream = event.streams[0]; final stream = event.streams[0];
@ -1113,7 +1116,6 @@ class VoIP {
String? currentCID; String? currentCID;
String? get localPartyId => client.deviceID; String? get localPartyId => client.deviceID;
final Client client; final Client client;
RTCFactory get factory => delegate.rtcFactory;
final WebRTCDelegate delegate; final WebRTCDelegate delegate;
VoIP(this.client, this.delegate) : super() { VoIP(this.client, this.delegate) : super() {
@ -1128,19 +1130,6 @@ class VoIP {
client.onSDPStreamMetadataChangedReceived.stream client.onSDPStreamMetadataChangedReceived.stream
.listen(onSDPStreamMetadataChangedReceived); .listen(onSDPStreamMetadataChangedReceived);
client.onAssertedIdentityReceived.stream.listen(onAssertedIdentityReceived); client.onAssertedIdentityReceived.stream.listen(onAssertedIdentityReceived);
/* TODO: implement this in the fanedly-app.
Connectivity().onConnectivityChanged.listen(_handleNetworkChanged);
Connectivity()
.checkConnectivity()
.then((result) => _currentConnectivity = result)
.catchError((e) => _currentConnectivity = ConnectivityResult.none);
if (!kIsWeb) {
final wb = WidgetsBinding.instance;
wb!.addObserver(this);
didChangeAppLifecycleState(wb.lifecycleState!);
}
*/
} }
Future<void> onCallInvite(Event event) async { Future<void> onCallInvite(Event event) async {
@ -1214,7 +1203,7 @@ class VoIP {
.then((_) { .then((_) {
// Popup CallingPage for incoming call. // Popup CallingPage for incoming call.
if (!delegate.isBackgroud) { if (!delegate.isBackgroud) {
delegate.onNewCall?.call(newCall); delegate.handleNewCall(newCall);
} }
}); });
currentCID = callId; currentCID = callId;
@ -1292,7 +1281,7 @@ class VoIP {
// hangup in any case, either if the other party hung up or we did on another device // hangup in any case, either if the other party hung up or we did on another device
call.terminate(CallParty.kRemote, call.terminate(CallParty.kRemote,
event.content['reason'] ?? CallErrorCode.UserHangup, true); event.content['reason'] ?? CallErrorCode.UserHangup, true);
delegate.onCallEnded?.call(call); delegate.handleCallEnded(call);
} else { } else {
Logs().v('[VOIP] onCallHangup: Session [$callId] not found!'); Logs().v('[VOIP] onCallHangup: Session [$callId] not found!');
} }
@ -1441,16 +1430,6 @@ class VoIP {
} }
]; ];
} }
/*
void _handleNetworkChanged(ConnectivityResult result) async {
// Got a new connectivity status!
if (_currentConnectivity != result) {
calls.forEach((_, sess) {
sess._restartIce();
});
}
_currentConnectivity = result;
}*/
Future<CallSession> inviteToCall(String roomId, CallType type) async { Future<CallSession> inviteToCall(String roomId, CallType type) async {
final room = client.getRoomById(roomId); final room = client.getRoomById(roomId);
@ -1472,7 +1451,7 @@ class VoIP {
currentCID = callId; currentCID = callId;
await newCall.initOutboundCall(type).then((_) { await newCall.initOutboundCall(type).then((_) {
if (!delegate.isBackgroud) { if (!delegate.isBackgroud) {
delegate.onNewCall?.call(newCall); delegate.handleNewCall(newCall);
} }
}); });
currentCID = callId; currentCID = callId;