In RtcEventLogEncoderNewFormat use propagated instead of global field trials

Bug: webrtc:10335
Change-Id: Id407a7bc25375cadccba4cf4ae0c771f22a19a0e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/333581
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41644}
diff --git a/logging/BUILD.gn b/logging/BUILD.gn
index 9189055..5b91a39 100644
--- a/logging/BUILD.gn
+++ b/logging/BUILD.gn
@@ -353,6 +353,7 @@
 
   deps = [
     ":rtc_event_number_encodings",
+    "../api:field_trials_view",
     "../api:rtp_headers",
     "../api:rtp_parameters",
     "../api/transport:network_control",
@@ -362,7 +363,6 @@
     "../rtc_base:checks",
     "../rtc_base:logging",
     "../rtc_base:safe_conversions",
-    "../system_wrappers:field_trial",
   ]
   absl_deps = [
     "//third_party/abseil-cpp/absl/memory",
@@ -617,6 +617,7 @@
         "../api/environment:environment_factory",
         "../api/rtc_event_log",
         "../api/rtc_event_log:rtc_event_log_factory",
+        "../api/transport:field_trial_based_config",
         "../api/units:time_delta",
         "../api/units:timestamp",
         "../call",
diff --git a/logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.cc b/logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.cc
index 4c662fd..ebda3bf 100644
--- a/logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.cc
+++ b/logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.cc
@@ -10,8 +10,11 @@
 
 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.h"
 
+#include <type_traits>
+
 #include "absl/types/optional.h"
 #include "api/array_view.h"
+#include "api/field_trials_view.h"
 #include "api/network_state_predictor.h"
 #include "logging/rtc_event_log/dependency_descriptor_encoder_decoder.h"
 #include "logging/rtc_event_log/encoder/blob_encoding.h"
@@ -62,7 +65,6 @@
 #include "modules/rtp_rtcp/source/rtp_packet.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
-#include "system_wrappers/include/field_trial.h"
 
 // *.pb.h files are generated at build-time by the protobuf compiler.
 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
@@ -381,10 +383,12 @@
   }
   proto_batch->set_raw_packet_blobs(EncodeBlobs(scrubed_packets));
 }
+}  // namespace
 
