xref: /aosp_15_r20/external/webrtc/sdk/objc/unittests/RTCPeerConnectionTest.mm (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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
11#import <Foundation/Foundation.h>
12#import <XCTest/XCTest.h>
13
14#include <memory>
15#include <vector>
16
17#include "rtc_base/gunit.h"
18
19#import "api/peerconnection/RTCConfiguration+Private.h"
20#import "api/peerconnection/RTCConfiguration.h"
21#import "api/peerconnection/RTCCryptoOptions.h"
22#import "api/peerconnection/RTCIceCandidate.h"
23#import "api/peerconnection/RTCIceServer.h"
24#import "api/peerconnection/RTCMediaConstraints.h"
25#import "api/peerconnection/RTCPeerConnection.h"
26#import "api/peerconnection/RTCPeerConnectionFactory+Native.h"
27#import "api/peerconnection/RTCPeerConnectionFactory.h"
28#import "api/peerconnection/RTCSessionDescription.h"
29#import "helpers/NSString+StdString.h"
30
31@interface RTCPeerConnectionTests : XCTestCase
32@end
33
34@implementation RTCPeerConnectionTests
35
36- (void)testConfigurationGetter {
37  NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
38  RTC_OBJC_TYPE(RTCIceServer) *server =
39      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
40
41  RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
42  config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
43  config.iceServers = @[ server ];
44  config.iceTransportPolicy = RTCIceTransportPolicyRelay;
45  config.bundlePolicy = RTCBundlePolicyMaxBundle;
46  config.rtcpMuxPolicy = RTCRtcpMuxPolicyNegotiate;
47  config.tcpCandidatePolicy = RTCTcpCandidatePolicyDisabled;
48  config.candidateNetworkPolicy = RTCCandidateNetworkPolicyLowCost;
49  const int maxPackets = 60;
50  const int timeout = 1500;
51  const int interval = 2000;
52  config.audioJitterBufferMaxPackets = maxPackets;
53  config.audioJitterBufferFastAccelerate = YES;
54  config.iceConnectionReceivingTimeout = timeout;
55  config.iceBackupCandidatePairPingInterval = interval;
56  config.continualGatheringPolicy =
57      RTCContinualGatheringPolicyGatherContinually;
58  config.shouldPruneTurnPorts = YES;
59  config.activeResetSrtpParams = YES;
60  config.cryptoOptions =
61      [[RTC_OBJC_TYPE(RTCCryptoOptions) alloc] initWithSrtpEnableGcmCryptoSuites:YES
62                                             srtpEnableAes128Sha1_32CryptoCipher:YES
63                                          srtpEnableEncryptedRtpHeaderExtensions:NO
64                                                    sframeRequireFrameEncryption:NO];
65
66  RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
67      [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
68                                                           optionalConstraints:nil];
69  RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
70      [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
71
72  RTC_OBJC_TYPE(RTCConfiguration) * newConfig;
73  @autoreleasepool {
74    RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
75        [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
76    newConfig = peerConnection.configuration;
77
78    EXPECT_TRUE([peerConnection setBweMinBitrateBps:[NSNumber numberWithInt:100000]
79                                  currentBitrateBps:[NSNumber numberWithInt:5000000]
80                                      maxBitrateBps:[NSNumber numberWithInt:500000000]]);
81    EXPECT_FALSE([peerConnection setBweMinBitrateBps:[NSNumber numberWithInt:2]
82                                   currentBitrateBps:[NSNumber numberWithInt:1]
83                                       maxBitrateBps:nil]);
84  }
85
86  EXPECT_EQ([config.iceServers count], [newConfig.iceServers count]);
87  RTC_OBJC_TYPE(RTCIceServer) *newServer = newConfig.iceServers[0];
88  RTC_OBJC_TYPE(RTCIceServer) *origServer = config.iceServers[0];
89  std::string origUrl = origServer.urlStrings.firstObject.UTF8String;
90  std::string url = newServer.urlStrings.firstObject.UTF8String;
91  EXPECT_EQ(origUrl, url);
92
93  EXPECT_EQ(config.iceTransportPolicy, newConfig.iceTransportPolicy);
94  EXPECT_EQ(config.bundlePolicy, newConfig.bundlePolicy);
95  EXPECT_EQ(config.rtcpMuxPolicy, newConfig.rtcpMuxPolicy);
96  EXPECT_EQ(config.tcpCandidatePolicy, newConfig.tcpCandidatePolicy);
97  EXPECT_EQ(config.candidateNetworkPolicy, newConfig.candidateNetworkPolicy);
98  EXPECT_EQ(config.audioJitterBufferMaxPackets, newConfig.audioJitterBufferMaxPackets);
99  EXPECT_EQ(config.audioJitterBufferFastAccelerate, newConfig.audioJitterBufferFastAccelerate);
100  EXPECT_EQ(config.iceConnectionReceivingTimeout, newConfig.iceConnectionReceivingTimeout);
101  EXPECT_EQ(config.iceBackupCandidatePairPingInterval,
102            newConfig.iceBackupCandidatePairPingInterval);
103  EXPECT_EQ(config.continualGatheringPolicy, newConfig.continualGatheringPolicy);
104  EXPECT_EQ(config.shouldPruneTurnPorts, newConfig.shouldPruneTurnPorts);
105  EXPECT_EQ(config.activeResetSrtpParams, newConfig.activeResetSrtpParams);
106  EXPECT_EQ(config.cryptoOptions.srtpEnableGcmCryptoSuites,
107            newConfig.cryptoOptions.srtpEnableGcmCryptoSuites);
108  EXPECT_EQ(config.cryptoOptions.srtpEnableAes128Sha1_32CryptoCipher,
109            newConfig.cryptoOptions.srtpEnableAes128Sha1_32CryptoCipher);
110  EXPECT_EQ(config.cryptoOptions.srtpEnableEncryptedRtpHeaderExtensions,
111            newConfig.cryptoOptions.srtpEnableEncryptedRtpHeaderExtensions);
112  EXPECT_EQ(config.cryptoOptions.sframeRequireFrameEncryption,
113            newConfig.cryptoOptions.sframeRequireFrameEncryption);
114}
115
116- (void)testWithDependencies {
117  NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
118  RTC_OBJC_TYPE(RTCIceServer) *server =
119      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
120
121  RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
122  config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
123  config.iceServers = @[ server ];
124  RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
125      [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
126                                                           optionalConstraints:nil];
127  RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
128      [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
129
130  std::unique_ptr<webrtc::PeerConnectionDependencies> pc_dependencies =
131      std::make_unique<webrtc::PeerConnectionDependencies>(nullptr);
132  @autoreleasepool {
133    RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
134        [factory peerConnectionWithDependencies:config
135                                    constraints:contraints
136                                   dependencies:std::move(pc_dependencies)
137                                       delegate:nil];
138    ASSERT_NE(peerConnection, nil);
139  }
140}
141
142- (void)testWithInvalidSDP {
143  RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
144      [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
145
146  RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
147  config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
148  RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
149      [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
150                                                           optionalConstraints:nil];
151  RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
152      [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
153
154  dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
155  [peerConnection setRemoteDescription:[[RTC_OBJC_TYPE(RTCSessionDescription) alloc]
156                                           initWithType:RTCSdpTypeOffer
157                                                    sdp:@"invalid"]
158                     completionHandler:^(NSError *error) {
159                       ASSERT_NE(error, nil);
160                       if (error != nil) {
161                         dispatch_semaphore_signal(negotiatedSem);
162                       }
163                     }];
164
165  NSTimeInterval timeout = 5;
166  ASSERT_EQ(
167      0,
168      dispatch_semaphore_wait(negotiatedSem,
169                              dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
170  [peerConnection close];
171}
172
173- (void)testWithInvalidIceCandidate {
174  RTC_OBJC_TYPE(RTCPeerConnectionFactory) *factory =
175      [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
176
177  RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
178  config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
179  RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
180      [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
181                                                           optionalConstraints:nil];
182  RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
183      [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
184
185  dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
186  [peerConnection addIceCandidate:[[RTC_OBJC_TYPE(RTCIceCandidate) alloc] initWithSdp:@"invalid"
187                                                                        sdpMLineIndex:-1
188                                                                               sdpMid:nil]
189                completionHandler:^(NSError *error) {
190                  ASSERT_NE(error, nil);
191                  if (error != nil) {
192                    dispatch_semaphore_signal(negotiatedSem);
193                  }
194                }];
195
196  NSTimeInterval timeout = 5;
197  ASSERT_EQ(
198      0,
199      dispatch_semaphore_wait(negotiatedSem,
200                              dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))));
201  [peerConnection close];
202}
203
204@end
205