blob: 050c9f4c95dd61acdacd6b23718e618373a4a88d [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
tkchin@webrtc.org81257442014-11-04 23:06:15 +000049@interface RTCFakeRenderer : NSObject <RTCVideoRenderer>
50@end
51
52@implementation RTCFakeRenderer
53
54- (void)setSize:(CGSize)size {}
55- (void)renderFrame:(RTCI420Frame*)frame {}
56
57@end
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059@interface RTCPeerConnectionTest : NSObject
60
61// Returns whether the two sessions are of the same type.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000062+ (BOOL)isSession:(RTCSessionDescription*)session1
63 ofSameTypeAsSession:(RTCSessionDescription*)session2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
65// Create and add tracks to pc, with the given source, label, and IDs
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000066- (RTCMediaStream*)addTracksToPeerConnection:(RTCPeerConnection*)pc
67 withFactory:(RTCPeerConnectionFactory*)factory
68 videoSource:(RTCVideoSource*)videoSource
69 streamLabel:(NSString*)streamLabel
70 videoTrackID:(NSString*)videoTrackID
71 audioTrackID:(NSString*)audioTrackID;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
fischman@webrtc.org385a7222014-03-25 05:16:29 +000073- (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
75@end
76
77@implementation RTCPeerConnectionTest
78
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000079+ (BOOL)isSession:(RTCSessionDescription*)session1
80 ofSameTypeAsSession:(RTCSessionDescription*)session2 {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 return [session1.type isEqual:session2.type];
82}
83
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000084- (RTCMediaStream*)addTracksToPeerConnection:(RTCPeerConnection*)pc
85 withFactory:(RTCPeerConnectionFactory*)factory
86 videoSource:(RTCVideoSource*)videoSource
87 streamLabel:(NSString*)streamLabel
88 videoTrackID:(NSString*)videoTrackID
89 audioTrackID:(NSString*)audioTrackID {
90 RTCMediaStream* localMediaStream = [factory mediaStreamWithLabel:streamLabel];
91 RTCVideoTrack* videoTrack =
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 [factory videoTrackWithID:videoTrackID source:videoSource];
tkchin@webrtc.org81257442014-11-04 23:06:15 +000093 RTCFakeRenderer* videoRenderer = [[RTCFakeRenderer alloc] init];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 [videoTrack addRenderer:videoRenderer];
95 [localMediaStream addVideoTrack:videoTrack];
96 // Test that removal/re-add works.
97 [localMediaStream removeVideoTrack:videoTrack];
98 [localMediaStream addVideoTrack:videoTrack];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000099 RTCAudioTrack* audioTrack = [factory audioTrackWithID:audioTrackID];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 [localMediaStream addAudioTrack:audioTrack];
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000101 [pc addStream:localMediaStream];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 return localMediaStream;
103}
104
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000105- (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory {
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000106 NSArray* mandatory = @[
107 [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement" value:@"true"],
108 [[RTCPair alloc] initWithKey:@"internalSctpDataChannels" value:@"true"],
109 ];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000110 RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] init];
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000111 RTCMediaConstraints* pcConstraints =
112 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatory
113 optionalConstraints:nil];
114
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000115 RTCPeerConnectionSyncObserver* offeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000117 RTCPeerConnection* pcOffer =
118 [factory peerConnectionWithICEServers:nil
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000119 constraints:pcConstraints
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 delegate:offeringExpectations];
121
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000122 RTCPeerConnectionSyncObserver* answeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000124
125 RTCPeerConnection* pcAnswer =
126 [factory peerConnectionWithICEServers:nil
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000127 constraints:pcConstraints
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 delegate:answeringExpectations];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // TODO(hughv): Create video capturer
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000130 RTCVideoCapturer* capturer = nil;
131 RTCVideoSource* videoSource =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 [factory videoSourceWithCapturer:capturer constraints:constraints];
133
134 // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS"
135 // refers to the answerer's local media stream, with suffixes of "a0" and "v0"
136 // for audio and video tracks, resp. These mirror chrome historical naming.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000137 RTCMediaStream* oLMSUnused = [self addTracksToPeerConnection:pcOffer
138 withFactory:factory
139 videoSource:videoSource
140 streamLabel:@"oLMS"
141 videoTrackID:@"oLMSv0"
142 audioTrackID:@"oLMSa0"];
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000143
144 RTCDataChannel* offerDC =
145 [pcOffer createDataChannelWithLabel:@"offerDC"
146 config:[[RTCDataChannelInit alloc] init]];
147 EXPECT_TRUE([offerDC.label isEqual:@"offerDC"]);
148 offerDC.delegate = offeringExpectations;
149 offeringExpectations.dataChannel = offerDC;
150
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000151 RTCSessionDescriptionSyncObserver* sdpObserver =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 [[RTCSessionDescriptionSyncObserver alloc] init];
153 [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints];
154 [sdpObserver wait];
155 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000156 RTCSessionDescription* offerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch],
158 NSOrderedSame);
159 EXPECT_GT([offerSDP.description length], 0);
160
161 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000162 [answeringExpectations expectSignalingChange:RTCSignalingHaveRemoteOffer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 [answeringExpectations expectAddStream:@"oLMS"];
164 [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver
165 sessionDescription:offerSDP];
166 [sdpObserver wait];
167
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000168 RTCMediaStream* aLMSUnused = [self addTracksToPeerConnection:pcAnswer
169 withFactory:factory
170 videoSource:videoSource
171 streamLabel:@"aLMS"
172 videoTrackID:@"aLMSv0"
173 audioTrackID:@"aLMSa0"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174
175 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
176 [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints];
177 [sdpObserver wait];
178 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000179 RTCSessionDescription* answerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch],
181 NSOrderedSame);
182 EXPECT_GT([answerSDP.description length], 0);
183
184 [offeringExpectations expectICECandidates:2];
torbjornga81a42f2015-09-23 02:16:58 -0700185 [answeringExpectations expectICECandidates:2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
187 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
188 [answeringExpectations expectSignalingChange:RTCSignalingStable];
189 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
190 sessionDescription:answerSDP];
191 [sdpObserver wait];
192 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
193
194 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
195 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
196 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
197 sessionDescription:offerSDP];
198 [sdpObserver wait];
199 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
200
201 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
202 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
fischman@webrtc.orga01daf02014-03-08 03:17:55 +0000203 // TODO(fischman): figure out why this is flaky and re-introduce (and remove
204 // special-casing from the observer!).
205 // [offeringExpectations expectICEConnectionChange:RTCICEConnectionCompleted];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
207 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
208
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000209 [offeringExpectations expectStateChange:kRTCDataChannelStateOpen];
210 [answeringExpectations expectDataChannel:@"offerDC"];
211 [answeringExpectations expectStateChange:kRTCDataChannelStateOpen];
212
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
214 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
215
216 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
217 [offeringExpectations expectSignalingChange:RTCSignalingStable];
218 [offeringExpectations expectAddStream:@"aLMS"];
219 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
220 sessionDescription:answerSDP];
221 [sdpObserver wait];
222 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
223
224 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
225 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
226 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
227 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
228
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000229 for (RTCICECandidate* candidate in offeringExpectations
230 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 [pcAnswer addICECandidate:candidate];
232 }
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000233 for (RTCICECandidate* candidate in answeringExpectations
234 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 [pcOffer addICECandidate:candidate];
236 }
237
238 [offeringExpectations waitForAllExpectationsToBeSatisfied];
239 [answeringExpectations waitForAllExpectationsToBeSatisfied];
240
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000241 EXPECT_EQ(pcOffer.signalingState, RTCSignalingStable);
242 EXPECT_EQ(pcAnswer.signalingState, RTCSignalingStable);
243
244 // Test send and receive UTF-8 text
245 NSString* text = @"你好";
246 NSData* textData = [text dataUsingEncoding:NSUTF8StringEncoding];
247 RTCDataBuffer* buffer =
248 [[RTCDataBuffer alloc] initWithData:textData isBinary:NO];
249 [answeringExpectations expectMessage:[textData copy] isBinary:NO];
250 EXPECT_TRUE([offeringExpectations.dataChannel sendData:buffer]);
251 [answeringExpectations waitForAllExpectationsToBeSatisfied];
252
253 // Test send and receive binary data
254 const size_t byteLength = 5;
255 char bytes[byteLength] = {1, 2, 3, 4, 5};
256 NSData* byteData = [NSData dataWithBytes:bytes length:byteLength];
257 buffer = [[RTCDataBuffer alloc] initWithData:byteData isBinary:YES];
258 [answeringExpectations expectMessage:[byteData copy] isBinary:YES];
259 EXPECT_TRUE([offeringExpectations.dataChannel sendData:buffer]);
260 [answeringExpectations waitForAllExpectationsToBeSatisfied];
261
262 [offeringExpectations expectStateChange:kRTCDataChannelStateClosing];
263 [answeringExpectations expectStateChange:kRTCDataChannelStateClosing];
264 [offeringExpectations expectStateChange:kRTCDataChannelStateClosed];
265 [answeringExpectations expectStateChange:kRTCDataChannelStateClosed];
266
267 [answeringExpectations.dataChannel close];
268 [offeringExpectations.dataChannel close];
269
270 [offeringExpectations waitForAllExpectationsToBeSatisfied];
271 [answeringExpectations waitForAllExpectationsToBeSatisfied];
272 // Don't need to listen to further state changes.
273 // TODO(tkchin): figure out why Closed->Closing without this.
274 offeringExpectations.dataChannel.delegate = nil;
275 answeringExpectations.dataChannel.delegate = nil;
276
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000277 // Let the audio feedback run for 2s to allow human testing and to ensure
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 // things stabilize. TODO(fischman): replace seconds with # of video frames,
279 // when we have video flowing.
280 [[NSRunLoop currentRunLoop]
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000281 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000283 [offeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
284 [answeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
285 [offeringExpectations expectSignalingChange:RTCSignalingClosed];
286 [answeringExpectations expectSignalingChange:RTCSignalingClosed];
287
288 [pcOffer close];
289 [pcAnswer close];
290
291 [offeringExpectations waitForAllExpectationsToBeSatisfied];
292 [answeringExpectations waitForAllExpectationsToBeSatisfied];
293
294 capturer = nil;
295 videoSource = nil;
296 pcOffer = nil;
297 pcAnswer = nil;
298 // TODO(fischman): be stricter about shutdown checks; ensure thread
299 // counts return to where they were before the test kicked off, and
300 // that all objects have in fact shut down.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301}
302
303@end
304
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000305// TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of
306// RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being
307// a TestBase since it's not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308TEST(RTCPeerConnectionTest, SessionTest) {
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000309 @autoreleasepool {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000310 rtc::InitializeSSL();
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000311 // Since |factory| will own the signaling & worker threads, it's important
312 // that it outlive the created PeerConnections since they self-delete on the
313 // signaling thread, and if |factory| is freed first then a last refcount on
314 // the factory will expire during this teardown, causing the signaling
315 // thread to try to Join() with itself. This is a hack to ensure that the
316 // factory outlives RTCPeerConnection:dealloc.
317 // See https://code.google.com/p/webrtc/issues/detail?id=3100.
318 RTCPeerConnectionFactory* factory = [[RTCPeerConnectionFactory alloc] init];
319 @autoreleasepool {
320 RTCPeerConnectionTest* pcTest = [[RTCPeerConnectionTest alloc] init];
321 [pcTest testCompleteSessionWithFactory:factory];
322 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000323 rtc::CleanupSSL();
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000324 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325}