-template <typename EventType, typename ProtoType>
-void EncodeRtpPacket(const std::vector<const EventType*>& batch,
-                     ProtoType* proto_batch) {
+template <typename Batch, typename ProtoType>
+void RtcEventLogEncoderNewFormat::EncodeRtpPacket(const Batch& batch,
+                                                  ProtoType* proto_batch) {
+  using EventType = std::remove_pointer_t<typename Batch::value_type>;
   if (batch.empty()) {
     return;
   }
@@ -459,8 +463,7 @@
   {
     // TODO(webrtc:14975) Remove this kill switch after DD in RTC event log has
     //                    been rolled out.
-    if (!webrtc::field_trial::IsDisabled(
-            "WebRTC-RtcEventLogEncodeDependencyDescriptor")) {
+    if (encode_dependency_descriptor_) {
       std::vector<rtc::ArrayView<const uint8_t>> raw_dds(batch.size());
       bool has_dd = false;
       for (size_t i = 0; i < batch.size(); ++i) {
@@ -674,15 +677,13 @@
     proto_batch->set_voice_activity_deltas(encoded_deltas);
   }
 }
-}  // namespace
 
-RtcEventLogEncoderNewFormat::RtcEventLogEncoderNewFormat() {
-  encode_neteq_set_minimum_delay_kill_switch_ = false;
-  if (webrtc::field_trial::IsEnabled(
-          "WebRTC-RtcEventLogEncodeNetEqSetMinimumDelayKillSwitch")) {
-    encode_neteq_set_minimum_delay_kill_switch_ = true;
-  }
-}
+RtcEventLogEncoderNewFormat::RtcEventLogEncoderNewFormat(
+    const FieldTrialsView& field_trials)
+    : encode_neteq_set_minimum_delay_kill_switch_(field_trials.IsEnabled(
+          "WebRTC-RtcEventLogEncodeNetEqSetMinimumDelayKillSwitch")),
+      encode_dependency_descriptor_(!field_trials.IsDisabled(
+          "WebRTC-RtcEventLogEncodeDependencyDescriptor")) {}
 
 std::string RtcEventLogEncoderNewFormat::EncodeLogStart(int64_t timestamp_us,
                                                         int64_t utc_time_us) {
diff --git a/logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.h b/logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.h
index 6747f41..6a65709 100644
--- a/logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.h
+++ b/logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.h
@@ -18,6 +18,7 @@
 #include <vector>
 
 #include "api/array_view.h"
+#include "api/field_trials_view.h"
 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder.h"
 
 namespace webrtc {
@@ -59,7 +60,7 @@
 
 class RtcEventLogEncoderNewFormat final : public RtcEventLogEncoder {
  public:
-  RtcEventLogEncoderNewFormat();
+  explicit RtcEventLogEncoderNewFormat(const FieldTrialsView& field_trials);
   ~RtcEventLogEncoderNewFormat() override = default;
 
   std::string EncodeBatch(
@@ -71,8 +72,6 @@
   std::string EncodeLogEnd(int64_t timestamp_us) override;
 
  private:
-  bool encode_neteq_set_minimum_delay_kill_switch_ = false;
-
   // Encoding entry-point for the various RtcEvent subclasses.
   void EncodeAlrState(rtc::ArrayView<const RtcEventAlrState*> batch,
                       rtclog2::EventStream* event_stream);
@@ -157,6 +156,11 @@
   void EncodeVideoSendStreamConfig(
       rtc::ArrayView<const RtcEventVideoSendStreamConfig*> batch,
       rtclog2::EventStream* event_stream);
+  template <typename Batch, typename ProtoType>
+  void EncodeRtpPacket(const Batch& batch, ProtoType* proto_batch);
+
+  const bool encode_neteq_set_minimum_delay_kill_switch_;
+  const bool encode_dependency_descriptor_;
 };
 
 }  // namespace webrtc
diff --git a/logging/rtc_event_log/encoder/rtc_event_log_encoder_unittest.cc b/logging/rtc_event_log/encoder/rtc_event_log_encoder_unittest.cc
index 612f85b..73ff67e 100644
--- a/logging/rtc_event_log/encoder/rtc_event_log_encoder_unittest.cc
+++ b/logging/rtc_event_log/encoder/rtc_event_log_encoder_unittest.cc
@@ -14,6 +14,7 @@
 #include <string>
 #include <tuple>
 
+#include "api/transport/field_trial_based_config.h"
 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder_legacy.h"
 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder_new_format.h"
 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder_v3.h"
@@ -40,10 +41,14 @@
 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
 #include "rtc_base/fake_clock.h"
 #include "rtc_base/random.h"
+#include "test/explicit_key_value_config.h"
 #include "test/field_trial.h"
 #include "test/gtest.h"
 
 namespace webrtc {
+
+using test::ExplicitKeyValueConfig;
+
 class RtcEventLogEncoderTest
     : public ::testing::TestWithParam<
           std::tuple<int, RtcEventLog::EncodingType, size_t, bool>> {
@@ -55,22 +60,26 @@
         event_count_(std::get<2>(GetParam())),
         force_repeated_fields_(std::get<3>(GetParam())),
         gen_(seed_ * 880001UL),
-        verifier_(encoding_type_) {
+        verifier_(encoding_type_) {}
+  ~RtcEventLogEncoderTest() override = default;
+
+  std::unique_ptr<RtcEventLogEncoder> CreateEncoder() {
+    std::unique_ptr<RtcEventLogEncoder> encoder;
     switch (encoding_type_) {
       case RtcEventLog::EncodingType::Legacy:
-        encoder_ = std::make_unique<RtcEventLogEncoderLegacy>();
+        encoder = std::make_unique<RtcEventLogEncoderLegacy>();
         break;
       case RtcEventLog::EncodingType::NewFormat:
-        encoder_ = std::make_unique<RtcEventLogEncoderNewFormat>();
+        encoder = std::make_unique<RtcEventLogEncoderNewFormat>(
+            FieldTrialBasedConfig());
         break;
       case RtcEventLog::EncodingType::ProtoFree:
-        encoder_ = std::make_unique<RtcEventLogEncoderV3>();
+        encoder = std::make_unique<RtcEventLogEncoderV3>();
         break;
     }
-    encoded_ =
-        encoder_->EncodeLogStart(rtc::TimeMillis(), rtc::TimeUTCMillis());
+    encoded_ = encoder->EncodeLogStart(rtc::TimeMillis(), rtc::TimeUTCMillis());
+    return encoder;
   }
-  ~RtcEventLogEncoderTest() override = default;
 
   // ANA events have some optional fields, so we want to make sure that we get
   // correct behavior both when all of the values are there, as well as when
@@ -92,7 +101,6 @@
   void TestRtpPackets();
 
   std::deque<std::unique_ptr<RtcEvent>> history_;
-  std::unique_ptr<RtcEventLogEncoder> encoder_;
   ParsedRtcEventLog parsed_log_;
   const uint64_t seed_;
   Random prng_;
@@ -108,12 +116,13 @@
     const std::vector<std::unique_ptr<RtcEventAudioNetworkAdaptation>>&
         events) {
   ASSERT_TRUE(history_.empty()) << "Function should be called once per test.";
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
 
   for (auto& event : events) {
     history_.push_back(event->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& ana_configs = parsed_log_.audio_network_adaptation_events();
 
@@ -172,6 +181,7 @@
   const std::vector<uint32_t> kSsrcPool = {0x00000000, 0x12345678, 0xabcdef01,
                                            0xffffffff, 0x20171024, 0x19840730,
                                            0x19831230};
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
 
   // TODO(terelius): Test extensions for legacy encoding, too.
   RtpHeaderExtensionMap extension_map;
@@ -193,7 +203,7 @@
   }
 
   // Encode and parse.
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   // For each SSRC, make sure the RTP packets associated with it to have been
@@ -213,6 +223,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventAlrState) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventAlrState>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_) ? gen_.NewAlrState()
@@ -220,7 +231,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& alr_state_events = parsed_log_.alr_state_events();
 
@@ -234,6 +245,7 @@
   if (encoding_type_ == RtcEventLog::EncodingType::Legacy) {
     return;
   }
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventRouteChange>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_) ? gen_.NewRouteChange()
@@ -241,7 +253,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& route_change_events = parsed_log_.route_change_events();
 
@@ -252,6 +264,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventRemoteEstimate) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventRemoteEstimate>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_)
@@ -260,7 +273,7 @@
     history_.push_back(std::make_unique<RtcEventRemoteEstimate>(*events[i]));
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& parsed_events = parsed_log_.remote_estimate_events();
 
@@ -395,6 +408,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventAudioPlayout) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   // SSRCs will be randomly assigned out of this small pool, significant only
   // in that it also covers such edge cases as SSRC = 0 and SSRC = 0xffffffff.
   // The pool is intentionally small, so as to produce collisions.
@@ -414,7 +428,7 @@
     original_events_by_ssrc[ssrc].push_back(std::move(event));
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& parsed_playout_events_by_ssrc =
@@ -443,6 +457,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventNetEqSetMinimumDelayDecoded) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   // SSRCs will be randomly assigned out of this small pool, significant only
   // in that it also covers such edge cases as SSRC = 0 and SSRC = 0xffffffff.
   // The pool is intentionally small, so as to produce collisions.
@@ -461,7 +476,7 @@
     original_events_by_ssrc[ssrc].push_back(std::move(event));
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& parsed_neteq_set_minimum_delay_events_by_ssrc =
@@ -502,13 +517,14 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventAudioReceiveStreamConfig) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   uint32_t ssrc = prng_.Rand<uint32_t>();
   RtpHeaderExtensionMap extensions = gen_.NewRtpHeaderExtensionMap();
   std::unique_ptr<RtcEventAudioReceiveStreamConfig> event =
       gen_.NewAudioReceiveStreamConfig(ssrc, extensions);
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& audio_recv_configs = parsed_log_.audio_recv_configs();
 
@@ -518,13 +534,14 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventAudioSendStreamConfig) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   uint32_t ssrc = prng_.Rand<uint32_t>();
   RtpHeaderExtensionMap extensions = gen_.NewRtpHeaderExtensionMap();
   std::unique_ptr<RtcEventAudioSendStreamConfig> event =
       gen_.NewAudioSendStreamConfig(ssrc, extensions);
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& audio_send_configs = parsed_log_.audio_send_configs();
 
