blob: 8605ef0e54ee857062ae43a4098d8932a4aedb63 [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];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 RTCMediaConstraints *constraints = [[RTCMediaConstraints alloc] init];
101 RTCPeerConnectionSyncObserver *offeringExpectations =
102 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000103 RTCPeerConnection* pcOffer =
104 [factory peerConnectionWithICEServers:nil
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 constraints:constraints
106 delegate:offeringExpectations];
107
108 RTCPeerConnectionSyncObserver *answeringExpectations =
109 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000110
111 RTCPeerConnection* pcAnswer =
112 [factory peerConnectionWithICEServers:nil
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 constraints:constraints
114 delegate:answeringExpectations];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 // TODO(hughv): Create video capturer
116 RTCVideoCapturer *capturer = nil;
117 RTCVideoSource *videoSource =
118 [factory videoSourceWithCapturer:capturer constraints:constraints];
119
120 // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS"
121 // refers to the answerer's local media stream, with suffixes of "a0" and "v0"
122 // for audio and video tracks, resp. These mirror chrome historical naming.
123 RTCMediaStream *oLMSUnused =
124 [self addTracksToPeerConnection:pcOffer
125 withFactory:factory
126 videoSource:videoSource
127 streamLabel:@"oLMS"
128 videoTrackID:@"oLMSv0"
129 audioTrackID:@"oLMSa0"];
130 RTCSessionDescriptionSyncObserver *sdpObserver =
131 [[RTCSessionDescriptionSyncObserver alloc] init];
132 [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints];
133 [sdpObserver wait];
134 EXPECT_TRUE(sdpObserver.success);
135 RTCSessionDescription *offerSDP = sdpObserver.sessionDescription;
136 EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch],
137 NSOrderedSame);
138 EXPECT_GT([offerSDP.description length], 0);
139
140 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
141 [answeringExpectations
142 expectSignalingChange:RTCSignalingHaveRemoteOffer];
143 [answeringExpectations expectAddStream:@"oLMS"];
144 [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver
145 sessionDescription:offerSDP];
146 [sdpObserver wait];
147
148 RTCMediaStream *aLMSUnused =
149 [self addTracksToPeerConnection:pcAnswer
150 withFactory:factory
151 videoSource:videoSource
152 streamLabel:@"aLMS"
153 videoTrackID:@"aLMSv0"
154 audioTrackID:@"aLMSa0"];
155
156 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
157 [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints];
158 [sdpObserver wait];
159 EXPECT_TRUE(sdpObserver.success);
160 RTCSessionDescription *answerSDP = sdpObserver.sessionDescription;
161 EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch],
162 NSOrderedSame);
163 EXPECT_GT([answerSDP.description length], 0);
164
165 [offeringExpectations expectICECandidates:2];
166 [answeringExpectations expectICECandidates:2];
167
168 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
169 [answeringExpectations expectSignalingChange:RTCSignalingStable];
170 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
171 sessionDescription:answerSDP];
172 [sdpObserver wait];
173 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
174
175 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
176 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
177 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
178 sessionDescription:offerSDP];
179 [sdpObserver wait];
180 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
181
182 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
183 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000184 [offeringExpectations expectICEConnectionChange:RTCICEConnectionCompleted];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
186 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
187
188 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
189 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
190
191 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
192 [offeringExpectations expectSignalingChange:RTCSignalingStable];
193 [offeringExpectations expectAddStream:@"aLMS"];
194 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
195 sessionDescription:answerSDP];
196 [sdpObserver wait];
197 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
198
199 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
200 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
201 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
202 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
203
204 for (RTCICECandidate *candidate in
205 offeringExpectations.releaseReceivedICECandidates) {
206 [pcAnswer addICECandidate:candidate];
207 }
208 for (RTCICECandidate *candidate in
209 answeringExpectations.releaseReceivedICECandidates) {
210 [pcOffer addICECandidate:candidate];
211 }
212
213 [offeringExpectations waitForAllExpectationsToBeSatisfied];
214 [answeringExpectations waitForAllExpectationsToBeSatisfied];
215
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000216 // Let the audio feedback run for 2s to allow human testing and to ensure
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 // things stabilize. TODO(fischman): replace seconds with # of video frames,
218 // when we have video flowing.
219 [[NSRunLoop currentRunLoop]
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000220 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221
222 // TODO(hughv): Implement orderly shutdown.
223}
224
225@end
226
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000227// TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of
228// RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being
229// a TestBase since it's not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230TEST(RTCPeerConnectionTest, SessionTest) {
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000231 talk_base::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 RTCPeerConnectionTest *pcTest = [[RTCPeerConnectionTest alloc] init];
233 [pcTest testCompleteSession];
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000234 talk_base::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235}