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