blob: 0a63f69fd9819efb4cd1945c63b290e8eaa057a1 [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;
hjon6d49a8e2016-01-26 13:06:42 -080034
35- (instancetype)init {
36 if (self = [super init]) {
37 _iceServers = [NSMutableArray array];
38 // Copy defaults.
39 webrtc::PeerConnectionInterface::RTCConfiguration config;
40 _iceTransportPolicy =
41 [[self class] transportPolicyForTransportsType:config.type];
42 _bundlePolicy =
43 [[self class] bundlePolicyForNativePolicy:config.bundle_policy];
44 _rtcpMuxPolicy =
45 [[self class] rtcpMuxPolicyForNativePolicy:config.rtcp_mux_policy];
46 _tcpCandidatePolicy = [[self class] tcpCandidatePolicyForNativePolicy:
47 config.tcp_candidate_policy];
Honghai Zhang3108fc92016-05-11 10:10:39 -070048 webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy =
49 config.continual_gathering_policy;
50 _continualGatheringPolicy =
51 [[self class] continualGatheringPolicyForNativePolicy:nativePolicy];
hjon6d49a8e2016-01-26 13:06:42 -080052 _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets;
53 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
54 _iceBackupCandidatePairPingInterval =
55 config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080056 _keyType = RTCEncryptionKeyTypeECDSA;
hjon6d49a8e2016-01-26 13:06:42 -080057 }
58 return self;
59}
60
61- (NSString *)description {
62 return [NSString stringWithFormat:
Honghai Zhang3108fc92016-05-11 10:10:39 -070063 @"RTCConfiguration: {\n%@\n%@\n%@\n%@\n%@\n%@\n%d\n%d\n%d\n}\n",
hjon6d49a8e2016-01-26 13:06:42 -080064 _iceServers,
65 [[self class] stringForTransportPolicy:_iceTransportPolicy],
66 [[self class] stringForBundlePolicy:_bundlePolicy],
67 [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy],
68 [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy],
Honghai Zhang3108fc92016-05-11 10:10:39 -070069 [[self class]
70 stringForContinualGatheringPolicy:_continualGatheringPolicy],
hjon6d49a8e2016-01-26 13:06:42 -080071 _audioJitterBufferMaxPackets,
72 _iceConnectionReceivingTimeout,
73 _iceBackupCandidatePairPingInterval];
74}
75
76#pragma mark - Private
77
hbosa73ca562016-05-17 03:28:58 -070078- (webrtc::PeerConnectionInterface::RTCConfiguration *)
79 createNativeConfiguration {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020080 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
81 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration());
hjon6d49a8e2016-01-26 13:06:42 -080082
83 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020084 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -080085 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +020086 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -080087 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020088 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -080089 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020090 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -080091 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020092 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -080093 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020094 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -070095 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020096 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
97 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -080098 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +020099 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -0800100 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200101 rtc::KeyType keyType =
102 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
103 // Generate non-default certificate.
104 if (keyType != rtc::KT_DEFAULT) {
105 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
106 rtc::RTCCertificateGenerator::GenerateCertificate(
107 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
108 if (!certificate) {
hbosa73ca562016-05-17 03:28:58 -0700109 RTCLogError(@"Failed to generate certificate.");
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200110 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800111 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200112 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800113 }
hjon6d49a8e2016-01-26 13:06:42 -0800114
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200115 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800116}
117
hjon6d49a8e2016-01-26 13:06:42 -0800118+ (webrtc::PeerConnectionInterface::IceTransportsType)
119 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
120 switch (policy) {
121 case RTCIceTransportPolicyNone:
122 return webrtc::PeerConnectionInterface::kNone;
123 case RTCIceTransportPolicyRelay:
124 return webrtc::PeerConnectionInterface::kRelay;
125 case RTCIceTransportPolicyNoHost:
126 return webrtc::PeerConnectionInterface::kNoHost;
127 case RTCIceTransportPolicyAll:
128 return webrtc::PeerConnectionInterface::kAll;
129 }
130}
131
132+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
133 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
134 switch (nativeType) {
135 case webrtc::PeerConnectionInterface::kNone:
136 return RTCIceTransportPolicyNone;
137 case webrtc::PeerConnectionInterface::kRelay:
138 return RTCIceTransportPolicyRelay;
139 case webrtc::PeerConnectionInterface::kNoHost:
140 return RTCIceTransportPolicyNoHost;
141 case webrtc::PeerConnectionInterface::kAll:
142 return RTCIceTransportPolicyAll;
143 }
144}
145
146+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
147 switch (policy) {
148 case RTCIceTransportPolicyNone:
149 return @"NONE";
150 case RTCIceTransportPolicyRelay:
151 return @"RELAY";
152 case RTCIceTransportPolicyNoHost:
153 return @"NO_HOST";
154 case RTCIceTransportPolicyAll:
155 return @"ALL";
156 }
157}
158
159+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
160 (RTCBundlePolicy)policy {
161 switch (policy) {
162 case RTCBundlePolicyBalanced:
163 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
164 case RTCBundlePolicyMaxCompat:
165 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
166 case RTCBundlePolicyMaxBundle:
167 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
168 }
169}
170
171+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
172 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
173 switch (nativePolicy) {
174 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
175 return RTCBundlePolicyBalanced;
176 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
177 return RTCBundlePolicyMaxCompat;
178 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
179 return RTCBundlePolicyMaxBundle;
180 }
181}
182
183+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
184 switch (policy) {
185 case RTCBundlePolicyBalanced:
186 return @"BALANCED";
187 case RTCBundlePolicyMaxCompat:
188 return @"MAX_COMPAT";
189 case RTCBundlePolicyMaxBundle:
190 return @"MAX_BUNDLE";
191 }
192}
193
194+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
195 (RTCRtcpMuxPolicy)policy {
196 switch (policy) {
197 case RTCRtcpMuxPolicyNegotiate:
198 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
199 case RTCRtcpMuxPolicyRequire:
200 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
201 }
202}
203
204+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
205 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
206 switch (nativePolicy) {
207 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
208 return RTCRtcpMuxPolicyNegotiate;
209 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
210 return RTCRtcpMuxPolicyRequire;
211 }
212}
213
214+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
215 switch (policy) {
216 case RTCRtcpMuxPolicyNegotiate:
217 return @"NEGOTIATE";
218 case RTCRtcpMuxPolicyRequire:
219 return @"REQUIRE";
220 }
221}
222
223+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
224 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
225 switch (policy) {
226 case RTCTcpCandidatePolicyEnabled:
227 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
228 case RTCTcpCandidatePolicyDisabled:
229 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
230 }
231}
232
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200233+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
234 (RTCEncryptionKeyType)keyType {
235 switch (keyType) {
236 case RTCEncryptionKeyTypeRSA:
237 return rtc::KT_RSA;
238 case RTCEncryptionKeyTypeECDSA:
239 return rtc::KT_ECDSA;
240 }
241}
242
hjon6d49a8e2016-01-26 13:06:42 -0800243+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
244 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
245 switch (nativePolicy) {
246 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
247 return RTCTcpCandidatePolicyEnabled;
248 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
249 return RTCTcpCandidatePolicyDisabled;
250 }
251}
252
253+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
254 switch (policy) {
255 case RTCTcpCandidatePolicyEnabled:
256 return @"TCP_ENABLED";
257 case RTCTcpCandidatePolicyDisabled:
258 return @"TCP_DISABLED";
259 }
260}
261
Honghai Zhang3108fc92016-05-11 10:10:39 -0700262+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
263 nativeContinualGatheringPolicyForPolicy:
264 (RTCContinualGatheringPolicy)policy {
265 switch (policy) {
266 case RTCContinualGatheringPolicyGatherOnce:
267 return webrtc::PeerConnectionInterface::GATHER_ONCE;
268 case RTCContinualGatheringPolicyGatherContinually:
269 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
270 }
271}
272
273+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
274 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
275 switch (nativePolicy) {
276 case webrtc::PeerConnectionInterface::GATHER_ONCE:
277 return RTCContinualGatheringPolicyGatherOnce;
278 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
279 return RTCContinualGatheringPolicyGatherContinually;
280 }
281}
282
283+ (NSString *)stringForContinualGatheringPolicy:
284 (RTCContinualGatheringPolicy)policy {
285 switch (policy) {
286 case RTCContinualGatheringPolicyGatherOnce:
287 return @"GATHER_ONCE";
288 case RTCContinualGatheringPolicyGatherContinually:
289 return @"GATHER_CONTINUALLY";
290 }
291}
292
hjon6d49a8e2016-01-26 13:06:42 -0800293@end