blob: 28743a6fc109779d316fd635141b9f21513712d3 [file] [log] [blame]
Yura Yaroshevich5297bd22018-06-19 12:51:51 +03001/*
2 * Copyright 2018 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
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020011#import "api/peerconnection/RTCAudioSource.h"
12#import "api/peerconnection/RTCConfiguration.h"
13#import "api/peerconnection/RTCDataChannel.h"
14#import "api/peerconnection/RTCDataChannelConfiguration.h"
15#import "api/peerconnection/RTCMediaConstraints.h"
16#import "api/peerconnection/RTCMediaStreamTrack.h"
17#import "api/peerconnection/RTCPeerConnection.h"
18#import "api/peerconnection/RTCPeerConnectionFactory.h"
19#import "api/peerconnection/RTCRtpReceiver.h"
20#import "api/peerconnection/RTCRtpSender.h"
21#import "api/peerconnection/RTCRtpTransceiver.h"
Philipp Hanckedaec4882020-09-15 10:40:07 +020022#import "api/peerconnection/RTCSessionDescription.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020023#import "api/peerconnection/RTCVideoSource.h"
Byoungchan Lee83ceb902021-11-16 11:31:57 +090024#import "rtc_base/system/unused.h"
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030025
26#import <XCTest/XCTest.h>
27
28@interface RTCPeerConnectionFactoryTests : XCTestCase
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030029@end
30
31@implementation RTCPeerConnectionFactoryTests
32
33- (void)testPeerConnectionLifetime {
34 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020035 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030036
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020037 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
38 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
39 optionalConstraints:nil];
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030040
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020041 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
42 RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030043
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030044 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020045 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030046 peerConnection =
Yura Yaroshevich7a16c542018-07-11 12:55:04 +030047 [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030048 [peerConnection close];
49 factory = nil;
50 }
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030051 peerConnection = nil;
52 }
53
54 XCTAssertTrue(true, @"Expect test does not crash");
55}
56
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030057- (void)testMediaStreamLifetime {
58 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020059 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
60 RTC_OBJC_TYPE(RTCMediaStream) * mediaStream;
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030061
62 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020063 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030064 mediaStream = [factory mediaStreamWithStreamId:@"mediaStream"];
65 factory = nil;
66 }
67 mediaStream = nil;
Byoungchan Lee83ceb902021-11-16 11:31:57 +090068 RTC_UNUSED(mediaStream);
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030069 }
70
71 XCTAssertTrue(true, "Expect test does not crash");
72}
73
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030074- (void)testDataChannelLifetime {
75 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020076 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
77 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
78 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
79 optionalConstraints:nil];
80 RTC_OBJC_TYPE(RTCDataChannelConfiguration) *dataChannelConfig =
81 [[RTC_OBJC_TYPE(RTCDataChannelConfiguration) alloc] init];
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030082
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020083 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
84 RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
85 RTC_OBJC_TYPE(RTCDataChannel) * dataChannel;
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030086
87 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020088 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030089 peerConnection =
Yura Yaroshevich7a16c542018-07-11 12:55:04 +030090 [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030091 dataChannel =
92 [peerConnection dataChannelForLabel:@"test_channel" configuration:dataChannelConfig];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +030093 XCTAssertNotNil(dataChannel);
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030094 [peerConnection close];
95 peerConnection = nil;
96 factory = nil;
97 }
98 dataChannel = nil;
99 }
100
101 XCTAssertTrue(true, "Expect test does not crash");
102}
103
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300104- (void)testRTCRtpTransceiverLifetime {
105 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200106 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300107 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200108 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
109 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
110 optionalConstraints:nil];
111 RTC_OBJC_TYPE(RTCRtpTransceiverInit) *init =
112 [[RTC_OBJC_TYPE(RTCRtpTransceiverInit) alloc] init];
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300113
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200114 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
115 RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
116 RTC_OBJC_TYPE(RTCRtpTransceiver) * tranceiver;
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300117
118 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200119 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300120 peerConnection =
121 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
122 tranceiver = [peerConnection addTransceiverOfType:RTCRtpMediaTypeAudio init:init];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300123 XCTAssertNotNil(tranceiver);
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300124 [peerConnection close];
125 peerConnection = nil;
126 factory = nil;
127 }
128 tranceiver = nil;
129 }
130
131 XCTAssertTrue(true, "Expect test does not crash");
132}
133
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300134- (void)testRTCRtpSenderLifetime {
135 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200136 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Jeremy Leconte99d7d6b2022-08-19 09:29:55 +0200137 config.sdpSemantics = RTCSdpSemanticsPlanB;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200138 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
139 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
140 optionalConstraints:nil];
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300141
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200142 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
143 RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
144 RTC_OBJC_TYPE(RTCRtpSender) * sender;
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300145
146 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200147 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300148 peerConnection =
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300149 [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300150 sender = [peerConnection senderWithKind:kRTCMediaStreamTrackKindVideo streamId:@"stream"];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300151 XCTAssertNotNil(sender);
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300152 [peerConnection close];
153 peerConnection = nil;
154 factory = nil;
155 }
156 sender = nil;
157 }
158
159 XCTAssertTrue(true, "Expect test does not crash");
160}
161
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300162- (void)testRTCRtpReceiverLifetime {
163 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200164 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Jeremy Leconte99d7d6b2022-08-19 09:29:55 +0200165 config.sdpSemantics = RTCSdpSemanticsPlanB;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200166 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
167 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
168 optionalConstraints:nil];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300169
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200170 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
171 RTC_OBJC_TYPE(RTCPeerConnection) * pc1;
172 RTC_OBJC_TYPE(RTCPeerConnection) * pc2;
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300173
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200174 NSArray<RTC_OBJC_TYPE(RTCRtpReceiver) *> *receivers1;
175 NSArray<RTC_OBJC_TYPE(RTCRtpReceiver) *> *receivers2;
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300176
177 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200178 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300179 pc1 = [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
180 [pc1 senderWithKind:kRTCMediaStreamTrackKindAudio streamId:@"stream"];
181
182 pc2 = [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
183 [pc2 senderWithKind:kRTCMediaStreamTrackKindAudio streamId:@"stream"];
184
185 NSTimeInterval negotiationTimeout = 15;
186 XCTAssertTrue([self negotiatePeerConnection:pc1
187 withPeerConnection:pc2
188 negotiationTimeout:negotiationTimeout]);
189
190 XCTAssertEqual(pc1.signalingState, RTCSignalingStateStable);
191 XCTAssertEqual(pc2.signalingState, RTCSignalingStateStable);
192
193 receivers1 = pc1.receivers;
194 receivers2 = pc2.receivers;
195 XCTAssertTrue(receivers1.count > 0);
196 XCTAssertTrue(receivers2.count > 0);
197 [pc1 close];
198 [pc2 close];
199 pc1 = nil;
200 pc2 = nil;
201 factory = nil;
202 }
203 receivers1 = nil;
204 receivers2 = nil;
205 }
206
207 XCTAssertTrue(true, "Expect test does not crash");
208}
209
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300210- (void)testAudioSourceLifetime {
211 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200212 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
213 RTC_OBJC_TYPE(RTCAudioSource) * audioSource;
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300214
215 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200216 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300217 audioSource = [factory audioSourceWithConstraints:nil];
218 XCTAssertNotNil(audioSource);
219 factory = nil;
220 }
221 audioSource = nil;
222 }
223
224 XCTAssertTrue(true, "Expect test does not crash");
225}
226
227- (void)testVideoSourceLifetime {
228 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200229 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
230 RTC_OBJC_TYPE(RTCVideoSource) * videoSource;
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300231
232 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200233 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300234 videoSource = [factory videoSource];
235 XCTAssertNotNil(videoSource);
236 factory = nil;
237 }
238 videoSource = nil;
239 }
240
241 XCTAssertTrue(true, "Expect test does not crash");
242}
243
244- (void)testAudioTrackLifetime {
245 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200246 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
247 RTC_OBJC_TYPE(RTCAudioTrack) * audioTrack;
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300248
249 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200250 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300251 audioTrack = [factory audioTrackWithTrackId:@"audioTrack"];
252 XCTAssertNotNil(audioTrack);
253 factory = nil;
254 }
255 audioTrack = nil;
256 }
257
258 XCTAssertTrue(true, "Expect test does not crash");
259}
260
261- (void)testVideoTrackLifetime {
262 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200263 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
264 RTC_OBJC_TYPE(RTCVideoTrack) * videoTrack;
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300265
266 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200267 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300268 videoTrack = [factory videoTrackWithSource:[factory videoSource] trackId:@"videoTrack"];
269 XCTAssertNotNil(videoTrack);
270 factory = nil;
271 }
272 videoTrack = nil;
273 }
274
275 XCTAssertTrue(true, "Expect test does not crash");
276}
277
Philipp Hanckedaec4882020-09-15 10:40:07 +0200278- (void)testRollback {
279 @autoreleasepool {
280 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
281 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
282 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
283 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{
284 kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue
285 }
286 optionalConstraints:nil];
287
288 __block RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
289 __block RTC_OBJC_TYPE(RTCPeerConnection) * pc1;
290 RTCSessionDescription *rollback = [[RTCSessionDescription alloc] initWithType:RTCSdpTypeRollback
291 sdp:@""];
292
293 @autoreleasepool {
294 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
295 pc1 = [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
296 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
297 [pc1 offerForConstraints:constraints
298 completionHandler:^(RTC_OBJC_TYPE(RTCSessionDescription) * offer, NSError * error) {
299 XCTAssertNil(error);
300 XCTAssertNotNil(offer);
301
302 __weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC1 = pc1;
303 [pc1 setLocalDescription:offer
304 completionHandler:^(NSError *error) {
305 XCTAssertNil(error);
306 [weakPC1 setLocalDescription:rollback
307 completionHandler:^(NSError *error) {
308 XCTAssertNil(error);
309 }];
310 }];
311 NSTimeInterval negotiationTimeout = 15;
312 dispatch_semaphore_wait(
313 negotiatedSem,
314 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(negotiationTimeout * NSEC_PER_SEC)));
315
316 XCTAssertEqual(pc1.signalingState, RTCSignalingStateStable);
317
318 [pc1 close];
319 pc1 = nil;
320 factory = nil;
321 }];
322 }
323
324 XCTAssertTrue(true, "Expect test does not crash");
325 }
326}
327
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200328- (bool)negotiatePeerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)pc1
329 withPeerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)pc2
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300330 negotiationTimeout:(NSTimeInterval)timeout {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200331 __weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC1 = pc1;
332 __weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC2 = pc2;
333 RTC_OBJC_TYPE(RTCMediaConstraints) *sdpConstraints =
334 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300335 kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue
336 }
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200337 optionalConstraints:nil];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300338
339 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
340 [weakPC1 offerForConstraints:sdpConstraints
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200341 completionHandler:^(RTC_OBJC_TYPE(RTCSessionDescription) * offer, NSError * error) {
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300342 XCTAssertNil(error);
343 XCTAssertNotNil(offer);
344 [weakPC1
345 setLocalDescription:offer
346 completionHandler:^(NSError *error) {
347 XCTAssertNil(error);
348 [weakPC2
349 setRemoteDescription:offer
350 completionHandler:^(NSError *error) {
351 XCTAssertNil(error);
352 [weakPC2
353 answerForConstraints:sdpConstraints
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200354 completionHandler:^(
355 RTC_OBJC_TYPE(RTCSessionDescription) * answer,
356 NSError * error) {
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300357 XCTAssertNil(error);
358 XCTAssertNotNil(answer);
359 [weakPC2
360 setLocalDescription:answer
361 completionHandler:^(NSError *error) {
362 XCTAssertNil(error);
363 [weakPC1
364 setRemoteDescription:answer
365 completionHandler:^(NSError *error) {
366 XCTAssertNil(error);
367 dispatch_semaphore_signal(negotiatedSem);
368 }];
369 }];
370 }];
371 }];
372 }];
373 }];
374
375 return 0 ==
376 dispatch_semaphore_wait(negotiatedSem,
377 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)));
378}
379
Yura Yaroshevich5297bd22018-06-19 12:51:51 +0300380@end