[Nearby Connection] Add webrtc_stub.*, BUILD.bazel and rename webrtc_socket_wrapper.* => webrtc_socket.* & webrtc_socket.* => webrtc_socket_impl.*.

PiperOrigin-RevId: 408278672
This commit is contained in:
suetfei
2021-11-08 01:43:53 -08:00
committed by Copybara-Service
parent 3ad0700559
commit 1b4ae4b28a
25 changed files with 593 additions and 27 deletions
+1 -1
View File
@@ -74,7 +74,7 @@ cc_library(
hdrs = [
"utils.h",
"webrtc_peer_id.h",
"webrtc_socket_wrapper.h",
"webrtc_socket.h",
],
compatible_with = ["//buildenv/target:non_prod"],
visibility = [
+118
View File
@@ -0,0 +1,118 @@
# Copyright 2020 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.
licenses(["notice"])
cc_library(
name = "mediums",
srcs = [
"ble.cc",
"bloom_filter.cc",
"bluetooth_classic.cc",
"bluetooth_radio.cc",
"mediums.cc",
"uuid.cc",
"webrtc_stub.cc",
"wifi_lan.cc",
],
hdrs = [
"ble.h",
"bloom_filter.h",
"bluetooth_classic.h",
"bluetooth_radio.h",
"lost_entity_tracker.h",
"mediums.h",
"uuid.h",
"webrtc_stub.h",
"wifi_lan.h",
],
compatible_with = ["//buildenv/target:non_prod"],
defines = ["NO_WEBRTC"],
visibility = [
"//third_party/nearby_connections/cpp/core/internal:__subpackages__",
],
deps = [
":utils",
"//third_party/absl/container:flat_hash_map",
"//third_party/absl/container:flat_hash_set",
"//third_party/absl/functional:bind_front",
"//third_party/absl/numeric:int128",
"//third_party/absl/strings",
"//third_party/absl/strings:str_format",
"//third_party/absl/time",
"//third_party/nearby_connections/cpp/core:core_types",
"//third_party/nearby_connections/cpp/core/internal/mediums/ble_v2",
"//third_party/nearby_connections/cpp/platform/base",
"//third_party/nearby_connections/cpp/platform/base:cancellation_flag",
"//third_party/nearby_connections/cpp/platform/public:comm",
"//third_party/nearby_connections/cpp/platform/public:logging",
"//third_party/nearby_connections/cpp/platform/public:types",
"//third_party/nearby_connections/proto/connections:offline_wire_formats_portable_proto",
"//third_party/nearby_connections/proto/mediums:web_rtc_signaling_frames_cc_proto",
"//third_party/smhasher:libmurmur3",
],
)
cc_library(
name = "utils",
srcs = [
"utils.cc",
"webrtc_peer_id.cc",
],
hdrs = [
"utils.h",
"webrtc_peer_id.h",
"webrtc_socket_stub.h",
],
compatible_with = ["//buildenv/target:non_prod"],
defines = ["NO_WEBRTC"],
visibility = [
"//third_party/nearby_connections/cpp/core/internal:__pkg__",
"//third_party/nearby_connections/cpp/core/internal/mediums:__pkg__",
],
deps = [
"//third_party/absl/strings",
"//third_party/nearby_connections/cpp/platform/base",
"//third_party/nearby_connections/cpp/platform/public:types",
"//third_party/nearby_connections/proto/connections:offline_wire_formats_portable_proto",
],
)
cc_test(
name = "core_internal_mediums_test",
size = "small",
srcs = [
"ble_test.cc",
"bloom_filter_test.cc",
"bluetooth_classic_test.cc",
"bluetooth_radio_test.cc",
"lost_entity_tracker_test.cc",
"uuid_test.cc",
"webrtc_peer_id_test.cc",
"wifi_lan_test.cc",
],
defines = ["NO_WEBRTC"],
shard_count = 16,
deps = [
":mediums",
":utils",
"//testing/base/public:gunit_main",
"//third_party/absl/strings",
"//third_party/absl/time",
"//third_party/nearby_connections/cpp/platform/base",
"//third_party/nearby_connections/cpp/platform/base:test_util",
"//third_party/nearby_connections/cpp/platform/impl/g3", # build_cleaner: keep
"//third_party/nearby_connections/cpp/platform/public:comm",
"//third_party/nearby_connections/cpp/platform/public:types",
],
)
+4
View File
@@ -18,7 +18,11 @@
#include "core/internal/mediums/ble.h"
#include "core/internal/mediums/bluetooth_classic.h"
#include "core/internal/mediums/bluetooth_radio.h"
#ifdef NO_WEBRTC
#include "core/internal/mediums/webrtc_stub.h"
#else
#include "core/internal/mediums/webrtc.h"
#endif
#include "core/internal/mediums/wifi_lan.h"
namespace location {
+1 -1
View File
@@ -22,7 +22,7 @@
#include "absl/time/time.h"
#include "core/internal/mediums/webrtc/session_description_wrapper.h"
#include "core/internal/mediums/webrtc/signaling_frames.h"
#include "core/internal/mediums/webrtc_socket_wrapper.h"
#include "core/internal/mediums/webrtc_socket.h"
#include "platform/base/byte_array.h"
#include "platform/base/listeners.h"
#include "platform/public/cancelable_alarm.h"
+2 -2
View File
@@ -25,9 +25,9 @@
#include "core/internal/mediums/webrtc/connection_flow.h"
#include "core/internal/mediums/webrtc/data_channel_listener.h"
#include "core/internal/mediums/webrtc/local_ice_candidate_listener.h"
#include "core/internal/mediums/webrtc/webrtc_socket.h"
#include "core/internal/mediums/webrtc/webrtc_socket_impl.h"
#include "core/internal/mediums/webrtc_peer_id.h"
#include "core/internal/mediums/webrtc_socket_wrapper.h"
#include "core/internal/mediums/webrtc_socket.h"
#include "platform/base/byte_array.h"
#include "platform/base/cancellation_flag.h"
#include "platform/base/listeners.h"
+3 -3
View File
@@ -50,10 +50,10 @@ cc_library(
cc_library(
name = "data_types",
srcs = [
"webrtc_socket.cc",
"webrtc_socket_impl.cc",
],
hdrs = [
"webrtc_socket.h",
"webrtc_socket_impl.h",
],
compatible_with = ["//buildenv/target:non_prod"],
copts = ["-DCORE_ADAPTER_DLL"],
@@ -74,7 +74,7 @@ cc_test(
srcs = [
"connection_flow_test.cc",
"signaling_frames_test.cc",
"webrtc_socket_test.cc",
"webrtc_socket_impl_test.cc",
],
tags = [
"notsan", # NOTE(b/139734036): known data race in usrsctplib.
@@ -20,8 +20,8 @@
#include "absl/memory/memory.h"
#include "absl/time/time.h"
#include "core/internal/mediums/webrtc/session_description_wrapper.h"
#include "core/internal/mediums/webrtc/webrtc_socket.h"
#include "core/internal/mediums/webrtc_socket_wrapper.h"
#include "core/internal/mediums/webrtc/webrtc_socket_impl.h"
#include "core/internal/mediums/webrtc_socket.h"
#include "platform/public/logging.h"
#include "platform/public/mutex_lock.h"
#include "platform/public/webrtc.h"
@@ -20,7 +20,7 @@
#include "core/internal/mediums/webrtc/data_channel_listener.h"
#include "core/internal/mediums/webrtc/local_ice_candidate_listener.h"
#include "core/internal/mediums/webrtc/session_description_wrapper.h"
#include "core/internal/mediums/webrtc_socket_wrapper.h"
#include "core/internal/mediums/webrtc_socket.h"
#include "platform/base/runnable.h"
#include "platform/public/count_down_latch.h"
#include "platform/public/single_thread_executor.h"
@@ -21,7 +21,7 @@
#include "gtest/gtest.h"
#include "absl/time/time.h"
#include "core/internal/mediums/webrtc/session_description_wrapper.h"
#include "core/internal/mediums/webrtc_socket_wrapper.h"
#include "core/internal/mediums/webrtc_socket.h"
#include "platform/base/byte_array.h"
#include "platform/base/medium_environment.h"
#include "platform/public/count_down_latch.h"
@@ -15,7 +15,7 @@
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_DATA_CHANNEL_LISTENER_H_
#define CORE_INTERNAL_MEDIUMS_WEBRTC_DATA_CHANNEL_LISTENER_H_
#include "core/internal/mediums/webrtc_socket_wrapper.h"
#include "core/internal/mediums/webrtc_socket.h"
#include "core/listeners.h"
#include "platform/base/byte_array.h"
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "core/internal/mediums/webrtc/webrtc_socket.h"
#include "core/internal/mediums/webrtc/webrtc_socket_impl.h"
#include "platform/public/logging.h"
#include "platform/public/mutex_lock.h"
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_H_
#define CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_H_
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_IMPL_H_
#define CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_IMPL_H_
#include <memory>
@@ -120,4 +120,4 @@ class WebRtcSocket : public Socket, public webrtc::DataChannelObserver {
} // namespace nearby
} // namespace location
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_H_
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_IMPL_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "core/internal/mediums/webrtc/webrtc_socket.h"
#include "core/internal/mediums/webrtc/webrtc_socket_impl.h"
#include <memory>
@@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_WRAPPER_H_
#define CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_WRAPPER_H_
#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_H_
#define CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_H_
#include <memory>
#include "core/internal/mediums/webrtc/webrtc_socket.h"
#include "core/internal/mediums/webrtc/webrtc_socket_impl.h"
namespace location {
namespace nearby {
@@ -52,4 +52,4 @@ class WebRtcSocketWrapper final {
} // namespace nearby
} // namespace location
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_WRAPPER_H_
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_SOCKET_H_
@@ -0,0 +1,69 @@
// Copyright 2020 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 CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_STUB_H_
#define CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_STUB_H_
#include <memory>
#include "platform/base/input_stream.h"
#include "platform/base/output_stream.h"
namespace location {
namespace nearby {
namespace connections {
namespace mediums {
class FakeInputStream : public InputStream {
public:
ExceptionOr<ByteArray> Read(std::int64_t size) {
return {Exception::kSuccess};
}
Exception Close() { return {Exception::kSuccess}; }
};
class FakeOutputStream : public OutputStream {
public:
Exception Write(const ByteArray& data) override {
return {Exception::kSuccess};
}
Exception Flush() override { return {Exception::kSuccess}; }
Exception Close() override { return {Exception::kSuccess}; }
};
class WebRtcSocketWrapper final {
public:
WebRtcSocketWrapper() = default;
WebRtcSocketWrapper(const WebRtcSocketWrapper&) = default;
WebRtcSocketWrapper& operator=(const WebRtcSocketWrapper&) = default;
~WebRtcSocketWrapper() = default;
InputStream& GetInputStream() { return fake_input_stream_; }
OutputStream& GetOutputStream() { return fake_output_stream_; }
void Close() {}
bool IsValid() const { return false; }
private:
FakeInputStream fake_input_stream_;
FakeOutputStream fake_output_stream_;
};
} // namespace mediums
} // namespace connections
} // namespace nearby
} // namespace location
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_SOCKET_STUB_H_
+61
View File
@@ -0,0 +1,61 @@
// Copyright 2020 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 "core/internal/mediums/webrtc_stub.h"
#include <functional>
#include <memory>
#include "core/internal/mediums/webrtc_socket_stub.h"
#include "platform/base/listeners.h"
#include "platform/public/cancelable_alarm.h"
#include "platform/public/future.h"
namespace location {
namespace nearby {
namespace connections {
namespace mediums {
WebRtc::WebRtc() = default;
WebRtc::~WebRtc() {}
const std::string WebRtc::GetDefaultCountryCode() { return "US"; }
bool WebRtc::IsAvailable() { return false; }
bool WebRtc::IsAcceptingConnections(const std::string& service_id) {
return false;
}
bool WebRtc::StartAcceptingConnections(const std::string& service_id,
const WebrtcPeerId& self_peer_id,
const LocationHint& location_hint,
AcceptedConnectionCallback callback) {
return false;
}
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) {
return WebRtcSocketWrapper();
}
} // namespace mediums
} // namespace connections
} // namespace nearby
} // namespace location
+82
View File
@@ -0,0 +1,82 @@
// Copyright 2020 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 CORE_INTERNAL_MEDIUMS_WEBRTC_STUB_H_
#define CORE_INTERNAL_MEDIUMS_WEBRTC_STUB_H_
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include "core/internal/mediums/webrtc_peer_id.h"
#include "core/internal/mediums/webrtc_socket_stub.h"
#include "platform/base/cancellation_flag.h"
#include "platform/base/listeners.h"
#include "proto/connections/offline_wire_formats.pb.h"
namespace location {
namespace nearby {
namespace connections {
namespace mediums {
// Callback that is invoked when a new connection is accepted.
struct AcceptedConnectionCallback {
std::function<void(WebRtcSocketWrapper socket)> accepted_cb =
DefaultCallback<WebRtcSocketWrapper>();
};
// Entry point for connecting a data channel between two devices via WebRtc.
class WebRtc {
public:
WebRtc();
~WebRtc();
// Gets the default two-letter country code associated with current locale.
// For example, en_US locale resolves to "US".
const std::string GetDefaultCountryCode();
// Returns if WebRtc is available as a medium for nearby to transport data.
// Runs on @MainThread.
bool IsAvailable();
// Returns if the device is accepting connection with specific service id.
// Runs on @MainThread.
bool IsAcceptingConnections(const std::string& service_id);
// Prepares the device to accept incoming WebRtc connections. Returns a
// boolean value indicating if the device has started accepting connections.
// Runs on @MainThread.
bool StartAcceptingConnections(const std::string& service_id,
const WebrtcPeerId& self_peer_id,
const LocationHint& location_hint,
AcceptedConnectionCallback callback);
// Try to stop (accepting) the specific connection with provided service id.
// Runs on @MainThread
void StopAcceptingConnections(const std::string& service_id);
// Initiates a WebRtc connection with peer device identified by |peer_id|
// with internal retry for maximum attempts of kConnectAttemptsLimit.
// Runs on @MainThread.
WebRtcSocketWrapper Connect(const std::string& service_id,
const WebrtcPeerId& peer_id,
const LocationHint& location_hint,
CancellationFlag* cancellation_flag);
};
} // namespace mediums
} // namespace connections
} // namespace nearby
} // namespace location
#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_STUB_H_
+1 -1
View File
@@ -16,7 +16,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "core/internal/mediums/webrtc_socket_wrapper.h"
#include "core/internal/mediums/webrtc_socket.h"
#include "platform/base/listeners.h"
#include "platform/base/medium_environment.h"
#include "platform/public/mutex_lock.h"