mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
@@ -54,6 +54,8 @@ bool WebRtc::IsAcceptingConnections() {
|
||||
}
|
||||
|
||||
bool WebRtc::StartAcceptingConnections(const PeerId& self_id,
|
||||
const std::string& service_id,
|
||||
const std::string& local_endpoint_id,
|
||||
const LocationHint& location_hint,
|
||||
AcceptedConnectionCallback callback) {
|
||||
if (!IsAvailable()) {
|
||||
@@ -64,17 +66,10 @@ bool WebRtc::StartAcceptingConnections(const PeerId& self_id,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsAcceptingConnections()) {
|
||||
NEARBY_LOG(WARNING, "Already accepting WebRTC connections.");
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
if (role_ != Role::kNone) {
|
||||
NEARBY_LOG(WARNING,
|
||||
"Cannot start accepting WebRTC connections, current role %d",
|
||||
role_);
|
||||
if (self_id_.GetId() == self_id.GetId()) {
|
||||
NEARBY_LOG(WARNING, "Already accepting WebRTC connections.");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -96,6 +91,8 @@ bool WebRtc::StartAcceptingConnections(const PeerId& self_id,
|
||||
// the actual transport can begin.
|
||||
ListenForWebRtcSocketFuture(connection_flow_->GetDataChannel(),
|
||||
std::move(callback));
|
||||
latest_service_id_ = service_id;
|
||||
latest_local_endpoint_id_ = local_endpoint_id;
|
||||
NEARBY_LOG(INFO, "Started listening for WebRtc connections as %s",
|
||||
self_id.GetId().c_str());
|
||||
}
|
||||
@@ -169,6 +166,19 @@ void WebRtc::StopAcceptingConnections() {
|
||||
NEARBY_LOG(INFO, "Stopped accepting WebRTC connections");
|
||||
}
|
||||
|
||||
void WebRtc::StopAcceptingConnection(const std::string& service_id,
|
||||
const std::string& local_endpoint_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (service_id == latest_service_id_ &&
|
||||
local_endpoint_id == latest_local_endpoint_id_) {
|
||||
StopAcceptingConnections();
|
||||
} else {
|
||||
NEARBY_LOG(INFO,
|
||||
"Skipped StopAcceptingConnection since we are not the latest"
|
||||
"ongoing connection.");
|
||||
}
|
||||
}
|
||||
|
||||
Future<WebRtcSocketWrapper> WebRtc::ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
data_channel_future,
|
||||
@@ -256,8 +266,7 @@ bool WebRtc::InitWebRtcFlow(Role role, const PeerId& self_id,
|
||||
|
||||
connection_flow_ = ConnectionFlow::Create(GetLocalIceCandidateListener(),
|
||||
GetDataChannelListener(), medium_);
|
||||
if (!connection_flow_)
|
||||
return false;
|
||||
if (!connection_flow_) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "platform/public/single_thread_executor.h"
|
||||
#include "platform/public/webrtc.h"
|
||||
#include "location/nearby/mediums/proto/web_rtc_signaling_frames.pb.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "webrtc/api/data_channel_interface.h"
|
||||
#include "webrtc/api/jsep.h"
|
||||
#include "webrtc/api/scoped_refptr.h"
|
||||
@@ -56,10 +57,18 @@ class WebRtc {
|
||||
// Runs on @MainThread.
|
||||
bool IsAcceptingConnections() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns if the device is accepting connection with specific service id and
|
||||
// local endpoint id. Runs on @MainThread.
|
||||
bool IsAcceptingConnection(const std::string& service_id,
|
||||
const std::string& local_endpoint_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// 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 PeerId& self_id,
|
||||
const std::string& service_id,
|
||||
const std::string& local_endpoint_id,
|
||||
const LocationHint& location_hint,
|
||||
AcceptedConnectionCallback callback)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -69,6 +78,13 @@ class WebRtc {
|
||||
// Runs on @MainThread.
|
||||
void StopAcceptingConnections() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Try to stop (accepting) the specific connection with provided service id
|
||||
// and local endpoint id. If the specific connection is not the latest one,
|
||||
// then nothing will happen; if it's the latest one,
|
||||
void StopAcceptingConnection(const std::string& service_id,
|
||||
const std::string& local_endpoint_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Initiates a WebRtc connection with peer device identified by |peer_id|.
|
||||
// Runs on @MainThread.
|
||||
WebRtcSocketWrapper Connect(const PeerId& peer_id,
|
||||
@@ -168,6 +184,9 @@ class WebRtc {
|
||||
// Restarts the signaling messenger for receiving messages.
|
||||
ScheduledExecutor restart_receive_messages_executor_;
|
||||
CancelableAlarm restart_receive_messages_alarm_;
|
||||
|
||||
std::string latest_service_id_ ABSL_GUARDED_BY(mutex_);
|
||||
std::string latest_local_endpoint_id_ ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
|
||||
@@ -142,10 +142,6 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
|
||||
};
|
||||
}
|
||||
|
||||
// StopAcceptingConnections invokes for webrtc is suppressed for now to
|
||||
// unblock CrOS dogfood integration. Disconnect will invoke ShutdownSignaling
|
||||
// to release resources.
|
||||
// TODO (hais): add corresponding logic back (b/172518506).
|
||||
Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) {
|
||||
bluetooth_medium_.TurnOffDiscoverability();
|
||||
bluetooth_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId());
|
||||
@@ -153,6 +149,9 @@ Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) {
|
||||
ble_medium_.StopAdvertising(client->GetAdvertisingServiceId());
|
||||
ble_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId());
|
||||
|
||||
webrtc_medium_.StopAcceptingConnection(client->GetAdvertisingServiceId(),
|
||||
client->GetLocalEndpointId());
|
||||
|
||||
wifi_lan_medium_.StopAdvertising(client->GetAdvertisingServiceId());
|
||||
wifi_lan_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId());
|
||||
|
||||
@@ -1161,7 +1160,8 @@ P2pClusterPcpHandler::StartListeningForWebRtcConnections(
|
||||
service_id, local_endpoint_id, local_endpoint_info);
|
||||
std::string empty_country_code;
|
||||
if (!webrtc_medium_.StartAcceptingConnections(
|
||||
self_id, Utils::BuildLocationHint(empty_country_code),
|
||||
self_id, service_id, local_endpoint_id,
|
||||
Utils::BuildLocationHint(empty_country_code),
|
||||
{[this, client,
|
||||
local_endpoint_info](mediums::WebRtcSocketWrapper socket) {
|
||||
if (!socket.IsValid()) {
|
||||
|
||||
@@ -63,7 +63,8 @@ ByteArray WebrtcBwuHandler::InitializeUpgradedMediumForEndpoint(
|
||||
mediums::PeerId self_id{mediums::PeerId::FromRandom()};
|
||||
if (!webrtc_.IsAcceptingConnections()) {
|
||||
if (!webrtc_.StartAcceptingConnections(
|
||||
self_id, location_hint,
|
||||
self_id, upgrade_service_id, client->GetLocalEndpointId(),
|
||||
location_hint,
|
||||
{
|
||||
.accepted_cb = absl::bind_front(
|
||||
&WebrtcBwuHandler::OnIncomingWebrtcConnection, this, client,
|
||||
|
||||
@@ -290,9 +290,7 @@ void MediumEnvironment::UnregisterBluetoothMedium(
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
auto item = bluetooth_mediums_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
auto& context = item.mapped();
|
||||
NEARBY_LOG(INFO, "Unregistered medium for device=%s",
|
||||
context.adapter->GetName().c_str());
|
||||
NEARBY_LOGS(INFO) << "Unregistered Bluetooth medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user