Propagate Environment into fake ice transport

Query current time from that Environment instead of through global functions.

Bug: webrtc:42223992
Change-Id: Ib2e0ba3abca3e0c3bc16ef2f1c858253f0247e68
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/480220
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47972}
diff --git a/media/sctp/dcsctp_transport_unittest.cc b/media/sctp/dcsctp_transport_unittest.cc
index 5c23a71..8973803 100644
--- a/media/sctp/dcsctp_transport_unittest.cc
+++ b/media/sctp/dcsctp_transport_unittest.cc
@@ -15,7 +15,6 @@
 #include <utility>
 
 #include "api/environment/environment.h"
-#include "api/environment/environment_factory.h"
 #include "api/priority.h"
 #include "api/rtc_error.h"
 #include "api/transport/data_channel_transport_interface.h"
@@ -29,6 +28,7 @@
 #include "rtc_base/copy_on_write_buffer.h"
 #include "rtc_base/thread.h"
 #include "system_wrappers/include/clock.h"
+#include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/run_loop.h"
@@ -73,9 +73,9 @@
 class Peer {
  public:
   Peer()
-      : fake_dtls_transport_(kTransportName, kComponent),
-        simulated_clock_(1000),
-        env_(CreateEnvironment(&simulated_clock_)) {
+      : simulated_clock_(1000),
+        env_(CreateTestEnvironment({.time = &simulated_clock_})),
+        fake_dtls_transport_(env_, kTransportName, kComponent) {
     auto socket_ptr = std::make_unique<dcsctp::MockDcSctpSocket>();
     socket_ = socket_ptr.get();
 
@@ -92,9 +92,9 @@
     sctp_transport_->SetOnConnectedCallback([this]() { sink_.OnConnected(); });
   }
 
-  FakeDtlsTransport fake_dtls_transport_;
   SimulatedClock simulated_clock_;
   Environment env_;
+  FakeDtlsTransport fake_dtls_transport_;
   dcsctp::MockDcSctpSocket* socket_;
   std::unique_ptr<DcSctpTransport> sctp_transport_;
   NiceMock<MockDataChannelSink> sink_;
diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn
index 5e9e53d..8b41c90 100644
--- a/p2p/BUILD.gn
+++ b/p2p/BUILD.gn
@@ -945,6 +945,7 @@
       "../api:field_trials",
       "../api:ice_transport_interface",
       "../api:sequence_checker",
+      "../api/environment",
       "../api/task_queue",
       "../api/task_queue:pending_task_safety_flag",
       "../api/transport:enums",
diff --git a/p2p/dtls/dtls_transport_unittest.cc b/p2p/dtls/dtls_transport_unittest.cc
index 181550a..71429e0 100644
--- a/p2p/dtls/dtls_transport_unittest.cc
+++ b/p2p/dtls/dtls_transport_unittest.cc
@@ -132,8 +132,8 @@
     }
 
     auto fake_ice_transport = std::make_unique<FakeIceTransportInternal>(
-        absl::StrCat("fake-", name_), 0,
-        /* network_thread= */ nullptr, /* field_trials_string= */ "");
+        env, absl::StrCat("fake-", name_), 0,
+        /* network_thread= */ nullptr);
     if (rtt_estimate) {
       fake_ice_transport->set_rtt_estimate(
           async_delay_ms_ ? std::optional<int>(async_delay_ms_) : std::nullopt,
diff --git a/p2p/dtls/fake_dtls_transport.h b/p2p/dtls/fake_dtls_transport.h
index bec2ea2..f1eb613 100644
--- a/p2p/dtls/fake_dtls_transport.h
+++ b/p2p/dtls/fake_dtls_transport.h
@@ -22,6 +22,7 @@
 
 #include "absl/strings/string_view.h"
 #include "api/dtls_transport_interface.h"
+#include "api/environment/environment.h"
 #include "api/ice_transport_interface.h"
 #include "api/rtc_error.h"
 #include "api/scoped_refptr.h"
@@ -88,14 +89,18 @@
 
   // If this constructor is called, a new fake ICE transport will be created,
   // and this FakeDtlsTransport will take the ownership.
-  FakeDtlsTransport(const std::string& name, int component)
+  FakeDtlsTransport(const Environment& env,
+                    const std::string& name,
+                    int component)
       : FakeDtlsTransport(
-            std::make_unique<FakeIceTransportInternal>(name, component)) {}
-  FakeDtlsTransport(const std::string& name,
+            std::make_unique<FakeIceTransportInternal>(env, name, component)) {}
+  FakeDtlsTransport(const Environment& env,
+                    const std::string& name,
                     int component,
                     Thread* network_thread)
       : FakeDtlsTransport(
-            std::make_unique<FakeIceTransportInternal>(name,
+            std::make_unique<FakeIceTransportInternal>(env,
+                                                       name,
                                                        component,
                                                        network_thread)) {}
 
diff --git a/p2p/test/fake_ice_transport.h b/p2p/test/fake_ice_transport.h
index f41e324..0cec0fa 100644
--- a/p2p/test/fake_ice_transport.h
+++ b/p2p/test/fake_ice_transport.h
@@ -24,7 +24,7 @@
 #include "absl/functional/any_invocable.h"
 #include "absl/strings/string_view.h"
 #include "api/candidate.h"
-#include "api/field_trials.h"
+#include "api/environment/environment.h"
 #include "api/ice_transport_interface.h"
 #include "api/sequence_checker.h"
 #include "api/task_queue/pending_task_safety_flag.h"
@@ -49,8 +49,6 @@
 #include "rtc_base/socket.h"
 #include "rtc_base/task_queue_for_test.h"
 #include "rtc_base/thread_annotations.h"
-#include "rtc_base/time_utils.h"
-#include "test/create_test_field_trials.h"
 
 namespace webrtc {
 
@@ -59,16 +57,16 @@
 // constructor).
 class FakeIceTransportInternal : public IceTransportInternal {
  public:
-  explicit FakeIceTransportInternal(absl::string_view name,
+  explicit FakeIceTransportInternal(const Environment& env,
+                                    absl::string_view name,
                                     int component,
-                                    TaskQueueBase* network_thread = nullptr,
-                                    absl::string_view field_trials_string = "")
+                                    TaskQueueBase* network_thread = nullptr)
       : IceTransportInternal(network_thread),
+        env_(env),
         name_(name),
         component_(component),
         network_thread_(network_thread ? network_thread
-                                       : TaskQueueBase::Current()),
-        field_trials_(CreateTestFieldTrials(field_trials_string)) {
+                                       : TaskQueueBase::Current()) {
     RTC_DCHECK(network_thread_);
   }
 
@@ -351,7 +349,8 @@
       }
     }
 
-    SentPacketInfo sent_packet(options.packet_id, TimeMillis());
+    SentPacketInfo sent_packet(options.packet_id,
+                               env_.clock().TimeInMilliseconds());
     NotifySentPacket(this, sent_packet);
     return static_cast<int>(len);
   }
@@ -434,7 +433,7 @@
   bool SendIcePing() {
     RTC_DCHECK_RUN_ON(network_thread_);
     RTC_DLOG(LS_INFO) << name_ << ": SendIcePing()";
-    last_sent_ping_timestamp_ = TimeMicros();
+    last_sent_ping_timestamp_ = env_.clock().TimeInMicroseconds();
     auto msg = std::make_unique<IceMessage>(STUN_BINDING_REQUEST);
     MaybeAddDtlsPiggybackingAttributes(msg.get());
     msg->AddFingerprint();
@@ -490,7 +489,7 @@
 
   int GetCountOfReceivedPackets() { return received_packets_; }
 
-  const FieldTrialsView* field_trials() const { return &field_trials_; }
+  const FieldTrialsView* field_trials() const { return &env_.field_trials(); }
 
   void set_drop_non_stun_unless_writable(bool value) {
     drop_non_stun_unless_writable_ = value;
@@ -561,7 +560,7 @@
 
   void ReceivePacketInternal(const CopyOnWriteBuffer& packet) {
     RTC_DCHECK_RUN_ON(network_thread_);
-    auto now = TimeMicros();
+    int64_t now = env_.clock().TimeInMicroseconds();
     if (auto msg = GetStunMessage(packet)) {
       RTC_LOG(LS_INFO) << name_ << ": RECV STUN message: "
                        << ", data[0]: "
@@ -621,6 +620,7 @@
     return stun_msg;
   }
 
+  const Environment env_;
   const std::string name_;
   const int component_;
   FakeIceTransportInternal* dest_ RTC_GUARDED_BY(network_thread_) = nullptr;
@@ -662,7 +662,6 @@
   DtlsStunPiggybackCallbacks dtls_stun_piggyback_callbacks_;
   std::map<int, int> received_stun_messages_per_type;
   int received_packets_ = 0;
-  FieldTrials field_trials_;
   bool drop_non_stun_unless_writable_ = false;
 };
 
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index d87b42e..2b14254 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -4494,6 +4494,7 @@
       "../rtc_base/system:plan_b_only",
       "../rtc_base/task_utils:repeating_task",
       "../system_wrappers",
+      "../test:create_test_environment",
       "../test:frame_generator_capturer",
       "../test:run_loop",
       "../test:test_support",
diff --git a/pc/channel_unittest.cc b/pc/channel_unittest.cc
index 15b4227..5a2bd15 100644
--- a/pc/channel_unittest.cc
+++ b/pc/channel_unittest.cc
@@ -22,7 +22,7 @@
 #include "absl/functional/any_invocable.h"
 #include "api/audio_options.h"
 #include "api/crypto/crypto_options.h"
-#include "api/field_trials.h"
+#include "api/environment/environment.h"
 #include "api/jsep.h"
 #include "api/media_types.h"
 #include "api/rtc_error.h"
@@ -60,7 +60,7 @@
 #include "rtc_base/task_queue_for_test.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/unique_id_generator.h"
-#include "test/create_test_field_trials.h"
+#include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/run_loop.h"
@@ -222,11 +222,11 @@
         }
       } else {
         // Confirmed to work with KT_RSA and KT_ECDSA.
-        fake_rtp_dtls_transport1_.reset(new FakeDtlsTransport(
-            "channel1", ICE_CANDIDATE_COMPONENT_RTP, network_thread_));
+        fake_rtp_dtls_transport1_ = std::make_unique<FakeDtlsTransport>(
+            env_, "channel1", ICE_CANDIDATE_COMPONENT_RTP, network_thread_);
         if (!(flags1 & RTCP_MUX)) {
-          fake_rtcp_dtls_transport1_.reset(new FakeDtlsTransport(
-              "channel1", ICE_CANDIDATE_COMPONENT_RTCP, network_thread_));
+          fake_rtcp_dtls_transport1_ = std::make_unique<FakeDtlsTransport>(
+              env_, "channel1", ICE_CANDIDATE_COMPONENT_RTCP, network_thread_);
         }
         if (flags1 & DTLS) {
           auto cert1 = RTCCertificate::Create(
@@ -247,11 +247,11 @@
         }
       } else {
         // Confirmed to work with KT_RSA and KT_ECDSA.
-        fake_rtp_dtls_transport2_.reset(new FakeDtlsTransport(
-            "channel2", ICE_CANDIDATE_COMPONENT_RTP, network_thread_));
+        fake_rtp_dtls_transport2_ = std::make_unique<FakeDtlsTransport>(
+            env_, "channel2", ICE_CANDIDATE_COMPONENT_RTP, network_thread_);
         if (!(flags2 & RTCP_MUX)) {
-          fake_rtcp_dtls_transport2_.reset(new FakeDtlsTransport(
-              "channel2", ICE_CANDIDATE_COMPONENT_RTCP, network_thread_));
+          fake_rtcp_dtls_transport2_ = std::make_unique<FakeDtlsTransport>(
+              env_, "channel2", ICE_CANDIDATE_COMPONENT_RTCP, network_thread_);
         }
         if (flags2 & DTLS) {
           auto cert2 = RTCCertificate::Create(
@@ -348,7 +348,7 @@
       PacketTransportInternal* rtp_packet_transport,
       PacketTransportInternal* rtcp_packet_transport) {
     auto rtp_transport = std::make_unique<RtpTransport>(
-        rtcp_packet_transport == nullptr, field_trials_);
+        rtcp_packet_transport == nullptr, env_.field_trials());
 
     SendTask(network_thread_,
              [&rtp_transport, rtp_packet_transport, rtcp_packet_transport] {
@@ -364,7 +364,7 @@
       DtlsTransportInternal* rtp_dtls_transport,
       DtlsTransportInternal* rtcp_dtls_transport) {
     auto dtls_srtp_transport = std::make_unique<DtlsSrtpTransport>(
-        rtcp_dtls_transport == nullptr, field_trials_);
+        rtcp_dtls_transport == nullptr, env_.field_trials());
 
     SendTask(network_thread_,
              [&dtls_srtp_transport, rtp_dtls_transport, rtcp_dtls_transport] {
@@ -1769,6 +1769,7 @@
   }
 
   test::RunLoop main_thread_;
+  const Environment env_ = CreateTestEnvironment();
   // TODO(pbos): Remove playout from all media channels and let renderers mute
   // themselves.
   const bool verify_playout_;
@@ -1799,7 +1800,6 @@
   Buffer rtcp_packet_;
   CandidatePairInterface* last_selected_candidate_pair_;
   UniqueRandomIdGenerator ssrc_generator_;
-  FieldTrials field_trials_ = CreateTestFieldTrials();
 };
 
 template <>
diff --git a/pc/datagram_connection_unittest.cc b/pc/datagram_connection_unittest.cc
index 3abb034..3f35734 100644
--- a/pc/datagram_connection_unittest.cc
+++ b/pc/datagram_connection_unittest.cc
@@ -85,10 +85,10 @@
     std::string transport_name2 = "FakeTransport2";
 
     auto ice1 = std::make_unique<FakeIceTransportInternal>(
-        transport_name1, ICE_CANDIDATE_COMPONENT_RTP);
+        env_, transport_name1, ICE_CANDIDATE_COMPONENT_RTP);
     ice1->SetAsync(true);
     auto ice2 = std::make_unique<FakeIceTransportInternal>(
-        transport_name2, ICE_CANDIDATE_COMPONENT_RTP);
+        env_, transport_name2, ICE_CANDIDATE_COMPONENT_RTP);
     ice2->SetAsync(true);
     ice1_ = ice1.get();
     ice2_ = ice2.get();
diff --git a/pc/dtls_srtp_transport_integrationtest.cc b/pc/dtls_srtp_transport_integrationtest.cc
index 19eb7f9..6aace42 100644
--- a/pc/dtls_srtp_transport_integrationtest.cc
+++ b/pc/dtls_srtp_transport_integrationtest.cc
@@ -93,7 +93,7 @@
   }
   std::unique_ptr<FakeIceTransportInternal> MakeIceTransport(IceRole role) {
     auto ice_transport = std::make_unique<FakeIceTransportInternal>(
-        "fake_" + absl::StrCat(static_cast<int>(role)), 0);
+        env_, "fake_" + absl::StrCat(static_cast<int>(role)), 0);
     ice_transport->SetAsync(true);
     ice_transport->SetAsyncDelay(0);
     ice_transport->SetIceRole(role);
diff --git a/pc/dtls_srtp_transport_unittest.cc b/pc/dtls_srtp_transport_unittest.cc
index 48d5d94..d2ae119 100644
--- a/pc/dtls_srtp_transport_unittest.cc
+++ b/pc/dtls_srtp_transport_unittest.cc
@@ -19,7 +19,7 @@
 #include <vector>
 
 #include "absl/strings/string_view.h"
-#include "api/field_trials.h"
+#include "api/environment/environment.h"
 #include "api/make_ref_counted.h"
 #include "api/rtp_header_extension_id.h"
 #include "api/transport/ecn_marking.h"
@@ -39,7 +39,7 @@
 #include "rtc_base/copy_on_write_buffer.h"
 #include "rtc_base/rtc_certificate.h"
 #include "rtc_base/ssl_identity.h"
-#include "test/create_test_field_trials.h"
+#include "test/create_test_environment.h"
 #include "test/gtest.h"
 #include "test/run_loop.h"
 
@@ -65,8 +65,8 @@
       FakeDtlsTransport* rtp_dtls,
       FakeDtlsTransport* rtcp_dtls,
       bool rtcp_mux_enabled) {
-    auto dtls_srtp_transport =
-        std::make_unique<DtlsSrtpTransport>(rtcp_mux_enabled, field_trials_);
+    auto dtls_srtp_transport = std::make_unique<DtlsSrtpTransport>(
+        rtcp_mux_enabled, env_.field_trials());
 
     dtls_srtp_transport->SetDtlsTransports(rtp_dtls, rtcp_dtls);
 
@@ -264,23 +264,24 @@
     SendRecvRtcpPackets();
   }
 
+  std::unique_ptr<FakeDtlsTransport> CreateFakeDtlsTransport(
+      absl::string_view name,
+      int component) {
+    return std::make_unique<FakeDtlsTransport>(
+        make_ref_counted<FakeIceTransport>(
+            std::make_unique<FakeIceTransportInternal>(env_, name, component)));
+  }
+
   test::RunLoop main_thread_;
+  const Environment env_ = CreateTestEnvironment();
   std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport1_;
   std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport2_;
   TransportObserver transport_observer1_;
   TransportObserver transport_observer2_;
 
   int sequence_number_ = 0;
-  FieldTrials field_trials_ = CreateTestFieldTrials();
 };
 
-std::unique_ptr<FakeDtlsTransport> CreateFakeDtlsTransport(
-    absl::string_view name,
-    int component) {
-  return std::make_unique<FakeDtlsTransport>(make_ref_counted<FakeIceTransport>(
-      std::make_unique<FakeIceTransportInternal>(name, component)));
-}
-
 // Tests that if RTCP muxing is enabled and transports are set after RTP
 // transport finished the handshake, SRTP is set up.
 TEST_F(DtlsSrtpTransportTest, SetTransportsAfterHandshakeCompleteWithRtcpMux) {
@@ -401,11 +402,11 @@
   EXPECT_TRUE(dtls_srtp_transport2_->IsSrtpActive());
 
   auto rtp_ice3 = std::make_unique<FakeIceTransportInternal>(
-      "audio", ICE_CANDIDATE_COMPONENT_RTP);
+      env_, "audio", ICE_CANDIDATE_COMPONENT_RTP);
   auto rtp_dtls3 = std::make_unique<FakeDtlsTransport>(
       make_ref_counted<FakeIceTransport>(std::move(rtp_ice3)));
   auto rtp_ice4 = std::make_unique<FakeIceTransportInternal>(
-      "audio", ICE_CANDIDATE_COMPONENT_RTP);
+      env_, "audio", ICE_CANDIDATE_COMPONENT_RTP);
   auto rtp_dtls4 = std::make_unique<FakeDtlsTransport>(
       make_ref_counted<FakeIceTransport>(std::move(rtp_ice4)));
 
