From fd89ff5d933fa7e561716a89152aeb48695337d9 Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Wed, 15 May 2024 17:47:13 +0200 Subject: [PATCH] Provide Environment to SimulcastRateAllocator at construction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So that this class can use propagated field trials instead of the global Bug: webrtc:42220378 Change-Id: Ic1dba0c4967735606904329f7e9e6c09f186b809 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/350641 Reviewed-by: Erik Språng Reviewed-by: Philip Eliasson Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#42326} --- api/video/BUILD.gn | 1 - ...builtin_video_bitrate_allocator_factory.cc | 5 +-- api/video/video_bitrate_allocator_factory.h | 11 +------ ...oder_software_fallback_wrapper_unittest.cc | 14 +++++---- media/engine/simulcast_encoder_adapter.cc | 31 +++++++++---------- .../simulcast_encoder_adapter_unittest.cc | 21 +++++++------ modules/video_coding/BUILD.gn | 3 +- .../codecs/h264/h264_encoder_impl.cc | 5 +-- .../codecs/h264/h264_encoder_impl.h | 1 + .../vp8/default_temporal_layers_unittest.cc | 3 +- .../codecs/vp8/libvpx_vp8_encoder.cc | 2 +- .../utility/simulcast_rate_allocator.cc | 10 +++++- .../utility/simulcast_rate_allocator.h | 4 ++- .../simulcast_rate_allocator_unittest.cc | 27 +++++++++------- .../utility/simulcast_test_fixture_impl.cc | 10 +++--- .../utility/simulcast_test_fixture_impl.h | 1 + video/video_stream_encoder_unittest.cc | 2 +- 17 files changed, 80 insertions(+), 71 deletions(-) diff --git a/api/video/BUILD.gn b/api/video/BUILD.gn index 48b13c90e3..16e27af994 100644 --- a/api/video/BUILD.gn +++ b/api/video/BUILD.gn @@ -264,7 +264,6 @@ rtc_source_set("video_bitrate_allocator_factory") { sources = [ "video_bitrate_allocator_factory.h" ] deps = [ ":video_bitrate_allocator", - "../../rtc_base:checks", "../environment", "../video_codecs:video_codecs_api", ] diff --git a/api/video/builtin_video_bitrate_allocator_factory.cc b/api/video/builtin_video_bitrate_allocator_factory.cc index 252ae210b6..bfc41efe1b 100644 --- a/api/video/builtin_video_bitrate_allocator_factory.cc +++ b/api/video/builtin_video_bitrate_allocator_factory.cc @@ -29,7 +29,8 @@ class BuiltinVideoBitrateAllocatorFactory BuiltinVideoBitrateAllocatorFactory() = default; ~BuiltinVideoBitrateAllocatorFactory() override = default; - std::unique_ptr CreateVideoBitrateAllocator( + std::unique_ptr Create( + const Environment& env, const VideoCodec& codec) override { // TODO(https://crbug.com/webrtc/14884): Update SvcRateAllocator to // support simulcast and use it for VP9/AV1 simulcast as well. @@ -38,7 +39,7 @@ class BuiltinVideoBitrateAllocatorFactory codec.numberOfSimulcastStreams <= 1) { return std::make_unique(codec); } - return std::make_unique(codec); + return std::make_unique(env, codec); } }; diff --git a/api/video/video_bitrate_allocator_factory.h b/api/video/video_bitrate_allocator_factory.h index a6e0a1aef0..31b6794ad5 100644 --- a/api/video/video_bitrate_allocator_factory.h +++ b/api/video/video_bitrate_allocator_factory.h @@ -16,7 +16,6 @@ #include "api/environment/environment.h" #include "api/video/video_bitrate_allocator.h" #include "api/video_codecs/video_codec.h" -#include "rtc_base/checks.h" namespace webrtc { @@ -29,15 +28,7 @@ class VideoBitrateAllocatorFactory { // Creates a VideoBitrateAllocator for a specific video codec. virtual std::unique_ptr Create( const Environment& env, - const VideoCodec& codec) { - return CreateVideoBitrateAllocator(codec); - } - virtual std::unique_ptr CreateVideoBitrateAllocator( - const VideoCodec& codec) { - // Newer code shouldn't call this function, - // Older code should implement it in derived classes. - RTC_CHECK_NOTREACHED(); - } + const VideoCodec& codec) = 0; }; } // namespace webrtc diff --git a/api/video_codecs/test/video_encoder_software_fallback_wrapper_unittest.cc b/api/video_codecs/test/video_encoder_software_fallback_wrapper_unittest.cc index 9438b0d594..dbe72c2ead 100644 --- a/api/video_codecs/test/video_encoder_software_fallback_wrapper_unittest.cc +++ b/api/video_codecs/test/video_encoder_software_fallback_wrapper_unittest.cc @@ -93,10 +93,11 @@ class VideoEncoderSoftwareFallbackWrapperTestBase : public ::testing::Test { VideoEncoderSoftwareFallbackWrapperTestBase( const Environment& env, std::unique_ptr sw_encoder) - : fake_encoder_(new CountingFakeEncoder()), + : env_(env), + fake_encoder_(new CountingFakeEncoder()), wrapper_initialized_(false), fallback_wrapper_(CreateVideoEncoderSoftwareFallbackWrapper( - env, + env_, std::move(sw_encoder), std::unique_ptr(fake_encoder_), false)) {} @@ -172,6 +173,7 @@ class VideoEncoderSoftwareFallbackWrapperTestBase : public ::testing::Test { fallback_wrapper_->GetEncoderInfo().implementation_name); } + const Environment env_; FakeEncodedImageCallback callback_; // `fake_encoder_` is owned and released by `fallback_wrapper_`. CountingFakeEncoder* fake_encoder_; @@ -232,7 +234,7 @@ void VideoEncoderSoftwareFallbackWrapperTestBase::InitEncode() { codec_.width = kWidth; codec_.height = kHeight; codec_.VP8()->numberOfTemporalLayers = 1; - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); if (wrapper_initialized_) { fallback_wrapper_->Release(); @@ -263,7 +265,7 @@ void VideoEncoderSoftwareFallbackWrapperTestBase::UtilizeFallbackEncoder() { codec_.width = kWidth; codec_.height = kHeight; codec_.VP8()->numberOfTemporalLayers = 1; - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); if (wrapper_initialized_) { fallback_wrapper_->Release(); @@ -291,7 +293,7 @@ void VideoEncoderSoftwareFallbackWrapperTestBase::FallbackFromEncodeRequest() { codec_.width = kWidth; codec_.height = kHeight; codec_.VP8()->numberOfTemporalLayers = 1; - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); if (wrapper_initialized_) { fallback_wrapper_->Release(); } @@ -514,7 +516,7 @@ class ForcedFallbackTest : public VideoEncoderSoftwareFallbackWrapperTestBase { codec_.VP8()->numberOfTemporalLayers = 1; codec_.VP8()->automaticResizeOn = true; codec_.SetFrameDropEnabled(true); - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); } void InitEncode(int width, int height) { diff --git a/media/engine/simulcast_encoder_adapter.cc b/media/engine/simulcast_encoder_adapter.cc index b4a6486f71..08720321a5 100644 --- a/media/engine/simulcast_encoder_adapter.cc +++ b/media/engine/simulcast_encoder_adapter.cc @@ -39,12 +39,13 @@ #include "rtc_base/experiments/rate_control_settings.h" #include "rtc_base/logging.h" +namespace webrtc { namespace { // Max qp for lowest spatial resolution when doing simulcast. const unsigned int kLowestResMaxQp = 45; -uint32_t SumStreamMaxBitrate(int streams, const webrtc::VideoCodec& codec) { +uint32_t SumStreamMaxBitrate(int streams, const VideoCodec& codec) { uint32_t bitrate_sum = 0; for (int i = 0; i < streams; ++i) { bitrate_sum += codec.simulcastStream[i].maxBitrate; @@ -52,7 +53,7 @@ uint32_t SumStreamMaxBitrate(int streams, const webrtc::VideoCodec& codec) { return bitrate_sum; } -int CountAllStreams(const webrtc::VideoCodec& codec) { +int CountAllStreams(const VideoCodec& codec) { int total_streams_count = codec.numberOfSimulcastStreams < 1 ? 1 : codec.numberOfSimulcastStreams; uint32_t simulcast_max_bitrate = @@ -63,7 +64,7 @@ int CountAllStreams(const webrtc::VideoCodec& codec) { return total_streams_count; } -int CountActiveStreams(const webrtc::VideoCodec& codec) { +int CountActiveStreams(const VideoCodec& codec) { if (codec.numberOfSimulcastStreams < 1) { return 1; } @@ -77,7 +78,7 @@ int CountActiveStreams(const webrtc::VideoCodec& codec) { return active_streams_count; } -int VerifyCodec(const webrtc::VideoCodec* codec_settings) { +int VerifyCodec(const VideoCodec* codec_settings) { if (codec_settings == nullptr) { return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; } @@ -100,14 +101,13 @@ int VerifyCodec(const webrtc::VideoCodec* codec_settings) { return WEBRTC_VIDEO_CODEC_OK; } -bool StreamQualityCompare(const webrtc::SimulcastStream& a, - const webrtc::SimulcastStream& b) { +bool StreamQualityCompare(const SimulcastStream& a, const SimulcastStream& b) { return std::tie(a.height, a.width, a.maxBitrate, a.maxFramerate) < std::tie(b.height, b.width, b.maxBitrate, b.maxFramerate); } void GetLowestAndHighestQualityStreamIndixes( - rtc::ArrayView streams, + rtc::ArrayView streams, int* lowest_quality_stream_idx, int* highest_quality_stream_idx) { const auto lowest_highest_quality_streams = @@ -118,14 +118,13 @@ void GetLowestAndHighestQualityStreamIndixes( std::distance(streams.begin(), lowest_highest_quality_streams.second); } -std::vector GetStreamStartBitratesKbps( - const webrtc::VideoCodec& codec) { +std::vector GetStreamStartBitratesKbps(const Environment& env, + const VideoCodec& codec) { std::vector start_bitrates; - std::unique_ptr rate_allocator = - std::make_unique(codec); - webrtc::VideoBitrateAllocation allocation = - rate_allocator->Allocate(webrtc::VideoBitrateAllocationParameters( - codec.startBitrate * 1000, codec.maxFramerate)); + VideoBitrateAllocation allocation = + SimulcastRateAllocator(env, codec) + .Allocate(VideoBitrateAllocationParameters(codec.startBitrate * 1000, + codec.maxFramerate)); int total_streams_count = CountAllStreams(codec); for (int i = 0; i < total_streams_count; ++i) { @@ -137,8 +136,6 @@ std::vector GetStreamStartBitratesKbps( } // namespace -namespace webrtc { - SimulcastEncoderAdapter::EncoderContext::EncoderContext( std::unique_ptr encoder, bool prefer_temporal_support, @@ -377,7 +374,7 @@ int SimulcastEncoderAdapter::InitEncode( // Multi-encoder simulcast or singlecast (deactivated layers). std::vector stream_start_bitrate_kbps = - GetStreamStartBitratesKbps(codec_); + GetStreamStartBitratesKbps(env_, codec_); for (int stream_idx = 0; stream_idx < total_streams_count_; ++stream_idx) { if (!is_legacy_singlecast && !codec_.simulcastStream[stream_idx].active) { diff --git a/media/engine/simulcast_encoder_adapter_unittest.cc b/media/engine/simulcast_encoder_adapter_unittest.cc index b4d6b11490..049c78ef03 100644 --- a/media/engine/simulcast_encoder_adapter_unittest.cc +++ b/media/engine/simulcast_encoder_adapter_unittest.cc @@ -447,7 +447,7 @@ class TestSimulcastEncoderAdapterFake : public ::testing::Test, void SetUp() override { helper_ = std::make_unique( - CreateEnvironment(&field_trials_), use_fallback_factory_, + env_, use_fallback_factory_, SdpVideoFormat("VP8", sdp_video_parameters_)); adapter_ = helper_->CreateMockEncoderAdapter(); last_encoded_image_width_ = absl::nullopt; @@ -503,7 +503,7 @@ class TestSimulcastEncoderAdapterFake : public ::testing::Test, codec_.simulcastStream[stream_idx].active = active_streams[stream_idx]; } } - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); EXPECT_EQ(0, adapter_->InitEncode(&codec_, kSettings)); adapter_->RegisterEncodeCompleteCallback(this); } @@ -579,6 +579,8 @@ class TestSimulcastEncoderAdapterFake : public ::testing::Test, } protected: + test::ScopedKeyValueConfig field_trials_; + const Environment env_ = CreateEnvironment(&field_trials_); std::unique_ptr helper_; std::unique_ptr adapter_; VideoCodec codec_; @@ -588,7 +590,6 @@ class TestSimulcastEncoderAdapterFake : public ::testing::Test, std::unique_ptr rate_allocator_; bool use_fallback_factory_; CodecParameterMap sdp_video_parameters_; - test::ScopedKeyValueConfig field_trials_; }; TEST_F(TestSimulcastEncoderAdapterFake, InitEncode) { @@ -662,7 +663,7 @@ TEST_F(TestSimulcastEncoderAdapterFake, ReusesEncodersInOrder) { SimulcastTestFixtureImpl::DefaultSettings( &codec_, static_cast(kTestTemporalLayerProfile), kVideoCodecVP8); - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); adapter_->RegisterEncodeCompleteCallback(this); const uint32_t target_bitrate = 1000 * (codec_.simulcastStream[0].targetBitrate + @@ -913,7 +914,7 @@ TEST_F(TestSimulcastEncoderAdapterFake, SetRatesUnderMinBitrate) { codec_.minBitrate = 50; codec_.numberOfSimulcastStreams = 1; EXPECT_EQ(0, adapter_->InitEncode(&codec_, kSettings)); - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); // Above min should be respected. VideoBitrateAllocation target_bitrate = rate_allocator_->Allocate( @@ -1110,7 +1111,7 @@ TEST_F(TestSimulcastEncoderAdapterFake, GeneratesKeyFramesOnRequestedLayers) { SimulcastTestFixtureImpl::DefaultSettings( &codec_, static_cast(kTestTemporalLayerProfile), kVideoCodecVP8); - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); adapter_->RegisterEncodeCompleteCallback(this); // Input data. @@ -1299,7 +1300,7 @@ TEST_F(TestSimulcastEncoderAdapterFake, ActivatesCorrectStreamsInInitEncode) { SimulcastTestFixtureImpl::DefaultSettings( &codec_, static_cast(kTestTemporalLayerProfile), kVideoCodecVP8); - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); adapter_->RegisterEncodeCompleteCallback(this); // Only enough start bitrate for the lowest stream. @@ -1337,7 +1338,7 @@ TEST_F(TestSimulcastEncoderAdapterFake, TrustedRateControl) { SimulcastTestFixtureImpl::DefaultSettings( &codec_, static_cast(kTestTemporalLayerProfile), kVideoCodecVP8); - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); adapter_->RegisterEncodeCompleteCallback(this); // Only enough start bitrate for the lowest stream. @@ -1584,7 +1585,7 @@ TEST_F(TestSimulcastEncoderAdapterFake, SetRateDistributesBandwithAllocation) { const DataRate bandwidth_allocation = target_bitrate + DataRate::KilobitsPerSec(600); - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); EXPECT_EQ(0, adapter_->InitEncode(&codec_, kSettings)); adapter_->RegisterEncodeCompleteCallback(this); @@ -1620,7 +1621,7 @@ TEST_F(TestSimulcastEncoderAdapterFake, CanSetZeroBitrateWithHeadroom) { kVideoCodecVP8); codec_.numberOfSimulcastStreams = 3; - rate_allocator_.reset(new SimulcastRateAllocator(codec_)); + rate_allocator_ = std::make_unique(env_, codec_); EXPECT_EQ(0, adapter_->InitEncode(&codec_, kSettings)); adapter_->RegisterEncodeCompleteCallback(this); diff --git a/modules/video_coding/BUILD.gn b/modules/video_coding/BUILD.gn index 1b3a2442a1..c8b3a86483 100644 --- a/modules/video_coding/BUILD.gn +++ b/modules/video_coding/BUILD.gn @@ -420,9 +420,9 @@ rtc_library("video_coding_utility") { ":video_codec_interface", "../../api:array_view", "../../api:field_trials_view", - "../../api:field_trials_view", "../../api:scoped_refptr", "../../api:sequence_checker", + "../../api/environment", "../../api/units:time_delta", "../../api/video:encoded_frame", "../../api/video:encoded_image", @@ -453,7 +453,6 @@ rtc_library("video_coding_utility") { "../../rtc_base/system:file_wrapper", "../../rtc_base/system:no_unique_address", "../../rtc_base/task_utils:repeating_task", - "../../system_wrappers:field_trial", "../rtp_rtcp:rtp_rtcp_format", "svc:scalability_mode_util", ] diff --git a/modules/video_coding/codecs/h264/h264_encoder_impl.cc b/modules/video_coding/codecs/h264/h264_encoder_impl.cc index 5b21d6c5ee..cfd89d1b5f 100644 --- a/modules/video_coding/codecs/h264/h264_encoder_impl.cc +++ b/modules/video_coding/codecs/h264/h264_encoder_impl.cc @@ -174,7 +174,8 @@ static void RtpFragmentize(EncodedImage* encoded_image, SFrameBSInfo* info) { H264EncoderImpl::H264EncoderImpl(const Environment& env, H264EncoderSettings settings) - : packetization_mode_(settings.packetization_mode), + : env_(env), + packetization_mode_(settings.packetization_mode), max_payload_size_(0), number_of_cores_(0), encoded_image_callback_(nullptr), @@ -326,7 +327,7 @@ int32_t H264EncoderImpl::InitEncode(const VideoCodec* inst, } } - SimulcastRateAllocator init_allocator(codec_); + SimulcastRateAllocator init_allocator(env_, codec_); VideoBitrateAllocation allocation = init_allocator.Allocate(VideoBitrateAllocationParameters( DataRate::KilobitsPerSec(codec_.startBitrate), codec_.maxFramerate)); diff --git a/modules/video_coding/codecs/h264/h264_encoder_impl.h b/modules/video_coding/codecs/h264/h264_encoder_impl.h index cfc28d7d4e..2818711b06 100644 --- a/modules/video_coding/codecs/h264/h264_encoder_impl.h +++ b/modules/video_coding/codecs/h264/h264_encoder_impl.h @@ -106,6 +106,7 @@ class H264EncoderImpl : public VideoEncoder { absl::InlinedVector, kMaxSimulcastStreams> scalability_modes_; + const Environment env_; VideoCodec codec_; H264PacketizationMode packetization_mode_; size_t max_payload_size_; diff --git a/modules/video_coding/codecs/vp8/default_temporal_layers_unittest.cc b/modules/video_coding/codecs/vp8/default_temporal_layers_unittest.cc index ac9c565aec..85ec7fee5b 100644 --- a/modules/video_coding/codecs/vp8/default_temporal_layers_unittest.cc +++ b/modules/video_coding/codecs/vp8/default_temporal_layers_unittest.cc @@ -13,6 +13,7 @@ #include #include +#include "api/environment/environment_factory.h" #include "api/video/video_bitrate_allocation.h" #include "api/video_codecs/video_codec.h" #include "api/video_codecs/vp8_frame_config.h" @@ -98,7 +99,7 @@ std::vector GetTemporalLayerRates(int target_bitrate_kbps, codec.simulcastStream[0].maxBitrate = target_bitrate_kbps; codec.simulcastStream[0].numberOfTemporalLayers = num_temporal_layers; codec.simulcastStream[0].active = true; - SimulcastRateAllocator allocator(codec); + SimulcastRateAllocator allocator(CreateEnvironment(), codec); return allocator .Allocate( VideoBitrateAllocationParameters(target_bitrate_kbps, framerate_fps)) diff --git a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc index a5036cf52d..6dcdbfd2c8 100644 --- a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc +++ b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc @@ -678,7 +678,7 @@ int LibvpxVp8Encoder::InitEncode(const VideoCodec* inst, // Note the order we use is different from webm, we have lowest resolution // at position 0 and they have highest resolution at position 0. const size_t stream_idx_cfg_0 = encoders_.size() - 1; - SimulcastRateAllocator init_allocator(codec_); + SimulcastRateAllocator init_allocator(env_, codec_); VideoBitrateAllocation allocation = init_allocator.Allocate(VideoBitrateAllocationParameters( inst->startBitrate * 1000, inst->maxFramerate)); diff --git a/modules/video_coding/utility/simulcast_rate_allocator.cc b/modules/video_coding/utility/simulcast_rate_allocator.cc index 1496934e1c..3ef4d43a5e 100644 --- a/modules/video_coding/utility/simulcast_rate_allocator.cc +++ b/modules/video_coding/utility/simulcast_rate_allocator.cc @@ -20,9 +20,9 @@ #include #include +#include "api/environment/environment.h" #include "rtc_base/checks.h" #include "rtc_base/experiments/rate_control_settings.h" -#include "system_wrappers/include/field_trial.h" namespace webrtc { namespace { @@ -64,6 +64,14 @@ SimulcastRateAllocator::SimulcastRateAllocator(const VideoCodec& codec) rate_control_settings_(RateControlSettings::ParseFromFieldTrials()), legacy_conference_mode_(false) {} +SimulcastRateAllocator::SimulcastRateAllocator(const Environment& env, + const VideoCodec& codec) + : codec_(codec), + stable_rate_settings_(StableTargetRateExperiment::ParseFromKeyValueConfig( + &env.field_trials())), + rate_control_settings_(env.field_trials()), + legacy_conference_mode_(false) {} + SimulcastRateAllocator::~SimulcastRateAllocator() = default; VideoBitrateAllocation SimulcastRateAllocator::Allocate( diff --git a/modules/video_coding/utility/simulcast_rate_allocator.h b/modules/video_coding/utility/simulcast_rate_allocator.h index 6f93dbde74..60b2018380 100644 --- a/modules/video_coding/utility/simulcast_rate_allocator.h +++ b/modules/video_coding/utility/simulcast_rate_allocator.h @@ -16,6 +16,7 @@ #include +#include "api/environment/environment.h" #include "api/video/video_bitrate_allocation.h" #include "api/video/video_bitrate_allocator.h" #include "api/video_codecs/video_codec.h" @@ -26,7 +27,8 @@ namespace webrtc { class SimulcastRateAllocator : public VideoBitrateAllocator { public: - explicit SimulcastRateAllocator(const VideoCodec& codec); + [[deprecated]] explicit SimulcastRateAllocator(const VideoCodec& codec); + SimulcastRateAllocator(const Environment& env, const VideoCodec& codec); ~SimulcastRateAllocator() override; SimulcastRateAllocator(const SimulcastRateAllocator&) = delete; diff --git a/modules/video_coding/utility/simulcast_rate_allocator_unittest.cc b/modules/video_coding/utility/simulcast_rate_allocator_unittest.cc index 24d7c58bcd..528a40f7bf 100644 --- a/modules/video_coding/utility/simulcast_rate_allocator_unittest.cc +++ b/modules/video_coding/utility/simulcast_rate_allocator_unittest.cc @@ -15,16 +15,19 @@ #include #include +#include "api/environment/environment.h" +#include "api/environment/environment_factory.h" #include "api/video_codecs/vp8_frame_buffer_controller.h" #include "api/video_codecs/vp8_frame_config.h" #include "api/video_codecs/vp8_temporal_layers.h" #include "rtc_base/checks.h" -#include "test/field_trial.h" +#include "test/explicit_key_value_config.h" #include "test/gmock.h" #include "test/gtest.h" namespace webrtc { namespace { +using test::ExplicitKeyValueConfig; using ::testing::_; constexpr uint32_t kFramerateFps = 5; @@ -90,9 +93,8 @@ class SimulcastRateAllocatorTest : public ::testing::TestWithParam { EXPECT_EQ(sum, actual.get_sum_bps()); } - void CreateAllocator(bool legacy_conference_mode = false) { - allocator_.reset(new SimulcastRateAllocator(codec_)); - allocator_->SetLegacyConferenceMode(legacy_conference_mode); + void CreateAllocator(Environment env = CreateEnvironment()) { + allocator_ = std::make_unique(env, codec_); } void SetupCodec3SL3TL(const std::vector& active_streams) { @@ -298,11 +300,11 @@ TEST_F(SimulcastRateAllocatorTest, Regular3TLTemporalRateAllocation) { } TEST_F(SimulcastRateAllocatorTest, BaseHeavy3TLTemporalRateAllocation) { - test::ScopedFieldTrials field_trials( + ExplicitKeyValueConfig field_trials( "WebRTC-UseBaseHeavyVP8TL3RateAllocation/Enabled/"); SetupCodec3SL3TL({true, true, true}); - CreateAllocator(); + CreateAllocator(CreateEnvironment(&field_trials)); const VideoBitrateAllocation alloc = GetAllocation(kMinBitrateKbps); // 60/20/20. @@ -583,13 +585,13 @@ TEST_F(SimulcastRateAllocatorTest, NonConferenceModeScreenshare) { } TEST_F(SimulcastRateAllocatorTest, StableRate) { - webrtc::test::ScopedFieldTrials field_trials( + ExplicitKeyValueConfig field_trials( "WebRTC-StableTargetRate/" "enabled:true," "video_hysteresis_factor:1.1/"); SetupCodec3SL3TL({true, true, true}); - CreateAllocator(); + CreateAllocator(CreateEnvironment(&field_trials)); // Let the volatile rate always be be enough for all streams, in this test we // are only interested in how the stable rate affects enablement. @@ -685,7 +687,8 @@ INSTANTIATE_TEST_SUITE_P(ScreenshareTest, TEST_P(ScreenshareRateAllocationTest, ConferenceBitrateBelowTl0) { SetupConferenceScreenshare(GetParam()); - CreateAllocator(true); + CreateAllocator(); + allocator_->SetLegacyConferenceMode(true); VideoBitrateAllocation allocation = allocator_->Allocate(VideoBitrateAllocationParameters( @@ -700,7 +703,8 @@ TEST_P(ScreenshareRateAllocationTest, ConferenceBitrateBelowTl0) { TEST_P(ScreenshareRateAllocationTest, ConferenceBitrateAboveTl0) { SetupConferenceScreenshare(GetParam()); - CreateAllocator(true); + CreateAllocator(); + allocator_->SetLegacyConferenceMode(true); uint32_t target_bitrate_kbps = (kLegacyScreenshareTargetBitrateKbps + kLegacyScreenshareMaxBitrateKbps) / @@ -721,7 +725,8 @@ TEST_P(ScreenshareRateAllocationTest, ConferenceBitrateAboveTl0) { TEST_F(ScreenshareRateAllocationTest, ConferenceBitrateAboveTl1) { // This test is only for the non-simulcast case. SetupConferenceScreenshare(false); - CreateAllocator(true); + CreateAllocator(); + allocator_->SetLegacyConferenceMode(true); VideoBitrateAllocation allocation = allocator_->Allocate(VideoBitrateAllocationParameters( diff --git a/modules/video_coding/utility/simulcast_test_fixture_impl.cc b/modules/video_coding/utility/simulcast_test_fixture_impl.cc index 4f0bb327d0..7180993c9d 100644 --- a/modules/video_coding/utility/simulcast_test_fixture_impl.cc +++ b/modules/video_coding/utility/simulcast_test_fixture_impl.cc @@ -259,10 +259,10 @@ SimulcastTestFixtureImpl::SimulcastTestFixtureImpl( std::unique_ptr encoder_factory, std::unique_ptr decoder_factory, SdpVideoFormat video_format) - : codec_type_(PayloadStringToCodecType(video_format.name)) { - Environment env = CreateEnvironment(); - encoder_ = encoder_factory->Create(env, video_format); - decoder_ = decoder_factory->Create(env, video_format); + : env_(CreateEnvironment()), + codec_type_(PayloadStringToCodecType(video_format.name)) { + encoder_ = encoder_factory->Create(env_, video_format); + decoder_ = decoder_factory->Create(env_, video_format); SetUpCodec((codec_type_ == kVideoCodecVP8 || codec_type_ == kVideoCodecH264) ? kDefaultTemporalLayerProfile : kNoTemporalLayerProfile); @@ -294,7 +294,7 @@ void SimulcastTestFixtureImpl::SetUpCodec(const int* temporal_layer_profile) { } void SimulcastTestFixtureImpl::SetUpRateAllocator() { - rate_allocator_.reset(new SimulcastRateAllocator(settings_)); + rate_allocator_ = std::make_unique(env_, settings_); } void SimulcastTestFixtureImpl::SetRates(uint32_t bitrate_kbps, uint32_t fps) { diff --git a/modules/video_coding/utility/simulcast_test_fixture_impl.h b/modules/video_coding/utility/simulcast_test_fixture_impl.h index f142ab4813..e7a500d27b 100644 --- a/modules/video_coding/utility/simulcast_test_fixture_impl.h +++ b/modules/video_coding/utility/simulcast_test_fixture_impl.h @@ -78,6 +78,7 @@ class SimulcastTestFixtureImpl final : public SimulcastTestFixture { int num_spatial_layers); void SwitchingToOneStream(int width, int height); + const Environment env_; std::unique_ptr encoder_; MockEncodedImageCallback encoder_callback_; std::unique_ptr decoder_; diff --git a/video/video_stream_encoder_unittest.cc b/video/video_stream_encoder_unittest.cc index 267b7d66b8..38fe5d4c2c 100644 --- a/video/video_stream_encoder_unittest.cc +++ b/video/video_stream_encoder_unittest.cc @@ -4781,7 +4781,7 @@ TEST_F(VideoStreamEncoderTest, ReportsVideoBitrateAllocation) { const int kDefaultFps = 30; const VideoBitrateAllocation expected_bitrate = - SimulcastRateAllocator(fake_encoder_.config()) + SimulcastRateAllocator(env_, fake_encoder_.config()) .Allocate(VideoBitrateAllocationParameters(kLowTargetBitrate.bps(), kDefaultFps));