blob: ae37fc1c1f00caa732c67ef6c51b4ff851d93dbc [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 {
43 if (self = [super init]) {
44 _iceServers = [NSMutableArray array];
45 // Copy defaults.
Honghai Zhangf7ddc062016-09-01 15:34:01 -070046 webrtc::PeerConnectionInterface::RTCConfiguration config(
47 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive);
hjon6d49a8e2016-01-26 13:06:42 -080048 _iceTransportPolicy =
49 [[self class] transportPolicyForTransportsType:config.type];
50 _bundlePolicy =
51 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
52 _rtcpMuxPolicy =
53 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
54 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
55 config.tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070056 _candidateNetworkPolicy = [[self class]
57 candidateNetworkPolicyForNativePolicy:config.candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070058 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
59 config.continual_gathering_policy;
60 _continualGatheringPolicy =
61 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080062 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
hayscc9f95002016-12-05 14:24:32 -080063 _audioJitterBufferFastAccelerate = config.audio_jitter_buffer_fast_accelerate;
hjon6d49a8e2016-01-26 13:06:42 -080064 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
65 _iceBackupCandidatePairPingInterval =
66 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080067 _keyType = RTCEncryptionKeyTypeECDSA;
deadbeefbe0c96f2016-05-18 16:20:14 -070068 _iceCandidatePoolSize = config.ice_candidate_pool_size;
honghaizaf6b6e02016-07-11 15:09:26 -070069 _shouldPruneTurnPorts = config.prune_turn_ports;
70 _shouldPresumeWritableWhenFullyRelayed =
Taylor Brandstettere9851112016-07-01 11:11:13 -070071 config.presume_writable_when_fully_relayed;
skvlada5d94ff2017-02-02 13:02:30 -080072 if (config.ice_check_min_interval) {
73 _iceCheckMinInterval =
74 [NSNumber numberWithInt:*config.ice_check_min_interval];
75 }
hjon6d49a8e2016-01-26 13:06:42 -080076 }
77 return self;
78}
79
80- (NSString *)description {
81 return [NSString stringWithFormat:
skvlada5d94ff2017-02-02 13:02:30 -080082 @"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 -080083 _iceServers,
84 [[self class] stringForTransportPolicy:_iceTransportPolicy],
85 [[self class] stringForBundlePolicy:_bundlePolicy],
86 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
87 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070088 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070089 [[self class]
90 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080091 _audioJitterBufferMaxPackets,
hayscc9f95002016-12-05 14:24:32 -080092 _audioJitterBufferFastAccelerate,
hjon6d49a8e2016-01-26 13:06:42 -080093 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -070094 _iceBackupCandidatePairPingInterval,
Taylor Brandstettere9851112016-07-01 11:11:13 -070095 _iceCandidatePoolSize,
honghaizaf6b6e02016-07-11 15:09:26 -070096 _shouldPruneTurnPorts,
skvlada5d94ff2017-02-02 13:02:30 -080097 _shouldPresumeWritableWhenFullyRelayed,
98 _iceCheckMinInterval];
hjon6d49a8e2016-01-26 13:06:42 -080099}
100
101#pragma mark - Private
102
hbosa73ca562016-05-17 03:28:58 -0700103- (webrtc::PeerConnectionInterface::RTCConfiguration *)
104 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200105 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
Honghai Zhangf7ddc062016-09-01 15:34:01 -0700106 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration(
107 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive));
hjon6d49a8e2016-01-26 13:06:42 -0800108
109 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200110 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -0800111 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200112 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -0800113 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200114 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800115 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200116 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800117 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200118 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800119 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700120 nativeConfig->candidate_network_policy = [[self class]
121 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200122 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700123 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200124 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
hayscc9f95002016-12-05 14:24:32 -0800125 nativeConfig->audio_jitter_buffer_fast_accelerate =
126 _audioJitterBufferFastAccelerate ? true : false;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200127 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800128 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200129 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800130 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200131 rtc::KeyType keyType =
132 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
133 // Generate non-default certificate.
134 if (keyType != rtc::KT_DEFAULT) {
135 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
136 rtc::RTCCertificateGenerator::GenerateCertificate(
137 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
138 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700139 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200140 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800141 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200142 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800143 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700144 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
honghaizaf6b6e02016-07-11 15:09:26 -0700145 nativeConfig->prune_turn_ports = _shouldPruneTurnPorts ? true : false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700146 nativeConfig->presume_writable_when_fully_relayed =
honghaizaf6b6e02016-07-11 15:09:26 -0700147 _shouldPresumeWritableWhenFullyRelayed ? true : false;
skvlada5d94ff2017-02-02 13:02:30 -0800148 if (_iceCheckMinInterval != nil) {
149 nativeConfig->ice_check_min_interval =
150 rtc::Optional<int>(_iceCheckMinInterval.intValue);
151 }
hjon6d49a8e2016-01-26 13:06:42 -0800152
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200153 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800154}
155
hjon6d49a8e2016-01-26 13:06:42 -0800156+ (webrtc::PeerConnectionInterface::IceTransportsType)
157 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
158 switch (policy) {
159 case RTCIceTransportPolicyNone:
160 return webrtc::PeerConnectionInterface::kNone;
161 case RTCIceTransportPolicyRelay:
162 return webrtc::PeerConnectionInterface::kRelay;
163 case RTCIceTransportPolicyNoHost:
164 return webrtc::PeerConnectionInterface::kNoHost;
165 case RTCIceTransportPolicyAll:
166 return webrtc::PeerConnectionInterface::kAll;
167 }
168}
169
170+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
171 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
172 switch (nativeType) {
173 case webrtc::PeerConnectionInterface::kNone:
174 return RTCIceTransportPolicyNone;
175 case webrtc::PeerConnectionInterface::kRelay:
176 return RTCIceTransportPolicyRelay;
177 case webrtc::PeerConnectionInterface::kNoHost:
178 return RTCIceTransportPolicyNoHost;
179 case webrtc::PeerConnectionInterface::kAll:
180 return RTCIceTransportPolicyAll;
181 }
182}
183
184+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
185 switch (policy) {
186 case RTCIceTransportPolicyNone:
187 return @"NONE";
188 case RTCIceTransportPolicyRelay:
189 return @"RELAY";
190 case RTCIceTransportPolicyNoHost:
191 return @"NO_HOST";
192 case RTCIceTransportPolicyAll:
193 return @"ALL";
194 }
195}
196
197+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
198 (RTCBundlePolicy)policy {
199 switch (policy) {
200 case RTCBundlePolicyBalanced:
201 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
202 case RTCBundlePolicyMaxCompat:
203 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
204 case RTCBundlePolicyMaxBundle:
205 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
206 }
207}
208
209+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
210 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
211 switch (nativePolicy) {
212 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
213 return RTCBundlePolicyBalanced;
214 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
215 return RTCBundlePolicyMaxCompat;
216 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
217 return RTCBundlePolicyMaxBundle;
218 }
219}
220
221+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
222 switch (policy) {
223 case RTCBundlePolicyBalanced:
224 return @"BALANCED";
225 case RTCBundlePolicyMaxCompat:
226 return @"MAX_COMPAT";
227 case RTCBundlePolicyMaxBundle:
228 return @"MAX_BUNDLE";
229 }
230}
231
232+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
233 (RTCRtcpMuxPolicy)policy {
234 switch (policy) {
235 case RTCRtcpMuxPolicyNegotiate:
236 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
237 case RTCRtcpMuxPolicyRequire:
238 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
239 }
240}
241
242+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
243 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
244 switch (nativePolicy) {
245 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
246 return RTCRtcpMuxPolicyNegotiate;
247 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
248 return RTCRtcpMuxPolicyRequire;
249 }
250}
251
252+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
253 switch (policy) {
254 case RTCRtcpMuxPolicyNegotiate:
255 return @"NEGOTIATE";
256 case RTCRtcpMuxPolicyRequire:
257 return @"REQUIRE";
258 }
259}
260
261+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
262 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
263 switch (policy) {
264 case RTCTcpCandidatePolicyEnabled:
265 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
266 case RTCTcpCandidatePolicyDisabled:
267 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
268 }
269}
270
Honghai Zhang46007ae2016-06-03 16:31:32 -0700271+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
272 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
273 switch (policy) {
274 case RTCCandidateNetworkPolicyAll:
275 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
276 case RTCCandidateNetworkPolicyLowCost:
277 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
278 }
279}
280
hjon6d49a8e2016-01-26 13:06:42 -0800281+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
282 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
283 switch (nativePolicy) {
284 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
285 return RTCTcpCandidatePolicyEnabled;
286 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
287 return RTCTcpCandidatePolicyDisabled;
288 }
289}
290
291+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
292 switch (policy) {
293 case RTCTcpCandidatePolicyEnabled:
294 return @"TCP_ENABLED";
295 case RTCTcpCandidatePolicyDisabled:
296 return @"TCP_DISABLED";
297 }
298}
299
Honghai Zhang46007ae2016-06-03 16:31:32 -0700300+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
301 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
302 switch (nativePolicy) {
303 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
304 return RTCCandidateNetworkPolicyAll;
305 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
306 return RTCCandidateNetworkPolicyLowCost;
307 }
308}
309
310+ (NSString *)stringForCandidateNetworkPolicy:
311 (RTCCandidateNetworkPolicy)policy {
312 switch (policy) {
313 case RTCCandidateNetworkPolicyAll:
314 return @"CANDIDATE_ALL_NETWORKS";
315 case RTCCandidateNetworkPolicyLowCost:
316 return @"CANDIDATE_LOW_COST_NETWORKS";
317 }
318}
319
Honghai Zhang3108fc92016-05-11 10:10:39 -0700320+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
321 nativeContinualGatheringPolicyForPolicy:
322 (RTCContinualGatheringPolicy)policy {
323 switch (policy) {
324 case RTCContinualGatheringPolicyGatherOnce:
325 return webrtc::PeerConnectionInterface::GATHER_ONCE;
326 case RTCContinualGatheringPolicyGatherContinually:
327 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
328 }
329}
330
331+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
332 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
333 switch (nativePolicy) {
334 case webrtc::PeerConnectionInterface::GATHER_ONCE:
335 return RTCContinualGatheringPolicyGatherOnce;
336 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
337 return RTCContinualGatheringPolicyGatherContinually;
338 }
339}
340
341+ (NSString *)stringForContinualGatheringPolicy:
342 (RTCContinualGatheringPolicy)policy {
343 switch (policy) {
344 case RTCContinualGatheringPolicyGatherOnce:
345 return @"GATHER_ONCE";
346 case RTCContinualGatheringPolicyGatherContinually:
347 return @"GATHER_CONTINUALLY";
348 }
349}
350
hbosf9da44d2016-06-09 03:18:28 -0700351+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
352 (RTCEncryptionKeyType)keyType {
353 switch (keyType) {
354 case RTCEncryptionKeyTypeRSA:
355 return rtc::KT_RSA;
356 case RTCEncryptionKeyTypeECDSA:
357 return rtc::KT_ECDSA;
358 }
359}
360
hjon6d49a8e2016-01-26 13:06:42 -0800361@end