Move platform dependent part of WebRtcMedium into WebRtcPlatform.

PiperOrigin-RevId: 919888693
This commit is contained in:
Francis Tsui
2026-05-22 15:32:44 -07:00
committed by Copybara-Service
parent b536c8da3a
commit 4e387e7ea7
18 changed files with 25 additions and 70 deletions
@@ -40,10 +40,6 @@ class WebRtc {
virtual ~WebRtc() = default;
// Gets the default two-letter country code associated with current locale.
// For example, en_US locale resolves to "US".
virtual std::string GetDefaultCountryCode() { return ""; }
// Returns if WebRtc is available as a medium for nearby to transport data.
// Runs on @MainThread.
virtual bool IsAvailable() { return false; }
@@ -132,6 +132,7 @@ cc_library(
"//internal/platform:cancellation_flag",
"//internal/platform:logging",
"//internal/platform:types",
"//internal/platform/implementation:webrtc_platform",
"//proto/mediums:web_rtc_signaling_frames_cc_proto",
"//third_party/webrtc/files/stable/webrtc/api:jsep",
"//third_party/webrtc/files/stable/webrtc/rtc_base:network_constants",
@@ -17,7 +17,6 @@
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
@@ -72,10 +71,6 @@ class WebRtcMedium {
WebRtcMedium(WebRtcMedium&&) = default;
WebRtcMedium& operator=(WebRtcMedium&&) = delete;
// Gets the default two-letter country code associated with current locale.
// For example, en_US locale resolves to "US".
std::string GetDefaultCountryCode() { return impl_->GetDefaultCountryCode(); }
void SetNonCellular(bool non_cellular) { non_cellular_ = non_cellular; }
// Creates and returns a new webrtc::PeerConnectionInterface object via
@@ -30,6 +30,7 @@
#include "connections/implementation/offline_frames.h"
#include "connections/implementation/proto/offline_wire_formats.pb.h"
#include "internal/platform/expected.h"
#include "internal/platform/implementation/webrtc_platform.h"
#include "internal/platform/logging.h"
namespace nearby {
@@ -135,8 +136,8 @@ void WebrtcBwuHandler::HandleRevertInitiatorStateForService(
std::string WebrtcBwuHandler::HandleInitializeUpgradedMediumForEndpoint(
ClientProxy* client, const std::string& upgrade_service_id,
const std::string& endpoint_id) {
LocationHint location_hint =
BuildLocationHint(webrtc_.GetDefaultCountryCode());
LocationHint location_hint = BuildLocationHint(
api::WebRtcImplementationPlatform::GetDefaultCountryCode());
mediums::WebrtcPeerId self_id{mediums::WebrtcPeerId::FromRandom()};
if (!webrtc_.IsAcceptingConnections(upgrade_service_id)) {
@@ -81,10 +81,6 @@ WebRtcImpl::~WebRtcImpl() {
}
}
std::string WebRtcImpl::GetDefaultCountryCode() {
return medium_->GetDefaultCountryCode();
}
bool WebRtcImpl::IsAvailable() { return medium_->IsValid(); }
bool WebRtcImpl::IsAcceptingConnections(const std::string& service_id) {
@@ -52,7 +52,6 @@ class WebRtcImpl : public WebRtc {
~WebRtcImpl() override;
// Overrides for WebRtc:
std::string GetDefaultCountryCode() override;
bool IsAvailable() override;
bool IsAcceptingConnections(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
@@ -19,7 +19,6 @@
#include <memory>
#include <optional>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/webrtc.h"
@@ -31,11 +30,6 @@ class WebRtcMedium : public api::WebRtcMedium {
public:
~WebRtcMedium() override = default;
// Gets the default two-letter country code associated with current locale.
// For example, en_US locale resolves to "US".
// This follows the ISO 3166-1 Alpha-2 standard.
std::string GetDefaultCountryCode() override;
// Creates and returns a new webrtc::PeerConnectionInterface object via
// |callback|.
void CreatePeerConnection(webrtc::PeerConnectionObserver* observer,
@@ -20,7 +20,6 @@
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/status/status.h"
@@ -36,14 +35,6 @@
namespace nearby::apple {
std::string WebRtcMedium::GetDefaultCountryCode() {
NSString* countryCode = [NSLocale.currentLocale objectForKey:NSLocaleCountryCode];
if (countryCode) {
return std::string([countryCode UTF8String]);
}
return "US";
}
void WebRtcMedium::CreatePeerConnection(webrtc::PeerConnectionObserver* observer,
PeerConnectionCallback callback) {
CreatePeerConnection(std::nullopt, observer, std::move(callback));
@@ -27,5 +27,13 @@ std::unique_ptr<WebRtcMedium> WebRtcImplementationPlatform::CreateWebRtcMedium()
return std::make_unique<apple::WebRtcMedium>();
}
std::string WebRtcImplementationPlatform::GetDefaultCountryCode() {
NSString* countryCode = [NSLocale.currentLocale objectForKey:NSLocaleCountryCode];
if (countryCode) {
return std::string([countryCode UTF8String]);
}
return "US";
}
} // namespace api
} // namespace nearby
@@ -121,6 +121,7 @@ cc_library(
"//third_party/webrtc/files/stable/webrtc/api:peer_connection_interface",
"//third_party/webrtc/files/stable/webrtc/api:scoped_refptr",
"//third_party/webrtc/files/stable/webrtc/rtc_base:checks",
"//third_party/webrtc/files/stable/webrtc/rtc_base:threading",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
@@ -28,6 +28,7 @@
#include "webrtc/api/peer_connection_interface.h"
#include "webrtc/api/scoped_refptr.h"
#include "webrtc/rtc_base/checks.h"
#include "webrtc/rtc_base/thread.h"
namespace nearby {
namespace g3 {
@@ -60,8 +61,6 @@ void WebRtcSignalingMessenger::StopReceivingMessages() {
WebRtcMedium::~WebRtcMedium() { single_thread_executor_.Shutdown(); }
std::string WebRtcMedium::GetDefaultCountryCode() { return "US"; }
void WebRtcMedium::CreatePeerConnection(
webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) {
CreatePeerConnection(std::nullopt, observer, std::move(callback));
@@ -59,8 +59,6 @@ class WebRtcMedium : public api::WebRtcMedium {
WebRtcMedium() = default;
~WebRtcMedium() override;
std::string GetDefaultCountryCode() override;
// Creates and returns a new webrtc::PeerConnectionInterface object via
// |callback|.
void CreatePeerConnection(webrtc::PeerConnectionObserver* observer,
@@ -15,6 +15,7 @@
#include "internal/platform/implementation/webrtc_platform.h"
#include <memory>
#include <string>
#include "internal/platform/implementation/g3/webrtc.h"
#include "internal/platform/implementation/webrtc.h"
@@ -31,4 +32,8 @@ WebRtcImplementationPlatform::CreateWebRtcMedium() {
}
}
std::string WebRtcImplementationPlatform::GetDefaultCountryCode() {
return "US";
}
} // namespace nearby::api
@@ -17,7 +17,6 @@
#include <memory>
#include <optional>
#include <string>
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
@@ -52,10 +51,6 @@ class WebRtcMedium {
virtual ~WebRtcMedium() = default;
// Gets the default two-letter country code associated with current locale.
// For example, en_US locale resolves to "US".
virtual std::string GetDefaultCountryCode() = 0;
// Creates and returns a new webrtc::PeerConnectionInterface object via
// |callback|.
virtual void CreatePeerConnection(webrtc::PeerConnectionObserver* observer,
@@ -16,6 +16,7 @@
#define PLATFORM_API_WEBRTC_PLATFORM_H_
#include <memory>
#include <string>
#include "internal/platform/implementation/webrtc.h"
@@ -24,6 +25,11 @@ namespace nearby::api {
class WebRtcImplementationPlatform {
public:
static std::unique_ptr<WebRtcMedium> CreateWebRtcMedium();
// Gets the default two-letter country code associated with current locale.
// For example, en_US locale resolves to "US".
// This follows the ISO 3166-1 Alpha-2 standard.
static std::string GetDefaultCountryCode();
};
} // namespace nearby::api
@@ -14,17 +14,13 @@
#include "internal/platform/implementation/windows/webrtc.h"
#include <winnls.h>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
#include "connections/implementation/mediums/webrtc/tachyon_express_signaling_messenger.h"
#include "internal/platform/implementation/webrtc.h"
#include "internal/platform/logging.h"
#include "webrtc/api/create_modular_peer_connection_factory.h"
#include "webrtc/api/peer_connection_interface.h"
#include "webrtc/api/rtc_error.h"
@@ -35,20 +31,6 @@ namespace nearby::windows {
using ::nearby::connections::mediums::TachyonExpressSignalingMessenger;
std::string WebRtcMedium::GetDefaultCountryCode() {
wchar_t systemGeoName[LOCALE_NAME_MAX_LENGTH];
if (!GetUserDefaultGeoName(systemGeoName, LOCALE_NAME_MAX_LENGTH)) {
LOG(ERROR) << __func__
<< ": Failed to GetUserDefaultGeoName: " << ". Fall back to US.";
return "US";
}
std::wstring wideGeo(systemGeoName);
std::string systemGeoNameString(wideGeo.begin(), wideGeo.end());
VLOG(1) << "GetUserDefaultGeoName() returns: " << systemGeoNameString;
return systemGeoNameString;
}
void WebRtcMedium::CreatePeerConnection(
webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) {
CreatePeerConnection(std::nullopt, observer, std::move(callback));
@@ -17,7 +17,6 @@
#include <memory>
#include <optional>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/webrtc.h"
@@ -29,11 +28,6 @@ class WebRtcMedium : public api::WebRtcMedium {
public:
~WebRtcMedium() override = default;
// Gets the default two-letter country code associated with current locale.
// For example, en_US locale resolves to "US".
// This follows the ISO 3166-1 Alpha-2 standard.
std::string GetDefaultCountryCode() override;
// Creates and returns a new webrtc::PeerConnectionInterface object via
// |callback|.
void CreatePeerConnection(webrtc::PeerConnectionObserver* observer,
@@ -51,12 +51,6 @@ location::nearby::connections::LocationHint GetCountryCodeLocationHint(
return location_hint;
}
TEST(WebrtcTest, CountryCodeDefault) {
WebRtcMedium medium;
std::string result = medium.GetDefaultCountryCode();
EXPECT_EQ(result, "US");
}
TEST(WebrtcTest, CreatePeerConnectionSucceeds) {
auto observer = std::make_unique<MockPeerConnectionObserver>();
WebRtcMedium medium;