blob: 4bd666c32e411076a4776df612312c824212e845 [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.
Honghai Zhangf7ddc062016-09-01 15:34:01 -070044 webrtc::PeerConnectionInterface::RTCConfiguration config(
45 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive);
hjon6d49a8e2016-01-26 13:06:42 -080046 _iceTransportPolicy =
47 [[self class] transportPolicyForTransportsType:config.type];
48 _bundlePolicy =
49 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
50 _rtcpMuxPolicy =
51 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
52 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
53 config.tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070054 _candidateNetworkPolicy = [[self class]
55 candidateNetworkPolicyForNativePolicy:config.candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070056 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
57 config.continual_gathering_policy;
58 _continualGatheringPolicy =
59 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080060 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
61 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
62 _iceBackupCandidatePairPingInterval =
63 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080064 _keyType = RTCEncryptionKeyTypeECDSA;
deadbeefbe0c96f2016-05-18 16:20:14 -070065 _iceCandidatePoolSize = config.ice_candidate_pool_size;
honghaizaf6b6e02016-07-11 15:09:26 -070066 _shouldPruneTurnPorts = config.prune_turn_ports;
67 _shouldPresumeWritableWhenFullyRelayed =
Taylor Brandstettere9851112016-07-01 11:11:13 -070068 config.presume_writable_when_fully_relayed;
hjon6d49a8e2016-01-26 13:06:42 -080069 }
70 return self;
71}
72
73- (NSString *)description {
74 return [NSString stringWithFormat:
Honghai Zhange2e35ca2016-07-01 14:22:17 -070075 @"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 -080076 _iceServers,
77 [[self class] stringForTransportPolicy:_iceTransportPolicy],
78 [[self class] stringForBundlePolicy:_bundlePolicy],
79 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
80 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070081 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070082 [[self class]
83 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080084 _audioJitterBufferMaxPackets,
85 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -070086 _iceBackupCandidatePairPingInterval,
Taylor Brandstettere9851112016-07-01 11:11:13 -070087 _iceCandidatePoolSize,
honghaizaf6b6e02016-07-11 15:09:26 -070088 _shouldPruneTurnPorts,
89 _shouldPresumeWritableWhenFullyRelayed];
hjon6d49a8e2016-01-26 13:06:42 -080090}
91
92#pragma mark - Private
93
hbosa73ca562016-05-17 03:28:58 -070094- (webrtc::PeerConnectionInterface::RTCConfiguration *)
95 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020096 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
Honghai Zhangf7ddc062016-09-01 15:34:01 -070097 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration(
98 webrtc::PeerConnectionInterface::RTCConfigurationType::kAggressive));
hjon6d49a8e2016-01-26 13:06:42 -080099
100 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200101 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -0800102 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200103 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -0800104 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200105 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800106 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200107 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800108 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200109 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800110 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700111 nativeConfig->candidate_network_policy = [[self class]
112 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200113 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700114 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200115 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
116 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800117 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200118 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800119 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200120 rtc::KeyType keyType =
121 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
122 // Generate non-default certificate.
123 if (keyType != rtc::KT_DEFAULT) {
124 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
125 rtc::RTCCertificateGenerator::GenerateCertificate(
126 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
127 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700128 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200129 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800130 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200131 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800132 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700133 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
honghaizaf6b6e02016-07-11 15:09:26 -0700134 nativeConfig->prune_turn_ports = _shouldPruneTurnPorts ? true : false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700135 nativeConfig->presume_writable_when_fully_relayed =
honghaizaf6b6e02016-07-11 15:09:26 -0700136 _shouldPresumeWritableWhenFullyRelayed ? true : false;
hjon6d49a8e2016-01-26 13:06:42 -0800137
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200138 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800139}
140
hjon6d49a8e2016-01-26 13:06:42 -0800141+ (webrtc::PeerConnectionInterface::IceTransportsType)
142 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
143 switch (policy) {
144 case RTCIceTransportPolicyNone:
145 return webrtc::PeerConnectionInterface::kNone;
146 case RTCIceTransportPolicyRelay:
147 return webrtc::PeerConnectionInterface::kRelay;
148 case RTCIceTransportPolicyNoHost:
149 return webrtc::PeerConnectionInterface::kNoHost;
150 case RTCIceTransportPolicyAll:
151 return webrtc::PeerConnectionInterface::kAll;
152 }
153}
154
155+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
156 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
157 switch (nativeType) {
158 case webrtc::PeerConnectionInterface::kNone:
159 return RTCIceTransportPolicyNone;
160 case webrtc::PeerConnectionInterface::kRelay:
161 return RTCIceTransportPolicyRelay;
162 case webrtc::PeerConnectionInterface::kNoHost:
163 return RTCIceTransportPolicyNoHost;
164 case webrtc::PeerConnectionInterface::kAll:
165 return RTCIceTransportPolicyAll;
166 }
167}
168
169+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
170 switch (policy) {
171 case RTCIceTransportPolicyNone:
172 return @"NONE";
173 case RTCIceTransportPolicyRelay:
174 return @"RELAY";
175 case RTCIceTransportPolicyNoHost:
176 return @"NO_HOST";
177 case RTCIceTransportPolicyAll:
178 return @"ALL";
179 }
180}
181
182+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
183 (RTCBundlePolicy)policy {
184 switch (policy) {
185 case RTCBundlePolicyBalanced:
186 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
187 case RTCBundlePolicyMaxCompat:
188 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
189 case RTCBundlePolicyMaxBundle:
190 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
191 }
192}
193
194+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
195 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
196 switch (nativePolicy) {
197 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
198 return RTCBundlePolicyBalanced;
199 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
200 return RTCBundlePolicyMaxCompat;
201 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
202 return RTCBundlePolicyMaxBundle;
203 }
204}
205
206+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
207 switch (policy) {
208 case RTCBundlePolicyBalanced:
209 return @"BALANCED";
210 case RTCBundlePolicyMaxCompat:
211 return @"MAX_COMPAT";
212 case RTCBundlePolicyMaxBundle:
213 return @"MAX_BUNDLE";
214 }
215}
216
217+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
218 (RTCRtcpMuxPolicy)policy {
219 switch (policy) {
220 case RTCRtcpMuxPolicyNegotiate:
221 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
222 case RTCRtcpMuxPolicyRequire:
223 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
224 }
225}
226
227+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
228 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
229 switch (nativePolicy) {
230 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
231 return RTCRtcpMuxPolicyNegotiate;
232 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
233 return RTCRtcpMuxPolicyRequire;
234 }
235}
236
237+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
238 switch (policy) {
239 case RTCRtcpMuxPolicyNegotiate:
240 return @"NEGOTIATE";
241 case RTCRtcpMuxPolicyRequire:
242 return @"REQUIRE";
243 }
244}
245
246+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
247 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
248 switch (policy) {
249 case RTCTcpCandidatePolicyEnabled:
250 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
251 case RTCTcpCandidatePolicyDisabled:
252 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
253 }
254}
255
Honghai Zhang46007ae2016-06-03 16:31:32 -0700256+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
257 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
258 switch (policy) {
259 case RTCCandidateNetworkPolicyAll:
260 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
261 case RTCCandidateNetworkPolicyLowCost:
262 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
263 }
264}
265
hjon6d49a8e2016-01-26 13:06:42 -0800266+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
267 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
268 switch (nativePolicy) {
269 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
270 return RTCTcpCandidatePolicyEnabled;
271 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
272 return RTCTcpCandidatePolicyDisabled;
273 }
274}
275
276+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
277 switch (policy) {
278 case RTCTcpCandidatePolicyEnabled:
279 return @"TCP_ENABLED";
280 case RTCTcpCandidatePolicyDisabled:
281 return @"TCP_DISABLED";
282 }
283}
284
Honghai Zhang46007ae2016-06-03 16:31:32 -0700285+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
286 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
287 switch (nativePolicy) {
288 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
289 return RTCCandidateNetworkPolicyAll;
290 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
291 return RTCCandidateNetworkPolicyLowCost;
292 }
293}
294
295+ (NSString *)stringForCandidateNetworkPolicy:
296 (RTCCandidateNetworkPolicy)policy {
297 switch (policy) {
298 case RTCCandidateNetworkPolicyAll:
299 return @"CANDIDATE_ALL_NETWORKS";
300 case RTCCandidateNetworkPolicyLowCost:
301 return @"CANDIDATE_LOW_COST_NETWORKS";
302 }
303}
304
Honghai Zhang3108fc92016-05-11 10:10:39 -0700305+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
306 nativeContinualGatheringPolicyForPolicy:
307 (RTCContinualGatheringPolicy)policy {
308 switch (policy) {
309 case RTCContinualGatheringPolicyGatherOnce:
310 return webrtc::PeerConnectionInterface::GATHER_ONCE;
311 case RTCContinualGatheringPolicyGatherContinually:
312 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
313 }
314}
315
316+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
317 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
318 switch (nativePolicy) {
319 case webrtc::PeerConnectionInterface::GATHER_ONCE:
320 return RTCContinualGatheringPolicyGatherOnce;
321 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
322 return RTCContinualGatheringPolicyGatherContinually;
323 }
324}
325
326+ (NSString *)stringForContinualGatheringPolicy:
327 (RTCContinualGatheringPolicy)policy {
328 switch (policy) {
329 case RTCContinualGatheringPolicyGatherOnce:
330 return @"GATHER_ONCE";
331 case RTCContinualGatheringPolicyGatherContinually:
332 return @"GATHER_CONTINUALLY";
333 }
334}
335
hbosf9da44d2016-06-09 03:18:28 -0700336+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
337 (RTCEncryptionKeyType)keyType {
338 switch (keyType) {
339 case RTCEncryptionKeyTypeRSA:
340 return rtc::KT_RSA;
341 case RTCEncryptionKeyTypeECDSA:
342 return rtc::KT_ECDSA;
343 }
344}
345
hjon6d49a8e2016-01-26 13:06:42 -0800346@end