mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Add WEB_RTC_NON_CELLULAR enum, III
PiperOrigin-RevId: 686391802
This commit is contained in:
committed by
Copybara-Service
parent
91a41a1de9
commit
77a75f4389
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "connections/core.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
+7
-1
@@ -15,21 +15,27 @@
|
||||
#ifndef CORE_CORE_H_
|
||||
#define CORE_CORE_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/span.h"
|
||||
#include "connections/advertising_options.h"
|
||||
#include "connections/connection_options.h"
|
||||
#include "connections/discovery_options.h"
|
||||
#include "connections/implementation/client_proxy.h"
|
||||
#include "connections/implementation/service_controller.h"
|
||||
#include "connections/implementation/service_controller_router.h"
|
||||
#include "connections/listeners.h"
|
||||
#include "connections/medium_selector.h"
|
||||
#include "connections/out_of_band_connection_metadata.h"
|
||||
#include "connections/params.h"
|
||||
#include "connections/payload.h"
|
||||
#include "connections/v3/advertising_options.h"
|
||||
#include "connections/v3/connection_listening_options.h"
|
||||
#include "connections/v3/connections_device_provider.h"
|
||||
#include "connections/v3/discovery_options.h"
|
||||
#include "connections/v3/listeners.h"
|
||||
#include "connections/v3/listening_result.h"
|
||||
|
||||
@@ -1253,6 +1253,16 @@ bool ClientProxy::IsMultiplexSocketSupported(absl::string_view endpoint_id,
|
||||
}
|
||||
}
|
||||
|
||||
bool ClientProxy::GetWebRtcNonCellular() { return webrtc_non_cellular_; }
|
||||
|
||||
void ClientProxy::SetWebRtcNonCellular(bool webrtc_non_cellular) {
|
||||
std::string allow_webrtc_cellular_str =
|
||||
webrtc_non_cellular ? "disallow" : "allow";
|
||||
NEARBY_LOGS(INFO) << "ClientProxy: client=" << GetClientId()
|
||||
<< allow_webrtc_cellular_str << " to use mobile data.",
|
||||
webrtc_non_cellular_ = webrtc_non_cellular;
|
||||
}
|
||||
|
||||
std::string ClientProxy::ToString(PayloadProgressInfo::Status status) const {
|
||||
switch (status) {
|
||||
case PayloadProgressInfo::Status::kSuccess:
|
||||
|
||||
@@ -323,6 +323,12 @@ class ClientProxy final {
|
||||
// Returns true if the multiplex socket is supported for the given medium.
|
||||
bool IsMultiplexSocketSupported(absl::string_view endpoint_id, Medium medium);
|
||||
|
||||
// Gets the WebRTC non cellular network status.
|
||||
bool GetWebRtcNonCellular();
|
||||
|
||||
// Sets the WebRTC non cellular network status.
|
||||
void SetWebRtcNonCellular(bool webrtc_non_cellular);
|
||||
|
||||
/** Bitmask for bt multiplex connection support. */
|
||||
// Note. Deprecates the first and second bit of BT_MULTIPLEX_ENABLED and
|
||||
// WIFI_LAN_MULTIPLEX_ENABLED and shift them to the third and the forth bit.
|
||||
@@ -502,6 +508,8 @@ class ClientProxy final {
|
||||
bool supports_safe_to_disconnect_;
|
||||
bool support_auto_reconnect_;
|
||||
std::int32_t local_safe_to_disconnect_version_;
|
||||
// Allowed to use WebRTC over non-cellular networks.
|
||||
bool webrtc_non_cellular_ = false;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -162,6 +162,7 @@ cc_test(
|
||||
":mediums",
|
||||
":utils",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:cancellation_flag",
|
||||
"//internal/platform:comm",
|
||||
"//internal/platform:test_util",
|
||||
"//internal/platform:types",
|
||||
|
||||
@@ -18,19 +18,27 @@
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "absl/functional/bind_front.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "connections/implementation/mediums/webrtc/connection_flow.h"
|
||||
#include "connections/implementation/mediums/webrtc/session_description_wrapper.h"
|
||||
#include "connections/implementation/mediums/webrtc/signaling_frames.h"
|
||||
#include "connections/implementation/mediums/webrtc_peer_id.h"
|
||||
#include "connections/implementation/mediums/webrtc_socket.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/cancelable_alarm.h"
|
||||
#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/logging.h"
|
||||
#include "internal/platform/mutex_lock.h"
|
||||
#include "internal/platform/webrtc.h"
|
||||
#include "webrtc/api/jsep.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -86,7 +94,8 @@ bool WebRtc::IsAcceptingConnectionsLocked(const std::string& service_id) {
|
||||
bool WebRtc::StartAcceptingConnections(const std::string& service_id,
|
||||
const WebrtcPeerId& self_peer_id,
|
||||
const LocationHint& location_hint,
|
||||
AcceptedConnectionCallback callback) {
|
||||
AcceptedConnectionCallback callback,
|
||||
bool non_cellular) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (!IsAvailable()) {
|
||||
NEARBY_LOGS(WARNING) << "Cannot start accepting WebRTC connections because "
|
||||
@@ -107,6 +116,8 @@ bool WebRtc::StartAcceptingConnections(const std::string& service_id,
|
||||
info.self_peer_id = self_peer_id;
|
||||
info.accepted_connection_callback = std::move(callback);
|
||||
|
||||
medium_->SetNonCellular(non_cellular);
|
||||
|
||||
// Create a new SignalingMessenger so that we can communicate w/ Tachyon.
|
||||
info.signaling_messenger =
|
||||
medium_->GetSignalingMessenger(self_peer_id.GetId(), location_hint);
|
||||
@@ -197,8 +208,10 @@ void WebRtc::StopAcceptingConnections(const std::string& service_id) {
|
||||
WebRtcSocketWrapper WebRtc::Connect(const std::string& service_id,
|
||||
const WebrtcPeerId& remote_peer_id,
|
||||
const LocationHint& location_hint,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
CancellationFlag* cancellation_flag,
|
||||
bool non_cellular) {
|
||||
service_id_to_connect_attempts_count_map_[service_id] = 1;
|
||||
medium_->SetNonCellular(non_cellular);
|
||||
while (service_id_to_connect_attempts_count_map_[service_id] <=
|
||||
kConnectAttemptsLimit) {
|
||||
if (cancellation_flag->Cancelled()) {
|
||||
|
||||
@@ -20,10 +20,13 @@
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "connections/implementation/mediums/webrtc/connection_flow.h"
|
||||
#include "connections/implementation/mediums/webrtc/session_description_wrapper.h"
|
||||
#include "connections/implementation/mediums/webrtc_peer_id.h"
|
||||
#include "connections/implementation/mediums/webrtc_socket.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
@@ -70,7 +73,8 @@ class WebRtc {
|
||||
bool StartAcceptingConnections(
|
||||
const std::string& service_id, const WebrtcPeerId& self_peer_id,
|
||||
const location::nearby::connections::LocationHint& location_hint,
|
||||
AcceptedConnectionCallback callback) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
AcceptedConnectionCallback callback, bool non_cellular)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Try to stop (accepting) the specific connection with provided service id.
|
||||
// Runs on @MainThread
|
||||
@@ -83,7 +87,8 @@ class WebRtc {
|
||||
WebRtcSocketWrapper Connect(
|
||||
const std::string& service_id, const WebrtcPeerId& peer_id,
|
||||
const location::nearby::connections::LocationHint& location_hint,
|
||||
CancellationFlag* cancellation_flag) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
CancellationFlag* cancellation_flag, bool non_cellular)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
protected:
|
||||
// Use for unit tests only to inject a WebRtcMedium.
|
||||
|
||||
@@ -21,10 +21,14 @@
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "connections/implementation/mediums/webrtc_peer_id.h"
|
||||
#include "connections/implementation/mediums/webrtc_socket.h"
|
||||
#include "internal/platform/listeners.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/feature_flags.h"
|
||||
#include "internal/platform/future.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
#include "internal/platform/mutex_lock.h"
|
||||
#include "internal/platform/webrtc.h"
|
||||
#include "internal/test/fake_webrtc.h"
|
||||
|
||||
@@ -37,13 +41,9 @@ namespace {
|
||||
using FeatureFlags = FeatureFlags::Flags;
|
||||
using ::location::nearby::connections::LocationHint;
|
||||
|
||||
constexpr FeatureFlags kTestCases[] = {
|
||||
FeatureFlags{
|
||||
.enable_cancellation_flag = true,
|
||||
},
|
||||
FeatureFlags{
|
||||
.enable_cancellation_flag = false,
|
||||
},
|
||||
struct WebRtcTestParams {
|
||||
FeatureFlags feature_flags;
|
||||
bool non_cellular;
|
||||
};
|
||||
|
||||
class TestWebRtc : public WebRtc {
|
||||
@@ -56,7 +56,7 @@ class TestWebRtc : public WebRtc {
|
||||
}
|
||||
};
|
||||
|
||||
class WebRtcTest : public ::testing::TestWithParam<FeatureFlags> {
|
||||
class WebRtcTest : public ::testing::TestWithParam<WebRtcTestParams> {
|
||||
protected:
|
||||
using MockAcceptedCallback = testing::MockFunction<void(
|
||||
const std::string& service_id, WebRtcSocketWrapper socket)>;
|
||||
@@ -68,8 +68,8 @@ class WebRtcTest : public ::testing::TestWithParam<FeatureFlags> {
|
||||
// other but the signaling channel is closed before sending the data.
|
||||
TEST_P(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
WebRtcTestParams params = GetParam();
|
||||
env_.SetFeatureFlags(params.feature_flags);
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const WebrtcPeerId self_id("self_id");
|
||||
@@ -84,10 +84,12 @@ TEST_P(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) {
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
});
|
||||
},
|
||||
params.non_cellular);
|
||||
|
||||
CancellationFlag flag;
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag,
|
||||
params.non_cellular);
|
||||
EXPECT_TRUE(sender_socket.IsValid());
|
||||
|
||||
ExceptionOr<bool> devices_connected = connected.Get();
|
||||
@@ -107,8 +109,8 @@ TEST_P(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) {
|
||||
|
||||
TEST_P(WebRtcTest, CanCancelConnect) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
FeatureFlags feature_flags = GetParam();
|
||||
env_.SetFeatureFlags(feature_flags);
|
||||
WebRtcTestParams params = GetParam();
|
||||
env_.SetFeatureFlags(params.feature_flags);
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const WebrtcPeerId self_id("self_id");
|
||||
@@ -123,12 +125,14 @@ TEST_P(WebRtcTest, CanCancelConnect) {
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
});
|
||||
},
|
||||
params.non_cellular);
|
||||
|
||||
CancellationFlag flag(true);
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag,
|
||||
params.non_cellular);
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
if (!params.feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(sender_socket.IsValid());
|
||||
|
||||
ExceptionOr<bool> devices_connected = connected.Get();
|
||||
@@ -148,11 +152,8 @@ TEST_P(WebRtcTest, CanCancelConnect) {
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(ParametrisedWebRtcTest, WebRtcTest,
|
||||
::testing::ValuesIn(kTestCases));
|
||||
|
||||
// Basic test to check that device is accepting connections when initialized.
|
||||
TEST_F(WebRtcTest, NotAcceptingConnections) {
|
||||
TEST_P(WebRtcTest, NotAcceptingConnections) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtc webrtc;
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
@@ -162,8 +163,9 @@ TEST_F(WebRtcTest, NotAcceptingConnections) {
|
||||
|
||||
// Tests the flow when the device tries to accept connections twice. In this
|
||||
// case, only the first call is successful and subsequent calls fail.
|
||||
TEST_F(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
TEST_P(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtcTestParams params = GetParam();
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
WebRtc webrtc;
|
||||
WebrtcPeerId self_id("peer_id");
|
||||
@@ -173,10 +175,10 @@ TEST_F(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
ASSERT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
mock_accepted_callback_.AsStdFunction(), params.non_cellular));
|
||||
EXPECT_FALSE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
mock_accepted_callback_.AsStdFunction(), params.non_cellular));
|
||||
EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id));
|
||||
EXPECT_FALSE(webrtc.IsAcceptingConnections(std::string{}));
|
||||
env_.Stop();
|
||||
@@ -184,8 +186,9 @@ TEST_F(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
|
||||
// Tests the flow when the device tries to connect but there is no peer
|
||||
// accepting connections at the given peer ID.
|
||||
TEST_F(WebRtcTest, Connect_NoPeer) {
|
||||
TEST_P(WebRtcTest, Connect_NoPeer) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtcTestParams params = GetParam();
|
||||
WebRtc webrtc;
|
||||
WebrtcPeerId peer_id("peer_id");
|
||||
const std::string service_id("NearbySharing");
|
||||
@@ -193,20 +196,21 @@ TEST_F(WebRtcTest, Connect_NoPeer) {
|
||||
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
CancellationFlag flag;
|
||||
WebRtcSocketWrapper wrapper_1 =
|
||||
webrtc.Connect(service_id, peer_id, location_hint, &flag);
|
||||
WebRtcSocketWrapper wrapper_1 = webrtc.Connect(
|
||||
service_id, peer_id, location_hint, &flag, params.non_cellular);
|
||||
EXPECT_FALSE(wrapper_1.IsValid());
|
||||
|
||||
EXPECT_TRUE(webrtc.StartAcceptingConnections(service_id, peer_id,
|
||||
location_hint, nullptr));
|
||||
EXPECT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, peer_id, location_hint, nullptr, params.non_cellular));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
// Tests the flow when the device calls Connect() after calling
|
||||
// StartAcceptingConnections() without StopAcceptingConnections().
|
||||
TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
TEST_P(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
WebRtcTestParams params = GetParam();
|
||||
WebRtc webrtc;
|
||||
WebrtcPeerId self_id("peer_id");
|
||||
const std::string service_id("NearbySharing");
|
||||
@@ -215,23 +219,25 @@ TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
ASSERT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
mock_accepted_callback_.AsStdFunction(), params.non_cellular));
|
||||
CancellationFlag flag;
|
||||
WebRtcSocketWrapper wrapper = webrtc.Connect(
|
||||
service_id, WebrtcPeerId("random_peer_id"), location_hint, &flag);
|
||||
WebRtcSocketWrapper wrapper =
|
||||
webrtc.Connect(service_id, WebrtcPeerId("random_peer_id"), location_hint,
|
||||
&flag, params.non_cellular);
|
||||
EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id));
|
||||
EXPECT_FALSE(wrapper.IsValid());
|
||||
EXPECT_FALSE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
mock_accepted_callback_.AsStdFunction(), params.non_cellular));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
// Tests the flow when the device calls StartAcceptingConnections but the medium
|
||||
// is closed before a peer device can connect to it.
|
||||
TEST_F(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
TEST_P(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
WebRtcTestParams params = GetParam();
|
||||
WebRtc webrtc;
|
||||
WebrtcPeerId self_id("peer_id");
|
||||
const std::string service_id("NearbySharing");
|
||||
@@ -240,7 +246,7 @@ TEST_F(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
ASSERT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
mock_accepted_callback_.AsStdFunction(), params.non_cellular));
|
||||
EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id));
|
||||
webrtc.StopAcceptingConnections(service_id);
|
||||
EXPECT_FALSE(webrtc.IsAcceptingConnections(service_id));
|
||||
@@ -249,10 +255,11 @@ TEST_F(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
|
||||
// Tests the flow when the device tries to connect to two different peers
|
||||
// without disconnecting in between.
|
||||
TEST_F(WebRtcTest, ConnectTwice) {
|
||||
TEST_P(WebRtcTest, ConnectTwice) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtc receiver, sender, device_c;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
WebRtcTestParams params = GetParam();
|
||||
const WebrtcPeerId self_id("self_id"), other_id("other_id");
|
||||
const std::string service_id("NearbySharing");
|
||||
LocationHint location_hint;
|
||||
@@ -265,22 +272,25 @@ TEST_F(WebRtcTest, ConnectTwice) {
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
});
|
||||
},
|
||||
params.non_cellular);
|
||||
|
||||
device_c.StartAcceptingConnections(
|
||||
service_id, other_id, location_hint,
|
||||
[](const std::string& service_id, WebRtcSocketWrapper wrapper) {});
|
||||
[](const std::string& service_id, WebRtcSocketWrapper wrapper) {},
|
||||
params.non_cellular);
|
||||
|
||||
CancellationFlag flag;
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag,
|
||||
params.non_cellular);
|
||||
EXPECT_TRUE(sender_socket.IsValid());
|
||||
|
||||
ExceptionOr<bool> devices_connected = connected.Get();
|
||||
ASSERT_TRUE(devices_connected.ok());
|
||||
EXPECT_TRUE(devices_connected.result());
|
||||
|
||||
WebRtcSocketWrapper socket =
|
||||
sender.Connect(service_id, other_id, location_hint, &flag);
|
||||
WebRtcSocketWrapper socket = sender.Connect(
|
||||
service_id, other_id, location_hint, &flag, params.non_cellular);
|
||||
EXPECT_TRUE(socket.IsValid());
|
||||
socket.Close();
|
||||
|
||||
@@ -299,10 +309,11 @@ TEST_F(WebRtcTest, ConnectTwice) {
|
||||
|
||||
// Tests the flow when the two devices exchange SDP messages and connect to each
|
||||
// other but disconnect before being able to send/receive the actual data.
|
||||
TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
TEST_P(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
WebRtcTestParams params = GetParam();
|
||||
const WebrtcPeerId self_id("self_id");
|
||||
const std::string service_id("NearbySharing");
|
||||
LocationHint location_hint;
|
||||
@@ -315,10 +326,12 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
});
|
||||
},
|
||||
params.non_cellular);
|
||||
|
||||
CancellationFlag flag;
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag,
|
||||
params.non_cellular);
|
||||
EXPECT_TRUE(sender_socket.IsValid());
|
||||
|
||||
ExceptionOr<bool> devices_connected = connected.Get();
|
||||
@@ -331,10 +344,11 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
|
||||
// Tests the flow when the two devices exchange SDP messages and connect to each
|
||||
// other and the actual data is exchanged successfully between the devices.
|
||||
TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
TEST_P(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
WebRtcTestParams params = GetParam();
|
||||
const WebrtcPeerId self_id("self_id");
|
||||
const std::string service_id("NearbySharing");
|
||||
LocationHint location_hint;
|
||||
@@ -347,10 +361,12 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
});
|
||||
},
|
||||
params.non_cellular);
|
||||
|
||||
CancellationFlag flag;
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag,
|
||||
params.non_cellular);
|
||||
EXPECT_TRUE(sender_socket.IsValid());
|
||||
|
||||
ExceptionOr<bool> devices_connected = connected.Get();
|
||||
@@ -367,8 +383,9 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WebRtcTest, Connect_NullPeerConnection) {
|
||||
TEST_P(WebRtcTest, Connect_NullPeerConnection) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtcTestParams params = GetParam();
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
env_.SetUseValidPeerConnection(
|
||||
/*use_valid_peer_connection=*/false);
|
||||
@@ -380,17 +397,19 @@ TEST_F(WebRtcTest, Connect_NullPeerConnection) {
|
||||
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
CancellationFlag flag;
|
||||
WebRtcSocketWrapper wrapper = webrtc.Connect(
|
||||
service_id, WebrtcPeerId("random_peer_id"), location_hint, &flag);
|
||||
WebRtcSocketWrapper wrapper =
|
||||
webrtc.Connect(service_id, WebrtcPeerId("random_peer_id"), location_hint,
|
||||
&flag, params.non_cellular);
|
||||
EXPECT_FALSE(wrapper.IsValid());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
// Tests the flow when the device calls StartAcceptingConnections and the
|
||||
// receive messages stream fails.
|
||||
TEST_F(WebRtcTest, ContinueAcceptingConnectionsOnComplete) {
|
||||
TEST_P(WebRtcTest, ContinueAcceptingConnectionsOnComplete) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
WebRtcTestParams params = GetParam();
|
||||
WebRtc webrtc;
|
||||
WebrtcPeerId self_id("peer_id");
|
||||
const std::string service_id("NearbySharing");
|
||||
@@ -399,7 +418,7 @@ TEST_F(WebRtcTest, ContinueAcceptingConnectionsOnComplete) {
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
ASSERT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
mock_accepted_callback_.AsStdFunction(), params.non_cellular));
|
||||
EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id));
|
||||
|
||||
// Simulate a failure in receiving messages stream, WebRtc should restart
|
||||
@@ -420,11 +439,14 @@ TEST_F(WebRtcTest, ContinueAcceptingConnectionsOnComplete) {
|
||||
|
||||
// Tests when a CancellationFlag is cancelled during an attempt to
|
||||
// `WebRtc::AttemptToConnect` triggered by `WebRtc::Connect`.
|
||||
TEST_F(WebRtcTest, CancelDuringConnect) {
|
||||
TEST_P(WebRtcTest, CancelDuringConnect) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtcTestParams params = GetParam();
|
||||
|
||||
// Enable cancellation flags.
|
||||
env_.SetFeatureFlags(kTestCases[0]);
|
||||
env_.SetFeatureFlags(FeatureFlags{
|
||||
.enable_cancellation_flag = true,
|
||||
});
|
||||
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const WebrtcPeerId self_id("self_id");
|
||||
@@ -454,10 +476,11 @@ TEST_F(WebRtcTest, CancelDuringConnect) {
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
});
|
||||
},
|
||||
params.non_cellular);
|
||||
|
||||
sender_socket =
|
||||
sender->Connect(service_id, self_id, location_hint, &sender_flag);
|
||||
sender_socket = sender->Connect(service_id, self_id, location_hint,
|
||||
&sender_flag, params.non_cellular);
|
||||
|
||||
// Since the flag was cancelled during the initial `AttemptToConnect`, except
|
||||
// only one attempt instead of the usual three, because the cancellation flag
|
||||
@@ -473,11 +496,14 @@ TEST_F(WebRtcTest, CancelDuringConnect) {
|
||||
|
||||
// Tests when a CancellationFlag is cancelled before `WebRtc::Connect` is
|
||||
// called.
|
||||
TEST_F(WebRtcTest, CancelBeforeConnect) {
|
||||
TEST_P(WebRtcTest, CancelBeforeConnect) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtcTestParams params = GetParam();
|
||||
|
||||
// Enable cancellation flags.
|
||||
env_.SetFeatureFlags(kTestCases[0]);
|
||||
env_.SetFeatureFlags(FeatureFlags{
|
||||
.enable_cancellation_flag = true,
|
||||
});
|
||||
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const WebrtcPeerId self_id("self_id");
|
||||
@@ -500,10 +526,11 @@ TEST_F(WebRtcTest, CancelBeforeConnect) {
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
});
|
||||
},
|
||||
params.non_cellular);
|
||||
|
||||
sender_socket =
|
||||
sender->Connect(service_id, self_id, location_hint, &sender_flag);
|
||||
sender_socket = sender->Connect(service_id, self_id, location_hint,
|
||||
&sender_flag, params.non_cellular);
|
||||
|
||||
// Expect an invalid socket from stopping during the first attempt to connect,
|
||||
// because `Connect` returned immediatley when it checked for cancellation.
|
||||
@@ -516,11 +543,14 @@ TEST_F(WebRtcTest, CancelBeforeConnect) {
|
||||
// Tests when a CancellationFlag is cancelled during an attempt to
|
||||
// `WebRtc::AttemptToConnect` triggered by `WebRtc::Connect` when multiple
|
||||
// `WebRTC::Connect` calls are in flight for multiple service ids.
|
||||
TEST_F(WebRtcTest, CancelDuringConnect_MultipleConnect) {
|
||||
TEST_P(WebRtcTest, CancelDuringConnect_MultipleConnect) {
|
||||
env_.Start({.webrtc_enabled = true});
|
||||
WebRtcTestParams params = GetParam();
|
||||
|
||||
// Enable cancellation flags.
|
||||
env_.SetFeatureFlags(kTestCases[0]);
|
||||
env_.SetFeatureFlags(FeatureFlags{
|
||||
.enable_cancellation_flag = true,
|
||||
});
|
||||
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const WebrtcPeerId self_id("self_id");
|
||||
@@ -545,17 +575,20 @@ TEST_F(WebRtcTest, CancelDuringConnect_MultipleConnect) {
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
});
|
||||
},
|
||||
params.non_cellular);
|
||||
|
||||
// Simulate a successful connect for the endpoint of NearbySharing.
|
||||
sender_socket = sender->Connect(ns_service_id, self_id, location_hint, &flag);
|
||||
sender_socket = sender->Connect(ns_service_id, self_id, location_hint, &flag,
|
||||
params.non_cellular);
|
||||
EXPECT_TRUE(sender_socket.IsValid());
|
||||
|
||||
// Calls `CancellationFlag::Cancel` during a call to `GetSignalingMessenger`
|
||||
// to simulate the cancellation occuring during an `AttemptToConnect` for the
|
||||
// endpoint of Phone Hub.
|
||||
fake_sender_medium->TriggerCancellationDuringGetSignalingMessenger();
|
||||
sender_socket = sender->Connect(ph_service_id, self_id, location_hint, &flag);
|
||||
sender_socket = sender->Connect(ph_service_id, self_id, location_hint, &flag,
|
||||
params.non_cellular);
|
||||
EXPECT_FALSE(sender_socket.IsValid());
|
||||
|
||||
// Since the flag was cancelled during the initial `AttemptToConnect`, except
|
||||
@@ -571,6 +604,30 @@ TEST_F(WebRtcTest, CancelDuringConnect_MultipleConnect) {
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(ParametrisedWebRtcTest, WebRtcTest,
|
||||
testing::ValuesIn<WebRtcTestParams>({
|
||||
{.feature_flags =
|
||||
FeatureFlags{
|
||||
.enable_cancellation_flag = true,
|
||||
},
|
||||
.non_cellular = true},
|
||||
{.feature_flags =
|
||||
FeatureFlags{
|
||||
.enable_cancellation_flag = true,
|
||||
},
|
||||
.non_cellular = false},
|
||||
{.feature_flags =
|
||||
FeatureFlags{
|
||||
.enable_cancellation_flag = false,
|
||||
},
|
||||
.non_cellular = true},
|
||||
{.feature_flags =
|
||||
FeatureFlags{
|
||||
.enable_cancellation_flag = false,
|
||||
},
|
||||
.non_cellular = false},
|
||||
}));
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace mediums
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#include "connections/implementation/pcp.h"
|
||||
#include "connections/implementation/pcp_handler.h"
|
||||
#include "connections/listeners.h"
|
||||
#include "connections/medium_selector.h"
|
||||
#include "connections/out_of_band_connection_metadata.h"
|
||||
#include "connections/params.h"
|
||||
#include "connections/status.h"
|
||||
@@ -85,6 +86,9 @@ Status PcpManager::StartAdvertising(
|
||||
return {Status::kError};
|
||||
}
|
||||
|
||||
client->SetWebRtcNonCellular(GetWebRtcNonCellular(
|
||||
advertising_options.CompatibleOptions().allowed.GetMediums(true)));
|
||||
|
||||
return current_->StartAdvertising(client, service_id, advertising_options,
|
||||
info);
|
||||
}
|
||||
@@ -147,6 +151,9 @@ Status PcpManager::RequestConnection(
|
||||
return {Status::kOutOfOrderApiCall};
|
||||
}
|
||||
|
||||
client->SetWebRtcNonCellular(
|
||||
GetWebRtcNonCellular(connection_options.GetMediums()));
|
||||
|
||||
return current_->RequestConnection(client, endpoint_id, info,
|
||||
connection_options);
|
||||
}
|
||||
@@ -220,5 +227,14 @@ PcpHandler* PcpManager::GetPcpHandler(Pcp pcp) const {
|
||||
return item != handlers_.end() ? item->second.get() : nullptr;
|
||||
}
|
||||
|
||||
bool PcpManager::GetWebRtcNonCellular(const std::vector<Medium>& mediums) {
|
||||
for (const auto& medium : mediums) {
|
||||
if (medium == Medium::WEB_RTC_NON_CELLULAR) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
|
||||
@@ -108,6 +108,7 @@ class PcpManager {
|
||||
private:
|
||||
bool SetCurrentPcpHandler(Strategy strategy);
|
||||
PcpHandler* GetPcpHandler(Pcp pcp) const;
|
||||
bool GetWebRtcNonCellular(const std::vector<Medium>& mediums);
|
||||
|
||||
AtomicBoolean shutdown_{false};
|
||||
absl::flat_hash_map<Pcp, std::unique_ptr<BasePcpHandler>> handlers_;
|
||||
|
||||
@@ -12,21 +12,26 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#ifndef NO_WEBRTC
|
||||
|
||||
#include "connections/implementation/webrtc_bwu_handler.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/functional/bind_front.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "connections/implementation/base_bwu_handler.h"
|
||||
#include "connections/implementation/client_proxy.h"
|
||||
#include "connections/implementation/endpoint_channel.h"
|
||||
#include "connections/implementation/mediums/mediums.h"
|
||||
#include "connections/implementation/mediums/utils.h"
|
||||
#include "connections/implementation/mediums/webrtc_peer_id.h"
|
||||
#include "connections/implementation/mediums/webrtc_socket.h"
|
||||
#include "connections/implementation/offline_frames.h"
|
||||
#include "connections/implementation/webrtc_endpoint_channel.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -67,9 +72,9 @@ WebrtcBwuHandler::CreateUpgradedEndpointChannel(
|
||||
<< peer_id.GetId() << ", location hint "
|
||||
<< absl::StrCat(location_hint.location());
|
||||
|
||||
mediums::WebRtcSocketWrapper socket =
|
||||
webrtc_.Connect(service_id, peer_id, location_hint,
|
||||
client->GetCancellationFlag(endpoint_id));
|
||||
mediums::WebRtcSocketWrapper socket = webrtc_.Connect(
|
||||
service_id, peer_id, location_hint,
|
||||
client->GetCancellationFlag(endpoint_id), client->GetWebRtcNonCellular());
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOGS(ERROR) << "WebRtcBwuHandler failed to connect to remote peer ("
|
||||
<< peer_id.GetId() << ") on endpoint " << endpoint_id
|
||||
@@ -117,7 +122,8 @@ ByteArray WebrtcBwuHandler::HandleInitializeUpgradedMediumForEndpoint(
|
||||
if (!webrtc_.StartAcceptingConnections(
|
||||
upgrade_service_id, self_id, location_hint,
|
||||
absl::bind_front(&WebrtcBwuHandler::OnIncomingWebrtcConnection,
|
||||
this, client))) {
|
||||
this, client),
|
||||
client->GetWebRtcNonCellular())) {
|
||||
NEARBY_LOGS(ERROR) << "WebRtcBwuHandler couldn't initiate the WEB_RTC "
|
||||
"upgrade for endpoint "
|
||||
<< endpoint_id
|
||||
|
||||
@@ -17,13 +17,18 @@
|
||||
|
||||
#ifndef NO_WEBRTC
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "connections/implementation/base_bwu_handler.h"
|
||||
#include "connections/implementation/bwu_handler.h"
|
||||
#include "connections/implementation/client_proxy.h"
|
||||
#include "connections/implementation/endpoint_channel_manager.h"
|
||||
#include "connections/implementation/endpoint_channel.h"
|
||||
#include "connections/implementation/mediums/mediums.h"
|
||||
#include "connections/implementation/mediums/webrtc.h"
|
||||
#include "connections/implementation/mediums/webrtc_socket.h"
|
||||
#include "connections/medium_selector.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
@@ -95,6 +95,7 @@ cc_library(
|
||||
"//internal/platform/implementation/shared:count_down_latch",
|
||||
"//internal/proto:credential_cc_proto",
|
||||
# TODO: Support WebRTC
|
||||
"//third_party/webrtc/files/stable/webrtc/api:scoped_refptr",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
"@com_google_absl//absl/container:flat_hash_map",
|
||||
"@com_google_absl//absl/container:flat_hash_set",
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "internal/platform/implementation/webrtc.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
#include "webrtc/api/peer_connection_interface.h"
|
||||
#include "webrtc/api/scoped_refptr.h"
|
||||
#include "webrtc/api/task_queue/default_task_queue_factory.h"
|
||||
#include "webrtc/rtc_base/checks.h"
|
||||
|
||||
@@ -88,13 +89,17 @@ void WebRtcMedium::CreatePeerConnection(
|
||||
webrtc::CreateDefaultTaskQueueFactory();
|
||||
factory_dependencies.signaling_thread = signaling_thread.release();
|
||||
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
|
||||
peer_connection_factory = webrtc::CreateModularPeerConnectionFactory(
|
||||
std::move(factory_dependencies));
|
||||
RTC_CHECK(peer_connection_factory != nullptr)
|
||||
<< "Failed to create peer connection factory";
|
||||
if (options.has_value()) {
|
||||
// TODO(edwinwu): Add support for non-cellular networks.
|
||||
peer_connection_factory->SetOptions(options.value());
|
||||
}
|
||||
auto peer_connection_or_error =
|
||||
webrtc::CreateModularPeerConnectionFactory(
|
||||
std::move(factory_dependencies))
|
||||
->CreatePeerConnectionOrError(rtc_config, std::move(dependencies));
|
||||
peer_connection_factory->CreatePeerConnectionOrError(
|
||||
rtc_config, std::move(dependencies));
|
||||
RTC_CHECK(peer_connection_or_error.ok())
|
||||
<< "Failed to create peer connection";
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "internal/platform/implementation/webrtc.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "webrtc/api/peer_connection_interface.h"
|
||||
#include "webrtc/api/scoped_refptr.h"
|
||||
#include "webrtc/api/task_queue/default_task_queue_factory.h"
|
||||
#include "webrtc/rtc_base/thread.h"
|
||||
|
||||
@@ -101,14 +102,15 @@ void WebRtcMedium::CreatePeerConnection(
|
||||
webrtc::CreateDefaultTaskQueueFactory();
|
||||
factory_dependencies.signaling_thread = signaling_thread.release();
|
||||
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
|
||||
peer_connection_factory = webrtc::CreateModularPeerConnectionFactory(
|
||||
std::move(factory_dependencies));
|
||||
if (options.has_value()) {
|
||||
// TODO(edwinwu): Add support for non-cellular networks.
|
||||
peer_connection_factory->SetOptions(options.value());
|
||||
}
|
||||
auto peer_connection_or_error =
|
||||
webrtc::CreateModularPeerConnectionFactory(
|
||||
std::move(factory_dependencies))
|
||||
->CreatePeerConnectionOrError(rtc_config, std::move(dependencies));
|
||||
|
||||
peer_connection_factory->CreatePeerConnectionOrError(
|
||||
rtc_config, std::move(dependencies));
|
||||
if (peer_connection_or_error.ok()) {
|
||||
callback(peer_connection_or_error.MoveValue());
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user