diff --git a/pc/dtls_transport_unittest.cc b/pc/dtls_transport_unittest.cc
index 68889fc..1ee0ad1 100644
--- a/pc/dtls_transport_unittest.cc
+++ b/pc/dtls_transport_unittest.cc
@@ -15,6 +15,7 @@
 #include <vector>
 
 #include "api/dtls_transport_interface.h"
+#include "api/environment/environment.h"
 #include "api/make_ref_counted.h"
 #include "api/rtc_error.h"
 #include "api/scoped_refptr.h"
@@ -25,6 +26,7 @@
 #include "rtc_base/fake_ssl_identity.h"
 #include "rtc_base/rtc_certificate.h"
 #include "rtc_base/ssl_identity.h"
+#include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/run_loop.h"
@@ -72,7 +74,7 @@
 
   void CreateTransport(FakeSSLCertificate* certificate = nullptr) {
     internal_transport_ = std::make_unique<FakeDtlsTransport>(
-        "audio", ICE_CANDIDATE_COMPONENT_RTP);
+        env_, "audio", ICE_CANDIDATE_COMPONENT_RTP);
     if (certificate) {
       internal_transport_->SetRemoteSSLCertificate(certificate);
     }
@@ -89,7 +91,7 @@
   void CompleteDtlsHandshake() {
     auto fake_dtls1 = internal_transport_.get();
     auto fake_dtls2 = std::make_unique<FakeDtlsTransport>(
-        "audio", ICE_CANDIDATE_COMPONENT_RTP);
+        env_, "audio", ICE_CANDIDATE_COMPONENT_RTP);
     auto cert1 =
         RTCCertificate::Create(SSLIdentity::Create("session1", KT_DEFAULT));
     fake_dtls1->SetLocalCertificate(cert1);
