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) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 156 | test_queue_.SendTask( |
| 157 | [this] { |
| 158 | const bool kSuspend = false; |
| 159 | config_.suspend_below_min_bitrate = kSuspend; |
| 160 | auto vss_impl = CreateVideoSendStreamImpl( |
| 161 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 162 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 163 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
| 164 | .WillOnce(Invoke([&](BitrateAllocatorObserver*, |
| 165 | MediaStreamAllocationConfig config) { |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 166 | EXPECT_EQ(config.min_bitrate_bps, 0u); |
| 167 | EXPECT_EQ(config.max_bitrate_bps, kDefaultInitialBitrateBps); |
| 168 | EXPECT_EQ(config.pad_up_bitrate_bps, 0u); |
| 169 | EXPECT_EQ(config.enforce_min_bitrate, !kSuspend); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 170 | EXPECT_EQ(config.bitrate_priority, kDefaultBitratePriority); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 171 | })); |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 172 | vss_impl->Start(); |
| 173 | EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get())) |
| 174 | .Times(1); |
| 175 | vss_impl->Stop(); |
| 176 | }, |
| 177 | RTC_FROM_HERE); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 180 | TEST_F(VideoSendStreamImplTest, UpdatesObserverOnConfigurationChange) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 181 | test_queue_.SendTask( |
| 182 | [this] { |
| 183 | const bool kSuspend = false; |
| 184 | config_.suspend_below_min_bitrate = kSuspend; |
| 185 | config_.rtp.extensions.emplace_back( |
| 186 | RtpExtension::kTransportSequenceNumberUri, 1); |
| 187 | auto vss_impl = CreateVideoSendStreamImpl( |
| 188 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 189 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 190 | vss_impl->Start(); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 191 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 192 | // QVGA + VGA configuration matching defaults in |
| 193 | // media/engine/simulcast.cc. |
| 194 | VideoStream qvga_stream; |
| 195 | qvga_stream.width = 320; |
| 196 | qvga_stream.height = 180; |
| 197 | qvga_stream.max_framerate = 30; |
| 198 | qvga_stream.min_bitrate_bps = 30000; |
| 199 | qvga_stream.target_bitrate_bps = 150000; |
| 200 | qvga_stream.max_bitrate_bps = 200000; |
| 201 | qvga_stream.max_qp = 56; |
| 202 | qvga_stream.bitrate_priority = 1; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 203 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 204 | VideoStream vga_stream; |
| 205 | vga_stream.width = 640; |
| 206 | vga_stream.height = 360; |
| 207 | vga_stream.max_framerate = 30; |
| 208 | vga_stream.min_bitrate_bps = 150000; |
| 209 | vga_stream.target_bitrate_bps = 500000; |
| 210 | vga_stream.max_bitrate_bps = 700000; |
| 211 | vga_stream.max_qp = 56; |
| 212 | vga_stream.bitrate_priority = 1; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 213 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 214 | int min_transmit_bitrate_bps = 30000; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 215 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 216 | config_.rtp.ssrcs.emplace_back(1); |
| 217 | config_.rtp.ssrcs.emplace_back(2); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 218 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 219 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
| 220 | .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*, |
| 221 | MediaStreamAllocationConfig config) { |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 222 | EXPECT_EQ(config.min_bitrate_bps, |
| 223 | static_cast<uint32_t>(min_transmit_bitrate_bps)); |
| 224 | EXPECT_EQ(config.max_bitrate_bps, |
| 225 | static_cast<uint32_t>(qvga_stream.max_bitrate_bps + |
| 226 | vga_stream.max_bitrate_bps)); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 227 | if (config.pad_up_bitrate_bps != 0) { |
| 228 | EXPECT_EQ(config.pad_up_bitrate_bps, |
| 229 | static_cast<uint32_t>(qvga_stream.target_bitrate_bps + |
| 230 | vga_stream.min_bitrate_bps)); |
| 231 | } |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 232 | EXPECT_EQ(config.enforce_min_bitrate, !kSuspend); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 233 | })); |
| 234 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 235 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 236 | ->OnEncoderConfigurationChanged( |
| 237 | std::vector<VideoStream>{qvga_stream, vga_stream}, |
| 238 | VideoEncoderConfig::ContentType::kRealtimeVideo, |
| 239 | min_transmit_bitrate_bps); |
| 240 | vss_impl->Stop(); |
| 241 | }, |
| 242 | RTC_FROM_HERE); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | TEST_F(VideoSendStreamImplTest, UpdatesObserverOnConfigurationChangeWithAlr) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 246 | test_queue_.SendTask( |
| 247 | [this] { |
| 248 | const bool kSuspend = false; |
| 249 | config_.suspend_below_min_bitrate = kSuspend; |
| 250 | config_.rtp.extensions.emplace_back( |
| 251 | RtpExtension::kTransportSequenceNumberUri, 1); |
| 252 | config_.periodic_alr_bandwidth_probing = true; |
| 253 | auto vss_impl = CreateVideoSendStreamImpl( |
| 254 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 255 | VideoEncoderConfig::ContentType::kScreen); |
| 256 | vss_impl->Start(); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 257 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 258 | // Simulcast screenshare. |
| 259 | VideoStream low_stream; |
| 260 | low_stream.width = 1920; |
| 261 | low_stream.height = 1080; |
| 262 | low_stream.max_framerate = 5; |
| 263 | low_stream.min_bitrate_bps = 30000; |
| 264 | low_stream.target_bitrate_bps = 200000; |
| 265 | low_stream.max_bitrate_bps = 1000000; |
| 266 | low_stream.num_temporal_layers = 2; |
| 267 | low_stream.max_qp = 56; |
| 268 | low_stream.bitrate_priority = 1; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 269 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 270 | VideoStream high_stream; |
| 271 | high_stream.width = 1920; |
| 272 | high_stream.height = 1080; |
| 273 | high_stream.max_framerate = 30; |
| 274 | high_stream.min_bitrate_bps = 60000; |
| 275 | high_stream.target_bitrate_bps = 1250000; |
| 276 | high_stream.max_bitrate_bps = 1250000; |
| 277 | high_stream.num_temporal_layers = 2; |
| 278 | high_stream.max_qp = 56; |
| 279 | high_stream.bitrate_priority = 1; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 280 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 281 | // With ALR probing, this will be the padding target instead of |
| 282 | // low_stream.target_bitrate_bps + high_stream.min_bitrate_bps. |
| 283 | int min_transmit_bitrate_bps = 400000; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 284 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 285 | config_.rtp.ssrcs.emplace_back(1); |
| 286 | config_.rtp.ssrcs.emplace_back(2); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 287 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 288 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
| 289 | .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*, |
| 290 | MediaStreamAllocationConfig config) { |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 291 | EXPECT_EQ(config.min_bitrate_bps, |
| 292 | static_cast<uint32_t>(low_stream.min_bitrate_bps)); |
| 293 | EXPECT_EQ(config.max_bitrate_bps, |
| 294 | static_cast<uint32_t>(low_stream.max_bitrate_bps + |
| 295 | high_stream.max_bitrate_bps)); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 296 | if (config.pad_up_bitrate_bps != 0) { |
| 297 | EXPECT_EQ(config.pad_up_bitrate_bps, |
| 298 | static_cast<uint32_t>(min_transmit_bitrate_bps)); |
| 299 | } |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 300 | EXPECT_EQ(config.enforce_min_bitrate, !kSuspend); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 301 | })); |
| 302 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 303 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 304 | ->OnEncoderConfigurationChanged( |
| 305 | std::vector<VideoStream>{low_stream, high_stream}, |
| 306 | VideoEncoderConfig::ContentType::kScreen, |
| 307 | min_transmit_bitrate_bps); |
| 308 | vss_impl->Stop(); |
| 309 | }, |
| 310 | RTC_FROM_HERE); |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | TEST_F(VideoSendStreamImplTest, |
| 314 | UpdatesObserverOnConfigurationChangeWithSimulcastVideoHysteresis) { |
| 315 | test::ScopedFieldTrials hysteresis_experiment( |
| 316 | "WebRTC-VideoRateControl/video_hysteresis:1.25/"); |
| 317 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 318 | test_queue_.SendTask( |
| 319 | [this] { |
| 320 | auto vss_impl = CreateVideoSendStreamImpl( |
| 321 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 322 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 323 | vss_impl->Start(); |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 324 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 325 | // 2-layer video simulcast. |
| 326 | VideoStream low_stream; |
| 327 | low_stream.width = 320; |
| 328 | low_stream.height = 240; |
| 329 | low_stream.max_framerate = 30; |
| 330 | low_stream.min_bitrate_bps = 30000; |
| 331 | low_stream.target_bitrate_bps = 100000; |
| 332 | low_stream.max_bitrate_bps = 200000; |
| 333 | low_stream.max_qp = 56; |
| 334 | low_stream.bitrate_priority = 1; |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 335 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 336 | VideoStream high_stream; |
| 337 | high_stream.width = 640; |
| 338 | high_stream.height = 480; |
| 339 | high_stream.max_framerate = 30; |
| 340 | high_stream.min_bitrate_bps = 150000; |
| 341 | high_stream.target_bitrate_bps = 500000; |
| 342 | high_stream.max_bitrate_bps = 750000; |
| 343 | high_stream.max_qp = 56; |
| 344 | high_stream.bitrate_priority = 1; |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 345 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 346 | config_.rtp.ssrcs.emplace_back(1); |
| 347 | config_.rtp.ssrcs.emplace_back(2); |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 348 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 349 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
| 350 | .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*, |
| 351 | MediaStreamAllocationConfig config) { |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 352 | EXPECT_EQ(config.min_bitrate_bps, |
| 353 | static_cast<uint32_t>(low_stream.min_bitrate_bps)); |
| 354 | EXPECT_EQ(config.max_bitrate_bps, |
| 355 | static_cast<uint32_t>(low_stream.max_bitrate_bps + |
| 356 | high_stream.max_bitrate_bps)); |
| 357 | if (config.pad_up_bitrate_bps != 0) { |
| 358 | EXPECT_EQ( |
| 359 | config.pad_up_bitrate_bps, |
| 360 | static_cast<uint32_t>(low_stream.target_bitrate_bps + |
| 361 | 1.25 * high_stream.min_bitrate_bps)); |
| 362 | } |
| 363 | })); |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 364 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 365 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 366 | ->OnEncoderConfigurationChanged( |
| 367 | std::vector<VideoStream>{low_stream, high_stream}, |
| 368 | VideoEncoderConfig::ContentType::kRealtimeVideo, |
| 369 | /*min_transmit_bitrate_bps=*/0); |
| 370 | vss_impl->Stop(); |
| 371 | }, |
| 372 | RTC_FROM_HERE); |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 375 | TEST_F(VideoSendStreamImplTest, SetsScreensharePacingFactorWithFeedback) { |
| 376 | test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString()); |
| 377 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 378 | test_queue_.SendTask( |
| 379 | [this] { |
| 380 | constexpr int kId = 1; |
| 381 | config_.rtp.extensions.emplace_back( |
| 382 | RtpExtension::kTransportSequenceNumberUri, kId); |
| 383 | EXPECT_CALL(transport_controller_, |
| 384 | SetPacingFactor(kAlrProbingExperimentPaceMultiplier)) |
| 385 | .Times(1); |
| 386 | auto vss_impl = CreateVideoSendStreamImpl( |
| 387 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 388 | VideoEncoderConfig::ContentType::kScreen); |
| 389 | vss_impl->Start(); |
| 390 | vss_impl->Stop(); |
| 391 | }, |
| 392 | RTC_FROM_HERE); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | TEST_F(VideoSendStreamImplTest, DoesNotSetPacingFactorWithoutFeedback) { |
| 396 | test::ScopedFieldTrials alr_experiment(GetAlrProbingExperimentString()); |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 397 | test_queue_.SendTask( |
| 398 | [this] { |
| 399 | EXPECT_CALL(transport_controller_, SetPacingFactor(_)).Times(0); |
| 400 | auto vss_impl = CreateVideoSendStreamImpl( |
| 401 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 402 | VideoEncoderConfig::ContentType::kScreen); |
| 403 | vss_impl->Start(); |
| 404 | vss_impl->Stop(); |
| 405 | }, |
| 406 | RTC_FROM_HERE); |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 407 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 408 | |
| 409 | TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationWhenEnabled) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 410 | test_queue_.SendTask( |
| 411 | [this] { |
| 412 | EXPECT_CALL(transport_controller_, SetPacingFactor(_)).Times(0); |
| 413 | auto vss_impl = CreateVideoSendStreamImpl( |
| 414 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 415 | VideoEncoderConfig::ContentType::kScreen); |
| 416 | vss_impl->Start(); |
| 417 | VideoBitrateAllocationObserver* const observer = |
| 418 | static_cast<VideoBitrateAllocationObserver*>(vss_impl.get()); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 419 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 420 | // Populate a test instance of video bitrate allocation. |
| 421 | VideoBitrateAllocation alloc; |
| 422 | alloc.SetBitrate(0, 0, 10000); |
| 423 | alloc.SetBitrate(0, 1, 20000); |
| 424 | alloc.SetBitrate(1, 0, 30000); |
| 425 | alloc.SetBitrate(1, 1, 40000); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 426 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 427 | // Encoder starts out paused, don't forward allocation. |
| 428 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 429 | .Times(0); |
| 430 | observer->OnBitrateAllocationUpdated(alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 431 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 432 | // Unpause encoder, allocation should be passed through. |
| 433 | const uint32_t kBitrateBps = 100000; |
| 434 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 435 | .Times(1) |
| 436 | .WillOnce(Return(kBitrateBps)); |
| 437 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 438 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
| 439 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 440 | .Times(1); |
| 441 | observer->OnBitrateAllocationUpdated(alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 442 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 443 | // Pause encoder again, and block allocations. |
| 444 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 445 | .Times(1) |
| 446 | .WillOnce(Return(0)); |
| 447 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 448 | ->OnBitrateUpdated(CreateAllocation(0)); |
| 449 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 450 | .Times(0); |
| 451 | observer->OnBitrateAllocationUpdated(alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 452 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 453 | vss_impl->Stop(); |
| 454 | }, |
| 455 | RTC_FROM_HERE); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | TEST_F(VideoSendStreamImplTest, ThrottlesVideoBitrateAllocationWhenTooSimilar) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 459 | test_queue_.SendTask( |
| 460 | [this] { |
| 461 | auto vss_impl = CreateVideoSendStreamImpl( |
| 462 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 463 | VideoEncoderConfig::ContentType::kScreen); |
| 464 | vss_impl->Start(); |
| 465 | // Unpause encoder, to allows allocations to be passed through. |
| 466 | const uint32_t kBitrateBps = 100000; |
| 467 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 468 | .Times(1) |
| 469 | .WillOnce(Return(kBitrateBps)); |
| 470 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 471 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
| 472 | VideoBitrateAllocationObserver* const observer = |
| 473 | static_cast<VideoBitrateAllocationObserver*>(vss_impl.get()); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 474 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 475 | // Populate a test instance of video bitrate allocation. |
| 476 | VideoBitrateAllocation alloc; |
| 477 | alloc.SetBitrate(0, 0, 10000); |
| 478 | alloc.SetBitrate(0, 1, 20000); |
| 479 | alloc.SetBitrate(1, 0, 30000); |
| 480 | alloc.SetBitrate(1, 1, 40000); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 481 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 482 | // Initial value. |
| 483 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 484 | .Times(1); |
| 485 | observer->OnBitrateAllocationUpdated(alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 486 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 487 | VideoBitrateAllocation updated_alloc = alloc; |
| 488 | // Needs 10% increase in bitrate to trigger immediate forward. |
| 489 | const uint32_t base_layer_min_update_bitrate_bps = |
| 490 | alloc.GetBitrate(0, 0) + alloc.get_sum_bps() / 10; |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 491 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 492 | // Too small increase, don't forward. |
| 493 | updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps - 1); |
| 494 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(_)).Times(0); |
| 495 | observer->OnBitrateAllocationUpdated(updated_alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 496 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 497 | // Large enough increase, do forward. |
| 498 | updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps); |
| 499 | EXPECT_CALL(rtp_video_sender_, |
| 500 | OnBitrateAllocationUpdated(updated_alloc)) |
| 501 | .Times(1); |
| 502 | observer->OnBitrateAllocationUpdated(updated_alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 503 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 504 | // This is now a decrease compared to last forward allocation, forward |
| 505 | // immediately. |
| 506 | updated_alloc.SetBitrate(0, 0, base_layer_min_update_bitrate_bps - 1); |
| 507 | EXPECT_CALL(rtp_video_sender_, |
| 508 | OnBitrateAllocationUpdated(updated_alloc)) |
| 509 | .Times(1); |
| 510 | observer->OnBitrateAllocationUpdated(updated_alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 511 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 512 | vss_impl->Stop(); |
| 513 | }, |
| 514 | RTC_FROM_HERE); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationOnLayerChange) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 518 | test_queue_.SendTask( |
| 519 | [this] { |
| 520 | auto vss_impl = CreateVideoSendStreamImpl( |
| 521 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 522 | VideoEncoderConfig::ContentType::kScreen); |
| 523 | vss_impl->Start(); |
| 524 | // Unpause encoder, to allows allocations to be passed through. |
| 525 | const uint32_t kBitrateBps = 100000; |
| 526 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 527 | .Times(1) |
| 528 | .WillOnce(Return(kBitrateBps)); |
| 529 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 530 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
| 531 | VideoBitrateAllocationObserver* const observer = |
| 532 | static_cast<VideoBitrateAllocationObserver*>(vss_impl.get()); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 533 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 534 | // Populate a test instance of video bitrate allocation. |
| 535 | VideoBitrateAllocation alloc; |
| 536 | alloc.SetBitrate(0, 0, 10000); |
| 537 | alloc.SetBitrate(0, 1, 20000); |
| 538 | alloc.SetBitrate(1, 0, 30000); |
| 539 | alloc.SetBitrate(1, 1, 40000); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 540 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 541 | // Initial value. |
| 542 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 543 | .Times(1); |
| 544 | observer->OnBitrateAllocationUpdated(alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 545 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 546 | // Move some bitrate from one layer to a new one, but keep sum the same. |
| 547 | // Since layout has changed, immediately trigger forward. |
| 548 | VideoBitrateAllocation updated_alloc = alloc; |
| 549 | updated_alloc.SetBitrate(2, 0, 10000); |
| 550 | updated_alloc.SetBitrate(1, 1, alloc.GetBitrate(1, 1) - 10000); |
| 551 | EXPECT_EQ(alloc.get_sum_bps(), updated_alloc.get_sum_bps()); |
| 552 | EXPECT_CALL(rtp_video_sender_, |
| 553 | OnBitrateAllocationUpdated(updated_alloc)) |
| 554 | .Times(1); |
| 555 | observer->OnBitrateAllocationUpdated(updated_alloc); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 556 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 557 | vss_impl->Stop(); |
| 558 | }, |
| 559 | RTC_FROM_HERE); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | TEST_F(VideoSendStreamImplTest, ForwardsVideoBitrateAllocationAfterTimeout) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 563 | test_queue_.SendTask( |
| 564 | [this] { |
| 565 | auto vss_impl = CreateVideoSendStreamImpl( |
| 566 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 567 | VideoEncoderConfig::ContentType::kScreen); |
| 568 | vss_impl->Start(); |
| 569 | const uint32_t kBitrateBps = 100000; |
| 570 | // Unpause encoder, to allows allocations to be passed through. |
| 571 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 572 | .Times(1) |
| 573 | .WillRepeatedly(Return(kBitrateBps)); |
| 574 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 575 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
| 576 | VideoBitrateAllocationObserver* const observer = |
| 577 | static_cast<VideoBitrateAllocationObserver*>(vss_impl.get()); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 578 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 579 | // Populate a test instance of video bitrate allocation. |
| 580 | VideoBitrateAllocation alloc; |
| 581 | alloc.SetBitrate(0, 0, 10000); |
| 582 | alloc.SetBitrate(0, 1, 20000); |
| 583 | alloc.SetBitrate(1, 0, 30000); |
| 584 | alloc.SetBitrate(1, 1, 40000); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 585 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 586 | EncodedImage encoded_image; |
| 587 | CodecSpecificInfo codec_specific; |
| 588 | EXPECT_CALL(rtp_video_sender_, OnEncodedImage(_, _, _)) |
| 589 | .WillRepeatedly(Return(EncodedImageCallback::Result( |
| 590 | EncodedImageCallback::Result::OK))); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 591 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 592 | // Max time we will throttle similar video bitrate allocations. |
| 593 | static constexpr int64_t kMaxVbaThrottleTimeMs = 500; |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 594 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 595 | { |
| 596 | // Initial value. |
| 597 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 598 | .Times(1); |
| 599 | observer->OnBitrateAllocationUpdated(alloc); |
| 600 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 601 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 602 | { |
| 603 | // Sending same allocation again, this one should be throttled. |
| 604 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 605 | .Times(0); |
| 606 | observer->OnBitrateAllocationUpdated(alloc); |
| 607 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 608 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 609 | clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 610 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 611 | { |
| 612 | // Sending similar allocation again after timeout, should forward. |
| 613 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 614 | .Times(1); |
| 615 | observer->OnBitrateAllocationUpdated(alloc); |
| 616 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 617 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 618 | { |
| 619 | // Sending similar allocation again without timeout, throttle. |
| 620 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 621 | .Times(0); |
| 622 | observer->OnBitrateAllocationUpdated(alloc); |
| 623 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 624 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 625 | { |
| 626 | // Send encoded image, should be a noop. |
| 627 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 628 | .Times(0); |
| 629 | static_cast<EncodedImageCallback*>(vss_impl.get()) |
| 630 | ->OnEncodedImage(encoded_image, &codec_specific, nullptr); |
| 631 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 632 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 633 | { |
| 634 | // Advance time and send encoded image, this should wake up and send |
| 635 | // cached bitrate allocation. |
| 636 | clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000); |
| 637 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 638 | .Times(1); |
| 639 | static_cast<EncodedImageCallback*>(vss_impl.get()) |
| 640 | ->OnEncodedImage(encoded_image, &codec_specific, nullptr); |
| 641 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 642 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 643 | { |
| 644 | // Advance time and send encoded image, there should be no cached |
| 645 | // allocation to send. |
| 646 | clock_.AdvanceTimeMicroseconds(kMaxVbaThrottleTimeMs * 1000); |
| 647 | EXPECT_CALL(rtp_video_sender_, OnBitrateAllocationUpdated(alloc)) |
| 648 | .Times(0); |
| 649 | static_cast<EncodedImageCallback*>(vss_impl.get()) |
| 650 | ->OnEncodedImage(encoded_image, &codec_specific, nullptr); |
| 651 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 652 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 653 | vss_impl->Stop(); |
| 654 | }, |
| 655 | RTC_FROM_HERE); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 656 | } |
| 657 | |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 658 | TEST_F(VideoSendStreamImplTest, CallsVideoStreamEncoderOnBitrateUpdate) { |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 659 | test_queue_.SendTask( |
| 660 | [this] { |
| 661 | const bool kSuspend = false; |
| 662 | config_.suspend_below_min_bitrate = kSuspend; |
| 663 | config_.rtp.extensions.emplace_back( |
| 664 | RtpExtension::kTransportSequenceNumberUri, 1); |
| 665 | auto vss_impl = CreateVideoSendStreamImpl( |
| 666 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 667 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
| 668 | vss_impl->Start(); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 669 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 670 | VideoStream qvga_stream; |
| 671 | qvga_stream.width = 320; |
| 672 | qvga_stream.height = 180; |
| 673 | qvga_stream.max_framerate = 30; |
| 674 | qvga_stream.min_bitrate_bps = 30000; |
| 675 | qvga_stream.target_bitrate_bps = 150000; |
| 676 | qvga_stream.max_bitrate_bps = 200000; |
| 677 | qvga_stream.max_qp = 56; |
| 678 | qvga_stream.bitrate_priority = 1; |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 679 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 680 | int min_transmit_bitrate_bps = 30000; |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 681 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 682 | config_.rtp.ssrcs.emplace_back(1); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 683 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 684 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 685 | ->OnEncoderConfigurationChanged( |
| 686 | std::vector<VideoStream>{qvga_stream}, |
| 687 | VideoEncoderConfig::ContentType::kRealtimeVideo, |
| 688 | min_transmit_bitrate_bps); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 689 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 690 | const DataRate network_constrained_rate = |
| 691 | DataRate::bps(qvga_stream.target_bitrate_bps); |
| 692 | BitrateAllocationUpdate update; |
| 693 | update.target_bitrate = network_constrained_rate; |
| 694 | update.stable_target_bitrate = network_constrained_rate; |
| 695 | update.round_trip_time = TimeDelta::ms(1); |
| 696 | EXPECT_CALL(rtp_video_sender_, |
| 697 | OnBitrateUpdated(network_constrained_rate.bps(), _, |
| 698 | update.round_trip_time.ms(), _)); |
| 699 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 700 | .WillOnce(Return(network_constrained_rate.bps())); |
| 701 | EXPECT_CALL( |
| 702 | video_stream_encoder_, |
| 703 | OnBitrateUpdated(network_constrained_rate, network_constrained_rate, |
| 704 | network_constrained_rate, 0, _)); |
| 705 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 706 | ->OnBitrateUpdated(update); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 707 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 708 | // Test allocation where the link allocation is larger than the target, |
| 709 | // meaning we have some headroom on the link. |
| 710 | const DataRate qvga_max_bitrate = |
| 711 | DataRate::bps(qvga_stream.max_bitrate_bps); |
| 712 | const DataRate headroom = DataRate::bps(50000); |
| 713 | const DataRate rate_with_headroom = qvga_max_bitrate + headroom; |
| 714 | EXPECT_CALL(rtp_video_sender_, |
| 715 | OnBitrateUpdated(rate_with_headroom.bps(), _, |
| 716 | update.round_trip_time.ms(), _)); |
| 717 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 718 | .WillOnce(Return(rate_with_headroom.bps())); |
| 719 | EXPECT_CALL(video_stream_encoder_, |
| 720 | OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate, |
| 721 | rate_with_headroom, 0, _)); |
| 722 | update.target_bitrate = rate_with_headroom; |
| 723 | update.stable_target_bitrate = rate_with_headroom; |
| 724 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 725 | ->OnBitrateUpdated(update); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 726 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 727 | // Add protection bitrate to the mix, this should be subtracted from the |
| 728 | // headroom. |
| 729 | const uint32_t protection_bitrate_bps = 10000; |
| 730 | EXPECT_CALL(rtp_video_sender_, GetProtectionBitrateBps()) |
| 731 | .WillOnce(Return(protection_bitrate_bps)); |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 732 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 733 | EXPECT_CALL(rtp_video_sender_, |
| 734 | OnBitrateUpdated(rate_with_headroom.bps(), _, |
| 735 | update.round_trip_time.ms(), _)); |
| 736 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 737 | .WillOnce(Return(rate_with_headroom.bps())); |
| 738 | const DataRate headroom_minus_protection = |
| 739 | rate_with_headroom - DataRate::bps(protection_bitrate_bps); |
| 740 | EXPECT_CALL(video_stream_encoder_, |
| 741 | OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate, |
| 742 | headroom_minus_protection, 0, _)); |
| 743 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 744 | ->OnBitrateUpdated(update); |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 745 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 746 | // Protection bitrate exceeds head room, link allocation should be |
| 747 | // capped to target bitrate. |
| 748 | EXPECT_CALL(rtp_video_sender_, GetProtectionBitrateBps()) |
| 749 | .WillOnce(Return(headroom.bps() + 1000)); |
| 750 | EXPECT_CALL(rtp_video_sender_, |
| 751 | OnBitrateUpdated(rate_with_headroom.bps(), _, |
| 752 | update.round_trip_time.ms(), _)); |
| 753 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 754 | .WillOnce(Return(rate_with_headroom.bps())); |
| 755 | EXPECT_CALL(video_stream_encoder_, |
| 756 | OnBitrateUpdated(qvga_max_bitrate, qvga_max_bitrate, |
| 757 | qvga_max_bitrate, 0, _)); |
| 758 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 759 | ->OnBitrateUpdated(update); |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 760 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 761 | // Set rates to zero on stop. |
| 762 | EXPECT_CALL(video_stream_encoder_, |
| 763 | OnBitrateUpdated(DataRate::Zero(), DataRate::Zero(), |
| 764 | DataRate::Zero(), 0, 0)); |
| 765 | vss_impl->Stop(); |
| 766 | }, |
| 767 | RTC_FROM_HERE); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 770 | TEST_F(VideoSendStreamImplTest, DisablesPaddingOnPausedEncoder) { |
| 771 | int padding_bitrate = 0; |
| 772 | std::unique_ptr<VideoSendStreamImpl> vss_impl; |
| 773 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 774 | test_queue_.SendTask( |
| 775 | [&] { |
| 776 | vss_impl = CreateVideoSendStreamImpl( |
| 777 | kDefaultInitialBitrateBps, kDefaultBitratePriority, |
| 778 | VideoEncoderConfig::ContentType::kRealtimeVideo); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 779 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 780 | // Capture padding bitrate for testing. |
| 781 | EXPECT_CALL(bitrate_allocator_, AddObserver(vss_impl.get(), _)) |
| 782 | .WillRepeatedly(Invoke([&](BitrateAllocatorObserver*, |
| 783 | MediaStreamAllocationConfig config) { |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 784 | padding_bitrate = config.pad_up_bitrate_bps; |
| 785 | })); |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 786 | // If observer is removed, no padding will be sent. |
| 787 | EXPECT_CALL(bitrate_allocator_, RemoveObserver(vss_impl.get())) |
| 788 | .WillRepeatedly(Invoke( |
| 789 | [&](BitrateAllocatorObserver*) { padding_bitrate = 0; })); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 790 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 791 | EXPECT_CALL(rtp_video_sender_, OnEncodedImage(_, _, _)) |
| 792 | .WillRepeatedly(Return(EncodedImageCallback::Result( |
| 793 | EncodedImageCallback::Result::OK))); |
| 794 | const bool kSuspend = false; |
| 795 | config_.suspend_below_min_bitrate = kSuspend; |
| 796 | config_.rtp.extensions.emplace_back( |
| 797 | RtpExtension::kTransportSequenceNumberUri, 1); |
| 798 | VideoStream qvga_stream; |
| 799 | qvga_stream.width = 320; |
| 800 | qvga_stream.height = 180; |
| 801 | qvga_stream.max_framerate = 30; |
| 802 | qvga_stream.min_bitrate_bps = 30000; |
| 803 | qvga_stream.target_bitrate_bps = 150000; |
| 804 | qvga_stream.max_bitrate_bps = 200000; |
| 805 | qvga_stream.max_qp = 56; |
| 806 | qvga_stream.bitrate_priority = 1; |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 807 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 808 | int min_transmit_bitrate_bps = 30000; |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 809 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 810 | config_.rtp.ssrcs.emplace_back(1); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 811 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 812 | vss_impl->Start(); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 813 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 814 | // Starts without padding. |
| 815 | EXPECT_EQ(0, padding_bitrate); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 816 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 817 | // Reconfigure e.g. due to a fake frame. |
| 818 | static_cast<VideoStreamEncoderInterface::EncoderSink*>(vss_impl.get()) |
| 819 | ->OnEncoderConfigurationChanged( |
| 820 | std::vector<VideoStream>{qvga_stream}, |
| 821 | VideoEncoderConfig::ContentType::kRealtimeVideo, |
| 822 | min_transmit_bitrate_bps); |
| 823 | // Still no padding because no actual frames were passed, only |
| 824 | // reconfiguration happened. |
| 825 | EXPECT_EQ(0, padding_bitrate); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 826 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 827 | // Unpause encoder. |
| 828 | const uint32_t kBitrateBps = 100000; |
| 829 | EXPECT_CALL(rtp_video_sender_, GetPayloadBitrateBps()) |
| 830 | .Times(1) |
| 831 | .WillOnce(Return(kBitrateBps)); |
| 832 | static_cast<BitrateAllocatorObserver*>(vss_impl.get()) |
| 833 | ->OnBitrateUpdated(CreateAllocation(kBitrateBps)); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 834 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame^] | 835 | // A frame is encoded. |
| 836 | EncodedImage encoded_image; |
| 837 | CodecSpecificInfo codec_specific; |
| 838 | static_cast<EncodedImageCallback*>(vss_impl.get()) |
| 839 | ->OnEncodedImage(encoded_image, &codec_specific, nullptr); |
| 840 | // Only after actual frame is encoded are we enabling the padding. |
| 841 | EXPECT_GT(padding_bitrate, 0); |
| 842 | }, |
| 843 | RTC_FROM_HERE); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 844 | |
| 845 | rtc::Event done; |
| 846 | test_queue_.PostDelayedTask( |
| 847 | [&] { |
| 848 | // No padding supposed to be sent for paused observer |
| 849 | EXPECT_EQ(0, padding_bitrate); |
| 850 | testing::Mock::VerifyAndClearExpectations(&bitrate_allocator_); |
| 851 | vss_impl->Stop(); |
| 852 | vss_impl.reset(); |
| 853 | done.Set(); |
| 854 | }, |
| 855 | 5000); |
| 856 | |
| 857 | // Pause the test suite so that the last delayed task executes. |
| 858 | ASSERT_TRUE(done.Wait(10000)); |
| 859 | } |
| 860 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 861 | } // namespace internal |
| 862 | } // namespace webrtc |