Commit 8ec02b4e authored by Steve Anton's avatar Steve Anton Committed by Commit Bot

Add a new interface: IceTransportAdapter

The IceTransportAdapter splits P2PTransportChannel-specific logic
out of IceTransportHost and puts it behind an interface to
facilitate future testing. The API methods are changed to map to
the requirements of the RTCIceTransport rather than the
implementation details of the P2PTransportChannel.

Bug: 864871
Change-Id: I2a7036f675a11460e9721cc937beec17b7f03cc9
Reviewed-on: https://chromium-review.googlesource.com/1200127
Commit-Queue: Steve Anton <steveanton@chromium.org>
Reviewed-by: default avatarHenrik Boström <hbos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589616}
parent 6c773bec
...@@ -6,6 +6,9 @@ import("//third_party/blink/renderer/modules/modules.gni") ...@@ -6,6 +6,9 @@ import("//third_party/blink/renderer/modules/modules.gni")
blink_modules_sources("peerconnection") { blink_modules_sources("peerconnection") {
sources = [ sources = [
"adapters/ice_transport_adapter.h",
"adapters/ice_transport_adapter_impl.cc",
"adapters/ice_transport_adapter_impl.h",
"adapters/ice_transport_host.cc", "adapters/ice_transport_host.cc",
"adapters/ice_transport_host.h", "adapters/ice_transport_host.h",
"adapters/ice_transport_proxy.cc", "adapters/ice_transport_proxy.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_ICE_TRANSPORT_ADAPTER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_ICE_TRANSPORT_ADAPTER_H_
#include "third_party/webrtc/p2p/base/p2ptransportchannel.h"
namespace blink {
// Defines the ICE candidate policy the browser uses to surface the permitted
// candidates to the application.
// https://w3c.github.io/webrtc-pc/#dom-rtcicetransportpolicy
enum class IceTransportPolicy {
// The ICE Agent uses only media relay candidates.
kRelay,
// The ICE Agent can use any type of candidate.
kAll
};
// The IceTransportAdapter is the API used by the RTCIceTransport Blink binding
// to interact with the ICE implementation. It exactly mirrors the requirements
// of the RTCIceTransport: each JavaScript method that must interact with the
// ICE implementation should map to exactly one method call on this interface.
// This interface is designed to be fully asynchronous; all methods are void and
// callbacks occur via the Delegate (implemented by the client).
//
// The ICE Agent is immediately active once this object has been constructed. It
// can be stopped by deleting the IceTransportAdapter.
class IceTransportAdapter {
public:
// Delegate to receive callbacks from the IceTransportAdapter. The Delegate
// must outlive the IceTransportAdapter.
class Delegate {
public:
virtual ~Delegate() = default;
// Called asynchronously when the ICE gathering state changes.
virtual void OnGatheringStateChanged(cricket::IceGatheringState new_state) {
}
// Called asynchronously when a new ICE candidate has been gathered.
virtual void OnCandidateGathered(const cricket::Candidate& candidate) {}
// Called asynchronously when the ICE connection state has changed.
virtual void OnStateChanged(cricket::IceTransportState new_state) {}
};
virtual ~IceTransportAdapter() = default;
// Start ICE candidate gathering.
virtual void StartGathering(
const cricket::IceParameters& local_parameters,
const cricket::ServerAddresses& stun_servers,
const std::vector<cricket::RelayServerConfig>& turn_servers,
IceTransportPolicy policy) = 0;
// Start ICE connectivity checks with the given initial remote candidates.
virtual void Start(
const cricket::IceParameters& remote_parameters,
cricket::IceRole role,
const std::vector<cricket::Candidate>& initial_remote_candidates) = 0;
// Handle a remote ICE restart. This changes the remote parameters and clears
// all remote candidates.
virtual void HandleRemoteRestart(
const cricket::IceParameters& new_remote_parameters) = 0;
// Adds a remote candidate to potentially start connectivity checks with.
// The caller must ensure Start() has already bene called.
virtual void AddRemoteCandidate(const cricket::Candidate& candidate) = 0;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_ICE_TRANSPORT_ADAPTER_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_adapter_impl.h"
namespace blink {
IceTransportAdapterImpl::IceTransportAdapterImpl(
Delegate* delegate,
std::unique_ptr<cricket::PortAllocator> port_allocator,
rtc::Thread* thread)
: delegate_(delegate), port_allocator_(std::move(port_allocator)) {
// TODO(bugs.webrtc.org/9419): Remove once WebRTC can be built as a component.
if (!rtc::ThreadManager::Instance()->CurrentThread()) {
rtc::ThreadManager::Instance()->SetCurrentThread(thread);
}
// These settings are copied from PeerConnection:
// https://codesearch.chromium.org/chromium/src/third_party/webrtc/pc/peerconnection.cc?l=4708&rcl=820ebd0f661696043959b5105b2814e0edd8b694
port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
port_allocator_->set_flags(port_allocator_->flags() |
cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
cricket::PORTALLOCATOR_ENABLE_IPV6 |
cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
port_allocator_->Initialize();
p2p_transport_channel_ = std::make_unique<cricket::P2PTransportChannel>(
"", 0, port_allocator_.get());
p2p_transport_channel_->SignalGatheringState.connect(
this, &IceTransportAdapterImpl::OnGatheringStateChanged);
p2p_transport_channel_->SignalCandidateGathered.connect(
this, &IceTransportAdapterImpl::OnCandidateGathered);
p2p_transport_channel_->SignalStateChanged.connect(
this, &IceTransportAdapterImpl::OnStateChanged);
// The ICE tiebreaker is used to determine which side is controlling/
// controlled when both sides start in the same role. The number is randomly
// generated so that each peer can calculate a.tiebreaker <= b.tiebreaker
// consistently.
p2p_transport_channel_->SetIceTiebreaker(rtc::CreateRandomId64());
}
IceTransportAdapterImpl::~IceTransportAdapterImpl() = default;
static uint32_t GetCandidateFilterForPolicy(IceTransportPolicy policy) {
switch (policy) {
case IceTransportPolicy::kRelay:
return cricket::CF_RELAY;
case IceTransportPolicy::kAll:
return cricket::CF_ALL;
}
NOTREACHED();
return 0;
}
void IceTransportAdapterImpl::StartGathering(
const cricket::IceParameters& local_parameters,
const cricket::ServerAddresses& stun_servers,
const std::vector<cricket::RelayServerConfig>& turn_servers,
IceTransportPolicy policy) {
port_allocator_->set_candidate_filter(GetCandidateFilterForPolicy(policy));
port_allocator_->SetConfiguration(stun_servers, turn_servers,
port_allocator_->candidate_pool_size(),
port_allocator_->prune_turn_ports());
p2p_transport_channel_->SetIceParameters(local_parameters);
p2p_transport_channel_->MaybeStartGathering();
DCHECK_EQ(p2p_transport_channel_->gathering_state(),
cricket::kIceGatheringGathering);
}
void IceTransportAdapterImpl::Start(
const cricket::IceParameters& remote_parameters,
cricket::IceRole role,
const std::vector<cricket::Candidate>& initial_remote_candidates) {
p2p_transport_channel_->SetRemoteIceParameters(remote_parameters);
p2p_transport_channel_->SetIceRole(role);
for (const auto& candidate : initial_remote_candidates) {
p2p_transport_channel_->AddRemoteCandidate(candidate);
}
}
void IceTransportAdapterImpl::HandleRemoteRestart(
const cricket::IceParameters& new_remote_parameters) {
auto remote_candidates = p2p_transport_channel_->remote_candidates();
for (const auto& remote_candidate : remote_candidates) {
p2p_transport_channel_->RemoveRemoteCandidate(remote_candidate);
}
p2p_transport_channel_->SetRemoteIceParameters(new_remote_parameters);
}
void IceTransportAdapterImpl::AddRemoteCandidate(
const cricket::Candidate& candidate) {
p2p_transport_channel_->AddRemoteCandidate(candidate);
}
void IceTransportAdapterImpl::OnGatheringStateChanged(
cricket::IceTransportInternal* transport) {
DCHECK_EQ(transport, p2p_transport_channel_.get());
delegate_->OnGatheringStateChanged(p2p_transport_channel_->gathering_state());
}
void IceTransportAdapterImpl::OnCandidateGathered(
cricket::IceTransportInternal* transport,
const cricket::Candidate& candidate) {
DCHECK_EQ(transport, p2p_transport_channel_.get());
delegate_->OnCandidateGathered(candidate);
}
void IceTransportAdapterImpl::OnStateChanged(
cricket::IceTransportInternal* transport) {
DCHECK_EQ(transport, p2p_transport_channel_.get());
delegate_->OnStateChanged(p2p_transport_channel_->GetState());
}
} // namespace blink
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_ICE_TRANSPORT_ADAPTER_IMPL_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_ICE_TRANSPORT_ADAPTER_IMPL_H_
#include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_adapter.h"
namespace blink {
// IceTransportAdapter implementation backed by the WebRTC PortAllocator /
// P2PTransportChannel.
class IceTransportAdapterImpl final : public IceTransportAdapter,
public sigslot::has_slots<> {
public:
// Must be constructed on the WebRTC worker thread.
// |delegate| must outlive the IceTransportAdapter.
// |thread| should be the rtc::Thread instance associated with the WebRTC
// worker thread.
IceTransportAdapterImpl(
Delegate* delegate,
std::unique_ptr<cricket::PortAllocator> port_allocator,
rtc::Thread* thread);
~IceTransportAdapterImpl() override;
// IceTransportAdapter overrides.
void StartGathering(
const cricket::IceParameters& local_parameters,
const cricket::ServerAddresses& stun_servers,
const std::vector<cricket::RelayServerConfig>& turn_servers,
IceTransportPolicy policy) override;
void Start(const cricket::IceParameters& remote_parameters,
cricket::IceRole role,
const std::vector<cricket::Candidate>& initial_remote_candidates)
override;
void HandleRemoteRestart(
const cricket::IceParameters& new_remote_parameters) override;
void AddRemoteCandidate(const cricket::Candidate& candidate) override;
private:
// Callbacks from P2PTransportChannel.
void OnGatheringStateChanged(cricket::IceTransportInternal* transport);
void OnCandidateGathered(cricket::IceTransportInternal* transport,
const cricket::Candidate& candidate);
void OnStateChanged(cricket::IceTransportInternal* transport);
Delegate* const delegate_;
std::unique_ptr<cricket::PortAllocator> port_allocator_;
std::unique_ptr<cricket::P2PTransportChannel> p2p_transport_channel_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_ADAPTERS_ICE_TRANSPORT_ADAPTER_IMPL_H_
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_host.h" #include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_host.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_adapter_impl.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_proxy.h" #include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_proxy.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/web_rtc_cross_thread_copier.h" #include "third_party/blink/renderer/modules/peerconnection/adapters/web_rtc_cross_thread_copier.h"
#include "third_party/blink/renderer/platform/cross_thread_functional.h" #include "third_party/blink/renderer/platform/cross_thread_functional.h"
...@@ -13,117 +14,79 @@ namespace blink { ...@@ -13,117 +14,79 @@ namespace blink {
IceTransportHost::IceTransportHost( IceTransportHost::IceTransportHost(
scoped_refptr<base::SingleThreadTaskRunner> proxy_thread, scoped_refptr<base::SingleThreadTaskRunner> proxy_thread,
base::WeakPtr<IceTransportProxy> proxy, base::WeakPtr<IceTransportProxy> proxy)
std::unique_ptr<cricket::PortAllocator> port_allocator) : proxy_thread_(std::move(proxy_thread)), proxy_(std::move(proxy)) {
: proxy_thread_(std::move(proxy_thread)),
port_allocator_(std::move(port_allocator)),
proxy_(std::move(proxy)) {
DETACH_FROM_THREAD(thread_checker_); DETACH_FROM_THREAD(thread_checker_);
DCHECK(proxy_thread_); DCHECK(proxy_thread_);
DCHECK(proxy_); DCHECK(proxy_);
DCHECK(port_allocator_);
} }
IceTransportHost::~IceTransportHost() { IceTransportHost::~IceTransportHost() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
} }
void IceTransportHost::Initialize(rtc::Thread* host_thread_rtc_thread) { void IceTransportHost::Initialize(
std::unique_ptr<cricket::PortAllocator> port_allocator,
rtc::Thread* host_thread_rtc_thread) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// TODO(bugs.webrtc.org/9419): Remove once WebRTC can be built as a component. DCHECK(port_allocator);
if (!rtc::ThreadManager::Instance()->CurrentThread()) { DCHECK(host_thread_rtc_thread);
rtc::ThreadManager::Instance()->SetCurrentThread(host_thread_rtc_thread); transport_ = std::make_unique<IceTransportAdapterImpl>(
} this, std::move(port_allocator), host_thread_rtc_thread);
// These settings are copied from PeerConnection:
// https://codesearch.chromium.org/chromium/src/third_party/webrtc/pc/peerconnection.cc?l=4708&rcl=820ebd0f661696043959b5105b2814e0edd8b694
port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
port_allocator_->set_flags(port_allocator_->flags() |
cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
cricket::PORTALLOCATOR_ENABLE_IPV6 |
cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
port_allocator_->Initialize();
transport_ = std::make_unique<cricket::P2PTransportChannel>(
"", 0, port_allocator_.get());
transport_->SignalGatheringState.connect(
this, &IceTransportHost::OnGatheringStateChanged);
transport_->SignalCandidateGathered.connect(
this, &IceTransportHost::OnCandidateGathered);
transport_->SignalStateChanged.connect(this,
&IceTransportHost::OnStateChanged);
// The ICE tiebreaker is used to determine which side is controlling/
// controlled when both sides start in the same role. The number is randomly
// generated so that each peer can calculate a.tiebreaker <= b.tiebreaker
// consistently.
transport_->SetIceTiebreaker(rtc::CreateRandomId64());
} }
void IceTransportHost::StartGathering( void IceTransportHost::StartGathering(
const cricket::IceParameters& local_parameters, const cricket::IceParameters& local_parameters,
const cricket::ServerAddresses& stun_servers, const cricket::ServerAddresses& stun_servers,
const std::vector<cricket::RelayServerConfig>& turn_servers, const std::vector<cricket::RelayServerConfig>& turn_servers,
int32_t candidate_filter) { IceTransportPolicy policy) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
port_allocator_->set_candidate_filter(candidate_filter); transport_->StartGathering(local_parameters, stun_servers, turn_servers,
port_allocator_->SetConfiguration(stun_servers, turn_servers, policy);
port_allocator_->candidate_pool_size(),
port_allocator_->prune_turn_ports());
transport_->SetIceParameters(local_parameters);
transport_->MaybeStartGathering();
DCHECK_EQ(transport_->gathering_state(), cricket::kIceGatheringGathering);
} }
void IceTransportHost::SetRole(cricket::IceRole role) { void IceTransportHost::Start(
const cricket::IceParameters& remote_parameters,
cricket::IceRole role,
const std::vector<cricket::Candidate>& initial_remote_candidates) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
transport_->SetIceRole(role); transport_->Start(remote_parameters, role, initial_remote_candidates);
} }
void IceTransportHost::SetRemoteParameters( void IceTransportHost::HandleRemoteRestart(
const cricket::IceParameters& remote_parameters) { const cricket::IceParameters& new_remote_parameters) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
transport_->SetRemoteIceParameters(remote_parameters); transport_->HandleRemoteRestart(new_remote_parameters);
} }
void IceTransportHost::AddRemoteCandidate(const cricket::Candidate& candidate) { void IceTransportHost::AddRemoteCandidate(const cricket::Candidate& candidate) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
transport_->AddRemoteCandidate(candidate); transport_->AddRemoteCandidate(candidate);
}
void IceTransportHost::ClearRemoteCandidates() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto remote_candidates = transport_->remote_candidates();
for (const auto& remote_candidate : remote_candidates) {
transport_->RemoveRemoteCandidate(remote_candidate);
}
} }
void IceTransportHost::OnGatheringStateChanged( void IceTransportHost::OnGatheringStateChanged(
cricket::IceTransportInternal* transport) { cricket::IceGatheringState new_state) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK_EQ(transport, transport_.get());
PostCrossThreadTask( PostCrossThreadTask(
*proxy_thread_, FROM_HERE, *proxy_thread_, FROM_HERE,
CrossThreadBind(&IceTransportProxy::OnGatheringStateChanged, proxy_, CrossThreadBind(&IceTransportProxy::OnGatheringStateChanged, proxy_,
transport_->gathering_state())); new_state));
} }
void IceTransportHost::OnCandidateGathered( void IceTransportHost::OnCandidateGathered(
cricket::IceTransportInternal* transport,
const cricket::Candidate& candidate) { const cricket::Candidate& candidate) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK_EQ(transport, transport_.get());
PostCrossThreadTask(*proxy_thread_, FROM_HERE, PostCrossThreadTask(*proxy_thread_, FROM_HERE,
CrossThreadBind(&IceTransportProxy::OnCandidateGathered, CrossThreadBind(&IceTransportProxy::OnCandidateGathered,
proxy_, candidate)); proxy_, candidate));
} }
void IceTransportHost::OnStateChanged( void IceTransportHost::OnStateChanged(cricket::IceTransportState new_state) {
cricket::IceTransportInternal* transport) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK_EQ(transport, transport_.get()); PostCrossThreadTask(
PostCrossThreadTask(*proxy_thread_, FROM_HERE, *proxy_thread_, FROM_HERE,
CrossThreadBind(&IceTransportProxy::OnStateChanged, CrossThreadBind(&IceTransportProxy::OnStateChanged, proxy_, new_state));
proxy_, transport_->GetState()));
} }
} // namespace blink } // namespace blink
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "third_party/webrtc/p2p/base/p2ptransportchannel.h" #include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_adapter.h"
namespace rtc { namespace rtc {
class Thread; class Thread;
...@@ -41,37 +41,34 @@ class IceTransportProxy; ...@@ -41,37 +41,34 @@ class IceTransportProxy;
// //
// The Host can be constructed on any thread but after that point all methods // The Host can be constructed on any thread but after that point all methods
// must be called on the host thread. // must be called on the host thread.
class IceTransportHost final : public sigslot::has_slots<> { class IceTransportHost final : public IceTransportAdapter::Delegate {
public: public:
IceTransportHost(scoped_refptr<base::SingleThreadTaskRunner> proxy_thread, IceTransportHost(scoped_refptr<base::SingleThreadTaskRunner> proxy_thread,
base::WeakPtr<IceTransportProxy> proxy, base::WeakPtr<IceTransportProxy> proxy);
std::unique_ptr<cricket::PortAllocator> port_allocator);
~IceTransportHost() override; ~IceTransportHost() override;
void Initialize(rtc::Thread* host_thread_rtc_thread); void Initialize(std::unique_ptr<cricket::PortAllocator> port_allocator,
rtc::Thread* host_thread_rtc_thread);
void StartGathering( void StartGathering(
const cricket::IceParameters& local_parameters, const cricket::IceParameters& local_parameters,
const cricket::ServerAddresses& stun_servers, const cricket::ServerAddresses& stun_servers,
const std::vector<cricket::RelayServerConfig>& turn_servers, const std::vector<cricket::RelayServerConfig>& turn_servers,
int32_t candidate_filter); IceTransportPolicy policy);
void Start(const cricket::IceParameters& remote_parameters,
void SetRole(cricket::IceRole role); cricket::IceRole role,
void SetRemoteParameters(const cricket::IceParameters& remote_parameters); const std::vector<cricket::Candidate>& initial_remote_candidates);
void HandleRemoteRestart(const cricket::IceParameters& new_remote_parameters);
void AddRemoteCandidate(const cricket::Candidate& candidate); void AddRemoteCandidate(const cricket::Candidate& candidate);
void ClearRemoteCandidates();
private: private:
// Callbacks from P2PTransportChannel. // IceTransportAdapter::Delegate overrides.
void OnGatheringStateChanged(cricket::IceTransportInternal* transport); void OnGatheringStateChanged(cricket::IceGatheringState new_state) override;
void OnCandidateGathered(cricket::IceTransportInternal* transport, void OnCandidateGathered(const cricket::Candidate& candidate) override;
const cricket::Candidate& candidate); void OnStateChanged(cricket::IceTransportState new_state) override;
void OnStateChanged(cricket::IceTransportInternal* transport);
const scoped_refptr<base::SingleThreadTaskRunner> proxy_thread_; const scoped_refptr<base::SingleThreadTaskRunner> proxy_thread_;
std::unique_ptr<cricket::PortAllocator> port_allocator_; std::unique_ptr<IceTransportAdapter> transport_;
std::unique_ptr<cricket::P2PTransportChannel> transport_;
base::WeakPtr<IceTransportProxy> proxy_; base::WeakPtr<IceTransportProxy> proxy_;
THREAD_CHECKER(thread_checker_); THREAD_CHECKER(thread_checker_);
......
...@@ -32,12 +32,13 @@ IceTransportProxy::IceTransportProxy( ...@@ -32,12 +32,13 @@ IceTransportProxy::IceTransportProxy(
// The IceTransportHost is constructed on the proxy thread but should only be // The IceTransportHost is constructed on the proxy thread but should only be
// interacted with via PostTask to the host thread. The OnTaskRunnerDeleter // interacted with via PostTask to the host thread. The OnTaskRunnerDeleter
// (configured above) will ensure it gets deleted on the host thread. // (configured above) will ensure it gets deleted on the host thread.
host_.reset(new IceTransportHost(proxy_thread, weak_ptr_factory_.GetWeakPtr(), host_.reset(
std::move(port_allocator))); new IceTransportHost(proxy_thread, weak_ptr_factory_.GetWeakPtr()));
PostCrossThreadTask( PostCrossThreadTask(
*host_thread_, FROM_HERE, *host_thread_, FROM_HERE,
CrossThreadBind(&IceTransportHost::Initialize, CrossThreadBind(&IceTransportHost::Initialize,
CrossThreadUnretained(host_.get()), CrossThreadUnretained(host_.get()),
WTF::Passed(std::move(port_allocator)),
CrossThreadUnretained(host_thread_rtc_thread))); CrossThreadUnretained(host_thread_rtc_thread)));
} }
...@@ -50,30 +51,34 @@ void IceTransportProxy::StartGathering( ...@@ -50,30 +51,34 @@ void IceTransportProxy::StartGathering(
const cricket::IceParameters& local_parameters, const cricket::IceParameters& local_parameters,
const cricket::ServerAddresses& stun_servers, const cricket::ServerAddresses& stun_servers,
const std::vector<cricket::RelayServerConfig>& turn_servers, const std::vector<cricket::RelayServerConfig>& turn_servers,
int32_t candidate_filter) { IceTransportPolicy policy) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
PostCrossThreadTask( PostCrossThreadTask(
*host_thread_, FROM_HERE, *host_thread_, FROM_HERE,
CrossThreadBind(&IceTransportHost::StartGathering, CrossThreadBind(&IceTransportHost::StartGathering,
CrossThreadUnretained(host_.get()), local_parameters, CrossThreadUnretained(host_.get()), local_parameters,
stun_servers, turn_servers, candidate_filter)); stun_servers, turn_servers, policy));
} }
void IceTransportProxy::SetRole(cricket::IceRole role) { void IceTransportProxy::Start(
const cricket::IceParameters& remote_parameters,
cricket::IceRole role,
const std::vector<cricket::Candidate>& initial_remote_candidates) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
PostCrossThreadTask( PostCrossThreadTask(
*host_thread_, FROM_HERE, *host_thread_, FROM_HERE,
CrossThreadBind(&IceTransportHost::SetRole, CrossThreadBind(&IceTransportHost::Start,
CrossThreadUnretained(host_.get()), role)); CrossThreadUnretained(host_.get()), remote_parameters,
role, initial_remote_candidates));
} }
void IceTransportProxy::SetRemoteParameters( void IceTransportProxy::HandleRemoteRestart(
const cricket::IceParameters& remote_parameters) { const cricket::IceParameters& new_remote_parameters) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
PostCrossThreadTask( PostCrossThreadTask(*host_thread_, FROM_HERE,
*host_thread_, FROM_HERE, CrossThreadBind(&IceTransportHost::HandleRemoteRestart,
CrossThreadBind(&IceTransportHost::SetRemoteParameters, CrossThreadUnretained(host_.get()),
CrossThreadUnretained(host_.get()), remote_parameters)); new_remote_parameters));
} }
void IceTransportProxy::AddRemoteCandidate( void IceTransportProxy::AddRemoteCandidate(
...@@ -85,13 +90,6 @@ void IceTransportProxy::AddRemoteCandidate( ...@@ -85,13 +90,6 @@ void IceTransportProxy::AddRemoteCandidate(
CrossThreadUnretained(host_.get()), candidate)); CrossThreadUnretained(host_.get()), candidate));
} }
void IceTransportProxy::ClearRemoteCandidates() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
PostCrossThreadTask(*host_thread_, FROM_HERE,
CrossThreadBind(&IceTransportHost::ClearRemoteCandidates,
CrossThreadUnretained(host_.get())));
}
void IceTransportProxy::OnGatheringStateChanged( void IceTransportProxy::OnGatheringStateChanged(
cricket::IceGatheringState new_state) { cricket::IceGatheringState new_state) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_adapter.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_scheduler.h" #include "third_party/blink/renderer/platform/scheduler/public/frame_scheduler.h"
#include "third_party/webrtc/p2p/base/p2ptransportchannel.h" #include "third_party/webrtc/p2p/base/p2ptransportchannel.h"
...@@ -60,17 +61,17 @@ class IceTransportProxy final { ...@@ -60,17 +61,17 @@ class IceTransportProxy final {
std::unique_ptr<cricket::PortAllocator> port_allocator); std::unique_ptr<cricket::PortAllocator> port_allocator);
~IceTransportProxy(); ~IceTransportProxy();
// These methods are proxied to an IceTransportAdapter instance.
void StartGathering( void StartGathering(
const cricket::IceParameters& local_parameters, const cricket::IceParameters& local_parameters,
const cricket::ServerAddresses& stun_servers, const cricket::ServerAddresses& stun_servers,
const std::vector<cricket::RelayServerConfig>& turn_servers, const std::vector<cricket::RelayServerConfig>& turn_servers,
int32_t candidate_filter); IceTransportPolicy policy);
void Start(const cricket::IceParameters& remote_parameters,
void SetRole(cricket::IceRole role); cricket::IceRole role,
void SetRemoteParameters(const cricket::IceParameters& remote_parameters); const std::vector<cricket::Candidate>& initial_remote_candidates);
void HandleRemoteRestart(const cricket::IceParameters& new_remote_parameters);
void AddRemoteCandidate(const cricket::Candidate& candidate); void AddRemoteCandidate(const cricket::Candidate& candidate);
void ClearRemoteCandidates();
private: private:
// Callbacks from RTCIceTransportHost. // Callbacks from RTCIceTransportHost.
......
...@@ -44,6 +44,12 @@ struct CrossThreadCopier<std::vector<cricket::RelayServerConfig>> ...@@ -44,6 +44,12 @@ struct CrossThreadCopier<std::vector<cricket::RelayServerConfig>>
STATIC_ONLY(CrossThreadCopier); STATIC_ONLY(CrossThreadCopier);
}; };
template <>
struct CrossThreadCopier<std::vector<cricket::Candidate>>
: public CrossThreadCopierPassThrough<std::vector<cricket::Candidate>> {
STATIC_ONLY(CrossThreadCopier);
};
template <> template <>
struct CrossThreadCopier<cricket::Candidate> struct CrossThreadCopier<cricket::Candidate>
: public CrossThreadCopierPassThrough<cricket::Candidate> { : public CrossThreadCopierPassThrough<cricket::Candidate> {
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "third_party/blink/renderer/core/dom/document.h" #include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/events/event.h" #include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/frame/local_frame.h" #include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_proxy.h"
#include "third_party/blink/renderer/modules/peerconnection/rtc_error_util.h" #include "third_party/blink/renderer/modules/peerconnection/rtc_error_util.h"
#include "third_party/blink/renderer/modules/peerconnection/rtc_ice_candidate.h" #include "third_party/blink/renderer/modules/peerconnection/rtc_ice_candidate.h"
#include "third_party/blink/renderer/modules/peerconnection/rtc_ice_gather_options.h" #include "third_party/blink/renderer/modules/peerconnection/rtc_ice_gather_options.h"
...@@ -55,7 +56,7 @@ RTCIceTransport* RTCIceTransport::Create(ExecutionContext* context) { ...@@ -55,7 +56,7 @@ RTCIceTransport* RTCIceTransport::Create(ExecutionContext* context) {
RTCIceTransport::RTCIceTransport(ExecutionContext* context) RTCIceTransport::RTCIceTransport(ExecutionContext* context)
: ContextLifecycleObserver(context) { : ContextLifecycleObserver(context) {
Document* document = ToDocument(GetExecutionContext()); Document* document = ToDocument(context);
LocalFrame* frame = document->GetFrame(); LocalFrame* frame = document->GetFrame();
DCHECK(frame); DCHECK(frame);
...@@ -187,6 +188,17 @@ ConvertIceServers(const HeapVector<RTCIceServer>& ice_servers) { ...@@ -187,6 +188,17 @@ ConvertIceServers(const HeapVector<RTCIceServer>& ice_servers) {
return converted_ice_servers; return converted_ice_servers;
} }
static IceTransportPolicy IceTransportPolicyFromString(const String& str) {
if (str == "relay") {
return IceTransportPolicy::kRelay;
}
if (str == "all") {
return IceTransportPolicy::kAll;
}
NOTREACHED();
return IceTransportPolicy::kAll;
}
void RTCIceTransport::gather(const RTCIceGatherOptions& options, void RTCIceTransport::gather(const RTCIceGatherOptions& options,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (RaiseExceptionIfClosed(exception_state)) { if (RaiseExceptionIfClosed(exception_state)) {
...@@ -214,14 +226,9 @@ void RTCIceTransport::gather(const RTCIceGatherOptions& options, ...@@ -214,14 +226,9 @@ void RTCIceTransport::gather(const RTCIceGatherOptions& options,
return; return;
} }
gathering_state_ = cricket::kIceGatheringGathering; gathering_state_ = cricket::kIceGatheringGathering;
uint32_t candidate_filter = cricket::CF_ALL;
if (options.gatherPolicy() == "relay") {
candidate_filter = cricket::CF_RELAY;
} else {
DCHECK_EQ(options.gatherPolicy(), "all");
}
proxy_->StartGathering(ConvertIceParameters(local_parameters_), stun_servers, proxy_->StartGathering(ConvertIceParameters(local_parameters_), stun_servers,
turn_servers, candidate_filter); turn_servers,
IceTransportPolicyFromString(options.gatherPolicy()));
} }
static cricket::IceRole IceRoleFromString(const String& role_string) { static cricket::IceRole IceRoleFromString(const String& role_string) {
...@@ -267,21 +274,23 @@ void RTCIceTransport::start(const RTCIceParameters& remote_parameters, ...@@ -267,21 +274,23 @@ void RTCIceTransport::start(const RTCIceParameters& remote_parameters,
} }
if (!remote_parameters_) { if (!remote_parameters_) {
// Calling start() for the first time. // Calling start() for the first time.
proxy_->SetRole(role);
role_ = role; role_ = role;
if (remote_candidates_.size() > 0) { if (remote_candidates_.size() > 0) {
for (RTCIceCandidate* remote_candidate : remote_candidates_) {
// This conversion is safe since we throw an exception in
// addRemoteCandidate on malformed ICE candidates.
proxy_->AddRemoteCandidate(
*ConvertToCricketIceCandidate(*remote_candidate));
}
state_ = RTCIceTransportState::kChecking; state_ = RTCIceTransportState::kChecking;
} }
std::vector<cricket::Candidate> initial_remote_candidates;
for (RTCIceCandidate* remote_candidate : remote_candidates_) {
// This conversion is safe since we throw an exception in
// addRemoteCandidate on malformed ICE candidates.
initial_remote_candidates.push_back(
*ConvertToCricketIceCandidate(*remote_candidate));
}
proxy_->Start(ConvertIceParameters(remote_parameters), role,
initial_remote_candidates);
} else { } else {
proxy_->ClearRemoteCandidates();
remote_candidates_.clear(); remote_candidates_.clear();
state_ = RTCIceTransportState::kNew; state_ = RTCIceTransportState::kNew;
proxy_->HandleRemoteRestart(ConvertIceParameters(remote_parameters));
} }
remote_parameters_ = remote_parameters; remote_parameters_ = remote_parameters;
} }
......
...@@ -43,7 +43,7 @@ class MODULES_EXPORT RTCIceTransport final ...@@ -43,7 +43,7 @@ class MODULES_EXPORT RTCIceTransport final
: public EventTargetWithInlineData, : public EventTargetWithInlineData,
public ActiveScriptWrappable<RTCIceTransport>, public ActiveScriptWrappable<RTCIceTransport>,
public ContextLifecycleObserver, public ContextLifecycleObserver,
private IceTransportProxy::Delegate { public IceTransportProxy::Delegate {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
USING_GARBAGE_COLLECTED_MIXIN(RTCIceTransport); USING_GARBAGE_COLLECTED_MIXIN(RTCIceTransport);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment