hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "RTCConfiguration+Private.h" |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 12 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 15 | #import "RTCIceServer+Private.h" |
| 16 | #import "WebRTC/RTCLogging.h" |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 17 | |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 18 | #include "webrtc/base/rtccertificategenerator.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 19 | #include "webrtc/base/sslidentity.h" |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 20 | |
| 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 Zhang | 3108fc9 | 2016-05-11 10:10:39 -0700 | [diff] [blame] | 28 | @synthesize continualGatheringPolicy = _continualGatheringPolicy; |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 29 | @synthesize audioJitterBufferMaxPackets = _audioJitterBufferMaxPackets; |
| 30 | @synthesize iceConnectionReceivingTimeout = _iceConnectionReceivingTimeout; |
| 31 | @synthesize iceBackupCandidatePairPingInterval = |
| 32 | _iceBackupCandidatePairPingInterval; |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 33 | @synthesize keyType = _keyType; |
deadbeef | be0c96f | 2016-05-18 16:20:14 -0700 | [diff] [blame] | 34 | @synthesize iceCandidatePoolSize = _iceCandidatePoolSize; |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 35 | |
| 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 Zhang | 3108fc9 | 2016-05-11 10:10:39 -0700 | [diff] [blame] | 49 | webrtc::PeerConnectionInterface::ContinualGatheringPolicy nativePolicy = |
| 50 | config.continual_gathering_policy; |
| 51 | _continualGatheringPolicy = |
| 52 | [[self class] continualGatheringPolicyForNativePolicy:nativePolicy]; |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 53 | _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets; |
| 54 | _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout; |
| 55 | _iceBackupCandidatePairPingInterval = |
| 56 | config.ice_backup_candidate_pair_ping_interval; |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 57 | _keyType = RTCEncryptionKeyTypeECDSA; |
deadbeef | be0c96f | 2016-05-18 16:20:14 -0700 | [diff] [blame] | 58 | _iceCandidatePoolSize = config.ice_candidate_pool_size; |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 59 | } |
| 60 | return self; |
| 61 | } |
| 62 | |
| 63 | - (NSString *)description { |
| 64 | return [NSString stringWithFormat: |
deadbeef | be0c96f | 2016-05-18 16:20:14 -0700 | [diff] [blame] | 65 | @"RTCConfiguration: {\n%@\n%@\n%@\n%@\n%@\n%@\n%d\n%d\n%d\n%d\n}\n", |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 66 | _iceServers, |
| 67 | [[self class] stringForTransportPolicy:_iceTransportPolicy], |
| 68 | [[self class] stringForBundlePolicy:_bundlePolicy], |
| 69 | [[self class] stringForRtcpMuxPolicy:_rtcpMuxPolicy], |
| 70 | [[self class] stringForTcpCandidatePolicy:_tcpCandidatePolicy], |
Honghai Zhang | 3108fc9 | 2016-05-11 10:10:39 -0700 | [diff] [blame] | 71 | [[self class] |
| 72 | stringForContinualGatheringPolicy:_continualGatheringPolicy], |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 73 | _audioJitterBufferMaxPackets, |
| 74 | _iceConnectionReceivingTimeout, |
deadbeef | be0c96f | 2016-05-18 16:20:14 -0700 | [diff] [blame] | 75 | _iceBackupCandidatePairPingInterval, |
| 76 | _iceCandidatePoolSize]; |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | #pragma mark - Private |
| 80 | |
hbos | a73ca56 | 2016-05-17 03:28:58 -0700 | [diff] [blame] | 81 | - (webrtc::PeerConnectionInterface::RTCConfiguration *) |
| 82 | createNativeConfiguration { |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 83 | std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> |
| 84 | nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration()); |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 85 | |
| 86 | for (RTCIceServer *iceServer in _iceServers) { |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 87 | nativeConfig->servers.push_back(iceServer.nativeServer); |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 88 | } |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 89 | nativeConfig->type = |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 90 | [[self class] nativeTransportsTypeForTransportPolicy:_iceTransportPolicy]; |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 91 | nativeConfig->bundle_policy = |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 92 | [[self class] nativeBundlePolicyForPolicy:_bundlePolicy]; |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 93 | nativeConfig->rtcp_mux_policy = |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 94 | [[self class] nativeRtcpMuxPolicyForPolicy:_rtcpMuxPolicy]; |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 95 | nativeConfig->tcp_candidate_policy = |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 96 | [[self class] nativeTcpCandidatePolicyForPolicy:_tcpCandidatePolicy]; |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 97 | nativeConfig->continual_gathering_policy = [[self class] |
Honghai Zhang | 3108fc9 | 2016-05-11 10:10:39 -0700 | [diff] [blame] | 98 | nativeContinualGatheringPolicyForPolicy:_continualGatheringPolicy]; |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 99 | nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets; |
| 100 | nativeConfig->ice_connection_receiving_timeout = |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 101 | _iceConnectionReceivingTimeout; |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 102 | nativeConfig->ice_backup_candidate_pair_ping_interval = |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 103 | _iceBackupCandidatePairPingInterval; |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 104 | 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) { |
hbos | a73ca56 | 2016-05-17 03:28:58 -0700 | [diff] [blame] | 112 | RTCLogError(@"Failed to generate certificate."); |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 113 | return nullptr; |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 114 | } |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 115 | nativeConfig->certificates.push_back(certificate); |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 116 | } |
deadbeef | be0c96f | 2016-05-18 16:20:14 -0700 | [diff] [blame] | 117 | nativeConfig->ice_candidate_pool_size = _iceCandidatePoolSize; |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 118 | |
Henrik Boström | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 119 | return nativeConfig.release(); |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 120 | } |
| 121 | |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 122 | + (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öm | e06c2dd | 2016-05-13 13:50:38 +0200 | [diff] [blame] | 237 | + (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 | |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 247 | + (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 Zhang | 3108fc9 | 2016-05-11 10:10:39 -0700 | [diff] [blame] | 266 | + (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 | |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 297 | @end |