blob: 1c44cc8dd497486c63ebd489d671663eb63872ba [file] [log] [blame]
Sebastian Jansson652dc912018-04-19 17:09:15 +02001/*
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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "video/video_send_stream_impl.h"
12
Mirko Bonadei317a1f02019-09-17 17:06:18 +020013#include <memory>
Sebastian Jansson652dc912018-04-19 17:09:15 +020014#include <string>
15
Elad Alon8b60e8b2019-04-08 14:14:05 +020016#include "absl/types/optional.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020017#include "api/rtc_event_log/rtc_event_log.h"
Stefan Holmer9416ef82018-07-19 10:34:38 +020018#include "call/rtp_video_sender.h"
Sebastian Jansson652dc912018-04-19 17:09:15 +020019#include "call/test/mock_bitrate_allocator.h"
20#include "call/test/mock_rtp_transport_controller_send.h"
Elad Alon8b60e8b2019-04-08 14:14:05 +020021#include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020022#include "modules/utility/include/process_thread.h"
Sebastian Jansson652dc912018-04-19 17:09:15 +020023#include "modules/video_coding/fec_controller_default.h"
24#include "rtc_base/experiments/alr_experiment.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/fake_clock.h"
Sebastian Jansson652dc912018-04-19 17:09:15 +020026#include "rtc_base/task_queue_for_test.h"
27#include "test/field_trial.h"
28#include "test/gmock.h"
29#include "test/gtest.h"
30#include "test/mock_transport.h"
31#include "video/test/mock_video_stream_encoder.h"
Sebastian Jansson652dc912018-04-19 17:09:15 +020032
33namespace webrtc {
Sebastian Jansson82ed2e82019-10-15 15:58:37 +020034
35bool operator==(const BitrateAllocationUpdate& a,
36 const BitrateAllocationUpdate& b) {
37 return a.target_bitrate == b.target_bitrate &&
38 a.round_trip_time == b.round_trip_time &&
39 a.packet_loss_ratio == b.packet_loss_ratio;
40}
41
Sebastian Jansson652dc912018-04-19 17:09:15 +020042namespace internal {
43namespace {
Mirko Bonadei6a489f22019-04-09 15:11:12 +020044using ::testing::_;
45using ::testing::Invoke;
46using ::testing::NiceMock;
47using ::testing::Return;
Sebastian Jansson652dc912018-04-19 17:09:15 +020048
49constexpr int64_t kDefaultInitialBitrateBps = 333000;
50const double kDefaultBitratePriority = 0.5;
51
52const float kAlrProbingExperimentPaceMultiplier = 1.0f;
53std::string GetAlrProbingExperimentString() {
54 return std::string(
55 AlrExperimentSettings::kScreenshareProbingBweExperimentName) +
56 "/1.0,2875,80,40,-60,3/";
57}
Stefan Holmer64be7fa2018-10-04 15:21:55 +020058class MockRtpVideoSender : public RtpVideoSenderInterface {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020059 public:
60 MOCK_METHOD1(RegisterProcessThread, void(ProcessThread*));
61 MOCK_METHOD0(DeRegisterProcessThread, void());
62 MOCK_METHOD1(SetActive, void(bool));
63 MOCK_METHOD1(SetActiveModules, void(const std::vector<bool>));
64 MOCK_METHOD0(IsActive, bool());
65 MOCK_METHOD1(OnNetworkAvailability, void(bool));
66 MOCK_CONST_METHOD0(GetRtpStates, std::map<uint32_t, RtpState>());
67 MOCK_CONST_METHOD0(GetRtpPayloadStates,
68 std::map<uint32_t, RtpPayloadState>());
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020069 MOCK_METHOD2(DeliverRtcp, void(const uint8_t*, size_t));
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020070 MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&));
71 MOCK_METHOD3(OnEncodedImage,
72 EncodedImageCallback::Result(const EncodedImage&,
73 const CodecSpecificInfo*,
74 const RTPFragmentationHeader*));
Stefan Holmer64be7fa2018-10-04 15:21:55 +020075 MOCK_METHOD1(OnTransportOverheadChanged, void(size_t));
76 MOCK_METHOD1(OnOverheadChanged, void(size_t));
Sebastian Jansson82ed2e82019-10-15 15:58:37 +020077 MOCK_METHOD2(OnBitrateUpdated, void(BitrateAllocationUpdate, int));
Stefan Holmer64be7fa2018-10-04 15:21:55 +020078 MOCK_CONST_METHOD0(GetPayloadBitrateBps, uint32_t());
79 MOCK_CONST_METHOD0(GetProtectionBitrateBps, uint32_t());
80 MOCK_METHOD3(SetEncodingData, void(size_t, size_t, size_t));
Elad Alon898395d2019-04-10 15:55:00 +020081 MOCK_CONST_METHOD2(GetSentRtpPacketInfos,
82 std::vector<RtpSequenceNumberMap::Info>(
83 uint32_t ssrc,
84 rtc::ArrayView<const uint16_t> sequence_numbers));
Elad Alon8f01c4e2019-06-28 15:19:43 +020085
86 MOCK_METHOD1(SetFecAllowed, void(bool fec_allowed));
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020087};
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020088
89BitrateAllocationUpdate CreateAllocation(int bitrate_bps) {
90 BitrateAllocationUpdate update;
Sebastian Jansson13e59032018-11-21 19:13:07 +010091 update.target_bitrate = DataRate::bps(bitrate_bps);
92 update.packet_loss_ratio = 0;
93 update.round_trip_time = TimeDelta::Zero();
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020094 return update;
95}
Sebastian Jansson652dc912018-04-19 17:09:15 +020096} // namespace
97
98class VideoSendStreamImplTest : public ::testing::Test {
99 protected:
100 VideoSendStreamImplTest()
101 : clock_(1000 * 1000 * 1000),
102 config_(&transport_),
103 send_delay_stats_(&clock_),
Sebastian Jansson652dc912018-04-19 17:09:15 +0200104 test_queue_("test_queue"),
105 process_thread_(ProcessThread::Create("test_thread")),
106 call_stats_(&clock_, process_thread_.get()),
107 stats_proxy_(&clock_,
108 config_,
109 VideoEncoderConfig::ContentType::kRealtimeVideo) {
110 config_.rtp.ssrcs.push_back(8080);
111 config_.rtp.payload_type = 1;
112
Sebastian Jansson652dc912018-04-19 17:09:15 +0200113 EXPECT_CALL(transport_controller_, packet_router())
114 .WillRepeatedly(Return(&packet_router_));
Elad Alon8f01c4e2019-06-28 15:19:43 +0200115 EXPECT_CALL(transport_controller_, CreateRtpVideoSender)
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200116 .WillRepeatedly(Return(&rtp_video_sender_));
117 EXPECT_CALL(rtp_video_sender_, SetActive(_))
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200118 .WillRepeatedly(::testing::Invoke(
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200119 [&](bool active) { rtp_video_sender_active_ = active; }));
120 EXPECT_CALL(rtp_video_sender_, IsActive())
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200121 .WillRepeatedly(
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200122 ::testing::Invoke([&]() { return rtp_video_sender_active_; }));
Sebastian Jansson652dc912018-04-19 17:09:15 +0200123 }
124 ~VideoSendStreamImplTest() {}
125
126 std::unique_ptr<VideoSendStreamImpl> CreateVideoSendStreamImpl(
127 int initial_encoder_max_bitrate,
128 double initial_encoder_bitrate_priority,
129 VideoEncoderConfig::ContentType content_type) {
130 EXPECT_CALL(bitrate_allocator_, GetStartBitrate(_))
131 .WillOnce(Return(123000));
132 std::map<uint32_t, RtpState> suspended_ssrcs;
133 std::map<uint32_t, RtpPayloadState> suspended_payload_states;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200134 return std::make_unique<VideoSendStreamImpl>(
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100135 &clock_, &stats_proxy_, &test_queue_, &call_stats_,
136 &transport_controller_, &bitrate_allocator_, &send_delay_stats_,
137 &video_stream_encoder_, &event_log_, &config_,
138 initial_encoder_max_bitrate, initial_encoder_bitrate_priority,
139 suspended_ssrcs, suspended_payload_states, content_type,
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -0800140 std::make_unique<FecControllerDefault>(&clock_));
Sebastian Jansson652dc912018-04-19 17:09:15 +0200141 }
142
143 protected:
144 NiceMock<MockTransport> transport_;
145 NiceMock<MockRtpTransportControllerSend> transport_controller_;
146 NiceMock<MockBitrateAllocator> bitrate_allocator_;
147 NiceMock<MockVideoStreamEncoder> video_stream_encoder_;
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200148 NiceMock<MockRtpVideoSender> rtp_video_sender_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200149
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200150 bool rtp_video_sender_active_ = false;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200151 SimulatedClock clock_;
Danil Chapovalov83bbe912019-08-07 12:24:53 +0200152 RtcEventLogNull event_log_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200153 VideoSendStream::Config config_;
154 SendDelayStats send_delay_stats_;
Danil Chapovalovd26a9162019-03-19 18:08:37 +0100155 TaskQueueForTest test_queue_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200156 std::unique_ptr<ProcessThread> process_thread_;
157 CallStats call_stats_;
158 SendStatisticsProxy stats_proxy_;
159 PacketRouter packet_router_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200160};
161
162TEST_F(VideoSendStreamImplTest, RegistersAsBitrateObserverOnStart) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200163 test_queue_.SendTask(
164 [this] {
165 const bool kSuspend = false;
166 config_.suspend_below_min_bitrate = kSuspend;
167 auto vss_impl = CreateVideoSendStreamImpl(
168 kDefaultInitialBitrateBps, kDefaultBitratePriority,
169 VideoEncoderConfig::ContentType::kRealtimeVideo);
170 EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
171 .WillOnce(Invoke([&](BitrateAllocatorObserver*,
172 MediaStreamAllocationConfig config) {
Sebastian Jansson652dc912018-04-19 17:09:15 +0200173 EXPECT_EQ(config.min_bitrate_bps, 0u);
174 EXPECT_EQ(config.max_bitrate_bps, kDefaultInitialBitrateBps);
175 EXPECT_EQ(config.pad_up_bitrate_bps, 0u);
176 EXPECT_EQ(config.enforce_min_bitrate, !kSuspend);
Sebastian Jansson652dc912018-04-19 17:09:15 +0200177 EXPECT_EQ(config.bitrate_priority, kDefaultBitratePriority);
Sebastian Jansson652dc912018-04-19 17:09:15 +0200178 }));
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200179 vss_impl->Start();
180 EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get()))
181 .Times(1);
182 vss_impl->Stop();
183 },
184 RTC_FROM_HERE);
Sebastian Jansson652dc912018-04-19 17:09:15 +0200185}
186
Erik Språngb57ab382018-09-13 10:52:38 +0200187TEST_F(VideoSendStreamImplTest, UpdatesObserverOnConfigurationChange) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200188 test_queue_.SendTask(
189 [this] {
190 const bool kSuspend = false;
191 config_.suspend_below_min_bitrate = kSuspend;
192 config_.rtp.extensions.emplace_back(
193 RtpExtension::kTransportSequenceNumberUri, 1);
194 auto vss_impl = CreateVideoSendStreamImpl(
195 kDefaultInitialBitrateBps, kDefaultBitratePriority,
196 VideoEncoderConfig::ContentType::kRealtimeVideo);
197 vss_impl->Start();
Erik Språngb57ab382018-09-13 10:52:38 +0200198
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200199 // QVGA + VGA configuration matching defaults in
200 // media/engine/simulcast.cc.
201 VideoStream qvga_stream;
202 qvga_stream.width = 320;
203 qvga_stream.height = 180;
204 qvga_stream.max_framerate = 30;
205 qvga_stream.min_bitrate_bps = 30000;
206 qvga_stream.target_bitrate_bps = 150000;
207 qvga_stream.max_bitrate_bps = 200000;
208 qvga_stream.max_qp = 56;
209 qvga_stream.bitrate_priority = 1;
Erik Språngb57ab382018-09-13 10:52:38 +0200210
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200211 VideoStream vga_stream;
212 vga_stream.width = 640;
213 vga_stream.height = 360;
214 vga_stream.max_framerate = 30;
215 vga_stream.min_bitrate_bps = 150000;
216 vga_stream.target_bitrate_bps = 500000;
217 vga_stream.max_bitrate_bps = 700000;
218 vga_stream.max_qp = 56;
219 vga_stream.bitrate_priority = 1;
Erik Språngb57ab382018-09-13 10:52:38 +0200220
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200221 int min_transmit_bitrate_bps = 30000;
Erik Språngb57ab382018-09-13 10:52:38 +0200222
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200223 config_.rtp.ssrcs.emplace_back(1);
224 config_.rtp.ssrcs.emplace_back(2);
Erik Språngb57ab382018-09-13 10:52:38 +0200225
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200226 EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
227 .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*,
228 MediaStreamAllocationConfig config) {
Erik Språngb57ab382018-09-13 10:52:38 +0200229 EXPECT_EQ(config.min_bitrate_bps,
230 static_cast<uint32_t>(min_transmit_bitrate_bps));
231 EXPECT_EQ(config.max_bitrate_bps,
232 static_cast<uint32_t>(qvga_stream.max_bitrate_bps +
233 vga_stream.max_bitrate_bps));
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200234 if (config.pad_up_bitrate_bps != 0) {
235 EXPECT_EQ(config.pad_up_bitrate_bps,
236 static_cast<uint32_t>(qvga_stream.target_bitrate_bps +
237 vga_stream.min_bitrate_bps));
238 }
Erik Språngb57ab382018-09-13 10:52:38 +0200239 EXPECT_EQ(config.enforce_min_bitrate, !kSuspend);
Erik Språngb57ab382018-09-13 10:52:38 +0200240 }));
241
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200242 static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
243 ->OnEncoderConfigurationChanged(
244 std::vector<VideoStream>{qvga_stream, vga_stream},
245 VideoEncoderConfig::ContentType::kRealtimeVideo,
246 min_transmit_bitrate_bps);
247 vss_impl->Stop();
248 },
249 RTC_FROM_HERE);
Erik Språngb57ab382018-09-13 10:52:38 +0200250}
251
252TEST_F(VideoSendStreamImplTest, UpdatesObserverOnConfigurationChangeWithAlr) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200253 test_queue_.SendTask(
254 [this] {
255 const bool kSuspend = false;
256 config_.suspend_below_min_bitrate = kSuspend;
257 config_.rtp.extensions.emplace_back(
258 RtpExtension::kTransportSequenceNumberUri, 1);
259 config_.periodic_alr_bandwidth_probing = true;
260 auto vss_impl = CreateVideoSendStreamImpl(
261 kDefaultInitialBitrateBps, kDefaultBitratePriority,
262 VideoEncoderConfig::ContentType::kScreen);
263 vss_impl->Start();
Erik Språngb57ab382018-09-13 10:52:38 +0200264
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200265 // Simulcast screenshare.
266 VideoStream low_stream;
267 low_stream.width = 1920;
268 low_stream.height = 1080;
269 low_stream.max_framerate = 5;
270 low_stream.min_bitrate_bps = 30000;
271 low_stream.target_bitrate_bps = 200000;
272 low_stream.max_bitrate_bps = 1000000;
273 low_stream.num_temporal_layers = 2;
274 low_stream.max_qp = 56;
275 low_stream.bitrate_priority = 1;
Erik Språngb57ab382018-09-13 10:52:38 +0200276
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200277 VideoStream high_stream;
278 high_stream.width = 1920;
279 high_stream.height = 1080;
280 high_stream.max_framerate = 30;
281 high_stream.min_bitrate_bps = 60000;
282 high_stream.target_bitrate_bps = 1250000;
283 high_stream.max_bitrate_bps = 1250000;
284 high_stream.num_temporal_layers = 2;
285 high_stream.max_qp = 56;
286 high_stream.bitrate_priority = 1;
Erik Språngb57ab382018-09-13 10:52:38 +0200287
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200288 // With ALR probing, this will be the padding target instead of
289 // low_stream.target_bitrate_bps + high_stream.min_bitrate_bps.
290 int min_transmit_bitrate_bps = 400000;
Erik Språngb57ab382018-09-13 10:52:38 +0200291
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200292 config_.rtp.ssrcs.emplace_back(1);
293 config_.rtp.ssrcs.emplace_back(2);
Erik Språngb57ab382018-09-13 10:52:38 +0200294
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200295 EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
296 .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*,
297 MediaStreamAllocationConfig config) {
Erik Språngb57ab382018-09-13 10:52:38 +0200298 EXPECT_EQ(config.min_bitrate_bps,
299 static_cast<uint32_t>(low_stream.min_bitrate_bps));
300 EXPECT_EQ(config.max_bitrate_bps,
301 static_cast<uint32_t>(low_stream.max_bitrate_bps +
302 high_stream.max_bitrate_bps));
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200303 if (config.pad_up_bitrate_bps != 0) {
304 EXPECT_EQ(config.pad_up_bitrate_bps,
305 static_cast<uint32_t>(min_transmit_bitrate_bps));
306 }
Erik Språngb57ab382018-09-13 10:52:38 +0200307 EXPECT_EQ(config.enforce_min_bitrate, !kSuspend);
Erik Språngb57ab382018-09-13 10:52:38 +0200308 }));
309
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200310 static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
311 ->OnEncoderConfigurationChanged(
312 std::vector<VideoStream>{low_stream, high_stream},
313 VideoEncoderConfig::ContentType::kScreen,
314 min_transmit_bitrate_bps);
315 vss_impl->Stop();
316 },
317 RTC_FROM_HERE);
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100318}
319
320TEST_F(VideoSendStreamImplTest,
321 UpdatesObserverOnConfigurationChangeWithSimulcastVideoHysteresis) {
322 test::ScopedFieldTrials hysteresis_experiment(
323 "WebRTC-VideoRateControl/video_hysteresis:1.25/");
324
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200325 test_queue_.SendTask(
326 [this] {
327 auto vss_impl = CreateVideoSendStreamImpl(
328 kDefaultInitialBitrateBps, kDefaultBitratePriority,
329 VideoEncoderConfig::ContentType::kRealtimeVideo);
330 vss_impl->Start();
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100331
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200332 // 2-layer video simulcast.
333 VideoStream low_stream;
334 low_stream.width = 320;
335 low_stream.height = 240;
336 low_stream.max_framerate = 30;
337 low_stream.min_bitrate_bps = 30000;
338 low_stream.target_bitrate_bps = 100000;
339 low_stream.max_bitrate_bps = 200000;
340 low_stream.max_qp = 56;
341 low_stream.bitrate_priority = 1;
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100342
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200343 VideoStream high_stream;
344 high_stream.width = 640;
345 high_stream.height = 480;
346 high_stream.max_framerate = 30;
347 high_stream.min_bitrate_bps = 150000;
348 high_stream.target_bitrate_bps = 500000;
349 high_stream.max_bitrate_bps = 750000;
350 high_stream.max_qp = 56;
351 high_stream.bitrate_priority = 1;
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100352
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200353 config_.rtp.ssrcs.emplace_back(1);
354 config_.rtp.ssrcs.emplace_back(2);
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100355
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200356 EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
357 .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*,
358 MediaStreamAllocationConfig config) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200359 EXPECT_EQ(config.min_bitrate_bps,
360 static_cast<uint32_t>(low_stream.min_bitrate_bps));
361 EXPECT_EQ(config.max_bitrate_bps,
362 static_cast<uint32_t>(low_stream.max_bitrate_bps +
363 high_stream.max_bitrate_bps));
364 if (config.pad_up_bitrate_bps != 0) {
365 EXPECT_EQ(
366 config.pad_up_bitrate_bps,
367 static_cast<uint32_t>(low_stream.target_bitrate_bps +
368 1.25 * high_stream.min_bitrate_bps));
369 }
370 }));
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100371
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200372 static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
373 ->OnEncoderConfigurationChanged(
374 std::vector<VideoStream>{low_stream, high_stream},
375 VideoEncoderConfig::ContentType::kRealtimeVideo,
376 /*min_transmit_bitrate_bps=*/0);
377 vss_impl->Stop();
378 },
379 RTC_FROM_HERE);
Erik Språngb57ab382018-09-13 10:52:38 +0200380}
381
Sebastian Jansson652dc912018-04-19 17:09:15 +0200382TEST_F(VideoSendStreamImplTest, SetsScreensharePacingFactorWithFeedback) {
383 test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString());
384
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200385 test_queue_.SendTask(
386 [this] {
387 constexpr int kId = 1;
388 config_.rtp.extensions.emplace_back(
389 RtpExtension::kTransportSequenceNumberUri, kId);
390 EXPECT_CALL(transport_controller_,
391 SetPacingFactor(kAlrProbingExperimentPaceMultiplier))
392 .Times(1);
393 auto vss_impl = CreateVideoSendStreamImpl(
394 kDefaultInitialBitrateBps, kDefaultBitratePriority,
395 VideoEncoderConfig::ContentType::kScreen);
396 vss_impl->Start();
397 vss_impl->Stop();
398 },
399 RTC_FROM_HERE);
Sebastian Jansson652dc912018-04-19 17:09:15 +0200400}
401
402TEST_F(VideoSendStreamImplTest, DoesNotSetPacingFactorWithoutFeedback) {
403 test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString());
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200404 test_queue_.SendTask(
405 [this] {
406 EXPECT_CALL(transport_controller_, SetPacingFactor(_)).Times(0);
407 auto vss_impl = CreateVideoSendStreamImpl(
408 kDefaultInitialBitrateBps, kDefaultBitratePriority,
409 VideoEncoderConfig::ContentType::kScreen);
410 vss_impl->Start();
411 vss_impl->Stop();
412 },
413 RTC_FROM_HERE);
Sebastian Jansson652dc912018-04-19 17:09:15 +0200414}
Erik Språng4e193e42018-09-14 19:01:58 +0200415
416TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationWhenEnabled) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200417 test_queue_.SendTask(
418 [this] {
419 EXPECT_CALL(transport_controller_, SetPacingFactor(_)).Times(0);
420 auto vss_impl = CreateVideoSendStreamImpl(
421 kDefaultInitialBitrateBps, kDefaultBitratePriority,
422 VideoEncoderConfig::ContentType::kScreen);
423 vss_impl->Start();
424 VideoBitrateAllocationObserver* const observer =
425 static_cast<VideoBitrateAllocationObserver*>(vss_impl.get());
Erik Språng4e193e42018-09-14 19:01:58 +0200426
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200427 // Populate a test instance of video bitrate allocation.
428 VideoBitrateAllocation alloc;
429 alloc.SetBitrate(0, 0, 10000);
430 alloc.SetBitrate(0, 1, 20000);
431 alloc.SetBitrate(1, 0, 30000);
432 alloc.SetBitrate(1, 1, 40000);
Erik Språng4e193e42018-09-14 19:01:58 +0200433
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200434 // Encoder starts out paused, don't forward allocation.
435 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
436 .Times(0);
437 observer->OnBitrateAllocationUpdated(alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200438
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200439 // Unpause encoder, allocation should be passed through.
440 const uint32_t kBitrateBps = 100000;
441 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
442 .Times(1)
443 .WillOnce(Return(kBitrateBps));
444 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
445 ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
446 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
447 .Times(1);
448 observer->OnBitrateAllocationUpdated(alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200449
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200450 // Pause encoder again, and block allocations.
451 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
452 .Times(1)
453 .WillOnce(Return(0));
454 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
455 ->OnBitrateUpdated(CreateAllocation(0));
456 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
457 .Times(0);
458 observer->OnBitrateAllocationUpdated(alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200459
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200460 vss_impl->Stop();
461 },
462 RTC_FROM_HERE);
Erik Språng4e193e42018-09-14 19:01:58 +0200463}
464
465TEST_F(VideoSendStreamImplTest, ThrottlesVideoBitrateAllocationWhenTooSimilar) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200466 test_queue_.SendTask(
467 [this] {
468 auto vss_impl = CreateVideoSendStreamImpl(
469 kDefaultInitialBitrateBps, kDefaultBitratePriority,
470 VideoEncoderConfig::ContentType::kScreen);
471 vss_impl->Start();
472 // Unpause encoder, to allows allocations to be passed through.
473 const uint32_t kBitrateBps = 100000;
474 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
475 .Times(1)
476 .WillOnce(Return(kBitrateBps));
477 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
478 ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
479 VideoBitrateAllocationObserver* const observer =
480 static_cast<VideoBitrateAllocationObserver*>(vss_impl.get());
Erik Språng4e193e42018-09-14 19:01:58 +0200481
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200482 // Populate a test instance of video bitrate allocation.
483 VideoBitrateAllocation alloc;
484 alloc.SetBitrate(0, 0, 10000);
485 alloc.SetBitrate(0, 1, 20000);
486 alloc.SetBitrate(1, 0, 30000);
487 alloc.SetBitrate(1, 1, 40000);
Erik Språng4e193e42018-09-14 19:01:58 +0200488
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200489 // Initial value.
490 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
491 .Times(1);
492 observer->OnBitrateAllocationUpdated(alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200493
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200494 VideoBitrateAllocation updated_alloc = alloc;
495 // Needs 10% increase in bitrate to trigger immediate forward.
496 const uint32_t base_layer_min_update_bitrate_bps =
497 alloc.GetBitrate(0, 0) + alloc.get_sum_bps() / 10;
Erik Språng4e193e42018-09-14 19:01:58 +0200498
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200499 // Too small increase, don't forward.
500 updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps - 1);
501 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(_)).Times(0);
502 observer->OnBitrateAllocationUpdated(updated_alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200503
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200504 // Large enough increase, do forward.
505 updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps);
506 EXPECT_CALL(rtp_video_sender_,
507 OnBitrateAllocationUpdated(updated_alloc))
508 .Times(1);
509 observer->OnBitrateAllocationUpdated(updated_alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200510
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200511 // This is now a decrease compared to last forward allocation, forward
512 // immediately.
513 updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps - 1);
514 EXPECT_CALL(rtp_video_sender_,
515 OnBitrateAllocationUpdated(updated_alloc))
516 .Times(1);
517 observer->OnBitrateAllocationUpdated(updated_alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200518
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200519 vss_impl->Stop();
520 },
521 RTC_FROM_HERE);
Erik Språng4e193e42018-09-14 19:01:58 +0200522}
523
524TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationOnLayerChange) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200525 test_queue_.SendTask(
526 [this] {
527 auto vss_impl = CreateVideoSendStreamImpl(
528 kDefaultInitialBitrateBps, kDefaultBitratePriority,
529 VideoEncoderConfig::ContentType::kScreen);
530 vss_impl->Start();
531 // Unpause encoder, to allows allocations to be passed through.
532 const uint32_t kBitrateBps = 100000;
533 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
534 .Times(1)
535 .WillOnce(Return(kBitrateBps));
536 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
537 ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
538 VideoBitrateAllocationObserver* const observer =
539 static_cast<VideoBitrateAllocationObserver*>(vss_impl.get());
Erik Språng4e193e42018-09-14 19:01:58 +0200540
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200541 // Populate a test instance of video bitrate allocation.
542 VideoBitrateAllocation alloc;
543 alloc.SetBitrate(0, 0, 10000);
544 alloc.SetBitrate(0, 1, 20000);
545 alloc.SetBitrate(1, 0, 30000);
546 alloc.SetBitrate(1, 1, 40000);
Erik Språng4e193e42018-09-14 19:01:58 +0200547
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200548 // Initial value.
549 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
550 .Times(1);
551 observer->OnBitrateAllocationUpdated(alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200552
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200553 // Move some bitrate from one layer to a new one, but keep sum the same.
554 // Since layout has changed, immediately trigger forward.
555 VideoBitrateAllocation updated_alloc = alloc;
556 updated_alloc.SetBitrate(2, 0, 10000);
557 updated_alloc.SetBitrate(1, 1, alloc.GetBitrate(1, 1) - 10000);
558 EXPECT_EQ(alloc.get_sum_bps(), updated_alloc.get_sum_bps());
559 EXPECT_CALL(rtp_video_sender_,
560 OnBitrateAllocationUpdated(updated_alloc))
561 .Times(1);
562 observer->OnBitrateAllocationUpdated(updated_alloc);
Erik Språng4e193e42018-09-14 19:01:58 +0200563
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200564 vss_impl->Stop();
565 },
566 RTC_FROM_HERE);
Erik Språng4e193e42018-09-14 19:01:58 +0200567}
568
569TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationAfterTimeout) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200570 test_queue_.SendTask(
571 [this] {
572 auto vss_impl = CreateVideoSendStreamImpl(
573 kDefaultInitialBitrateBps, kDefaultBitratePriority,
574 VideoEncoderConfig::ContentType::kScreen);
575 vss_impl->Start();
576 const uint32_t kBitrateBps = 100000;
577 // Unpause encoder, to allows allocations to be passed through.
578 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
579 .Times(1)
580 .WillRepeatedly(Return(kBitrateBps));
581 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
582 ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
583 VideoBitrateAllocationObserver* const observer =
584 static_cast<VideoBitrateAllocationObserver*>(vss_impl.get());
Erik Språng4e193e42018-09-14 19:01:58 +0200585
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200586 // Populate a test instance of video bitrate allocation.
587 VideoBitrateAllocation alloc;
588 alloc.SetBitrate(0, 0, 10000);
589 alloc.SetBitrate(0, 1, 20000);
590 alloc.SetBitrate(1, 0, 30000);
591 alloc.SetBitrate(1, 1, 40000);
Erik Språng4e193e42018-09-14 19:01:58 +0200592
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200593 EncodedImage encoded_image;
594 CodecSpecificInfo codec_specific;
595 EXPECT_CALL(rtp_video_sender_, OnEncodedImage(_, _, _))
596 .WillRepeatedly(Return(EncodedImageCallback::Result(
597 EncodedImageCallback::Result::OK)));
Erik Språng4e193e42018-09-14 19:01:58 +0200598
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200599 // Max time we will throttle similar video bitrate allocations.
600 static constexpr int64_t kMaxVbaThrottleTimeMs = 500;
Erik Språng4e193e42018-09-14 19:01:58 +0200601
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200602 {
603 // Initial value.
604 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
605 .Times(1);
606 observer->OnBitrateAllocationUpdated(alloc);
607 }
Erik Språng4e193e42018-09-14 19:01:58 +0200608
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200609 {
610 // Sending same allocation again, this one should be throttled.
611 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
612 .Times(0);
613 observer->OnBitrateAllocationUpdated(alloc);
614 }
Erik Språng4e193e42018-09-14 19:01:58 +0200615
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200616 clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000);
Erik Språng4e193e42018-09-14 19:01:58 +0200617
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200618 {
619 // Sending similar allocation again after timeout, should forward.
620 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
621 .Times(1);
622 observer->OnBitrateAllocationUpdated(alloc);
623 }
Erik Språng4e193e42018-09-14 19:01:58 +0200624
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200625 {
626 // Sending similar allocation again without timeout, throttle.
627 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
628 .Times(0);
629 observer->OnBitrateAllocationUpdated(alloc);
630 }
Erik Språng4e193e42018-09-14 19:01:58 +0200631
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200632 {
633 // Send encoded image, should be a noop.
634 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
635 .Times(0);
636 static_cast<EncodedImageCallback*>(vss_impl.get())
637 ->OnEncodedImage(encoded_image, &codec_specific, nullptr);
638 }
Erik Språng4e193e42018-09-14 19:01:58 +0200639
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200640 {
641 // Advance time and send encoded image, this should wake up and send
642 // cached bitrate allocation.
643 clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000);
644 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
645 .Times(1);
646 static_cast<EncodedImageCallback*>(vss_impl.get())
647 ->OnEncodedImage(encoded_image, &codec_specific, nullptr);
648 }
Erik Språng4e193e42018-09-14 19:01:58 +0200649
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200650 {
651 // Advance time and send encoded image, there should be no cached
652 // allocation to send.
653 clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000);
654 EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc))
655 .Times(0);
656 static_cast<EncodedImageCallback*>(vss_impl.get())
657 ->OnEncodedImage(encoded_image, &codec_specific, nullptr);
658 }
Erik Språng4e193e42018-09-14 19:01:58 +0200659
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200660 vss_impl->Stop();
661 },
662 RTC_FROM_HERE);
Erik Språng4e193e42018-09-14 19:01:58 +0200663}
664
Erik Språng610c7632019-03-06 15:37:33 +0100665TEST_F(VideoSendStreamImplTest, CallsVideoStreamEncoderOnBitrateUpdate) {
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200666 test_queue_.SendTask(
667 [this] {
668 const bool kSuspend = false;
669 config_.suspend_below_min_bitrate = kSuspend;
670 config_.rtp.extensions.emplace_back(
671 RtpExtension::kTransportSequenceNumberUri, 1);
672 auto vss_impl = CreateVideoSendStreamImpl(
673 kDefaultInitialBitrateBps, kDefaultBitratePriority,
674 VideoEncoderConfig::ContentType::kRealtimeVideo);
675 vss_impl->Start();
Erik Språng610c7632019-03-06 15:37:33 +0100676
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200677 VideoStream qvga_stream;
678 qvga_stream.width = 320;
679 qvga_stream.height = 180;
680 qvga_stream.max_framerate = 30;
681 qvga_stream.min_bitrate_bps = 30000;
682 qvga_stream.target_bitrate_bps = 150000;
683 qvga_stream.max_bitrate_bps = 200000;
684 qvga_stream.max_qp = 56;
685 qvga_stream.bitrate_priority = 1;
Erik Språng610c7632019-03-06 15:37:33 +0100686
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200687 int min_transmit_bitrate_bps = 30000;
Erik Språng610c7632019-03-06 15:37:33 +0100688
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200689 config_.rtp.ssrcs.emplace_back(1);
Erik Språng610c7632019-03-06 15:37:33 +0100690
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200691 static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
692 ->OnEncoderConfigurationChanged(
693 std::vector<VideoStream>{qvga_stream},
694 VideoEncoderConfig::ContentType::kRealtimeVideo,
695 min_transmit_bitrate_bps);
Erik Språng610c7632019-03-06 15:37:33 +0100696
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200697 const DataRate network_constrained_rate =
698 DataRate::bps(qvga_stream.target_bitrate_bps);
699 BitrateAllocationUpdate update;
700 update.target_bitrate = network_constrained_rate;
701 update.stable_target_bitrate = network_constrained_rate;
702 update.round_trip_time = TimeDelta::ms(1);
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200703 EXPECT_CALL(rtp_video_sender_, OnBitrateUpdated(update, _));
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200704 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
705 .WillOnce(Return(network_constrained_rate.bps()));
706 EXPECT_CALL(
707 video_stream_encoder_,
708 OnBitrateUpdated(network_constrained_rate, network_constrained_rate,
709 network_constrained_rate, 0, _));
710 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
711 ->OnBitrateUpdated(update);
Erik Språng610c7632019-03-06 15:37:33 +0100712
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200713 // Test allocation where the link allocation is larger than the target,
714 // meaning we have some headroom on the link.
715 const DataRate qvga_max_bitrate =
716 DataRate::bps(qvga_stream.max_bitrate_bps);
717 const DataRate headroom = DataRate::bps(50000);
718 const DataRate rate_with_headroom = qvga_max_bitrate + headroom;
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200719 update.target_bitrate = rate_with_headroom;
720 update.stable_target_bitrate = rate_with_headroom;
721 EXPECT_CALL(rtp_video_sender_, OnBitrateUpdated(update, _));
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200722 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
723 .WillOnce(Return(rate_with_headroom.bps()));
724 EXPECT_CALL(video_stream_encoder_,
725 OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate,
726 rate_with_headroom, 0, _));
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200727 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
728 ->OnBitrateUpdated(update);
Erik Språng610c7632019-03-06 15:37:33 +0100729
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200730 // Add protection bitrate to the mix, this should be subtracted from the
731 // headroom.
732 const uint32_t protection_bitrate_bps = 10000;
733 EXPECT_CALL(rtp_video_sender_, GetProtectionBitrateBps())
734 .WillOnce(Return(protection_bitrate_bps));
Erik Språng26111642019-03-26 11:09:04 +0100735
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200736 EXPECT_CALL(rtp_video_sender_, OnBitrateUpdated(update, _));
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200737 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
738 .WillOnce(Return(rate_with_headroom.bps()));
739 const DataRate headroom_minus_protection =
740 rate_with_headroom - DataRate::bps(protection_bitrate_bps);
741 EXPECT_CALL(video_stream_encoder_,
742 OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate,
743 headroom_minus_protection, 0, _));
744 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
745 ->OnBitrateUpdated(update);
Erik Språng26111642019-03-26 11:09:04 +0100746
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200747 // Protection bitrate exceeds head room, link allocation should be
748 // capped to target bitrate.
749 EXPECT_CALL(rtp_video_sender_, GetProtectionBitrateBps())
750 .WillOnce(Return(headroom.bps() + 1000));
Sebastian Jansson82ed2e82019-10-15 15:58:37 +0200751 EXPECT_CALL(rtp_video_sender_, OnBitrateUpdated(update, _));
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200752 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
753 .WillOnce(Return(rate_with_headroom.bps()));
754 EXPECT_CALL(video_stream_encoder_,
755 OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate,
756 qvga_max_bitrate, 0, _));
757 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
758 ->OnBitrateUpdated(update);
Erik Språng26111642019-03-26 11:09:04 +0100759
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200760 // Set rates to zero on stop.
761 EXPECT_CALL(video_stream_encoder_,
762 OnBitrateUpdated(DataRate::Zero(), DataRate::Zero(),
763 DataRate::Zero(), 0, 0));
764 vss_impl->Stop();
765 },
766 RTC_FROM_HERE);
Erik Språng610c7632019-03-06 15:37:33 +0100767}
768
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200769TEST_F(VideoSendStreamImplTest, DisablesPaddingOnPausedEncoder) {
770 int padding_bitrate = 0;
771 std::unique_ptr<VideoSendStreamImpl> vss_impl;
772
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200773 test_queue_.SendTask(
774 [&] {
775 vss_impl = CreateVideoSendStreamImpl(
776 kDefaultInitialBitrateBps, kDefaultBitratePriority,
777 VideoEncoderConfig::ContentType::kRealtimeVideo);
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200778
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200779 // Capture padding bitrate for testing.
780 EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _))
781 .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*,
782 MediaStreamAllocationConfig config) {
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200783 padding_bitrate = config.pad_up_bitrate_bps;
784 }));
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200785 // If observer is removed, no padding will be sent.
786 EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get()))
787 .WillRepeatedly(Invoke(
788 [&](BitrateAllocatorObserver*) { padding_bitrate = 0; }));
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200789
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200790 EXPECT_CALL(rtp_video_sender_, OnEncodedImage(_, _, _))
791 .WillRepeatedly(Return(EncodedImageCallback::Result(
792 EncodedImageCallback::Result::OK)));
793 const bool kSuspend = false;
794 config_.suspend_below_min_bitrate = kSuspend;
795 config_.rtp.extensions.emplace_back(
796 RtpExtension::kTransportSequenceNumberUri, 1);
797 VideoStream qvga_stream;
798 qvga_stream.width = 320;
799 qvga_stream.height = 180;
800 qvga_stream.max_framerate = 30;
801 qvga_stream.min_bitrate_bps = 30000;
802 qvga_stream.target_bitrate_bps = 150000;
803 qvga_stream.max_bitrate_bps = 200000;
804 qvga_stream.max_qp = 56;
805 qvga_stream.bitrate_priority = 1;
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200806
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200807 int min_transmit_bitrate_bps = 30000;
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200808
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200809 config_.rtp.ssrcs.emplace_back(1);
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200810
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200811 vss_impl->Start();
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200812
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200813 // Starts without padding.
814 EXPECT_EQ(0, padding_bitrate);
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200815
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200816 // Reconfigure e.g. due to a fake frame.
817 static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get())
818 ->OnEncoderConfigurationChanged(
819 std::vector<VideoStream>{qvga_stream},
820 VideoEncoderConfig::ContentType::kRealtimeVideo,
821 min_transmit_bitrate_bps);
822 // Still no padding because no actual frames were passed, only
823 // reconfiguration happened.
824 EXPECT_EQ(0, padding_bitrate);
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200825
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200826 // Unpause encoder.
827 const uint32_t kBitrateBps = 100000;
828 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
829 .Times(1)
830 .WillOnce(Return(kBitrateBps));
831 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
832 ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200833
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +0200834 // A frame is encoded.
835 EncodedImage encoded_image;
836 CodecSpecificInfo codec_specific;
837 static_cast<EncodedImageCallback*>(vss_impl.get())
838 ->OnEncodedImage(encoded_image, &codec_specific, nullptr);
839 // Only after actual frame is encoded are we enabling the padding.
840 EXPECT_GT(padding_bitrate, 0);
841 },
842 RTC_FROM_HERE);
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200843
844 rtc::Event done;
845 test_queue_.PostDelayedTask(
846 [&] {
847 // No padding supposed to be sent for paused observer
848 EXPECT_EQ(0, padding_bitrate);
849 testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_);
850 vss_impl->Stop();
851 vss_impl.reset();
852 done.Set();
853 },
854 5000);
855
856 // Pause the test suite so that the last delayed task executes.
857 ASSERT_TRUE(done.Wait(10000));
858}
859
Jakob Ivarsson159b4172019-10-30 14:02:14 +0100860TEST_F(VideoSendStreamImplTest, KeepAliveOnDroppedFrame) {
861 std::unique_ptr<VideoSendStreamImpl> vss_impl;
862 test_queue_.SendTask(
863 [&] {
864 vss_impl = CreateVideoSendStreamImpl(
865 kDefaultInitialBitrateBps, kDefaultBitratePriority,
866 VideoEncoderConfig::ContentType::kRealtimeVideo);
867 vss_impl->Start();
868 const uint32_t kBitrateBps = 100000;
869 EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps())
870 .Times(1)
871 .WillOnce(Return(kBitrateBps));
872 static_cast<BitrateAllocatorObserver*>(vss_impl.get())
873 ->OnBitrateUpdated(CreateAllocation(kBitrateBps));
874
875 // Keep the stream from deallocating by dropping a frame.
876 static_cast<EncodedImageCallback*>(vss_impl.get())
877 ->OnDroppedFrame(
878 EncodedImageCallback::DropReason::kDroppedByEncoder);
879 EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get()))
880 .Times(0);
881 },
882 RTC_FROM_HERE);
883
884 rtc::Event done;
885 test_queue_.PostDelayedTask(
886 [&] {
887 testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_);
888 vss_impl->Stop();
889 vss_impl.reset();
890 done.Set();
891 },
892 2000);
893 ASSERT_TRUE(done.Wait(5000));
894}
895
Sebastian Jansson652dc912018-04-19 17:09:15 +0200896} // namespace internal
897} // namespace webrtc