Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 1 | /* |
| 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 Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 11 | #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" |
| 22 | #import "api/peerconnection/RTCVideoSource.h" |
Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 23 | |
| 24 | #import <XCTest/XCTest.h> |
| 25 | |
| 26 | @interface RTCPeerConnectionFactoryTests : XCTestCase |
Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 27 | @end |
| 28 | |
| 29 | @implementation RTCPeerConnectionFactoryTests |
| 30 | |
| 31 | - (void)testPeerConnectionLifetime { |
| 32 | @autoreleasepool { |
| 33 | RTCConfiguration *config = [[RTCConfiguration alloc] init]; |
| 34 | |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 35 | RTCMediaConstraints *constraints = |
Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 36 | [[RTCMediaConstraints alloc] initWithMandatoryConstraints:@{} optionalConstraints:nil]; |
| 37 | |
Yura Yaroshevich | c806c1d | 2018-06-21 12:51:11 +0300 | [diff] [blame] | 38 | RTCPeerConnectionFactory *factory; |
| 39 | RTCPeerConnection *peerConnection; |
Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 40 | |
Yura Yaroshevich | c806c1d | 2018-06-21 12:51:11 +0300 | [diff] [blame] | 41 | @autoreleasepool { |
| 42 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 43 | peerConnection = |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 44 | [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil]; |
Yura Yaroshevich | c806c1d | 2018-06-21 12:51:11 +0300 | [diff] [blame] | 45 | [peerConnection close]; |
| 46 | factory = nil; |
| 47 | } |
Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 48 | peerConnection = nil; |
| 49 | } |
| 50 | |
| 51 | XCTAssertTrue(true, @"Expect test does not crash"); |
| 52 | } |
| 53 | |
Yura Yaroshevich | c806c1d | 2018-06-21 12:51:11 +0300 | [diff] [blame] | 54 | - (void)testMediaStreamLifetime { |
| 55 | @autoreleasepool { |
| 56 | RTCPeerConnectionFactory *factory; |
| 57 | RTCMediaStream *mediaStream; |
| 58 | |
| 59 | @autoreleasepool { |
| 60 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 61 | mediaStream = [factory mediaStreamWithStreamId:@"mediaStream"]; |
| 62 | factory = nil; |
| 63 | } |
| 64 | mediaStream = nil; |
| 65 | } |
| 66 | |
| 67 | XCTAssertTrue(true, "Expect test does not crash"); |
| 68 | } |
| 69 | |
Yura Yaroshevich | c75b35a | 2018-06-27 17:09:14 +0300 | [diff] [blame] | 70 | - (void)testDataChannelLifetime { |
| 71 | @autoreleasepool { |
| 72 | RTCConfiguration *config = [[RTCConfiguration alloc] init]; |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 73 | RTCMediaConstraints *constraints = |
Yura Yaroshevich | c75b35a | 2018-06-27 17:09:14 +0300 | [diff] [blame] | 74 | [[RTCMediaConstraints alloc] initWithMandatoryConstraints:@{} optionalConstraints:nil]; |
| 75 | RTCDataChannelConfiguration *dataChannelConfig = [[RTCDataChannelConfiguration alloc] init]; |
| 76 | |
| 77 | RTCPeerConnectionFactory *factory; |
| 78 | RTCPeerConnection *peerConnection; |
| 79 | RTCDataChannel *dataChannel; |
| 80 | |
| 81 | @autoreleasepool { |
| 82 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 83 | peerConnection = |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 84 | [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil]; |
Yura Yaroshevich | c75b35a | 2018-06-27 17:09:14 +0300 | [diff] [blame] | 85 | dataChannel = |
| 86 | [peerConnection dataChannelForLabel:@"test_channel" configuration:dataChannelConfig]; |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 87 | XCTAssertNotNil(dataChannel); |
Yura Yaroshevich | c75b35a | 2018-06-27 17:09:14 +0300 | [diff] [blame] | 88 | [peerConnection close]; |
| 89 | peerConnection = nil; |
| 90 | factory = nil; |
| 91 | } |
| 92 | dataChannel = nil; |
| 93 | } |
| 94 | |
| 95 | XCTAssertTrue(true, "Expect test does not crash"); |
| 96 | } |
| 97 | |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 98 | - (void)testRTCRtpTransceiverLifetime { |
| 99 | @autoreleasepool { |
| 100 | RTCConfiguration *config = [[RTCConfiguration alloc] init]; |
| 101 | config.sdpSemantics = RTCSdpSemanticsUnifiedPlan; |
| 102 | RTCMediaConstraints *contraints = |
| 103 | [[RTCMediaConstraints alloc] initWithMandatoryConstraints:@{} optionalConstraints:nil]; |
| 104 | RTCRtpTransceiverInit *init = [[RTCRtpTransceiverInit alloc] init]; |
| 105 | |
| 106 | RTCPeerConnectionFactory *factory; |
| 107 | RTCPeerConnection *peerConnection; |
| 108 | RTCRtpTransceiver *tranceiver; |
| 109 | |
| 110 | @autoreleasepool { |
| 111 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 112 | peerConnection = |
| 113 | [factory peerConnectionWithConfiguration:config constraints:contraints delegate:nil]; |
| 114 | tranceiver = [peerConnection addTransceiverOfType:RTCRtpMediaTypeAudio init:init]; |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 115 | XCTAssertNotNil(tranceiver); |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 116 | [peerConnection close]; |
| 117 | peerConnection = nil; |
| 118 | factory = nil; |
| 119 | } |
| 120 | tranceiver = nil; |
| 121 | } |
| 122 | |
| 123 | XCTAssertTrue(true, "Expect test does not crash"); |
| 124 | } |
| 125 | |
Yura Yaroshevich | ef43aaf | 2018-07-09 19:16:32 +0300 | [diff] [blame] | 126 | - (void)testRTCRtpSenderLifetime { |
| 127 | @autoreleasepool { |
| 128 | RTCConfiguration *config = [[RTCConfiguration alloc] init]; |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 129 | RTCMediaConstraints *constraints = |
Yura Yaroshevich | ef43aaf | 2018-07-09 19:16:32 +0300 | [diff] [blame] | 130 | [[RTCMediaConstraints alloc] initWithMandatoryConstraints:@{} optionalConstraints:nil]; |
| 131 | |
| 132 | RTCPeerConnectionFactory *factory; |
| 133 | RTCPeerConnection *peerConnection; |
| 134 | RTCRtpSender *sender; |
| 135 | |
| 136 | @autoreleasepool { |
| 137 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 138 | peerConnection = |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 139 | [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil]; |
Yura Yaroshevich | ef43aaf | 2018-07-09 19:16:32 +0300 | [diff] [blame] | 140 | sender = [peerConnection senderWithKind:kRTCMediaStreamTrackKindVideo streamId:@"stream"]; |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 141 | XCTAssertNotNil(sender); |
Yura Yaroshevich | ef43aaf | 2018-07-09 19:16:32 +0300 | [diff] [blame] | 142 | [peerConnection close]; |
| 143 | peerConnection = nil; |
| 144 | factory = nil; |
| 145 | } |
| 146 | sender = nil; |
| 147 | } |
| 148 | |
| 149 | XCTAssertTrue(true, "Expect test does not crash"); |
| 150 | } |
| 151 | |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 152 | - (void)testRTCRtpReceiverLifetime { |
| 153 | @autoreleasepool { |
| 154 | RTCConfiguration *config = [[RTCConfiguration alloc] init]; |
| 155 | RTCMediaConstraints *constraints = |
| 156 | [[RTCMediaConstraints alloc] initWithMandatoryConstraints:@{} optionalConstraints:nil]; |
| 157 | |
| 158 | RTCPeerConnectionFactory *factory; |
| 159 | RTCPeerConnection *pc1; |
| 160 | RTCPeerConnection *pc2; |
| 161 | |
| 162 | NSArray<RTCRtpReceiver *> *receivers1; |
| 163 | NSArray<RTCRtpReceiver *> *receivers2; |
| 164 | |
| 165 | @autoreleasepool { |
| 166 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 167 | pc1 = [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil]; |
| 168 | [pc1 senderWithKind:kRTCMediaStreamTrackKindAudio streamId:@"stream"]; |
| 169 | |
| 170 | pc2 = [factory peerConnectionWithConfiguration:config constraints:constraints delegate:nil]; |
| 171 | [pc2 senderWithKind:kRTCMediaStreamTrackKindAudio streamId:@"stream"]; |
| 172 | |
| 173 | NSTimeInterval negotiationTimeout = 15; |
| 174 | XCTAssertTrue([self negotiatePeerConnection:pc1 |
| 175 | withPeerConnection:pc2 |
| 176 | negotiationTimeout:negotiationTimeout]); |
| 177 | |
| 178 | XCTAssertEqual(pc1.signalingState, RTCSignalingStateStable); |
| 179 | XCTAssertEqual(pc2.signalingState, RTCSignalingStateStable); |
| 180 | |
| 181 | receivers1 = pc1.receivers; |
| 182 | receivers2 = pc2.receivers; |
| 183 | XCTAssertTrue(receivers1.count > 0); |
| 184 | XCTAssertTrue(receivers2.count > 0); |
| 185 | [pc1 close]; |
| 186 | [pc2 close]; |
| 187 | pc1 = nil; |
| 188 | pc2 = nil; |
| 189 | factory = nil; |
| 190 | } |
| 191 | receivers1 = nil; |
| 192 | receivers2 = nil; |
| 193 | } |
| 194 | |
| 195 | XCTAssertTrue(true, "Expect test does not crash"); |
| 196 | } |
| 197 | |
Yura Yaroshevich | 01cee07 | 2018-07-11 15:35:40 +0300 | [diff] [blame] | 198 | - (void)testAudioSourceLifetime { |
| 199 | @autoreleasepool { |
| 200 | RTCPeerConnectionFactory *factory; |
| 201 | RTCAudioSource *audioSource; |
| 202 | |
| 203 | @autoreleasepool { |
| 204 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 205 | audioSource = [factory audioSourceWithConstraints:nil]; |
| 206 | XCTAssertNotNil(audioSource); |
| 207 | factory = nil; |
| 208 | } |
| 209 | audioSource = nil; |
| 210 | } |
| 211 | |
| 212 | XCTAssertTrue(true, "Expect test does not crash"); |
| 213 | } |
| 214 | |
| 215 | - (void)testVideoSourceLifetime { |
| 216 | @autoreleasepool { |
| 217 | RTCPeerConnectionFactory *factory; |
| 218 | RTCVideoSource *videoSource; |
| 219 | |
| 220 | @autoreleasepool { |
| 221 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 222 | videoSource = [factory videoSource]; |
| 223 | XCTAssertNotNil(videoSource); |
| 224 | factory = nil; |
| 225 | } |
| 226 | videoSource = nil; |
| 227 | } |
| 228 | |
| 229 | XCTAssertTrue(true, "Expect test does not crash"); |
| 230 | } |
| 231 | |
| 232 | - (void)testAudioTrackLifetime { |
| 233 | @autoreleasepool { |
| 234 | RTCPeerConnectionFactory *factory; |
| 235 | RTCAudioTrack *audioTrack; |
| 236 | |
| 237 | @autoreleasepool { |
| 238 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 239 | audioTrack = [factory audioTrackWithTrackId:@"audioTrack"]; |
| 240 | XCTAssertNotNil(audioTrack); |
| 241 | factory = nil; |
| 242 | } |
| 243 | audioTrack = nil; |
| 244 | } |
| 245 | |
| 246 | XCTAssertTrue(true, "Expect test does not crash"); |
| 247 | } |
| 248 | |
| 249 | - (void)testVideoTrackLifetime { |
| 250 | @autoreleasepool { |
| 251 | RTCPeerConnectionFactory *factory; |
| 252 | RTCVideoTrack *videoTrack; |
| 253 | |
| 254 | @autoreleasepool { |
| 255 | factory = [[RTCPeerConnectionFactory alloc] init]; |
| 256 | videoTrack = [factory videoTrackWithSource:[factory videoSource] trackId:@"videoTrack"]; |
| 257 | XCTAssertNotNil(videoTrack); |
| 258 | factory = nil; |
| 259 | } |
| 260 | videoTrack = nil; |
| 261 | } |
| 262 | |
| 263 | XCTAssertTrue(true, "Expect test does not crash"); |
| 264 | } |
| 265 | |
Yura Yaroshevich | 7a16c54 | 2018-07-11 12:55:04 +0300 | [diff] [blame] | 266 | - (bool)negotiatePeerConnection:(RTCPeerConnection *)pc1 |
| 267 | withPeerConnection:(RTCPeerConnection *)pc2 |
| 268 | negotiationTimeout:(NSTimeInterval)timeout { |
| 269 | __weak RTCPeerConnection *weakPC1 = pc1; |
| 270 | __weak RTCPeerConnection *weakPC2 = pc2; |
| 271 | RTCMediaConstraints *sdpConstraints = |
| 272 | [[RTCMediaConstraints alloc] initWithMandatoryConstraints:@{ |
| 273 | kRTCMediaConstraintsOfferToReceiveAudio : kRTCMediaConstraintsValueTrue |
| 274 | } |
| 275 | optionalConstraints:nil]; |
| 276 | |
| 277 | dispatch_semaphore_t negotiatedSem = dispatch_semaphore_create(0); |
| 278 | [weakPC1 offerForConstraints:sdpConstraints |
| 279 | completionHandler:^(RTCSessionDescription *offer, NSError *error) { |
| 280 | XCTAssertNil(error); |
| 281 | XCTAssertNotNil(offer); |
| 282 | [weakPC1 |
| 283 | setLocalDescription:offer |
| 284 | completionHandler:^(NSError *error) { |
| 285 | XCTAssertNil(error); |
| 286 | [weakPC2 |
| 287 | setRemoteDescription:offer |
| 288 | completionHandler:^(NSError *error) { |
| 289 | XCTAssertNil(error); |
| 290 | [weakPC2 |
| 291 | answerForConstraints:sdpConstraints |
| 292 | completionHandler:^(RTCSessionDescription *answer, |
| 293 | NSError *error) { |
| 294 | XCTAssertNil(error); |
| 295 | XCTAssertNotNil(answer); |
| 296 | [weakPC2 |
| 297 | setLocalDescription:answer |
| 298 | completionHandler:^(NSError *error) { |
| 299 | XCTAssertNil(error); |
| 300 | [weakPC1 |
| 301 | setRemoteDescription:answer |
| 302 | completionHandler:^(NSError *error) { |
| 303 | XCTAssertNil(error); |
| 304 | dispatch_semaphore_signal(negotiatedSem); |
| 305 | }]; |
| 306 | }]; |
| 307 | }]; |
| 308 | }]; |
| 309 | }]; |
| 310 | }]; |
| 311 | |
| 312 | return 0 == |
| 313 | dispatch_semaphore_wait(negotiatedSem, |
| 314 | dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))); |
| 315 | } |
| 316 | |
Yura Yaroshevich | 5297bd2 | 2018-06-19 12:51:51 +0300 | [diff] [blame] | 317 | @end |