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