blob: 5beae99ed990a9ada498b6d069612842034902d3 [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
Henrik Boströme06c2dd2016-05-13 13:50:38 +020078- (webrtc::PeerConnectionInterface::RTCConfiguration*)nativeConfiguration {
79 std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration>
80 nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration());
hjon6d49a8e2016-01-26 13:06:42 -080081
82 for (RTCIceServer *iceServer in _iceServers) {
Henrik Boströme06c2dd2016-05-13 13:50:38 +020083 nativeConfig->servers.push_back(iceServer.nativeServer);
hjon6d49a8e2016-01-26 13:06:42 -080084 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +020085 nativeConfig->type =
hjon6d49a8e2016-01-26 13:06:42 -080086 [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020087 nativeConfig->bundle_policy =
hjon6d49a8e2016-01-26 13:06:42 -080088 [[self class] nativeBundlePolicyForPolicy:_bundlePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020089 nativeConfig->rtcp_mux_policy =
hjon6d49a8e2016-01-26 13:06:42 -080090 [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020091 nativeConfig->tcp_candidate_policy =
hjon6d49a8e2016-01-26 13:06:42 -080092 [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020093 nativeConfig->continual_gathering_policy = [[self class]
Honghai Zhang3108fc92016-05-11 10:10:39 -070094 nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy];
Henrik Boströme06c2dd2016-05-13 13:50:38 +020095 nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets;
96 nativeConfig->ice_connection_receiving_timeout =
hjon6d49a8e2016-01-26 13:06:42 -080097 _iceConnectionReceivingTimeout;
Henrik Boströme06c2dd2016-05-13 13:50:38 +020098 nativeConfig->ice_backup_candidate_pair_ping_interval =
hjon6d49a8e2016-01-26 13:06:42 -080099 _iceBackupCandidatePairPingInterval;
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200100 rtc::KeyType keyType =
101 [[self class] nativeEncryptionKeyTypeForKeyType:_keyType];
102 // Generate non-default certificate.
103 if (keyType != rtc::KT_DEFAULT) {
104 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
105 rtc::RTCCertificateGenerator::GenerateCertificate(
106 rtc::KeyParams(keyType), rtc::Optional<uint64_t>());
107 if (!certificate) {
108 RTCLogWarning(@"Failed to generate certificate.");
109 return nullptr;
tkchinab8f82f2016-01-27 17:50:11 -0800110 }
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200111 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800112 }
hjon6d49a8e2016-01-26 13:06:42 -0800113
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200114 return nativeConfig.release();
hjon6d49a8e2016-01-26 13:06:42 -0800115}
116
hjon6d49a8e2016-01-26 13:06:42 -0800117+ (webrtc::PeerConnectionInterface::IceTransportsType)
118 nativeTransportsTypeForTransportPolicy:(RTCIceTransportPolicy)policy {
119 switch (policy) {
120 case RTCIceTransportPolicyNone:
121 return webrtc::PeerConnectionInterface::kNone;
122 case RTCIceTransportPolicyRelay:
123 return webrtc::PeerConnectionInterface::kRelay;
124 case RTCIceTransportPolicyNoHost:
125 return webrtc::PeerConnectionInterface::kNoHost;
126 case RTCIceTransportPolicyAll:
127 return webrtc::PeerConnectionInterface::kAll;
128 }
129}
130
131+ (RTCIceTransportPolicy)transportPolicyForTransportsType:
132 (webrtc::PeerConnectionInterface::IceTransportsType)nativeType {
133 switch (nativeType) {
134 case webrtc::PeerConnectionInterface::kNone:
135 return RTCIceTransportPolicyNone;
136 case webrtc::PeerConnectionInterface::kRelay:
137 return RTCIceTransportPolicyRelay;
138 case webrtc::PeerConnectionInterface::kNoHost:
139 return RTCIceTransportPolicyNoHost;
140 case webrtc::PeerConnectionInterface::kAll:
141 return RTCIceTransportPolicyAll;
142 }
143}
144
145+ (NSString *)stringForTransportPolicy:(RTCIceTransportPolicy)policy {
146 switch (policy) {
147 case RTCIceTransportPolicyNone:
148 return @"NONE";
149 case RTCIceTransportPolicyRelay:
150 return @"RELAY";
151 case RTCIceTransportPolicyNoHost:
152 return @"NO_HOST";
153 case RTCIceTransportPolicyAll:
154 return @"ALL";
155 }
156}
157
158+ (webrtc::PeerConnectionInterface::BundlePolicy)nativeBundlePolicyForPolicy:
159 (RTCBundlePolicy)policy {
160 switch (policy) {
161 case RTCBundlePolicyBalanced:
162 return webrtc::PeerConnectionInterface::kBundlePolicyBalanced;
163 case RTCBundlePolicyMaxCompat:
164 return webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat;
165 case RTCBundlePolicyMaxBundle:
166 return webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
167 }
168}
169
170+ (RTCBundlePolicy)bundlePolicyForNativePolicy:
171 (webrtc::PeerConnectionInterface::BundlePolicy)nativePolicy {
172 switch (nativePolicy) {
173 case webrtc::PeerConnectionInterface::kBundlePolicyBalanced:
174 return RTCBundlePolicyBalanced;
175 case webrtc::PeerConnectionInterface::kBundlePolicyMaxCompat:
176 return RTCBundlePolicyMaxCompat;
177 case webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle:
178 return RTCBundlePolicyMaxBundle;
179 }
180}
181
182+ (NSString *)stringForBundlePolicy:(RTCBundlePolicy)policy {
183 switch (policy) {
184 case RTCBundlePolicyBalanced:
185 return @"BALANCED";
186 case RTCBundlePolicyMaxCompat:
187 return @"MAX_COMPAT";
188 case RTCBundlePolicyMaxBundle:
189 return @"MAX_BUNDLE";
190 }
191}
192
193+ (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativeRtcpMuxPolicyForPolicy:
194 (RTCRtcpMuxPolicy)policy {
195 switch (policy) {
196 case RTCRtcpMuxPolicyNegotiate:
197 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
198 case RTCRtcpMuxPolicyRequire:
199 return webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire;
200 }
201}
202
203+ (RTCRtcpMuxPolicy)rtcpMuxPolicyForNativePolicy:
204 (webrtc::PeerConnectionInterface::RtcpMuxPolicy)nativePolicy {
205 switch (nativePolicy) {
206 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyNegotiate:
207 return RTCRtcpMuxPolicyNegotiate;
208 case webrtc::PeerConnectionInterface::kRtcpMuxPolicyRequire:
209 return RTCRtcpMuxPolicyRequire;
210 }
211}
212
213+ (NSString *)stringForRtcpMuxPolicy:(RTCRtcpMuxPolicy)policy {
214 switch (policy) {
215 case RTCRtcpMuxPolicyNegotiate:
216 return @"NEGOTIATE";
217 case RTCRtcpMuxPolicyRequire:
218 return @"REQUIRE";
219 }
220}
221
222+ (webrtc::PeerConnectionInterface::TcpCandidatePolicy)
223 nativeTcpCandidatePolicyForPolicy:(RTCTcpCandidatePolicy)policy {
224 switch (policy) {
225 case RTCTcpCandidatePolicyEnabled:
226 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled;
227 case RTCTcpCandidatePolicyDisabled:
228 return webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled;
229 }
230}
231
Henrik Boströme06c2dd2016-05-13 13:50:38 +0200232+ (rtc::KeyType)nativeEncryptionKeyTypeForKeyType:
233 (RTCEncryptionKeyType)keyType {
234 switch (keyType) {
235 case RTCEncryptionKeyTypeRSA:
236 return rtc::KT_RSA;
237 case RTCEncryptionKeyTypeECDSA:
238 return rtc::KT_ECDSA;
239 }
240}
241
hjon6d49a8e2016-01-26 13:06:42 -0800242+ (RTCTcpCandidatePolicy)tcpCandidatePolicyForNativePolicy:
243 (webrtc::PeerConnectionInterface::TcpCandidatePolicy)nativePolicy {
244 switch (nativePolicy) {
245 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyEnabled:
246 return RTCTcpCandidatePolicyEnabled;
247 case webrtc::PeerConnectionInterface::kTcpCandidatePolicyDisabled:
248 return RTCTcpCandidatePolicyDisabled;
249 }
250}
251
252+ (NSString *)stringForTcpCandidatePolicy:(RTCTcpCandidatePolicy)policy {
253 switch (policy) {
254 case RTCTcpCandidatePolicyEnabled:
255 return @"TCP_ENABLED";
256 case RTCTcpCandidatePolicyDisabled:
257 return @"TCP_DISABLED";
258 }
259}
260
Honghai Zhang3108fc92016-05-11 10:10:39 -0700261+ (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)
262 nativeContinualGatheringPolicyForPolicy:
263 (RTCContinualGatheringPolicy)policy {
264 switch (policy) {
265 case RTCContinualGatheringPolicyGatherOnce:
266 return webrtc::PeerConnectionInterface::GATHER_ONCE;
267 case RTCContinualGatheringPolicyGatherContinually:
268 return webrtc::PeerConnectionInterface::GATHER_CONTINUALLY;
269 }
270}
271
272+ (RTCContinualGatheringPolicy)continualGatheringPolicyForNativePolicy:
273 (webrtc::PeerConnectionInterface::ContinualGatheringPolicy)nativePolicy {
274 switch (nativePolicy) {
275 case webrtc::PeerConnectionInterface::GATHER_ONCE:
276 return RTCContinualGatheringPolicyGatherOnce;
277 case webrtc::PeerConnectionInterface::GATHER_CONTINUALLY:
278 return RTCContinualGatheringPolicyGatherContinually;
279 }
280}
281
282+ (NSString *)stringForContinualGatheringPolicy:
283 (RTCContinualGatheringPolicy)policy {
284 switch (policy) {
285 case RTCContinualGatheringPolicyGatherOnce:
286 return @"GATHER_ONCE";
287 case RTCContinualGatheringPolicyGatherContinually:
288 return @"GATHER_CONTINUALLY";
289 }
290}
291
hjon6d49a8e2016-01-26 13:06:42 -0800292@end