@@ -533,6 +550,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventBweUpdateDelayBased) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventBweUpdateDelayBased>> events(
       event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
@@ -542,7 +560,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& bwe_delay_updates = parsed_log_.bwe_delay_updates();
@@ -554,6 +572,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventBweUpdateLossBased) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventBweUpdateLossBased>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_)
@@ -562,7 +581,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& bwe_loss_updates = parsed_log_.bwe_loss_updates();
@@ -577,6 +596,7 @@
   if (encoding_type_ == RtcEventLog::EncodingType::Legacy) {
     return;
   }
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventGenericPacketReceived>> events(
       event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
@@ -586,7 +606,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& packets_received = parsed_log_.generic_packets_received();
@@ -602,6 +622,7 @@
   if (encoding_type_ == RtcEventLog::EncodingType::Legacy) {
     return;
   }
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventGenericPacketSent>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_)
@@ -610,7 +631,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& packets_sent = parsed_log_.generic_packets_sent();
@@ -625,6 +646,7 @@
   if (encoding_type_ == RtcEventLog::EncodingType::Legacy) {
     return;
   }
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventGenericAckReceived>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_)
@@ -633,7 +655,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& decoded_events = parsed_log_.generic_acks_received();
@@ -645,6 +667,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventDtlsTransportState) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventDtlsTransportState>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_)
@@ -653,7 +676,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& dtls_transport_states = parsed_log_.dtls_transport_states();
@@ -670,6 +693,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventDtlsWritableState) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventDtlsWritableState>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_)
@@ -678,7 +702,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& dtls_writable_states = parsed_log_.dtls_writable_states();
@@ -696,6 +720,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventFrameDecoded) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   // SSRCs will be randomly assigned out of this small pool, significant only
   // in that it also covers such edge cases as SSRC = 0 and SSRC = 0xffffffff.
   // The pool is intentionally small, so as to produce collisions.
