Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #import "talk/app/webrtc/objc/RTCPeerConnectionInterface+Internal.h" |
| 29 | |
| 30 | #import "talk/app/webrtc/objc/RTCEnumConverter.h" |
| 31 | #import "talk/app/webrtc/objc/RTCICEServer+Internal.h" |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 32 | #import "talk/app/webrtc/objc/public/RTCLogging.h" |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 33 | |
kwiberg | 84cc918 | 2016-03-11 22:12:57 -0800 | [diff] [blame] | 34 | #include <memory> |
| 35 | |
hbos | f9da44d | 2016-06-09 03:18:28 -0700 | [diff] [blame] | 36 | #include "webrtc/base/rtccertificategenerator.h" |
| 37 | |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 38 | @implementation RTCConfiguration |
| 39 | |
| 40 | @synthesize iceTransportsType = _iceTransportsType; |
| 41 | @synthesize iceServers = _iceServers; |
| 42 | @synthesize bundlePolicy = _bundlePolicy; |
| 43 | @synthesize rtcpMuxPolicy = _rtcpMuxPolicy; |
| 44 | @synthesize tcpCandidatePolicy = _tcpCandidatePolicy; |
| 45 | @synthesize audioJitterBufferMaxPackets = _audioJitterBufferMaxPackets; |
honghaiz | 4edc39c | 2015-09-01 09:53:56 -0700 | [diff] [blame] | 46 | @synthesize iceConnectionReceivingTimeout = _iceConnectionReceivingTimeout; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 47 | @synthesize iceBackupCandidatePairPingInterval = _iceBackupCandidatePairPingInterval; |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 48 | @synthesize keyType = _keyType; |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 49 | |
| 50 | - (instancetype)init { |
| 51 | if (self = [super init]) { |
| 52 | // Copy defaults. |
| 53 | webrtc::PeerConnectionInterface::RTCConfiguration config; |
| 54 | _iceTransportsType = [RTCEnumConverter iceTransportsTypeForNativeEnum:config.type]; |
| 55 | _bundlePolicy = [RTCEnumConverter bundlePolicyForNativeEnum:config.bundle_policy]; |
| 56 | _rtcpMuxPolicy = [RTCEnumConverter rtcpMuxPolicyForNativeEnum:config.rtcp_mux_policy]; |
| 57 | _tcpCandidatePolicy = |
| 58 | [RTCEnumConverter tcpCandidatePolicyForNativeEnum:config.tcp_candidate_policy]; |
| 59 | _audioJitterBufferMaxPackets = config.audio_jitter_buffer_max_packets; |
honghaiz | 4edc39c | 2015-09-01 09:53:56 -0700 | [diff] [blame] | 60 | _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 61 | _iceBackupCandidatePairPingInterval = config.ice_backup_candidate_pair_ping_interval; |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 62 | _keyType = kRTCEncryptionKeyTypeECDSA; |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 63 | } |
| 64 | return self; |
| 65 | } |
| 66 | |
| 67 | - (instancetype)initWithIceTransportsType:(RTCIceTransportsType)iceTransportsType |
| 68 | bundlePolicy:(RTCBundlePolicy)bundlePolicy |
| 69 | rtcpMuxPolicy:(RTCRtcpMuxPolicy)rtcpMuxPolicy |
| 70 | tcpCandidatePolicy:(RTCTcpCandidatePolicy)tcpCandidatePolicy |
honghaiz | 4edc39c | 2015-09-01 09:53:56 -0700 | [diff] [blame] | 71 | audioJitterBufferMaxPackets:(int)audioJitterBufferMaxPackets |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 72 | iceConnectionReceivingTimeout:(int)iceConnectionReceivingTimeout |
| 73 | iceBackupCandidatePairPingInterval:(int)iceBackupCandidatePairPingInterval { |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 74 | if (self = [super init]) { |
| 75 | _iceTransportsType = iceTransportsType; |
| 76 | _bundlePolicy = bundlePolicy; |
| 77 | _rtcpMuxPolicy = rtcpMuxPolicy; |
| 78 | _tcpCandidatePolicy = tcpCandidatePolicy; |
| 79 | _audioJitterBufferMaxPackets = audioJitterBufferMaxPackets; |
honghaiz | 4edc39c | 2015-09-01 09:53:56 -0700 | [diff] [blame] | 80 | _iceConnectionReceivingTimeout = iceConnectionReceivingTimeout; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 81 | _iceBackupCandidatePairPingInterval = iceBackupCandidatePairPingInterval; |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 82 | } |
| 83 | return self; |
| 84 | } |
| 85 | |
| 86 | #pragma mark - Private |
| 87 | |
hbos | f9da44d | 2016-06-09 03:18:28 -0700 | [diff] [blame] | 88 | - (webrtc::PeerConnectionInterface::RTCConfiguration *) |
| 89 | createNativeConfiguration { |
| 90 | std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> |
| 91 | nativeConfig(new webrtc::PeerConnectionInterface::RTCConfiguration()); |
| 92 | nativeConfig->type = |
| 93 | [RTCEnumConverter nativeEnumForIceTransportsType:_iceTransportsType]; |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 94 | for (RTCICEServer *iceServer : _iceServers) { |
hbos | f9da44d | 2016-06-09 03:18:28 -0700 | [diff] [blame] | 95 | nativeConfig->servers.push_back(iceServer.iceServer); |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 96 | } |
hbos | f9da44d | 2016-06-09 03:18:28 -0700 | [diff] [blame] | 97 | nativeConfig->bundle_policy = |
| 98 | [RTCEnumConverter nativeEnumForBundlePolicy:_bundlePolicy]; |
| 99 | nativeConfig->rtcp_mux_policy = |
| 100 | [RTCEnumConverter nativeEnumForRtcpMuxPolicy:_rtcpMuxPolicy]; |
| 101 | nativeConfig->tcp_candidate_policy = |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 102 | [RTCEnumConverter nativeEnumForTcpCandidatePolicy:_tcpCandidatePolicy]; |
hbos | f9da44d | 2016-06-09 03:18:28 -0700 | [diff] [blame] | 103 | nativeConfig->audio_jitter_buffer_max_packets = _audioJitterBufferMaxPackets; |
| 104 | nativeConfig->ice_connection_receiving_timeout = |
| 105 | _iceConnectionReceivingTimeout; |
| 106 | nativeConfig->ice_backup_candidate_pair_ping_interval = |
| 107 | _iceBackupCandidatePairPingInterval; |
| 108 | rtc::KeyType keyType = |
| 109 | [[self class] nativeEncryptionKeyTypeForKeyType:_keyType]; |
| 110 | if (keyType != rtc::KT_DEFAULT) { |
| 111 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
| 112 | rtc::RTCCertificateGenerator::GenerateCertificate( |
| 113 | rtc::KeyParams(keyType), rtc::Optional<uint64_t>()); |
| 114 | if (!certificate) { |
| 115 | RTCLogError(@"Failed to generate certificate."); |
| 116 | return nullptr; |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 117 | } |
hbos | f9da44d | 2016-06-09 03:18:28 -0700 | [diff] [blame] | 118 | nativeConfig->certificates.push_back(certificate); |
tkchin | ab8f82f | 2016-01-27 17:50:11 -0800 | [diff] [blame] | 119 | } |
hbos | f9da44d | 2016-06-09 03:18:28 -0700 | [diff] [blame] | 120 | return nativeConfig.release(); |
| 121 | } |
| 122 | |
| 123 | + (rtc::KeyType)nativeEncryptionKeyTypeForKeyType: |
| 124 | (RTCEncryptionKeyType)keyType { |
| 125 | switch (keyType) { |
| 126 | case kRTCEncryptionKeyTypeRSA: |
| 127 | return rtc::KT_RSA; |
| 128 | case kRTCEncryptionKeyTypeECDSA: |
| 129 | return rtc::KT_ECDSA; |
| 130 | } |
Zeke Chin | bc7dd7e | 2015-05-29 14:59:14 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @end |