Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "video/video_send_stream_impl.h" |
| 12 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 13 | #include <memory> |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 14 | #include <string> |
| 15 | |
Elad Alon | 8b60e8b | 2019-04-08 14:14:05 +0200 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 17 | #include "api/rtc_event_log/rtc_event_log.h" |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 18 | #include "call/rtp_video_sender.h" |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 19 | #include "call/test/mock_bitrate_allocator.h" |
| 20 | #include "call/test/mock_rtp_transport_controller_send.h" |
Elad Alon | 8b60e8b | 2019-04-08 14:14:05 +0200 | [diff] [blame] | 21 | #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h" |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 22 | #include "modules/utility/include/process_thread.h" |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 23 | #include "modules/video_coding/fec_controller_default.h" |
| 24 | #include "rtc_base/experiments/alr_experiment.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/fake_clock.h" |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 26 | #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 Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
| 34 | namespace internal { |
| 35 | namespace { |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 36 | using ::testing::_; |
| 37 | using ::testing::Invoke; |
| 38 | using ::testing::NiceMock; |
| 39 | using ::testing::Return; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 40 | |
| 41 | constexpr int64_t kDefaultInitialBitrateBps = 333000; |
| 42 | const double kDefaultBitratePriority = 0.5; |
| 43 | |
| 44 | const float kAlrProbingExperimentPaceMultiplier = 1.0f; |
| 45 | std::string GetAlrProbingExperimentString() { |
| 46 | return std::string( |
| 47 | AlrExperimentSettings::kScreenshareProbingBweExperimentName) + |
| 48 | "/1.0,2875,80,40,-60,3/"; |
| 49 | } |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 50 | class MockRtpVideoSender : public RtpVideoSenderInterface { |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 51 | public: |
| 52 | MOCK_METHOD1(RegisterProcessThread, void(ProcessThread*)); |
| 53 | MOCK_METHOD0(DeRegisterProcessThread, void()); |
| 54 | MOCK_METHOD1(SetActive, void(bool)); |
| 55 | MOCK_METHOD1(SetActiveModules, void(const std::vector<bool>)); |
| 56 | MOCK_METHOD0(IsActive, bool()); |
| 57 | MOCK_METHOD1(OnNetworkAvailability, void(bool)); |
| 58 | MOCK_CONST_METHOD0(GetRtpStates, std::map<uint32_t, RtpState>()); |
| 59 | MOCK_CONST_METHOD0(GetRtpPayloadStates, |
| 60 | std::map<uint32_t, RtpPayloadState>()); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 61 | MOCK_METHOD2(DeliverRtcp, void(const uint8_t*, size_t)); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 62 | MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&)); |
| 63 | MOCK_METHOD3(OnEncodedImage, |
| 64 | EncodedImageCallback::Result(const EncodedImage&, |
| 65 | const CodecSpecificInfo*, |
| 66 | const RTPFragmentationHeader*)); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 67 | MOCK_METHOD1(OnTransportOverheadChanged, void(size_t)); |
| 68 | MOCK_METHOD1(OnOverheadChanged, void(size_t)); |
| 69 | MOCK_METHOD4(OnBitrateUpdated, void(uint32_t, uint8_t, int64_t, int)); |
| 70 | MOCK_CONST_METHOD0(GetPayloadBitrateBps, uint32_t()); |
| 71 | MOCK_CONST_METHOD0(GetProtectionBitrateBps, uint32_t()); |
| 72 | MOCK_METHOD3(SetEncodingData, void(size_t, size_t, size_t)); |
Elad Alon | 898395d | 2019-04-10 15:55:00 +0200 | [diff] [blame] | 73 | MOCK_CONST_METHOD2(GetSentRtpPacketInfos, |
| 74 | std::vector<RtpSequenceNumberMap::Info>( |
| 75 | uint32_t ssrc, |
| 76 | rtc::ArrayView<const uint16_t> sequence_numbers)); |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 77 | |
| 78 | MOCK_METHOD1(SetFecAllowed, void(bool fec_allowed)); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 79 | }; |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 80 | |
| 81 | BitrateAllocationUpdate CreateAllocation(int bitrate_bps) { |
| 82 | BitrateAllocationUpdate update; |
Sebastian Jansson | 13e5903 | 2018-11-21 19:13:07 +0100 | [diff] [blame] | 83 | update.target_bitrate = DataRate::bps(bitrate_bps); |
| 84 | update.packet_loss_ratio = 0; |
| 85 | update.round_trip_time = TimeDelta::Zero(); |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 86 | return update; |
| 87 | } |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 88 | } // namespace |
| 89 | |
| 90 | class VideoSendStreamImplTest : public ::testing::Test { |
| 91 | protected: |
| 92 | VideoSendStreamImplTest() |
| 93 | : clock_(1000 * 1000 * 1000), |
| 94 | config_(&transport_), |
| 95 | send_delay_stats_(&clock_), |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 96 | test_queue_("test_queue"), |
| 97 | process_thread_(ProcessThread::Create("test_thread")), |
| 98 | call_stats_(&clock_, process_thread_.get()), |
| 99 | stats_proxy_(&clock_, |
| 100 | config_, |
| 101 | VideoEncoderConfig::ContentType::kRealtimeVideo) { |
| 102 | config_.rtp.ssrcs.push_back(8080); |
| 103 | config_.rtp.payload_type = 1; |
| 104 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 105 | EXPECT_CALL(transport_controller_, packet_router()) |
| 106 | .WillRepeatedly(Return(&packet_router_)); |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 107 | EXPECT_CALL(transport_controller_, CreateRtpVideoSender) |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 108 | .WillRepeatedly(Return(&rtp_video_sender_)); |
| 109 | EXPECT_CALL(rtp_video_sender_, SetActive(_)) |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 110 | .WillRepeatedly(::testing::Invoke( |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 111 | [&](bool active) { rtp_video_sender_active_ = active; })); |
| 112 | EXPECT_CALL(rtp_video_sender_, IsActive()) |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 113 | .WillRepeatedly( |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 114 | ::testing::Invoke([&]() { return rtp_video_sender_active_; })); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 115 | } |
| 116 | ~VideoSendStreamImplTest() {} |
| 117 | |
| 118 | std::unique_ptr<VideoSendStreamImpl> CreateVideoSendStreamImpl( |
| 119 | int initial_encoder_max_bitrate, |
| 120 | double initial_encoder_bitrate_priority, |
| 121 | VideoEncoderConfig::ContentType content_type) { |
| 122 | EXPECT_CALL(bitrate_allocator_, GetStartBitrate(_)) |
| 123 | .WillOnce(Return(123000)); |
| 124 | std::map<uint32_t, RtpState> suspended_ssrcs; |
| 125 | std::map<uint32_t, RtpPayloadState> suspended_payload_states; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 126 | return std::make_unique<VideoSendStreamImpl>( |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 127 | &clock_, &stats_proxy_, &test_queue_, &call_stats_, |
| 128 | &transport_controller_, &bitrate_allocator_, &send_delay_stats_, |
| 129 | &video_stream_encoder_, &event_log_, &config_, |
| 130 | initial_encoder_max_bitrate, initial_encoder_bitrate_priority, |
| 131 | suspended_ssrcs, suspended_payload_states, content_type, |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 132 | std::make_unique<FecControllerDefault>(&clock_), |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 133 | /*media_transport=*/nullptr); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | protected: |
| 137 | NiceMock<MockTransport> transport_; |
| 138 | NiceMock<MockRtpTransportControllerSend> transport_controller_; |
| 139 | NiceMock<MockBitrateAllocator> bitrate_allocator_; |
| 140 | NiceMock<MockVideoStreamEncoder> video_stream_encoder_; |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 141 | NiceMock<MockRtpVideoSender> rtp_video_sender_; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 142 | |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 143 | bool rtp_video_sender_active_ = false; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 144 | SimulatedClock clock_; |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 145 | RtcEventLogNull event_log_; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 146 | VideoSendStream::Config config_; |
| 147 | SendDelayStats send_delay_stats_; |
Danil Chapovalov | d26a916 | 2019-03-19 18:08:37 +0100 | [diff] [blame] | 148 | TaskQueueForTest test_queue_; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 149 | std::unique_ptr<ProcessThread> process_thread_; |
| 150 | CallStats call_stats_; |
| 151 | SendStatisticsProxy stats_proxy_; |
| 152 | PacketRouter packet_router_; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | TEST_F(VideoSendStreamImplTest, RegistersAsBitrateObserverOnStart) { |
| 156 | test_queue_.SendTask([this] { |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 157 | const bool kSuspend = false; |
| 158 | config_.suspend_below_min_bitrate = kSuspend; |
| 159 | auto vss_impl = CreateVideoSendStreamImpl( |
| 160 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 161 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 162 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
| 163 | .WillOnce(Invoke( |
| 164 | [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) { |
| 165 | EXPECT_EQ(config.min_bitrate_bps, 0u); |
| 166 | EXPECT_EQ(config.max_bitrate_bps, kDefaultInitialBitrateBps); |
| 167 | EXPECT_EQ(config.pad_up_bitrate_bps, 0u); |
| 168 | EXPECT_EQ(config.enforce_min_bitrate, !kSuspend); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 169 | EXPECT_EQ(config.bitrate_priority, kDefaultBitratePriority); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 170 | })); |
| 171 | vss_impl->Start(); |
| 172 | EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get())).Times(1); |
| 173 | vss_impl->Stop(); |
| 174 | }); |
| 175 | } |
| 176 | |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 177 | TEST_F(VideoSendStreamImplTest, UpdatesObserverOnConfigurationChange) { |
| 178 | test_queue_.SendTask([this] { |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 179 | const bool kSuspend = false; |
| 180 | config_.suspend_below_min_bitrate = kSuspend; |
| 181 | config_.rtp.extensions.emplace_back( |
| 182 | RtpExtension::kTransportSequenceNumberUri, 1); |
| 183 | auto vss_impl = CreateVideoSendStreamImpl( |
| 184 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 185 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 186 | vss_impl->Start(); |
| 187 | |
| 188 | // QVGA + VGA configuration matching defaults in media/engine/simulcast.cc. |
| 189 | VideoStream qvga_stream; |
| 190 | qvga_stream.width = 320; |
| 191 | qvga_stream.height = 180; |
| 192 | qvga_stream.max_framerate = 30; |
| 193 | qvga_stream.min_bitrate_bps = 30000; |
| 194 | qvga_stream.target_bitrate_bps = 150000; |
| 195 | qvga_stream.max_bitrate_bps = 200000; |
| 196 | qvga_stream.max_qp = 56; |
| 197 | qvga_stream.bitrate_priority = 1; |
| 198 | |
| 199 | VideoStream vga_stream; |
| 200 | vga_stream.width = 640; |
| 201 | vga_stream.height = 360; |
| 202 | vga_stream.max_framerate = 30; |
| 203 | vga_stream.min_bitrate_bps = 150000; |
| 204 | vga_stream.target_bitrate_bps = 500000; |
| 205 | vga_stream.max_bitrate_bps = 700000; |
| 206 | vga_stream.max_qp = 56; |
| 207 | vga_stream.bitrate_priority = 1; |
| 208 | |
| 209 | int min_transmit_bitrate_bps = 30000; |
| 210 | |
| 211 | config_.rtp.ssrcs.emplace_back(1); |
| 212 | config_.rtp.ssrcs.emplace_back(2); |
| 213 | |
| 214 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 215 | .WillRepeatedly(Invoke( |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 216 | [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) { |
| 217 | EXPECT_EQ(config.min_bitrate_bps, |
| 218 | static_cast<uint32_t>(min_transmit_bitrate_bps)); |
| 219 | EXPECT_EQ(config.max_bitrate_bps, |
| 220 | static_cast<uint32_t>(qvga_stream.max_bitrate_bps + |
| 221 | vga_stream.max_bitrate_bps)); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 222 | if (config.pad_up_bitrate_bps != 0) { |
| 223 | EXPECT_EQ(config.pad_up_bitrate_bps, |
| 224 | static_cast<uint32_t>(qvga_stream.target_bitrate_bps + |
| 225 | vga_stream.min_bitrate_bps)); |
| 226 | } |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 227 | EXPECT_EQ(config.enforce_min_bitrate, !kSuspend); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 228 | })); |
| 229 | |
| 230 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 231 | ->OnEncoderConfigurationChanged( |
| 232 | std::vector<VideoStream>{qvga_stream, vga_stream}, |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 233 | VideoEncoderConfig::ContentType::kRealtimeVideo, |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 234 | min_transmit_bitrate_bps); |
| 235 | vss_impl->Stop(); |
| 236 | }); |
| 237 | } |
| 238 | |
| 239 | TEST_F(VideoSendStreamImplTest, UpdatesObserverOnConfigurationChangeWithAlr) { |
| 240 | test_queue_.SendTask([this] { |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 241 | const bool kSuspend = false; |
| 242 | config_.suspend_below_min_bitrate = kSuspend; |
| 243 | config_.rtp.extensions.emplace_back( |
| 244 | RtpExtension::kTransportSequenceNumberUri, 1); |
| 245 | config_.periodic_alr_bandwidth_probing = true; |
| 246 | auto vss_impl = CreateVideoSendStreamImpl( |
| 247 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 248 | VideoEncoderConfig::ContentType::kScreen); |
| 249 | vss_impl->Start(); |
| 250 | |
| 251 | // Simulcast screenshare. |
| 252 | VideoStream low_stream; |
| 253 | low_stream.width = 1920; |
| 254 | low_stream.height = 1080; |
| 255 | low_stream.max_framerate = 5; |
| 256 | low_stream.min_bitrate_bps = 30000; |
| 257 | low_stream.target_bitrate_bps = 200000; |
| 258 | low_stream.max_bitrate_bps = 1000000; |
| 259 | low_stream.num_temporal_layers = 2; |
| 260 | low_stream.max_qp = 56; |
| 261 | low_stream.bitrate_priority = 1; |
| 262 | |
| 263 | VideoStream high_stream; |
| 264 | high_stream.width = 1920; |
| 265 | high_stream.height = 1080; |
| 266 | high_stream.max_framerate = 30; |
| 267 | high_stream.min_bitrate_bps = 60000; |
| 268 | high_stream.target_bitrate_bps = 1250000; |
| 269 | high_stream.max_bitrate_bps = 1250000; |
| 270 | high_stream.num_temporal_layers = 2; |
| 271 | high_stream.max_qp = 56; |
| 272 | high_stream.bitrate_priority = 1; |
| 273 | |
| 274 | // With ALR probing, this will be the padding target instead of |
| 275 | // low_stream.target_bitrate_bps + high_stream.min_bitrate_bps. |
| 276 | int min_transmit_bitrate_bps = 400000; |
| 277 | |
| 278 | config_.rtp.ssrcs.emplace_back(1); |
| 279 | config_.rtp.ssrcs.emplace_back(2); |
| 280 | |
| 281 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 282 | .WillRepeatedly(Invoke( |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 283 | [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) { |
| 284 | EXPECT_EQ(config.min_bitrate_bps, |
| 285 | static_cast<uint32_t>(low_stream.min_bitrate_bps)); |
| 286 | EXPECT_EQ(config.max_bitrate_bps, |
| 287 | static_cast<uint32_t>(low_stream.max_bitrate_bps + |
| 288 | high_stream.max_bitrate_bps)); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 289 | if (config.pad_up_bitrate_bps != 0) { |
| 290 | EXPECT_EQ(config.pad_up_bitrate_bps, |
| 291 | static_cast<uint32_t>(min_transmit_bitrate_bps)); |
| 292 | } |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 293 | EXPECT_EQ(config.enforce_min_bitrate, !kSuspend); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 294 | })); |
| 295 | |
| 296 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 297 | ->OnEncoderConfigurationChanged( |
| 298 | std::vector<VideoStream>{low_stream, high_stream}, |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 299 | VideoEncoderConfig::ContentType::kScreen, min_transmit_bitrate_bps); |
| 300 | vss_impl->Stop(); |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | TEST_F(VideoSendStreamImplTest, |
| 305 | UpdatesObserverOnConfigurationChangeWithSimulcastVideoHysteresis) { |
| 306 | test::ScopedFieldTrials hysteresis_experiment( |
| 307 | "WebRTC-VideoRateControl/video_hysteresis:1.25/"); |
| 308 | |
| 309 | test_queue_.SendTask([this] { |
| 310 | auto vss_impl = CreateVideoSendStreamImpl( |
| 311 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 312 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 313 | vss_impl->Start(); |
| 314 | |
| 315 | // 2-layer video simulcast. |
| 316 | VideoStream low_stream; |
| 317 | low_stream.width = 320; |
| 318 | low_stream.height = 240; |
| 319 | low_stream.max_framerate = 30; |
| 320 | low_stream.min_bitrate_bps = 30000; |
| 321 | low_stream.target_bitrate_bps = 100000; |
| 322 | low_stream.max_bitrate_bps = 200000; |
| 323 | low_stream.max_qp = 56; |
| 324 | low_stream.bitrate_priority = 1; |
| 325 | |
| 326 | VideoStream high_stream; |
| 327 | high_stream.width = 640; |
| 328 | high_stream.height = 480; |
| 329 | high_stream.max_framerate = 30; |
| 330 | high_stream.min_bitrate_bps = 150000; |
| 331 | high_stream.target_bitrate_bps = 500000; |
| 332 | high_stream.max_bitrate_bps = 750000; |
| 333 | high_stream.max_qp = 56; |
| 334 | high_stream.bitrate_priority = 1; |
| 335 | |
| 336 | config_.rtp.ssrcs.emplace_back(1); |
| 337 | config_.rtp.ssrcs.emplace_back(2); |
| 338 | |
| 339 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 340 | .WillRepeatedly(Invoke( |
| 341 | [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) { |
| 342 | EXPECT_EQ(config.min_bitrate_bps, |
| 343 | static_cast<uint32_t>(low_stream.min_bitrate_bps)); |
| 344 | EXPECT_EQ(config.max_bitrate_bps, |
| 345 | static_cast<uint32_t>(low_stream.max_bitrate_bps + |
| 346 | high_stream.max_bitrate_bps)); |
| 347 | if (config.pad_up_bitrate_bps != 0) { |
| 348 | EXPECT_EQ( |
| 349 | config.pad_up_bitrate_bps, |
| 350 | static_cast<uint32_t>(low_stream.target_bitrate_bps + |
| 351 | 1.25 * high_stream.min_bitrate_bps)); |
| 352 | } |
| 353 | })); |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 354 | |
| 355 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 356 | ->OnEncoderConfigurationChanged( |
| 357 | std::vector<VideoStream>{low_stream, high_stream}, |
| 358 | VideoEncoderConfig::ContentType::kRealtimeVideo, |
| 359 | /*min_transmit_bitrate_bps=*/0); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 360 | vss_impl->Stop(); |
| 361 | }); |
| 362 | } |
| 363 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 364 | TEST_F(VideoSendStreamImplTest, SetsScreensharePacingFactorWithFeedback) { |
| 365 | test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString()); |
| 366 | |
| 367 | test_queue_.SendTask([this] { |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 368 | constexpr int kId = 1; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 369 | config_.rtp.extensions.emplace_back( |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 370 | RtpExtension::kTransportSequenceNumberUri, kId); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 371 | EXPECT_CALL(transport_controller_, |
| 372 | SetPacingFactor(kAlrProbingExperimentPaceMultiplier)) |
| 373 | .Times(1); |
| 374 | auto vss_impl = CreateVideoSendStreamImpl( |
| 375 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 376 | VideoEncoderConfig::ContentType::kScreen); |
| 377 | vss_impl->Start(); |
| 378 | vss_impl->Stop(); |
| 379 | }); |
| 380 | } |
| 381 | |
| 382 | TEST_F(VideoSendStreamImplTest, DoesNotSetPacingFactorWithoutFeedback) { |
| 383 | test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString()); |
| 384 | test_queue_.SendTask([this] { |
| 385 | EXPECT_CALL(transport_controller_, SetPacingFactor(_)).Times(0); |
| 386 | auto vss_impl = CreateVideoSendStreamImpl( |
| 387 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 388 | VideoEncoderConfig::ContentType::kScreen); |
| 389 | vss_impl->Start(); |
| 390 | vss_impl->Stop(); |
| 391 | }); |
| 392 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 393 | |
| 394 | TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationWhenEnabled) { |
| 395 | test_queue_.SendTask([this] { |
| 396 | EXPECT_CALL(transport_controller_, SetPacingFactor(_)).Times(0); |
| 397 | auto vss_impl = CreateVideoSendStreamImpl( |
| 398 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 399 | VideoEncoderConfig::ContentType::kScreen); |
| 400 | vss_impl->Start(); |
| 401 | VideoBitrateAllocationObserver* const observer = |
| 402 | static_cast<VideoBitrateAllocationObserver*>(vss_impl.get()); |
| 403 | |
| 404 | // Populate a test instance of video bitrate allocation. |
| 405 | VideoBitrateAllocation alloc; |
| 406 | alloc.SetBitrate(0, 0, 10000); |
| 407 | alloc.SetBitrate(0, 1, 20000); |
| 408 | alloc.SetBitrate(1, 0, 30000); |
| 409 | alloc.SetBitrate(1, 1, 40000); |
| 410 | |
| 411 | // Encoder starts out paused, don't forward allocation. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 412 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(0); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 413 | observer->OnBitrateAllocationUpdated(alloc); |
| 414 | |
| 415 | // Unpause encoder, allocation should be passed through. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 416 | const uint32_t kBitrateBps = 100000; |
| 417 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 418 | .Times(1) |
| 419 | .WillOnce(Return(kBitrateBps)); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 420 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 421 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 422 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 423 | observer->OnBitrateAllocationUpdated(alloc); |
| 424 | |
| 425 | // Pause encoder again, and block allocations. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 426 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 427 | .Times(1) |
| 428 | .WillOnce(Return(0)); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 429 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 430 | ->OnBitrateUpdated(CreateAllocation(0)); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 431 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(0); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 432 | observer->OnBitrateAllocationUpdated(alloc); |
| 433 | |
| 434 | vss_impl->Stop(); |
| 435 | }); |
| 436 | } |
| 437 | |
| 438 | TEST_F(VideoSendStreamImplTest, ThrottlesVideoBitrateAllocationWhenTooSimilar) { |
| 439 | test_queue_.SendTask([this] { |
| 440 | auto vss_impl = CreateVideoSendStreamImpl( |
| 441 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 442 | VideoEncoderConfig::ContentType::kScreen); |
| 443 | vss_impl->Start(); |
| 444 | // Unpause encoder, to allows allocations to be passed through. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 445 | const uint32_t kBitrateBps = 100000; |
| 446 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 447 | .Times(1) |
| 448 | .WillOnce(Return(kBitrateBps)); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 449 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 450 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 451 | VideoBitrateAllocationObserver* const observer = |
| 452 | static_cast<VideoBitrateAllocationObserver*>(vss_impl.get()); |
| 453 | |
| 454 | // Populate a test instance of video bitrate allocation. |
| 455 | VideoBitrateAllocation alloc; |
| 456 | alloc.SetBitrate(0, 0, 10000); |
| 457 | alloc.SetBitrate(0, 1, 20000); |
| 458 | alloc.SetBitrate(1, 0, 30000); |
| 459 | alloc.SetBitrate(1, 1, 40000); |
| 460 | |
| 461 | // Initial value. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 462 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 463 | observer->OnBitrateAllocationUpdated(alloc); |
| 464 | |
| 465 | VideoBitrateAllocation updated_alloc = alloc; |
| 466 | // Needs 10% increase in bitrate to trigger immediate forward. |
| 467 | const uint32_t base_layer_min_update_bitrate_bps = |
| 468 | alloc.GetBitrate(0, 0) + alloc.get_sum_bps() / 10; |
| 469 | |
| 470 | // Too small increase, don't forward. |
| 471 | updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps - 1); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 472 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(_)).Times(0); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 473 | observer->OnBitrateAllocationUpdated(updated_alloc); |
| 474 | |
| 475 | // Large enough increase, do forward. |
| 476 | updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 477 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(updated_alloc)) |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 478 | .Times(1); |
| 479 | observer->OnBitrateAllocationUpdated(updated_alloc); |
| 480 | |
| 481 | // This is now a decrease compared to last forward allocation, forward |
| 482 | // immediately. |
| 483 | updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps - 1); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 484 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(updated_alloc)) |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 485 | .Times(1); |
| 486 | observer->OnBitrateAllocationUpdated(updated_alloc); |
| 487 | |
| 488 | vss_impl->Stop(); |
| 489 | }); |
| 490 | } |
| 491 | |
| 492 | TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationOnLayerChange) { |
| 493 | test_queue_.SendTask([this] { |
| 494 | auto vss_impl = CreateVideoSendStreamImpl( |
| 495 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 496 | VideoEncoderConfig::ContentType::kScreen); |
| 497 | vss_impl->Start(); |
| 498 | // Unpause encoder, to allows allocations to be passed through. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 499 | const uint32_t kBitrateBps = 100000; |
| 500 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 501 | .Times(1) |
| 502 | .WillOnce(Return(kBitrateBps)); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 503 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 504 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 505 | VideoBitrateAllocationObserver* const observer = |
| 506 | static_cast<VideoBitrateAllocationObserver*>(vss_impl.get()); |
| 507 | |
| 508 | // Populate a test instance of video bitrate allocation. |
| 509 | VideoBitrateAllocation alloc; |
| 510 | alloc.SetBitrate(0, 0, 10000); |
| 511 | alloc.SetBitrate(0, 1, 20000); |
| 512 | alloc.SetBitrate(1, 0, 30000); |
| 513 | alloc.SetBitrate(1, 1, 40000); |
| 514 | |
| 515 | // Initial value. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 516 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)).Times(1); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 517 | observer->OnBitrateAllocationUpdated(alloc); |
| 518 | |
| 519 | // Move some bitrate from one layer to a new one, but keep sum the same. |
| 520 | // Since layout has changed, immediately trigger forward. |
| 521 | VideoBitrateAllocation updated_alloc = alloc; |
| 522 | updated_alloc.SetBitrate(2, 0, 10000); |
| 523 | updated_alloc.SetBitrate(1, 1, alloc.GetBitrate(1, 1) - 10000); |
| 524 | EXPECT_EQ(alloc.get_sum_bps(), updated_alloc.get_sum_bps()); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 525 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(updated_alloc)) |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 526 | .Times(1); |
| 527 | observer->OnBitrateAllocationUpdated(updated_alloc); |
| 528 | |
| 529 | vss_impl->Stop(); |
| 530 | }); |
| 531 | } |
| 532 | |
| 533 | TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationAfterTimeout) { |
| 534 | test_queue_.SendTask([this] { |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 535 | auto vss_impl = CreateVideoSendStreamImpl( |
| 536 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 537 | VideoEncoderConfig::ContentType::kScreen); |
| 538 | vss_impl->Start(); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 539 | const uint32_t kBitrateBps = 100000; |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 540 | // Unpause encoder, to allows allocations to be passed through. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 541 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 542 | .Times(1) |
| 543 | .WillRepeatedly(Return(kBitrateBps)); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 544 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 545 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 546 | VideoBitrateAllocationObserver* const observer = |
| 547 | static_cast<VideoBitrateAllocationObserver*>(vss_impl.get()); |
| 548 | |
| 549 | // Populate a test instance of video bitrate allocation. |
| 550 | VideoBitrateAllocation alloc; |
| 551 | alloc.SetBitrate(0, 0, 10000); |
| 552 | alloc.SetBitrate(0, 1, 20000); |
| 553 | alloc.SetBitrate(1, 0, 30000); |
| 554 | alloc.SetBitrate(1, 1, 40000); |
| 555 | |
| 556 | EncodedImage encoded_image; |
| 557 | CodecSpecificInfo codec_specific; |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 558 | EXPECT_CALL(rtp_video_sender_, OnEncodedImage(_, _, _)) |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 559 | .WillRepeatedly(Return( |
| 560 | EncodedImageCallback::Result(EncodedImageCallback::Result::OK))); |
| 561 | |
| 562 | // Max time we will throttle similar video bitrate allocations. |
| 563 | static constexpr int64_t kMaxVbaThrottleTimeMs = 500; |
| 564 | |
| 565 | { |
| 566 | // Initial value. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 567 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 568 | .Times(1); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 569 | observer->OnBitrateAllocationUpdated(alloc); |
| 570 | } |
| 571 | |
| 572 | { |
| 573 | // Sending same allocation again, this one should be throttled. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 574 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 575 | .Times(0); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 576 | observer->OnBitrateAllocationUpdated(alloc); |
| 577 | } |
| 578 | |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 579 | clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 580 | |
| 581 | { |
| 582 | // Sending similar allocation again after timeout, should forward. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 583 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 584 | .Times(1); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 585 | observer->OnBitrateAllocationUpdated(alloc); |
| 586 | } |
| 587 | |
| 588 | { |
| 589 | // Sending similar allocation again without timeout, throttle. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 590 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 591 | .Times(0); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 592 | observer->OnBitrateAllocationUpdated(alloc); |
| 593 | } |
| 594 | |
| 595 | { |
| 596 | // Send encoded image, should be a noop. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 597 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 598 | .Times(0); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 599 | static_cast<EncodedImageCallback*>(vss_impl.get()) |
| 600 | ->OnEncodedImage(encoded_image, &codec_specific, nullptr); |
| 601 | } |
| 602 | |
| 603 | { |
| 604 | // Advance time and send encoded image, this should wake up and send |
| 605 | // cached bitrate allocation. |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 606 | clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 607 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 608 | .Times(1); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 609 | static_cast<EncodedImageCallback*>(vss_impl.get()) |
| 610 | ->OnEncodedImage(encoded_image, &codec_specific, nullptr); |
| 611 | } |
| 612 | |
| 613 | { |
| 614 | // Advance time and send encoded image, there should be no cached |
| 615 | // allocation to send. |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 616 | clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 617 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 618 | .Times(0); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 619 | static_cast<EncodedImageCallback*>(vss_impl.get()) |
| 620 | ->OnEncodedImage(encoded_image, &codec_specific, nullptr); |
| 621 | } |
| 622 | |
| 623 | vss_impl->Stop(); |
| 624 | }); |
| 625 | } |
| 626 | |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 627 | TEST_F(VideoSendStreamImplTest, CallsVideoStreamEncoderOnBitrateUpdate) { |
| 628 | test_queue_.SendTask([this] { |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 629 | const bool kSuspend = false; |
| 630 | config_.suspend_below_min_bitrate = kSuspend; |
| 631 | config_.rtp.extensions.emplace_back( |
| 632 | RtpExtension::kTransportSequenceNumberUri, 1); |
| 633 | auto vss_impl = CreateVideoSendStreamImpl( |
| 634 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 635 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 636 | vss_impl->Start(); |
| 637 | |
| 638 | VideoStream qvga_stream; |
| 639 | qvga_stream.width = 320; |
| 640 | qvga_stream.height = 180; |
| 641 | qvga_stream.max_framerate = 30; |
| 642 | qvga_stream.min_bitrate_bps = 30000; |
| 643 | qvga_stream.target_bitrate_bps = 150000; |
| 644 | qvga_stream.max_bitrate_bps = 200000; |
| 645 | qvga_stream.max_qp = 56; |
| 646 | qvga_stream.bitrate_priority = 1; |
| 647 | |
| 648 | int min_transmit_bitrate_bps = 30000; |
| 649 | |
| 650 | config_.rtp.ssrcs.emplace_back(1); |
| 651 | |
| 652 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 653 | ->OnEncoderConfigurationChanged( |
| 654 | std::vector<VideoStream>{qvga_stream}, |
| 655 | VideoEncoderConfig::ContentType::kRealtimeVideo, |
| 656 | min_transmit_bitrate_bps); |
| 657 | |
| 658 | const DataRate network_constrained_rate = |
| 659 | DataRate::bps(qvga_stream.target_bitrate_bps); |
| 660 | BitrateAllocationUpdate update; |
| 661 | update.target_bitrate = network_constrained_rate; |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 662 | update.stable_target_bitrate = network_constrained_rate; |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 663 | update.round_trip_time = TimeDelta::ms(1); |
| 664 | EXPECT_CALL(rtp_video_sender_, |
| 665 | OnBitrateUpdated(network_constrained_rate.bps(), _, |
| 666 | update.round_trip_time.ms(), _)); |
| 667 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 668 | .WillOnce(Return(network_constrained_rate.bps())); |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 669 | EXPECT_CALL( |
| 670 | video_stream_encoder_, |
| 671 | OnBitrateUpdated(network_constrained_rate, network_constrained_rate, |
| 672 | network_constrained_rate, 0, _)); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 673 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 674 | ->OnBitrateUpdated(update); |
| 675 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 676 | // Test allocation where the link allocation is larger than the target, |
| 677 | // meaning we have some headroom on the link. |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 678 | const DataRate qvga_max_bitrate = |
| 679 | DataRate::bps(qvga_stream.max_bitrate_bps); |
| 680 | const DataRate headroom = DataRate::bps(50000); |
| 681 | const DataRate rate_with_headroom = qvga_max_bitrate + headroom; |
| 682 | EXPECT_CALL(rtp_video_sender_, |
| 683 | OnBitrateUpdated(rate_with_headroom.bps(), _, |
| 684 | update.round_trip_time.ms(), _)); |
| 685 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 686 | .WillOnce(Return(rate_with_headroom.bps())); |
| 687 | EXPECT_CALL(video_stream_encoder_, |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 688 | OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate, |
| 689 | rate_with_headroom, 0, _)); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 690 | update.target_bitrate = rate_with_headroom; |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 691 | update.stable_target_bitrate = rate_with_headroom; |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 692 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 693 | ->OnBitrateUpdated(update); |
| 694 | |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 695 | // Add protection bitrate to the mix, this should be subtracted from the |
| 696 | // headroom. |
| 697 | const uint32_t protection_bitrate_bps = 10000; |
| 698 | EXPECT_CALL(rtp_video_sender_, GetProtectionBitrateBps()) |
| 699 | .WillOnce(Return(protection_bitrate_bps)); |
| 700 | |
| 701 | EXPECT_CALL(rtp_video_sender_, |
| 702 | OnBitrateUpdated(rate_with_headroom.bps(), _, |
| 703 | update.round_trip_time.ms(), _)); |
| 704 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 705 | .WillOnce(Return(rate_with_headroom.bps())); |
| 706 | const DataRate headroom_minus_protection = |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 707 | rate_with_headroom - DataRate::bps(protection_bitrate_bps); |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 708 | EXPECT_CALL(video_stream_encoder_, |
| 709 | OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate, |
| 710 | headroom_minus_protection, 0, _)); |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 711 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 712 | ->OnBitrateUpdated(update); |
| 713 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 714 | // Protection bitrate exceeds head room, link allocation should be capped to |
| 715 | // target bitrate. |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 716 | EXPECT_CALL(rtp_video_sender_, GetProtectionBitrateBps()) |
| 717 | .WillOnce(Return(headroom.bps() + 1000)); |
| 718 | EXPECT_CALL(rtp_video_sender_, |
| 719 | OnBitrateUpdated(rate_with_headroom.bps(), _, |
| 720 | update.round_trip_time.ms(), _)); |
| 721 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 722 | .WillOnce(Return(rate_with_headroom.bps())); |
| 723 | EXPECT_CALL(video_stream_encoder_, |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 724 | OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate, |
| 725 | qvga_max_bitrate, 0, _)); |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 726 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 727 | ->OnBitrateUpdated(update); |
| 728 | |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 729 | // Set rates to zero on stop. |
| 730 | EXPECT_CALL(video_stream_encoder_, |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 731 | OnBitrateUpdated(DataRate::Zero(), DataRate::Zero(), |
| 732 | DataRate::Zero(), 0, 0)); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 733 | vss_impl->Stop(); |
| 734 | }); |
| 735 | } |
| 736 | |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 737 | TEST_F(VideoSendStreamImplTest, DisablesPaddingOnPausedEncoder) { |
| 738 | int padding_bitrate = 0; |
| 739 | std::unique_ptr<VideoSendStreamImpl> vss_impl; |
| 740 | |
| 741 | test_queue_.SendTask([&] { |
| 742 | vss_impl = CreateVideoSendStreamImpl( |
| 743 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 744 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 745 | |
| 746 | // Capture padding bitrate for testing. |
| 747 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
| 748 | .WillRepeatedly(Invoke( |
| 749 | [&](BitrateAllocatorObserver*, MediaStreamAllocationConfig config) { |
| 750 | padding_bitrate = config.pad_up_bitrate_bps; |
| 751 | })); |
| 752 | // If observer is removed, no padding will be sent. |
| 753 | EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get())) |
| 754 | .WillRepeatedly( |
| 755 | Invoke([&](BitrateAllocatorObserver*) { padding_bitrate = 0; })); |
| 756 | |
| 757 | EXPECT_CALL(rtp_video_sender_, OnEncodedImage(_, _, _)) |
| 758 | .WillRepeatedly(Return( |
| 759 | EncodedImageCallback::Result(EncodedImageCallback::Result::OK))); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 760 | const bool kSuspend = false; |
| 761 | config_.suspend_below_min_bitrate = kSuspend; |
| 762 | config_.rtp.extensions.emplace_back( |
| 763 | RtpExtension::kTransportSequenceNumberUri, 1); |
| 764 | VideoStream qvga_stream; |
| 765 | qvga_stream.width = 320; |
| 766 | qvga_stream.height = 180; |
| 767 | qvga_stream.max_framerate = 30; |
| 768 | qvga_stream.min_bitrate_bps = 30000; |
| 769 | qvga_stream.target_bitrate_bps = 150000; |
| 770 | qvga_stream.max_bitrate_bps = 200000; |
| 771 | qvga_stream.max_qp = 56; |
| 772 | qvga_stream.bitrate_priority = 1; |
| 773 | |
| 774 | int min_transmit_bitrate_bps = 30000; |
| 775 | |
| 776 | config_.rtp.ssrcs.emplace_back(1); |
| 777 | |
| 778 | vss_impl->Start(); |
| 779 | |
| 780 | // Starts without padding. |
| 781 | EXPECT_EQ(0, padding_bitrate); |
| 782 | |
| 783 | // Reconfigure e.g. due to a fake frame. |
| 784 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 785 | ->OnEncoderConfigurationChanged( |
| 786 | std::vector<VideoStream>{qvga_stream}, |
| 787 | VideoEncoderConfig::ContentType::kRealtimeVideo, |
| 788 | min_transmit_bitrate_bps); |
| 789 | // Still no padding because no actual frames were passed, only |
| 790 | // reconfiguration happened. |
| 791 | EXPECT_EQ(0, padding_bitrate); |
| 792 | |
| 793 | // Unpause encoder. |
| 794 | const uint32_t kBitrateBps = 100000; |
| 795 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 796 | .Times(1) |
| 797 | .WillOnce(Return(kBitrateBps)); |
| 798 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 799 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
| 800 | |
| 801 | // A frame is encoded. |
| 802 | EncodedImage encoded_image; |
| 803 | CodecSpecificInfo codec_specific; |
| 804 | static_cast<EncodedImageCallback*>(vss_impl.get()) |
| 805 | ->OnEncodedImage(encoded_image, &codec_specific, nullptr); |
| 806 | // Only after actual frame is encoded are we enabling the padding. |
| 807 | EXPECT_GT(padding_bitrate, 0); |
| 808 | }); |
| 809 | |
| 810 | rtc::Event done; |
| 811 | test_queue_.PostDelayedTask( |
| 812 | [&] { |
| 813 | // No padding supposed to be sent for paused observer |
| 814 | EXPECT_EQ(0, padding_bitrate); |
| 815 | testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_); |
| 816 | vss_impl->Stop(); |
| 817 | vss_impl.reset(); |
| 818 | done.Set(); |
| 819 | }, |
| 820 | 5000); |
| 821 | |
| 822 | // Pause the test suite so that the last delayed task executes. |
| 823 | ASSERT_TRUE(done.Wait(10000)); |
| 824 | } |
| 825 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 826 | } // namespace internal |
| 827 | } // namespace webrtc |