@@ -715,7 +740,7 @@
     original_events_by_ssrc[ssrc].push_back(std::move(event));
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   auto status = parsed_log_.ParseString(encoded_);
   if (!status.ok())
     RTC_LOG(LS_ERROR) << status.message();
@@ -751,11 +776,12 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventIceCandidatePairConfig) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::unique_ptr<RtcEventIceCandidatePairConfig> event =
       gen_.NewIceCandidatePairConfig();
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& ice_candidate_pair_configs =
       parsed_log_.ice_candidate_pair_configs();
@@ -767,10 +793,11 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventIceCandidatePair) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::unique_ptr<RtcEventIceCandidatePair> event = gen_.NewIceCandidatePair();
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& ice_candidate_pair_events =
       parsed_log_.ice_candidate_pair_events();
@@ -781,11 +808,12 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventLoggingStarted) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   const int64_t timestamp_ms = prng_.Rand(1'000'000'000);
   const int64_t utc_time_ms = prng_.Rand(1'000'000'000);
 
   // Overwrite the previously encoded LogStart event.
-  encoded_ = encoder_->EncodeLogStart(timestamp_ms * 1000, utc_time_ms * 1000);
+  encoded_ = encoder->EncodeLogStart(timestamp_ms * 1000, utc_time_ms * 1000);
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& start_log_events = parsed_log_.start_log_events();
 
@@ -795,16 +823,17 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventLoggingStopped) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   const int64_t start_timestamp_ms = prng_.Rand(1'000'000'000);
   const int64_t start_utc_time_ms = prng_.Rand(1'000'000'000);
 
   // Overwrite the previously encoded LogStart event.
-  encoded_ = encoder_->EncodeLogStart(start_timestamp_ms * 1000,
-                                      start_utc_time_ms * 1000);
+  encoded_ = encoder->EncodeLogStart(start_timestamp_ms * 1000,
+                                     start_utc_time_ms * 1000);
 
   const int64_t stop_timestamp_ms =
       prng_.Rand(start_timestamp_ms, 2'000'000'000);
-  encoded_ += encoder_->EncodeLogEnd(stop_timestamp_ms * 1000);
+  encoded_ += encoder->EncodeLogEnd(stop_timestamp_ms * 1000);
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& stop_log_events = parsed_log_.stop_log_events();
 
@@ -814,11 +843,12 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventProbeClusterCreated) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::unique_ptr<RtcEventProbeClusterCreated> event =
       gen_.NewProbeClusterCreated();
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& bwe_probe_cluster_created_events =
       parsed_log_.bwe_probe_cluster_created_events();
@@ -830,11 +860,12 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventProbeResultFailure) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::unique_ptr<RtcEventProbeResultFailure> event =
       gen_.NewProbeResultFailure();
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& bwe_probe_failure_events = parsed_log_.bwe_probe_failure_events();
 
@@ -845,11 +876,12 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventProbeResultSuccess) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::unique_ptr<RtcEventProbeResultSuccess> event =
       gen_.NewProbeResultSuccess();
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& bwe_probe_success_events = parsed_log_.bwe_probe_success_events();
 
@@ -864,6 +896,7 @@
     // As a work around, we're removing duplicates in the parser.
     return;
   }
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
 
   std::vector<std::unique_ptr<RtcEventRtcpPacketIncoming>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
