3 * Copyright 2013, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #import <Foundation/Foundation.h>
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"
41 #include "talk/base/gunit.h"
43 #if !defined(__has_feature) || !__has_feature(objc_arc)
44 #error "This file requires ARC support."
47 @interface RTCPeerConnectionTest : NSObject
49 // Returns whether the two sessions are of the same type.
50 + (BOOL)isSession:(RTCSessionDescription *)session1
51 ofSameTypeAsSession:(RTCSessionDescription *)session2;
53 // Create and add tracks to pc, with the given source, label, and IDs
55 addTracksToPeerConnection:(RTCPeerConnection *)pc
56 withFactory:(RTCPeerConnectionFactory *)factory
57 videoSource:(RTCVideoSource *)videoSource
58 streamLabel:(NSString *)streamLabel
59 videoTrackID:(NSString *)videoTrackID
60 audioTrackID:(NSString *)audioTrackID;
62 - (void)testCompleteSession;
66 @implementation RTCPeerConnectionTest
68 + (BOOL)isSession:(RTCSessionDescription *)session1
69 ofSameTypeAsSession:(RTCSessionDescription *)session2 {
70 return [session1.type isEqual:session2.type];
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;
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]
104 NSArray *iceServers = @[stunServer];
106 RTCMediaConstraints *constraints = [[RTCMediaConstraints alloc] init];
107 RTCPeerConnectionSyncObserver *offeringExpectations =
108 [[RTCPeerConnectionSyncObserver alloc] init];
109 RTCPeerConnection *pcOffer =
110 [factory peerConnectionWithICEServers:iceServers
111 constraints:constraints
112 delegate:offeringExpectations];
114 RTCPeerConnectionSyncObserver *answeringExpectations =
115 [[RTCPeerConnectionSyncObserver alloc] init];
116 RTCPeerConnection *pcAnswer =
117 [factory peerConnectionWithICEServers:iceServers
118 constraints:constraints
119 delegate:answeringExpectations];
121 // TODO(hughv): Create video capturer
122 RTCVideoCapturer *capturer = nil;
123 RTCVideoSource *videoSource =
124 [factory videoSourceWithCapturer:capturer constraints:constraints];
126 // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS"
127 // refers to the answerer's local media stream, with suffixes of "a0" and "v0"
128 // for audio and video tracks, resp. These mirror chrome historical naming.
129 RTCMediaStream *oLMSUnused =
130 [self addTracksToPeerConnection:pcOffer
132 videoSource:videoSource
134 videoTrackID:@"oLMSv0"
135 audioTrackID:@"oLMSa0"];
136 RTCSessionDescriptionSyncObserver *sdpObserver =
137 [[RTCSessionDescriptionSyncObserver alloc] init];
138 [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints];
140 EXPECT_TRUE(sdpObserver.success);
141 RTCSessionDescription *offerSDP = sdpObserver.sessionDescription;
142 EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch],
144 EXPECT_GT([offerSDP.description length], 0);
146 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
147 [answeringExpectations
148 expectSignalingChange:RTCSignalingHaveRemoteOffer];
149 [answeringExpectations expectAddStream:@"oLMS"];
150 [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver
151 sessionDescription:offerSDP];
154 RTCMediaStream *aLMSUnused =
155 [self addTracksToPeerConnection:pcAnswer
157 videoSource:videoSource
159 videoTrackID:@"aLMSv0"
160 audioTrackID:@"aLMSa0"];
162 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
163 [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints];
165 EXPECT_TRUE(sdpObserver.success);
166 RTCSessionDescription *answerSDP = sdpObserver.sessionDescription;
167 EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch],
169 EXPECT_GT([answerSDP.description length], 0);
171 [offeringExpectations expectICECandidates:2];
172 [answeringExpectations expectICECandidates:2];
174 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
175 [answeringExpectations expectSignalingChange:RTCSignalingStable];
176 [pcAnswer setLocalDescriptionWithDelegate:sdpObserver
177 sessionDescription:answerSDP];
179 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
181 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
182 [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer];
183 [pcOffer setLocalDescriptionWithDelegate:sdpObserver
184 sessionDescription:offerSDP];
186 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
188 [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
189 [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
190 [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking];
191 [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected];
193 [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
194 [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete];
196 sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init];
197 [offeringExpectations expectSignalingChange:RTCSignalingStable];
198 [offeringExpectations expectAddStream:@"aLMS"];
199 [pcOffer setRemoteDescriptionWithDelegate:sdpObserver
200 sessionDescription:answerSDP];
202 EXPECT_TRUE(sdpObserver.sessionDescription == NULL);
204 EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]);
205 EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]);
206 EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]);
207 EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]);
209 for (RTCICECandidate *candidate in
210 offeringExpectations.releaseReceivedICECandidates) {
211 [pcAnswer addICECandidate:candidate];
213 for (RTCICECandidate *candidate in
214 answeringExpectations.releaseReceivedICECandidates) {
215 [pcOffer addICECandidate:candidate];
218 [offeringExpectations waitForAllExpectationsToBeSatisfied];
219 [answeringExpectations waitForAllExpectationsToBeSatisfied];
221 // Let the audio feedback run for 10s to allow human testing and to ensure
222 // things stabilize. TODO(fischman): replace seconds with # of video frames,
223 // when we have video flowing.
224 [[NSRunLoop currentRunLoop]
225 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
227 // TODO(hughv): Implement orderly shutdown.
233 TEST(RTCPeerConnectionTest, SessionTest) {
234 RTCPeerConnectionTest *pcTest = [[RTCPeerConnectionTest alloc] init];
235 [pcTest testCompleteSession];