@@ -100,14 +102,15 @@
   }
 
   test::RunLoop main_thread_;
+  const Environment env_ = CreateTestEnvironment();
   scoped_refptr<DtlsTransport> transport_;
   std::unique_ptr<FakeDtlsTransport> internal_transport_;
   TestDtlsTransportObserver observer_;
 };
 
 TEST_F(DtlsTransportTest, CreateClearDelete) {
-  auto transport =
-      std::make_unique<FakeDtlsTransport>("audio", ICE_CANDIDATE_COMPONENT_RTP);
+  auto transport = std::make_unique<FakeDtlsTransport>(
+      env_, "audio", ICE_CANDIDATE_COMPONENT_RTP);
   auto dtls_transport = make_ref_counted<DtlsTransport>(transport.get());
   ASSERT_EQ(DtlsTransportState::kNew, dtls_transport->Information().state());
   dtls_transport->Clear(transport.get());
diff --git a/pc/ice_transport_unittest.cc b/pc/ice_transport_unittest.cc
index 9b91e86..2af8274 100644
--- a/pc/ice_transport_unittest.cc
+++ b/pc/ice_transport_unittest.cc
@@ -42,8 +42,8 @@
 };
 
 TEST_F(IceTransportTest, CreateNonSelfDeletingTransport) {
-  auto cricket_transport =
-      std::make_unique<FakeIceTransportInternal>("name", 0, nullptr);
+  auto cricket_transport = std::make_unique<FakeIceTransportInternal>(
+      CreateTestEnvironment(), "name", 0, nullptr);
   auto ice_transport =
       make_ref_counted<IceTransportWithPointer>(cricket_transport.get());
   EXPECT_EQ(ice_transport->internal(), cricket_transport.get());
diff --git a/pc/jsep_transport_controller_unittest.cc b/pc/jsep_transport_controller_unittest.cc
index 12a154c..0b990a5 100644
--- a/pc/jsep_transport_controller_unittest.cc
+++ b/pc/jsep_transport_controller_unittest.cc
@@ -100,7 +100,8 @@
       int component,
       IceTransportInit init) override {
     return make_ref_counted<FakeIceTransport>(
-        std::make_unique<FakeIceTransportInternal>(transport_name, component));
+        std::make_unique<FakeIceTransportInternal>(init.env(), transport_name,
+                                                   component));
   }
 };
 
