diff --git a/api/rtc_event_log/BUILD.gn b/api/rtc_event_log/BUILD.gn index 158dc06a7b..274c6ca75a 100644 --- a/api/rtc_event_log/BUILD.gn +++ b/api/rtc_event_log/BUILD.gn @@ -22,8 +22,10 @@ rtc_library("rtc_event_log") { "..:libjingle_logging_api", "../../rtc_base:checks", "../../rtc_base:timeutils", + "../environment", "../task_queue", ] + absl_deps = [ "//third_party/abseil-cpp/absl/base:nullability" ] } rtc_library("rtc_event_log_factory") { @@ -35,12 +37,16 @@ rtc_library("rtc_event_log_factory") { deps = [ ":rtc_event_log", + "..:field_trials_view", "../../rtc_base:checks", "../../rtc_base/system:rtc_export", "../../system_wrappers:field_trial", + "../environment", "../task_queue", ] + absl_deps = [ "//third_party/abseil-cpp/absl/base:nullability" ] + if (rtc_enable_protobuf) { defines = [ "WEBRTC_ENABLE_RTC_EVENT_LOG" ] deps += [ "../../logging:rtc_event_log_impl" ] diff --git a/api/rtc_event_log/rtc_event_log_factory.cc b/api/rtc_event_log/rtc_event_log_factory.cc index a3cb68cf54..c77011a322 100644 --- a/api/rtc_event_log/rtc_event_log_factory.cc +++ b/api/rtc_event_log/rtc_event_log_factory.cc @@ -13,6 +13,7 @@ #include #include +#include "api/field_trials_view.h" #include "rtc_base/checks.h" #include "system_wrappers/include/field_trial.h" @@ -27,9 +28,27 @@ RtcEventLogFactory::RtcEventLogFactory(TaskQueueFactory* task_queue_factory) RTC_DCHECK(task_queue_factory_); } +absl::Nonnull> RtcEventLogFactory::Create( + const Environment& env) const { +#ifndef WEBRTC_ENABLE_RTC_EVENT_LOG + return std::make_unique(); +#else + if (env.field_trials().IsEnabled("WebRTC-RtcEventLogKillSwitch")) { + return std::make_unique(); + } + RtcEventLog::EncodingType encoding_type = + env.field_trials().IsDisabled("WebRTC-RtcEventLogNewFormat") + ? RtcEventLog::EncodingType::Legacy + : RtcEventLog::EncodingType::NewFormat; + return std::make_unique( + RtcEventLogImpl::CreateEncoder(encoding_type), &env.task_queue_factory()); +#endif +} + std::unique_ptr RtcEventLogFactory::Create( RtcEventLog::EncodingType encoding_type) const { #ifdef WEBRTC_ENABLE_RTC_EVENT_LOG + RTC_DCHECK(task_queue_factory_); if (field_trial::IsEnabled("WebRTC-RtcEventLogKillSwitch")) { return std::make_unique(); } diff --git a/api/rtc_event_log/rtc_event_log_factory.h b/api/rtc_event_log/rtc_event_log_factory.h index fd1db3c728..f0cf6d7823 100644 --- a/api/rtc_event_log/rtc_event_log_factory.h +++ b/api/rtc_event_log/rtc_event_log_factory.h @@ -13,6 +13,8 @@ #include +#include "absl/base/nullability.h" +#include "api/environment/environment.h" #include "api/rtc_event_log/rtc_event_log.h" #include "api/rtc_event_log/rtc_event_log_factory_interface.h" #include "api/task_queue/task_queue_factory.h" @@ -22,8 +24,16 @@ namespace webrtc { class RTC_EXPORT RtcEventLogFactory : public RtcEventLogFactoryInterface { public: + RtcEventLogFactory() = default; + + // TODO(bugs.webrtc.org/15656): deprecate and delete constructor taking + // task queue factory in favor of using task queue factory provided through + // the Environment parameter in Create function. explicit RtcEventLogFactory(TaskQueueFactory* task_queue_factory); - ~RtcEventLogFactory() override {} + ~RtcEventLogFactory() override = default; + + absl::Nonnull> Create( + const Environment& env) const override; std::unique_ptr Create( RtcEventLog::EncodingType encoding_type) const override; @@ -31,7 +41,7 @@ class RTC_EXPORT RtcEventLogFactory : public RtcEventLogFactoryInterface { RtcEventLog::EncodingType encoding_type) override; private: - TaskQueueFactory* const task_queue_factory_; + TaskQueueFactory* const task_queue_factory_ = nullptr; }; } // namespace webrtc diff --git a/api/rtc_event_log/rtc_event_log_factory_interface.h b/api/rtc_event_log/rtc_event_log_factory_interface.h index a6f4dee92f..d0374fdf35 100644 --- a/api/rtc_event_log/rtc_event_log_factory_interface.h +++ b/api/rtc_event_log/rtc_event_log_factory_interface.h @@ -13,6 +13,8 @@ #include +#include "absl/base/nullability.h" +#include "api/environment/environment.h" #include "api/rtc_event_log/rtc_event_log.h" namespace webrtc { @@ -24,6 +26,11 @@ class RtcEventLogFactoryInterface { public: virtual ~RtcEventLogFactoryInterface() = default; + virtual absl::Nonnull> Create( + const Environment& env) const = 0; + + // TODO(bugs.webrtc.org/15656): Delete functions below when all usage is + // migrated to the Create(const Environment&) function above. virtual std::unique_ptr Create( RtcEventLog::EncodingType encoding_type) const = 0; [[deprecated]] virtual std::unique_ptr CreateRtcEventLog( diff --git a/logging/BUILD.gn b/logging/BUILD.gn index 95c2c901b2..92f55edfa0 100644 --- a/logging/BUILD.gn +++ b/logging/BUILD.gn @@ -482,10 +482,12 @@ rtc_library("fake_rtc_event_log") { ] deps = [ + "../api/environment", "../api/rtc_event_log", "../rtc_base:macromagic", "../rtc_base/synchronization:mutex", ] + absl_deps = [ "//third_party/abseil-cpp/absl/base:nullability" ] } if (rtc_enable_protobuf) { @@ -604,9 +606,10 @@ if (rtc_enable_protobuf) { "../api:rtc_event_log_output_file", "../api:rtp_headers", "../api:rtp_parameters", + "../api/environment", + "../api/environment:environment_factory", "../api/rtc_event_log", "../api/rtc_event_log:rtc_event_log_factory", - "../api/task_queue:default_task_queue_factory", "../api/units:time_delta", "../api/units:timestamp", "../call", @@ -621,6 +624,7 @@ if (rtc_enable_protobuf) { "../rtc_base:timeutils", "../system_wrappers", "../system_wrappers:field_trial", + "../test:explicit_key_value_config", "../test:field_trial", "../test:fileutils", "../test:test_support", diff --git a/logging/rtc_event_log/fake_rtc_event_log_factory.cc b/logging/rtc_event_log/fake_rtc_event_log_factory.cc index 47db40c9f4..9a27b63f2d 100644 --- a/logging/rtc_event_log/fake_rtc_event_log_factory.cc +++ b/logging/rtc_event_log/fake_rtc_event_log_factory.cc @@ -17,17 +17,26 @@ namespace webrtc { -std::unique_ptr FakeRtcEventLogFactory::Create( - RtcEventLog::EncodingType /*encoding_type*/) const { +absl::Nonnull> +FakeRtcEventLogFactory::CreateFake() const { auto fake_event_log = std::make_unique(); - const_cast(this)->last_log_created_ = - fake_event_log.get(); + const_cast(last_log_created_) = fake_event_log.get(); return fake_event_log; } +std::unique_ptr FakeRtcEventLogFactory::Create( + const Environment& /*env*/) const { + return CreateFake(); +} + +std::unique_ptr FakeRtcEventLogFactory::Create( + RtcEventLog::EncodingType /*encoding_type*/) const { + return CreateFake(); +} + std::unique_ptr FakeRtcEventLogFactory::CreateRtcEventLog( - RtcEventLog::EncodingType encoding_type) { - return Create(encoding_type); + RtcEventLog::EncodingType /*encoding_type*/) { + return CreateFake(); } } // namespace webrtc diff --git a/logging/rtc_event_log/fake_rtc_event_log_factory.h b/logging/rtc_event_log/fake_rtc_event_log_factory.h index c7ff33dee4..71105558e4 100644 --- a/logging/rtc_event_log/fake_rtc_event_log_factory.h +++ b/logging/rtc_event_log/fake_rtc_event_log_factory.h @@ -13,6 +13,8 @@ #include +#include "absl/base/nullability.h" +#include "api/environment/environment.h" #include "api/rtc_event_log/rtc_event_log_factory_interface.h" #include "logging/rtc_event_log/fake_rtc_event_log.h" @@ -23,16 +25,21 @@ class FakeRtcEventLogFactory : public RtcEventLogFactoryInterface { FakeRtcEventLogFactory() = default; ~FakeRtcEventLogFactory() override = default; + absl::Nonnull> Create( + const Environment& env) const override; + std::unique_ptr Create( RtcEventLog::EncodingType encoding_type) const override; std::unique_ptr CreateRtcEventLog( RtcEventLog::EncodingType encoding_type) override; - webrtc::FakeRtcEventLog* last_log_created() { return last_log_created_; } + FakeRtcEventLog* last_log_created() { return last_log_created_; } private: - webrtc::FakeRtcEventLog* last_log_created_; + absl::Nonnull> CreateFake() const; + + FakeRtcEventLog* last_log_created_ = nullptr; }; } // namespace webrtc diff --git a/logging/rtc_event_log/rtc_event_log_unittest.cc b/logging/rtc_event_log/rtc_event_log_unittest.cc index 3730a080dd..a3005729ab 100644 --- a/logging/rtc_event_log/rtc_event_log_unittest.cc +++ b/logging/rtc_event_log/rtc_event_log_unittest.cc @@ -19,8 +19,9 @@ #include #include +#include "api/environment/environment.h" +#include "api/environment/environment_factory.h" #include "api/rtc_event_log/rtc_event_log_factory.h" -#include "api/task_queue/default_task_queue_factory.h" #include "logging/rtc_event_log/events/rtc_event_audio_network_adaptation.h" #include "logging/rtc_event_log/events/rtc_event_audio_playout.h" #include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h" @@ -50,6 +51,7 @@ #include "rtc_base/checks.h" #include "rtc_base/fake_clock.h" #include "rtc_base/random.h" +#include "test/explicit_key_value_config.h" #include "test/gtest.h" #include "test/logging/memory_log_writer.h" #include "test/testsupport/file_utils.h" @@ -58,6 +60,8 @@ namespace webrtc { namespace { +using test::ExplicitKeyValueConfig; + struct EventCounts { size_t audio_send_streams = 0; size_t audio_recv_streams = 0; @@ -105,6 +109,21 @@ struct EventCounts { } }; +std::unique_ptr CreateFieldTrialsFor( + RtcEventLog::EncodingType encoding_type) { + switch (encoding_type) { + case RtcEventLog::EncodingType::Legacy: + return std::make_unique( + "WebRTC-RtcEventLogNewFormat/Disabled/"); + case RtcEventLog::EncodingType::NewFormat: + return std::make_unique( + "WebRTC-RtcEventLogNewFormat/Enabled/"); + case RtcEventLog::EncodingType::ProtoFree: + RTC_CHECK(false); + return nullptr; + } +} + class RtcEventLogSession : public ::testing::TestWithParam< std::tuple> { @@ -336,14 +355,13 @@ void RtcEventLogSession::WriteVideoSendConfigs(size_t video_send_streams, void RtcEventLogSession::WriteLog(EventCounts count, size_t num_events_before_start) { - // TODO(terelius): Allow test to run with either a real or a fake clock_. - // Maybe always use the ScopedFakeClock, but conditionally SleepMs()? + // TODO(terelius): Allow test to run with either a real or a fake clock_ + // e.g. by using clock and task_queue_factory from TimeController + // when RtcEventLogImpl switches to use injected clock from the environment. - auto task_queue_factory = CreateDefaultTaskQueueFactory(); - RtcEventLogFactory rtc_event_log_factory(task_queue_factory.get()); // The log will be flushed to output when the event_log goes out of scope. - std::unique_ptr event_log = - rtc_event_log_factory.CreateRtcEventLog(encoding_type_); + std::unique_ptr event_log = RtcEventLogFactory().Create( + CreateEnvironment(CreateFieldTrialsFor(encoding_type_))); // We can't send or receive packets without configured streams. RTC_CHECK_GE(count.video_recv_streams, 1); @@ -934,12 +952,9 @@ TEST_P(RtcEventLogCircularBufferTest, KeepsMostRecentEvents) { int64_t start_time_us, utc_start_time_us, stop_time_us; { - auto task_queue_factory = CreateDefaultTaskQueueFactory(); - RtcEventLogFactory rtc_event_log_factory(task_queue_factory.get()); - // When `log` goes out of scope, the contents are flushed - // to the output. - std::unique_ptr log = - rtc_event_log_factory.CreateRtcEventLog(encoding_type_); + // When `log` goes out of scope, the contents are flushed to the output. + std::unique_ptr log = RtcEventLogFactory().Create( + CreateEnvironment(CreateFieldTrialsFor(encoding_type_))); for (size_t i = 0; i < kNumEvents; i++) { // The purpose of the test is to verify that the log can handle