xref: /aosp_15_r20/external/webrtc/video/video_send_stream_impl_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "video/video_send_stream_impl.h"
12 
13 #include <algorithm>
14 #include <memory>
15 #include <string>
16 
17 #include "absl/types/optional.h"
18 #include "api/rtc_event_log/rtc_event_log.h"
19 #include "api/task_queue/task_queue_base.h"
20 #include "api/units/time_delta.h"
21 #include "api/units/timestamp.h"
22 #include "call/rtp_video_sender.h"
23 #include "call/test/mock_bitrate_allocator.h"
24 #include "call/test/mock_rtp_transport_controller_send.h"
25 #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
26 #include "modules/utility/maybe_worker_thread.h"
27 #include "modules/video_coding/fec_controller_default.h"
28 #include "rtc_base/event.h"
29 #include "rtc_base/experiments/alr_experiment.h"
30 #include "rtc_base/fake_clock.h"
31 #include "rtc_base/logging.h"
32 #include "test/gmock.h"
33 #include "test/gtest.h"
34 #include "test/mock_transport.h"
35 #include "test/scoped_key_value_config.h"
36 #include "test/time_controller/simulated_time_controller.h"
37 #include "video/test/mock_video_stream_encoder.h"
38 #include "video/video_send_stream.h"
39 
40 namespace webrtc {
41 
operator ==(const BitrateAllocationUpdate & a,const BitrateAllocationUpdate & b)42 bool operator==(const BitrateAllocationUpdate& a,
43                 const BitrateAllocationUpdate& b) {
44   return a.target_bitrate == b.target_bitrate &&
45          a.round_trip_time == b.round_trip_time &&
46          a.packet_loss_ratio == b.packet_loss_ratio;
47 }
48 
49 namespace internal {
50 namespace {
51 using ::testing::_;
52 using ::testing::AllOf;
53 using ::testing::Field;
54 using ::testing::Invoke;
55 using ::testing::NiceMock;
56 using ::testing::Return;
57 
58 constexpr int64_t kDefaultInitialBitrateBps = 333000;
59 const double kDefaultBitratePriority = 0.5;
60 
61 const float kAlrProbingExperimentPaceMultiplier = 1.0f;
GetAlrProbingExperimentString()62 std::string GetAlrProbingExperimentString() {
63   return std::string(
64              AlrExperimentSettings::kScreenshareProbingBweExperimentName) +
65          "/1.0,2875,80,40,-60,3/";
66 }
67 class MockRtpVideoSender : public RtpVideoSenderInterface {
68  public:
69   MOCK_METHOD(void, SetActiveModules, (const std::vector<bool>&), (override));
70   MOCK_METHOD(void, Stop, (), (override));
71   MOCK_METHOD(bool, IsActive, (), (override));
72   MOCK_METHOD(void, OnNetworkAvailability, (bool), (override));
73   MOCK_METHOD((std::map<uint32_t, RtpState>),
74               GetRtpStates,
75               (),
76               (const, override));
77   MOCK_METHOD((std::map<uint32_t, RtpPayloadState>),
78               GetRtpPayloadStates,
79               (),
80               (const, override));
81   MOCK_METHOD(void, DeliverRtcp, (const uint8_t*, size_t), (override));
82   MOCK_METHOD(void,
83               OnBitrateAllocationUpdated,
84               (const VideoBitrateAllocation&),
85               (override));
86   MOCK_METHOD(void,
87               OnVideoLayersAllocationUpdated,
88               (const VideoLayersAllocation&),
89               (override));
90   MOCK_METHOD(EncodedImageCallback::Result,
91               OnEncodedImage,
92               (const EncodedImage&, const CodecSpecificInfo*),
93               (override));
94   MOCK_METHOD(void, OnTransportOverheadChanged, (size_t), (override));
95   MOCK_METHOD(void,
96               OnBitrateUpdated,
97               (BitrateAllocationUpdate, int),
98               (override));
99   MOCK_METHOD(uint32_t, GetPayloadBitrateBps, (), (const, override));
100   MOCK_METHOD(uint32_t, GetProtectionBitrateBps, (), (const, override));
101   MOCK_METHOD(void, SetEncodingData, (size_t, size_t, size_t), (override));
102   MOCK_METHOD(std::vector<RtpSequenceNumberMap::Info>,
103               GetSentRtpPacketInfos,
104               (uint32_t ssrc, rtc::ArrayView<const uint16_t> sequence_numbers),
105               (const, override));
106 
107   MOCK_METHOD(void, SetFecAllowed, (bool fec_allowed), (override));
108 };
109 
CreateAllocation(int bitrate_bps)110 BitrateAllocationUpdate CreateAllocation(int bitrate_bps) {
111   BitrateAllocationUpdate update;
112   update.target_bitrate = DataRate::BitsPerSec(bitrate_bps);
113   update.packet_loss_ratio = 0;
114   update.round_trip_time = TimeDelta::Zero();
115   return update;
116 }
117 }  // namespace
118 
119 class VideoSendStreamImplTest : public ::testing::Test {
120  protected:
VideoSendStreamImplTest()121   VideoSendStreamImplTest()
122       : time_controller_(Timestamp::Seconds(1000)),
123         config_(&transport_),
124         send_delay_stats_(time_controller_.GetClock()),
125         worker_queue_(field_trials_,
126                       "worker_queue",
127                       time_controller_.GetTaskQueueFactory()),
128         encoder_queue_(time_controller_.GetTaskQueueFactory()->CreateTaskQueue(
129             "encoder_queue",
130             TaskQueueFactory::Priority::NORMAL)),
131         stats_proxy_(time_controller_.GetClock(),
132                      config_,
133                      VideoEncoderConfig::ContentType::kRealtimeVideo,
134                      field_trials_) {
135     config_.rtp.ssrcs.push_back(8080);
136     config_.rtp.payload_type = 1;
137 
138     EXPECT_CALL(transport_controller_, packet_router())
139         .WillRepeatedly(Return(&packet_router_));
140     EXPECT_CALL(transport_controller_, CreateRtpVideoSender)
141         .WillRepeatedly(Return(&rtp_video_sender_));
142     ON_CALL(rtp_video_sender_, Stop()).WillByDefault(::testing::Invoke([&] {
143       active_modules_.clear();
144     }));
145     ON_CALL(rtp_video_sender_, IsActive())
146         .WillByDefault(::testing::Invoke([&]() {
147           for (bool enabled : active_modules_) {
148             if (enabled)
149               return true;
150           }
151           return false;
152         }));
153     ON_CALL(rtp_video_sender_, SetActiveModules)
154         .WillByDefault(::testing::SaveArg<0>(&active_modules_));
155     ON_CALL(transport_controller_, GetWorkerQueue())
156         .WillByDefault(Return(&worker_queue_));
157   }
~VideoSendStreamImplTest()158   ~VideoSendStreamImplTest() {}
159 
CreateVideoSendStreamImpl(int initial_encoder_max_bitrate,double initial_encoder_bitrate_priority,VideoEncoderConfig::ContentType content_type)160   std::unique_ptr<VideoSendStreamImpl> CreateVideoSendStreamImpl(
161       int initial_encoder_max_bitrate,
162       double initial_encoder_bitrate_priority,
163       VideoEncoderConfig::ContentType content_type) {
164     RTC_DCHECK(!worker_queue_.IsCurrent());
165 
166     EXPECT_CALL(bitrate_allocator_, GetStartBitrate(_))
167         .WillOnce(Return(123000));
168 
169     std::map<uint32_t, RtpState> suspended_ssrcs;
170     std::map<uint32_t, RtpPayloadState> suspended_payload_states;
171     auto ret = std::make_unique<VideoSendStreamImpl>(
172         time_controller_.GetClock(), &stats_proxy_, &transport_controller_,
173         &bitrate_allocator_, &video_stream_encoder_, &config_,
174         initial_encoder_max_bitrate, initial_encoder_bitrate_priority,
175         content_type, &rtp_video_sender_, field_trials_);
176 
177     // The call to GetStartBitrate() executes asynchronously on the tq.
178     // Ensure all tasks get to run.
179     time_controller_.AdvanceTime(TimeDelta::Zero());
180     testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_);
181 
182     return ret;
183   }
184 
185  protected:
186   GlobalSimulatedTimeController time_controller_;
187   webrtc::test::ScopedKeyValueConfig field_trials_;
188   NiceMock<MockTransport> transport_;
189   NiceMock<MockRtpTransportControllerSend> transport_controller_;
190   NiceMock<MockBitrateAllocator> bitrate_allocator_;
191   NiceMock<MockVideoStreamEncoder> video_stream_encoder_;
192   NiceMock<MockRtpVideoSender> rtp_video_sender_;
193   std::vector<bool> active_modules_;
194 
195   RtcEventLogNull event_log_;
196   VideoSendStream::Config config_;
197   SendDelayStats send_delay_stats_;
198   MaybeWorkerThread worker_queue_;
199   std::unique_ptr<TaskQueueBase, TaskQueueDeleter> encoder_queue_;
200   SendStatisticsProxy stats_proxy_;
201   PacketRouter packet_router_;
202 };
203 
TEST_F(VideoSendStreamImplTest,RegistersAsBitrateObserverOnStart)204 TEST_F(VideoSendStreamImplTest, RegistersAsBitrateObserverOnStart) {
205   auto vss_impl = CreateVideoSendStreamImpl(
206       kDefaultInitialBitrateBps, kDefaultBitratePriority,
207       VideoEncoderConfig::ContentType::kRealtimeVideo);
208   const bool kSuspend = false;
209   config_.suspend_below_min_bitrate = kSuspend;
210   EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
211       .WillOnce(Invoke(
212           [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) {
213             EXPECT_EQ(config.min_bitrate_bps, 0u);
214             EXPECT_EQ(config.max_bitrate_bps, kDefaultInitialBitrateBps);
215             EXPECT_EQ(config.pad_up_bitrate_bps, 0u);
216             EXPECT_EQ(config.enforce_min_bitrate, !kSuspend);
217             EXPECT_EQ(config.bitrate_priority, kDefaultBitratePriority);
218           }));
219   worker_queue_.RunSynchronous([&] {
220     vss_impl->StartPerRtpStream({true});
221     EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get())).Times(1);
222     vss_impl->Stop();
223   });
224 }
225 
TEST_F(VideoSendStreamImplTest,UpdatesObserverOnConfigurationChange)226 TEST_F(VideoSendStreamImplTest, UpdatesObserverOnConfigurationChange) {
227   const bool kSuspend = false;
228   config_.suspend_below_min_bitrate = kSuspend;
229   config_.rtp.extensions.emplace_back(RtpExtension::kTransportSequenceNumberUri,
230                                       1);
231   auto vss_impl = CreateVideoSendStreamImpl(
232       kDefaultInitialBitrateBps, kDefaultBitratePriority,
233       VideoEncoderConfig::ContentType::kRealtimeVideo);
234 
235   worker_queue_.RunSynchronous([&] { vss_impl->StartPerRtpStream({true}); });
236 
237   // QVGA + VGA configuration matching defaults in
238   // media/engine/simulcast.cc.
239   VideoStream qvga_stream;
240   qvga_stream.width = 320;
241   qvga_stream.height = 180;
242   qvga_stream.max_framerate = 30;
243   qvga_stream.min_bitrate_bps = 30000;
244   qvga_stream.target_bitrate_bps = 150000;
245   qvga_stream.max_bitrate_bps = 200000;
246   qvga_stream.max_qp = 56;
247   qvga_stream.bitrate_priority = 1;
248 
249   VideoStream vga_stream;
250   vga_stream.width = 640;
251   vga_stream.height = 360;
252   vga_stream.max_framerate = 30;
253   vga_stream.min_bitrate_bps = 150000;
254   vga_stream.target_bitrate_bps = 500000;
255   vga_stream.max_bitrate_bps = 700000;
256   vga_stream.max_qp = 56;
257   vga_stream.bitrate_priority = 1;
258 
259   int min_transmit_bitrate_bps = 30000;
260 
261   config_.rtp.ssrcs.emplace_back(1);
262   config_.rtp.ssrcs.emplace_back(2);
263 
264   EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
265       .WillRepeatedly(Invoke(
266           [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) {
267             EXPECT_TRUE(worker_queue_.IsCurrent());
268             EXPECT_EQ(config.min_bitrate_bps,
269                       static_cast<uint32_t>(min_transmit_bitrate_bps));
270             EXPECT_EQ(config.max_bitrate_bps,
271                       static_cast<uint32_t>(qvga_stream.max_bitrate_bps +
272                                             vga_stream.max_bitrate_bps));
273             if (config.pad_up_bitrate_bps != 0) {
274               EXPECT_EQ(config.pad_up_bitrate_bps,
275                         static_cast<uint32_t>(qvga_stream.target_bitrate_bps +
276                                               vga_stream.min_bitrate_bps));
277             }
278             EXPECT_EQ(config.enforce_min_bitrate, !kSuspend);
279           }));
280 
281   encoder_queue_->PostTask([&] {
282     static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
283         ->OnEncoderConfigurationChanged(
284             std::vector<VideoStream>{qvga_stream, vga_stream}, false,
285             VideoEncoderConfig::ContentType::kRealtimeVideo,
286             min_transmit_bitrate_bps);
287   });
288   time_controller_.AdvanceTime(TimeDelta::Zero());
289   worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
290 }
291 
TEST_F(VideoSendStreamImplTest,UpdatesObserverOnConfigurationChangeWithAlr)292 TEST_F(VideoSendStreamImplTest, UpdatesObserverOnConfigurationChangeWithAlr) {
293   const bool kSuspend = false;
294   config_.suspend_below_min_bitrate = kSuspend;
295   config_.rtp.extensions.emplace_back(RtpExtension::kTransportSequenceNumberUri,
296                                       1);
297   config_.periodic_alr_bandwidth_probing = true;
298   auto vss_impl = CreateVideoSendStreamImpl(
299       kDefaultInitialBitrateBps, kDefaultBitratePriority,
300       VideoEncoderConfig::ContentType::kScreen);
301   worker_queue_.RunSynchronous([&] { vss_impl->StartPerRtpStream({true}); });
302 
303   // Simulcast screenshare.
304   VideoStream low_stream;
305   low_stream.width = 1920;
306   low_stream.height = 1080;
307   low_stream.max_framerate = 5;
308   low_stream.min_bitrate_bps = 30000;
309   low_stream.target_bitrate_bps = 200000;
310   low_stream.max_bitrate_bps = 1000000;
311   low_stream.num_temporal_layers = 2;
312   low_stream.max_qp = 56;
313   low_stream.bitrate_priority = 1;
314 
315   VideoStream high_stream;
316   high_stream.width = 1920;
317   high_stream.height = 1080;
318   high_stream.max_framerate = 30;
319   high_stream.min_bitrate_bps = 60000;
320   high_stream.target_bitrate_bps = 1250000;
321   high_stream.max_bitrate_bps = 1250000;
322   high_stream.num_temporal_layers = 2;
323   high_stream.max_qp = 56;
324   high_stream.bitrate_priority = 1;
325 
326   // With ALR probing, this will be the padding target instead of
327   // low_stream.target_bitrate_bps + high_stream.min_bitrate_bps.
328   int min_transmit_bitrate_bps = 400000;
329 
330   config_.rtp.ssrcs.emplace_back(1);
331   config_.rtp.ssrcs.emplace_back(2);
332 
333   EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
334       .WillRepeatedly(Invoke(
335           [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) {
336             EXPECT_TRUE(worker_queue_.IsCurrent());
337             EXPECT_EQ(config.min_bitrate_bps,
338                       static_cast<uint32_t>(low_stream.min_bitrate_bps));
339             EXPECT_EQ(config.max_bitrate_bps,
340                       static_cast<uint32_t>(low_stream.max_bitrate_bps +
341                                             high_stream.max_bitrate_bps));
342             if (config.pad_up_bitrate_bps != 0) {
343               EXPECT_EQ(config.pad_up_bitrate_bps,
344                         static_cast<uint32_t>(min_transmit_bitrate_bps));
345             }
346             EXPECT_EQ(config.enforce_min_bitrate, !kSuspend);
347           }));
348   encoder_queue_->PostTask([&] {
349     static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
350         ->OnEncoderConfigurationChanged(
351             std::vector<VideoStream>{low_stream, high_stream}, false,
352             VideoEncoderConfig::ContentType::kScreen, min_transmit_bitrate_bps);
353   });
354   time_controller_.AdvanceTime(TimeDelta::Zero());
355   worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
356 }
357 
TEST_F(VideoSendStreamImplTest,UpdatesObserverOnConfigurationChangeWithSimulcastVideoHysteresis)358 TEST_F(VideoSendStreamImplTest,
359        UpdatesObserverOnConfigurationChangeWithSimulcastVideoHysteresis) {
360   test::ScopedKeyValueConfig hysteresis_experiment(
361       field_trials_, "WebRTC-VideoRateControl/video_hysteresis:1.25/");
362 
363   auto vss_impl = CreateVideoSendStreamImpl(
364       kDefaultInitialBitrateBps, kDefaultBitratePriority,
365       VideoEncoderConfig::ContentType::kRealtimeVideo);
366 
367   worker_queue_.RunSynchronous([&] { vss_impl->StartPerRtpStream({true}); });
368   // 2-layer video simulcast.
369   VideoStream low_stream;
370   low_stream.width = 320;
371   low_stream.height = 240;
372   low_stream.max_framerate = 30;
373   low_stream.min_bitrate_bps = 30000;
374   low_stream.target_bitrate_bps = 100000;
375   low_stream.max_bitrate_bps = 200000;
376   low_stream.max_qp = 56;
377   low_stream.bitrate_priority = 1;
378 
379   VideoStream high_stream;
380   high_stream.width = 640;
381   high_stream.height = 480;
382   high_stream.max_framerate = 30;
383   high_stream.min_bitrate_bps = 150000;
384   high_stream.target_bitrate_bps = 500000;
385   high_stream.max_bitrate_bps = 750000;
386   high_stream.max_qp = 56;
387   high_stream.bitrate_priority = 1;
388 
389   config_.rtp.ssrcs.emplace_back(1);
390   config_.rtp.ssrcs.emplace_back(2);
391 
392   EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
393       .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*,
394                                  MediaStreamAllocationConfig config) {
395         EXPECT_TRUE(worker_queue_.IsCurrent());
396         EXPECT_EQ(config.min_bitrate_bps,
397                   static_cast<uint32_t>(low_stream.min_bitrate_bps));
398         EXPECT_EQ(config.max_bitrate_bps,
399                   static_cast<uint32_t>(low_stream.max_bitrate_bps +
400                                         high_stream.max_bitrate_bps));
401         if (config.pad_up_bitrate_bps != 0) {
402           EXPECT_EQ(config.pad_up_bitrate_bps,
403                     static_cast<uint32_t>(low_stream.target_bitrate_bps +
404                                           1.25 * high_stream.min_bitrate_bps));
405         }
406       }));
407 
408   encoder_queue_->PostTask([&] {
409     static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
410         ->OnEncoderConfigurationChanged(
411             std::vector<VideoStream>{low_stream, high_stream}, false,
412             VideoEncoderConfig::ContentType::kRealtimeVideo,
413             /*min_transmit_bitrate_bps=*/0);
414   });
415   time_controller_.AdvanceTime(TimeDelta::Zero());
416   worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
417 }
418 
TEST_F(VideoSendStreamImplTest,SetsScreensharePacingFactorWithFeedback)419 TEST_F(VideoSendStreamImplTest, SetsScreensharePacingFactorWithFeedback) {
420   test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString());
421 
422   constexpr int kId = 1;
423   config_.rtp.extensions.emplace_back(RtpExtension::kTransportSequenceNumberUri,
424                                       kId);
425   EXPECT_CALL(transport_controller_,
426               SetPacingFactor(kAlrProbingExperimentPaceMultiplier))
427       .Times(1);
428   auto vss_impl = CreateVideoSendStreamImpl(
429       kDefaultInitialBitrateBps, kDefaultBitratePriority,
430       VideoEncoderConfig::ContentType::kScreen);
431   worker_queue_.RunSynchronous([&] {
432     vss_impl->StartPerRtpStream({true});
433     vss_impl->Stop();
434   });
435 }
436 
TEST_F(VideoSendStreamImplTest,DoesNotSetPacingFactorWithoutFeedback)437 TEST_F(VideoSendStreamImplTest, DoesNotSetPacingFactorWithoutFeedback) {
438   test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString());
439   auto vss_impl = CreateVideoSendStreamImpl(
440       kDefaultInitialBitrateBps, kDefaultBitratePriority,
441       VideoEncoderConfig::ContentType::kScreen);
442   worker_queue_.RunSynchronous([&] {
443     EXPECT_CALL(transport_controller_, SetPacingFactor(_)).Times(0);
444     vss_impl->StartPerRtpStream({true});
445     vss_impl->Stop();
446   });
447 }
448 
TEST_F(VideoSendStreamImplTest,ForwardsVideoBitrateAllocationWhenEnabled)449 TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationWhenEnabled) {
450   auto vss_impl = CreateVideoSendStreamImpl(
451       kDefaultInitialBitrateBps, kDefaultBitratePriority,
452       VideoEncoderConfig::ContentType::kScreen);
453 
454   EXPECT_CALL(transport_controller_, SetPacingFactor(_)).Times(0);
455   VideoStreamEncoderInterface::EncoderSink* const sink =
456       static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get());
457   worker_queue_.RunSynchronous([&] { vss_impl->StartPerRtpStream({true}); });
458   // Populate a test instance of video bitrate allocation.
459   VideoBitrateAllocation alloc;
460   alloc.SetBitrate(0, 0, 10000);
461   alloc.SetBitrate(0, 1, 20000);
462   alloc.SetBitrate(1, 0, 30000);
463   alloc.SetBitrate(1, 1, 40000);
464 
465   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(0);
466   encoder_queue_->PostTask([&] {
467     // Encoder starts out paused, don't forward allocation.
468 
469     sink->OnBitrateAllocationUpdated(alloc);
470   });
471   time_controller_.AdvanceTime(TimeDelta::Zero());
472 
473   worker_queue_.RunSynchronous([&] {
474     // Unpause encoder, allocation should be passed through.
475     const uint32_t kBitrateBps = 100000;
476     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
477         .Times(1)
478         .WillOnce(Return(kBitrateBps));
479     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
480         ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
481   });
482   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1);
483   encoder_queue_->PostTask([&] { sink->OnBitrateAllocationUpdated(alloc); });
484   time_controller_.AdvanceTime(TimeDelta::Zero());
485   worker_queue_.RunSynchronous([&] {
486     // Pause encoder again, and block allocations.
487     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
488         .Times(1)
489         .WillOnce(Return(0));
490     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
491         ->OnBitrateUpdated(CreateAllocation(0));
492   });
493   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(0);
494   encoder_queue_->PostTask([&] { sink->OnBitrateAllocationUpdated(alloc); });
495   time_controller_.AdvanceTime(TimeDelta::Zero());
496   worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
497 }
498 
TEST_F(VideoSendStreamImplTest,ThrottlesVideoBitrateAllocationWhenTooSimilar)499 TEST_F(VideoSendStreamImplTest, ThrottlesVideoBitrateAllocationWhenTooSimilar) {
500   auto vss_impl = CreateVideoSendStreamImpl(
501       kDefaultInitialBitrateBps, kDefaultBitratePriority,
502       VideoEncoderConfig::ContentType::kScreen);
503   worker_queue_.RunSynchronous([&] {
504     vss_impl->StartPerRtpStream({true});
505     // Unpause encoder, to allows allocations to be passed through.
506     const uint32_t kBitrateBps = 100000;
507     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
508         .Times(1)
509         .WillOnce(Return(kBitrateBps));
510     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
511         ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
512   });
513   VideoStreamEncoderInterface::EncoderSink* const sink =
514       static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get());
515 
516   // Populate a test instance of video bitrate allocation.
517   VideoBitrateAllocation alloc;
518   alloc.SetBitrate(0, 0, 10000);
519   alloc.SetBitrate(0, 1, 20000);
520   alloc.SetBitrate(1, 0, 30000);
521   alloc.SetBitrate(1, 1, 40000);
522 
523   // Initial value.
524   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1);
525   encoder_queue_->PostTask([&] { sink->OnBitrateAllocationUpdated(alloc); });
526   time_controller_.AdvanceTime(TimeDelta::Zero());
527 
528   VideoBitrateAllocation updated_alloc = alloc;
529   // Needs 10% increase in bitrate to trigger immediate forward.
530   const uint32_t base_layer_min_update_bitrate_bps =
531       alloc.GetBitrate(0, 0) + alloc.get_sum_bps() / 10;
532 
533   // Too small increase, don't forward.
534   updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps - 1);
535   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(_)).Times(0);
536   encoder_queue_->PostTask(
537       [&] { sink->OnBitrateAllocationUpdated(updated_alloc); });
538   time_controller_.AdvanceTime(TimeDelta::Zero());
539 
540   // Large enough increase, do forward.
541   updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps);
542   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(updated_alloc))
543       .Times(1);
544   encoder_queue_->PostTask(
545       [&] { sink->OnBitrateAllocationUpdated(updated_alloc); });
546   time_controller_.AdvanceTime(TimeDelta::Zero());
547 
548   // This is now a decrease compared to last forward allocation,
549   // forward immediately.
550   updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps - 1);
551   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(updated_alloc))
552       .Times(1);
553   encoder_queue_->PostTask(
554       [&] { sink->OnBitrateAllocationUpdated(updated_alloc); });
555   time_controller_.AdvanceTime(TimeDelta::Zero());
556 
557   worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
558 }
559 
TEST_F(VideoSendStreamImplTest,ForwardsVideoBitrateAllocationOnLayerChange)560 TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationOnLayerChange) {
561   auto vss_impl = CreateVideoSendStreamImpl(
562       kDefaultInitialBitrateBps, kDefaultBitratePriority,
563       VideoEncoderConfig::ContentType::kScreen);
564 
565   worker_queue_.RunSynchronous([&] {
566     vss_impl->StartPerRtpStream({true});
567     // Unpause encoder, to allows allocations to be passed through.
568     const uint32_t kBitrateBps = 100000;
569     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
570         .Times(1)
571         .WillOnce(Return(kBitrateBps));
572     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
573         ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
574   });
575   VideoStreamEncoderInterface::EncoderSink* const sink =
576       static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get());
577 
578   // Populate a test instance of video bitrate allocation.
579   VideoBitrateAllocation alloc;
580   alloc.SetBitrate(0, 0, 10000);
581   alloc.SetBitrate(0, 1, 20000);
582   alloc.SetBitrate(1, 0, 30000);
583   alloc.SetBitrate(1, 1, 40000);
584 
585   // Initial value.
586   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1);
587   sink->OnBitrateAllocationUpdated(alloc);
588 
589   // Move some bitrate from one layer to a new one, but keep sum the
590   // same. Since layout has changed, immediately trigger forward.
591   VideoBitrateAllocation updated_alloc = alloc;
592   updated_alloc.SetBitrate(2, 0, 10000);
593   updated_alloc.SetBitrate(1, 1, alloc.GetBitrate(1, 1) - 10000);
594   EXPECT_EQ(alloc.get_sum_bps(), updated_alloc.get_sum_bps());
595   EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(updated_alloc))
596       .Times(1);
597   encoder_queue_->PostTask(
598       [&] { sink->OnBitrateAllocationUpdated(updated_alloc); });
599   time_controller_.AdvanceTime(TimeDelta::Zero());
600 
601   worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
602 }
603 
TEST_F(VideoSendStreamImplTest,ForwardsVideoBitrateAllocationAfterTimeout)604 TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationAfterTimeout) {
605   auto vss_impl = CreateVideoSendStreamImpl(
606       kDefaultInitialBitrateBps, kDefaultBitratePriority,
607       VideoEncoderConfig::ContentType::kScreen);
608   worker_queue_.RunSynchronous([&] {
609     vss_impl->StartPerRtpStream({true});
610     const uint32_t kBitrateBps = 100000;
611     // Unpause encoder, to allows allocations to be passed through.
612     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
613         .Times(1)
614         .WillRepeatedly(Return(kBitrateBps));
615     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
616         ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
617   });
618   VideoStreamEncoderInterface::EncoderSink* const sink =
619       static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get());
620 
621   // Populate a test instance of video bitrate allocation.
622   VideoBitrateAllocation alloc;
623 
624   alloc.SetBitrate(0, 0, 10000);
625   alloc.SetBitrate(0, 1, 20000);
626   alloc.SetBitrate(1, 0, 30000);
627   alloc.SetBitrate(1, 1, 40000);
628 
629   EncodedImage encoded_image;
630   CodecSpecificInfo codec_specific;
631   EXPECT_CALL(rtp_video_sender_, OnEncodedImage)
632       .WillRepeatedly(Return(
633           EncodedImageCallback::Result(EncodedImageCallback::Result::OK)));
634   // Max time we will throttle similar video bitrate allocations.
635   static constexpr int64_t kMaxVbaThrottleTimeMs = 500;
636 
637   {
638     // Initial value.
639     EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1);
640     encoder_queue_->PostTask([&] { sink->OnBitrateAllocationUpdated(alloc); });
641     time_controller_.AdvanceTime(TimeDelta::Zero());
642   }
643 
644   {
645     EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(0);
646     encoder_queue_->PostTask([&] {
647       // Sending same allocation again, this one should be throttled.
648       sink->OnBitrateAllocationUpdated(alloc);
649     });
650     time_controller_.AdvanceTime(TimeDelta::Zero());
651   }
652 
653   time_controller_.AdvanceTime(TimeDelta::Millis(kMaxVbaThrottleTimeMs));
654   {
655     EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1);
656     encoder_queue_->PostTask([&] {
657       // Sending similar allocation again after timeout, should
658       // forward.
659       sink->OnBitrateAllocationUpdated(alloc);
660     });
661     time_controller_.AdvanceTime(TimeDelta::Zero());
662   }
663 
664   {
665     EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(0);
666     encoder_queue_->PostTask([&] {
667       // Sending similar allocation again without timeout, throttle.
668       sink->OnBitrateAllocationUpdated(alloc);
669     });
670     time_controller_.AdvanceTime(TimeDelta::Zero());
671   }
672 
673   {
674     EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(0);
675     encoder_queue_->PostTask([&] {
676       // Send encoded image, should be a noop.
677       static_cast<EncodedImageCallback*>(vss_impl.get())
678           ->OnEncodedImage(encoded_image, &codec_specific);
679     });
680     time_controller_.AdvanceTime(TimeDelta::Zero());
681   }
682 
683   {
684     // Advance time and send encoded image, this should wake up and
685     // send cached bitrate allocation.
686     time_controller_.AdvanceTime(TimeDelta::Millis(kMaxVbaThrottleTimeMs));
687 
688     EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1);
689     encoder_queue_->PostTask([&] {
690       static_cast<EncodedImageCallback*>(vss_impl.get())
691           ->OnEncodedImage(encoded_image, &codec_specific);
692     });
693     time_controller_.AdvanceTime(TimeDelta::Zero());
694   }
695 
696   {
697     // Advance time and send encoded image, there should be no
698     // cached allocation to send.
699     time_controller_.AdvanceTime(TimeDelta::Millis(kMaxVbaThrottleTimeMs));
700     EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(0);
701     encoder_queue_->PostTask([&] {
702       static_cast<EncodedImageCallback*>(vss_impl.get())
703           ->OnEncodedImage(encoded_image, &codec_specific);
704     });
705     time_controller_.AdvanceTime(TimeDelta::Zero());
706   }
707 
708   worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
709 }
710 
TEST_F(VideoSendStreamImplTest,CallsVideoStreamEncoderOnBitrateUpdate)711 TEST_F(VideoSendStreamImplTest, CallsVideoStreamEncoderOnBitrateUpdate) {
712   const bool kSuspend = false;
713   config_.suspend_below_min_bitrate = kSuspend;
714   config_.rtp.extensions.emplace_back(RtpExtension::kTransportSequenceNumberUri,
715                                       1);
716   auto vss_impl = CreateVideoSendStreamImpl(
717       kDefaultInitialBitrateBps, kDefaultBitratePriority,
718       VideoEncoderConfig::ContentType::kRealtimeVideo);
719   worker_queue_.RunSynchronous([&] { vss_impl->StartPerRtpStream({true}); });
720   VideoStream qvga_stream;
721   qvga_stream.width = 320;
722   qvga_stream.height = 180;
723   qvga_stream.max_framerate = 30;
724   qvga_stream.min_bitrate_bps = 30000;
725   qvga_stream.target_bitrate_bps = 150000;
726   qvga_stream.max_bitrate_bps = 200000;
727   qvga_stream.max_qp = 56;
728   qvga_stream.bitrate_priority = 1;
729 
730   int min_transmit_bitrate_bps = 30000;
731 
732   config_.rtp.ssrcs.emplace_back(1);
733 
734   encoder_queue_->PostTask([&] {
735     static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
736         ->OnEncoderConfigurationChanged(
737             std::vector<VideoStream>{qvga_stream}, false,
738             VideoEncoderConfig::ContentType::kRealtimeVideo,
739             min_transmit_bitrate_bps);
740   });
741   time_controller_.AdvanceTime(TimeDelta::Zero());
742 
743   worker_queue_.RunSynchronous([&] {
744     const DataRate network_constrained_rate =
745         DataRate::BitsPerSec(qvga_stream.target_bitrate_bps);
746     BitrateAllocationUpdate update;
747     update.target_bitrate = network_constrained_rate;
748     update.stable_target_bitrate = network_constrained_rate;
749     update.round_trip_time = TimeDelta::Millis(1);
750     EXPECT_CALL(rtp_video_sender_, OnBitrateUpdated(update, _));
751     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
752         .WillOnce(Return(network_constrained_rate.bps()));
753     EXPECT_CALL(
754         video_stream_encoder_,
755         OnBitrateUpdated(network_constrained_rate, network_constrained_rate,
756                          network_constrained_rate, 0, _, 0));
757     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
758         ->OnBitrateUpdated(update);
759 
760     // Test allocation where the link allocation is larger than the
761     // target, meaning we have some headroom on the link.
762     const DataRate qvga_max_bitrate =
763         DataRate::BitsPerSec(qvga_stream.max_bitrate_bps);
764     const DataRate headroom = DataRate::BitsPerSec(50000);
765     const DataRate rate_with_headroom = qvga_max_bitrate + headroom;
766     update.target_bitrate = rate_with_headroom;
767     update.stable_target_bitrate = rate_with_headroom;
768     EXPECT_CALL(rtp_video_sender_, OnBitrateUpdated(update, _));
769     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
770         .WillOnce(Return(rate_with_headroom.bps()));
771     EXPECT_CALL(video_stream_encoder_,
772                 OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate,
773                                  rate_with_headroom, 0, _, 0));
774     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
775         ->OnBitrateUpdated(update);
776 
777     // Add protection bitrate to the mix, this should be subtracted
778     // from the headroom.
779     const uint32_t protection_bitrate_bps = 10000;
780     EXPECT_CALL(rtp_video_sender_, GetProtectionBitrateBps())
781         .WillOnce(Return(protection_bitrate_bps));
782 
783     EXPECT_CALL(rtp_video_sender_, OnBitrateUpdated(update, _));
784     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
785         .WillOnce(Return(rate_with_headroom.bps()));
786     const DataRate headroom_minus_protection =
787         rate_with_headroom - DataRate::BitsPerSec(protection_bitrate_bps);
788     EXPECT_CALL(video_stream_encoder_,
789                 OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate,
790                                  headroom_minus_protection, 0, _, 0));
791     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
792         ->OnBitrateUpdated(update);
793 
794     // Protection bitrate exceeds head room, link allocation should be
795     // capped to target bitrate.
796     EXPECT_CALL(rtp_video_sender_, GetProtectionBitrateBps())
797         .WillOnce(Return(headroom.bps() + 1000));
798     EXPECT_CALL(rtp_video_sender_, OnBitrateUpdated(update, _));
799     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
800         .WillOnce(Return(rate_with_headroom.bps()));
801     EXPECT_CALL(video_stream_encoder_,
802                 OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate,
803                                  qvga_max_bitrate, 0, _, 0));
804     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
805         ->OnBitrateUpdated(update);
806 
807     // Set rates to zero on stop.
808     EXPECT_CALL(video_stream_encoder_,
809                 OnBitrateUpdated(DataRate::Zero(), DataRate::Zero(),
810                                  DataRate::Zero(), 0, 0, 0));
811     vss_impl->Stop();
812   });
813 }
814 
TEST_F(VideoSendStreamImplTest,DisablesPaddingOnPausedEncoder)815 TEST_F(VideoSendStreamImplTest, DisablesPaddingOnPausedEncoder) {
816   int padding_bitrate = 0;
817   std::unique_ptr<VideoSendStreamImpl> vss_impl = CreateVideoSendStreamImpl(
818       kDefaultInitialBitrateBps, kDefaultBitratePriority,
819       VideoEncoderConfig::ContentType::kRealtimeVideo);
820 
821   // Capture padding bitrate for testing.
822   EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
823       .WillRepeatedly(Invoke(
824           [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) {
825             padding_bitrate = config.pad_up_bitrate_bps;
826           }));
827   // If observer is removed, no padding will be sent.
828   EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get()))
829       .WillRepeatedly(
830           Invoke([&](BitrateAllocatorObserver*) { padding_bitrate = 0; }));
831 
832   EXPECT_CALL(rtp_video_sender_, OnEncodedImage)
833       .WillRepeatedly(Return(
834           EncodedImageCallback::Result(EncodedImageCallback::Result::OK)));
835   const bool kSuspend = false;
836   config_.suspend_below_min_bitrate = kSuspend;
837   config_.rtp.extensions.emplace_back(RtpExtension::kTransportSequenceNumberUri,
838                                       1);
839   VideoStream qvga_stream;
840   qvga_stream.width = 320;
841   qvga_stream.height = 180;
842   qvga_stream.max_framerate = 30;
843   qvga_stream.min_bitrate_bps = 30000;
844   qvga_stream.target_bitrate_bps = 150000;
845   qvga_stream.max_bitrate_bps = 200000;
846   qvga_stream.max_qp = 56;
847   qvga_stream.bitrate_priority = 1;
848 
849   int min_transmit_bitrate_bps = 30000;
850 
851   config_.rtp.ssrcs.emplace_back(1);
852   worker_queue_.RunSynchronous([&] { vss_impl->StartPerRtpStream({true}); });
853   // Starts without padding.
854   EXPECT_EQ(0, padding_bitrate);
855   encoder_queue_->PostTask([&] {
856     // Reconfigure e.g. due to a fake frame.
857     static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
858         ->OnEncoderConfigurationChanged(
859             std::vector<VideoStream>{qvga_stream}, false,
860             VideoEncoderConfig::ContentType::kRealtimeVideo,
861             min_transmit_bitrate_bps);
862   });
863   time_controller_.AdvanceTime(TimeDelta::Zero());
864   // Still no padding because no actual frames were passed, only
865   // reconfiguration happened.
866   EXPECT_EQ(0, padding_bitrate);
867 
868   worker_queue_.RunSynchronous([&] {
869     // Unpause encoder.
870     const uint32_t kBitrateBps = 100000;
871     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
872         .Times(1)
873         .WillOnce(Return(kBitrateBps));
874     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
875         ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
876   });
877 
878   encoder_queue_->PostTask([&] {
879     // A frame is encoded.
880     EncodedImage encoded_image;
881     CodecSpecificInfo codec_specific;
882     static_cast<EncodedImageCallback*>(vss_impl.get())
883         ->OnEncodedImage(encoded_image, &codec_specific);
884   });
885   time_controller_.AdvanceTime(TimeDelta::Zero());
886   // Only after actual frame is encoded are we enabling the padding.
887   EXPECT_GT(padding_bitrate, 0);
888 
889   time_controller_.AdvanceTime(TimeDelta::Seconds(5));
890   // Since no more frames are sent the last 5s, no padding is supposed to be
891   // sent.
892   EXPECT_EQ(0, padding_bitrate);
893   testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_);
894   worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
895 }
896 
TEST_F(VideoSendStreamImplTest,KeepAliveOnDroppedFrame)897 TEST_F(VideoSendStreamImplTest, KeepAliveOnDroppedFrame) {
898   std::unique_ptr<VideoSendStreamImpl> vss_impl = CreateVideoSendStreamImpl(
899       kDefaultInitialBitrateBps, kDefaultBitratePriority,
900       VideoEncoderConfig::ContentType::kRealtimeVideo);
901   EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get())).Times(0);
902   worker_queue_.RunSynchronous([&] {
903     vss_impl->StartPerRtpStream({true});
904     const uint32_t kBitrateBps = 100000;
905     EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
906         .Times(1)
907         .WillOnce(Return(kBitrateBps));
908     static_cast<BitrateAllocatorObserver*>(vss_impl.get())
909         ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
910   });
911   encoder_queue_->PostTask([&] {
912     // Keep the stream from deallocating by dropping a frame.
913     static_cast<EncodedImageCallback*>(vss_impl.get())
914         ->OnDroppedFrame(EncodedImageCallback::DropReason::kDroppedByEncoder);
915   });
916   time_controller_.AdvanceTime(TimeDelta::Seconds(2));
917   worker_queue_.RunSynchronous([&] {
918     testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_);
919     vss_impl->Stop();
920   });
921 }
922 
TEST_F(VideoSendStreamImplTest,ConfiguresBitratesForSvc)923 TEST_F(VideoSendStreamImplTest, ConfiguresBitratesForSvc) {
924   struct TestConfig {
925     bool screenshare = false;
926     bool alr = false;
927     int min_padding_bitrate_bps = 0;
928   };
929 
930   std::vector<TestConfig> test_variants;
931   for (bool screenshare : {false, true}) {
932     for (bool alr : {false, true}) {
933       for (int min_padding : {0, 400000}) {
934         test_variants.push_back({screenshare, alr, min_padding});
935       }
936     }
937   }
938 
939   for (const TestConfig& test_config : test_variants) {
940     const bool kSuspend = false;
941     config_.suspend_below_min_bitrate = kSuspend;
942     config_.rtp.extensions.emplace_back(
943         RtpExtension::kTransportSequenceNumberUri, 1);
944     config_.periodic_alr_bandwidth_probing = test_config.alr;
945     auto vss_impl = CreateVideoSendStreamImpl(
946         kDefaultInitialBitrateBps, kDefaultBitratePriority,
947         test_config.screenshare
948             ? VideoEncoderConfig::ContentType::kScreen
949             : VideoEncoderConfig::ContentType::kRealtimeVideo);
950 
951     worker_queue_.RunSynchronous([&] { vss_impl->StartPerRtpStream({true}); });
952 
953     // Svc
954     VideoStream stream;
955     stream.width = 1920;
956     stream.height = 1080;
957     stream.max_framerate = 30;
958     stream.min_bitrate_bps = 60000;
959     stream.target_bitrate_bps = 6000000;
960     stream.max_bitrate_bps = 1250000;
961     stream.num_temporal_layers = 2;
962     stream.max_qp = 56;
963     stream.bitrate_priority = 1;
964 
965     config_.rtp.ssrcs.emplace_back(1);
966     config_.rtp.ssrcs.emplace_back(2);
967 
968     EXPECT_CALL(
969         bitrate_allocator_,
970         AddObserver(
971             vss_impl.get(),
972             AllOf(Field(&MediaStreamAllocationConfig::min_bitrate_bps,
973                         static_cast<uint32_t>(stream.min_bitrate_bps)),
974                   Field(&MediaStreamAllocationConfig::max_bitrate_bps,
975                         static_cast<uint32_t>(stream.max_bitrate_bps)),
976                   // Stream not yet active - no padding.
977                   Field(&MediaStreamAllocationConfig::pad_up_bitrate_bps, 0u),
978                   Field(&MediaStreamAllocationConfig::enforce_min_bitrate,
979                         !kSuspend))));
980     encoder_queue_->PostTask([&] {
981       static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
982           ->OnEncoderConfigurationChanged(
983               std::vector<VideoStream>{stream}, true,
984               test_config.screenshare
985                   ? VideoEncoderConfig::ContentType::kScreen
986                   : VideoEncoderConfig::ContentType::kRealtimeVideo,
987               test_config.min_padding_bitrate_bps);
988     });
989     time_controller_.AdvanceTime(TimeDelta::Zero());
990     ::testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_);
991 
992     // Simulate an encoded image, this will turn the stream active and
993     // enable padding.
994     EXPECT_CALL(rtp_video_sender_, OnEncodedImage)
995         .WillRepeatedly(Return(
996             EncodedImageCallback::Result(EncodedImageCallback::Result::OK)));
997     // Screensharing implicitly forces ALR.
998     const bool using_alr = test_config.alr || test_config.screenshare;
999     // If ALR is used, pads only to min bitrate as rampup is handled by
1000     // probing. Otherwise target_bitrate contains the padding target.
1001     int expected_padding =
1002         using_alr ? stream.min_bitrate_bps
1003                   : static_cast<int>(stream.target_bitrate_bps *
1004                                      (test_config.screenshare ? 1.35 : 1.2));
1005     // Min padding bitrate may override padding target.
1006     expected_padding =
1007         std::max(expected_padding, test_config.min_padding_bitrate_bps);
1008     EXPECT_CALL(
1009         bitrate_allocator_,
1010         AddObserver(
1011             vss_impl.get(),
1012             AllOf(Field(&MediaStreamAllocationConfig::min_bitrate_bps,
1013                         static_cast<uint32_t>(stream.min_bitrate_bps)),
1014                   Field(&MediaStreamAllocationConfig::max_bitrate_bps,
1015                         static_cast<uint32_t>(stream.max_bitrate_bps)),
1016                   // Stream now active - min bitrate use as padding target
1017                   // when ALR is active.
1018                   Field(&MediaStreamAllocationConfig::pad_up_bitrate_bps,
1019                         expected_padding),
1020                   Field(&MediaStreamAllocationConfig::enforce_min_bitrate,
1021                         !kSuspend))));
1022     encoder_queue_->PostTask([&] {
1023       EncodedImage encoded_image;
1024       CodecSpecificInfo codec_specific;
1025 
1026       static_cast<EncodedImageCallback*>(vss_impl.get())
1027           ->OnEncodedImage(encoded_image, &codec_specific);
1028     });
1029     time_controller_.AdvanceTime(TimeDelta::Zero());
1030     ::testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_);
1031 
1032     worker_queue_.RunSynchronous([&] { vss_impl->Stop(); });
1033   }
1034 }
1035 }  // namespace internal
1036 }  // namespace webrtc
1037