refactor timeout logic for calls incoming and reconnecting

This commit is contained in:
ryanzhao 2022-04-06 15:41:38 +10:00
parent 7ae982a328
commit 5fa63286da
4 changed files with 25 additions and 4 deletions

View File

@ -118,6 +118,8 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
var hasConnectedDidChange: (() -> Void)?
var hasEndedDidChange: (() -> Void)?
var remoteVideoStateDidChange: ((Bool) -> Void)?
var hasStartedReconnecting: (() -> Void)?
var hasReconnected: (() -> Void)?
// MARK: Derived Properties
var hasStartedConnecting: Bool {
@ -172,6 +174,7 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
func reportIncomingCallIfNeeded(completion: @escaping (Error?) -> Void) {
guard case .answer = mode else { return }
setupTimeoutTimer()
AppEnvironment.shared.callManager.reportIncomingCall(self, callerName: contactName) { error in
completion(error)
}
@ -282,8 +285,12 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
// MARK: Delegate
public func webRTCIsConnected() {
self.invalidateTimeoutTimer()
self.reconnectTimer?.invalidate()
guard !self.hasConnected else { return }
guard !self.hasConnected else {
hasReconnected?()
return
}
self.hasConnected = true
self.answerCallAction?.fulfill()
}
@ -312,6 +319,8 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
}
public func reconnectIfNeeded() {
setupTimeoutTimer()
hasStartedReconnecting?()
guard isOutgoing else { return }
tryToReconnect()
}
@ -331,7 +340,9 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
// MARK: Timeout
public func setupTimeoutTimer() {
timeOutTimer = Timer.scheduledTimerOnMainThread(withTimeInterval: 30, repeats: false) { _ in
invalidateTimeoutTimer()
let timeInterval: TimeInterval = hasConnected ? 60 : 30
timeOutTimer = Timer.scheduledTimerOnMainThread(withTimeInterval: timeInterval, repeats: false) { _ in
self.didTimeout = true
AppEnvironment.shared.callManager.endCall(self) { error in
self.timeOutTimer = nil

View File

@ -129,7 +129,7 @@ public final class SessionCallManager: NSObject {
let message = CallMessage()
message.uuid = offerMessage.uuid
message.kind = .endCall
SNLog("[Calls] Sending end call message.")
SNLog("[Calls] Sending end call message because there is an ongoing call.")
MessageSender.sendNonDurably(message, in: thread, using: transaction).retainUntilComplete()
let infoMessage = TSInfoMessage.from(offerMessage, associatedWith: thread)
infoMessage.save(with: transaction)

View File

@ -234,6 +234,17 @@ final class CallVC : UIViewController, VideoPreviewDelegate {
self.handleEndCallMessage()
}
}
self.call.hasStartedReconnecting = {
DispatchQueue.main.async {
self.callInfoLabel.text = "Reconnecting..."
self.callInfoLabel.isHidden = false
}
}
self.call.hasReconnected = {
DispatchQueue.main.async {
self.callInfoLabel.isHidden = true
}
}
}
required init(coder: NSCoder) { preconditionFailure("Use init(for:) instead.") }

View File

@ -120,7 +120,6 @@ extension AppDelegate {
self.dismissAllCallUI()
AppEnvironment.shared.callManager.reportCurrentCallEnded(reason: .answeredElsewhere)
} else {
call.invalidateTimeoutTimer()
call.hasStartedConnecting = true
let sdp = RTCSessionDescription(type: .answer, sdp: message.sdps![0])
call.didReceiveRemoteSDP(sdp: sdp)