blob: 6c5950b892fb17fb03f3633ede9ea8dbf8aa8461 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
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
49@interface RTCPeerConnectionTest : NSObject
50
51// Returns whether the two sessions are of the same type.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000052+ (BOOL)isSession:(RTCSessionDescription*)session1
53 ofSameTypeAsSession:(RTCSessionDescription*)session2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
55// Create and add tracks to pc, with the given source, label, and IDs
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000056- (RTCMediaStream*)addTracksToPeerConnection:(RTCPeerConnection*)pc
57 withFactory:(RTCPeerConnectionFactory*)factory
58 videoSource:(RTCVideoSource*)videoSource
59 streamLabel:(NSString*)streamLabel
60 videoTrackID:(NSString*)videoTrackID
61 audioTrackID:(NSString*)audioTrackID;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
fischman@webrtc.org385a7222014-03-25 05:16:29 +000063- (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
65@end
66
67@implementation RTCPeerConnectionTest
68
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000069+ (BOOL)isSession:(RTCSessionDescription*)session1
70 ofSameTypeAsSession:(RTCSessionDescription*)session2 {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 return [session1.type isEqual:session2.type];
72}
73
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000074- (RTCMediaStream*)addTracksToPeerConnection:(RTCPeerConnection*)pc
75 withFactory:(RTCPeerConnectionFactory*)factory
76 videoSource:(RTCVideoSource*)videoSource
77 streamLabel:(NSString*)streamLabel
78 videoTrackID:(NSString*)videoTrackID
79 audioTrackID:(NSString*)audioTrackID {
80 RTCMediaStream* localMediaStream = [factory mediaStreamWithLabel:streamLabel];
81 RTCVideoTrack* videoTrack =
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 [factory videoTrackWithID:videoTrackID source:videoSource];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000083 RTCVideoRenderer* videoRenderer =
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 [[RTCVideoRenderer alloc] initWithDelegate:nil];
85 [videoTrack addRenderer:videoRenderer];
86 [localMediaStream addVideoTrack:videoTrack];
87 // Test that removal/re-add works.
88 [localMediaStream removeVideoTrack:videoTrack];
89 [localMediaStream addVideoTrack:videoTrack];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000090 RTCAudioTrack* audioTrack = [factory audioTrackWithID:audioTrackID];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 [localMediaStream addAudioTrack:audioTrack];
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +000092 [pc addStream:localMediaStream];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 return localMediaStream;
94}
95
fischman@webrtc.org385a7222014-03-25 05:16:29 +000096- (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory {
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000097 NSArray* mandatory = @[
98 [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement" value:@"true"],
99 [[RTCPair alloc] initWithKey:@"internalSctpDataChannels" value:@"true"],
100 ];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000101 RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] init];
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000102 RTCMediaConstraints* pcConstraints =
103 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatory
104 optionalConstraints:nil];
105
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000106 RTCPeerConnectionSyncObserver* offeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000108 RTCPeerConnection* pcOffer =
109 [factory peerConnectionWithICEServers:nil
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000110 constraints:pcConstraints
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 delegate:offeringExpectations];
112
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000113 RTCPeerConnectionSyncObserver* answeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000115
116 RTCPeerConnection* pcAnswer =
117 [factory peerConnectionWithICEServers:nil
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000118 constraints:pcConstraints
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 delegate:answeringExpectations];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 // TODO(hughv): Create video capturer
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000121 RTCVideoCapturer* capturer = nil;
122 RTCVideoSource* videoSource =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 [factory videoSourceWithCapturer:capturer constraints:constraints];
124
125 // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS"
126 // refers to the answerer's local media stream, with suffixes of "a0" and "v0"
127 // for audio and video tracks, resp. These mirror chrome historical naming.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000128 RTCMediaStream* oLMSUnused = [self addTracksToPeerConnection:pcOffer
129 withFactory:factory
130 videoSource:videoSource
131 streamLabel:@"oLMS"
132 videoTrackID:@"oLMSv0"
133 audioTrackID:@"oLMSa0"];
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000134
135 RTCDataChannel* offerDC =
136 [pcOffer createDataChannelWithLabel:@"offerDC"
137 config:[[RTCDataChannelInit alloc] init]];
138 EXPECT_TRUE([offerDC.label isEqual:@"offerDC"]);
139 offerDC.delegate = offeringExpectations;
140 offeringExpectations.dataChannel = offerDC;
141
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000142 RTCSessionDescriptionSyncObserver* sdpObserver =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 [[RTCSessionDescriptionSyncObserver alloc] init];
144 [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints];
145 [sdpObserver wait];
146 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000147 RTCSessionDescription* offerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch],
149 NSOrderedSame);
150 EXPECT_GT([offerSDP.description length], 0);
151
152 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000153 [answeringExpectations expectSignalingChange:RTCSignalingHaveRemoteOffer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 [answeringExpectations expectAddStream:@"oLMS"];
155 [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver
156 sessionDescription:offerSDP];
157 [sdpObserver wait];
158
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000159 RTCMediaStream* aLMSUnused = [self addTracksToPeerConnection:pcAnswer
160 withFactory:factory
161 videoSource:videoSource
162 streamLabel:@"aLMS"
163 videoTrackID:@"aLMSv0"
164 audioTrackID:@"aLMSa0"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
166 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
167 [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints];
168 [sdpObserver wait];
169 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000170 RTCSessionDescription* answerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch],
172 NSOrderedSame);
173 EXPECT_GT([answerSDP.description length], 0);
174
175 [offeringExpectations expectICECandidates:2];
176 [answeringExpectations expectICECandidates:2];
177
178 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
179 [answeringExpectations expectSignalingChange:RTCSignalingStable];
180 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
181 sessionDescription:answerSDP];
182 [sdpObserver wait];
183 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
184
185 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
186 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
187 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
188 sessionDescription:offerSDP];
189 [sdpObserver wait];
190 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
191
192 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
193 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
fischman@webrtc.orga01daf02014-03-08 03:17:55 +0000194 // TODO(fischman): figure out why this is flaky and re-introduce (and remove
195 // special-casing from the observer!).
196 // [offeringExpectations expectICEConnectionChange:RTCICEConnectionCompleted];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
198 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
199
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000200 [offeringExpectations expectStateChange:kRTCDataChannelStateOpen];
201 [answeringExpectations expectDataChannel:@"offerDC"];
202 [answeringExpectations expectStateChange:kRTCDataChannelStateOpen];
203
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
205 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
206
207 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
208 [offeringExpectations expectSignalingChange:RTCSignalingStable];
209 [offeringExpectations expectAddStream:@"aLMS"];
210 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
211 sessionDescription:answerSDP];
212 [sdpObserver wait];
213 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
214
215 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
216 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
217 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
218 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
219
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000220 for (RTCICECandidate* candidate in offeringExpectations
221 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 [pcAnswer addICECandidate:candidate];
223 }
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000224 for (RTCICECandidate* candidate in answeringExpectations
225 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 [pcOffer addICECandidate:candidate];
227 }
228
229 [offeringExpectations waitForAllExpectationsToBeSatisfied];
230 [answeringExpectations waitForAllExpectationsToBeSatisfied];
231
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000232 EXPECT_EQ(pcOffer.signalingState, RTCSignalingStable);
233 EXPECT_EQ(pcAnswer.signalingState, RTCSignalingStable);
234
235 // Test send and receive UTF-8 text
236 NSString* text = @"你好";
237 NSData* textData = [text dataUsingEncoding:NSUTF8StringEncoding];
238 RTCDataBuffer* buffer =
239 [[RTCDataBuffer alloc] initWithData:textData isBinary:NO];
240 [answeringExpectations expectMessage:[textData copy] isBinary:NO];
241 EXPECT_TRUE([offeringExpectations.dataChannel sendData:buffer]);
242 [answeringExpectations waitForAllExpectationsToBeSatisfied];
243
244 // Test send and receive binary data
245 const size_t byteLength = 5;
246 char bytes[byteLength] = {1, 2, 3, 4, 5};
247 NSData* byteData = [NSData dataWithBytes:bytes length:byteLength];
248 buffer = [[RTCDataBuffer alloc] initWithData:byteData isBinary:YES];
249 [answeringExpectations expectMessage:[byteData copy] isBinary:YES];
250 EXPECT_TRUE([offeringExpectations.dataChannel sendData:buffer]);
251 [answeringExpectations waitForAllExpectationsToBeSatisfied];
252
253 [offeringExpectations expectStateChange:kRTCDataChannelStateClosing];
254 [answeringExpectations expectStateChange:kRTCDataChannelStateClosing];
255 [offeringExpectations expectStateChange:kRTCDataChannelStateClosed];
256 [answeringExpectations expectStateChange:kRTCDataChannelStateClosed];
257
258 [answeringExpectations.dataChannel close];
259 [offeringExpectations.dataChannel close];
260
261 [offeringExpectations waitForAllExpectationsToBeSatisfied];
262 [answeringExpectations waitForAllExpectationsToBeSatisfied];
263 // Don't need to listen to further state changes.
264 // TODO(tkchin): figure out why Closed->Closing without this.
265 offeringExpectations.dataChannel.delegate = nil;
266 answeringExpectations.dataChannel.delegate = nil;
267
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000268 // Let the audio feedback run for 2s to allow human testing and to ensure
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 // things stabilize. TODO(fischman): replace seconds with # of video frames,
270 // when we have video flowing.
271 [[NSRunLoop currentRunLoop]
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000272 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000274 [offeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
275 [answeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
276 [offeringExpectations expectSignalingChange:RTCSignalingClosed];
277 [answeringExpectations expectSignalingChange:RTCSignalingClosed];
278
279 [pcOffer close];
280 [pcAnswer close];
281
282 [offeringExpectations waitForAllExpectationsToBeSatisfied];
283 [answeringExpectations waitForAllExpectationsToBeSatisfied];
284
285 capturer = nil;
286 videoSource = nil;
287 pcOffer = nil;
288 pcAnswer = nil;
289 // TODO(fischman): be stricter about shutdown checks; ensure thread
290 // counts return to where they were before the test kicked off, and
291 // that all objects have in fact shut down.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292}
293
294@end
295
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000296// TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of
297// RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being
298// a TestBase since it's not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299TEST(RTCPeerConnectionTest, SessionTest) {
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000300 @autoreleasepool {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000301 rtc::InitializeSSL();
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000302 // Since |factory| will own the signaling & worker threads, it's important
303 // that it outlive the created PeerConnections since they self-delete on the
304 // signaling thread, and if |factory| is freed first then a last refcount on
305 // the factory will expire during this teardown, causing the signaling
306 // thread to try to Join() with itself. This is a hack to ensure that the
307 // factory outlives RTCPeerConnection:dealloc.
308 // See https://code.google.com/p/webrtc/issues/detail?id=3100.
309 RTCPeerConnectionFactory* factory = [[RTCPeerConnectionFactory alloc] init];
310 @autoreleasepool {
311 RTCPeerConnectionTest* pcTest = [[RTCPeerConnectionTest alloc] init];
312 [pcTest testCompleteSessionWithFactory:factory];
313 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000314 rtc::CleanupSSL();
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000315 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316}