blob: 55e738839ae0b6f73209f4aeac2153656a538f0f [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.
51+ (BOOL)isSession:(RTCSessionDescription *)session1
52 ofSameTypeAsSession:(RTCSessionDescription *)session2;
53
54// Create and add tracks to pc, with the given source, label, and IDs
55- (RTCMediaStream *)
56 addTracksToPeerConnection:(RTCPeerConnection *)pc
57 withFactory:(RTCPeerConnectionFactory *)factory
58 videoSource:(RTCVideoSource *)videoSource
59 streamLabel:(NSString *)streamLabel
60 videoTrackID:(NSString *)videoTrackID
61 audioTrackID:(NSString *)audioTrackID;
62
63- (void)testCompleteSession;
64
65@end
66
67@implementation RTCPeerConnectionTest
68
69+ (BOOL)isSession:(RTCSessionDescription *)session1
70 ofSameTypeAsSession:(RTCSessionDescription *)session2 {
71 return [session1.type isEqual:session2.type];
72}
73
74- (RTCMediaStream *)
75 addTracksToPeerConnection:(RTCPeerConnection *)pc
76 withFactory:(RTCPeerConnectionFactory *)factory
77 videoSource:(RTCVideoSource *)videoSource
78 streamLabel:(NSString *)streamLabel
79 videoTrackID:(NSString *)videoTrackID
80 audioTrackID:(NSString *)audioTrackID {
81 RTCMediaStream *localMediaStream = [factory mediaStreamWithLabel:streamLabel];
82 RTCVideoTrack *videoTrack =
83 [factory videoTrackWithID:videoTrackID source:videoSource];
84 RTCVideoRenderer *videoRenderer =
85 [[RTCVideoRenderer alloc] initWithDelegate:nil];
86 [videoTrack addRenderer:videoRenderer];
87 [localMediaStream addVideoTrack:videoTrack];
88 // Test that removal/re-add works.
89 [localMediaStream removeVideoTrack:videoTrack];
90 [localMediaStream addVideoTrack:videoTrack];
91 RTCAudioTrack *audioTrack = [factory audioTrackWithID:audioTrackID];
92 [localMediaStream addAudioTrack:audioTrack];
93 RTCMediaConstraints *constraints = [[RTCMediaConstraints alloc] init];
94 [pc addStream:localMediaStream constraints:constraints];
95 return localMediaStream;
96}
97
98- (void)testCompleteSession {
99 RTCPeerConnectionFactory *factory = [[RTCPeerConnectionFactory alloc] init];
100 NSString *stunURL = @"stun:stun.l.google.com:19302";
101 RTCICEServer *stunServer =
102 [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:stunURL]
fischman@webrtc.org016eec02013-09-05 23:11:55 +0000103 username:@""
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 password:@""];
105 NSArray *iceServers = @[stunServer];
106
107 RTCMediaConstraints *constraints = [[RTCMediaConstraints alloc] init];
108 RTCPeerConnectionSyncObserver *offeringExpectations =
109 [[RTCPeerConnectionSyncObserver alloc] init];
110 RTCPeerConnection *pcOffer =
111 [factory peerConnectionWithICEServers:iceServers
112 constraints:constraints
113 delegate:offeringExpectations];
114
115 RTCPeerConnectionSyncObserver *answeringExpectations =
116 [[RTCPeerConnectionSyncObserver alloc] init];
117 RTCPeerConnection *pcAnswer =
118 [factory peerConnectionWithICEServers:iceServers
119 constraints:constraints
120 delegate:answeringExpectations];
121
122 // TODO(hughv): Create video capturer
123 RTCVideoCapturer *capturer = nil;
124 RTCVideoSource *videoSource =
125 [factory videoSourceWithCapturer:capturer constraints:constraints];
126
127 // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS"
128 // refers to the answerer's local media stream, with suffixes of "a0" and "v0"
129 // for audio and video tracks, resp. These mirror chrome historical naming.
130 RTCMediaStream *oLMSUnused =
131 [self addTracksToPeerConnection:pcOffer
132 withFactory:factory
133 videoSource:videoSource
134 streamLabel:@"oLMS"
135 videoTrackID:@"oLMSv0"
136 audioTrackID:@"oLMSa0"];
137 RTCSessionDescriptionSyncObserver *sdpObserver =
138 [[RTCSessionDescriptionSyncObserver alloc] init];
139 [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints];
140 [sdpObserver wait];
141 EXPECT_TRUE(sdpObserver.success);
142 RTCSessionDescription *offerSDP = sdpObserver.sessionDescription;
143 EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch],
144 NSOrderedSame);
145 EXPECT_GT([offerSDP.description length], 0);
146
147 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
148 [answeringExpectations
149 expectSignalingChange:RTCSignalingHaveRemoteOffer];
150 [answeringExpectations expectAddStream:@"oLMS"];
151 [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver
152 sessionDescription:offerSDP];
153 [sdpObserver wait];
154
155 RTCMediaStream *aLMSUnused =
156 [self addTracksToPeerConnection:pcAnswer
157 withFactory:factory
158 videoSource:videoSource
159 streamLabel:@"aLMS"
160 videoTrackID:@"aLMSv0"
161 audioTrackID:@"aLMSa0"];
162
163 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
164 [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints];
165 [sdpObserver wait];
166 EXPECT_TRUE(sdpObserver.success);
167 RTCSessionDescription *answerSDP = sdpObserver.sessionDescription;
168 EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch],
169 NSOrderedSame);
170 EXPECT_GT([answerSDP.description length], 0);
171
172 [offeringExpectations expectICECandidates:2];
173 [answeringExpectations expectICECandidates:2];
174
175 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
176 [answeringExpectations expectSignalingChange:RTCSignalingStable];
177 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
178 sessionDescription:answerSDP];
179 [sdpObserver wait];
180 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
181
182 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
183 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
184 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
185 sessionDescription:offerSDP];
186 [sdpObserver wait];
187 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
188
189 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
190 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
191 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
192 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
193
194 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
195 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
196
197 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
198 [offeringExpectations expectSignalingChange:RTCSignalingStable];
199 [offeringExpectations expectAddStream:@"aLMS"];
200 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
201 sessionDescription:answerSDP];
202 [sdpObserver wait];
203 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
204
205 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
206 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
207 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
208 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
209
210 for (RTCICECandidate *candidate in
211 offeringExpectations.releaseReceivedICECandidates) {
212 [pcAnswer addICECandidate:candidate];
213 }
214 for (RTCICECandidate *candidate in
215 answeringExpectations.releaseReceivedICECandidates) {
216 [pcOffer addICECandidate:candidate];
217 }
218
219 [offeringExpectations waitForAllExpectationsToBeSatisfied];
220 [answeringExpectations waitForAllExpectationsToBeSatisfied];
221
222 // Let the audio feedback run for 10s to allow human testing and to ensure
223 // things stabilize. TODO(fischman): replace seconds with # of video frames,
224 // when we have video flowing.
225 [[NSRunLoop currentRunLoop]
226 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
227
228 // TODO(hughv): Implement orderly shutdown.
229}
230
231@end
232
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000233// TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of
234// RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being
235// a TestBase since it's not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236TEST(RTCPeerConnectionTest, SessionTest) {
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000237 talk_base::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 RTCPeerConnectionTest *pcTest = [[RTCPeerConnectionTest alloc] init];
239 [pcTest testCompleteSession];
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000240 talk_base::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241}