blob: 9a0291411a8397b71b4bb92b1cca12e5dd0252be [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
62- (void)testCompleteSession;
63
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
96- (void)testCompleteSession {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000097 RTCPeerConnectionFactory* factory = [[RTCPeerConnectionFactory alloc] init];
98 RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] init];
99 RTCPeerConnectionSyncObserver* offeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000101 RTCPeerConnection* pcOffer =
102 [factory peerConnectionWithICEServers:nil
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 constraints:constraints
104 delegate:offeringExpectations];
105
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000106 RTCPeerConnectionSyncObserver* answeringExpectations =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 [[RTCPeerConnectionSyncObserver alloc] init];
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000108
109 RTCPeerConnection* pcAnswer =
110 [factory peerConnectionWithICEServers:nil
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 constraints:constraints
112 delegate:answeringExpectations];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 // TODO(hughv): Create video capturer
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000114 RTCVideoCapturer* capturer = nil;
115 RTCVideoSource* videoSource =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 [factory videoSourceWithCapturer:capturer constraints:constraints];
117
118 // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS"
119 // refers to the answerer's local media stream, with suffixes of "a0" and "v0"
120 // for audio and video tracks, resp. These mirror chrome historical naming.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000121 RTCMediaStream* oLMSUnused = [self addTracksToPeerConnection:pcOffer
122 withFactory:factory
123 videoSource:videoSource
124 streamLabel:@"oLMS"
125 videoTrackID:@"oLMSv0"
126 audioTrackID:@"oLMSa0"];
127 RTCSessionDescriptionSyncObserver* sdpObserver =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 [[RTCSessionDescriptionSyncObserver alloc] init];
129 [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints];
130 [sdpObserver wait];
131 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000132 RTCSessionDescription* offerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch],
134 NSOrderedSame);
135 EXPECT_GT([offerSDP.description length], 0);
136
137 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000138 [answeringExpectations expectSignalingChange:RTCSignalingHaveRemoteOffer];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 [answeringExpectations expectAddStream:@"oLMS"];
140 [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver
141 sessionDescription:offerSDP];
142 [sdpObserver wait];
143
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000144 RTCMediaStream* aLMSUnused = [self addTracksToPeerConnection:pcAnswer
145 withFactory:factory
146 videoSource:videoSource
147 streamLabel:@"aLMS"
148 videoTrackID:@"aLMSv0"
149 audioTrackID:@"aLMSa0"];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150
151 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
152 [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints];
153 [sdpObserver wait];
154 EXPECT_TRUE(sdpObserver.success);
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000155 RTCSessionDescription* answerSDP = sdpObserver.sessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch],
157 NSOrderedSame);
158 EXPECT_GT([answerSDP.description length], 0);
159
160 [offeringExpectations expectICECandidates:2];
161 [answeringExpectations expectICECandidates:2];
162
163 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
164 [answeringExpectations expectSignalingChange:RTCSignalingStable];
165 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
166 sessionDescription:answerSDP];
167 [sdpObserver wait];
168 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
169
170 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
171 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
172 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
173 sessionDescription:offerSDP];
174 [sdpObserver wait];
175 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
176
177 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
178 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
fischman@webrtc.orga01daf02014-03-08 03:17:55 +0000179 // TODO(fischman): figure out why this is flaky and re-introduce (and remove
180 // special-casing from the observer!).
181 // [offeringExpectations expectICEConnectionChange:RTCICEConnectionCompleted];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
183 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
184
185 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
186 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
187
188 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
189 [offeringExpectations expectSignalingChange:RTCSignalingStable];
190 [offeringExpectations expectAddStream:@"aLMS"];
191 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
192 sessionDescription:answerSDP];
193 [sdpObserver wait];
194 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
195
196 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
197 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
198 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
199 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
200
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000201 for (RTCICECandidate* candidate in offeringExpectations
202 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 [pcAnswer addICECandidate:candidate];
204 }
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000205 for (RTCICECandidate* candidate in answeringExpectations
206 .releaseReceivedICECandidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 [pcOffer addICECandidate:candidate];
208 }
209
210 [offeringExpectations waitForAllExpectationsToBeSatisfied];
211 [answeringExpectations waitForAllExpectationsToBeSatisfied];
212
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000213 // Let the audio feedback run for 2s to allow human testing and to ensure
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 // things stabilize. TODO(fischman): replace seconds with # of video frames,
215 // when we have video flowing.
216 [[NSRunLoop currentRunLoop]
fischman@webrtc.org13320ea2014-03-07 22:15:30 +0000217 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
219 // TODO(hughv): Implement orderly shutdown.
220}
221
222@end
223
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000224// TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of
225// RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being
226// a TestBase since it's not.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227TEST(RTCPeerConnectionTest, SessionTest) {
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000228 talk_base::InitializeSSL();
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000229 RTCPeerConnectionTest* pcTest = [[RTCPeerConnectionTest alloc] init];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 [pcTest testCompleteSession];
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000231 talk_base::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232}