diff --git a/pc/jsep_transport_unittest.cc b/pc/jsep_transport_unittest.cc
index 12b5645..3676710 100644
--- a/pc/jsep_transport_unittest.cc
+++ b/pc/jsep_transport_unittest.cc
@@ -23,7 +23,7 @@
 
 #include "api/candidate.h"
 #include "api/crypto/crypto_options.h"
-#include "api/field_trials.h"
+#include "api/environment/environment.h"
 #include "api/ice_transport_interface.h"
 #include "api/jsep.h"
 #include "api/make_ref_counted.h"
@@ -55,7 +55,7 @@
 #include "rtc_base/ssl_fingerprint.h"
 #include "rtc_base/ssl_identity.h"
 #include "rtc_base/ssl_stream_adapter.h"
-#include "test/create_test_field_trials.h"
+#include "test/create_test_environment.h"
 #include "test/gtest.h"
 #include "test/run_loop.h"
 
@@ -104,7 +104,7 @@
       PacketTransportInternal* rtp_packet_transport,
       PacketTransportInternal* rtcp_packet_transport) {
     auto srtp_transport = std::make_unique<SrtpTransport>(
-        rtcp_packet_transport == nullptr, field_trials_);
+        rtcp_packet_transport == nullptr, env_.field_trials());
 
     srtp_transport->SetRtpPacketTransport(rtp_packet_transport);
     if (rtcp_packet_transport) {
@@ -117,7 +117,7 @@
       std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
       std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport) {
     auto dtls_srtp_transport = std::make_unique<DtlsSrtpTransport>(
-        rtcp_dtls_transport == nullptr, field_trials_);
+        rtcp_dtls_transport == nullptr, env_.field_trials());
     dtls_srtp_transport->SetDtlsTransportsOwned(std::move(rtp_dtls_transport),
                                                 std::move(rtcp_dtls_transport));
     return dtls_srtp_transport;
