Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Karl Wiberg | 32df86e | 2017-11-03 10:24:27 +0100 | [diff] [blame] | 11 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 12 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 13 | #include "api/peerconnectionproxy.h" |
| 14 | #include "p2p/base/fakeportallocator.h" |
| 15 | #include "p2p/base/teststunserver.h" |
| 16 | #include "p2p/client/basicportallocator.h" |
| 17 | #include "pc/mediasession.h" |
| 18 | #include "pc/peerconnection.h" |
| 19 | #include "pc/peerconnectionwrapper.h" |
| 20 | #include "pc/sdputils.h" |
| 21 | #ifdef WEBRTC_ANDROID |
| 22 | #include "pc/test/androidtestinitializer.h" |
| 23 | #endif |
| 24 | #include "pc/test/fakeaudiocapturemodule.h" |
| 25 | #include "rtc_base/fakenetwork.h" |
| 26 | #include "rtc_base/gunit.h" |
| 27 | #include "rtc_base/ptr_util.h" |
| 28 | #include "rtc_base/virtualsocketserver.h" |
| 29 | #include "test/gmock.h" |
| 30 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | using BundlePolicy = PeerConnectionInterface::BundlePolicy; |
| 34 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 35 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
| 36 | using RtcpMuxPolicy = PeerConnectionInterface::RtcpMuxPolicy; |
| 37 | using rtc::SocketAddress; |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 38 | using ::testing::Combine; |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 39 | using ::testing::ElementsAre; |
| 40 | using ::testing::UnorderedElementsAre; |
| 41 | using ::testing::Values; |
| 42 | |
| 43 | constexpr int kDefaultTimeout = 10000; |
| 44 | |
| 45 | // TODO(steveanton): These tests should be rewritten to use the standard |
| 46 | // RtpSenderInterface/DtlsTransportInterface objects once they're available in |
| 47 | // the API. The RtpSender can be used to determine which transport a given media |
| 48 | // will use: https://www.w3.org/TR/webrtc/#dom-rtcrtpsender-transport |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 49 | // Should also be able to remove GetTransceiversForTesting at that point. |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 50 | |
| 51 | class PeerConnectionWrapperForBundleTest : public PeerConnectionWrapper { |
| 52 | public: |
| 53 | using PeerConnectionWrapper::PeerConnectionWrapper; |
| 54 | |
| 55 | bool AddIceCandidateToMedia(cricket::Candidate* candidate, |
| 56 | cricket::MediaType media_type) { |
| 57 | auto* desc = pc()->remote_description()->description(); |
| 58 | for (size_t i = 0; i < desc->contents().size(); i++) { |
| 59 | const auto& content = desc->contents()[i]; |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 60 | if (content.media_description()->type() == media_type) { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 61 | candidate->set_transport_name(content.name); |
| 62 | JsepIceCandidate jsep_candidate(content.name, i, *candidate); |
| 63 | return pc()->AddIceCandidate(&jsep_candidate); |
| 64 | } |
| 65 | } |
| 66 | RTC_NOTREACHED(); |
| 67 | return false; |
| 68 | } |
| 69 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 70 | rtc::PacketTransportInternal* voice_rtp_transport() { |
| 71 | return (voice_channel() ? voice_channel()->rtp_packet_transport() |
| 72 | : nullptr); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 75 | rtc::PacketTransportInternal* voice_rtcp_transport() { |
| 76 | return (voice_channel() ? voice_channel()->rtcp_packet_transport() |
| 77 | : nullptr); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | cricket::VoiceChannel* voice_channel() { |
Steve Anton | b886711 | 2018-02-13 10:07:54 -0800 | [diff] [blame] | 81 | auto transceivers = GetInternalPeerConnection()->GetTransceiversInternal(); |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 82 | for (auto transceiver : transceivers) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 83 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 84 | return static_cast<cricket::VoiceChannel*>( |
| 85 | transceiver->internal()->channel()); |
| 86 | } |
| 87 | } |
| 88 | return nullptr; |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 91 | rtc::PacketTransportInternal* video_rtp_transport() { |
| 92 | return (video_channel() ? video_channel()->rtp_packet_transport() |
| 93 | : nullptr); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 96 | rtc::PacketTransportInternal* video_rtcp_transport() { |
| 97 | return (video_channel() ? video_channel()->rtcp_packet_transport() |
| 98 | : nullptr); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | cricket::VideoChannel* video_channel() { |
Steve Anton | b886711 | 2018-02-13 10:07:54 -0800 | [diff] [blame] | 102 | auto transceivers = GetInternalPeerConnection()->GetTransceiversInternal(); |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 103 | for (auto transceiver : transceivers) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 104 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 105 | return static_cast<cricket::VideoChannel*>( |
| 106 | transceiver->internal()->channel()); |
| 107 | } |
| 108 | } |
| 109 | return nullptr; |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | PeerConnection* GetInternalPeerConnection() { |
Mirko Bonadei | e97de91 | 2017-12-13 11:29:34 +0100 | [diff] [blame] | 113 | auto* pci = |
| 114 | static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>( |
| 115 | pc()); |
| 116 | return static_cast<PeerConnection*>(pci->internal()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Returns true if the stats indicate that an ICE connection is either in |
| 120 | // progress or established with the given remote address. |
| 121 | bool HasConnectionWithRemoteAddress(const SocketAddress& address) { |
| 122 | auto report = GetStats(); |
| 123 | if (!report) { |
| 124 | return false; |
| 125 | } |
| 126 | std::string matching_candidate_id; |
| 127 | for (auto* ice_candidate_stats : |
| 128 | report->GetStatsOfType<RTCRemoteIceCandidateStats>()) { |
| 129 | if (*ice_candidate_stats->ip == address.HostAsURIString() && |
| 130 | *ice_candidate_stats->port == address.port()) { |
| 131 | matching_candidate_id = ice_candidate_stats->id(); |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | if (matching_candidate_id.empty()) { |
| 136 | return false; |
| 137 | } |
| 138 | for (auto* pair_stats : |
| 139 | report->GetStatsOfType<RTCIceCandidatePairStats>()) { |
| 140 | if (*pair_stats->remote_candidate_id == matching_candidate_id) { |
| 141 | if (*pair_stats->state == RTCStatsIceCandidatePairState::kInProgress || |
| 142 | *pair_stats->state == RTCStatsIceCandidatePairState::kSucceeded) { |
| 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | rtc::FakeNetworkManager* network() { return network_; } |
| 151 | |
| 152 | void set_network(rtc::FakeNetworkManager* network) { network_ = network; } |
| 153 | |
| 154 | private: |
| 155 | rtc::FakeNetworkManager* network_; |
| 156 | }; |
| 157 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 158 | class PeerConnectionBundleBaseTest : public ::testing::Test { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 159 | protected: |
| 160 | typedef std::unique_ptr<PeerConnectionWrapperForBundleTest> WrapperPtr; |
| 161 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 162 | explicit PeerConnectionBundleBaseTest(SdpSemantics sdp_semantics) |
| 163 | : vss_(new rtc::VirtualSocketServer()), |
| 164 | main_(vss_.get()), |
| 165 | sdp_semantics_(sdp_semantics) { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 166 | #ifdef WEBRTC_ANDROID |
| 167 | InitializeAndroidObjects(); |
| 168 | #endif |
| 169 | pc_factory_ = CreatePeerConnectionFactory( |
| 170 | rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(), |
Karl Wiberg | 32df86e | 2017-11-03 10:24:27 +0100 | [diff] [blame] | 171 | FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(), |
| 172 | CreateBuiltinAudioDecoderFactory(), nullptr, nullptr); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | WrapperPtr CreatePeerConnection() { |
| 176 | return CreatePeerConnection(RTCConfiguration()); |
| 177 | } |
| 178 | |
| 179 | WrapperPtr CreatePeerConnection(const RTCConfiguration& config) { |
| 180 | auto* fake_network = NewFakeNetwork(); |
| 181 | auto port_allocator = |
| 182 | rtc::MakeUnique<cricket::BasicPortAllocator>(fake_network); |
| 183 | port_allocator->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP | |
| 184 | cricket::PORTALLOCATOR_DISABLE_RELAY); |
| 185 | port_allocator->set_step_delay(cricket::kMinimumStepDelay); |
| 186 | auto observer = rtc::MakeUnique<MockPeerConnectionObserver>(); |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 187 | RTCConfiguration modified_config = config; |
| 188 | modified_config.sdp_semantics = sdp_semantics_; |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 189 | auto pc = pc_factory_->CreatePeerConnection( |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 190 | modified_config, std::move(port_allocator), nullptr, observer.get()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 191 | if (!pc) { |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
| 195 | auto wrapper = rtc::MakeUnique<PeerConnectionWrapperForBundleTest>( |
| 196 | pc_factory_, pc, std::move(observer)); |
| 197 | wrapper->set_network(fake_network); |
| 198 | return wrapper; |
| 199 | } |
| 200 | |
| 201 | // Accepts the same arguments as CreatePeerConnection and adds default audio |
| 202 | // and video tracks. |
| 203 | template <typename... Args> |
| 204 | WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) { |
| 205 | auto wrapper = CreatePeerConnection(std::forward<Args>(args)...); |
| 206 | if (!wrapper) { |
| 207 | return nullptr; |
| 208 | } |
| 209 | wrapper->AddAudioTrack("a"); |
| 210 | wrapper->AddVideoTrack("v"); |
| 211 | return wrapper; |
| 212 | } |
| 213 | |
| 214 | cricket::Candidate CreateLocalUdpCandidate( |
| 215 | const rtc::SocketAddress& address) { |
| 216 | cricket::Candidate candidate; |
| 217 | candidate.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 218 | candidate.set_protocol(cricket::UDP_PROTOCOL_NAME); |
| 219 | candidate.set_address(address); |
| 220 | candidate.set_type(cricket::LOCAL_PORT_TYPE); |
| 221 | return candidate; |
| 222 | } |
| 223 | |
| 224 | rtc::FakeNetworkManager* NewFakeNetwork() { |
| 225 | // The PeerConnection's port allocator is tied to the PeerConnection's |
| 226 | // lifetime and expects the underlying NetworkManager to outlive it. If |
| 227 | // PeerConnectionWrapper owned the NetworkManager, it would be destroyed |
| 228 | // before the PeerConnection (since subclass members are destroyed before |
| 229 | // base class members). Therefore, the test fixture will own all the fake |
| 230 | // networks even though tests should access the fake network through the |
| 231 | // PeerConnectionWrapper. |
| 232 | auto* fake_network = new rtc::FakeNetworkManager(); |
| 233 | fake_networks_.emplace_back(fake_network); |
| 234 | return fake_network; |
| 235 | } |
| 236 | |
| 237 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 238 | rtc::AutoSocketServerThread main_; |
| 239 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
| 240 | std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_; |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 241 | const SdpSemantics sdp_semantics_; |
| 242 | }; |
| 243 | |
| 244 | class PeerConnectionBundleTest |
| 245 | : public PeerConnectionBundleBaseTest, |
| 246 | public ::testing::WithParamInterface<SdpSemantics> { |
| 247 | protected: |
| 248 | PeerConnectionBundleTest() : PeerConnectionBundleBaseTest(GetParam()) {} |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | SdpContentMutator RemoveRtcpMux() { |
| 252 | return [](cricket::ContentInfo* content, cricket::TransportInfo* transport) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 253 | content->media_description()->set_rtcp_mux(false); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 254 | }; |
| 255 | } |
| 256 | |
| 257 | std::vector<int> GetCandidateComponents( |
| 258 | const std::vector<IceCandidateInterface*> candidates) { |
| 259 | std::vector<int> components; |
| 260 | for (auto* candidate : candidates) { |
| 261 | components.push_back(candidate->candidate().component()); |
| 262 | } |
| 263 | return components; |
| 264 | } |
| 265 | |
| 266 | // Test that there are 2 local UDP candidates (1 RTP and 1 RTCP candidate) for |
| 267 | // each media section when disabling bundling and disabling RTCP multiplexing. |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 268 | TEST_P(PeerConnectionBundleTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 269 | TwoCandidatesForEachTransportWhenNoBundleNoRtcpMux) { |
| 270 | const SocketAddress kCallerAddress("1.1.1.1", 0); |
| 271 | const SocketAddress kCalleeAddress("2.2.2.2", 0); |
| 272 | |
| 273 | RTCConfiguration config; |
| 274 | config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate; |
| 275 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 276 | caller->network()->AddInterface(kCallerAddress); |
| 277 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 278 | callee->network()->AddInterface(kCalleeAddress); |
| 279 | |
| 280 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 281 | RTCOfferAnswerOptions options_no_bundle; |
| 282 | options_no_bundle.use_rtp_mux = false; |
| 283 | auto answer = callee->CreateAnswer(options_no_bundle); |
| 284 | SdpContentsForEach(RemoveRtcpMux(), answer->description()); |
| 285 | ASSERT_TRUE( |
| 286 | callee->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 287 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 288 | |
| 289 | // Check that caller has separate RTP and RTCP candidates for each media. |
| 290 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kDefaultTimeout); |
| 291 | EXPECT_THAT( |
| 292 | GetCandidateComponents(caller->observer()->GetCandidatesByMline(0)), |
| 293 | UnorderedElementsAre(cricket::ICE_CANDIDATE_COMPONENT_RTP, |
| 294 | cricket::ICE_CANDIDATE_COMPONENT_RTCP)); |
| 295 | EXPECT_THAT( |
| 296 | GetCandidateComponents(caller->observer()->GetCandidatesByMline(1)), |
| 297 | UnorderedElementsAre(cricket::ICE_CANDIDATE_COMPONENT_RTP, |
| 298 | cricket::ICE_CANDIDATE_COMPONENT_RTCP)); |
| 299 | |
| 300 | // Check that callee has separate RTP and RTCP candidates for each media. |
| 301 | EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kDefaultTimeout); |
| 302 | EXPECT_THAT( |
| 303 | GetCandidateComponents(callee->observer()->GetCandidatesByMline(0)), |
| 304 | UnorderedElementsAre(cricket::ICE_CANDIDATE_COMPONENT_RTP, |
| 305 | cricket::ICE_CANDIDATE_COMPONENT_RTCP)); |
| 306 | EXPECT_THAT( |
| 307 | GetCandidateComponents(callee->observer()->GetCandidatesByMline(1)), |
| 308 | UnorderedElementsAre(cricket::ICE_CANDIDATE_COMPONENT_RTP, |
| 309 | cricket::ICE_CANDIDATE_COMPONENT_RTCP)); |
| 310 | } |
| 311 | |
| 312 | // Test that there is 1 local UDP candidate for both RTP and RTCP for each media |
| 313 | // section when disabling bundle but enabling RTCP multiplexing. |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 314 | TEST_P(PeerConnectionBundleTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 315 | OneCandidateForEachTransportWhenNoBundleButRtcpMux) { |
| 316 | const SocketAddress kCallerAddress("1.1.1.1", 0); |
| 317 | |
| 318 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 319 | caller->network()->AddInterface(kCallerAddress); |
| 320 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 321 | |
| 322 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 323 | RTCOfferAnswerOptions options_no_bundle; |
| 324 | options_no_bundle.use_rtp_mux = false; |
| 325 | ASSERT_TRUE( |
| 326 | caller->SetRemoteDescription(callee->CreateAnswer(options_no_bundle))); |
| 327 | |
| 328 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kDefaultTimeout); |
| 329 | |
| 330 | EXPECT_EQ(1u, caller->observer()->GetCandidatesByMline(0).size()); |
| 331 | EXPECT_EQ(1u, caller->observer()->GetCandidatesByMline(1).size()); |
| 332 | } |
| 333 | |
| 334 | // Test that there is 1 local UDP candidate in only the first media section when |
| 335 | // bundling and enabling RTCP multiplexing. |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 336 | TEST_P(PeerConnectionBundleTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 337 | OneCandidateOnlyOnFirstTransportWhenBundleAndRtcpMux) { |
| 338 | const SocketAddress kCallerAddress("1.1.1.1", 0); |
| 339 | |
| 340 | RTCConfiguration config; |
| 341 | config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle; |
| 342 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 343 | caller->network()->AddInterface(kCallerAddress); |
| 344 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 345 | |
| 346 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 347 | ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateAnswer())); |
| 348 | |
| 349 | EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kDefaultTimeout); |
| 350 | |
| 351 | EXPECT_EQ(1u, caller->observer()->GetCandidatesByMline(0).size()); |
| 352 | EXPECT_EQ(0u, caller->observer()->GetCandidatesByMline(1).size()); |
| 353 | } |
| 354 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 355 | // It will fail if the offerer uses the mux-BUNDLE policy but the answerer |
| 356 | // doesn't support BUNDLE. |
| 357 | TEST_P(PeerConnectionBundleTest, MaxBundleNotSupportedInAnswer) { |
| 358 | RTCConfiguration config; |
| 359 | config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle; |
| 360 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 361 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 362 | |
| 363 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 364 | bool equal_before = |
| 365 | (caller->voice_rtp_transport() == caller->video_rtp_transport()); |
| 366 | EXPECT_EQ(true, equal_before); |
| 367 | RTCOfferAnswerOptions options; |
| 368 | options.use_rtp_mux = false; |
| 369 | EXPECT_FALSE( |
| 370 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(options))); |
| 371 | } |
| 372 | |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 373 | // The following parameterized test verifies that an offer/answer with varying |
| 374 | // bundle policies and either bundle in the answer or not will produce the |
| 375 | // expected RTP transports for audio and video. In particular, for bundling we |
| 376 | // care about whether they are separate transports or the same. |
| 377 | |
| 378 | enum class BundleIncluded { kBundleInAnswer, kBundleNotInAnswer }; |
| 379 | std::ostream& operator<<(std::ostream& out, BundleIncluded value) { |
| 380 | switch (value) { |
| 381 | case BundleIncluded::kBundleInAnswer: |
| 382 | return out << "bundle in answer"; |
| 383 | case BundleIncluded::kBundleNotInAnswer: |
| 384 | return out << "bundle not in answer"; |
| 385 | } |
| 386 | return out << "unknown"; |
| 387 | } |
| 388 | |
| 389 | class PeerConnectionBundleMatrixTest |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 390 | : public PeerConnectionBundleBaseTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 391 | public ::testing::WithParamInterface< |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 392 | std::tuple<SdpSemantics, |
| 393 | std::tuple<BundlePolicy, BundleIncluded, bool, bool>>> { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 394 | protected: |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 395 | PeerConnectionBundleMatrixTest() |
| 396 | : PeerConnectionBundleBaseTest(std::get<0>(GetParam())) { |
| 397 | auto param = std::get<1>(GetParam()); |
| 398 | bundle_policy_ = std::get<0>(param); |
| 399 | bundle_included_ = std::get<1>(param); |
| 400 | expected_same_before_ = std::get<2>(param); |
| 401 | expected_same_after_ = std::get<3>(param); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | PeerConnectionInterface::BundlePolicy bundle_policy_; |
| 405 | BundleIncluded bundle_included_; |
| 406 | bool expected_same_before_; |
| 407 | bool expected_same_after_; |
| 408 | }; |
| 409 | |
| 410 | TEST_P(PeerConnectionBundleMatrixTest, |
| 411 | VerifyTransportsBeforeAndAfterSettingRemoteAnswer) { |
| 412 | RTCConfiguration config; |
| 413 | config.bundle_policy = bundle_policy_; |
| 414 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 415 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 416 | |
| 417 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 418 | bool equal_before = |
| 419 | (caller->voice_rtp_transport() == caller->video_rtp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 420 | EXPECT_EQ(expected_same_before_, equal_before); |
| 421 | |
| 422 | RTCOfferAnswerOptions options; |
| 423 | options.use_rtp_mux = (bundle_included_ == BundleIncluded::kBundleInAnswer); |
| 424 | ASSERT_TRUE( |
| 425 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(options))); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 426 | bool equal_after = |
| 427 | (caller->voice_rtp_transport() == caller->video_rtp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 428 | EXPECT_EQ(expected_same_after_, equal_after); |
| 429 | } |
| 430 | |
| 431 | // The max-bundle policy means we should anticipate bundling being negotiated, |
| 432 | // and multiplex audio/video from the start. |
| 433 | // For all other policies, bundling should only be enabled if negotiated by the |
| 434 | // answer. |
| 435 | INSTANTIATE_TEST_CASE_P( |
| 436 | PeerConnectionBundleTest, |
| 437 | PeerConnectionBundleMatrixTest, |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 438 | Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan), |
| 439 | Values(std::make_tuple(BundlePolicy::kBundlePolicyBalanced, |
| 440 | BundleIncluded::kBundleInAnswer, |
| 441 | false, |
| 442 | true), |
| 443 | std::make_tuple(BundlePolicy::kBundlePolicyBalanced, |
| 444 | BundleIncluded::kBundleNotInAnswer, |
| 445 | false, |
| 446 | false), |
| 447 | std::make_tuple(BundlePolicy::kBundlePolicyMaxBundle, |
| 448 | BundleIncluded::kBundleInAnswer, |
| 449 | true, |
| 450 | true), |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 451 | std::make_tuple(BundlePolicy::kBundlePolicyMaxCompat, |
| 452 | BundleIncluded::kBundleInAnswer, |
| 453 | false, |
| 454 | true), |
| 455 | std::make_tuple(BundlePolicy::kBundlePolicyMaxCompat, |
| 456 | BundleIncluded::kBundleNotInAnswer, |
| 457 | false, |
| 458 | false)))); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 459 | |
| 460 | // Test that the audio/video transports on the callee side are the same before |
| 461 | // and after setting a local answer when max BUNDLE is enabled and an offer with |
| 462 | // BUNDLE is received. |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 463 | TEST_P(PeerConnectionBundleTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 464 | TransportsSameForMaxBundleWithBundleInRemoteOffer) { |
| 465 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 466 | RTCConfiguration config; |
| 467 | config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle; |
| 468 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 469 | |
| 470 | RTCOfferAnswerOptions options_with_bundle; |
| 471 | options_with_bundle.use_rtp_mux = true; |
| 472 | ASSERT_TRUE(callee->SetRemoteDescription( |
| 473 | caller->CreateOfferAndSetAsLocal(options_with_bundle))); |
| 474 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 475 | EXPECT_EQ(callee->voice_rtp_transport(), callee->video_rtp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 476 | |
| 477 | ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer())); |
| 478 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 479 | EXPECT_EQ(callee->voice_rtp_transport(), callee->video_rtp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 482 | TEST_P(PeerConnectionBundleTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 483 | FailToSetRemoteOfferWithNoBundleWhenBundlePolicyMaxBundle) { |
| 484 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 485 | RTCConfiguration config; |
| 486 | config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle; |
| 487 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 488 | |
| 489 | RTCOfferAnswerOptions options_no_bundle; |
| 490 | options_no_bundle.use_rtp_mux = false; |
| 491 | EXPECT_FALSE(callee->SetRemoteDescription( |
| 492 | caller->CreateOfferAndSetAsLocal(options_no_bundle))); |
| 493 | } |
| 494 | |
| 495 | // Test that if the media section which has the bundled transport is rejected, |
| 496 | // then the peers still connect and the bundled transport switches to the other |
| 497 | // media section. |
| 498 | // Note: This is currently failing because of the following bug: |
| 499 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=6280 |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 500 | TEST_P(PeerConnectionBundleTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 501 | DISABLED_SuccessfullyNegotiateMaxBundleIfBundleTransportMediaRejected) { |
| 502 | RTCConfiguration config; |
| 503 | config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle; |
| 504 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 505 | auto callee = CreatePeerConnection(); |
| 506 | callee->AddVideoTrack("v"); |
| 507 | |
| 508 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 509 | |
| 510 | RTCOfferAnswerOptions options; |
| 511 | options.offer_to_receive_audio = 0; |
| 512 | ASSERT_TRUE( |
| 513 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(options))); |
| 514 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 515 | EXPECT_FALSE(caller->voice_rtp_transport()); |
| 516 | EXPECT_TRUE(caller->video_rtp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // When requiring RTCP multiplexing, the PeerConnection never makes RTCP |
| 520 | // transport channels. |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 521 | TEST_P(PeerConnectionBundleTest, NeverCreateRtcpTransportWithRtcpMuxRequired) { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 522 | RTCConfiguration config; |
| 523 | config.rtcp_mux_policy = RtcpMuxPolicy::kRtcpMuxPolicyRequire; |
| 524 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 525 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 526 | |
| 527 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 528 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 529 | EXPECT_FALSE(caller->voice_rtcp_transport()); |
| 530 | EXPECT_FALSE(caller->video_rtcp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 531 | |
| 532 | ASSERT_TRUE( |
| 533 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 534 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 535 | EXPECT_FALSE(caller->voice_rtcp_transport()); |
| 536 | EXPECT_FALSE(caller->video_rtcp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 539 | // When negotiating RTCP multiplexing, the PeerConnection makes RTCP transports |
| 540 | // when the offer is sent, but will destroy them once the remote answer is set. |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 541 | TEST_P(PeerConnectionBundleTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 542 | CreateRtcpTransportOnlyBeforeAnswerWithRtcpMuxNegotiate) { |
| 543 | RTCConfiguration config; |
| 544 | config.rtcp_mux_policy = RtcpMuxPolicy::kRtcpMuxPolicyNegotiate; |
| 545 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 546 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 547 | |
| 548 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 549 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 550 | EXPECT_TRUE(caller->voice_rtcp_transport()); |
| 551 | EXPECT_TRUE(caller->video_rtcp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 552 | |
| 553 | ASSERT_TRUE( |
| 554 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 555 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 556 | EXPECT_FALSE(caller->voice_rtcp_transport()); |
| 557 | EXPECT_FALSE(caller->video_rtcp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 560 | TEST_P(PeerConnectionBundleTest, FailToSetDescriptionWithBundleAndNoRtcpMux) { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 561 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 562 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 563 | |
| 564 | RTCOfferAnswerOptions options; |
| 565 | options.use_rtp_mux = true; |
| 566 | |
| 567 | auto offer = caller->CreateOffer(options); |
| 568 | SdpContentsForEach(RemoveRtcpMux(), offer->description()); |
| 569 | |
| 570 | std::string error; |
| 571 | EXPECT_FALSE(caller->SetLocalDescription(CloneSessionDescription(offer.get()), |
| 572 | &error)); |
| 573 | EXPECT_EQ( |
| 574 | "Failed to set local offer sdp: rtcp-mux must be enabled when BUNDLE is " |
| 575 | "enabled.", |
| 576 | error); |
| 577 | |
| 578 | EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer), &error)); |
| 579 | EXPECT_EQ( |
| 580 | "Failed to set remote offer sdp: rtcp-mux must be enabled when BUNDLE is " |
| 581 | "enabled.", |
| 582 | error); |
| 583 | } |
| 584 | |
| 585 | // Test that candidates sent to the "video" transport do not get pushed down to |
| 586 | // the "audio" transport channel when bundling. |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 587 | TEST_P(PeerConnectionBundleTest, |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 588 | IgnoreCandidatesForUnusedTransportWhenBundling) { |
| 589 | const SocketAddress kAudioAddress1("1.1.1.1", 1111); |
| 590 | const SocketAddress kAudioAddress2("2.2.2.2", 2222); |
| 591 | const SocketAddress kVideoAddress("3.3.3.3", 3333); |
| 592 | const SocketAddress kCallerAddress("4.4.4.4", 0); |
| 593 | const SocketAddress kCalleeAddress("5.5.5.5", 0); |
| 594 | |
| 595 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 596 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 597 | |
| 598 | caller->network()->AddInterface(kCallerAddress); |
| 599 | callee->network()->AddInterface(kCalleeAddress); |
| 600 | |
| 601 | RTCOfferAnswerOptions options; |
| 602 | options.use_rtp_mux = true; |
| 603 | |
| 604 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 605 | ASSERT_TRUE( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 606 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(options))); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 607 | |
| 608 | // The way the *_WAIT checks work is they only wait if the condition fails, |
| 609 | // which does not help in the case where state is not changing. This is |
| 610 | // problematic in this test since we want to verify that adding a video |
| 611 | // candidate does _not_ change state. So we interleave candidates and assume |
| 612 | // that messages are executed in the order they were posted. |
| 613 | |
| 614 | cricket::Candidate audio_candidate1 = CreateLocalUdpCandidate(kAudioAddress1); |
| 615 | ASSERT_TRUE(caller->AddIceCandidateToMedia(&audio_candidate1, |
| 616 | cricket::MEDIA_TYPE_AUDIO)); |
| 617 | |
| 618 | cricket::Candidate video_candidate = CreateLocalUdpCandidate(kVideoAddress); |
| 619 | ASSERT_TRUE(caller->AddIceCandidateToMedia(&video_candidate, |
| 620 | cricket::MEDIA_TYPE_VIDEO)); |
| 621 | |
| 622 | cricket::Candidate audio_candidate2 = CreateLocalUdpCandidate(kAudioAddress2); |
| 623 | ASSERT_TRUE(caller->AddIceCandidateToMedia(&audio_candidate2, |
| 624 | cricket::MEDIA_TYPE_AUDIO)); |
| 625 | |
| 626 | EXPECT_TRUE_WAIT(caller->HasConnectionWithRemoteAddress(kAudioAddress1), |
| 627 | kDefaultTimeout); |
| 628 | EXPECT_TRUE_WAIT(caller->HasConnectionWithRemoteAddress(kAudioAddress2), |
| 629 | kDefaultTimeout); |
| 630 | EXPECT_FALSE(caller->HasConnectionWithRemoteAddress(kVideoAddress)); |
| 631 | } |
| 632 | |
| 633 | // Test that the transport used by both audio and video is the transport |
| 634 | // associated with the first MID in the answer BUNDLE group, even if it's in a |
| 635 | // different order from the offer. |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 636 | TEST_P(PeerConnectionBundleTest, BundleOnFirstMidInAnswer) { |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 637 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 638 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 639 | |
| 640 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 641 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 642 | auto* old_video_transport = caller->video_rtp_transport(); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 643 | |
| 644 | auto answer = callee->CreateAnswer(); |
| 645 | auto* old_bundle_group = |
| 646 | answer->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 647 | std::string first_mid = old_bundle_group->content_names()[0]; |
| 648 | std::string second_mid = old_bundle_group->content_names()[1]; |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 649 | answer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 650 | |
| 651 | cricket::ContentGroup new_bundle_group(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 652 | new_bundle_group.AddContentName(second_mid); |
| 653 | new_bundle_group.AddContentName(first_mid); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 654 | answer->description()->AddGroup(new_bundle_group); |
| 655 | |
| 656 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 657 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame^] | 658 | EXPECT_EQ(old_video_transport, caller->video_rtp_transport()); |
| 659 | EXPECT_EQ(caller->voice_rtp_transport(), caller->video_rtp_transport()); |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 662 | INSTANTIATE_TEST_CASE_P(PeerConnectionBundleTest, |
| 663 | PeerConnectionBundleTest, |
| 664 | Values(SdpSemantics::kPlanB, |
| 665 | SdpSemantics::kUnifiedPlan)); |
| 666 | |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 667 | } // namespace webrtc |