You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

890 lines
36 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include <memory>
  11. #include "api/audio_codecs/builtin_audio_decoder_factory.h"
  12. #include "api/audio_codecs/builtin_audio_encoder_factory.h"
  13. #include "api/create_peerconnection_factory.h"
  14. #include "api/peer_connection_proxy.h"
  15. #include "api/video_codecs/builtin_video_decoder_factory.h"
  16. #include "api/video_codecs/builtin_video_encoder_factory.h"
  17. #include "p2p/base/fake_port_allocator.h"
  18. #include "p2p/base/test_stun_server.h"
  19. #include "p2p/client/basic_port_allocator.h"
  20. #include "pc/media_session.h"
  21. #include "pc/peer_connection.h"
  22. #include "pc/peer_connection_wrapper.h"
  23. #include "pc/sdp_utils.h"
  24. #ifdef WEBRTC_ANDROID
  25. #include "pc/test/android_test_initializer.h"
  26. #endif
  27. #include "pc/test/fake_audio_capture_module.h"
  28. #include "rtc_base/fake_network.h"
  29. #include "rtc_base/gunit.h"
  30. #include "rtc_base/virtual_socket_server.h"
  31. #include "test/gmock.h"
  32. namespace webrtc {
  33. using BundlePolicy = PeerConnectionInterface::BundlePolicy;
  34. using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
  35. using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
  36. using RtcpMuxPolicy = PeerConnectionInterface::RtcpMuxPolicy;
  37. using rtc::SocketAddress;
  38. using ::testing::Combine;
  39. using ::testing::ElementsAre;
  40. using ::testing::UnorderedElementsAre;
  41. using ::testing::Values;
  42. constexpr int kDefaultTimeout = 10000;
  43. // TODO(steveanton): These tests should be rewritten to use the standard
  44. // RtpSenderInterface/DtlsTransportInterface objects once they're available in
  45. // the API. The RtpSender can be used to determine which transport a given media
  46. // will use: https://www.w3.org/TR/webrtc/#dom-rtcrtpsender-transport
  47. // Should also be able to remove GetTransceiversForTesting at that point.
  48. class FakeNetworkManagerWithNoAnyNetwork : public rtc::FakeNetworkManager {
  49. public:
  50. void GetAnyAddressNetworks(NetworkList* networks) override {
  51. // This function allocates networks that are owned by the
  52. // NetworkManager. But some tests assume that they can release
  53. // all networks independent of the network manager.
  54. // In order to prevent use-after-free issues, don't allow this
  55. // function to have any effect when run in tests.
  56. RTC_LOG(LS_INFO) << "FakeNetworkManager::GetAnyAddressNetworks ignored";
  57. }
  58. };
  59. class PeerConnectionWrapperForBundleTest : public PeerConnectionWrapper {
  60. public:
  61. using PeerConnectionWrapper::PeerConnectionWrapper;
  62. bool AddIceCandidateToMedia(cricket::Candidate* candidate,
  63. cricket::MediaType media_type) {
  64. auto* desc = pc()->remote_description()->description();
  65. for (size_t i = 0; i < desc->contents().size(); i++) {
  66. const auto& content = desc->contents()[i];
  67. if (content.media_description()->type() == media_type) {
  68. candidate->set_transport_name(content.name);
  69. std::unique_ptr<IceCandidateInterface> jsep_candidate =
  70. CreateIceCandidate(content.name, i, *candidate);
  71. return pc()->AddIceCandidate(jsep_candidate.get());
  72. }
  73. }
  74. RTC_NOTREACHED();
  75. return false;
  76. }
  77. RtpTransportInternal* voice_rtp_transport() {
  78. return (voice_channel() ? voice_channel()->rtp_transport() : nullptr);
  79. }
  80. cricket::VoiceChannel* voice_channel() {
  81. auto transceivers = GetInternalPeerConnection()->GetTransceiversInternal();
  82. for (const auto& transceiver : transceivers) {
  83. if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
  84. return static_cast<cricket::VoiceChannel*>(
  85. transceiver->internal()->channel());
  86. }
  87. }
  88. return nullptr;
  89. }
  90. RtpTransportInternal* video_rtp_transport() {
  91. return (video_channel() ? video_channel()->rtp_transport() : nullptr);
  92. }
  93. cricket::VideoChannel* video_channel() {
  94. auto transceivers = GetInternalPeerConnection()->GetTransceiversInternal();
  95. for (const auto& transceiver : transceivers) {
  96. if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
  97. return static_cast<cricket::VideoChannel*>(
  98. transceiver->internal()->channel());
  99. }
  100. }
  101. return nullptr;
  102. }
  103. PeerConnection* GetInternalPeerConnection() {
  104. auto* pci =
  105. static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
  106. pc());
  107. return static_cast<PeerConnection*>(pci->internal());
  108. }
  109. // Returns true if the stats indicate that an ICE connection is either in
  110. // progress or established with the given remote address.
  111. bool HasConnectionWithRemoteAddress(const SocketAddress& address) {
  112. auto report = GetStats();
  113. if (!report) {
  114. return false;
  115. }
  116. std::string matching_candidate_id;
  117. for (auto* ice_candidate_stats :
  118. report->GetStatsOfType<RTCRemoteIceCandidateStats>()) {
  119. if (*ice_candidate_stats->ip == address.HostAsURIString() &&
  120. *ice_candidate_stats->port == address.port()) {
  121. matching_candidate_id = ice_candidate_stats->id();
  122. break;
  123. }
  124. }
  125. if (matching_candidate_id.empty()) {
  126. return false;
  127. }
  128. for (auto* pair_stats :
  129. report->GetStatsOfType<RTCIceCandidatePairStats>()) {
  130. if (*pair_stats->remote_candidate_id == matching_candidate_id) {
  131. if (*pair_stats->state == RTCStatsIceCandidatePairState::kInProgress ||
  132. *pair_stats->state == RTCStatsIceCandidatePairState::kSucceeded) {
  133. return true;
  134. }
  135. }
  136. }
  137. return false;
  138. }
  139. rtc::FakeNetworkManager* network() { return network_; }
  140. void set_network(rtc::FakeNetworkManager* network) { network_ = network; }
  141. private:
  142. rtc::FakeNetworkManager* network_;
  143. };
  144. class PeerConnectionBundleBaseTest : public ::testing::Test {
  145. protected:
  146. typedef std::unique_ptr<PeerConnectionWrapperForBundleTest> WrapperPtr;
  147. explicit PeerConnectionBundleBaseTest(SdpSemantics sdp_semantics)
  148. : vss_(new rtc::VirtualSocketServer()),
  149. main_(vss_.get()),
  150. sdp_semantics_(sdp_semantics) {
  151. #ifdef WEBRTC_ANDROID
  152. InitializeAndroidObjects();
  153. #endif
  154. pc_factory_ = CreatePeerConnectionFactory(
  155. rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
  156. rtc::scoped_refptr<AudioDeviceModule>(FakeAudioCaptureModule::Create()),
  157. CreateBuiltinAudioEncoderFactory(), CreateBuiltinAudioDecoderFactory(),
  158. CreateBuiltinVideoEncoderFactory(), CreateBuiltinVideoDecoderFactory(),
  159. nullptr /* audio_mixer */, nullptr /* audio_processing */);
  160. }
  161. WrapperPtr CreatePeerConnection() {
  162. return CreatePeerConnection(RTCConfiguration());
  163. }
  164. WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
  165. auto* fake_network = NewFakeNetwork();
  166. auto port_allocator =
  167. std::make_unique<cricket::BasicPortAllocator>(fake_network);
  168. port_allocator->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
  169. cricket::PORTALLOCATOR_DISABLE_RELAY);
  170. port_allocator->set_step_delay(cricket::kMinimumStepDelay);
  171. auto observer = std::make_unique<MockPeerConnectionObserver>();
  172. RTCConfiguration modified_config = config;
  173. modified_config.sdp_semantics = sdp_semantics_;
  174. auto pc = pc_factory_->CreatePeerConnection(
  175. modified_config, std::move(port_allocator), nullptr, observer.get());
  176. if (!pc) {
  177. return nullptr;
  178. }
  179. auto wrapper = std::make_unique<PeerConnectionWrapperForBundleTest>(
  180. pc_factory_, pc, std::move(observer));
  181. wrapper->set_network(fake_network);
  182. return wrapper;
  183. }
  184. // Accepts the same arguments as CreatePeerConnection and adds default audio
  185. // and video tracks.
  186. template <typename... Args>
  187. WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) {
  188. auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
  189. if (!wrapper) {
  190. return nullptr;
  191. }
  192. wrapper->AddAudioTrack("a");
  193. wrapper->AddVideoTrack("v");
  194. return wrapper;
  195. }
  196. cricket::Candidate CreateLocalUdpCandidate(
  197. const rtc::SocketAddress& address) {
  198. cricket::Candidate candidate;
  199. candidate.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
  200. candidate.set_protocol(cricket::UDP_PROTOCOL_NAME);
  201. candidate.set_address(address);
  202. candidate.set_type(cricket::LOCAL_PORT_TYPE);
  203. return candidate;
  204. }
  205. rtc::FakeNetworkManager* NewFakeNetwork() {
  206. // The PeerConnection's port allocator is tied to the PeerConnection's
  207. // lifetime and expects the underlying NetworkManager to outlive it. If
  208. // PeerConnectionWrapper owned the NetworkManager, it would be destroyed
  209. // before the PeerConnection (since subclass members are destroyed before
  210. // base class members). Therefore, the test fixture will own all the fake
  211. // networks even though tests should access the fake network through the
  212. // PeerConnectionWrapper.
  213. auto* fake_network = new FakeNetworkManagerWithNoAnyNetwork();
  214. fake_networks_.emplace_back(fake_network);
  215. return fake_network;
  216. }
  217. std::unique_ptr<rtc::VirtualSocketServer> vss_;
  218. rtc::AutoSocketServerThread main_;
  219. rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
  220. std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_;
  221. const SdpSemantics sdp_semantics_;
  222. };
  223. class PeerConnectionBundleTest
  224. : public PeerConnectionBundleBaseTest,
  225. public ::testing::WithParamInterface<SdpSemantics> {
  226. protected:
  227. PeerConnectionBundleTest() : PeerConnectionBundleBaseTest(GetParam()) {}
  228. };
  229. class PeerConnectionBundleTestUnifiedPlan
  230. : public PeerConnectionBundleBaseTest {
  231. protected:
  232. PeerConnectionBundleTestUnifiedPlan()
  233. : PeerConnectionBundleBaseTest(SdpSemantics::kUnifiedPlan) {}
  234. };
  235. SdpContentMutator RemoveRtcpMux() {
  236. return [](cricket::ContentInfo* content, cricket::TransportInfo* transport) {
  237. content->media_description()->set_rtcp_mux(false);
  238. };
  239. }
  240. std::vector<int> GetCandidateComponents(
  241. const std::vector<IceCandidateInterface*> candidates) {
  242. std::vector<int> components;
  243. components.reserve(candidates.size());
  244. for (auto* candidate : candidates) {
  245. components.push_back(candidate->candidate().component());
  246. }
  247. return components;
  248. }
  249. // Test that there are 2 local UDP candidates (1 RTP and 1 RTCP candidate) for
  250. // each media section when disabling bundling and disabling RTCP multiplexing.
  251. TEST_P(PeerConnectionBundleTest,
  252. TwoCandidatesForEachTransportWhenNoBundleNoRtcpMux) {
  253. const SocketAddress kCallerAddress("1.1.1.1", 0);
  254. const SocketAddress kCalleeAddress("2.2.2.2", 0);
  255. RTCConfiguration config;
  256. config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
  257. auto caller = CreatePeerConnectionWithAudioVideo(config);
  258. caller->network()->AddInterface(kCallerAddress);
  259. auto callee = CreatePeerConnectionWithAudioVideo(config);
  260. callee->network()->AddInterface(kCalleeAddress);
  261. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  262. RTCOfferAnswerOptions options_no_bundle;
  263. options_no_bundle.use_rtp_mux = false;
  264. auto answer = callee->CreateAnswer(options_no_bundle);
  265. SdpContentsForEach(RemoveRtcpMux(), answer->description());
  266. ASSERT_TRUE(
  267. callee->SetLocalDescription(CloneSessionDescription(answer.get())));
  268. ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
  269. // Check that caller has separate RTP and RTCP candidates for each media.
  270. EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kDefaultTimeout);
  271. EXPECT_THAT(
  272. GetCandidateComponents(caller->observer()->GetCandidatesByMline(0)),
  273. UnorderedElementsAre(cricket::ICE_CANDIDATE_COMPONENT_RTP,
  274. cricket::ICE_CANDIDATE_COMPONENT_RTCP));
  275. EXPECT_THAT(
  276. GetCandidateComponents(caller->observer()->GetCandidatesByMline(1)),
  277. UnorderedElementsAre(cricket::ICE_CANDIDATE_COMPONENT_RTP,
  278. cricket::ICE_CANDIDATE_COMPONENT_RTCP));
  279. // Check that callee has separate RTP and RTCP candidates for each media.
  280. EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kDefaultTimeout);
  281. EXPECT_THAT(
  282. GetCandidateComponents(callee->observer()->GetCandidatesByMline(0)),
  283. UnorderedElementsAre(cricket::ICE_CANDIDATE_COMPONENT_RTP,
  284. cricket::ICE_CANDIDATE_COMPONENT_RTCP));
  285. EXPECT_THAT(
  286. GetCandidateComponents(callee->observer()->GetCandidatesByMline(1)),
  287. UnorderedElementsAre(cricket::ICE_CANDIDATE_COMPONENT_RTP,
  288. cricket::ICE_CANDIDATE_COMPONENT_RTCP));
  289. }
  290. // Test that there is 1 local UDP candidate for both RTP and RTCP for each media
  291. // section when disabling bundle but enabling RTCP multiplexing.
  292. TEST_P(PeerConnectionBundleTest,
  293. OneCandidateForEachTransportWhenNoBundleButRtcpMux) {
  294. const SocketAddress kCallerAddress("1.1.1.1", 0);
  295. auto caller = CreatePeerConnectionWithAudioVideo();
  296. caller->network()->AddInterface(kCallerAddress);
  297. auto callee = CreatePeerConnectionWithAudioVideo();
  298. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  299. RTCOfferAnswerOptions options_no_bundle;
  300. options_no_bundle.use_rtp_mux = false;
  301. ASSERT_TRUE(
  302. caller->SetRemoteDescription(callee->CreateAnswer(options_no_bundle)));
  303. EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kDefaultTimeout);
  304. EXPECT_EQ(1u, caller->observer()->GetCandidatesByMline(0).size());
  305. EXPECT_EQ(1u, caller->observer()->GetCandidatesByMline(1).size());
  306. }
  307. // Test that there is 1 local UDP candidate in only the first media section when
  308. // bundling and enabling RTCP multiplexing.
  309. TEST_P(PeerConnectionBundleTest,
  310. OneCandidateOnlyOnFirstTransportWhenBundleAndRtcpMux) {
  311. const SocketAddress kCallerAddress("1.1.1.1", 0);
  312. RTCConfiguration config;
  313. config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle;
  314. auto caller = CreatePeerConnectionWithAudioVideo(config);
  315. caller->network()->AddInterface(kCallerAddress);
  316. auto callee = CreatePeerConnectionWithAudioVideo(config);
  317. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  318. ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateAnswer()));
  319. EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kDefaultTimeout);
  320. EXPECT_EQ(1u, caller->observer()->GetCandidatesByMline(0).size());
  321. EXPECT_EQ(0u, caller->observer()->GetCandidatesByMline(1).size());
  322. }
  323. // It will fail if the offerer uses the mux-BUNDLE policy but the answerer
  324. // doesn't support BUNDLE.
  325. TEST_P(PeerConnectionBundleTest, MaxBundleNotSupportedInAnswer) {
  326. RTCConfiguration config;
  327. config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle;
  328. auto caller = CreatePeerConnectionWithAudioVideo(config);
  329. auto callee = CreatePeerConnectionWithAudioVideo();
  330. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  331. bool equal_before =
  332. (caller->voice_rtp_transport() == caller->video_rtp_transport());
  333. EXPECT_EQ(true, equal_before);
  334. RTCOfferAnswerOptions options;
  335. options.use_rtp_mux = false;
  336. EXPECT_FALSE(
  337. caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(options)));
  338. }
  339. // The following parameterized test verifies that an offer/answer with varying
  340. // bundle policies and either bundle in the answer or not will produce the
  341. // expected RTP transports for audio and video. In particular, for bundling we
  342. // care about whether they are separate transports or the same.
  343. enum class BundleIncluded { kBundleInAnswer, kBundleNotInAnswer };
  344. std::ostream& operator<<(std::ostream& out, BundleIncluded value) {
  345. switch (value) {
  346. case BundleIncluded::kBundleInAnswer:
  347. return out << "bundle in answer";
  348. case BundleIncluded::kBundleNotInAnswer:
  349. return out << "bundle not in answer";
  350. }
  351. return out << "unknown";
  352. }
  353. class PeerConnectionBundleMatrixTest
  354. : public PeerConnectionBundleBaseTest,
  355. public ::testing::WithParamInterface<
  356. std::tuple<SdpSemantics,
  357. std::tuple<BundlePolicy, BundleIncluded, bool, bool>>> {
  358. protected:
  359. PeerConnectionBundleMatrixTest()
  360. : PeerConnectionBundleBaseTest(std::get<0>(GetParam())) {
  361. auto param = std::get<1>(GetParam());
  362. bundle_policy_ = std::get<0>(param);
  363. bundle_included_ = std::get<1>(param);
  364. expected_same_before_ = std::get<2>(param);
  365. expected_same_after_ = std::get<3>(param);
  366. }
  367. PeerConnectionInterface::BundlePolicy bundle_policy_;
  368. BundleIncluded bundle_included_;
  369. bool expected_same_before_;
  370. bool expected_same_after_;
  371. };
  372. TEST_P(PeerConnectionBundleMatrixTest,
  373. VerifyTransportsBeforeAndAfterSettingRemoteAnswer) {
  374. RTCConfiguration config;
  375. config.bundle_policy = bundle_policy_;
  376. auto caller = CreatePeerConnectionWithAudioVideo(config);
  377. auto callee = CreatePeerConnectionWithAudioVideo();
  378. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  379. bool equal_before =
  380. (caller->voice_rtp_transport() == caller->video_rtp_transport());
  381. EXPECT_EQ(expected_same_before_, equal_before);
  382. RTCOfferAnswerOptions options;
  383. options.use_rtp_mux = (bundle_included_ == BundleIncluded::kBundleInAnswer);
  384. ASSERT_TRUE(
  385. caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(options)));
  386. bool equal_after =
  387. (caller->voice_rtp_transport() == caller->video_rtp_transport());
  388. EXPECT_EQ(expected_same_after_, equal_after);
  389. }
  390. // The max-bundle policy means we should anticipate bundling being negotiated,
  391. // and multiplex audio/video from the start.
  392. // For all other policies, bundling should only be enabled if negotiated by the
  393. // answer.
  394. INSTANTIATE_TEST_SUITE_P(
  395. PeerConnectionBundleTest,
  396. PeerConnectionBundleMatrixTest,
  397. Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
  398. Values(std::make_tuple(BundlePolicy::kBundlePolicyBalanced,
  399. BundleIncluded::kBundleInAnswer,
  400. false,
  401. true),
  402. std::make_tuple(BundlePolicy::kBundlePolicyBalanced,
  403. BundleIncluded::kBundleNotInAnswer,
  404. false,
  405. false),
  406. std::make_tuple(BundlePolicy::kBundlePolicyMaxBundle,
  407. BundleIncluded::kBundleInAnswer,
  408. true,
  409. true),
  410. std::make_tuple(BundlePolicy::kBundlePolicyMaxCompat,
  411. BundleIncluded::kBundleInAnswer,
  412. false,
  413. true),
  414. std::make_tuple(BundlePolicy::kBundlePolicyMaxCompat,
  415. BundleIncluded::kBundleNotInAnswer,
  416. false,
  417. false))));
  418. // Test that the audio/video transports on the callee side are the same before
  419. // and after setting a local answer when max BUNDLE is enabled and an offer with
  420. // BUNDLE is received.
  421. TEST_P(PeerConnectionBundleTest,
  422. TransportsSameForMaxBundleWithBundleInRemoteOffer) {
  423. auto caller = CreatePeerConnectionWithAudioVideo();
  424. RTCConfiguration config;
  425. config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle;
  426. auto callee = CreatePeerConnectionWithAudioVideo(config);
  427. RTCOfferAnswerOptions options_with_bundle;
  428. options_with_bundle.use_rtp_mux = true;
  429. ASSERT_TRUE(callee->SetRemoteDescription(
  430. caller->CreateOfferAndSetAsLocal(options_with_bundle)));
  431. EXPECT_EQ(callee->voice_rtp_transport(), callee->video_rtp_transport());
  432. ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
  433. EXPECT_EQ(callee->voice_rtp_transport(), callee->video_rtp_transport());
  434. }
  435. TEST_P(PeerConnectionBundleTest,
  436. FailToSetRemoteOfferWithNoBundleWhenBundlePolicyMaxBundle) {
  437. auto caller = CreatePeerConnectionWithAudioVideo();
  438. RTCConfiguration config;
  439. config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle;
  440. auto callee = CreatePeerConnectionWithAudioVideo(config);
  441. RTCOfferAnswerOptions options_no_bundle;
  442. options_no_bundle.use_rtp_mux = false;
  443. EXPECT_FALSE(callee->SetRemoteDescription(
  444. caller->CreateOfferAndSetAsLocal(options_no_bundle)));
  445. }
  446. // Test that if the media section which has the bundled transport is rejected,
  447. // then the peers still connect and the bundled transport switches to the other
  448. // media section.
  449. // Note: This is currently failing because of the following bug:
  450. // https://bugs.chromium.org/p/webrtc/issues/detail?id=6280
  451. TEST_P(PeerConnectionBundleTest,
  452. DISABLED_SuccessfullyNegotiateMaxBundleIfBundleTransportMediaRejected) {
  453. RTCConfiguration config;
  454. config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle;
  455. auto caller = CreatePeerConnectionWithAudioVideo(config);
  456. auto callee = CreatePeerConnection();
  457. callee->AddVideoTrack("v");
  458. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  459. RTCOfferAnswerOptions options;
  460. options.offer_to_receive_audio = 0;
  461. ASSERT_TRUE(
  462. caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(options)));
  463. EXPECT_FALSE(caller->voice_rtp_transport());
  464. EXPECT_TRUE(caller->video_rtp_transport());
  465. }
  466. // When requiring RTCP multiplexing, the PeerConnection never makes RTCP
  467. // transport channels.
  468. TEST_P(PeerConnectionBundleTest, NeverCreateRtcpTransportWithRtcpMuxRequired) {
  469. RTCConfiguration config;
  470. config.rtcp_mux_policy = RtcpMuxPolicy::kRtcpMuxPolicyRequire;
  471. auto caller = CreatePeerConnectionWithAudioVideo(config);
  472. auto callee = CreatePeerConnectionWithAudioVideo();
  473. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  474. EXPECT_FALSE(caller->voice_rtp_transport()->rtcp_mux_enabled());
  475. EXPECT_FALSE(caller->video_rtp_transport()->rtcp_mux_enabled());
  476. ASSERT_TRUE(
  477. caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
  478. EXPECT_TRUE(caller->voice_rtp_transport()->rtcp_mux_enabled());
  479. EXPECT_TRUE(caller->video_rtp_transport()->rtcp_mux_enabled());
  480. }
  481. // When negotiating RTCP multiplexing, the PeerConnection makes RTCP transports
  482. // when the offer is sent, but will destroy them once the remote answer is set.
  483. TEST_P(PeerConnectionBundleTest,
  484. CreateRtcpTransportOnlyBeforeAnswerWithRtcpMuxNegotiate) {
  485. RTCConfiguration config;
  486. config.rtcp_mux_policy = RtcpMuxPolicy::kRtcpMuxPolicyNegotiate;
  487. auto caller = CreatePeerConnectionWithAudioVideo(config);
  488. auto callee = CreatePeerConnectionWithAudioVideo();
  489. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  490. EXPECT_FALSE(caller->voice_rtp_transport()->rtcp_mux_enabled());
  491. EXPECT_FALSE(caller->video_rtp_transport()->rtcp_mux_enabled());
  492. ASSERT_TRUE(
  493. caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
  494. EXPECT_TRUE(caller->voice_rtp_transport()->rtcp_mux_enabled());
  495. EXPECT_TRUE(caller->video_rtp_transport()->rtcp_mux_enabled());
  496. }
  497. TEST_P(PeerConnectionBundleTest, FailToSetDescriptionWithBundleAndNoRtcpMux) {
  498. auto caller = CreatePeerConnectionWithAudioVideo();
  499. auto callee = CreatePeerConnectionWithAudioVideo();
  500. RTCOfferAnswerOptions options;
  501. options.use_rtp_mux = true;
  502. auto offer = caller->CreateOffer(options);
  503. SdpContentsForEach(RemoveRtcpMux(), offer->description());
  504. std::string error;
  505. EXPECT_FALSE(caller->SetLocalDescription(CloneSessionDescription(offer.get()),
  506. &error));
  507. EXPECT_EQ(
  508. "Failed to set local offer sdp: rtcp-mux must be enabled when BUNDLE is "
  509. "enabled.",
  510. error);
  511. EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer), &error));
  512. EXPECT_EQ(
  513. "Failed to set remote offer sdp: rtcp-mux must be enabled when BUNDLE is "
  514. "enabled.",
  515. error);
  516. }
  517. // Test that candidates sent to the "video" transport do not get pushed down to
  518. // the "audio" transport channel when bundling.
  519. TEST_P(PeerConnectionBundleTest,
  520. IgnoreCandidatesForUnusedTransportWhenBundling) {
  521. const SocketAddress kAudioAddress1("1.1.1.1", 1111);
  522. const SocketAddress kAudioAddress2("2.2.2.2", 2222);
  523. const SocketAddress kVideoAddress("3.3.3.3", 3333);
  524. const SocketAddress kCallerAddress("4.4.4.4", 0);
  525. const SocketAddress kCalleeAddress("5.5.5.5", 0);
  526. auto caller = CreatePeerConnectionWithAudioVideo();
  527. auto callee = CreatePeerConnectionWithAudioVideo();
  528. caller->network()->AddInterface(kCallerAddress);
  529. callee->network()->AddInterface(kCalleeAddress);
  530. RTCOfferAnswerOptions options;
  531. options.use_rtp_mux = true;
  532. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  533. ASSERT_TRUE(
  534. caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal(options)));
  535. // The way the *_WAIT checks work is they only wait if the condition fails,
  536. // which does not help in the case where state is not changing. This is
  537. // problematic in this test since we want to verify that adding a video
  538. // candidate does _not_ change state. So we interleave candidates and assume
  539. // that messages are executed in the order they were posted.
  540. cricket::Candidate audio_candidate1 = CreateLocalUdpCandidate(kAudioAddress1);
  541. ASSERT_TRUE(caller->AddIceCandidateToMedia(&audio_candidate1,
  542. cricket::MEDIA_TYPE_AUDIO));
  543. cricket::Candidate video_candidate = CreateLocalUdpCandidate(kVideoAddress);
  544. ASSERT_TRUE(caller->AddIceCandidateToMedia(&video_candidate,
  545. cricket::MEDIA_TYPE_VIDEO));
  546. cricket::Candidate audio_candidate2 = CreateLocalUdpCandidate(kAudioAddress2);
  547. ASSERT_TRUE(caller->AddIceCandidateToMedia(&audio_candidate2,
  548. cricket::MEDIA_TYPE_AUDIO));
  549. EXPECT_TRUE_WAIT(caller->HasConnectionWithRemoteAddress(kAudioAddress1),
  550. kDefaultTimeout);
  551. EXPECT_TRUE_WAIT(caller->HasConnectionWithRemoteAddress(kAudioAddress2),
  552. kDefaultTimeout);
  553. EXPECT_FALSE(caller->HasConnectionWithRemoteAddress(kVideoAddress));
  554. }
  555. // Test that the transport used by both audio and video is the transport
  556. // associated with the first MID in the answer BUNDLE group, even if it's in a
  557. // different order from the offer.
  558. TEST_P(PeerConnectionBundleTest, BundleOnFirstMidInAnswer) {
  559. auto caller = CreatePeerConnectionWithAudioVideo();
  560. auto callee = CreatePeerConnectionWithAudioVideo();
  561. ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  562. auto* old_video_transport = caller->video_rtp_transport();
  563. auto answer = callee->CreateAnswer();
  564. auto* old_bundle_group =
  565. answer->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
  566. std::string first_mid = old_bundle_group->content_names()[0];
  567. std::string second_mid = old_bundle_group->content_names()[1];
  568. answer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
  569. cricket::ContentGroup new_bundle_group(cricket::GROUP_TYPE_BUNDLE);
  570. new_bundle_group.AddContentName(second_mid);
  571. new_bundle_group.AddContentName(first_mid);
  572. answer->description()->AddGroup(new_bundle_group);
  573. ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
  574. EXPECT_EQ(old_video_transport, caller->video_rtp_transport());
  575. EXPECT_EQ(caller->voice_rtp_transport(), caller->video_rtp_transport());
  576. }
  577. // This tests that applying description with conflicted RTP demuxing criteria
  578. // will fail.
  579. TEST_P(PeerConnectionBundleTest,
  580. ApplyDescriptionWithConflictedDemuxCriteriaFail) {
  581. auto caller = CreatePeerConnectionWithAudioVideo();
  582. auto callee = CreatePeerConnectionWithAudioVideo();
  583. RTCOfferAnswerOptions options;
  584. options.use_rtp_mux = false;
  585. auto offer = caller->CreateOffer(options);
  586. // Modified the SDP to make two m= sections have the same SSRC.
  587. ASSERT_GE(offer->description()->contents().size(), 2U);
  588. offer->description()
  589. ->contents()[0]
  590. .media_description()
  591. ->mutable_streams()[0]
  592. .ssrcs[0] = 1111222;
  593. offer->description()
  594. ->contents()[1]
  595. .media_description()
  596. ->mutable_streams()[0]
  597. .ssrcs[0] = 1111222;
  598. EXPECT_TRUE(
  599. caller->SetLocalDescription(CloneSessionDescription(offer.get())));
  600. EXPECT_TRUE(callee->SetRemoteDescription(std::move(offer)));
  601. EXPECT_TRUE(callee->CreateAnswerAndSetAsLocal(options));
  602. // Enable BUNDLE in subsequent offer/answer exchange and two m= sections are
  603. // expectd to use one RtpTransport underneath.
  604. options.use_rtp_mux = true;
  605. EXPECT_TRUE(
  606. callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
  607. auto answer = callee->CreateAnswer(options);
  608. // When BUNDLE is enabled, applying the description is expected to fail
  609. // because the demuxing criteria is conflicted.
  610. EXPECT_FALSE(callee->SetLocalDescription(std::move(answer)));
  611. }
  612. // This tests that changing the pre-negotiated BUNDLE tag is not supported.
  613. TEST_P(PeerConnectionBundleTest, RejectDescriptionChangingBundleTag) {
  614. RTCConfiguration config;
  615. config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle;
  616. auto caller = CreatePeerConnectionWithAudioVideo(config);
  617. auto callee = CreatePeerConnectionWithAudioVideo(config);
  618. RTCOfferAnswerOptions options;
  619. options.use_rtp_mux = true;
  620. auto offer = caller->CreateOfferAndSetAsLocal(options);
  621. // Create a new bundle-group with different bundled_mid.
  622. auto* old_bundle_group =
  623. offer->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
  624. std::string first_mid = old_bundle_group->content_names()[0];
  625. std::string second_mid = old_bundle_group->content_names()[1];
  626. cricket::ContentGroup new_bundle_group(cricket::GROUP_TYPE_BUNDLE);
  627. new_bundle_group.AddContentName(second_mid);
  628. auto re_offer = CloneSessionDescription(offer.get());
  629. callee->SetRemoteDescription(std::move(offer));
  630. auto answer = callee->CreateAnswer(options);
  631. // Reject the first MID.
  632. answer->description()->contents()[0].rejected = true;
  633. // Remove the first MID from the bundle group.
  634. answer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
  635. answer->description()->AddGroup(new_bundle_group);
  636. // The answer is expected to be rejected.
  637. EXPECT_FALSE(caller->SetRemoteDescription(std::move(answer)));
  638. // Do the same thing for re-offer.
  639. re_offer->description()->contents()[0].rejected = true;
  640. re_offer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
  641. re_offer->description()->AddGroup(new_bundle_group);
  642. // The re-offer is expected to be rejected.
  643. EXPECT_FALSE(caller->SetLocalDescription(std::move(re_offer)));
  644. }
  645. // This tests that removing contents from BUNDLE group and reject the whole
  646. // BUNDLE group could work. This is a regression test for
  647. // (https://bugs.chromium.org/p/chromium/issues/detail?id=827917)
  648. TEST_P(PeerConnectionBundleTest, RemovingContentAndRejectBundleGroup) {
  649. RTCConfiguration config;
  650. #ifndef HAVE_SCTP
  651. config.enable_rtp_data_channel = true;
  652. #endif
  653. config.bundle_policy = BundlePolicy::kBundlePolicyMaxBundle;
  654. auto caller = CreatePeerConnectionWithAudioVideo(config);
  655. caller->CreateDataChannel("dc");
  656. auto offer = caller->CreateOfferAndSetAsLocal();
  657. auto re_offer = CloneSessionDescription(offer.get());
  658. // Removing the second MID from the BUNDLE group.
  659. auto* old_bundle_group =
  660. offer->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
  661. std::string first_mid = old_bundle_group->content_names()[0];
  662. std::string third_mid = old_bundle_group->content_names()[2];
  663. cricket::ContentGroup new_bundle_group(cricket::GROUP_TYPE_BUNDLE);
  664. new_bundle_group.AddContentName(first_mid);
  665. new_bundle_group.AddContentName(third_mid);
  666. // Reject the entire new bundle group.
  667. re_offer->description()->contents()[0].rejected = true;
  668. re_offer->description()->contents()[2].rejected = true;
  669. re_offer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
  670. re_offer->description()->AddGroup(new_bundle_group);
  671. EXPECT_TRUE(caller->SetLocalDescription(std::move(re_offer)));
  672. }
  673. // This tests that the BUNDLE group in answer should be a subset of the offered
  674. // group.
  675. TEST_P(PeerConnectionBundleTest, AddContentToBundleGroupInAnswerNotSupported) {
  676. auto caller = CreatePeerConnectionWithAudioVideo();
  677. auto callee = CreatePeerConnectionWithAudioVideo();
  678. auto offer = caller->CreateOffer();
  679. std::string first_mid = offer->description()->contents()[0].name;
  680. std::string second_mid = offer->description()->contents()[1].name;
  681. cricket::ContentGroup bundle_group(cricket::GROUP_TYPE_BUNDLE);
  682. bundle_group.AddContentName(first_mid);
  683. offer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
  684. offer->description()->AddGroup(bundle_group);
  685. EXPECT_TRUE(
  686. caller->SetLocalDescription(CloneSessionDescription(offer.get())));
  687. EXPECT_TRUE(callee->SetRemoteDescription(std::move(offer)));
  688. auto answer = callee->CreateAnswer();
  689. bundle_group.AddContentName(second_mid);
  690. answer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
  691. answer->description()->AddGroup(bundle_group);
  692. // The answer is expected to be rejected because second mid is not in the
  693. // offered BUNDLE group.
  694. EXPECT_FALSE(callee->SetLocalDescription(std::move(answer)));
  695. }
  696. // This tests that the BUNDLE group with non-existing MID should be rejectd.
  697. TEST_P(PeerConnectionBundleTest, RejectBundleGroupWithNonExistingMid) {
  698. auto caller = CreatePeerConnectionWithAudioVideo();
  699. auto callee = CreatePeerConnectionWithAudioVideo();
  700. auto offer = caller->CreateOffer();
  701. auto invalid_bundle_group =
  702. *offer->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
  703. invalid_bundle_group.AddContentName("non-existing-MID");
  704. offer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
  705. offer->description()->AddGroup(invalid_bundle_group);
  706. EXPECT_FALSE(
  707. caller->SetLocalDescription(CloneSessionDescription(offer.get())));
  708. EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer)));
  709. }
  710. // This tests that an answer shouldn't be able to remove an m= section from an
  711. // established group without rejecting it.
  712. TEST_P(PeerConnectionBundleTest, RemoveContentFromBundleGroup) {
  713. auto caller = CreatePeerConnectionWithAudioVideo();
  714. auto callee = CreatePeerConnectionWithAudioVideo();
  715. EXPECT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  716. EXPECT_TRUE(
  717. caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
  718. EXPECT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  719. auto answer = callee->CreateAnswer();
  720. std::string second_mid = answer->description()->contents()[1].name;
  721. auto invalid_bundle_group =
  722. *answer->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
  723. invalid_bundle_group.RemoveContentName(second_mid);
  724. answer->description()->RemoveGroupByName(cricket::GROUP_TYPE_BUNDLE);
  725. answer->description()->AddGroup(invalid_bundle_group);
  726. EXPECT_FALSE(
  727. callee->SetLocalDescription(CloneSessionDescription(answer.get())));
  728. }
  729. INSTANTIATE_TEST_SUITE_P(PeerConnectionBundleTest,
  730. PeerConnectionBundleTest,
  731. Values(SdpSemantics::kPlanB,
  732. SdpSemantics::kUnifiedPlan));
  733. // According to RFC5888, if an endpoint understands the semantics of an
  734. // "a=group", it MUST return an answer with that group. So, an empty BUNDLE
  735. // group is valid when the answerer rejects all m= sections (by stopping all
  736. // transceivers), meaning there's nothing to bundle.
  737. //
  738. // Only writing this test for Unified Plan mode, since there's no way to reject
  739. // m= sections in answers for Plan B without SDP munging.
  740. TEST_F(PeerConnectionBundleTestUnifiedPlan,
  741. EmptyBundleGroupCreatedInAnswerWhenAppropriate) {
  742. auto caller = CreatePeerConnectionWithAudioVideo();
  743. auto callee = CreatePeerConnection();
  744. EXPECT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
  745. // Stop all transceivers, causing all m= sections to be rejected.
  746. for (const auto& transceiver : callee->pc()->GetTransceivers()) {
  747. transceiver->Stop();
  748. }
  749. EXPECT_TRUE(
  750. caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
  751. // Verify that the answer actually contained an empty bundle group.
  752. const SessionDescriptionInterface* desc = callee->pc()->local_description();
  753. ASSERT_NE(nullptr, desc);
  754. const cricket::ContentGroup* bundle_group =
  755. desc->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
  756. ASSERT_NE(nullptr, bundle_group);
  757. EXPECT_TRUE(bundle_group->content_names().empty());
  758. }
  759. } // namespace webrtc