hjon | aa32c3e | 2015-12-13 19:58:11 -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 "RTCIceServer+Private.h" |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 12 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 13 | #import "NSString+StdString.h" |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 14 | |
| 15 | @implementation RTCIceServer |
| 16 | |
| 17 | @synthesize urlStrings = _urlStrings; |
| 18 | @synthesize username = _username; |
| 19 | @synthesize credential = _credential; |
hnsl | 6741516 | 2017-02-02 13:04:27 -0800 | [diff] [blame] | 20 | @synthesize tlsCertPolicy = _tlsCertPolicy; |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 21 | |
Jon Hjelle | 32e0c01 | 2016-03-08 16:04:46 -0800 | [diff] [blame] | 22 | - (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings { |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 23 | return [self initWithURLStrings:urlStrings |
| 24 | username:nil |
| 25 | credential:nil]; |
| 26 | } |
| 27 | |
Jon Hjelle | 32e0c01 | 2016-03-08 16:04:46 -0800 | [diff] [blame] | 28 | - (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 29 | username:(NSString *)username |
| 30 | credential:(NSString *)credential { |
hnsl | 6741516 | 2017-02-02 13:04:27 -0800 | [diff] [blame] | 31 | return [self initWithURLStrings:urlStrings |
| 32 | username:username |
| 33 | credential:credential |
| 34 | tlsCertPolicy:RTCTlsCertPolicySecure]; |
| 35 | } |
| 36 | |
| 37 | - (instancetype)initWithURLStrings:(NSArray<NSString *> *)urlStrings |
| 38 | username:(NSString *)username |
| 39 | credential:(NSString *)credential |
| 40 | tlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy { |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 41 | NSParameterAssert(urlStrings.count); |
| 42 | if (self = [super init]) { |
| 43 | _urlStrings = [[NSArray alloc] initWithArray:urlStrings copyItems:YES]; |
| 44 | _username = [username copy]; |
| 45 | _credential = [credential copy]; |
hnsl | 6741516 | 2017-02-02 13:04:27 -0800 | [diff] [blame] | 46 | _tlsCertPolicy = tlsCertPolicy; |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 47 | } |
| 48 | return self; |
| 49 | } |
| 50 | |
| 51 | - (NSString *)description { |
hnsl | 6741516 | 2017-02-02 13:04:27 -0800 | [diff] [blame] | 52 | return |
| 53 | [NSString stringWithFormat:@"RTCIceServer:\n%@\n%@\n%@\n%@", _urlStrings, |
| 54 | _username, _credential, |
| 55 | [self stringForTlsCertPolicy:_tlsCertPolicy]]; |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | #pragma mark - Private |
| 59 | |
hnsl | 6741516 | 2017-02-02 13:04:27 -0800 | [diff] [blame] | 60 | - (NSString *)stringForTlsCertPolicy:(RTCTlsCertPolicy)tlsCertPolicy { |
| 61 | switch (tlsCertPolicy) { |
| 62 | case RTCTlsCertPolicySecure: |
| 63 | return @"RTCTlsCertPolicySecure"; |
| 64 | case RTCTlsCertPolicyInsecureNoCheck: |
| 65 | return @"RTCTlsCertPolicyInsecureNoCheck"; |
| 66 | } |
| 67 | } |
| 68 | |
hjon | a2f7798 | 2016-03-04 07:09:09 -0800 | [diff] [blame] | 69 | - (webrtc::PeerConnectionInterface::IceServer)nativeServer { |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 70 | __block webrtc::PeerConnectionInterface::IceServer iceServer; |
| 71 | |
| 72 | iceServer.username = [NSString stdStringForString:_username]; |
| 73 | iceServer.password = [NSString stdStringForString:_credential]; |
| 74 | |
| 75 | [_urlStrings enumerateObjectsUsingBlock:^(NSString *url, |
| 76 | NSUInteger idx, |
| 77 | BOOL *stop) { |
| 78 | iceServer.urls.push_back(url.stdString); |
| 79 | }]; |
hnsl | 6741516 | 2017-02-02 13:04:27 -0800 | [diff] [blame] | 80 | |
| 81 | switch (_tlsCertPolicy) { |
| 82 | case RTCTlsCertPolicySecure: |
| 83 | iceServer.tls_cert_policy = |
| 84 | webrtc::PeerConnectionInterface::kTlsCertPolicySecure; |
| 85 | break; |
| 86 | case RTCTlsCertPolicyInsecureNoCheck: |
| 87 | iceServer.tls_cert_policy = |
| 88 | webrtc::PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck; |
| 89 | break; |
| 90 | } |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 91 | return iceServer; |
| 92 | } |
| 93 | |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 94 | - (instancetype)initWithNativeServer: |
| 95 | (webrtc::PeerConnectionInterface::IceServer)nativeServer { |
| 96 | NSMutableArray *urls = |
| 97 | [NSMutableArray arrayWithCapacity:nativeServer.urls.size()]; |
| 98 | for (auto const &url : nativeServer.urls) { |
| 99 | [urls addObject:[NSString stringForStdString:url]]; |
| 100 | } |
| 101 | NSString *username = [NSString stringForStdString:nativeServer.username]; |
| 102 | NSString *credential = [NSString stringForStdString:nativeServer.password]; |
hnsl | 6741516 | 2017-02-02 13:04:27 -0800 | [diff] [blame] | 103 | RTCTlsCertPolicy tlsCertPolicy; |
| 104 | |
| 105 | switch (nativeServer.tls_cert_policy) { |
| 106 | case webrtc::PeerConnectionInterface::kTlsCertPolicySecure: |
| 107 | tlsCertPolicy = RTCTlsCertPolicySecure; |
| 108 | break; |
| 109 | case webrtc::PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck: |
| 110 | tlsCertPolicy = RTCTlsCertPolicyInsecureNoCheck; |
| 111 | break; |
| 112 | } |
| 113 | |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 114 | self = [self initWithURLStrings:urls |
| 115 | username:username |
hnsl | 6741516 | 2017-02-02 13:04:27 -0800 | [diff] [blame] | 116 | credential:credential |
| 117 | tlsCertPolicy:tlsCertPolicy]; |
hjon | 6d49a8e | 2016-01-26 13:06:42 -0800 | [diff] [blame] | 118 | return self; |
| 119 | } |
| 120 | |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 121 | @end |