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