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" |
jiayl@webrtc.org | a576faf | 2014-01-29 17:45:53 +0000 | [diff] [blame] | 42 | #include "talk/base/ssladapter.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | |
| 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.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 51 | + (BOOL)isSession:(RTCSessionDescription*)session1 |
| 52 | ofSameTypeAsSession:(RTCSessionDescription*)session2; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | |
| 54 | // Create and add tracks to pc, with the given source, label, and IDs |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 55 | - (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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | |
fischman@webrtc.org | 385a722 | 2014-03-25 05:16:29 +0000 | [diff] [blame^] | 62 | - (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | |
| 64 | @end |
| 65 | |
| 66 | @implementation RTCPeerConnectionTest |
| 67 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 68 | + (BOOL)isSession:(RTCSessionDescription*)session1 |
| 69 | ofSameTypeAsSession:(RTCSessionDescription*)session2 { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | return [session1.type isEqual:session2.type]; |
| 71 | } |
| 72 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 73 | - (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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | [factory videoTrackWithID:videoTrackID source:videoSource]; |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 82 | RTCVideoRenderer* videoRenderer = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | [[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.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 89 | RTCAudioTrack* audioTrack = [factory audioTrackWithID:audioTrackID]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | [localMediaStream addAudioTrack:audioTrack]; |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 91 | RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] init]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | [pc addStream:localMediaStream constraints:constraints]; |
| 93 | return localMediaStream; |
| 94 | } |
| 95 | |
fischman@webrtc.org | 385a722 | 2014-03-25 05:16:29 +0000 | [diff] [blame^] | 96 | - (void)testCompleteSessionWithFactory:(RTCPeerConnectionFactory*)factory { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 97 | RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] init]; |
| 98 | RTCPeerConnectionSyncObserver* offeringExpectations = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | [[RTCPeerConnectionSyncObserver alloc] init]; |
fischman@webrtc.org | 13320ea | 2014-03-07 22:15:30 +0000 | [diff] [blame] | 100 | RTCPeerConnection* pcOffer = |
| 101 | [factory peerConnectionWithICEServers:nil |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | constraints:constraints |
| 103 | delegate:offeringExpectations]; |
| 104 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 105 | RTCPeerConnectionSyncObserver* answeringExpectations = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | [[RTCPeerConnectionSyncObserver alloc] init]; |
fischman@webrtc.org | 13320ea | 2014-03-07 22:15:30 +0000 | [diff] [blame] | 107 | |
| 108 | RTCPeerConnection* pcAnswer = |
| 109 | [factory peerConnectionWithICEServers:nil |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | constraints:constraints |
| 111 | delegate:answeringExpectations]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | // TODO(hughv): Create video capturer |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 113 | RTCVideoCapturer* capturer = nil; |
| 114 | RTCVideoSource* videoSource = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | [factory videoSourceWithCapturer:capturer constraints:constraints]; |
| 116 | |
| 117 | // Here and below, "oLMS" refers to offerer's local media stream, and "aLMS" |
| 118 | // refers to the answerer's local media stream, with suffixes of "a0" and "v0" |
| 119 | // for audio and video tracks, resp. These mirror chrome historical naming. |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 120 | RTCMediaStream* oLMSUnused = [self addTracksToPeerConnection:pcOffer |
| 121 | withFactory:factory |
| 122 | videoSource:videoSource |
| 123 | streamLabel:@"oLMS" |
| 124 | videoTrackID:@"oLMSv0" |
| 125 | audioTrackID:@"oLMSa0"]; |
| 126 | RTCSessionDescriptionSyncObserver* sdpObserver = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 128 | [pcOffer createOfferWithDelegate:sdpObserver constraints:constraints]; |
| 129 | [sdpObserver wait]; |
| 130 | EXPECT_TRUE(sdpObserver.success); |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 131 | RTCSessionDescription* offerSDP = sdpObserver.sessionDescription; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | EXPECT_EQ([@"offer" compare:offerSDP.type options:NSCaseInsensitiveSearch], |
| 133 | NSOrderedSame); |
| 134 | EXPECT_GT([offerSDP.description length], 0); |
| 135 | |
| 136 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 137 | [answeringExpectations expectSignalingChange:RTCSignalingHaveRemoteOffer]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | [answeringExpectations expectAddStream:@"oLMS"]; |
| 139 | [pcAnswer setRemoteDescriptionWithDelegate:sdpObserver |
| 140 | sessionDescription:offerSDP]; |
| 141 | [sdpObserver wait]; |
| 142 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 143 | RTCMediaStream* aLMSUnused = [self addTracksToPeerConnection:pcAnswer |
| 144 | withFactory:factory |
| 145 | videoSource:videoSource |
| 146 | streamLabel:@"aLMS" |
| 147 | videoTrackID:@"aLMSv0" |
| 148 | audioTrackID:@"aLMSa0"]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 149 | |
| 150 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 151 | [pcAnswer createAnswerWithDelegate:sdpObserver constraints:constraints]; |
| 152 | [sdpObserver wait]; |
| 153 | EXPECT_TRUE(sdpObserver.success); |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 154 | RTCSessionDescription* answerSDP = sdpObserver.sessionDescription; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | EXPECT_EQ([@"answer" compare:answerSDP.type options:NSCaseInsensitiveSearch], |
| 156 | NSOrderedSame); |
| 157 | EXPECT_GT([answerSDP.description length], 0); |
| 158 | |
| 159 | [offeringExpectations expectICECandidates:2]; |
| 160 | [answeringExpectations expectICECandidates:2]; |
| 161 | |
| 162 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 163 | [answeringExpectations expectSignalingChange:RTCSignalingStable]; |
| 164 | [pcAnswer setLocalDescriptionWithDelegate:sdpObserver |
| 165 | sessionDescription:answerSDP]; |
| 166 | [sdpObserver wait]; |
| 167 | EXPECT_TRUE(sdpObserver.sessionDescription == NULL); |
| 168 | |
| 169 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 170 | [offeringExpectations expectSignalingChange:RTCSignalingHaveLocalOffer]; |
| 171 | [pcOffer setLocalDescriptionWithDelegate:sdpObserver |
| 172 | sessionDescription:offerSDP]; |
| 173 | [sdpObserver wait]; |
| 174 | EXPECT_TRUE(sdpObserver.sessionDescription == NULL); |
| 175 | |
| 176 | [offeringExpectations expectICEConnectionChange:RTCICEConnectionChecking]; |
| 177 | [offeringExpectations expectICEConnectionChange:RTCICEConnectionConnected]; |
fischman@webrtc.org | a01daf0 | 2014-03-08 03:17:55 +0000 | [diff] [blame] | 178 | // TODO(fischman): figure out why this is flaky and re-introduce (and remove |
| 179 | // special-casing from the observer!). |
| 180 | // [offeringExpectations expectICEConnectionChange:RTCICEConnectionCompleted]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 181 | [answeringExpectations expectICEConnectionChange:RTCICEConnectionChecking]; |
| 182 | [answeringExpectations expectICEConnectionChange:RTCICEConnectionConnected]; |
| 183 | |
| 184 | [offeringExpectations expectICEGatheringChange:RTCICEGatheringComplete]; |
| 185 | [answeringExpectations expectICEGatheringChange:RTCICEGatheringComplete]; |
| 186 | |
| 187 | sdpObserver = [[RTCSessionDescriptionSyncObserver alloc] init]; |
| 188 | [offeringExpectations expectSignalingChange:RTCSignalingStable]; |
| 189 | [offeringExpectations expectAddStream:@"aLMS"]; |
| 190 | [pcOffer setRemoteDescriptionWithDelegate:sdpObserver |
| 191 | sessionDescription:answerSDP]; |
| 192 | [sdpObserver wait]; |
| 193 | EXPECT_TRUE(sdpObserver.sessionDescription == NULL); |
| 194 | |
| 195 | EXPECT_TRUE([offerSDP.type isEqual:pcOffer.localDescription.type]); |
| 196 | EXPECT_TRUE([answerSDP.type isEqual:pcOffer.remoteDescription.type]); |
| 197 | EXPECT_TRUE([offerSDP.type isEqual:pcAnswer.remoteDescription.type]); |
| 198 | EXPECT_TRUE([answerSDP.type isEqual:pcAnswer.localDescription.type]); |
| 199 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 200 | for (RTCICECandidate* candidate in offeringExpectations |
| 201 | .releaseReceivedICECandidates) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 202 | [pcAnswer addICECandidate:candidate]; |
| 203 | } |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 204 | for (RTCICECandidate* candidate in answeringExpectations |
| 205 | .releaseReceivedICECandidates) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 206 | [pcOffer addICECandidate:candidate]; |
| 207 | } |
| 208 | |
| 209 | [offeringExpectations waitForAllExpectationsToBeSatisfied]; |
| 210 | [answeringExpectations waitForAllExpectationsToBeSatisfied]; |
| 211 | |
fischman@webrtc.org | 13320ea | 2014-03-07 22:15:30 +0000 | [diff] [blame] | 212 | // Let the audio feedback run for 2s to allow human testing and to ensure |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 213 | // things stabilize. TODO(fischman): replace seconds with # of video frames, |
| 214 | // when we have video flowing. |
| 215 | [[NSRunLoop currentRunLoop] |
fischman@webrtc.org | 13320ea | 2014-03-07 22:15:30 +0000 | [diff] [blame] | 216 | runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 217 | |
fischman@webrtc.org | 385a722 | 2014-03-25 05:16:29 +0000 | [diff] [blame^] | 218 | [offeringExpectations expectICEConnectionChange:RTCICEConnectionClosed]; |
| 219 | [answeringExpectations expectICEConnectionChange:RTCICEConnectionClosed]; |
| 220 | [offeringExpectations expectSignalingChange:RTCSignalingClosed]; |
| 221 | [answeringExpectations expectSignalingChange:RTCSignalingClosed]; |
| 222 | |
| 223 | [pcOffer close]; |
| 224 | [pcAnswer close]; |
| 225 | |
| 226 | [offeringExpectations waitForAllExpectationsToBeSatisfied]; |
| 227 | [answeringExpectations waitForAllExpectationsToBeSatisfied]; |
| 228 | |
| 229 | capturer = nil; |
| 230 | videoSource = nil; |
| 231 | pcOffer = nil; |
| 232 | pcAnswer = nil; |
| 233 | // TODO(fischman): be stricter about shutdown checks; ensure thread |
| 234 | // counts return to where they were before the test kicked off, and |
| 235 | // that all objects have in fact shut down. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @end |
| 239 | |
jiayl@webrtc.org | a576faf | 2014-01-29 17:45:53 +0000 | [diff] [blame] | 240 | // TODO(fischman): move {Initialize,Cleanup}SSL into alloc/dealloc of |
| 241 | // RTCPeerConnectionTest and avoid the appearance of RTCPeerConnectionTest being |
| 242 | // a TestBase since it's not. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 243 | TEST(RTCPeerConnectionTest, SessionTest) { |
fischman@webrtc.org | 385a722 | 2014-03-25 05:16:29 +0000 | [diff] [blame^] | 244 | @autoreleasepool { |
| 245 | talk_base::InitializeSSL(); |
| 246 | // Since |factory| will own the signaling & worker threads, it's important |
| 247 | // that it outlive the created PeerConnections since they self-delete on the |
| 248 | // signaling thread, and if |factory| is freed first then a last refcount on |
| 249 | // the factory will expire during this teardown, causing the signaling |
| 250 | // thread to try to Join() with itself. This is a hack to ensure that the |
| 251 | // factory outlives RTCPeerConnection:dealloc. |
| 252 | // See https://code.google.com/p/webrtc/issues/detail?id=3100. |
| 253 | RTCPeerConnectionFactory* factory = [[RTCPeerConnectionFactory alloc] init]; |
| 254 | @autoreleasepool { |
| 255 | RTCPeerConnectionTest* pcTest = [[RTCPeerConnectionTest alloc] init]; |
| 256 | [pcTest testCompleteSessionWithFactory:factory]; |
| 257 | } |
| 258 | talk_base::CleanupSSL(); |
| 259 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | } |