From e949d815689695f8168da948dc242ace5fcac5dc Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 30 May 2018 11:29:42 -0400 Subject: [PATCH] Fix more call state edge cases. --- Signal/src/call/CallService.swift | 33 ++++++++++++++----- .../call/UserInterface/CallUIAdapter.swift | 4 +-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Signal/src/call/CallService.swift b/Signal/src/call/CallService.swift index 4c6245ece..d81897a7a 100644 --- a/Signal/src/call/CallService.swift +++ b/Signal/src/call/CallService.swift @@ -244,10 +244,11 @@ private class SignalCallData: NSObject { if let oldValue = oldValue { DeviceSleepManager.sharedInstance.removeBlock(blockObject: oldValue) } - stopAnyCallTimer() if let callData = callData { DeviceSleepManager.sharedInstance.addBlock(blockObject: callData) self.startCallTimer() + } else { + stopAnyCallTimer() } } @@ -1233,9 +1234,9 @@ private class SignalCallData: NSObject { SwiftAssertIsOnMainThread(#function) guard let call = self.call else { - // This can happen if you tap the video button right after the other - // user hangs up. - Logger.warn("\(self.logTag) ignoring local video change; no call.") + // This can happen if you toggle local video right after + // the other user ends the call. + Logger.debug("\(self.logTag) \(#function) Ignoring event from obsolete call") return } @@ -1526,6 +1527,14 @@ private class SignalCallData: NSObject { terminateCall() } + public func terminateCallDueToCriticalError() { + SwiftAssertIsOnMainThread(#function) + + owsFail("\(logTag) \(#function)") + + terminateCall() + } + /** * Clean up any existing call state and get ready to receive a new call. */ @@ -1665,11 +1674,8 @@ private class SignalCallData: NSObject { func startCallTimer() { SwiftAssertIsOnMainThread(#function) - if self.activeCallTimer != nil { - owsFail("\(self.logTag) activeCallTimer should only be set once per call") - self.activeCallTimer!.invalidate() - self.activeCallTimer = nil - } + stopAnyCallTimer() + assert(self.activeCallTimer == nil) self.activeCallTimer = WeakTimer.scheduledTimer(timeInterval: 1, target: self, userInfo: nil, repeats: true) { [weak self] timer in guard let strongSelf = self else { @@ -1687,6 +1693,15 @@ private class SignalCallData: NSObject { } func ensureCallScreenPresented(call: SignalCall) { + guard let currentCall = self.call else { + owsFail("\(self.logTag) obsolete call: \(call.identifiersForLogs) in \(#function)") + return + } + guard currentCall == call else { + owsFail("\(self.logTag) obsolete call: \(call.identifiersForLogs) in \(#function)") + return + } + guard let connectedDate = call.connectedDate else { // Ignore; call hasn't connected yet. return diff --git a/Signal/src/call/UserInterface/CallUIAdapter.swift b/Signal/src/call/UserInterface/CallUIAdapter.swift index 72833fef4..914cd70f1 100644 --- a/Signal/src/call/UserInterface/CallUIAdapter.swift +++ b/Signal/src/call/UserInterface/CallUIAdapter.swift @@ -67,8 +67,8 @@ extension CallUIAdaptee { SwiftAssertIsOnMainThread(#function) guard self.callService.call == nil else { - Logger.info("unexpectedly found an existing call when trying to start outgoing call: \(recipientId)") - //TODO terminate existing call. + owsFail("unexpectedly found an existing call when trying to start outgoing call: \(recipientId)") + self.callService.terminateCallDueToCriticalError() return }