blob: c9730cf8c8e736625ea7dee19b98d344ea16d367 [file] [log] [blame]
hjon6d49a8e2016-01-26 13:06:42 -08001/*
2 * Copyright 2015 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
tkchin9eeb6242016-04-27 01:54:20 -070011#import "RTCConfiguration+Private.h"
hjon6d49a8e2016-01-26 13:06:42 -080012
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
14
tkchin9eeb6242016-04-27 01:54:20 -070015#import "RTCIceServer+Private.h"
16#import "WebRTC/RTCLogging.h"
tkchinab8f82f2016-01-27 17:50:11 -080017
Henrik Boströme06c2dd2016-05-13 13:50:38 +020018#include "webrtc/base/rtccertificategenerator.h"
tkchin9eeb6242016-04-27 01:54:20 -070019#include "webrtc/base/sslidentity.h"
hjon6d49a8e2016-01-26 13:06:42 -080020
21@implementation RTCConfiguration
22
23@synthesize iceServers = _iceServers;
24@synthesize iceTransportPolicy = _iceTransportPolicy;
25@synthesize bundlePolicy = _bundlePolicy;
26@synthesize rtcpMuxPolicy = _rtcpMuxPolicy;
27@synthesize tcpCandidatePolicy = _tcpCandidatePolicy;
Honghai Zhang46007ae2016-06-03 16:31:32 -070028@synthesize candidateNetworkPolicy = _candidateNetworkPolicy;
Honghai Zhang3108fc92016-05-11 10:10:39 -070029@synthesize continualGatheringPolicy = _continualGatheringPolicy;
hjon6d49a8e2016-01-26 13:06:42 -080030@synthesize audioJitterBufferMaxPackets = _audioJitterBufferMaxPackets;
31@synthesize iceConnectionReceivingTimeout = _iceConnectionReceivingTimeout;
32@synthesize iceBackupCandidatePairPingInterval =
33 _iceBackupCandidatePairPingInterval;
tkchinab8f82f2016-01-27 17:50:11 -080034@synthesize keyType = _keyType;
deadbeefbe0c96f2016-05-18 16:20:14 -070035@synthesize iceCandidatePoolSize = _iceCandidatePoolSize;
hjon6d49a8e2016-01-26 13:06:42 -080036
37- (instancetype)init {
38 if (self = [super init]) {
39 _iceServers = [NSMutableArray array];
40 // Copy defaults.
41 webrtc::PeerConnectionInterface::RTCConfiguration config;
42 _iceTransportPolicy =
43 [[self class] transportPolicyForTransportsType:config.type];
44 _bundlePolicy =
45 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
46 _rtcpMuxPolicy =
47 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
48 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
49 config.tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070050 _candidateNetworkPolicy = [[self class]
51 candidateNetworkPolicyForNativePolicy:config.candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070052 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
53 config.continual_gathering_policy;
54 _continualGatheringPolicy =
55 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080056 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
57 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
58 _iceBackupCandidatePairPingInterval =
59 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080060 _keyType = RTCEncryptionKeyTypeECDSA;
deadbeefbe0c96f2016-05-18 16:20:14 -070061 _iceCandidatePoolSize = config.ice_candidate_pool_size;
hjon6d49a8e2016-01-26 13:06:42 -080062 }
63 return self;
64}
65
66- (NSString *)description {
67 return [NSString stringWithFormat:
Honghai Zhang46007ae2016-06-03 16:31:32 -070068 @"RTCConfiguration: {\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%d\n%d\n%d\n%d\n}\n",
hjon6d49a8e2016-01-26 13:06:42 -080069 _iceServers,
70 [[self class] stringForTransportPolicy:_iceTransportPolicy],
71 [[self class] stringForBundlePolicy:_bundlePolicy],
72 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
73 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070074 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070075 [[self class]
76 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080077 _audioJitterBufferMaxPackets,
78 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -070079 _iceBackupCandidatePairPingInterval,
80 _iceCandidatePoolSize];
hjon6d49a8e2016-01-26 13:06:42 -080081}
82
83#pragma mark - Private
84
hbosa73ca562016-05-17 03:28:58 -070085- (webrtc::PeerConnectionInterface::RTCConfiguration *)
86 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020087 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
88 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration());
hjon6d49a8e2016-01-26 13:06:42 -080089
90 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020091 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -080092 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +020093 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -080094 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020095 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -080096 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020097 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -080098 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020099 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800100 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700101 nativeConfig->candidate_network_policy = [[self class]
102 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200103 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700104 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200105 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
106 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800107 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200108 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800109 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200110 rtc::KeyType keyType =
111 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
112 // Generate non-default certificate.
113 if (keyType != rtc::KT_DEFAULT) {
114 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
115 rtc::RTCCertificateGenerator::GenerateCertificate(
116 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
117 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700118 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200119 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800120 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200121 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800122 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700123 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
hjon6d49a8e2016-01-26 13:06:42 -0800124
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200125 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800126}
127
hjon6d49a8e2016-01-26 13:06:42 -0800128+ (webrtc::PeerConnectionInterface::IceTransportsType)
129 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
130 switch (policy) {
131 case RTCIceTransportPolicyNone:
132 return webrtc::PeerConnectionInterface::kNone;
133 case RTCIceTransportPolicyRelay:
134 return webrtc::PeerConnectionInterface::kRelay;
135 case RTCIceTransportPolicyNoHost:
136 return webrtc::PeerConnectionInterface::kNoHost;
137 case RTCIceTransportPolicyAll:
138 return webrtc::PeerConnectionInterface::kAll;
139 }
140}
141
142+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
143 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
144 switch (nativeType) {
145 case webrtc::PeerConnectionInterface::kNone:
146 return RTCIceTransportPolicyNone;
147 case webrtc::PeerConnectionInterface::kRelay:
148 return RTCIceTransportPolicyRelay;
149 case webrtc::PeerConnectionInterface::kNoHost:
150 return RTCIceTransportPolicyNoHost;
151 case webrtc::PeerConnectionInterface::kAll:
152 return RTCIceTransportPolicyAll;
153 }
154}
155
156+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
157 switch (policy) {
158 case RTCIceTransportPolicyNone:
159 return @"NONE";
160 case RTCIceTransportPolicyRelay:
161 return @"RELAY";
162 case RTCIceTransportPolicyNoHost:
163 return @"NO_HOST";
164 case RTCIceTransportPolicyAll:
165 return @"ALL";
166 }
167}
168
169+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
170 (RTCBundlePolicy)policy {
171 switch (policy) {
172 case RTCBundlePolicyBalanced:
173 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
174 case RTCBundlePolicyMaxCompat:
175 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
176 case RTCBundlePolicyMaxBundle:
177 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
178 }
179}
180
181+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
182 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
183 switch (nativePolicy) {
184 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
185 return RTCBundlePolicyBalanced;
186 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
187 return RTCBundlePolicyMaxCompat;
188 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
189 return RTCBundlePolicyMaxBundle;
190 }
191}
192
193+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
194 switch (policy) {
195 case RTCBundlePolicyBalanced:
196 return @"BALANCED";
197 case RTCBundlePolicyMaxCompat:
198 return @"MAX_COMPAT";
199 case RTCBundlePolicyMaxBundle:
200 return @"MAX_BUNDLE";
201 }
202}
203
204+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
205 (RTCRtcpMuxPolicy)policy {
206 switch (policy) {
207 case RTCRtcpMuxPolicyNegotiate:
208 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
209 case RTCRtcpMuxPolicyRequire:
210 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
211 }
212}
213
214+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
215 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
216 switch (nativePolicy) {
217 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
218 return RTCRtcpMuxPolicyNegotiate;
219 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
220 return RTCRtcpMuxPolicyRequire;
221 }
222}
223
224+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
225 switch (policy) {
226 case RTCRtcpMuxPolicyNegotiate:
227 return @"NEGOTIATE";
228 case RTCRtcpMuxPolicyRequire:
229 return @"REQUIRE";
230 }
231}
232
233+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
234 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
235 switch (policy) {
236 case RTCTcpCandidatePolicyEnabled:
237 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
238 case RTCTcpCandidatePolicyDisabled:
239 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
240 }
241}
242
Honghai Zhang46007ae2016-06-03 16:31:32 -0700243+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
244 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
245 switch (policy) {
246 case RTCCandidateNetworkPolicyAll:
247 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
248 case RTCCandidateNetworkPolicyLowCost:
249 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
250 }
251}
252
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200253+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
254 (RTCEncryptionKeyType)keyType {
255 switch (keyType) {
256 case RTCEncryptionKeyTypeRSA:
257 return rtc::KT_RSA;
258 case RTCEncryptionKeyTypeECDSA:
259 return rtc::KT_ECDSA;
260 }
261}
262
hjon6d49a8e2016-01-26 13:06:42 -0800263+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
264 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
265 switch (nativePolicy) {
266 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
267 return RTCTcpCandidatePolicyEnabled;
268 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
269 return RTCTcpCandidatePolicyDisabled;
270 }
271}
272
273+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
274 switch (policy) {
275 case RTCTcpCandidatePolicyEnabled:
276 return @"TCP_ENABLED";
277 case RTCTcpCandidatePolicyDisabled:
278 return @"TCP_DISABLED";
279 }
280}
281
Honghai Zhang46007ae2016-06-03 16:31:32 -0700282+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
283 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
284 switch (nativePolicy) {
285 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
286 return RTCCandidateNetworkPolicyAll;
287 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
288 return RTCCandidateNetworkPolicyLowCost;
289 }
290}
291
292+ (NSString *)stringForCandidateNetworkPolicy:
293 (RTCCandidateNetworkPolicy)policy {
294 switch (policy) {
295 case RTCCandidateNetworkPolicyAll:
296 return @"CANDIDATE_ALL_NETWORKS";
297 case RTCCandidateNetworkPolicyLowCost:
298 return @"CANDIDATE_LOW_COST_NETWORKS";
299 }
300}
301
Honghai Zhang3108fc92016-05-11 10:10:39 -0700302+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
303 nativeContinualGatheringPolicyForPolicy:
304 (RTCContinualGatheringPolicy)policy {
305 switch (policy) {
306 case RTCContinualGatheringPolicyGatherOnce:
307 return webrtc::PeerConnectionInterface::GATHER_ONCE;
308 case RTCContinualGatheringPolicyGatherContinually:
309 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
310 }
311}
312
313+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
314 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
315 switch (nativePolicy) {
316 case webrtc::PeerConnectionInterface::GATHER_ONCE:
317 return RTCContinualGatheringPolicyGatherOnce;
318 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
319 return RTCContinualGatheringPolicyGatherContinually;
320 }
321}
322
323+ (NSString *)stringForContinualGatheringPolicy:
324 (RTCContinualGatheringPolicy)policy {
325 switch (policy) {
326 case RTCContinualGatheringPolicyGatherOnce:
327 return @"GATHER_ONCE";
328 case RTCContinualGatheringPolicyGatherContinually:
329 return @"GATHER_CONTINUALLY";
330 }
331}
332
hjon6d49a8e2016-01-26 13:06:42 -0800333@end