Replace Thread::Invoke with Thread::BlockingCall

BlockingCall doesn't take rtc::Location parameter and thus most of the dependencies on location can be removed

Bug: webrtc:11318
Change-Id: I91a17e342dd9a9e3e2c8f7fbe267474c98a8d0e5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/274620
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38045}
This commit is contained in:
Danil Chapovalov
2022-09-08 18:38:10 +02:00
committed by WebRTC LUCI CQ
parent b190ca9e70
commit 9e09a1f327
54 changed files with 209 additions and 320 deletions

View File

@ -52,7 +52,6 @@
#include "pc/webrtc_session_description_factory.h"
#include "rtc_base/helpers.h"
#include "rtc_base/ip_address.h"
#include "rtc_base/location.h"
#include "rtc_base/logging.h"
#include "rtc_base/net_helper.h"
#include "rtc_base/network.h"
@ -525,7 +524,7 @@ PeerConnection::PeerConnection(
data_channel_controller_(this),
message_handler_(signaling_thread()),
weak_factory_(this) {
worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
worker_thread()->BlockingCall([this] {
RTC_DCHECK_RUN_ON(worker_thread());
worker_thread_safety_ = PendingTaskSafetyFlag::Create();
if (!call_)
@ -569,7 +568,7 @@ PeerConnection::~PeerConnection() {
// port_allocator_ and transport_controller_ live on the network thread and
// should be destroyed there.
transport_controller_copy_ = nullptr;
network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
network_thread()->BlockingCall([this] {
RTC_DCHECK_RUN_ON(network_thread());
TeardownDataChannelTransport_n();
transport_controller_.reset();
@ -579,7 +578,7 @@ PeerConnection::~PeerConnection() {
});
// call_ and event_log_ must be destroyed on the worker thread.
worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
worker_thread()->BlockingCall([this] {
RTC_DCHECK_RUN_ON(worker_thread());
worker_thread_safety_->SetNotAlive();
call_.reset();
@ -618,7 +617,7 @@ RTCError PeerConnection::Initialize(
// Network thread initialization.
transport_controller_copy_ =
network_thread()->Invoke<JsepTransportController*>(RTC_FROM_HERE, [&] {
network_thread()->BlockingCall([&] {
RTC_DCHECK_RUN_ON(network_thread());
network_thread_safety_ = PendingTaskSafetyFlag::Create();
InitializePortAllocatorResult pa_result = InitializePortAllocator_n(
@ -940,13 +939,12 @@ RtpTransportInternal* PeerConnection::GetRtpTransport(const std::string& mid) {
// TODO(bugs.webrtc.org/9987): Avoid the thread jump.
// This might be done by caching the value on the signaling thread.
RTC_DCHECK_RUN_ON(signaling_thread());
return network_thread()->Invoke<RtpTransportInternal*>(
RTC_FROM_HERE, [this, &mid] {
RTC_DCHECK_RUN_ON(network_thread());
auto rtp_transport = transport_controller_->GetRtpTransport(mid);
RTC_DCHECK(rtp_transport);
return rtp_transport;
});
return network_thread()->BlockingCall([this, &mid] {
RTC_DCHECK_RUN_ON(network_thread());
auto rtp_transport = transport_controller_->GetRtpTransport(mid);
RTC_DCHECK(rtp_transport);
return rtp_transport;
});
}
RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
@ -1556,8 +1554,7 @@ RTCError PeerConnection::SetConfiguration(
// Apply part of the configuration on the network thread. In theory this
// shouldn't fail.
if (!network_thread()->Invoke<bool>(
RTC_FROM_HERE,
if (!network_thread()->BlockingCall(
[this, needs_ice_restart, &ice_config, &stun_servers, &turn_servers,
&modified_config, has_local_description] {
RTC_DCHECK_RUN_ON(network_thread());
@ -1583,8 +1580,8 @@ RTCError PeerConnection::SetConfiguration(
if (configuration_.active_reset_srtp_params !=
modified_config.active_reset_srtp_params) {
// TODO(tommi): merge invokes
network_thread()->Invoke<void>(RTC_FROM_HERE, [this, &modified_config] {
// TODO(tommi): merge BlockingCalls
network_thread()->BlockingCall([this, &modified_config] {
RTC_DCHECK_RUN_ON(network_thread());
transport_controller_->SetActiveResetSrtpParams(
modified_config.active_reset_srtp_params);
@ -1603,8 +1600,7 @@ RTCError PeerConnection::SetConfiguration(
video_channel->media_channel()));
}
worker_thread()->Invoke<void>(
RTC_FROM_HERE,
worker_thread()->BlockingCall(
[channels = std::move(channels),
allow_codec_switching = *modified_config.allow_codec_switching]() {
for (auto* ch : channels)
@ -1643,8 +1639,7 @@ bool PeerConnection::RemoveIceCandidates(
RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) {
if (!worker_thread()->IsCurrent()) {
return worker_thread()->Invoke<RTCError>(
RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); });
return worker_thread()->BlockingCall([&]() { return SetBitrate(bitrate); });
}
RTC_DCHECK_RUN_ON(worker_thread());
@ -1685,8 +1680,8 @@ RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) {
void PeerConnection::SetAudioPlayout(bool playout) {
if (!worker_thread()->IsCurrent()) {
worker_thread()->Invoke<void>(
RTC_FROM_HERE, [this, playout] { SetAudioPlayout(playout); });
worker_thread()->BlockingCall(
[this, playout] { SetAudioPlayout(playout); });
return;
}
auto audio_state = context_->media_engine()->voice().GetAudioState();
@ -1695,8 +1690,8 @@ void PeerConnection::SetAudioPlayout(bool playout) {
void PeerConnection::SetAudioRecording(bool recording) {
if (!worker_thread()->IsCurrent()) {
worker_thread()->Invoke<void>(
RTC_FROM_HERE, [this, recording] { SetAudioRecording(recording); });
worker_thread()->BlockingCall(
[this, recording] { SetAudioRecording(recording); });
return;
}
auto audio_state = context_->media_engine()->voice().GetAudioState();
@ -1706,9 +1701,8 @@ void PeerConnection::SetAudioRecording(bool recording) {
void PeerConnection::AddAdaptationResource(
rtc::scoped_refptr<Resource> resource) {
if (!worker_thread()->IsCurrent()) {
return worker_thread()->Invoke<void>(RTC_FROM_HERE, [this, resource]() {
return AddAdaptationResource(resource);
});
return worker_thread()->BlockingCall(
[this, resource]() { return AddAdaptationResource(resource); });
}
RTC_DCHECK_RUN_ON(worker_thread());
if (!call_) {
@ -1724,8 +1718,7 @@ bool PeerConnection::ConfiguredForMedia() const {
bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
int64_t output_period_ms) {
return worker_thread()->Invoke<bool>(
RTC_FROM_HERE,
return worker_thread()->BlockingCall(
[this, output = std::move(output), output_period_ms]() mutable {
return StartRtcEventLog_w(std::move(output), output_period_ms);
});
@ -1741,7 +1734,7 @@ bool PeerConnection::StartRtcEventLog(
}
void PeerConnection::StopRtcEventLog() {
worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { StopRtcEventLog_w(); });
worker_thread()->BlockingCall([this] { StopRtcEventLog_w(); });
}
rtc::scoped_refptr<DtlsTransportInterface>
@ -1755,11 +1748,10 @@ PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) {
RTC_DCHECK_RUN_ON(signaling_thread());
// TODO(bugs.webrtc.org/9987): Avoid the thread jump.
// This might be done by caching the value on the signaling thread.
return network_thread()->Invoke<rtc::scoped_refptr<DtlsTransport>>(
RTC_FROM_HERE, [this, mid]() {
RTC_DCHECK_RUN_ON(network_thread());
return transport_controller_->LookupDtlsTransportByMid(mid);
});
return network_thread()->BlockingCall([this, mid]() {
RTC_DCHECK_RUN_ON(network_thread());
return transport_controller_->LookupDtlsTransportByMid(mid);
});
}
rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport()
@ -1856,7 +1848,7 @@ void PeerConnection::Close() {
rtp_manager_->Close();
}
network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
network_thread()->BlockingCall([this] {
// Data channels will already have been unset via the DestroyAllChannels()
// call above, which triggers a call to TeardownDataChannelTransport_n().
// TODO(tommi): ^^ That's not exactly optimal since this is yet another
@ -1870,7 +1862,7 @@ void PeerConnection::Close() {
}
});
worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
worker_thread()->BlockingCall([this] {
RTC_DCHECK_RUN_ON(worker_thread());
worker_thread_safety_->SetNotAlive();
call_.reset();
@ -2225,11 +2217,10 @@ bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) {
absl::optional<rtc::SSLRole> dtls_role;
if (sctp_mid_s_) {
dtls_role = network_thread()->Invoke<absl::optional<rtc::SSLRole>>(
RTC_FROM_HERE, [this] {
RTC_DCHECK_RUN_ON(network_thread());
return transport_controller_->GetDtlsRole(*sctp_mid_n_);
});
dtls_role = network_thread()->BlockingCall([this] {
RTC_DCHECK_RUN_ON(network_thread());
return transport_controller_->GetDtlsRole(*sctp_mid_n_);
});
if (!dtls_role && sdp_handler_->is_caller().has_value()) {
// This works fine if we are the offerer, but can be a mistake if
// we are the answerer and the remote offer is ACTIVE. In that
@ -2259,11 +2250,10 @@ bool PeerConnection::GetSslRole(const std::string& content_name,
return false;
}
auto dtls_role = network_thread()->Invoke<absl::optional<rtc::SSLRole>>(
RTC_FROM_HERE, [this, content_name]() {
RTC_DCHECK_RUN_ON(network_thread());
return transport_controller_->GetDtlsRole(content_name);
});
auto dtls_role = network_thread()->BlockingCall([this, content_name]() {
RTC_DCHECK_RUN_ON(network_thread());
return transport_controller_->GetDtlsRole(content_name);
});
if (dtls_role) {
*role = *dtls_role;
return true;
@ -2360,7 +2350,7 @@ bool PeerConnection::IceRestartPending(const std::string& content_name) const {
}
bool PeerConnection::NeedsIceRestart(const std::string& content_name) const {
return network_thread()->Invoke<bool>(RTC_FROM_HERE, [this, &content_name] {
return network_thread()->BlockingCall([this, &content_name] {
RTC_DCHECK_RUN_ON(network_thread());
return transport_controller_->NeedsIceRestart(content_name);
});
@ -2489,8 +2479,7 @@ bool PeerConnection::GetLocalCandidateMediaIndex(
Call::Stats PeerConnection::GetCallStats() {
if (!worker_thread()->IsCurrent()) {
return worker_thread()->Invoke<Call::Stats>(
RTC_FROM_HERE, [this] { return GetCallStats(); });
return worker_thread()->BlockingCall([this] { return GetCallStats(); });
}
RTC_DCHECK_RUN_ON(worker_thread());
rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;