blob: bf602d7da70ee22306824e9ee27fb0a379233677 [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;
hjon6d49a8e2016-01-26 13:06:42 -080040
41- (instancetype)init {
42 if (self = [super init]) {
43 _iceServers = [NSMutableArray array];
44 // Copy defaults.
Honghai Zhangf7ddc062016-09-01 15:34:01 -070045 webrtc::PeerConnectionInterface::RTCConfiguration config(
46 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive);
hjon6d49a8e2016-01-26 13:06:42 -080047 _iceTransportPolicy =
48 [[self class] transportPolicyForTransportsType:config.type];
49 _bundlePolicy =
50 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
51 _rtcpMuxPolicy =
52 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
53 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
54 config.tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070055 _candidateNetworkPolicy = [[self class]
56 candidateNetworkPolicyForNativePolicy:config.candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070057 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
58 config.continual_gathering_policy;
59 _continualGatheringPolicy =
60 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080061 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
hayscc9f95002016-12-05 14:24:32 -080062 _audioJitterBufferFastAccelerate = config.audio_jitter_buffer_fast_accelerate;
hjon6d49a8e2016-01-26 13:06:42 -080063 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
64 _iceBackupCandidatePairPingInterval =
65 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080066 _keyType = RTCEncryptionKeyTypeECDSA;
deadbeefbe0c96f2016-05-18 16:20:14 -070067 _iceCandidatePoolSize = config.ice_candidate_pool_size;
honghaizaf6b6e02016-07-11 15:09:26 -070068 _shouldPruneTurnPorts = config.prune_turn_ports;
69 _shouldPresumeWritableWhenFullyRelayed =
Taylor Brandstettere9851112016-07-01 11:11:13 -070070 config.presume_writable_when_fully_relayed;
hjon6d49a8e2016-01-26 13:06:42 -080071 }
72 return self;
73}
74
75- (NSString *)description {
76 return [NSString stringWithFormat:
hayscc9f95002016-12-05 14:24:32 -080077 @"RTCConfiguration: {\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n}\n",
hjon6d49a8e2016-01-26 13:06:42 -080078 _iceServers,
79 [[self class] stringForTransportPolicy:_iceTransportPolicy],
80 [[self class] stringForBundlePolicy:_bundlePolicy],
81 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
82 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070083 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070084 [[self class]
85 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080086 _audioJitterBufferMaxPackets,
hayscc9f95002016-12-05 14:24:32 -080087 _audioJitterBufferFastAccelerate,
hjon6d49a8e2016-01-26 13:06:42 -080088 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -070089 _iceBackupCandidatePairPingInterval,
Taylor Brandstettere9851112016-07-01 11:11:13 -070090 _iceCandidatePoolSize,
honghaizaf6b6e02016-07-11 15:09:26 -070091 _shouldPruneTurnPorts,
92 _shouldPresumeWritableWhenFullyRelayed];
hjon6d49a8e2016-01-26 13:06:42 -080093}
94
95#pragma mark - Private
96
hbosa73ca562016-05-17 03:28:58 -070097- (webrtc::PeerConnectionInterface::RTCConfiguration *)
98 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020099 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
Honghai Zhangf7ddc062016-09-01 15:34:01 -0700100 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration(
101 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive));
hjon6d49a8e2016-01-26 13:06:42 -0800102
103 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200104 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -0800105 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200106 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -0800107 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200108 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800109 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200110 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800111 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200112 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800113 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700114 nativeConfig->candidate_network_policy = [[self class]
115 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200116 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700117 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200118 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
hayscc9f95002016-12-05 14:24:32 -0800119 nativeConfig->audio_jitter_buffer_fast_accelerate =
120 _audioJitterBufferFastAccelerate ? true : false;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200121 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800122 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200123 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800124 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200125 rtc::KeyType keyType =
126 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
127 // Generate non-default certificate.
128 if (keyType != rtc::KT_DEFAULT) {
129 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
130 rtc::RTCCertificateGenerator::GenerateCertificate(
131 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
132 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700133 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200134 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800135 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200136 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800137 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700138 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
honghaizaf6b6e02016-07-11 15:09:26 -0700139 nativeConfig->prune_turn_ports = _shouldPruneTurnPorts ? true : false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700140 nativeConfig->presume_writable_when_fully_relayed =
honghaizaf6b6e02016-07-11 15:09:26 -0700141 _shouldPresumeWritableWhenFullyRelayed ? true : false;
hjon6d49a8e2016-01-26 13:06:42 -0800142
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200143 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800144}
145
hjon6d49a8e2016-01-26 13:06:42 -0800146+ (webrtc::PeerConnectionInterface::IceTransportsType)
147 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
148 switch (policy) {
149 case RTCIceTransportPolicyNone:
150 return webrtc::PeerConnectionInterface::kNone;
151 case RTCIceTransportPolicyRelay:
152 return webrtc::PeerConnectionInterface::kRelay;
153 case RTCIceTransportPolicyNoHost:
154 return webrtc::PeerConnectionInterface::kNoHost;
155 case RTCIceTransportPolicyAll:
156 return webrtc::PeerConnectionInterface::kAll;
157 }
158}
159
160+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
161 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
162 switch (nativeType) {
163 case webrtc::PeerConnectionInterface::kNone:
164 return RTCIceTransportPolicyNone;
165 case webrtc::PeerConnectionInterface::kRelay:
166 return RTCIceTransportPolicyRelay;
167 case webrtc::PeerConnectionInterface::kNoHost:
168 return RTCIceTransportPolicyNoHost;
169 case webrtc::PeerConnectionInterface::kAll:
170 return RTCIceTransportPolicyAll;
171 }
172}
173
174+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
175 switch (policy) {
176 case RTCIceTransportPolicyNone:
177 return @"NONE";
178 case RTCIceTransportPolicyRelay:
179 return @"RELAY";
180 case RTCIceTransportPolicyNoHost:
181 return @"NO_HOST";
182 case RTCIceTransportPolicyAll:
183 return @"ALL";
184 }
185}
186
187+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
188 (RTCBundlePolicy)policy {
189 switch (policy) {
190 case RTCBundlePolicyBalanced:
191 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
192 case RTCBundlePolicyMaxCompat:
193 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
194 case RTCBundlePolicyMaxBundle:
195 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
196 }
197}
198
199+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
200 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
201 switch (nativePolicy) {
202 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
203 return RTCBundlePolicyBalanced;
204 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
205 return RTCBundlePolicyMaxCompat;
206 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
207 return RTCBundlePolicyMaxBundle;
208 }
209}
210
211+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
212 switch (policy) {
213 case RTCBundlePolicyBalanced:
214 return @"BALANCED";
215 case RTCBundlePolicyMaxCompat:
216 return @"MAX_COMPAT";
217 case RTCBundlePolicyMaxBundle:
218 return @"MAX_BUNDLE";
219 }
220}
221
222+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
223 (RTCRtcpMuxPolicy)policy {
224 switch (policy) {
225 case RTCRtcpMuxPolicyNegotiate:
226 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
227 case RTCRtcpMuxPolicyRequire:
228 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
229 }
230}
231
232+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
233 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
234 switch (nativePolicy) {
235 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
236 return RTCRtcpMuxPolicyNegotiate;
237 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
238 return RTCRtcpMuxPolicyRequire;
239 }
240}
241
242+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
243 switch (policy) {
244 case RTCRtcpMuxPolicyNegotiate:
245 return @"NEGOTIATE";
246 case RTCRtcpMuxPolicyRequire:
247 return @"REQUIRE";
248 }
249}
250
251+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
252 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
253 switch (policy) {
254 case RTCTcpCandidatePolicyEnabled:
255 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
256 case RTCTcpCandidatePolicyDisabled:
257 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
258 }
259}
260
Honghai Zhang46007ae2016-06-03 16:31:32 -0700261+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
262 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
263 switch (policy) {
264 case RTCCandidateNetworkPolicyAll:
265 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
266 case RTCCandidateNetworkPolicyLowCost:
267 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
268 }
269}
270
hjon6d49a8e2016-01-26 13:06:42 -0800271+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
272 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
273 switch (nativePolicy) {
274 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
275 return RTCTcpCandidatePolicyEnabled;
276 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
277 return RTCTcpCandidatePolicyDisabled;
278 }
279}
280
281+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
282 switch (policy) {
283 case RTCTcpCandidatePolicyEnabled:
284 return @"TCP_ENABLED";
285 case RTCTcpCandidatePolicyDisabled:
286 return @"TCP_DISABLED";
287 }
288}
289
Honghai Zhang46007ae2016-06-03 16:31:32 -0700290+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
291 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
292 switch (nativePolicy) {
293 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
294 return RTCCandidateNetworkPolicyAll;
295 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
296 return RTCCandidateNetworkPolicyLowCost;
297 }
298}
299
300+ (NSString *)stringForCandidateNetworkPolicy:
301 (RTCCandidateNetworkPolicy)policy {
302 switch (policy) {
303 case RTCCandidateNetworkPolicyAll:
304 return @"CANDIDATE_ALL_NETWORKS";
305 case RTCCandidateNetworkPolicyLowCost:
306 return @"CANDIDATE_LOW_COST_NETWORKS";
307 }
308}
309
Honghai Zhang3108fc92016-05-11 10:10:39 -0700310+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
311 nativeContinualGatheringPolicyForPolicy:
312 (RTCContinualGatheringPolicy)policy {
313 switch (policy) {
314 case RTCContinualGatheringPolicyGatherOnce:
315 return webrtc::PeerConnectionInterface::GATHER_ONCE;
316 case RTCContinualGatheringPolicyGatherContinually:
317 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
318 }
319}
320
321+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
322 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
323 switch (nativePolicy) {
324 case webrtc::PeerConnectionInterface::GATHER_ONCE:
325 return RTCContinualGatheringPolicyGatherOnce;
326 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
327 return RTCContinualGatheringPolicyGatherContinually;
328 }
329}
330
331+ (NSString *)stringForContinualGatheringPolicy:
332 (RTCContinualGatheringPolicy)policy {
333 switch (policy) {
334 case RTCContinualGatheringPolicyGatherOnce:
335 return @"GATHER_ONCE";
336 case RTCContinualGatheringPolicyGatherContinually:
337 return @"GATHER_CONTINUALLY";
338 }
339}
340
hbosf9da44d2016-06-09 03:18:28 -0700341+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
342 (RTCEncryptionKeyType)keyType {
343 switch (keyType) {
344 case RTCEncryptionKeyTypeRSA:
345 return rtc::KT_RSA;
346 case RTCEncryptionKeyTypeECDSA:
347 return rtc::KT_ECDSA;
348 }
349}
350
hjon6d49a8e2016-01-26 13:06:42 -0800351@end