remove redundant turn servers and reduce turn servers connections to 2

This commit is contained in:
Ryan Zhao 2022-02-11 11:32:21 +11:00
parent f7af8141c0
commit 83c7283a76
2 changed files with 9 additions and 5 deletions

View File

@ -8,7 +8,7 @@ struct TurnServerInfo {
let username: String
let urls: [String]
init?(attributes: JSON) {
init?(attributes: JSON, random: Int? = nil) {
if let passwordAttribute = (attributes["password"] as? String) {
password = passwordAttribute
} else {
@ -22,7 +22,12 @@ struct TurnServerInfo {
}
if let urlsAttribute = attributes["urls"] as? [String] {
urls = urlsAttribute
if let random = random {
urls = Array(urlsAttribute.shuffled()[0..<random])
} else {
urls = urlsAttribute
}
} else {
return nil
}

View File

@ -22,7 +22,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
let url = Bundle.main.url(forResource: "Session-Turn-Server", withExtension: nil)!
let data = try! Data(contentsOf: url)
let json = try! JSONSerialization.jsonObject(with: data, options: [ .fragmentsAllowed ]) as! JSON
return TurnServerInfo(attributes: json)
return TurnServerInfo(attributes: json, random: 2)
}()
internal lazy var factory: RTCPeerConnectionFactory = {
@ -36,9 +36,8 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
/// remote peer, maintain and monitor the connection, and close the connection once it's no longer needed.
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") ]
if let defaultICEServer = defaultICEServer {
configuration.iceServers.append(RTCIceServer(urlStrings: defaultICEServer.urls, username: defaultICEServer.username, credential: defaultICEServer.password))
configuration.iceServers = [ RTCIceServer(urlStrings: defaultICEServer.urls, username: defaultICEServer.username, credential: defaultICEServer.password) ]
}
configuration.sdpSemantics = .unifiedPlan
let constraints = RTCMediaConstraints(mandatoryConstraints: [:], optionalConstraints: [:])