blob: 99cefbff0be97991ba6f8aa769dc5df02fd170f7 [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
Mirko Bonadei19640aa2020-10-19 16:12:43 +020013#import "sdk/objc/base/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";
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070020static NSString const *kRTCICECandidatesTypeKey = @"candidates";
21
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020022@implementation RTC_OBJC_TYPE (RTCIceCandidate)
23(JSON)
Donald E Curtisa8736442015-08-05 15:48:13 -070024
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020025 + (RTC_OBJC_TYPE(RTCIceCandidate) *)candidateFromJSONDictionary : (NSDictionary *)dictionary {
Donald E Curtisa8736442015-08-05 15:48:13 -070026 NSString *mid = dictionary[kRTCICECandidateMidKey];
27 NSString *sdp = dictionary[kRTCICECandidateSdpKey];
28 NSNumber *num = dictionary[kRTCICECandidateMLineIndexKey];
29 NSInteger mLineIndex = [num integerValue];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020030 return [[RTC_OBJC_TYPE(RTCIceCandidate) alloc] initWithSdp:sdp
31 sdpMLineIndex:mLineIndex
32 sdpMid:mid];
Donald E Curtisa8736442015-08-05 15:48:13 -070033}
34
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020035+ (NSData *)JSONDataForIceCandidates:(NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *)candidates
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070036 withType:(NSString *)typeValue {
37 NSMutableArray *jsonCandidates =
38 [NSMutableArray arrayWithCapacity:candidates.count];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020039 for (RTC_OBJC_TYPE(RTCIceCandidate) * candidate in candidates) {
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070040 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
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020059+ (NSArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *)candidatesFromJSONDictionary:
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070060 (NSDictionary *)dictionary {
61 NSArray *jsonCandidates = dictionary[kRTCICECandidatesTypeKey];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020062 NSMutableArray<RTC_OBJC_TYPE(RTCIceCandidate) *> *candidates =
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070063 [NSMutableArray arrayWithCapacity:jsonCandidates.count];
64 for (NSDictionary *jsonCandidate in jsonCandidates) {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020065 RTC_OBJC_TYPE(RTCIceCandidate) *candidate =
66 [RTC_OBJC_TYPE(RTCIceCandidate) candidateFromJSONDictionary:jsonCandidate];
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070067 [candidates addObject:candidate];
68 }
69 return candidates;
70}
71
Donald E Curtisa8736442015-08-05 15:48:13 -070072- (NSData *)JSONData {
73 NSDictionary *json = @{
74 kRTCICECandidateTypeKey : kRTCICECandidateTypeValue,
75 kRTCICECandidateMLineIndexKey : @(self.sdpMLineIndex),
76 kRTCICECandidateMidKey : self.sdpMid,
77 kRTCICECandidateSdpKey : self.sdp
78 };
79 NSError *error = nil;
80 NSData *data =
81 [NSJSONSerialization dataWithJSONObject:json
82 options:NSJSONWritingPrettyPrinted
83 error:&error];
84 if (error) {
85 RTCLogError(@"Error serializing JSON: %@", error);
86 return nil;
87 }
88 return data;
89}
90
Honghai Zhangda2ba4d2016-05-23 11:53:14 -070091- (NSDictionary *)JSONDictionary{
92 NSDictionary *json = @{
93 kRTCICECandidateMLineIndexKey : @(self.sdpMLineIndex),
94 kRTCICECandidateMidKey : self.sdpMid,
95 kRTCICECandidateSdpKey : self.sdp
96 };
97 return json;
98}
99
Donald E Curtisa8736442015-08-05 15:48:13 -0700100@end