blob: 0ea17e0d9c196f2372b233b4e643253954d74166 [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 Zhang3108fc92016-05-11 10:10:39 -070028@synthesize continualGatheringPolicy = _continualGatheringPolicy;
hjon6d49a8e2016-01-26 13:06:42 -080029@synthesize audioJitterBufferMaxPackets = _audioJitterBufferMaxPackets;
30@synthesize iceConnectionReceivingTimeout = _iceConnectionReceivingTimeout;
31@synthesize iceBackupCandidatePairPingInterval =
32 _iceBackupCandidatePairPingInterval;
tkchinab8f82f2016-01-27 17:50:11 -080033@synthesize keyType = _keyType;
deadbeefbe0c96f2016-05-18 16:20:14 -070034@synthesize iceCandidatePoolSize = _iceCandidatePoolSize;
hjon6d49a8e2016-01-26 13:06:42 -080035
36- (instancetype)init {
37 if (self = [super init]) {
38 _iceServers = [NSMutableArray array];
39 // Copy defaults.
40 webrtc::PeerConnectionInterface::RTCConfiguration config;
41 _iceTransportPolicy =
42 [[self class] transportPolicyForTransportsType:config.type];
43 _bundlePolicy =
44 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
45 _rtcpMuxPolicy =
46 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
47 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
48 config.tcp_candidate_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070049 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
50 config.continual_gathering_policy;
51 _continualGatheringPolicy =
52 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080053 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
54 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
55 _iceBackupCandidatePairPingInterval =
56 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080057 _keyType = RTCEncryptionKeyTypeECDSA;
deadbeefbe0c96f2016-05-18 16:20:14 -070058 _iceCandidatePoolSize = config.ice_candidate_pool_size;
hjon6d49a8e2016-01-26 13:06:42 -080059 }
60 return self;
61}
62
63- (NSString *)description {
64 return [NSString stringWithFormat:
deadbeefbe0c96f2016-05-18 16:20:14 -070065 @"RTCConfiguration: {\n%@\n%@\n%@\n%@\n%@\n%@\n%d\n%d\n%d\n%d\n}\n",
hjon6d49a8e2016-01-26 13:06:42 -080066 _iceServers,
67 [[self class] stringForTransportPolicy:_iceTransportPolicy],
68 [[self class] stringForBundlePolicy:_bundlePolicy],
69 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
70 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070071 [[self class]
72 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080073 _audioJitterBufferMaxPackets,
74 _iceConnectionReceivingTimeout,
deadbeefbe0c96f2016-05-18 16:20:14 -070075 _iceBackupCandidatePairPingInterval,
76 _iceCandidatePoolSize];
hjon6d49a8e2016-01-26 13:06:42 -080077}
78
79#pragma mark - Private
80
hbosa73ca562016-05-17 03:28:58 -070081- (webrtc::PeerConnectionInterface::RTCConfiguration *)
82 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020083 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
84 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration());
hjon6d49a8e2016-01-26 13:06:42 -080085
86 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020087 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -080088 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +020089 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -080090 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020091 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -080092 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020093 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -080094 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020095 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -080096 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020097 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -070098 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020099 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
100 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -0800101 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200102 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800103 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200104 rtc::KeyType keyType =
105 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
106 // Generate non-default certificate.
107 if (keyType != rtc::KT_DEFAULT) {
108 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
109 rtc::RTCCertificateGenerator::GenerateCertificate(
110 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
111 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700112 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200113 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800114 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200115 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800116 }
deadbeefbe0c96f2016-05-18 16:20:14 -0700117 nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize;
hjon6d49a8e2016-01-26 13:06:42 -0800118
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200119 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800120}
121
hjon6d49a8e2016-01-26 13:06:42 -0800122+ (webrtc::PeerConnectionInterface::IceTransportsType)
123 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
124 switch (policy) {
125 case RTCIceTransportPolicyNone:
126 return webrtc::PeerConnectionInterface::kNone;
127 case RTCIceTransportPolicyRelay:
128 return webrtc::PeerConnectionInterface::kRelay;
129 case RTCIceTransportPolicyNoHost:
130 return webrtc::PeerConnectionInterface::kNoHost;
131 case RTCIceTransportPolicyAll:
132 return webrtc::PeerConnectionInterface::kAll;
133 }
134}
135
136+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
137 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
138 switch (nativeType) {
139 case webrtc::PeerConnectionInterface::kNone:
140 return RTCIceTransportPolicyNone;
141 case webrtc::PeerConnectionInterface::kRelay:
142 return RTCIceTransportPolicyRelay;
143 case webrtc::PeerConnectionInterface::kNoHost:
144 return RTCIceTransportPolicyNoHost;
145 case webrtc::PeerConnectionInterface::kAll:
146 return RTCIceTransportPolicyAll;
147 }
148}
149
150+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
151 switch (policy) {
152 case RTCIceTransportPolicyNone:
153 return @"NONE";
154 case RTCIceTransportPolicyRelay:
155 return @"RELAY";
156 case RTCIceTransportPolicyNoHost:
157 return @"NO_HOST";
158 case RTCIceTransportPolicyAll:
159 return @"ALL";
160 }
161}
162
163+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
164 (RTCBundlePolicy)policy {
165 switch (policy) {
166 case RTCBundlePolicyBalanced:
167 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
168 case RTCBundlePolicyMaxCompat:
169 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
170 case RTCBundlePolicyMaxBundle:
171 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
172 }
173}
174
175+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
176 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
177 switch (nativePolicy) {
178 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
179 return RTCBundlePolicyBalanced;
180 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
181 return RTCBundlePolicyMaxCompat;
182 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
183 return RTCBundlePolicyMaxBundle;
184 }
185}
186
187+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
188 switch (policy) {
189 case RTCBundlePolicyBalanced:
190 return @"BALANCED";
191 case RTCBundlePolicyMaxCompat:
192 return @"MAX_COMPAT";
193 case RTCBundlePolicyMaxBundle:
194 return @"MAX_BUNDLE";
195 }
196}
197
198+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
199 (RTCRtcpMuxPolicy)policy {
200 switch (policy) {
201 case RTCRtcpMuxPolicyNegotiate:
202 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
203 case RTCRtcpMuxPolicyRequire:
204 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
205 }
206}
207
208+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
209 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
210 switch (nativePolicy) {
211 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
212 return RTCRtcpMuxPolicyNegotiate;
213 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
214 return RTCRtcpMuxPolicyRequire;
215 }
216}
217
218+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
219 switch (policy) {
220 case RTCRtcpMuxPolicyNegotiate:
221 return @"NEGOTIATE";
222 case RTCRtcpMuxPolicyRequire:
223 return @"REQUIRE";
224 }
225}
226
227+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
228 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
229 switch (policy) {
230 case RTCTcpCandidatePolicyEnabled:
231 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
232 case RTCTcpCandidatePolicyDisabled:
233 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
234 }
235}
236
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200237+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
238 (RTCEncryptionKeyType)keyType {
239 switch (keyType) {
240 case RTCEncryptionKeyTypeRSA:
241 return rtc::KT_RSA;
242 case RTCEncryptionKeyTypeECDSA:
243 return rtc::KT_ECDSA;
244 }
245}
246
hjon6d49a8e2016-01-26 13:06:42 -0800247+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
248 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
249 switch (nativePolicy) {
250 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
251 return RTCTcpCandidatePolicyEnabled;
252 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
253 return RTCTcpCandidatePolicyDisabled;
254 }
255}
256
257+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
258 switch (policy) {
259 case RTCTcpCandidatePolicyEnabled:
260 return @"TCP_ENABLED";
261 case RTCTcpCandidatePolicyDisabled:
262 return @"TCP_DISABLED";
263 }
264}
265
Honghai Zhang3108fc92016-05-11 10:10:39 -0700266+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
267 nativeContinualGatheringPolicyForPolicy:
268 (RTCContinualGatheringPolicy)policy {
269 switch (policy) {
270 case RTCContinualGatheringPolicyGatherOnce:
271 return webrtc::PeerConnectionInterface::GATHER_ONCE;
272 case RTCContinualGatheringPolicyGatherContinually:
273 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
274 }
275}
276
277+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
278 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
279 switch (nativePolicy) {
280 case webrtc::PeerConnectionInterface::GATHER_ONCE:
281 return RTCContinualGatheringPolicyGatherOnce;
282 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
283 return RTCContinualGatheringPolicyGatherContinually;
284 }
285}
286
287+ (NSString *)stringForContinualGatheringPolicy:
288 (RTCContinualGatheringPolicy)policy {
289 switch (policy) {
290 case RTCContinualGatheringPolicyGatherOnce:
291 return @"GATHER_ONCE";
292 case RTCContinualGatheringPolicyGatherContinually:
293 return @"GATHER_CONTINUALLY";
294 }
295}
296
hjon6d49a8e2016-01-26 13:06:42 -0800297@end