blob: 74b5c5df83942ad13705cfd6f1cb5553b7bb9faf [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;
Honghai Zhange2e35ca2016-07-01 14:22:17 -070036@synthesize pruneTurnPorts = _pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -070037@synthesize presumeWritableWhenFullyRelayed = _presumeWritableWhenFullyRelayed;
hjon6d49a8e2016-01-26 13:06:42 -080038
39- (instancetype)init {
40 if (self = [super init]) {
41 _iceServers = [NSMutableArray array];
42 // Copy defaults.
43 webrtc::PeerConnectionInterface::RTCConfiguration config;
44 _iceTransportPolicy =
45 [[self class] transportPolicyForTransportsType:config.type];
46 _bundlePolicy =
47 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
48 _rtcpMuxPolicy =
49 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
50 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
51 config.tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070052 _candidateNetworkPolicy = [[self class]
53 candidateNetworkPolicyForNativePolicy:config.candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070054 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
55 config.continual_gathering_policy;
56 _continualGatheringPolicy =
57 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080058 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
59 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
60 _iceBackupCandidatePairPingInterval =
61 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080062 _keyType = RTCEncryptionKeyTypeECDSA;
deadbeefbe0c96f2016-05-18 16:20:14 -070063 _iceCandidatePoolSize = config.ice_candidate_pool_size;
Honghai Zhange2e35ca2016-07-01 14:22:17 -070064 _pruneTurnPorts = config.prune_turn_ports;
Taylor Brandstettere9851112016-07-01 11:11:13 -070065 _presumeWritableWhenFullyRelayed =
66 config.presume_writable_when_fully_relayed;
hjon6d49a8e2016-01-26 13:06:42 -080067 }
68 return self;
69}
70
71- (NSString *)description {
72 return [NSString stringWithFormat:
Honghai Zhange2e35ca2016-07-01 14:22:17 -070073 @"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 -080074 _iceServers,
75 [[self class] stringForTransportPolicy:_iceTransportPolicy],
76 [[self class] stringForBundlePolicy:_bundlePolicy],
77 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
78 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070079 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070080 [[self class]
81 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080082 _audioJitterBufferMaxPackets,
83 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -070084 _iceBackupCandidatePairPingInterval,
Taylor Brandstettere9851112016-07-01 11:11:13 -070085 _iceCandidatePoolSize,
Honghai Zhange2e35ca2016-07-01 14:22:17 -070086 _pruneTurnPorts,
Taylor Brandstettere9851112016-07-01 11:11:13 -070087 _presumeWritableWhenFullyRelayed];
hjon6d49a8e2016-01-26 13:06:42 -080088}
89
90#pragma mark - Private
91
hbosa73ca562016-05-17 03:28:58 -070092- (webrtc::PeerConnectionInterface::RTCConfiguration *)
93 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020094 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
95 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration());
hjon6d49a8e2016-01-26 13:06:42 -080096
97 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020098 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -080099 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200100 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -0800101 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200102 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800103 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200104 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800105 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200106 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800107 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700108 nativeConfig->candidate_network_policy = [[self class]
109 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200110 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700111 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200112 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
113 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800114 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200115 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800116 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200117 rtc::KeyType keyType =
118 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
119 // Generate non-default certificate.
120 if (keyType != rtc::KT_DEFAULT) {
121 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
122 rtc::RTCCertificateGenerator::GenerateCertificate(
123 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
124 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700125 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200126 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800127 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200128 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800129 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700130 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
Honghai Zhange2e35ca2016-07-01 14:22:17 -0700131 nativeConfig->prune_turn_ports = _pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700132 nativeConfig->presume_writable_when_fully_relayed =
133 _presumeWritableWhenFullyRelayed;
hjon6d49a8e2016-01-26 13:06:42 -0800134
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200135 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800136}
137
hjon6d49a8e2016-01-26 13:06:42 -0800138+ (webrtc::PeerConnectionInterface::IceTransportsType)
139 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
140 switch (policy) {
141 case RTCIceTransportPolicyNone:
142 return webrtc::PeerConnectionInterface::kNone;
143 case RTCIceTransportPolicyRelay:
144 return webrtc::PeerConnectionInterface::kRelay;
145 case RTCIceTransportPolicyNoHost:
146 return webrtc::PeerConnectionInterface::kNoHost;
147 case RTCIceTransportPolicyAll:
148 return webrtc::PeerConnectionInterface::kAll;
149 }
150}
151
152+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
153 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
154 switch (nativeType) {
155 case webrtc::PeerConnectionInterface::kNone:
156 return RTCIceTransportPolicyNone;
157 case webrtc::PeerConnectionInterface::kRelay:
158 return RTCIceTransportPolicyRelay;
159 case webrtc::PeerConnectionInterface::kNoHost:
160 return RTCIceTransportPolicyNoHost;
161 case webrtc::PeerConnectionInterface::kAll:
162 return RTCIceTransportPolicyAll;
163 }
164}
165
166+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
167 switch (policy) {
168 case RTCIceTransportPolicyNone:
169 return @"NONE";
170 case RTCIceTransportPolicyRelay:
171 return @"RELAY";
172 case RTCIceTransportPolicyNoHost:
173 return @"NO_HOST";
174 case RTCIceTransportPolicyAll:
175 return @"ALL";
176 }
177}
178
179+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
180 (RTCBundlePolicy)policy {
181 switch (policy) {
182 case RTCBundlePolicyBalanced:
183 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
184 case RTCBundlePolicyMaxCompat:
185 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
186 case RTCBundlePolicyMaxBundle:
187 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
188 }
189}
190
191+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
192 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
193 switch (nativePolicy) {
194 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
195 return RTCBundlePolicyBalanced;
196 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
197 return RTCBundlePolicyMaxCompat;
198 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
199 return RTCBundlePolicyMaxBundle;
200 }
201}
202
203+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
204 switch (policy) {
205 case RTCBundlePolicyBalanced:
206 return @"BALANCED";
207 case RTCBundlePolicyMaxCompat:
208 return @"MAX_COMPAT";
209 case RTCBundlePolicyMaxBundle:
210 return @"MAX_BUNDLE";
211 }
212}
213
214+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
215 (RTCRtcpMuxPolicy)policy {
216 switch (policy) {
217 case RTCRtcpMuxPolicyNegotiate:
218 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
219 case RTCRtcpMuxPolicyRequire:
220 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
221 }
222}
223
224+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
225 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
226 switch (nativePolicy) {
227 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
228 return RTCRtcpMuxPolicyNegotiate;
229 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
230 return RTCRtcpMuxPolicyRequire;
231 }
232}
233
234+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
235 switch (policy) {
236 case RTCRtcpMuxPolicyNegotiate:
237 return @"NEGOTIATE";
238 case RTCRtcpMuxPolicyRequire:
239 return @"REQUIRE";
240 }
241}
242
243+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
244 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
245 switch (policy) {
246 case RTCTcpCandidatePolicyEnabled:
247 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
248 case RTCTcpCandidatePolicyDisabled:
249 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
250 }
251}
252
Honghai Zhang46007ae2016-06-03 16:31:32 -0700253+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
254 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
255 switch (policy) {
256 case RTCCandidateNetworkPolicyAll:
257 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
258 case RTCCandidateNetworkPolicyLowCost:
259 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
260 }
261}
262
hjon6d49a8e2016-01-26 13:06:42 -0800263+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
264 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
265 switch (nativePolicy) {
266 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
267 return RTCTcpCandidatePolicyEnabled;
268 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
269 return RTCTcpCandidatePolicyDisabled;
270 }
271}
272
273+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
274 switch (policy) {
275 case RTCTcpCandidatePolicyEnabled:
276 return @"TCP_ENABLED";
277 case RTCTcpCandidatePolicyDisabled:
278 return @"TCP_DISABLED";
279 }
280}
281
Honghai Zhang46007ae2016-06-03 16:31:32 -0700282+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
283 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
284 switch (nativePolicy) {
285 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
286 return RTCCandidateNetworkPolicyAll;
287 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
288 return RTCCandidateNetworkPolicyLowCost;
289 }
290}
291
292+ (NSString *)stringForCandidateNetworkPolicy:
293 (RTCCandidateNetworkPolicy)policy {
294 switch (policy) {
295 case RTCCandidateNetworkPolicyAll:
296 return @"CANDIDATE_ALL_NETWORKS";
297 case RTCCandidateNetworkPolicyLowCost:
298 return @"CANDIDATE_LOW_COST_NETWORKS";
299 }
300}
301
Honghai Zhang3108fc92016-05-11 10:10:39 -0700302+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
303 nativeContinualGatheringPolicyForPolicy:
304 (RTCContinualGatheringPolicy)policy {
305 switch (policy) {
306 case RTCContinualGatheringPolicyGatherOnce:
307 return webrtc::PeerConnectionInterface::GATHER_ONCE;
308 case RTCContinualGatheringPolicyGatherContinually:
309 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
310 }
311}
312
313+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
314 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
315 switch (nativePolicy) {
316 case webrtc::PeerConnectionInterface::GATHER_ONCE:
317 return RTCContinualGatheringPolicyGatherOnce;
318 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
319 return RTCContinualGatheringPolicyGatherContinually;
320 }
321}
322
323+ (NSString *)stringForContinualGatheringPolicy:
324 (RTCContinualGatheringPolicy)policy {
325 switch (policy) {
326 case RTCContinualGatheringPolicyGatherOnce:
327 return @"GATHER_ONCE";
328 case RTCContinualGatheringPolicyGatherContinually:
329 return @"GATHER_CONTINUALLY";
330 }
331}
332
hbosf9da44d2016-06-09 03:18:28 -0700333+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
334 (RTCEncryptionKeyType)keyType {
335 switch (keyType) {
336 case RTCEncryptionKeyTypeRSA:
337 return rtc::KT_RSA;
338 case RTCEncryptionKeyTypeECDSA:
339 return rtc::KT_ECDSA;
340 }
341}
342
hjon6d49a8e2016-01-26 13:06:42 -0800343@end