blob: 1ab9c564296be13b9e6c524cf8691930336d7f86 [file] [log] [blame]
Zeke Chinbc7dd7e2015-05-29 14:59:14 -07001/*
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"
tkchinab8f82f2016-01-27 17:50:11 -080032#import "talk/app/webrtc/objc/public/RTCLogging.h"
Zeke Chinbc7dd7e2015-05-29 14:59:14 -070033
kwiberg84cc9182016-03-11 22:12:57 -080034#include <memory>
35
hbosf9da44d2016-06-09 03:18:28 -070036#include "webrtc/base/rtccertificategenerator.h"
37
Zeke Chinbc7dd7e2015-05-29 14:59:14 -070038@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;
honghaiz4edc39c2015-09-01 09:53:56 -070046@synthesize iceConnectionReceivingTimeout = _iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -080047@synthesize iceBackupCandidatePairPingInterval = _iceBackupCandidatePairPingInterval;
tkchinab8f82f2016-01-27 17:50:11 -080048@synthesize keyType = _keyType;
Zeke Chinbc7dd7e2015-05-29 14:59:14 -070049
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;
honghaiz4edc39c2015-09-01 09:53:56 -070060 _iceConnectionReceivingTimeout = config.ice_connection_receiving_timeout;
Honghai Zhang381b4212015-12-04 12:24:03 -080061 _iceBackupCandidatePairPingInterval = config.ice_backup_candidate_pair_ping_interval;
tkchinab8f82f2016-01-27 17:50:11 -080062 _keyType = kRTCEncryptionKeyTypeECDSA;
Zeke Chinbc7dd7e2015-05-29 14:59:14 -070063 }
64 return self;
65}
66
67- (instancetype)initWithIceTransportsType:(RTCIceTransportsType)iceTransportsType
68 bundlePolicy:(RTCBundlePolicy)bundlePolicy
69 rtcpMuxPolicy:(RTCRtcpMuxPolicy)rtcpMuxPolicy
70 tcpCandidatePolicy:(RTCTcpCandidatePolicy)tcpCandidatePolicy
honghaiz4edc39c2015-09-01 09:53:56 -070071 audioJitterBufferMaxPackets:(int)audioJitterBufferMaxPackets
Honghai Zhang381b4212015-12-04 12:24:03 -080072 iceConnectionReceivingTimeout:(int)iceConnectionReceivingTimeout
73 iceBackupCandidatePairPingInterval:(int)iceBackupCandidatePairPingInterval {
Zeke Chinbc7dd7e2015-05-29 14:59:14 -070074 if (self = [super init]) {
75 _iceTransportsType = iceTransportsType;
76 _bundlePolicy = bundlePolicy;
77 _rtcpMuxPolicy = rtcpMuxPolicy;
78 _tcpCandidatePolicy = tcpCandidatePolicy;
79 _audioJitterBufferMaxPackets = audioJitterBufferMaxPackets;
honghaiz4edc39c2015-09-01 09:53:56 -070080 _iceConnectionReceivingTimeout = iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -080081 _iceBackupCandidatePairPingInterval = iceBackupCandidatePairPingInterval;
Zeke Chinbc7dd7e2015-05-29 14:59:14 -070082 }
83 return self;
84}
85
86#pragma mark - Private
87
hbosf9da44d2016-06-09 03:18:28 -070088- (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 Chinbc7dd7e2015-05-29 14:59:14 -070094 for (RTCICEServer *iceServer : _iceServers) {
hbosf9da44d2016-06-09 03:18:28 -070095 nativeConfig->servers.push_back(iceServer.iceServer);
Zeke Chinbc7dd7e2015-05-29 14:59:14 -070096 }
hbosf9da44d2016-06-09 03:18:28 -070097 nativeConfig->bundle_policy =
98 [RTCEnumConverter nativeEnumForBundlePolicy:_bundlePolicy];
99 nativeConfig->rtcp_mux_policy =
100 [RTCEnumConverter nativeEnumForRtcpMuxPolicy:_rtcpMuxPolicy];
101 nativeConfig->tcp_candidate_policy =
Zeke Chinbc7dd7e2015-05-29 14:59:14 -0700102 [RTCEnumConverter nativeEnumForTcpCandidatePolicy:_tcpCandidatePolicy];
hbosf9da44d2016-06-09 03:18:28 -0700103 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;
tkchinab8f82f2016-01-27 17:50:11 -0800117 }
hbosf9da44d2016-06-09 03:18:28 -0700118 nativeConfig->certificates.push_back(certificate);
tkchinab8f82f2016-01-27 17:50:11 -0800119 }
hbosf9da44d2016-06-09 03:18:28 -0700120 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 Chinbc7dd7e2015-05-29 14:59:14 -0700131}
132
133@end