@@ -873,7 +906,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& incoming_rtcp_packets = parsed_log_.incoming_rtcp_packets();
@@ -886,6 +919,7 @@
 }
 
 TEST_P(RtcEventLogEncoderTest, RtcEventRtcpPacketOutgoing) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   std::vector<std::unique_ptr<RtcEventRtcpPacketOutgoing>> events(event_count_);
   for (size_t i = 0; i < event_count_; ++i) {
     events[i] = (i == 0 || !force_repeated_fields_)
@@ -894,7 +928,7 @@
     history_.push_back(events[i]->Copy());
   }
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
   const auto& outgoing_rtcp_packets = parsed_log_.outgoing_rtcp_packets();
@@ -914,6 +948,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::ReceiverReport> events(event_count_);
     std::vector<int64_t> timestamps_ms(event_count_);
@@ -931,7 +967,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& receiver_reports = parsed_log_.receiver_reports(direction);
@@ -952,6 +988,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::SenderReport> events(event_count_);
     std::vector<int64_t> timestamps_ms(event_count_);
@@ -969,7 +1007,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& sender_reports = parsed_log_.sender_reports(direction);
@@ -990,6 +1028,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::ExtendedReports> events(event_count_);
     std::vector<int64_t> timestamps_ms(event_count_);
@@ -1007,7 +1047,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& extended_reports = parsed_log_.extended_reports(direction);
@@ -1028,6 +1068,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::Fir> events(event_count_);
     std::vector<int64_t> timestamps_ms(event_count_);
@@ -1045,7 +1087,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& firs = parsed_log_.firs(direction);
@@ -1065,6 +1107,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::Pli> events(event_count_);
     std::vector<int64_t> timestamps_ms(event_count_);
@@ -1082,7 +1126,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& plis = parsed_log_.plis(direction);
@@ -1102,6 +1146,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::Bye> events(event_count_);
     std::vector<int64_t> timestamps_ms(event_count_);
@@ -1119,7 +1165,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& byes = parsed_log_.byes(direction);
@@ -1139,6 +1185,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::Nack> events(event_count_);
     std::vector<int64_t> timestamps_ms(event_count_);
@@ -1156,7 +1204,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& nacks = parsed_log_.nacks(direction);
@@ -1176,6 +1224,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::Remb> events(event_count_);
     std::vector<int64_t> timestamps_ms(event_count_);
@@ -1193,7 +1243,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& rembs = parsed_log_.rembs(direction);
@@ -1213,6 +1263,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::TransportFeedback> events;
     events.reserve(event_count_);
@@ -1231,7 +1283,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& transport_feedbacks =
@@ -1253,6 +1305,8 @@
   rtc::ScopedFakeClock fake_clock;
   fake_clock.SetTime(Timestamp::Millis(prng_.Rand<uint32_t>()));
 
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
+
   for (auto direction : {kIncomingPacket, kOutgoingPacket}) {
     std::vector<rtcp::LossNotification> events;
     events.reserve(event_count_);
@@ -1271,7 +1325,7 @@
       fake_clock.AdvanceTime(TimeDelta::Millis(prng_.Rand(0, 1000)));
     }
 
-    encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+    encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
     ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
 
     const auto& loss_notifications = parsed_log_.loss_notifications(direction);
@@ -1308,13 +1362,14 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventVideoReceiveStreamConfig) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   uint32_t ssrc = prng_.Rand<uint32_t>();
   RtpHeaderExtensionMap extensions = gen_.NewRtpHeaderExtensionMap();
   std::unique_ptr<RtcEventVideoReceiveStreamConfig> event =
       gen_.NewVideoReceiveStreamConfig(ssrc, extensions);
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& video_recv_configs = parsed_log_.video_recv_configs();
 
@@ -1324,13 +1379,14 @@
 
 // TODO(eladalon/terelius): Test with multiple events in the batch.
 TEST_P(RtcEventLogEncoderTest, RtcEventVideoSendStreamConfig) {
+  std::unique_ptr<RtcEventLogEncoder> encoder = CreateEncoder();
   uint32_t ssrc = prng_.Rand<uint32_t>();
   RtpHeaderExtensionMap extensions = gen_.NewRtpHeaderExtensionMap();
   std::unique_ptr<RtcEventVideoSendStreamConfig> event =
       gen_.NewVideoSendStreamConfig(ssrc, extensions);
   history_.push_back(event->Copy());
 
-  encoded_ += encoder_->EncodeBatch(history_.begin(), history_.end());
+  encoded_ += encoder->EncodeBatch(history_.begin(), history_.end());
   ASSERT_TRUE(parsed_log_.ParseString(encoded_).ok());
   const auto& video_send_configs = parsed_log_.video_send_configs();
 
@@ -1357,7 +1413,8 @@
         encoder_ = std::make_unique<RtcEventLogEncoderLegacy>();
         break;
       case RtcEventLog::EncodingType::NewFormat:
-        encoder_ = std::make_unique<RtcEventLogEncoderNewFormat>();
+        encoder_ = std::make_unique<RtcEventLogEncoderNewFormat>(
+            ExplicitKeyValueConfig(""));
         break;
       case RtcEventLog::EncodingType::ProtoFree:
         encoder_ = std::make_unique<RtcEventLogEncoderV3>();
diff --git a/logging/rtc_event_log/rtc_event_log_impl.cc b/logging/rtc_event_log/rtc_event_log_impl.cc
index 419afd33..5cafe8a 100644
--- a/logging/rtc_event_log/rtc_event_log_impl.cc
+++ b/logging/rtc_event_log/rtc_event_log_impl.cc
@@ -39,7 +39,7 @@
     return std::make_unique<RtcEventLogEncoderLegacy>();
   } else {
     RTC_DLOG(LS_INFO) << "Creating new format encoder for RTC event log.";
-    return std::make_unique<RtcEventLogEncoderNewFormat>();
+    return std::make_unique<RtcEventLogEncoderNewFormat>(env.field_trials());
   }
 }