Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 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 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame^] | 11 | #import "RTCIceCandidate+JSON.h" |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 12 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame^] | 13 | #import "webrtc/base/objc/RTCLogging.h" |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 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 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame^] | 21 | @implementation RTCIceCandidate (JSON) |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 22 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame^] | 23 | + (RTCIceCandidate *)candidateFromJSONDictionary:(NSDictionary *)dictionary { |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 24 | NSString *mid = dictionary[kRTCICECandidateMidKey]; |
| 25 | NSString *sdp = dictionary[kRTCICECandidateSdpKey]; |
| 26 | NSNumber *num = dictionary[kRTCICECandidateMLineIndexKey]; |
| 27 | NSInteger mLineIndex = [num integerValue]; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame^] | 28 | return [[RTCIceCandidate alloc] initWithSdp:sdp |
| 29 | sdpMLineIndex:mLineIndex |
| 30 | sdpMid:mid]; |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | - (NSData *)JSONData { |
| 34 | NSDictionary *json = @{ |
| 35 | kRTCICECandidateTypeKey : kRTCICECandidateTypeValue, |
| 36 | kRTCICECandidateMLineIndexKey : @(self.sdpMLineIndex), |
| 37 | kRTCICECandidateMidKey : self.sdpMid, |
| 38 | kRTCICECandidateSdpKey : self.sdp |
| 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 | |
| 52 | @end |