blob: 773c346c6b6f13ed49544084e965886b67013b37 [file] [log] [blame]
Donald E Curtisa8736442015-08-05 15:48:13 -07001/*
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
hjon79858f82016-03-13 22:08:26 -070011#import "RTCIceCandidate+JSON.h"
Donald E Curtisa8736442015-08-05 15:48:13 -070012
hjon79858f82016-03-13 22:08:26 -070013#import "webrtc/base/objc/RTCLogging.h"
Donald E Curtisa8736442015-08-05 15:48:13 -070014
15static NSString const *kRTCICECandidateTypeKey = @"type";
16static NSString const *kRTCICECandidateTypeValue = @"candidate";
17static NSString const *kRTCICECandidateMidKey = @"id";
18static NSString const *kRTCICECandidateMLineIndexKey = @"label";
19static NSString const *kRTCICECandidateSdpKey = @"candidate";
20
hjon79858f82016-03-13 22:08:26 -070021@implementation RTCIceCandidate (JSON)
Donald E Curtisa8736442015-08-05 15:48:13 -070022
hjon79858f82016-03-13 22:08:26 -070023+ (RTCIceCandidate *)candidateFromJSONDictionary:(NSDictionary *)dictionary {
Donald E Curtisa8736442015-08-05 15:48:13 -070024 NSString *mid = dictionary[kRTCICECandidateMidKey];
25 NSString *sdp = dictionary[kRTCICECandidateSdpKey];
26 NSNumber *num = dictionary[kRTCICECandidateMLineIndexKey];
27 NSInteger mLineIndex = [num integerValue];
hjon79858f82016-03-13 22:08:26 -070028 return [[RTCIceCandidate alloc] initWithSdp:sdp
29 sdpMLineIndex:mLineIndex
30 sdpMid:mid];
Donald E Curtisa8736442015-08-05 15:48:13 -070031}
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