blob: b46df7f418ae88d88f23e92fba34987cfcc9d9a6 [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"
33#import "RTCPeerConnection.h"
34#import "RTCPeerConnectionFactory.h"
35#import "RTCPeerConnectionSyncObserver.h"
36#import "RTCSessionDescription.h"
37#import "RTCSessionDescriptionSyncObserver.h"
38#import "RTCVideoRenderer.h"
39#import "RTCVideoTrack.h"
40
41#include "talk/base/gunit.h"
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +000042#include "talk/base/ssladapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44#if !defined(__has_feature) || !__has_feature(objc_arc)
45#error "This file requires ARC support."
46#endif
47
48@interface RTCPeerConnectionTest : NSObject
49
50// Returns whether the two sessions are of the same type.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000051+ (BOOL)isSession:(RTCSessionDescription*)session1
52 ofSameTypeAsSession:(RTCSessionDescription*)session2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54// Create and add tracks to pc, with the given source, label, and IDs
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000055- (RTCMediaStream*)addTracksToPeerConnection:(RTCPeerConnection*)pc
56 withFactory:(RTCPeerConnectionFactory*)factory
57 videoSource:(RTCVideoSource*)videoSource
58 streamLabel:(NSString*)streamLabel
59 videoTrackID:(NSString*)videoTrackID
60 audioTrackID:(NSString*)audioTrackID;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
fischman@webrtc.org385a7222014-03-25 05:16:29 +000062- (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64@end
65
66@implementation RTCPeerConnectionTest
67
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000068+ (BOOL)isSession:(RTCSessionDescription*)session1
69 ofSameTypeAsSession:(RTCSessionDescription*)session2 {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 return [session1.type isEqual:session2.type];
71}
72
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000073- (RTCMediaStream*)addTracksToPeerConnection:(RTCPeerConnection*)pc
74 withFactory:(RTCPeerConnectionFactory*)factory
75 videoSource:(RTCVideoSource*)videoSource
76 streamLabel:(NSString*)streamLabel
77 videoTrackID:(NSString*)videoTrackID
78 audioTrackID:(NSString*)audioTrackID {
79 RTCMediaStream* localMediaStream = [factory mediaStreamWithLabel:streamLabel];
80 RTCVideoTrack* videoTrack =
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 [factory videoTrackWithID:videoTrackID source:videoSource];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000082 RTCVideoRenderer* videoRenderer =
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 [[RTCVideoRenderer alloc] initWithDelegate:nil];
84 [videoTrack addRenderer:videoRenderer];
85 [localMediaStream addVideoTrack:videoTrack];
86 // Test that removal/re-add works.
87 [localMediaStream removeVideoTrack:videoTrack];
88 [localMediaStream addVideoTrack:videoTrack];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000089 RTCAudioTrack* audioTrack = [factory audioTrackWithID:audioTrackID];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 [localMediaStream addAudioTrack:audioTrack];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000091 RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] init];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 [pc addStream:localMediaStream constraints:constraints];
93 return localMediaStream;
94}
95
fischman@webrtc.org385a7222014-03-25 05:16:29 +000096- (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000097 RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] init];
98 RTCPeerConnectionSyncObserver* offeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000100 RTCPeerConnection* pcOffer =
101 [factory peerConnectionWithICEServers:nil
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 constraints:constraints
103 delegate:offeringExpectations];
104
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000105 RTCPeerConnectionSyncObserver* answeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000107
108 RTCPeerConnection* pcAnswer =
109 [factory peerConnectionWithICEServers:nil
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 constraints:constraints
111 delegate:answeringExpectations];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 // TODO(hughv): Create video capturer
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000113 RTCVideoCapturer* capturer = nil;
114 RTCVideoSource* videoSource =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 [factory videoSourceWithCapturer:capturer constraints:constraints];
116
117 // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS"
118 // refers to the answerer's local media stream, with suffixes of "a0" and "v0"
119 // for audio and video tracks, resp. These mirror chrome historical naming.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000120 RTCMediaStream* oLMSUnused = [self addTracksToPeerConnection:pcOffer
121 withFactory:factory
122 videoSource:videoSource
123 streamLabel:@"oLMS"
124 videoTrackID:@"oLMSv0"
125 audioTrackID:@"oLMSa0"];
126 RTCSessionDescriptionSyncObserver* sdpObserver =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 [[RTCSessionDescriptionSyncObserver alloc] init];
128 [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints];
129 [sdpObserver wait];
130 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000131 RTCSessionDescription* offerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch],
133 NSOrderedSame);
134 EXPECT_GT([offerSDP.description length], 0);
135
136 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000137 [answeringExpectations expectSignalingChange:RTCSignalingHaveRemoteOffer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 [answeringExpectations expectAddStream:@"oLMS"];
139 [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver
140 sessionDescription:offerSDP];
141 [sdpObserver wait];
142
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000143 RTCMediaStream* aLMSUnused = [self addTracksToPeerConnection:pcAnswer
144 withFactory:factory
145 videoSource:videoSource
146 streamLabel:@"aLMS"
147 videoTrackID:@"aLMSv0"
148 audioTrackID:@"aLMSa0"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149
150 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
151 [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints];
152 [sdpObserver wait];
153 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000154 RTCSessionDescription* answerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch],
156 NSOrderedSame);
157 EXPECT_GT([answerSDP.description length], 0);
158
159 [offeringExpectations expectICECandidates:2];
160 [answeringExpectations expectICECandidates:2];
161
162 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
163 [answeringExpectations expectSignalingChange:RTCSignalingStable];
164 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
165 sessionDescription:answerSDP];
166 [sdpObserver wait];
167 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
168
169 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
170 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
171 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
172 sessionDescription:offerSDP];
173 [sdpObserver wait];
174 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
175
176 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
177 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
fischman@webrtc.orga01daf02014-03-08 03:17:55 +0000178 // TODO(fischman): figure out why this is flaky and re-introduce (and remove
179 // special-casing from the observer!).
180 // [offeringExpectations expectICEConnectionChange:RTCICEConnectionCompleted];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
182 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
183
184 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
185 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
186
187 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
188 [offeringExpectations expectSignalingChange:RTCSignalingStable];
189 [offeringExpectations expectAddStream:@"aLMS"];
190 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
191 sessionDescription:answerSDP];
192 [sdpObserver wait];
193 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
194
195 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
196 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
197 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
198 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
199
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000200 for (RTCICECandidate* candidate in offeringExpectations
201 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 [pcAnswer addICECandidate:candidate];
203 }
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000204 for (RTCICECandidate* candidate in answeringExpectations
205 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 [pcOffer addICECandidate:candidate];
207 }
208
209 [offeringExpectations waitForAllExpectationsToBeSatisfied];
210 [answeringExpectations waitForAllExpectationsToBeSatisfied];
211
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000212 // Let the audio feedback run for 2s to allow human testing and to ensure
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 // things stabilize. TODO(fischman): replace seconds with # of video frames,
214 // when we have video flowing.
215 [[NSRunLoop currentRunLoop]
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000216 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000218 [offeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
219 [answeringExpectations expectICEConnectionChange:RTCICEConnectionClosed];
220 [offeringExpectations expectSignalingChange:RTCSignalingClosed];
221 [answeringExpectations expectSignalingChange:RTCSignalingClosed];
222
223 [pcOffer close];
224 [pcAnswer close];
225
226 [offeringExpectations waitForAllExpectationsToBeSatisfied];
227 [answeringExpectations waitForAllExpectationsToBeSatisfied];
228
229 capturer = nil;
230 videoSource = nil;
231 pcOffer = nil;
232 pcAnswer = nil;
233 // TODO(fischman): be stricter about shutdown checks; ensure thread
234 // counts return to where they were before the test kicked off, and
235 // that all objects have in fact shut down.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236}
237
238@end
239
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000240// TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of
241// RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being
242// a TestBase since it's not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243TEST(RTCPeerConnectionTest, SessionTest) {
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000244 @autoreleasepool {
245 talk_base::InitializeSSL();
246 // Since |factory| will own the signaling & worker threads, it's important
247 // that it outlive the created PeerConnections since they self-delete on the
248 // signaling thread, and if |factory| is freed first then a last refcount on
249 // the factory will expire during this teardown, causing the signaling
250 // thread to try to Join() with itself. This is a hack to ensure that the
251 // factory outlives RTCPeerConnection:dealloc.
252 // See https://code.google.com/p/webrtc/issues/detail?id=3100.
253 RTCPeerConnectionFactory* factory = [[RTCPeerConnectionFactory alloc] init];
254 @autoreleasepool {
255 RTCPeerConnectionTest* pcTest = [[RTCPeerConnectionTest alloc] init];
256 [pcTest testCompleteSessionWithFactory:factory];
257 }
258 talk_base::CleanupSSL();
259 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260}