RTCIceCandidate+JSON.m (3687B)
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 11 #import "RTCIceCandidate+JSON.h" 12 13 #import "sdk/objc/base/RTCLogging.h" 14 15 static NSString const *kRTCICECandidateTypeKey = @"type"; 16 static NSString const *kRTCICECandidateTypeValue = @"candidate"; 17 static NSString const *kRTCICECandidateMidKey = @"id"; 18 static NSString const *kRTCICECandidateMLineIndexKey = @"label"; 19 static NSString const *kRTCICECandidateSdpKey = @"candidate"; 20 static NSString const *kRTCICECandidatesTypeKey = @"candidates"; 21 22 @implementation RTC_OBJC_TYPE (RTCIceCandidate) 23 (JSON) 24 25 + (RTC_OBJC_TYPE(RTCIceCandidate) *)candidateFromJSONDictionary 26 : (NSDictionary *)dictionary { 27 NSString *mid = dictionary[kRTCICECandidateMidKey]; 28 NSString *sdp = dictionary[kRTCICECandidateSdpKey]; 29 NSNumber *num = dictionary[kRTCICECandidateMLineIndexKey]; 30 NSInteger mLineIndex = [num integerValue]; 31 return [[RTC_OBJC_TYPE(RTCIceCandidate) alloc] initWithSdp:sdp 32 sdpMLineIndex:mLineIndex 33 sdpMid:mid]; 34 } 35 36 + (NSData *)JSONDataForIceCandidates: 37 (NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *)candidates 38 withType:(NSString *)typeValue { 39 NSMutableArray *jsonCandidates = 40 [NSMutableArray arrayWithCapacity:candidates.count]; 41 for (RTC_OBJC_TYPE(RTCIceCandidate) * candidate in candidates) { 42 NSDictionary *jsonCandidate = [candidate JSONDictionary]; 43 [jsonCandidates addObject:jsonCandidate]; 44 } 45 NSDictionary *json = @{ 46 kRTCICECandidateTypeKey : typeValue, 47 kRTCICECandidatesTypeKey : jsonCandidates 48 }; 49 NSError *error = nil; 50 NSData *data = 51 [NSJSONSerialization dataWithJSONObject:json 52 options:NSJSONWritingPrettyPrinted 53 error:&error]; 54 if (error) { 55 RTCLogError(@"Error serializing JSON: %@", error); 56 return nil; 57 } 58 return data; 59 } 60 61 + (NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *)candidatesFromJSONDictionary: 62 (NSDictionary *)dictionary { 63 NSArray *jsonCandidates = dictionary[kRTCICECandidatesTypeKey]; 64 NSMutableArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *candidates = 65 [NSMutableArray arrayWithCapacity:jsonCandidates.count]; 66 for (NSDictionary *jsonCandidate in jsonCandidates) { 67 RTC_OBJC_TYPE(RTCIceCandidate) *candidate = [RTC_OBJC_TYPE(RTCIceCandidate) 68 candidateFromJSONDictionary:jsonCandidate]; 69 [candidates addObject:candidate]; 70 } 71 return candidates; 72 } 73 74 - (NSData *)JSONData { 75 NSDictionary *json = @{ 76 kRTCICECandidateTypeKey : kRTCICECandidateTypeValue, 77 kRTCICECandidateMLineIndexKey : @(self.sdpMLineIndex), 78 kRTCICECandidateMidKey : self.sdpMid, 79 kRTCICECandidateSdpKey : self.sdp 80 }; 81 NSError *error = nil; 82 NSData *data = 83 [NSJSONSerialization dataWithJSONObject:json 84 options:NSJSONWritingPrettyPrinted 85 error:&error]; 86 if (error) { 87 RTCLogError(@"Error serializing JSON: %@", error); 88 return nil; 89 } 90 return data; 91 } 92 93 - (NSDictionary *)JSONDictionary { 94 NSDictionary *json = @{ 95 kRTCICECandidateMLineIndexKey : @(self.sdpMLineIndex), 96 kRTCICECandidateMidKey : self.sdpMid, 97 kRTCICECandidateSdpKey : self.sdp 98 }; 99 return json; 100 } 101 102 @end