Create platform independent WebRtcMedium impl.

PiperOrigin-RevId: 919894367
This commit is contained in:
Francis Tsui
2026-05-22 15:45:18 -07:00
committed by Copybara-Service
parent 4e387e7ea7
commit 4181803eb6
11 changed files with 127 additions and 218 deletions
@@ -99,6 +99,25 @@ cc_library(
],
)
cc_library(
name = "webrtc_medium_impl",
srcs = ["webrtc_medium_impl.cc"],
hdrs = ["webrtc_medium_impl.h"],
visibility = [
"//internal/platform/implementation:__subpackages__",
],
deps = [
":tachyon_express_signaling_messenger",
"//internal/platform/implementation:webrtc_platform",
"//third_party/webrtc/files/stable/webrtc/api:create_modular_peer_connection_factory",
"//third_party/webrtc/files/stable/webrtc/api:libjingle_peerconnection_api",
"//third_party/webrtc/files/stable/webrtc/api:rtc_error",
"//third_party/webrtc/files/stable/webrtc/api:scoped_refptr",
"//third_party/webrtc/files/stable/webrtc/rtc_base:threading",
"@com_google_absl//absl/strings:string_view",
],
)
cc_library(
name = "webrtc_impl",
srcs = [
@@ -149,11 +168,6 @@ cc_library(
name = "tachyon_express_signaling_messenger",
srcs = ["tachyon_express_signaling_messenger.cc"],
hdrs = ["tachyon_express_signaling_messenger.h"],
visibility = [
"//connections:__subpackages__",
"//internal/platform/implementation:__subpackages__",
"//internal/test:__subpackages__",
],
deps = [
"//internal/account",
"//internal/platform:base",
@@ -229,3 +243,20 @@ cc_test(
"@com_google_protobuf//:protobuf",
],
)
cc_test(
name = "webrtc_medium_impl_test",
size = "small",
srcs = ["webrtc_medium_impl_test.cc"],
deps = [
":webrtc_medium_impl",
"//internal/platform/implementation:platform_impl",
"//internal/platform/implementation:webrtc_platform",
"//third_party/webrtc/files/stable/webrtc/api:data_channel_interface",
"//third_party/webrtc/files/stable/webrtc/api:jsep",
"//third_party/webrtc/files/stable/webrtc/api:peer_connection_interface",
"//third_party/webrtc/files/stable/webrtc/api:scoped_refptr",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
@@ -0,0 +1,17 @@
# WebRtc support for Nearby Connections
This directory contains the implementation to support WebRtc in the Nearby
Connection library.
All dependencies on webrtc MUST be limited to targets in this directory.
## To Enable WebRtc support
To enabled WebRtc support undefine the ```NO_WEBRTC``` preprocessor symbol in
the file **connections/implementation/mediums/mediums.cc**.
When building using bazel, pass the build flag
```--//:enable_webrtc=true``` to set the correct symbol.
Make sure the binary is linked with an implementation of the
```WebRtcImplementationPlatform```.
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/windows/webrtc.h"
#include "connections/implementation/mediums/webrtc/webrtc_medium_impl.h"
#include <memory>
#include <optional>
@@ -27,16 +27,14 @@
#include "webrtc/api/scoped_refptr.h"
#include "webrtc/rtc_base/thread.h"
namespace nearby::windows {
namespace nearby::connections::mediums {
using ::nearby::connections::mediums::TachyonExpressSignalingMessenger;
void WebRtcMedium::CreatePeerConnection(
void WebRtcMediumImpl::CreatePeerConnection(
webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) {
CreatePeerConnection(std::nullopt, observer, std::move(callback));
}
void WebRtcMedium::CreatePeerConnection(
void WebRtcMediumImpl::CreatePeerConnection(
std::optional<webrtc::PeerConnectionFactoryInterface::Options> options,
webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) {
webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
@@ -79,11 +77,11 @@ void WebRtcMedium::CreatePeerConnection(
}
std::unique_ptr<api::WebRtcSignalingMessenger>
WebRtcMedium::GetSignalingMessenger(
WebRtcMediumImpl::GetSignalingMessenger(
absl::string_view self_id,
const location::nearby::connections::LocationHint& location_hint) {
return std::make_unique<TachyonExpressSignalingMessenger>(self_id,
location_hint);
}
} // namespace nearby::windows
} // namespace nearby::connections::mediums
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_IMPL_WINDOWS_WEBRTC_H_
#define PLATFORM_IMPL_WINDOWS_WEBRTC_H_
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_MEDIUM_IMPL_H_
#define CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_MEDIUM_IMPL_H_
#include <memory>
#include <optional>
@@ -22,11 +22,11 @@
#include "internal/platform/implementation/webrtc.h"
#include "webrtc/api/peer_connection_interface.h"
namespace nearby::windows {
namespace nearby::connections::mediums {
class WebRtcMedium : public api::WebRtcMedium {
class WebRtcMediumImpl : public api::WebRtcMedium {
public:
~WebRtcMedium() override = default;
~WebRtcMediumImpl() override = default;
// Creates and returns a new webrtc::PeerConnectionInterface object via
// |callback|.
@@ -47,6 +47,6 @@ class WebRtcMedium : public api::WebRtcMedium {
override;
};
} // namespace nearby::windows
} // namespace nearby::connections::mediums
#endif // PLATFORM_IMPL_WINDOWS_WEBRTC_H_
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_MEDIUM_IMPL_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/windows/webrtc.h"
#include "connections/implementation/mediums/webrtc/webrtc_medium_impl.h"
#include <memory>
#include <optional>
@@ -25,8 +25,7 @@
#include "webrtc/api/peer_connection_interface.h"
#include "webrtc/api/scoped_refptr.h"
namespace nearby {
namespace windows {
namespace nearby::connections::mediums {
class MockPeerConnectionObserver : public webrtc::PeerConnectionObserver {
public:
@@ -51,9 +50,9 @@ location::nearby::connections::LocationHint GetCountryCodeLocationHint(
return location_hint;
}
TEST(WebrtcTest, CreatePeerConnectionSucceeds) {
TEST(WebrtcMediumImplTest, CreatePeerConnectionSucceeds) {
auto observer = std::make_unique<MockPeerConnectionObserver>();
WebRtcMedium medium;
WebRtcMediumImpl medium;
medium.CreatePeerConnection(
std::nullopt, observer.get(),
[](webrtc::scoped_refptr<webrtc::PeerConnectionInterface>
@@ -65,12 +64,11 @@ TEST(WebrtcTest, CreatePeerConnectionSucceeds) {
});
}
TEST(WebrtcTest, GetSignalingMessengerSucceeds) {
WebRtcMedium medium;
TEST(WebrtcMediumImplTest, GetSignalingMessengerSucceeds) {
WebRtcMediumImpl medium;
std::unique_ptr<api::WebRtcSignalingMessenger> messenger =
medium.GetSignalingMessenger("US", GetCountryCodeLocationHint("US"));
EXPECT_TRUE(messenger);
}
} // namespace windows
} // namespace nearby
} // namespace nearby::connections::mediums
+1 -13
View File
@@ -54,24 +54,12 @@ objc_library(
objc_library(
name = "apple_webrtc",
srcs = [
"webrtc.mm",
"webrtc_platform.mm",
],
hdrs = [
"webrtc.h",
],
deps = [
"//connections/implementation/mediums/webrtc:tachyon_express_signaling_messenger",
"//internal/platform:logging",
"//internal/platform:types",
"//connections/implementation/mediums/webrtc:webrtc_medium_impl",
"//internal/platform/implementation:webrtc_platform",
"//internal/proto:tachyon_cc_proto",
"//third_party/apple_frameworks:Foundation",
"//third_party/webrtc/files/stable/webrtc/api:create_modular_peer_connection_factory",
"//third_party/webrtc/files/stable/webrtc/api:peer_connection_interface",
"//third_party/webrtc/files/stable/webrtc/api/task_queue:default_task_queue_factory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
)
@@ -1,56 +0,0 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_IMPL_APPLE_WEBRTC_H_
#define PLATFORM_IMPL_APPLE_WEBRTC_H_
#ifndef NO_WEBRTC
#include <memory>
#include <optional>
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/webrtc.h"
#include "webrtc/api/peer_connection_interface.h"
namespace nearby::apple {
class WebRtcMedium : public api::WebRtcMedium {
public:
~WebRtcMedium() override = default;
// Creates and returns a new webrtc::PeerConnectionInterface object via
// |callback|.
void CreatePeerConnection(webrtc::PeerConnectionObserver* observer,
PeerConnectionCallback callback) override;
// Creates and returns a new webrtc::PeerConnectionInterface object via
// |callback| with |PeerConnectionFactoryInterface::Options|.
void CreatePeerConnection(
std::optional<webrtc::PeerConnectionFactoryInterface::Options> options,
webrtc::PeerConnectionObserver* observer,
PeerConnectionCallback callback) override;
// Returns a signaling messenger for sending WebRTC signaling messages.
std::unique_ptr<api::WebRtcSignalingMessenger> GetSignalingMessenger(
absl::string_view self_id,
const location::nearby::connections::LocationHint& location_hint)
override;
};
} // namespace nearby::apple
#endif // #ifndef NO_WEBRTC
#endif // PLATFORM_IMPL_APPLE_WEBRTC_H_
@@ -1,91 +0,0 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef NO_WEBRTC
#include "internal/platform/implementation/apple/webrtc.h"
#import <Foundation/Foundation.h>
#include <memory>
#include <optional>
#include <utility>
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "connections/implementation/mediums/webrtc/tachyon_express_signaling_messenger.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/crypto.h"
#include "internal/platform/logging.h"
#include "internal/proto/tachyon.pb.h"
#include "internal/proto/tachyon_enums.proto.h"
#include "webrtc/api/create_modular_peer_connection_factory.h"
#include "webrtc/api/task_queue/default_task_queue_factory.h"
namespace nearby::apple {
void WebRtcMedium::CreatePeerConnection(webrtc::PeerConnectionObserver* observer,
PeerConnectionCallback callback) {
CreatePeerConnection(std::nullopt, observer, std::move(callback));
}
void WebRtcMedium::CreatePeerConnection(
std::optional<webrtc::PeerConnectionFactoryInterface::Options> options,
webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) {
webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
rtc_config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
// TODO: b/261663238 - Add the TURN servers and go beyond the default servers.
webrtc::PeerConnectionInterface::IceServer ice_server;
ice_server.urls.emplace_back("stun:stun.l.google.com:19302");
ice_server.urls.emplace_back("stun:stun1.l.google.com:19302");
ice_server.urls.emplace_back("stun:stun2.l.google.com:19302");
ice_server.urls.emplace_back("stun:stun3.l.google.com:19302");
ice_server.urls.emplace_back("stun:stun4.l.google.com:19302");
rtc_config.servers.push_back(ice_server);
std::unique_ptr<webrtc::Thread> signaling_thread = webrtc::Thread::Create();
signaling_thread->SetName("signaling_thread", nullptr);
if (!signaling_thread->Start()) {
callback(/*peer_connection=*/nullptr);
return;
}
webrtc::PeerConnectionDependencies dependencies(observer);
webrtc::PeerConnectionFactoryDependencies factory_dependencies;
factory_dependencies.signaling_thread = signaling_thread.release();
webrtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory =
webrtc::CreateModularPeerConnectionFactory(std::move(factory_dependencies));
if (options.has_value()) {
peer_connection_factory->SetOptions(options.value());
}
webrtc::RTCErrorOr<webrtc::scoped_refptr<webrtc::PeerConnectionInterface>>
peer_connection_or_error =
peer_connection_factory->CreatePeerConnectionOrError(rtc_config, std::move(dependencies));
if (peer_connection_or_error.ok()) {
callback(peer_connection_or_error.MoveValue());
} else {
callback(/*peer_connection=*/nullptr);
}
}
std::unique_ptr<api::WebRtcSignalingMessenger> WebRtcMedium::GetSignalingMessenger(
absl::string_view self_id, const location::nearby::connections::LocationHint& location_hint) {
return std::make_unique<
nearby::connections::mediums::TachyonExpressSignalingMessenger>(self_id, location_hint);
}
} // namespace nearby::apple
#endif // #ifndef NO_WEBRTC
@@ -18,13 +18,13 @@
#include <memory>
#import "internal/platform/implementation/apple/webrtc.h"
#include "connections/implementation/mediums/webrtc/webrtc_medium_impl.h"
namespace nearby {
namespace api {
std::unique_ptr<WebRtcMedium> WebRtcImplementationPlatform::CreateWebRtcMedium() {
return std::make_unique<apple::WebRtcMedium>();
return std::make_unique<nearby::connections::mediums::WebRtcMediumImpl>();
}
std::string WebRtcImplementationPlatform::GetDefaultCountryCode() {
+4 -27
View File
@@ -233,21 +233,15 @@ cc_library(
)
cc_library(
name = "webrtc",
srcs = ["webrtc.cc"],
hdrs = ["webrtc.h"],
name = "webrtc_platform",
srcs = ["webrtc_platform.cc"],
tags = ["windows"],
deps = [
"//connections/implementation/mediums/webrtc:tachyon_express_signaling_messenger",
"//connections/implementation/mediums/webrtc:webrtc_medium_impl",
"//internal/platform:logging",
"//internal/platform/implementation:webrtc_platform",
"//third_party/webrtc/files/stable/webrtc/api:create_modular_peer_connection_factory",
"//third_party/webrtc/files/stable/webrtc/api:peer_connection_interface",
"//third_party/webrtc/files/stable/webrtc/api:rtc_error",
"//third_party/webrtc/files/stable/webrtc/api:scoped_refptr",
"//third_party/webrtc/files/stable/webrtc/rtc_base:threading",
"@com_google_absl//absl/strings",
],
alwayslink = True,
)
cc_library(
@@ -436,23 +430,6 @@ cc_proto_library(
deps = [":preferences_manager_test_proto"],
)
cc_test(
name = "webrtc_test",
size = "small",
srcs = ["webrtc_test.cc"],
deps = [
":webrtc",
"//internal/platform/implementation:comm",
"//internal/platform/implementation:platform_impl",
"//third_party/webrtc/files/stable/webrtc/api:data_channel_interface",
"//third_party/webrtc/files/stable/webrtc/api:jsep",
"//third_party/webrtc/files/stable/webrtc/api:peer_connection_interface",
"//third_party/webrtc/files/stable/webrtc/api:scoped_refptr",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "impl_test",
size = "small",
@@ -0,0 +1,47 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/webrtc_platform.h"
#include <winnls.h>
#include <memory>
#include <string>
#include "internal/platform/logging.h"
#include "internal/platform/implementation/webrtc.h"
#include "connections/implementation/mediums/webrtc/webrtc_medium_impl.h"
namespace nearby::api {
std::unique_ptr<WebRtcMedium>
WebRtcImplementationPlatform::CreateWebRtcMedium() {
return std::make_unique<nearby::connections::mediums::WebRtcMediumImpl>();
}
std::string WebRtcImplementationPlatform::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;
}
} // namespace nearby::api