@@ -127,7 +127,7 @@
   // FakeIceTransportInternal.
   std::unique_ptr<JsepTransport> CreateJsepTransport2(bool rtcp_mux_enabled) {
     auto ice_internal = std::make_unique<FakeIceTransportInternal>(
-        kTransportName, ICE_CANDIDATE_COMPONENT_RTP);
+        env_, kTransportName, ICE_CANDIDATE_COMPONENT_RTP);
     auto ice = CreateIceTransport(std::move(ice_internal));
     auto rtp_dtls_transport = std::make_unique<FakeDtlsTransport>(ice);
 
@@ -136,7 +136,7 @@
     scoped_refptr<IceTransportInterface> rtcp_ice;
     if (!rtcp_mux_enabled) {
       rtcp_ice_internal = std::make_unique<FakeIceTransportInternal>(
-          kTransportName, ICE_CANDIDATE_COMPONENT_RTCP);
+          env_, kTransportName, ICE_CANDIDATE_COMPONENT_RTCP);
       rtcp_ice = CreateIceTransport(std::move(rtcp_ice_internal));
       rtcp_dtls_transport = std::make_unique<FakeDtlsTransport>(rtcp_ice);
     }
@@ -190,9 +190,9 @@
   void OnRtcpMuxActive() { signal_rtcp_mux_active_received_ = true; }
 
   test::RunLoop main_thread_;
