From 7fca68d5430e55c3099f0274276f5c8e8106efe2 Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Tue, 29 Oct 2024 02:55:26 -0700 Subject: [PATCH] Add WEB_RTC_NON_CELLULAR enum, VI PiperOrigin-RevId: 690941154 --- connections/implementation/mediums/webrtc.cc | 38 +++++++++---------- connections/implementation/mediums/webrtc.h | 2 +- .../implementation/mediums/webrtc/BUILD | 1 - .../mediums/webrtc/connection_flow.cc | 10 +++++ .../mediums/webrtc/connection_flow.h | 12 ++---- .../mediums/webrtc/connection_flow_test.cc | 22 ++++++----- .../mediums/webrtc/webrtc_socket_impl.h | 1 - internal/platform/webrtc.h | 7 ++-- 8 files changed, 50 insertions(+), 43 deletions(-) diff --git a/connections/implementation/mediums/webrtc.cc b/connections/implementation/mediums/webrtc.cc index 80b6bd27..c0d5b5ee 100644 --- a/connections/implementation/mediums/webrtc.cc +++ b/connections/implementation/mediums/webrtc.cc @@ -35,8 +35,8 @@ #include "internal/platform/cancellation_flag.h" #include "internal/platform/cancellation_flag_listener.h" #include "internal/platform/exception.h" -#include "internal/platform/future.h" #include "internal/platform/feature_flags.h" +#include "internal/platform/future.h" #include "internal/platform/logging.h" #include "internal/platform/mutex_lock.h" #include "internal/platform/webrtc.h" @@ -744,29 +744,27 @@ std::unique_ptr WebRtc::CreateConnectionFlow( }}, }, { - .adapter_type_changed_cb = - {[this](/*rtc::AdapterType*/ int adapter_type) { - OffloadFromThread( - "rtc-adapter-type-changed", [this, adapter_type]() { - if (FeatureFlags::GetInstance() - .GetFlags() - .support_web_rtc_non_cellular_medium) { - AdapterTypeChangedHandler(adapter_type); - } - }); - }}, + .adapter_type_changed_cb = {[this](rtc::AdapterType adapter_type) { + OffloadFromThread("rtc-adapter-type-changed", + [this, adapter_type]() { + if (FeatureFlags::GetInstance() + .GetFlags() + .support_web_rtc_non_cellular_medium) { + AdapterTypeChangedHandler(adapter_type); + } + }); + }}, }, *medium_); } -void WebRtc::AdapterTypeChangedHandler(/*rtc::AdapterType*/ int adapter_type) { - // TODO(edwinwu): Uncomment this once OSS supports WEB_RTC - // MutexLock lock(&mutex_); - // is_using_cellular_ = adapter_type == rtc::ADAPTER_TYPE_CELLULAR || - // adapter_type == rtc::ADAPTER_TYPE_CELLULAR_2G || - // adapter_type == rtc::ADAPTER_TYPE_CELLULAR_3G || - // adapter_type == rtc::ADAPTER_TYPE_CELLULAR_4G || - // adapter_type == rtc::ADAPTER_TYPE_CELLULAR_5G; +void WebRtc::AdapterTypeChangedHandler(rtc::AdapterType adapter_type) { + MutexLock lock(&mutex_); + is_using_cellular_ = adapter_type == rtc::ADAPTER_TYPE_CELLULAR || + adapter_type == rtc::ADAPTER_TYPE_CELLULAR_2G || + adapter_type == rtc::ADAPTER_TYPE_CELLULAR_3G || + adapter_type == rtc::ADAPTER_TYPE_CELLULAR_4G || + adapter_type == rtc::ADAPTER_TYPE_CELLULAR_5G; } void WebRtc::RemoveConnectionFlow(const WebrtcPeerId& remote_peer_id) { diff --git a/connections/implementation/mediums/webrtc.h b/connections/implementation/mediums/webrtc.h index 88314843..7d5d50d6 100644 --- a/connections/implementation/mediums/webrtc.h +++ b/connections/implementation/mediums/webrtc.h @@ -235,7 +235,7 @@ class WebRtc { ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Runs on |single_thread_executor_|. - void AdapterTypeChangedHandler(/*rtc::AdapterType*/ int adapter_type) + void AdapterTypeChangedHandler(rtc::AdapterType adapter_type) ABSL_LOCKS_EXCLUDED(mutex_); void OffloadFromThread(const std::string& name, Runnable runnable); diff --git a/connections/implementation/mediums/webrtc/BUILD b/connections/implementation/mediums/webrtc/BUILD index ff2f5ca0..d821a221 100644 --- a/connections/implementation/mediums/webrtc/BUILD +++ b/connections/implementation/mediums/webrtc/BUILD @@ -68,7 +68,6 @@ cc_library( "//connections:core_types", "//internal/platform:base", "//internal/platform:types", - # TODO: Support WebRTC ], ) diff --git a/connections/implementation/mediums/webrtc/connection_flow.cc b/connections/implementation/mediums/webrtc/connection_flow.cc index 83a56fe6..578dc901 100644 --- a/connections/implementation/mediums/webrtc/connection_flow.cc +++ b/connections/implementation/mediums/webrtc/connection_flow.cc @@ -485,6 +485,16 @@ void ConnectionFlow::OnRenegotiationNeeded() { CHECK(IsRunningOnSignalingThread()); } +void ConnectionFlow::OnIceSelectedCandidatePairChanged( + const cricket::CandidatePairChangeEvent& event) { + NEARBY_LOGS(INFO) << "OnIceSelectedCandidatePairChanged"; + CHECK(IsRunningOnSignalingThread()); + // TODO(edwinwu) - Implement the unit test for this. We should be able to get + // the adapter type from the PeerConnection. + adapter_type_listener_.adapter_type_changed_cb( + event.selected_candidate_pair.local_candidate().network_type()); +} + bool ConnectionFlow::TransitionState(State current_state, State new_state) { CHECK(IsRunningOnSignalingThread()); if (current_state != state_) { diff --git a/connections/implementation/mediums/webrtc/connection_flow.h b/connections/implementation/mediums/webrtc/connection_flow.h index d1c0da5f..4793cfa4 100644 --- a/connections/implementation/mediums/webrtc/connection_flow.h +++ b/connections/implementation/mediums/webrtc/connection_flow.h @@ -86,12 +86,9 @@ class ConnectionFlow : public webrtc::PeerConnectionObserver { kEnded, }; - // The listener that notifies the AdapterType has been changed. - // TODO(edwinwu): replace param |int| to |rtc::AdapterType| once OSS supports - // WebRtc. struct AdapterTypeListener { - absl::AnyInvocable - adapter_type_changed_cb = DefaultCallback(); + absl::AnyInvocable + adapter_type_changed_cb = DefaultCallback(); }; // This method blocks on the creation of the peer connection object. @@ -149,9 +146,8 @@ class ConnectionFlow : public webrtc::PeerConnectionObserver { void OnConnectionChange( webrtc::PeerConnectionInterface::PeerConnectionState new_state) override; void OnRenegotiationNeeded() override; - // TODO(edwinwu): Implement once OSS supports WebRtc. - // void OnIceSelectedCandidatePairChanged( - // const cricket::CandidatePairChangeEvent& event) override; + void OnIceSelectedCandidatePairChanged( + const cricket::CandidatePairChangeEvent& event) override; // Public because it's used in tests too. rtc::scoped_refptr GetPeerConnection(); diff --git a/connections/implementation/mediums/webrtc/connection_flow_test.cc b/connections/implementation/mediums/webrtc/connection_flow_test.cc index 5d349fb6..3e7f5402 100644 --- a/connections/implementation/mediums/webrtc/connection_flow_test.cc +++ b/connections/implementation/mediums/webrtc/connection_flow_test.cc @@ -81,7 +81,7 @@ TEST_F(ConnectionFlowTest, SuccessfulOfferAnswerFlow) { offerer_socket_future.Set(std::move(socket)); }}, {.adapter_type_changed_cb = - [](/*rtc::AdapterType*/ int adapter_type) { + [](rtc::AdapterType adapter_type) { // Do nothing }}, webrtc_medium_offerer); @@ -101,7 +101,7 @@ TEST_F(ConnectionFlowTest, SuccessfulOfferAnswerFlow) { answerer_socket_future.Set(std::move(socket)); }}, {.adapter_type_changed_cb = - [](/*rtc::AdapterType*/ int adapter_type) { + [](rtc::AdapterType adapter_type) { // Do nothing }}, webrtc_medium_answerer); @@ -280,7 +280,7 @@ TEST_F(ConnectionFlowTest, TerminateAnswerer) { offerer_socket_future.Set(std::move(socket)); }}, {.adapter_type_changed_cb = - [](/*rtc::AdapterType*/ int adapter_type) { + [](rtc::AdapterType adapter_type) { // Do nothing }}, webrtc_medium_offerer); @@ -300,8 +300,9 @@ TEST_F(ConnectionFlowTest, TerminateAnswerer) { answerer_socket_future.Set(std::move(wrapper)); }}, {.adapter_type_changed_cb = - [](/*rtc::AdapterType*/ int adapter_type) { - // Do nothing + [](rtc::AdapterType adapter_type) { + EXPECT_GE(adapter_type, rtc::ADAPTER_TYPE_UNKNOWN); + EXPECT_LE(adapter_type, rtc::ADAPTER_TYPE_CELLULAR_5G); }}, webrtc_medium_answerer); ASSERT_NE(answerer, nullptr); @@ -369,8 +370,9 @@ TEST_F(ConnectionFlowTest, TerminateOfferer) { offerer_socket_future.Set(std::move(socket)); }}, {.adapter_type_changed_cb = - [](/*rtc::AdapterType*/ int adapter_type) { - // Do nothing + [](rtc::AdapterType adapter_type) { + EXPECT_GE(adapter_type, rtc::ADAPTER_TYPE_UNKNOWN); + EXPECT_LE(adapter_type, rtc::ADAPTER_TYPE_CELLULAR_5G); }}, webrtc_medium_offerer); ASSERT_NE(offerer, nullptr); @@ -389,8 +391,9 @@ TEST_F(ConnectionFlowTest, TerminateOfferer) { answerer_socket_future.Set(std::move(wrapper)); }}, {.adapter_type_changed_cb = - [](/*rtc::AdapterType*/ int adapter_type) { - // Do nothing + [](rtc::AdapterType adapter_type) { + EXPECT_GE(adapter_type, rtc::ADAPTER_TYPE_UNKNOWN); + EXPECT_LE(adapter_type, rtc::ADAPTER_TYPE_CELLULAR_5G); }}, webrtc_medium_answerer); ASSERT_NE(answerer, nullptr); @@ -432,6 +435,7 @@ TEST_F(ConnectionFlowTest, TerminateOfferer) { answerer_socket.result().GetInputStream().Read(4); EXPECT_TRUE(received_message.GetResult().Empty()); } + } // namespace } // namespace mediums } // namespace connections diff --git a/connections/implementation/mediums/webrtc/webrtc_socket_impl.h b/connections/implementation/mediums/webrtc/webrtc_socket_impl.h index 2c11f57f..00bf08c6 100644 --- a/connections/implementation/mediums/webrtc/webrtc_socket_impl.h +++ b/connections/implementation/mediums/webrtc/webrtc_socket_impl.h @@ -31,7 +31,6 @@ #include "internal/platform/output_stream.h" #include "internal/platform/single_thread_executor.h" #include "internal/platform/socket.h" -#include "webrtc/api/data_channel_interface.h" namespace nearby { namespace connections { diff --git a/internal/platform/webrtc.h b/internal/platform/webrtc.h index 96a83f9b..0b6463b1 100644 --- a/internal/platform/webrtc.h +++ b/internal/platform/webrtc.h @@ -90,9 +90,10 @@ class WebRtcMedium { PeerConnectionCallback callback) { if (FeatureFlags::GetInstance() .GetFlags() - .support_web_rtc_non_cellular_medium) { - // TODO(edwinwu): Add support for non-cellular networks. - impl_->CreatePeerConnection(std::nullopt, observer, std::move(callback)); + .support_web_rtc_non_cellular_medium && non_cellular_) { + std::optional options; + options->network_ignore_mask |= rtc::ADAPTER_TYPE_CELLULAR; + impl_->CreatePeerConnection(options, observer, std::move(callback)); } else { impl_->CreatePeerConnection(observer, std::move(callback)); }