henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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] |
fischman@webrtc.org | 016eec0 | 2013-09-05 23:11:55 +0000 | [diff] [blame^] | 102 | username:@"" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | password:@""]; |
| 104 | NSArray *iceServers = @[stunServer]; |
| 105 | |
| 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]; |
| 113 | |
| 114 | RTCPeerConnectionSyncObserver *answeringExpectations = |
| 115 | [[RTCPeerConnectionSyncObserver alloc] init]; |
| 116 | RTCPeerConnection *pcAnswer = |
| 117 | [factory peerConnectionWithICEServers:iceServers |
| 118 | constraints:constraints |
| 119 | delegate:answeringExpectations]; |
| 120 | |
| 121 | // TODO(hughv): Create video capturer |
| 122 | RTCVideoCapturer *capturer = nil; |
| 123 | RTCVideoSource *videoSource = |
| 124 | [factory videoSourceWithCapturer:capturer constraints:constraints]; |
| 125 | |
| 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 |
| 131 | withFactory:factory |
| 132 | videoSource:videoSource |
| 133 | streamLabel:@"oLMS" |
| 134 | videoTrackID:@"oLMSv0" |
| 135 | audioTrackID:@"oLMSa0"]; |
| 136 | RTCSessionDescriptionSyncObserver *sdpObserver = |
| 137 | [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 138 | [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints]; |
| 139 | [sdpObserver wait]; |
| 140 | EXPECT_TRUE(sdpObserver.success); |
| 141 | RTCSessionDescription *offerSDP = sdpObserver.sessionDescription; |
| 142 | EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch], |
| 143 | NSOrderedSame); |
| 144 | EXPECT_GT([offerSDP.description length], 0); |
| 145 | |
| 146 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 147 | [answeringExpectations |
| 148 | expectSignalingChange:RTCSignalingHaveRemoteOffer]; |
| 149 | [answeringExpectations expectAddStream:@"oLMS"]; |
| 150 | [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver |
| 151 | sessionDescription:offerSDP]; |
| 152 | [sdpObserver wait]; |
| 153 | |
| 154 | RTCMediaStream *aLMSUnused = |
| 155 | [self addTracksToPeerConnection:pcAnswer |
| 156 | withFactory:factory |
| 157 | videoSource:videoSource |
| 158 | streamLabel:@"aLMS" |
| 159 | videoTrackID:@"aLMSv0" |
| 160 | audioTrackID:@"aLMSa0"]; |
| 161 | |
| 162 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 163 | [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints]; |
| 164 | [sdpObserver wait]; |
| 165 | EXPECT_TRUE(sdpObserver.success); |
| 166 | RTCSessionDescription *answerSDP = sdpObserver.sessionDescription; |
| 167 | EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch], |
| 168 | NSOrderedSame); |
| 169 | EXPECT_GT([answerSDP.description length], 0); |
| 170 | |
| 171 | [offeringExpectations expectICECandidates:2]; |
| 172 | [answeringExpectations expectICECandidates:2]; |
| 173 | |
| 174 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 175 | [answeringExpectations expectSignalingChange:RTCSignalingStable]; |
| 176 | [pcAnswer setLocalDescriptionWithDelegate:sdpObserver |
| 177 | sessionDescription:answerSDP]; |
| 178 | [sdpObserver wait]; |
| 179 | EXPECT_TRUE(sdpObserver.sessionDescription == NULL); |
| 180 | |
| 181 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 182 | [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer]; |
| 183 | [pcOffer setLocalDescriptionWithDelegate:sdpObserver |
| 184 | sessionDescription:offerSDP]; |
| 185 | [sdpObserver wait]; |
| 186 | EXPECT_TRUE(sdpObserver.sessionDescription == NULL); |
| 187 | |
| 188 | [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking]; |
| 189 | [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected]; |
| 190 | [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking]; |
| 191 | [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected]; |
| 192 | |
| 193 | [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete]; |
| 194 | [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete]; |
| 195 | |
| 196 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 197 | [offeringExpectations expectSignalingChange:RTCSignalingStable]; |
| 198 | [offeringExpectations expectAddStream:@"aLMS"]; |
| 199 | [pcOffer setRemoteDescriptionWithDelegate:sdpObserver |
| 200 | sessionDescription:answerSDP]; |
| 201 | [sdpObserver wait]; |
| 202 | EXPECT_TRUE(sdpObserver.sessionDescription == NULL); |
| 203 | |
| 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]); |
| 208 | |
| 209 | for (RTCICECandidate *candidate in |
| 210 | offeringExpectations.releaseReceivedICECandidates) { |
| 211 | [pcAnswer addICECandidate:candidate]; |
| 212 | } |
| 213 | for (RTCICECandidate *candidate in |
| 214 | answeringExpectations.releaseReceivedICECandidates) { |
| 215 | [pcOffer addICECandidate:candidate]; |
| 216 | } |
| 217 | |
| 218 | [offeringExpectations waitForAllExpectationsToBeSatisfied]; |
| 219 | [answeringExpectations waitForAllExpectationsToBeSatisfied]; |
| 220 | |
| 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]]; |
| 226 | |
| 227 | // TODO(hughv): Implement orderly shutdown. |
| 228 | } |
| 229 | |
| 230 | @end |
| 231 | |
| 232 | |
| 233 | TEST(RTCPeerConnectionTest, SessionTest) { |
| 234 | RTCPeerConnectionTest *pcTest = [[RTCPeerConnectionTest alloc] init]; |
| 235 | [pcTest testCompleteSession]; |
| 236 | } |