+  const Environment env_ = CreateTestEnvironment();
   std::unique_ptr<JsepTransport> jsep_transport_;
   bool signal_rtcp_mux_active_received_ = false;
-  FieldTrials field_trials_ = CreateTestFieldTrials();
 };
 
 // The parameterized tests cover both cases when RTCP mux is enable and
diff --git a/pc/rtp_sender_receiver_unittest.cc b/pc/rtp_sender_receiver_unittest.cc
index 3733e28..b4746ce 100644
--- a/pc/rtp_sender_receiver_unittest.cc
+++ b/pc/rtp_sender_receiver_unittest.cc
@@ -127,7 +127,7 @@
         fake_call_(env_, worker_thread_.get(), network_thread_.get()),
         local_stream_(MediaStream::Create(kStreamId1)) {
     rtp_dtls_transport_ = std::make_unique<FakeDtlsTransport>(
-        "fake_dtls_transport", ICE_CANDIDATE_COMPONENT_RTP);
+        env_, "fake_dtls_transport", ICE_CANDIDATE_COMPONENT_RTP);
     rtp_transport_ = CreateDtlsSrtpTransport();
 
     // Create the channels, discard the result; we get them later.
diff --git a/pc/rtp_transceiver_unittest.cc b/pc/rtp_transceiver_unittest.cc
index 1968e77..44d25a2 100644
--- a/pc/rtp_transceiver_unittest.cc
+++ b/pc/rtp_transceiver_unittest.cc
@@ -312,7 +312,8 @@
 TEST_F(RtpTransceiverTestWithFakeCall, TransportNameIsUpdated) {
   const std::string content_name("my_mid");
 
-  auto fake_dtls = std::make_unique<FakeDtlsTransport>("test_transport", false);
+  auto fake_dtls =
+      std::make_unique<FakeDtlsTransport>(env(), "test_transport", false);
   auto rtp_transport = std::make_unique<RtpTransport>(/*rtcp_mux_enabled=*/true,
                                                       env().field_trials());
   rtp_transport->SetRtpPacketTransport(fake_dtls.get());
@@ -1139,7 +1140,8 @@
 TEST_F(RtpTransceiverTestWithFakeCall, OnNetworkRouteChangedForwardsToChannel) {
   const std::string content_name("my_mid");
 
-  auto fake_dtls = std::make_unique<FakeDtlsTransport>("test_transport", false);
+  auto fake_dtls =
+      std::make_unique<FakeDtlsTransport>(env(), "test_transport", false);
   auto rtp_transport = std::make_unique<RtpTransport>(/*rtcp_mux_enabled=*/true,
                                                       env().field_trials());
   rtp_transport->SetRtpPacketTransport(fake_dtls.get());
@@ -1185,7 +1187,8 @@
        OnNetworkRouteChangedForwardsToVideoChannel) {
   const std::string content_name("my_mid");
 
-  auto fake_dtls = std::make_unique<FakeDtlsTransport>("test_transport", false);
+  auto fake_dtls =
+      std::make_unique<FakeDtlsTransport>(env(), "test_transport", false);
   auto rtp_transport = std::make_unique<RtpTransport>(/*rtcp_mux_enabled=*/true,
                                                       env().field_trials());
   rtp_transport->SetRtpPacketTransport(fake_dtls.get());
@@ -1240,7 +1243,7 @@
                                                       env().field_trials());
   context()->network_thread()->BlockingCall([&]() {
     fake_dtls = std::make_unique<FakeDtlsTransport>(
-        "test_transport", 0, context()->network_thread());
+        env(), "test_transport", 0, context()->network_thread());
     rtp_transport->SetRtpPacketTransport(fake_dtls.get());
   });
 
