Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Neuro App/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 17 additions & 9 deletions Neuro App/SignalingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ final class SignalingClient: NSObject, RTCPeerConnectionDelegate,
}
}
}

startFetchingOnlineUsers()
// print("Unique ID OUTSIDE OF CODE IS ", uniqueID)

}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -327,7 +327,8 @@ final class SignalingClient: NSObject, RTCPeerConnectionDelegate,
}

})
isReadToAddIceCandidate = true
self.webRTCClient.peerConnection.delegate = self
//isReadToAddIceCandidate = true
isRinging = true
}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -579,6 +586,7 @@ extension SignalingClient: WebSocketProviderDelegate {
}

}

}
} else {
// Fallback on earlier versions
Expand Down
59 changes: 56 additions & 3 deletions Neuro App/WebRTCClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -37,13 +37,35 @@ 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() {
fatalError("WebRTCClient:init is unavailable")
}

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)]

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -239,6 +291,7 @@ final class WebRTCClient: NSObject, ObservableObject {
dataChannel.delegate = self
self.localDataChannel = dataChannel
}

}

private func createAudioTrack() -> RTCAudioTrack {
Expand Down
Loading