mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Roll forward to cl/318180324
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I71bb49fe74104964088d392b12775ec2a253b1c9
This commit is contained in:
@@ -16,6 +16,8 @@ WifiLan& Mediums::GetWifiLan() {
|
||||
return wifi_lan_;
|
||||
}
|
||||
|
||||
mediums::WebRtc& Mediums::GetWebRtc() { return webrtc_; }
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
#include "core_v2/internal/mediums/bluetooth_classic.h"
|
||||
#include "core_v2/internal/mediums/bluetooth_radio.h"
|
||||
#include "core_v2/internal/mediums/webrtc.h"
|
||||
#include "core_v2/internal/mediums/wifi_lan.h"
|
||||
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
@@ -25,6 +25,9 @@ class Mediums {
|
||||
// Returns a handle to the Wifi-Lan medium.
|
||||
WifiLan& GetWifiLan();
|
||||
|
||||
// Returns a handle to the WebRtc medium.
|
||||
mediums::WebRtc& GetWebRtc();
|
||||
|
||||
private:
|
||||
// The order of declaration is critical for both construction and
|
||||
// destruction.
|
||||
@@ -37,6 +40,7 @@ class Mediums {
|
||||
BluetoothRadio bluetooth_radio_;
|
||||
BluetoothClassic bluetooth_classic_{bluetooth_radio_};
|
||||
WifiLan wifi_lan_;
|
||||
mediums::WebRtc webrtc_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -111,14 +111,13 @@ WebRtcSocketWrapper WebRtc::Connect(const PeerId& peer_id) {
|
||||
NEARBY_LOG(INFO, "Attempting to make a WebRTC connection to %s.",
|
||||
peer_id.GetId().c_str());
|
||||
|
||||
std::shared_ptr<Future<WebRtcSocketWrapper>> socket_future =
|
||||
ListenForWebRtcSocketFuture(connection_flow_->GetDataChannel(),
|
||||
AcceptedConnectionCallback());
|
||||
Future<WebRtcSocketWrapper> socket_future = ListenForWebRtcSocketFuture(
|
||||
connection_flow_->GetDataChannel(), AcceptedConnectionCallback());
|
||||
|
||||
// The two devices have discovered each other, hence we have a timeout for
|
||||
// establishing the transport channel.
|
||||
ExceptionOr<WebRtcSocketWrapper> result =
|
||||
socket_future->Get(kDataChannelTimeout);
|
||||
socket_future.Get(kDataChannelTimeout);
|
||||
if (result.ok()) return result.result();
|
||||
|
||||
Disconnect();
|
||||
@@ -149,18 +148,17 @@ void WebRtc::StopAcceptingConnections() {
|
||||
NEARBY_LOG(INFO, "Stopped accepting WebRTC connections");
|
||||
}
|
||||
|
||||
std::shared_ptr<Future<WebRtcSocketWrapper>>
|
||||
WebRtc::ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>*
|
||||
Future<WebRtcSocketWrapper> WebRtc::ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
data_channel_future,
|
||||
AcceptedConnectionCallback callback) {
|
||||
auto socket_future = std::make_shared<Future<WebRtcSocketWrapper>>();
|
||||
Future<WebRtcSocketWrapper> socket_future;
|
||||
auto data_channel_runnable = [this, socket_future, data_channel_future,
|
||||
callback{std::move(callback)}]() {
|
||||
callback{std::move(callback)}]() mutable {
|
||||
// The overall timeout of creating the socket and data channel is controlled
|
||||
// by the caller of this function.
|
||||
ExceptionOr<rtc::scoped_refptr<webrtc::DataChannelInterface>> res =
|
||||
data_channel_future->Get();
|
||||
data_channel_future.Get();
|
||||
if (res.ok()) {
|
||||
WebRtcSocketWrapper wrapper = CreateWebRtcSocketWrapper(res.result());
|
||||
callback.accepted_cb(wrapper);
|
||||
@@ -168,15 +166,15 @@ WebRtc::ListenForWebRtcSocketFuture(
|
||||
MutexLock lock(&mutex_);
|
||||
socket_ = wrapper;
|
||||
}
|
||||
socket_future->Set(wrapper);
|
||||
socket_future.Set(wrapper);
|
||||
} else {
|
||||
NEARBY_LOG(WARNING, "Failed to get WebRtcSocket.");
|
||||
socket_future->Set(WebRtcSocketWrapper());
|
||||
socket_future.Set(WebRtcSocketWrapper());
|
||||
}
|
||||
};
|
||||
|
||||
data_channel_future->AddListener(std::move(data_channel_runnable),
|
||||
&single_thread_executor_);
|
||||
data_channel_future.AddListener(std::move(data_channel_runnable),
|
||||
&single_thread_executor_);
|
||||
|
||||
return socket_future;
|
||||
}
|
||||
|
||||
@@ -74,8 +74,8 @@ class WebRtc {
|
||||
bool InitWebRtcFlow(Role role, const PeerId& self_id)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
std::shared_ptr<Future<WebRtcSocketWrapper>> ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>*
|
||||
Future<WebRtcSocketWrapper> ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
data_channel_future,
|
||||
AcceptedConnectionCallback callback);
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
constexpr absl::Duration ConnectionFlow::kTimeout;
|
||||
|
||||
namespace {
|
||||
// This is the same as the nearby data channel name.
|
||||
const char kDataChannelName[] = "dataChannel";
|
||||
@@ -217,9 +219,9 @@ bool ConnectionFlow::OnRemoteIceCandidatesReceived(
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>*
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
ConnectionFlow::GetDataChannel() {
|
||||
return &data_channel_future_;
|
||||
return data_channel_future_;
|
||||
}
|
||||
|
||||
bool ConnectionFlow::Close() {
|
||||
|
||||
@@ -87,7 +87,7 @@ class ConnectionFlow {
|
||||
std::vector<std::unique_ptr<webrtc::IceCandidateInterface>>
|
||||
ice_candidates) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
// Get a future for the data channel.
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>* GetDataChannel();
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>> GetDataChannel();
|
||||
// Close the peer connection and data channel.
|
||||
bool Close() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
|
||||
@@ -80,10 +80,10 @@ TEST(ConnectionFlowTest, SuccessfulOfferAnswerFlow) {
|
||||
|
||||
// Retrieve Data Channels
|
||||
ExceptionOr<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
offerer_channel = offerer->GetDataChannel()->Get(absl::Seconds(1));
|
||||
offerer_channel = offerer->GetDataChannel().Get(absl::Seconds(1));
|
||||
EXPECT_TRUE(offerer_channel.ok());
|
||||
ExceptionOr<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
answerer_channel = answerer->GetDataChannel()->Get(absl::Seconds(1));
|
||||
answerer_channel = answerer->GetDataChannel().Get(absl::Seconds(1));
|
||||
EXPECT_TRUE(answerer_channel.ok());
|
||||
|
||||
// Send message on data channel
|
||||
|
||||
Reference in New Issue
Block a user