diff --git a/pc/sctp_transport_unittest.cc b/pc/sctp_transport_unittest.cc
index d3b796e..5349538 100644
--- a/pc/sctp_transport_unittest.cc
+++ b/pc/sctp_transport_unittest.cc
@@ -19,6 +19,7 @@
 
 #include "absl/memory/memory.h"
 #include "api/dtls_transport_interface.h"
+#include "api/environment/environment.h"
 #include "api/make_ref_counted.h"
 #include "api/priority.h"
 #include "api/rtc_error.h"
@@ -32,6 +33,7 @@
 #include "p2p/dtls/fake_dtls_transport.h"
 #include "pc/dtls_transport.h"
 #include "rtc_base/copy_on_write_buffer.h"
+#include "test/create_test_environment.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/run_loop.h"
@@ -134,7 +136,7 @@
 
   void CreateTransport() {
     internal_transport_ = std::make_unique<FakeDtlsTransport>(
-        "audio", ICE_CANDIDATE_COMPONENT_RTP);
+        env_, "audio", ICE_CANDIDATE_COMPONENT_RTP);
     dtls_transport_ =
         make_ref_counted<DtlsTransport>(internal_transport_.get());
     internal_transport_->SubscribeDtlsTransportState(
@@ -162,6 +164,7 @@
   }
 
   test::RunLoop main_thread_;
