blob: f0703f5ad854abff184f63c8eaee77a1c18405c7 [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];
deadbeef47ee2f32015-09-22 15:08:23 -0700185 // It's possible to only have 1 ICE candidate for the answerer, since we use
186 // BUNDLE and rtcp-mux by default, and don't provide any ICE servers in this
187 // test.
188 [answeringExpectations expectICECandidates:1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
190 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
191 [answeringExpectations expectSignalingChange:RTCSignalingStable];
192 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
193 sessionDescription:answerSDP];
194 [sdpObserver wait];
195 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
196
197 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
198 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
199 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
200 sessionDescription:offerSDP];
201 [sdpObserver wait];
202 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
203
204 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
205 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
fischman@webrtc.orga01daf02014-03-08 03:17:55 +0000206 // TODO(fischman): figure out why this is flaky and re-introduce (and remove
207 // special-casing from the observer!).
208 // [offeringExpectations expectICEConnectionChange:RTCICEConnectionCompleted];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
210 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
211
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000212 [offeringExpectations expectStateChange:kRTCDataChannelStateOpen];
213 [answeringExpectations expectDataChannel:@"offerDC"];
214 [answeringExpectations expectStateChange:kRTCDataChannelStateOpen];
215
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
217 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
218
219 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
220 [offeringExpectations expectSignalingChange:RTCSignalingStable];
221 [offeringExpectations expectAddStream:@"aLMS"];
222 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
223 sessionDescription:answerSDP];
224 [sdpObserver wait];
225 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
226
227 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
228 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
229 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
230 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
231
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000232 for (RTCICECandidate* candidate in offeringExpectations
233 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 [pcAnswer addICECandidate:candidate];
235 }
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000236 for (RTCICECandidate* candidate in answeringExpectations
237 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 [pcOffer addICECandidate:candidate];
239 }
240
241 [offeringExpectations waitForAllExpectationsToBeSatisfied];
242 [answeringExpectations waitForAllExpectationsToBeSatisfied];
243
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000244 EXPECT_EQ(pcOffer.signalingState, RTCSignalingStable);
245 EXPECT_EQ(pcAnswer.signalingState, RTCSignalingStable);
246
247 // Test send and receive UTF-8 text
248 NSString* text = @"你好";
249 NSData* textData = [text dataUsingEncoding:NSUTF8StringEncoding];
250 RTCDataBuffer* buffer =
251 [[RTCDataBuffer alloc] initWithData:textData isBinary:NO];
252 [answeringExpectations expectMessage:[textData copy] isBinary:NO];
253 EXPECT_TRUE([offeringExpectations.dataChannel sendData:buffer]);
254 [answeringExpectations waitForAllExpectationsToBeSatisfied];
255
256 // Test send and receive binary data
257 const size_t byteLength = 5;
258 char bytes[byteLength] = {1, 2, 3, 4, 5};
259 NSData* byteData = [NSData dataWithBytes:bytes length:byteLength];
260 buffer = [[RTCDataBuffer alloc] initWithData:byteData isBinary:YES];
261 [answeringExpectations expectMessage:[byteData copy] isBinary:YES];
262 EXPECT_TRUE([offeringExpectations.dataChannel sendData:buffer]);
263 [answeringExpectations waitForAllExpectationsToBeSatisfied];
264
265 [offeringExpectations expectStateChange:kRTCDataChannelStateClosing];
266 [answeringExpectations expectStateChange:kRTCDataChannelStateClosing];
267 [offeringExpectations expectStateChange:kRTCDataChannelStateClosed];
268 [answeringExpectations expectStateChange:kRTCDataChannelStateClosed];
269
270 [answeringExpectations.dataChannel close];
271 [offeringExpectations.dataChannel close];
272
273 [offeringExpectations waitForAllExpectationsToBeSatisfied];
274 [answeringExpectations waitForAllExpectationsToBeSatisfied];
275 // Don't need to listen to further state changes.
276 // TODO(tkchin): figure out why Closed->Closing without this.
277 offeringExpectations.dataChannel.delegate = nil;
278 answeringExpectations.dataChannel.delegate = nil;
279
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000280 // Let the audio feedback run for 2s to allow human testing and to ensure
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 // things stabilize. TODO(fischman): replace seconds with # of video frames,
282 // when we have video flowing.
283 [[NSRunLoop currentRunLoop]
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000284 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000286 [offeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
287 [answeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
288 [offeringExpectations expectSignalingChange:RTCSignalingClosed];
289 [answeringExpectations expectSignalingChange:RTCSignalingClosed];
290
291 [pcOffer close];
292 [pcAnswer close];
293
294 [offeringExpectations waitForAllExpectationsToBeSatisfied];
295 [answeringExpectations waitForAllExpectationsToBeSatisfied];
296
297 capturer = nil;
298 videoSource = nil;
299 pcOffer = nil;
300 pcAnswer = nil;
301 // TODO(fischman): be stricter about shutdown checks; ensure thread
302 // counts return to where they were before the test kicked off, and
303 // that all objects have in fact shut down.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304}
305
306@end
307
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000308// TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of
309// RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being
310// a TestBase since it's not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311TEST(RTCPeerConnectionTest, SessionTest) {
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000312 @autoreleasepool {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000313 rtc::InitializeSSL();
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000314 // Since |factory| will own the signaling & worker threads, it's important
315 // that it outlive the created PeerConnections since they self-delete on the
316 // signaling thread, and if |factory| is freed first then a last refcount on
317 // the factory will expire during this teardown, causing the signaling
318 // thread to try to Join() with itself. This is a hack to ensure that the
319 // factory outlives RTCPeerConnection:dealloc.
320 // See https://code.google.com/p/webrtc/issues/detail?id=3100.
321 RTCPeerConnectionFactory* factory = [[RTCPeerConnectionFactory alloc] init];
322 @autoreleasepool {
323 RTCPeerConnectionTest* pcTest = [[RTCPeerConnectionTest alloc] init];
324 [pcTest testCompleteSessionWithFactory:factory];
325 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000326 rtc::CleanupSSL();
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000327 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328}