chore: add more interface for delegate.

This commit is contained in:
cloudwebrtc 2021-11-27 01:29:11 +08:00
parent 60618d1775
commit 01276bbf60
1 changed files with 19 additions and 52 deletions

View File

@ -8,7 +8,13 @@ import '../matrix.dart';
abstract class WebRTCDelegate { abstract class WebRTCDelegate {
RTCFactory get rtcFactory; RTCFactory get rtcFactory;
bool get isBackgroud;
bool get isWeb;
VideoRenderer createRenderer(); VideoRenderer createRenderer();
void playRingtone();
void stopRingtone();
Function(CallSession session)? onNewCall;
Function(CallSession session)? onCallEnded;
} }
/// The default life time for call events, in millisecond. /// The default life time for call events, in millisecond.
@ -553,8 +559,7 @@ class CallSession {
if (purpose == SDPStreamMetadataPurpose.Usermedia) { if (purpose == SDPStreamMetadataPurpose.Usermedia) {
speakerOn = type == CallType.kVideo; speakerOn = type == CallType.kVideo;
//TODO: Confirm that the platform is not Web. if (voip.delegate.isWeb && !voip.delegate.isBackgroud) {
if (/*!kIsWeb && */ !voip.background) {
final audioTrack = stream.getAudioTracks()[0]; final audioTrack = stream.getAudioTracks()[0];
audioTrack.enableSpeakerphone(speakerOn); audioTrack.enableSpeakerphone(speakerOn);
} }
@ -653,29 +658,12 @@ class CallSession {
return callOnHold; return callOnHold;
} }
void setSpeakerOn() {
speakerOn = !speakerOn;
}
//TODO: move to the app.
Future<void> switchCamera() async {
if (localUserMediaStream != null) {
/*
await Helper.switchCamera(
localUserMediaStream!.stream!.getVideoTracks()[0]);
if (kIsMobile) {
facingMode == 'user' ? facingMode = 'environment' : facingMode = 'user';
}
*/
}
}
void answer() async { void answer() async {
if (inviteOrAnswerSent) { if (inviteOrAnswerSent) {
return; return;
} }
// stop play ringtone // stop play ringtone
voip.stopRingTone(); voip.delegate.stopRingtone();
if (direction == CallDirection.kIncoming) { if (direction == CallDirection.kIncoming) {
setCallState(CallState.kCreateAnswer); setCallState(CallState.kCreateAnswer);
@ -722,7 +710,7 @@ class CallSession {
void hangup([String? reason, bool suppressEvent = true]) async { void hangup([String? reason, bool suppressEvent = true]) async {
// stop play ringtone // stop play ringtone
voip.stopRingTone(); voip.delegate.stopRingtone();
terminate( terminate(
CallParty.kLocal, reason ?? CallErrorCode.UserHangup, !suppressEvent); CallParty.kLocal, reason ?? CallErrorCode.UserHangup, !suppressEvent);
@ -1123,10 +1111,7 @@ class VoIP {
TurnServerCredentials? _turnServerCredentials; TurnServerCredentials? _turnServerCredentials;
Map<String, CallSession> calls = <String, CallSession>{}; Map<String, CallSession> calls = <String, CallSession>{};
String? currentCID; String? currentCID;
Function(CallSession session)? onNewCall;
Function(CallSession session)? onCallEnded;
String? get localPartyId => client.deviceID; String? get localPartyId => client.deviceID;
bool background = false;
final Client client; final Client client;
RTCFactory get factory => delegate.rtcFactory; RTCFactory get factory => delegate.rtcFactory;
final WebRTCDelegate delegate; final WebRTCDelegate delegate;
@ -1228,38 +1213,20 @@ class VoIP {
.initWithInvite(callType, offer, sdpStreamMetadata, lifetime) .initWithInvite(callType, offer, sdpStreamMetadata, lifetime)
.then((_) { .then((_) {
// Popup CallingPage for incoming call. // Popup CallingPage for incoming call.
if (!background) { if (!delegate.isBackgroud) {
onNewCall?.call(newCall); delegate.onNewCall?.call(newCall);
} }
}); });
currentCID = callId; currentCID = callId;
if (background) { if (delegate.isBackgroud) {
/// Forced to enable signaling synchronization until the end of the call. /// Forced to enable signaling synchronization until the end of the call.
client.backgroundSync = true; client.backgroundSync = true;
///TODO: notify the callkeep that the call is incoming. ///TODO: notify the callkeep that the call is incoming.
} }
// Play ringtone // Play ringtone
playRingtone(); delegate.playRingtone();
}
void playRingtone() async {
if (!background) {
try {
// TODO: callback the event to the user.
// await UserMediaManager().startRinginTone();
} catch (_) {}
}
}
void stopRingTone() async {
if (!background) {
try {
// TODO:
// await UserMediaManager().stopRingingTone();
} catch (_) {}
}
} }
void onCallAnswer(Event event) async { void onCallAnswer(Event event) async {
@ -1272,7 +1239,7 @@ class VoIP {
if (event.senderId == client.userID) { if (event.senderId == client.userID) {
// Ignore messages to yourself. // Ignore messages to yourself.
if (!call._answeredByUs) { if (!call._answeredByUs) {
stopRingTone(); delegate.stopRingtone();
} }
return; return;
} }
@ -1315,8 +1282,8 @@ class VoIP {
void onCallHangup(Event event) async { void onCallHangup(Event event) async {
// stop play ringtone, if this is an incoming call // stop play ringtone, if this is an incoming call
if (!background) { if (!delegate.isBackgroud) {
stopRingTone(); delegate.stopRingtone();
} }
Logs().v('[VOIP] onCallHangup => ${event.content.toString()}'); Logs().v('[VOIP] onCallHangup => ${event.content.toString()}');
final String callId = event.content['call_id']; final String callId = event.content['call_id'];
@ -1325,7 +1292,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);
onCallEnded?.call(call); delegate.onCallEnded?.call(call);
} else { } else {
Logs().v('[VOIP] onCallHangup: Session [$callId] not found!'); Logs().v('[VOIP] onCallHangup: Session [$callId] not found!');
} }
@ -1504,8 +1471,8 @@ class VoIP {
final newCall = createNewCall(opts); final newCall = createNewCall(opts);
currentCID = callId; currentCID = callId;
await newCall.initOutboundCall(type).then((_) { await newCall.initOutboundCall(type).then((_) {
if (!background) { if (!delegate.isBackgroud) {
onNewCall?.call(newCall); delegate.onNewCall?.call(newCall);
} }
}); });
currentCID = callId; currentCID = callId;