+  const Environment env_ = CreateTestEnvironment();
   scoped_refptr<SctpTransport> transport_;
   scoped_refptr<DtlsTransport> dtls_transport_;
   std::unique_ptr<FakeDtlsTransport> internal_transport_;
@@ -171,7 +174,8 @@
 TEST(SctpTransportSimpleTest, CreateClearDelete) {
   test::RunLoop main_thread;
   std::unique_ptr<DtlsTransportInternal> internal_transport =
-      std::make_unique<FakeDtlsTransport>("audio", ICE_CANDIDATE_COMPONENT_RTP);
+      std::make_unique<FakeDtlsTransport>(CreateTestEnvironment(), "audio",
+                                          ICE_CANDIDATE_COMPONENT_RTP);
   scoped_refptr<DtlsTransport> dtls_transport =
       make_ref_counted<DtlsTransport>(internal_transport.get());
 
diff --git a/pc/test/fake_peer_connection_for_stats.h b/pc/test/fake_peer_connection_for_stats.h
index adfd8cf..c2478cf 100644
--- a/pc/test/fake_peer_connection_for_stats.h
+++ b/pc/test/fake_peer_connection_for_stats.h
@@ -89,8 +89,8 @@
       const std::string& transport_name,
       int component,
       IceTransportInit init) override {
-    auto internal =
-        std::make_unique<FakeIceTransportInternal>(transport_name, component);
+    auto internal = std::make_unique<FakeIceTransportInternal>(
+        init.env(), transport_name, component);
     return make_ref_counted<FakeIceTransport>(std::move(internal));
   }
 };
@@ -441,7 +441,7 @@
     auto dtls_transport = transport_controller_->LookupDtlsTransportByMid(mid);
     if (!dtls_transport) {
       auto fake_dtls = std::make_unique<FakeDtlsTransport>(
-          transport_name, ICE_CANDIDATE_COMPONENT_RTP);
+          env(), transport_name, ICE_CANDIDATE_COMPONENT_RTP);
       auto wrapper = make_ref_counted<DtlsTransport>(fake_dtls.get());
       fake_dtls_transports_[mid] = std::move(fake_dtls);
       dtls_transport = wrapper;
@@ -487,7 +487,7 @@
     auto dtls_transport = transport_controller_->LookupDtlsTransportByMid(mid);
     if (!dtls_transport) {
       auto fake_dtls = std::make_unique<FakeDtlsTransport>(
-          transport_name, ICE_CANDIDATE_COMPONENT_RTP);
+          env(), transport_name, ICE_CANDIDATE_COMPONENT_RTP);
       auto wrapper = make_ref_counted<DtlsTransport>(fake_dtls.get());
       fake_dtls_transports_[mid] = std::move(fake_dtls);
       dtls_transport = wrapper;
diff --git a/pc/test/integration_test_helpers.h b/pc/test/integration_test_helpers.h
index 0bfe0a7..a9ed9b4 100644
--- a/pc/test/integration_test_helpers.h
+++ b/pc/test/integration_test_helpers.h
@@ -836,8 +836,11 @@
 
 class MockIceTransport : public IceTransportInterface {
  public:
-  MockIceTransport(const std::string& name, int component)
-      : internal_(std::make_unique<FakeIceTransportInternal>(name,
+  MockIceTransport(const Environment& env,
+                   absl::string_view name,
+                   int component)
+      : internal_(std::make_unique<FakeIceTransportInternal>(env,
+                                                             name,
                                                              component,
                                                              nullptr)) {}
   ~MockIceTransport() override = default;
@@ -855,7 +858,8 @@
       int component,
       IceTransportInit init) override {
     RecordIceTransportCreated();
-    return make_ref_counted<MockIceTransport>(transport_name, component);
+    return make_ref_counted<MockIceTransport>(init.env(), transport_name,
+                                              component);
   }
   MOCK_METHOD(void, RecordIceTransportCreated, ());
 };