blob: c43a6fcf24676f513ed4d6116935c67563119551 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2013 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#import <Foundation/Foundation.h>
29
30#import "RTCICEServer.h"
31#import "RTCMediaConstraints.h"
32#import "RTCMediaStream.h"
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000033#import "RTCPair.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#import "RTCPeerConnection.h"
35#import "RTCPeerConnectionFactory.h"
36#import "RTCPeerConnectionSyncObserver.h"
37#import "RTCSessionDescription.h"
38#import "RTCSessionDescriptionSyncObserver.h"
39#import "RTCVideoRenderer.h"
40#import "RTCVideoTrack.h"
41
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000042#include "webrtc/base/gunit.h"
43#include "webrtc/base/ssladapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45#if !defined(__has_feature) || !__has_feature(objc_arc)
46#error "This file requires ARC support."
47#endif
48
deadbeef4fa648b2015-09-28 14:08:17 -070049const NSTimeInterval kRTCPeerConnectionTestTimeout = 20;
50
tkchin@webrtc.org81257442014-11-04 23:06:15 +000051@interface RTCFakeRenderer : NSObject <RTCVideoRenderer>
52@end
53
54@implementation RTCFakeRenderer
55
56- (void)setSize:(CGSize)size {}
57- (void)renderFrame:(RTCI420Frame*)frame {}
58
59@end
60
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061@interface RTCPeerConnectionTest : NSObject
62
63// Returns whether the two sessions are of the same type.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000064+ (BOOL)isSession:(RTCSessionDescription*)session1
65 ofSameTypeAsSession:(RTCSessionDescription*)session2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
67// Create and add tracks to pc, with the given source, label, and IDs
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000068- (RTCMediaStream*)addTracksToPeerConnection:(RTCPeerConnection*)pc
69 withFactory:(RTCPeerConnectionFactory*)factory
70 videoSource:(RTCVideoSource*)videoSource
71 streamLabel:(NSString*)streamLabel
72 videoTrackID:(NSString*)videoTrackID
73 audioTrackID:(NSString*)audioTrackID;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
fischman@webrtc.org385a7222014-03-25 05:16:29 +000075- (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77@end
78
79@implementation RTCPeerConnectionTest
80
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000081+ (BOOL)isSession:(RTCSessionDescription*)session1
82 ofSameTypeAsSession:(RTCSessionDescription*)session2 {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 return [session1.type isEqual:session2.type];
84}
85
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000086- (RTCMediaStream*)addTracksToPeerConnection:(RTCPeerConnection*)pc
87 withFactory:(RTCPeerConnectionFactory*)factory
88 videoSource:(RTCVideoSource*)videoSource
89 streamLabel:(NSString*)streamLabel
90 videoTrackID:(NSString*)videoTrackID
91 audioTrackID:(NSString*)audioTrackID {
92 RTCMediaStream* localMediaStream = [factory mediaStreamWithLabel:streamLabel];
93 RTCVideoTrack* videoTrack =
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 [factory videoTrackWithID:videoTrackID source:videoSource];
tkchin@webrtc.org81257442014-11-04 23:06:15 +000095 RTCFakeRenderer* videoRenderer = [[RTCFakeRenderer alloc] init];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 [videoTrack addRenderer:videoRenderer];
97 [localMediaStream addVideoTrack:videoTrack];
98 // Test that removal/re-add works.
99 [localMediaStream removeVideoTrack:videoTrack];
100 [localMediaStream addVideoTrack:videoTrack];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000101 RTCAudioTrack* audioTrack = [factory audioTrackWithID:audioTrackID];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 [localMediaStream addAudioTrack:audioTrack];
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000103 [pc addStream:localMediaStream];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 return localMediaStream;
105}
106
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000107- (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory {
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000108 NSArray* mandatory = @[
109 [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement" value:@"true"],
110 [[RTCPair alloc] initWithKey:@"internalSctpDataChannels" value:@"true"],
111 ];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000112 RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] init];
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000113 RTCMediaConstraints* pcConstraints =
114 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatory
115 optionalConstraints:nil];
116
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000117 RTCPeerConnectionSyncObserver* offeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000119 RTCPeerConnection* pcOffer =
120 [factory peerConnectionWithICEServers:nil
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000121 constraints:pcConstraints
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 delegate:offeringExpectations];
123
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000124 RTCPeerConnectionSyncObserver* answeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000126
127 RTCPeerConnection* pcAnswer =
128 [factory peerConnectionWithICEServers:nil
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000129 constraints:pcConstraints
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 delegate:answeringExpectations];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 // TODO(hughv): Create video capturer
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000132 RTCVideoCapturer* capturer = nil;
133 RTCVideoSource* videoSource =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 [factory videoSourceWithCapturer:capturer constraints:constraints];
135
136 // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS"
137 // refers to the answerer's local media stream, with suffixes of "a0" and "v0"
138 // for audio and video tracks, resp. These mirror chrome historical naming.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000139 RTCMediaStream* oLMSUnused = [self addTracksToPeerConnection:pcOffer
140 withFactory:factory
141 videoSource:videoSource
142 streamLabel:@"oLMS"
143 videoTrackID:@"oLMSv0"
144 audioTrackID:@"oLMSa0"];
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000145
146 RTCDataChannel* offerDC =
147 [pcOffer createDataChannelWithLabel:@"offerDC"
148 config:[[RTCDataChannelInit alloc] init]];
149 EXPECT_TRUE([offerDC.label isEqual:@"offerDC"]);
150 offerDC.delegate = offeringExpectations;
151 offeringExpectations.dataChannel = offerDC;
152
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000153 RTCSessionDescriptionSyncObserver* sdpObserver =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 [[RTCSessionDescriptionSyncObserver alloc] init];
155 [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints];
156 [sdpObserver wait];
157 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000158 RTCSessionDescription* offerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch],
160 NSOrderedSame);
161 EXPECT_GT([offerSDP.description length], 0);
162
163 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000164 [answeringExpectations expectSignalingChange:RTCSignalingHaveRemoteOffer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 [answeringExpectations expectAddStream:@"oLMS"];
166 [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver
167 sessionDescription:offerSDP];
168 [sdpObserver wait];
169
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000170 RTCMediaStream* aLMSUnused = [self addTracksToPeerConnection:pcAnswer
171 withFactory:factory
172 videoSource:videoSource
173 streamLabel:@"aLMS"
174 videoTrackID:@"aLMSv0"
175 audioTrackID:@"aLMSa0"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
177 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
178 [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints];
179 [sdpObserver wait];
180 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000181 RTCSessionDescription* answerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch],
183 NSOrderedSame);
184 EXPECT_GT([answerSDP.description length], 0);
185
186 [offeringExpectations expectICECandidates:2];
deadbeefcbecd352015-09-23 11:50:27 -0700187 // It's possible to only have 1 ICE candidate for the answerer, since we use
188 // BUNDLE and rtcp-mux by default, and don't provide any ICE servers in this
189 // test.
190 [answeringExpectations expectICECandidates:1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191
192 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
193 [answeringExpectations expectSignalingChange:RTCSignalingStable];
194 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
195 sessionDescription:answerSDP];
196 [sdpObserver wait];
197 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
198
199 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
200 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
201 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
202 sessionDescription:offerSDP];
203 [sdpObserver wait];
204 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
205
206 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
207 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
fischman@webrtc.orga01daf02014-03-08 03:17:55 +0000208 // TODO(fischman): figure out why this is flaky and re-introduce (and remove
209 // special-casing from the observer!).
210 // [offeringExpectations expectICEConnectionChange:RTCICEConnectionCompleted];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
212 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
213
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000214 [offeringExpectations expectStateChange:kRTCDataChannelStateOpen];
215 [answeringExpectations expectDataChannel:@"offerDC"];
216 [answeringExpectations expectStateChange:kRTCDataChannelStateOpen];
217
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
219 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
220
221 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
222 [offeringExpectations expectSignalingChange:RTCSignalingStable];
223 [offeringExpectations expectAddStream:@"aLMS"];
224 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
225 sessionDescription:answerSDP];
226 [sdpObserver wait];
227 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
228
229 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
230 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
231 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
232 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
233
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000234 for (RTCICECandidate* candidate in offeringExpectations
235 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 [pcAnswer addICECandidate:candidate];
237 }
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000238 for (RTCICECandidate* candidate in answeringExpectations
239 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 [pcOffer addICECandidate:candidate];
241 }
242
deadbeef4fa648b2015-09-28 14:08:17 -0700243 EXPECT_TRUE(
244 [offeringExpectations waitForAllExpectationsToBeSatisfiedWithTimeout:
245 kRTCPeerConnectionTestTimeout]);
246 EXPECT_TRUE(
247 [answeringExpectations waitForAllExpectationsToBeSatisfiedWithTimeout:
248 kRTCPeerConnectionTestTimeout]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000250 EXPECT_EQ(pcOffer.signalingState, RTCSignalingStable);
251 EXPECT_EQ(pcAnswer.signalingState, RTCSignalingStable);
252
253 // Test send and receive UTF-8 text
254 NSString* text = @"你好";
255 NSData* textData = [text dataUsingEncoding:NSUTF8StringEncoding];
256 RTCDataBuffer* buffer =
257 [[RTCDataBuffer alloc] initWithData:textData isBinary:NO];
258 [answeringExpectations expectMessage:[textData copy] isBinary:NO];
259 EXPECT_TRUE([offeringExpectations.dataChannel sendData:buffer]);
deadbeef4fa648b2015-09-28 14:08:17 -0700260 EXPECT_TRUE(
261 [answeringExpectations waitForAllExpectationsToBeSatisfiedWithTimeout:
262 kRTCPeerConnectionTestTimeout]);
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000263
264 // Test send and receive binary data
265 const size_t byteLength = 5;
266 char bytes[byteLength] = {1, 2, 3, 4, 5};
267 NSData* byteData = [NSData dataWithBytes:bytes length:byteLength];
268 buffer = [[RTCDataBuffer alloc] initWithData:byteData isBinary:YES];
269 [answeringExpectations expectMessage:[byteData copy] isBinary:YES];
270 EXPECT_TRUE([offeringExpectations.dataChannel sendData:buffer]);
deadbeef4fa648b2015-09-28 14:08:17 -0700271 EXPECT_TRUE(
272 [answeringExpectations waitForAllExpectationsToBeSatisfiedWithTimeout:
273 kRTCPeerConnectionTestTimeout]);
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000274
275 [offeringExpectations expectStateChange:kRTCDataChannelStateClosing];
276 [answeringExpectations expectStateChange:kRTCDataChannelStateClosing];
277 [offeringExpectations expectStateChange:kRTCDataChannelStateClosed];
278 [answeringExpectations expectStateChange:kRTCDataChannelStateClosed];
279
280 [answeringExpectations.dataChannel close];
281 [offeringExpectations.dataChannel close];
282
deadbeef4fa648b2015-09-28 14:08:17 -0700283 EXPECT_TRUE(
284 [offeringExpectations waitForAllExpectationsToBeSatisfiedWithTimeout:
285 kRTCPeerConnectionTestTimeout]);
286 EXPECT_TRUE(
287 [answeringExpectations waitForAllExpectationsToBeSatisfiedWithTimeout:
288 kRTCPeerConnectionTestTimeout]);
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000289 // Don't need to listen to further state changes.
290 // TODO(tkchin): figure out why Closed->Closing without this.
291 offeringExpectations.dataChannel.delegate = nil;
292 answeringExpectations.dataChannel.delegate = nil;
293
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000294 // Let the audio feedback run for 2s to allow human testing and to ensure
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 // things stabilize. TODO(fischman): replace seconds with # of video frames,
296 // when we have video flowing.
297 [[NSRunLoop currentRunLoop]
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000298 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000300 [offeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
301 [answeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
302 [offeringExpectations expectSignalingChange:RTCSignalingClosed];
303 [answeringExpectations expectSignalingChange:RTCSignalingClosed];
304
305 [pcOffer close];
306 [pcAnswer close];
307
deadbeef4fa648b2015-09-28 14:08:17 -0700308 EXPECT_TRUE(
309 [offeringExpectations waitForAllExpectationsToBeSatisfiedWithTimeout:
310 kRTCPeerConnectionTestTimeout]);
311 EXPECT_TRUE(
312 [answeringExpectations waitForAllExpectationsToBeSatisfiedWithTimeout:
313 kRTCPeerConnectionTestTimeout]);
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000314
315 capturer = nil;
316 videoSource = nil;
317 pcOffer = nil;
318 pcAnswer = nil;
319 // TODO(fischman): be stricter about shutdown checks; ensure thread
320 // counts return to where they were before the test kicked off, and
321 // that all objects have in fact shut down.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322}
323
324@end
325
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000326// TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of
327// RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being
328// a TestBase since it's not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329TEST(RTCPeerConnectionTest, SessionTest) {
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000330 @autoreleasepool {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000331 rtc::InitializeSSL();
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000332 // Since |factory| will own the signaling & worker threads, it's important
333 // that it outlive the created PeerConnections since they self-delete on the
334 // signaling thread, and if |factory| is freed first then a last refcount on
335 // the factory will expire during this teardown, causing the signaling
336 // thread to try to Join() with itself. This is a hack to ensure that the
337 // factory outlives RTCPeerConnection:dealloc.
338 // See https://code.google.com/p/webrtc/issues/detail?id=3100.
339 RTCPeerConnectionFactory* factory = [[RTCPeerConnectionFactory alloc] init];
340 @autoreleasepool {
341 RTCPeerConnectionTest* pcTest = [[RTCPeerConnectionTest alloc] init];
342 [pcTest testCompleteSessionWithFactory:factory];
343 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000344 rtc::CleanupSSL();
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000345 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346}