minor refactoring & handle network change

This commit is contained in:
ryanzhao 2021-11-11 16:09:39 +11:00
parent 336c694b52
commit fa79124bb7
6 changed files with 43 additions and 25 deletions

View file

@ -13,7 +13,6 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
let isOutgoing: Bool
var remoteSDP: RTCSessionDescription? = nil
var callMessageTimestamp: UInt64?
var isWaitingForRemoteSDP = false
var answerCallAction: CXAnswerCallAction? = nil
var contactName: String {
let contact = Storage.shared.getContact(with: self.sessionID)
@ -157,11 +156,9 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
}
func didReceiveRemoteSDP(sdp: RTCSessionDescription) {
guard remoteSDP == nil else { return }
remoteSDP = sdp
if isWaitingForRemoteSDP {
if hasStartedConnecting {
webRTCSession.handleRemoteSDP(sdp, from: sessionID) // This sends an answer message internally
isWaitingForRemoteSDP = false
}
}
@ -186,8 +183,6 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
hasStartedConnecting = true
if let sdp = remoteSDP {
webRTCSession.handleRemoteSDP(sdp, from: sessionID) // This sends an answer message internally
} else {
isWaitingForRemoteSDP = true
}
}
@ -222,12 +217,18 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
let durationString = NSString.formatDurationSeconds(UInt32(self.duration), useShortFormat: true)
newMessageBody = "\(self.isOutgoing ? NSLocalizedString("call_outgoing", comment: "") : NSLocalizedString("call_incoming", comment: "")): \(durationString)"
shouldMarkAsRead = true
} else if self.hasStartedConnecting {
newMessageBody = NSLocalizedString("call_cancelled", comment: "")
shouldMarkAsRead = true
} else {
switch mode {
case .local:
newMessageBody = self.isOutgoing ? NSLocalizedString("call_cancelled", comment: "") : NSLocalizedString("call_rejected", comment: "")
shouldMarkAsRead = true
case .remote:
if self.hasStartedConnecting {
}
newMessageBody = self.isOutgoing ? NSLocalizedString("call_rejected", comment: "") : NSLocalizedString("call_missing", comment: "")
}
}
@ -254,6 +255,7 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
// MARK: Delegate
public func webRTCIsConnected() {
guard !self.hasConnected else { return }
self.hasConnected = true
self.answerCallAction?.fulfill()
}
@ -263,6 +265,7 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
}
public func didReceiveHangUpSignal() {
self.hasEnded = true
DispatchQueue.main.async {
if let currentBanner = IncomingCallBanner.current { currentBanner.dismiss() }
if let callVC = CurrentAppContext().frontmostViewController() as? CallVC { callVC.handleEndCallMessage() }

View file

@ -193,8 +193,7 @@ final class CallVC : UIViewController, VideoPreviewDelegate {
}
self.call.hasEndedDidChange = {
DispatchQueue.main.async {
self.conversationVC?.showInputAccessoryView()
self.presentingViewController?.dismiss(animated: true, completion: nil)
self.handleEndCallMessage()
}
}
}
@ -331,6 +330,10 @@ final class CallVC : UIViewController, VideoPreviewDelegate {
self.call.endSessionCall()
AppEnvironment.shared.callManager.reportCurrentCallEnded(reason: nil)
}
DispatchQueue.main.async {
self.conversationVC?.showInputAccessoryView()
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
}

View file

@ -25,7 +25,7 @@ extension AppDelegate {
MessageReceiver.handlePreOfferCallMessage = { (message, transaction) in
guard CurrentAppContext().isMainApp else { return }
let callManager = AppEnvironment.shared.callManager
guard callManager.currentCall == nil || !SSKPreferences.areCallsEnabled else {
guard callManager.currentCall == nil && SSKPreferences.areCallsEnabled else {
callManager.handleIncomingCallOfferInBusyOrUnenabledState(offerMessage: message, using: transaction)
return
}
@ -65,6 +65,10 @@ extension AppDelegate {
// Answer messages
MessageReceiver.handleAnswerCallMessage = { message in
DispatchQueue.main.async {
guard let call = AppEnvironment.shared.callManager.currentCall, message.uuid == call.uuid.uuidString else { return }
call.hasStartedConnecting = true
let sdp = RTCSessionDescription(type: .answer, sdp: message.sdps![0])
call.didReceiveRemoteSDP(sdp: sdp)
guard let callVC = CurrentAppContext().frontmostViewController() as? CallVC else { return }
callVC.handleAnswerMessage(message)
}

View file

@ -13,8 +13,7 @@ extension WebRTCSession {
if let error = error {
SNLog("[Calls] Couldn't set SDP due to error: \(error).")
} else {
guard let self = self,
sdp.type == .offer, self.peerConnection.localDescription == nil else { return }
guard let self = self, sdp.type == .offer else { return }
Storage.write { transaction in
self.sendAnswer(to: sessionID, using: transaction).retainUntilComplete()
}

View file

@ -41,15 +41,6 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
return factory.peerConnection(with: configuration, constraints: constraints, delegate: self)
}()
internal lazy var mediaConstraints: RTCMediaConstraints = {
let mandatory: [String:String] = [
kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue,
kRTCMediaConstraintsOfferToReceiveVideo : kRTCMediaConstraintsValueTrue,
]
let optional: [String:String] = [:]
return RTCMediaConstraints(mandatoryConstraints: mandatory, optionalConstraints: optional)
}()
// Audio
internal lazy var audioSource: RTCAudioSource = {
let constraints = RTCMediaConstraints(mandatoryConstraints: [:], optionalConstraints: [:])
@ -110,6 +101,16 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
dataChannel.delegate = self
self.localDataChannel = dataChannel
}
// Network reachability
NotificationCenter.default.addObserver(forName: .reachabilityChanged, object: nil, queue: nil) { _ in
print("[Calls] Reachability did change.")
if self.peerConnection.signalingState == .stable {
Storage.write { transaction in
self.sendOffer(to: self.contactSessionID, using: transaction, isRestartingICEConnection: true).retainUntilComplete()
}
}
}
}
// MARK: Signaling
@ -133,11 +134,11 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
return promise
}
public func sendOffer(to sessionID: String, using transaction: YapDatabaseReadWriteTransaction) -> Promise<Void> {
public func sendOffer(to sessionID: String, using transaction: YapDatabaseReadWriteTransaction, isRestartingICEConnection: Bool = false) -> Promise<Void> {
print("[Calls] Sending offer message.")
guard let thread = TSContactThread.fetch(for: sessionID, using: transaction) else { return Promise(error: Error.noThread) }
let (promise, seal) = Promise<Void>.pending()
peerConnection.offer(for: mediaConstraints) { [weak self] sdp, error in
peerConnection.offer(for: mediaConstraints(isRestartingICEConnection)) { [weak self] sdp, error in
if let error = error {
seal.reject(error)
} else {
@ -169,7 +170,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
print("[Calls] Sending answer message.")
guard let thread = TSContactThread.fetch(for: sessionID, using: transaction) else { return Promise(error: Error.noThread) }
let (promise, seal) = Promise<Void>.pending()
peerConnection.answer(for: mediaConstraints) { [weak self] sdp, error in
peerConnection.answer(for: mediaConstraints(false)) { [weak self] sdp, error in
if let error = error {
seal.reject(error)
} else {
@ -236,6 +237,16 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
peerConnection.close()
}
private func mediaConstraints(_ isRestartingICEConnection: Bool) -> RTCMediaConstraints {
var mandatory: [String:String] = [
kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue,
kRTCMediaConstraintsOfferToReceiveVideo : kRTCMediaConstraintsValueTrue,
]
if isRestartingICEConnection { mandatory[kRTCMediaConstraintsIceRestart] = kRTCMediaConstraintsValueTrue }
let optional: [String:String] = [:]
return RTCMediaConstraints(mandatoryConstraints: mandatory, optionalConstraints: optional)
}
// MARK: Peer connection delegate
public func peerConnection(_ peerConnection: RTCPeerConnection, didChange state: RTCSignalingState) {
print("[Calls] Signaling state changed to: \(state).")

View file

@ -287,8 +287,6 @@ extension MessageReceiver {
case .answer:
print("[Calls] Received answer message.")
guard let currentWebRTCSession = WebRTCSession.current, currentWebRTCSession.uuid == message.uuid! else { return }
let sdp = RTCSessionDescription(type: .answer, sdp: message.sdps![0])
currentWebRTCSession.handleRemoteSDP(sdp, from: message.sender!)
handleAnswerCallMessage?(message)
case .provisionalAnswer: break // TODO: Implement
case let .iceCandidates(sdpMLineIndexes, sdpMids):