blob: 54d8eac86774f3a6a43dbaca0a24ffa40cf8a9cc [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;
hayscc9f95002016-12-05 14:24:32 -080031@synthesize audioJitterBufferFastAccelerate = _audioJitterBufferFastAccelerate;
hjon6d49a8e2016-01-26 13:06:42 -080032@synthesize iceConnectionReceivingTimeout = _iceConnectionReceivingTimeout;
33@synthesize iceBackupCandidatePairPingInterval =
34 _iceBackupCandidatePairPingInterval;
tkchinab8f82f2016-01-27 17:50:11 -080035@synthesize keyType = _keyType;
deadbeefbe0c96f2016-05-18 16:20:14 -070036@synthesize iceCandidatePoolSize = _iceCandidatePoolSize;
honghaizaf6b6e02016-07-11 15:09:26 -070037@synthesize shouldPruneTurnPorts = _shouldPruneTurnPorts;
38@synthesize shouldPresumeWritableWhenFullyRelayed =
39 _shouldPresumeWritableWhenFullyRelayed;
skvlada5d94ff2017-02-02 13:02:30 -080040@synthesize iceCheckMinInterval = _iceCheckMinInterval;
hjon6d49a8e2016-01-26 13:06:42 -080041
42- (instancetype)init {
jtteh4eeb5372017-04-03 15:06:37 -070043 // Copy defaults.
44 webrtc::PeerConnectionInterface::RTCConfiguration config(
45 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive);
46 return [self initWithNativeConfiguration:&config];
47}
48
49- (instancetype)initWithNativeConfiguration:
50 (const webrtc::PeerConnectionInterface::RTCConfiguration *)config {
51 NSParameterAssert(config);
hjon6d49a8e2016-01-26 13:06:42 -080052 if (self = [super init]) {
jtteh4eeb5372017-04-03 15:06:37 -070053 NSMutableArray *iceServers = [NSMutableArray array];
54 for (const webrtc::PeerConnectionInterface::IceServer& server : config->servers) {
55 RTCIceServer *iceServer = [[RTCIceServer alloc] initWithNativeServer:server];
56 [iceServers addObject:iceServer];
57 }
58 _iceServers = iceServers;
hjon6d49a8e2016-01-26 13:06:42 -080059 _iceTransportPolicy =
jtteh4eeb5372017-04-03 15:06:37 -070060 [[self class] transportPolicyForTransportsType:config->type];
hjon6d49a8e2016-01-26 13:06:42 -080061 _bundlePolicy =
jtteh4eeb5372017-04-03 15:06:37 -070062 [[self class] bundlePolicyForNativePolicy:config->bundle_policy];
hjon6d49a8e2016-01-26 13:06:42 -080063 _rtcpMuxPolicy =
jtteh4eeb5372017-04-03 15:06:37 -070064 [[self class] rtcpMuxPolicyForNativePolicy:config->rtcp_mux_policy];
hjon6d49a8e2016-01-26 13:06:42 -080065 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
jtteh4eeb5372017-04-03 15:06:37 -070066 config->tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070067 _candidateNetworkPolicy = [[self class]
jtteh4eeb5372017-04-03 15:06:37 -070068 candidateNetworkPolicyForNativePolicy:config->candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070069 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
jtteh4eeb5372017-04-03 15:06:37 -070070 config->continual_gathering_policy;
Honghai Zhang3108fc92016-05-11 10:10:39 -070071 _continualGatheringPolicy =
72 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
jtteh4eeb5372017-04-03 15:06:37 -070073 _audioJitterBufferMaxPackets = config->audio_jitter_buffer_max_packets;
74 _audioJitterBufferFastAccelerate = config->audio_jitter_buffer_fast_accelerate;
75 _iceConnectionReceivingTimeout = config->ice_connection_receiving_timeout;
hjon6d49a8e2016-01-26 13:06:42 -080076 _iceBackupCandidatePairPingInterval =
jtteh4eeb5372017-04-03 15:06:37 -070077 config->ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080078 _keyType = RTCEncryptionKeyTypeECDSA;
jtteh4eeb5372017-04-03 15:06:37 -070079 _iceCandidatePoolSize = config->ice_candidate_pool_size;
80 _shouldPruneTurnPorts = config->prune_turn_ports;
honghaizaf6b6e02016-07-11 15:09:26 -070081 _shouldPresumeWritableWhenFullyRelayed =
jtteh4eeb5372017-04-03 15:06:37 -070082 config->presume_writable_when_fully_relayed;
83 if (config->ice_check_min_interval) {
skvlada5d94ff2017-02-02 13:02:30 -080084 _iceCheckMinInterval =
jtteh4eeb5372017-04-03 15:06:37 -070085 [NSNumber numberWithInt:*config->ice_check_min_interval];
skvlada5d94ff2017-02-02 13:02:30 -080086 }
hjon6d49a8e2016-01-26 13:06:42 -080087 }
88 return self;
89}
90
91- (NSString *)description {
92 return [NSString stringWithFormat:
skvlada5d94ff2017-02-02 13:02:30 -080093 @"RTCConfiguration: {\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%@\n}\n",
hjon6d49a8e2016-01-26 13:06:42 -080094 _iceServers,
95 [[self class] stringForTransportPolicy:_iceTransportPolicy],
96 [[self class] stringForBundlePolicy:_bundlePolicy],
97 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
98 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070099 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -0700100 [[self class]
101 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -0800102 _audioJitterBufferMaxPackets,
hayscc9f95002016-12-05 14:24:32 -0800103 _audioJitterBufferFastAccelerate,
hjon6d49a8e2016-01-26 13:06:42 -0800104 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -0700105 _iceBackupCandidatePairPingInterval,
Taylor Brandstettere9851112016-07-01 11:11:13 -0700106 _iceCandidatePoolSize,
honghaizaf6b6e02016-07-11 15:09:26 -0700107 _shouldPruneTurnPorts,
skvlada5d94ff2017-02-02 13:02:30 -0800108 _shouldPresumeWritableWhenFullyRelayed,
109 _iceCheckMinInterval];
hjon6d49a8e2016-01-26 13:06:42 -0800110}
111
112#pragma mark - Private
113
hbosa73ca562016-05-17 03:28:58 -0700114- (webrtc::PeerConnectionInterface::RTCConfiguration *)
115 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200116 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
Honghai Zhangf7ddc062016-09-01 15:34:01 -0700117 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration(
118 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive));
hjon6d49a8e2016-01-26 13:06:42 -0800119
120 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200121 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -0800122 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200123 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -0800124 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200125 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800126 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200127 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800128 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200129 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800130 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700131 nativeConfig->candidate_network_policy = [[self class]
132 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200133 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700134 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200135 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
hayscc9f95002016-12-05 14:24:32 -0800136 nativeConfig->audio_jitter_buffer_fast_accelerate =
137 _audioJitterBufferFastAccelerate ? true : false;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200138 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800139 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200140 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800141 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200142 rtc::KeyType keyType =
143 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
144 // Generate non-default certificate.
145 if (keyType != rtc::KT_DEFAULT) {
146 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
147 rtc::RTCCertificateGenerator::GenerateCertificate(
148 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
149 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700150 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200151 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800152 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200153 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800154 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700155 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
honghaizaf6b6e02016-07-11 15:09:26 -0700156 nativeConfig->prune_turn_ports = _shouldPruneTurnPorts ? true : false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700157 nativeConfig->presume_writable_when_fully_relayed =
honghaizaf6b6e02016-07-11 15:09:26 -0700158 _shouldPresumeWritableWhenFullyRelayed ? true : false;
skvlada5d94ff2017-02-02 13:02:30 -0800159 if (_iceCheckMinInterval != nil) {
160 nativeConfig->ice_check_min_interval =
161 rtc::Optional<int>(_iceCheckMinInterval.intValue);
162 }
hjon6d49a8e2016-01-26 13:06:42 -0800163
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200164 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800165}
166
hjon6d49a8e2016-01-26 13:06:42 -0800167+ (webrtc::PeerConnectionInterface::IceTransportsType)
168 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
169 switch (policy) {
170 case RTCIceTransportPolicyNone:
171 return webrtc::PeerConnectionInterface::kNone;
172 case RTCIceTransportPolicyRelay:
173 return webrtc::PeerConnectionInterface::kRelay;
174 case RTCIceTransportPolicyNoHost:
175 return webrtc::PeerConnectionInterface::kNoHost;
176 case RTCIceTransportPolicyAll:
177 return webrtc::PeerConnectionInterface::kAll;
178 }
179}
180
181+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
182 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
183 switch (nativeType) {
184 case webrtc::PeerConnectionInterface::kNone:
185 return RTCIceTransportPolicyNone;
186 case webrtc::PeerConnectionInterface::kRelay:
187 return RTCIceTransportPolicyRelay;
188 case webrtc::PeerConnectionInterface::kNoHost:
189 return RTCIceTransportPolicyNoHost;
190 case webrtc::PeerConnectionInterface::kAll:
191 return RTCIceTransportPolicyAll;
192 }
193}
194
195+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
196 switch (policy) {
197 case RTCIceTransportPolicyNone:
198 return @"NONE";
199 case RTCIceTransportPolicyRelay:
200 return @"RELAY";
201 case RTCIceTransportPolicyNoHost:
202 return @"NO_HOST";
203 case RTCIceTransportPolicyAll:
204 return @"ALL";
205 }
206}
207
208+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
209 (RTCBundlePolicy)policy {
210 switch (policy) {
211 case RTCBundlePolicyBalanced:
212 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
213 case RTCBundlePolicyMaxCompat:
214 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
215 case RTCBundlePolicyMaxBundle:
216 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
217 }
218}
219
220+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
221 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
222 switch (nativePolicy) {
223 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
224 return RTCBundlePolicyBalanced;
225 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
226 return RTCBundlePolicyMaxCompat;
227 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
228 return RTCBundlePolicyMaxBundle;
229 }
230}
231
232+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
233 switch (policy) {
234 case RTCBundlePolicyBalanced:
235 return @"BALANCED";
236 case RTCBundlePolicyMaxCompat:
237 return @"MAX_COMPAT";
238 case RTCBundlePolicyMaxBundle:
239 return @"MAX_BUNDLE";
240 }
241}
242
243+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
244 (RTCRtcpMuxPolicy)policy {
245 switch (policy) {
246 case RTCRtcpMuxPolicyNegotiate:
247 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
248 case RTCRtcpMuxPolicyRequire:
249 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
250 }
251}
252
253+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
254 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
255 switch (nativePolicy) {
256 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
257 return RTCRtcpMuxPolicyNegotiate;
258 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
259 return RTCRtcpMuxPolicyRequire;
260 }
261}
262
263+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
264 switch (policy) {
265 case RTCRtcpMuxPolicyNegotiate:
266 return @"NEGOTIATE";
267 case RTCRtcpMuxPolicyRequire:
268 return @"REQUIRE";
269 }
270}
271
272+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
273 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
274 switch (policy) {
275 case RTCTcpCandidatePolicyEnabled:
276 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
277 case RTCTcpCandidatePolicyDisabled:
278 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
279 }
280}
281
Honghai Zhang46007ae2016-06-03 16:31:32 -0700282+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
283 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
284 switch (policy) {
285 case RTCCandidateNetworkPolicyAll:
286 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
287 case RTCCandidateNetworkPolicyLowCost:
288 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
289 }
290}
291
hjon6d49a8e2016-01-26 13:06:42 -0800292+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
293 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
294 switch (nativePolicy) {
295 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
296 return RTCTcpCandidatePolicyEnabled;
297 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
298 return RTCTcpCandidatePolicyDisabled;
299 }
300}
301
302+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
303 switch (policy) {
304 case RTCTcpCandidatePolicyEnabled:
305 return @"TCP_ENABLED";
306 case RTCTcpCandidatePolicyDisabled:
307 return @"TCP_DISABLED";
308 }
309}
310
Honghai Zhang46007ae2016-06-03 16:31:32 -0700311+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
312 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
313 switch (nativePolicy) {
314 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
315 return RTCCandidateNetworkPolicyAll;
316 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
317 return RTCCandidateNetworkPolicyLowCost;
318 }
319}
320
321+ (NSString *)stringForCandidateNetworkPolicy:
322 (RTCCandidateNetworkPolicy)policy {
323 switch (policy) {
324 case RTCCandidateNetworkPolicyAll:
325 return @"CANDIDATE_ALL_NETWORKS";
326 case RTCCandidateNetworkPolicyLowCost:
327 return @"CANDIDATE_LOW_COST_NETWORKS";
328 }
329}
330
Honghai Zhang3108fc92016-05-11 10:10:39 -0700331+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
332 nativeContinualGatheringPolicyForPolicy:
333 (RTCContinualGatheringPolicy)policy {
334 switch (policy) {
335 case RTCContinualGatheringPolicyGatherOnce:
336 return webrtc::PeerConnectionInterface::GATHER_ONCE;
337 case RTCContinualGatheringPolicyGatherContinually:
338 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
339 }
340}
341
342+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
343 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
344 switch (nativePolicy) {
345 case webrtc::PeerConnectionInterface::GATHER_ONCE:
346 return RTCContinualGatheringPolicyGatherOnce;
347 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
348 return RTCContinualGatheringPolicyGatherContinually;
349 }
350}
351
352+ (NSString *)stringForContinualGatheringPolicy:
353 (RTCContinualGatheringPolicy)policy {
354 switch (policy) {
355 case RTCContinualGatheringPolicyGatherOnce:
356 return @"GATHER_ONCE";
357 case RTCContinualGatheringPolicyGatherContinually:
358 return @"GATHER_CONTINUALLY";
359 }
360}
361
hbosf9da44d2016-06-09 03:18:28 -0700362+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
363 (RTCEncryptionKeyType)keyType {
364 switch (keyType) {
365 case RTCEncryptionKeyTypeRSA:
366 return rtc::KT_RSA;
367 case RTCEncryptionKeyTypeECDSA:
368 return rtc::KT_ECDSA;
369 }
370}
371
hjon6d49a8e2016-01-26 13:06:42 -0800372@end