blob: be6628888bcd5888052d18d2c2fb8ad149c13ca6 [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;
honghaizaf6b6e02016-07-11 15:09:26 -070036@synthesize shouldPruneTurnPorts = _shouldPruneTurnPorts;
37@synthesize shouldPresumeWritableWhenFullyRelayed =
38 _shouldPresumeWritableWhenFullyRelayed;
hjon6d49a8e2016-01-26 13:06:42 -080039
40- (instancetype)init {
41 if (self = [super init]) {
42 _iceServers = [NSMutableArray array];
43 // Copy defaults.
44 webrtc::PeerConnectionInterface::RTCConfiguration config;
45 _iceTransportPolicy =
46 [[self class] transportPolicyForTransportsType:config.type];
47 _bundlePolicy =
48 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
49 _rtcpMuxPolicy =
50 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
51 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
52 config.tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070053 _candidateNetworkPolicy = [[self class]
54 candidateNetworkPolicyForNativePolicy:config.candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070055 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
56 config.continual_gathering_policy;
57 _continualGatheringPolicy =
58 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080059 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
60 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
61 _iceBackupCandidatePairPingInterval =
62 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080063 _keyType = RTCEncryptionKeyTypeECDSA;
deadbeefbe0c96f2016-05-18 16:20:14 -070064 _iceCandidatePoolSize = config.ice_candidate_pool_size;
honghaizaf6b6e02016-07-11 15:09:26 -070065 _shouldPruneTurnPorts = config.prune_turn_ports;
66 _shouldPresumeWritableWhenFullyRelayed =
Taylor Brandstettere9851112016-07-01 11:11:13 -070067 config.presume_writable_when_fully_relayed;
hjon6d49a8e2016-01-26 13:06:42 -080068 }
69 return self;
70}
71
72- (NSString *)description {
73 return [NSString stringWithFormat:
Honghai Zhange2e35ca2016-07-01 14:22:17 -070074 @"RTCConfiguration: {\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%d\n%d\n%d\n%d\n%d\n%d\n}\n",
hjon6d49a8e2016-01-26 13:06:42 -080075 _iceServers,
76 [[self class] stringForTransportPolicy:_iceTransportPolicy],
77 [[self class] stringForBundlePolicy:_bundlePolicy],
78 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
79 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070080 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070081 [[self class]
82 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080083 _audioJitterBufferMaxPackets,
84 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -070085 _iceBackupCandidatePairPingInterval,
Taylor Brandstettere9851112016-07-01 11:11:13 -070086 _iceCandidatePoolSize,
honghaizaf6b6e02016-07-11 15:09:26 -070087 _shouldPruneTurnPorts,
88 _shouldPresumeWritableWhenFullyRelayed];
hjon6d49a8e2016-01-26 13:06:42 -080089}
90
91#pragma mark - Private
92
hbosa73ca562016-05-17 03:28:58 -070093- (webrtc::PeerConnectionInterface::RTCConfiguration *)
94 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020095 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
96 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration());
hjon6d49a8e2016-01-26 13:06:42 -080097
98 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020099 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -0800100 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200101 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -0800102 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200103 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800104 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200105 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800106 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200107 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800108 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700109 nativeConfig->candidate_network_policy = [[self class]
110 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200111 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700112 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200113 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
114 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800115 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200116 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800117 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200118 rtc::KeyType keyType =
119 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
120 // Generate non-default certificate.
121 if (keyType != rtc::KT_DEFAULT) {
122 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
123 rtc::RTCCertificateGenerator::GenerateCertificate(
124 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
125 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700126 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200127 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800128 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200129 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800130 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700131 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
honghaizaf6b6e02016-07-11 15:09:26 -0700132 nativeConfig->prune_turn_ports = _shouldPruneTurnPorts ? true : false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700133 nativeConfig->presume_writable_when_fully_relayed =
honghaizaf6b6e02016-07-11 15:09:26 -0700134 _shouldPresumeWritableWhenFullyRelayed ? true : false;
hjon6d49a8e2016-01-26 13:06:42 -0800135
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200136 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800137}
138
hjon6d49a8e2016-01-26 13:06:42 -0800139+ (webrtc::PeerConnectionInterface::IceTransportsType)
140 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
141 switch (policy) {
142 case RTCIceTransportPolicyNone:
143 return webrtc::PeerConnectionInterface::kNone;
144 case RTCIceTransportPolicyRelay:
145 return webrtc::PeerConnectionInterface::kRelay;
146 case RTCIceTransportPolicyNoHost:
147 return webrtc::PeerConnectionInterface::kNoHost;
148 case RTCIceTransportPolicyAll:
149 return webrtc::PeerConnectionInterface::kAll;
150 }
151}
152
153+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
154 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
155 switch (nativeType) {
156 case webrtc::PeerConnectionInterface::kNone:
157 return RTCIceTransportPolicyNone;
158 case webrtc::PeerConnectionInterface::kRelay:
159 return RTCIceTransportPolicyRelay;
160 case webrtc::PeerConnectionInterface::kNoHost:
161 return RTCIceTransportPolicyNoHost;
162 case webrtc::PeerConnectionInterface::kAll:
163 return RTCIceTransportPolicyAll;
164 }
165}
166
167+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
168 switch (policy) {
169 case RTCIceTransportPolicyNone:
170 return @"NONE";
171 case RTCIceTransportPolicyRelay:
172 return @"RELAY";
173 case RTCIceTransportPolicyNoHost:
174 return @"NO_HOST";
175 case RTCIceTransportPolicyAll:
176 return @"ALL";
177 }
178}
179
180+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
181 (RTCBundlePolicy)policy {
182 switch (policy) {
183 case RTCBundlePolicyBalanced:
184 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
185 case RTCBundlePolicyMaxCompat:
186 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
187 case RTCBundlePolicyMaxBundle:
188 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
189 }
190}
191
192+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
193 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
194 switch (nativePolicy) {
195 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
196 return RTCBundlePolicyBalanced;
197 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
198 return RTCBundlePolicyMaxCompat;
199 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
200 return RTCBundlePolicyMaxBundle;
201 }
202}
203
204+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
205 switch (policy) {
206 case RTCBundlePolicyBalanced:
207 return @"BALANCED";
208 case RTCBundlePolicyMaxCompat:
209 return @"MAX_COMPAT";
210 case RTCBundlePolicyMaxBundle:
211 return @"MAX_BUNDLE";
212 }
213}
214
215+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
216 (RTCRtcpMuxPolicy)policy {
217 switch (policy) {
218 case RTCRtcpMuxPolicyNegotiate:
219 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
220 case RTCRtcpMuxPolicyRequire:
221 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
222 }
223}
224
225+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
226 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
227 switch (nativePolicy) {
228 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
229 return RTCRtcpMuxPolicyNegotiate;
230 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
231 return RTCRtcpMuxPolicyRequire;
232 }
233}
234
235+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
236 switch (policy) {
237 case RTCRtcpMuxPolicyNegotiate:
238 return @"NEGOTIATE";
239 case RTCRtcpMuxPolicyRequire:
240 return @"REQUIRE";
241 }
242}
243
244+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
245 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
246 switch (policy) {
247 case RTCTcpCandidatePolicyEnabled:
248 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
249 case RTCTcpCandidatePolicyDisabled:
250 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
251 }
252}
253
Honghai Zhang46007ae2016-06-03 16:31:32 -0700254+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
255 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
256 switch (policy) {
257 case RTCCandidateNetworkPolicyAll:
258 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
259 case RTCCandidateNetworkPolicyLowCost:
260 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
261 }
262}
263
hjon6d49a8e2016-01-26 13:06:42 -0800264+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
265 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
266 switch (nativePolicy) {
267 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
268 return RTCTcpCandidatePolicyEnabled;
269 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
270 return RTCTcpCandidatePolicyDisabled;
271 }
272}
273
274+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
275 switch (policy) {
276 case RTCTcpCandidatePolicyEnabled:
277 return @"TCP_ENABLED";
278 case RTCTcpCandidatePolicyDisabled:
279 return @"TCP_DISABLED";
280 }
281}
282
Honghai Zhang46007ae2016-06-03 16:31:32 -0700283+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
284 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
285 switch (nativePolicy) {
286 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
287 return RTCCandidateNetworkPolicyAll;
288 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
289 return RTCCandidateNetworkPolicyLowCost;
290 }
291}
292
293+ (NSString *)stringForCandidateNetworkPolicy:
294 (RTCCandidateNetworkPolicy)policy {
295 switch (policy) {
296 case RTCCandidateNetworkPolicyAll:
297 return @"CANDIDATE_ALL_NETWORKS";
298 case RTCCandidateNetworkPolicyLowCost:
299 return @"CANDIDATE_LOW_COST_NETWORKS";
300 }
301}
302
Honghai Zhang3108fc92016-05-11 10:10:39 -0700303+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
304 nativeContinualGatheringPolicyForPolicy:
305 (RTCContinualGatheringPolicy)policy {
306 switch (policy) {
307 case RTCContinualGatheringPolicyGatherOnce:
308 return webrtc::PeerConnectionInterface::GATHER_ONCE;
309 case RTCContinualGatheringPolicyGatherContinually:
310 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
311 }
312}
313
314+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
315 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
316 switch (nativePolicy) {
317 case webrtc::PeerConnectionInterface::GATHER_ONCE:
318 return RTCContinualGatheringPolicyGatherOnce;
319 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
320 return RTCContinualGatheringPolicyGatherContinually;
321 }
322}
323
324+ (NSString *)stringForContinualGatheringPolicy:
325 (RTCContinualGatheringPolicy)policy {
326 switch (policy) {
327 case RTCContinualGatheringPolicyGatherOnce:
328 return @"GATHER_ONCE";
329 case RTCContinualGatheringPolicyGatherContinually:
330 return @"GATHER_CONTINUALLY";
331 }
332}
333
hbosf9da44d2016-06-09 03:18:28 -0700334+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
335 (RTCEncryptionKeyType)keyType {
336 switch (keyType) {
337 case RTCEncryptionKeyTypeRSA:
338 return rtc::KT_RSA;
339 case RTCEncryptionKeyTypeECDSA:
340 return rtc::KT_ECDSA;
341 }
342}
343
hjon6d49a8e2016-01-26 13:06:42 -0800344@end