VoIPIceCandidatesMessage.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 VoIPCallIceCandidatesMessage: NSObject {
  22. @objc let removed: Bool
  23. @objc let candidates: [RTCIceCandidate]
  24. @objc var contact: Contact?
  25. @objc let callId: VoIPCallId
  26. var completion: (() -> Void)?
  27. @objc init(removed: Bool, candidates: [RTCIceCandidate], contact: Contact?, callId: VoIPCallId, completion: (() -> Void)?) {
  28. self.removed = removed
  29. self.candidates = candidates
  30. self.contact = contact
  31. self.callId = callId
  32. self.completion = completion
  33. super.init()
  34. }
  35. }
  36. extension VoIPCallIceCandidatesMessage {
  37. enum VoIPCallIceCandidatesMessageError: Error {
  38. case generateJson(error: Error)
  39. }
  40. enum Keys: String {
  41. case removed = "removed"
  42. case candidates = "candidates"
  43. case candidate = "candidate"
  44. case sdpMid = "sdpMid"
  45. case sdpMLineIndex = "sdpMLineIndex"
  46. case ufrag = "ufrag"
  47. case callId = "callId"
  48. }
  49. @objc class func iceCandidates(dictionary: [AnyHashable: Any]) -> VoIPCallIceCandidatesMessage {
  50. let removed = dictionary[Keys.removed.rawValue] as! Bool
  51. let tmpCandidates = dictionary[Keys.candidates.rawValue] as? [[AnyHashable: Any]]
  52. var candidates = [RTCIceCandidate]()
  53. if tmpCandidates != nil {
  54. for (_, dict) in tmpCandidates!.enumerated() {
  55. let sdp = dict[Keys.candidate.rawValue] as! String
  56. let sdpMLineIndex = dict[Keys.sdpMLineIndex.rawValue] as! Int32
  57. let sdpMid = dict[Keys.sdpMid.rawValue] as! String
  58. let candidate = RTCIceCandidate.init(sdp: sdp, sdpMLineIndex: sdpMLineIndex, sdpMid: sdpMid)
  59. candidates.append(candidate)
  60. }
  61. }
  62. let tmpCallId = VoIPCallId(callId: dictionary[Keys.callId.rawValue] as? UInt32)
  63. let message = VoIPCallIceCandidatesMessage.init(removed: removed, candidates: candidates, contact: nil, callId: tmpCallId, completion: nil)
  64. return message
  65. }
  66. @objc func jsonData() throws -> Data {
  67. var candidates = [[AnyHashable: Any]]()
  68. for (_, candidate) in self.candidates.enumerated() {
  69. var dict = [AnyHashable: Any]()
  70. dict[Keys.candidate.rawValue] = candidate.sdp
  71. dict[Keys.sdpMid.rawValue] = candidate.sdpMid
  72. dict[Keys.sdpMLineIndex.rawValue] = candidate.sdpMLineIndex
  73. candidates.append(dict)
  74. }
  75. let json = [Keys.callId.rawValue: callId.callId, Keys.removed.rawValue: self.removed, Keys.candidates.rawValue: candidates] as [AnyHashable : Any]
  76. do {
  77. return try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
  78. }
  79. catch let error {
  80. throw VoIPCallIceCandidatesMessageError.generateJson(error: error)
  81. }
  82. }
  83. }