VoIPCallAnswerMessage.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. @objc class VoIPCallAnswerMessage: NSObject {
  22. @objc enum MessageAction: Int {
  23. case reject
  24. case call
  25. }
  26. @objc enum MessageRejectReason: Int {
  27. case unknown
  28. case busy
  29. case timeout
  30. case reject
  31. case disabled
  32. case offHours
  33. }
  34. @objc let action: MessageAction
  35. @objc var contact: Contact?
  36. @objc var answer: RTCSessionDescription?
  37. var completion: (() -> Void)?
  38. let callId: VoIPCallId
  39. let rejectReason: MessageRejectReason?
  40. let features: [String: Any?]?
  41. let isVideoAvailable: Bool
  42. init(action: MessageAction, contact: Contact?, answer: RTCSessionDescription?, rejectReason: MessageRejectReason?, features: [String: Any?]?, isVideoAvailable: Bool, callId: VoIPCallId, completion: (() -> Void)?) {
  43. self.action = action
  44. self.contact = contact
  45. self.answer = answer
  46. self.rejectReason = rejectReason
  47. self.completion = completion
  48. self.features = features
  49. self.isVideoAvailable = isVideoAvailable
  50. self.callId = callId
  51. super.init()
  52. }
  53. }
  54. extension VoIPCallAnswerMessage {
  55. static let kActionKey = "action"
  56. static let kAnswerKey = "answer"
  57. static let kRejectReasonKey = "rejectReason"
  58. static let kRTCSessionDescriptionTypeKey = "sdpType"
  59. static let kRTCSessionDescriptionSdpKey = "sdp"
  60. static let kFeaturesKey = "features"
  61. static let kFeaturesVideoKey = "video"
  62. static let kCallIdKey = "callId"
  63. enum VoIPCallAnswerMessageError: Error {
  64. case generateJson(error: Error)
  65. }
  66. @objc class func answerFromJSONDictionary(_ dictionary: [AnyHashable: Any]) -> VoIPCallAnswerMessage {
  67. let tmpAction: MessageAction = VoIPCallAnswerMessage.MessageAction(rawValue: dictionary[VoIPCallAnswerMessage.kActionKey] as! Int)!
  68. var tmpRejectReason: VoIPCallAnswerMessage.MessageRejectReason? = nil
  69. if let rejectReasonValue = dictionary[VoIPCallAnswerMessage.kRejectReasonKey] {
  70. tmpRejectReason = VoIPCallAnswerMessage.MessageRejectReason.init(rawValue: rejectReasonValue as! Int)
  71. }
  72. var tmpAnswer: RTCSessionDescription? = nil
  73. if let answerKey = dictionary[VoIPCallAnswerMessage.kAnswerKey] {
  74. tmpAnswer = RTCSessionDescription.description(from: answerKey as! [AnyHashable : Any])
  75. }
  76. var tmpFeatures: [String: Any?]?
  77. var isVideoAvailable: Bool = false
  78. if let features = dictionary[kFeaturesKey] as? [String: Any?] {
  79. tmpFeatures = features
  80. if features.keys.contains(kFeaturesVideoKey) {
  81. isVideoAvailable = true
  82. }
  83. }
  84. let tmpCallId = VoIPCallId(callId: dictionary[kCallIdKey] as? UInt32)
  85. return VoIPCallAnswerMessage.init(action: tmpAction, contact: nil, answer: tmpAnswer, rejectReason: tmpRejectReason, features: tmpFeatures, isVideoAvailable: isVideoAvailable, callId: tmpCallId, completion: nil)
  86. }
  87. private func stringForType() -> String {
  88. if answer != nil {
  89. switch answer!.type {
  90. case .offer:
  91. return "offer"
  92. case .prAnswer:
  93. return "pranswer"
  94. case .answer:
  95. return "answer"
  96. default:
  97. return ""
  98. }
  99. }
  100. return ""
  101. }
  102. func jsonData() throws -> Data {
  103. var json = [AnyHashable: Any]()
  104. if answer != nil {
  105. let extensionConfig: VoIPCallSdpPatcher.RtpHeaderExtensionConfig = contact?.isVideoCallAvailable() ?? false ? .ENABLE_WITH_ONE_AND_TWO_BYTE_HEADER : .DISABLE
  106. json = [VoIPCallAnswerMessage.kCallIdKey: callId.callId, VoIPCallAnswerMessage.kActionKey: action.rawValue, VoIPCallAnswerMessage.kAnswerKey: [VoIPCallAnswerMessage.kRTCSessionDescriptionTypeKey: stringForType(), VoIPCallAnswerMessage.kRTCSessionDescriptionSdpKey: try VoIPCallSdpPatcher(extensionConfig).patch(type: .LOCAL_ANSWER_OR_REMOTE_SDP, sdp: answer!.sdp)], VoIPCallAnswerMessage.kRejectReasonKey: rejectReason ?? 0]
  107. if isVideoAvailable {
  108. json[VoIPCallAnswerMessage.kFeaturesKey] = [VoIPCallAnswerMessage.kFeaturesVideoKey: nil]
  109. }
  110. } else {
  111. json = [VoIPCallAnswerMessage.kCallIdKey: callId.callId, VoIPCallAnswerMessage.kActionKey: action.rawValue, VoIPCallAnswerMessage.kRejectReasonKey: rejectReason?.rawValue ?? 0]
  112. }
  113. do {
  114. return try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
  115. }
  116. catch let error {
  117. throw VoIPCallAnswerMessageError.generateJson(error: error)
  118. }
  119. }
  120. }