From edffbe7d4c362791d556605b53e3cecc5e3eadab Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Fri, 12 Nov 2021 13:32:27 +1100 Subject: [PATCH] fix data channel and uuid check --- .../ConversationVC+Interaction.swift | 2 +- Session/Meta/AppDelegate.swift | 6 +++++- .../Calls/WebRTCSession+DataChannel.swift | 15 ++++++++++---- SessionMessagingKit/Calls/WebRTCSession.swift | 20 +++++-------------- .../MessageReceiver+Handling.swift | 6 +++++- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index 0aa3c5985..2f3a3cd9c 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -35,7 +35,7 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc } else if SSKPreferences.areCallsEnabled { guard let contactSessionID = (thread as? TSContactThread)?.contactSessionID() else { return } guard AppEnvironment.shared.callManager.currentCall == nil else { return } - let call = SessionCall(for: contactSessionID, uuid: UUID().uuidString, mode: .offer, outgoing: true) + let call = SessionCall(for: contactSessionID, uuid: UUID().uuidString.lowercased(), mode: .offer, outgoing: true) let callVC = CallVC(for: call) callVC.conversationVC = self self.inputAccessoryView?.isHidden = true diff --git a/Session/Meta/AppDelegate.swift b/Session/Meta/AppDelegate.swift index e7a7dd2f1..f0e5c45e8 100644 --- a/Session/Meta/AppDelegate.swift +++ b/Session/Meta/AppDelegate.swift @@ -65,7 +65,11 @@ extension AppDelegate { // Answer messages MessageReceiver.handleAnswerCallMessage = { message in DispatchQueue.main.async { - guard let call = AppEnvironment.shared.callManager.currentCall, message.uuid! == call.uuid else { return } + guard let call = AppEnvironment.shared.callManager.currentCall, message.uuid! == call.uuid else { + let call = AppEnvironment.shared.callManager.currentCall + print("[Calls] \(call == nil), \(message.uuid!), \(call?.uuid)") + return + } AppEnvironment.shared.callManager.invalidateTimeoutTimer() call.hasStartedConnecting = true let sdp = RTCSessionDescription(type: .answer, sdp: message.sdps![0]) diff --git a/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift b/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift index 1587e081a..acdfe5be0 100644 --- a/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift +++ b/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift @@ -5,7 +5,10 @@ extension WebRTCSession: RTCDataChannelDelegate { internal func createDataChannel() -> RTCDataChannel? { let dataChannelConfiguration = RTCDataChannelConfiguration() - guard let dataChannel = peerConnection.dataChannel(forLabel: "VIDEOCONTROL", configuration: dataChannelConfiguration) else { + dataChannelConfiguration.isOrdered = true + dataChannelConfiguration.isNegotiated = true + dataChannelConfiguration.channelId = 548 + guard let dataChannel = peerConnection.dataChannel(forLabel: "CONTROL", configuration: dataChannelConfiguration) else { print("[Calls] Couldn't create data channel.") return nil } @@ -13,7 +16,8 @@ extension WebRTCSession: RTCDataChannelDelegate { } public func sendJSON(_ json: JSON) { - if let dataChannel = remoteDataChannel, let jsonAsData = try? JSONSerialization.data(withJSONObject: json, options: [ .fragmentsAllowed ]) { + if let dataChannel = self.dataChannel, let jsonAsData = try? JSONSerialization.data(withJSONObject: json, options: [ .fragmentsAllowed ]) { + print("[Calls] Send json to data channel") let dataBuffer = RTCDataBuffer(data: jsonAsData, isBinary: false) dataChannel.sendData(dataBuffer) } @@ -21,12 +25,15 @@ extension WebRTCSession: RTCDataChannelDelegate { // MARK: Data channel delegate public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) { - print("[Calls] Data channel did change to \(dataChannel.readyState)") + print("[Calls] Data channel did change to \(dataChannel.readyState.rawValue)") + if dataChannel.readyState == .open { + delegate?.dataChannelDidOpen() + } } public func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) { - print("[Calls] Data channel did receive data: \(buffer)") if let json = try? JSONSerialization.jsonObject(with: buffer.data, options: [ .fragmentsAllowed ]) as? JSON { + print("[Calls] Data channel did receive data: \(json)") if let isRemoteVideoEnabled = json["video"] as? Bool { delegate?.isRemoteVideoDidChange(isEnabled: isRemoteVideoEnabled) } diff --git a/SessionMessagingKit/Calls/WebRTCSession.swift b/SessionMessagingKit/Calls/WebRTCSession.swift index fa8b37591..11fd3ad27 100644 --- a/SessionMessagingKit/Calls/WebRTCSession.swift +++ b/SessionMessagingKit/Calls/WebRTCSession.swift @@ -18,17 +18,11 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { private var queuedICECandidates: [RTCIceCandidate] = [] private var iceCandidateSendTimer: Timer? - private let defaultICEServers = [ - "stun:stun.l.google.com:19302", - "stun:stun1.l.google.com:19302", - "stun:stun2.l.google.com:19302", - "stun:stun3.l.google.com:19302", - "stun:stun4.l.google.com:19302" - ] - internal lazy var factory: RTCPeerConnectionFactory = { RTCInitializeSSL() - return RTCPeerConnectionFactory() + let videoEncoderFactory = RTCDefaultVideoEncoderFactory() + let videoDecoderFactory = RTCDefaultVideoDecoderFactory() + return RTCPeerConnectionFactory(encoderFactory: videoEncoderFactory, decoderFactory: videoDecoderFactory) }() /// Represents a WebRTC connection between the user and a remote peer. Provides methods to connect to a @@ -36,7 +30,6 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { internal lazy var peerConnection: RTCPeerConnection = { let configuration = RTCConfiguration() configuration.iceServers = [ RTCIceServer(urlStrings: ["stun:freyr.getsession.org:5349"]), RTCIceServer(urlStrings: ["turn:freyr.getsession.org"], username: "session", credential: "session") ] -// configuration.iceServers = [ RTCIceServer(urlStrings: defaultICEServers) ] configuration.sdpSemantics = .unifiedPlan let constraints = RTCMediaConstraints(mandatoryConstraints: [:], optionalConstraints: [:]) return factory.peerConnection(with: configuration, constraints: constraints, delegate: self) @@ -68,8 +61,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { }() // Data Channel - internal var localDataChannel: RTCDataChannel? - internal var remoteDataChannel: RTCDataChannel? + internal var dataChannel: RTCDataChannel? // MARK: Error public enum Error : LocalizedError { @@ -100,7 +92,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { // Data channel if let dataChannel = createDataChannel() { dataChannel.delegate = self - self.localDataChannel = dataChannel + self.dataChannel = dataChannel } // Network reachability @@ -287,8 +279,6 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { public func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) { print("[Calls] Data channel opened.") - self.remoteDataChannel = dataChannel - delegate?.dataChannelDidOpen() } } diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 4db78aa44..bd4bb37eb 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -286,7 +286,11 @@ extension MessageReceiver { handleOfferCallMessage?(message) case .answer: print("[Calls] Received answer message.") - guard let currentWebRTCSession = WebRTCSession.current, currentWebRTCSession.uuid == message.uuid! else { return } + guard let currentWebRTCSession = WebRTCSession.current, currentWebRTCSession.uuid == message.uuid! else { + let currentWebRTCSession = WebRTCSession.current + print("[Calls] \(currentWebRTCSession == nil), \(currentWebRTCSession?.uuid) \(message.uuid)") + return + } handleAnswerCallMessage?(message) case .provisionalAnswer: break // TODO: Implement case let .iceCandidates(sdpMLineIndexes, sdpMids):