mirror of https://github.com/oxen-io/session-ios
Further implement CallVC
parent
eb2cba7410
commit
f1f48ec865
@ -0,0 +1,28 @@
|
|||||||
|
import WebRTC
|
||||||
|
|
||||||
|
extension CallManager {
|
||||||
|
|
||||||
|
public func handleCandidateMessage(_ candidate: RTCIceCandidate) {
|
||||||
|
candidateQueue.append(candidate)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func handleRemoteDescription(_ sdp: RTCSessionDescription) {
|
||||||
|
peerConnection.setRemoteDescription(sdp, completionHandler: { [weak self] error in
|
||||||
|
if let error = error {
|
||||||
|
SNLog("Couldn't set SDP due to error: \(error).")
|
||||||
|
} else {
|
||||||
|
guard let self = self else { return }
|
||||||
|
if sdp.type == .offer, self.peerConnection.localDescription == nil {
|
||||||
|
self.acceptCall()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public func drainMessageQueue() {
|
||||||
|
for candidate in candidateQueue {
|
||||||
|
peerConnection.add(candidate)
|
||||||
|
}
|
||||||
|
candidateQueue.removeAll()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
import Foundation
|
||||||
|
import WebRTC
|
||||||
|
|
||||||
|
public enum SignalingMessage {
|
||||||
|
case none
|
||||||
|
case candidate(_ message: RTCIceCandidate)
|
||||||
|
case answer(_ message: RTCSessionDescription)
|
||||||
|
case offer(_ message: RTCSessionDescription)
|
||||||
|
case bye
|
||||||
|
|
||||||
|
public static func from(message: String) -> SignalingMessage {
|
||||||
|
guard let data = message.data(using: String.Encoding.utf8) else { return .none }
|
||||||
|
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? JSON else { return .none }
|
||||||
|
let messageAsJSON: JSON
|
||||||
|
if let foo = json["msg"] as? String {
|
||||||
|
guard let data = foo.data(using: String.Encoding.utf8) else { return .none }
|
||||||
|
guard let bar = try? JSONSerialization.jsonObject(with: data, options: []) as? JSON else { return .none }
|
||||||
|
messageAsJSON = bar
|
||||||
|
} else {
|
||||||
|
messageAsJSON = json
|
||||||
|
}
|
||||||
|
guard let type = messageAsJSON["type"] as? String else { return .none }
|
||||||
|
switch type {
|
||||||
|
case "candidate":
|
||||||
|
guard let candidate = RTCIceCandidate.candidate(from: messageAsJSON) else { return .none }
|
||||||
|
return .candidate(candidate)
|
||||||
|
case "answer":
|
||||||
|
guard let sdp = messageAsJSON["sdp"] as? String else { return .none }
|
||||||
|
return .answer(RTCSessionDescription(type: .answer, sdp: sdp))
|
||||||
|
case "offer":
|
||||||
|
guard let sdp = messageAsJSON["sdp"] as? String else { return .none }
|
||||||
|
return .offer(RTCSessionDescription(type: .offer, sdp: sdp))
|
||||||
|
case "bye":
|
||||||
|
return .bye
|
||||||
|
default: return .none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension RTCIceCandidate {
|
||||||
|
|
||||||
|
public func serialize() -> Data? {
|
||||||
|
let json = [
|
||||||
|
"type": "candidate",
|
||||||
|
"label": "\(sdpMLineIndex)",
|
||||||
|
"id": sdpMid,
|
||||||
|
"candidate": sdp
|
||||||
|
]
|
||||||
|
return try? JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted])
|
||||||
|
}
|
||||||
|
|
||||||
|
static func candidate(from json: JSON) -> RTCIceCandidate? {
|
||||||
|
let sdp = json["candidate"] as? String
|
||||||
|
let sdpMid = json["id"] as? String
|
||||||
|
let labelStr = json["label"] as? String
|
||||||
|
let label = (json["label"] as? Int32) ?? 0
|
||||||
|
return RTCIceCandidate(sdp: sdp ?? "", sdpMLineIndex: Int32(labelStr ?? "") ?? label, sdpMid: sdpMid)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue