diff --git a/Neuro App/HomeView.swift b/Neuro App/HomeView.swift index c92f306..6866007 100644 --- a/Neuro App/HomeView.swift +++ b/Neuro App/HomeView.swift @@ -132,7 +132,7 @@ struct HomeView: View { .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) .padding() .onAppear { - signalingClient.fetchOnlineUsers() + } .background( LinearGradient(colors: [.gray, .white, .gray], startPoint: .topLeading, endPoint: .bottomTrailing) diff --git a/Neuro App/SignalingClient.swift b/Neuro App/SignalingClient.swift index 9462dd3..5eaecb9 100644 --- a/Neuro App/SignalingClient.swift +++ b/Neuro App/SignalingClient.swift @@ -207,7 +207,7 @@ final class SignalingClient: NSObject, RTCPeerConnectionDelegate, } } } - + startFetchingOnlineUsers() // print("Unique ID OUTSIDE OF CODE IS ", uniqueID) } @@ -268,7 +268,7 @@ final class SignalingClient: NSObject, RTCPeerConnectionDelegate, var sdpString = "placeholderText" theirPeerID = id // implement logic to call a user - + print("Calling user with ID: \(id)") let constraints = RTCMediaConstraints( @@ -327,7 +327,8 @@ final class SignalingClient: NSObject, RTCPeerConnectionDelegate, } }) - isReadToAddIceCandidate = true + self.webRTCClient.peerConnection.delegate = self + //isReadToAddIceCandidate = true isRinging = true } @@ -376,7 +377,7 @@ final class SignalingClient: NSObject, RTCPeerConnectionDelegate, // Reset the signaling client state isRinging = false isInCall = false - + isReadToAddIceCandidate = false // Inform delegate about disconnection self.delegate?.signalClientDidDisconnect(self) @@ -422,7 +423,7 @@ extension SignalingClient: WebSocketProviderDelegate { } func webSocket(_ webSocket: WebSocketProvider, didReceiveData data: Data) { - + } // handle message is where we determine what to do when the server sends us a message @@ -496,9 +497,10 @@ extension SignalingClient: WebSocketProviderDelegate { Task { do { - try await self.webRTCClient.peerConnection.setRemoteDescription( + await self.webRTCClient.setRemoteSDP( remoteSDP) - + self.isReadToAddIceCandidate = true + self.handleIceCandidates() } catch { debugPrint("Error setting remoteDescriptionToAnswer") @@ -538,7 +540,7 @@ extension SignalingClient: WebSocketProviderDelegate { // this is the part where we "answer" their message func handleOfferMessage() { - + let msg = offerMessage["sdp"] as? [String: Any] let sdp = msg?["sdp"] print("IN HANDLE OFFER MESG TRYNA GET THEIR REMOTE SDP", msg) @@ -549,12 +551,17 @@ extension SignalingClient: WebSocketProviderDelegate { print("Processed sdp:", sdp as Any) let sessionDescription = RTCSessionDescription( type: RTCSdpType.offer, sdp: sdp as! String) - + self.webRTCClient.createAndAssignPeerConnection() + self.webRTCClient.setMediaSettings() + self.webRTCClient.peerConnection.delegate = self if #available(iOS 13.0, *) { Task { print("WE ARE GONNA SEND AN ANSWER SOON") // first we set the remote sdp (we received an offer) + + await self.webRTCClient.setRemoteSDP(sessionDescription) + // then we create an answer sdp await self.webRTCClient.setPeerSDP( sessionDescription, theirSrc, connectionID @@ -579,6 +586,7 @@ extension SignalingClient: WebSocketProviderDelegate { } } + } } else { // Fallback on earlier versions diff --git a/Neuro App/WebRTCClient.swift b/Neuro App/WebRTCClient.swift index 38074bd..88653af 100644 --- a/Neuro App/WebRTCClient.swift +++ b/Neuro App/WebRTCClient.swift @@ -27,7 +27,7 @@ final class WebRTCClient: NSObject, ObservableObject { }() weak var delegate: WebRTCClientDelegate? - let peerConnection: RTCPeerConnection + var peerConnection: RTCPeerConnection private let rtcAudioSession = RTCAudioSession.sharedInstance() private let audioQueue = DispatchQueue(label: "audio") private let mediaConstrains = [kRTCMediaConstraintsOfferToReceiveAudio: kRTCMediaConstraintsValueTrue, @@ -37,6 +37,7 @@ final class WebRTCClient: NSObject, ObservableObject { public var remoteVideoTrack: RTCVideoTrack? private var localDataChannel: RTCDataChannel? public var remoteDataChannel: RTCDataChannel? + private var iceServers : [String] @available(*, unavailable) override init() { @@ -44,6 +45,27 @@ final class WebRTCClient: NSObject, ObservableObject { } required init(iceServers: [String]) { + self.iceServers = iceServers +// let config = RTCConfiguration() +// config.iceServers = [RTCIceServer(urlStrings: iceServers)] +// +// // Unified plan is more superior than planB +// config.sdpSemantics = .unifiedPlan +// +// // gatherContinually will let WebRTC to listen to any network changes and send any new candidates to the other client +// config.continualGatheringPolicy = .gatherOnce +// config.iceTransportPolicy = .all +// +// // Define media constraints. DtlsSrtpKeyAgreement is required to be true to be able to connect with web browsers. +// let constraints = RTCMediaConstraints(mandatoryConstraints: nil, +// optionalConstraints: ["DtlsSrtpKeyAgreement": kRTCMediaConstraintsValueTrue, "setup": "actpass"]) +// // creating a peerConnection +// guard let peerConnection = WebRTCClient.factory.peerConnection(with: config, constraints: constraints, delegate: nil) else { +// debugPrint("peerconnection failed") +// fatalError("Could not create new RTCPeerConnection") +// } +// debugPrint("webrtc made (does not impact signaling client or websocket)") +// self.peerConnection = peerConnection let config = RTCConfiguration() config.iceServers = [RTCIceServer(urlStrings: iceServers)] @@ -64,18 +86,47 @@ final class WebRTCClient: NSObject, ObservableObject { } debugPrint("webrtc made (does not impact signaling client or websocket)") self.peerConnection = peerConnection - super.init() + + self.setMediaSettings() + } + + func setMediaSettings(){ self.createMediaSenders() self.configureAudioSession() // self.peerConnection.delegate = self self.setVideoEnabled(true) + } + + func createAndAssignPeerConnection() { + let config = RTCConfiguration() + config.iceServers = [RTCIceServer(urlStrings: iceServers)] + + // Unified plan is more superior than planB + config.sdpSemantics = .unifiedPlan + // gatherContinually will let WebRTC to listen to any network changes and send any new candidates to the other client + config.continualGatheringPolicy = .gatherOnce + config.iceTransportPolicy = .all + + // Define media constraints. DtlsSrtpKeyAgreement is required to be true to be able to connect with web browsers. + let constraints = RTCMediaConstraints(mandatoryConstraints: nil, + optionalConstraints: ["DtlsSrtpKeyAgreement": kRTCMediaConstraintsValueTrue, "setup": "actpass"]) + // creating a peerConnection + guard let peerConnection = WebRTCClient.factory.peerConnection(with: config, constraints: constraints, delegate: nil) else { + debugPrint("peerconnection failed") + fatalError("Could not create new RTCPeerConnection") + } + debugPrint("webrtc made (does not impact signaling client or websocket)") + self.peerConnection = peerConnection + } // MARK: Signaling func offer(completion: @escaping (_ sdp: RTCSessionDescription) -> Void) { + self.createAndAssignPeerConnection() + self.createMediaSenders() let constrains = RTCMediaConstraints(mandatoryConstraints: self.mediaConstrains, optionalConstraints: nil) @@ -147,7 +198,8 @@ final class WebRTCClient: NSObject, ObservableObject { } func answer(completion: @escaping (_ sdp: RTCSessionDescription) -> Void) { - + + let constrains = RTCMediaConstraints(mandatoryConstraints: self.mediaConstrains, optionalConstraints: nil) // debugPrint("constrains are ", constrains as Any) @@ -239,6 +291,7 @@ final class WebRTCClient: NSObject, ObservableObject { dataChannel.delegate = self self.localDataChannel = dataChannel } + } private func createAudioTrack() -> RTCAudioTrack {