Create PeerConnection specific environment

Bug: webrtc:15656
Change-Id: I11616e3470798b43cb07a776f5d58669d629e24d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/328960
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41283}
This commit is contained in:
Danil Chapovalov 2023-11-30 08:59:39 +01:00 committed by WebRTC LUCI CQ
parent 13834cfacd
commit 3bdb49b483
6 changed files with 52 additions and 61 deletions

View File

@ -1236,6 +1236,7 @@ rtc_source_set("peer_connection") {
"../api:turn_customizer", "../api:turn_customizer",
"../api/adaptation:resource_adaptation_api", "../api/adaptation:resource_adaptation_api",
"../api/crypto:options", "../api/crypto:options",
"../api/environment",
"../api/rtc_event_log", "../api/rtc_event_log",
"../api/task_queue:pending_task_safety_flag", "../api/task_queue:pending_task_safety_flag",
"../api/transport:bitrate_settings", "../api/transport:bitrate_settings",

View File

@ -76,6 +76,11 @@ class ConnectionContext final
rtc::Thread* network_thread() { return network_thread_; } rtc::Thread* network_thread() { return network_thread_; }
const rtc::Thread* network_thread() const { return network_thread_; } const rtc::Thread* network_thread() const { return network_thread_; }
// Environment associated with the PeerConnectionFactory.
// Note: environments are different for different PeerConnections,
// but they are not supposed to change after creating the PeerConnection.
const Environment& env() const { return env_; }
// Field trials associated with the PeerConnectionFactory. // Field trials associated with the PeerConnectionFactory.
// Note: that there can be different field trials for different // Note: that there can be different field trials for different
// PeerConnections (but they are not supposed change after creating the // PeerConnections (but they are not supposed change after creating the

View File

@ -23,6 +23,7 @@
#include "absl/strings/match.h" #include "absl/strings/match.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "api/environment/environment.h"
#include "api/jsep_ice_candidate.h" #include "api/jsep_ice_candidate.h"
#include "api/media_types.h" #include "api/media_types.h"
#include "api/rtp_parameters.h" #include "api/rtp_parameters.h"
@ -536,9 +537,9 @@ bool PeerConnectionInterface::RTCConfiguration::operator!=(
} }
RTCErrorOr<rtc::scoped_refptr<PeerConnection>> PeerConnection::Create( RTCErrorOr<rtc::scoped_refptr<PeerConnection>> PeerConnection::Create(
const Environment& env,
rtc::scoped_refptr<ConnectionContext> context, rtc::scoped_refptr<ConnectionContext> context,
const PeerConnectionFactoryInterface::Options& options, const PeerConnectionFactoryInterface::Options& options,
std::unique_ptr<RtcEventLog> event_log,
std::unique_ptr<Call> call, std::unique_ptr<Call> call,
const PeerConnectionInterface::RTCConfiguration& configuration, const PeerConnectionInterface::RTCConfiguration& configuration,
PeerConnectionDependencies dependencies) { PeerConnectionDependencies dependencies) {
@ -605,8 +606,8 @@ RTCErrorOr<rtc::scoped_refptr<PeerConnection>> PeerConnection::Create(
// The PeerConnection constructor consumes some, but not all, dependencies. // The PeerConnection constructor consumes some, but not all, dependencies.
auto pc = rtc::make_ref_counted<PeerConnection>( auto pc = rtc::make_ref_counted<PeerConnection>(
context, options, is_unified_plan, std::move(event_log), std::move(call), env, context, options, is_unified_plan, std::move(call), dependencies,
dependencies, dtls_enabled); dtls_enabled);
RTCError init_error = pc->Initialize(configuration, std::move(dependencies)); RTCError init_error = pc->Initialize(configuration, std::move(dependencies));
if (!init_error.ok()) { if (!init_error.ok()) {
RTC_LOG(LS_ERROR) << "PeerConnection initialization failed"; RTC_LOG(LS_ERROR) << "PeerConnection initialization failed";
@ -616,19 +617,18 @@ RTCErrorOr<rtc::scoped_refptr<PeerConnection>> PeerConnection::Create(
} }
PeerConnection::PeerConnection( PeerConnection::PeerConnection(
const Environment& env,
rtc::scoped_refptr<ConnectionContext> context, rtc::scoped_refptr<ConnectionContext> context,
const PeerConnectionFactoryInterface::Options& options, const PeerConnectionFactoryInterface::Options& options,
bool is_unified_plan, bool is_unified_plan,
std::unique_ptr<RtcEventLog> event_log,
std::unique_ptr<Call> call, std::unique_ptr<Call> call,
PeerConnectionDependencies& dependencies, PeerConnectionDependencies& dependencies,
bool dtls_enabled) bool dtls_enabled)
: context_(context), : env_(env),
trials_(std::move(dependencies.trials), &context->field_trials()), context_(context),
options_(options), options_(options),
observer_(dependencies.observer), observer_(dependencies.observer),
is_unified_plan_(is_unified_plan), is_unified_plan_(is_unified_plan),
event_log_(std::move(event_log)),
async_dns_resolver_factory_( async_dns_resolver_factory_(
std::move(dependencies.async_dns_resolver_factory)), std::move(dependencies.async_dns_resolver_factory)),
port_allocator_(std::move(dependencies.allocator)), port_allocator_(std::move(dependencies.allocator)),
@ -648,7 +648,8 @@ PeerConnection::PeerConnection(
data_channel_controller_(this), data_channel_controller_(this),
message_handler_(signaling_thread()), message_handler_(signaling_thread()),
weak_factory_(this) { weak_factory_(this) {
RTC_CHECK(event_log_); // Field trials specific to the peerconnection should be owned by the `env`,
RTC_DCHECK(dependencies.trials == nullptr);
} }
PeerConnection::~PeerConnection() { PeerConnection::~PeerConnection() {
@ -796,7 +797,7 @@ JsepTransportController* PeerConnection::InitializeTransportController_n(
config.transport_observer = this; config.transport_observer = this;
config.rtcp_handler = InitializeRtcpCallback(); config.rtcp_handler = InitializeRtcpCallback();
config.un_demuxable_packet_handler = InitializeUnDemuxablePacketHandler(); config.un_demuxable_packet_handler = InitializeUnDemuxablePacketHandler();
config.event_log = event_log_.get(); config.event_log = &env_.event_log();
#if defined(ENABLE_EXTERNAL_AUTH) #if defined(ENABLE_EXTERNAL_AUTH)
config.enable_external_auth = true; config.enable_external_auth = true;
#endif #endif
@ -815,7 +816,7 @@ JsepTransportController* PeerConnection::InitializeTransportController_n(
} }
}; };
config.field_trials = trials_.get(); config.field_trials = &env_.field_trials();
transport_controller_.reset(new JsepTransportController( transport_controller_.reset(new JsepTransportController(
network_thread(), port_allocator_.get(), network_thread(), port_allocator_.get(),
@ -2256,12 +2257,12 @@ bool PeerConnection::StartRtcEventLog_w(
if (!worker_thread_safety_->alive()) { if (!worker_thread_safety_->alive()) {
return false; return false;
} }
return event_log_->StartLogging(std::move(output), output_period_ms); return env_.event_log().StartLogging(std::move(output), output_period_ms);
} }
void PeerConnection::StopRtcEventLog_w() { void PeerConnection::StopRtcEventLog_w() {
RTC_DCHECK_RUN_ON(worker_thread()); RTC_DCHECK_RUN_ON(worker_thread());
event_log_->StopLogging(); env_.event_log().StopLogging();
} }
absl::optional<rtc::SSLRole> PeerConnection::GetSctpSslRole_n() { absl::optional<rtc::SSLRole> PeerConnection::GetSctpSslRole_n() {

View File

@ -27,6 +27,7 @@
#include "api/crypto/crypto_options.h" #include "api/crypto/crypto_options.h"
#include "api/data_channel_interface.h" #include "api/data_channel_interface.h"
#include "api/dtls_transport_interface.h" #include "api/dtls_transport_interface.h"
#include "api/environment/environment.h"
#include "api/field_trials_view.h" #include "api/field_trials_view.h"
#include "api/ice_transport_interface.h" #include "api/ice_transport_interface.h"
#include "api/jsep.h" #include "api/jsep.h"
@ -110,9 +111,9 @@ class PeerConnection : public PeerConnectionInternal,
// Note that the function takes ownership of dependencies, and will // Note that the function takes ownership of dependencies, and will
// either use them or release them, whether it succeeds or fails. // either use them or release them, whether it succeeds or fails.
static RTCErrorOr<rtc::scoped_refptr<PeerConnection>> Create( static RTCErrorOr<rtc::scoped_refptr<PeerConnection>> Create(
const Environment& env,
rtc::scoped_refptr<ConnectionContext> context, rtc::scoped_refptr<ConnectionContext> context,
const PeerConnectionFactoryInterface::Options& options, const PeerConnectionFactoryInterface::Options& options,
std::unique_ptr<RtcEventLog> event_log,
std::unique_ptr<Call> call, std::unique_ptr<Call> call,
const PeerConnectionInterface::RTCConfiguration& configuration, const PeerConnectionInterface::RTCConfiguration& configuration,
PeerConnectionDependencies dependencies); PeerConnectionDependencies dependencies);
@ -428,7 +429,7 @@ class PeerConnection : public PeerConnectionInternal,
void TeardownDataChannelTransport_n(RTCError error) void TeardownDataChannelTransport_n(RTCError error)
RTC_RUN_ON(network_thread()); RTC_RUN_ON(network_thread());
const FieldTrialsView& trials() const override { return *trials_; } const FieldTrialsView& trials() const override { return env_.field_trials(); }
bool ConfiguredForMedia() const; bool ConfiguredForMedia() const;
@ -441,10 +442,10 @@ class PeerConnection : public PeerConnectionInternal,
protected: protected:
// Available for rtc::scoped_refptr creation // Available for rtc::scoped_refptr creation
PeerConnection(rtc::scoped_refptr<ConnectionContext> context, PeerConnection(const Environment& env,
rtc::scoped_refptr<ConnectionContext> context,
const PeerConnectionFactoryInterface::Options& options, const PeerConnectionFactoryInterface::Options& options,
bool is_unified_plan, bool is_unified_plan,
std::unique_ptr<RtcEventLog> event_log,
std::unique_ptr<Call> call, std::unique_ptr<Call> call,
PeerConnectionDependencies& dependencies, PeerConnectionDependencies& dependencies,
bool dtls_enabled); bool dtls_enabled);
@ -597,23 +598,14 @@ class PeerConnection : public PeerConnectionInternal,
std::function<void(const RtpPacketReceived& parsed_packet)> std::function<void(const RtpPacketReceived& parsed_packet)>
InitializeUnDemuxablePacketHandler(); InitializeUnDemuxablePacketHandler();
const Environment env_;
const rtc::scoped_refptr<ConnectionContext> context_; const rtc::scoped_refptr<ConnectionContext> context_;
// Field trials active for this PeerConnection is the first of:
// a) Specified in PeerConnectionDependencies (owned).
// b) Accessed via ConnectionContext (e.g PeerConnectionFactoryDependencies>
// c) Created as Default (FieldTrialBasedConfig).
const AlwaysValidPointer<const FieldTrialsView, FieldTrialBasedConfig>
trials_;
const PeerConnectionFactoryInterface::Options options_; const PeerConnectionFactoryInterface::Options options_;
PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) = PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
nullptr; nullptr;
const bool is_unified_plan_; const bool is_unified_plan_;
// The EventLog needs to outlive `call_` (and any other object that uses it).
const std::unique_ptr<RtcEventLog> event_log_
RTC_PT_GUARDED_BY(worker_thread());
IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) = IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) =
kIceConnectionNew; kIceConnectionNew;
PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_ PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_

View File

@ -220,6 +220,23 @@ PeerConnectionFactory::CreatePeerConnectionOrError(
PeerConnectionDependencies dependencies) { PeerConnectionDependencies dependencies) {
RTC_DCHECK_RUN_ON(signaling_thread()); RTC_DCHECK_RUN_ON(signaling_thread());
EnvironmentFactory env_factory(context_->env());
// Field trials active for this PeerConnection is the first of:
// a) Specified in the PeerConnectionDependencies
// b) Specified in the PeerConnectionFactoryDependencies
// c) Created as default by the EnvironmentFactory.
env_factory.Set(std::move(dependencies.trials));
if (event_log_factory_ != nullptr) {
worker_thread()->BlockingCall([&] {
Environment env_for_rtc_event_log = env_factory.Create();
env_factory.Set(event_log_factory_->Create(env_for_rtc_event_log));
});
}
const Environment env = env_factory.Create();
// Set internal defaults if optional dependencies are not set. // Set internal defaults if optional dependencies are not set.
if (!dependencies.cert_generator) { if (!dependencies.cert_generator) {
dependencies.cert_generator = dependencies.cert_generator =
@ -227,11 +244,10 @@ PeerConnectionFactory::CreatePeerConnectionOrError(
network_thread()); network_thread());
} }
if (!dependencies.allocator) { if (!dependencies.allocator) {
const FieldTrialsView* trials =
dependencies.trials ? dependencies.trials.get() : &field_trials();
dependencies.allocator = std::make_unique<cricket::BasicPortAllocator>( dependencies.allocator = std::make_unique<cricket::BasicPortAllocator>(
context_->default_network_manager(), context_->default_socket_factory(), context_->default_network_manager(), context_->default_socket_factory(),
configuration.turn_customizer, /*relay_port_factory=*/nullptr, trials); configuration.turn_customizer, /*relay_port_factory=*/nullptr,
&env.field_trials());
dependencies.allocator->SetPortRange( dependencies.allocator->SetPortRange(
configuration.port_allocator_config.min_port, configuration.port_allocator_config.min_port,
configuration.port_allocator_config.max_port); configuration.port_allocator_config.max_port);
@ -247,19 +263,13 @@ PeerConnectionFactory::CreatePeerConnectionOrError(
dependencies.allocator->SetNetworkIgnoreMask(options().network_ignore_mask); dependencies.allocator->SetNetworkIgnoreMask(options().network_ignore_mask);
dependencies.allocator->SetVpnList(configuration.vpn_list); dependencies.allocator->SetVpnList(configuration.vpn_list);
std::unique_ptr<RtcEventLog> event_log =
worker_thread()->BlockingCall([this] { return CreateRtcEventLog_w(); });
const FieldTrialsView* trials =
dependencies.trials ? dependencies.trials.get() : &field_trials();
std::unique_ptr<Call> call = std::unique_ptr<Call> call =
worker_thread()->BlockingCall([this, &event_log, trials, &configuration] { worker_thread()->BlockingCall([this, &env, &configuration] {
return CreateCall_w(event_log.get(), *trials, configuration); return CreateCall_w(env, configuration);
}); });
auto result = PeerConnection::Create(context_, options_, std::move(event_log), auto result = PeerConnection::Create(env, context_, options_, std::move(call),
std::move(call), configuration, configuration, std::move(dependencies));
std::move(dependencies));
if (!result.ok()) { if (!result.ok()) {
return result.MoveError(); return result.MoveError();
} }
@ -300,23 +310,12 @@ rtc::scoped_refptr<AudioTrackInterface> PeerConnectionFactory::CreateAudioTrack(
return AudioTrackProxy::Create(signaling_thread(), track); return AudioTrackProxy::Create(signaling_thread(), track);
} }
std::unique_ptr<RtcEventLog> PeerConnectionFactory::CreateRtcEventLog_w() {
RTC_DCHECK_RUN_ON(worker_thread());
auto encoding_type = RtcEventLog::EncodingType::NewFormat;
if (field_trials().IsDisabled("WebRTC-RtcEventLogNewFormat"))
encoding_type = RtcEventLog::EncodingType::Legacy;
return event_log_factory_ ? event_log_factory_->Create(encoding_type)
: std::make_unique<RtcEventLogNull>();
}
std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w( std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w(
RtcEventLog* event_log, const Environment& env,
const FieldTrialsView& field_trials,
const PeerConnectionInterface::RTCConfiguration& configuration) { const PeerConnectionInterface::RTCConfiguration& configuration) {
RTC_DCHECK_RUN_ON(worker_thread()); RTC_DCHECK_RUN_ON(worker_thread());
CallConfig call_config(event_log, network_thread()); CallConfig call_config(env, network_thread());
if (!media_engine() || !context_->call_factory()) { if (!media_engine() || !context_->call_factory()) {
return nullptr; return nullptr;
} }
@ -329,7 +328,7 @@ std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w(
FieldTrialParameter<DataRate> max_bandwidth("max", FieldTrialParameter<DataRate> max_bandwidth("max",
DataRate::KilobitsPerSec(2000)); DataRate::KilobitsPerSec(2000));
ParseFieldTrial({&min_bandwidth, &start_bandwidth, &max_bandwidth}, ParseFieldTrial({&min_bandwidth, &start_bandwidth, &max_bandwidth},
field_trials.Lookup("WebRTC-PcFactoryDefaultBitrates")); env.field_trials().Lookup("WebRTC-PcFactoryDefaultBitrates"));
call_config.bitrate_config.min_bitrate_bps = call_config.bitrate_config.min_bitrate_bps =
rtc::saturated_cast<int>(min_bandwidth->bps()); rtc::saturated_cast<int>(min_bandwidth->bps());
@ -339,7 +338,6 @@ std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w(
rtc::saturated_cast<int>(max_bandwidth->bps()); rtc::saturated_cast<int>(max_bandwidth->bps());
call_config.fec_controller_factory = fec_controller_factory_.get(); call_config.fec_controller_factory = fec_controller_factory_.get();
call_config.task_queue_factory = task_queue_factory_.get();
call_config.network_state_predictor_factory = call_config.network_state_predictor_factory =
network_state_predictor_factory_.get(); network_state_predictor_factory_.get();
call_config.neteq_factory = neteq_factory_.get(); call_config.neteq_factory = neteq_factory_.get();
@ -352,7 +350,6 @@ std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w(
RTC_LOG(LS_INFO) << "Using default network controller factory"; RTC_LOG(LS_INFO) << "Using default network controller factory";
} }
call_config.trials = &field_trials;
call_config.rtp_transport_controller_send_factory = call_config.rtp_transport_controller_send_factory =
transport_controller_send_factory_.get(); transport_controller_send_factory_.get();
call_config.metronome = metronome_.get(); call_config.metronome = metronome_.get();

View File

@ -29,7 +29,6 @@
#include "api/network_state_predictor.h" #include "api/network_state_predictor.h"
#include "api/peer_connection_interface.h" #include "api/peer_connection_interface.h"
#include "api/rtc_error.h" #include "api/rtc_error.h"
#include "api/rtc_event_log/rtc_event_log.h"
#include "api/rtc_event_log/rtc_event_log_factory_interface.h" #include "api/rtc_event_log/rtc_event_log_factory_interface.h"
#include "api/rtp_parameters.h" #include "api/rtp_parameters.h"
#include "api/scoped_refptr.h" #include "api/scoped_refptr.h"
@ -53,8 +52,6 @@ class BasicPacketSocketFactory;
namespace webrtc { namespace webrtc {
class RtcEventLog;
class PeerConnectionFactory : public PeerConnectionFactoryInterface { class PeerConnectionFactory : public PeerConnectionFactoryInterface {
public: public:
// Creates a PeerConnectionFactory. It returns nullptr on initialization // Creates a PeerConnectionFactory. It returns nullptr on initialization
@ -135,10 +132,8 @@ class PeerConnectionFactory : public PeerConnectionFactoryInterface {
bool IsTrialEnabled(absl::string_view key) const; bool IsTrialEnabled(absl::string_view key) const;
std::unique_ptr<RtcEventLog> CreateRtcEventLog_w();
std::unique_ptr<Call> CreateCall_w( std::unique_ptr<Call> CreateCall_w(
RtcEventLog* event_log, const Environment& env,
const FieldTrialsView& field_trials,
const PeerConnectionInterface::RTCConfiguration& configuration); const PeerConnectionInterface::RTCConfiguration& configuration);
rtc::scoped_refptr<ConnectionContext> context_; rtc::scoped_refptr<ConnectionContext> context_;