session-ios/SessionMessagingKit/Messages/Control Messages/CallMessage.swift

141 lines
5.1 KiB
Swift
Raw Normal View History

import WebRTC
/// See https://developer.mozilla.org/en-US/docs/Web/API/RTCSessionDescription for more information.
@objc(SNCallMessage)
public final class CallMessage : ControlMessage {
public var kind: Kind?
/// See https://developer.mozilla.org/en-US/docs/Glossary/SDP for more information.
2021-08-17 08:02:20 +02:00
public var sdps: [String]?
2021-08-19 05:34:33 +02:00
public override var ttl: UInt64 { 2 * 60 * 1000 }
2021-08-18 01:00:55 +02:00
// NOTE: Multiple ICE candidates may be batched together for performance
// MARK: Kind
public enum Kind : Codable, CustomStringConvertible {
case offer
case answer
case provisionalAnswer
2021-08-17 08:02:20 +02:00
case iceCandidates(sdpMLineIndexes: [UInt32], sdpMids: [String])
2021-08-18 02:33:33 +02:00
case endCall
public var description: String {
switch self {
case .offer: return "offer"
case .answer: return "answer"
case .provisionalAnswer: return "provisionalAnswer"
2021-08-17 08:02:20 +02:00
case .iceCandidates(_, _): return "iceCandidates"
2021-08-18 02:33:33 +02:00
case .endCall: return "endCall"
}
}
}
// MARK: Initialization
public override init() { super.init() }
2021-08-17 08:02:20 +02:00
internal init(kind: Kind, sdps: [String]) {
super.init()
self.kind = kind
2021-08-17 08:02:20 +02:00
self.sdps = sdps
}
// MARK: Validation
public override var isValid: Bool {
guard super.isValid else { return false }
2021-08-17 08:02:20 +02:00
return kind != nil
}
// MARK: Coding
public required init?(coder: NSCoder) {
super.init(coder: coder)
guard let rawKind = coder.decodeObject(forKey: "kind") as! String? else { return nil }
switch rawKind {
case "offer": kind = .offer
case "answer": kind = .answer
case "provisionalAnswer": kind = .provisionalAnswer
2021-08-17 08:02:20 +02:00
case "iceCandidates":
guard let sdpMLineIndexes = coder.decodeObject(forKey: "sdpMLineIndexes") as? [UInt32],
let sdpMids = coder.decodeObject(forKey: "sdpMids") as? [String] else { return nil }
kind = .iceCandidates(sdpMLineIndexes: sdpMLineIndexes, sdpMids: sdpMids)
2021-08-18 02:33:33 +02:00
case "endCall": kind = .endCall
default: preconditionFailure()
}
2021-08-17 08:02:20 +02:00
if let sdps = coder.decodeObject(forKey: "sdps") as! [String]? { self.sdps = sdps }
}
public override func encode(with coder: NSCoder) {
super.encode(with: coder)
switch kind {
case .offer: coder.encode("offer", forKey: "kind")
case .answer: coder.encode("answer", forKey: "kind")
case .provisionalAnswer: coder.encode("provisionalAnswer", forKey: "kind")
2021-08-17 08:02:20 +02:00
case let .iceCandidates(sdpMLineIndexes, sdpMids):
coder.encode("iceCandidates", forKey: "kind")
coder.encode(sdpMLineIndexes, forKey: "sdpMLineIndexes")
coder.encode(sdpMids, forKey: "sdpMids")
2021-08-18 02:33:33 +02:00
case .endCall: coder.encode("endCall", forKey: "kind")
default: preconditionFailure()
}
2021-08-17 08:02:20 +02:00
coder.encode(sdps, forKey: "sdps")
}
// MARK: Proto Conversion
public override class func fromProto(_ proto: SNProtoContent) -> CallMessage? {
guard let callMessageProto = proto.callMessage else { return nil }
let kind: Kind
switch callMessageProto.type {
case .offer: kind = .offer
case .answer: kind = .answer
case .provisionalAnswer: kind = .provisionalAnswer
2021-08-17 08:02:20 +02:00
case .iceCandidates:
let sdpMLineIndexes = callMessageProto.sdpMlineIndexes
let sdpMids = callMessageProto.sdpMids
kind = .iceCandidates(sdpMLineIndexes: sdpMLineIndexes, sdpMids: sdpMids)
2021-08-18 02:33:33 +02:00
case .endCall: kind = .endCall
}
2021-08-17 08:02:20 +02:00
let sdps = callMessageProto.sdps
return CallMessage(kind: kind, sdps: sdps)
}
public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? {
2021-08-18 02:45:55 +02:00
guard let kind = kind else {
SNLog("Couldn't construct call message proto from: \(self).")
return nil
}
let type: SNProtoCallMessage.SNProtoCallMessageType
switch kind {
case .offer: type = .offer
case .answer: type = .answer
case .provisionalAnswer: type = .provisionalAnswer
2021-08-17 08:02:20 +02:00
case .iceCandidates(_, _): type = .iceCandidates
2021-08-18 02:33:33 +02:00
case .endCall: type = .endCall
}
2021-08-17 08:02:20 +02:00
let callMessageProto = SNProtoCallMessage.builder(type: type)
2021-08-18 02:45:55 +02:00
if let sdps = sdps, !sdps.isEmpty {
callMessageProto.setSdps(sdps)
}
2021-08-17 08:02:20 +02:00
if case let .iceCandidates(sdpMLineIndexes, sdpMids) = kind {
callMessageProto.setSdpMlineIndexes(sdpMLineIndexes)
callMessageProto.setSdpMids(sdpMids)
}
let contentProto = SNProtoContent.builder()
do {
contentProto.setCallMessage(try callMessageProto.build())
return try contentProto.build()
} catch {
SNLog("Couldn't construct call message proto from: \(self).")
return nil
}
}
// MARK: Description
public override var description: String {
"""
CallMessage(
kind: \(kind?.description ?? "null"),
2021-08-17 08:02:20 +02:00
sdps: \(sdps?.description ?? "null")
)
"""
}
}