From ec025502dc213d03395b4e6e5d95c61cdf7b8e1c Mon Sep 17 00:00:00 2001 From: Nick Bourdakos Date: Wed, 22 Mar 2023 00:24:36 -0700 Subject: [PATCH] Refactor WebRTC code to avoid use of `select` for stubs PiperOrigin-RevId: 518491132 --- connections/implementation/BUILD | 22 ++---- connections/implementation/mediums/BUILD | 73 ++++++++++--------- connections/implementation/mediums/webrtc.cc | 4 + connections/implementation/mediums/webrtc.h | 4 + .../implementation/mediums/webrtc_peer_id.cc | 4 + .../implementation/mediums/webrtc_peer_id.h | 4 + .../mediums/webrtc_peer_id_stub.cc | 4 + .../mediums/webrtc_peer_id_stub.h | 10 ++- .../implementation/mediums/webrtc_socket.h | 4 + .../mediums/webrtc_socket_stub.h | 4 + .../implementation/mediums/webrtc_stub.cc | 4 + .../implementation/mediums/webrtc_stub.h | 4 + .../implementation/webrtc_bwu_handler.cc | 4 + .../implementation/webrtc_bwu_handler.h | 8 +- .../implementation/webrtc_bwu_handler_stub.cc | 4 + .../implementation/webrtc_bwu_handler_stub.h | 10 ++- internal/platform/BUILD | 1 + internal/platform/implementation/BUILD | 1 + internal/platform/implementation/g3/BUILD | 2 + internal/platform/implementation/webrtc.h | 4 + 20 files changed, 115 insertions(+), 60 deletions(-) diff --git a/connections/implementation/BUILD b/connections/implementation/BUILD index 2b395594..03c46a89 100644 --- a/connections/implementation/BUILD +++ b/connections/implementation/BUILD @@ -66,6 +66,8 @@ cc_library( "payload_manager.cc", "pcp_manager.cc", "service_controller_router.cc", + "webrtc_bwu_handler.cc", + "webrtc_bwu_handler_stub.cc", "webrtc_endpoint_channel.cc", "wifi_direct_bwu_handler.cc", "wifi_direct_endpoint_channel.cc", @@ -74,14 +76,7 @@ cc_library( "wifi_lan_bwu_handler.cc", "wifi_lan_endpoint_channel.cc", "wifi_lan_service_info.cc", - ] + select({ - "@platforms//cpu:arm": [ - "webrtc_bwu_handler_stub.cc", - ], - "//conditions:default": [ - "webrtc_bwu_handler_stub.cc", - ], - }), + ], hdrs = [ "base_bwu_handler.h", "base_endpoint_channel.h", @@ -115,6 +110,8 @@ cc_library( "service_controller.h", "service_controller_router.h", "service_id_constants.h", + "webrtc_bwu_handler.h", + "webrtc_bwu_handler_stub.h", "webrtc_endpoint_channel.h", "wifi_direct_bwu_handler.h", "wifi_direct_endpoint_channel.h", @@ -123,14 +120,7 @@ cc_library( "wifi_lan_bwu_handler.h", "wifi_lan_endpoint_channel.h", "wifi_lan_service_info.h", - ] + select({ - "@platforms//cpu:arm": [ - "webrtc_bwu_handler_stub.h", - ], - "//conditions:default": [ - "webrtc_bwu_handler_stub.h", - ], - }), + ], copts = ["-DCORE_ADAPTER_DLL"] + select({ "@platforms//cpu:arm": [ "-DNO_WEBRTC", diff --git a/connections/implementation/mediums/BUILD b/connections/implementation/mediums/BUILD index 57b02893..80115585 100644 --- a/connections/implementation/mediums/BUILD +++ b/connections/implementation/mediums/BUILD @@ -21,35 +21,25 @@ cc_library( "bluetooth_classic.cc", "bluetooth_radio.cc", "mediums.cc", + "webrtc.cc", + "webrtc_stub.cc", "wifi_direct.cc", "wifi_hotspot.cc", "wifi_lan.cc", - ] + select({ - "@platforms//cpu:arm": [ - "webrtc_stub.cc", - ], - "//conditions:default": [ - "webrtc_stub.cc", - ], - }), + ], hdrs = [ "ble.h", "ble_v2.h", "bluetooth_classic.h", "bluetooth_radio.h", "mediums.h", + "webrtc.h", + "webrtc_stub.h", "wifi.h", "wifi_direct.h", "wifi_hotspot.h", "wifi_lan.h", - ] + select({ - "@platforms//cpu:arm": [ - "webrtc_stub.h", - ], - "//conditions:default": [ - "webrtc_stub.h", - ], - }), + ], copts = select({ "@platforms//cpu:arm": [ "-DNO_WEBRTC", @@ -91,27 +81,17 @@ cc_library( name = "utils", srcs = [ "utils.cc", - ] + select({ - "@platforms//cpu:arm": [ - "webrtc_peer_id_stub.cc", - ], - "//conditions:default": [ - "webrtc_peer_id_stub.cc", - ], - }), + "webrtc_peer_id.cc", + "webrtc_peer_id_stub.cc", + ], hdrs = [ "lost_entity_tracker.h", "utils.h", - ] + select({ - "@platforms//cpu:arm": [ - "webrtc_peer_id_stub.h", - "webrtc_socket_stub.h", - ], - "//conditions:default": [ - "webrtc_peer_id_stub.h", - "webrtc_socket_stub.h", - ], - }), + "webrtc_peer_id.h", + "webrtc_peer_id_stub.h", + "webrtc_socket.h", + "webrtc_socket_stub.h", + ], copts = select({ "@platforms//cpu:arm": [ "-DNO_WEBRTC", @@ -164,3 +144,28 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "core_internal_mediums_webrtc_test", + size = "small", + srcs = [ + "webrtc_peer_id_test.cc", + "webrtc_test.cc", + ], + defines = ["NO_WEBRTC"], + shard_count = 16, + tags = [ + "notsan", # NOTE(b/139734036): known data race in usrsctplib. + "requires-net:external", + ], + deps = [ + ":mediums", + ":utils", + "//internal/platform:base", + "//internal/platform:test_util", + "//internal/platform:types", + "//internal/platform/implementation/g3", # build_cleaner: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/connections/implementation/mediums/webrtc.cc b/connections/implementation/mediums/webrtc.cc index 6033cd93..17ec1866 100644 --- a/connections/implementation/mediums/webrtc.cc +++ b/connections/implementation/mediums/webrtc.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef NO_WEBRTC + #include "connections/implementation/mediums/webrtc.h" #include @@ -741,3 +743,5 @@ void WebRtc::OffloadFromThread(const std::string& name, Runnable runnable) { } // namespace mediums } // namespace connections } // namespace nearby + +#endif diff --git a/connections/implementation/mediums/webrtc.h b/connections/implementation/mediums/webrtc.h index ebfd6f77..f8d8ba1e 100644 --- a/connections/implementation/mediums/webrtc.h +++ b/connections/implementation/mediums/webrtc.h @@ -15,6 +15,8 @@ #ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_H_ #define CORE_INTERNAL_MEDIUMS_WEBRTC_H_ +#ifndef NO_WEBRTC + #include #include #include @@ -249,4 +251,6 @@ class WebRtc { } // namespace connections } // namespace nearby +#endif + #endif // CORE_INTERNAL_MEDIUMS_WEBRTC_H_ diff --git a/connections/implementation/mediums/webrtc_peer_id.cc b/connections/implementation/mediums/webrtc_peer_id.cc index 8d11894c..801d40c6 100644 --- a/connections/implementation/mediums/webrtc_peer_id.cc +++ b/connections/implementation/mediums/webrtc_peer_id.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef NO_WEBRTC + #include "connections/implementation/mediums/webrtc_peer_id.h" #include @@ -50,3 +52,5 @@ bool WebrtcPeerId::IsValid() const { return !id_.empty(); } } // namespace mediums } // namespace connections } // namespace nearby + +#endif diff --git a/connections/implementation/mediums/webrtc_peer_id.h b/connections/implementation/mediums/webrtc_peer_id.h index 2027b74e..93013902 100644 --- a/connections/implementation/mediums/webrtc_peer_id.h +++ b/connections/implementation/mediums/webrtc_peer_id.h @@ -15,6 +15,8 @@ #ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_ #define CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_ +#ifndef NO_WEBRTC + #include #include @@ -47,4 +49,6 @@ class WebrtcPeerId { } // namespace connections } // namespace nearby +#endif + #endif // CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_ diff --git a/connections/implementation/mediums/webrtc_peer_id_stub.cc b/connections/implementation/mediums/webrtc_peer_id_stub.cc index 0b6983e0..1b74dad2 100644 --- a/connections/implementation/mediums/webrtc_peer_id_stub.cc +++ b/connections/implementation/mediums/webrtc_peer_id_stub.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifdef NO_WEBRTC + #include "connections/implementation/mediums/webrtc_peer_id_stub.h" #include @@ -33,3 +35,5 @@ bool WebrtcPeerId::IsValid() const { return false; } } // namespace mediums } // namespace connections } // namespace nearby + +#endif diff --git a/connections/implementation/mediums/webrtc_peer_id_stub.h b/connections/implementation/mediums/webrtc_peer_id_stub.h index 2027b74e..8d249a8c 100644 --- a/connections/implementation/mediums/webrtc_peer_id_stub.h +++ b/connections/implementation/mediums/webrtc_peer_id_stub.h @@ -12,8 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_ -#define CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_ +#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_STUB_H_ +#define CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_STUB_H_ + +#ifdef NO_WEBRTC #include #include @@ -47,4 +49,6 @@ class WebrtcPeerId { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_H_ +#endif + +#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_PEER_ID_STUB_H_ diff --git a/connections/implementation/mediums/webrtc_socket.h b/connections/implementation/mediums/webrtc_socket.h index 6af35b59..3c1370e6 100644 --- a/connections/implementation/mediums/webrtc_socket.h +++ b/connections/implementation/mediums/webrtc_socket.h @@ -15,6 +15,8 @@ #ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_H_ #define CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_H_ +#ifndef NO_WEBRTC + #include #include "connections/implementation/mediums/webrtc/webrtc_socket_impl.h" @@ -50,4 +52,6 @@ class WebRtcSocketWrapper final { } // namespace connections } // namespace nearby +#endif + #endif // CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_H_ diff --git a/connections/implementation/mediums/webrtc_socket_stub.h b/connections/implementation/mediums/webrtc_socket_stub.h index 70448652..8cecff4e 100644 --- a/connections/implementation/mediums/webrtc_socket_stub.h +++ b/connections/implementation/mediums/webrtc_socket_stub.h @@ -15,6 +15,8 @@ #ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_STUB_H_ #define CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_STUB_H_ +#ifdef NO_WEBRTC + #include #include "internal/platform/input_stream.h" @@ -64,4 +66,6 @@ class WebRtcSocketWrapper final { } // namespace connections } // namespace nearby +#endif + #endif // CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_STUB_H_ diff --git a/connections/implementation/mediums/webrtc_stub.cc b/connections/implementation/mediums/webrtc_stub.cc index c459761c..425cce4c 100644 --- a/connections/implementation/mediums/webrtc_stub.cc +++ b/connections/implementation/mediums/webrtc_stub.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifdef NO_WEBRTC + #include "connections/implementation/mediums/webrtc_stub.h" #include @@ -58,3 +60,5 @@ WebRtcSocketWrapper WebRtc::Connect(const std::string& service_id, } // namespace mediums } // namespace connections } // namespace nearby + +#endif diff --git a/connections/implementation/mediums/webrtc_stub.h b/connections/implementation/mediums/webrtc_stub.h index f65bef1d..a0609d95 100644 --- a/connections/implementation/mediums/webrtc_stub.h +++ b/connections/implementation/mediums/webrtc_stub.h @@ -15,6 +15,8 @@ #ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_STUB_H_ #define CORE_INTERNAL_MEDIUMS_WEBRTC_STUB_H_ +#ifdef NO_WEBRTC + #include #include #include @@ -77,4 +79,6 @@ class WebRtc { } // namespace connections } // namespace nearby +#endif + #endif // CORE_INTERNAL_MEDIUMS_WEBRTC_STUB_H_ diff --git a/connections/implementation/webrtc_bwu_handler.cc b/connections/implementation/webrtc_bwu_handler.cc index 81ec54e9..535d2bc1 100644 --- a/connections/implementation/webrtc_bwu_handler.cc +++ b/connections/implementation/webrtc_bwu_handler.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef NO_WEBRTC + #include "connections/implementation/webrtc_bwu_handler.h" #include @@ -152,3 +154,5 @@ void WebrtcBwuHandler::OnIncomingWebrtcConnection( } // namespace connections } // namespace nearby + +#endif diff --git a/connections/implementation/webrtc_bwu_handler.h b/connections/implementation/webrtc_bwu_handler.h index 1fd46bbb..46faae34 100644 --- a/connections/implementation/webrtc_bwu_handler.h +++ b/connections/implementation/webrtc_bwu_handler.h @@ -15,17 +15,15 @@ #ifndef CORE_INTERNAL_WEBRTC_BWU_HANDLER_H_ #define CORE_INTERNAL_WEBRTC_BWU_HANDLER_H_ +#ifndef NO_WEBRTC + #include #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel_manager.h" #include "connections/implementation/mediums/mediums.h" -#ifdef NO_WEBRTC -#include "connections/implementation/mediums/webrtc_socket_stub.h" -#else #include "connections/implementation/mediums/webrtc_socket.h" -#endif namespace nearby { namespace connections { @@ -77,4 +75,6 @@ class WebrtcBwuHandler : public BaseBwuHandler { } // namespace connections } // namespace nearby +#endif + #endif // CORE_INTERNAL_WEBRTC_BWU_HANDLER_H_ diff --git a/connections/implementation/webrtc_bwu_handler_stub.cc b/connections/implementation/webrtc_bwu_handler_stub.cc index 09f66d4c..f4590027 100644 --- a/connections/implementation/webrtc_bwu_handler_stub.cc +++ b/connections/implementation/webrtc_bwu_handler_stub.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifdef NO_WEBRTC + #include "connections/implementation/webrtc_bwu_handler_stub.h" #include @@ -70,3 +72,5 @@ void WebrtcBwuHandler::OnIncomingWebrtcConnection( } // namespace connections } // namespace nearby + +#endif diff --git a/connections/implementation/webrtc_bwu_handler_stub.h b/connections/implementation/webrtc_bwu_handler_stub.h index 1fd46bbb..f4aec1ac 100644 --- a/connections/implementation/webrtc_bwu_handler_stub.h +++ b/connections/implementation/webrtc_bwu_handler_stub.h @@ -12,8 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_WEBRTC_BWU_HANDLER_H_ -#define CORE_INTERNAL_WEBRTC_BWU_HANDLER_H_ +#ifndef CORE_INTERNAL_WEBRTC_BWU_HANDLER_STUB_H_ +#define CORE_INTERNAL_WEBRTC_BWU_HANDLER_STUB_H_ + +#ifdef NO_WEBRTC #include @@ -77,4 +79,6 @@ class WebrtcBwuHandler : public BaseBwuHandler { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_WEBRTC_BWU_HANDLER_H_ +#endif + +#endif // CORE_INTERNAL_WEBRTC_BWU_HANDLER_STUB_H_ diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 8d44f2cc..b528fc16 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -397,6 +397,7 @@ cc_library( "bluetooth_adapter.h", "bluetooth_classic.h", "credential_storage_impl.h", + "webrtc.h", "wifi.h", "wifi_direct.h", "wifi_hotspot.h", diff --git a/internal/platform/implementation/BUILD b/internal/platform/implementation/BUILD index 5fabe6f4..6cd77a99 100644 --- a/internal/platform/implementation/BUILD +++ b/internal/platform/implementation/BUILD @@ -66,6 +66,7 @@ cc_library( "credential_storage.h", "http_loader.h", "server_sync.h", + "webrtc.h", "wifi.h", "wifi_direct.h", "wifi_hotspot.h", diff --git a/internal/platform/implementation/g3/BUILD b/internal/platform/implementation/g3/BUILD index 1c4ca8eb..5985d414 100644 --- a/internal/platform/implementation/g3/BUILD +++ b/internal/platform/implementation/g3/BUILD @@ -62,6 +62,7 @@ cc_library( "bluetooth_adapter.cc", "bluetooth_classic.cc", "credential_storage_impl.cc", + "webrtc.cc", "wifi_direct.cc", "wifi_hotspot.cc", "wifi_lan.cc", @@ -72,6 +73,7 @@ cc_library( "bluetooth_adapter.h", "bluetooth_classic.h", "credential_storage_impl.h", + "webrtc.h", "wifi.h", "wifi_direct.h", "wifi_hotspot.h", diff --git a/internal/platform/implementation/webrtc.h b/internal/platform/implementation/webrtc.h index bf03303c..1c0bb04e 100644 --- a/internal/platform/implementation/webrtc.h +++ b/internal/platform/implementation/webrtc.h @@ -15,6 +15,8 @@ #ifndef PLATFORM_API_WEBRTC_H_ #define PLATFORM_API_WEBRTC_H_ +#ifndef NO_WEBRTC + #include #include @@ -67,4 +69,6 @@ class WebRtcMedium { } // namespace api } // namespace nearby +#endif + #endif // PLATFORM_API_WEBRTC_H_