blob: 48f9e3716814458077fc883116393a44c2be5fb3 [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);
jtteh465faf02017-04-04 14:00:16 -070046 return [self initWithNativeConfiguration:config];
jtteh4eeb5372017-04-03 15:06:37 -070047}
48
49- (instancetype)initWithNativeConfiguration:
jtteh465faf02017-04-04 14:00:16 -070050 (const webrtc::PeerConnectionInterface::RTCConfiguration &)config {
hjon6d49a8e2016-01-26 13:06:42 -080051 if (self = [super init]) {
jtteh4eeb5372017-04-03 15:06:37 -070052 NSMutableArray *iceServers = [NSMutableArray array];
jtteh465faf02017-04-04 14:00:16 -070053 for (const webrtc::PeerConnectionInterface::IceServer& server : config.servers) {
jtteh4eeb5372017-04-03 15:06:37 -070054 RTCIceServer *iceServer = [[RTCIceServer alloc] initWithNativeServer:server];
55 [iceServers addObject:iceServer];
56 }
57 _iceServers = iceServers;
hjon6d49a8e2016-01-26 13:06:42 -080058 _iceTransportPolicy =
jtteh465faf02017-04-04 14:00:16 -070059 [[self class] transportPolicyForTransportsType:config.type];
hjon6d49a8e2016-01-26 13:06:42 -080060 _bundlePolicy =
jtteh465faf02017-04-04 14:00:16 -070061 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
hjon6d49a8e2016-01-26 13:06:42 -080062 _rtcpMuxPolicy =
jtteh465faf02017-04-04 14:00:16 -070063 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
hjon6d49a8e2016-01-26 13:06:42 -080064 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
jtteh465faf02017-04-04 14:00:16 -070065 config.tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070066 _candidateNetworkPolicy = [[self class]
jtteh465faf02017-04-04 14:00:16 -070067 candidateNetworkPolicyForNativePolicy:config.candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070068 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
jtteh465faf02017-04-04 14:00:16 -070069 config.continual_gathering_policy;
Honghai Zhang3108fc92016-05-11 10:10:39 -070070 _continualGatheringPolicy =
71 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
jtteh465faf02017-04-04 14:00:16 -070072 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
73 _audioJitterBufferFastAccelerate = config.audio_jitter_buffer_fast_accelerate;
74 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
hjon6d49a8e2016-01-26 13:06:42 -080075 _iceBackupCandidatePairPingInterval =
jtteh465faf02017-04-04 14:00:16 -070076 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080077 _keyType = RTCEncryptionKeyTypeECDSA;
jtteh465faf02017-04-04 14:00:16 -070078 _iceCandidatePoolSize = config.ice_candidate_pool_size;
79 _shouldPruneTurnPorts = config.prune_turn_ports;
honghaizaf6b6e02016-07-11 15:09:26 -070080 _shouldPresumeWritableWhenFullyRelayed =
jtteh465faf02017-04-04 14:00:16 -070081 config.presume_writable_when_fully_relayed;
82 if (config.ice_check_min_interval) {
skvlada5d94ff2017-02-02 13:02:30 -080083 _iceCheckMinInterval =
jtteh465faf02017-04-04 14:00:16 -070084 [NSNumber numberWithInt:*config.ice_check_min_interval];
skvlada5d94ff2017-02-02 13:02:30 -080085 }
hjon6d49a8e2016-01-26 13:06:42 -080086 }
87 return self;
88}
89
90- (NSString *)description {
91 return [NSString stringWithFormat:
skvlada5d94ff2017-02-02 13:02:30 -080092 @"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 -080093 _iceServers,
94 [[self class] stringForTransportPolicy:_iceTransportPolicy],
95 [[self class] stringForBundlePolicy:_bundlePolicy],
96 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
97 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070098 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070099 [[self class]
100 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -0800101 _audioJitterBufferMaxPackets,
hayscc9f95002016-12-05 14:24:32 -0800102 _audioJitterBufferFastAccelerate,
hjon6d49a8e2016-01-26 13:06:42 -0800103 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -0700104 _iceBackupCandidatePairPingInterval,
Taylor Brandstettere9851112016-07-01 11:11:13 -0700105 _iceCandidatePoolSize,
honghaizaf6b6e02016-07-11 15:09:26 -0700106 _shouldPruneTurnPorts,
skvlada5d94ff2017-02-02 13:02:30 -0800107 _shouldPresumeWritableWhenFullyRelayed,
108 _iceCheckMinInterval];
hjon6d49a8e2016-01-26 13:06:42 -0800109}
110
111#pragma mark - Private
112
hbosa73ca562016-05-17 03:28:58 -0700113- (webrtc::PeerConnectionInterface::RTCConfiguration *)
114 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200115 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
Honghai Zhangf7ddc062016-09-01 15:34:01 -0700116 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration(
117 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive));
hjon6d49a8e2016-01-26 13:06:42 -0800118
119 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200120 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -0800121 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200122 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -0800123 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200124 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800125 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200126 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800127 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200128 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800129 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700130 nativeConfig->candidate_network_policy = [[self class]
131 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200132 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700133 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200134 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
hayscc9f95002016-12-05 14:24:32 -0800135 nativeConfig->audio_jitter_buffer_fast_accelerate =
136 _audioJitterBufferFastAccelerate ? true : false;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200137 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800138 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200139 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800140 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200141 rtc::KeyType keyType =
142 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
143 // Generate non-default certificate.
144 if (keyType != rtc::KT_DEFAULT) {
145 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
146 rtc::RTCCertificateGenerator::GenerateCertificate(
147 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
148 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700149 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200150 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800151 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200152 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800153 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700154 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
honghaizaf6b6e02016-07-11 15:09:26 -0700155 nativeConfig->prune_turn_ports = _shouldPruneTurnPorts ? true : false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700156 nativeConfig->presume_writable_when_fully_relayed =
honghaizaf6b6e02016-07-11 15:09:26 -0700157 _shouldPresumeWritableWhenFullyRelayed ? true : false;
skvlada5d94ff2017-02-02 13:02:30 -0800158 if (_iceCheckMinInterval != nil) {
159 nativeConfig->ice_check_min_interval =
160 rtc::Optional<int>(_iceCheckMinInterval.intValue);
161 }
hjon6d49a8e2016-01-26 13:06:42 -0800162
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200163 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800164}
165
hjon6d49a8e2016-01-26 13:06:42 -0800166+ (webrtc::PeerConnectionInterface::IceTransportsType)
167 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
168 switch (policy) {
169 case RTCIceTransportPolicyNone:
170 return webrtc::PeerConnectionInterface::kNone;
171 case RTCIceTransportPolicyRelay:
172 return webrtc::PeerConnectionInterface::kRelay;
173 case RTCIceTransportPolicyNoHost:
174 return webrtc::PeerConnectionInterface::kNoHost;
175 case RTCIceTransportPolicyAll:
176 return webrtc::PeerConnectionInterface::kAll;
177 }
178}
179
180+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
181 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
182 switch (nativeType) {
183 case webrtc::PeerConnectionInterface::kNone:
184 return RTCIceTransportPolicyNone;
185 case webrtc::PeerConnectionInterface::kRelay:
186 return RTCIceTransportPolicyRelay;
187 case webrtc::PeerConnectionInterface::kNoHost:
188 return RTCIceTransportPolicyNoHost;
189 case webrtc::PeerConnectionInterface::kAll:
190 return RTCIceTransportPolicyAll;
191 }
192}
193
194+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
195 switch (policy) {
196 case RTCIceTransportPolicyNone:
197 return @"NONE";
198 case RTCIceTransportPolicyRelay:
199 return @"RELAY";
200 case RTCIceTransportPolicyNoHost:
201 return @"NO_HOST";
202 case RTCIceTransportPolicyAll:
203 return @"ALL";
204 }
205}
206
207+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
208 (RTCBundlePolicy)policy {
209 switch (policy) {
210 case RTCBundlePolicyBalanced:
211 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
212 case RTCBundlePolicyMaxCompat:
213 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
214 case RTCBundlePolicyMaxBundle:
215 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
216 }
217}
218
219+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
220 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
221 switch (nativePolicy) {
222 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
223 return RTCBundlePolicyBalanced;
224 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
225 return RTCBundlePolicyMaxCompat;
226 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
227 return RTCBundlePolicyMaxBundle;
228 }
229}
230
231+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
232 switch (policy) {
233 case RTCBundlePolicyBalanced:
234 return @"BALANCED";
235 case RTCBundlePolicyMaxCompat:
236 return @"MAX_COMPAT";
237 case RTCBundlePolicyMaxBundle:
238 return @"MAX_BUNDLE";
239 }
240}
241
242+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
243 (RTCRtcpMuxPolicy)policy {
244 switch (policy) {
245 case RTCRtcpMuxPolicyNegotiate:
246 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
247 case RTCRtcpMuxPolicyRequire:
248 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
249 }
250}
251
252+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
253 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
254 switch (nativePolicy) {
255 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
256 return RTCRtcpMuxPolicyNegotiate;
257 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
258 return RTCRtcpMuxPolicyRequire;
259 }
260}
261
262+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
263 switch (policy) {
264 case RTCRtcpMuxPolicyNegotiate:
265 return @"NEGOTIATE";
266 case RTCRtcpMuxPolicyRequire:
267 return @"REQUIRE";
268 }
269}
270
271+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
272 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
273 switch (policy) {
274 case RTCTcpCandidatePolicyEnabled:
275 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
276 case RTCTcpCandidatePolicyDisabled:
277 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
278 }
279}
280
Honghai Zhang46007ae2016-06-03 16:31:32 -0700281+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
282 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
283 switch (policy) {
284 case RTCCandidateNetworkPolicyAll:
285 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
286 case RTCCandidateNetworkPolicyLowCost:
287 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
288 }
289}
290
hjon6d49a8e2016-01-26 13:06:42 -0800291+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
292 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
293 switch (nativePolicy) {
294 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
295 return RTCTcpCandidatePolicyEnabled;
296 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
297 return RTCTcpCandidatePolicyDisabled;
298 }
299}
300
301+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
302 switch (policy) {
303 case RTCTcpCandidatePolicyEnabled:
304 return @"TCP_ENABLED";
305 case RTCTcpCandidatePolicyDisabled:
306 return @"TCP_DISABLED";
307 }
308}
309
Honghai Zhang46007ae2016-06-03 16:31:32 -0700310+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
311 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
312 switch (nativePolicy) {
313 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
314 return RTCCandidateNetworkPolicyAll;
315 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
316 return RTCCandidateNetworkPolicyLowCost;
317 }
318}
319
320+ (NSString *)stringForCandidateNetworkPolicy:
321 (RTCCandidateNetworkPolicy)policy {
322 switch (policy) {
323 case RTCCandidateNetworkPolicyAll:
324 return @"CANDIDATE_ALL_NETWORKS";
325 case RTCCandidateNetworkPolicyLowCost:
326 return @"CANDIDATE_LOW_COST_NETWORKS";
327 }
328}
329
Honghai Zhang3108fc92016-05-11 10:10:39 -0700330+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
331 nativeContinualGatheringPolicyForPolicy:
332 (RTCContinualGatheringPolicy)policy {
333 switch (policy) {
334 case RTCContinualGatheringPolicyGatherOnce:
335 return webrtc::PeerConnectionInterface::GATHER_ONCE;
336 case RTCContinualGatheringPolicyGatherContinually:
337 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
338 }
339}
340
341+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
342 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
343 switch (nativePolicy) {
344 case webrtc::PeerConnectionInterface::GATHER_ONCE:
345 return RTCContinualGatheringPolicyGatherOnce;
346 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
347 return RTCContinualGatheringPolicyGatherContinually;
348 }
349}
350
351+ (NSString *)stringForContinualGatheringPolicy:
352 (RTCContinualGatheringPolicy)policy {
353 switch (policy) {
354 case RTCContinualGatheringPolicyGatherOnce:
355 return @"GATHER_ONCE";
356 case RTCContinualGatheringPolicyGatherContinually:
357 return @"GATHER_CONTINUALLY";
358 }
359}
360
hbosf9da44d2016-06-09 03:18:28 -0700361+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
362 (RTCEncryptionKeyType)keyType {
363 switch (keyType) {
364 case RTCEncryptionKeyTypeRSA:
365 return rtc::KT_RSA;
366 case RTCEncryptionKeyTypeECDSA:
367 return rtc::KT_ECDSA;
368 }
369}
370
hjon6d49a8e2016-01-26 13:06:42 -0800371@end