fix data channel and uuid check

pull/560/head
ryanzhao 3 years ago
parent 925bc8538c
commit edffbe7d4c

@ -35,7 +35,7 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc
} else if SSKPreferences.areCallsEnabled { } else if SSKPreferences.areCallsEnabled {
guard let contactSessionID = (thread as? TSContactThread)?.contactSessionID() else { return } guard let contactSessionID = (thread as? TSContactThread)?.contactSessionID() else { return }
guard AppEnvironment.shared.callManager.currentCall == nil 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) let callVC = CallVC(for: call)
callVC.conversationVC = self callVC.conversationVC = self
self.inputAccessoryView?.isHidden = true self.inputAccessoryView?.isHidden = true

@ -65,7 +65,11 @@ extension AppDelegate {
// Answer messages // Answer messages
MessageReceiver.handleAnswerCallMessage = { message in MessageReceiver.handleAnswerCallMessage = { message in
DispatchQueue.main.async { 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() AppEnvironment.shared.callManager.invalidateTimeoutTimer()
call.hasStartedConnecting = true call.hasStartedConnecting = true
let sdp = RTCSessionDescription(type: .answer, sdp: message.sdps![0]) let sdp = RTCSessionDescription(type: .answer, sdp: message.sdps![0])

@ -5,7 +5,10 @@ extension WebRTCSession: RTCDataChannelDelegate {
internal func createDataChannel() -> RTCDataChannel? { internal func createDataChannel() -> RTCDataChannel? {
let dataChannelConfiguration = RTCDataChannelConfiguration() 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.") print("[Calls] Couldn't create data channel.")
return nil return nil
} }
@ -13,7 +16,8 @@ extension WebRTCSession: RTCDataChannelDelegate {
} }
public func sendJSON(_ json: JSON) { 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) let dataBuffer = RTCDataBuffer(data: jsonAsData, isBinary: false)
dataChannel.sendData(dataBuffer) dataChannel.sendData(dataBuffer)
} }
@ -21,12 +25,15 @@ extension WebRTCSession: RTCDataChannelDelegate {
// MARK: Data channel delegate // MARK: Data channel delegate
public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) { 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) { 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 { 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 { if let isRemoteVideoEnabled = json["video"] as? Bool {
delegate?.isRemoteVideoDidChange(isEnabled: isRemoteVideoEnabled) delegate?.isRemoteVideoDidChange(isEnabled: isRemoteVideoEnabled)
} }

@ -18,17 +18,11 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
private var queuedICECandidates: [RTCIceCandidate] = [] private var queuedICECandidates: [RTCIceCandidate] = []
private var iceCandidateSendTimer: Timer? 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 = { internal lazy var factory: RTCPeerConnectionFactory = {
RTCInitializeSSL() 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 /// 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 = { internal lazy var peerConnection: RTCPeerConnection = {
let configuration = RTCConfiguration() 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: ["stun:freyr.getsession.org:5349"]), RTCIceServer(urlStrings: ["turn:freyr.getsession.org"], username: "session", credential: "session") ]
// configuration.iceServers = [ RTCIceServer(urlStrings: defaultICEServers) ]
configuration.sdpSemantics = .unifiedPlan configuration.sdpSemantics = .unifiedPlan
let constraints = RTCMediaConstraints(mandatoryConstraints: [:], optionalConstraints: [:]) let constraints = RTCMediaConstraints(mandatoryConstraints: [:], optionalConstraints: [:])
return factory.peerConnection(with: configuration, constraints: constraints, delegate: self) return factory.peerConnection(with: configuration, constraints: constraints, delegate: self)
@ -68,8 +61,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
}() }()
// Data Channel // Data Channel
internal var localDataChannel: RTCDataChannel? internal var dataChannel: RTCDataChannel?
internal var remoteDataChannel: RTCDataChannel?
// MARK: Error // MARK: Error
public enum Error : LocalizedError { public enum Error : LocalizedError {
@ -100,7 +92,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
// Data channel // Data channel
if let dataChannel = createDataChannel() { if let dataChannel = createDataChannel() {
dataChannel.delegate = self dataChannel.delegate = self
self.localDataChannel = dataChannel self.dataChannel = dataChannel
} }
// Network reachability // Network reachability
@ -287,8 +279,6 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
public func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) { public func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) {
print("[Calls] Data channel opened.") print("[Calls] Data channel opened.")
self.remoteDataChannel = dataChannel
delegate?.dataChannelDidOpen()
} }
} }

@ -286,7 +286,11 @@ extension MessageReceiver {
handleOfferCallMessage?(message) handleOfferCallMessage?(message)
case .answer: case .answer:
print("[Calls] Received answer message.") 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) handleAnswerCallMessage?(message)
case .provisionalAnswer: break // TODO: Implement case .provisionalAnswer: break // TODO: Implement
case let .iceCandidates(sdpMLineIndexes, sdpMids): case let .iceCandidates(sdpMLineIndexes, sdpMids):

Loading…
Cancel
Save