blob: 629095b81d3fef393787646f4b2b5cfcb81e1b54 [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"
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030024
25#import <XCTest/XCTest.h>
26
27@interface RTCPeerConnectionFactoryTests : XCTestCase
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030028@end
29
30@implementation RTCPeerConnectionFactoryTests
31
32- (void)testPeerConnectionLifetime {
33 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020034 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030035
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020036 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
37 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
38 optionalConstraints:nil];
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030039
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020040 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
41 RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030042
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030043 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020044 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030045 peerConnection =
Yura Yaroshevich7a16c542018-07-11 12:55:04 +030046 [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030047 [peerConnection close];
48 factory = nil;
49 }
Yura Yaroshevich5297bd22018-06-19 12:51:51 +030050 peerConnection = nil;
51 }
52
53 XCTAssertTrue(true, @"Expect test does not crash");
54}
55
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030056- (void)testMediaStreamLifetime {
57 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020058 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
59 RTC_OBJC_TYPE(RTCMediaStream) * mediaStream;
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030060
61 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020062 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevichc806c1d2018-06-21 12:51:11 +030063 mediaStream = [factory mediaStreamWithStreamId:@"mediaStream"];
64 factory = nil;
65 }
66 mediaStream = nil;
67 }
68
69 XCTAssertTrue(true, "Expect test does not crash");
70}
71
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030072- (void)testDataChannelLifetime {
73 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020074 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
75 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
76 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
77 optionalConstraints:nil];
78 RTC_OBJC_TYPE(RTCDataChannelConfiguration) *dataChannelConfig =
79 [[RTC_OBJC_TYPE(RTCDataChannelConfiguration) alloc] init];
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030080
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020081 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
82 RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
83 RTC_OBJC_TYPE(RTCDataChannel) * dataChannel;
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030084
85 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020086 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030087 peerConnection =
Yura Yaroshevich7a16c542018-07-11 12:55:04 +030088 [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030089 dataChannel =
90 [peerConnection dataChannelForLabel:@"test_channel" configuration:dataChannelConfig];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +030091 XCTAssertNotNil(dataChannel);
Yura Yaroshevichc75b35a2018-06-27 17:09:14 +030092 [peerConnection close];
93 peerConnection = nil;
94 factory = nil;
95 }
96 dataChannel = nil;
97 }
98
99 XCTAssertTrue(true, "Expect test does not crash");
100}
101
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300102- (void)testRTCRtpTransceiverLifetime {
103 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200104 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300105 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200106 RTC_OBJC_TYPE(RTCMediaConstraints) *contraints =
107 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
108 optionalConstraints:nil];
109 RTC_OBJC_TYPE(RTCRtpTransceiverInit) *init =
110 [[RTC_OBJC_TYPE(RTCRtpTransceiverInit) alloc] init];
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300111
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200112 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
113 RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
114 RTC_OBJC_TYPE(RTCRtpTransceiver) * tranceiver;
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300115
116 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200117 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300118 peerConnection =
119 [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil];
120 tranceiver = [peerConnection addTransceiverOfType:RTCRtpMediaTypeAudio init:init];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300121 XCTAssertNotNil(tranceiver);
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300122 [peerConnection close];
123 peerConnection = nil;
124 factory = nil;
125 }
126 tranceiver = nil;
127 }
128
129 XCTAssertTrue(true, "Expect test does not crash");
130}
131
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300132- (void)testRTCRtpSenderLifetime {
133 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200134 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
135 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
136 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
137 optionalConstraints:nil];
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300138
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200139 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
140 RTC_OBJC_TYPE(RTCPeerConnection) * peerConnection;
141 RTC_OBJC_TYPE(RTCRtpSender) * sender;
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300142
143 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200144 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300145 peerConnection =
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300146 [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300147 sender = [peerConnection senderWithKind:kRTCMediaStreamTrackKindVideo streamId:@"stream"];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300148 XCTAssertNotNil(sender);
Yura Yaroshevichef43aaf2018-07-09 19:16:32 +0300149 [peerConnection close];
150 peerConnection = nil;
151 factory = nil;
152 }
153 sender = nil;
154 }
155
156 XCTAssertTrue(true, "Expect test does not crash");
157}
158
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300159- (void)testRTCRtpReceiverLifetime {
160 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200161 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
162 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
163 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{}
164 optionalConstraints:nil];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300165
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200166 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
167 RTC_OBJC_TYPE(RTCPeerConnection) * pc1;
168 RTC_OBJC_TYPE(RTCPeerConnection) * pc2;
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300169
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200170 NSArray<RTC_OBJC_TYPE(RTCRtpReceiver) *> *receivers1;
171 NSArray<RTC_OBJC_TYPE(RTCRtpReceiver) *> *receivers2;
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300172
173 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200174 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300175 pc1 = [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
176 [pc1 senderWithKind:kRTCMediaStreamTrackKindAudio streamId:@"stream"];
177
178 pc2 = [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
179 [pc2 senderWithKind:kRTCMediaStreamTrackKindAudio streamId:@"stream"];
180
181 NSTimeInterval negotiationTimeout = 15;
182 XCTAssertTrue([self negotiatePeerConnection:pc1
183 withPeerConnection:pc2
184 negotiationTimeout:negotiationTimeout]);
185
186 XCTAssertEqual(pc1.signalingState, RTCSignalingStateStable);
187 XCTAssertEqual(pc2.signalingState, RTCSignalingStateStable);
188
189 receivers1 = pc1.receivers;
190 receivers2 = pc2.receivers;
191 XCTAssertTrue(receivers1.count > 0);
192 XCTAssertTrue(receivers2.count > 0);
193 [pc1 close];
194 [pc2 close];
195 pc1 = nil;
196 pc2 = nil;
197 factory = nil;
198 }
199 receivers1 = nil;
200 receivers2 = nil;
201 }
202
203 XCTAssertTrue(true, "Expect test does not crash");
204}
205
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300206- (void)testAudioSourceLifetime {
207 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200208 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
209 RTC_OBJC_TYPE(RTCAudioSource) * audioSource;
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300210
211 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200212 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300213 audioSource = [factory audioSourceWithConstraints:nil];
214 XCTAssertNotNil(audioSource);
215 factory = nil;
216 }
217 audioSource = nil;
218 }
219
220 XCTAssertTrue(true, "Expect test does not crash");
221}
222
223- (void)testVideoSourceLifetime {
224 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200225 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
226 RTC_OBJC_TYPE(RTCVideoSource) * videoSource;
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300227
228 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200229 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300230 videoSource = [factory videoSource];
231 XCTAssertNotNil(videoSource);
232 factory = nil;
233 }
234 videoSource = nil;
235 }
236
237 XCTAssertTrue(true, "Expect test does not crash");
238}
239
240- (void)testAudioTrackLifetime {
241 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200242 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
243 RTC_OBJC_TYPE(RTCAudioTrack) * audioTrack;
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300244
245 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200246 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300247 audioTrack = [factory audioTrackWithTrackId:@"audioTrack"];
248 XCTAssertNotNil(audioTrack);
249 factory = nil;
250 }
251 audioTrack = nil;
252 }
253
254 XCTAssertTrue(true, "Expect test does not crash");
255}
256
257- (void)testVideoTrackLifetime {
258 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200259 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
260 RTC_OBJC_TYPE(RTCVideoTrack) * videoTrack;
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300261
262 @autoreleasepool {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200263 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300264 videoTrack = [factory videoTrackWithSource:[factory videoSource] trackId:@"videoTrack"];
265 XCTAssertNotNil(videoTrack);
266 factory = nil;
267 }
268 videoTrack = nil;
269 }
270
271 XCTAssertTrue(true, "Expect test does not crash");
272}
273
Philipp Hanckedaec4882020-09-15 10:40:07 +0200274- (void)testRollback {
275 @autoreleasepool {
276 RTC_OBJC_TYPE(RTCConfiguration) *config = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
277 config.sdpSemantics = RTCSdpSemanticsUnifiedPlan;
278 RTC_OBJC_TYPE(RTCMediaConstraints) *constraints =
279 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{
280 kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue
281 }
282 optionalConstraints:nil];
283
284 __block RTC_OBJC_TYPE(RTCPeerConnectionFactory) * factory;
285 __block RTC_OBJC_TYPE(RTCPeerConnection) * pc1;
286 RTCSessionDescription *rollback = [[RTCSessionDescription alloc] initWithType:RTCSdpTypeRollback
287 sdp:@""];
288
289 @autoreleasepool {
290 factory = [[RTC_OBJC_TYPE(RTCPeerConnectionFactory) alloc] init];
291 pc1 = [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil];
292 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
293 [pc1 offerForConstraints:constraints
294 completionHandler:^(RTC_OBJC_TYPE(RTCSessionDescription) * offer, NSError * error) {
295 XCTAssertNil(error);
296 XCTAssertNotNil(offer);
297
298 __weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC1 = pc1;
299 [pc1 setLocalDescription:offer
300 completionHandler:^(NSError *error) {
301 XCTAssertNil(error);
302 [weakPC1 setLocalDescription:rollback
303 completionHandler:^(NSError *error) {
304 XCTAssertNil(error);
305 }];
306 }];
307 NSTimeInterval negotiationTimeout = 15;
308 dispatch_semaphore_wait(
309 negotiatedSem,
310 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(negotiationTimeout * NSEC_PER_SEC)));
311
312 XCTAssertEqual(pc1.signalingState, RTCSignalingStateStable);
313
314 [pc1 close];
315 pc1 = nil;
316 factory = nil;
317 }];
318 }
319
320 XCTAssertTrue(true, "Expect test does not crash");
321 }
322}
323
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200324- (bool)negotiatePeerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)pc1
325 withPeerConnection:(RTC_OBJC_TYPE(RTCPeerConnection) *)pc2
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300326 negotiationTimeout:(NSTimeInterval)timeout {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200327 __weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC1 = pc1;
328 __weak RTC_OBJC_TYPE(RTCPeerConnection) *weakPC2 = pc2;
329 RTC_OBJC_TYPE(RTCMediaConstraints) *sdpConstraints =
330 [[RTC_OBJC_TYPE(RTCMediaConstraints) alloc] initWithMandatoryConstraints:@{
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300331 kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue
332 }
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200333 optionalConstraints:nil];
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300334
335 dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0);
336 [weakPC1 offerForConstraints:sdpConstraints
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200337 completionHandler:^(RTC_OBJC_TYPE(RTCSessionDescription) * offer, NSError * error) {
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300338 XCTAssertNil(error);
339 XCTAssertNotNil(offer);
340 [weakPC1
341 setLocalDescription:offer
342 completionHandler:^(NSError *error) {
343 XCTAssertNil(error);
344 [weakPC2
345 setRemoteDescription:offer
346 completionHandler:^(NSError *error) {
347 XCTAssertNil(error);
348 [weakPC2
349 answerForConstraints:sdpConstraints
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200350 completionHandler:^(
351 RTC_OBJC_TYPE(RTCSessionDescription) * answer,
352 NSError * error) {
Yura Yaroshevich7a16c542018-07-11 12:55:04 +0300353 XCTAssertNil(error);
354 XCTAssertNotNil(answer);
355 [weakPC2
356 setLocalDescription:answer
357 completionHandler:^(NSError *error) {
358 XCTAssertNil(error);
359 [weakPC1
360 setRemoteDescription:answer
361 completionHandler:^(NSError *error) {
362 XCTAssertNil(error);
363 dispatch_semaphore_signal(negotiatedSem);
364 }];
365 }];
366 }];
367 }];
368 }];
369 }];
370
371 return 0 ==
372 dispatch_semaphore_wait(negotiatedSem,
373 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)));
374}
375
Yura Yaroshevich5297bd22018-06-19 12:51:51 +0300376@end