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