RTCIceCandidate+JSON.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2014 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #import "RTCIceCandidate+JSON.h"
  11. #import "WebRTC/RTCLogging.h"
  12. static NSString const *kRTCICECandidateTypeKey = @"type";
  13. static NSString const *kRTCICECandidateTypeValue = @"candidate";
  14. static NSString const *kRTCICECandidateMidKey = @"sdpMid";
  15. static NSString const *kRTCICECandidateMLineIndexKey = @"sdpMLineIndex";
  16. static NSString const *kRTCICECandidateSdpKey = @"candidate";
  17. static NSString const *kRTCICECandidatesTypeKey = @"candidates";
  18. @implementation RTCIceCandidate (JSON)
  19. + (RTCIceCandidate *)candidateFromJSONDictionary:(NSDictionary *)dictionary {
  20. NSString *mid = dictionary[kRTCICECandidateMidKey];
  21. NSString *sdp = dictionary[kRTCICECandidateSdpKey];
  22. NSNumber *num = dictionary[kRTCICECandidateMLineIndexKey];
  23. NSInteger mLineIndex = [num integerValue];
  24. return [[RTCIceCandidate alloc] initWithSdp:sdp
  25. sdpMLineIndex:mLineIndex
  26. sdpMid:mid];
  27. }
  28. + (NSData *)JSONDataForIceCandidates:(NSArray<RTCIceCandidate *> *)candidates
  29. withType:(NSString *)typeValue {
  30. NSMutableArray *jsonCandidates =
  31. [NSMutableArray arrayWithCapacity:candidates.count];
  32. for (RTCIceCandidate *candidate in candidates) {
  33. NSDictionary *jsonCandidate = [candidate JSONDictionary];
  34. [jsonCandidates addObject:jsonCandidate];
  35. }
  36. NSDictionary *json = @{
  37. kRTCICECandidateTypeKey : typeValue,
  38. kRTCICECandidatesTypeKey : jsonCandidates
  39. };
  40. NSError *error = nil;
  41. NSData *data =
  42. [NSJSONSerialization dataWithJSONObject:json
  43. options:NSJSONWritingPrettyPrinted
  44. error:&error];
  45. if (error) {
  46. RTCLogError(@"Error serializing JSON: %@", error);
  47. return nil;
  48. }
  49. return data;
  50. }
  51. + (NSArray<RTCIceCandidate *> *)candidatesFromJSONDictionary:
  52. (NSDictionary *)dictionary {
  53. NSArray *jsonCandidates = dictionary[kRTCICECandidatesTypeKey];
  54. NSMutableArray<RTCIceCandidate *> *candidates =
  55. [NSMutableArray arrayWithCapacity:jsonCandidates.count];
  56. for (NSDictionary *jsonCandidate in jsonCandidates) {
  57. RTCIceCandidate *candidate =
  58. [RTCIceCandidate candidateFromJSONDictionary:jsonCandidate];
  59. [candidates addObject:candidate];
  60. }
  61. return candidates;
  62. }
  63. - (NSData *)JSONData {
  64. NSDictionary *json = @{
  65. kRTCICECandidateTypeKey : kRTCICECandidateTypeValue,
  66. kRTCICECandidateMLineIndexKey : @(self.sdpMLineIndex),
  67. kRTCICECandidateMidKey : self.sdpMid,
  68. kRTCICECandidateSdpKey : self.sdp
  69. };
  70. NSError *error = nil;
  71. NSData *data =
  72. [NSJSONSerialization dataWithJSONObject:json
  73. options:NSJSONWritingPrettyPrinted
  74. error:&error];
  75. if (error) {
  76. RTCLogError(@"Error serializing JSON: %@", error);
  77. return nil;
  78. }
  79. return data;
  80. }
  81. - (NSDictionary *)JSONDictionary {
  82. NSDictionary *json = @{
  83. kRTCICECandidateMLineIndexKey : @(self.sdpMLineIndex),
  84. kRTCICECandidateMidKey : self.sdpMid,
  85. kRTCICECandidateSdpKey : self.sdp
  86. };
  87. return json;
  88. }
  89. @end