blob: 2a11f2d316a74f4ad43b4b1cdd9e5467bb517977 [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;
Taylor Brandstettere9851112016-07-01 11:11:13 -070036@synthesize presumeWritableWhenFullyRelayed = _presumeWritableWhenFullyRelayed;
hjon6d49a8e2016-01-26 13:06:42 -080037
38- (instancetype)init {
39 if (self = [super init]) {
40 _iceServers = [NSMutableArray array];
41 // Copy defaults.
42 webrtc::PeerConnectionInterface::RTCConfiguration config;
43 _iceTransportPolicy =
44 [[self class] transportPolicyForTransportsType:config.type];
45 _bundlePolicy =
46 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
47 _rtcpMuxPolicy =
48 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
49 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
50 config.tcp_candidate_policy];
Honghai Zhang46007ae2016-06-03 16:31:32 -070051 _candidateNetworkPolicy = [[self class]
52 candidateNetworkPolicyForNativePolicy:config.candidate_network_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070053 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
54 config.continual_gathering_policy;
55 _continualGatheringPolicy =
56 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080057 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
58 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
59 _iceBackupCandidatePairPingInterval =
60 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080061 _keyType = RTCEncryptionKeyTypeECDSA;
deadbeefbe0c96f2016-05-18 16:20:14 -070062 _iceCandidatePoolSize = config.ice_candidate_pool_size;
Taylor Brandstettere9851112016-07-01 11:11:13 -070063 _presumeWritableWhenFullyRelayed =
64 config.presume_writable_when_fully_relayed;
hjon6d49a8e2016-01-26 13:06:42 -080065 }
66 return self;
67}
68
69- (NSString *)description {
70 return [NSString stringWithFormat:
Taylor Brandstettere9851112016-07-01 11:11:13 -070071 @"RTCConfiguration: {\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%d\n%d\n%d\n%d\n%d\n}\n",
hjon6d49a8e2016-01-26 13:06:42 -080072 _iceServers,
73 [[self class] stringForTransportPolicy:_iceTransportPolicy],
74 [[self class] stringForBundlePolicy:_bundlePolicy],
75 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
76 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang46007ae2016-06-03 16:31:32 -070077 [[self class] stringForCandidateNetworkPolicy:_candidateNetworkPolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070078 [[self class]
79 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080080 _audioJitterBufferMaxPackets,
81 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -070082 _iceBackupCandidatePairPingInterval,
Taylor Brandstettere9851112016-07-01 11:11:13 -070083 _iceCandidatePoolSize,
84 _presumeWritableWhenFullyRelayed];
hjon6d49a8e2016-01-26 13:06:42 -080085}
86
87#pragma mark - Private
88
hbosa73ca562016-05-17 03:28:58 -070089- (webrtc::PeerConnectionInterface::RTCConfiguration *)
90 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020091 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
92 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration());
hjon6d49a8e2016-01-26 13:06:42 -080093
94 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020095 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -080096 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +020097 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -080098 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020099 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800100 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200101 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800102 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200103 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -0800104 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Honghai Zhang46007ae2016-06-03 16:31:32 -0700105 nativeConfig->candidate_network_policy = [[self class]
106 nativeCandidateNetworkPolicyForPolicy:_candidateNetworkPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200107 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -0700108 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200109 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
110 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800111 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200112 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800113 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200114 rtc::KeyType keyType =
115 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
116 // Generate non-default certificate.
117 if (keyType != rtc::KT_DEFAULT) {
118 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
119 rtc::RTCCertificateGenerator::GenerateCertificate(
120 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
121 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700122 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200123 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800124 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200125 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800126 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700127 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700128 nativeConfig->presume_writable_when_fully_relayed =
129 _presumeWritableWhenFullyRelayed;
hjon6d49a8e2016-01-26 13:06:42 -0800130
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200131 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800132}
133
hjon6d49a8e2016-01-26 13:06:42 -0800134+ (webrtc::PeerConnectionInterface::IceTransportsType)
135 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
136 switch (policy) {
137 case RTCIceTransportPolicyNone:
138 return webrtc::PeerConnectionInterface::kNone;
139 case RTCIceTransportPolicyRelay:
140 return webrtc::PeerConnectionInterface::kRelay;
141 case RTCIceTransportPolicyNoHost:
142 return webrtc::PeerConnectionInterface::kNoHost;
143 case RTCIceTransportPolicyAll:
144 return webrtc::PeerConnectionInterface::kAll;
145 }
146}
147
148+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
149 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
150 switch (nativeType) {
151 case webrtc::PeerConnectionInterface::kNone:
152 return RTCIceTransportPolicyNone;
153 case webrtc::PeerConnectionInterface::kRelay:
154 return RTCIceTransportPolicyRelay;
155 case webrtc::PeerConnectionInterface::kNoHost:
156 return RTCIceTransportPolicyNoHost;
157 case webrtc::PeerConnectionInterface::kAll:
158 return RTCIceTransportPolicyAll;
159 }
160}
161
162+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
163 switch (policy) {
164 case RTCIceTransportPolicyNone:
165 return @"NONE";
166 case RTCIceTransportPolicyRelay:
167 return @"RELAY";
168 case RTCIceTransportPolicyNoHost:
169 return @"NO_HOST";
170 case RTCIceTransportPolicyAll:
171 return @"ALL";
172 }
173}
174
175+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
176 (RTCBundlePolicy)policy {
177 switch (policy) {
178 case RTCBundlePolicyBalanced:
179 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
180 case RTCBundlePolicyMaxCompat:
181 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
182 case RTCBundlePolicyMaxBundle:
183 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
184 }
185}
186
187+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
188 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
189 switch (nativePolicy) {
190 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
191 return RTCBundlePolicyBalanced;
192 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
193 return RTCBundlePolicyMaxCompat;
194 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
195 return RTCBundlePolicyMaxBundle;
196 }
197}
198
199+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
200 switch (policy) {
201 case RTCBundlePolicyBalanced:
202 return @"BALANCED";
203 case RTCBundlePolicyMaxCompat:
204 return @"MAX_COMPAT";
205 case RTCBundlePolicyMaxBundle:
206 return @"MAX_BUNDLE";
207 }
208}
209
210+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
211 (RTCRtcpMuxPolicy)policy {
212 switch (policy) {
213 case RTCRtcpMuxPolicyNegotiate:
214 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
215 case RTCRtcpMuxPolicyRequire:
216 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
217 }
218}
219
220+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
221 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
222 switch (nativePolicy) {
223 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
224 return RTCRtcpMuxPolicyNegotiate;
225 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
226 return RTCRtcpMuxPolicyRequire;
227 }
228}
229
230+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
231 switch (policy) {
232 case RTCRtcpMuxPolicyNegotiate:
233 return @"NEGOTIATE";
234 case RTCRtcpMuxPolicyRequire:
235 return @"REQUIRE";
236 }
237}
238
239+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
240 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
241 switch (policy) {
242 case RTCTcpCandidatePolicyEnabled:
243 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
244 case RTCTcpCandidatePolicyDisabled:
245 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
246 }
247}
248
Honghai Zhang46007ae2016-06-03 16:31:32 -0700249+ (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)
250 nativeCandidateNetworkPolicyForPolicy:(RTCCandidateNetworkPolicy)policy {
251 switch (policy) {
252 case RTCCandidateNetworkPolicyAll:
253 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll;
254 case RTCCandidateNetworkPolicyLowCost:
255 return webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
256 }
257}
258
hjon6d49a8e2016-01-26 13:06:42 -0800259+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
260 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
261 switch (nativePolicy) {
262 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
263 return RTCTcpCandidatePolicyEnabled;
264 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
265 return RTCTcpCandidatePolicyDisabled;
266 }
267}
268
269+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
270 switch (policy) {
271 case RTCTcpCandidatePolicyEnabled:
272 return @"TCP_ENABLED";
273 case RTCTcpCandidatePolicyDisabled:
274 return @"TCP_DISABLED";
275 }
276}
277
Honghai Zhang46007ae2016-06-03 16:31:32 -0700278+ (RTCCandidateNetworkPolicy)candidateNetworkPolicyForNativePolicy:
279 (webrtc::PeerConnectionInterface::CandidateNetworkPolicy)nativePolicy {
280 switch (nativePolicy) {
281 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyAll:
282 return RTCCandidateNetworkPolicyAll;
283 case webrtc::PeerConnectionInterface::kCandidateNetworkPolicyLowCost:
284 return RTCCandidateNetworkPolicyLowCost;
285 }
286}
287
288+ (NSString *)stringForCandidateNetworkPolicy:
289 (RTCCandidateNetworkPolicy)policy {
290 switch (policy) {
291 case RTCCandidateNetworkPolicyAll:
292 return @"CANDIDATE_ALL_NETWORKS";
293 case RTCCandidateNetworkPolicyLowCost:
294 return @"CANDIDATE_LOW_COST_NETWORKS";
295 }
296}
297
Honghai Zhang3108fc92016-05-11 10:10:39 -0700298+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
299 nativeContinualGatheringPolicyForPolicy:
300 (RTCContinualGatheringPolicy)policy {
301 switch (policy) {
302 case RTCContinualGatheringPolicyGatherOnce:
303 return webrtc::PeerConnectionInterface::GATHER_ONCE;
304 case RTCContinualGatheringPolicyGatherContinually:
305 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
306 }
307}
308
309+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
310 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
311 switch (nativePolicy) {
312 case webrtc::PeerConnectionInterface::GATHER_ONCE:
313 return RTCContinualGatheringPolicyGatherOnce;
314 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
315 return RTCContinualGatheringPolicyGatherContinually;
316 }
317}
318
319+ (NSString *)stringForContinualGatheringPolicy:
320 (RTCContinualGatheringPolicy)policy {
321 switch (policy) {
322 case RTCContinualGatheringPolicyGatherOnce:
323 return @"GATHER_ONCE";
324 case RTCContinualGatheringPolicyGatherContinually:
325 return @"GATHER_CONTINUALLY";
326 }
327}
328
hbosf9da44d2016-06-09 03:18:28 -0700329+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
330 (RTCEncryptionKeyType)keyType {
331 switch (keyType) {
332 case RTCEncryptionKeyTypeRSA:
333 return rtc::KT_RSA;
334 case RTCEncryptionKeyTypeECDSA:
335 return rtc::KT_ECDSA;
336 }
337}
338
hjon6d49a8e2016-01-26 13:06:42 -0800339@end